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