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 |
| 7 | from frappe.utils.make_random import how_many, can_make, get_random |
| 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 | |
| 16 | def run_purchase(current_date): |
| 17 | if random.random() < 0.3: |
| 18 | report = "Items To Be Requested" |
| 19 | for row in query_report.run(report)["result"][:how_many("Material Request")]: |
| 20 | item_code, qty = row[0], abs(row[-1]) |
| 21 | |
| 22 | mr = make_material_request(item_code, qty) |
| 23 | |
| 24 | if random.random() < 0.3: |
| 25 | for mr in frappe.get_all('Material Request', filters={'material_request_type': 'Purchase', 'status': 'Open'}): |
| 26 | rfq = make_request_for_quotation(mr.name) |
| 27 | rfq.transaction_date = frappe.flags.current_date |
| 28 | add_suppliers(rfq) |
| 29 | rfq.save() |
| 30 | rfq.submit() |
| 31 | |
| 32 | # Make suppier quotation from RFQ against each supplier. |
| 33 | if random.random() < 0.3: |
| 34 | for supplier_quotation in frappe.get_all('Request for Quotation', {'status': 'Open'}): |
| 35 | rfq = frappe.get_doc('Request for Quotation', rfq.name) |
| 36 | for supplier in rfq.suppliers: |
| 37 | supplier_quotation = make_quotation_from_rfq(rfq.name, supplier.supplier) |
| 38 | supplier_quotation.save() |
| 39 | supplier_quotation.submit() |
| 40 | |
| 41 | # get supplier details |
| 42 | supplier = get_random("Supplier") |
| 43 | |
| 44 | company_currency = frappe.db.get_value("Company", "Wind Power LLC", "default_currency") |
| 45 | party_account_currency = get_party_account_currency("Supplier", supplier, "Wind Power LLC") |
| 46 | if company_currency == party_account_currency: |
| 47 | exchange_rate = 1 |
| 48 | else: |
| 49 | exchange_rate = get_exchange_rate(party_account_currency, company_currency) |
| 50 | |
| 51 | # make supplier quotations |
| 52 | if can_make("Supplier Quotation"): |
| 53 | from erpnext.stock.doctype.material_request.material_request import make_supplier_quotation |
| 54 | |
| 55 | report = "Material Requests for which Supplier Quotations are not created" |
| 56 | for row in query_report.run(report)["result"][:how_many("Supplier Quotation")]: |
| 57 | if row[0] != "'Total'": |
| 58 | sq = frappe.get_doc(make_supplier_quotation(row[0])) |
| 59 | sq.transaction_date = current_date |
| 60 | sq.supplier = supplier |
| 61 | sq.currency = party_account_currency or company_currency |
| 62 | sq.conversion_rate = exchange_rate |
| 63 | sq.insert() |
| 64 | sq.submit() |
| 65 | frappe.db.commit() |
| 66 | |
| 67 | # make purchase orders |
| 68 | if can_make("Purchase Order"): |
| 69 | from erpnext.stock.doctype.material_request.material_request import make_purchase_order |
| 70 | report = "Requested Items To Be Ordered" |
| 71 | for row in query_report.run(report)["result"][:how_many("Purchase Order")]: |
| 72 | if row[0] != "'Total'": |
| 73 | po = frappe.get_doc(make_purchase_order(row[0])) |
| 74 | po.supplier = supplier |
| 75 | po.currency = party_account_currency or company_currency |
| 76 | po.conversion_rate = exchange_rate |
| 77 | po.transaction_date = current_date |
| 78 | po.insert() |
| 79 | po.submit() |
| 80 | frappe.db.commit() |
| 81 | |
| 82 | if can_make("Subcontract"): |
| 83 | make_subcontract(current_date) |
| 84 | |
| 85 | def make_material_request(item_code, qty): |
| 86 | mr = frappe.new_doc("Material Request") |
| 87 | |
| 88 | if frappe.db.get_value('BOM', {'item': item_code, 'is_default': 1, 'is_active': 1}): |
| 89 | mr.material_request_type = 'Manufacture' |
| 90 | else: |
| 91 | mr.material_request_type = "Purchase" |
| 92 | |
| 93 | mr.transaction_date = frappe.flags.current_date |
| 94 | mr.append("items", { |
| 95 | "doctype": "Material Request Item", |
| 96 | "schedule_date": frappe.utils.add_days(mr.transaction_date, 7), |
| 97 | "item_code": item_code, |
| 98 | "qty": qty |
| 99 | }) |
| 100 | mr.insert() |
| 101 | mr.submit() |
| 102 | return mr |
| 103 | |
| 104 | def add_suppliers(rfq): |
| 105 | for i in xrange(2): |
| 106 | supplier = get_random("Supplier") |
| 107 | if supplier not in [d.supplier for d in rfq.get('suppliers')]: |
| 108 | rfq.append("suppliers", { "supplier": supplier }) |
| 109 | |
| 110 | def make_subcontract(): |
| 111 | from erpnext.buying.doctype.purchase_order.purchase_order import make_stock_entry |
| 112 | |
| 113 | # make sub-contract PO |
| 114 | po = frappe.new_doc("Purchase Order") |
| 115 | po.is_subcontracted = "Yes" |
| 116 | po.supplier = get_random("Supplier") |
| 117 | |
| 118 | po.append("items", { |
| 119 | "item_code": get_random("Item", {"is_sub_contracted_item": 1}), |
| 120 | "schedule_date": frappe.utils.add_days(frappe.flags.current_date, 7), |
| 121 | "qty": 20 |
| 122 | }) |
| 123 | po.set_missing_values() |
| 124 | try: |
| 125 | po.insert() |
| 126 | except InvalidCurrency: |
| 127 | return |
| 128 | |
| 129 | po.submit() |
| 130 | |
| 131 | # make material request for |
| 132 | make_material_request(po.items[0].item_code, po.items[0].qty) |
| 133 | |
| 134 | # transfer material for sub-contract |
| 135 | stock_entry = frappe.get_doc(make_stock_entry(po.name, po.items[0].item_code)) |
| 136 | stock_entry.from_warehouse = "Stores - WP" |
| 137 | stock_entry.to_warehouse = "Supplier - WP" |
| 138 | stock_entry.insert() |