2013-11-20 07:29:58 +00:00
|
|
|
# Copyright (c) 2013, Web Notes 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
|
|
|
|
from frappe.utils import flt, cstr
|
|
|
|
from frappe import msgprint
|
2013-06-03 11:15:38 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
from frappe.model.controller import DocListController
|
2013-06-03 11:15:38 +00:00
|
|
|
|
2013-10-03 11:56:33 +00:00
|
|
|
status_map = {
|
|
|
|
"Contact": [
|
|
|
|
["Replied", "communication_sent"],
|
|
|
|
["Open", "communication_received"]
|
|
|
|
],
|
|
|
|
"Job Applicant": [
|
|
|
|
["Replied", "communication_sent"],
|
|
|
|
["Open", "communication_received"]
|
|
|
|
],
|
|
|
|
"Lead": [
|
|
|
|
["Replied", "communication_sent"],
|
|
|
|
["Converted", "has_customer"],
|
|
|
|
["Opportunity", "has_opportunity"],
|
|
|
|
["Open", "communication_received"],
|
|
|
|
],
|
|
|
|
"Opportunity": [
|
|
|
|
["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
|
|
|
["Quotation", "has_quotation"],
|
|
|
|
["Replied", "communication_sent"],
|
2014-03-28 08:25:00 +00:00
|
|
|
["Cancelled", "eval:self.docstatus==2"],
|
2013-10-03 11:56:33 +00:00
|
|
|
["Open", "communication_received"],
|
|
|
|
],
|
|
|
|
"Quotation": [
|
|
|
|
["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"],
|
|
|
|
["Replied", "communication_sent"],
|
2014-03-28 08:25:00 +00:00
|
|
|
["Cancelled", "eval:self.docstatus==2"],
|
2013-10-03 11:56:33 +00:00
|
|
|
["Open", "communication_received"],
|
|
|
|
],
|
|
|
|
"Sales Order": [
|
|
|
|
["Draft", None],
|
2014-03-28 08:25:00 +00:00
|
|
|
["Submitted", "eval:self.docstatus==1"],
|
|
|
|
["Stopped", "eval:self.status=='Stopped'"],
|
|
|
|
["Cancelled", "eval:self.docstatus==2"],
|
2013-10-03 11:56:33 +00:00
|
|
|
],
|
|
|
|
"Support Ticket": [
|
|
|
|
["Replied", "communication_sent"],
|
|
|
|
["Open", "communication_received"]
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
2013-06-03 11:15:38 +00:00
|
|
|
class StatusUpdater(DocListController):
|
|
|
|
"""
|
|
|
|
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()
|
|
|
|
|
2013-10-03 11:56:33 +00:00
|
|
|
def set_status(self, update=False):
|
2014-03-28 08:25:00 +00:00
|
|
|
if self.get("__islocal"):
|
2013-10-03 11:56:33 +00:00
|
|
|
return
|
|
|
|
|
2014-03-28 08:25:00 +00:00
|
|
|
if self.doctype in status_map:
|
|
|
|
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
|
|
|
|
|
|
|
|
if update:
|
2014-03-28 08:25:00 +00:00
|
|
|
frappe.db.set_value(self.doctype, self.name, "status", self.status)
|
2013-10-03 11:56:33 +00:00
|
|
|
|
|
|
|
def on_communication(self):
|
2013-10-04 08:03:24 +00:00
|
|
|
self.communication_set = True
|
2013-10-03 11:56:33 +00:00
|
|
|
self.set_status(update=True)
|
2013-10-04 08:03:24 +00:00
|
|
|
del self.communication_set
|
2013-10-03 11:56:33 +00:00
|
|
|
|
|
|
|
def communication_received(self):
|
2013-10-04 08:03:24 +00:00
|
|
|
if getattr(self, "communication_set", False):
|
|
|
|
last_comm = self.doclist.get({"doctype":"Communication"})
|
|
|
|
if last_comm:
|
|
|
|
return last_comm[-1].sent_or_received == "Received"
|
2013-10-03 11:56:33 +00:00
|
|
|
|
|
|
|
def communication_sent(self):
|
2013-10-04 08:03:24 +00:00
|
|
|
if getattr(self, "communication_set", False):
|
|
|
|
last_comm = self.doclist.get({"doctype":"Communication"})
|
|
|
|
if last_comm:
|
|
|
|
return last_comm[-1].sent_or_received == "Sent"
|
2013-10-03 11:56:33 +00:00
|
|
|
|
2013-06-03 11:15:38 +00:00
|
|
|
def validate_qty(self):
|
|
|
|
"""
|
|
|
|
Validates qty at row level
|
|
|
|
"""
|
|
|
|
self.tolerance = {}
|
|
|
|
self.global_tolerance = None
|
|
|
|
|
|
|
|
for args in self.status_updater:
|
|
|
|
# get unique transactions to update
|
|
|
|
for d in self.doclist:
|
2014-03-28 08:25:00 +00:00
|
|
|
if d.doctype == args['source_dt'] and d.get(args["join_field"]):
|
2013-06-03 11:15:38 +00:00
|
|
|
args['name'] = d.fields[args['join_field']]
|
|
|
|
|
|
|
|
# get all qty where qty > target_field
|
2014-03-03 10:21:13 +00:00
|
|
|
item = frappe.db.sql("""select item_code, `{target_ref_field}`,
|
2014-03-25 12:58:49 +00:00
|
|
|
`{target_field}`, parenttype, parent from `tab{target_dt}`
|
2014-03-03 10:21:13 +00:00
|
|
|
where `{target_ref_field}` < `{target_field}`
|
|
|
|
and name=%s and docstatus=1""".format(**args),
|
|
|
|
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']]:
|
|
|
|
msgprint("""As %(target_ref_field)s for item: %(item_code)s in \
|
|
|
|
%(parenttype)s: %(parent)s is zero, system will not check \
|
|
|
|
over-delivery or over-billed""" % item)
|
|
|
|
elif args.get('no_tolerance'):
|
|
|
|
item['reduce_by'] = item[args['target_field']] - \
|
|
|
|
item[args['target_ref_field']]
|
|
|
|
if item['reduce_by'] > .01:
|
|
|
|
msgprint("""
|
|
|
|
Row #%(idx)s: Max %(target_ref_field)s allowed for <b>Item \
|
|
|
|
%(item_code)s</b> against <b>%(parenttype)s %(parent)s</b> \
|
|
|
|
is <b>""" % item + cstr(item[args['target_ref_field']]) +
|
|
|
|
"""</b>.<br>You must reduce the %(target_ref_field)s by \
|
|
|
|
%(reduce_by)s""" % item, raise_exception=1)
|
|
|
|
|
|
|
|
else:
|
|
|
|
self.check_overflow_with_tolerance(item, args)
|
|
|
|
|
|
|
|
def check_overflow_with_tolerance(self, item, args):
|
|
|
|
"""
|
|
|
|
Checks if there is overflow condering a relaxation tolerance
|
|
|
|
"""
|
|
|
|
|
|
|
|
# check if overflow is within tolerance
|
2014-01-03 12:13:19 +00:00
|
|
|
tolerance, self.tolerance, self.global_tolerance = get_tolerance_for(item['item_code'],
|
|
|
|
self.tolerance, self.global_tolerance)
|
|
|
|
|
2013-06-03 11:15:38 +00:00
|
|
|
overflow_percent = ((item[args['target_field']] - item[args['target_ref_field']]) /
|
|
|
|
item[args['target_ref_field']]) * 100
|
|
|
|
|
|
|
|
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']
|
|
|
|
|
|
|
|
msgprint("""
|
|
|
|
Row #%(idx)s: Max %(target_ref_field)s allowed for <b>Item %(item_code)s</b> \
|
|
|
|
against <b>%(parenttype)s %(parent)s</b> is <b>%(max_allowed)s</b>.
|
|
|
|
|
|
|
|
If you want to increase your overflow tolerance, please increase tolerance %% in \
|
|
|
|
Global Defaults or Item master.
|
|
|
|
|
|
|
|
Or, you must reduce the %(target_ref_field)s by %(reduce_by)s
|
|
|
|
|
|
|
|
Also, please check if the order item has already been billed in the Sales Order""" %
|
|
|
|
item, raise_exception=1)
|
|
|
|
|
|
|
|
|
|
|
|
def update_qty(self, change_modified=True):
|
|
|
|
"""
|
|
|
|
Updates qty at row level
|
|
|
|
"""
|
|
|
|
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('"', '\"')
|
2013-06-03 11:15:38 +00:00
|
|
|
|
|
|
|
args['modified_cond'] = ''
|
|
|
|
if change_modified:
|
|
|
|
args['modified_cond'] = ', modified = now()'
|
|
|
|
|
|
|
|
# update quantities in child table
|
|
|
|
for d in self.doclist:
|
|
|
|
if d.doctype == args['source_dt']:
|
|
|
|
# updates qty in the child table
|
2014-03-28 08:25:00 +00:00
|
|
|
args['detail_id'] = d.get(args['join_field'])
|
2013-06-03 11:15:38 +00:00
|
|
|
|
|
|
|
args['second_source_condition'] = ""
|
|
|
|
if args.get('second_source_dt') and args.get('second_source_field') \
|
|
|
|
and args.get('second_join_field'):
|
|
|
|
args['second_source_condition'] = """ + (select sum(%(second_source_field)s)
|
|
|
|
from `tab%(second_source_dt)s`
|
|
|
|
where `%(second_join_field)s`="%(detail_id)s"
|
|
|
|
and (docstatus=1))""" % args
|
|
|
|
|
|
|
|
if args['detail_id']:
|
2014-02-26 07:05:33 +00:00
|
|
|
frappe.db.sql("""update `tab%(target_dt)s`
|
2013-06-03 11:15:38 +00:00
|
|
|
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)) %(second_source_condition)s
|
|
|
|
where name='%(detail_id)s'""" % args)
|
|
|
|
|
|
|
|
# get unique transactions to update
|
2014-03-28 08:25:00 +00:00
|
|
|
for name in set([d.get(args['percent_join_field']) for d in self.doclist
|
2013-06-03 11:15:38 +00:00
|
|
|
if d.doctype == args['source_dt']]):
|
|
|
|
if name:
|
|
|
|
args['name'] = name
|
|
|
|
|
|
|
|
# update percent complete in the parent table
|
2014-02-26 07:05:33 +00:00
|
|
|
frappe.db.sql("""update `tab%(target_parent_dt)s`
|
2013-06-03 11:15:38 +00:00
|
|
|
set %(target_parent_field)s = (select sum(if(%(target_ref_field)s >
|
|
|
|
ifnull(%(target_field)s, 0), %(target_field)s,
|
|
|
|
%(target_ref_field)s))/sum(%(target_ref_field)s)*100
|
|
|
|
from `tab%(target_dt)s` where parent="%(name)s") %(modified_cond)s
|
|
|
|
where name='%(name)s'""" % args)
|
|
|
|
|
|
|
|
# update field
|
|
|
|
if args.get('status_field'):
|
2014-02-26 07:05:33 +00:00
|
|
|
frappe.db.sql("""update `tab%(target_parent_dt)s`
|
2013-06-03 11:15:38 +00:00
|
|
|
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'))
|
2014-01-03 12:13:19 +00:00
|
|
|
where name='%(name)s'""" % args)
|
|
|
|
|
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-02-26 07:05:33 +00:00
|
|
|
all_zero_amount_refdoc = frappe.db.sql_list("""select name from `tab%s`
|
2014-01-15 12:06:18 +00:00
|
|
|
where docstatus=1 and net_total = 0""" % ref_dt)
|
|
|
|
|
2014-03-27 10:42:56 +00:00
|
|
|
for item in self.get("entries"):
|
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-01-15 12:06:18 +00:00
|
|
|
zero_amount_refdoc.append(item.fields[ref_fieldname])
|
|
|
|
|
|
|
|
if zero_amount_refdoc:
|
|
|
|
self.update_biling_status(zero_amount_refdoc, ref_dt, ref_fieldname)
|
|
|
|
|
|
|
|
def update_biling_status(self, zero_amount_refdoc, ref_dt, ref_fieldname):
|
|
|
|
for ref_dn in zero_amount_refdoc:
|
2014-02-26 07:05:33 +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-02-26 07:05:33 +00:00
|
|
|
billed_qty = flt(frappe.db.sql("""select sum(ifnull(qty, 0))
|
2014-01-15 12:06:18 +00:00
|
|
|
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-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-01-15 12:06:18 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
from frappe.model.meta import has_field
|
2014-01-15 12:06:18 +00:00
|
|
|
if has_field(ref_dt, "billing_status"):
|
|
|
|
if per_billed < 0.001: billing_status = "Not Billed"
|
|
|
|
elif per_billed >= 99.99: billing_status = "Fully Billed"
|
|
|
|
else: billing_status = "Partly Billed"
|
|
|
|
|
2014-02-26 07:05:33 +00:00
|
|
|
frappe.db.set_value(ref_dt, ref_dn, "billing_status", billing_status)
|
2014-01-15 12:06:18 +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-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-02-26 07:05:33 +00:00
|
|
|
global_tolerance = flt(frappe.db.get_value('Global Defaults', None,
|
2014-01-03 12:13:19 +00:00
|
|
|
'tolerance'))
|
|
|
|
tolerance = global_tolerance
|
|
|
|
|
|
|
|
item_tolerance[item_code] = tolerance
|
|
|
|
return tolerance, item_tolerance, global_tolerance
|