blob: 61f081c26f98e2cb52eccdd2bf4e605af3dae6cd [file] [log] [blame]
Rushabh Mehtadc8067e2016-06-29 18:38:32 +05301# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
2# License: GNU General Public License v3. See license.txt
3
Rushabh Mehtadc8067e2016-06-29 18:38:32 +05304
Chillar Anand915b3432021-09-02 16:44:59 +05305import json
6import random
7
8import frappe
Rushabh Mehtadc8067e2016-06-29 18:38:32 +05309from frappe.desk import query_report
Chillar Anand915b3432021-09-02 16:44:59 +053010from frappe.utils.make_random import get_random, how_many
11
12import erpnext
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053013from erpnext.accounts.party import get_party_account_currency
Chillar Anand915b3432021-09-02 16:44:59 +053014from erpnext.buying.doctype.request_for_quotation.request_for_quotation import (
15 make_supplier_quotation_from_rfq,
16)
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053017from erpnext.exceptions import InvalidCurrency
Chillar Anand915b3432021-09-02 16:44:59 +053018from erpnext.setup.utils import get_exchange_rate
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053019from erpnext.stock.doctype.material_request.material_request import make_request_for_quotation
Chillar Anand915b3432021-09-02 16:44:59 +053020
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053021
Rushabh Mehtaea0ff232016-07-07 14:02:26 +053022def work():
23 frappe.set_user(frappe.db.get_global('demo_purchase_user'))
24
Zlash65dcf74012018-09-27 18:51:18 +053025 if random.random() < 0.6:
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053026 report = "Items To Be Requested"
Rushabh Mehtaea0ff232016-07-07 14:02:26 +053027 for row in query_report.run(report)["result"][:random.randint(1, 5)]:
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053028 item_code, qty = row[0], abs(row[-1])
29
30 mr = make_material_request(item_code, qty)
31
Zlash65dcf74012018-09-27 18:51:18 +053032 if random.random() < 0.6:
Rushabh Mehtaea0ff232016-07-07 14:02:26 +053033 for mr in frappe.get_all('Material Request',
34 filters={'material_request_type': 'Purchase', 'status': 'Open'},
35 limit=random.randint(1,6)):
36 if not frappe.get_all('Request for Quotation',
37 filters={'material_request': mr.name}, limit=1):
38 rfq = make_request_for_quotation(mr.name)
39 rfq.transaction_date = frappe.flags.current_date
40 add_suppliers(rfq)
41 rfq.save()
42 rfq.submit()
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053043
Rushabh Mehtaea0ff232016-07-07 14:02:26 +053044 # Make suppier quotation from RFQ against each supplier.
Zlash65dcf74012018-09-27 18:51:18 +053045 if random.random() < 0.6:
Rushabh Mehtaea0ff232016-07-07 14:02:26 +053046 for rfq in frappe.get_all('Request for Quotation',
47 filters={'status': 'Open'}, limit=random.randint(1, 6)):
48 if not frappe.get_all('Supplier Quotation',
49 filters={'request_for_quotation': rfq.name}, limit=1):
50 rfq = frappe.get_doc('Request for Quotation', rfq.name)
51
52 for supplier in rfq.suppliers:
Maricac5e139a2020-10-13 16:53:10 +053053 supplier_quotation = make_supplier_quotation_from_rfq(rfq.name, for_supplier=supplier.supplier)
Rushabh Mehtaea0ff232016-07-07 14:02:26 +053054 supplier_quotation.save()
55 supplier_quotation.submit()
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053056
57 # get supplier details
58 supplier = get_random("Supplier")
59
Zlash65fd9c4512018-10-10 14:27:07 +053060 company_currency = frappe.get_cached_value('Company', erpnext.get_default_company(), "default_currency")
61 party_account_currency = get_party_account_currency("Supplier", supplier, erpnext.get_default_company())
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053062 if company_currency == party_account_currency:
63 exchange_rate = 1
64 else:
Shreya3f778522018-05-15 16:59:20 +053065 exchange_rate = get_exchange_rate(party_account_currency, company_currency, args="for_buying")
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053066
67 # make supplier quotations
Zlash65dcf74012018-09-27 18:51:18 +053068 if random.random() < 0.5:
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053069 from erpnext.stock.doctype.material_request.material_request import make_supplier_quotation
70
71 report = "Material Requests for which Supplier Quotations are not created"
Rushabh Mehtacca33b22016-07-08 18:24:46 +053072 for row in query_report.run(report)["result"][:random.randint(1, 3)]:
Aditya Hase41d189a2019-01-07 14:25:58 +053073 if row[0] != "Total":
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053074 sq = frappe.get_doc(make_supplier_quotation(row[0]))
Rushabh Mehtaea0ff232016-07-07 14:02:26 +053075 sq.transaction_date = frappe.flags.current_date
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053076 sq.supplier = supplier
77 sq.currency = party_account_currency or company_currency
78 sq.conversion_rate = exchange_rate
79 sq.insert()
80 sq.submit()
81 frappe.db.commit()
82
83 # make purchase orders
Rushabh Mehta99c814b2016-12-19 11:51:02 +053084 if random.random() < 0.5:
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053085 from erpnext.stock.doctype.material_request.material_request import make_purchase_order
86 report = "Requested Items To Be Ordered"
87 for row in query_report.run(report)["result"][:how_many("Purchase Order")]:
Aditya Hase41d189a2019-01-07 14:25:58 +053088 if row[0] != "Total":
Zlash658b1133c2018-10-01 12:55:07 +053089 try:
90 po = frappe.get_doc(make_purchase_order(row[0]))
91 po.supplier = supplier
92 po.currency = party_account_currency or company_currency
93 po.conversion_rate = exchange_rate
94 po.transaction_date = frappe.flags.current_date
95 po.insert()
96 po.submit()
97 except Exception:
98 pass
99 else:
100 frappe.db.commit()
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530101
Zlash65dcf74012018-09-27 18:51:18 +0530102 if random.random() < 0.5:
Rushabh Mehtaea0ff232016-07-07 14:02:26 +0530103 make_subcontract()
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530104
105def make_material_request(item_code, qty):
106 mr = frappe.new_doc("Material Request")
107
Rushabh Mehtacca33b22016-07-08 18:24:46 +0530108 variant_of = frappe.db.get_value('Item', item_code, 'variant_of') or item_code
109
110 if frappe.db.get_value('BOM', {'item': variant_of, 'is_default': 1, 'is_active': 1}):
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530111 mr.material_request_type = 'Manufacture'
112 else:
113 mr.material_request_type = "Purchase"
114
115 mr.transaction_date = frappe.flags.current_date
Sunny0d91d3f2017-09-26 10:56:31 +0800116 mr.schedule_date = frappe.utils.add_days(mr.transaction_date, 7)
Rushabh Mehta99c814b2016-12-19 11:51:02 +0530117
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530118 mr.append("items", {
119 "doctype": "Material Request Item",
120 "schedule_date": frappe.utils.add_days(mr.transaction_date, 7),
121 "item_code": item_code,
Neil Trini Lasrado06724592016-08-22 12:57:09 +0530122 "qty": qty
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530123 })
124 mr.insert()
125 mr.submit()
126 return mr
127
128def add_suppliers(rfq):
Achilles Rasquinha96698c92018-02-28 16:12:51 +0530129 for i in range(2):
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530130 supplier = get_random("Supplier")
131 if supplier not in [d.supplier for d in rfq.get('suppliers')]:
132 rfq.append("suppliers", { "supplier": supplier })
133
134def make_subcontract():
Zlash65796bffb2018-09-26 12:27:09 +0530135 from erpnext.buying.doctype.purchase_order.purchase_order import make_rm_stock_entry
Rushabh Mehta92d1b8c2016-07-14 15:46:12 +0530136 item_code = get_random("Item", {"is_sub_contracted_item": 1})
Neil Trini Lasrado06724592016-08-22 12:57:09 +0530137 if item_code:
138 # make sub-contract PO
139 po = frappe.new_doc("Purchase Order")
140 po.is_subcontracted = "Yes"
141 po.supplier = get_random("Supplier")
Zlash65dcf74012018-09-27 18:51:18 +0530142 po.transaction_date = frappe.flags.current_date # added
Sunny3a22fd12017-09-26 12:21:41 +0800143 po.schedule_date = frappe.utils.add_days(frappe.flags.current_date, 7)
Rushabh Mehta92d1b8c2016-07-14 15:46:12 +0530144
Neil Trini Lasrado06724592016-08-22 12:57:09 +0530145 item_code = get_random("Item", {"is_sub_contracted_item": 1})
Rushabh Mehta99c814b2016-12-19 11:51:02 +0530146
Neil Trini Lasrado06724592016-08-22 12:57:09 +0530147 po.append("items", {
148 "item_code": item_code,
149 "schedule_date": frappe.utils.add_days(frappe.flags.current_date, 7),
150 "qty": random.randint(10, 30)
151 })
152 po.set_missing_values()
153 try:
154 po.insert()
155 except InvalidCurrency:
156 return
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530157
Neil Trini Lasrado06724592016-08-22 12:57:09 +0530158 po.submit()
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530159
Neil Trini Lasrado06724592016-08-22 12:57:09 +0530160 # make material request for
161 make_material_request(po.items[0].item_code, po.items[0].qty)
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530162
Neil Trini Lasrado06724592016-08-22 12:57:09 +0530163 # transfer material for sub-contract
Zlash65523f77e2018-09-28 13:59:09 +0530164 rm_items = get_rm_item(po.items[0], po.supplied_items[0])
165 stock_entry = frappe.get_doc(make_rm_stock_entry(po.name, json.dumps([rm_items])))
Neil Trini Lasrado06724592016-08-22 12:57:09 +0530166 stock_entry.from_warehouse = "Stores - WPL"
167 stock_entry.to_warehouse = "Supplier - WPL"
168 stock_entry.insert()
Zlash65523f77e2018-09-28 13:59:09 +0530169
170def get_rm_item(items, supplied_items):
171 return {
172 "item_code": items.get("item_code"),
173 "rm_item_code": supplied_items.get("rm_item_code"),
174 "item_name": supplied_items.get("rm_item_code"),
Zlash65d080a172018-10-01 10:49:06 +0530175 "qty": supplied_items.get("required_qty") + random.randint(3,10),
Zlash65523f77e2018-09-28 13:59:09 +0530176 "amount": supplied_items.get("amount"),
177 "warehouse": supplied_items.get("reserve_warehouse"),
178 "rate": supplied_items.get("rate"),
179 "stock_uom": supplied_items.get("stock_uom")
180 }