2015-03-03 09:25:30 +00:00
|
|
|
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
2013-08-05 09:29:54 +00:00
|
|
|
# License: GNU General Public License v3. See license.txt
|
2013-01-15 13:09:21 +00:00
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
2014-02-14 10:17:51 +00:00
|
|
|
import frappe
|
|
|
|
from frappe import _, msgprint
|
2017-10-05 14:21:10 +00:00
|
|
|
from frappe.utils import flt,cint, cstr, getdate
|
2014-09-10 07:37:59 +00:00
|
|
|
|
2014-02-03 10:44:56 +00:00
|
|
|
from erpnext.accounts.party import get_party_details
|
2014-10-07 09:59:58 +00:00
|
|
|
from erpnext.stock.get_item_details import get_conversion_factor
|
2017-03-31 07:14:29 +00:00
|
|
|
from erpnext.buying.utils import validate_for_items
|
|
|
|
from erpnext.stock.stock_ledger import get_valuation_rate
|
2013-01-15 13:09:21 +00:00
|
|
|
|
2013-12-12 13:42:19 +00:00
|
|
|
from erpnext.controllers.stock_controller import StockController
|
2013-01-30 07:19:08 +00:00
|
|
|
|
2013-03-19 06:31:46 +00:00
|
|
|
class BuyingController(StockController):
|
2014-07-21 12:55:45 +00:00
|
|
|
def __setup__(self):
|
2015-02-22 19:36:00 +00:00
|
|
|
if hasattr(self, "taxes"):
|
2017-09-14 09:47:38 +00:00
|
|
|
self.flags.print_taxes_with_zero_amount = cint(frappe.db.get_single_value("Print Settings",
|
|
|
|
"print_taxes_with_zero_amount"))
|
2015-02-03 12:25:52 +00:00
|
|
|
self.print_templates = {
|
|
|
|
"taxes": "templates/print_formats/includes/taxes.html"
|
2014-07-21 12:55:45 +00:00
|
|
|
}
|
|
|
|
|
2014-09-09 10:45:35 +00:00
|
|
|
def get_feed(self):
|
2016-03-03 08:30:35 +00:00
|
|
|
if self.get("supplier_name"):
|
|
|
|
return _("From {0} | {1} {2}").format(self.supplier_name, self.currency,
|
|
|
|
self.grand_total)
|
2014-09-09 10:45:35 +00:00
|
|
|
|
2013-03-20 07:25:28 +00:00
|
|
|
def validate(self):
|
|
|
|
super(BuyingController, self).validate()
|
2014-04-03 12:08:54 +00:00
|
|
|
if getattr(self, "supplier", None) and not self.supplier_name:
|
2015-07-17 09:49:02 +00:00
|
|
|
self.supplier_name = frappe.db.get_value("Supplier", self.supplier, "supplier_name")
|
2016-07-07 08:32:26 +00:00
|
|
|
|
2014-07-14 05:17:50 +00:00
|
|
|
self.set_qty_as_per_stock_uom()
|
2013-04-26 08:05:06 +00:00
|
|
|
self.validate_stock_or_nonstock_items()
|
2013-10-10 10:34:40 +00:00
|
|
|
self.validate_warehouse()
|
2016-07-04 06:08:37 +00:00
|
|
|
|
2016-04-18 10:24:01 +00:00
|
|
|
if self.doctype=="Purchase Invoice":
|
|
|
|
self.validate_purchase_receipt_if_update_stock()
|
2016-07-04 06:08:37 +00:00
|
|
|
|
2016-04-18 10:24:01 +00:00
|
|
|
if self.doctype=="Purchase Receipt" or (self.doctype=="Purchase Invoice" and self.update_stock):
|
2016-08-29 12:49:32 +00:00
|
|
|
# self.validate_purchase_return()
|
2016-04-07 11:11:31 +00:00
|
|
|
self.validate_rejected_warehouse()
|
|
|
|
self.validate_accepted_rejected_qty()
|
2017-03-31 07:14:29 +00:00
|
|
|
validate_for_items(self)
|
2016-07-04 06:08:37 +00:00
|
|
|
|
2016-04-07 11:11:31 +00:00
|
|
|
#sub-contracting
|
|
|
|
self.validate_for_subcontracting()
|
|
|
|
self.create_raw_materials_supplied("supplied_items")
|
|
|
|
self.set_landed_cost_voucher_amount()
|
2016-07-04 06:08:37 +00:00
|
|
|
|
2016-04-18 10:24:01 +00:00
|
|
|
if self.doctype in ("Purchase Receipt", "Purchase Invoice"):
|
2016-04-07 11:11:31 +00:00
|
|
|
self.update_valuation_rate("items")
|
2014-04-14 13:50:45 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
def set_missing_values(self, for_validate=False):
|
2013-06-14 12:14:03 +00:00
|
|
|
super(BuyingController, self).set_missing_values(for_validate)
|
2016-07-04 06:08:37 +00:00
|
|
|
|
2013-08-09 09:59:59 +00:00
|
|
|
self.set_supplier_from_item_default()
|
2013-10-17 11:31:14 +00:00
|
|
|
self.set_price_list_currency("Buying")
|
2014-04-14 13:50:45 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
# set contact and address details for supplier, if they are not mentioned
|
2014-04-03 12:08:54 +00:00
|
|
|
if getattr(self, "supplier", None):
|
2017-10-02 10:29:27 +00:00
|
|
|
self.update_if_missing(get_party_details(self.supplier, party_type="Supplier", ignore_permissions=self.flags.ignore_permissions, doctype=self.doctype, company=self.company))
|
2013-07-08 06:38:06 +00:00
|
|
|
|
2016-10-05 11:45:43 +00:00
|
|
|
self.set_missing_item_details(for_validate)
|
2013-07-08 07:07:59 +00:00
|
|
|
|
2013-08-09 09:59:59 +00:00
|
|
|
def set_supplier_from_item_default(self):
|
2014-03-28 08:25:00 +00:00
|
|
|
if self.meta.get_field("supplier") and not self.supplier:
|
2014-12-26 07:45:21 +00:00
|
|
|
for d in self.get("items"):
|
2014-02-26 07:05:33 +00:00
|
|
|
supplier = frappe.db.get_value("Item", d.item_code, "default_supplier")
|
2013-08-09 09:59:59 +00:00
|
|
|
if supplier:
|
2014-03-28 08:25:00 +00:00
|
|
|
self.supplier = supplier
|
2013-08-09 09:59:59 +00:00
|
|
|
break
|
2014-04-14 13:50:45 +00:00
|
|
|
|
2013-04-26 08:05:06 +00:00
|
|
|
def validate_stock_or_nonstock_items(self):
|
2014-12-25 10:31:55 +00:00
|
|
|
if self.meta.get_field("taxes") and not self.get_stock_items():
|
2017-06-16 10:00:14 +00:00
|
|
|
tax_for_valuation = [d for d in self.get("taxes")
|
2013-04-26 08:05:06 +00:00
|
|
|
if d.category in ["Valuation", "Valuation and Total"]]
|
2017-06-16 10:00:14 +00:00
|
|
|
|
2013-04-26 08:05:06 +00:00
|
|
|
if tax_for_valuation:
|
2017-06-16 10:00:14 +00:00
|
|
|
for d in tax_for_valuation:
|
|
|
|
d.category = 'Total'
|
|
|
|
msgprint(_('Tax Category has been changed to "Total" because all the Items are non-stock items'))
|
2014-04-14 13:50:45 +00:00
|
|
|
|
2016-04-07 11:11:31 +00:00
|
|
|
def set_landed_cost_voucher_amount(self):
|
|
|
|
for d in self.get("items"):
|
2017-08-10 06:02:59 +00:00
|
|
|
lc_voucher_data = frappe.db.sql("""select sum(applicable_charges), cost_center
|
2016-04-07 11:11:31 +00:00
|
|
|
from `tabLanded Cost Item`
|
|
|
|
where docstatus = 1 and purchase_receipt_item = %s""", d.name)
|
2017-08-10 06:02:59 +00:00
|
|
|
d.landed_cost_voucher_amount = lc_voucher_data[0][0] if lc_voucher_data else 0.0
|
|
|
|
if not d.cost_center and lc_voucher_data and lc_voucher_data[0][1]:
|
|
|
|
d.db_set('cost_center', lc_voucher_data[0][1])
|
2016-07-04 06:08:37 +00:00
|
|
|
|
2013-01-21 11:54:31 +00:00
|
|
|
def set_total_in_words(self):
|
2014-02-14 10:17:51 +00:00
|
|
|
from frappe.utils import money_in_words
|
2015-02-12 10:39:11 +00:00
|
|
|
if self.meta.get_field("base_in_words"):
|
2017-03-31 07:14:29 +00:00
|
|
|
self.base_in_words = money_in_words(self.base_grand_total, self.company_currency)
|
2013-01-22 05:42:02 +00:00
|
|
|
if self.meta.get_field("in_words"):
|
2015-02-12 12:25:50 +00:00
|
|
|
self.in_words = money_in_words(self.grand_total, self.currency)
|
2014-04-14 13:50:45 +00:00
|
|
|
|
2014-01-02 11:00:16 +00:00
|
|
|
# update valuation rate
|
|
|
|
def update_valuation_rate(self, parentfield):
|
2013-02-08 13:58:14 +00:00
|
|
|
"""
|
|
|
|
item_tax_amount is the total tax amount applied on that item
|
2014-04-14 13:50:45 +00:00
|
|
|
stored for valuation
|
|
|
|
|
2013-02-08 13:58:14 +00:00
|
|
|
TODO: rename item_tax_amount to valuation_tax_amount
|
|
|
|
"""
|
2014-01-02 11:00:16 +00:00
|
|
|
stock_items = self.get_stock_items()
|
2014-04-14 13:50:45 +00:00
|
|
|
|
2014-01-03 06:43:18 +00:00
|
|
|
stock_items_qty, stock_items_amount = 0, 0
|
2014-01-14 12:14:34 +00:00
|
|
|
last_stock_item_idx = 1
|
2014-03-27 10:42:56 +00:00
|
|
|
for d in self.get(parentfield):
|
2014-01-03 06:43:18 +00:00
|
|
|
if d.item_code and d.item_code in stock_items:
|
|
|
|
stock_items_qty += flt(d.qty)
|
2015-02-23 11:28:30 +00:00
|
|
|
stock_items_amount += flt(d.base_net_amount)
|
2014-01-14 12:14:34 +00:00
|
|
|
last_stock_item_idx = d.idx
|
2014-04-14 13:50:45 +00:00
|
|
|
|
2015-02-23 11:28:30 +00:00
|
|
|
total_valuation_amount = sum([flt(d.base_tax_amount_after_discount_amount) for d in self.get("taxes")
|
2014-01-02 11:00:16 +00:00
|
|
|
if d.category in ["Valuation", "Valuation and Total"]])
|
2014-04-14 13:50:45 +00:00
|
|
|
|
2014-01-14 12:14:34 +00:00
|
|
|
valuation_amount_adjustment = total_valuation_amount
|
2014-03-27 10:42:56 +00:00
|
|
|
for i, item in enumerate(self.get(parentfield)):
|
2014-01-03 06:43:18 +00:00
|
|
|
if item.item_code and item.qty and item.item_code in stock_items:
|
2015-02-23 11:28:30 +00:00
|
|
|
item_proportion = flt(item.base_net_amount) / stock_items_amount if stock_items_amount \
|
2014-01-03 06:43:18 +00:00
|
|
|
else flt(item.qty) / stock_items_qty
|
2014-01-14 12:14:34 +00:00
|
|
|
if i == (last_stock_item_idx - 1):
|
2014-04-14 13:50:45 +00:00
|
|
|
item.item_tax_amount = flt(valuation_amount_adjustment,
|
2014-01-14 12:14:34 +00:00
|
|
|
self.precision("item_tax_amount", item))
|
|
|
|
else:
|
2014-04-14 13:50:45 +00:00
|
|
|
item.item_tax_amount = flt(item_proportion * total_valuation_amount,
|
2014-01-14 12:14:34 +00:00
|
|
|
self.precision("item_tax_amount", item))
|
|
|
|
valuation_amount_adjustment -= item.item_tax_amount
|
2014-01-02 11:00:16 +00:00
|
|
|
|
2013-05-11 14:09:53 +00:00
|
|
|
self.round_floats_in(item)
|
2016-07-07 08:32:26 +00:00
|
|
|
if flt(item.conversion_factor)==0.0:
|
2015-08-05 04:30:12 +00:00
|
|
|
item.conversion_factor = get_conversion_factor(item.item_code, item.uom).get("conversion_factor") or 1.0
|
2014-10-07 09:59:58 +00:00
|
|
|
|
2013-12-23 06:44:45 +00:00
|
|
|
qty_in_stock_uom = flt(item.qty * item.conversion_factor)
|
2016-04-07 11:11:31 +00:00
|
|
|
rm_supp_cost = flt(item.rm_supp_cost) if self.doctype in ["Purchase Receipt", "Purchase Invoice"] else 0.0
|
2014-07-17 13:46:38 +00:00
|
|
|
|
2014-07-16 14:18:29 +00:00
|
|
|
landed_cost_voucher_amount = flt(item.landed_cost_voucher_amount) \
|
2016-04-07 11:11:31 +00:00
|
|
|
if self.doctype in ["Purchase Receipt", "Purchase Invoice"] else 0.0
|
2014-08-28 07:12:28 +00:00
|
|
|
|
2015-02-23 11:28:30 +00:00
|
|
|
item.valuation_rate = ((item.base_net_amount + item.item_tax_amount + rm_supp_cost
|
2014-07-16 14:18:29 +00:00
|
|
|
+ landed_cost_voucher_amount) / qty_in_stock_uom)
|
2013-02-27 12:40:30 +00:00
|
|
|
else:
|
2013-05-10 13:53:02 +00:00
|
|
|
item.valuation_rate = 0.0
|
2014-04-14 13:50:45 +00:00
|
|
|
|
2013-03-01 13:21:10 +00:00
|
|
|
def validate_for_subcontracting(self):
|
2014-03-28 08:25:00 +00:00
|
|
|
if not self.is_subcontracted and self.sub_contracted_items:
|
2014-04-14 13:50:45 +00:00
|
|
|
frappe.throw(_("Please enter 'Is Subcontracted' as Yes or No"))
|
|
|
|
|
2014-11-05 14:44:53 +00:00
|
|
|
if self.is_subcontracted == "Yes":
|
2016-04-07 11:11:31 +00:00
|
|
|
if self.doctype in ["Purchase Receipt", "Purchase Invoice"] and not self.supplier_warehouse:
|
2014-04-14 13:50:45 +00:00
|
|
|
frappe.throw(_("Supplier Warehouse mandatory for sub-contracted Purchase Receipt"))
|
|
|
|
|
2014-12-26 07:45:21 +00:00
|
|
|
for item in self.get("items"):
|
2014-11-05 14:44:53 +00:00
|
|
|
if item in self.sub_contracted_items and not item.bom:
|
|
|
|
frappe.throw(_("Please select BOM in BOM field for Item {0}").format(item.item_code))
|
|
|
|
|
|
|
|
else:
|
2014-12-26 07:45:21 +00:00
|
|
|
for item in self.get("items"):
|
2014-11-05 14:44:53 +00:00
|
|
|
if item.bom:
|
|
|
|
item.bom = None
|
|
|
|
|
2014-05-08 13:38:20 +00:00
|
|
|
def create_raw_materials_supplied(self, raw_material_table):
|
2014-03-28 08:25:00 +00:00
|
|
|
if self.is_subcontracted=="Yes":
|
2014-05-08 13:38:20 +00:00
|
|
|
parent_items = []
|
2014-12-26 07:45:21 +00:00
|
|
|
for item in self.get("items"):
|
2016-04-07 11:11:31 +00:00
|
|
|
if self.doctype in ["Purchase Receipt", "Purchase Invoice"]:
|
2014-04-01 13:24:38 +00:00
|
|
|
item.rm_supp_cost = 0.0
|
2017-11-22 09:51:47 +00:00
|
|
|
if item.bom and item.item_code in self.sub_contracted_items:
|
2015-03-03 10:20:03 +00:00
|
|
|
self.update_raw_materials_supplied(item, raw_material_table)
|
2014-05-08 13:38:20 +00:00
|
|
|
|
|
|
|
if [item.item_code, item.name] not in parent_items:
|
|
|
|
parent_items.append([item.item_code, item.name])
|
|
|
|
|
|
|
|
self.cleanup_raw_materials_supplied(parent_items, raw_material_table)
|
2013-03-01 13:21:10 +00:00
|
|
|
|
2016-04-07 11:11:31 +00:00
|
|
|
elif self.doctype in ["Purchase Receipt", "Purchase Invoice"]:
|
2014-12-26 07:45:21 +00:00
|
|
|
for item in self.get("items"):
|
2014-04-01 13:24:38 +00:00
|
|
|
item.rm_supp_cost = 0.0
|
|
|
|
|
2017-02-16 10:23:40 +00:00
|
|
|
if self.is_subcontracted == "No" and self.get("supplied_items"):
|
|
|
|
self.set('supplied_items', [])
|
|
|
|
|
2015-03-03 10:20:03 +00:00
|
|
|
def update_raw_materials_supplied(self, item, raw_material_table):
|
2014-11-05 14:44:53 +00:00
|
|
|
bom_items = self.get_items_from_bom(item.item_code, item.bom)
|
2013-03-01 13:21:10 +00:00
|
|
|
raw_materials_cost = 0
|
2014-05-08 13:38:20 +00:00
|
|
|
|
|
|
|
for bom_item in bom_items:
|
|
|
|
# check if exists
|
|
|
|
exists = 0
|
|
|
|
for d in self.get(raw_material_table):
|
|
|
|
if d.main_item_code == item.item_code and d.rm_item_code == bom_item.item_code \
|
|
|
|
and d.reference_name == item.name:
|
|
|
|
rm, exists = d, 1
|
|
|
|
break
|
|
|
|
|
|
|
|
if not exists:
|
|
|
|
rm = self.append(raw_material_table, {})
|
|
|
|
|
|
|
|
required_qty = flt(bom_item.qty_consumed_per_unit) * flt(item.qty) * flt(item.conversion_factor)
|
|
|
|
rm.reference_name = item.name
|
|
|
|
rm.bom_detail_no = bom_item.name
|
|
|
|
rm.main_item_code = item.item_code
|
|
|
|
rm.rm_item_code = bom_item.item_code
|
|
|
|
rm.stock_uom = bom_item.stock_uom
|
|
|
|
rm.required_qty = required_qty
|
|
|
|
|
|
|
|
rm.conversion_factor = item.conversion_factor
|
|
|
|
|
2016-04-07 11:11:31 +00:00
|
|
|
if self.doctype in ["Purchase Receipt", "Purchase Invoice"]:
|
2014-05-08 13:38:20 +00:00
|
|
|
rm.consumed_qty = required_qty
|
|
|
|
rm.description = bom_item.description
|
|
|
|
if item.batch_no and not rm.batch_no:
|
|
|
|
rm.batch_no = item.batch_no
|
|
|
|
|
2014-10-08 12:36:14 +00:00
|
|
|
# get raw materials rate
|
2014-10-08 13:08:27 +00:00
|
|
|
if self.doctype == "Purchase Receipt":
|
|
|
|
from erpnext.stock.utils import get_incoming_rate
|
2014-10-15 06:04:40 +00:00
|
|
|
rm.rate = get_incoming_rate({
|
2014-10-08 13:08:27 +00:00
|
|
|
"item_code": bom_item.item_code,
|
|
|
|
"warehouse": self.supplier_warehouse,
|
|
|
|
"posting_date": self.posting_date,
|
|
|
|
"posting_time": self.posting_time,
|
|
|
|
"qty": -1 * required_qty,
|
|
|
|
"serial_no": rm.serial_no
|
|
|
|
})
|
2014-10-15 06:04:40 +00:00
|
|
|
if not rm.rate:
|
2017-03-31 07:14:29 +00:00
|
|
|
rm.rate = get_valuation_rate(bom_item.item_code, self.supplier_warehouse,
|
2017-06-16 09:51:36 +00:00
|
|
|
self.doctype, self.name, currency=self.company_currency, company = self.company)
|
2014-10-08 13:08:27 +00:00
|
|
|
else:
|
|
|
|
rm.rate = bom_item.rate
|
2014-10-08 12:36:14 +00:00
|
|
|
|
2014-10-08 13:08:27 +00:00
|
|
|
rm.amount = required_qty * flt(rm.rate)
|
2014-10-08 12:36:14 +00:00
|
|
|
raw_materials_cost += flt(rm.amount)
|
2014-04-14 13:50:45 +00:00
|
|
|
|
2016-04-18 10:24:01 +00:00
|
|
|
if self.doctype in ("Purchase Receipt", "Purchase Invoice"):
|
2014-05-08 13:38:20 +00:00
|
|
|
item.rm_supp_cost = raw_materials_cost
|
|
|
|
|
|
|
|
def cleanup_raw_materials_supplied(self, parent_items, raw_material_table):
|
|
|
|
"""Remove all those child items which are no longer present in main item table"""
|
|
|
|
delete_list = []
|
|
|
|
for d in self.get(raw_material_table):
|
|
|
|
if [d.main_item_code, d.reference_name] not in parent_items:
|
|
|
|
# mark for deletion from doclist
|
2014-06-04 11:11:22 +00:00
|
|
|
delete_list.append(d)
|
2014-05-08 13:38:20 +00:00
|
|
|
|
|
|
|
# delete from doclist
|
|
|
|
if delete_list:
|
|
|
|
rm_supplied_details = self.get(raw_material_table)
|
|
|
|
self.set(raw_material_table, [])
|
|
|
|
for d in rm_supplied_details:
|
|
|
|
if d not in delete_list:
|
|
|
|
self.append(raw_material_table, d)
|
2013-03-01 13:21:10 +00:00
|
|
|
|
2014-11-05 14:44:53 +00:00
|
|
|
def get_items_from_bom(self, item_code, bom):
|
2014-08-28 07:12:28 +00:00
|
|
|
bom_items = frappe.db.sql("""select t2.item_code,
|
2017-05-23 22:15:17 +00:00
|
|
|
t2.stock_qty / ifnull(t1.quantity, 1) as qty_consumed_per_unit,
|
2014-04-14 13:50:45 +00:00
|
|
|
t2.rate, t2.stock_uom, t2.name, t2.description
|
2015-05-07 10:59:29 +00:00
|
|
|
from `tabBOM` t1, `tabBOM Item` t2, tabItem t3
|
2014-11-05 14:44:53 +00:00
|
|
|
where t2.parent = t1.name and t1.item = %s
|
2015-07-24 09:46:25 +00:00
|
|
|
and t1.docstatus = 1 and t1.is_active = 1 and t1.name = %s
|
|
|
|
and t2.item_code = t3.name and t3.is_stock_item = 1""", (item_code, bom), as_dict=1)
|
2014-11-05 14:44:53 +00:00
|
|
|
|
2013-03-01 13:21:10 +00:00
|
|
|
if not bom_items:
|
2014-11-05 14:44:53 +00:00
|
|
|
msgprint(_("Specified BOM {0} does not exist for Item {1}").format(bom, item_code), raise_exception=1)
|
2014-04-14 13:50:45 +00:00
|
|
|
|
2013-03-01 13:21:10 +00:00
|
|
|
return bom_items
|
|
|
|
|
2013-02-27 12:41:17 +00:00
|
|
|
@property
|
|
|
|
def sub_contracted_items(self):
|
|
|
|
if not hasattr(self, "_sub_contracted_items"):
|
2013-04-23 10:06:26 +00:00
|
|
|
self._sub_contracted_items = []
|
2014-04-14 13:50:45 +00:00
|
|
|
item_codes = list(set(item.item_code for item in
|
2014-12-26 07:45:21 +00:00
|
|
|
self.get("items")))
|
2013-04-23 10:06:26 +00:00
|
|
|
if item_codes:
|
2014-02-26 07:05:33 +00:00
|
|
|
self._sub_contracted_items = [r[0] for r in frappe.db.sql("""select name
|
2015-07-24 09:46:25 +00:00
|
|
|
from `tabItem` where name in (%s) and is_sub_contracted_item=1""" % \
|
2013-04-23 10:06:26 +00:00
|
|
|
(", ".join((["%s"]*len(item_codes))),), item_codes)]
|
2013-02-27 12:41:17 +00:00
|
|
|
|
|
|
|
return self._sub_contracted_items
|
2014-04-14 13:50:45 +00:00
|
|
|
|
2014-07-14 05:17:50 +00:00
|
|
|
def set_qty_as_per_stock_uom(self):
|
2014-12-26 07:45:21 +00:00
|
|
|
for d in self.get("items"):
|
2015-05-22 11:25:40 +00:00
|
|
|
if d.meta.get_field("stock_qty"):
|
2014-07-14 05:17:50 +00:00
|
|
|
if not d.conversion_factor:
|
2015-05-13 11:51:44 +00:00
|
|
|
frappe.throw(_("Row {0}: Conversion Factor is mandatory").format(d.idx))
|
2014-07-21 12:55:45 +00:00
|
|
|
d.stock_qty = flt(d.qty) * flt(d.conversion_factor)
|
2016-07-04 06:08:37 +00:00
|
|
|
|
2016-04-04 10:19:36 +00:00
|
|
|
def validate_purchase_return(self):
|
|
|
|
for d in self.get("items"):
|
|
|
|
if self.is_return and flt(d.rejected_qty) != 0:
|
|
|
|
frappe.throw(_("Row #{0}: Rejected Qty can not be entered in Purchase Return").format(d.idx))
|
|
|
|
|
|
|
|
# validate rate with ref PR
|
|
|
|
|
|
|
|
def validate_rejected_warehouse(self):
|
|
|
|
for d in self.get("items"):
|
|
|
|
if flt(d.rejected_qty) and not d.rejected_warehouse:
|
2016-04-07 11:11:31 +00:00
|
|
|
if self.rejected_warehouse:
|
|
|
|
d.rejected_warehouse = self.rejected_warehouse
|
2016-07-04 06:08:37 +00:00
|
|
|
|
2016-04-04 10:19:36 +00:00
|
|
|
if not d.rejected_warehouse:
|
|
|
|
frappe.throw(_("Row #{0}: Rejected Warehouse is mandatory against rejected Item {1}").format(d.idx, d.item_code))
|
|
|
|
|
|
|
|
# validate accepted and rejected qty
|
|
|
|
def validate_accepted_rejected_qty(self):
|
|
|
|
for d in self.get("items"):
|
2017-03-13 09:01:48 +00:00
|
|
|
self.validate_negative_quantity(d, ["received_qty","qty", "rejected_qty"])
|
2016-04-04 10:19:36 +00:00
|
|
|
if not flt(d.received_qty) and flt(d.qty):
|
|
|
|
d.received_qty = flt(d.qty) - flt(d.rejected_qty)
|
|
|
|
|
|
|
|
elif not flt(d.qty) and flt(d.rejected_qty):
|
|
|
|
d.qty = flt(d.received_qty) - flt(d.rejected_qty)
|
|
|
|
|
|
|
|
elif not flt(d.rejected_qty):
|
|
|
|
d.rejected_qty = flt(d.received_qty) - flt(d.qty)
|
|
|
|
|
|
|
|
# Check Received Qty = Accepted Qty + Rejected Qty
|
|
|
|
if ((flt(d.qty) + flt(d.rejected_qty)) != flt(d.received_qty)):
|
|
|
|
frappe.throw(_("Accepted + Rejected Qty must be equal to Received quantity for Item {0}").format(d.item_code))
|
2016-07-04 06:08:37 +00:00
|
|
|
|
2017-03-13 09:01:48 +00:00
|
|
|
def validate_negative_quantity(self, item_row, field_list):
|
|
|
|
if self.is_return:
|
|
|
|
return
|
|
|
|
|
|
|
|
item_row = item_row.as_dict()
|
|
|
|
for fieldname in field_list:
|
|
|
|
if flt(item_row[fieldname]) < 0:
|
|
|
|
frappe.throw(_("Row #{0}: {1} can not be negative for item {2}".format(item_row['idx'],
|
|
|
|
frappe.get_meta(item_row.doctype).get_label(fieldname), item_row['item_code'])))
|
|
|
|
|
2016-04-04 06:25:52 +00:00
|
|
|
def update_stock_ledger(self, allow_negative_stock=False, via_landed_cost_voucher=False):
|
2016-04-18 10:24:01 +00:00
|
|
|
self.update_ordered_qty()
|
2016-07-04 06:08:37 +00:00
|
|
|
|
2016-04-04 06:25:52 +00:00
|
|
|
sl_entries = []
|
|
|
|
stock_items = self.get_stock_items()
|
|
|
|
|
|
|
|
for d in self.get('items'):
|
|
|
|
if d.item_code in stock_items and d.warehouse:
|
|
|
|
pr_qty = flt(d.qty) * flt(d.conversion_factor)
|
|
|
|
|
|
|
|
if pr_qty:
|
|
|
|
sle = self.get_sl_entries(d, {
|
|
|
|
"actual_qty": flt(pr_qty),
|
|
|
|
"serial_no": cstr(d.serial_no).strip()
|
|
|
|
})
|
|
|
|
if self.is_return:
|
2016-07-04 06:08:37 +00:00
|
|
|
original_incoming_rate = frappe.db.get_value("Stock Ledger Entry",
|
|
|
|
{"voucher_type": "Purchase Receipt", "voucher_no": self.return_against,
|
2016-05-11 07:22:31 +00:00
|
|
|
"item_code": d.item_code}, "incoming_rate")
|
2016-07-04 06:08:37 +00:00
|
|
|
|
2016-04-04 06:25:52 +00:00
|
|
|
sle.update({
|
2016-05-11 07:22:31 +00:00
|
|
|
"outgoing_rate": original_incoming_rate
|
2016-04-04 06:25:52 +00:00
|
|
|
})
|
|
|
|
else:
|
2016-05-11 07:22:31 +00:00
|
|
|
val_rate_db_precision = 6 if cint(self.precision("valuation_rate", d)) <= 6 else 9
|
|
|
|
incoming_rate = flt(d.valuation_rate, val_rate_db_precision)
|
2016-04-04 06:25:52 +00:00
|
|
|
sle.update({
|
2016-05-11 07:22:31 +00:00
|
|
|
"incoming_rate": incoming_rate
|
2016-04-04 06:25:52 +00:00
|
|
|
})
|
|
|
|
sl_entries.append(sle)
|
|
|
|
|
2016-08-29 12:49:32 +00:00
|
|
|
if flt(d.rejected_qty) != 0:
|
2016-04-04 06:25:52 +00:00
|
|
|
sl_entries.append(self.get_sl_entries(d, {
|
|
|
|
"warehouse": d.rejected_warehouse,
|
|
|
|
"actual_qty": flt(d.rejected_qty) * flt(d.conversion_factor),
|
|
|
|
"serial_no": cstr(d.rejected_serial_no).strip(),
|
|
|
|
"incoming_rate": 0.0
|
|
|
|
}))
|
|
|
|
|
|
|
|
self.make_sl_entries_for_supplier_warehouse(sl_entries)
|
|
|
|
self.make_sl_entries(sl_entries, allow_negative_stock=allow_negative_stock,
|
|
|
|
via_landed_cost_voucher=via_landed_cost_voucher)
|
2016-07-04 06:08:37 +00:00
|
|
|
|
2016-04-18 10:24:01 +00:00
|
|
|
def update_ordered_qty(self):
|
|
|
|
po_map = {}
|
|
|
|
for d in self.get("items"):
|
|
|
|
if self.doctype=="Purchase Receipt" \
|
2016-07-04 19:04:00 +00:00
|
|
|
and d.purchase_order:
|
|
|
|
po_map.setdefault(d.purchase_order, []).append(d.purchase_order_item)
|
2016-07-04 06:08:37 +00:00
|
|
|
|
2016-04-18 10:24:01 +00:00
|
|
|
elif self.doctype=="Purchase Invoice" and d.purchase_order and d.po_detail:
|
|
|
|
po_map.setdefault(d.purchase_order, []).append(d.po_detail)
|
|
|
|
|
|
|
|
for po, po_item_rows in po_map.items():
|
|
|
|
if po and po_item_rows:
|
|
|
|
po_obj = frappe.get_doc("Purchase Order", po)
|
|
|
|
|
|
|
|
if po_obj.status in ["Closed", "Cancelled"]:
|
|
|
|
frappe.throw(_("{0} {1} is cancelled or closed").format(_("Purchase Order"), po),
|
|
|
|
frappe.InvalidStatusError)
|
|
|
|
|
|
|
|
po_obj.update_ordered_qty(po_item_rows)
|
2016-07-04 06:08:37 +00:00
|
|
|
|
2016-04-04 06:25:52 +00:00
|
|
|
def make_sl_entries_for_supplier_warehouse(self, sl_entries):
|
|
|
|
if hasattr(self, 'supplied_items'):
|
|
|
|
for d in self.get('supplied_items'):
|
|
|
|
# negative quantity is passed, as raw material qty has to be decreased
|
|
|
|
# when PR is submitted and it has to be increased when PR is cancelled
|
|
|
|
sl_entries.append(self.get_sl_entries(d, {
|
|
|
|
"item_code": d.rm_item_code,
|
|
|
|
"warehouse": self.supplier_warehouse,
|
|
|
|
"actual_qty": -1*flt(d.consumed_qty),
|
|
|
|
}))
|
2016-07-04 06:08:37 +00:00
|
|
|
|
2017-10-05 14:21:10 +00:00
|
|
|
def validate_schedule_date(self):
|
|
|
|
if not self.schedule_date:
|
|
|
|
self.schedule_date = min([d.schedule_date for d in self.get("items")])
|
|
|
|
|
|
|
|
if self.schedule_date:
|
|
|
|
for d in self.get('items'):
|
|
|
|
if not d.schedule_date:
|
|
|
|
d.schedule_date = self.schedule_date
|
|
|
|
|
|
|
|
if d.schedule_date and getdate(d.schedule_date) < getdate(self.transaction_date):
|
|
|
|
frappe.throw(_("Expected Date cannot be before Transaction Date"))
|
|
|
|
else:
|
|
|
|
frappe.throw(_("Please enter Schedule Date"))
|