brotherton-erpnext/erpnext/demo/user/purchase.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

182 lines
6.1 KiB
Python
Raw Normal View History

2016-06-29 13:08:32 +00:00
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt
from __future__ import unicode_literals
import json
import random
import frappe
2016-06-29 13:08:32 +00:00
from frappe.desk import query_report
from frappe.utils.make_random import get_random, how_many
import erpnext
2016-06-29 13:08:32 +00:00
from erpnext.accounts.party import get_party_account_currency
from erpnext.buying.doctype.request_for_quotation.request_for_quotation import (
make_supplier_quotation_from_rfq,
)
2016-06-29 13:08:32 +00:00
from erpnext.exceptions import InvalidCurrency
from erpnext.setup.utils import get_exchange_rate
2016-06-29 13:08:32 +00:00
from erpnext.stock.doctype.material_request.material_request import make_request_for_quotation
2016-06-29 13:08:32 +00:00
2016-07-07 08:32:26 +00:00
def work():
frappe.set_user(frappe.db.get_global('demo_purchase_user'))
2018-09-27 13:21:18 +00:00
if random.random() < 0.6:
2016-06-29 13:08:32 +00:00
report = "Items To Be Requested"
2016-07-07 08:32:26 +00:00
for row in query_report.run(report)["result"][:random.randint(1, 5)]:
2016-06-29 13:08:32 +00:00
item_code, qty = row[0], abs(row[-1])
mr = make_material_request(item_code, qty)
2018-09-27 13:21:18 +00:00
if random.random() < 0.6:
2016-07-07 08:32:26 +00:00
for mr in frappe.get_all('Material Request',
filters={'material_request_type': 'Purchase', 'status': 'Open'},
limit=random.randint(1,6)):
if not frappe.get_all('Request for Quotation',
filters={'material_request': mr.name}, limit=1):
rfq = make_request_for_quotation(mr.name)
rfq.transaction_date = frappe.flags.current_date
add_suppliers(rfq)
rfq.save()
rfq.submit()
# Make suppier quotation from RFQ against each supplier.
2018-09-27 13:21:18 +00:00
if random.random() < 0.6:
2016-07-07 08:32:26 +00:00
for rfq in frappe.get_all('Request for Quotation',
filters={'status': 'Open'}, limit=random.randint(1, 6)):
if not frappe.get_all('Supplier Quotation',
filters={'request_for_quotation': rfq.name}, limit=1):
rfq = frappe.get_doc('Request for Quotation', rfq.name)
for supplier in rfq.suppliers:
supplier_quotation = make_supplier_quotation_from_rfq(rfq.name, for_supplier=supplier.supplier)
2016-07-07 08:32:26 +00:00
supplier_quotation.save()
supplier_quotation.submit()
2016-06-29 13:08:32 +00:00
# get supplier details
supplier = get_random("Supplier")
company_currency = frappe.get_cached_value('Company', erpnext.get_default_company(), "default_currency")
party_account_currency = get_party_account_currency("Supplier", supplier, erpnext.get_default_company())
2016-06-29 13:08:32 +00:00
if company_currency == party_account_currency:
exchange_rate = 1
else:
exchange_rate = get_exchange_rate(party_account_currency, company_currency, args="for_buying")
2016-06-29 13:08:32 +00:00
# make supplier quotations
2018-09-27 13:21:18 +00:00
if random.random() < 0.5:
2016-06-29 13:08:32 +00:00
from erpnext.stock.doctype.material_request.material_request import make_supplier_quotation
report = "Material Requests for which Supplier Quotations are not created"
2016-07-08 12:54:46 +00:00
for row in query_report.run(report)["result"][:random.randint(1, 3)]:
if row[0] != "Total":
2016-06-29 13:08:32 +00:00
sq = frappe.get_doc(make_supplier_quotation(row[0]))
2016-07-07 08:32:26 +00:00
sq.transaction_date = frappe.flags.current_date
2016-06-29 13:08:32 +00:00
sq.supplier = supplier
sq.currency = party_account_currency or company_currency
sq.conversion_rate = exchange_rate
sq.insert()
sq.submit()
frappe.db.commit()
# make purchase orders
2016-12-19 06:21:02 +00:00
if random.random() < 0.5:
2016-06-29 13:08:32 +00:00
from erpnext.stock.doctype.material_request.material_request import make_purchase_order
report = "Requested Items To Be Ordered"
for row in query_report.run(report)["result"][:how_many("Purchase Order")]:
if row[0] != "Total":
try:
po = frappe.get_doc(make_purchase_order(row[0]))
po.supplier = supplier
po.currency = party_account_currency or company_currency
po.conversion_rate = exchange_rate
po.transaction_date = frappe.flags.current_date
po.insert()
po.submit()
except Exception:
pass
else:
frappe.db.commit()
2016-06-29 13:08:32 +00:00
2018-09-27 13:21:18 +00:00
if random.random() < 0.5:
2016-07-07 08:32:26 +00:00
make_subcontract()
2016-06-29 13:08:32 +00:00
def make_material_request(item_code, qty):
mr = frappe.new_doc("Material Request")
2016-07-08 12:54:46 +00:00
variant_of = frappe.db.get_value('Item', item_code, 'variant_of') or item_code
if frappe.db.get_value('BOM', {'item': variant_of, 'is_default': 1, 'is_active': 1}):
2016-06-29 13:08:32 +00:00
mr.material_request_type = 'Manufacture'
else:
mr.material_request_type = "Purchase"
mr.transaction_date = frappe.flags.current_date
mr.schedule_date = frappe.utils.add_days(mr.transaction_date, 7)
2016-12-19 06:21:02 +00:00
2016-06-29 13:08:32 +00:00
mr.append("items", {
"doctype": "Material Request Item",
"schedule_date": frappe.utils.add_days(mr.transaction_date, 7),
"item_code": item_code,
2016-08-22 07:27:09 +00:00
"qty": qty
2016-06-29 13:08:32 +00:00
})
mr.insert()
mr.submit()
return mr
def add_suppliers(rfq):
for i in range(2):
2016-06-29 13:08:32 +00:00
supplier = get_random("Supplier")
if supplier not in [d.supplier for d in rfq.get('suppliers')]:
rfq.append("suppliers", { "supplier": supplier })
def make_subcontract():
2018-09-26 06:57:09 +00:00
from erpnext.buying.doctype.purchase_order.purchase_order import make_rm_stock_entry
item_code = get_random("Item", {"is_sub_contracted_item": 1})
2016-08-22 07:27:09 +00:00
if item_code:
# make sub-contract PO
po = frappe.new_doc("Purchase Order")
po.is_subcontracted = "Yes"
po.supplier = get_random("Supplier")
2018-09-27 13:21:18 +00:00
po.transaction_date = frappe.flags.current_date # added
po.schedule_date = frappe.utils.add_days(frappe.flags.current_date, 7)
2016-08-22 07:27:09 +00:00
item_code = get_random("Item", {"is_sub_contracted_item": 1})
2016-12-19 06:21:02 +00:00
2016-08-22 07:27:09 +00:00
po.append("items", {
"item_code": item_code,
"schedule_date": frappe.utils.add_days(frappe.flags.current_date, 7),
"qty": random.randint(10, 30)
})
po.set_missing_values()
try:
po.insert()
except InvalidCurrency:
return
po.submit()
# make material request for
make_material_request(po.items[0].item_code, po.items[0].qty)
# transfer material for sub-contract
2018-09-28 08:29:09 +00:00
rm_items = get_rm_item(po.items[0], po.supplied_items[0])
stock_entry = frappe.get_doc(make_rm_stock_entry(po.name, json.dumps([rm_items])))
2016-08-22 07:27:09 +00:00
stock_entry.from_warehouse = "Stores - WPL"
stock_entry.to_warehouse = "Supplier - WPL"
stock_entry.insert()
2018-09-28 08:29:09 +00:00
def get_rm_item(items, supplied_items):
return {
"item_code": items.get("item_code"),
"rm_item_code": supplied_items.get("rm_item_code"),
"item_name": supplied_items.get("rm_item_code"),
"qty": supplied_items.get("required_qty") + random.randint(3,10),
2018-09-28 08:29:09 +00:00
"amount": supplied_items.get("amount"),
"warehouse": supplied_items.get("reserve_warehouse"),
"rate": supplied_items.get("rate"),
"stock_uom": supplied_items.get("stock_uom")
}