Skip to content

Commit

Permalink
Merge pull request #317 from aakvatech/202-05-03
Browse files Browse the repository at this point in the history
v14.5.2
  • Loading branch information
miteshpc authored May 3, 2024
2 parents e93f203 + c39f437 commit a9ba48c
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
{
"fieldname": "description",
"fieldtype": "Data",
"fieldtype": "Small Text",
"in_list_view": 1,
"label": "Description"
},
Expand Down
242 changes: 165 additions & 77 deletions csf_tz/csf_tz/report/excise_duty_stock/excise_duty_stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def execute(filters=None):
company_currency = erpnext.get_company_currency(filters.get("company"))
else:
company_currency = frappe.db.get_single_value(
"Global Defaults", "default_currency")
"Global Defaults", "default_currency"
)

include_uom = filters.get("include_uom")
columns = get_columns(filters)
Expand All @@ -40,23 +41,23 @@ def execute(filters=None):
data = []
conversion_factors = {}

def _func(x): return x[1]
def _func(x):
return x[1]

for (company, item) in sorted(iwb_map):
for company, item in sorted(iwb_map):
if item_map.get(item):
qty_dict = iwb_map[(company, item)]

report_data = {
'currency': company_currency,
'item_code': item,
'company': company,
"currency": company_currency,
"item_code": item,
"company": company,
}
report_data.update(item_map[item])
report_data.update(qty_dict)

if include_uom:
conversion_factors.setdefault(
item, item_map[item].conversion_factor)
conversion_factors.setdefault(item, item_map[item].conversion_factor)

data.append(report_data)

Expand All @@ -67,30 +68,77 @@ def _func(x): return x[1]
def get_columns(filters):
"""return columns"""
columns = [
{"label": _("Item"), "fieldname": "item_code",
"fieldtype": "Link", "options": "Item", "width": 100},
{
"label": _("Item"),
"fieldname": "item_code",
"fieldtype": "Link",
"options": "Item",
"width": 100,
},
{"label": _("Item Name"), "fieldname": "item_name", "width": 150},
{"label": _("Item Group"), "fieldname": "item_group",
"fieldtype": "Link", "options": "Item Group", "width": 100},
{"label": _("Stock UOM"), "fieldname": "stock_uom",
"fieldtype": "Link", "options": "UOM", "width": 90},
{"label": _("Opening Qty"), "fieldname": "opening_qty",
"fieldtype": "Float", "width": 100, "convertible": "qty"},
{"label": _("In Qty"), "fieldname": "in_qty",
"fieldtype": "Float", "width": 80, "convertible": "qty"},
{"label": _("Out Qty"), "fieldname": "out_qty",
"fieldtype": "Float", "width": 80, "convertible": "qty"},
{"label": _("Balance Qty"), "fieldname": "bal_qty",
"fieldtype": "Float", "width": 100, "convertible": "qty"},
{"label": _("Excise Qty"), "fieldname": "excise_stock",
"fieldtype": "Float", "width": 100, "convertible": "qty"},
{"label": _("Company"), "fieldname": "company",
"fieldtype": "Link", "options": "Company", "width": 100}
{
"label": _("Item Group"),
"fieldname": "item_group",
"fieldtype": "Link",
"options": "Item Group",
"width": 100,
},
{
"label": _("Stock UOM"),
"fieldname": "stock_uom",
"fieldtype": "Link",
"options": "UOM",
"width": 90,
},
{
"label": _("Opening Qty"),
"fieldname": "opening_qty",
"fieldtype": "Float",
"width": 100,
"convertible": "qty",
},
{
"label": _("In Qty"),
"fieldname": "in_qty",
"fieldtype": "Float",
"width": 80,
"convertible": "qty",
},
{
"label": _("Out Qty"),
"fieldname": "out_qty",
"fieldtype": "Float",
"width": 80,
"convertible": "qty",
},
{
"label": _("Balance Qty"),
"fieldname": "bal_qty",
"fieldtype": "Float",
"width": 100,
"convertible": "qty",
},
{
"label": _("Excise Qty"),
"fieldname": "excise_stock",
"fieldtype": "Float",
"width": 100,
"convertible": "qty",
},
{
"label": _("Company"),
"fieldname": "company",
"fieldtype": "Link",
"options": "Company",
"width": 100,
},
]

if filters.get('show_variant_attributes'):
columns += [{'label': att_name, 'fieldname': att_name, 'width': 100}
for att_name in get_variants_attributes()]
if filters.get("show_variant_attributes"):
columns += [
{"label": att_name, "fieldname": att_name, "width": 100}
for att_name in get_variants_attributes()
]

