blob: 6f06f26fb1110105d2b0273c598d32eb1bb3c65f [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)):
Rushabh Mehtacca33b22016-07-08 18:24:46 +053030 print mr.name
Rushabh Mehtaea0ff232016-07-07 14:02:26 +053031 if not frappe.get_all('Request for Quotation',
32 filters={'material_request': mr.name}, limit=1):
33 rfq = make_request_for_quotation(mr.name)
34 rfq.transaction_date = frappe.flags.current_date
35 add_suppliers(rfq)
36 rfq.save()
37 rfq.submit()
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053038
Rushabh Mehtaea0ff232016-07-07 14:02:26 +053039 # Make suppier quotation from RFQ against each supplier.
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053040 if random.random() < 0.3:
Rushabh Mehtaea0ff232016-07-07 14:02:26 +053041 for rfq in frappe.get_all('Request for Quotation',
42 filters={'status': 'Open'}, limit=random.randint(1, 6)):
43 if not frappe.get_all('Supplier Quotation',
44 filters={'request_for_quotation': rfq.name}, limit=1):
45 rfq = frappe.get_doc('Request for Quotation', rfq.name)
46
47 for supplier in rfq.suppliers:
48 supplier_quotation = make_quotation_from_rfq(rfq.name, supplier.supplier)
49 supplier_quotation.save()
50 supplier_quotation.submit()
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053051
52 # get supplier details
53 supplier = get_random("Supplier")
54
55 company_currency = frappe.db.get_value("Company", "Wind Power LLC", "default_currency")
56 party_account_currency = get_party_account_currency("Supplier", supplier, "Wind Power LLC")
57 if company_currency == party_account_currency:
58 exchange_rate = 1
59 else:
60 exchange_rate = get_exchange_rate(party_account_currency, company_currency)
61
62 # make supplier quotations
Rushabh Mehtacca33b22016-07-08 18:24:46 +053063 if random.random() < 0.2:
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053064 from erpnext.stock.doctype.material_request.material_request import make_supplier_quotation
65
66 report = "Material Requests for which Supplier Quotations are not created"
Rushabh Mehtacca33b22016-07-08 18:24:46 +053067 for row in query_report.run(report)["result"][:random.randint(1, 3)]:
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053068 if row[0] != "'Total'":
69 sq = frappe.get_doc(make_supplier_quotation(row[0]))
Rushabh Mehtaea0ff232016-07-07 14:02:26 +053070 sq.transaction_date = frappe.flags.current_date
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053071 sq.supplier = supplier
72 sq.currency = party_account_currency or company_currency
73 sq.conversion_rate = exchange_rate
74 sq.insert()
75 sq.submit()
76 frappe.db.commit()
77
78 # make purchase orders
Rushabh Mehtaea0ff232016-07-07 14:02:26 +053079 if random.random() < 0.3:
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053080 from erpnext.stock.doctype.material_request.material_request import make_purchase_order
81 report = "Requested Items To Be Ordered"
82 for row in query_report.run(report)["result"][:how_many("Purchase Order")]:
83 if row[0] != "'Total'":
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
Rushabh Mehtaea0ff232016-07-07 14:02:26 +053088 po.transaction_date = frappe.flags.current_date
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053089 po.insert()
90 po.submit()
91 frappe.db.commit()
92
Rushabh Mehtacca33b22016-07-08 18:24:46 +053093 if random.random() < 0.2:
Rushabh Mehtaea0ff232016-07-07 14:02:26 +053094 make_subcontract()
Rushabh Mehtadc8067e2016-06-29 18:38:32 +053095
96def make_material_request(item_code, qty):
97 mr = frappe.new_doc("Material Request")
98
Rushabh Mehtacca33b22016-07-08 18:24:46 +053099 variant_of = frappe.db.get_value('Item', item_code, 'variant_of') or item_code
100
101 if frappe.db.get_value('BOM', {'item': variant_of, 'is_default': 1, 'is_active': 1}):
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530102 mr.material_request_type = 'Manufacture'
103 else:
104 mr.material_request_type = "Purchase"
105
106 mr.transaction_date = frappe.flags.current_date
Rushabh Mehtaffa70e32016-07-13 17:41:03 +0530107
108 moq = frappe.db.get_value('Item', item_code, 'min_order_qty')
109
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530110 mr.append("items", {
111 "doctype": "Material Request Item",
112 "schedule_date": frappe.utils.add_days(mr.transaction_date, 7),
113 "item_code": item_code,
Rushabh Mehtaffa70e32016-07-13 17:41:03 +0530114 "qty": qty if qty > moq else moq
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530115 })
116 mr.insert()
117 mr.submit()
118 return mr
119
120def add_suppliers(rfq):
121 for i in xrange(2):
122 supplier = get_random("Supplier")
123 if supplier not in [d.supplier for d in rfq.get('suppliers')]:
124 rfq.append("suppliers", { "supplier": supplier })
125
126def make_subcontract():
127 from erpnext.buying.doctype.purchase_order.purchase_order import make_stock_entry
128
129 # make sub-contract PO
130 po = frappe.new_doc("Purchase Order")
131 po.is_subcontracted = "Yes"
132 po.supplier = get_random("Supplier")
133
Rushabh Mehta92d1b8c2016-07-14 15:46:12 +0530134 item_code = get_random("Item", {"is_sub_contracted_item": 1})
135 moq = frappe.db.get_value('Item', item_code, 'min_order_qty')
136
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530137 po.append("items", {
Rushabh Mehta92d1b8c2016-07-14 15:46:12 +0530138 "item_code": item_code,
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530139 "schedule_date": frappe.utils.add_days(frappe.flags.current_date, 7),
Rushabh Mehta92d1b8c2016-07-14 15:46:12 +0530140 "qty": moq
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530141 })
142 po.set_missing_values()
143 try:
144 po.insert()
145 except InvalidCurrency:
146 return
147
148 po.submit()
149
150 # make material request for
151 make_material_request(po.items[0].item_code, po.items[0].qty)
152
153 # transfer material for sub-contract
154 stock_entry = frappe.get_doc(make_stock_entry(po.name, po.items[0].item_code))
Rushabh Mehtacca33b22016-07-08 18:24:46 +0530155 stock_entry.from_warehouse = "Stores - WPL"
156 stock_entry.to_warehouse = "Supplier - WPL"
Rushabh Mehtadc8067e2016-06-29 18:38:32 +0530157 stock_entry.insert()