Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 1 | # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. |
| 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
| 4 | from __future__ import unicode_literals |
| 5 | import webnotes |
| 6 | |
| 7 | from webnotes.utils import flt |
| 8 | |
| 9 | |
| 10 | def repost(): |
| 11 | """ |
| 12 | Repost everything! |
| 13 | """ |
| 14 | webnotes.conn.auto_commit_on_many_writes = 1 |
| 15 | |
| 16 | for d in webnotes.conn.sql("select item_code, warehouse from tabBin"): |
| 17 | repost_stock(d[0], d[1]) |
| 18 | |
| 19 | webnotes.conn.auto_commit_on_many_writes = 0 |
| 20 | |
| 21 | def repost_stock(item_code, warehouse): |
| 22 | repost_actual_qty(item_code, warehouse) |
| 23 | |
| 24 | if item_code and warehouse: |
| 25 | update_bin(item_code, warehouse, { |
| 26 | "reserved_qty": get_reserved_qty(item_code, warehouse), |
| 27 | "indented_qty": get_indented_qty(item_code, warehouse), |
| 28 | "ordered_qty": get_ordered_qty(item_code, warehouse), |
| 29 | "planned_qty": get_planned_qty(item_code, warehouse) |
| 30 | }) |
| 31 | |
| 32 | def repost_actual_qty(item_code, warehouse): |
| 33 | from stock.stock_ledger import update_entries_after |
| 34 | update_entries_after({ "item_code": item_code, "warehouse": warehouse }) |
| 35 | |
| 36 | def get_reserved_qty(item_code, warehouse): |
| 37 | reserved_qty = webnotes.conn.sql(""" |
| 38 | select |
| 39 | sum((dnpi_qty / so_item_qty) * (so_item_qty - so_item_delivered_qty)) |
| 40 | from |
| 41 | ( |
| 42 | (select |
| 43 | qty as dnpi_qty, |
| 44 | ( |
| 45 | select qty from `tabSales Order Item` |
| 46 | where name = dnpi.parent_detail_docname |
| 47 | ) as so_item_qty, |
| 48 | ( |
| 49 | select ifnull(delivered_qty, 0) from `tabSales Order Item` |
| 50 | where name = dnpi.parent_detail_docname |
| 51 | ) as so_item_delivered_qty, |
| 52 | parent, name |
| 53 | from |
| 54 | ( |
| 55 | select qty, parent_detail_docname, parent, name |
| 56 | from `tabDelivery Note Packing Item` dnpi_in |
| 57 | where item_code = %s and warehouse = %s |
| 58 | and parenttype="Sales Order" |
| 59 | and item_code != parent_item |
| 60 | and exists (select * from `tabSales Order` so |
| 61 | where name = dnpi_in.parent and docstatus = 1 and status != 'Stopped') |
| 62 | ) dnpi) |
| 63 | union |
| 64 | (select qty as dnpi_qty, qty as so_item_qty, |
| 65 | ifnull(delivered_qty, 0) as so_item_delivered_qty, parent, name |
| 66 | from `tabSales Order Item` so_item |
| 67 | where item_code = %s and reserved_warehouse = %s |
| 68 | and exists(select * from `tabSales Order` so |
| 69 | where so.name = so_item.parent and so.docstatus = 1 |
| 70 | and so.status != 'Stopped')) |
| 71 | ) tab |
| 72 | where |
| 73 | so_item_qty >= so_item_delivered_qty |
| 74 | """, (item_code, warehouse, item_code, warehouse)) |
| 75 | |
| 76 | return flt(reserved_qty[0][0]) if reserved_qty else 0 |
| 77 | |
| 78 | def get_indented_qty(item_code, warehouse): |
Nabin Hait | 1a60fd8 | 2013-10-14 14:27:08 +0530 | [diff] [blame] | 79 | indented_qty = webnotes.conn.sql("""select sum(pr_item.qty - ifnull(pr_item.ordered_qty, 0)) |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 80 | from `tabMaterial Request Item` pr_item, `tabMaterial Request` pr |
| 81 | where pr_item.item_code=%s and pr_item.warehouse=%s |
Nabin Hait | 1a60fd8 | 2013-10-14 14:27:08 +0530 | [diff] [blame] | 82 | 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] | 83 | and pr.status!='Stopped' and pr.docstatus=1""", (item_code, warehouse)) |
| 84 | |
| 85 | return flt(indented_qty[0][0]) if indented_qty else 0 |
| 86 | |
| 87 | def get_ordered_qty(item_code, warehouse): |
| 88 | ordered_qty = webnotes.conn.sql(""" |
Nabin Hait | 1a60fd8 | 2013-10-14 14:27:08 +0530 | [diff] [blame] | 89 | 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] | 90 | from `tabPurchase Order Item` po_item, `tabPurchase Order` po |
| 91 | where po_item.item_code=%s and po_item.warehouse=%s |
Nabin Hait | 1a60fd8 | 2013-10-14 14:27:08 +0530 | [diff] [blame] | 92 | 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] | 93 | and po.status!='Stopped' and po.docstatus=1""", (item_code, warehouse)) |
| 94 | |
| 95 | return flt(ordered_qty[0][0]) if ordered_qty else 0 |
| 96 | |
| 97 | def get_planned_qty(item_code, warehouse): |
| 98 | planned_qty = webnotes.conn.sql(""" |
Nabin Hait | 1a60fd8 | 2013-10-14 14:27:08 +0530 | [diff] [blame] | 99 | select sum(ifnull(qty, 0) - ifnull(produced_qty, 0)) from `tabProduction Order` |
Nabin Hait | eea2b34 | 2013-10-11 18:31:33 +0530 | [diff] [blame] | 100 | where production_item = %s and fg_warehouse = %s and status != "Stopped" |
Nabin Hait | 1a60fd8 | 2013-10-14 14:27:08 +0530 | [diff] [blame] | 101 | 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] | 102 | |
| 103 | return flt(planned_qty[0][0]) if planned_qty else 0 |
| 104 | |
| 105 | |
| 106 | def update_bin(item_code, warehouse, qty_dict=None): |
| 107 | from stock.utils import get_bin |
| 108 | bin = get_bin(item_code, warehouse) |
| 109 | mismatch = False |
| 110 | for fld, val in qty_dict.items(): |
| 111 | if flt(bin.doc.fields.get(fld)) != flt(val): |
| 112 | bin.doc.fields[fld] = flt(val) |
| 113 | mismatch = True |
| 114 | |
| 115 | if mismatch: |
| 116 | bin.doc.projected_qty = flt(bin.doc.actual_qty) + flt(bin.doc.ordered_qty) + \ |
| 117 | flt(bin.doc.indented_qty) + flt(bin.doc.planned_qty) - flt(bin.doc.reserved_qty) |
| 118 | |
| 119 | bin.doc.save() |