blob: 69930448dc957893d6970e2081785e84264c9f25 [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
Zlash65fd9c4512018-10-10 14:27:07 +05306import frappe, random, json, erpnext
Rushabh Mehtaea0ff232016-07-07 14:02:26 +05307from frappe.utils.make_random import how_many, get_random
Rushabh Mehtadc8067e2016-06-29 18:38:32 +05308from frappe.desk import query_report
9from erpnext.setup.utils import get_exchange_rate
10from erpnext.accounts.party import get_party_account_currency
11from erpnext.exceptions import InvalidCurrency
12from erpnext.stock.doctype.material_request.material_request import make_request_for_quotation
13from erpnext.buying.doctype.request_for_quotation.request_for_quotation import \
14 make_supplier_quotation as make_quotation_from_rfq
15
Rushabh Mehtaea0ff232016-07-07 14:02:26 +053016def work():
17 frappe.set_user(frappe.db.get_global('demo_purchase_user'))
18
Zlash65dcf74012018-09-27 18:51:18 +053019 if random.random() < 0.6:
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053020 report = "Items To Be Requested"
Rushabh Mehtaea0ff232016-07-07 14:02:26 +053021 for row in query_report.run(report)["result"][:random.randint(1, 5)]:
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053022 item_code, qty = row[0], abs(row[-1])
23
24 mr = make_material_request(item_code, qty)
25
Zlash65dcf74012018-09-27 18:51:18 +053026 if random.random() < 0.6:
Rushabh Mehtaea0ff232016-07-07 14:02:26 +053027 for mr in frappe.get_all('Material Request',
28 filters={'material_request_type': 'Purchase', 'status': 'Open'},
29 limit=random.randint(1,6)):
30 if not frappe.get_all('Request for Quotation',
31 filters={'material_request': mr.name}, limit=1):
32 rfq = make_request_for_quotation(mr.name)
33 rfq.transaction_date = frappe.flags.current_date
34 add_suppliers(rfq)
35 rfq.save()
36 rfq.submit()
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053037
Rushabh Mehtaea0ff232016-07-07 14:02:26 +053038 # Make suppier quotation from RFQ against each supplier.
Zlash65dcf74012018-09-27 18:51:18 +053039 if random.random() < 0.6:
Rushabh Mehtaea0ff232016-07-07 14:02:26 +053040 for rfq in frappe.get_all('Request for Quotation',
41 filters={'status': 'Open'}, limit=random.randint(1, 6)):
42 if not frappe.get_all('Supplier Quotation',
43 filters={'request_for_quotation': rfq.name}, limit=1):
44 rfq = frappe.get_doc('Request for Quotation', rfq.name)
45
46 for supplier in rfq.suppliers:
47 supplier_quotation = make_quotation_from_rfq(rfq.name, supplier.supplier)
48 supplier_quotation.save()
49 supplier_quotation.submit()
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053050
51 # get supplier details
52 supplier = get_random("Supplier")
53
Zlash65fd9c4512018-10-10 14:27:07 +053054 company_currency = frappe.get_cached_value('Company', erpnext.get_default_company(), "default_currency")
55 party_account_currency = get_party_account_currency("Supplier", supplier, erpnext.get_default_company())
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053056 if company_currency == party_account_currency:
57 exchange_rate = 1
58 else:
Shreya3f778522018-05-15 16:59:20 +053059 exchange_rate = get_exchange_rate(party_account_currency, company_currency, args="for_buying")
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053060
61 # make supplier quotations
Zlash65dcf74012018-09-27 18:51:18 +053062 if random.random() < 0.5:
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053063 from erpnext.stock.doctype.material_request.material_request import make_supplier_quotation
64
65 report = "Material Requests for which Supplier Quotations are not created"
Rushabh Mehtacca33b22016-07-08 18:24:46 +053066 for row in query_report.run(report)["result"][:random.randint(1, 3)]:
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053067 if row[0] != "'Total'":
68 sq = frappe.get_doc(make_supplier_quotation(row[0]))
Rushabh Mehtaea0ff232016-07-07 14:02:26 +053069 sq.transaction_date = frappe.flags.current_date
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053070 sq.supplier = supplier
71 sq.currency = party_account_currency or company_currency
72 sq.conversion_rate = exchange_rate
73 sq.insert()
74 sq.submit()
75 frappe.db.commit()
76
77 # make purchase orders
Rushabh Mehta99c814b2016-12-19 11:51:02 +053078 if random.random() < 0.5:
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053079 from erpnext.stock.doctype.material_request.material_request import make_purchase_order
80 report = "Requested Items To Be Ordered"
81 for row in query_report.run(report)["result"][:how_many("Purchase Order")]:
82 if row[0] != "'Total'":
Zlash658b1133c2018-10-01 12:55:07 +053083 try:
84 po = frappe.get_doc(make_purchase_order(row[0]))
85 po.supplier = supplier
86 po.currency = party_account_currency or company_currency
87 po.conversion_rate = exchange_rate
88 po.transaction_date = frappe.flags.current_date
89 po.insert()
90 po.submit()
91 except Exception:
92 pass
93 else:
94 frappe.db.commit()
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053095
Zlash65dcf74012018-09-27 18:51:18 +053096 if random.random() < 0.5:
Rushabh Mehtaea0ff232016-07-07 14:02:26 +053097 make_subcontract()
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053098
99def make_material_request(item_code, qty):
100 mr = frappe.new_doc("Material Request")
101
Rushabh Mehtacca33b22016-07-08 18:24:46 +0530102 variant_of = frappe.db.get_value('Item', item_code, 'variant_of') or item_code
103
104 if frappe.db.get_value('BOM', {'item': variant_of, 'is_default': 1, 'is_active': 1}):
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530105 mr.material_request_type = 'Manufacture'
106 else:
107 mr.material_request_type = "Purchase"
108
109 mr.transaction_date = frappe.flags.current_date
Sunny0d91d3f2017-09-26 10:56:31 +0800110 mr.schedule_date = frappe.utils.add_days(mr.transaction_date, 7)
Rushabh Mehta99c814b2016-12-19 11:51:02 +0530111
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530112 mr.append("items", {
113 "doctype": "Material Request Item",
114 "schedule_date": frappe.utils.add_days(mr.transaction_date, 7),
115 "item_code": item_code,
Neil Trini Lasrado06724592016-08-22 12:57:09 +0530116 "qty": qty
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530117 })
118 mr.insert()
119 mr.submit()
120 return mr
121
122def add_suppliers(rfq):
Achilles Rasquinha96698c92018-02-28 16:12:51 +0530123 for i in range(2):
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530124 supplier = get_random("Supplier")
125 if supplier not in [d.supplier for d in rfq.get('suppliers')]:
126 rfq.append("suppliers", { "supplier": supplier })
127
128def make_subcontract():
Zlash65796bffb2018-09-26 12:27:09 +0530129 from erpnext.buying.doctype.purchase_order.purchase_order import make_rm_stock_entry
Rushabh Mehta92d1b8c2016-07-14 15:46:12 +0530130 item_code = get_random("Item", {"is_sub_contracted_item": 1})
Neil Trini Lasrado06724592016-08-22 12:57:09 +0530131 if item_code:
132 # make sub-contract PO
133 po = frappe.new_doc("Purchase Order")
134 po.is_subcontracted = "Yes"
135 po.supplier = get_random("Supplier")
Zlash65dcf74012018-09-27 18:51:18 +0530136 po.transaction_date = frappe.flags.current_date # added
Sunny3a22fd12017-09-26 12:21:41 +0800137 po.schedule_date = frappe.utils.add_days(frappe.flags.current_date, 7)
Rushabh Mehta92d1b8c2016-07-14 15:46:12 +0530138
Neil Trini Lasrado06724592016-08-22 12:57:09 +0530139 item_code = get_random("Item", {"is_sub_contracted_item": 1})
Rushabh Mehta99c814b2016-12-19 11:51:02 +0530140
Neil Trini Lasrado06724592016-08-22 12:57:09 +0530141 po.append("items", {
142 "item_code": item_code,
143 "schedule_date": frappe.utils.add_days(frappe.flags.current_date, 7),
144 "qty": random.randint(10, 30)
145 })
146 po.set_missing_values()
147 try:
148 po.insert()
149 except InvalidCurrency:
150 return
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530151
Neil Trini Lasrado06724592016-08-22 12:57:09 +0530152 po.submit()
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530153
Neil Trini Lasrado06724592016-08-22 12:57:09 +0530154 # make material request for
155 make_material_request(po.items[0].item_code, po.items[0].qty)
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530156
Neil Trini Lasrado06724592016-08-22 12:57:09 +0530157 # transfer material for sub-contract
Zlash65523f77e2018-09-28 13:59:09 +0530158 rm_items = get_rm_item(po.items[0], po.supplied_items[0])
159 stock_entry = frappe.get_doc(make_rm_stock_entry(po.name, json.dumps([rm_items])))
Neil Trini Lasrado06724592016-08-22 12:57:09 +0530160 stock_entry.from_warehouse = "Stores - WPL"
161 stock_entry.to_warehouse = "Supplier - WPL"
162 stock_entry.insert()
Zlash65523f77e2018-09-28 13:59:09 +0530163
164def get_rm_item(items, supplied_items):
165 return {
166 "item_code": items.get("item_code"),
167 "rm_item_code": supplied_items.get("rm_item_code"),
168 "item_name": supplied_items.get("rm_item_code"),
Zlash65d080a172018-10-01 10:49:06 +0530169 "qty": supplied_items.get("required_qty") + random.randint(3,10),
Zlash65523f77e2018-09-28 13:59:09 +0530170 "amount": supplied_items.get("amount"),
171 "warehouse": supplied_items.get("reserve_warehouse"),
172 "rate": supplied_items.get("rate"),
173 "stock_uom": supplied_items.get("stock_uom")
174 }