Skip to content

Commit

Permalink
fix(pos): loyalty points in case of returned pos invoice (#30242)
Browse files Browse the repository at this point in the history
(cherry picked from commit 7b0a97d)
  • Loading branch information
nextchamp-saqib authored and mergify-bot committed Mar 15, 2022
1 parent 5d458d6 commit 3f63934
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
4 changes: 2 additions & 2 deletions erpnext/accounts/doctype/pos_invoice/pos_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def validate(self):

def on_submit(self):
# create the loyalty point ledger entry if the customer is enrolled in any loyalty program
if self.loyalty_program:
if not self.is_return and self.loyalty_program:
self.make_loyalty_point_entry()
elif self.is_return and self.return_against and self.loyalty_program:
against_psi_doc = frappe.get_doc("POS Invoice", self.return_against)
Expand Down Expand Up @@ -88,7 +88,7 @@ def before_cancel(self):
def on_cancel(self):
# run on cancel method of selling controller
super(SalesInvoice, self).on_cancel()
if self.loyalty_program:
if not self.is_return and self.loyalty_program:
self.delete_loyalty_point_entry()
elif self.is_return and self.return_against and self.loyalty_program:
against_psi_doc = frappe.get_doc("POS Invoice", self.return_against)
Expand Down
19 changes: 13 additions & 6 deletions erpnext/accounts/doctype/sales_invoice/sales_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -1422,12 +1422,19 @@ def set_loyalty_program_tier(self):
frappe.db.set_value("Customer", self.customer, "loyalty_program_tier", lp_details.tier_name)

def get_returned_amount(self):
returned_amount = frappe.db.sql("""
select sum(grand_total)
from `tabSales Invoice`
where docstatus=1 and is_return=1 and ifnull(return_against, '')=%s
""", self.name)
return abs(flt(returned_amount[0][0])) if returned_amount else 0
from frappe.query_builder.functions import Coalesce, Sum
doc = frappe.qb.DocType(self.doctype)
returned_amount = (
frappe.qb.from_(doc)
.select(Sum(doc.grand_total))
.where(
(doc.docstatus == 1)
& (doc.is_return == 1)
& (Coalesce(doc.return_against, '') == self.name)
)
).run()

return abs(returned_amount[0][0]) if returned_amount[0][0] else 0

# redeem the loyalty points.
def apply_loyalty_points(self):
Expand Down

0 comments on commit 3f63934

Please sign in to comment.