return columns

Expand All @@ -102,44 +150,52 @@ def get_conditions(filters):

if filters.get("to_date"):
conditions += " and sle.posting_date <= %s" % frappe.db.escape(
filters.get("to_date"))
filters.get("to_date")
)
else:
frappe.throw(_("'To Date' is required"))

if filters.get("company"):
conditions += " and sle.company = %s" % frappe.db.escape(
filters.get("company"))
conditions += " and sle.company = %s" % frappe.db.escape(filters.get("company"))

if filters.get("warehouse"):
warehouse_details = frappe.db.get_value("Warehouse",
filters.get("warehouse"), ["lft", "rgt"], as_dict=1)
warehouse_details = frappe.db.get_value(
"Warehouse", filters.get("warehouse"), ["lft", "rgt"], as_dict=1
)
if warehouse_details:
conditions += " and exists (select name from `tabWarehouse` wh \
where wh.lft >= %s and wh.rgt <= %s and sle.warehouse = wh.name)" % (warehouse_details.lft,
warehouse_details.rgt)
conditions += (
" and exists (select name from `tabWarehouse` wh \
where wh.lft >= %s and wh.rgt <= %s and sle.warehouse = wh.name)"
% (warehouse_details.lft, warehouse_details.rgt)
)

if filters.get("warehouse_type") and not filters.get("warehouse"):
conditions += " and exists (select name from `tabWarehouse` wh \
where wh.warehouse_type = '%s' and sle.warehouse = wh.name)" % (filters.get("warehouse_type"))
conditions += (
" and exists (select name from `tabWarehouse` wh \
where wh.warehouse_type = '%s' and sle.warehouse = wh.name)"
% (filters.get("warehouse_type"))
)

return conditions


def get_stock_ledger_entries(filters, items):
item_conditions_sql = ''
item_conditions_sql = ""
if items:
item_conditions_sql = ' and sle.item_code in ({})'\
.format(', '.join([frappe.db.escape(i, percent=False) for i in items]))
item_conditions_sql = " and sle.item_code in ({})".format(
", ".join([frappe.db.escape(i, percent=False) for i in items])
)

conditions = get_conditions(filters)

return frappe.db.sql("""
return frappe.db.sql(
"""
select
sle.item_code, sle.warehouse, sle.posting_date, sle.actual_qty, sle.valuation_rate,
sle.company, sle.voucher_type, sle.qty_after_transaction, sle.stock_value_difference,
sle.item_code as name, sle.voucher_no, sle.stock_value, 0 as excise_stock
from
`tabStock Ledger Entry` sle force index (posting_sort_index)
`tabStock Ledger Entry` sle
inner join `tabStock Entry` se on sle.voucher_type = "Stock Entry" and se.name = sle.voucher_no
inner join `tabItem` i on sle.item_code = i.name
where sle.is_cancelled = 0
Expand All @@ -152,7 +208,7 @@ def get_stock_ledger_entries(filters, items):
sle.company, sle.voucher_type, sle.qty_after_transaction, sle.stock_value_difference,
sle.item_code as name, sle.voucher_no, sle.stock_value, sle.actual_qty * si.excise_duty_applicable as excise_stock
from
`tabStock Ledger Entry` sle force index (posting_sort_index)
`tabStock Ledger Entry` sle
inner join `tabSales Invoice` si on sle.voucher_type = "Sales Invoice" and si.name = sle.voucher_no
inner join `tabItem` i on sle.item_code = i.name
where sle.is_cancelled = 0
Expand All @@ -164,14 +220,23 @@ def get_stock_ledger_entries(filters, items):
sle.company, sle.voucher_type, sle.qty_after_transaction, sle.stock_value_difference,
sle.item_code as name, sle.voucher_no, sle.stock_value, 0 as excise_stock
from
`tabStock Ledger Entry` sle force index (posting_sort_index)
`tabStock Ledger Entry` sle
inner join `tabItem` i on sle.item_code = i.name
where sle.is_cancelled = 0
and sle.voucher_type NOT IN ("Stock Entry", "Sales Invoice")
and i.excisable_item = 1
and sle.docstatus < 2 %s %s
order by 3""" % # nosec
(item_conditions_sql, conditions, item_conditions_sql, conditions, item_conditions_sql, conditions), as_dict=1)
order by 3""" # nosec
% (
item_conditions_sql,
conditions,
item_conditions_sql,
conditions,
item_conditions_sql,
conditions,
),
as_dict=1,
)


