blob: 80bb9143a12a089e32ff95aa004afc69d3d71f68 [file] [log] [blame]
Nabin Haitec52db82013-01-22 11:53:14 +05301# ERPNext - web based ERP (http://erpnext.com)
2# Copyright (C) 2012 Web Notes Technologies Pvt Ltd
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17from __future__ import unicode_literals
18import webnotes
Anand Doshif3096132013-05-21 19:35:06 +053019from webnotes.utils import cint, flt, comma_or
Nabin Haitec52db82013-01-22 11:53:14 +053020from setup.utils import get_company_currency
Anand Doshi3543f302013-05-24 19:25:01 +053021from selling.utils import get_item_details
Nabin Hait0fc24542013-03-25 11:06:00 +053022from webnotes import msgprint, _
Nabin Haitec52db82013-01-22 11:53:14 +053023
Nabin Haitc3afb252013-03-19 12:01:24 +053024from controllers.stock_controller import StockController
Nabin Haitbf495c92013-01-30 12:49:08 +053025
Nabin Haitc3afb252013-03-19 12:01:24 +053026class SellingController(StockController):
Anand Doshif3096132013-05-21 19:35:06 +053027 def onload_post_render(self):
Anand Doshif3096132013-05-21 19:35:06 +053028 # contact, address, item details and pos details (if applicable)
29 self.set_missing_values()
30
Anand Doshi99100a42013-07-04 17:13:53 +053031 self.set_taxes("other_charges", "charge")
Anand Doshif3096132013-05-21 19:35:06 +053032
Anand Doshif3096132013-05-21 19:35:06 +053033 def set_missing_values(self, for_validate=False):
Anand Doshiabc10032013-06-14 17:44:03 +053034 super(SellingController, self).set_missing_values(for_validate)
35
Anand Doshif3096132013-05-21 19:35:06 +053036 # set contact and address details for customer, if they are not mentioned
Anand Doshiabc10032013-06-14 17:44:03 +053037 self.set_missing_lead_customer_details()
Anand Doshi99100a42013-07-04 17:13:53 +053038
39 self.set_price_list_and_item_details()
Anand Doshiabc10032013-06-14 17:44:03 +053040
41 def set_missing_lead_customer_details(self):
42 if self.doc.customer:
Anand Doshi99100a42013-07-04 17:13:53 +053043 if not (self.doc.contact_person and self.doc.customer_address and self.doc.customer_name):
44 for fieldname, val in self.get_customer_defaults().items():
Anand Doshiabc10032013-06-14 17:44:03 +053045 if not self.doc.fields.get(fieldname) and self.meta.get_field(fieldname):
46 self.doc.fields[fieldname] = val
47
Anand Doshiabc10032013-06-14 17:44:03 +053048 elif self.doc.lead:
Anand Doshi99100a42013-07-04 17:13:53 +053049 if not (self.doc.customer_address and self.doc.customer_name and \
50 self.doc.contact_display):
51 for fieldname, val in self.get_lead_defaults().items():
52 if not self.doc.fields.get(fieldname) and self.meta.get_field(fieldname):
53 self.doc.fields[fieldname] = val
54
55 def set_price_list_and_item_details(self):
56 self.set_price_list_currency("Selling")
57 self.set_missing_item_details(get_item_details)
Nabin Hait2bd37772013-07-08 19:00:29 +053058
Anand Doshif3096132013-05-21 19:35:06 +053059 def get_other_charges(self):
60 self.doclist = self.doc.clear_table(self.doclist, "other_charges")
Anand Doshi99100a42013-07-04 17:13:53 +053061 self.set_taxes("other_charges", "charge")
Anand Doshif3096132013-05-21 19:35:06 +053062
Anand Doshi99100a42013-07-04 17:13:53 +053063 def apply_shipping_rule(self):
64 if self.doc.shipping_rule:
65 shipping_rule = webnotes.bean("Shipping Rule", self.doc.shipping_rule)
66 value = self.doc.net_total
67
68 # TODO
69 # shipping rule calculation based on item's net weight
70
71 shipping_amount = 0.0
72 for condition in shipping_rule.doclist.get({"parentfield": "shipping_rule_conditions"}):
73 if not condition.to_value or (flt(condition.from_value) <= value <= flt(condition.to_value)):
74 shipping_amount = condition.shipping_amount
75 break
Anand Doshi0b4943c2013-07-04 23:45:22 +053076
77 self.doclist.append({
78 "doctype": "Sales Taxes and Charges",
79 "parentfield": "other_charges",
80 "charge_type": "Actual",
81 "account_head": shipping_rule.doc.account,
82 "cost_center": shipping_rule.doc.cost_center,
83 "description": shipping_rule.doc.label,
84 "rate": shipping_amount
85 })
Anand Doshifc777182013-05-27 19:29:07 +053086
Nabin Haitec52db82013-01-22 11:53:14 +053087 def set_total_in_words(self):
88 from webnotes.utils import money_in_words
89 company_currency = get_company_currency(self.doc.company)
Nabin Hait6f1a5ec2013-02-20 16:25:05 +053090
91 disable_rounded_total = cint(webnotes.conn.get_value("Global Defaults", None,
92 "disable_rounded_total"))
93
Nabin Haitec52db82013-01-22 11:53:14 +053094 if self.meta.get_field("in_words"):
Nabin Hait6f1a5ec2013-02-20 16:25:05 +053095 self.doc.in_words = money_in_words(disable_rounded_total and
96 self.doc.grand_total or self.doc.rounded_total, company_currency)
Nabin Haitec52db82013-01-22 11:53:14 +053097 if self.meta.get_field("in_words_export"):
Nabin Hait6f1a5ec2013-02-20 16:25:05 +053098 self.doc.in_words_export = money_in_words(disable_rounded_total and
Nabin Hait8c7234f2013-03-11 16:32:33 +053099 self.doc.grand_total_export or self.doc.rounded_total_export, self.doc.currency)
Nabin Haita0e7c152013-03-21 18:37:06 +0530100
Nabin Hait787c02e2013-03-29 16:42:33 +0530101 def set_buying_amount(self, stock_ledger_entries = None):
Anand Doshi8c5844e2013-03-21 20:24:10 +0530102 from stock.utils import get_buying_amount
Nabin Hait787c02e2013-03-29 16:42:33 +0530103 if not stock_ledger_entries:
104 stock_ledger_entries = self.get_stock_ledger_entries()
Anand Doshi8c5844e2013-03-21 20:24:10 +0530105
106 item_sales_bom = {}
107 for d in self.doclist.get({"parentfield": "packing_details"}):
108 new_d = webnotes._dict(d.fields.copy())
109 new_d.total_qty = -1 * d.qty
110 item_sales_bom.setdefault(d.parent_item, []).append(new_d)
Nabin Haita0e7c152013-03-21 18:37:06 +0530111
112 if stock_ledger_entries:
113 for item in self.doclist.get({"parentfield": self.fname}):
114 if item.item_code in self.stock_items or \
115 (item_sales_bom and item_sales_bom.get(item.item_code)):
116 buying_amount = get_buying_amount(item.item_code, item.warehouse, -1*item.qty,
117 self.doc.doctype, self.doc.name, item.name, stock_ledger_entries,
118 item_sales_bom)
Nabin Haitf2d4df92013-05-07 13:12:02 +0530119
120 item.buying_amount = buying_amount >= 0.01 and buying_amount or 0
Nabin Hait0fc24542013-03-25 11:06:00 +0530121 webnotes.conn.set_value(item.doctype, item.name, "buying_amount",
122 item.buying_amount)
123
124 def check_expense_account(self, item):
125 if item.buying_amount and not item.expense_account:
126 msgprint(_("""Expense account is mandatory for item: """) + item.item_code,
Nabin Hait787c02e2013-03-29 16:42:33 +0530127 raise_exception=1)
128
129 if item.buying_amount and not item.cost_center:
130 msgprint(_("""Cost Center is mandatory for item: """) + item.item_code,
Anand Doshi21f4ea32013-05-10 18:08:32 +0530131 raise_exception=1)
132
133 def calculate_taxes_and_totals(self):
Anand Doshi3543f302013-05-24 19:25:01 +0530134 self.other_fname = "other_charges"
Anand Doshi21f4ea32013-05-10 18:08:32 +0530135
Anand Doshi3543f302013-05-24 19:25:01 +0530136 super(SellingController, self).calculate_taxes_and_totals()
137
Anand Doshi923d41d2013-05-28 17:23:36 +0530138 self.calculate_total_advance("Sales Invoice", "advance_adjustment_details")
Anand Doshif3096132013-05-21 19:35:06 +0530139 self.calculate_commission()
140 self.calculate_contribution()
Anand Doshi3543f302013-05-24 19:25:01 +0530141
Anand Doshif3096132013-05-21 19:35:06 +0530142 def determine_exclusive_rate(self):
Anand Doshi21f4ea32013-05-10 18:08:32 +0530143 if not any((cint(tax.included_in_print_rate) for tax in self.tax_doclist)):
144 # no inclusive tax
145 return
146
147 for item in self.item_doclist:
148 item_tax_map = self._load_item_tax_rate(item.item_tax_rate)
149 cumulated_tax_fraction = 0
150 for i, tax in enumerate(self.tax_doclist):
Anand Doshif3096132013-05-21 19:35:06 +0530151 tax.tax_fraction_for_current_item = self.get_current_tax_fraction(tax, item_tax_map)
152
Anand Doshi21f4ea32013-05-10 18:08:32 +0530153 if i==0:
Anand Doshif3096132013-05-21 19:35:06 +0530154 tax.grand_total_fraction_for_current_item = 1 + tax.tax_fraction_for_current_item
Anand Doshi21f4ea32013-05-10 18:08:32 +0530155 else:
156 tax.grand_total_fraction_for_current_item = \
157 self.tax_doclist[i-1].grand_total_fraction_for_current_item \
158 + tax.tax_fraction_for_current_item
159
160 cumulated_tax_fraction += tax.tax_fraction_for_current_item
161
162 if cumulated_tax_fraction:
163 item.basic_rate = flt((item.export_rate * self.doc.conversion_rate) /
Anand Doshi39384d32013-05-11 19:39:53 +0530164 (1 + cumulated_tax_fraction), self.precision("basic_rate", item))
Anand Doshi21f4ea32013-05-10 18:08:32 +0530165
Anand Doshi39384d32013-05-11 19:39:53 +0530166 item.amount = flt(item.basic_rate * item.qty, self.precision("amount", item))
Anand Doshi21f4ea32013-05-10 18:08:32 +0530167
Anand Doshif3096132013-05-21 19:35:06 +0530168 if item.adj_rate == 100:
169 item.base_ref_rate = item.basic_rate
170 item.basic_rate = 0.0
171 else:
172 item.base_ref_rate = flt(item.basic_rate / (1 - (item.adj_rate / 100.0)),
173 self.precision("base_ref_rate", item))
Anand Doshi21f4ea32013-05-10 18:08:32 +0530174
175 def get_current_tax_fraction(self, tax, item_tax_map):
176 """
177 Get tax fraction for calculating tax exclusive amount
178 from tax inclusive amount
179 """
180 current_tax_fraction = 0
181
182 if cint(tax.included_in_print_rate):
183 tax_rate = self._get_tax_rate(tax, item_tax_map)
184
185 if tax.charge_type == "On Net Total":
186 current_tax_fraction = tax_rate / 100.0
187
188 elif tax.charge_type == "On Previous Row Amount":
189 current_tax_fraction = (tax_rate / 100.0) * \
190 self.tax_doclist[cint(tax.row_id) - 1].tax_fraction_for_current_item
191
192 elif tax.charge_type == "On Previous Row Total":
193 current_tax_fraction = (tax_rate / 100.0) * \
194 self.tax_doclist[cint(tax.row_id) - 1].grand_total_fraction_for_current_item
195
196 return current_tax_fraction
197
198 def calculate_item_values(self):
Anand Doshi21f4ea32013-05-10 18:08:32 +0530199 for item in self.item_doclist:
Anand Doshi39384d32013-05-11 19:39:53 +0530200 self.round_floats_in(item)
Anand Doshi21f4ea32013-05-10 18:08:32 +0530201
202 if item.adj_rate == 100:
Anand Doshi21f4ea32013-05-10 18:08:32 +0530203 item.export_rate = 0
Anand Doshi3543f302013-05-24 19:25:01 +0530204 elif item.ref_rate:
205 item.export_rate = flt(item.ref_rate * (1.0 - (item.adj_rate / 100.0)),
206 self.precision("export_rate", item))
Anand Doshi21f4ea32013-05-10 18:08:32 +0530207
208 item.export_amount = flt(item.export_rate * item.qty,
Anand Doshi39384d32013-05-11 19:39:53 +0530209 self.precision("export_amount", item))
Anand Doshi21f4ea32013-05-10 18:08:32 +0530210
Anand Doshi3543f302013-05-24 19:25:01 +0530211 self._set_in_company_currency(item, "ref_rate", "base_ref_rate")
212 self._set_in_company_currency(item, "export_rate", "basic_rate")
213 self._set_in_company_currency(item, "export_amount", "amount")
Anand Doshi21f4ea32013-05-10 18:08:32 +0530214
215 def calculate_net_total(self):
Anand Doshif3096132013-05-21 19:35:06 +0530216 self.doc.net_total = self.doc.net_total_export = 0.0
Anand Doshi21f4ea32013-05-10 18:08:32 +0530217
218 for item in self.item_doclist:
219 self.doc.net_total += item.amount
220 self.doc.net_total_export += item.export_amount
Anand Doshif3096132013-05-21 19:35:06 +0530221
222 self.round_floats_in(self.doc, ["net_total", "net_total_export"])
Anand Doshi21f4ea32013-05-10 18:08:32 +0530223
224 def calculate_totals(self):
225 self.doc.grand_total = flt(self.tax_doclist and \
Anand Doshi5af812a2013-05-10 19:23:02 +0530226 self.tax_doclist[-1].total or self.doc.net_total, self.precision("grand_total"))
Anand Doshi21f4ea32013-05-10 18:08:32 +0530227 self.doc.grand_total_export = flt(self.doc.grand_total / self.doc.conversion_rate,
Anand Doshi5af812a2013-05-10 19:23:02 +0530228 self.precision("grand_total_export"))
Anand Doshif3096132013-05-21 19:35:06 +0530229
230 self.doc.other_charges_total = flt(self.doc.grand_total - self.doc.net_total,
231 self.precision("other_charges_total"))
232 self.doc.other_charges_total_export = flt(self.doc.grand_total_export - self.doc.net_total_export,
233 self.precision("other_charges_total_export"))
Anand Doshi21f4ea32013-05-10 18:08:32 +0530234
235 self.doc.rounded_total = round(self.doc.grand_total)
236 self.doc.rounded_total_export = round(self.doc.grand_total_export)
Anand Doshif3096132013-05-21 19:35:06 +0530237
Anand Doshi923d41d2013-05-28 17:23:36 +0530238 def calculate_outstanding_amount(self):
239 # NOTE:
240 # write_off_amount is only for POS Invoice
241 # total_advance is only for non POS Invoice
242 if self.doc.doctype == "Sales Invoice" and self.doc.docstatus < 2:
243 self.round_floats_in(self.doc, ["grand_total", "total_advance", "write_off_amount",
244 "paid_amount"])
245 total_amount_to_pay = self.doc.grand_total - self.doc.write_off_amount
246 self.doc.outstanding_amount = flt(total_amount_to_pay - self.doc.total_advance - self.doc.paid_amount,
247 self.precision("outstanding_amount"))
248
249 def calculate_commission(self):
250 if self.meta.get_field("commission_rate"):
251 self.round_floats_in(self.doc, ["net_total", "commission_rate"])
252 if self.doc.commission_rate > 100.0:
253 msgprint(_(self.meta.get_label("commission_rate")) + " " +
254 _("cannot be greater than 100"), raise_exception=True)
255
256 self.doc.total_commission = flt(self.doc.net_total * self.doc.commission_rate / 100.0,
257 self.precision("total_commission"))
Anand Doshif3096132013-05-21 19:35:06 +0530258
259 def calculate_contribution(self):
260 total = 0.0
261 sales_team = self.doclist.get({"parentfield": "sales_team"})
262 for sales_person in sales_team:
263 self.round_floats_in(sales_person)
264
265 sales_person.allocated_amount = flt(
266 self.doc.net_total * sales_person.allocated_percentage / 100.0,
267 self.precision("allocated_amount", sales_person))
268
269 total += sales_person.allocated_percentage
270
271 if sales_team and total != 100.0:
272 msgprint(_("Total") + " " +
273 _(self.meta.get_label("allocated_percentage", parentfield="sales_team")) +
274 " " + _("should be 100%"), raise_exception=True)
Anand Doshi1dde46a2013-05-15 21:15:57 +0530275
276 def validate_order_type(self):
Anand Doshiabc10032013-06-14 17:44:03 +0530277 valid_types = ["Sales", "Maintenance", "Shopping Cart"]
Rushabh Mehta080fcc82013-07-04 15:27:04 +0530278 if not self.doc.order_type:
279 self.doc.order_type = "Sales"
280 elif self.doc.order_type not in valid_types:
Anand Doshi1dde46a2013-05-15 21:15:57 +0530281 msgprint(_(self.meta.get_label("order_type")) + " " +
282 _("must be one of") + ": " + comma_or(valid_types),
283 raise_exception=True)