blob: 536112662d58bee6a8889a96194eb66e2d7b5c9a [file] [log] [blame]
Anand Doshi885e0742015-03-03 14:55:30 +05301# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302# License: GNU General Public License v3. See license.txt
Anand Doshi756dca72013-01-15 18:39:21 +05303
4from __future__ import unicode_literals
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05305import frappe
6from frappe import _, msgprint
Nabin Hait3237c752015-02-17 11:11:11 +05307from frappe.utils import flt
Sambhaji Kolatee3d26432014-09-10 13:07:59 +05308
Rushabh Mehta1f847992013-12-12 19:12:19 +05309from erpnext.setup.utils import get_company_currency
Rushabh Mehtacc008cc2014-02-03 16:14:56 +053010from erpnext.accounts.party import get_party_details
Rushabh Mehta724f9e52014-10-07 15:29:58 +053011from erpnext.stock.get_item_details import get_conversion_factor
Anand Doshi756dca72013-01-15 18:39:21 +053012
Rushabh Mehta1f847992013-12-12 19:12:19 +053013from erpnext.controllers.stock_controller import StockController
Nabin Haitbf495c92013-01-30 12:49:08 +053014
Nabin Hait89a94d82013-03-19 12:01:46 +053015class BuyingController(StockController):
Rushabh Mehta5b51cc82014-07-21 18:25:45 +053016 def __setup__(self):
Nabin Haitde9c8a92015-02-23 01:06:00 +053017 if hasattr(self, "taxes"):
Rushabh Mehtac567e8e2015-02-03 17:55:52 +053018 self.print_templates = {
19 "taxes": "templates/print_formats/includes/taxes.html"
Rushabh Mehta5b51cc82014-07-21 18:25:45 +053020 }
21
Rushabh Mehtac0bb4532014-09-09 16:15:35 +053022 def get_feed(self):
rohitwaghchaurea1064a62016-03-03 14:00:35 +053023 if self.get("supplier_name"):
24 return _("From {0} | {1} {2}").format(self.supplier_name, self.currency,
25 self.grand_total)
Rushabh Mehtac0bb4532014-09-09 16:15:35 +053026
Saurabh6f753182013-03-20 12:55:28 +053027 def validate(self):
28 super(BuyingController, self).validate()
Nabin Hait365ae272014-04-03 17:38:54 +053029 if getattr(self, "supplier", None) and not self.supplier_name:
Nabin Hait1d218422015-07-17 15:19:02 +053030 self.supplier_name = frappe.db.get_value("Supplier", self.supplier, "supplier_name")
Nabin Hait5566bca2013-10-18 17:00:53 +053031 self.is_item_table_empty()
nabinhait614fb752014-07-14 10:47:50 +053032 self.set_qty_as_per_stock_uom()
Nabin Hait205f7ce2013-04-26 13:35:06 +053033 self.validate_stock_or_nonstock_items()
Anand Doshi373680b2013-10-10 16:04:40 +053034 self.validate_warehouse()
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053035
Anand Doshi3543f302013-05-24 19:25:01 +053036 def set_missing_values(self, for_validate=False):
Anand Doshiabc10032013-06-14 17:44:03 +053037 super(BuyingController, self).set_missing_values(for_validate)
38
Rushabh Mehta0b995402013-08-09 15:29:59 +053039 self.set_supplier_from_item_default()
Nabin Hait096d3632013-10-17 17:01:14 +053040 self.set_price_list_currency("Buying")
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053041
Anand Doshi3543f302013-05-24 19:25:01 +053042 # set contact and address details for supplier, if they are not mentioned
Nabin Hait365ae272014-04-03 17:38:54 +053043 if getattr(self, "supplier", None):
rohitwaghchaurea1064a62016-03-03 14:00:35 +053044 self.update_if_missing(get_party_details(self.supplier, party_type="Supplier", ignore_permissions=self.flags.ignore_permissions))
Rushabh Mehta886c9ef2013-07-08 12:08:06 +053045
Nabin Hait0aa71a52014-02-11 16:14:52 +053046 self.set_missing_item_details()
Rushabh Mehta436467a2013-07-08 12:37:59 +053047
Rushabh Mehta0b995402013-08-09 15:29:59 +053048 def set_supplier_from_item_default(self):
Anand Doshif78d1ae2014-03-28 13:55:00 +053049 if self.meta.get_field("supplier") and not self.supplier:
Nabin Haitdd38a262014-12-26 13:15:21 +053050 for d in self.get("items"):
Anand Doshie9baaa62014-02-26 12:35:33 +053051 supplier = frappe.db.get_value("Item", d.item_code, "default_supplier")
Rushabh Mehta0b995402013-08-09 15:29:59 +053052 if supplier:
Anand Doshif78d1ae2014-03-28 13:55:00 +053053 self.supplier = supplier
Rushabh Mehta0b995402013-08-09 15:29:59 +053054 break
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053055
Nabin Hait205f7ce2013-04-26 13:35:06 +053056 def validate_stock_or_nonstock_items(self):
Nabin Haite7d15362014-12-25 16:01:55 +053057 if self.meta.get_field("taxes") and not self.get_stock_items():
58 tax_for_valuation = [d.account_head for d in self.get("taxes")
Nabin Hait205f7ce2013-04-26 13:35:06 +053059 if d.category in ["Valuation", "Valuation and Total"]]
60 if tax_for_valuation:
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053061 frappe.throw(_("Tax Category can not be 'Valuation' or 'Valuation and Total' as all items are non-stock items"))
62
Nabin Haitd3b62502013-01-21 17:24:31 +053063 def set_total_in_words(self):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053064 from frappe.utils import money_in_words
Anand Doshif78d1ae2014-03-28 13:55:00 +053065 company_currency = get_company_currency(self.company)
Nabin Hait5690be12015-02-12 16:09:11 +053066 if self.meta.get_field("base_in_words"):
67 self.base_in_words = money_in_words(self.base_grand_total, company_currency)
Nabin Hait5b4c2942013-01-22 11:12:02 +053068 if self.meta.get_field("in_words"):
Nabin Hait188f69a2015-02-12 17:55:50 +053069 self.in_words = money_in_words(self.grand_total, self.currency)
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053070
Nabin Haitdc15b4f2014-01-20 16:48:49 +053071 # update valuation rate
72 def update_valuation_rate(self, parentfield):
Anand Doshi8595fcf2013-02-08 19:28:14 +053073 """
74 item_tax_amount is the total tax amount applied on that item
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053075 stored for valuation
76
Anand Doshi8595fcf2013-02-08 19:28:14 +053077 TODO: rename item_tax_amount to valuation_tax_amount
78 """
Nabin Haitdc15b4f2014-01-20 16:48:49 +053079 stock_items = self.get_stock_items()
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053080
Nabin Haitdc15b4f2014-01-20 16:48:49 +053081 stock_items_qty, stock_items_amount = 0, 0
82 last_stock_item_idx = 1
Rushabh Mehtad2b34dc2014-03-27 16:12:56 +053083 for d in self.get(parentfield):
Nabin Haitdc15b4f2014-01-20 16:48:49 +053084 if d.item_code and d.item_code in stock_items:
85 stock_items_qty += flt(d.qty)
Nabin Hait82e3e252015-02-23 16:58:30 +053086 stock_items_amount += flt(d.base_net_amount)
Nabin Haitdc15b4f2014-01-20 16:48:49 +053087 last_stock_item_idx = d.idx
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053088
Nabin Hait82e3e252015-02-23 16:58:30 +053089 total_valuation_amount = sum([flt(d.base_tax_amount_after_discount_amount) for d in self.get("taxes")
Nabin Haitdc15b4f2014-01-20 16:48:49 +053090 if d.category in ["Valuation", "Valuation and Total"]])
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053091
Nabin Haitdc15b4f2014-01-20 16:48:49 +053092 valuation_amount_adjustment = total_valuation_amount
Rushabh Mehtad2b34dc2014-03-27 16:12:56 +053093 for i, item in enumerate(self.get(parentfield)):
Nabin Haitdc15b4f2014-01-20 16:48:49 +053094 if item.item_code and item.qty and item.item_code in stock_items:
Nabin Hait82e3e252015-02-23 16:58:30 +053095 item_proportion = flt(item.base_net_amount) / stock_items_amount if stock_items_amount \
Nabin Haitdc15b4f2014-01-20 16:48:49 +053096 else flt(item.qty) / stock_items_qty
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053097
Nabin Haitdc15b4f2014-01-20 16:48:49 +053098 if i == (last_stock_item_idx - 1):
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053099 item.item_tax_amount = flt(valuation_amount_adjustment,
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530100 self.precision("item_tax_amount", item))
101 else:
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530102 item.item_tax_amount = flt(item_proportion * total_valuation_amount,
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530103 self.precision("item_tax_amount", item))
104 valuation_amount_adjustment -= item.item_tax_amount
105
Anand Doshi39384d32013-05-11 19:39:53 +0530106 self.round_floats_in(item)
bobzz-zoneb4c7bad2015-08-05 11:30:12 +0700107 if flt(item.conversion_factor)==0:
108 item.conversion_factor = get_conversion_factor(item.item_code, item.uom).get("conversion_factor") or 1.0
Rushabh Mehta724f9e52014-10-07 15:29:58 +0530109
Rushabh Mehta54047782013-12-26 11:07:46 +0530110 qty_in_stock_uom = flt(item.qty * item.conversion_factor)
nabinhait87f24012014-07-16 19:48:29 +0530111 rm_supp_cost = flt(item.rm_supp_cost) if self.doctype=="Purchase Receipt" else 0.0
nabinhaitcc0692d2014-07-17 19:16:38 +0530112
nabinhait87f24012014-07-16 19:48:29 +0530113 landed_cost_voucher_amount = flt(item.landed_cost_voucher_amount) \
114 if self.doctype == "Purchase Receipt" else 0.0
Nabin Hait14b8af22014-08-28 12:42:28 +0530115
Nabin Hait82e3e252015-02-23 16:58:30 +0530116 item.valuation_rate = ((item.base_net_amount + item.item_tax_amount + rm_supp_cost
nabinhait87f24012014-07-16 19:48:29 +0530117 + landed_cost_voucher_amount) / qty_in_stock_uom)
Anand Doshi4a7248e2013-02-27 18:10:30 +0530118 else:
Anand Doshi5af812a2013-05-10 19:23:02 +0530119 item.valuation_rate = 0.0
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530120
Nabin Hait54d209f2013-03-01 18:51:10 +0530121 def validate_for_subcontracting(self):
Anand Doshif78d1ae2014-03-28 13:55:00 +0530122 if not self.is_subcontracted and self.sub_contracted_items:
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530123 frappe.throw(_("Please enter 'Is Subcontracted' as Yes or No"))
124
ankitjavalkarwork5edae0e2014-11-05 20:14:53 +0530125 if self.is_subcontracted == "Yes":
126 if self.doctype == "Purchase Receipt" and not self.supplier_warehouse:
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530127 frappe.throw(_("Supplier Warehouse mandatory for sub-contracted Purchase Receipt"))
128
Nabin Haitdd38a262014-12-26 13:15:21 +0530129 for item in self.get("items"):
ankitjavalkarwork5edae0e2014-11-05 20:14:53 +0530130 if item in self.sub_contracted_items and not item.bom:
131 frappe.throw(_("Please select BOM in BOM field for Item {0}").format(item.item_code))
132
133 else:
Nabin Haitdd38a262014-12-26 13:15:21 +0530134 for item in self.get("items"):
ankitjavalkarwork5edae0e2014-11-05 20:14:53 +0530135 if item.bom:
136 item.bom = None
137
Nabin Hait344f4432014-05-08 19:08:20 +0530138 def create_raw_materials_supplied(self, raw_material_table):
Anand Doshif78d1ae2014-03-28 13:55:00 +0530139 if self.is_subcontracted=="Yes":
Nabin Hait344f4432014-05-08 19:08:20 +0530140 parent_items = []
Nabin Haitdd38a262014-12-26 13:15:21 +0530141 for item in self.get("items"):
Nabin Haita0c239d2014-04-01 18:54:38 +0530142 if self.doctype == "Purchase Receipt":
143 item.rm_supp_cost = 0.0
Nabin Hait54d209f2013-03-01 18:51:10 +0530144 if item.item_code in self.sub_contracted_items:
Rushabh Mehtae693f792015-03-03 15:50:03 +0530145 self.update_raw_materials_supplied(item, raw_material_table)
Nabin Hait344f4432014-05-08 19:08:20 +0530146
147 if [item.item_code, item.name] not in parent_items:
148 parent_items.append([item.item_code, item.name])
149
150 self.cleanup_raw_materials_supplied(parent_items, raw_material_table)
Nabin Hait54d209f2013-03-01 18:51:10 +0530151
Nabin Haita0c239d2014-04-01 18:54:38 +0530152 elif self.doctype == "Purchase Receipt":
Nabin Haitdd38a262014-12-26 13:15:21 +0530153 for item in self.get("items"):
Nabin Haita0c239d2014-04-01 18:54:38 +0530154 item.rm_supp_cost = 0.0
155
Rushabh Mehtae693f792015-03-03 15:50:03 +0530156 def update_raw_materials_supplied(self, item, raw_material_table):
ankitjavalkarwork5edae0e2014-11-05 20:14:53 +0530157 bom_items = self.get_items_from_bom(item.item_code, item.bom)
Nabin Hait54d209f2013-03-01 18:51:10 +0530158 raw_materials_cost = 0
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530159
Nabin Hait344f4432014-05-08 19:08:20 +0530160 for bom_item in bom_items:
161 # check if exists
162 exists = 0
163 for d in self.get(raw_material_table):
164 if d.main_item_code == item.item_code and d.rm_item_code == bom_item.item_code \
165 and d.reference_name == item.name:
166 rm, exists = d, 1
167 break
168
169 if not exists:
170 rm = self.append(raw_material_table, {})
171
172 required_qty = flt(bom_item.qty_consumed_per_unit) * flt(item.qty) * flt(item.conversion_factor)
173 rm.reference_name = item.name
174 rm.bom_detail_no = bom_item.name
175 rm.main_item_code = item.item_code
176 rm.rm_item_code = bom_item.item_code
177 rm.stock_uom = bom_item.stock_uom
178 rm.required_qty = required_qty
179
180 rm.conversion_factor = item.conversion_factor
Nabin Hait344f4432014-05-08 19:08:20 +0530181
Nabin Hait966edff2014-05-09 10:54:12 +0530182 if self.doctype == "Purchase Receipt":
Nabin Hait344f4432014-05-08 19:08:20 +0530183 rm.consumed_qty = required_qty
184 rm.description = bom_item.description
185 if item.batch_no and not rm.batch_no:
186 rm.batch_no = item.batch_no
187
Nabin Haite96e83d2014-10-08 18:06:14 +0530188 # get raw materials rate
Nabin Haitfce28812014-10-08 18:38:27 +0530189 if self.doctype == "Purchase Receipt":
190 from erpnext.stock.utils import get_incoming_rate
Nabin Haitfb6e4342014-10-15 11:34:40 +0530191 rm.rate = get_incoming_rate({
Nabin Haitfce28812014-10-08 18:38:27 +0530192 "item_code": bom_item.item_code,
193 "warehouse": self.supplier_warehouse,
194 "posting_date": self.posting_date,
195 "posting_time": self.posting_time,
196 "qty": -1 * required_qty,
197 "serial_no": rm.serial_no
198 })
Nabin Haitfb6e4342014-10-15 11:34:40 +0530199 if not rm.rate:
200 from erpnext.stock.stock_ledger import get_valuation_rate
201 rm.rate = get_valuation_rate(bom_item.item_code, self.supplier_warehouse)
Nabin Haitfce28812014-10-08 18:38:27 +0530202 else:
203 rm.rate = bom_item.rate
Nabin Haite96e83d2014-10-08 18:06:14 +0530204
Nabin Haitfce28812014-10-08 18:38:27 +0530205 rm.amount = required_qty * flt(rm.rate)
Nabin Haite96e83d2014-10-08 18:06:14 +0530206 raw_materials_cost += flt(rm.amount)
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530207
Anand Doshif78d1ae2014-03-28 13:55:00 +0530208 if self.doctype == "Purchase Receipt":
Nabin Hait344f4432014-05-08 19:08:20 +0530209 item.rm_supp_cost = raw_materials_cost
210
211 def cleanup_raw_materials_supplied(self, parent_items, raw_material_table):
212 """Remove all those child items which are no longer present in main item table"""
213 delete_list = []
214 for d in self.get(raw_material_table):
215 if [d.main_item_code, d.reference_name] not in parent_items:
216 # mark for deletion from doclist
Nabin Haitc3d1d6a2014-06-04 16:41:22 +0530217 delete_list.append(d)
Nabin Hait344f4432014-05-08 19:08:20 +0530218
219 # delete from doclist
220 if delete_list:
221 rm_supplied_details = self.get(raw_material_table)
222 self.set(raw_material_table, [])
223 for d in rm_supplied_details:
224 if d not in delete_list:
225 self.append(raw_material_table, d)
Nabin Hait54d209f2013-03-01 18:51:10 +0530226
ankitjavalkarwork5edae0e2014-11-05 20:14:53 +0530227 def get_items_from_bom(self, item_code, bom):
Nabin Hait14b8af22014-08-28 12:42:28 +0530228 bom_items = frappe.db.sql("""select t2.item_code,
Anand Doshi602e8252015-11-16 19:05:46 +0530229 t2.qty / ifnull(t1.quantity, 1) as qty_consumed_per_unit,
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530230 t2.rate, t2.stock_uom, t2.name, t2.description
Nabin Hait574d5ff2015-05-07 16:29:29 +0530231 from `tabBOM` t1, `tabBOM Item` t2, tabItem t3
ankitjavalkarwork5edae0e2014-11-05 20:14:53 +0530232 where t2.parent = t1.name and t1.item = %s
Rushabh Mehta1e8025b2015-07-24 15:16:25 +0530233 and t1.docstatus = 1 and t1.is_active = 1 and t1.name = %s
234 and t2.item_code = t3.name and t3.is_stock_item = 1""", (item_code, bom), as_dict=1)
ankitjavalkarwork5edae0e2014-11-05 20:14:53 +0530235
Nabin Hait54d209f2013-03-01 18:51:10 +0530236 if not bom_items:
ankitjavalkarwork5edae0e2014-11-05 20:14:53 +0530237 msgprint(_("Specified BOM {0} does not exist for Item {1}").format(bom, item_code), raise_exception=1)
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530238
Nabin Hait54d209f2013-03-01 18:51:10 +0530239 return bom_items
240
Nabin Hait5418d712013-02-27 18:11:17 +0530241 @property
242 def sub_contracted_items(self):
243 if not hasattr(self, "_sub_contracted_items"):
Nabin Haitebd51442013-04-23 15:36:26 +0530244 self._sub_contracted_items = []
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530245 item_codes = list(set(item.item_code for item in
Nabin Haitdd38a262014-12-26 13:15:21 +0530246 self.get("items")))
Nabin Haitebd51442013-04-23 15:36:26 +0530247 if item_codes:
Anand Doshie9baaa62014-02-26 12:35:33 +0530248 self._sub_contracted_items = [r[0] for r in frappe.db.sql("""select name
Rushabh Mehta1e8025b2015-07-24 15:16:25 +0530249 from `tabItem` where name in (%s) and is_sub_contracted_item=1""" % \
Nabin Haitebd51442013-04-23 15:36:26 +0530250 (", ".join((["%s"]*len(item_codes))),), item_codes)]
Nabin Hait5418d712013-02-27 18:11:17 +0530251
252 return self._sub_contracted_items
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530253
Nabin Hait5418d712013-02-27 18:11:17 +0530254 @property
255 def purchase_items(self):
256 if not hasattr(self, "_purchase_items"):
Nabin Haitebd51442013-04-23 15:36:26 +0530257 self._purchase_items = []
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530258 item_codes = list(set(item.item_code for item in
Nabin Haitdd38a262014-12-26 13:15:21 +0530259 self.get("items")))
Nabin Haitebd51442013-04-23 15:36:26 +0530260 if item_codes:
Anand Doshie9baaa62014-02-26 12:35:33 +0530261 self._purchase_items = [r[0] for r in frappe.db.sql("""select name
Nabin Haitebd51442013-04-23 15:36:26 +0530262 from `tabItem` where name in (%s) and is_purchase_item='Yes'""" % \
263 (", ".join((["%s"]*len(item_codes))),), item_codes)]
Nabin Hait5418d712013-02-27 18:11:17 +0530264
Anand Doshi55d9a622013-02-27 18:14:28 +0530265 return self._purchase_items
Nabin Hait5566bca2013-10-18 17:00:53 +0530266
Nabin Hait5566bca2013-10-18 17:00:53 +0530267 def is_item_table_empty(self):
Nabin Haitdd38a262014-12-26 13:15:21 +0530268 if not len(self.get("items")):
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530269 frappe.throw(_("Item table can not be blank"))
nabinhait614fb752014-07-14 10:47:50 +0530270
271 def set_qty_as_per_stock_uom(self):
Nabin Haitdd38a262014-12-26 13:15:21 +0530272 for d in self.get("items"):
Nabin Haitfd4bcd82015-05-22 16:55:40 +0530273 if d.meta.get_field("stock_qty"):
nabinhait614fb752014-07-14 10:47:50 +0530274 if not d.conversion_factor:
Nabin Haite6e456b2015-05-13 17:21:44 +0530275 frappe.throw(_("Row {0}: Conversion Factor is mandatory").format(d.idx))
Rushabh Mehta5b51cc82014-07-21 18:25:45 +0530276 d.stock_qty = flt(d.qty) * flt(d.conversion_factor)