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