blob: 14717c6902acbc8a14bc8d5cb0b4c3f5f9bb0491 [file] [log] [blame]
Rohit Waghchauree6143ab2023-03-13 14:51:43 +05301import frappe
2from frappe.query_builder.functions import CombineDatetime, Sum
3from frappe.utils import flt
4
5
6class DeprecatedSerialNoValuation:
Rohit Waghchaure5ddd55a2023-03-16 12:58:48 +05307 # Will be deperecated in v16
8
Rohit Waghchauree6143ab2023-03-13 14:51:43 +05309 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
28 def get_incoming_value_for_serial_nos(self, serial_nos):
29 # get rate from serial nos within same company
30 all_serial_nos = frappe.get_all(
31 "Serial No", fields=["purchase_rate", "name", "company"], filters={"name": ("in", serial_nos)}
32 )
33
34 incoming_values = 0.0
35 for d in all_serial_nos:
36 if d.company == self.sle.company:
37 self.serial_no_incoming_rate[d.name] = flt(d.purchase_rate)
38 incoming_values += flt(d.purchase_rate)
39
40 # Get rate for serial nos which has been transferred to other company
41 invalid_serial_nos = [d.name for d in all_serial_nos if d.company != self.sle.company]
42 for serial_no in invalid_serial_nos:
43 incoming_rate = frappe.db.sql(
44 """
45 select incoming_rate
46 from `tabStock Ledger Entry`
47 where
48 company = %s
49 and actual_qty > 0
50 and is_cancelled = 0
51 and (serial_no = %s
52 or serial_no like %s
53 or serial_no like %s
54 or serial_no like %s
55 )
56 order by posting_date desc
57 limit 1
58 """,
59 (self.sle.company, serial_no, serial_no + "\n%", "%\n" + serial_no, "%\n" + serial_no + "\n%"),
60 )
61
62 self.serial_no_incoming_rate[serial_no] = flt(incoming_rate[0][0]) if incoming_rate else 0
63 incoming_values += self.serial_no_incoming_rate[serial_no]
64
65 return incoming_values
66
67
68class DeprecatedBatchNoValuation:
69 def calculate_avg_rate_from_deprecarated_ledgers(self):
Rohit Waghchaure5bb31732023-03-21 10:54:41 +053070 entries = self.get_sle_for_batches()
71 for ledger in entries:
Rohit Waghchaure86da3062023-03-20 14:15:34 +053072 self.batch_avg_rate[ledger.batch_no] += flt(ledger.batch_value) / flt(ledger.batch_qty)
73 self.available_qty[ledger.batch_no] += flt(ledger.batch_qty)
Rohit Waghchauree6143ab2023-03-13 14:51:43 +053074
75 def get_sle_for_batches(self):
76 batch_nos = list(self.batch_nos.keys())
77 sle = frappe.qb.DocType("Stock Ledger Entry")
78
79 timestamp_condition = CombineDatetime(sle.posting_date, sle.posting_time) < CombineDatetime(
80 self.sle.posting_date, self.sle.posting_time
81 )
82 if self.sle.creation:
83 timestamp_condition |= (
84 CombineDatetime(sle.posting_date, sle.posting_time)
85 == CombineDatetime(self.sle.posting_date, self.sle.posting_time)
86 ) & (sle.creation < self.sle.creation)
87
88 return (
89 frappe.qb.from_(sle)
90 .select(
91 sle.batch_no,
92 Sum(sle.stock_value_difference).as_("batch_value"),
93 Sum(sle.actual_qty).as_("batch_qty"),
94 )
95 .where(
96 (sle.item_code == self.sle.item_code)
97 & (sle.name != self.sle.name)
98 & (sle.warehouse == self.sle.warehouse)
99 & (sle.batch_no.isin(batch_nos))
100 & (sle.is_cancelled == 0)
101 )
102 .where(timestamp_condition)
103 .groupby(sle.batch_no)
104 ).run(as_dict=True)