blob: 937e026ef9e697e3a1847bfd92842109930a76d0 [file] [log] [blame]
Rushabh Mehtaad45e312013-11-20 12:59:58 +05301# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302# License: GNU General Public License v3. See license.txt
Nabin Haitec52db82013-01-22 11:53:14 +05303
4from __future__ import unicode_literals
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05305import frappe
Anand Doshi62b1cbf2014-07-25 12:50:00 +05306from frappe.utils import cint, flt, rounded, cstr, comma_or
Rushabh Mehta1f847992013-12-12 19:12:19 +05307from erpnext.setup.utils import get_company_currency
Rushabh Mehta9f0d6252014-04-14 19:20:45 +05308from frappe import _, throw
Rushabh Mehtab5e76892014-07-29 16:07:43 +05309from erpnext.stock.get_item_details import get_available_qty
Nabin Haitec52db82013-01-22 11:53:14 +053010
Rushabh Mehta1f847992013-12-12 19:12:19 +053011from erpnext.controllers.stock_controller import StockController
Nabin Haitbf495c92013-01-30 12:49:08 +053012
Nabin Haitc3afb252013-03-19 12:01:24 +053013class SellingController(StockController):
Rushabh Mehtaf84c2402014-07-18 16:39:31 +053014 def __setup__(self):
Nabin Haitdd38a262014-12-26 13:15:21 +053015 if hasattr(self, "items"):
Rushabh Mehta5b51cc82014-07-21 18:25:45 +053016 self.table_print_templates = {
Nabin Haitdd38a262014-12-26 13:15:21 +053017 "items": "templates/print_formats/includes/item_grid.html",
Nabin Haite7d15362014-12-25 16:01:55 +053018 "taxes": "templates/print_formats/includes/taxes.html",
Rushabh Mehta5b51cc82014-07-21 18:25:45 +053019 }
Rushabh Mehtaf84c2402014-07-18 16:39:31 +053020
Rushabh Mehtac0bb4532014-09-09 16:15:35 +053021 def get_feed(self):
22 return _("To {0} | {1} {2}").format(self.customer_name, self.currency,
23 self.grand_total_export)
24
Rushabh Mehtab5e76892014-07-29 16:07:43 +053025 def onload(self):
26 if self.doctype in ("Sales Order", "Delivery Note", "Sales Invoice"):
Nabin Haitdd38a262014-12-26 13:15:21 +053027 for item in self.get("items"):
Rushabh Mehtab5e76892014-07-29 16:07:43 +053028 item.update(get_available_qty(item.item_code,
29 item.warehouse))
30
Nabin Haitd1fd1e22013-10-18 12:29:11 +053031 def validate(self):
32 super(SellingController, self).validate()
33 self.validate_max_discount()
34 check_active_sales_items(self)
Anand Doshid2946502014-04-08 20:10:03 +053035
Nabin Haite9daefe2014-08-27 16:46:33 +053036 def check_credit_limit(self):
37 from erpnext.selling.doctype.customer.customer import check_credit_limit
38 check_credit_limit(self.customer, self.company)
39
Anand Doshif3096132013-05-21 19:35:06 +053040 def set_missing_values(self, for_validate=False):
Anand Doshiabc10032013-06-14 17:44:03 +053041 super(SellingController, self).set_missing_values(for_validate)
Anand Doshid2946502014-04-08 20:10:03 +053042
Anand Doshif3096132013-05-21 19:35:06 +053043 # set contact and address details for customer, if they are not mentioned
Anand Doshiabc10032013-06-14 17:44:03 +053044 self.set_missing_lead_customer_details()
Nabin Hait096d3632013-10-17 17:01:14 +053045 self.set_price_list_and_item_details()
Anand Doshif78d1ae2014-03-28 13:55:00 +053046 if self.get("__islocal"):
Nabin Haite7d15362014-12-25 16:01:55 +053047 self.set_taxes("taxes", "taxes_and_charges")
Anand Doshid2946502014-04-08 20:10:03 +053048
Anand Doshiabc10032013-06-14 17:44:03 +053049 def set_missing_lead_customer_details(self):
Rushabh Mehtaf14b8092014-04-03 14:30:42 +053050 if getattr(self, "customer", None):
Anand Doshi43624432014-02-25 15:42:16 +053051 from erpnext.accounts.party import _get_party_details
Nabin Hait6b039142014-05-02 15:45:10 +053052 party_details = _get_party_details(self.customer,
53 ignore_permissions=getattr(self, "ignore_permissions", None))
54 if not self.meta.get_field("sales_team"):
55 party_details.pop("sales_team")
56
57 self.update_if_missing(party_details)
Anand Doshid2946502014-04-08 20:10:03 +053058
Rushabh Mehtaf14b8092014-04-03 14:30:42 +053059 elif getattr(self, "lead", None):
Nabin Hait9d1f0772014-02-19 17:43:24 +053060 from erpnext.selling.doctype.lead.lead import get_lead_details
Anand Doshif78d1ae2014-03-28 13:55:00 +053061 self.update_if_missing(get_lead_details(self.lead))
Anand Doshid2946502014-04-08 20:10:03 +053062
Nabin Hait096d3632013-10-17 17:01:14 +053063 def set_price_list_and_item_details(self):
64 self.set_price_list_currency("Selling")
Nabin Hait0aa71a52014-02-11 16:14:52 +053065 self.set_missing_item_details()
Anand Doshid2946502014-04-08 20:10:03 +053066
Anand Doshi99100a42013-07-04 17:13:53 +053067 def apply_shipping_rule(self):
Anand Doshif78d1ae2014-03-28 13:55:00 +053068 if self.shipping_rule:
Rushabh Mehtab385ecf2014-03-28 16:44:37 +053069 shipping_rule = frappe.get_doc("Shipping Rule", self.shipping_rule)
Anand Doshif78d1ae2014-03-28 13:55:00 +053070 value = self.net_total
Anand Doshid2946502014-04-08 20:10:03 +053071
Anand Doshi99100a42013-07-04 17:13:53 +053072 # TODO
73 # shipping rule calculation based on item's net weight
Anand Doshid2946502014-04-08 20:10:03 +053074
Anand Doshi99100a42013-07-04 17:13:53 +053075 shipping_amount = 0.0
Nabin Haite7d15362014-12-25 16:01:55 +053076 for condition in shipping_rule.get("conditions"):
Anand Doshi99100a42013-07-04 17:13:53 +053077 if not condition.to_value or (flt(condition.from_value) <= value <= flt(condition.to_value)):
78 shipping_amount = condition.shipping_amount
79 break
Anand Doshid2946502014-04-08 20:10:03 +053080
Anand Doshiac32bad2014-04-18 01:30:14 +053081 shipping_charge = {
Anand Doshi0b4943c2013-07-04 23:45:22 +053082 "doctype": "Sales Taxes and Charges",
Anand Doshi0b4943c2013-07-04 23:45:22 +053083 "charge_type": "Actual",
Anand Doshif78d1ae2014-03-28 13:55:00 +053084 "account_head": shipping_rule.account,
Anand Doshiac32bad2014-04-18 01:30:14 +053085 "cost_center": shipping_rule.cost_center
86 }
87
Nabin Haite7d15362014-12-25 16:01:55 +053088 existing_shipping_charge = self.get("taxes", filters=shipping_charge)
Anand Doshiac32bad2014-04-18 01:30:14 +053089 if existing_shipping_charge:
90 # take the last record found
91 existing_shipping_charge[-1].rate = shipping_amount
92 else:
93 shipping_charge["rate"] = shipping_amount
94 shipping_charge["description"] = shipping_rule.label
Nabin Haite7d15362014-12-25 16:01:55 +053095 self.append("taxes", shipping_charge)
Anand Doshiac32bad2014-04-18 01:30:14 +053096
97 self.calculate_taxes_and_totals()
98
99 def remove_shipping_charge(self):
100 if self.shipping_rule:
101 shipping_rule = frappe.get_doc("Shipping Rule", self.shipping_rule)
Nabin Haite7d15362014-12-25 16:01:55 +0530102 existing_shipping_charge = self.get("taxes", {
Anand Doshiac32bad2014-04-18 01:30:14 +0530103 "doctype": "Sales Taxes and Charges",
104 "charge_type": "Actual",
105 "account_head": shipping_rule.account,
106 "cost_center": shipping_rule.cost_center
Anand Doshi0b4943c2013-07-04 23:45:22 +0530107 })
Anand Doshiac32bad2014-04-18 01:30:14 +0530108 if existing_shipping_charge:
Nabin Haite7d15362014-12-25 16:01:55 +0530109 self.get("taxes").remove(existing_shipping_charge[-1])
Anand Doshiac32bad2014-04-18 01:30:14 +0530110 self.calculate_taxes_and_totals()
Anand Doshid2946502014-04-08 20:10:03 +0530111
Nabin Haitec52db82013-01-22 11:53:14 +0530112 def set_total_in_words(self):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530113 from frappe.utils import money_in_words
Anand Doshif78d1ae2014-03-28 13:55:00 +0530114 company_currency = get_company_currency(self.company)
Anand Doshid2946502014-04-08 20:10:03 +0530115
116 disable_rounded_total = cint(frappe.db.get_value("Global Defaults", None,
Nabin Hait6f1a5ec2013-02-20 16:25:05 +0530117 "disable_rounded_total"))
Anand Doshid2946502014-04-08 20:10:03 +0530118
Nabin Haitec52db82013-01-22 11:53:14 +0530119 if self.meta.get_field("in_words"):
Anand Doshid2946502014-04-08 20:10:03 +0530120 self.in_words = money_in_words(disable_rounded_total and
Anand Doshif78d1ae2014-03-28 13:55:00 +0530121 self.grand_total or self.rounded_total, company_currency)
Nabin Haitec52db82013-01-22 11:53:14 +0530122 if self.meta.get_field("in_words_export"):
Anand Doshid2946502014-04-08 20:10:03 +0530123 self.in_words_export = money_in_words(disable_rounded_total and
Anand Doshif78d1ae2014-03-28 13:55:00 +0530124 self.grand_total_export or self.rounded_total_export, self.currency)
Anand Doshid2946502014-04-08 20:10:03 +0530125
Anand Doshi21f4ea32013-05-10 18:08:32 +0530126 def calculate_taxes_and_totals(self):
Anand Doshi3543f302013-05-24 19:25:01 +0530127 super(SellingController, self).calculate_taxes_and_totals()
Anand Doshid2946502014-04-08 20:10:03 +0530128
Nabin Haite7d15362014-12-25 16:01:55 +0530129 self.calculate_total_advance("Sales Invoice", "advances")
Anand Doshif3096132013-05-21 19:35:06 +0530130 self.calculate_commission()
131 self.calculate_contribution()
Anand Doshid2946502014-04-08 20:10:03 +0530132
Anand Doshif3096132013-05-21 19:35:06 +0530133 def determine_exclusive_rate(self):
Nabin Haitdd38a262014-12-26 13:15:21 +0530134 if not any((cint(tax.included_in_print_rate) for tax in self.get("taxes"))):
Anand Doshi21f4ea32013-05-10 18:08:32 +0530135 # no inclusive tax
136 return
Anand Doshid2946502014-04-08 20:10:03 +0530137
Nabin Haitdd38a262014-12-26 13:15:21 +0530138 for item in self.get("items"):
Anand Doshi21f4ea32013-05-10 18:08:32 +0530139 item_tax_map = self._load_item_tax_rate(item.item_tax_rate)
140 cumulated_tax_fraction = 0
Nabin Haitdd38a262014-12-26 13:15:21 +0530141 for i, tax in enumerate(self.get("taxes")):
Anand Doshif3096132013-05-21 19:35:06 +0530142 tax.tax_fraction_for_current_item = self.get_current_tax_fraction(tax, item_tax_map)
Anand Doshid2946502014-04-08 20:10:03 +0530143
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 = \
Nabin Haitdd38a262014-12-26 13:15:21 +0530148 self.get("taxes")[i-1].grand_total_fraction_for_current_item \
Anand Doshi21f4ea32013-05-10 18:08:32 +0530149 + tax.tax_fraction_for_current_item
Anand Doshid2946502014-04-08 20:10:03 +0530150
Anand Doshi21f4ea32013-05-10 18:08:32 +0530151 cumulated_tax_fraction += tax.tax_fraction_for_current_item
Anand Doshid2946502014-04-08 20:10:03 +0530152
Nabin Haitc41a9482014-08-18 15:40:15 +0530153 if cumulated_tax_fraction and not self.discount_amount_applied and item.qty:
Anand Doshif78d1ae2014-03-28 13:55:00 +0530154 item.base_amount = flt((item.amount * self.conversion_rate) /
Nabin Hait1eb56012014-02-10 19:20:15 +0530155 (1 + cumulated_tax_fraction), self.precision("base_amount", item))
Anand Doshid2946502014-04-08 20:10:03 +0530156
Nabin Hait1eb56012014-02-10 19:20:15 +0530157 item.base_rate = flt(item.base_amount / item.qty, self.precision("base_rate", item))
Anand Doshi8f49cf72014-07-25 13:01:00 +0530158 item.discount_percentage = flt(item.discount_percentage, self.precision("discount_percentage", item))
Anand Doshid2946502014-04-08 20:10:03 +0530159
Nabin Haita7f757a2014-02-10 17:54:04 +0530160 if item.discount_percentage == 100:
Nabin Hait7979f7e2014-02-10 18:26:49 +0530161 item.base_price_list_rate = item.base_rate
162 item.base_rate = 0.0
Anand Doshif3096132013-05-21 19:35:06 +0530163 else:
Nabin Hait7979f7e2014-02-10 18:26:49 +0530164 item.base_price_list_rate = flt(item.base_rate / (1 - (item.discount_percentage / 100.0)),
Nabin Haita7f757a2014-02-10 17:54:04 +0530165 self.precision("base_price_list_rate", item))
Anand Doshid2946502014-04-08 20:10:03 +0530166
Anand Doshi21f4ea32013-05-10 18:08:32 +0530167 def get_current_tax_fraction(self, tax, item_tax_map):
168 """
169 Get tax fraction for calculating tax exclusive amount
170 from tax inclusive amount
171 """
172 current_tax_fraction = 0
Anand Doshid2946502014-04-08 20:10:03 +0530173
Anand Doshi21f4ea32013-05-10 18:08:32 +0530174 if cint(tax.included_in_print_rate):
175 tax_rate = self._get_tax_rate(tax, item_tax_map)
Anand Doshid2946502014-04-08 20:10:03 +0530176
Anand Doshi21f4ea32013-05-10 18:08:32 +0530177 if tax.charge_type == "On Net Total":
178 current_tax_fraction = tax_rate / 100.0
Anand Doshid2946502014-04-08 20:10:03 +0530179
Anand Doshi21f4ea32013-05-10 18:08:32 +0530180 elif tax.charge_type == "On Previous Row Amount":
181 current_tax_fraction = (tax_rate / 100.0) * \
Nabin Haitdd38a262014-12-26 13:15:21 +0530182 self.get("taxes")[cint(tax.row_id) - 1].tax_fraction_for_current_item
Anand Doshid2946502014-04-08 20:10:03 +0530183
Anand Doshi21f4ea32013-05-10 18:08:32 +0530184 elif tax.charge_type == "On Previous Row Total":
185 current_tax_fraction = (tax_rate / 100.0) * \
Nabin Haitdd38a262014-12-26 13:15:21 +0530186 self.get("taxes")[cint(tax.row_id) - 1].grand_total_fraction_for_current_item
Anand Doshid2946502014-04-08 20:10:03 +0530187
Anand Doshi21f4ea32013-05-10 18:08:32 +0530188 return current_tax_fraction
Anand Doshid2946502014-04-08 20:10:03 +0530189
Anand Doshi21f4ea32013-05-10 18:08:32 +0530190 def calculate_item_values(self):
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530191 if not self.discount_amount_applied:
Nabin Haitdd38a262014-12-26 13:15:21 +0530192 for item in self.get("items"):
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530193 self.round_floats_in(item)
Rushabh Mehta4dca4012013-07-25 17:45:59 +0530194
Nabin Haita7f757a2014-02-10 17:54:04 +0530195 if item.discount_percentage == 100:
Nabin Hait7979f7e2014-02-10 18:26:49 +0530196 item.rate = 0
197 elif not item.rate:
198 item.rate = flt(item.price_list_rate * (1.0 - (item.discount_percentage / 100.0)),
199 self.precision("rate", item))
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530200
Nabin Hait1eb56012014-02-10 19:20:15 +0530201 item.amount = flt(item.rate * item.qty,
202 self.precision("amount", item))
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530203
Nabin Haita7f757a2014-02-10 17:54:04 +0530204 self._set_in_company_currency(item, "price_list_rate", "base_price_list_rate")
Nabin Hait7979f7e2014-02-10 18:26:49 +0530205 self._set_in_company_currency(item, "rate", "base_rate")
Nabin Hait1eb56012014-02-10 19:20:15 +0530206 self._set_in_company_currency(item, "amount", "base_amount")
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530207
Anand Doshi21f4ea32013-05-10 18:08:32 +0530208 def calculate_net_total(self):
Anand Doshif78d1ae2014-03-28 13:55:00 +0530209 self.net_total = self.net_total_export = 0.0
Anand Doshi21f4ea32013-05-10 18:08:32 +0530210
Nabin Haitdd38a262014-12-26 13:15:21 +0530211 for item in self.get("items"):
Anand Doshif78d1ae2014-03-28 13:55:00 +0530212 self.net_total += item.base_amount
213 self.net_total_export += item.amount
Anand Doshid2946502014-04-08 20:10:03 +0530214
Anand Doshi81ba0b22014-03-28 15:23:26 +0530215 self.round_floats_in(self, ["net_total", "net_total_export"])
Anand Doshid2946502014-04-08 20:10:03 +0530216
Anand Doshi21f4ea32013-05-10 18:08:32 +0530217 def calculate_totals(self):
Nabin Haitdd38a262014-12-26 13:15:21 +0530218 self.grand_total = flt(self.get("taxes")[-1].total if self.get("taxes") else self.net_total)
Anand Doshid2946502014-04-08 20:10:03 +0530219
Nabin Haitccd9fd32014-11-14 14:27:24 +0530220 self.grand_total_export = flt(self.grand_total / self.conversion_rate)
Nabin Hait5c6d13a2014-01-20 17:18:16 +0530221
Nabin Haitccd9fd32014-11-14 14:27:24 +0530222 self.other_charges_total = flt(self.grand_total - self.net_total, self.precision("other_charges_total"))
223
224 self.other_charges_total_export = flt(self.grand_total_export - self.net_total_export +
225 flt(self.discount_amount), self.precision("other_charges_total_export"))
226
227 self.grand_total = flt(self.grand_total, self.precision("grand_total"))
228 self.grand_total_export = flt(self.grand_total_export, self.precision("grand_total_export"))
Anand Doshid2946502014-04-08 20:10:03 +0530229
Anand Doshi62b1cbf2014-07-25 12:50:00 +0530230 self.rounded_total = rounded(self.grand_total)
231 self.rounded_total_export = rounded(self.grand_total_export)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530232
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530233 def apply_discount_amount(self):
Anand Doshif78d1ae2014-03-28 13:55:00 +0530234 if self.discount_amount:
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530235 grand_total_for_discount_amount = self.get_grand_total_for_discount_amount()
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530236
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530237 if grand_total_for_discount_amount:
238 # calculate item amount after Discount Amount
Nabin Haitdd38a262014-12-26 13:15:21 +0530239 for item in self.get("items"):
Anand Doshif78d1ae2014-03-28 13:55:00 +0530240 distributed_amount = flt(self.discount_amount) * item.base_amount / grand_total_for_discount_amount
Nabin Hait1eb56012014-02-10 19:20:15 +0530241 item.base_amount = flt(item.base_amount - distributed_amount, self.precision("base_amount", item))
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530242
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530243 self.discount_amount_applied = True
244 self._calculate_taxes_and_totals()
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530245
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530246 def get_grand_total_for_discount_amount(self):
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530247 actual_taxes_dict = {}
248
Nabin Haitdd38a262014-12-26 13:15:21 +0530249 for tax in self.get("taxes"):
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530250 if tax.charge_type == "Actual":
251 actual_taxes_dict.setdefault(tax.idx, tax.tax_amount)
252 elif tax.row_id in actual_taxes_dict:
253 actual_tax_amount = flt(actual_taxes_dict.get(tax.row_id, 0)) * \
254 flt(tax.rate) / 100
255 actual_taxes_dict.setdefault(tax.idx, actual_tax_amount)
256
Anand Doshid2946502014-04-08 20:10:03 +0530257 grand_total_for_discount_amount = flt(self.grand_total - sum(actual_taxes_dict.values()),
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530258 self.precision("grand_total"))
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530259 return grand_total_for_discount_amount
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530260
Anand Doshi923d41d2013-05-28 17:23:36 +0530261 def calculate_outstanding_amount(self):
Anand Doshid2946502014-04-08 20:10:03 +0530262 # NOTE:
Anand Doshi923d41d2013-05-28 17:23:36 +0530263 # write_off_amount is only for POS Invoice
264 # total_advance is only for non POS Invoice
Anand Doshif78d1ae2014-03-28 13:55:00 +0530265 if self.doctype == "Sales Invoice" and self.docstatus == 0:
Anand Doshi81ba0b22014-03-28 15:23:26 +0530266 self.round_floats_in(self, ["grand_total", "total_advance", "write_off_amount",
Anand Doshi923d41d2013-05-28 17:23:36 +0530267 "paid_amount"])
Anand Doshif78d1ae2014-03-28 13:55:00 +0530268 total_amount_to_pay = self.grand_total - self.write_off_amount
269 self.outstanding_amount = flt(total_amount_to_pay - self.total_advance \
270 - self.paid_amount, self.precision("outstanding_amount"))
Anand Doshid2946502014-04-08 20:10:03 +0530271
Anand Doshi923d41d2013-05-28 17:23:36 +0530272 def calculate_commission(self):
273 if self.meta.get_field("commission_rate"):
Anand Doshi81ba0b22014-03-28 15:23:26 +0530274 self.round_floats_in(self, ["net_total", "commission_rate"])
Anand Doshif78d1ae2014-03-28 13:55:00 +0530275 if self.commission_rate > 100.0:
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530276 throw(_("Commission rate cannot be greater than 100"))
Anand Doshid2946502014-04-08 20:10:03 +0530277
Anand Doshif78d1ae2014-03-28 13:55:00 +0530278 self.total_commission = flt(self.net_total * self.commission_rate / 100.0,
Anand Doshi923d41d2013-05-28 17:23:36 +0530279 self.precision("total_commission"))
Anand Doshif3096132013-05-21 19:35:06 +0530280
281 def calculate_contribution(self):
Anand Doshiac32bad2014-04-18 01:30:14 +0530282 if not self.meta.get_field("sales_team"):
283 return
284
Anand Doshif3096132013-05-21 19:35:06 +0530285 total = 0.0
Rushabh Mehtad2b34dc2014-03-27 16:12:56 +0530286 sales_team = self.get("sales_team")
Anand Doshif3096132013-05-21 19:35:06 +0530287 for sales_person in sales_team:
288 self.round_floats_in(sales_person)
289
290 sales_person.allocated_amount = flt(
Anand Doshif78d1ae2014-03-28 13:55:00 +0530291 self.net_total * sales_person.allocated_percentage / 100.0,
Anand Doshif3096132013-05-21 19:35:06 +0530292 self.precision("allocated_amount", sales_person))
Anand Doshid2946502014-04-08 20:10:03 +0530293
Anand Doshif3096132013-05-21 19:35:06 +0530294 total += sales_person.allocated_percentage
Anand Doshid2946502014-04-08 20:10:03 +0530295
Anand Doshif3096132013-05-21 19:35:06 +0530296 if sales_team and total != 100.0:
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530297 throw(_("Total allocated percentage for sales team should be 100"))
Anand Doshid2946502014-04-08 20:10:03 +0530298
Anand Doshi1dde46a2013-05-15 21:15:57 +0530299 def validate_order_type(self):
Anand Doshiabc10032013-06-14 17:44:03 +0530300 valid_types = ["Sales", "Maintenance", "Shopping Cart"]
Anand Doshif78d1ae2014-03-28 13:55:00 +0530301 if not self.order_type:
302 self.order_type = "Sales"
303 elif self.order_type not in valid_types:
Anand Doshi80a988c2014-07-07 11:35:54 +0530304 throw(_("Order Type must be one of {0}").format(comma_or(valid_types)))
Anand Doshid2946502014-04-08 20:10:03 +0530305
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530306 def validate_max_discount(self):
Nabin Haitdd38a262014-12-26 13:15:21 +0530307 for d in self.get("items"):
Anand Doshie9baaa62014-02-26 12:35:33 +0530308 discount = flt(frappe.db.get_value("Item", d.item_code, "max_discount"))
Anand Doshid2946502014-04-08 20:10:03 +0530309
Nabin Haita7f757a2014-02-10 17:54:04 +0530310 if discount and flt(d.discount_percentage) > discount:
Rushabh Mehtac38fc712014-04-16 17:21:25 +0530311 frappe.throw(_("Maxiumm discount for Item {0} is {1}%").format(d.item_code, discount))
Anand Doshid2946502014-04-08 20:10:03 +0530312
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530313 def get_item_list(self):
314 il = []
Nabin Haitdd38a262014-12-26 13:15:21 +0530315 for d in self.get("items"):
Nabin Hait0aa71a52014-02-11 16:14:52 +0530316 reserved_warehouse = ""
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530317 reserved_qty_for_main_item = 0
Anand Doshid2946502014-04-08 20:10:03 +0530318
nathando247e9ff2014-09-03 15:03:31 +0800319 if d.qty is None:
Nabin Haitc41a9482014-08-18 15:40:15 +0530320 frappe.throw(_("Row {0}: Qty is mandatory").format(d.idx))
321
Anand Doshif78d1ae2014-03-28 13:55:00 +0530322 if self.doctype == "Sales Order":
Anand Doshid2946502014-04-08 20:10:03 +0530323 if (frappe.db.get_value("Item", d.item_code, "is_stock_item") == 'Yes' or
Nabin Haita7f757a2014-02-10 17:54:04 +0530324 self.has_sales_bom(d.item_code)) and not d.warehouse:
Nabin Hait2cebf9b2014-05-02 16:12:07 +0530325 frappe.throw(_("Reserved Warehouse required for stock Item {0} in row {1}").format(d.item_code, d.idx))
Nabin Hait0aa71a52014-02-11 16:14:52 +0530326 reserved_warehouse = d.warehouse
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530327 if flt(d.qty) > flt(d.delivered_qty):
328 reserved_qty_for_main_item = flt(d.qty) - flt(d.delivered_qty)
Anand Doshid2946502014-04-08 20:10:03 +0530329
330 elif self.doctype == "Delivery Note" and d.against_sales_order:
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530331 # if SO qty is 10 and there is tolerance of 20%, then it will allow DN of 12.
332 # But in this case reserved qty should only be reduced by 10 and not 12
Anand Doshid2946502014-04-08 20:10:03 +0530333
334 already_delivered_qty = self.get_already_delivered_qty(self.name,
ankitjavalkarworke69a6112014-10-08 12:46:02 +0530335 d.against_sales_order, d.so_detail)
336 so_qty, reserved_warehouse = self.get_so_qty_and_warehouse(d.so_detail)
Anand Doshid2946502014-04-08 20:10:03 +0530337
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530338 if already_delivered_qty + d.qty > so_qty:
339 reserved_qty_for_main_item = -(so_qty - already_delivered_qty)
340 else:
341 reserved_qty_for_main_item = -flt(d.qty)
342
343 if self.has_sales_bom(d.item_code):
Nabin Haite7d15362014-12-25 16:01:55 +0530344 for p in self.get("packed_items"):
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530345 if p.parent_detail_docname == d.name and p.parent_item == d.item_code:
346 # the packing details table's qty is already multiplied with parent's qty
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530347 il.append(frappe._dict({
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530348 'warehouse': p.warehouse,
Nabin Hait0aa71a52014-02-11 16:14:52 +0530349 'reserved_warehouse': reserved_warehouse,
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530350 'item_code': p.item_code,
351 'qty': flt(p.qty),
352 'reserved_qty': (flt(p.qty)/flt(d.qty)) * reserved_qty_for_main_item,
353 'uom': p.uom,
354 'batch_no': cstr(p.batch_no).strip(),
355 'serial_no': cstr(p.serial_no).strip(),
356 'name': d.name
357 }))
358 else:
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530359 il.append(frappe._dict({
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530360 'warehouse': d.warehouse,
Nabin Hait0aa71a52014-02-11 16:14:52 +0530361 'reserved_warehouse': reserved_warehouse,
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530362 'item_code': d.item_code,
363 'qty': d.qty,
364 'reserved_qty': reserved_qty_for_main_item,
365 'uom': d.stock_uom,
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530366 'batch_no': cstr(d.get("batch_no")).strip(),
367 'serial_no': cstr(d.get("serial_no")).strip(),
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530368 'name': d.name
369 }))
370 return il
Anand Doshid2946502014-04-08 20:10:03 +0530371
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530372 def has_sales_bom(self, item_code):
Anand Doshid2946502014-04-08 20:10:03 +0530373 return frappe.db.sql("""select name from `tabSales BOM`
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530374 where new_item_code=%s and docstatus != 2""", item_code)
Anand Doshid2946502014-04-08 20:10:03 +0530375
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530376 def get_already_delivered_qty(self, dn, so, so_detail):
Anand Doshid2946502014-04-08 20:10:03 +0530377 qty = frappe.db.sql("""select sum(qty) from `tabDelivery Note Item`
ankitjavalkarworke69a6112014-10-08 12:46:02 +0530378 where so_detail = %s and docstatus = 1
Anand Doshid2946502014-04-08 20:10:03 +0530379 and against_sales_order = %s
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530380 and parent != %s""", (so_detail, so, dn))
381 return qty and flt(qty[0][0]) or 0.0
382
383 def get_so_qty_and_warehouse(self, so_detail):
Anand Doshie9baaa62014-02-26 12:35:33 +0530384 so_item = frappe.db.sql("""select qty, warehouse from `tabSales Order Item`
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530385 where name = %s and docstatus = 1""", so_detail, as_dict=1)
386 so_qty = so_item and flt(so_item[0]["qty"]) or 0.0
Nabin Haita7f757a2014-02-10 17:54:04 +0530387 so_warehouse = so_item and so_item[0]["warehouse"] or ""
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530388 return so_qty, so_warehouse
Anand Doshid2946502014-04-08 20:10:03 +0530389
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530390 def check_stop_sales_order(self, ref_fieldname):
Nabin Haitdd38a262014-12-26 13:15:21 +0530391 for d in self.get("items"):
Anand Doshif78d1ae2014-03-28 13:55:00 +0530392 if d.get(ref_fieldname):
Rushabh Mehtaf2227d02014-03-31 23:37:40 +0530393 status = frappe.db.get_value("Sales Order", d.get(ref_fieldname), "status")
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530394 if status == "Stopped":
Rushabh Mehta8a40c132014-04-15 16:30:55 +0530395 frappe.throw(_("Sales Order {0} is stopped").format(d.get(ref_fieldname)))
Anand Doshid2946502014-04-08 20:10:03 +0530396
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530397def check_active_sales_items(obj):
Nabin Haitdd38a262014-12-26 13:15:21 +0530398 for d in obj.get("items"):
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530399 if d.item_code:
Anand Doshid2946502014-04-08 20:10:03 +0530400 item = frappe.db.sql("""select docstatus, is_sales_item,
401 is_service_item, income_account from tabItem where name = %s""",
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530402 d.item_code, as_dict=True)[0]
403 if item.is_sales_item == 'No' and item.is_service_item == 'No':
Rushabh Mehtac38fc712014-04-16 17:21:25 +0530404 frappe.throw(_("Item {0} must be Sales or Service Item in {1}").format(d.item_code, d.idx))
Nabin Hait365ae272014-04-03 17:38:54 +0530405 if getattr(d, "income_account", None) and not item.income_account:
Anand Doshid2946502014-04-08 20:10:03 +0530406 frappe.db.set_value("Item", d.item_code, "income_account",
Nabin Hait89ad6f22013-10-18 12:55:59 +0530407 d.income_account)