blob: 48ff25f1231182a319fa3f478a4d5c28ea839471 [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
5import webnotes
6
7from webnotes.utils import flt
8
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 """
14 webnotes.conn.auto_commit_on_many_writes = 1
15
Nabin Haitca471f42013-11-20 13:14:12 +053016 if allow_negative_stock:
17 webnotes.conn.set_default("allow_negative_stock", 1)
18
Nabin Hait4ed7f682013-10-23 11:50:09 +053019 for d in webnotes.conn.sql("""select distinct item_code, warehouse from
20 (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:
26 webnotes.conn.set_default("allow_negative_stock",
27 webnotes.conn.get_value("Stock Settings", None, "allow_negative_stock"))
Nabin Haiteea2b342013-10-11 18:31:33 +053028 webnotes.conn.auto_commit_on_many_writes = 0
29
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):
42 from 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):
49 reserved_qty = webnotes.conn.sql("""
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 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
79 where item_code = %s and reserved_warehouse = %s
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
90def get_indented_qty(item_code, warehouse):
Nabin Hait1a60fd82013-10-14 14:27:08 +053091 indented_qty = webnotes.conn.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):
100 ordered_qty = webnotes.conn.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):
110 planned_qty = webnotes.conn.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):
119 from stock.utils import get_bin
120 bin = get_bin(item_code, warehouse)
121 mismatch = False
122 for fld, val in qty_dict.items():
123 if flt(bin.doc.fields.get(fld)) != flt(val):
124 bin.doc.fields[fld] = flt(val)
125 mismatch = True
126
127 if mismatch:
128 bin.doc.projected_qty = flt(bin.doc.actual_qty) + flt(bin.doc.ordered_qty) + \
129 flt(bin.doc.indented_qty) + flt(bin.doc.planned_qty) - flt(bin.doc.reserved_qty)
130
131 bin.doc.save()