Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
| 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
| 4 | from __future__ import unicode_literals |
| 5 | |
| 6 | import frappe, random |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 7 | from frappe.utils.make_random import how_many, get_random |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 8 | from frappe.desk import query_report |
| 9 | from erpnext.setup.utils import get_exchange_rate |
| 10 | from erpnext.accounts.party import get_party_account_currency |
| 11 | from erpnext.exceptions import InvalidCurrency |
| 12 | from erpnext.stock.doctype.material_request.material_request import make_request_for_quotation |
| 13 | from erpnext.buying.doctype.request_for_quotation.request_for_quotation import \ |
| 14 | make_supplier_quotation as make_quotation_from_rfq |
| 15 | |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 16 | def work(): |
| 17 | frappe.set_user(frappe.db.get_global('demo_purchase_user')) |
| 18 | |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 19 | if random.random() < 0.3: |
| 20 | report = "Items To Be Requested" |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 21 | for row in query_report.run(report)["result"][:random.randint(1, 5)]: |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 22 | 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 Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 27 | 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 Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 37 | |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 38 | # Make suppier quotation from RFQ against each supplier. |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 39 | if random.random() < 0.3: |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 40 | 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 Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 50 | |
| 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 Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 62 | if random.random() < 0.2: |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 63 | 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 Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 66 | for row in query_report.run(report)["result"][:random.randint(1, 3)]: |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 67 | if row[0] != "'Total'": |
| 68 | sq = frappe.get_doc(make_supplier_quotation(row[0])) |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 69 | sq.transaction_date = frappe.flags.current_date |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 70 | 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 Mehta | 99c814b | 2016-12-19 11:51:02 +0530 | [diff] [blame] | 78 | if random.random() < 0.5: |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 79 | 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 Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 87 | po.transaction_date = frappe.flags.current_date |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 88 | po.insert() |
| 89 | po.submit() |
| 90 | frappe.db.commit() |
| 91 | |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 92 | if random.random() < 0.2: |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 93 | make_subcontract() |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 94 | |
| 95 | def make_material_request(item_code, qty): |
| 96 | mr = frappe.new_doc("Material Request") |
| 97 | |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 98 | 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 Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 101 | mr.material_request_type = 'Manufacture' |
| 102 | else: |
| 103 | mr.material_request_type = "Purchase" |
| 104 | |
| 105 | mr.transaction_date = frappe.flags.current_date |
Rushabh Mehta | 99c814b | 2016-12-19 11:51:02 +0530 | [diff] [blame] | 106 | |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 107 | 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 Lasrado | 0672459 | 2016-08-22 12:57:09 +0530 | [diff] [blame] | 111 | "qty": qty |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 112 | }) |
| 113 | mr.insert() |
| 114 | mr.submit() |
| 115 | return mr |
| 116 | |
| 117 | def 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 | |
| 123 | def make_subcontract(): |
| 124 | from erpnext.buying.doctype.purchase_order.purchase_order import make_stock_entry |
Rushabh Mehta | 92d1b8c | 2016-07-14 15:46:12 +0530 | [diff] [blame] | 125 | item_code = get_random("Item", {"is_sub_contracted_item": 1}) |
Neil Trini Lasrado | 0672459 | 2016-08-22 12:57:09 +0530 | [diff] [blame] | 126 | 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 Mehta | 92d1b8c | 2016-07-14 15:46:12 +0530 | [diff] [blame] | 131 | |
Neil Trini Lasrado | 0672459 | 2016-08-22 12:57:09 +0530 | [diff] [blame] | 132 | item_code = get_random("Item", {"is_sub_contracted_item": 1}) |
Rushabh Mehta | 99c814b | 2016-12-19 11:51:02 +0530 | [diff] [blame] | 133 | |
Neil Trini Lasrado | 0672459 | 2016-08-22 12:57:09 +0530 | [diff] [blame] | 134 | 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 Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 144 | |
Neil Trini Lasrado | 0672459 | 2016-08-22 12:57:09 +0530 | [diff] [blame] | 145 | po.submit() |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 146 | |
Neil Trini Lasrado | 0672459 | 2016-08-22 12:57:09 +0530 | [diff] [blame] | 147 | # make material request for |
| 148 | make_material_request(po.items[0].item_code, po.items[0].qty) |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 149 | |
Neil Trini Lasrado | 0672459 | 2016-08-22 12:57:09 +0530 | [diff] [blame] | 150 | # 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() |