blob: 61e5e6a1c7bc06ab5421cfc6c9b5882511745840 [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
Chillar Anand915b3432021-09-02 16:44:59 +05304
mbauskarc482aed2017-05-02 12:53:12 +05305import json
marination982a2462022-03-31 11:47:20 +05306from typing import Dict
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +05307
Chillar Anand915b3432021-09-02 16:44:59 +05308import frappe
9from frappe import _
marination982a2462022-03-31 11:47:20 +053010from frappe.utils import cint, cstr, flt, getdate
Chillar Anand915b3432021-09-02 16:44:59 +053011
12from erpnext.stock.doctype.item.item import get_last_purchase_details, validate_end_of_life
13
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +053014
marination982a2462022-03-31 11:47:20 +053015def update_last_purchase_rate(doc, is_submit) -> None:
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +053016 """updates last_purchase_rate in item table for each item"""
Ankush Menat494bd9e2022-03-28 18:52:46 +053017
marination982a2462022-03-31 11:47:20 +053018 this_purchase_date = getdate(doc.get("posting_date") or doc.get("transaction_date"))
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +053019
20 for d in doc.get("items"):
21 # get last purchase details
22 last_purchase_details = get_last_purchase_details(d.item_code, doc.name)
23
24 # compare last purchase date and this transaction's date
25 last_purchase_rate = None
Ankush Menat494bd9e2022-03-28 18:52:46 +053026 if last_purchase_details and (
27 doc.get("docstatus") == 2 or last_purchase_details.purchase_date > this_purchase_date
28 ):
29 last_purchase_rate = last_purchase_details["base_net_rate"]
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +053030 elif is_submit == 1:
31 # even if this transaction is the latest one, it should be submitted
32 # for it to be considered for latest purchase rate
33 if flt(d.conversion_factor):
Saqib290253f2019-11-22 12:12:29 +053034 last_purchase_rate = flt(d.base_net_rate) / flt(d.conversion_factor)
Deepesh Garg8b0302b2019-08-05 10:14:19 +053035 # Check if item code is present
36 # Conversion factor should not be mandatory for non itemized items
37 elif d.item_code:
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +053038 frappe.throw(_("UOM Conversion factor is required in row {0}").format(d.idx))
39
40 # update last purchsae rate
Ankush Menat494bd9e2022-03-28 18:52:46 +053041 frappe.db.set_value("Item", d.item_code, "last_purchase_rate", flt(last_purchase_rate))
Mohammad Hasnain Mohsin Rajan00981202021-01-14 19:23:18 +053042
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +053043
marination982a2462022-03-31 11:47:20 +053044def validate_for_items(doc) -> None:
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +053045 items = []
46 for d in doc.get("items"):
marination982a2462022-03-31 11:47:20 +053047 set_stock_levels(row=d) # update with latest quantities
48 item = validate_item_and_get_basic_data(row=d)
49 validate_stock_item_warehouse(row=d, item=item)
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +053050 validate_end_of_life(d.item_code, item.end_of_life, item.disabled)
51
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +053052 items.append(cstr(d.item_code))
53
Ankush Menat494bd9e2022-03-28 18:52:46 +053054 if (
55 items
56 and len(items) != len(set(items))
57 and not cint(frappe.db.get_single_value("Buying Settings", "allow_multiple_items") or 0)
58 ):
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +053059 frappe.throw(_("Same item cannot be entered multiple times."))
60
Ankush Menat494bd9e2022-03-28 18:52:46 +053061
marination982a2462022-03-31 11:47:20 +053062def set_stock_levels(row) -> None:
63 projected_qty = frappe.db.get_value(
64 "Bin",
65 {
66 "item_code": row.item_code,
67 "warehouse": row.warehouse,
68 },
69 "projected_qty",
70 )
71
72 qty_data = {
73 "projected_qty": flt(projected_qty),
74 "ordered_qty": 0,
75 "received_qty": 0,
76 }
77 if row.doctype in ("Purchase Receipt Item", "Purchase Invoice Item"):
78 qty_data.pop("received_qty")
79
80 for field in qty_data:
81 if row.meta.get_field(field):
82 row.set(field, qty_data[field])
83
84
85def validate_item_and_get_basic_data(row) -> Dict:
86 item = frappe.db.get_values(
87 "Item",
88 filters={"name": row.item_code},
89 fieldname=["is_stock_item", "is_sub_contracted_item", "end_of_life", "disabled"],
90 as_dict=1,
91 )
92 if not item:
93 frappe.throw(_("Row #{0}: Item {1} does not exist").format(row.idx, frappe.bold(row.item_code)))
94
95 return item[0]
96
97
98def validate_stock_item_warehouse(row, item) -> None:
99 if (
100 item.is_stock_item == 1
101 and row.qty
102 and not row.warehouse
103 and not row.get("delivered_by_supplier")
104 ):
105 frappe.throw(
106 _("Row #{1}: Warehouse is mandatory for stock Item {0}").format(
107 frappe.bold(row.item_code), row.idx
108 )
109 )
110
111
112def check_on_hold_or_closed_status(doctype, docname) -> None:
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +0530113 status = frappe.db.get_value(doctype, docname, "status")
114
Mangesh-Khairnara1afd782019-03-11 17:15:05 +0530115 if status in ("Closed", "On Hold"):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530116 frappe.throw(
117 _("{0} {1} status is {2}").format(doctype, docname, status), frappe.InvalidStatusError
118 )
119
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +0530120
mbauskarc482aed2017-05-02 12:53:12 +0530121@frappe.whitelist()
122def get_linked_material_requests(items):
123 items = json.loads(items)
124 mr_list = []
125 for item in items:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530126 material_request = frappe.db.sql(
127 """SELECT distinct mr.name AS mr_name,
Deepesh Garg8b0302b2019-08-05 10:14:19 +0530128 (mr_item.qty - mr_item.ordered_qty) AS qty,
mbauskarc482aed2017-05-02 12:53:12 +0530129 mr_item.item_code AS item_code,
Deepesh Garg8b0302b2019-08-05 10:14:19 +0530130 mr_item.name AS mr_item
mbauskarc482aed2017-05-02 12:53:12 +0530131 FROM `tabMaterial Request` mr, `tabMaterial Request Item` mr_item
132 WHERE mr.name = mr_item.parent
Deepesh Garg8b0302b2019-08-05 10:14:19 +0530133 AND mr_item.item_code = %(item)s
mbauskarc482aed2017-05-02 12:53:12 +0530134 AND mr.material_request_type = 'Purchase'
135 AND mr.per_ordered < 99.99
136 AND mr.docstatus = 1
137 AND mr.status != 'Stopped'
Ankush Menat494bd9e2022-03-28 18:52:46 +0530138 ORDER BY mr_item.item_code ASC""",
139 {"item": item},
140 as_dict=1,
141 )
mbauskarc482aed2017-05-02 12:53:12 +0530142 if material_request:
143 mr_list.append(material_request)
Deepesh Garg8b0302b2019-08-05 10:14:19 +0530144
mbauskarc482aed2017-05-02 12:53:12 +0530145 return mr_list