blob: b7c5d57d960738518570c2e32d2a9fa132771433 [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
Rohit Waghchaurec1132d12023-03-24 10:14:09 +05304from frappe.utils.deprecations import deprecated
Rohit Waghchauree6143ab2023-03-13 14:51:43 +05305
6
7class DeprecatedSerialNoValuation:
Rohit Waghchaurec1132d12023-03-24 10:14:09 +05308 @deprecated
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
Rohit Waghchaurec1132d12023-03-24 10:14:09 +053028 @deprecated
Rohit Waghchauree6143ab2023-03-13 14:51:43 +053029 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
69class DeprecatedBatchNoValuation:
Rohit Waghchaurec1132d12023-03-24 10:14:09 +053070 @deprecated
Rohit Waghchauree6143ab2023-03-13 14:51:43 +053071 def calculate_avg_rate_from_deprecarated_ledgers(self):
Rohit Waghchaure5bb31732023-03-21 10:54:41 +053072 entries = self.get_sle_for_batches()
73 for ledger in entries:
Rohit Waghchaure86da3062023-03-20 14:15:34 +053074 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 Waghchauree6143ab2023-03-13 14:51:43 +053076
Rohit Waghchaurec1132d12023-03-24 10:14:09 +053077 @deprecated
Rohit Waghchauree6143ab2023-03-13 14:51:43 +053078 def get_sle_for_batches(self):
Rohit Waghchauree6143ab2023-03-13 14:51:43 +053079 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 Waghchaurec2d74612023-03-29 11:40:36 +053090 batch_nos = self.batch_nos
91 if isinstance(self.batch_nos, dict):
92 batch_nos = list(self.batch_nos.keys())
93
94 query = (
Rohit Waghchauree6143ab2023-03-13 14:51:43 +053095 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 Waghchauree6143ab2023-03-13 14:51:43 +0530103 & (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 Waghchaurec2d74612023-03-29 11:40:36 +0530109 )
110
111 if self.sle.name:
112 query = query.where(sle.name != self.sle.name)
113
114 return query.run(as_dict=True)