def get_item_warehouse_map(filters, sle):
Expand All @@ -184,13 +249,15 @@ def get_item_warehouse_map(filters, sle):
for d in sle:
key = (d.company, d.item_code)
if key not in iwb_map:
iwb_map[key] = frappe._dict({
"opening_qty": 0.0,
"in_qty": 0.0,
"out_qty": 0.0,
"excise_stock": 0.0,
"bal_qty": 0.0
})
iwb_map[key] = frappe._dict(
{
"opening_qty": 0.0,
"in_qty": 0.0,
"out_qty": 0.0,
"excise_stock": 0.0,
"bal_qty": 0.0,
}
)

qty_dict = iwb_map[(d.company, d.item_code)]

Expand All @@ -217,7 +284,7 @@ def get_item_warehouse_map(filters, sle):


def filter_items_with_no_transactions(iwb_map, float_precision):
for (company, item) in sorted(iwb_map):
for company, item in sorted(iwb_map):
qty_dict = iwb_map[(company, item)]

no_transactions = True
Expand All @@ -239,13 +306,16 @@ def get_items(filters):
conditions.append("item.name=%(item_code)s")
else:
if filters.get("item_group"):
conditions.append(get_item_group_condition(
filters.get("item_group")))
conditions.append(get_item_group_condition(filters.get("item_group")))

items = []
if conditions:
items = frappe.db.sql_list("""select name from `tabItem` item where {}"""
.format(" and ".join(conditions)), filters)
items = frappe.db.sql_list(
"""select name from `tabItem` item where {}""".format(
" and ".join(conditions)
),
filters,
)
return items


Expand All @@ -260,52 +330,70 @@ def get_item_details(items, sle, filters):
cf_field = cf_join = ""
if filters.get("include_uom"):
cf_field = ", ucd.conversion_factor"
cf_join = "left join `tabUOM Conversion Detail` ucd on ucd.parent=item.name and ucd.uom=%s" \
cf_join = (
"left join `tabUOM Conversion Detail` ucd on ucd.parent=item.name and ucd.uom=%s"
% frappe.db.escape(filters.get("include_uom"))
)

res = frappe.db.sql("""
res = frappe.db.sql(
"""
select
item.name, item.item_name, item.description, item.item_group, item.brand, item.stock_uom %s
from
`tabItem` item
%s
where
item.name in (%s)
""" % (cf_field, cf_join, ','.join(['%s'] * len(items))), items, as_dict=1)
"""
% (cf_field, cf_join, ",".join(["%s"] * len(items))),
items,
as_dict=1,
)

for item in res:
item_details.setdefault(item.name, item)

if filters.get('show_variant_attributes', 0) == 1:
if filters.get("show_variant_attributes", 0) == 1:
variant_values = get_variant_values_for(list(item_details))
item_details = {k: v.update(variant_values.get(k, {}))
for k, v in iteritems(item_details)}
item_details = {
k: v.update(variant_values.get(k, {})) for k, v in iteritems(item_details)
}

return item_details


def validate_filters(filters):
if not (filters.get("item_code") or filters.get("warehouse")):
sle_count = flt(frappe.db.sql(
"""select count(name) from `tabStock Ledger Entry`""")[0][0])
sle_count = flt(
frappe.db.sql("""select count(name) from `tabStock Ledger Entry`""")[0][0]
)
if sle_count > 500000:
frappe.throw(
_("Please set filter based on Item or Warehouse due to a large amount of entries."))
_(
"Please set filter based on Item or Warehouse due to a large amount of entries."
)
)


def get_variants_attributes():
'''Return all item variant attributes.'''
return [i.name for i in frappe.get_all('Item Attribute')]
"""Return all item variant attributes."""
return [i.name for i in frappe.get_all("Item Attribute")]


def get_variant_values_for(items):
'''Returns variant values for items.'''
"""Returns variant values for items."""
attribute_map = {}
for attr in frappe.db.sql('''select parent, attribute, attribute_value
for attr in frappe.db.sql(
"""select parent, attribute, attribute_value
from `tabItem Variant Attribute` where parent in (%s)
''' % ", ".join(["%s"] * len(items)), tuple(items), as_dict=1):
attribute_map.setdefault(attr['parent'], {})
attribute_map[attr['parent']].update(
{attr['attribute']: attr['attribute_value']})
"""
% ", ".join(["%s"] * len(items)),
tuple(items),
as_dict=1,
):
attribute_map.setdefault(attr["parent"], {})
attribute_map[attr["parent"]].update(
{attr["attribute"]: attr["attribute_value"]}
)

return attribute_map

0 comments on commit a9ba48c

Please sign in to comment.