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 | |
Zlash65 | fd9c451 | 2018-10-10 14:27:07 +0530 | [diff] [blame] | 6 | import frappe, random, json, erpnext |
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 | |
Zlash65 | dcf7401 | 2018-09-27 18:51:18 +0530 | [diff] [blame] | 19 | if random.random() < 0.6: |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 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 | |
Zlash65 | dcf7401 | 2018-09-27 18:51:18 +0530 | [diff] [blame] | 26 | if random.random() < 0.6: |
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. |
Zlash65 | dcf7401 | 2018-09-27 18:51:18 +0530 | [diff] [blame] | 39 | if random.random() < 0.6: |
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 | |
Zlash65 | fd9c451 | 2018-10-10 14:27:07 +0530 | [diff] [blame] | 54 | company_currency = frappe.get_cached_value('Company', erpnext.get_default_company(), "default_currency") |
| 55 | party_account_currency = get_party_account_currency("Supplier", supplier, erpnext.get_default_company()) |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 56 | if company_currency == party_account_currency: |
| 57 | exchange_rate = 1 |
| 58 | else: |
Shreya | 3f77852 | 2018-05-15 16:59:20 +0530 | [diff] [blame] | 59 | exchange_rate = get_exchange_rate(party_account_currency, company_currency, args="for_buying") |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 60 | |
| 61 | # make supplier quotations |
Zlash65 | dcf7401 | 2018-09-27 18:51:18 +0530 | [diff] [blame] | 62 | if random.random() < 0.5: |
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'": |
Zlash65 | 8b1133c | 2018-10-01 12:55:07 +0530 | [diff] [blame] | 83 | try: |
| 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 |
| 88 | po.transaction_date = frappe.flags.current_date |
| 89 | po.insert() |
| 90 | po.submit() |
| 91 | except Exception: |
| 92 | pass |
| 93 | else: |
| 94 | frappe.db.commit() |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 95 | |
Zlash65 | dcf7401 | 2018-09-27 18:51:18 +0530 | [diff] [blame] | 96 | if random.random() < 0.5: |
Rushabh Mehta | ea0ff23 | 2016-07-07 14:02:26 +0530 | [diff] [blame] | 97 | make_subcontract() |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 98 | |
| 99 | def make_material_request(item_code, qty): |
| 100 | mr = frappe.new_doc("Material Request") |
| 101 | |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 102 | variant_of = frappe.db.get_value('Item', item_code, 'variant_of') or item_code |
| 103 | |
| 104 | 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] | 105 | mr.material_request_type = 'Manufacture' |
| 106 | else: |
| 107 | mr.material_request_type = "Purchase" |
| 108 | |
| 109 | mr.transaction_date = frappe.flags.current_date |
Sunny | 0d91d3f | 2017-09-26 10:56:31 +0800 | [diff] [blame] | 110 | mr.schedule_date = frappe.utils.add_days(mr.transaction_date, 7) |
Rushabh Mehta | 99c814b | 2016-12-19 11:51:02 +0530 | [diff] [blame] | 111 | |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 112 | mr.append("items", { |
| 113 | "doctype": "Material Request Item", |
| 114 | "schedule_date": frappe.utils.add_days(mr.transaction_date, 7), |
| 115 | "item_code": item_code, |
Neil Trini Lasrado | 0672459 | 2016-08-22 12:57:09 +0530 | [diff] [blame] | 116 | "qty": qty |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 117 | }) |
| 118 | mr.insert() |
| 119 | mr.submit() |
| 120 | return mr |
| 121 | |
| 122 | def add_suppliers(rfq): |
Achilles Rasquinha | 96698c9 | 2018-02-28 16:12:51 +0530 | [diff] [blame] | 123 | for i in range(2): |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 124 | supplier = get_random("Supplier") |
| 125 | if supplier not in [d.supplier for d in rfq.get('suppliers')]: |
| 126 | rfq.append("suppliers", { "supplier": supplier }) |
| 127 | |
| 128 | def make_subcontract(): |
Zlash65 | 796bffb | 2018-09-26 12:27:09 +0530 | [diff] [blame] | 129 | from erpnext.buying.doctype.purchase_order.purchase_order import make_rm_stock_entry |
Rushabh Mehta | 92d1b8c | 2016-07-14 15:46:12 +0530 | [diff] [blame] | 130 | item_code = get_random("Item", {"is_sub_contracted_item": 1}) |
Neil Trini Lasrado | 0672459 | 2016-08-22 12:57:09 +0530 | [diff] [blame] | 131 | if item_code: |
| 132 | # make sub-contract PO |
| 133 | po = frappe.new_doc("Purchase Order") |
| 134 | po.is_subcontracted = "Yes" |
| 135 | po.supplier = get_random("Supplier") |
Zlash65 | dcf7401 | 2018-09-27 18:51:18 +0530 | [diff] [blame] | 136 | po.transaction_date = frappe.flags.current_date # added |
Sunny | 3a22fd1 | 2017-09-26 12:21:41 +0800 | [diff] [blame] | 137 | po.schedule_date = frappe.utils.add_days(frappe.flags.current_date, 7) |
Rushabh Mehta | 92d1b8c | 2016-07-14 15:46:12 +0530 | [diff] [blame] | 138 | |
Neil Trini Lasrado | 0672459 | 2016-08-22 12:57:09 +0530 | [diff] [blame] | 139 | item_code = get_random("Item", {"is_sub_contracted_item": 1}) |
Rushabh Mehta | 99c814b | 2016-12-19 11:51:02 +0530 | [diff] [blame] | 140 | |
Neil Trini Lasrado | 0672459 | 2016-08-22 12:57:09 +0530 | [diff] [blame] | 141 | po.append("items", { |
| 142 | "item_code": item_code, |
| 143 | "schedule_date": frappe.utils.add_days(frappe.flags.current_date, 7), |
| 144 | "qty": random.randint(10, 30) |
| 145 | }) |
| 146 | po.set_missing_values() |
| 147 | try: |
| 148 | po.insert() |
| 149 | except InvalidCurrency: |
| 150 | return |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 151 | |
Neil Trini Lasrado | 0672459 | 2016-08-22 12:57:09 +0530 | [diff] [blame] | 152 | po.submit() |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 153 | |
Neil Trini Lasrado | 0672459 | 2016-08-22 12:57:09 +0530 | [diff] [blame] | 154 | # make material request for |
| 155 | make_material_request(po.items[0].item_code, po.items[0].qty) |
Rushabh Mehta | dc8067e | 2016-06-29 18:38:32 +0530 | [diff] [blame] | 156 | |
Neil Trini Lasrado | 0672459 | 2016-08-22 12:57:09 +0530 | [diff] [blame] | 157 | # transfer material for sub-contract |
Zlash65 | 523f77e | 2018-09-28 13:59:09 +0530 | [diff] [blame] | 158 | rm_items = get_rm_item(po.items[0], po.supplied_items[0]) |
| 159 | stock_entry = frappe.get_doc(make_rm_stock_entry(po.name, json.dumps([rm_items]))) |
Neil Trini Lasrado | 0672459 | 2016-08-22 12:57:09 +0530 | [diff] [blame] | 160 | stock_entry.from_warehouse = "Stores - WPL" |
| 161 | stock_entry.to_warehouse = "Supplier - WPL" |
| 162 | stock_entry.insert() |
Zlash65 | 523f77e | 2018-09-28 13:59:09 +0530 | [diff] [blame] | 163 | |
| 164 | def get_rm_item(items, supplied_items): |
| 165 | return { |
| 166 | "item_code": items.get("item_code"), |
| 167 | "rm_item_code": supplied_items.get("rm_item_code"), |
| 168 | "item_name": supplied_items.get("rm_item_code"), |
Zlash65 | d080a17 | 2018-10-01 10:49:06 +0530 | [diff] [blame] | 169 | "qty": supplied_items.get("required_qty") + random.randint(3,10), |
Zlash65 | 523f77e | 2018-09-28 13:59:09 +0530 | [diff] [blame] | 170 | "amount": supplied_items.get("amount"), |
| 171 | "warehouse": supplied_items.get("reserve_warehouse"), |
| 172 | "rate": supplied_items.get("rate"), |
| 173 | "stock_uom": supplied_items.get("stock_uom") |
| 174 | } |