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)): |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 30 | print mr.name |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 31 | 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 Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 38 | |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 39 | # Make suppier quotation from RFQ against each supplier. |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 40 | if random.random() < 0.3: |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 41 | 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 Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 51 | |
| 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 Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 63 | if random.random() < 0.2: |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 64 | 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 Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 67 | for row in query_report.run(report)["result"][:random.randint(1, 3)]: |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 68 | if row[0] != "'Total'": |
| 69 | sq = frappe.get_doc(make_supplier_quotation(row[0])) |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 70 | sq.transaction_date = frappe.flags.current_date |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 71 | 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 Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 79 | if random.random() < 0.3: |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 80 | 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 Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 88 | po.transaction_date = frappe.flags.current_date |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 89 | po.insert() |
| 90 | po.submit() |
| 91 | frappe.db.commit() |
| 92 | |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 93 | if random.random() < 0.2: |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 94 | make_subcontract() |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 95 | |
| 96 | def make_material_request(item_code, qty): |
| 97 | mr = frappe.new_doc("Material Request") |
| 98 | |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 99 | 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 Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 102 | mr.material_request_type = 'Manufacture' |
| 103 | else: |
| 104 | mr.material_request_type = "Purchase" |
| 105 | |
| 106 | mr.transaction_date = frappe.flags.current_date |
Rushabh Mehta | ffa70e3 | 2016-07-13 17:41:03 +0530 | [diff] [blame] | 107 | |
| 108 | moq = frappe.db.get_value('Item', item_code, 'min_order_qty') |
| 109 | |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 110 | 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 Mehta | ffa70e3 | 2016-07-13 17:41:03 +0530 | [diff] [blame] | 114 | "qty": qty if qty > moq else moq |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 115 | }) |
| 116 | mr.insert() |
| 117 | mr.submit() |
| 118 | return mr |
| 119 | |
| 120 | def 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 | |
| 126 | def 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 Mehta | 92d1b8c | 2016-07-14 15:46:12 +0530 | [diff] [blame] | 134 | item_code = get_random("Item", {"is_sub_contracted_item": 1}) |
| 135 | moq = frappe.db.get_value('Item', item_code, 'min_order_qty') |
| 136 | |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 137 | po.append("items", { |
Rushabh Mehta | 92d1b8c | 2016-07-14 15:46:12 +0530 | [diff] [blame] | 138 | "item_code": item_code, |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 139 | "schedule_date": frappe.utils.add_days(frappe.flags.current_date, 7), |
Rushabh Mehta | 92d1b8c | 2016-07-14 15:46:12 +0530 | [diff] [blame] | 140 | "qty": moq |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 141 | }) |
| 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 Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 155 | stock_entry.from_warehouse = "Stores - WPL" |
| 156 | stock_entry.to_warehouse = "Supplier - WPL" |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 157 | stock_entry.insert() |