Anand Doshi | 885e074 | 2015-03-03 14:55:30 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 4 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 5 | import frappe |
Ankush Menat | 77be982 | 2022-02-11 11:29:37 +0530 | [diff] [blame] | 6 | from frappe.utils import cstr, flt, now, nowdate, nowtime |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 7 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 8 | from erpnext.controllers.stock_controller import create_repost_item_valuation_entry |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 9 | |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 10 | |
Nabin Hait | b7e46c4 | 2015-10-12 16:46:29 +0530 | [diff] [blame] | 11 | def repost(only_actual=False, allow_negative_stock=False, allow_zero_rate=False, only_bin=False): |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 12 | """ |
| 13 | Repost everything! |
| 14 | """ |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 15 | frappe.db.auto_commit_on_many_writes = 1 |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 16 | |
Nabin Hait | ca471f4 | 2013-11-20 13:14:12 +0530 | [diff] [blame] | 17 | if allow_negative_stock: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 18 | existing_allow_negative_stock = frappe.db.get_value( |
| 19 | "Stock Settings", None, "allow_negative_stock" |
| 20 | ) |
Nabin Hait | 249bbbc | 2014-11-26 15:35:08 +0530 | [diff] [blame] | 21 | frappe.db.set_value("Stock Settings", None, "allow_negative_stock", 1) |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 22 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 23 | item_warehouses = frappe.db.sql( |
| 24 | """ |
Nabin Hait | 001edb4 | 2019-09-26 16:44:35 +0530 | [diff] [blame] | 25 | select distinct item_code, warehouse |
| 26 | from |
| 27 | (select item_code, warehouse from tabBin |
| 28 | union |
| 29 | select item_code, warehouse from `tabStock Ledger Entry`) a |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 30 | """ |
| 31 | ) |
Nabin Hait | 001edb4 | 2019-09-26 16:44:35 +0530 | [diff] [blame] | 32 | for d in item_warehouses: |
| 33 | try: |
| 34 | repost_stock(d[0], d[1], allow_zero_rate, only_actual, only_bin, allow_negative_stock) |
| 35 | frappe.db.commit() |
Ankush Menat | 694ae81 | 2021-09-01 14:40:56 +0530 | [diff] [blame] | 36 | except Exception: |
Nabin Hait | 001edb4 | 2019-09-26 16:44:35 +0530 | [diff] [blame] | 37 | frappe.db.rollback() |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 38 | |
Nabin Hait | ca471f4 | 2013-11-20 13:14:12 +0530 | [diff] [blame] | 39 | if allow_negative_stock: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 40 | frappe.db.set_value( |
| 41 | "Stock Settings", None, "allow_negative_stock", existing_allow_negative_stock |
| 42 | ) |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 43 | frappe.db.auto_commit_on_many_writes = 0 |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 44 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 45 | |
| 46 | def repost_stock( |
| 47 | item_code, |
| 48 | warehouse, |
| 49 | allow_zero_rate=False, |
| 50 | only_actual=False, |
| 51 | only_bin=False, |
| 52 | allow_negative_stock=False, |
| 53 | ): |
Nabin Hait | 001edb4 | 2019-09-26 16:44:35 +0530 | [diff] [blame] | 54 | |
Nabin Hait | b7e46c4 | 2015-10-12 16:46:29 +0530 | [diff] [blame] | 55 | if not only_bin: |
Nabin Hait | 001edb4 | 2019-09-26 16:44:35 +0530 | [diff] [blame] | 56 | repost_actual_qty(item_code, warehouse, allow_zero_rate, allow_negative_stock) |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 57 | |
Nabin Hait | 2348a5f | 2014-10-15 15:31:33 +0530 | [diff] [blame] | 58 | if item_code and warehouse and not only_actual: |
Nabin Hait | b7e46c4 | 2015-10-12 16:46:29 +0530 | [diff] [blame] | 59 | qty_dict = { |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 60 | "reserved_qty": get_reserved_qty(item_code, warehouse), |
| 61 | "indented_qty": get_indented_qty(item_code, warehouse), |
| 62 | "ordered_qty": get_ordered_qty(item_code, warehouse), |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 63 | "planned_qty": get_planned_qty(item_code, warehouse), |
Nabin Hait | b7e46c4 | 2015-10-12 16:46:29 +0530 | [diff] [blame] | 64 | } |
| 65 | if only_bin: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 66 | qty_dict.update({"actual_qty": get_balance_qty_from_sle(item_code, warehouse)}) |
Rushabh Mehta | c4d4c7f | 2015-10-14 17:37:28 +0530 | [diff] [blame] | 67 | |
Nabin Hait | b7e46c4 | 2015-10-12 16:46:29 +0530 | [diff] [blame] | 68 | update_bin_qty(item_code, warehouse, qty_dict) |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 69 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 70 | |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 71 | def repost_actual_qty(item_code, warehouse, allow_zero_rate=False, allow_negative_stock=False): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 72 | create_repost_item_valuation_entry( |
| 73 | { |
| 74 | "item_code": item_code, |
| 75 | "warehouse": warehouse, |
| 76 | "posting_date": "1900-01-01", |
| 77 | "posting_time": "00:01", |
| 78 | "allow_negative_stock": allow_negative_stock, |
| 79 | "allow_zero_rate": allow_zero_rate, |
| 80 | } |
| 81 | ) |
| 82 | |
Rushabh Mehta | c4d4c7f | 2015-10-14 17:37:28 +0530 | [diff] [blame] | 83 | |
Nabin Hait | b7e46c4 | 2015-10-12 16:46:29 +0530 | [diff] [blame] | 84 | def get_balance_qty_from_sle(item_code, warehouse): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 85 | balance_qty = frappe.db.sql( |
| 86 | """select qty_after_transaction from `tabStock Ledger Entry` |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 87 | where item_code=%s and warehouse=%s and is_cancelled=0 |
Aditya Hase | 0c16424 | 2019-01-07 22:07:13 +0530 | [diff] [blame] | 88 | order by posting_date desc, posting_time desc, creation desc |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 89 | limit 1""", |
| 90 | (item_code, warehouse), |
| 91 | ) |
Rushabh Mehta | c4d4c7f | 2015-10-14 17:37:28 +0530 | [diff] [blame] | 92 | |
Nabin Hait | b7e46c4 | 2015-10-12 16:46:29 +0530 | [diff] [blame] | 93 | return flt(balance_qty[0][0]) if balance_qty else 0.0 |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 94 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 95 | |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 96 | def get_reserved_qty(item_code, warehouse): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 97 | reserved_qty = frappe.db.sql( |
| 98 | """ |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 99 | select |
rohitwaghchaure | afa93c6 | 2017-03-29 17:29:20 +0530 | [diff] [blame] | 100 | sum(dnpi_qty * ((so_item_qty - so_item_delivered_qty) / so_item_qty)) |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 101 | from |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 102 | ( |
| 103 | (select |
| 104 | qty as dnpi_qty, |
| 105 | ( |
rohitwaghchaure | afa93c6 | 2017-03-29 17:29:20 +0530 | [diff] [blame] | 106 | select qty from `tabSales Order Item` |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 107 | where name = dnpi.parent_detail_docname |
Saurabh | 2e29206 | 2015-11-18 17:03:33 +0530 | [diff] [blame] | 108 | and (delivered_by_supplier is null or delivered_by_supplier = 0) |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 109 | ) as so_item_qty, |
| 110 | ( |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 111 | select delivered_qty from `tabSales Order Item` |
| 112 | where name = dnpi.parent_detail_docname |
| 113 | and delivered_by_supplier = 0 |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 114 | ) as so_item_delivered_qty, |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 115 | parent, name |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 116 | from |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 117 | ( |
| 118 | select qty, parent_detail_docname, parent, name |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 119 | from `tabPacked Item` dnpi_in |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 120 | where item_code = %s and warehouse = %s |
Conor | 74a782d | 2022-06-17 06:31:27 -0500 | [diff] [blame] | 121 | and parenttype='Sales Order' |
Nabin Hait | fc2dd44 | 2014-10-17 13:05:24 +0530 | [diff] [blame] | 122 | and item_code != parent_item |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 123 | and exists (select * from `tabSales Order` so |
patilsangram | a812d67 | 2016-02-23 19:04:29 +0530 | [diff] [blame] | 124 | where name = dnpi_in.parent and docstatus = 1 and status != 'Closed') |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 125 | ) dnpi) |
| 126 | union |
rohitwaghchaure | afa93c6 | 2017-03-29 17:29:20 +0530 | [diff] [blame] | 127 | (select stock_qty as dnpi_qty, qty as so_item_qty, |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 128 | delivered_qty as so_item_delivered_qty, parent, name |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 129 | from `tabSales Order Item` so_item |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 130 | where item_code = %s and warehouse = %s |
Saurabh | 2e29206 | 2015-11-18 17:03:33 +0530 | [diff] [blame] | 131 | and (so_item.delivered_by_supplier is null or so_item.delivered_by_supplier = 0) |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 132 | and exists(select * from `tabSales Order` so |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 133 | where so.name = so_item.parent and so.docstatus = 1 |
patilsangram | a812d67 | 2016-02-23 19:04:29 +0530 | [diff] [blame] | 134 | and so.status != 'Closed')) |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 135 | ) tab |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 136 | where |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 137 | so_item_qty >= so_item_delivered_qty |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 138 | """, |
| 139 | (item_code, warehouse, item_code, warehouse), |
| 140 | ) |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 141 | |
| 142 | return flt(reserved_qty[0][0]) if reserved_qty else 0 |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 143 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 144 | |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 145 | def get_indented_qty(item_code, warehouse): |
Nabin Hait | 7eaa7f2 | 2020-04-17 10:52:28 +0530 | [diff] [blame] | 146 | # Ordered Qty is always maintained in stock UOM |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 147 | inward_qty = frappe.db.sql( |
| 148 | """ |
Nabin Hait | 03af029 | 2020-04-16 20:11:32 +0530 | [diff] [blame] | 149 | select sum(mr_item.stock_qty - mr_item.ordered_qty) |
Nabin Hait | 4acd431 | 2014-11-04 15:32:31 +0530 | [diff] [blame] | 150 | from `tabMaterial Request Item` mr_item, `tabMaterial Request` mr |
| 151 | where mr_item.item_code=%s and mr_item.warehouse=%s |
Nabin Hait | 7eaa7f2 | 2020-04-17 10:52:28 +0530 | [diff] [blame] | 152 | and mr.material_request_type in ('Purchase', 'Manufacture', 'Customer Provided', 'Material Transfer') |
Nabin Hait | 03af029 | 2020-04-16 20:11:32 +0530 | [diff] [blame] | 153 | and mr_item.stock_qty > mr_item.ordered_qty and mr_item.parent=mr.name |
| 154 | and mr.status!='Stopped' and mr.docstatus=1 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 155 | """, |
| 156 | (item_code, warehouse), |
| 157 | ) |
Nabin Hait | 7eaa7f2 | 2020-04-17 10:52:28 +0530 | [diff] [blame] | 158 | inward_qty = flt(inward_qty[0][0]) if inward_qty else 0 |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 159 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 160 | outward_qty = frappe.db.sql( |
| 161 | """ |
Nabin Hait | 03af029 | 2020-04-16 20:11:32 +0530 | [diff] [blame] | 162 | select sum(mr_item.stock_qty - mr_item.ordered_qty) |
| 163 | from `tabMaterial Request Item` mr_item, `tabMaterial Request` mr |
| 164 | where mr_item.item_code=%s and mr_item.warehouse=%s |
Nabin Hait | 7eaa7f2 | 2020-04-17 10:52:28 +0530 | [diff] [blame] | 165 | and mr.material_request_type = 'Material Issue' |
Nabin Hait | 03af029 | 2020-04-16 20:11:32 +0530 | [diff] [blame] | 166 | and mr_item.stock_qty > mr_item.ordered_qty and mr_item.parent=mr.name |
| 167 | and mr.status!='Stopped' and mr.docstatus=1 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 168 | """, |
| 169 | (item_code, warehouse), |
| 170 | ) |
Nabin Hait | 7eaa7f2 | 2020-04-17 10:52:28 +0530 | [diff] [blame] | 171 | outward_qty = flt(outward_qty[0][0]) if outward_qty else 0 |
marination | 815c36a | 2020-03-26 14:49:28 +0530 | [diff] [blame] | 172 | |
Nabin Hait | 7eaa7f2 | 2020-04-17 10:52:28 +0530 | [diff] [blame] | 173 | requested_qty = inward_qty - outward_qty |
Nabin Hait | 03af029 | 2020-04-16 20:11:32 +0530 | [diff] [blame] | 174 | |
| 175 | return requested_qty |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 176 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 177 | |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 178 | def get_ordered_qty(item_code, warehouse): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 179 | ordered_qty = frappe.db.sql( |
| 180 | """ |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 181 | select sum((po_item.qty - po_item.received_qty)*po_item.conversion_factor) |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 182 | from `tabPurchase Order Item` po_item, `tabPurchase Order` po |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 183 | where po_item.item_code=%s and po_item.warehouse=%s |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 184 | and po_item.qty > po_item.received_qty and po_item.parent=po.name |
patilsangram | bf2b511 | 2016-02-22 16:24:23 +0530 | [diff] [blame] | 185 | and po.status not in ('Closed', 'Delivered') and po.docstatus=1 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 186 | and po_item.delivered_by_supplier = 0""", |
| 187 | (item_code, warehouse), |
| 188 | ) |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 189 | |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 190 | return flt(ordered_qty[0][0]) if ordered_qty else 0 |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 191 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 192 | |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 193 | def get_planned_qty(item_code, warehouse): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 194 | planned_qty = frappe.db.sql( |
| 195 | """ |
Zarrar | 13ddc7e | 2018-03-20 12:38:43 +0530 | [diff] [blame] | 196 | select sum(qty - produced_qty) from `tabWork Order` |
Conor | 74a782d | 2022-06-17 06:31:27 -0500 | [diff] [blame] | 197 | where production_item = %s and fg_warehouse = %s and status not in ('Stopped', 'Completed', 'Closed') |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 198 | and docstatus=1 and qty > produced_qty""", |
| 199 | (item_code, warehouse), |
| 200 | ) |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 201 | |
| 202 | return flt(planned_qty[0][0]) if planned_qty else 0 |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 203 | |
| 204 | |
Nabin Hait | 7f3f2a0 | 2014-09-01 18:16:05 +0530 | [diff] [blame] | 205 | def update_bin_qty(item_code, warehouse, qty_dict=None): |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 206 | from erpnext.stock.utils import get_bin |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 207 | |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 208 | bin = get_bin(item_code, warehouse) |
| 209 | mismatch = False |
marination | 815c36a | 2020-03-26 14:49:28 +0530 | [diff] [blame] | 210 | for field, value in qty_dict.items(): |
| 211 | if flt(bin.get(field)) != flt(value): |
| 212 | bin.set(field, flt(value)) |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 213 | mismatch = True |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 214 | |
Ankush Menat | 77be982 | 2022-02-11 11:29:37 +0530 | [diff] [blame] | 215 | bin.modified = now() |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 216 | if mismatch: |
Himanshu Mishra | f56aac6 | 2018-08-08 18:32:03 +0530 | [diff] [blame] | 217 | bin.set_projected_qty() |
| 218 | bin.db_update() |
| 219 | bin.clear_cache() |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 220 | |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 221 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 222 | def set_stock_balance_as_per_serial_no( |
| 223 | item_code=None, posting_date=None, posting_time=None, fiscal_year=None |
| 224 | ): |
| 225 | if not posting_date: |
| 226 | posting_date = nowdate() |
| 227 | if not posting_time: |
| 228 | posting_time = nowtime() |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 229 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 230 | condition = " and item.name='%s'" % item_code.replace("'", "'") if item_code else "" |
| 231 | |
| 232 | bin = frappe.db.sql( |
| 233 | """select bin.item_code, bin.warehouse, bin.actual_qty, item.stock_uom |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 234 | from `tabBin` bin, tabItem item |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 235 | where bin.item_code = item.name and item.has_serial_no = 1 %s""" |
| 236 | % condition |
| 237 | ) |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 238 | |
| 239 | for d in bin: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 240 | serial_nos = frappe.db.sql( |
| 241 | """select count(name) from `tabSerial No` |
| 242 | where item_code=%s and warehouse=%s and docstatus < 2""", |
| 243 | (d[0], d[1]), |
| 244 | ) |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 245 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 246 | sle = frappe.db.sql( |
| 247 | """select valuation_rate, company from `tabStock Ledger Entry` |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 248 | where item_code = %s and warehouse = %s and is_cancelled = 0 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 249 | order by posting_date desc limit 1""", |
| 250 | (d[0], d[1]), |
| 251 | ) |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 252 | |
| 253 | sle_dict = { |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 254 | "doctype": "Stock Ledger Entry", |
| 255 | "item_code": d[0], |
| 256 | "warehouse": d[1], |
| 257 | "transaction_date": nowdate(), |
| 258 | "posting_date": posting_date, |
| 259 | "posting_time": posting_time, |
| 260 | "voucher_type": "Stock Reconciliation (Manual)", |
| 261 | "voucher_no": "", |
| 262 | "voucher_detail_no": "", |
| 263 | "actual_qty": flt(serial_nos[0][0]) - flt(d[2]), |
| 264 | "stock_uom": d[3], |
| 265 | "incoming_rate": sle and flt(serial_nos[0][0]) > flt(d[2]) and flt(sle[0][0]) or 0, |
| 266 | "company": sle and cstr(sle[0][1]) or 0, |
| 267 | "batch_no": "", |
| 268 | "serial_no": "", |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | sle_doc = frappe.get_doc(sle_dict) |
Anand Doshi | 6dfd430 | 2015-02-10 14:41:27 +0530 | [diff] [blame] | 272 | sle_doc.flags.ignore_validate = True |
| 273 | sle_doc.flags.ignore_links = True |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 274 | sle_doc.insert() |
| 275 | |
| 276 | args = sle_dict.copy() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 277 | args.update({"sle_id": sle_doc.name}) |
Nabin Hait | 6298536 | 2014-04-04 12:05:16 +0530 | [diff] [blame] | 278 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 279 | create_repost_item_valuation_entry( |
| 280 | { |
| 281 | "item_code": d[0], |
| 282 | "warehouse": d[1], |
| 283 | "posting_date": posting_date, |
| 284 | "posting_time": posting_time, |
| 285 | } |
| 286 | ) |
| 287 | |
nabinhait | 5c38488 | 2014-07-14 11:43:00 +0530 | [diff] [blame] | 288 | |
nabinhait | 7700c62 | 2014-07-14 14:21:21 +0530 | [diff] [blame] | 289 | def reset_serial_no_status_and_warehouse(serial_nos=None): |
nabinhait | 5c38488 | 2014-07-14 11:43:00 +0530 | [diff] [blame] | 290 | if not serial_nos: |
Nabin Hait | c865f22 | 2015-09-21 09:18:43 +0530 | [diff] [blame] | 291 | serial_nos = frappe.db.sql_list("""select name from `tabSerial No` where docstatus = 0""") |
nabinhait | 5c38488 | 2014-07-14 11:43:00 +0530 | [diff] [blame] | 292 | for serial_no in serial_nos: |
| 293 | try: |
| 294 | sr = frappe.get_doc("Serial No", serial_no) |
nabinhait | b0a8d00 | 2014-07-14 11:56:03 +0530 | [diff] [blame] | 295 | last_sle = sr.get_last_sle() |
| 296 | if flt(last_sle.actual_qty) > 0: |
| 297 | sr.warehouse = last_sle.warehouse |
Nabin Hait | 7f3f2a0 | 2014-09-01 18:16:05 +0530 | [diff] [blame] | 298 | |
nabinhait | 5c38488 | 2014-07-14 11:43:00 +0530 | [diff] [blame] | 299 | sr.via_stock_ledger = True |
| 300 | sr.save() |
Ankush Menat | 694ae81 | 2021-09-01 14:40:56 +0530 | [diff] [blame] | 301 | except Exception: |
nabinhait | 5c38488 | 2014-07-14 11:43:00 +0530 | [diff] [blame] | 302 | pass |