# -*- coding: utf-8 -*- from odoo.tests.common import TransactionCase from odoo.fields import Date, Datetime import pytz class TestPOSLoyaltySubscription(TransactionCase): def setUp(self): super().setUp() # Create a product for the reward self.reward_product = self.env['product.product'].create({ 'name': 'Test Free Coffee', 'type': 'consu', }) # Create a subscription program self.program = self.env['loyalty.program'].create({ 'name': 'Makan Pagi Gratis', 'program_type': 'subscription', }) # Create a reward for the program self.reward = self.env['loyalty.reward'].create({ 'program_id': self.program.id, 'reward_type': 'product', 'required_points': 1, 'reward_product_id': self.reward_product.id, }) # Create a partner self.partner = self.env['res.partner'].create({ 'name': 'John Doe', }) # Create a subscription card self.card = self.env['loyalty.card'].create({ 'program_id': self.program.id, 'partner_id': self.partner.id, 'subscription_start_date': Date.today(), 'subscription_end_date': Date.today(), }) # Find or create a POS config and session pos_config = self.env['pos.config'].search([], limit=1) if not pos_config: pos_config = self.env['pos.config'].create({ 'name': 'Test POS Config', }) self.pos_session = self.env['pos.session'].create({ 'config_id': pos_config.id, 'user_id': self.env.uid, }) def test_program_default_values(self): """Verify that default values for subscription program are correctly applied""" self.assertTrue(self.program.manual_membership, "Subscription program should default to manual membership") self.assertEqual(self.program.trigger, 'auto', "Subscription program should trigger automatically") def test_validation_date_ranges(self): """Verify date validity checks in validate_coupon_programs""" pos_order = self.env['pos.order'].create({ 'name': 'Test POS Order', 'partner_id': self.partner.id, 'session_id': self.pos_session.id, 'amount_tax': 0.0, 'amount_total': 0.0, 'amount_paid': 0.0, 'amount_return': 0.0, }) # 1. Valid case (today is within range) point_changes = {str(self.card.id): -1} res = pos_order.validate_coupon_programs(point_changes, []) self.assertTrue(res.get('successful', False), "Validation should succeed within valid date ranges") # 2. Start date in future self.card.subscription_start_date = Date.add(Date.today(), days=1) res = pos_order.validate_coupon_programs(point_changes, []) self.assertFalse(res.get('successful', True), "Validation should fail if subscription starts in future") # Reset start date self.card.subscription_start_date = Date.today() # 3. End date in past self.card.subscription_end_date = Date.subtract(Date.today(), days=1) res = pos_order.validate_coupon_programs(point_changes, []) self.assertFalse(res.get('successful', True), "Validation should fail if subscription has expired") def test_validation_daily_limit(self): """Verify that daily claim limit is enforced""" pos_order = self.env['pos.order'].create({ 'name': 'Test POS Order', 'partner_id': self.partner.id, 'session_id': self.pos_session.id, 'amount_tax': 0.0, 'amount_total': 0.0, 'amount_paid': 0.0, 'amount_return': 0.0, }) point_changes = {str(self.card.id): -1} # Succeeds initially res = pos_order.validate_coupon_programs(point_changes, []) self.assertTrue(res.get('successful', False)) # Create history entry today self.env['loyalty.history'].create({ 'card_id': self.card.id, 'used': 1, 'description': 'Test Claim', 'create_date': Datetime.now(), }) # Should fail now res = pos_order.validate_coupon_programs(point_changes, []) self.assertFalse(res.get('successful', True), "Validation should fail if already claimed today")