blob: ec32f973dae5482c2787b0cfcb078aafb1a5974a [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
4from __future__ import unicode_literals
5
Chillar Anand915b3432021-09-02 16:44:59 +05306import json
7import random
8
9import frappe
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053010from frappe.desk import query_report
Chillar Anand915b3432021-09-02 16:44:59 +053011from frappe.utils.make_random import get_random, how_many
12
13import erpnext
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053014from erpnext.accounts.party import get_party_account_currency
Chillar Anand915b3432021-09-02 16:44:59 +053015from erpnext.buying.doctype.request_for_quotation.request_for_quotation import (
16 make_supplier_quotation_from_rfq,
17)
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053018from erpnext.exceptions import InvalidCurrency
Chillar Anand915b3432021-09-02 16:44:59 +053019from erpnext.setup.utils import get_exchange_rate
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053020from erpnext.stock.doctype.material_request.material_request import make_request_for_quotation
Chillar Anand915b3432021-09-02 16:44:59 +053021
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053022
Rushabh Mehtaea0ff232016-07-07 14:02:26 +053023def work():
24 frappe.set_user(frappe.db.get_global('demo_purchase_user'))
25
Zlash65dcf74012018-09-27 18:51:18 +053026 if random.random() < 0.6:
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053027 report = "Items To Be Requested"
Rushabh Mehtaea0ff232016-07-07 14:02:26 +053028 for row in query_report.run(report)["result"][:random.randint(1, 5)]:
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053029 item_code, qty = row[0], abs(row[-1])
30
31 mr = make_material_request(item_code, qty)
32
Zlash65dcf74012018-09-27 18:51:18 +053033 if random.random() < 0.6:
Rushabh Mehtaea0ff232016-07-07 14:02:26 +053034 for mr in frappe.get_all('Material Request',
35 filters={'material_request_type': 'Purchase', 'status': 'Open'},
36 limit=random.randint(1,6)):
37 if not frappe.get_all('Request for Quotation',
38 filters={'material_request': mr.name}, limit=1):
39 rfq = make_request_for_quotation(mr.name)
40 rfq.transaction_date = frappe.flags.current_date
41 add_suppliers(rfq)
42 rfq.save()
43 rfq.submit()
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053044
Rushabh Mehtaea0ff232016-07-07 14:02:26 +053045 # Make suppier quotation from RFQ against each supplier.
Zlash65dcf74012018-09-27 18:51:18 +053046 if random.random() < 0.6:
Rushabh Mehtaea0ff232016-07-07 14:02:26 +053047 for rfq in frappe.get_all('Request for Quotation',
48 filters={'status': 'Open'}, limit=random.randint(1, 6)):
49 if not frappe.get_all('Supplier Quotation',
50 filters={'request_for_quotation': rfq.name}, limit=1):
51 rfq = frappe.get_doc('Request for Quotation', rfq.name)
52
53 for supplier in rfq.suppliers:
Maricac5e139a2020-10-13 16:53:10 +053054 supplier_quotation = make_supplier_quotation_from_rfq(rfq.name, for_supplier=supplier.supplier)
Rushabh Mehtaea0ff232016-07-07 14:02:26 +053055 supplier_quotation.save()
56 supplier_quotation.submit()
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053057
58 # get supplier details
59 supplier = get_random("Supplier")
60
Zlash65fd9c4512018-10-10 14:27:07 +053061 company_currency = frappe.get_cached_value('Company', erpnext.get_default_company(), "default_currency")
62 party_account_currency = get_party_account_currency("Supplier", supplier, erpnext.get_default_company())
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053063 if company_currency == party_account_currency:
64 exchange_rate = 1
65 else:
Shreya3f778522018-05-15 16:59:20 +053066 exchange_rate = get_exchange_rate(party_account_currency, company_currency, args="for_buying")
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053067
68 # make supplier quotations
Zlash65dcf74012018-09-27 18:51:18 +053069 if random.random() < 0.5:
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053070 from erpnext.stock.doctype.material_request.material_request import make_supplier_quotation
71
72 report = "Material Requests for which Supplier Quotations are not created"
Rushabh Mehtacca33b22016-07-08 18:24:46 +053073 for row in query_report.run(report)["result"][:random.randint(1, 3)]:
Aditya Hase41d189a2019-01-07 14:25:58 +053074 if row[0] != "Total":
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053075 sq = frappe.get_doc(make_supplier_quotation(row[0]))
Rushabh Mehtaea0ff232016-07-07 14:02:26 +053076 sq.transaction_date = frappe.flags.current_date
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053077 sq.supplier = supplier
78 sq.currency = party_account_currency or company_currency
79 sq.conversion_rate = exchange_rate
80 sq.insert()
81 sq.submit()
82 frappe.db.commit()
83
84 # make purchase orders
Rushabh Mehta99c814b2016-12-19 11:51:02 +053085 if random.random() < 0.5:
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053086 from erpnext.stock.doctype.material_request.material_request import make_purchase_order
87 report = "Requested Items To Be Ordered"
88 for row in query_report.run(report)["result"][:how_many("Purchase Order")]:
Aditya Hase41d189a2019-01-07 14:25:58 +053089 if row[0] != "Total":
Zlash658b1133c2018-10-01 12:55:07 +053090 try:
91 po = frappe.get_doc(make_purchase_order(row[0]))
92 po.supplier = supplier
93 po.currency = party_account_currency or company_currency
94 po.conversion_rate = exchange_rate
95 po.transaction_date = frappe.flags.current_date
96 po.insert()
97 po.submit()
98 except Exception:
99 pass
100 else:
101 frappe.db.commit()
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530102
Zlash65dcf74012018-09-27 18:51:18 +0530103 if random.random() < 0.5:
Rushabh Mehtaea0ff232016-07-07 14:02:26 +0530104 make_subcontract()
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530105
106def make_material_request(item_code, qty):
107 mr = frappe.new_doc("Material Request")
108
Rushabh Mehtacca33b22016-07-08 18:24:46 +0530109 variant_of = frappe.db.get_value('Item', item_code, 'variant_of') or item_code
110
111 if frappe.db.get_value('BOM', {'item': variant_of, 'is_default': 1, 'is_active': 1}):
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530112 mr.material_request_type = 'Manufacture'
113 else:
114 mr.material_request_type = "Purchase"
115
116 mr.transaction_date = frappe.flags.current_date
Sunny0d91d3f2017-09-26 10:56:31 +0800117 mr.schedule_date = frappe.utils.add_days(mr.transaction_date, 7)
Rushabh Mehta99c814b2016-12-19 11:51:02 +0530118
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530119 mr.append("items", {
120 "doctype": "Material Request Item",
121 "schedule_date": frappe.utils.add_days(mr.transaction_date, 7),
122 "item_code": item_code,
Neil Trini Lasrado06724592016-08-22 12:57:09 +0530123 "qty": qty
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530124 })
125 mr.insert()
126 mr.submit()
127 return mr
128
129def add_suppliers(rfq):
Achilles Rasquinha96698c92018-02-28 16:12:51 +0530130 for i in range(2):
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530131 supplier = get_random("Supplier")
132 if supplier not in [d.supplier for d in rfq.get('suppliers')]:
133 rfq.append("suppliers", { "supplier": supplier })
134
135def make_subcontract():
Zlash65796bffb2018-09-26 12:27:09 +0530136 from erpnext.buying.doctype.purchase_order.purchase_order import make_rm_stock_entry
Rushabh Mehta92d1b8c2016-07-14 15:46:12 +0530137 item_code = get_random("Item", {"is_sub_contracted_item": 1})
Neil Trini Lasrado06724592016-08-22 12:57:09 +0530138 if item_code:
139 # make sub-contract PO
140 po = frappe.new_doc("Purchase Order")
141 po.is_subcontracted = "Yes"
142 po.supplier = get_random("Supplier")
Zlash65dcf74012018-09-27 18:51:18 +0530143 po.transaction_date = frappe.flags.current_date # added
Sunny3a22fd12017-09-26 12:21:41 +0800144 po.schedule_date = frappe.utils.add_days(frappe.flags.current_date, 7)
Rushabh Mehta92d1b8c2016-07-14 15:46:12 +0530145
Neil Trini Lasrado06724592016-08-22 12:57:09 +0530146 item_code = get_random("Item", {"is_sub_contracted_item": 1})
Rushabh Mehta99c814b2016-12-19 11:51:02 +0530147
Neil Trini Lasrado06724592016-08-22 12:57:09 +0530148 po.append("items", {
149 "item_code": item_code,
150 "schedule_date": frappe.utils.add_days(frappe.flags.current_date, 7),
151 "qty": random.randint(10, 30)
152 })
153 po.set_missing_values()
154 try:
155 po.insert()
156 except InvalidCurrency:
157 return
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530158
Neil Trini Lasrado06724592016-08-22 12:57:09 +0530159 po.submit()
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530160
Neil Trini Lasrado06724592016-08-22 12:57:09 +0530161 # make material request for
162 make_material_request(po.items[0].item_code, po.items[0].qty)
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530163
Neil Trini Lasrado06724592016-08-22 12:57:09 +0530164 # transfer material for sub-contract
Zlash65523f77e2018-09-28 13:59:09 +0530165 rm_items = get_rm_item(po.items[0], po.supplied_items[0])
166 stock_entry = frappe.get_doc(make_rm_stock_entry(po.name, json.dumps([rm_items])))
Neil Trini Lasrado06724592016-08-22 12:57:09 +0530167 stock_entry.from_warehouse = "Stores - WPL"
168 stock_entry.to_warehouse = "Supplier - WPL"
169 stock_entry.insert()
Zlash65523f77e2018-09-28 13:59:09 +0530170
171def get_rm_item(items, supplied_items):
172 return {
173 "item_code": items.get("item_code"),
174 "rm_item_code": supplied_items.get("rm_item_code"),
175 "item_name": supplied_items.get("rm_item_code"),
Zlash65d080a172018-10-01 10:49:06 +0530176 "qty": supplied_items.get("required_qty") + random.randint(3,10),
Zlash65523f77e2018-09-28 13:59:09 +0530177 "amount": supplied_items.get("amount"),
178 "warehouse": supplied_items.get("reserve_warehouse"),
179 "rate": supplied_items.get("rate"),
180 "stock_uom": supplied_items.get("stock_uom")
181 }