refactor: convert CTE validation to python (#29324)

Some old servers running on mariadb <10.2 can't work with CTEs hence
reverting to basic python code

Co-authored-by: Ankush Menat <ankush@frappe.io>
This commit is contained in:
mergify[bot]
2022-01-18 11:17:32 +05:30
committed by GitHub
parent d5d7f67ac2
commit d7f16c4924

View File

@@ -7,7 +7,7 @@ import json
import frappe import frappe
from frappe import _ from frappe import _
from frappe.model.meta import get_field_precision from frappe.model.meta import get_field_precision
from frappe.utils import cint, cstr, flt, get_link_to_form, getdate, now, nowdate from frappe.utils import cint, cstr, flt, get_datetime, get_link_to_form, getdate, now, nowdate
from six import iteritems from six import iteritems
import erpnext import erpnext
@@ -1139,26 +1139,31 @@ def get_future_sle_with_negative_qty(args):
def get_future_sle_with_negative_batch_qty(args): def get_future_sle_with_negative_batch_qty(args):
return frappe.db.sql(""" batch_ledger = frappe.db.sql("""
with batch_ledger as ( select
select posting_date, posting_time, voucher_type, voucher_no, actual_qty
posting_date, posting_time, voucher_type, voucher_no, from `tabStock Ledger Entry`
sum(actual_qty) over (order by posting_date, posting_time, creation) as cumulative_total
from `tabStock Ledger Entry`
where
item_code = %(item_code)s
and warehouse = %(warehouse)s
and batch_no=%(batch_no)s
and is_cancelled = 0
order by posting_date, posting_time, creation
)
select * from batch_ledger
where where
cumulative_total < 0.0 item_code = %(item_code)s
and timestamp(posting_date, posting_time) >= timestamp(%(posting_date)s, %(posting_time)s) and warehouse = %(warehouse)s
limit 1 and batch_no=%(batch_no)s
and is_cancelled = 0
order by timestamp(posting_date, posting_time), creation
""", args, as_dict=1) """, args, as_dict=1)
cumulative_total = 0.0
current_posting_datetime = get_datetime(str(args.posting_date) + " " + str(args.posting_time))
for entry in batch_ledger:
cumulative_total += entry.actual_qty
if cumulative_total > -1e-6:
continue
if (get_datetime(str(entry.posting_date) + " " + str(entry.posting_time))
>= current_posting_datetime):
entry.cumulative_total = cumulative_total
return [entry]
def _round_off_if_near_zero(number: float, precision: int = 6) -> float: def _round_off_if_near_zero(number: float, precision: int = 6) -> float:
""" Rounds off the number to zero only if number is close to zero for decimal """ Rounds off the number to zero only if number is close to zero for decimal