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-12-28 10:41:27 +00:00
|
|
|
from erpnext.buying.utils import validate_for_items, update_last_purchase_rate
|
2017-03-31 07:14:29 +00:00
|
|
|
from erpnext.stock.stock_ledger import get_valuation_rate
|
2018-03-28 06:21:44 +00:00
|
|
|
from erpnext.stock.doctype.stock_entry.stock_entry import get_used_alternative_items
|
2018-05-10 20:26:05 +00:00
|
|
|
from erpnext.stock.doctype.serial_no.serial_no import get_auto_serial_nos, auto_make_serial_nos, get_serial_nos
|
2018-11-15 11:34:02 +00:00
|
|
|
from frappe.contacts.doctype.address.address import get_address_display
|
2013-01-15 13:09:21 +00:00
|
|
|
|
2018-05-15 16:42:44 +00:00
|
|
|
from erpnext.accounts.doctype.budget.budget import validate_expense_against_budget
|
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"))
|
2018-01-08 09:50:15 +00:00
|
|
|
self.flags.show_inclusive_tax_in_print = self.is_inclusive_tax()
|
|
|
|
|
2015-02-03 12:25:52 +00:00
|
|
|
self.print_templates = {
|
2018-01-08 09:50:15 +00:00
|
|
|
"total": "templates/print_formats/includes/total.html",
|
2015-02-03 12:25:52 +00:00
|
|
|
"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
|
|
|
|
2018-06-04 07:22:52 +00:00
|
|
|
self.validate_items()
|
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()
|
2018-11-15 11:34:02 +00:00
|
|
|
self.set_supplier_address()
|
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):
|
2018-10-07 06:12:07 +00:00
|
|
|
self.update_if_missing(get_party_details(self.supplier, party_type="Supplier", ignore_permissions=self.flags.ignore_permissions,
|
2018-10-11 11:29:40 +00:00
|
|
|
doctype=self.doctype, company=self.company, party_address=self.supplier_address, shipping_address=self.get('shipping_address')))
|
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"):
|
2018-05-26 03:39:02 +00:00
|
|
|
supplier = frappe.db.get_value("Item Default",
|
|
|
|
{"parent": d.item_code, "company": self.company}, "default_supplier")
|
2013-08-09 09:59:59 +00:00
|
|
|
if supplier:
|
2014-03-28 08:25:00 +00:00
|
|
|
self.supplier = supplier
|
2018-07-17 12:31:44 +00:00
|
|
|
else:
|
|
|
|
item_group = frappe.db.get_value("Item", d.item_code, "item_group")
|
|
|
|
supplier = frappe.db.get_value("Item Default",
|
|
|
|
{"parent": item_group, "company": self.company}, "default_supplier")
|
|
|
|
if supplier:
|
|
|
|
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):
|
2018-05-07 13:16:53 +00:00
|
|
|
if self.meta.get_field("taxes") and not self.get_stock_items() and not self.get_asset_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
|
|
|
|
2018-05-07 13:16:53 +00:00
|
|
|
def get_asset_items(self):
|
2019-11-18 06:16:55 +00:00
|
|
|
if self.doctype not in ['Purchase Order', 'Purchase Invoice', 'Purchase Receipt']:
|
2018-05-12 09:57:18 +00:00
|
|
|
return []
|
|
|
|
|
2018-05-07 13:16:53 +00:00
|
|
|
return [d.item_code for d in self.items if d.is_fixed_asset]
|
|
|
|
|
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
|
|
|
|
2018-11-15 11:34:02 +00:00
|
|
|
def set_supplier_address(self):
|
|
|
|
address_dict = {
|
|
|
|
'supplier_address': 'address_display',
|
|
|
|
'shipping_address': 'shipping_address_display'
|
|
|
|
}
|
|
|
|
|
|
|
|
for address_field, address_display_field in address_dict.items():
|
|
|
|
if self.get(address_field):
|
|
|
|
self.set(address_display_field, get_address_display(self.get(address_field)))
|
|
|
|
|
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"):
|
2018-08-29 07:11:12 +00:00
|
|
|
if self.meta.get_field("base_rounded_total") and not self.is_rounded_total_disabled():
|
|
|
|
amount = self.base_rounded_total
|
|
|
|
else:
|
|
|
|
amount = self.base_grand_total
|
2018-08-01 12:39:51 +00:00
|
|
|
self.base_in_words = money_in_words(amount, self.company_currency)
|
|
|
|
|
2013-01-22 05:42:02 +00:00
|
|
|
if self.meta.get_field("in_words"):
|
2018-08-29 07:11:12 +00:00
|
|
|
if self.meta.get_field("rounded_total") and not self.is_rounded_total_disabled():
|
|
|
|
amount = self.rounded_total
|
|
|
|
else:
|
|
|
|
amount = self.grand_total
|
2018-08-01 12:39:51 +00:00
|
|
|
|
|
|
|
self.in_words = money_in_words(amount, 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
|
|
|
|
"""
|
2019-11-18 06:16:55 +00:00
|
|
|
stock_and_asset_items = self.get_stock_items() + self.get_asset_items()
|
2014-04-14 13:50:45 +00:00
|
|
|
|
2019-11-18 06:16:55 +00:00
|
|
|
stock_and_asset_items_qty, stock_and_asset_items_amount = 0, 0
|
|
|
|
last_item_idx = 1
|
2014-03-27 10:42:56 +00:00
|
|
|
for d in self.get(parentfield):
|
2019-11-18 06:16:55 +00:00
|
|
|
if d.item_code and d.item_code in stock_and_asset_items:
|
|
|
|
stock_and_asset_items_qty += flt(d.qty)
|
|
|
|
stock_and_asset_items_amount += flt(d.base_net_amount)
|
|
|
|
last_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)):
|
2019-11-18 06:16:55 +00:00
|
|
|
if item.item_code and item.qty and item.item_code in stock_and_asset_items:
|
|
|
|
item_proportion = flt(item.base_net_amount) / stock_and_asset_items_amount if stock_and_asset_items_amount \
|
|
|
|
else flt(item.qty) / stock_and_asset_items_qty
|
2020-01-29 09:36:18 +00:00
|
|
|
|
2019-11-18 06:16:55 +00:00
|
|
|
if i == (last_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))
|
|
|
|
|
2018-01-05 12:49:59 +00:00
|
|
|
if self.doctype == "Purchase Order":
|
|
|
|
for supplied_item in self.get("supplied_items"):
|
|
|
|
if not supplied_item.reserve_warehouse:
|
|
|
|
frappe.throw(_("Reserved Warehouse is mandatory for Item {0} in Raw Materials supplied").format(frappe.bold(supplied_item.rm_item_code)))
|
2017-11-29 04:48:38 +00:00
|
|
|
|
2014-11-05 14:44:53 +00:00
|
|
|
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 = []
|
2018-06-05 07:36:52 +00:00
|
|
|
backflush_raw_materials_based_on = frappe.db.get_single_value("Buying Settings",
|
|
|
|
"backflush_raw_materials_of_subcontract_based_on")
|
|
|
|
if (self.doctype == 'Purchase Receipt' and
|
|
|
|
backflush_raw_materials_based_on != 'BOM'):
|
2019-11-29 07:56:52 +00:00
|
|
|
self.update_raw_materials_supplied_based_on_stock_entries()
|
2018-06-05 07:36:52 +00:00
|
|
|
else:
|
|
|
|
for item in self.get("items"):
|
|
|
|
if self.doctype in ["Purchase Receipt", "Purchase Invoice"]:
|
|
|
|
item.rm_supp_cost = 0.0
|
|
|
|
if item.bom and item.item_code in self.sub_contracted_items:
|
|
|
|
self.update_raw_materials_supplied_based_on_bom(item, raw_material_table)
|
2014-05-08 13:38:20 +00:00
|
|
|
|
2018-06-05 07:36:52 +00:00
|
|
|
if [item.item_code, item.name] not in parent_items:
|
|
|
|
parent_items.append([item.item_code, item.name])
|
2014-05-08 13:38:20 +00:00
|
|
|
|
2018-06-05 07:36:52 +00:00
|
|
|
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', [])
|
|
|
|
|
2019-11-29 07:56:52 +00:00
|
|
|
def update_raw_materials_supplied_based_on_stock_entries(self):
|
|
|
|
self.set('supplied_items', [])
|
2018-06-05 07:36:52 +00:00
|
|
|
|
2019-11-29 07:56:52 +00:00
|
|
|
purchase_orders = set([d.purchase_order for d in self.items])
|
|
|
|
|
|
|
|
# qty of raw materials backflushed (for each item per purchase order)
|
|
|
|
backflushed_raw_materials_map = get_backflushed_subcontracted_raw_materials(purchase_orders)
|
|
|
|
|
|
|
|
# qty of "finished good" item yet to be received
|
|
|
|
qty_to_be_received_map = get_qty_to_be_received(purchase_orders)
|
2018-06-05 07:36:52 +00:00
|
|
|
|
2019-11-29 07:56:52 +00:00
|
|
|
for item in self.get('items'):
|
|
|
|
# reset raw_material cost
|
|
|
|
item.rm_supp_cost = 0
|
|
|
|
|
|
|
|
# qty of raw materials transferred to the supplier
|
|
|
|
transferred_raw_materials = get_subcontracted_raw_materials_from_se(item.purchase_order, item.item_code)
|
|
|
|
|
|
|
|
non_stock_items = get_non_stock_items(item.purchase_order, item.item_code)
|
|
|
|
|
|
|
|
item_key = '{}{}'.format(item.item_code, item.purchase_order)
|
|
|
|
|
|
|
|
fg_yet_to_be_received = qty_to_be_received_map.get(item_key)
|
|
|
|
|
|
|
|
transferred_batch_qty_map = get_transferred_batch_qty_map(item.purchase_order, item.item_code)
|
|
|
|
backflushed_batch_qty_map = get_backflushed_batch_qty_map(item.purchase_order, item.item_code)
|
|
|
|
|
|
|
|
for raw_material in transferred_raw_materials + non_stock_items:
|
2019-12-20 07:26:01 +00:00
|
|
|
rm_item_key = '{}{}'.format(raw_material.rm_item_code, item.purchase_order)
|
|
|
|
raw_material_data = backflushed_raw_materials_map.get(rm_item_key, {})
|
|
|
|
|
|
|
|
consumed_qty = raw_material_data.get('qty', 0)
|
|
|
|
consumed_serial_nos = raw_material_data.get('serial_nos', '')
|
|
|
|
consumed_batch_nos = raw_material_data.get('batch_nos', '')
|
|
|
|
|
2019-11-29 07:56:52 +00:00
|
|
|
transferred_qty = raw_material.qty
|
|
|
|
|
|
|
|
rm_qty_to_be_consumed = transferred_qty - consumed_qty
|
|
|
|
|
|
|
|
# backflush all remaining transferred qty in the last Purchase Receipt
|
|
|
|
if fg_yet_to_be_received == item.qty:
|
|
|
|
qty = rm_qty_to_be_consumed
|
|
|
|
else:
|
|
|
|
qty = (rm_qty_to_be_consumed / fg_yet_to_be_received) * item.qty
|
|
|
|
|
|
|
|
if frappe.get_cached_value('UOM', raw_material.stock_uom, 'must_be_whole_number'):
|
|
|
|
qty = frappe.utils.ceil(qty)
|
|
|
|
|
|
|
|
if qty > rm_qty_to_be_consumed:
|
|
|
|
qty = rm_qty_to_be_consumed
|
|
|
|
|
|
|
|
if not qty: continue
|
|
|
|
|
|
|
|
if raw_material.serial_nos:
|
|
|
|
set_serial_nos(raw_material, consumed_serial_nos, qty)
|
|
|
|
|
|
|
|
if raw_material.batch_nos:
|
|
|
|
batches_qty = get_batches_with_qty(raw_material.rm_item_code, raw_material.main_item_code,
|
|
|
|
qty, transferred_batch_qty_map, backflushed_batch_qty_map)
|
|
|
|
for batch_data in batches_qty:
|
|
|
|
qty = batch_data['qty']
|
|
|
|
raw_material.batch_no = batch_data['batch']
|
|
|
|
self.append_raw_material_to_be_backflushed(item, raw_material, qty)
|
|
|
|
else:
|
|
|
|
self.append_raw_material_to_be_backflushed(item, raw_material, qty)
|
|
|
|
|
|
|
|
def append_raw_material_to_be_backflushed(self, fg_item_doc, raw_material_data, qty):
|
|
|
|
rm = self.append('supplied_items', {})
|
|
|
|
rm.update(raw_material_data)
|
|
|
|
|
|
|
|
rm.required_qty = qty
|
|
|
|
rm.consumed_qty = qty
|
|
|
|
|
|
|
|
if not raw_material_data.get('non_stock_item'):
|
|
|
|
from erpnext.stock.utils import get_incoming_rate
|
|
|
|
rm.rate = get_incoming_rate({
|
|
|
|
"item_code": raw_material_data.rm_item_code,
|
|
|
|
"warehouse": self.supplier_warehouse,
|
|
|
|
"posting_date": self.posting_date,
|
|
|
|
"posting_time": self.posting_time,
|
|
|
|
"qty": -1 * qty,
|
|
|
|
"serial_no": rm.serial_no
|
|
|
|
})
|
|
|
|
|
|
|
|
if not rm.rate:
|
|
|
|
rm.rate = get_valuation_rate(raw_material_data.item_code, self.supplier_warehouse,
|
|
|
|
self.doctype, self.name, currency=self.company_currency, company=self.company)
|
|
|
|
|
|
|
|
rm.amount = qty * flt(rm.rate)
|
|
|
|
fg_item_doc.rm_supp_cost += rm.amount
|
2018-06-05 07:36:52 +00:00
|
|
|
|
|
|
|
def update_raw_materials_supplied_based_on_bom(self, item, raw_material_table):
|
2018-03-07 10:09:40 +00:00
|
|
|
exploded_item = 1
|
|
|
|
if hasattr(item, 'include_exploded_items'):
|
|
|
|
exploded_item = item.get('include_exploded_items')
|
|
|
|
|
|
|
|
bom_items = get_items_from_bom(item.item_code, item.bom, exploded_item)
|
2018-03-28 06:21:44 +00:00
|
|
|
|
|
|
|
used_alternative_items = []
|
|
|
|
if self.doctype == 'Purchase Receipt' and item.purchase_order:
|
|
|
|
used_alternative_items = get_used_alternative_items(purchase_order = item.purchase_order)
|
|
|
|
|
2013-03-01 13:21:10 +00:00
|
|
|
raw_materials_cost = 0
|
2017-11-30 12:44:55 +00:00
|
|
|
items = list(set([d.item_code for d in bom_items]))
|
2018-05-04 10:32:38 +00:00
|
|
|
item_wh = frappe._dict(frappe.db.sql("""select i.item_code, id.default_warehouse
|
2018-05-23 14:07:06 +00:00
|
|
|
from `tabItem` i, `tabItem Default` id
|
|
|
|
where id.parent=i.name and id.company=%s and i.name in ({0})"""
|
2018-05-04 10:32:38 +00:00
|
|
|
.format(", ".join(["%s"] * len(items))), [self.company] + items))
|
2014-05-08 13:38:20 +00:00
|
|
|
|
|
|
|
for bom_item in bom_items:
|
2018-01-05 12:49:59 +00:00
|
|
|
if self.doctype == "Purchase Order":
|
|
|
|
reserve_warehouse = bom_item.source_warehouse or item_wh.get(bom_item.item_code)
|
|
|
|
if frappe.db.get_value("Warehouse", reserve_warehouse, "company") != self.company:
|
|
|
|
reserve_warehouse = None
|
|
|
|
|
2018-03-28 06:21:44 +00:00
|
|
|
conversion_factor = item.conversion_factor
|
|
|
|
if (self.doctype == 'Purchase Receipt' and item.purchase_order and
|
|
|
|
bom_item.item_code in used_alternative_items):
|
|
|
|
alternative_item_data = used_alternative_items.get(bom_item.item_code)
|
|
|
|
bom_item.item_code = alternative_item_data.item_code
|
|
|
|
bom_item.item_name = alternative_item_data.item_name
|
|
|
|
bom_item.stock_uom = alternative_item_data.stock_uom
|
|
|
|
conversion_factor = alternative_item_data.conversion_factor
|
|
|
|
bom_item.description = alternative_item_data.description
|
|
|
|
|
2014-05-08 13:38:20 +00:00
|
|
|
# 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, {})
|
|
|
|
|
2018-02-12 06:29:07 +00:00
|
|
|
required_qty = flt(flt(bom_item.qty_consumed_per_unit) * (flt(item.qty) + getattr(item, 'rejected_qty', 0)) *
|
2018-03-28 06:21:44 +00:00
|
|
|
flt(conversion_factor), rm.precision("required_qty"))
|
2014-05-08 13:38:20 +00:00
|
|
|
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
|
2018-01-05 12:49:59 +00:00
|
|
|
if self.doctype == "Purchase Order" and not rm.reserve_warehouse:
|
|
|
|
rm.reserve_warehouse = reserve_warehouse
|
2014-05-08 13:38:20 +00:00
|
|
|
|
2018-03-28 06:21:44 +00:00
|
|
|
rm.conversion_factor = conversion_factor
|
2014-05-08 13:38:20 +00:00
|
|
|
|
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
|
2019-09-10 08:37:45 +00:00
|
|
|
if item.batch_no and frappe.db.get_value("Item", rm.rm_item_code, "has_batch_no") and not rm.batch_no:
|
2014-05-08 13:38:20 +00:00
|
|
|
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
|
|
|
|
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:
|
2019-11-29 07:56:52 +00:00
|
|
|
items = frappe.get_all('Item', filters={
|
|
|
|
'name': ['in', item_codes],
|
|
|
|
'is_sub_contracted_item': 1
|
|
|
|
})
|
|
|
|
self._sub_contracted_items = [item.name for item in items]
|
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"):
|
2019-08-05 04:44:19 +00:00
|
|
|
# Check if item code is present
|
|
|
|
# Conversion factor should not be mandatory for non itemized items
|
|
|
|
if not d.conversion_factor and d.item_code:
|
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)
|
|
|
|
|
2019-06-26 05:40:17 +00:00
|
|
|
val = flt(d.qty) + flt(d.rejected_qty)
|
2016-04-04 10:19:36 +00:00
|
|
|
# Check Received Qty = Accepted Qty + Rejected Qty
|
2019-06-26 05:40:17 +00:00
|
|
|
if (flt(val, d.precision("received_qty")) != flt(d.received_qty, d.precision("received_qty"))):
|
2016-04-04 10:19:36 +00:00
|
|
|
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:
|
2020-01-29 10:04:06 +00:00
|
|
|
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']))
|
2017-03-13 09:01:48 +00:00
|
|
|
|
2019-03-06 09:16:38 +00:00
|
|
|
def check_for_on_hold_or_closed_status(self, ref_doctype, ref_fieldname):
|
|
|
|
for d in self.get("items"):
|
|
|
|
if d.get(ref_fieldname):
|
|
|
|
status = frappe.db.get_value(ref_doctype, d.get(ref_fieldname), "status")
|
|
|
|
if status in ("Closed", "On Hold"):
|
|
|
|
frappe.throw(_("{0} {1} is {2}").format(ref_doctype,d.get(ref_fieldname), status))
|
|
|
|
|
2016-04-04 06:25:52 +00:00
|
|
|
def update_stock_ledger(self, allow_negative_stock=False, via_landed_cost_voucher=False):
|
2018-01-05 12:49:59 +00:00
|
|
|
self.update_ordered_and_reserved_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
|
|
|
|
2018-01-05 12:49:59 +00:00
|
|
|
def update_ordered_and_reserved_qty(self):
|
2016-04-18 10:24:01 +00:00
|
|
|
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)
|
2018-01-05 12:49:59 +00:00
|
|
|
if self.is_subcontracted:
|
|
|
|
po_obj.update_reserved_qty_for_subcontract()
|
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-12-28 10:41:27 +00:00
|
|
|
def on_submit(self):
|
|
|
|
if self.get('is_return'):
|
|
|
|
return
|
|
|
|
|
2018-04-26 13:48:29 +00:00
|
|
|
if self.doctype in ['Purchase Receipt', 'Purchase Invoice']:
|
2018-05-07 10:28:41 +00:00
|
|
|
field = 'purchase_invoice' if self.doctype == 'Purchase Invoice' else 'purchase_receipt'
|
|
|
|
|
|
|
|
self.process_fixed_asset()
|
|
|
|
self.update_fixed_asset(field)
|
2018-04-26 13:48:29 +00:00
|
|
|
|
2017-12-28 10:41:27 +00:00
|
|
|
update_last_purchase_rate(self, is_submit = 1)
|
|
|
|
|
|
|
|
def on_cancel(self):
|
2019-03-31 19:10:38 +00:00
|
|
|
super(BuyingController, self).on_cancel()
|
|
|
|
|
2017-12-28 10:41:27 +00:00
|
|
|
if self.get('is_return'):
|
|
|
|
return
|
|
|
|
|
|
|
|
update_last_purchase_rate(self, is_submit = 0)
|
2018-04-26 13:48:29 +00:00
|
|
|
if self.doctype in ['Purchase Receipt', 'Purchase Invoice']:
|
2018-05-07 10:28:41 +00:00
|
|
|
field = 'purchase_invoice' if self.doctype == 'Purchase Invoice' else 'purchase_receipt'
|
|
|
|
|
2018-05-07 13:16:53 +00:00
|
|
|
self.delete_linked_asset()
|
|
|
|
self.update_fixed_asset(field, delete_asset=True)
|
2018-04-26 13:48:29 +00:00
|
|
|
|
2018-05-15 16:42:44 +00:00
|
|
|
def validate_budget(self):
|
|
|
|
if self.docstatus == 1:
|
|
|
|
for data in self.get('items'):
|
2018-06-15 12:33:31 +00:00
|
|
|
args = data.as_dict()
|
|
|
|
args.update({
|
|
|
|
'doctype': self.doctype,
|
2018-06-25 05:01:08 +00:00
|
|
|
'company': self.company,
|
|
|
|
'posting_date': (self.schedule_date
|
|
|
|
if self.doctype == 'Material Request' else self.transaction_date)
|
2018-06-15 12:33:31 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
validate_expense_against_budget(args)
|
2018-05-15 16:42:44 +00:00
|
|
|
|
2018-04-26 13:48:29 +00:00
|
|
|
def process_fixed_asset(self):
|
2018-05-07 10:28:41 +00:00
|
|
|
if self.doctype == 'Purchase Invoice' and not self.update_stock:
|
2018-04-26 13:48:29 +00:00
|
|
|
return
|
|
|
|
|
2018-05-07 13:16:53 +00:00
|
|
|
asset_items = self.get_asset_items()
|
2018-04-26 13:48:29 +00:00
|
|
|
if asset_items:
|
2019-11-18 06:16:55 +00:00
|
|
|
self.auto_make_assets(asset_items)
|
2018-04-26 13:48:29 +00:00
|
|
|
|
2019-11-18 06:16:55 +00:00
|
|
|
def auto_make_assets(self, asset_items):
|
2018-04-26 13:48:29 +00:00
|
|
|
items_data = get_asset_item_details(asset_items)
|
2019-11-22 11:02:50 +00:00
|
|
|
messages = []
|
2018-04-26 13:48:29 +00:00
|
|
|
|
|
|
|
for d in self.items:
|
|
|
|
if d.is_fixed_asset:
|
|
|
|
item_data = items_data.get(d.item_code)
|
|
|
|
|
2019-11-18 06:16:55 +00:00
|
|
|
if item_data.get('auto_create_assets'):
|
|
|
|
# If asset has to be auto created
|
|
|
|
# Check for asset naming series
|
|
|
|
if item_data.get('asset_naming_series'):
|
|
|
|
for qty in range(cint(d.qty)):
|
|
|
|
self.make_asset(d)
|
|
|
|
is_plural = 's' if cint(d.qty) != 1 else ''
|
2019-11-22 11:02:50 +00:00
|
|
|
messages.append(_('{0} Asset{2} Created for <b>{1}</b>').format(cint(d.qty), d.item_code, is_plural))
|
2019-11-18 06:16:55 +00:00
|
|
|
else:
|
2019-11-22 11:02:50 +00:00
|
|
|
frappe.throw(_("Row {1}: Asset Naming Series is mandatory for the auto creation for item {0}")
|
|
|
|
.format(d.item_code, d.idx))
|
2019-11-18 06:16:55 +00:00
|
|
|
else:
|
2019-11-22 11:02:50 +00:00
|
|
|
messages.append(_("Assets not created for <b>{0}</b>. You will have to create asset manually.")
|
|
|
|
.format(d.item_code))
|
|
|
|
|
|
|
|
for message in messages:
|
|
|
|
frappe.msgprint(message, title="Success")
|
2018-04-26 13:48:29 +00:00
|
|
|
|
|
|
|
def make_asset(self, row):
|
2018-05-10 20:26:05 +00:00
|
|
|
if not row.asset_location:
|
|
|
|
frappe.throw(_("Row {0}: Enter location for the asset item {1}").format(row.idx, row.item_code))
|
|
|
|
|
2018-05-08 07:52:22 +00:00
|
|
|
item_data = frappe.db.get_value('Item',
|
|
|
|
row.item_code, ['asset_naming_series', 'asset_category'], as_dict=1)
|
|
|
|
|
2019-11-18 06:16:55 +00:00
|
|
|
purchase_amount = flt(row.base_rate + row.item_tax_amount)
|
2018-04-26 13:48:29 +00:00
|
|
|
asset = frappe.get_doc({
|
|
|
|
'doctype': 'Asset',
|
|
|
|
'item_code': row.item_code,
|
2018-05-07 10:28:41 +00:00
|
|
|
'asset_name': row.item_name,
|
2018-05-08 07:52:22 +00:00
|
|
|
'naming_series': item_data.get('asset_naming_series') or 'AST',
|
|
|
|
'asset_category': item_data.get('asset_category'),
|
2018-05-10 20:26:05 +00:00
|
|
|
'location': row.asset_location,
|
2018-04-26 13:48:29 +00:00
|
|
|
'company': self.company,
|
2019-09-24 13:19:16 +00:00
|
|
|
'supplier': self.supplier,
|
2018-04-26 13:48:29 +00:00
|
|
|
'purchase_date': self.posting_date,
|
2018-05-08 18:01:58 +00:00
|
|
|
'calculate_depreciation': 1,
|
2018-05-12 06:36:00 +00:00
|
|
|
'purchase_receipt_amount': purchase_amount,
|
|
|
|
'gross_purchase_amount': purchase_amount,
|
2018-04-26 13:48:29 +00:00
|
|
|
'purchase_receipt': self.name if self.doctype == 'Purchase Receipt' else None,
|
|
|
|
'purchase_invoice': self.name if self.doctype == 'Purchase Invoice' else None
|
|
|
|
})
|
|
|
|
|
|
|
|
asset.flags.ignore_validate = True
|
|
|
|
asset.flags.ignore_mandatory = True
|
2018-05-10 20:26:05 +00:00
|
|
|
asset.set_missing_values()
|
2018-04-26 13:48:29 +00:00
|
|
|
asset.insert()
|
|
|
|
|
2018-05-07 13:16:53 +00:00
|
|
|
def update_fixed_asset(self, field, delete_asset = False):
|
2018-04-26 13:48:29 +00:00
|
|
|
for d in self.get("items"):
|
2019-11-18 06:16:55 +00:00
|
|
|
if d.is_fixed_asset:
|
|
|
|
is_auto_create_enabled = frappe.db.get_value('Item', d.item_code, 'auto_create_assets')
|
|
|
|
assets = frappe.db.get_all('Asset', filters={ field : self.name, 'item_code' : d.item_code })
|
|
|
|
|
|
|
|
for asset in assets:
|
|
|
|
asset = frappe.get_doc('Asset', asset.name)
|
|
|
|
if delete_asset and is_auto_create_enabled:
|
|
|
|
# need to delete movements to delete assets otherwise throws link exists error
|
2019-11-25 06:54:34 +00:00
|
|
|
movements = frappe.db.sql(
|
2020-01-29 09:36:18 +00:00
|
|
|
"""SELECT asm.name
|
2019-11-25 06:54:34 +00:00
|
|
|
FROM `tabAsset Movement` asm, `tabAsset Movement Item` asm_item
|
|
|
|
WHERE asm_item.parent=asm.name and asm_item.asset=%s""", asset.name, as_dict=1)
|
2019-11-18 06:16:55 +00:00
|
|
|
for movement in movements:
|
|
|
|
frappe.delete_doc('Asset Movement', movement.name, force=1)
|
|
|
|
frappe.delete_doc("Asset", asset.name, force=1)
|
|
|
|
continue
|
|
|
|
|
|
|
|
if self.docstatus in [0, 1] and not asset.get(field):
|
|
|
|
asset.set(field, self.name)
|
|
|
|
asset.purchase_date = self.posting_date
|
|
|
|
asset.supplier = self.supplier
|
|
|
|
elif self.docstatus == 2:
|
2019-11-25 06:54:34 +00:00
|
|
|
if asset.docstatus == 0:
|
|
|
|
asset.set(field, None)
|
|
|
|
asset.supplier = None
|
|
|
|
if asset.docstatus == 1 and delete_asset:
|
|
|
|
frappe.throw(_('Cannot cancel this document as it is linked with submitted asset {0}.\
|
|
|
|
Please cancel the it to continue.').format(asset.name))
|
2019-11-18 06:16:55 +00:00
|
|
|
|
|
|
|
asset.flags.ignore_validate_update_after_submit = True
|
|
|
|
asset.flags.ignore_mandatory = True
|
|
|
|
if asset.docstatus == 0:
|
|
|
|
asset.flags.ignore_validate = True
|
|
|
|
|
|
|
|
asset.save()
|
2018-04-26 13:48:29 +00:00
|
|
|
|
2018-05-07 13:16:53 +00:00
|
|
|
def delete_linked_asset(self):
|
2018-05-07 10:28:41 +00:00
|
|
|
if self.doctype == 'Purchase Invoice' and not self.get('update_stock'):
|
2018-04-26 13:48:29 +00:00
|
|
|
return
|
|
|
|
|
2018-09-26 12:31:00 +00:00
|
|
|
frappe.db.sql("delete from `tabAsset Movement` where reference_name=%s", self.name)
|
2017-12-28 10:41:27 +00:00
|
|
|
|
2017-10-05 14:21:10 +00:00
|
|
|
def validate_schedule_date(self):
|
2018-12-20 13:20:24 +00:00
|
|
|
if not self.get("items"):
|
|
|
|
return
|
2019-10-15 07:13:27 +00:00
|
|
|
|
2019-10-21 08:10:00 +00:00
|
|
|
earliest_schedule_date = min([d.schedule_date for d in self.get("items")])
|
|
|
|
if earliest_schedule_date:
|
|
|
|
self.schedule_date = earliest_schedule_date
|
2017-10-05 14:21:10 +00:00
|
|
|
|
|
|
|
if self.schedule_date:
|
|
|
|
for d in self.get('items'):
|
|
|
|
if not d.schedule_date:
|
|
|
|
d.schedule_date = self.schedule_date
|
|
|
|
|
2018-01-05 15:58:01 +00:00
|
|
|
if (d.schedule_date and self.transaction_date and
|
|
|
|
getdate(d.schedule_date) < getdate(self.transaction_date)):
|
2018-01-02 06:20:29 +00:00
|
|
|
frappe.throw(_("Row #{0}: Reqd by Date cannot be before Transaction Date").format(d.idx))
|
2017-10-05 14:21:10 +00:00
|
|
|
else:
|
2018-01-02 06:20:29 +00:00
|
|
|
frappe.throw(_("Please enter Reqd by Date"))
|
2017-11-17 09:01:09 +00:00
|
|
|
|
2018-06-04 07:22:52 +00:00
|
|
|
def validate_items(self):
|
|
|
|
# validate items to see if they have is_purchase_item or is_subcontracted_item enabled
|
2018-06-18 11:03:48 +00:00
|
|
|
if self.doctype=="Material Request": return
|
2018-06-04 07:22:52 +00:00
|
|
|
|
2018-06-05 07:38:10 +00:00
|
|
|
if hasattr(self, "is_subcontracted") and self.is_subcontracted == 'Yes':
|
2018-06-04 07:22:52 +00:00
|
|
|
validate_item_type(self, "is_sub_contracted_item", "subcontracted")
|
|
|
|
else:
|
|
|
|
validate_item_type(self, "is_purchase_item", "purchase")
|
|
|
|
|
2018-03-07 10:09:40 +00:00
|
|
|
def get_items_from_bom(item_code, bom, exploded_item=1):
|
|
|
|
doctype = "BOM Item" if not exploded_item else "BOM Explosion Item"
|
|
|
|
|
|
|
|
bom_items = frappe.db.sql("""select t2.item_code, t2.name,
|
|
|
|
t2.rate, t2.stock_uom, t2.source_warehouse, t2.description,
|
|
|
|
t2.stock_qty / ifnull(t1.quantity, 1) as qty_consumed_per_unit
|
|
|
|
from
|
|
|
|
`tabBOM` t1, `tab{0}` t2, tabItem t3
|
|
|
|
where
|
|
|
|
t2.parent = t1.name and t1.item = %s
|
|
|
|
and t1.docstatus = 1 and t1.is_active = 1 and t1.name = %s
|
2019-08-08 12:49:24 +00:00
|
|
|
and t2.item_code = t3.name""".format(doctype),
|
2018-03-07 10:09:40 +00:00
|
|
|
(item_code, bom), as_dict=1)
|
|
|
|
|
|
|
|
if not bom_items:
|
|
|
|
msgprint(_("Specified BOM {0} does not exist for Item {1}").format(bom, item_code), raise_exception=1)
|
|
|
|
|
2018-03-28 06:21:44 +00:00
|
|
|
return bom_items
|
2018-04-26 13:48:29 +00:00
|
|
|
|
2019-11-29 07:56:52 +00:00
|
|
|
def get_subcontracted_raw_materials_from_se(purchase_order, fg_item):
|
|
|
|
common_query = """
|
|
|
|
SELECT
|
|
|
|
sed.item_code AS rm_item_code,
|
|
|
|
SUM(sed.qty) AS qty,
|
|
|
|
sed.description,
|
|
|
|
sed.stock_uom,
|
|
|
|
sed.subcontracted_item AS main_item_code,
|
|
|
|
{serial_no_concat_syntax} AS serial_nos,
|
|
|
|
{batch_no_concat_syntax} AS batch_nos
|
|
|
|
FROM `tabStock Entry` se,`tabStock Entry Detail` sed
|
|
|
|
WHERE
|
|
|
|
se.name = sed.parent
|
|
|
|
AND se.docstatus=1
|
|
|
|
AND se.purpose='Send to Subcontractor'
|
|
|
|
AND se.purchase_order = %s
|
|
|
|
AND IFNULL(sed.t_warehouse, '') != ''
|
|
|
|
AND sed.subcontracted_item = %s
|
|
|
|
GROUP BY sed.item_code, sed.subcontracted_item
|
|
|
|
"""
|
|
|
|
raw_materials = frappe.db.multisql({
|
|
|
|
'mariadb': common_query.format(
|
|
|
|
serial_no_concat_syntax="GROUP_CONCAT(sed.serial_no)",
|
|
|
|
batch_no_concat_syntax="GROUP_CONCAT(sed.batch_no)"
|
|
|
|
),
|
|
|
|
'postgres': common_query.format(
|
|
|
|
serial_no_concat_syntax="STRING_AGG(sed.serial_no, ',')",
|
|
|
|
batch_no_concat_syntax="STRING_AGG(sed.batch_no, ',')"
|
|
|
|
)
|
|
|
|
}, (purchase_order, fg_item), as_dict=1)
|
|
|
|
|
|
|
|
return raw_materials
|
|
|
|
|
|
|
|
def get_backflushed_subcontracted_raw_materials(purchase_orders):
|
|
|
|
common_query = """
|
|
|
|
SELECT
|
|
|
|
CONCAT(prsi.rm_item_code, pri.purchase_order) AS item_key,
|
|
|
|
SUM(prsi.consumed_qty) AS qty,
|
|
|
|
{serial_no_concat_syntax} AS serial_nos,
|
|
|
|
{batch_no_concat_syntax} AS batch_nos
|
|
|
|
FROM `tabPurchase Receipt` pr, `tabPurchase Receipt Item` pri, `tabPurchase Receipt Item Supplied` prsi
|
|
|
|
WHERE
|
|
|
|
pr.name = pri.parent
|
|
|
|
AND pr.name = prsi.parent
|
|
|
|
AND pri.purchase_order IN %s
|
|
|
|
AND pri.item_code = prsi.main_item_code
|
|
|
|
AND pr.docstatus = 1
|
|
|
|
GROUP BY prsi.rm_item_code, pri.purchase_order
|
|
|
|
"""
|
|
|
|
|
|
|
|
backflushed_raw_materials = frappe.db.multisql({
|
|
|
|
'mariadb': common_query.format(
|
|
|
|
serial_no_concat_syntax="GROUP_CONCAT(prsi.serial_no)",
|
|
|
|
batch_no_concat_syntax="GROUP_CONCAT(prsi.batch_no)"
|
|
|
|
),
|
|
|
|
'postgres': common_query.format(
|
|
|
|
serial_no_concat_syntax="STRING_AGG(prsi.serial_no, ',')",
|
|
|
|
batch_no_concat_syntax="STRING_AGG(prsi.batch_no, ',')"
|
|
|
|
)
|
|
|
|
}, (purchase_orders, ), as_dict=1)
|
|
|
|
|
|
|
|
backflushed_raw_materials_map = frappe._dict()
|
|
|
|
for item in backflushed_raw_materials:
|
|
|
|
backflushed_raw_materials_map.setdefault(item.item_key, item)
|
|
|
|
|
|
|
|
return backflushed_raw_materials_map
|
2018-06-05 07:36:52 +00:00
|
|
|
|
2018-04-26 13:48:29 +00:00
|
|
|
def get_asset_item_details(asset_items):
|
|
|
|
asset_items_data = {}
|
2019-11-18 06:16:55 +00:00
|
|
|
for d in frappe.get_all('Item', fields = ["name", "auto_create_assets", "asset_naming_series"],
|
2018-04-26 13:48:29 +00:00
|
|
|
filters = {'name': ('in', asset_items)}):
|
|
|
|
asset_items_data.setdefault(d.name, d)
|
|
|
|
|
|
|
|
return asset_items_data
|
2018-06-06 05:53:19 +00:00
|
|
|
|
2018-06-04 07:22:52 +00:00
|
|
|
def validate_item_type(doc, fieldname, message):
|
|
|
|
# iterate through items and check if they are valid sales or purchase items
|
2018-06-05 05:02:09 +00:00
|
|
|
items = [d.item_code for d in doc.items if d.item_code]
|
|
|
|
|
|
|
|
# No validation check inase of creating transaction using 'Opening Invoice Creation Tool'
|
|
|
|
if not items:
|
|
|
|
return
|
|
|
|
|
2018-09-21 04:50:52 +00:00
|
|
|
item_list = ", ".join(["%s" % frappe.db.escape(d) for d in items])
|
2018-06-04 07:22:52 +00:00
|
|
|
|
|
|
|
invalid_items = [d[0] for d in frappe.db.sql("""
|
|
|
|
select item_code from tabItem where name in ({0}) and {1}=0
|
|
|
|
""".format(item_list, fieldname), as_list=True)]
|
|
|
|
|
|
|
|
if invalid_items:
|
2018-09-19 07:43:59 +00:00
|
|
|
items = ", ".join([d for d in invalid_items])
|
|
|
|
|
|
|
|
if len(invalid_items) > 1:
|
2020-01-29 09:36:18 +00:00
|
|
|
error_message = _("Following items {0} are not marked as {1} item. You can enable them as {1} item from its Item master").format(items, message)
|
2018-09-19 07:43:59 +00:00
|
|
|
else:
|
2020-01-29 09:36:18 +00:00
|
|
|
error_message = _("Following item {0} is not marked as {1} item. You can enable them as {1} item from its Item master").format(items, message)
|
2018-09-19 07:43:59 +00:00
|
|
|
|
|
|
|
frappe.throw(error_message)
|
2019-11-29 07:56:52 +00:00
|
|
|
|
|
|
|
def get_qty_to_be_received(purchase_orders):
|
|
|
|
return frappe._dict(frappe.db.sql("""
|
|
|
|
SELECT CONCAT(poi.`item_code`, poi.`parent`) AS item_key,
|
|
|
|
SUM(poi.`qty`) - SUM(poi.`received_qty`) AS qty_to_be_received
|
|
|
|
FROM `tabPurchase Order Item` poi
|
|
|
|
WHERE
|
|
|
|
poi.`parent` in %s
|
|
|
|
GROUP BY poi.`item_code`, poi.`parent`
|
|
|
|
HAVING SUM(poi.`qty`) > SUM(poi.`received_qty`)
|
|
|
|
""", (purchase_orders)))
|
|
|
|
|
|
|
|
def get_non_stock_items(purchase_order, fg_item_code):
|
|
|
|
return frappe.db.sql("""
|
|
|
|
SELECT
|
|
|
|
pois.main_item_code,
|
|
|
|
pois.rm_item_code,
|
|
|
|
item.description,
|
|
|
|
pois.required_qty AS qty,
|
|
|
|
pois.rate,
|
|
|
|
1 as non_stock_item,
|
|
|
|
pois.stock_uom
|
|
|
|
FROM `tabPurchase Order Item Supplied` pois, `tabItem` item
|
|
|
|
WHERE
|
|
|
|
pois.`rm_item_code` = item.`name`
|
|
|
|
AND item.is_stock_item = 0
|
|
|
|
AND pois.`parent` = %s
|
|
|
|
AND pois.`main_item_code` = %s
|
|
|
|
""", (purchase_order, fg_item_code), as_dict=1)
|
|
|
|
|
|
|
|
|
|
|
|
def set_serial_nos(raw_material, consumed_serial_nos, qty):
|
|
|
|
serial_nos = set(get_serial_nos(raw_material.serial_nos)) - \
|
|
|
|
set(get_serial_nos(consumed_serial_nos))
|
|
|
|
if serial_nos and qty <= len(serial_nos):
|
|
|
|
raw_material.serial_no = '\n'.join(list(serial_nos)[0:frappe.utils.cint(qty)])
|
|
|
|
|
|
|
|
def get_transferred_batch_qty_map(purchase_order, fg_item):
|
|
|
|
# returns
|
|
|
|
# {
|
|
|
|
# (item_code, fg_code): {
|
|
|
|
# batch1: 10, # qty
|
|
|
|
# batch2: 16
|
|
|
|
# },
|
|
|
|
# }
|
|
|
|
transferred_batch_qty_map = {}
|
|
|
|
transferred_batches = frappe.db.sql("""
|
|
|
|
SELECT
|
|
|
|
sed.batch_no,
|
|
|
|
SUM(sed.qty) AS qty,
|
|
|
|
sed.item_code
|
|
|
|
FROM `tabStock Entry` se,`tabStock Entry Detail` sed
|
|
|
|
WHERE
|
|
|
|
se.name = sed.parent
|
|
|
|
AND se.docstatus=1
|
|
|
|
AND se.purpose='Send to Subcontractor'
|
|
|
|
AND se.purchase_order = %s
|
|
|
|
AND sed.subcontracted_item = %s
|
|
|
|
AND sed.batch_no IS NOT NULL
|
|
|
|
GROUP BY
|
|
|
|
sed.batch_no,
|
|
|
|
sed.item_code
|
|
|
|
""", (purchase_order, fg_item), as_dict=1)
|
|
|
|
|
|
|
|
for batch_data in transferred_batches:
|
|
|
|
transferred_batch_qty_map.setdefault((batch_data.item_code, fg_item), {})
|
|
|
|
transferred_batch_qty_map[(batch_data.item_code, fg_item)][batch_data.batch_no] = batch_data.qty
|
|
|
|
|
|
|
|
return transferred_batch_qty_map
|
|
|
|
|
|
|
|
def get_backflushed_batch_qty_map(purchase_order, fg_item):
|
|
|
|
# returns
|
|
|
|
# {
|
|
|
|
# (item_code, fg_code): {
|
|
|
|
# batch1: 10, # qty
|
|
|
|
# batch2: 16
|
|
|
|
# },
|
|
|
|
# }
|
|
|
|
backflushed_batch_qty_map = {}
|
|
|
|
backflushed_batches = frappe.db.sql("""
|
|
|
|
SELECT
|
|
|
|
pris.batch_no,
|
|
|
|
SUM(pris.consumed_qty) AS qty,
|
|
|
|
pris.rm_item_code AS item_code
|
|
|
|
FROM `tabPurchase Receipt` pr, `tabPurchase Receipt Item` pri, `tabPurchase Receipt Item Supplied` pris
|
|
|
|
WHERE
|
|
|
|
pr.name = pri.parent
|
|
|
|
AND pri.parent = pris.parent
|
|
|
|
AND pri.purchase_order = %s
|
|
|
|
AND pri.item_code = pris.main_item_code
|
|
|
|
AND pr.docstatus = 1
|
|
|
|
AND pris.main_item_code = %s
|
|
|
|
AND pris.batch_no IS NOT NULL
|
|
|
|
GROUP BY
|
|
|
|
pris.rm_item_code, pris.batch_no
|
|
|
|
""", (purchase_order, fg_item), as_dict=1)
|
|
|
|
|
|
|
|
for batch_data in backflushed_batches:
|
|
|
|
backflushed_batch_qty_map.setdefault((batch_data.item_code, fg_item), {})
|
|
|
|
backflushed_batch_qty_map[(batch_data.item_code, fg_item)][batch_data.batch_no] = batch_data.qty
|
|
|
|
|
|
|
|
return backflushed_batch_qty_map
|
|
|
|
|
|
|
|
def get_batches_with_qty(item_code, fg_item, required_qty, transferred_batch_qty_map, backflushed_batch_qty_map):
|
|
|
|
# Returns available batches to be backflushed based on requirements
|
|
|
|
transferred_batches = transferred_batch_qty_map.get((item_code, fg_item), {})
|
|
|
|
backflushed_batches = backflushed_batch_qty_map.get((item_code, fg_item), {})
|
|
|
|
|
|
|
|
available_batches = []
|
|
|
|
|
|
|
|
for (batch, transferred_qty) in transferred_batches.items():
|
|
|
|
backflushed_qty = backflushed_batches.get(batch, 0)
|
|
|
|
available_qty = transferred_qty - backflushed_qty
|
|
|
|
|
|
|
|
if available_qty >= required_qty:
|
|
|
|
available_batches.append({'batch': batch, 'qty': required_qty})
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
available_batches.append({'batch': batch, 'qty': available_qty})
|
|
|
|
required_qty -= available_qty
|
|
|
|
|
|
|
|
return available_batches
|