Rushabh Mehta | ad45e31 | 2013-11-20 12:59:58 +0530 | [diff] [blame] | 1 | # Copyright (c) 2013, Web Notes 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 | |
| 4 | from __future__ import 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 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 7 | from frappe.utils import flt |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 8 | |
| 9 | |
Nabin Hait | ca471f4 | 2013-11-20 13:14:12 +0530 | [diff] [blame] | 10 | def repost(allow_negative_stock=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 | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 15 | |
Nabin Hait | ca471f4 | 2013-11-20 13:14:12 +0530 | [diff] [blame] | 16 | if allow_negative_stock: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 17 | frappe.db.set_default("allow_negative_stock", 1) |
Nabin Hait | ca471f4 | 2013-11-20 13:14:12 +0530 | [diff] [blame] | 18 | |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 19 | for d in frappe.db.sql("""select distinct item_code, warehouse from |
Nabin Hait | 4ed7f68 | 2013-10-23 11:50:09 +0530 | [diff] [blame] | 20 | (select item_code, warehouse from tabBin |
| 21 | union |
Nabin Hait | c2caae5 | 2013-10-23 12:02:08 +0530 | [diff] [blame] | 22 | select item_code, warehouse from `tabStock Ledger Entry`) a"""): |
Nabin Hait | ca471f4 | 2013-11-20 13:14:12 +0530 | [diff] [blame] | 23 | repost_stock(d[0], d[1], allow_negative_stock) |
| 24 | |
| 25 | if allow_negative_stock: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 26 | frappe.db.set_default("allow_negative_stock", |
| 27 | frappe.db.get_value("Stock Settings", None, "allow_negative_stock")) |
| 28 | frappe.db.auto_commit_on_many_writes = 0 |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 29 | |
| 30 | def repost_stock(item_code, warehouse): |
| 31 | repost_actual_qty(item_code, warehouse) |
| 32 | |
| 33 | if item_code and warehouse: |
| 34 | update_bin(item_code, warehouse, { |
| 35 | "reserved_qty": get_reserved_qty(item_code, warehouse), |
| 36 | "indented_qty": get_indented_qty(item_code, warehouse), |
| 37 | "ordered_qty": get_ordered_qty(item_code, warehouse), |
| 38 | "planned_qty": get_planned_qty(item_code, warehouse) |
| 39 | }) |
| 40 | |
| 41 | def repost_actual_qty(item_code, warehouse): |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 42 | from erpnext.stock.stock_ledger import update_entries_after |
Nabin Hait | 5048c98 | 2013-10-23 12:14:32 +0530 | [diff] [blame] | 43 | try: |
| 44 | update_entries_after({ "item_code": item_code, "warehouse": warehouse }) |
| 45 | except: |
| 46 | pass |
Nabin Hait | ca471f4 | 2013-11-20 13:14:12 +0530 | [diff] [blame] | 47 | |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 48 | def get_reserved_qty(item_code, warehouse): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 49 | reserved_qty = frappe.db.sql(""" |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 50 | select |
| 51 | sum((dnpi_qty / so_item_qty) * (so_item_qty - so_item_delivered_qty)) |
| 52 | from |
| 53 | ( |
| 54 | (select |
| 55 | qty as dnpi_qty, |
| 56 | ( |
| 57 | select qty from `tabSales Order Item` |
| 58 | where name = dnpi.parent_detail_docname |
| 59 | ) as so_item_qty, |
| 60 | ( |
| 61 | select ifnull(delivered_qty, 0) from `tabSales Order Item` |
| 62 | where name = dnpi.parent_detail_docname |
| 63 | ) as so_item_delivered_qty, |
| 64 | parent, name |
| 65 | from |
| 66 | ( |
| 67 | select qty, parent_detail_docname, parent, name |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 68 | from `tabPacked Item` dnpi_in |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 69 | where item_code = %s and warehouse = %s |
| 70 | and parenttype="Sales Order" |
| 71 | and item_code != parent_item |
| 72 | and exists (select * from `tabSales Order` so |
| 73 | where name = dnpi_in.parent and docstatus = 1 and status != 'Stopped') |
| 74 | ) dnpi) |
| 75 | union |
| 76 | (select qty as dnpi_qty, qty as so_item_qty, |
| 77 | ifnull(delivered_qty, 0) as so_item_delivered_qty, parent, name |
| 78 | from `tabSales Order Item` so_item |
Nabin Hait | a7f757a | 2014-02-10 17:54:04 +0530 | [diff] [blame] | 79 | where item_code = %s and warehouse = %s |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 80 | and exists(select * from `tabSales Order` so |
| 81 | where so.name = so_item.parent and so.docstatus = 1 |
| 82 | and so.status != 'Stopped')) |
| 83 | ) tab |
| 84 | where |
| 85 | so_item_qty >= so_item_delivered_qty |
| 86 | """, (item_code, warehouse, item_code, warehouse)) |
| 87 | |
| 88 | return flt(reserved_qty[0][0]) if reserved_qty else 0 |
| 89 | |
| 90 | def get_indented_qty(item_code, warehouse): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 91 | indented_qty = frappe.db.sql("""select sum(pr_item.qty - ifnull(pr_item.ordered_qty, 0)) |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 92 | from `tabMaterial Request Item` pr_item, `tabMaterial Request` pr |
| 93 | where pr_item.item_code=%s and pr_item.warehouse=%s |
Nabin Hait | 1a60fd8 | 2013-10-14 14:27:08 +0530 | [diff] [blame] | 94 | and pr_item.qty > ifnull(pr_item.ordered_qty, 0) and pr_item.parent=pr.name |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 95 | and pr.status!='Stopped' and pr.docstatus=1""", (item_code, warehouse)) |
| 96 | |
| 97 | return flt(indented_qty[0][0]) if indented_qty else 0 |
| 98 | |
| 99 | def get_ordered_qty(item_code, warehouse): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 100 | ordered_qty = frappe.db.sql(""" |
Nabin Hait | 1a60fd8 | 2013-10-14 14:27:08 +0530 | [diff] [blame] | 101 | select sum((po_item.qty - ifnull(po_item.received_qty, 0))*po_item.conversion_factor) |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 102 | from `tabPurchase Order Item` po_item, `tabPurchase Order` po |
| 103 | where po_item.item_code=%s and po_item.warehouse=%s |
Nabin Hait | 1a60fd8 | 2013-10-14 14:27:08 +0530 | [diff] [blame] | 104 | and po_item.qty > ifnull(po_item.received_qty, 0) and po_item.parent=po.name |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 105 | and po.status!='Stopped' and po.docstatus=1""", (item_code, warehouse)) |
| 106 | |
| 107 | return flt(ordered_qty[0][0]) if ordered_qty else 0 |
| 108 | |
| 109 | def get_planned_qty(item_code, warehouse): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 110 | planned_qty = frappe.db.sql(""" |
Nabin Hait | 1a60fd8 | 2013-10-14 14:27:08 +0530 | [diff] [blame] | 111 | select sum(ifnull(qty, 0) - ifnull(produced_qty, 0)) from `tabProduction Order` |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 112 | where production_item = %s and fg_warehouse = %s and status != "Stopped" |
Nabin Hait | 1a60fd8 | 2013-10-14 14:27:08 +0530 | [diff] [blame] | 113 | and docstatus=1 and ifnull(qty, 0) > ifnull(produced_qty, 0)""", (item_code, warehouse)) |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 114 | |
| 115 | return flt(planned_qty[0][0]) if planned_qty else 0 |
| 116 | |
| 117 | |
| 118 | def update_bin(item_code, warehouse, qty_dict=None): |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 119 | from erpnext.stock.utils import get_bin |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 120 | bin = get_bin(item_code, warehouse) |
| 121 | mismatch = False |
| 122 | for fld, val in qty_dict.items(): |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 123 | if flt(bin.get(fld)) != flt(val): |
| 124 | bin.set(fld, flt(val)) |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 125 | mismatch = True |
| 126 | |
| 127 | if mismatch: |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 128 | bin.projected_qty = flt(bin.actual_qty) + flt(bin.ordered_qty) + \ |
| 129 | flt(bin.indented_qty) + flt(bin.planned_qty) - flt(bin.reserved_qty) |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 130 | |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 131 | bin.save() |