From 144e9b178aabdeff04cca5cfd491c591e6974a30 Mon Sep 17 00:00:00 2001 From: Rohit Waghchaure Date: Wed, 16 May 2018 12:15:57 +0530 Subject: [PATCH] Added project condition in budget validation --- erpnext/accounts/doctype/budget/budget.py | 27 +++++++++++++++++------ erpnext/controllers/buying_controller.py | 1 + 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/erpnext/accounts/doctype/budget/budget.py b/erpnext/accounts/doctype/budget/budget.py index f2aa59b8b7..d3a0d1d754 100644 --- a/erpnext/accounts/doctype/budget/budget.py +++ b/erpnext/accounts/doctype/budget/budget.py @@ -181,28 +181,41 @@ def get_amount(args, budget): amount = 0 if args.get('doctype') == 'Material Request' and budget.for_material_request: - amount = (get_requested_amount(args.item_code) - + get_ordered_amount(args.item_code) + get_actual_expense(args)) + amount = (get_requested_amount(args) + + get_ordered_amount(args) + get_actual_expense(args)) elif args.get('doctype') == 'Purchase Order' and budget.for_purchase_order: - amount = get_ordered_amount(args.item_code) + get_actual_expense(args) + amount = get_ordered_amount(args) + get_actual_expense(args) return amount -def get_requested_amount(item_code): +def get_requested_amount(args): + item_code = args.get('item_code') + condition = get_project_condiion(args) + data = frappe.db.sql(""" select ifnull((sum(stock_qty - ordered_qty) * rate), 0) as amount from `tabMaterial Request Item` where item_code = %s and docstatus = 1 - and stock_qty > ordered_qty """, item_code, as_list=1) + and stock_qty > ordered_qty and {0}""".format(condition), item_code, as_list=1) return data[0][0] if data else 0 -def get_ordered_amount(item_code): +def get_ordered_amount(args): + item_code = args.get('item_code') + condition = get_project_condiion(args) + data = frappe.db.sql(""" select ifnull(sum(amount - billed_amt), 0) as amount from `tabPurchase Order Item` where item_code = %s and docstatus = 1 - and amount > billed_amt""", item_code, as_list=1) + and amount > billed_amt and {0}""".format(condition), item_code, as_list=1) return data[0][0] if data else 0 +def get_project_condiion(args): + condition = "1=1" + if args.get('project'): + condition = "project = '%s'" %(args.get('project')) + + return condition + def get_actual_expense(args): condition1 = " and gle.posting_date <= %(month_end_date)s" \ if args.get("month_end_date") else "" diff --git a/erpnext/controllers/buying_controller.py b/erpnext/controllers/buying_controller.py index 771687962c..fa2436d647 100644 --- a/erpnext/controllers/buying_controller.py +++ b/erpnext/controllers/buying_controller.py @@ -473,6 +473,7 @@ class BuyingController(StockController): 'item_code': data.item_code, 'item_group': data.item_group, 'posting_date': data.schedule_date, + 'project': data.project, 'doctype': self.doctype }, self.company)