Rushabh Mehta | ad45e31 | 2013-11-20 12:59:58 +0530 | [diff] [blame] | 1 | # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors |
Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 2 | # License: GNU General Public License v3. See license.txt |
Nabin Hait | ec52db8 | 2013-01-22 11:53:14 +0530 | [diff] [blame] | 3 | |
| 4 | from __future__ import unicode_literals |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 5 | import frappe |
| 6 | from frappe.utils import cint, flt, comma_or, _round, cstr |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 7 | from erpnext.setup.utils import get_company_currency |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 8 | from frappe import msgprint, _ |
Nabin Hait | ec52db8 | 2013-01-22 11:53:14 +0530 | [diff] [blame] | 9 | |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 10 | from erpnext.controllers.stock_controller import StockController |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 11 | |
Nabin Hait | c3afb25 | 2013-03-19 12:01:24 +0530 | [diff] [blame] | 12 | class SellingController(StockController): |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 13 | def onload_post_render(self): |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 14 | # contact, address, item details and pos details (if applicable) |
| 15 | self.set_missing_values() |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 16 | |
| 17 | def validate(self): |
| 18 | super(SellingController, self).validate() |
| 19 | self.validate_max_discount() |
| 20 | check_active_sales_items(self) |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 21 | |
| 22 | def get_sender(self, comm): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 23 | return frappe.db.get_value('Sales Email Settings', None, 'email_id') |
Rushabh Mehta | 800b3aa | 2013-10-03 17:26:33 +0530 | [diff] [blame] | 24 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 25 | def set_missing_values(self, for_validate=False): |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 26 | super(SellingController, self).set_missing_values(for_validate) |
| 27 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 28 | # set contact and address details for customer, if they are not mentioned |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 29 | self.set_missing_lead_customer_details() |
Nabin Hait | 096d363 | 2013-10-17 17:01:14 +0530 | [diff] [blame] | 30 | self.set_price_list_and_item_details() |
Rushabh Mehta | e89c533 | 2013-08-29 15:32:46 +0530 | [diff] [blame] | 31 | if self.doc.fields.get("__islocal"): |
Akhilesh Darjee | 2ced3b0 | 2014-01-28 19:16:05 +0530 | [diff] [blame] | 32 | self.set_taxes("other_charges", "taxes_and_charges") |
Nabin Hait | 7a75e10 | 2013-09-17 10:21:20 +0530 | [diff] [blame] | 33 | |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 34 | def set_missing_lead_customer_details(self): |
| 35 | if self.doc.customer: |
Anand Doshi | 4362443 | 2014-02-25 15:42:16 +0530 | [diff] [blame] | 36 | from erpnext.accounts.party import _get_party_details |
| 37 | self.doc.update_if_missing(_get_party_details(self.doc.customer, |
| 38 | ignore_permissions=self.bean.ignore_permissions)) |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 39 | |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 40 | elif self.doc.lead: |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 41 | from erpnext.selling.doctype.lead.lead import get_lead_details |
| 42 | self.doc.update_if_missing(get_lead_details(self.doc.lead)) |
Anand Doshi | 4362443 | 2014-02-25 15:42:16 +0530 | [diff] [blame] | 43 | |
Nabin Hait | 096d363 | 2013-10-17 17:01:14 +0530 | [diff] [blame] | 44 | def set_price_list_and_item_details(self): |
| 45 | self.set_price_list_currency("Selling") |
Nabin Hait | 0aa71a5 | 2014-02-11 16:14:52 +0530 | [diff] [blame] | 46 | self.set_missing_item_details() |
Nabin Hait | 2bd3777 | 2013-07-08 19:00:29 +0530 | [diff] [blame] | 47 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 48 | def apply_shipping_rule(self): |
| 49 | if self.doc.shipping_rule: |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 50 | shipping_rule = frappe.bean("Shipping Rule", self.doc.shipping_rule) |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 51 | value = self.doc.net_total |
| 52 | |
| 53 | # TODO |
| 54 | # shipping rule calculation based on item's net weight |
| 55 | |
| 56 | shipping_amount = 0.0 |
| 57 | for condition in shipping_rule.doclist.get({"parentfield": "shipping_rule_conditions"}): |
| 58 | if not condition.to_value or (flt(condition.from_value) <= value <= flt(condition.to_value)): |
| 59 | shipping_amount = condition.shipping_amount |
| 60 | break |
Anand Doshi | 0b4943c | 2013-07-04 23:45:22 +0530 | [diff] [blame] | 61 | |
| 62 | self.doclist.append({ |
| 63 | "doctype": "Sales Taxes and Charges", |
| 64 | "parentfield": "other_charges", |
| 65 | "charge_type": "Actual", |
| 66 | "account_head": shipping_rule.doc.account, |
| 67 | "cost_center": shipping_rule.doc.cost_center, |
| 68 | "description": shipping_rule.doc.label, |
| 69 | "rate": shipping_amount |
| 70 | }) |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 71 | |
Nabin Hait | ec52db8 | 2013-01-22 11:53:14 +0530 | [diff] [blame] | 72 | def set_total_in_words(self): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 73 | from frappe.utils import money_in_words |
Nabin Hait | ec52db8 | 2013-01-22 11:53:14 +0530 | [diff] [blame] | 74 | company_currency = get_company_currency(self.doc.company) |
Nabin Hait | 6f1a5ec | 2013-02-20 16:25:05 +0530 | [diff] [blame] | 75 | |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 76 | disable_rounded_total = cint(frappe.db.get_value("Global Defaults", None, |
Nabin Hait | 6f1a5ec | 2013-02-20 16:25:05 +0530 | [diff] [blame] | 77 | "disable_rounded_total")) |
| 78 | |
Nabin Hait | ec52db8 | 2013-01-22 11:53:14 +0530 | [diff] [blame] | 79 | if self.meta.get_field("in_words"): |
Nabin Hait | 6f1a5ec | 2013-02-20 16:25:05 +0530 | [diff] [blame] | 80 | self.doc.in_words = money_in_words(disable_rounded_total and |
| 81 | self.doc.grand_total or self.doc.rounded_total, company_currency) |
Nabin Hait | ec52db8 | 2013-01-22 11:53:14 +0530 | [diff] [blame] | 82 | if self.meta.get_field("in_words_export"): |
Nabin Hait | 6f1a5ec | 2013-02-20 16:25:05 +0530 | [diff] [blame] | 83 | self.doc.in_words_export = money_in_words(disable_rounded_total and |
Nabin Hait | 8c7234f | 2013-03-11 16:32:33 +0530 | [diff] [blame] | 84 | self.doc.grand_total_export or self.doc.rounded_total_export, self.doc.currency) |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 85 | |
| 86 | def calculate_taxes_and_totals(self): |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 87 | self.other_fname = "other_charges" |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 88 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 89 | super(SellingController, self).calculate_taxes_and_totals() |
| 90 | |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 91 | self.calculate_total_advance("Sales Invoice", "advance_adjustment_details") |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 92 | self.calculate_commission() |
| 93 | self.calculate_contribution() |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 94 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 95 | def determine_exclusive_rate(self): |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 96 | if not any((cint(tax.included_in_print_rate) for tax in self.tax_doclist)): |
| 97 | # no inclusive tax |
| 98 | return |
| 99 | |
| 100 | for item in self.item_doclist: |
| 101 | item_tax_map = self._load_item_tax_rate(item.item_tax_rate) |
| 102 | cumulated_tax_fraction = 0 |
| 103 | for i, tax in enumerate(self.tax_doclist): |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 104 | tax.tax_fraction_for_current_item = self.get_current_tax_fraction(tax, item_tax_map) |
| 105 | |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 106 | if i==0: |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 107 | tax.grand_total_fraction_for_current_item = 1 + tax.tax_fraction_for_current_item |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 108 | else: |
| 109 | tax.grand_total_fraction_for_current_item = \ |
| 110 | self.tax_doclist[i-1].grand_total_fraction_for_current_item \ |
| 111 | + tax.tax_fraction_for_current_item |
| 112 | |
| 113 | cumulated_tax_fraction += tax.tax_fraction_for_current_item |
| 114 | |
Akhilesh Darjee | 57738a0 | 2014-01-03 18:15:07 +0530 | [diff] [blame] | 115 | if cumulated_tax_fraction and not self.discount_amount_applied: |
Nabin Hait | 1eb5601 | 2014-02-10 19:20:15 +0530 | [diff] [blame] | 116 | item.base_amount = flt((item.amount * self.doc.conversion_rate) / |
| 117 | (1 + cumulated_tax_fraction), self.precision("base_amount", item)) |
Anand Doshi | fbe1e16 | 2013-08-06 19:38:19 +0530 | [diff] [blame] | 118 | |
Nabin Hait | 1eb5601 | 2014-02-10 19:20:15 +0530 | [diff] [blame] | 119 | item.base_rate = flt(item.base_amount / item.qty, self.precision("base_rate", item)) |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 120 | |
Nabin Hait | a7f757a | 2014-02-10 17:54:04 +0530 | [diff] [blame] | 121 | if item.discount_percentage == 100: |
Nabin Hait | 7979f7e | 2014-02-10 18:26:49 +0530 | [diff] [blame] | 122 | item.base_price_list_rate = item.base_rate |
| 123 | item.base_rate = 0.0 |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 124 | else: |
Nabin Hait | 7979f7e | 2014-02-10 18:26:49 +0530 | [diff] [blame] | 125 | item.base_price_list_rate = flt(item.base_rate / (1 - (item.discount_percentage / 100.0)), |
Nabin Hait | a7f757a | 2014-02-10 17:54:04 +0530 | [diff] [blame] | 126 | self.precision("base_price_list_rate", item)) |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 127 | |
| 128 | def get_current_tax_fraction(self, tax, item_tax_map): |
| 129 | """ |
| 130 | Get tax fraction for calculating tax exclusive amount |
| 131 | from tax inclusive amount |
| 132 | """ |
| 133 | current_tax_fraction = 0 |
| 134 | |
| 135 | if cint(tax.included_in_print_rate): |
| 136 | tax_rate = self._get_tax_rate(tax, item_tax_map) |
| 137 | |
| 138 | if tax.charge_type == "On Net Total": |
| 139 | current_tax_fraction = tax_rate / 100.0 |
| 140 | |
| 141 | elif tax.charge_type == "On Previous Row Amount": |
| 142 | current_tax_fraction = (tax_rate / 100.0) * \ |
| 143 | self.tax_doclist[cint(tax.row_id) - 1].tax_fraction_for_current_item |
| 144 | |
| 145 | elif tax.charge_type == "On Previous Row Total": |
| 146 | current_tax_fraction = (tax_rate / 100.0) * \ |
| 147 | self.tax_doclist[cint(tax.row_id) - 1].grand_total_fraction_for_current_item |
| 148 | |
| 149 | return current_tax_fraction |
| 150 | |
| 151 | def calculate_item_values(self): |
Akhilesh Darjee | 57738a0 | 2014-01-03 18:15:07 +0530 | [diff] [blame] | 152 | if not self.discount_amount_applied: |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 153 | for item in self.item_doclist: |
| 154 | self.round_floats_in(item) |
Rushabh Mehta | 4dca401 | 2013-07-25 17:45:59 +0530 | [diff] [blame] | 155 | |
Nabin Hait | a7f757a | 2014-02-10 17:54:04 +0530 | [diff] [blame] | 156 | if item.discount_percentage == 100: |
Nabin Hait | 7979f7e | 2014-02-10 18:26:49 +0530 | [diff] [blame] | 157 | item.rate = 0 |
| 158 | elif not item.rate: |
| 159 | item.rate = flt(item.price_list_rate * (1.0 - (item.discount_percentage / 100.0)), |
| 160 | self.precision("rate", item)) |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 161 | |
Nabin Hait | 1eb5601 | 2014-02-10 19:20:15 +0530 | [diff] [blame] | 162 | item.amount = flt(item.rate * item.qty, |
| 163 | self.precision("amount", item)) |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 164 | |
Nabin Hait | a7f757a | 2014-02-10 17:54:04 +0530 | [diff] [blame] | 165 | self._set_in_company_currency(item, "price_list_rate", "base_price_list_rate") |
Nabin Hait | 7979f7e | 2014-02-10 18:26:49 +0530 | [diff] [blame] | 166 | self._set_in_company_currency(item, "rate", "base_rate") |
Nabin Hait | 1eb5601 | 2014-02-10 19:20:15 +0530 | [diff] [blame] | 167 | self._set_in_company_currency(item, "amount", "base_amount") |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 168 | |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 169 | def calculate_net_total(self): |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 170 | self.doc.net_total = self.doc.net_total_export = 0.0 |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 171 | |
| 172 | for item in self.item_doclist: |
Nabin Hait | 1eb5601 | 2014-02-10 19:20:15 +0530 | [diff] [blame] | 173 | self.doc.net_total += item.base_amount |
| 174 | self.doc.net_total_export += item.amount |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 175 | |
| 176 | self.round_floats_in(self.doc, ["net_total", "net_total_export"]) |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 177 | |
| 178 | def calculate_totals(self): |
| 179 | self.doc.grand_total = flt(self.tax_doclist and \ |
Anand Doshi | 5af812a | 2013-05-10 19:23:02 +0530 | [diff] [blame] | 180 | self.tax_doclist[-1].total or self.doc.net_total, self.precision("grand_total")) |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 181 | self.doc.grand_total_export = flt(self.doc.grand_total / self.doc.conversion_rate, |
Anand Doshi | 5af812a | 2013-05-10 19:23:02 +0530 | [diff] [blame] | 182 | self.precision("grand_total_export")) |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 183 | |
| 184 | self.doc.other_charges_total = flt(self.doc.grand_total - self.doc.net_total, |
| 185 | self.precision("other_charges_total")) |
Nabin Hait | 5c6d13a | 2014-01-20 17:18:16 +0530 | [diff] [blame] | 186 | |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 187 | self.doc.other_charges_total_export = flt(self.doc.grand_total_export - |
Nabin Hait | 5c6d13a | 2014-01-20 17:18:16 +0530 | [diff] [blame] | 188 | self.doc.net_total_export + flt(self.doc.discount_amount), |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 189 | self.precision("other_charges_total_export")) |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 190 | |
Anand Doshi | 2e31e09 | 2013-08-19 19:58:23 +0530 | [diff] [blame] | 191 | self.doc.rounded_total = _round(self.doc.grand_total) |
| 192 | self.doc.rounded_total_export = _round(self.doc.grand_total_export) |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 193 | |
Akhilesh Darjee | 57738a0 | 2014-01-03 18:15:07 +0530 | [diff] [blame] | 194 | def apply_discount_amount(self): |
| 195 | if self.doc.discount_amount: |
| 196 | grand_total_for_discount_amount = self.get_grand_total_for_discount_amount() |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 197 | |
Akhilesh Darjee | 57738a0 | 2014-01-03 18:15:07 +0530 | [diff] [blame] | 198 | if grand_total_for_discount_amount: |
| 199 | # calculate item amount after Discount Amount |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 200 | for item in self.item_doclist: |
Nabin Hait | 1eb5601 | 2014-02-10 19:20:15 +0530 | [diff] [blame] | 201 | distributed_amount = flt(self.doc.discount_amount) * item.base_amount / grand_total_for_discount_amount |
| 202 | item.base_amount = flt(item.base_amount - distributed_amount, self.precision("base_amount", item)) |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 203 | |
Akhilesh Darjee | 57738a0 | 2014-01-03 18:15:07 +0530 | [diff] [blame] | 204 | self.discount_amount_applied = True |
| 205 | self._calculate_taxes_and_totals() |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 206 | |
Akhilesh Darjee | 57738a0 | 2014-01-03 18:15:07 +0530 | [diff] [blame] | 207 | def get_grand_total_for_discount_amount(self): |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 208 | actual_taxes_dict = {} |
| 209 | |
| 210 | for tax in self.tax_doclist: |
| 211 | if tax.charge_type == "Actual": |
| 212 | actual_taxes_dict.setdefault(tax.idx, tax.tax_amount) |
| 213 | elif tax.row_id in actual_taxes_dict: |
| 214 | actual_tax_amount = flt(actual_taxes_dict.get(tax.row_id, 0)) * \ |
| 215 | flt(tax.rate) / 100 |
| 216 | actual_taxes_dict.setdefault(tax.idx, actual_tax_amount) |
| 217 | |
Akhilesh Darjee | 57738a0 | 2014-01-03 18:15:07 +0530 | [diff] [blame] | 218 | grand_total_for_discount_amount = flt(self.doc.grand_total - sum(actual_taxes_dict.values()), |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 219 | self.precision("grand_total")) |
Akhilesh Darjee | 57738a0 | 2014-01-03 18:15:07 +0530 | [diff] [blame] | 220 | return grand_total_for_discount_amount |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 221 | |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 222 | def calculate_outstanding_amount(self): |
| 223 | # NOTE: |
| 224 | # write_off_amount is only for POS Invoice |
| 225 | # total_advance is only for non POS Invoice |
Nabin Hait | 2f8d445 | 2014-02-20 14:59:37 +0530 | [diff] [blame] | 226 | if self.doc.doctype == "Sales Invoice" and self.doc.docstatus == 0: |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 227 | self.round_floats_in(self.doc, ["grand_total", "total_advance", "write_off_amount", |
| 228 | "paid_amount"]) |
| 229 | total_amount_to_pay = self.doc.grand_total - self.doc.write_off_amount |
Rushabh Mehta | 5404778 | 2013-12-26 11:07:46 +0530 | [diff] [blame] | 230 | self.doc.outstanding_amount = flt(total_amount_to_pay - self.doc.total_advance \ |
| 231 | - self.doc.paid_amount, self.precision("outstanding_amount")) |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 232 | |
| 233 | def calculate_commission(self): |
| 234 | if self.meta.get_field("commission_rate"): |
| 235 | self.round_floats_in(self.doc, ["net_total", "commission_rate"]) |
| 236 | if self.doc.commission_rate > 100.0: |
| 237 | msgprint(_(self.meta.get_label("commission_rate")) + " " + |
| 238 | _("cannot be greater than 100"), raise_exception=True) |
| 239 | |
| 240 | self.doc.total_commission = flt(self.doc.net_total * self.doc.commission_rate / 100.0, |
| 241 | self.precision("total_commission")) |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 242 | |
| 243 | def calculate_contribution(self): |
| 244 | total = 0.0 |
| 245 | sales_team = self.doclist.get({"parentfield": "sales_team"}) |
| 246 | for sales_person in sales_team: |
| 247 | self.round_floats_in(sales_person) |
| 248 | |
| 249 | sales_person.allocated_amount = flt( |
| 250 | self.doc.net_total * sales_person.allocated_percentage / 100.0, |
| 251 | self.precision("allocated_amount", sales_person)) |
| 252 | |
| 253 | total += sales_person.allocated_percentage |
| 254 | |
| 255 | if sales_team and total != 100.0: |
| 256 | msgprint(_("Total") + " " + |
| 257 | _(self.meta.get_label("allocated_percentage", parentfield="sales_team")) + |
| 258 | " " + _("should be 100%"), raise_exception=True) |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 259 | |
| 260 | def validate_order_type(self): |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 261 | valid_types = ["Sales", "Maintenance", "Shopping Cart"] |
Rushabh Mehta | 080fcc8 | 2013-07-04 15:27:04 +0530 | [diff] [blame] | 262 | if not self.doc.order_type: |
| 263 | self.doc.order_type = "Sales" |
| 264 | elif self.doc.order_type not in valid_types: |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 265 | msgprint(_(self.meta.get_label("order_type")) + " " + |
Anand Doshi | 373680b | 2013-10-10 16:04:40 +0530 | [diff] [blame] | 266 | _("must be one of") + ": " + comma_or(valid_types), raise_exception=True) |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 267 | |
| 268 | def check_credit(self, grand_total): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 269 | customer_account = frappe.db.get_value("Account", {"company": self.doc.company, |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 270 | "master_name": self.doc.customer}, "name") |
| 271 | if customer_account: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 272 | total_outstanding = frappe.db.sql("""select |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 273 | sum(ifnull(debit, 0)) - sum(ifnull(credit, 0)) |
Anand Doshi | 0c26f81 | 2013-11-15 19:12:09 +0530 | [diff] [blame] | 274 | from `tabGL Entry` where account = %s""", customer_account) |
| 275 | total_outstanding = total_outstanding[0][0] if total_outstanding else 0 |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 276 | |
| 277 | outstanding_including_current = flt(total_outstanding) + flt(grand_total) |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 278 | frappe.bean('Account', customer_account).run_method("check_credit_limit", |
Anand Doshi | 0c26f81 | 2013-11-15 19:12:09 +0530 | [diff] [blame] | 279 | outstanding_including_current) |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 280 | |
| 281 | def validate_max_discount(self): |
| 282 | for d in self.doclist.get({"parentfield": self.fname}): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 283 | discount = flt(frappe.db.get_value("Item", d.item_code, "max_discount")) |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 284 | |
Nabin Hait | a7f757a | 2014-02-10 17:54:04 +0530 | [diff] [blame] | 285 | if discount and flt(d.discount_percentage) > discount: |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 286 | frappe.throw(_("You cannot give more than ") + cstr(discount) + "% " + |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 287 | _("discount on Item Code") + ": " + cstr(d.item_code)) |
| 288 | |
| 289 | def get_item_list(self): |
| 290 | il = [] |
| 291 | for d in self.doclist.get({"parentfield": self.fname}): |
Nabin Hait | 0aa71a5 | 2014-02-11 16:14:52 +0530 | [diff] [blame] | 292 | reserved_warehouse = "" |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 293 | reserved_qty_for_main_item = 0 |
| 294 | |
| 295 | if self.doc.doctype == "Sales Order": |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 296 | if (frappe.db.get_value("Item", d.item_code, "is_stock_item") == 'Yes' or |
Nabin Hait | a7f757a | 2014-02-10 17:54:04 +0530 | [diff] [blame] | 297 | self.has_sales_bom(d.item_code)) and not d.warehouse: |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 298 | frappe.throw(_("Please enter Reserved Warehouse for item ") + |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 299 | d.item_code + _(" as it is stock Item or packing item")) |
Nabin Hait | 0aa71a5 | 2014-02-11 16:14:52 +0530 | [diff] [blame] | 300 | reserved_warehouse = d.warehouse |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 301 | if flt(d.qty) > flt(d.delivered_qty): |
| 302 | reserved_qty_for_main_item = flt(d.qty) - flt(d.delivered_qty) |
| 303 | |
| 304 | if self.doc.doctype == "Delivery Note" and d.against_sales_order: |
| 305 | # if SO qty is 10 and there is tolerance of 20%, then it will allow DN of 12. |
| 306 | # But in this case reserved qty should only be reduced by 10 and not 12 |
| 307 | |
| 308 | already_delivered_qty = self.get_already_delivered_qty(self.doc.name, |
| 309 | d.against_sales_order, d.prevdoc_detail_docname) |
Nabin Hait | 0aa71a5 | 2014-02-11 16:14:52 +0530 | [diff] [blame] | 310 | so_qty, reserved_warehouse = self.get_so_qty_and_warehouse(d.prevdoc_detail_docname) |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 311 | |
| 312 | if already_delivered_qty + d.qty > so_qty: |
| 313 | reserved_qty_for_main_item = -(so_qty - already_delivered_qty) |
| 314 | else: |
| 315 | reserved_qty_for_main_item = -flt(d.qty) |
| 316 | |
| 317 | if self.has_sales_bom(d.item_code): |
| 318 | for p in self.doclist.get({"parentfield": "packing_details"}): |
| 319 | if p.parent_detail_docname == d.name and p.parent_item == d.item_code: |
| 320 | # the packing details table's qty is already multiplied with parent's qty |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 321 | il.append(frappe._dict({ |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 322 | 'warehouse': p.warehouse, |
Nabin Hait | 0aa71a5 | 2014-02-11 16:14:52 +0530 | [diff] [blame] | 323 | 'reserved_warehouse': reserved_warehouse, |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 324 | 'item_code': p.item_code, |
| 325 | 'qty': flt(p.qty), |
| 326 | 'reserved_qty': (flt(p.qty)/flt(d.qty)) * reserved_qty_for_main_item, |
| 327 | 'uom': p.uom, |
| 328 | 'batch_no': cstr(p.batch_no).strip(), |
| 329 | 'serial_no': cstr(p.serial_no).strip(), |
| 330 | 'name': d.name |
| 331 | })) |
| 332 | else: |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 333 | il.append(frappe._dict({ |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 334 | 'warehouse': d.warehouse, |
Nabin Hait | 0aa71a5 | 2014-02-11 16:14:52 +0530 | [diff] [blame] | 335 | 'reserved_warehouse': reserved_warehouse, |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 336 | 'item_code': d.item_code, |
| 337 | 'qty': d.qty, |
| 338 | 'reserved_qty': reserved_qty_for_main_item, |
| 339 | 'uom': d.stock_uom, |
| 340 | 'batch_no': cstr(d.batch_no).strip(), |
| 341 | 'serial_no': cstr(d.serial_no).strip(), |
| 342 | 'name': d.name |
| 343 | })) |
| 344 | return il |
| 345 | |
| 346 | def has_sales_bom(self, item_code): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 347 | return frappe.db.sql("""select name from `tabSales BOM` |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 348 | where new_item_code=%s and docstatus != 2""", item_code) |
| 349 | |
| 350 | def get_already_delivered_qty(self, dn, so, so_detail): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 351 | qty = frappe.db.sql("""select sum(qty) from `tabDelivery Note Item` |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 352 | where prevdoc_detail_docname = %s and docstatus = 1 |
| 353 | and against_sales_order = %s |
| 354 | and parent != %s""", (so_detail, so, dn)) |
| 355 | return qty and flt(qty[0][0]) or 0.0 |
| 356 | |
| 357 | def get_so_qty_and_warehouse(self, so_detail): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 358 | so_item = frappe.db.sql("""select qty, warehouse from `tabSales Order Item` |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 359 | where name = %s and docstatus = 1""", so_detail, as_dict=1) |
| 360 | so_qty = so_item and flt(so_item[0]["qty"]) or 0.0 |
Nabin Hait | a7f757a | 2014-02-10 17:54:04 +0530 | [diff] [blame] | 361 | so_warehouse = so_item and so_item[0]["warehouse"] or "" |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 362 | return so_qty, so_warehouse |
| 363 | |
| 364 | def check_stop_sales_order(self, ref_fieldname): |
| 365 | for d in self.doclist.get({"parentfield": self.fname}): |
| 366 | if d.fields.get(ref_fieldname): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 367 | status = frappe.db.get_value("Sales Order", d.fields[ref_fieldname], "status") |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 368 | if status == "Stopped": |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 369 | frappe.throw(self.doc.doctype + |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 370 | _(" can not be created/modified against stopped Sales Order ") + |
| 371 | d.fields[ref_fieldname]) |
| 372 | |
| 373 | def check_active_sales_items(obj): |
| 374 | for d in obj.doclist.get({"parentfield": obj.fname}): |
| 375 | if d.item_code: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 376 | item = frappe.db.sql("""select docstatus, is_sales_item, |
Nabin Hait | eba1bdb | 2014-02-12 16:04:17 +0530 | [diff] [blame] | 377 | is_service_item, income_account from tabItem where name = %s""", |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 378 | d.item_code, as_dict=True)[0] |
| 379 | if item.is_sales_item == 'No' and item.is_service_item == 'No': |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 380 | frappe.throw(_("Item is neither Sales nor Service Item") + ": " + d.item_code) |
Nabin Hait | eba1bdb | 2014-02-12 16:04:17 +0530 | [diff] [blame] | 381 | if d.income_account and not item.income_account: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 382 | frappe.db.set_value("Item", d.item_code, "income_account", |
Nabin Hait | 89ad6f2 | 2013-10-18 12:55:59 +0530 | [diff] [blame] | 383 | d.income_account) |