Skip to content

Commit

Permalink
Merge pull request #32347 from s-aga-r/refactor/stock/report/incorrec…
Browse files Browse the repository at this point in the history
…t-stock-value

refactor: rewrite `Incorrect Stock Value Report` queries in `QB`
  • Loading branch information
s-aga-r authored Sep 26, 2022
2 parents 2647018 + afb323b commit a80e872
Showing 1 changed file with 24 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import frappe
from frappe import _
from frappe.query_builder import Field
from frappe.query_builder.functions import Min, Timestamp
from frappe.utils import add_days, getdate, today

import erpnext
Expand All @@ -28,7 +30,7 @@ def execute(filters=None):
def get_unsync_date(filters):
date = filters.from_date
if not date:
date = frappe.db.sql(""" SELECT min(posting_date) from `tabStock Ledger Entry`""")
date = (frappe.qb.from_("Stock Ledger Entry").select(Min(Field("posting_date")))).run()
date = date[0][0]

if not date:
Expand All @@ -54,22 +56,27 @@ def get_data(report_filters):
result = []

voucher_wise_dict = {}
data = frappe.db.sql(
"""
SELECT
name, posting_date, posting_time, voucher_type, voucher_no,
stock_value_difference, stock_value, warehouse, item_code
FROM
`tabStock Ledger Entry`
WHERE
posting_date
= %s and company = %s
and is_cancelled = 0
ORDER BY timestamp(posting_date, posting_time) asc, creation asc
""",
(from_date, report_filters.company),
as_dict=1,
)
sle = frappe.qb.DocType("Stock Ledger Entry")
data = (
frappe.qb.from_(sle)
.select(
sle.name,
sle.posting_date,
sle.posting_time,
sle.voucher_type,
sle.voucher_no,
sle.stock_value_difference,
sle.stock_value,
sle.warehouse,
sle.item_code,
)
.where(
(sle.posting_date == from_date)
& (sle.company == report_filters.company)
& (sle.is_cancelled == 0)
)
.orderby(Timestamp(sle.posting_date, sle.posting_time), sle.creation)
).run(as_dict=True)

for d in data:
voucher_wise_dict.setdefault((d.item_code, d.warehouse), []).append(d)
Expand Down

0 comments on commit a80e872

Please sign in to comment.