From f2718d106460c6b10714b6e389a3c34024f9da37 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Wed, 14 Sep 2011 11:40:01 +0530 Subject: [PATCH] delievery billing status patch --- patches/delivery_billing_status_patch.py | 56 ++++++++++++++---------- patches/patch.py | 5 ++- 2 files changed, 38 insertions(+), 23 deletions(-) diff --git a/patches/delivery_billing_status_patch.py b/patches/delivery_billing_status_patch.py index 2f9501398c..1fcd8bfdda 100644 --- a/patches/delivery_billing_status_patch.py +++ b/patches/delivery_billing_status_patch.py @@ -1,32 +1,43 @@ import webnotes sql = webnotes.conn.sql +test=1 + +# Update SO and DN Detail +#-------------------------- +def update_delivered_billed_qty(): + # update billed amt in item table in so and dn + sql(""" update `tabSales Order Detail` so + set billed_amt = (select sum(amount) from `tabRV Detail` where `so_detail`= so.name and docstatus=1 and parent not like 'old%%'), + delivered_qty = (select sum(qty) from `tabDelivery Note Detail` where `prevdoc_detail_docname`= so.name and docstatus=1 and parent not like 'old%%'), + modified = now() + where docstatus = 1 + """) + + sql(""" update `tabDelivery Note Detail` dn + set billed_amt = (select sum(amount) from `tabRV Detail` where `dn_detail`= dn.name and docstatus=1 and parent not like 'old%%'), + modified = now() + where docstatus = 1 + """) + # update SO #--------------- def update_percent(): - so = sql("select name from `tabSales Order` where docstatus = 1") - for d in so: - sql(""" - update - `tabSales Order` - set - per_delivered = (select sum(if(qty > ifnull(delivered_qty, 0), delivered_qty, qty))/sum(qty)*100 from `tabSales Order Detail` where parent='%s'), - per_billed = (select sum(if(qty > ifnull(billed_qty, 0), billed_qty, qty))/sum(qty)*100 from `tabSales Order Detail` where parent='%s') - where - name='%s'""" % (d[0], d[0], d[0])) - + # calculate % billed based on item table + sql(""" update `tabSales Order` so + set per_delivered = (select sum(if(qty > ifnull(delivered_qty, 0), delivered_qty, qty))/sum(qty)*100 from `tabSales Order Detail` where parent=so.name), + per_billed = (select sum(if(amount > ifnull(billed_amt, 0), billed_amt, amount))/sum(amount)*100 from `tabSales Order Detail` where parent = so.name), + modified = now() + where docstatus = 1 + """) + # update DN - # --------- - dn = sql("select name from `tabDelivery Note` where docstatus = 1") - for d in dn: - sql(""" - update - `tabDelivery Note` - set - per_billed = (select sum(if(qty > ifnull(billed_qty, 0), billed_qty, qty))/sum(qty)*100 from `tabDelivery Note Detail` where parent='%s') - where - name='%s'""" % (d[0], d[0])) - + # --------- + sql(""" update `tabDelivery Note` dn + set per_billed = (select sum(if(amount > ifnull(billed_amt, 0), billed_amt, amount))/sum(amount)*100 from `tabDelivery Note Detail` where parent = dn.name), + modified = now() + where docstatus=1 + """) # update delivery/billing status #------------------------------- @@ -39,5 +50,6 @@ def update_status(): if(per_billed >= 99.99, 'Fully Billed', 'Partly Billed'))""") def run_patch(): + update_delivered_billed_qty() update_percent() update_status() diff --git a/patches/patch.py b/patches/patch.py index fa3afca7cb..ca289f1672 100644 --- a/patches/patch.py +++ b/patches/patch.py @@ -1,7 +1,7 @@ # REMEMBER to update this # ======================== -last_patch = 364 +last_patch = 365 #------------------------------------------- @@ -337,3 +337,6 @@ def execute(patch_no): for d in mappers: if d[0] and d[1]: reload_doc(d[1].lower(), 'DocType Mapper', d[0]) + elif patch_no == 365: + from patches.delivery_billing_status_patch import run_patch + run_patch()