blob: e904af0dce3bb13540041a7f113ecb86a40c5624 [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"):
47 if not d.qty:
48 if doc.doctype == "Purchase Receipt" and d.rejected_qty:
49 continue
50 frappe.throw(_("Please enter quantity for Item {0}").format(d.item_code))
51
marination982a2462022-03-31 11:47:20 +053052 set_stock_levels(row=d) # update with latest quantities
53 item = validate_item_and_get_basic_data(row=d)
54 validate_stock_item_warehouse(row=d, item=item)
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +053055 validate_end_of_life(d.item_code, item.end_of_life, item.disabled)
56
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +053057 items.append(cstr(d.item_code))
58
Ankush Menat494bd9e2022-03-28 18:52:46 +053059 if (
60 items
61 and len(items) != len(set(items))
62 and not cint(frappe.db.get_single_value("Buying Settings", "allow_multiple_items") or 0)
63 ):
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +053064 frappe.throw(_("Same item cannot be entered multiple times."))
65
Ankush Menat494bd9e2022-03-28 18:52:46 +053066
marination982a2462022-03-31 11:47:20 +053067def set_stock_levels(row) -> None:
68 projected_qty = frappe.db.get_value(
69 "Bin",
70 {
71 "item_code": row.item_code,
72 "warehouse": row.warehouse,
73 },
74 "projected_qty",
75 )
76
77 qty_data = {
78 "projected_qty": flt(projected_qty),
79 "ordered_qty": 0,
80 "received_qty": 0,
81 }
82 if row.doctype in ("Purchase Receipt Item", "Purchase Invoice Item"):
83 qty_data.pop("received_qty")
84
85 for field in qty_data:
86 if row.meta.get_field(field):
87 row.set(field, qty_data[field])
88
89
90def validate_item_and_get_basic_data(row) -> Dict:
91 item = frappe.db.get_values(
92 "Item",
93 filters={"name": row.item_code},
94 fieldname=["is_stock_item", "is_sub_contracted_item", "end_of_life", "disabled"],
95 as_dict=1,
96 )
97 if not item:
98 frappe.throw(_("Row #{0}: Item {1} does not exist").format(row.idx, frappe.bold(row.item_code)))
99
100 return item[0]
101
102
103def validate_stock_item_warehouse(row, item) -> None:
104 if (
105 item.is_stock_item == 1
106 and row.qty
107 and not row.warehouse
108 and not row.get("delivered_by_supplier")
109 ):
110 frappe.throw(
111 _("Row #{1}: Warehouse is mandatory for stock Item {0}").format(
112 frappe.bold(row.item_code), row.idx
113 )
114 )
115
116
117def check_on_hold_or_closed_status(doctype, docname) -> None:
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +0530118 status = frappe.db.get_value(doctype, docname, "status")
119
Mangesh-Khairnara1afd782019-03-11 17:15:05 +0530120 if status in ("Closed", "On Hold"):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530121 frappe.throw(
122 _("{0} {1} status is {2}").format(doctype, docname, status), frappe.InvalidStatusError
123 )
124
Rushabh Mehtacc8b2b22017-03-31 12:44:29 +0530125
mbauskarc482aed2017-05-02 12:53:12 +0530126@frappe.whitelist()
127def get_linked_material_requests(items):
128 items = json.loads(items)
129 mr_list = []
130 for item in items:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530131 material_request = frappe.db.sql(
132 """SELECT distinct mr.name AS mr_name,
Deepesh Garg8b0302b2019-08-05 10:14:19 +0530133 (mr_item.qty - mr_item.ordered_qty) AS qty,
mbauskarc482aed2017-05-02 12:53:12 +0530134 mr_item.item_code AS item_code,
Deepesh Garg8b0302b2019-08-05 10:14:19 +0530135 mr_item.name AS mr_item
mbauskarc482aed2017-05-02 12:53:12 +0530136 FROM `tabMaterial Request` mr, `tabMaterial Request Item` mr_item
137 WHERE mr.name = mr_item.parent
Deepesh Garg8b0302b2019-08-05 10:14:19 +0530138 AND mr_item.item_code = %(item)s
mbauskarc482aed2017-05-02 12:53:12 +0530139 AND mr.material_request_type = 'Purchase'
140 AND mr.per_ordered < 99.99
141 AND mr.docstatus = 1
142 AND mr.status != 'Stopped'
Ankush Menat494bd9e2022-03-28 18:52:46 +0530143 ORDER BY mr_item.item_code ASC""",
144 {"item": item},
145 as_dict=1,
146 )
mbauskarc482aed2017-05-02 12:53:12 +0530147 if material_request:
148 mr_list.append(material_request)
Deepesh Garg8b0302b2019-08-05 10:14:19 +0530149
mbauskarc482aed2017-05-02 12:53:12 +0530150 return mr_list