blob: f58366830c6d70eb3ff983023f023f0c68cef4ad [file] [log] [blame]
Rushabh Mehtaad45e312013-11-20 12:59:58 +05301# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302# License: GNU General Public License v3. See license.txt
Nabin Haitec52db82013-01-22 11:53:14 +05303
4from __future__ import unicode_literals
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05305import frappe
6from frappe.utils import cint, flt, comma_or, _round, cstr
Rushabh Mehta1f847992013-12-12 19:12:19 +05307from erpnext.setup.utils import get_company_currency
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05308from frappe import msgprint, _
Nabin Haitec52db82013-01-22 11:53:14 +05309
Rushabh Mehta1f847992013-12-12 19:12:19 +053010from erpnext.controllers.stock_controller import StockController
Nabin Haitbf495c92013-01-30 12:49:08 +053011
Nabin Haitc3afb252013-03-19 12:01:24 +053012class SellingController(StockController):
Anand Doshif3096132013-05-21 19:35:06 +053013 def onload_post_render(self):
Anand Doshif3096132013-05-21 19:35:06 +053014 # contact, address, item details and pos details (if applicable)
15 self.set_missing_values()
Nabin Haitd1fd1e22013-10-18 12:29:11 +053016
17 def validate(self):
18 super(SellingController, self).validate()
19 self.validate_max_discount()
20 check_active_sales_items(self)
Rushabh Mehta800b3aa2013-10-03 17:26:33 +053021
22 def get_sender(self, comm):
Anand Doshie9baaa62014-02-26 12:35:33 +053023 return frappe.db.get_value('Sales Email Settings', None, 'email_id')
Rushabh Mehta800b3aa2013-10-03 17:26:33 +053024
Anand Doshif3096132013-05-21 19:35:06 +053025 def set_missing_values(self, for_validate=False):
Anand Doshiabc10032013-06-14 17:44:03 +053026 super(SellingController, self).set_missing_values(for_validate)
27
Anand Doshif3096132013-05-21 19:35:06 +053028 # set contact and address details for customer, if they are not mentioned
Anand Doshiabc10032013-06-14 17:44:03 +053029 self.set_missing_lead_customer_details()
Nabin Hait096d3632013-10-17 17:01:14 +053030 self.set_price_list_and_item_details()
Rushabh Mehtae89c5332013-08-29 15:32:46 +053031 if self.doc.fields.get("__islocal"):
Akhilesh Darjee2ced3b02014-01-28 19:16:05 +053032 self.set_taxes("other_charges", "taxes_and_charges")
Nabin Hait7a75e102013-09-17 10:21:20 +053033
Anand Doshiabc10032013-06-14 17:44:03 +053034 def set_missing_lead_customer_details(self):
35 if self.doc.customer:
Anand Doshi43624432014-02-25 15:42:16 +053036 from erpnext.accounts.party import _get_party_details
37 self.doc.update_if_missing(_get_party_details(self.doc.customer,
38 ignore_permissions=self.bean.ignore_permissions))
Anand Doshiabc10032013-06-14 17:44:03 +053039
Anand Doshiabc10032013-06-14 17:44:03 +053040 elif self.doc.lead:
Nabin Hait9d1f0772014-02-19 17:43:24 +053041 from erpnext.selling.doctype.lead.lead import get_lead_details
42 self.doc.update_if_missing(get_lead_details(self.doc.lead))
Anand Doshi43624432014-02-25 15:42:16 +053043
Nabin Hait096d3632013-10-17 17:01:14 +053044 def set_price_list_and_item_details(self):
45 self.set_price_list_currency("Selling")
Nabin Hait0aa71a52014-02-11 16:14:52 +053046 self.set_missing_item_details()
Nabin Hait2bd37772013-07-08 19:00:29 +053047
Anand Doshi99100a42013-07-04 17:13:53 +053048 def apply_shipping_rule(self):
49 if self.doc.shipping_rule:
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053050 shipping_rule = frappe.bean("Shipping Rule", self.doc.shipping_rule)
Anand Doshi99100a42013-07-04 17:13:53 +053051 value = self.doc.net_total
52
53 # TODO
54 # shipping rule calculation based on item's net weight
55
56 shipping_amount = 0.0
57 for condition in shipping_rule.doclist.get({"parentfield": "shipping_rule_conditions"}):
58 if not condition.to_value or (flt(condition.from_value) <= value <= flt(condition.to_value)):
59 shipping_amount = condition.shipping_amount
60 break
Anand Doshi0b4943c2013-07-04 23:45:22 +053061
62 self.doclist.append({
63 "doctype": "Sales Taxes and Charges",
64 "parentfield": "other_charges",
65 "charge_type": "Actual",
66 "account_head": shipping_rule.doc.account,
67 "cost_center": shipping_rule.doc.cost_center,
68 "description": shipping_rule.doc.label,
69 "rate": shipping_amount
70 })
Anand Doshifc777182013-05-27 19:29:07 +053071
Nabin Haitec52db82013-01-22 11:53:14 +053072 def set_total_in_words(self):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053073 from frappe.utils import money_in_words
Nabin Haitec52db82013-01-22 11:53:14 +053074 company_currency = get_company_currency(self.doc.company)
Nabin Hait6f1a5ec2013-02-20 16:25:05 +053075
Anand Doshie9baaa62014-02-26 12:35:33 +053076 disable_rounded_total = cint(frappe.db.get_value("Global Defaults", None,
Nabin Hait6f1a5ec2013-02-20 16:25:05 +053077 "disable_rounded_total"))
78
Nabin Haitec52db82013-01-22 11:53:14 +053079 if self.meta.get_field("in_words"):
Nabin Hait6f1a5ec2013-02-20 16:25:05 +053080 self.doc.in_words = money_in_words(disable_rounded_total and
81 self.doc.grand_total or self.doc.rounded_total, company_currency)
Nabin Haitec52db82013-01-22 11:53:14 +053082 if self.meta.get_field("in_words_export"):
Nabin Hait6f1a5ec2013-02-20 16:25:05 +053083 self.doc.in_words_export = money_in_words(disable_rounded_total and
Nabin Hait8c7234f2013-03-11 16:32:33 +053084 self.doc.grand_total_export or self.doc.rounded_total_export, self.doc.currency)
Anand Doshi21f4ea32013-05-10 18:08:32 +053085
86 def calculate_taxes_and_totals(self):
Anand Doshi3543f302013-05-24 19:25:01 +053087 self.other_fname = "other_charges"
Anand Doshi21f4ea32013-05-10 18:08:32 +053088
Anand Doshi3543f302013-05-24 19:25:01 +053089 super(SellingController, self).calculate_taxes_and_totals()
90
Anand Doshi923d41d2013-05-28 17:23:36 +053091 self.calculate_total_advance("Sales Invoice", "advance_adjustment_details")
Anand Doshif3096132013-05-21 19:35:06 +053092 self.calculate_commission()
93 self.calculate_contribution()
Anand Doshi3543f302013-05-24 19:25:01 +053094
Anand Doshif3096132013-05-21 19:35:06 +053095 def determine_exclusive_rate(self):
Anand Doshi21f4ea32013-05-10 18:08:32 +053096 if not any((cint(tax.included_in_print_rate) for tax in self.tax_doclist)):
97 # no inclusive tax
98 return
99
100 for item in self.item_doclist:
101 item_tax_map = self._load_item_tax_rate(item.item_tax_rate)
102 cumulated_tax_fraction = 0
103 for i, tax in enumerate(self.tax_doclist):
Anand Doshif3096132013-05-21 19:35:06 +0530104 tax.tax_fraction_for_current_item = self.get_current_tax_fraction(tax, item_tax_map)
105
Anand Doshi21f4ea32013-05-10 18:08:32 +0530106 if i==0:
Anand Doshif3096132013-05-21 19:35:06 +0530107 tax.grand_total_fraction_for_current_item = 1 + tax.tax_fraction_for_current_item
Anand Doshi21f4ea32013-05-10 18:08:32 +0530108 else:
109 tax.grand_total_fraction_for_current_item = \
110 self.tax_doclist[i-1].grand_total_fraction_for_current_item \
111 + tax.tax_fraction_for_current_item
112
113 cumulated_tax_fraction += tax.tax_fraction_for_current_item
114
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530115 if cumulated_tax_fraction and not self.discount_amount_applied:
Nabin Hait1eb56012014-02-10 19:20:15 +0530116 item.base_amount = flt((item.amount * self.doc.conversion_rate) /
117 (1 + cumulated_tax_fraction), self.precision("base_amount", item))
Anand Doshifbe1e162013-08-06 19:38:19 +0530118
Nabin Hait1eb56012014-02-10 19:20:15 +0530119 item.base_rate = flt(item.base_amount / item.qty, self.precision("base_rate", item))
Anand Doshi21f4ea32013-05-10 18:08:32 +0530120
Nabin Haita7f757a2014-02-10 17:54:04 +0530121 if item.discount_percentage == 100:
Nabin Hait7979f7e2014-02-10 18:26:49 +0530122 item.base_price_list_rate = item.base_rate
123 item.base_rate = 0.0
Anand Doshif3096132013-05-21 19:35:06 +0530124 else:
Nabin Hait7979f7e2014-02-10 18:26:49 +0530125 item.base_price_list_rate = flt(item.base_rate / (1 - (item.discount_percentage / 100.0)),
Nabin Haita7f757a2014-02-10 17:54:04 +0530126 self.precision("base_price_list_rate", item))
Anand Doshi21f4ea32013-05-10 18:08:32 +0530127
128 def get_current_tax_fraction(self, tax, item_tax_map):
129 """
130 Get tax fraction for calculating tax exclusive amount
131 from tax inclusive amount
132 """
133 current_tax_fraction = 0
134
135 if cint(tax.included_in_print_rate):
136 tax_rate = self._get_tax_rate(tax, item_tax_map)
137
138 if tax.charge_type == "On Net Total":
139 current_tax_fraction = tax_rate / 100.0
140
141 elif tax.charge_type == "On Previous Row Amount":
142 current_tax_fraction = (tax_rate / 100.0) * \
143 self.tax_doclist[cint(tax.row_id) - 1].tax_fraction_for_current_item
144
145 elif tax.charge_type == "On Previous Row Total":
146 current_tax_fraction = (tax_rate / 100.0) * \
147 self.tax_doclist[cint(tax.row_id) - 1].grand_total_fraction_for_current_item
148
149 return current_tax_fraction
150
151 def calculate_item_values(self):
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530152 if not self.discount_amount_applied:
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530153 for item in self.item_doclist:
154 self.round_floats_in(item)
Rushabh Mehta4dca4012013-07-25 17:45:59 +0530155
Nabin Haita7f757a2014-02-10 17:54:04 +0530156 if item.discount_percentage == 100:
Nabin Hait7979f7e2014-02-10 18:26:49 +0530157 item.rate = 0
158 elif not item.rate:
159 item.rate = flt(item.price_list_rate * (1.0 - (item.discount_percentage / 100.0)),
160 self.precision("rate", item))
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530161
Nabin Hait1eb56012014-02-10 19:20:15 +0530162 item.amount = flt(item.rate * item.qty,
163 self.precision("amount", item))
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530164
Nabin Haita7f757a2014-02-10 17:54:04 +0530165 self._set_in_company_currency(item, "price_list_rate", "base_price_list_rate")
Nabin Hait7979f7e2014-02-10 18:26:49 +0530166 self._set_in_company_currency(item, "rate", "base_rate")
Nabin Hait1eb56012014-02-10 19:20:15 +0530167 self._set_in_company_currency(item, "amount", "base_amount")
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530168
Anand Doshi21f4ea32013-05-10 18:08:32 +0530169 def calculate_net_total(self):
Anand Doshif3096132013-05-21 19:35:06 +0530170 self.doc.net_total = self.doc.net_total_export = 0.0
Anand Doshi21f4ea32013-05-10 18:08:32 +0530171
172 for item in self.item_doclist:
Nabin Hait1eb56012014-02-10 19:20:15 +0530173 self.doc.net_total += item.base_amount
174 self.doc.net_total_export += item.amount
Anand Doshif3096132013-05-21 19:35:06 +0530175
176 self.round_floats_in(self.doc, ["net_total", "net_total_export"])
Anand Doshi21f4ea32013-05-10 18:08:32 +0530177
178 def calculate_totals(self):
179 self.doc.grand_total = flt(self.tax_doclist and \
Anand Doshi5af812a2013-05-10 19:23:02 +0530180 self.tax_doclist[-1].total or self.doc.net_total, self.precision("grand_total"))
Anand Doshi21f4ea32013-05-10 18:08:32 +0530181 self.doc.grand_total_export = flt(self.doc.grand_total / self.doc.conversion_rate,
Anand Doshi5af812a2013-05-10 19:23:02 +0530182 self.precision("grand_total_export"))
Anand Doshif3096132013-05-21 19:35:06 +0530183
184 self.doc.other_charges_total = flt(self.doc.grand_total - self.doc.net_total,
185 self.precision("other_charges_total"))
Nabin Hait5c6d13a2014-01-20 17:18:16 +0530186
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530187 self.doc.other_charges_total_export = flt(self.doc.grand_total_export -
Nabin Hait5c6d13a2014-01-20 17:18:16 +0530188 self.doc.net_total_export + flt(self.doc.discount_amount),
Anand Doshif3096132013-05-21 19:35:06 +0530189 self.precision("other_charges_total_export"))
Anand Doshi21f4ea32013-05-10 18:08:32 +0530190
Anand Doshi2e31e092013-08-19 19:58:23 +0530191 self.doc.rounded_total = _round(self.doc.grand_total)
192 self.doc.rounded_total_export = _round(self.doc.grand_total_export)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530193
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530194 def apply_discount_amount(self):
195 if self.doc.discount_amount:
196 grand_total_for_discount_amount = self.get_grand_total_for_discount_amount()
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530197
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530198 if grand_total_for_discount_amount:
199 # calculate item amount after Discount Amount
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530200 for item in self.item_doclist:
Nabin Hait1eb56012014-02-10 19:20:15 +0530201 distributed_amount = flt(self.doc.discount_amount) * item.base_amount / grand_total_for_discount_amount
202 item.base_amount = flt(item.base_amount - distributed_amount, self.precision("base_amount", item))
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530203
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530204 self.discount_amount_applied = True
205 self._calculate_taxes_and_totals()
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530206
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530207 def get_grand_total_for_discount_amount(self):
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530208 actual_taxes_dict = {}
209
210 for tax in self.tax_doclist:
211 if tax.charge_type == "Actual":
212 actual_taxes_dict.setdefault(tax.idx, tax.tax_amount)
213 elif tax.row_id in actual_taxes_dict:
214 actual_tax_amount = flt(actual_taxes_dict.get(tax.row_id, 0)) * \
215 flt(tax.rate) / 100
216 actual_taxes_dict.setdefault(tax.idx, actual_tax_amount)
217
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530218 grand_total_for_discount_amount = flt(self.doc.grand_total - sum(actual_taxes_dict.values()),
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530219 self.precision("grand_total"))
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530220 return grand_total_for_discount_amount
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530221
Anand Doshi923d41d2013-05-28 17:23:36 +0530222 def calculate_outstanding_amount(self):
223 # NOTE:
224 # write_off_amount is only for POS Invoice
225 # total_advance is only for non POS Invoice
Nabin Hait2f8d4452014-02-20 14:59:37 +0530226 if self.doc.doctype == "Sales Invoice" and self.doc.docstatus == 0:
Anand Doshi923d41d2013-05-28 17:23:36 +0530227 self.round_floats_in(self.doc, ["grand_total", "total_advance", "write_off_amount",
228 "paid_amount"])
229 total_amount_to_pay = self.doc.grand_total - self.doc.write_off_amount
Rushabh Mehta54047782013-12-26 11:07:46 +0530230 self.doc.outstanding_amount = flt(total_amount_to_pay - self.doc.total_advance \
231 - self.doc.paid_amount, self.precision("outstanding_amount"))
Anand Doshi923d41d2013-05-28 17:23:36 +0530232
233 def calculate_commission(self):
234 if self.meta.get_field("commission_rate"):
235 self.round_floats_in(self.doc, ["net_total", "commission_rate"])
236 if self.doc.commission_rate > 100.0:
237 msgprint(_(self.meta.get_label("commission_rate")) + " " +
238 _("cannot be greater than 100"), raise_exception=True)
239
240 self.doc.total_commission = flt(self.doc.net_total * self.doc.commission_rate / 100.0,
241 self.precision("total_commission"))
Anand Doshif3096132013-05-21 19:35:06 +0530242
243 def calculate_contribution(self):
244 total = 0.0
245 sales_team = self.doclist.get({"parentfield": "sales_team"})
246 for sales_person in sales_team:
247 self.round_floats_in(sales_person)
248
249 sales_person.allocated_amount = flt(
250 self.doc.net_total * sales_person.allocated_percentage / 100.0,
251 self.precision("allocated_amount", sales_person))
252
253 total += sales_person.allocated_percentage
254
255 if sales_team and total != 100.0:
256 msgprint(_("Total") + " " +
257 _(self.meta.get_label("allocated_percentage", parentfield="sales_team")) +
258 " " + _("should be 100%"), raise_exception=True)
Anand Doshi1dde46a2013-05-15 21:15:57 +0530259
260 def validate_order_type(self):
Anand Doshiabc10032013-06-14 17:44:03 +0530261 valid_types = ["Sales", "Maintenance", "Shopping Cart"]
Rushabh Mehta080fcc82013-07-04 15:27:04 +0530262 if not self.doc.order_type:
263 self.doc.order_type = "Sales"
264 elif self.doc.order_type not in valid_types:
Anand Doshi1dde46a2013-05-15 21:15:57 +0530265 msgprint(_(self.meta.get_label("order_type")) + " " +
Anand Doshi373680b2013-10-10 16:04:40 +0530266 _("must be one of") + ": " + comma_or(valid_types), raise_exception=True)
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530267
268 def check_credit(self, grand_total):
Anand Doshie9baaa62014-02-26 12:35:33 +0530269 customer_account = frappe.db.get_value("Account", {"company": self.doc.company,
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530270 "master_name": self.doc.customer}, "name")
271 if customer_account:
Anand Doshie9baaa62014-02-26 12:35:33 +0530272 total_outstanding = frappe.db.sql("""select
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530273 sum(ifnull(debit, 0)) - sum(ifnull(credit, 0))
Anand Doshi0c26f812013-11-15 19:12:09 +0530274 from `tabGL Entry` where account = %s""", customer_account)
275 total_outstanding = total_outstanding[0][0] if total_outstanding else 0
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530276
277 outstanding_including_current = flt(total_outstanding) + flt(grand_total)
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530278 frappe.bean('Account', customer_account).run_method("check_credit_limit",
Anand Doshi0c26f812013-11-15 19:12:09 +0530279 outstanding_including_current)
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530280
281 def validate_max_discount(self):
282 for d in self.doclist.get({"parentfield": self.fname}):
Anand Doshie9baaa62014-02-26 12:35:33 +0530283 discount = flt(frappe.db.get_value("Item", d.item_code, "max_discount"))
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530284
Nabin Haita7f757a2014-02-10 17:54:04 +0530285 if discount and flt(d.discount_percentage) > discount:
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530286 frappe.throw(_("You cannot give more than ") + cstr(discount) + "% " +
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530287 _("discount on Item Code") + ": " + cstr(d.item_code))
288
289 def get_item_list(self):
290 il = []
291 for d in self.doclist.get({"parentfield": self.fname}):
Nabin Hait0aa71a52014-02-11 16:14:52 +0530292 reserved_warehouse = ""
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530293 reserved_qty_for_main_item = 0
294
295 if self.doc.doctype == "Sales Order":
Anand Doshie9baaa62014-02-26 12:35:33 +0530296 if (frappe.db.get_value("Item", d.item_code, "is_stock_item") == 'Yes' or
Nabin Haita7f757a2014-02-10 17:54:04 +0530297 self.has_sales_bom(d.item_code)) and not d.warehouse:
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530298 frappe.throw(_("Please enter Reserved Warehouse for item ") +
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530299 d.item_code + _(" as it is stock Item or packing item"))
Nabin Hait0aa71a52014-02-11 16:14:52 +0530300 reserved_warehouse = d.warehouse
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530301 if flt(d.qty) > flt(d.delivered_qty):
302 reserved_qty_for_main_item = flt(d.qty) - flt(d.delivered_qty)
303
304 if self.doc.doctype == "Delivery Note" and d.against_sales_order:
305 # if SO qty is 10 and there is tolerance of 20%, then it will allow DN of 12.
306 # But in this case reserved qty should only be reduced by 10 and not 12
307
308 already_delivered_qty = self.get_already_delivered_qty(self.doc.name,
309 d.against_sales_order, d.prevdoc_detail_docname)
Nabin Hait0aa71a52014-02-11 16:14:52 +0530310 so_qty, reserved_warehouse = self.get_so_qty_and_warehouse(d.prevdoc_detail_docname)
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530311
312 if already_delivered_qty + d.qty > so_qty:
313 reserved_qty_for_main_item = -(so_qty - already_delivered_qty)
314 else:
315 reserved_qty_for_main_item = -flt(d.qty)
316
317 if self.has_sales_bom(d.item_code):
318 for p in self.doclist.get({"parentfield": "packing_details"}):
319 if p.parent_detail_docname == d.name and p.parent_item == d.item_code:
320 # the packing details table's qty is already multiplied with parent's qty
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530321 il.append(frappe._dict({
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530322 'warehouse': p.warehouse,
Nabin Hait0aa71a52014-02-11 16:14:52 +0530323 'reserved_warehouse': reserved_warehouse,
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530324 'item_code': p.item_code,
325 'qty': flt(p.qty),
326 'reserved_qty': (flt(p.qty)/flt(d.qty)) * reserved_qty_for_main_item,
327 'uom': p.uom,
328 'batch_no': cstr(p.batch_no).strip(),
329 'serial_no': cstr(p.serial_no).strip(),
330 'name': d.name
331 }))
332 else:
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530333 il.append(frappe._dict({
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530334 'warehouse': d.warehouse,
Nabin Hait0aa71a52014-02-11 16:14:52 +0530335 'reserved_warehouse': reserved_warehouse,
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530336 'item_code': d.item_code,
337 'qty': d.qty,
338 'reserved_qty': reserved_qty_for_main_item,
339 'uom': d.stock_uom,
340 'batch_no': cstr(d.batch_no).strip(),
341 'serial_no': cstr(d.serial_no).strip(),
342 'name': d.name
343 }))
344 return il
345
346 def has_sales_bom(self, item_code):
Anand Doshie9baaa62014-02-26 12:35:33 +0530347 return frappe.db.sql("""select name from `tabSales BOM`
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530348 where new_item_code=%s and docstatus != 2""", item_code)
349
350 def get_already_delivered_qty(self, dn, so, so_detail):
Anand Doshie9baaa62014-02-26 12:35:33 +0530351 qty = frappe.db.sql("""select sum(qty) from `tabDelivery Note Item`
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530352 where prevdoc_detail_docname = %s and docstatus = 1
353 and against_sales_order = %s
354 and parent != %s""", (so_detail, so, dn))
355 return qty and flt(qty[0][0]) or 0.0
356
357 def get_so_qty_and_warehouse(self, so_detail):
Anand Doshie9baaa62014-02-26 12:35:33 +0530358 so_item = frappe.db.sql("""select qty, warehouse from `tabSales Order Item`
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530359 where name = %s and docstatus = 1""", so_detail, as_dict=1)
360 so_qty = so_item and flt(so_item[0]["qty"]) or 0.0
Nabin Haita7f757a2014-02-10 17:54:04 +0530361 so_warehouse = so_item and so_item[0]["warehouse"] or ""
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530362 return so_qty, so_warehouse
363
364 def check_stop_sales_order(self, ref_fieldname):
365 for d in self.doclist.get({"parentfield": self.fname}):
366 if d.fields.get(ref_fieldname):
Anand Doshie9baaa62014-02-26 12:35:33 +0530367 status = frappe.db.get_value("Sales Order", d.fields[ref_fieldname], "status")
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530368 if status == "Stopped":
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530369 frappe.throw(self.doc.doctype +
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530370 _(" can not be created/modified against stopped Sales Order ") +
371 d.fields[ref_fieldname])
372
373def check_active_sales_items(obj):
374 for d in obj.doclist.get({"parentfield": obj.fname}):
375 if d.item_code:
Anand Doshie9baaa62014-02-26 12:35:33 +0530376 item = frappe.db.sql("""select docstatus, is_sales_item,
Nabin Haiteba1bdb2014-02-12 16:04:17 +0530377 is_service_item, income_account from tabItem where name = %s""",
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530378 d.item_code, as_dict=True)[0]
379 if item.is_sales_item == 'No' and item.is_service_item == 'No':
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530380 frappe.throw(_("Item is neither Sales nor Service Item") + ": " + d.item_code)
Nabin Haiteba1bdb2014-02-12 16:04:17 +0530381 if d.income_account and not item.income_account:
Anand Doshie9baaa62014-02-26 12:35:33 +0530382 frappe.db.set_value("Item", d.item_code, "income_account",
Nabin Hait89ad6f22013-10-18 12:55:59 +0530383 d.income_account)