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