Skip to content

Commit

Permalink
wip: material demand report improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
agritheory committed Jun 17, 2023
1 parent 3f50d5f commit c20379a
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"aggregated_purchasing_warehouse",
"work_order_subcontracting_section",
"enable_work_order_subcontracting",
"create_purchase_orders"
"create_purchase_orders",
"section_break_ksp0d",
"update_warehouse_path"
],
"fields": [
{
Expand All @@ -30,6 +32,7 @@
"label": "Material Demand"
},
{
"description": "Leave this field blank to disallow aggregation for this company",
"fieldname": "purchase_order_aggregation_company",
"fieldtype": "Link",
"label": "Purchase Order Aggregation Company",
Expand Down Expand Up @@ -62,11 +65,21 @@
"fieldname": "create_purchase_orders",
"fieldtype": "Check",
"label": "Create Purchase Orders in Production Plan"
},
{
"fieldname": "section_break_ksp0d",
"fieldtype": "Section Break"
},
{
"default": "0",
"fieldname": "update_warehouse_path",
"fieldtype": "Check",
"label": "Update Warehouse Path"
}
],
"index_web_pages_for_search": 1,
"links": [],
"modified": "2023-05-30 20:26:59.132951",
"modified": "2023-06-16 15:13:17.067894",
"modified_by": "Administrator",
"module": "Inventory Tools",
"name": "Inventory Tools Settings",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
# Copyright (c) 2023, AgriTheory and contributors
# For license information, please see license.txt

# import frappe
import frappe
from frappe.model.document import Document


class InventoryToolsSettings(Document):
pass
def validate(self):
if self.update_warehouse_path:
create_warehouse_path_custom_field()


def create_warehouse_path_custom_field():
if frappe.db.exists("Custom Field", "Warehouse-warehouse_path"):
return
cf = frappe.new_doc("Custom Field")
cf.field_name = ""
cf.field_type = "Data"
cf.label = "Warehouse Path"
# cf.save()

# property setter for warehouse path


@frappe.whitelist()
def create_inventory_tools_settings(doc, method=None) -> None:
if frappe.db.exists("Inventory Tools Settings", {"company": doc.name}):
return
its = frappe.new_doc("Inventory Tools Settings")
its.company = doc.name
its.save()
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright (c) 2023, AgriTheory and Contributors
# See license.txt

import json

import frappe
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright (c) 2023, AgriTheory and Contributors
# See license.txt

import json
import types

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright (c) 2023, AgriTheory and Contributors
# See license.txt

import json

import frappe
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@

