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