blob: 070f2002764b05b3bcd0d2276ad3a1db1f15d4ec [file] [log] [blame]
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05301# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
2# License: GNU General Public License v3. See license.txt
Nabin Hait0feebc12013-06-03 16:45:38 +05303
4from __future__ import unicode_literals
5import webnotes
6from webnotes.utils import flt, cstr
7from webnotes import msgprint
8
9from webnotes.model.controller import DocListController
10
11class StatusUpdater(DocListController):
12 """
13 Updates the status of the calling records
14 Delivery Note: Update Delivered Qty, Update Percent and Validate over delivery
15 Sales Invoice: Update Billed Amt, Update Percent and Validate over billing
16 Installation Note: Update Installed Qty, Update Percent Qty and Validate over installation
17 """
18
19 def update_prevdoc_status(self):
20 self.update_qty()
21 self.validate_qty()
22
23 def validate_qty(self):
24 """
25 Validates qty at row level
26 """
27 self.tolerance = {}
28 self.global_tolerance = None
29
30 for args in self.status_updater:
31 # get unique transactions to update
32 for d in self.doclist:
33 if d.doctype == args['source_dt'] and d.fields.get(args["join_field"]):
34 args['name'] = d.fields[args['join_field']]
35
36 # get all qty where qty > target_field
37 item = webnotes.conn.sql("""select item_code, `%(target_ref_field)s`,
38 `%(target_field)s`, parenttype, parent from `tab%(target_dt)s`
39 where `%(target_ref_field)s` < `%(target_field)s`
40 and name="%(name)s" and docstatus=1""" % args, as_dict=1)
41 if item:
42 item = item[0]
43 item['idx'] = d.idx
44 item['target_ref_field'] = args['target_ref_field'].replace('_', ' ')
45
46 if not item[args['target_ref_field']]:
47 msgprint("""As %(target_ref_field)s for item: %(item_code)s in \
48 %(parenttype)s: %(parent)s is zero, system will not check \
49 over-delivery or over-billed""" % item)
50 elif args.get('no_tolerance'):
51 item['reduce_by'] = item[args['target_field']] - \
52 item[args['target_ref_field']]
53 if item['reduce_by'] > .01:
54 msgprint("""
55 Row #%(idx)s: Max %(target_ref_field)s allowed for <b>Item \
56 %(item_code)s</b> against <b>%(parenttype)s %(parent)s</b> \
57 is <b>""" % item + cstr(item[args['target_ref_field']]) +
58 """</b>.<br>You must reduce the %(target_ref_field)s by \
59 %(reduce_by)s""" % item, raise_exception=1)
60
61 else:
62 self.check_overflow_with_tolerance(item, args)
63
64 def check_overflow_with_tolerance(self, item, args):
65 """
66 Checks if there is overflow condering a relaxation tolerance
67 """
68
69 # check if overflow is within tolerance
70 tolerance = self.get_tolerance_for(item['item_code'])
71 overflow_percent = ((item[args['target_field']] - item[args['target_ref_field']]) /
72 item[args['target_ref_field']]) * 100
73
74 if overflow_percent - tolerance > 0.01:
75 item['max_allowed'] = flt(item[args['target_ref_field']] * (100+tolerance)/100)
76 item['reduce_by'] = item[args['target_field']] - item['max_allowed']
77
78 msgprint("""
79 Row #%(idx)s: Max %(target_ref_field)s allowed for <b>Item %(item_code)s</b> \
80 against <b>%(parenttype)s %(parent)s</b> is <b>%(max_allowed)s</b>.
81
82 If you want to increase your overflow tolerance, please increase tolerance %% in \
83 Global Defaults or Item master.
84
85 Or, you must reduce the %(target_ref_field)s by %(reduce_by)s
86
87 Also, please check if the order item has already been billed in the Sales Order""" %
88 item, raise_exception=1)
89
90 def get_tolerance_for(self, item_code):
91 """
92 Returns the tolerance for the item, if not set, returns global tolerance
93 """
94 if self.tolerance.get(item_code): return self.tolerance[item_code]
95
96 tolerance = flt(webnotes.conn.get_value('Item',item_code,'tolerance') or 0)
97
98 if not tolerance:
99 if self.global_tolerance == None:
100 self.global_tolerance = flt(webnotes.conn.get_value('Global Defaults', None,
101 'tolerance'))
102 tolerance = self.global_tolerance
103
104 self.tolerance[item_code] = tolerance
105 return tolerance
106
107
108 def update_qty(self, change_modified=True):
109 """
110 Updates qty at row level
111 """
112 for args in self.status_updater:
113 # condition to include current record (if submit or no if cancel)
114 if self.doc.docstatus == 1:
115 args['cond'] = ' or parent="%s"' % self.doc.name
116 else:
117 args['cond'] = ' and parent!="%s"' % self.doc.name
118
119 args['modified_cond'] = ''
120 if change_modified:
121 args['modified_cond'] = ', modified = now()'
122
123 # update quantities in child table
124 for d in self.doclist:
125 if d.doctype == args['source_dt']:
126 # updates qty in the child table
127 args['detail_id'] = d.fields.get(args['join_field'])
128
129 args['second_source_condition'] = ""
130 if args.get('second_source_dt') and args.get('second_source_field') \
131 and args.get('second_join_field'):
132 args['second_source_condition'] = """ + (select sum(%(second_source_field)s)
133 from `tab%(second_source_dt)s`
134 where `%(second_join_field)s`="%(detail_id)s"
135 and (docstatus=1))""" % args
136
137 if args['detail_id']:
138 webnotes.conn.sql("""update `tab%(target_dt)s`
139 set %(target_field)s = (select sum(%(source_field)s)
140 from `tab%(source_dt)s` where `%(join_field)s`="%(detail_id)s"
141 and (docstatus=1 %(cond)s)) %(second_source_condition)s
142 where name='%(detail_id)s'""" % args)
143
144 # get unique transactions to update
145 for name in set([d.fields.get(args['percent_join_field']) for d in self.doclist
146 if d.doctype == args['source_dt']]):
147 if name:
148 args['name'] = name
149
150 # update percent complete in the parent table
151 webnotes.conn.sql("""update `tab%(target_parent_dt)s`
152 set %(target_parent_field)s = (select sum(if(%(target_ref_field)s >
153 ifnull(%(target_field)s, 0), %(target_field)s,
154 %(target_ref_field)s))/sum(%(target_ref_field)s)*100
155 from `tab%(target_dt)s` where parent="%(name)s") %(modified_cond)s
156 where name='%(name)s'""" % args)
157
158 # update field
159 if args.get('status_field'):
160 webnotes.conn.sql("""update `tab%(target_parent_dt)s`
161 set %(status_field)s = if(ifnull(%(target_parent_field)s,0)<0.001,
162 'Not %(keyword)s', if(%(target_parent_field)s>=99.99,
163 'Fully %(keyword)s', 'Partly %(keyword)s'))
164 where name='%(name)s'""" % args)