Nabin Hait | ec52db8 | 2013-01-22 11:53:14 +0530 | [diff] [blame] | 1 | # ERPNext - web based ERP (http://erpnext.com) |
| 2 | # Copyright (C) 2012 Web Notes Technologies Pvt Ltd |
| 3 | # |
| 4 | # This program is free software: you can redistribute it and/or modify |
| 5 | # it under the terms of the GNU General Public License as published by |
| 6 | # the Free Software Foundation, either version 3 of the License, or |
| 7 | # (at your option) any later version. |
| 8 | # |
| 9 | # This program is distributed in the hope that it will be useful, |
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | # GNU General Public License for more details. |
| 13 | # |
| 14 | # You should have received a copy of the GNU General Public License |
| 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | from __future__ import unicode_literals |
| 18 | import webnotes |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 19 | from webnotes.utils import cint, flt, comma_or |
Nabin Hait | ec52db8 | 2013-01-22 11:53:14 +0530 | [diff] [blame] | 20 | from setup.utils import get_company_currency |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 21 | from selling.utils import get_item_details |
Nabin Hait | 0fc2454 | 2013-03-25 11:06:00 +0530 | [diff] [blame] | 22 | from webnotes import msgprint, _ |
Nabin Hait | ec52db8 | 2013-01-22 11:53:14 +0530 | [diff] [blame] | 23 | |
Nabin Hait | c3afb25 | 2013-03-19 12:01:24 +0530 | [diff] [blame] | 24 | from controllers.stock_controller import StockController |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 25 | |
Nabin Hait | c3afb25 | 2013-03-19 12:01:24 +0530 | [diff] [blame] | 26 | class SellingController(StockController): |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 27 | def onload_post_render(self): |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 28 | self.set_price_list_currency("selling") |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 29 | |
| 30 | # contact, address, item details and pos details (if applicable) |
| 31 | self.set_missing_values() |
| 32 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 33 | self.set_taxes("Sales Taxes and Charges", "other_charges", "charge") |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 34 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 35 | def set_missing_values(self, for_validate=False): |
| 36 | # set contact and address details for customer, if they are not mentioned |
| 37 | if self.doc.customer and not (self.doc.contact_person and self.doc.customer_address): |
| 38 | for fieldname, val in self.get_default_address_and_contact("customer").items(): |
| 39 | if not self.doc.fields.get(fieldname) and self.meta.get_field(fieldname): |
| 40 | self.doc.fields[fieldname] = val |
| 41 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 42 | self.set_missing_item_details(get_item_details) |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 43 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 44 | def get_other_charges(self): |
| 45 | self.doclist = self.doc.clear_table(self.doclist, "other_charges") |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 46 | self.set_taxes("Sales Taxes and Charges", "other_charges", "charge") |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 47 | |
| 48 | def set_customer_defaults(self): |
| 49 | self.get_default_customer_address() |
Nabin Hait | ec52db8 | 2013-01-22 11:53:14 +0530 | [diff] [blame] | 50 | |
| 51 | def set_total_in_words(self): |
| 52 | from webnotes.utils import money_in_words |
| 53 | company_currency = get_company_currency(self.doc.company) |
Nabin Hait | 6f1a5ec | 2013-02-20 16:25:05 +0530 | [diff] [blame] | 54 | |
| 55 | disable_rounded_total = cint(webnotes.conn.get_value("Global Defaults", None, |
| 56 | "disable_rounded_total")) |
| 57 | |
Nabin Hait | ec52db8 | 2013-01-22 11:53:14 +0530 | [diff] [blame] | 58 | if self.meta.get_field("in_words"): |
Nabin Hait | 6f1a5ec | 2013-02-20 16:25:05 +0530 | [diff] [blame] | 59 | self.doc.in_words = money_in_words(disable_rounded_total and |
| 60 | self.doc.grand_total or self.doc.rounded_total, company_currency) |
Nabin Hait | ec52db8 | 2013-01-22 11:53:14 +0530 | [diff] [blame] | 61 | if self.meta.get_field("in_words_export"): |
Nabin Hait | 6f1a5ec | 2013-02-20 16:25:05 +0530 | [diff] [blame] | 62 | self.doc.in_words_export = money_in_words(disable_rounded_total and |
Nabin Hait | 8c7234f | 2013-03-11 16:32:33 +0530 | [diff] [blame] | 63 | self.doc.grand_total_export or self.doc.rounded_total_export, self.doc.currency) |
Nabin Hait | a0e7c15 | 2013-03-21 18:37:06 +0530 | [diff] [blame] | 64 | |
Nabin Hait | 787c02e | 2013-03-29 16:42:33 +0530 | [diff] [blame] | 65 | def set_buying_amount(self, stock_ledger_entries = None): |
Anand Doshi | 8c5844e | 2013-03-21 20:24:10 +0530 | [diff] [blame] | 66 | from stock.utils import get_buying_amount |
Nabin Hait | 787c02e | 2013-03-29 16:42:33 +0530 | [diff] [blame] | 67 | if not stock_ledger_entries: |
| 68 | stock_ledger_entries = self.get_stock_ledger_entries() |
Anand Doshi | 8c5844e | 2013-03-21 20:24:10 +0530 | [diff] [blame] | 69 | |
| 70 | item_sales_bom = {} |
| 71 | for d in self.doclist.get({"parentfield": "packing_details"}): |
| 72 | new_d = webnotes._dict(d.fields.copy()) |
| 73 | new_d.total_qty = -1 * d.qty |
| 74 | item_sales_bom.setdefault(d.parent_item, []).append(new_d) |
Nabin Hait | a0e7c15 | 2013-03-21 18:37:06 +0530 | [diff] [blame] | 75 | |
| 76 | if stock_ledger_entries: |
| 77 | for item in self.doclist.get({"parentfield": self.fname}): |
| 78 | if item.item_code in self.stock_items or \ |
| 79 | (item_sales_bom and item_sales_bom.get(item.item_code)): |
| 80 | buying_amount = get_buying_amount(item.item_code, item.warehouse, -1*item.qty, |
| 81 | self.doc.doctype, self.doc.name, item.name, stock_ledger_entries, |
| 82 | item_sales_bom) |
Nabin Hait | f2d4df9 | 2013-05-07 13:12:02 +0530 | [diff] [blame] | 83 | |
| 84 | item.buying_amount = buying_amount >= 0.01 and buying_amount or 0 |
Nabin Hait | 0fc2454 | 2013-03-25 11:06:00 +0530 | [diff] [blame] | 85 | webnotes.conn.set_value(item.doctype, item.name, "buying_amount", |
| 86 | item.buying_amount) |
| 87 | |
| 88 | def check_expense_account(self, item): |
| 89 | if item.buying_amount and not item.expense_account: |
| 90 | msgprint(_("""Expense account is mandatory for item: """) + item.item_code, |
Nabin Hait | 787c02e | 2013-03-29 16:42:33 +0530 | [diff] [blame] | 91 | raise_exception=1) |
| 92 | |
| 93 | if item.buying_amount and not item.cost_center: |
| 94 | msgprint(_("""Cost Center is mandatory for item: """) + item.item_code, |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 95 | raise_exception=1) |
| 96 | |
| 97 | def calculate_taxes_and_totals(self): |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 98 | self.other_fname = "other_charges" |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 99 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 100 | super(SellingController, self).calculate_taxes_and_totals() |
| 101 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 102 | self.calculate_commission() |
| 103 | self.calculate_contribution() |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 104 | # self.calculate_outstanding_amount() |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 105 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 106 | def determine_exclusive_rate(self): |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 107 | if not any((cint(tax.included_in_print_rate) for tax in self.tax_doclist)): |
| 108 | # no inclusive tax |
| 109 | return |
| 110 | |
| 111 | for item in self.item_doclist: |
| 112 | item_tax_map = self._load_item_tax_rate(item.item_tax_rate) |
| 113 | cumulated_tax_fraction = 0 |
| 114 | for i, tax in enumerate(self.tax_doclist): |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 115 | tax.tax_fraction_for_current_item = self.get_current_tax_fraction(tax, item_tax_map) |
| 116 | |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 117 | if i==0: |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 118 | 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] | 119 | else: |
| 120 | tax.grand_total_fraction_for_current_item = \ |
| 121 | self.tax_doclist[i-1].grand_total_fraction_for_current_item \ |
| 122 | + tax.tax_fraction_for_current_item |
| 123 | |
| 124 | cumulated_tax_fraction += tax.tax_fraction_for_current_item |
| 125 | |
| 126 | if cumulated_tax_fraction: |
| 127 | item.basic_rate = flt((item.export_rate * self.doc.conversion_rate) / |
Anand Doshi | 39384d3 | 2013-05-11 19:39:53 +0530 | [diff] [blame] | 128 | (1 + cumulated_tax_fraction), self.precision("basic_rate", item)) |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 129 | |
Anand Doshi | 39384d3 | 2013-05-11 19:39:53 +0530 | [diff] [blame] | 130 | item.amount = flt(item.basic_rate * item.qty, self.precision("amount", item)) |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 131 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 132 | if item.adj_rate == 100: |
| 133 | item.base_ref_rate = item.basic_rate |
| 134 | item.basic_rate = 0.0 |
| 135 | else: |
| 136 | item.base_ref_rate = flt(item.basic_rate / (1 - (item.adj_rate / 100.0)), |
| 137 | self.precision("base_ref_rate", item)) |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 138 | |
| 139 | def get_current_tax_fraction(self, tax, item_tax_map): |
| 140 | """ |
| 141 | Get tax fraction for calculating tax exclusive amount |
| 142 | from tax inclusive amount |
| 143 | """ |
| 144 | current_tax_fraction = 0 |
| 145 | |
| 146 | if cint(tax.included_in_print_rate): |
| 147 | tax_rate = self._get_tax_rate(tax, item_tax_map) |
| 148 | |
| 149 | if tax.charge_type == "On Net Total": |
| 150 | current_tax_fraction = tax_rate / 100.0 |
| 151 | |
| 152 | elif tax.charge_type == "On Previous Row Amount": |
| 153 | current_tax_fraction = (tax_rate / 100.0) * \ |
| 154 | self.tax_doclist[cint(tax.row_id) - 1].tax_fraction_for_current_item |
| 155 | |
| 156 | elif tax.charge_type == "On Previous Row Total": |
| 157 | current_tax_fraction = (tax_rate / 100.0) * \ |
| 158 | self.tax_doclist[cint(tax.row_id) - 1].grand_total_fraction_for_current_item |
| 159 | |
| 160 | return current_tax_fraction |
| 161 | |
| 162 | def calculate_item_values(self): |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 163 | for item in self.item_doclist: |
Anand Doshi | 39384d3 | 2013-05-11 19:39:53 +0530 | [diff] [blame] | 164 | self.round_floats_in(item) |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 165 | |
| 166 | if item.adj_rate == 100: |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 167 | item.export_rate = 0 |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 168 | elif item.ref_rate: |
| 169 | item.export_rate = flt(item.ref_rate * (1.0 - (item.adj_rate / 100.0)), |
| 170 | self.precision("export_rate", item)) |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 171 | |
| 172 | item.export_amount = flt(item.export_rate * item.qty, |
Anand Doshi | 39384d3 | 2013-05-11 19:39:53 +0530 | [diff] [blame] | 173 | self.precision("export_amount", item)) |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 174 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 175 | self._set_in_company_currency(item, "ref_rate", "base_ref_rate") |
| 176 | self._set_in_company_currency(item, "export_rate", "basic_rate") |
| 177 | self._set_in_company_currency(item, "export_amount", "amount") |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 178 | |
| 179 | def calculate_net_total(self): |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 180 | self.doc.net_total = self.doc.net_total_export = 0.0 |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 181 | |
| 182 | for item in self.item_doclist: |
| 183 | self.doc.net_total += item.amount |
| 184 | self.doc.net_total_export += item.export_amount |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 185 | |
| 186 | self.round_floats_in(self.doc, ["net_total", "net_total_export"]) |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 187 | |
| 188 | def calculate_totals(self): |
| 189 | self.doc.grand_total = flt(self.tax_doclist and \ |
Anand Doshi | 5af812a | 2013-05-10 19:23:02 +0530 | [diff] [blame] | 190 | 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] | 191 | 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] | 192 | self.precision("grand_total_export")) |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 193 | |
| 194 | self.doc.other_charges_total = flt(self.doc.grand_total - self.doc.net_total, |
| 195 | self.precision("other_charges_total")) |
| 196 | self.doc.other_charges_total_export = flt(self.doc.grand_total_export - self.doc.net_total_export, |
| 197 | self.precision("other_charges_total_export")) |
Anand Doshi | 21f4ea3 | 2013-05-10 18:08:32 +0530 | [diff] [blame] | 198 | |
| 199 | self.doc.rounded_total = round(self.doc.grand_total) |
| 200 | self.doc.rounded_total_export = round(self.doc.grand_total_export) |
| 201 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 202 | def calculate_commission(self): |
Anand Doshi | 65d8755 | 2013-05-21 19:53:33 +0530 | [diff] [blame] | 203 | self.round_floats_in(self.doc, ["net_total", "commission_rate"]) |
| 204 | if self.doc.commission_rate > 100.0: |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 205 | msgprint(_(self.meta.get_label("commission_rate")) + " " + |
| 206 | _("cannot be greater than 100"), raise_exception=True) |
| 207 | |
| 208 | self.doc.total_commission = flt(self.doc.net_total * self.doc.commission_rate / 100.0, |
| 209 | self.precision("total_commission")) |
| 210 | |
| 211 | def calculate_contribution(self): |
| 212 | total = 0.0 |
| 213 | sales_team = self.doclist.get({"parentfield": "sales_team"}) |
| 214 | for sales_person in sales_team: |
| 215 | self.round_floats_in(sales_person) |
| 216 | |
| 217 | sales_person.allocated_amount = flt( |
| 218 | self.doc.net_total * sales_person.allocated_percentage / 100.0, |
| 219 | self.precision("allocated_amount", sales_person)) |
| 220 | |
| 221 | total += sales_person.allocated_percentage |
| 222 | |
| 223 | if sales_team and total != 100.0: |
| 224 | msgprint(_("Total") + " " + |
| 225 | _(self.meta.get_label("allocated_percentage", parentfield="sales_team")) + |
| 226 | " " + _("should be 100%"), raise_exception=True) |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 227 | |
| 228 | def validate_order_type(self): |
| 229 | valid_types = ["Sales", "Maintenance"] |
| 230 | if self.doc.order_type not in valid_types: |
| 231 | msgprint(_(self.meta.get_label("order_type")) + " " + |
| 232 | _("must be one of") + ": " + comma_or(valid_types), |
| 233 | raise_exception=True) |