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