You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

194 lines
6.2 KiB

  1. # Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors
  2. # License: GNU General Public License v3. See license.txt
  3. import frappe
  4. from frappe.utils import cint, flt, fmt_money, getdate, nowdate
  5. from erpnext.accounts.doctype.pricing_rule.pricing_rule import get_pricing_rule_for_item
  6. from erpnext.stock.doctype.batch.batch import get_batch_qty
  7. def get_web_item_qty_in_stock(item_code, item_warehouse_field, warehouse=None):
  8. in_stock, stock_qty = 0, ""
  9. template_item_code, is_stock_item = frappe.db.get_value(
  10. "Item", item_code, ["variant_of", "is_stock_item"]
  11. )
  12. if not warehouse:
  13. warehouse = frappe.db.get_value("Website Item", {"item_code": item_code}, item_warehouse_field)
  14. if not warehouse and template_item_code and template_item_code != item_code:
  15. warehouse = frappe.db.get_value(
  16. "Website Item", {"item_code": template_item_code}, item_warehouse_field
  17. )
  18. if warehouse:
  19. stock_qty = frappe.db.sql(
  20. """
  21. select GREATEST(S.actual_qty - S.reserved_qty - S.reserved_qty_for_production - S.reserved_qty_for_sub_contract, 0) / IFNULL(C.conversion_factor, 1)
  22. from tabBin S
  23. inner join `tabItem` I on S.item_code = I.Item_code
  24. left join `tabUOM Conversion Detail` C on I.sales_uom = C.uom and C.parent = I.Item_code
  25. where S.item_code=%s and S.warehouse=%s""",
  26. (item_code, warehouse),
  27. )
  28. if stock_qty:
  29. stock_qty = adjust_qty_for_expired_items(item_code, stock_qty, warehouse)
  30. in_stock = stock_qty[0][0] > 0 and 1 or 0
  31. return frappe._dict(
  32. {"in_stock": in_stock, "stock_qty": stock_qty, "is_stock_item": is_stock_item}
  33. )
  34. def adjust_qty_for_expired_items(item_code, stock_qty, warehouse):
  35. batches = frappe.get_all("Batch", filters=[{"item": item_code}], fields=["expiry_date", "name"])
  36. expired_batches = get_expired_batches(batches)
  37. stock_qty = [list(item) for item in stock_qty]
  38. for batch in expired_batches:
  39. if warehouse:
  40. stock_qty[0][0] = max(0, stock_qty[0][0] - get_batch_qty(batch, warehouse))
  41. else:
  42. stock_qty[0][0] = max(0, stock_qty[0][0] - qty_from_all_warehouses(get_batch_qty(batch)))
  43. if not stock_qty[0][0]:
  44. break
  45. return stock_qty
  46. def get_expired_batches(batches):
  47. """
  48. :param batches: A list of dict in the form [{'expiry_date': datetime.date(20XX, 1, 1), 'name': 'batch_id'}, ...]
  49. """
  50. return [b.name for b in batches if b.expiry_date and b.expiry_date <= getdate(nowdate())]
  51. def qty_from_all_warehouses(batch_info):
  52. """
  53. :param batch_info: A list of dict in the form [{u'warehouse': u'Stores - I', u'qty': 0.8}, ...]
  54. """
  55. qty = 0
  56. for batch in batch_info:
  57. qty = qty + batch.qty
  58. return qty
  59. def get_price(item_code, price_list, customer_group, company, qty=1):
  60. from erpnext.e_commerce.shopping_cart.cart import get_party
  61. template_item_code = frappe.db.get_value("Item", item_code, "variant_of")
  62. if price_list:
  63. price = frappe.get_all(
  64. "Item Price",
  65. fields=["price_list_rate", "currency"],
  66. filters={"price_list": price_list, "item_code": item_code},
  67. )
  68. if template_item_code and not price:
  69. price = frappe.get_all(
  70. "Item Price",
  71. fields=["price_list_rate", "currency"],
  72. filters={"price_list": price_list, "item_code": template_item_code},
  73. )
  74. if price:
  75. party = get_party()
  76. pricing_rule_dict = frappe._dict(
  77. {
  78. "item_code": item_code,
  79. "qty": qty,
  80. "stock_qty": qty,
  81. "transaction_type": "selling",
  82. "price_list": price_list,
  83. "customer_group": customer_group,
  84. "company": company,
  85. "conversion_rate": 1,
  86. "for_shopping_cart": True,
  87. "currency": frappe.db.get_value("Price List", price_list, "currency"),
  88. "doctype": "Quotation",
  89. }
  90. )
  91. if party and party.doctype == "Customer":
  92. pricing_rule_dict.update({"customer": party.name})
  93. pricing_rule = get_pricing_rule_for_item(pricing_rule_dict)
  94. price_obj = price[0]
  95. if pricing_rule:
  96. # price without any rules applied
  97. mrp = price_obj.price_list_rate or 0
  98. if pricing_rule.pricing_rule_for == "Discount Percentage":
  99. price_obj.discount_percent = pricing_rule.discount_percentage
  100. price_obj.formatted_discount_percent = str(flt(pricing_rule.discount_percentage, 0)) + "%"
  101. price_obj.price_list_rate = flt(
  102. price_obj.price_list_rate * (1.0 - (flt(pricing_rule.discount_percentage) / 100.0))
  103. )
  104. if pricing_rule.pricing_rule_for == "Rate":
  105. rate_discount = flt(mrp) - flt(pricing_rule.price_list_rate)
  106. if rate_discount > 0:
  107. price_obj.formatted_discount_rate = fmt_money(rate_discount, currency=price_obj["currency"])
  108. price_obj.price_list_rate = pricing_rule.price_list_rate or 0
  109. if price_obj:
  110. price_obj["formatted_price"] = fmt_money(
  111. price_obj["price_list_rate"], currency=price_obj["currency"]
  112. )
  113. if mrp != price_obj["price_list_rate"]:
  114. price_obj["formatted_mrp"] = fmt_money(mrp, currency=price_obj["currency"])
  115. price_obj["currency_symbol"] = (
  116. not cint(frappe.db.get_default("hide_currency_symbol"))
  117. and (
  118. frappe.db.get_value("Currency", price_obj.currency, "symbol", cache=True)
  119. or price_obj.currency
  120. )
  121. or ""
  122. )
  123. uom_conversion_factor = frappe.db.sql(
  124. """select C.conversion_factor
  125. from `tabUOM Conversion Detail` C
  126. inner join `tabItem` I on C.parent = I.name and C.uom = I.sales_uom
  127. where I.name = %s""",
  128. item_code,
  129. )
  130. uom_conversion_factor = uom_conversion_factor[0][0] if uom_conversion_factor else 1
  131. price_obj["formatted_price_sales_uom"] = fmt_money(
  132. price_obj["price_list_rate"] * uom_conversion_factor, currency=price_obj["currency"]
  133. )
  134. if not price_obj["price_list_rate"]:
  135. price_obj["price_list_rate"] = 0
  136. if not price_obj["currency"]:
  137. price_obj["currency"] = ""
  138. if not price_obj["formatted_price"]:
  139. price_obj["formatted_price"], price_obj["formatted_mrp"] = "", ""
  140. return price_obj
  141. def get_non_stock_item_status(item_code, item_warehouse_field):
  142. # if item is a product bundle, check if its bundle items are in stock
  143. if frappe.db.exists("Product Bundle", item_code):
  144. items = frappe.get_doc("Product Bundle", item_code).get_all_children()
  145. bundle_warehouse = frappe.db.get_value(
  146. "Website Item", {"item_code": item_code}, item_warehouse_field
  147. )
  148. return all(
  149. get_web_item_qty_in_stock(d.item_code, item_warehouse_field, bundle_warehouse).in_stock
  150. for d in items
  151. )
  152. else:
  153. return 1