@@ -1,37 +1,70 @@
|
|||||||
import frappe
|
import frappe
|
||||||
|
from frappe.query_builder.functions import Sum
|
||||||
from frappe.utils import flt
|
from frappe.utils import flt
|
||||||
|
|
||||||
from erpnext.stock.doctype.purchase_receipt.purchase_receipt import (
|
from erpnext.accounts.utils import get_fiscal_year
|
||||||
adjust_incoming_rate_for_pr,
|
from erpnext.stock.doctype.purchase_receipt.purchase_receipt import adjust_incoming_rate_for_pr
|
||||||
get_billed_qty_against_purchase_receipt,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def execute():
|
def execute():
|
||||||
|
fiscal_year_dates = get_fiscal_year(frappe.utils.datetime.date.today())
|
||||||
table = frappe.qb.DocType("Purchase Receipt Item")
|
table = frappe.qb.DocType("Purchase Receipt Item")
|
||||||
|
parent = frappe.qb.DocType("Purchase Receipt")
|
||||||
query = (
|
query = (
|
||||||
frappe.qb.from_(table)
|
frappe.qb.from_(table)
|
||||||
.select(table.parent)
|
.join(parent)
|
||||||
.distinct()
|
.on(table.parent == parent.name)
|
||||||
.where((table.amount_difference_with_purchase_invoice > 0) & (table.docstatus == 1))
|
.select(
|
||||||
|
table.parent,
|
||||||
|
table.name,
|
||||||
|
table.amount,
|
||||||
|
table.billed_amt,
|
||||||
|
table.amount_difference_with_purchase_invoice,
|
||||||
|
table.rate,
|
||||||
|
table.qty,
|
||||||
|
parent.conversion_rate,
|
||||||
|
)
|
||||||
|
.where(
|
||||||
|
(table.amount_difference_with_purchase_invoice != 0)
|
||||||
|
& (table.docstatus == 1)
|
||||||
|
& (parent.posting_date.between(fiscal_year_dates[1], fiscal_year_dates[2]))
|
||||||
|
)
|
||||||
)
|
)
|
||||||
pr_names = [item.parent for item in query.run(as_dict=True)]
|
result = query.run(as_dict=True)
|
||||||
|
|
||||||
for pr_name in pr_names:
|
item_wise_billed_qty = get_billed_qty_against_purchase_receipt([item.name for item in result])
|
||||||
pr_doc = frappe.get_doc("Purchase Receipt", pr_name)
|
|
||||||
for item in pr_doc.items:
|
|
||||||
adjusted_amt = 0.0
|
|
||||||
item_wise_billed_qty = get_billed_qty_against_purchase_receipt(pr_doc)
|
|
||||||
|
|
||||||
if (
|
for item in result:
|
||||||
item.billed_amt is not None
|
adjusted_amt = 0.0
|
||||||
and item.amount is not None
|
|
||||||
and item_wise_billed_qty.get(item.name)
|
|
||||||
):
|
|
||||||
adjusted_amt = (
|
|
||||||
flt(item.billed_amt / item_wise_billed_qty.get(item.name)) - flt(item.rate)
|
|
||||||
) * item.qty
|
|
||||||
|
|
||||||
adjusted_amt = flt(adjusted_amt * flt(pr_doc.conversion_rate), item.precision("amount"))
|
if item.billed_amt is not None and item.amount is not None and item_wise_billed_qty.get(item.name):
|
||||||
item.db_set("amount_difference_with_purchase_invoice", adjusted_amt, update_modified=False)
|
adjusted_amt = (
|
||||||
adjust_incoming_rate_for_pr(pr_doc)
|
flt(item.billed_amt / item_wise_billed_qty.get(item.name)) - flt(item.rate)
|
||||||
|
) * item.qty
|
||||||
|
adjusted_amt = flt(
|
||||||
|
adjusted_amt * flt(item.conversion_rate), frappe.get_precision("Purchase Receipt Item", "amount")
|
||||||
|
)
|
||||||
|
|
||||||
|
if adjusted_amt != item.amount_difference_with_purchase_invoice:
|
||||||
|
frappe.db.set_value(
|
||||||
|
"Purchase Receipt Item",
|
||||||
|
item.name,
|
||||||
|
"amount_difference_with_purchase_invoice",
|
||||||
|
adjusted_amt,
|
||||||
|
update_modified=False,
|
||||||
|
)
|
||||||
|
adjust_incoming_rate_for_pr(frappe.get_doc("Purchase Receipt", item.parent))
|
||||||
|
|
||||||
|
|
||||||
|
def get_billed_qty_against_purchase_receipt(pr_names):
|
||||||
|
table = frappe.qb.DocType("Purchase Invoice Item")
|
||||||
|
query = (
|
||||||
|
frappe.qb.from_(table)
|
||||||
|
.select(table.pr_detail, Sum(table.qty).as_("qty"))
|
||||||
|
.where((table.pr_detail.isin(pr_names)) & (table.docstatus == 1))
|
||||||
|
)
|
||||||
|
invoice_data = query.run(as_list=1)
|
||||||
|
|
||||||
|
if not invoice_data:
|
||||||
|
return frappe._dict()
|
||||||
|
return frappe._dict(invoice_data)
|
||||||
|
|||||||
Reference in New Issue
Block a user