mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-26 16:34:46 +00:00
Merge pull request #46354 from mihir-kandoi/46351
fix: recalculate_amount_difference_field patch
This commit is contained in:
@@ -1,12 +1,16 @@
|
|||||||
import frappe
|
import frappe
|
||||||
from frappe.query_builder.functions import Sum
|
from frappe.query_builder.functions import Sum
|
||||||
from frappe.utils import flt
|
from frappe.utils import flt, getdate
|
||||||
|
|
||||||
from erpnext.accounts.utils import get_fiscal_year
|
from erpnext.accounts.utils import get_fiscal_year
|
||||||
from erpnext.stock.doctype.purchase_receipt.purchase_receipt import adjust_incoming_rate_for_pr
|
from erpnext.stock.doctype.purchase_receipt.purchase_receipt import adjust_incoming_rate_for_pr
|
||||||
|
|
||||||
|
|
||||||
def execute():
|
def execute():
|
||||||
|
if not frappe.db.get_single_value("Buying Settings", "set_landed_cost_based_on_purchase_invoice_rate"):
|
||||||
|
return
|
||||||
|
|
||||||
|
for company in frappe.get_all("Company", pluck="name"):
|
||||||
table = frappe.qb.DocType("Purchase Receipt Item")
|
table = frappe.qb.DocType("Purchase Receipt Item")
|
||||||
parent = frappe.qb.DocType("Purchase Receipt")
|
parent = frappe.qb.DocType("Purchase Receipt")
|
||||||
query = (
|
query = (
|
||||||
@@ -23,10 +27,45 @@ def execute():
|
|||||||
table.qty,
|
table.qty,
|
||||||
parent.conversion_rate,
|
parent.conversion_rate,
|
||||||
)
|
)
|
||||||
.where((table.amount_difference_with_purchase_invoice != 0) & (table.docstatus == 1))
|
.where(
|
||||||
|
(table.amount_difference_with_purchase_invoice != 0)
|
||||||
|
& (table.docstatus == 1)
|
||||||
|
& (parent.company == company)
|
||||||
)
|
)
|
||||||
if fiscal_year_dates := get_fiscal_year(frappe.utils.datetime.date.today(), raise_on_missing=False):
|
)
|
||||||
query.where(parent.posting_date.between(fiscal_year_dates[1], fiscal_year_dates[2]))
|
|
||||||
|
posting_date = "2024-04-01"
|
||||||
|
|
||||||
|
# Get the last accounting period end date
|
||||||
|
accounting_period = frappe.get_all(
|
||||||
|
"Accounting Period", {"company": company}, ["end_date"], order_by="end_date desc", limit=1
|
||||||
|
)
|
||||||
|
if (
|
||||||
|
accounting_period
|
||||||
|
and accounting_period[0].end_date
|
||||||
|
and getdate(accounting_period[0].end_date) > getdate(posting_date)
|
||||||
|
):
|
||||||
|
posting_date = accounting_period[0].end_date
|
||||||
|
|
||||||
|
# Get the last period closing voucher end date
|
||||||
|
period_closing_voucher = frappe.get_all(
|
||||||
|
"Period Closing Voucher",
|
||||||
|
{"company": company, "docstatus": 1},
|
||||||
|
["period_end_date"],
|
||||||
|
order_by="period_end_date desc",
|
||||||
|
limit=1,
|
||||||
|
)
|
||||||
|
if (
|
||||||
|
period_closing_voucher
|
||||||
|
and period_closing_voucher[0].period_end_date
|
||||||
|
and getdate(period_closing_voucher[0].period_end_date) > getdate(posting_date)
|
||||||
|
):
|
||||||
|
posting_date = period_closing_voucher[0].period_end_date
|
||||||
|
|
||||||
|
fiscal_year = get_fiscal_year(frappe.utils.datetime.date.today(), raise_on_missing=False)
|
||||||
|
if fiscal_year and getdate(fiscal_year[1]) > getdate(posting_date):
|
||||||
|
posting_date = fiscal_year[1]
|
||||||
|
query = query.where(parent.posting_date > posting_date)
|
||||||
|
|
||||||
if result := query.run(as_dict=True):
|
if result := query.run(as_dict=True):
|
||||||
item_wise_billed_qty = get_billed_qty_against_purchase_receipt([item.name for item in result])
|
item_wise_billed_qty = get_billed_qty_against_purchase_receipt([item.name for item in result])
|
||||||
|
|||||||
Reference in New Issue
Block a user