2015-03-03 09:25:30 +00:00
|
|
|
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
2013-08-05 09:29:54 +00:00
|
|
|
# License: GNU General Public License v3. See license.txt
|
2013-06-03 11:15:38 +00:00
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
2014-02-14 10:17:51 +00:00
|
|
|
import frappe
|
2015-05-18 05:48:55 +00:00
|
|
|
from frappe.utils import flt, comma_or
|
2014-04-14 13:50:45 +00:00
|
|
|
from frappe import msgprint, _, throw
|
2014-04-21 09:36:56 +00:00
|
|
|
from frappe.model.document import Document
|
2013-06-03 11:15:38 +00:00
|
|
|
|
2015-05-18 05:48:55 +00:00
|
|
|
def validate_status(status, options):
|
|
|
|
if status not in options:
|
|
|
|
frappe.throw(_("Status must be one of {0}").format(comma_or(options)))
|
|
|
|
|
2013-10-03 11:56:33 +00:00
|
|
|
status_map = {
|
|
|
|
"Lead": [
|
|
|
|
["Converted", "has_customer"],
|
|
|
|
["Opportunity", "has_opportunity"],
|
|
|
|
],
|
|
|
|
"Opportunity": [
|
2014-03-28 08:25:00 +00:00
|
|
|
["Lost", "eval:self.status=='Lost'"],
|
2013-10-03 11:56:33 +00:00
|
|
|
["Quotation", "has_quotation"],
|
2015-04-13 10:51:58 +00:00
|
|
|
["Converted", "has_ordered_quotation"]
|
2013-10-03 11:56:33 +00:00
|
|
|
],
|
|
|
|
"Quotation": [
|
2015-04-13 11:26:03 +00:00
|
|
|
["Draft", None],
|
2014-03-28 08:25:00 +00:00
|
|
|
["Submitted", "eval:self.docstatus==1"],
|
|
|
|
["Lost", "eval:self.status=='Lost'"],
|
2013-10-03 11:56:33 +00:00
|
|
|
["Ordered", "has_sales_order"],
|
2014-03-28 08:25:00 +00:00
|
|
|
["Cancelled", "eval:self.docstatus==2"],
|
2013-10-03 11:56:33 +00:00
|
|
|
],
|
|
|
|
"Sales Order": [
|
|
|
|
["Draft", None],
|
2015-10-02 07:12:48 +00:00
|
|
|
["To Deliver and Bill", "eval:self.per_delivered < 100 and self.per_billed < 100 and self.docstatus == 1"],
|
|
|
|
["To Bill", "eval:self.per_delivered == 100 and self.per_billed < 100 and self.docstatus == 1"],
|
|
|
|
["To Deliver", "eval:self.per_delivered < 100 and self.per_billed == 100 and self.docstatus == 1"],
|
|
|
|
["Completed", "eval:self.per_delivered == 100 and self.per_billed == 100 and self.docstatus == 1"],
|
|
|
|
["Stopped", "eval:self.status=='Stopped'"],
|
|
|
|
["Cancelled", "eval:self.docstatus==2"],
|
|
|
|
],
|
|
|
|
"Purchase Order": [
|
|
|
|
["Draft", None],
|
|
|
|
["To Receive and Bill", "eval:self.per_received < 100 and self.per_billed < 100 and self.docstatus == 1"],
|
|
|
|
["To Bill", "eval:self.per_received == 100 and self.per_billed < 100 and self.docstatus == 1"],
|
|
|
|
["To Receive", "eval:self.per_received < 100 and self.per_billed == 100 and self.docstatus == 1"],
|
|
|
|
["Completed", "eval:self.per_received == 100 and self.per_billed == 100 and self.docstatus == 1"],
|
2014-03-28 08:25:00 +00:00
|
|
|
["Stopped", "eval:self.status=='Stopped'"],
|
|
|
|
["Cancelled", "eval:self.docstatus==2"],
|
2013-10-03 11:56:33 +00:00
|
|
|
],
|
2015-05-18 05:48:55 +00:00
|
|
|
"Delivery Note": [
|
|
|
|
["Draft", None],
|
|
|
|
["Submitted", "eval:self.docstatus==1"],
|
|
|
|
["Cancelled", "eval:self.docstatus==2"],
|
|
|
|
],
|
|
|
|
"Purchase Receipt": [
|
|
|
|
["Draft", None],
|
|
|
|
["Submitted", "eval:self.docstatus==1"],
|
|
|
|
["Cancelled", "eval:self.docstatus==2"],
|
|
|
|
]
|
2013-10-03 11:56:33 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 09:36:56 +00:00
|
|
|
class StatusUpdater(Document):
|
2013-06-03 11:15:38 +00:00
|
|
|
"""
|
|
|
|
Updates the status of the calling records
|
|
|
|
Delivery Note: Update Delivered Qty, Update Percent and Validate over delivery
|
|
|
|
Sales Invoice: Update Billed Amt, Update Percent and Validate over billing
|
|
|
|
Installation Note: Update Installed Qty, Update Percent Qty and Validate over installation
|
|
|
|
"""
|
|
|
|
|
|
|
|
def update_prevdoc_status(self):
|
|
|
|
self.update_qty()
|
|
|
|
self.validate_qty()
|
2014-04-09 13:50:01 +00:00
|
|
|
|
2013-10-03 11:56:33 +00:00
|
|
|
def set_status(self, update=False):
|
2014-08-19 10:50:04 +00:00
|
|
|
if self.is_new():
|
2013-10-03 11:56:33 +00:00
|
|
|
return
|
2014-04-09 13:50:01 +00:00
|
|
|
|
2014-03-28 08:25:00 +00:00
|
|
|
if self.doctype in status_map:
|
2014-08-21 11:04:38 +00:00
|
|
|
_status = self.status
|
2014-03-28 08:25:00 +00:00
|
|
|
sl = status_map[self.doctype][:]
|
2013-10-03 12:42:36 +00:00
|
|
|
sl.reverse()
|
|
|
|
for s in sl:
|
2013-10-03 11:56:33 +00:00
|
|
|
if not s[1]:
|
2014-03-28 08:25:00 +00:00
|
|
|
self.status = s[0]
|
2013-10-03 11:56:33 +00:00
|
|
|
break
|
2013-10-03 12:42:36 +00:00
|
|
|
elif s[1].startswith("eval:"):
|
2013-10-03 11:56:33 +00:00
|
|
|
if eval(s[1][5:]):
|
2014-03-28 08:25:00 +00:00
|
|
|
self.status = s[0]
|
2013-10-03 11:56:33 +00:00
|
|
|
break
|
|
|
|
elif getattr(self, s[1])():
|
2014-03-28 08:25:00 +00:00
|
|
|
self.status = s[0]
|
2013-10-03 11:56:33 +00:00
|
|
|
break
|
2014-04-09 13:50:01 +00:00
|
|
|
|
2015-06-02 06:47:14 +00:00
|
|
|
if self.status != _status and self.status not in ("Submitted", "Cancelled"):
|
2015-01-07 10:11:32 +00:00
|
|
|
self.add_comment("Label", _(self.status))
|
2014-08-19 10:50:04 +00:00
|
|
|
|
2013-10-03 11:56:33 +00:00
|
|
|
if update:
|
2014-03-28 08:25:00 +00:00
|
|
|
frappe.db.set_value(self.doctype, self.name, "status", self.status)
|
2014-04-09 13:50:01 +00:00
|
|
|
|
2013-06-03 11:15:38 +00:00
|
|
|
def validate_qty(self):
|
2014-12-19 10:50:32 +00:00
|
|
|
"""Validates qty at row level"""
|
2013-06-03 11:15:38 +00:00
|
|
|
self.tolerance = {}
|
|
|
|
self.global_tolerance = None
|
2014-04-09 13:50:01 +00:00
|
|
|
|
2013-06-03 11:15:38 +00:00
|
|
|
for args in self.status_updater:
|
2015-08-25 07:19:40 +00:00
|
|
|
if "target_ref_field" not in args:
|
|
|
|
# if target_ref_field is not specified, the programmer does not want to validate qty / amount
|
|
|
|
continue
|
|
|
|
|
2013-06-03 11:15:38 +00:00
|
|
|
# get unique transactions to update
|
2014-04-02 12:39:34 +00:00
|
|
|
for d in self.get_all_children():
|
2014-03-28 08:25:00 +00:00
|
|
|
if d.doctype == args['source_dt'] and d.get(args["join_field"]):
|
2014-03-31 18:07:40 +00:00
|
|
|
args['name'] = d.get(args['join_field'])
|
2013-06-03 11:15:38 +00:00
|
|
|
|
|
|
|
# get all qty where qty > target_field
|
2014-04-09 13:50:01 +00:00
|
|
|
item = frappe.db.sql("""select item_code, `{target_ref_field}`,
|
|
|
|
`{target_field}`, parenttype, parent from `tab{target_dt}`
|
|
|
|
where `{target_ref_field}` < `{target_field}`
|
|
|
|
and name=%s and docstatus=1""".format(**args),
|
2014-03-03 10:21:13 +00:00
|
|
|
args['name'], as_dict=1)
|
2013-06-03 11:15:38 +00:00
|
|
|
if item:
|
|
|
|
item = item[0]
|
|
|
|
item['idx'] = d.idx
|
|
|
|
item['target_ref_field'] = args['target_ref_field'].replace('_', ' ')
|
|
|
|
|
|
|
|
if not item[args['target_ref_field']]:
|
2014-04-14 13:50:45 +00:00
|
|
|
msgprint(_("Note: System will not check over-delivery and over-booking for Item {0} as quantity or amount is 0").format(item.item_code))
|
2013-06-03 11:15:38 +00:00
|
|
|
elif args.get('no_tolerance'):
|
2014-07-09 12:06:38 +00:00
|
|
|
item['reduce_by'] = item[args['target_field']] - item[args['target_ref_field']]
|
2013-06-03 11:15:38 +00:00
|
|
|
if item['reduce_by'] > .01:
|
2014-07-09 12:06:38 +00:00
|
|
|
msgprint(_("Allowance for over-{0} crossed for Item {1}")
|
|
|
|
.format(args["overflow_type"], item.item_code))
|
|
|
|
throw(_("{0} must be reduced by {1} or you should increase overflow tolerance")
|
|
|
|
.format(_(item.target_ref_field.title()), item["reduce_by"]))
|
2014-04-09 13:50:01 +00:00
|
|
|
|
2013-06-03 11:15:38 +00:00
|
|
|
else:
|
|
|
|
self.check_overflow_with_tolerance(item, args)
|
2014-04-09 13:50:01 +00:00
|
|
|
|
2013-06-03 11:15:38 +00:00
|
|
|
def check_overflow_with_tolerance(self, item, args):
|
|
|
|
"""
|
|
|
|
Checks if there is overflow condering a relaxation tolerance
|
|
|
|
"""
|
|
|
|
# check if overflow is within tolerance
|
2014-04-09 13:50:01 +00:00
|
|
|
tolerance, self.tolerance, self.global_tolerance = get_tolerance_for(item['item_code'],
|
2014-01-03 12:13:19 +00:00
|
|
|
self.tolerance, self.global_tolerance)
|
2014-04-09 13:50:01 +00:00
|
|
|
|
|
|
|
overflow_percent = ((item[args['target_field']] - item[args['target_ref_field']]) /
|
2013-06-03 11:15:38 +00:00
|
|
|
item[args['target_ref_field']]) * 100
|
2014-07-19 11:30:15 +00:00
|
|
|
|
2013-06-03 11:15:38 +00:00
|
|
|
if overflow_percent - tolerance > 0.01:
|
|
|
|
item['max_allowed'] = flt(item[args['target_ref_field']] * (100+tolerance)/100)
|
|
|
|
item['reduce_by'] = item[args['target_field']] - item['max_allowed']
|
2014-04-09 13:50:01 +00:00
|
|
|
|
2014-07-09 12:06:38 +00:00
|
|
|
msgprint(_("Allowance for over-{0} crossed for Item {1}.")
|
|
|
|
.format(args["overflow_type"], item["item_code"]))
|
|
|
|
throw(_("{0} must be reduced by {1} or you should increase overflow tolerance")
|
|
|
|
.format(_(item["target_ref_field"].title()), item["reduce_by"]))
|
2013-06-03 11:15:38 +00:00
|
|
|
|
|
|
|
def update_qty(self, change_modified=True):
|
2015-08-25 07:19:40 +00:00
|
|
|
"""Updates qty or amount at row level
|
|
|
|
|
|
|
|
:param change_modified: If true, updates `modified` and `modified_by` for target parent doc
|
2013-06-03 11:15:38 +00:00
|
|
|
"""
|
|
|
|
for args in self.status_updater:
|
|
|
|
# condition to include current record (if submit or no if cancel)
|
2014-03-28 08:25:00 +00:00
|
|
|
if self.docstatus == 1:
|
|
|
|
args['cond'] = ' or parent="%s"' % self.name.replace('"', '\"')
|
2013-06-03 11:15:38 +00:00
|
|
|
else:
|
2014-03-28 08:25:00 +00:00
|
|
|
args['cond'] = ' and parent!="%s"' % self.name.replace('"', '\"')
|
2014-04-09 13:50:01 +00:00
|
|
|
|
2015-08-25 07:19:40 +00:00
|
|
|
args['set_modified'] = ''
|
2013-06-03 11:15:38 +00:00
|
|
|
if change_modified:
|
2015-08-25 07:19:40 +00:00
|
|
|
args['set_modified'] = ', modified = now(), modified_by = "{0}"'\
|
|
|
|
.format(frappe.db.escape(frappe.session.user))
|
|
|
|
|
|
|
|
self._update_children(args)
|
|
|
|
|
|
|
|
if "percent_join_field" in args:
|
|
|
|
self._update_percent_field(args)
|
|
|
|
|
|
|
|
def _update_children(self, args):
|
|
|
|
"""Update quantities or amount in child table"""
|
|
|
|
for d in self.get_all_children():
|
|
|
|
if d.doctype != args['source_dt']:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# updates qty in the child table
|
|
|
|
args['detail_id'] = d.get(args['join_field'])
|
|
|
|
|
|
|
|
args['second_source_condition'] = ""
|
|
|
|
if args.get('second_source_dt') and args.get('second_source_field') \
|
|
|
|
and args.get('second_join_field'):
|
|
|
|
if not args.get("second_source_extra_cond"):
|
|
|
|
args["second_source_extra_cond"] = ""
|
|
|
|
|
|
|
|
args['second_source_condition'] = """ + ifnull((select sum(%(second_source_field)s)
|
|
|
|
from `tab%(second_source_dt)s`
|
|
|
|
where `%(second_join_field)s`="%(detail_id)s"
|
|
|
|
and (`tab%(second_source_dt)s`.docstatus=1) %(second_source_extra_cond)s), 0) """ % args
|
|
|
|
|
|
|
|
if args['detail_id']:
|
|
|
|
if not args.get("extra_cond"): args["extra_cond"] = ""
|
|
|
|
|
|
|
|
frappe.db.sql("""update `tab%(target_dt)s`
|
|
|
|
set %(target_field)s = (select sum(%(source_field)s)
|
|
|
|
from `tab%(source_dt)s` where `%(join_field)s`="%(detail_id)s"
|
|
|
|
and (docstatus=1 %(cond)s) %(extra_cond)s) %(second_source_condition)s
|
|
|
|
where name='%(detail_id)s'""" % args)
|
|
|
|
|
|
|
|
def _update_percent_field(self, args):
|
|
|
|
"""Update percent field in parent transaction"""
|
|
|
|
unique_transactions = set([d.get(args['percent_join_field']) for d in self.get_all_children(args['source_dt'])])
|
|
|
|
|
|
|
|
for name in unique_transactions:
|
|
|
|
if not name:
|
|
|
|
continue
|
|
|
|
|
|
|
|
args['name'] = name
|
|
|
|
|
|
|
|
# update percent complete in the parent table
|
|
|
|
if args.get('target_parent_field'):
|
|
|
|
frappe.db.sql("""update `tab%(target_parent_dt)s`
|
2015-09-25 12:27:20 +00:00
|
|
|
set %(target_parent_field)s = round((select sum(if(%(target_ref_field)s >
|
2015-08-25 07:19:40 +00:00
|
|
|
ifnull(%(target_field)s, 0), %(target_field)s,
|
|
|
|
%(target_ref_field)s))/sum(%(target_ref_field)s)*100
|
2015-09-25 12:27:20 +00:00
|
|
|
from `tab%(target_dt)s` where parent="%(name)s"), 2) %(set_modified)s
|
2015-08-25 07:19:40 +00:00
|
|
|
where name='%(name)s'""" % args)
|
|
|
|
|
|
|
|
# update field
|
|
|
|
if args.get('status_field'):
|
|
|
|
frappe.db.sql("""update `tab%(target_parent_dt)s`
|
|
|
|
set %(status_field)s = if(ifnull(%(target_parent_field)s,0)<0.001,
|
|
|
|
'Not %(keyword)s', if(%(target_parent_field)s>=99.99,
|
|
|
|
'Fully %(keyword)s', 'Partly %(keyword)s'))
|
|
|
|
where name='%(name)s'""" % args)
|
|
|
|
|
|
|
|
if args.get("set_modified"):
|
2015-10-02 07:12:48 +00:00
|
|
|
target = frappe.get_doc(args["target_parent_dt"], name)
|
|
|
|
target.set_status(update=True)
|
|
|
|
target.notify_update()
|
2014-04-09 13:50:01 +00:00
|
|
|
|
2014-01-15 12:06:18 +00:00
|
|
|
def update_billing_status_for_zero_amount_refdoc(self, ref_dt):
|
|
|
|
ref_fieldname = ref_dt.lower().replace(" ", "_")
|
|
|
|
zero_amount_refdoc = []
|
2014-04-09 13:50:01 +00:00
|
|
|
all_zero_amount_refdoc = frappe.db.sql_list("""select name from `tab%s`
|
2015-02-12 10:39:11 +00:00
|
|
|
where docstatus=1 and base_net_total = 0""" % ref_dt)
|
2014-04-09 13:50:01 +00:00
|
|
|
|
2014-12-25 12:49:39 +00:00
|
|
|
for item in self.get("items"):
|
2014-03-28 08:25:00 +00:00
|
|
|
if item.get(ref_fieldname) \
|
|
|
|
and item.get(ref_fieldname) in all_zero_amount_refdoc \
|
|
|
|
and item.get(ref_fieldname) not in zero_amount_refdoc:
|
2014-03-31 18:07:40 +00:00
|
|
|
zero_amount_refdoc.append(item.get(ref_fieldname))
|
2014-04-09 13:50:01 +00:00
|
|
|
|
2014-01-15 12:06:18 +00:00
|
|
|
if zero_amount_refdoc:
|
|
|
|
self.update_biling_status(zero_amount_refdoc, ref_dt, ref_fieldname)
|
2014-04-09 13:50:01 +00:00
|
|
|
|
2014-01-15 12:06:18 +00:00
|
|
|
def update_biling_status(self, zero_amount_refdoc, ref_dt, ref_fieldname):
|
|
|
|
for ref_dn in zero_amount_refdoc:
|
2014-04-09 13:50:01 +00:00
|
|
|
ref_doc_qty = flt(frappe.db.sql("""select sum(ifnull(qty, 0)) from `tab%s Item`
|
2014-01-15 12:06:18 +00:00
|
|
|
where parent=%s""" % (ref_dt, '%s'), (ref_dn))[0][0])
|
2014-04-09 13:50:01 +00:00
|
|
|
|
|
|
|
billed_qty = flt(frappe.db.sql("""select sum(ifnull(qty, 0))
|
|
|
|
from `tab%s Item` where %s=%s and docstatus=1""" %
|
2014-03-28 08:25:00 +00:00
|
|
|
(self.doctype, ref_fieldname, '%s'), (ref_dn))[0][0])
|
2014-04-09 13:50:01 +00:00
|
|
|
|
2014-01-15 12:06:18 +00:00
|
|
|
per_billed = ((ref_doc_qty if billed_qty > ref_doc_qty else billed_qty)\
|
|
|
|
/ ref_doc_qty)*100
|
2014-02-26 07:05:33 +00:00
|
|
|
frappe.db.set_value(ref_dt, ref_dn, "per_billed", per_billed)
|
2014-04-09 13:50:01 +00:00
|
|
|
|
2014-03-31 11:57:06 +00:00
|
|
|
if frappe.get_meta(ref_dt).get_field("billing_status"):
|
2014-01-15 12:06:18 +00:00
|
|
|
if per_billed < 0.001: billing_status = "Not Billed"
|
|
|
|
elif per_billed >= 99.99: billing_status = "Fully Billed"
|
|
|
|
else: billing_status = "Partly Billed"
|
2014-04-09 13:50:01 +00:00
|
|
|
|
2014-02-26 07:05:33 +00:00
|
|
|
frappe.db.set_value(ref_dt, ref_dn, "billing_status", billing_status)
|
2014-04-09 13:50:01 +00:00
|
|
|
|
2014-01-03 12:13:19 +00:00
|
|
|
def get_tolerance_for(item_code, item_tolerance={}, global_tolerance=None):
|
|
|
|
"""
|
|
|
|
Returns the tolerance for the item, if not set, returns global tolerance
|
|
|
|
"""
|
|
|
|
if item_tolerance.get(item_code):
|
|
|
|
return item_tolerance[item_code], item_tolerance, global_tolerance
|
2014-04-09 13:50:01 +00:00
|
|
|
|
2014-02-26 07:05:33 +00:00
|
|
|
tolerance = flt(frappe.db.get_value('Item',item_code,'tolerance') or 0)
|
2014-01-03 12:13:19 +00:00
|
|
|
|
|
|
|
if not tolerance:
|
|
|
|
if global_tolerance == None:
|
2014-07-19 11:23:45 +00:00
|
|
|
global_tolerance = flt(frappe.db.get_value('Stock Settings', None, 'tolerance'))
|
2014-01-03 12:13:19 +00:00
|
|
|
tolerance = global_tolerance
|
2014-04-09 13:50:01 +00:00
|
|
|
|
2014-01-03 12:13:19 +00:00
|
|
|
item_tolerance[item_code] = tolerance
|
2014-04-09 13:50:01 +00:00
|
|
|
return tolerance, item_tolerance, global_tolerance
|