blob: 553c7b345a98e5b3e13122a9d09aa1530a50ad85 [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
Saurabhe29248b2016-04-04 11:55:52 +05307from frappe.utils import flt,cint, cstr
Sambhaji Kolatee3d26432014-09-10 13:07:59 +05308
Rushabh Mehtacc008cc2014-02-03 16:14:56 +05309from erpnext.accounts.party import get_party_details
Rushabh Mehta724f9e52014-10-07 15:29:58 +053010from erpnext.stock.get_item_details import get_conversion_factor
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +053011from erpnext.buying.utils import validate_for_items
12from erpnext.stock.stock_ledger import get_valuation_rate
Anand Doshi756dca72013-01-15 18:39:21 +053013
Rushabh Mehta1f847992013-12-12 19:12:19 +053014from erpnext.controllers.stock_controller import StockController
Nabin Haitbf495c92013-01-30 12:49:08 +053015
Nabin Hait89a94d82013-03-19 12:01:46 +053016class BuyingController(StockController):
Rushabh Mehta5b51cc82014-07-21 18:25:45 +053017 def __setup__(self):
Nabin Haitde9c8a92015-02-23 01:06:00 +053018 if hasattr(self, "taxes"):
Rushabh Mehtac567e8e2015-02-03 17:55:52 +053019 self.print_templates = {
20 "taxes": "templates/print_formats/includes/taxes.html"
Rushabh Mehta5b51cc82014-07-21 18:25:45 +053021 }
22
Rushabh Mehtac0bb4532014-09-09 16:15:35 +053023 def get_feed(self):
rohitwaghchaurea1064a62016-03-03 14:00:35 +053024 if self.get("supplier_name"):
25 return _("From {0} | {1} {2}").format(self.supplier_name, self.currency,
26 self.grand_total)
Rushabh Mehtac0bb4532014-09-09 16:15:35 +053027
Saurabh6f753182013-03-20 12:55:28 +053028 def validate(self):
29 super(BuyingController, self).validate()
Nabin Hait365ae272014-04-03 17:38:54 +053030 if getattr(self, "supplier", None) and not self.supplier_name:
Nabin Hait1d218422015-07-17 15:19:02 +053031 self.supplier_name = frappe.db.get_value("Supplier", self.supplier, "supplier_name")
Rushabh Mehtaea0ff232016-07-07 14:02:26 +053032
nabinhait614fb752014-07-14 10:47:50 +053033 self.set_qty_as_per_stock_uom()
Nabin Hait205f7ce2013-04-26 13:35:06 +053034 self.validate_stock_or_nonstock_items()
Anand Doshi373680b2013-10-10 16:04:40 +053035 self.validate_warehouse()
Rushabh Mehtab33df4a2016-07-04 11:38:37 +053036
Nabin Hait14aa9c52016-04-18 15:54:01 +053037 if self.doctype=="Purchase Invoice":
38 self.validate_purchase_receipt_if_update_stock()
Rushabh Mehtab33df4a2016-07-04 11:38:37 +053039
Nabin Hait14aa9c52016-04-18 15:54:01 +053040 if self.doctype=="Purchase Receipt" or (self.doctype=="Purchase Invoice" and self.update_stock):
Rohit Waghchaure560ba392016-08-29 18:19:32 +053041 # self.validate_purchase_return()
Saurabhfbb342c2016-04-07 16:41:31 +053042 self.validate_rejected_warehouse()
43 self.validate_accepted_rejected_qty()
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +053044 validate_for_items(self)
Rushabh Mehtab33df4a2016-07-04 11:38:37 +053045
Saurabhfbb342c2016-04-07 16:41:31 +053046 #sub-contracting
47 self.validate_for_subcontracting()
48 self.create_raw_materials_supplied("supplied_items")
49 self.set_landed_cost_voucher_amount()
Rushabh Mehtab33df4a2016-07-04 11:38:37 +053050
Nabin Hait14aa9c52016-04-18 15:54:01 +053051 if self.doctype in ("Purchase Receipt", "Purchase Invoice"):
Saurabhfbb342c2016-04-07 16:41:31 +053052 self.update_valuation_rate("items")
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053053
Anand Doshi3543f302013-05-24 19:25:01 +053054 def set_missing_values(self, for_validate=False):
Anand Doshiabc10032013-06-14 17:44:03 +053055 super(BuyingController, self).set_missing_values(for_validate)
Rushabh Mehtab33df4a2016-07-04 11:38:37 +053056
Rushabh Mehta0b995402013-08-09 15:29:59 +053057 self.set_supplier_from_item_default()
Nabin Hait096d3632013-10-17 17:01:14 +053058 self.set_price_list_currency("Buying")
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053059
Anand Doshi3543f302013-05-24 19:25:01 +053060 # set contact and address details for supplier, if they are not mentioned
Nabin Hait365ae272014-04-03 17:38:54 +053061 if getattr(self, "supplier", None):
rohitwaghchaurea1064a62016-03-03 14:00:35 +053062 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 +053063
Nabin Haitcccc45e2016-10-05 17:15:43 +053064 self.set_missing_item_details(for_validate)
Rushabh Mehta436467a2013-07-08 12:37:59 +053065
Rushabh Mehta0b995402013-08-09 15:29:59 +053066 def set_supplier_from_item_default(self):
Anand Doshif78d1ae2014-03-28 13:55:00 +053067 if self.meta.get_field("supplier") and not self.supplier:
Nabin Haitdd38a262014-12-26 13:15:21 +053068 for d in self.get("items"):
Anand Doshie9baaa62014-02-26 12:35:33 +053069 supplier = frappe.db.get_value("Item", d.item_code, "default_supplier")
Rushabh Mehta0b995402013-08-09 15:29:59 +053070 if supplier:
Anand Doshif78d1ae2014-03-28 13:55:00 +053071 self.supplier = supplier
Rushabh Mehta0b995402013-08-09 15:29:59 +053072 break
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053073
Nabin Hait205f7ce2013-04-26 13:35:06 +053074 def validate_stock_or_nonstock_items(self):
Nabin Haite7d15362014-12-25 16:01:55 +053075 if self.meta.get_field("taxes") and not self.get_stock_items():
tundebabzy9a346202017-06-16 11:00:14 +010076 tax_for_valuation = [d for d in self.get("taxes")
Nabin Hait205f7ce2013-04-26 13:35:06 +053077 if d.category in ["Valuation", "Valuation and Total"]]
tundebabzy9a346202017-06-16 11:00:14 +010078
Nabin Hait205f7ce2013-04-26 13:35:06 +053079 if tax_for_valuation:
tundebabzy9a346202017-06-16 11:00:14 +010080 for d in tax_for_valuation:
81 d.category = 'Total'
82 msgprint(_('Tax Category has been changed to "Total" because all the Items are non-stock items'))
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053083
Saurabhfbb342c2016-04-07 16:41:31 +053084 def set_landed_cost_voucher_amount(self):
85 for d in self.get("items"):
86 lc_voucher_amount = frappe.db.sql("""select sum(applicable_charges)
87 from `tabLanded Cost Item`
88 where docstatus = 1 and purchase_receipt_item = %s""", d.name)
89 d.landed_cost_voucher_amount = lc_voucher_amount[0][0] if lc_voucher_amount else 0.0
Rushabh Mehtab33df4a2016-07-04 11:38:37 +053090
Nabin Haitd3b62502013-01-21 17:24:31 +053091 def set_total_in_words(self):
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053092 from frappe.utils import money_in_words
Nabin Hait5690be12015-02-12 16:09:11 +053093 if self.meta.get_field("base_in_words"):
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +053094 self.base_in_words = money_in_words(self.base_grand_total, self.company_currency)
Nabin Hait5b4c2942013-01-22 11:12:02 +053095 if self.meta.get_field("in_words"):
Nabin Hait188f69a2015-02-12 17:55:50 +053096 self.in_words = money_in_words(self.grand_total, self.currency)
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053097
Nabin Haitdc15b4f2014-01-20 16:48:49 +053098 # update valuation rate
99 def update_valuation_rate(self, parentfield):
Anand Doshi8595fcf2013-02-08 19:28:14 +0530100 """
101 item_tax_amount is the total tax amount applied on that item
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530102 stored for valuation
103
Anand Doshi8595fcf2013-02-08 19:28:14 +0530104 TODO: rename item_tax_amount to valuation_tax_amount
105 """
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530106 stock_items = self.get_stock_items()
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530107
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530108 stock_items_qty, stock_items_amount = 0, 0
109 last_stock_item_idx = 1
Rushabh Mehtad2b34dc2014-03-27 16:12:56 +0530110 for d in self.get(parentfield):
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530111 if d.item_code and d.item_code in stock_items:
112 stock_items_qty += flt(d.qty)
Nabin Hait82e3e252015-02-23 16:58:30 +0530113 stock_items_amount += flt(d.base_net_amount)
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530114 last_stock_item_idx = d.idx
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530115
Nabin Hait82e3e252015-02-23 16:58:30 +0530116 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 +0530117 if d.category in ["Valuation", "Valuation and Total"]])
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530118
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530119 valuation_amount_adjustment = total_valuation_amount
Rushabh Mehtad2b34dc2014-03-27 16:12:56 +0530120 for i, item in enumerate(self.get(parentfield)):
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530121 if item.item_code and item.qty and item.item_code in stock_items:
Nabin Hait82e3e252015-02-23 16:58:30 +0530122 item_proportion = flt(item.base_net_amount) / stock_items_amount if stock_items_amount \
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530123 else flt(item.qty) / stock_items_qty
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530124 if i == (last_stock_item_idx - 1):
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530125 item.item_tax_amount = flt(valuation_amount_adjustment,
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530126 self.precision("item_tax_amount", item))
127 else:
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530128 item.item_tax_amount = flt(item_proportion * total_valuation_amount,
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530129 self.precision("item_tax_amount", item))
130 valuation_amount_adjustment -= item.item_tax_amount
131
Anand Doshi39384d32013-05-11 19:39:53 +0530132 self.round_floats_in(item)
Rushabh Mehtaea0ff232016-07-07 14:02:26 +0530133 if flt(item.conversion_factor)==0.0:
bobzz-zoneb4c7bad2015-08-05 11:30:12 +0700134 item.conversion_factor = get_conversion_factor(item.item_code, item.uom).get("conversion_factor") or 1.0
Rushabh Mehta724f9e52014-10-07 15:29:58 +0530135
Rushabh Mehta54047782013-12-26 11:07:46 +0530136 qty_in_stock_uom = flt(item.qty * item.conversion_factor)
Saurabhfbb342c2016-04-07 16:41:31 +0530137 rm_supp_cost = flt(item.rm_supp_cost) if self.doctype in ["Purchase Receipt", "Purchase Invoice"] else 0.0
nabinhaitcc0692d2014-07-17 19:16:38 +0530138
nabinhait87f24012014-07-16 19:48:29 +0530139 landed_cost_voucher_amount = flt(item.landed_cost_voucher_amount) \
Saurabhfbb342c2016-04-07 16:41:31 +0530140 if self.doctype in ["Purchase Receipt", "Purchase Invoice"] else 0.0
Nabin Hait14b8af22014-08-28 12:42:28 +0530141
Nabin Hait82e3e252015-02-23 16:58:30 +0530142 item.valuation_rate = ((item.base_net_amount + item.item_tax_amount + rm_supp_cost
nabinhait87f24012014-07-16 19:48:29 +0530143 + landed_cost_voucher_amount) / qty_in_stock_uom)
Anand Doshi4a7248e2013-02-27 18:10:30 +0530144 else:
Anand Doshi5af812a2013-05-10 19:23:02 +0530145 item.valuation_rate = 0.0
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530146
Nabin Hait54d209f2013-03-01 18:51:10 +0530147 def validate_for_subcontracting(self):
Anand Doshif78d1ae2014-03-28 13:55:00 +0530148 if not self.is_subcontracted and self.sub_contracted_items:
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530149 frappe.throw(_("Please enter 'Is Subcontracted' as Yes or No"))
150
ankitjavalkarwork5edae0e2014-11-05 20:14:53 +0530151 if self.is_subcontracted == "Yes":
Saurabhfbb342c2016-04-07 16:41:31 +0530152 if self.doctype in ["Purchase Receipt", "Purchase Invoice"] and not self.supplier_warehouse:
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530153 frappe.throw(_("Supplier Warehouse mandatory for sub-contracted Purchase Receipt"))
154
Nabin Haitdd38a262014-12-26 13:15:21 +0530155 for item in self.get("items"):
ankitjavalkarwork5edae0e2014-11-05 20:14:53 +0530156 if item in self.sub_contracted_items and not item.bom:
157 frappe.throw(_("Please select BOM in BOM field for Item {0}").format(item.item_code))
158
159 else:
Nabin Haitdd38a262014-12-26 13:15:21 +0530160 for item in self.get("items"):
ankitjavalkarwork5edae0e2014-11-05 20:14:53 +0530161 if item.bom:
162 item.bom = None
163
Nabin Hait344f4432014-05-08 19:08:20 +0530164 def create_raw_materials_supplied(self, raw_material_table):
Anand Doshif78d1ae2014-03-28 13:55:00 +0530165 if self.is_subcontracted=="Yes":
Nabin Hait344f4432014-05-08 19:08:20 +0530166 parent_items = []
Nabin Haitdd38a262014-12-26 13:15:21 +0530167 for item in self.get("items"):
Saurabhfbb342c2016-04-07 16:41:31 +0530168 if self.doctype in ["Purchase Receipt", "Purchase Invoice"]:
Nabin Haita0c239d2014-04-01 18:54:38 +0530169 item.rm_supp_cost = 0.0
Nabin Hait54d209f2013-03-01 18:51:10 +0530170 if item.item_code in self.sub_contracted_items:
Rushabh Mehtae693f792015-03-03 15:50:03 +0530171 self.update_raw_materials_supplied(item, raw_material_table)
Nabin Hait344f4432014-05-08 19:08:20 +0530172
173 if [item.item_code, item.name] not in parent_items:
174 parent_items.append([item.item_code, item.name])
175
176 self.cleanup_raw_materials_supplied(parent_items, raw_material_table)
Nabin Hait54d209f2013-03-01 18:51:10 +0530177
Saurabhfbb342c2016-04-07 16:41:31 +0530178 elif self.doctype in ["Purchase Receipt", "Purchase Invoice"]:
Nabin Haitdd38a262014-12-26 13:15:21 +0530179 for item in self.get("items"):
Nabin Haita0c239d2014-04-01 18:54:38 +0530180 item.rm_supp_cost = 0.0
181
Rohit Waghchaure4b9d2f22017-02-16 15:53:40 +0530182 if self.is_subcontracted == "No" and self.get("supplied_items"):
183 self.set('supplied_items', [])
184
Rushabh Mehtae693f792015-03-03 15:50:03 +0530185 def update_raw_materials_supplied(self, item, raw_material_table):
ankitjavalkarwork5edae0e2014-11-05 20:14:53 +0530186 bom_items = self.get_items_from_bom(item.item_code, item.bom)
Nabin Hait54d209f2013-03-01 18:51:10 +0530187 raw_materials_cost = 0
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530188
Nabin Hait344f4432014-05-08 19:08:20 +0530189 for bom_item in bom_items:
190 # check if exists
191 exists = 0
192 for d in self.get(raw_material_table):
193 if d.main_item_code == item.item_code and d.rm_item_code == bom_item.item_code \
194 and d.reference_name == item.name:
195 rm, exists = d, 1
196 break
197
198 if not exists:
199 rm = self.append(raw_material_table, {})
200
201 required_qty = flt(bom_item.qty_consumed_per_unit) * flt(item.qty) * flt(item.conversion_factor)
202 rm.reference_name = item.name
203 rm.bom_detail_no = bom_item.name
204 rm.main_item_code = item.item_code
205 rm.rm_item_code = bom_item.item_code
206 rm.stock_uom = bom_item.stock_uom
207 rm.required_qty = required_qty
208
209 rm.conversion_factor = item.conversion_factor
Nabin Hait344f4432014-05-08 19:08:20 +0530210
Saurabhfbb342c2016-04-07 16:41:31 +0530211 if self.doctype in ["Purchase Receipt", "Purchase Invoice"]:
Nabin Hait344f4432014-05-08 19:08:20 +0530212 rm.consumed_qty = required_qty
213 rm.description = bom_item.description
214 if item.batch_no and not rm.batch_no:
215 rm.batch_no = item.batch_no
216
Nabin Haite96e83d2014-10-08 18:06:14 +0530217 # get raw materials rate
Nabin Haitfce28812014-10-08 18:38:27 +0530218 if self.doctype == "Purchase Receipt":
219 from erpnext.stock.utils import get_incoming_rate
Nabin Haitfb6e4342014-10-15 11:34:40 +0530220 rm.rate = get_incoming_rate({
Nabin Haitfce28812014-10-08 18:38:27 +0530221 "item_code": bom_item.item_code,
222 "warehouse": self.supplier_warehouse,
223 "posting_date": self.posting_date,
224 "posting_time": self.posting_time,
225 "qty": -1 * required_qty,
226 "serial_no": rm.serial_no
227 })
Nabin Haitfb6e4342014-10-15 11:34:40 +0530228 if not rm.rate:
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +0530229 rm.rate = get_valuation_rate(bom_item.item_code, self.supplier_warehouse,
Rohit Waghchaurea5f40942017-06-16 15:21:36 +0530230 self.doctype, self.name, currency=self.company_currency, company = self.company)
Nabin Haitfce28812014-10-08 18:38:27 +0530231 else:
232 rm.rate = bom_item.rate
Nabin Haite96e83d2014-10-08 18:06:14 +0530233
Nabin Haitfce28812014-10-08 18:38:27 +0530234 rm.amount = required_qty * flt(rm.rate)
Nabin Haite96e83d2014-10-08 18:06:14 +0530235 raw_materials_cost += flt(rm.amount)
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530236
Nabin Hait14aa9c52016-04-18 15:54:01 +0530237 if self.doctype in ("Purchase Receipt", "Purchase Invoice"):
Nabin Hait344f4432014-05-08 19:08:20 +0530238 item.rm_supp_cost = raw_materials_cost
239
240 def cleanup_raw_materials_supplied(self, parent_items, raw_material_table):
241 """Remove all those child items which are no longer present in main item table"""
242 delete_list = []
243 for d in self.get(raw_material_table):
244 if [d.main_item_code, d.reference_name] not in parent_items:
245 # mark for deletion from doclist
Nabin Haitc3d1d6a2014-06-04 16:41:22 +0530246 delete_list.append(d)
Nabin Hait344f4432014-05-08 19:08:20 +0530247
248 # delete from doclist
249 if delete_list:
250 rm_supplied_details = self.get(raw_material_table)
251 self.set(raw_material_table, [])
252 for d in rm_supplied_details:
253 if d not in delete_list:
254 self.append(raw_material_table, d)
Nabin Hait54d209f2013-03-01 18:51:10 +0530255
ankitjavalkarwork5edae0e2014-11-05 20:14:53 +0530256 def get_items_from_bom(self, item_code, bom):
Nabin Hait14b8af22014-08-28 12:42:28 +0530257 bom_items = frappe.db.sql("""select t2.item_code,
Anand Doshi602e8252015-11-16 19:05:46 +0530258 t2.qty / ifnull(t1.quantity, 1) as qty_consumed_per_unit,
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530259 t2.rate, t2.stock_uom, t2.name, t2.description
Nabin Hait574d5ff2015-05-07 16:29:29 +0530260 from `tabBOM` t1, `tabBOM Item` t2, tabItem t3
ankitjavalkarwork5edae0e2014-11-05 20:14:53 +0530261 where t2.parent = t1.name and t1.item = %s
Rushabh Mehta1e8025b2015-07-24 15:16:25 +0530262 and t1.docstatus = 1 and t1.is_active = 1 and t1.name = %s
263 and t2.item_code = t3.name and t3.is_stock_item = 1""", (item_code, bom), as_dict=1)
ankitjavalkarwork5edae0e2014-11-05 20:14:53 +0530264
Nabin Hait54d209f2013-03-01 18:51:10 +0530265 if not bom_items:
ankitjavalkarwork5edae0e2014-11-05 20:14:53 +0530266 msgprint(_("Specified BOM {0} does not exist for Item {1}").format(bom, item_code), raise_exception=1)
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530267
Nabin Hait54d209f2013-03-01 18:51:10 +0530268 return bom_items
269
Nabin Hait5418d712013-02-27 18:11:17 +0530270 @property
271 def sub_contracted_items(self):
272 if not hasattr(self, "_sub_contracted_items"):
Nabin Haitebd51442013-04-23 15:36:26 +0530273 self._sub_contracted_items = []
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530274 item_codes = list(set(item.item_code for item in
Nabin Haitdd38a262014-12-26 13:15:21 +0530275 self.get("items")))
Nabin Haitebd51442013-04-23 15:36:26 +0530276 if item_codes:
Anand Doshie9baaa62014-02-26 12:35:33 +0530277 self._sub_contracted_items = [r[0] for r in frappe.db.sql("""select name
Rushabh Mehta1e8025b2015-07-24 15:16:25 +0530278 from `tabItem` where name in (%s) and is_sub_contracted_item=1""" % \
Nabin Haitebd51442013-04-23 15:36:26 +0530279 (", ".join((["%s"]*len(item_codes))),), item_codes)]
Nabin Hait5418d712013-02-27 18:11:17 +0530280
281 return self._sub_contracted_items
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530282
nabinhait614fb752014-07-14 10:47:50 +0530283 def set_qty_as_per_stock_uom(self):
Nabin Haitdd38a262014-12-26 13:15:21 +0530284 for d in self.get("items"):
Nabin Haitfd4bcd82015-05-22 16:55:40 +0530285 if d.meta.get_field("stock_qty"):
nabinhait614fb752014-07-14 10:47:50 +0530286 if not d.conversion_factor:
Nabin Haite6e456b2015-05-13 17:21:44 +0530287 frappe.throw(_("Row {0}: Conversion Factor is mandatory").format(d.idx))
Rushabh Mehta5b51cc82014-07-21 18:25:45 +0530288 d.stock_qty = flt(d.qty) * flt(d.conversion_factor)
Rushabh Mehtab33df4a2016-07-04 11:38:37 +0530289
Saurabh130c57b2016-04-04 15:49:36 +0530290 def validate_purchase_return(self):
291 for d in self.get("items"):
292 if self.is_return and flt(d.rejected_qty) != 0:
293 frappe.throw(_("Row #{0}: Rejected Qty can not be entered in Purchase Return").format(d.idx))
294
295 # validate rate with ref PR
296
297 def validate_rejected_warehouse(self):
298 for d in self.get("items"):
299 if flt(d.rejected_qty) and not d.rejected_warehouse:
Saurabhfbb342c2016-04-07 16:41:31 +0530300 if self.rejected_warehouse:
301 d.rejected_warehouse = self.rejected_warehouse
Rushabh Mehtab33df4a2016-07-04 11:38:37 +0530302
Saurabh130c57b2016-04-04 15:49:36 +0530303 if not d.rejected_warehouse:
304 frappe.throw(_("Row #{0}: Rejected Warehouse is mandatory against rejected Item {1}").format(d.idx, d.item_code))
305
306 # validate accepted and rejected qty
307 def validate_accepted_rejected_qty(self):
308 for d in self.get("items"):
Saurabh4f4d0a82017-03-13 14:31:48 +0530309 self.validate_negative_quantity(d, ["received_qty","qty", "rejected_qty"])
Saurabh130c57b2016-04-04 15:49:36 +0530310 if not flt(d.received_qty) and flt(d.qty):
311 d.received_qty = flt(d.qty) - flt(d.rejected_qty)
312
313 elif not flt(d.qty) and flt(d.rejected_qty):
314 d.qty = flt(d.received_qty) - flt(d.rejected_qty)
315
316 elif not flt(d.rejected_qty):
317 d.rejected_qty = flt(d.received_qty) - flt(d.qty)
318
319 # Check Received Qty = Accepted Qty + Rejected Qty
320 if ((flt(d.qty) + flt(d.rejected_qty)) != flt(d.received_qty)):
321 frappe.throw(_("Accepted + Rejected Qty must be equal to Received quantity for Item {0}").format(d.item_code))
Rushabh Mehtab33df4a2016-07-04 11:38:37 +0530322
Saurabh4f4d0a82017-03-13 14:31:48 +0530323 def validate_negative_quantity(self, item_row, field_list):
324 if self.is_return:
325 return
326
327 item_row = item_row.as_dict()
328 for fieldname in field_list:
329 if flt(item_row[fieldname]) < 0:
330 frappe.throw(_("Row #{0}: {1} can not be negative for item {2}".format(item_row['idx'],
331 frappe.get_meta(item_row.doctype).get_label(fieldname), item_row['item_code'])))
332
Saurabhe29248b2016-04-04 11:55:52 +0530333 def update_stock_ledger(self, allow_negative_stock=False, via_landed_cost_voucher=False):
Nabin Hait14aa9c52016-04-18 15:54:01 +0530334 self.update_ordered_qty()
Rushabh Mehtab33df4a2016-07-04 11:38:37 +0530335
Saurabhe29248b2016-04-04 11:55:52 +0530336 sl_entries = []
337 stock_items = self.get_stock_items()
338
339 for d in self.get('items'):
340 if d.item_code in stock_items and d.warehouse:
341 pr_qty = flt(d.qty) * flt(d.conversion_factor)
342
343 if pr_qty:
Saurabhe29248b2016-04-04 11:55:52 +0530344 sle = self.get_sl_entries(d, {
345 "actual_qty": flt(pr_qty),
346 "serial_no": cstr(d.serial_no).strip()
347 })
348 if self.is_return:
Rushabh Mehtab33df4a2016-07-04 11:38:37 +0530349 original_incoming_rate = frappe.db.get_value("Stock Ledger Entry",
350 {"voucher_type": "Purchase Receipt", "voucher_no": self.return_against,
Nabin Haitb81ed452016-05-11 12:52:31 +0530351 "item_code": d.item_code}, "incoming_rate")
Rushabh Mehtab33df4a2016-07-04 11:38:37 +0530352
Saurabhe29248b2016-04-04 11:55:52 +0530353 sle.update({
Nabin Haitb81ed452016-05-11 12:52:31 +0530354 "outgoing_rate": original_incoming_rate
Saurabhe29248b2016-04-04 11:55:52 +0530355 })
356 else:
Nabin Haitb81ed452016-05-11 12:52:31 +0530357 val_rate_db_precision = 6 if cint(self.precision("valuation_rate", d)) <= 6 else 9
358 incoming_rate = flt(d.valuation_rate, val_rate_db_precision)
Saurabhe29248b2016-04-04 11:55:52 +0530359 sle.update({
Nabin Haitb81ed452016-05-11 12:52:31 +0530360 "incoming_rate": incoming_rate
Saurabhe29248b2016-04-04 11:55:52 +0530361 })
362 sl_entries.append(sle)
363
Rohit Waghchaure560ba392016-08-29 18:19:32 +0530364 if flt(d.rejected_qty) != 0:
Saurabhe29248b2016-04-04 11:55:52 +0530365 sl_entries.append(self.get_sl_entries(d, {
366 "warehouse": d.rejected_warehouse,
367 "actual_qty": flt(d.rejected_qty) * flt(d.conversion_factor),
368 "serial_no": cstr(d.rejected_serial_no).strip(),
369 "incoming_rate": 0.0
370 }))
371
372 self.make_sl_entries_for_supplier_warehouse(sl_entries)
373 self.make_sl_entries(sl_entries, allow_negative_stock=allow_negative_stock,
374 via_landed_cost_voucher=via_landed_cost_voucher)
Rushabh Mehtab33df4a2016-07-04 11:38:37 +0530375
Nabin Hait14aa9c52016-04-18 15:54:01 +0530376 def update_ordered_qty(self):
377 po_map = {}
378 for d in self.get("items"):
379 if self.doctype=="Purchase Receipt" \
Rohit Waghchaurea71d9d32016-07-05 00:34:00 +0530380 and d.purchase_order:
381 po_map.setdefault(d.purchase_order, []).append(d.purchase_order_item)
Rushabh Mehtab33df4a2016-07-04 11:38:37 +0530382
Nabin Hait14aa9c52016-04-18 15:54:01 +0530383 elif self.doctype=="Purchase Invoice" and d.purchase_order and d.po_detail:
384 po_map.setdefault(d.purchase_order, []).append(d.po_detail)
385
386 for po, po_item_rows in po_map.items():
387 if po and po_item_rows:
388 po_obj = frappe.get_doc("Purchase Order", po)
389
390 if po_obj.status in ["Closed", "Cancelled"]:
391 frappe.throw(_("{0} {1} is cancelled or closed").format(_("Purchase Order"), po),
392 frappe.InvalidStatusError)
393
394 po_obj.update_ordered_qty(po_item_rows)
Rushabh Mehtab33df4a2016-07-04 11:38:37 +0530395
Saurabhe29248b2016-04-04 11:55:52 +0530396 def make_sl_entries_for_supplier_warehouse(self, sl_entries):
397 if hasattr(self, 'supplied_items'):
398 for d in self.get('supplied_items'):
399 # negative quantity is passed, as raw material qty has to be decreased
400 # when PR is submitted and it has to be increased when PR is cancelled
401 sl_entries.append(self.get_sl_entries(d, {
402 "item_code": d.rm_item_code,
403 "warehouse": self.supplier_warehouse,
404 "actual_qty": -1*flt(d.consumed_qty),
405 }))
Rushabh Mehtab33df4a2016-07-04 11:38:37 +0530406