Skip to content

Commit

Permalink
test: loyalty points redemption from shopping cart
Browse files Browse the repository at this point in the history
(cherry picked from commit 74fb070)
  • Loading branch information
blaggacao authored and mergify[bot] committed Sep 5, 2024
1 parent 9a19650 commit a4eed8a
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -3711,6 +3711,65 @@ def test_partial_allocation_on_advance_as_liability(self):
check_gl_entries(self, pe.name, expected_gle, nowdate(), voucher_type="Payment Entry")
set_advance_flag(company="_Test Company", flag=0, default_account="")

def test_loyalty_points_redemption_with_shopping_cart(self):
from erpnext.accounts.doctype.loyalty_program.test_loyalty_program import (
create_records,
create_sales_invoice_record,
)
from erpnext.selling.doctype.sales_order.sales_order import make_sales_invoice
from erpnext.selling.doctype.sales_order.test_sales_order import make_sales_order

# Set up loyalty program
create_records()
frappe.db.set_value("Customer", "Test Loyalty Customer", "loyalty_program", "Test Single Loyalty")
create_sales_invoice_record(10).insert().submit()

# Create a sales order
so = make_sales_order(qty=10, do_not_save=True, customer="Test Loyalty Customer")
so.name = "_T-Sales Order LP-0001"
so.order_type = "Shopping Cart"
so.loyalty_points = 50
so.loyalty_amount = 50
so.insert()
so.submit()

# Create sales invoice from the sales order
si = make_sales_invoice(so.name)
from frappe.model.trace import traced_field_context

with traced_field_context(si.__class__, "loyalty_program", forbidden_values=[None]):
si.insert()
si.submit()

# Check if loyalty points are applied correctly
self.assertEqual(si.loyalty_program, "Test Single Loyalty")
self.assertEqual(si.loyalty_points, 50)
self.assertEqual(si.loyalty_amount, 50)

# Check GL entries for loyalty points redemption
gl_entries = frappe.get_all(
"GL Entry",
filters={"voucher_type": "Sales Invoice", "voucher_no": si.name},
fields=["account", "debit", "credit"],
)

loyalty_account = frappe.db.get_value("Loyalty Program", "Test Single Loyalty", "expense_account")
expected_gl_entries = [
{"account": si.debit_to, "debit": si.grand_total, "credit": 0},
{"account": si.items[0].income_account, "debit": 0, "credit": si.net_total},
{"account": loyalty_account, "debit": 50, "credit": 0},
]

for entry in expected_gl_entries:
self.assertTrue(
any(
gl_entry.account == entry["account"]
and gl_entry.debit == entry["debit"]
and gl_entry.credit == entry["credit"]
for gl_entry in gl_entries
)
)

def test_pulling_advance_based_on_debit_to(self):
from erpnext.accounts.doctype.payment_entry.test_payment_entry import create_payment_entry

Expand Down

0 comments on commit a4eed8a

Please sign in to comment.