blob: 960a3169cf36631e190e13882e6da8452c86a0ba [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
6import frappe, random
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
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053019 if random.random() < 0.3:
20 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
26 if random.random() < 0.3:
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.
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053039 if random.random() < 0.3:
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
54 company_currency = frappe.db.get_value("Company", "Wind Power LLC", "default_currency")
55 party_account_currency = get_party_account_currency("Supplier", supplier, "Wind Power LLC")
56 if company_currency == party_account_currency:
57 exchange_rate = 1
58 else:
59 exchange_rate = get_exchange_rate(party_account_currency, company_currency)
60
61 # make supplier quotations
Rushabh Mehtacca33b22016-07-08 18:24:46 +053062 if random.random() < 0.2:
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'":
83 po = frappe.get_doc(make_purchase_order(row[0]))
84 po.supplier = supplier
85 po.currency = party_account_currency or company_currency
86 po.conversion_rate = exchange_rate
Rushabh Mehtaea0ff232016-07-07 14:02:26 +053087 po.transaction_date = frappe.flags.current_date
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053088 po.insert()
89 po.submit()
90 frappe.db.commit()
91
Rushabh Mehtacca33b22016-07-08 18:24:46 +053092 if random.random() < 0.2:
Rushabh Mehtaea0ff232016-07-07 14:02:26 +053093 make_subcontract()
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053094
95def make_material_request(item_code, qty):
96 mr = frappe.new_doc("Material Request")
97
Rushabh Mehtacca33b22016-07-08 18:24:46 +053098 variant_of = frappe.db.get_value('Item', item_code, 'variant_of') or item_code
99
100 if frappe.db.get_value('BOM', {'item': variant_of, 'is_default': 1, 'is_active': 1}):
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530101 mr.material_request_type = 'Manufacture'
102 else:
103 mr.material_request_type = "Purchase"
104
105 mr.transaction_date = frappe.flags.current_date
Rushabh Mehta99c814b2016-12-19 11:51:02 +0530106
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530107 mr.append("items", {
108 "doctype": "Material Request Item",
109 "schedule_date": frappe.utils.add_days(mr.transaction_date, 7),
110 "item_code": item_code,
Neil Trini Lasrado06724592016-08-22 12:57:09 +0530111 "qty": qty
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530112 })
113 mr.insert()
114 mr.submit()
115 return mr
116
117def add_suppliers(rfq):
118 for i in xrange(2):
119 supplier = get_random("Supplier")
120 if supplier not in [d.supplier for d in rfq.get('suppliers')]:
121 rfq.append("suppliers", { "supplier": supplier })
122
123def make_subcontract():
124 from erpnext.buying.doctype.purchase_order.purchase_order import make_stock_entry
Rushabh Mehta92d1b8c2016-07-14 15:46:12 +0530125 item_code = get_random("Item", {"is_sub_contracted_item": 1})
Neil Trini Lasrado06724592016-08-22 12:57:09 +0530126 if item_code:
127 # make sub-contract PO
128 po = frappe.new_doc("Purchase Order")
129 po.is_subcontracted = "Yes"
130 po.supplier = get_random("Supplier")
Rushabh Mehta92d1b8c2016-07-14 15:46:12 +0530131
Neil Trini Lasrado06724592016-08-22 12:57:09 +0530132 item_code = get_random("Item", {"is_sub_contracted_item": 1})
Rushabh Mehta99c814b2016-12-19 11:51:02 +0530133
Neil Trini Lasrado06724592016-08-22 12:57:09 +0530134 po.append("items", {
135 "item_code": item_code,
136 "schedule_date": frappe.utils.add_days(frappe.flags.current_date, 7),
137 "qty": random.randint(10, 30)
138 })
139 po.set_missing_values()
140 try:
141 po.insert()
142 except InvalidCurrency:
143 return
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530144
Neil Trini Lasrado06724592016-08-22 12:57:09 +0530145 po.submit()
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530146
Neil Trini Lasrado06724592016-08-22 12:57:09 +0530147 # make material request for
148 make_material_request(po.items[0].item_code, po.items[0].qty)
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530149
Neil Trini Lasrado06724592016-08-22 12:57:09 +0530150 # transfer material for sub-contract
151 stock_entry = frappe.get_doc(make_stock_entry(po.name, po.items[0].item_code))
152 stock_entry.from_warehouse = "Stores - WPL"
153 stock_entry.to_warehouse = "Supplier - WPL"
154 stock_entry.insert()