2013-11-20 07:29:58 +00:00
|
|
|
# Copyright (c) 2013, Web Notes 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
|
2013-10-18 11:30:53 +00:00
|
|
|
import webnotes
|
2013-01-17 14:50:56 +00:00
|
|
|
from webnotes import _, msgprint
|
2013-08-19 14:28:23 +00:00
|
|
|
from webnotes.utils import flt, _round
|
2013-12-12 13:42:19 +00:00
|
|
|
from erpnext.buying.utils import get_item_details
|
|
|
|
from erpnext.setup.utils import get_company_currency
|
2014-01-28 12:13:10 +00:00
|
|
|
from erpnext.buying.doctype.supplier.supplier import get_supplier_details
|
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):
|
2013-05-24 13:55:01 +00:00
|
|
|
def onload_post_render(self):
|
|
|
|
# contact, address, item details
|
|
|
|
self.set_missing_values()
|
|
|
|
|
2013-03-20 07:25:28 +00:00
|
|
|
def validate(self):
|
|
|
|
super(BuyingController, self).validate()
|
2013-07-08 06:38:06 +00:00
|
|
|
if self.doc.supplier and not self.doc.supplier_name:
|
|
|
|
self.doc.supplier_name = webnotes.conn.get_value("Supplier",
|
|
|
|
self.doc.supplier, "supplier_name")
|
2013-10-18 11:30:53 +00:00
|
|
|
self.is_item_table_empty()
|
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()
|
2013-05-15 15:45:57 +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)
|
|
|
|
|
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")
|
2013-06-14 12:14:03 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
# set contact and address details for supplier, if they are not mentioned
|
2014-01-28 12:13:10 +00:00
|
|
|
if self.doc.supplier:
|
2014-01-29 09:56:04 +00:00
|
|
|
self.doc.update_if_missing(get_supplier_details(self.doc.supplier))
|
2013-07-08 06:38:06 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
self.set_missing_item_details(get_item_details)
|
2013-08-29 10:02:46 +00:00
|
|
|
if self.doc.fields.get("__islocal"):
|
2014-01-29 11:01:38 +00:00
|
|
|
self.set_taxes("other_charges", "taxes_and_charges")
|
2013-07-08 07:07:59 +00:00
|
|
|
|
2013-08-09 09:59:59 +00:00
|
|
|
def set_supplier_from_item_default(self):
|
|
|
|
if self.meta.get_field("supplier") and not self.doc.supplier:
|
|
|
|
for d in self.doclist.get({"doctype": self.tname}):
|
|
|
|
supplier = webnotes.conn.get_value("Item", d.item_code, "default_supplier")
|
|
|
|
if supplier:
|
|
|
|
self.doc.supplier = supplier
|
|
|
|
break
|
2013-10-10 10:34:40 +00:00
|
|
|
|
|
|
|
def validate_warehouse(self):
|
2013-12-26 13:00:39 +00:00
|
|
|
from erpnext.stock.utils import validate_warehouse_company
|
2013-10-10 10:34:40 +00:00
|
|
|
|
|
|
|
warehouses = list(set([d.warehouse for d in
|
|
|
|
self.doclist.get({"doctype": self.tname}) if d.warehouse]))
|
|
|
|
|
|
|
|
for w in warehouses:
|
|
|
|
validate_warehouse_company(w, self.doc.company)
|
2013-08-09 09:59:59 +00:00
|
|
|
|
2013-04-26 08:05:06 +00:00
|
|
|
def validate_stock_or_nonstock_items(self):
|
2013-08-02 09:20:12 +00:00
|
|
|
if not self.get_stock_items():
|
2013-04-26 08:05:06 +00:00
|
|
|
tax_for_valuation = [d.account_head for d in
|
2014-01-29 11:01:38 +00:00
|
|
|
self.doclist.get({"parentfield": "other_charges"})
|
2013-04-26 08:05:06 +00:00
|
|
|
if d.category in ["Valuation", "Valuation and Total"]]
|
|
|
|
if tax_for_valuation:
|
2013-12-19 12:57:48 +00:00
|
|
|
webnotes.msgprint(_("""Tax Category can not be 'Valuation' or 'Valuation and Total' as all items are non-stock items"""), raise_exception=1)
|
2013-04-26 08:05:06 +00:00
|
|
|
|
2013-01-21 11:54:31 +00:00
|
|
|
def set_total_in_words(self):
|
2013-01-22 05:42:02 +00:00
|
|
|
from webnotes.utils import money_in_words
|
2013-01-21 11:54:31 +00:00
|
|
|
company_currency = get_company_currency(self.doc.company)
|
2013-01-22 05:42:02 +00:00
|
|
|
if self.meta.get_field("in_words"):
|
|
|
|
self.doc.in_words = money_in_words(self.doc.grand_total, company_currency)
|
|
|
|
if self.meta.get_field("in_words_import"):
|
|
|
|
self.doc.in_words_import = money_in_words(self.doc.grand_total_import,
|
2013-02-06 12:03:46 +00:00
|
|
|
self.doc.currency)
|
|
|
|
|
|
|
|
def calculate_taxes_and_totals(self):
|
2014-01-29 11:01:38 +00:00
|
|
|
self.other_fname = "other_charges"
|
2013-05-24 13:55:01 +00:00
|
|
|
super(BuyingController, self).calculate_taxes_and_totals()
|
2013-05-28 11:53:36 +00:00
|
|
|
self.calculate_total_advance("Purchase Invoice", "advance_allocation_details")
|
2013-02-08 13:58:14 +00:00
|
|
|
|
|
|
|
def calculate_item_values(self):
|
2013-05-06 07:22:46 +00:00
|
|
|
# hack! - cleaned up in _cleanup()
|
|
|
|
if self.doc.doctype != "Purchase Invoice":
|
2013-05-10 12:38:32 +00:00
|
|
|
df = self.meta.get_field("purchase_rate", parentfield=self.fname)
|
|
|
|
df.fieldname = "rate"
|
2013-02-26 12:39:47 +00:00
|
|
|
|
2013-05-06 07:22:46 +00:00
|
|
|
for item in self.item_doclist:
|
2013-02-26 12:39:47 +00:00
|
|
|
# hack! - cleaned up in _cleanup()
|
|
|
|
if self.doc.doctype != "Purchase Invoice":
|
|
|
|
item.rate = item.purchase_rate
|
|
|
|
|
2013-05-11 14:09:53 +00:00
|
|
|
self.round_floats_in(item)
|
2013-02-08 13:58:14 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
if item.discount_rate == 100.0:
|
|
|
|
item.import_rate = 0.0
|
2013-08-13 07:31:12 +00:00
|
|
|
elif not item.import_rate:
|
2013-05-24 13:55:01 +00:00
|
|
|
item.import_rate = flt(item.import_ref_rate * (1.0 - (item.discount_rate / 100.0)),
|
|
|
|
self.precision("import_rate", item))
|
2013-02-26 12:39:47 +00:00
|
|
|
|
2013-02-08 13:58:14 +00:00
|
|
|
item.import_amount = flt(item.import_rate * item.qty,
|
2013-05-11 14:09:53 +00:00
|
|
|
self.precision("import_amount", item))
|
2013-05-24 13:55:01 +00:00
|
|
|
item.item_tax_amount = 0.0;
|
2013-12-23 06:44:45 +00:00
|
|
|
|
|
|
|
self._set_in_company_currency(item, "import_amount", "amount")
|
2013-05-24 13:55:01 +00:00
|
|
|
self._set_in_company_currency(item, "import_ref_rate", "purchase_ref_rate")
|
|
|
|
self._set_in_company_currency(item, "import_rate", "rate")
|
2013-12-23 06:44:45 +00:00
|
|
|
|
2013-05-10 13:53:02 +00:00
|
|
|
|
2013-02-08 13:58:14 +00:00
|
|
|
def calculate_net_total(self):
|
2013-05-24 13:55:01 +00:00
|
|
|
self.doc.net_total = self.doc.net_total_import = 0.0
|
2013-02-08 13:58:14 +00:00
|
|
|
|
|
|
|
for item in self.item_doclist:
|
|
|
|
self.doc.net_total += item.amount
|
|
|
|
self.doc.net_total_import += item.import_amount
|
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
self.round_floats_in(self.doc, ["net_total", "net_total_import"])
|
2013-02-08 13:58:14 +00:00
|
|
|
|
|
|
|
def calculate_totals(self):
|
2014-01-16 08:54:32 +00:00
|
|
|
self.doc.grand_total = flt(self.tax_doclist[-1].total if self.tax_doclist
|
|
|
|
else self.doc.net_total, self.precision("grand_total"))
|
2013-05-24 13:55:01 +00:00
|
|
|
self.doc.grand_total_import = flt(self.doc.grand_total / self.doc.conversion_rate,
|
|
|
|
self.precision("grand_total_import"))
|
2013-02-08 13:58:14 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
self.doc.total_tax = flt(self.doc.grand_total - self.doc.net_total,
|
2013-05-10 13:53:02 +00:00
|
|
|
self.precision("total_tax"))
|
2013-02-08 13:58:14 +00:00
|
|
|
|
|
|
|
if self.meta.get_field("rounded_total"):
|
2013-08-19 14:28:23 +00:00
|
|
|
self.doc.rounded_total = _round(self.doc.grand_total)
|
2013-02-08 13:58:14 +00:00
|
|
|
|
|
|
|
if self.meta.get_field("rounded_total_import"):
|
2013-08-19 14:28:23 +00:00
|
|
|
self.doc.rounded_total_import = _round(self.doc.grand_total_import)
|
2014-01-16 08:54:32 +00:00
|
|
|
|
|
|
|
if self.meta.get_field("other_charges_added"):
|
|
|
|
self.doc.other_charges_added = flt(sum([flt(d.tax_amount) for d in self.tax_doclist
|
|
|
|
if d.add_deduct_tax=="Add" and d.category in ["Valuation and Total", "Total"]]),
|
|
|
|
self.precision("other_charges_added"))
|
|
|
|
|
|
|
|
if self.meta.get_field("other_charges_deducted"):
|
|
|
|
self.doc.other_charges_deducted = flt(sum([flt(d.tax_amount) for d in self.tax_doclist
|
|
|
|
if d.add_deduct_tax=="Deduct" and d.category in ["Valuation and Total", "Total"]]),
|
|
|
|
self.precision("other_charges_deducted"))
|
|
|
|
|
|
|
|
if self.meta.get_field("other_charges_added_import"):
|
|
|
|
self.doc.other_charges_added_import = flt(self.doc.other_charges_added /
|
|
|
|
self.doc.conversion_rate, self.precision("other_charges_added_import"))
|
|
|
|
|
|
|
|
if self.meta.get_field("other_charges_deducted_import"):
|
|
|
|
self.doc.other_charges_deducted_import = flt(self.doc.other_charges_deducted /
|
|
|
|
self.doc.conversion_rate, self.precision("other_charges_deducted_import"))
|
2013-02-08 13:58:14 +00:00
|
|
|
|
|
|
|
def calculate_outstanding_amount(self):
|
2013-05-14 08:05:09 +00:00
|
|
|
if self.doc.doctype == "Purchase Invoice" and self.doc.docstatus < 2:
|
2013-02-08 13:58:14 +00:00
|
|
|
self.doc.total_advance = flt(self.doc.total_advance,
|
2013-05-10 13:53:02 +00:00
|
|
|
self.precision("total_advance"))
|
2013-02-27 08:04:22 +00:00
|
|
|
self.doc.total_amount_to_pay = flt(self.doc.grand_total - flt(self.doc.write_off_amount,
|
2013-05-10 13:53:02 +00:00
|
|
|
self.precision("write_off_amount")), self.precision("total_amount_to_pay"))
|
2013-02-27 08:04:22 +00:00
|
|
|
self.doc.outstanding_amount = flt(self.doc.total_amount_to_pay - self.doc.total_advance,
|
2013-05-10 13:53:02 +00:00
|
|
|
self.precision("outstanding_amount"))
|
2013-02-08 13:58:14 +00:00
|
|
|
|
|
|
|
def _cleanup(self):
|
2013-05-24 13:55:01 +00:00
|
|
|
super(BuyingController, self)._cleanup()
|
|
|
|
|
2014-01-03 12:45:07 +00:00
|
|
|
# except in purchase invoice, rate field is purchase_rate
|
2013-05-10 13:53:02 +00:00
|
|
|
# reset fieldname of rate
|
|
|
|
if self.doc.doctype != "Purchase Invoice":
|
|
|
|
df = self.meta.get_field("rate", parentfield=self.fname)
|
|
|
|
df.fieldname = "purchase_rate"
|
2013-02-08 13:58:14 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
for item in self.item_doclist:
|
|
|
|
item.purchase_rate = item.rate
|
|
|
|
del item.fields["rate"]
|
2013-06-05 12:16:20 +00:00
|
|
|
|
2013-05-28 11:53:36 +00:00
|
|
|
if not self.meta.get_field("item_tax_amount", parentfield=self.fname):
|
|
|
|
for item in self.item_doclist:
|
|
|
|
del item.fields["item_tax_amount"]
|
2014-01-20 11:48:16 +00:00
|
|
|
|
|
|
|
if not self.meta.get_field("tax_amount_after_discount_amount",
|
|
|
|
parentfield=self.other_fname):
|
2013-12-23 10:19:08 +00:00
|
|
|
for tax in self.tax_doclist:
|
2014-01-03 12:45:07 +00:00
|
|
|
del tax.fields["tax_amount_after_discount_amount"]
|
2013-05-28 11:53:36 +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
|
|
|
|
stored for valuation
|
|
|
|
|
|
|
|
TODO: rename item_tax_amount to valuation_tax_amount
|
|
|
|
"""
|
2014-01-02 11:00:16 +00:00
|
|
|
stock_items = self.get_stock_items()
|
|
|
|
|
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-01-03 06:43:18 +00:00
|
|
|
for d in self.doclist.get({"parentfield": parentfield}):
|
|
|
|
if d.item_code and d.item_code in stock_items:
|
|
|
|
stock_items_qty += flt(d.qty)
|
|
|
|
stock_items_amount += flt(d.amount)
|
2014-01-14 12:14:34 +00:00
|
|
|
last_stock_item_idx = d.idx
|
2014-01-02 11:00:16 +00:00
|
|
|
|
|
|
|
total_valuation_amount = sum([flt(d.tax_amount) for d in
|
2014-01-29 11:01:38 +00:00
|
|
|
self.doclist.get({"parentfield": "other_charges"})
|
2014-01-02 11:00:16 +00:00
|
|
|
if d.category in ["Valuation", "Valuation and Total"]])
|
2013-05-11 14:09:53 +00:00
|
|
|
|
2014-01-14 12:14:34 +00:00
|
|
|
|
|
|
|
valuation_amount_adjustment = total_valuation_amount
|
|
|
|
for i, item in enumerate(self.doclist.get({"parentfield": parentfield})):
|
2014-01-03 06:43:18 +00:00
|
|
|
if item.item_code and item.qty and item.item_code in stock_items:
|
|
|
|
item_proportion = flt(item.amount) / stock_items_amount if stock_items_amount \
|
|
|
|
else flt(item.qty) / stock_items_qty
|
|
|
|
|
2014-01-14 12:14:34 +00:00
|
|
|
if i == (last_stock_item_idx - 1):
|
|
|
|
item.item_tax_amount = flt(valuation_amount_adjustment,
|
|
|
|
self.precision("item_tax_amount", item))
|
|
|
|
else:
|
|
|
|
item.item_tax_amount = flt(item_proportion * total_valuation_amount,
|
|
|
|
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)
|
2014-01-02 11:00:16 +00:00
|
|
|
|
|
|
|
item.conversion_factor = item.conversion_factor or flt(webnotes.conn.get_value(
|
|
|
|
"UOM Conversion Detail", {"parent": item.item_code, "uom": item.uom},
|
|
|
|
"conversion_factor")) or 1
|
2013-12-23 06:44:45 +00:00
|
|
|
qty_in_stock_uom = flt(item.qty * item.conversion_factor)
|
|
|
|
item.valuation_rate = ((item.amount + item.item_tax_amount + item.rm_supp_cost)
|
|
|
|
/ 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
|
2013-03-01 13:21:10 +00:00
|
|
|
|
|
|
|
def validate_for_subcontracting(self):
|
|
|
|
if not self.doc.is_subcontracted and self.sub_contracted_items:
|
|
|
|
webnotes.msgprint(_("""Please enter whether %s is made for subcontracting or purchasing,
|
|
|
|
in 'Is Subcontracted' field""" % self.doc.doctype), raise_exception=1)
|
|
|
|
|
|
|
|
if self.doc.doctype == "Purchase Receipt" and self.doc.is_subcontracted=="Yes" \
|
|
|
|
and not self.doc.supplier_warehouse:
|
|
|
|
webnotes.msgprint(_("Supplier Warehouse mandatory subcontracted purchase receipt"),
|
|
|
|
raise_exception=1)
|
|
|
|
|
|
|
|
def update_raw_materials_supplied(self, raw_material_table):
|
|
|
|
self.doclist = self.doc.clear_table(self.doclist, raw_material_table)
|
|
|
|
if self.doc.is_subcontracted=="Yes":
|
|
|
|
for item in self.doclist.get({"parentfield": self.fname}):
|
|
|
|
if item.item_code in self.sub_contracted_items:
|
|
|
|
self.add_bom_items(item, raw_material_table)
|
|
|
|
|
|
|
|
def add_bom_items(self, d, raw_material_table):
|
|
|
|
bom_items = self.get_items_from_default_bom(d.item_code)
|
|
|
|
raw_materials_cost = 0
|
|
|
|
for item in bom_items:
|
|
|
|
required_qty = flt(item.qty_consumed_per_unit) * flt(d.qty) * flt(d.conversion_factor)
|
|
|
|
rm_doclist = {
|
|
|
|
"parentfield": raw_material_table,
|
|
|
|
"doctype": self.doc.doctype + " Item Supplied",
|
|
|
|
"reference_name": d.name,
|
|
|
|
"bom_detail_no": item.name,
|
|
|
|
"main_item_code": d.item_code,
|
|
|
|
"rm_item_code": item.item_code,
|
|
|
|
"stock_uom": item.stock_uom,
|
|
|
|
"required_qty": required_qty,
|
|
|
|
"conversion_factor": d.conversion_factor,
|
|
|
|
"rate": item.rate,
|
|
|
|
"amount": required_qty * flt(item.rate)
|
|
|
|
}
|
|
|
|
if self.doc.doctype == "Purchase Receipt":
|
|
|
|
rm_doclist.update({
|
|
|
|
"consumed_qty": required_qty,
|
|
|
|
"description": item.description,
|
|
|
|
})
|
|
|
|
|
|
|
|
self.doclist.append(rm_doclist)
|
|
|
|
|
|
|
|
raw_materials_cost += required_qty * flt(item.rate)
|
|
|
|
|
|
|
|
if self.doc.doctype == "Purchase Receipt":
|
|
|
|
d.rm_supp_cost = raw_materials_cost
|
|
|
|
|
|
|
|
def get_items_from_default_bom(self, item_code):
|
|
|
|
# print webnotes.conn.sql("""select name from `tabBOM` where item = '_Test FG Item'""")
|
|
|
|
bom_items = webnotes.conn.sql("""select t2.item_code, t2.qty_consumed_per_unit,
|
|
|
|
t2.rate, t2.stock_uom, t2.name, t2.description
|
|
|
|
from `tabBOM` t1, `tabBOM Item` t2
|
|
|
|
where t2.parent = t1.name and t1.item = %s and t1.is_default = 1
|
|
|
|
and t1.docstatus = 1 and t1.is_active = 1""", item_code, as_dict=1)
|
|
|
|
if not bom_items:
|
|
|
|
msgprint(_("No default BOM exists for item: ") + item_code, raise_exception=1)
|
|
|
|
|
|
|
|
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 = []
|
2013-02-27 12:41:17 +00:00
|
|
|
item_codes = list(set(item.item_code for item in
|
|
|
|
self.doclist.get({"parentfield": self.fname})))
|
2013-04-23 10:06:26 +00:00
|
|
|
if item_codes:
|
|
|
|
self._sub_contracted_items = [r[0] for r in webnotes.conn.sql("""select name
|
|
|
|
from `tabItem` where name in (%s) and is_sub_contracted_item='Yes'""" % \
|
|
|
|
(", ".join((["%s"]*len(item_codes))),), item_codes)]
|
2013-02-27 12:41:17 +00:00
|
|
|
|
|
|
|
return self._sub_contracted_items
|
|
|
|
|
|
|
|
@property
|
|
|
|
def purchase_items(self):
|
|
|
|
if not hasattr(self, "_purchase_items"):
|
2013-04-23 10:06:26 +00:00
|
|
|
self._purchase_items = []
|
2013-02-27 12:41:17 +00:00
|
|
|
item_codes = list(set(item.item_code for item in
|
|
|
|
self.doclist.get({"parentfield": self.fname})))
|
2013-04-23 10:06:26 +00:00
|
|
|
if item_codes:
|
|
|
|
self._purchase_items = [r[0] for r in webnotes.conn.sql("""select name
|
|
|
|
from `tabItem` where name in (%s) and is_purchase_item='Yes'""" % \
|
|
|
|
(", ".join((["%s"]*len(item_codes))),), item_codes)]
|
2013-02-27 12:41:17 +00:00
|
|
|
|
2013-02-27 12:44:28 +00:00
|
|
|
return self._purchase_items
|
2013-10-18 11:30:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
def is_item_table_empty(self):
|
|
|
|
if not len(self.doclist.get({"parentfield": self.fname})):
|
|
|
|
webnotes.throw(_("Item table can not be blank"))
|