frappe.query_reports['Material Demand'] = {
filters: [
// {
// fieldname: 'price_list',
// label: __('Price List'),
// fieldtype: 'Link',
// options: 'Price List',
// reqd: 1,
// default: 'Standard Buying',
// // only buying price lists?
// },
{
fieldname: 'company',
label: __('Company'),
Expand All @@ -33,7 +24,14 @@ frappe.query_reports['Material Demand'] = {
],
get_datatable_options(options) {
return Object.assign(options, {
treeView: true,
checkedRowStatus: false,
checkboxColumn: true,
events: {
onCheckRow: row => {
update_selection(row)
},
},
})
},
onload: reportview => {
Expand Down Expand Up @@ -68,3 +66,39 @@ async function create_pos() {
.then(r => {})
}
}

function update_selection(row) {
if (!row[5]) {
// const checked = true //frappe.query_report.datatable.rowmanager.checkMap[row.rowIndex]
// // console.log(checked)
// frappe.query_report.datatable.datamanager.data.forEach((supplier_row, index) => {
// if(supplier_row.supplier === row[2].content){
// frappe.query_report.datatable.rowmanager.checkMap.splice(index, 1, checked)
// }
// })
} else {
// this should be a standalone function
// update selected_qty, format red if its over demand qty
update_selected_qty(row)
}
}

function update_selected_qty(row) {
const checked = frappe.query_report.datatable.rowmanager.checkMap[row[0].rowIndex]
let total_item_qty = 0.0
frappe.query_report.datatable.datamanager.data.forEach((supplier_row, index) => {
if (supplier_row.item_code === row[5].content) {
if (checked) {
total_item_qty += supplier_row.qty
}
}
})
console.log(total_item_qty)
// frappe.query_report.datatable.datamanager.data.forEach((supplier_row, index) => {
// if (supplier_row.item_code === row[5].content) {
// if (checked) {
// console.log('total', total_item_qty)
// }
// }
// })
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,26 @@ def get_columns(filters):
},
{"fieldname": "item_name", "fieldtype": "Data", "hidden": 1}, # unset for export
{
"label": "Qty",
"label": "MR Qty",
"fieldname": "qty",
"fieldtype": "Data",
"width": "90px",
"align": "right",
},
{
"label": "Total Demand",
"fieldname": "total_demand",
"fieldtype": "Data",
"width": "90px",
"align": "right",
},
{
"label": "Total Selected",
"fieldname": "total_selected",
"fieldtype": "Data",
"width": "90px",
"align": "right",
},
{
"label": "UOM",
"fieldname": "uom",
Expand All @@ -88,6 +102,13 @@ def get_columns(filters):
"width": "90px",
"align": "right",
},
{
"label": "Selected Amount",
"fieldname": "amount",
"fieldtype": "Data",
"width": "90px",
"align": "left",
},
{"fieldname": "currency", "fieldtype": "Link", "options": "Currency", "hidden": 1},
]

Expand All @@ -102,6 +123,7 @@ def get_data(filters):
`tabMaterial Request`.name AS material_request,
`tabMaterial Request`.company,
`tabMaterial Request`.schedule_date,
`tabMaterial Request Item`.name AS mri,
`tabMaterial Request Item`.item_code,
`tabMaterial Request Item`.item_name,
`tabMaterial Request Item`.qty,
Expand Down Expand Up @@ -129,11 +151,22 @@ def get_data(filters):
as_dict=True,
# debug=True,
)
total_demand = frappe._dict()
mris = []
for row in data:
if row.item_code not in total_demand and row.mri not in mris:
total_demand[row.item_code] = row.qty
mris.append(row.mri)
elif row.item_code in total_demand and row.mri not in mris:
total_demand[row.item_code] += row.qty
mris.append(row.mri)

for supplier, _rows in groupby(data, lambda x: x.get("supplier")):
rows = list(_rows)
output.append({"supplier": supplier, "indent": 0})
for r in rows:
r.supplier_price = fmt_money(r.get("supplier_price"), 2, r.get("currency")).replace(" ", "")
r.total_demand = total_demand[r.item_code]
output.append({**r, "indent": 1})
return output

Expand Down
6 changes: 3 additions & 3 deletions inventory_tools/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@
"item_group": "Bakery Supplies",
"item_price": 0.4,
"default_warehouse": "Storeroom - APC",
"supplier": "Unity Bakery Supply",
"supplier": ["Freedom Provisions", "Unity Bakery Supply"],
},
{
"item_code": "Pie Tin",
Expand All @@ -327,7 +327,7 @@
"item_price": 0.18,
"item_group": "Bakery Supplies",
"default_warehouse": "Storeroom - APC",
"supplier": "Unity Bakery Supply",
"supplier": ["Freedom Provisions", "Unity Bakery Supply"],
},
{
"item_code": "Parchment Paper",
Expand All @@ -336,7 +336,7 @@
"item_group": "Bakery Supplies",
"item_price": 0.02,
"default_warehouse": "Storeroom - APC",
"supplier": "Unity Bakery Supply",
"supplier": ["Freedom Provisions", "Unity Bakery Supply"],
},
{
"item_code": "Salt",
Expand Down
72 changes: 38 additions & 34 deletions inventory_tools/tests/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def create_test_data():
create_items(settings)
create_boms(settings)
create_material_request(settings)
# create_production_plan(settings)
create_production_plan(settings)


def create_suppliers(settings):
Expand Down Expand Up @@ -245,7 +245,10 @@ def create_items(settings):
{"company": settings.company, "default_warehouse": item.get("default_warehouse")},
)
if i.is_purchase_item and item.get("supplier"):
i.append("supplier_items", {"supplier": item.get("supplier")})
if isinstance(item.get("supplier"), list):
[i.append("supplier_items", {"supplier": s}) for s in item.get("supplier")]
else:
i.append("supplier_items", {"supplier": item.get("supplier")})
i.save()
if item.get("item_price"):
ip = frappe.new_doc("Item Price")
Expand Down Expand Up @@ -384,38 +387,39 @@ def create_production_plan(settings):
mr.save()
mr.submit()

for item in mr.items:
supplier = frappe.get_value("Item Supplier", {"parent": item.get("item_code")}, "supplier")
item.supplier = supplier

for supplier, _items in groupby(
sorted((m for m in mr.items if m.supplier), key=lambda d: d.supplier),
lambda x: x.get("supplier"),
):
items = list(_items)
if not supplier:
continue
pr = frappe.new_doc("Purchase Receipt")
pr.company = settings.company
pr.supplier = supplier
pr.posting_date = settings.day
pr.set_posting_time = 1
pr.buying_price_list = "Bakery Buying"
for item in items:
item_details = get_item_details(
{
"item_code": item.item_code,
"qty": item.qty,
"supplier": pr.supplier,
"company": pr.company,
"doctype": pr.doctype,
"currency": pr.currency,
"buying_price_list": pr.buying_price_list,
}
)
pr.append("items", {**item_details})
pr.save()
pr.submit()
# TODO: this should test the material demand report instead
# for item in mr.items:
# supplier = frappe.get_value("Item Supplier", {"parent": item.get("item_code")}, "supplier")
# item.supplier = supplier

# for supplier, _items in groupby(
# sorted((m for m in mr.items if m.supplier), key=lambda d: d.supplier),
# lambda x: x.get("supplier"),
# ):
# items = list(_items)
# if not supplier:
# continue
# pr = frappe.new_doc("Purchase Receipt")
# pr.company = settings.company
# pr.supplier = supplier
# pr.posting_date = settings.day
# pr.set_posting_time = 1
# pr.buying_price_list = "Bakery Buying"
# for item in items:
# item_details = get_item_details(
# {
# "item_code": item.item_code,
# "qty": item.qty,
# "supplier": pr.supplier,
# "company": pr.company,
# "doctype": pr.doctype,
# "currency": pr.currency,
# "buying_price_list": pr.buying_price_list,
# }
# )
# pr.append("items", {**item_details})
# pr.save()
# pr.submit()

pp.make_work_order()
wos = frappe.get_all("Work Order", {"production_plan": pp.name})
Expand Down

0 comments on commit c20379a

Please sign in to comment.