fix: create entries for only PR items present in LCV (#36852)

* fix: check if item code exists in lcv before creating gle

* refactor: use qb to fetch lcv items
This commit is contained in:
Gursheen Kaur Anand 2023-08-29 09:03:31 +05:30 committed by GitHub
parent 3a2933db4d
commit 26e8b8f959
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 47 deletions

View File

@ -759,6 +759,7 @@ class PurchaseInvoice(BuyingController):
# Amount added through landed-cost-voucher # Amount added through landed-cost-voucher
if landed_cost_entries: if landed_cost_entries:
if (item.item_code, item.name) in landed_cost_entries:
for account, amount in landed_cost_entries[(item.item_code, item.name)].items(): for account, amount in landed_cost_entries[(item.item_code, item.name)].items():
gl_entries.append( gl_entries.append(
self.get_gl_dict( self.get_gl_dict(

View File

@ -6,6 +6,7 @@ import frappe
from frappe import _ from frappe import _
from frappe.model.document import Document from frappe.model.document import Document
from frappe.model.meta import get_field_precision from frappe.model.meta import get_field_precision
from frappe.query_builder.custom import ConstantColumn
from frappe.utils import flt from frappe.utils import flt
import erpnext import erpnext
@ -19,19 +20,7 @@ class LandedCostVoucher(Document):
self.set("items", []) self.set("items", [])
for pr in self.get("purchase_receipts"): for pr in self.get("purchase_receipts"):
if pr.receipt_document_type and pr.receipt_document: if pr.receipt_document_type and pr.receipt_document:
pr_items = frappe.db.sql( pr_items = get_pr_items(pr)
"""select pr_item.item_code, pr_item.description,
pr_item.qty, pr_item.base_rate, pr_item.base_amount, pr_item.name,
pr_item.cost_center, pr_item.is_fixed_asset
from `tab{doctype} Item` pr_item where parent = %s
and exists(select name from tabItem
where name = pr_item.item_code and (is_stock_item = 1 or is_fixed_asset=1))
""".format(
doctype=pr.receipt_document_type
),
pr.receipt_document,
as_dict=True,
)
for d in pr_items: for d in pr_items:
item = self.append("items") item = self.append("items")
@ -247,3 +236,30 @@ class LandedCostVoucher(Document):
), ),
tuple([item.valuation_rate] + serial_nos), tuple([item.valuation_rate] + serial_nos),
) )
def get_pr_items(purchase_receipt):
item = frappe.qb.DocType("Item")
pr_item = frappe.qb.DocType(purchase_receipt.receipt_document_type + " Item")
return (
frappe.qb.from_(pr_item)
.inner_join(item)
.on(item.name == pr_item.item_code)
.select(
pr_item.item_code,
pr_item.description,
pr_item.qty,
pr_item.base_rate,
pr_item.base_amount,
pr_item.name,
pr_item.cost_center,
pr_item.is_fixed_asset,
ConstantColumn(purchase_receipt.receipt_document_type).as_("receipt_document_type"),
ConstantColumn(purchase_receipt.receipt_document).as_("receipt_document"),
)
.where(
(pr_item.parent == purchase_receipt.receipt_document)
& ((item.is_stock_item == 1) | (item.is_fixed_asset == 1))
)
.run(as_dict=True)
)

View File

@ -470,6 +470,7 @@ class PurchaseReceipt(BuyingController):
# Amount added through landed-cos-voucher # Amount added through landed-cos-voucher
if d.landed_cost_voucher_amount and landed_cost_entries: if d.landed_cost_voucher_amount and landed_cost_entries:
if (d.item_code, d.name) in landed_cost_entries:
for account, amount in landed_cost_entries[(d.item_code, d.name)].items(): for account, amount in landed_cost_entries[(d.item_code, d.name)].items():
account_currency = get_account_currency(account) account_currency = get_account_currency(account)
credit_amount = ( credit_amount = (