Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 1 | import frappe |
| 2 | from frappe.query_builder.functions import CombineDatetime, Sum |
| 3 | from frappe.utils import flt |
Rohit Waghchaure | c1132d1 | 2023-03-24 10:14:09 +0530 | [diff] [blame] | 4 | from frappe.utils.deprecations import deprecated |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 5 | |
| 6 | |
| 7 | class DeprecatedSerialNoValuation: |
Rohit Waghchaure | c1132d1 | 2023-03-24 10:14:09 +0530 | [diff] [blame] | 8 | @deprecated |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 9 | def calculate_stock_value_from_deprecarated_ledgers(self): |
| 10 | serial_nos = list( |
| 11 | filter(lambda x: x not in self.serial_no_incoming_rate and x, self.get_serial_nos()) |
| 12 | ) |
| 13 | |
| 14 | actual_qty = flt(self.sle.actual_qty) |
| 15 | |
| 16 | stock_value_change = 0 |
| 17 | if actual_qty < 0: |
| 18 | # In case of delivery/stock issue, get average purchase rate |
| 19 | # of serial nos of current entry |
| 20 | if not self.sle.is_cancelled: |
| 21 | outgoing_value = self.get_incoming_value_for_serial_nos(serial_nos) |
| 22 | stock_value_change = -1 * outgoing_value |
| 23 | else: |
| 24 | stock_value_change = actual_qty * self.sle.outgoing_rate |
| 25 | |
| 26 | self.stock_value_change += stock_value_change |
| 27 | |
Rohit Waghchaure | c1132d1 | 2023-03-24 10:14:09 +0530 | [diff] [blame] | 28 | @deprecated |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 29 | def get_incoming_value_for_serial_nos(self, serial_nos): |
| 30 | # get rate from serial nos within same company |
| 31 | all_serial_nos = frappe.get_all( |
| 32 | "Serial No", fields=["purchase_rate", "name", "company"], filters={"name": ("in", serial_nos)} |
| 33 | ) |
| 34 | |
| 35 | incoming_values = 0.0 |
| 36 | for d in all_serial_nos: |
| 37 | if d.company == self.sle.company: |
| 38 | self.serial_no_incoming_rate[d.name] = flt(d.purchase_rate) |
| 39 | incoming_values += flt(d.purchase_rate) |
| 40 | |
| 41 | # Get rate for serial nos which has been transferred to other company |
| 42 | invalid_serial_nos = [d.name for d in all_serial_nos if d.company != self.sle.company] |
| 43 | for serial_no in invalid_serial_nos: |
| 44 | incoming_rate = frappe.db.sql( |
| 45 | """ |
| 46 | select incoming_rate |
| 47 | from `tabStock Ledger Entry` |
| 48 | where |
| 49 | company = %s |
| 50 | and actual_qty > 0 |
| 51 | and is_cancelled = 0 |
| 52 | and (serial_no = %s |
| 53 | or serial_no like %s |
| 54 | or serial_no like %s |
| 55 | or serial_no like %s |
| 56 | ) |
| 57 | order by posting_date desc |
| 58 | limit 1 |
| 59 | """, |
| 60 | (self.sle.company, serial_no, serial_no + "\n%", "%\n" + serial_no, "%\n" + serial_no + "\n%"), |
| 61 | ) |
| 62 | |
| 63 | self.serial_no_incoming_rate[serial_no] = flt(incoming_rate[0][0]) if incoming_rate else 0 |
| 64 | incoming_values += self.serial_no_incoming_rate[serial_no] |
| 65 | |
| 66 | return incoming_values |
| 67 | |
| 68 | |
| 69 | class DeprecatedBatchNoValuation: |
Rohit Waghchaure | c1132d1 | 2023-03-24 10:14:09 +0530 | [diff] [blame] | 70 | @deprecated |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 71 | def calculate_avg_rate_from_deprecarated_ledgers(self): |
Rohit Waghchaure | 5bb3173 | 2023-03-21 10:54:41 +0530 | [diff] [blame] | 72 | entries = self.get_sle_for_batches() |
| 73 | for ledger in entries: |
Rohit Waghchaure | 86da306 | 2023-03-20 14:15:34 +0530 | [diff] [blame] | 74 | self.batch_avg_rate[ledger.batch_no] += flt(ledger.batch_value) / flt(ledger.batch_qty) |
| 75 | self.available_qty[ledger.batch_no] += flt(ledger.batch_qty) |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 76 | |
Rohit Waghchaure | c1132d1 | 2023-03-24 10:14:09 +0530 | [diff] [blame] | 77 | @deprecated |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 78 | def get_sle_for_batches(self): |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 79 | sle = frappe.qb.DocType("Stock Ledger Entry") |
| 80 | |
| 81 | timestamp_condition = CombineDatetime(sle.posting_date, sle.posting_time) < CombineDatetime( |
| 82 | self.sle.posting_date, self.sle.posting_time |
| 83 | ) |
| 84 | if self.sle.creation: |
| 85 | timestamp_condition |= ( |
| 86 | CombineDatetime(sle.posting_date, sle.posting_time) |
| 87 | == CombineDatetime(self.sle.posting_date, self.sle.posting_time) |
| 88 | ) & (sle.creation < self.sle.creation) |
| 89 | |
Rohit Waghchaure | c2d7461 | 2023-03-29 11:40:36 +0530 | [diff] [blame^] | 90 | batch_nos = self.batch_nos |
| 91 | if isinstance(self.batch_nos, dict): |
| 92 | batch_nos = list(self.batch_nos.keys()) |
| 93 | |
| 94 | query = ( |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 95 | frappe.qb.from_(sle) |
| 96 | .select( |
| 97 | sle.batch_no, |
| 98 | Sum(sle.stock_value_difference).as_("batch_value"), |
| 99 | Sum(sle.actual_qty).as_("batch_qty"), |
| 100 | ) |
| 101 | .where( |
| 102 | (sle.item_code == self.sle.item_code) |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 103 | & (sle.warehouse == self.sle.warehouse) |
| 104 | & (sle.batch_no.isin(batch_nos)) |
| 105 | & (sle.is_cancelled == 0) |
| 106 | ) |
| 107 | .where(timestamp_condition) |
| 108 | .groupby(sle.batch_no) |
Rohit Waghchaure | c2d7461 | 2023-03-29 11:40:36 +0530 | [diff] [blame^] | 109 | ) |
| 110 | |
| 111 | if self.sle.name: |
| 112 | query = query.where(sle.name != self.sle.name) |
| 113 | |
| 114 | return query.run(as_dict=True) |