blob: a13d747279c173f0912003500bf8c9a57c06848f [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 Haitec52db82013-01-22 11:53:14 +05303
4from __future__ import unicode_literals
5import webnotes
Nabin Hait040d1f32013-08-21 17:04:33 +05306from webnotes.utils import cint, flt, comma_or, _round, add_days, cstr
Nabin Haitec52db82013-01-22 11:53:14 +05307from setup.utils import get_company_currency
Anand Doshi3543f302013-05-24 19:25:01 +05308from selling.utils import get_item_details
Nabin Hait0fc24542013-03-25 11:06:00 +05309from webnotes import msgprint, _
Nabin Haitec52db82013-01-22 11:53:14 +053010
Nabin Haitc3afb252013-03-19 12:01:24 +053011from controllers.stock_controller import StockController
Nabin Haitbf495c92013-01-30 12:49:08 +053012
Nabin Haitc3afb252013-03-19 12:01:24 +053013class SellingController(StockController):
Anand Doshif3096132013-05-21 19:35:06 +053014 def onload_post_render(self):
Anand Doshif3096132013-05-21 19:35:06 +053015 # contact, address, item details and pos details (if applicable)
16 self.set_missing_values()
Nabin Haita9ec49e2013-07-15 16:33:24 +053017
Anand Doshif3096132013-05-21 19:35:06 +053018 def set_missing_values(self, for_validate=False):
Anand Doshiabc10032013-06-14 17:44:03 +053019 super(SellingController, self).set_missing_values(for_validate)
20
Anand Doshif3096132013-05-21 19:35:06 +053021 # set contact and address details for customer, if they are not mentioned
Anand Doshiabc10032013-06-14 17:44:03 +053022 self.set_missing_lead_customer_details()
Anand Doshi99100a42013-07-04 17:13:53 +053023 self.set_price_list_and_item_details()
Rushabh Mehtae89c5332013-08-29 15:32:46 +053024 if self.doc.fields.get("__islocal"):
25 self.set_taxes("other_charges", "charge")
Anand Doshiabc10032013-06-14 17:44:03 +053026
27 def set_missing_lead_customer_details(self):
28 if self.doc.customer:
Anand Doshi99100a42013-07-04 17:13:53 +053029 if not (self.doc.contact_person and self.doc.customer_address and self.doc.customer_name):
30 for fieldname, val in self.get_customer_defaults().items():
Anand Doshiabc10032013-06-14 17:44:03 +053031 if not self.doc.fields.get(fieldname) and self.meta.get_field(fieldname):
32 self.doc.fields[fieldname] = val
33
Anand Doshiabc10032013-06-14 17:44:03 +053034 elif self.doc.lead:
Anand Doshi99100a42013-07-04 17:13:53 +053035 if not (self.doc.customer_address and self.doc.customer_name and \
36 self.doc.contact_display):
37 for fieldname, val in self.get_lead_defaults().items():
38 if not self.doc.fields.get(fieldname) and self.meta.get_field(fieldname):
39 self.doc.fields[fieldname] = val
40
41 def set_price_list_and_item_details(self):
42 self.set_price_list_currency("Selling")
43 self.set_missing_item_details(get_item_details)
Nabin Hait2bd37772013-07-08 19:00:29 +053044
Anand Doshif3096132013-05-21 19:35:06 +053045 def get_other_charges(self):
46 self.doclist = self.doc.clear_table(self.doclist, "other_charges")
Anand Doshi99100a42013-07-04 17:13:53 +053047 self.set_taxes("other_charges", "charge")
Anand Doshif3096132013-05-21 19:35:06 +053048
Anand Doshi99100a42013-07-04 17:13:53 +053049 def apply_shipping_rule(self):
50 if self.doc.shipping_rule:
51 shipping_rule = webnotes.bean("Shipping Rule", self.doc.shipping_rule)
52 value = self.doc.net_total
53
54 # TODO
55 # shipping rule calculation based on item's net weight
56
57 shipping_amount = 0.0
58 for condition in shipping_rule.doclist.get({"parentfield": "shipping_rule_conditions"}):
59 if not condition.to_value or (flt(condition.from_value) <= value <= flt(condition.to_value)):
60 shipping_amount = condition.shipping_amount
61 break
Anand Doshi0b4943c2013-07-04 23:45:22 +053062
63 self.doclist.append({
64 "doctype": "Sales Taxes and Charges",
65 "parentfield": "other_charges",
66 "charge_type": "Actual",
67 "account_head": shipping_rule.doc.account,
68 "cost_center": shipping_rule.doc.cost_center,
69 "description": shipping_rule.doc.label,
70 "rate": shipping_amount
71 })
Anand Doshifc777182013-05-27 19:29:07 +053072
Nabin Haitec52db82013-01-22 11:53:14 +053073 def set_total_in_words(self):
74 from webnotes.utils import money_in_words
75 company_currency = get_company_currency(self.doc.company)
Nabin Hait6f1a5ec2013-02-20 16:25:05 +053076
77 disable_rounded_total = cint(webnotes.conn.get_value("Global Defaults", None,
78 "disable_rounded_total"))
79
Nabin Haitec52db82013-01-22 11:53:14 +053080 if self.meta.get_field("in_words"):
Nabin Hait6f1a5ec2013-02-20 16:25:05 +053081 self.doc.in_words = money_in_words(disable_rounded_total and
82 self.doc.grand_total or self.doc.rounded_total, company_currency)
Nabin Haitec52db82013-01-22 11:53:14 +053083 if self.meta.get_field("in_words_export"):
Nabin Hait6f1a5ec2013-02-20 16:25:05 +053084 self.doc.in_words_export = money_in_words(disable_rounded_total and
Nabin Hait8c7234f2013-03-11 16:32:33 +053085 self.doc.grand_total_export or self.doc.rounded_total_export, self.doc.currency)
Nabin Haita0e7c152013-03-21 18:37:06 +053086
Nabin Hait787c02e2013-03-29 16:42:33 +053087 def set_buying_amount(self, stock_ledger_entries = None):
Nabin Hait94c90bd2013-08-30 22:48:19 +053088 from stock.utils import get_buying_amount, get_sales_bom_buying_amount
Nabin Hait787c02e2013-03-29 16:42:33 +053089 if not stock_ledger_entries:
90 stock_ledger_entries = self.get_stock_ledger_entries()
Anand Doshi8c5844e2013-03-21 20:24:10 +053091
92 item_sales_bom = {}
93 for d in self.doclist.get({"parentfield": "packing_details"}):
94 new_d = webnotes._dict(d.fields.copy())
95 new_d.total_qty = -1 * d.qty
96 item_sales_bom.setdefault(d.parent_item, []).append(new_d)
Nabin Haita0e7c152013-03-21 18:37:06 +053097
98 if stock_ledger_entries:
99 for item in self.doclist.get({"parentfield": self.fname}):
100 if item.item_code in self.stock_items or \
101 (item_sales_bom and item_sales_bom.get(item.item_code)):
Nabin Hait94c90bd2013-08-30 22:48:19 +0530102 if item.item_code in self.stock_items:
103 buying_amount = get_buying_amount(self.doc.doctype, self.doc.name,
104 item.name, stock_ledger_entries.get((item.item_code,
105 item.warehouse), []))
106 elif item_sales_bom and item_sales_bom.get(item.item_code):
107 buying_amount = get_sales_bom_buying_amount(item.item_code, item.warehouse,
108 self.doc.doctype, self.doc.name, item.name, stock_ledger_entries,
109 item_sales_bom)
Nabin Haitf2d4df92013-05-07 13:12:02 +0530110
Nabin Hait94c90bd2013-08-30 22:48:19 +0530111 item.buying_amount = buying_amount >= 0.01 and buying_amount or 0
112 webnotes.conn.set_value(item.doctype, item.name, "buying_amount",
113 item.buying_amount)
Nabin Hait0fc24542013-03-25 11:06:00 +0530114
115 def check_expense_account(self, item):
116 if item.buying_amount and not item.expense_account:
117 msgprint(_("""Expense account is mandatory for item: """) + item.item_code,
Nabin Hait787c02e2013-03-29 16:42:33 +0530118 raise_exception=1)
119
120 if item.buying_amount and not item.cost_center:
121 msgprint(_("""Cost Center is mandatory for item: """) + item.item_code,
Anand Doshi21f4ea32013-05-10 18:08:32 +0530122 raise_exception=1)
123
124 def calculate_taxes_and_totals(self):
Anand Doshi3543f302013-05-24 19:25:01 +0530125 self.other_fname = "other_charges"
Anand Doshi21f4ea32013-05-10 18:08:32 +0530126
Anand Doshi3543f302013-05-24 19:25:01 +0530127 super(SellingController, self).calculate_taxes_and_totals()
128
Anand Doshi923d41d2013-05-28 17:23:36 +0530129 self.calculate_total_advance("Sales Invoice", "advance_adjustment_details")
Anand Doshif3096132013-05-21 19:35:06 +0530130 self.calculate_commission()
131 self.calculate_contribution()
Anand Doshi3543f302013-05-24 19:25:01 +0530132
Anand Doshif3096132013-05-21 19:35:06 +0530133 def determine_exclusive_rate(self):
Anand Doshi21f4ea32013-05-10 18:08:32 +0530134 if not any((cint(tax.included_in_print_rate) for tax in self.tax_doclist)):
135 # no inclusive tax
136 return
137
138 for item in self.item_doclist:
139 item_tax_map = self._load_item_tax_rate(item.item_tax_rate)
140 cumulated_tax_fraction = 0
141 for i, tax in enumerate(self.tax_doclist):
Anand Doshif3096132013-05-21 19:35:06 +0530142 tax.tax_fraction_for_current_item = self.get_current_tax_fraction(tax, item_tax_map)
143
Anand Doshi21f4ea32013-05-10 18:08:32 +0530144 if i==0:
Anand Doshif3096132013-05-21 19:35:06 +0530145 tax.grand_total_fraction_for_current_item = 1 + tax.tax_fraction_for_current_item
Anand Doshi21f4ea32013-05-10 18:08:32 +0530146 else:
147 tax.grand_total_fraction_for_current_item = \
148 self.tax_doclist[i-1].grand_total_fraction_for_current_item \
149 + tax.tax_fraction_for_current_item
150
151 cumulated_tax_fraction += tax.tax_fraction_for_current_item
152
153 if cumulated_tax_fraction:
Anand Doshifbe1e162013-08-06 19:38:19 +0530154 item.amount = flt((item.export_amount * self.doc.conversion_rate) /
155 (1 + cumulated_tax_fraction), self.precision("amount", item))
156
157 item.basic_rate = flt(item.amount / item.qty, self.precision("basic_rate", item))
Anand Doshi21f4ea32013-05-10 18:08:32 +0530158
Anand Doshif3096132013-05-21 19:35:06 +0530159 if item.adj_rate == 100:
160 item.base_ref_rate = item.basic_rate
161 item.basic_rate = 0.0
162 else:
163 item.base_ref_rate = flt(item.basic_rate / (1 - (item.adj_rate / 100.0)),
164 self.precision("base_ref_rate", item))
Anand Doshi21f4ea32013-05-10 18:08:32 +0530165
166 def get_current_tax_fraction(self, tax, item_tax_map):
167 """
168 Get tax fraction for calculating tax exclusive amount
169 from tax inclusive amount
170 """
171 current_tax_fraction = 0
172
173 if cint(tax.included_in_print_rate):
174 tax_rate = self._get_tax_rate(tax, item_tax_map)
175
176 if tax.charge_type == "On Net Total":
177 current_tax_fraction = tax_rate / 100.0
178
179 elif tax.charge_type == "On Previous Row Amount":
180 current_tax_fraction = (tax_rate / 100.0) * \
181 self.tax_doclist[cint(tax.row_id) - 1].tax_fraction_for_current_item
182
183 elif tax.charge_type == "On Previous Row Total":
184 current_tax_fraction = (tax_rate / 100.0) * \
185 self.tax_doclist[cint(tax.row_id) - 1].grand_total_fraction_for_current_item
186
187 return current_tax_fraction
188
189 def calculate_item_values(self):
Anand Doshi21f4ea32013-05-10 18:08:32 +0530190 for item in self.item_doclist:
Anand Doshi39384d32013-05-11 19:39:53 +0530191 self.round_floats_in(item)
Anand Doshi21f4ea32013-05-10 18:08:32 +0530192
193 if item.adj_rate == 100:
Anand Doshi21f4ea32013-05-10 18:08:32 +0530194 item.export_rate = 0
Anand Doshi47e14a92013-08-13 13:01:12 +0530195 elif not item.export_rate:
Anand Doshi3543f302013-05-24 19:25:01 +0530196 item.export_rate = flt(item.ref_rate * (1.0 - (item.adj_rate / 100.0)),
197 self.precision("export_rate", item))
Anand Doshi21f4ea32013-05-10 18:08:32 +0530198
199 item.export_amount = flt(item.export_rate * item.qty,
Anand Doshi39384d32013-05-11 19:39:53 +0530200 self.precision("export_amount", item))
Rushabh Mehta4dca4012013-07-25 17:45:59 +0530201
Anand Doshi3543f302013-05-24 19:25:01 +0530202 self._set_in_company_currency(item, "ref_rate", "base_ref_rate")
203 self._set_in_company_currency(item, "export_rate", "basic_rate")
204 self._set_in_company_currency(item, "export_amount", "amount")
Anand Doshi21f4ea32013-05-10 18:08:32 +0530205
206 def calculate_net_total(self):
Anand Doshif3096132013-05-21 19:35:06 +0530207 self.doc.net_total = self.doc.net_total_export = 0.0
Anand Doshi21f4ea32013-05-10 18:08:32 +0530208
209 for item in self.item_doclist:
210 self.doc.net_total += item.amount
211 self.doc.net_total_export += item.export_amount
Anand Doshif3096132013-05-21 19:35:06 +0530212
213 self.round_floats_in(self.doc, ["net_total", "net_total_export"])
Anand Doshi21f4ea32013-05-10 18:08:32 +0530214
215 def calculate_totals(self):
216 self.doc.grand_total = flt(self.tax_doclist and \
Anand Doshi5af812a2013-05-10 19:23:02 +0530217 self.tax_doclist[-1].total or self.doc.net_total, self.precision("grand_total"))
Anand Doshi21f4ea32013-05-10 18:08:32 +0530218 self.doc.grand_total_export = flt(self.doc.grand_total / self.doc.conversion_rate,
Anand Doshi5af812a2013-05-10 19:23:02 +0530219 self.precision("grand_total_export"))
Anand Doshif3096132013-05-21 19:35:06 +0530220
221 self.doc.other_charges_total = flt(self.doc.grand_total - self.doc.net_total,
222 self.precision("other_charges_total"))
223 self.doc.other_charges_total_export = flt(self.doc.grand_total_export - self.doc.net_total_export,
224 self.precision("other_charges_total_export"))
Anand Doshi21f4ea32013-05-10 18:08:32 +0530225
Anand Doshi2e31e092013-08-19 19:58:23 +0530226 self.doc.rounded_total = _round(self.doc.grand_total)
227 self.doc.rounded_total_export = _round(self.doc.grand_total_export)
Anand Doshif3096132013-05-21 19:35:06 +0530228
Anand Doshi923d41d2013-05-28 17:23:36 +0530229 def calculate_outstanding_amount(self):
230 # NOTE:
231 # write_off_amount is only for POS Invoice
232 # total_advance is only for non POS Invoice
233 if self.doc.doctype == "Sales Invoice" and self.doc.docstatus < 2:
234 self.round_floats_in(self.doc, ["grand_total", "total_advance", "write_off_amount",
235 "paid_amount"])
236 total_amount_to_pay = self.doc.grand_total - self.doc.write_off_amount
237 self.doc.outstanding_amount = flt(total_amount_to_pay - self.doc.total_advance - self.doc.paid_amount,
238 self.precision("outstanding_amount"))
239
240 def calculate_commission(self):
241 if self.meta.get_field("commission_rate"):
242 self.round_floats_in(self.doc, ["net_total", "commission_rate"])
243 if self.doc.commission_rate > 100.0:
244 msgprint(_(self.meta.get_label("commission_rate")) + " " +
245 _("cannot be greater than 100"), raise_exception=True)
246
247 self.doc.total_commission = flt(self.doc.net_total * self.doc.commission_rate / 100.0,
248 self.precision("total_commission"))
Anand Doshif3096132013-05-21 19:35:06 +0530249
250 def calculate_contribution(self):
251 total = 0.0
252 sales_team = self.doclist.get({"parentfield": "sales_team"})
253 for sales_person in sales_team:
254 self.round_floats_in(sales_person)
255
256 sales_person.allocated_amount = flt(
257 self.doc.net_total * sales_person.allocated_percentage / 100.0,
258 self.precision("allocated_amount", sales_person))
259
260 total += sales_person.allocated_percentage
261
262 if sales_team and total != 100.0:
263 msgprint(_("Total") + " " +
264 _(self.meta.get_label("allocated_percentage", parentfield="sales_team")) +
265 " " + _("should be 100%"), raise_exception=True)
Anand Doshi1dde46a2013-05-15 21:15:57 +0530266
267 def validate_order_type(self):
Anand Doshiabc10032013-06-14 17:44:03 +0530268 valid_types = ["Sales", "Maintenance", "Shopping Cart"]
Rushabh Mehta080fcc82013-07-04 15:27:04 +0530269 if not self.doc.order_type:
270 self.doc.order_type = "Sales"
271 elif self.doc.order_type not in valid_types:
Anand Doshi1dde46a2013-05-15 21:15:57 +0530272 msgprint(_(self.meta.get_label("order_type")) + " " +
273 _("must be one of") + ": " + comma_or(valid_types),
274 raise_exception=True)
Rushabh Mehta62030e02013-08-14 18:37:28 +0530275
276 def update_serial_nos(self, cancel=False):
277 from stock.doctype.stock_ledger_entry.stock_ledger_entry import update_serial_nos_after_submit, get_serial_nos
278 update_serial_nos_after_submit(self, self.doc.doctype, self.fname)
279 update_serial_nos_after_submit(self, self.doc.doctype, "packing_details")
280
281 for table_fieldname in (self.fname, "packing_details"):
282 for d in self.doclist.get({"parentfield": table_fieldname}):
283 for serial_no in get_serial_nos(d.serial_no):
284 sr = webnotes.bean("Serial No", serial_no)
285 if cancel:
286 sr.doc.status = "Available"
287 for fieldname in ("warranty_expiry_date", "delivery_document_type",
288 "delivery_document_no", "delivery_date", "delivery_time", "customer",
289 "customer_name"):
290 sr.doc.fields[fieldname] = None
291 else:
292 sr.doc.delivery_document_type = self.doc.doctype
293 sr.doc.delivery_document_no = self.doc.name
294 sr.doc.delivery_date = self.doc.posting_date
295 sr.doc.delivery_time = self.doc.posting_time
296 sr.doc.customer = self.doc.customer
297 sr.doc.customer_name = self.doc.customer_name
298 if sr.doc.warranty_period:
Nabin Haite98ab7a2013-08-21 16:56:12 +0530299 sr.doc.warranty_expiry_date = add_days(cstr(self.doc.posting_date),
Rushabh Mehta62030e02013-08-14 18:37:28 +0530300 cint(sr.doc.warranty_period))
301 sr.doc.status = 'Delivered'
302
303 sr.save()