blob: 28c757948a8bc0eb69640b8755611e245e4cdefd [file] [log] [blame]
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +05301# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
2# License: GNU General Public License v3. See license.txt
3
4from __future__ import unicode_literals
5import frappe
6from frappe.utils import flt, cstr, cint
7from frappe import _
8
9from erpnext.stock.doctype.item.item import get_last_purchase_details
10from erpnext.stock.doctype.item.item import validate_end_of_life
11
12def update_last_purchase_rate(doc, is_submit):
13 """updates last_purchase_rate in item table for each item"""
14
15 import frappe.utils
16 this_purchase_date = frappe.utils.getdate(doc.get('posting_date') or doc.get('transaction_date'))
17
18 for d in doc.get("items"):
19 # get last purchase details
20 last_purchase_details = get_last_purchase_details(d.item_code, doc.name)
21
22 # compare last purchase date and this transaction's date
23 last_purchase_rate = None
24 if last_purchase_details and \
25 (last_purchase_details.purchase_date > this_purchase_date):
26 last_purchase_rate = last_purchase_details['base_rate']
27 elif is_submit == 1:
28 # even if this transaction is the latest one, it should be submitted
29 # for it to be considered for latest purchase rate
30 if flt(d.conversion_factor):
31 last_purchase_rate = flt(d.base_rate) / flt(d.conversion_factor)
32 else:
33 frappe.throw(_("UOM Conversion factor is required in row {0}").format(d.idx))
34
35 # update last purchsae rate
36 if last_purchase_rate:
37 frappe.db.sql("""update `tabItem` set last_purchase_rate = %s where name = %s""",
38 (flt(last_purchase_rate), d.item_code))
39
40def validate_for_items(doc):
41 items = []
42 for d in doc.get("items"):
43 if not d.qty:
44 if doc.doctype == "Purchase Receipt" and d.rejected_qty:
45 continue
46 frappe.throw(_("Please enter quantity for Item {0}").format(d.item_code))
47
48 # update with latest quantities
49 bin = frappe.db.sql("""select projected_qty from `tabBin` where
50 item_code = %s and warehouse = %s""", (d.item_code, d.warehouse), as_dict=1)
51
52 f_lst ={'projected_qty': bin and flt(bin[0]['projected_qty']) or 0, 'ordered_qty': 0, 'received_qty' : 0}
53 if d.doctype in ('Purchase Receipt Item', 'Purchase Invoice Item'):
54 f_lst.pop('received_qty')
55 for x in f_lst :
56 if d.meta.get_field(x):
57 d.set(x, f_lst[x])
58
59 item = frappe.db.sql("""select is_stock_item,
60 is_sub_contracted_item, end_of_life, disabled from `tabItem` where name=%s""",
61 d.item_code, as_dict=1)[0]
62
63 validate_end_of_life(d.item_code, item.end_of_life, item.disabled)
64
65 # validate stock item
66 if item.is_stock_item==1 and d.qty and not d.warehouse and not d.delivered_by_supplier:
67 frappe.throw(_("Warehouse is mandatory for stock Item {0} in row {1}").format(d.item_code, d.idx))
68
69 items.append(cstr(d.item_code))
70
71 if items and len(items) != len(set(items)) and \
72 not cint(frappe.db.get_single_value("Buying Settings", "allow_multiple_items") or 0):
73 frappe.throw(_("Same item cannot be entered multiple times."))
74
75def check_for_closed_status(doctype, docname):
76 status = frappe.db.get_value(doctype, docname, "status")
77
78 if status == "Closed":
79 frappe.throw(_("{0} {1} status is {2}").format(doctype, docname, status), frappe.InvalidStatusError)
80