blob: 507974bfc7659c7118c6bc0aa7c41723f2be98d3 [file] [log] [blame]
Rushabh Mehtaad45e312013-11-20 12:59:58 +05301# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
Nabin Haiteea2b342013-10-11 18:31:33 +05302# License: GNU General Public License v3. See license.txt
3
4from __future__ import unicode_literals
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05305import frappe
Nabin Haiteea2b342013-10-11 18:31:33 +05306
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05307from frappe.utils import flt
Nabin Haiteea2b342013-10-11 18:31:33 +05308
9
Nabin Haitca471f42013-11-20 13:14:12 +053010def repost(allow_negative_stock=False):
Nabin Haiteea2b342013-10-11 18:31:33 +053011 """
12 Repost everything!
13 """
Anand Doshie9baaa62014-02-26 12:35:33 +053014 frappe.db.auto_commit_on_many_writes = 1
Nabin Haiteea2b342013-10-11 18:31:33 +053015
Nabin Haitca471f42013-11-20 13:14:12 +053016 if allow_negative_stock:
Anand Doshie9baaa62014-02-26 12:35:33 +053017 frappe.db.set_default("allow_negative_stock", 1)
Nabin Haitca471f42013-11-20 13:14:12 +053018
Anand Doshie9baaa62014-02-26 12:35:33 +053019 for d in frappe.db.sql("""select distinct item_code, warehouse from
Nabin Hait4ed7f682013-10-23 11:50:09 +053020 (select item_code, warehouse from tabBin
21 union
Nabin Haitc2caae52013-10-23 12:02:08 +053022 select item_code, warehouse from `tabStock Ledger Entry`) a"""):
Nabin Haitca471f42013-11-20 13:14:12 +053023 repost_stock(d[0], d[1], allow_negative_stock)
24
25 if allow_negative_stock:
Anand Doshie9baaa62014-02-26 12:35:33 +053026 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 Haiteea2b342013-10-11 18:31:33 +053029
30def 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
41def repost_actual_qty(item_code, warehouse):
Rushabh Mehta1f847992013-12-12 19:12:19 +053042 from erpnext.stock.stock_ledger import update_entries_after
Nabin Hait5048c982013-10-23 12:14:32 +053043 try:
44 update_entries_after({ "item_code": item_code, "warehouse": warehouse })
45 except:
46 pass
Nabin Haitca471f42013-11-20 13:14:12 +053047
Nabin Haiteea2b342013-10-11 18:31:33 +053048def get_reserved_qty(item_code, warehouse):
Anand Doshie9baaa62014-02-26 12:35:33 +053049 reserved_qty = frappe.db.sql("""
Nabin Haiteea2b342013-10-11 18:31:33 +053050 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 Haitd1fd1e22013-10-18 12:29:11 +053068 from `tabPacked Item` dnpi_in
Nabin Haiteea2b342013-10-11 18:31:33 +053069 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 Haita7f757a2014-02-10 17:54:04 +053079 where item_code = %s and warehouse = %s
Nabin Haiteea2b342013-10-11 18:31:33 +053080 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
90def get_indented_qty(item_code, warehouse):
Anand Doshie9baaa62014-02-26 12:35:33 +053091 indented_qty = frappe.db.sql("""select sum(pr_item.qty - ifnull(pr_item.ordered_qty, 0))
Nabin Haiteea2b342013-10-11 18:31:33 +053092 from `tabMaterial Request Item` pr_item, `tabMaterial Request` pr
93 where pr_item.item_code=%s and pr_item.warehouse=%s
Nabin Hait1a60fd82013-10-14 14:27:08 +053094 and pr_item.qty > ifnull(pr_item.ordered_qty, 0) and pr_item.parent=pr.name
Nabin Haiteea2b342013-10-11 18:31:33 +053095 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
99def get_ordered_qty(item_code, warehouse):
Anand Doshie9baaa62014-02-26 12:35:33 +0530100 ordered_qty = frappe.db.sql("""
Nabin Hait1a60fd82013-10-14 14:27:08 +0530101 select sum((po_item.qty - ifnull(po_item.received_qty, 0))*po_item.conversion_factor)
Nabin Haiteea2b342013-10-11 18:31:33 +0530102 from `tabPurchase Order Item` po_item, `tabPurchase Order` po
103 where po_item.item_code=%s and po_item.warehouse=%s
Nabin Hait1a60fd82013-10-14 14:27:08 +0530104 and po_item.qty > ifnull(po_item.received_qty, 0) and po_item.parent=po.name
Nabin Haiteea2b342013-10-11 18:31:33 +0530105 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
109def get_planned_qty(item_code, warehouse):
Anand Doshie9baaa62014-02-26 12:35:33 +0530110 planned_qty = frappe.db.sql("""
Nabin Hait1a60fd82013-10-14 14:27:08 +0530111 select sum(ifnull(qty, 0) - ifnull(produced_qty, 0)) from `tabProduction Order`
Nabin Haiteea2b342013-10-11 18:31:33 +0530112 where production_item = %s and fg_warehouse = %s and status != "Stopped"
Nabin Hait1a60fd82013-10-14 14:27:08 +0530113 and docstatus=1 and ifnull(qty, 0) > ifnull(produced_qty, 0)""", (item_code, warehouse))
Nabin Haiteea2b342013-10-11 18:31:33 +0530114
115 return flt(planned_qty[0][0]) if planned_qty else 0
116
117
118def update_bin(item_code, warehouse, qty_dict=None):
Rushabh Mehta1f847992013-12-12 19:12:19 +0530119 from erpnext.stock.utils import get_bin
Nabin Haiteea2b342013-10-11 18:31:33 +0530120 bin = get_bin(item_code, warehouse)
121 mismatch = False
122 for fld, val in qty_dict.items():
Anand Doshif78d1ae2014-03-28 13:55:00 +0530123 if flt(bin.get(fld)) != flt(val):
124 bin.set(fld, flt(val))
Nabin Haiteea2b342013-10-11 18:31:33 +0530125 mismatch = True
126
127 if mismatch:
Anand Doshif78d1ae2014-03-28 13:55:00 +0530128 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 Haiteea2b342013-10-11 18:31:33 +0530130
Anand Doshif78d1ae2014-03-28 13:55:00 +0530131 bin.save()