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