Files
opensourcepos/tests/Libraries/Sale_libPaymentTest.php
objecttothis 9c542efaf6 Feature: Payment reference code (#4587)
* Add `reference_code` to sale payment queries and group by statements

Signed-off-by: Travis Garrison <travis@chiraqbookstore.com>

* Refactor `Sales` controller to improve payment handling readability and replace snake_case with camelCase variables

Signed-off-by: Travis Garrison <travis@chiraqbookstore.com>

* Add missing translations for `Sales` language file and include new keys like `must_enter_rrn` and `reference_code`

Signed-off-by: Travis Garrison <travis@chiraqbookstore.com>

* Refactor `Sales` payment handling to use camelCase and extend `addPayment` with `referenceCode` support

Signed-off-by: Travis Garrison <travis@chiraqbookstore.com>

* Refactor `Sales` models, controllers, and libraries to adopt camelCase naming conventions and improve readability

Signed-off-by: Travis Garrison <travis@chiraqbookstore.com>

* Add translations and updates for `must_enter_reference_code` and `reference_code` across language files and update `Sales` controller to replace `must_enter_rrn` with the new key

Signed-off-by: Travis Garrison <travis@chiraqbookstore.com>

* feat(sales): add reference code input and payment type helper

- Add `get_reference_code_payment_types()` to locale_helper as single
  source of truth for card-requiring payment types
- Add reference code row to register view, shown/hidden via JS based
  on selected payment type
- Fix payment type dropdown width to 100% for consistent layout
- Add min-width to payment buttons and right-padding to button group

Signed-off-by: Travis Garrison <travis@chiraqbookstore.com>

* feat(config): add payment reference code length configuration

- Add payment_reference_code_min and payment_reference_code_max fields
  to Config controller save logic
- Add translation keys for reference code length limits across all
  language files (min/max label + section header)
- Align array key formatting in Config controller for readability

Signed-off-by: Travis Garrison <travis@chiraqbookstore.com>

* style(lang): normalize string delimiters to single quotes across all language files

Convert double-quoted array keys and values to single quotes in all
app/Language/*/Config.php and app/Language/*/Sales.php variants.
No translation content changed — formatting only.

Also add Localization section to AGENTS.md documenting language file
conventions for new keys and fallback behavior.

Signed-off-by: Travis Garrison <travis@chiraqbookstore.com>

* feat(lang): add payment reference code length translations

Add localized strings for payment_reference_code_length_limits,
payment_reference_code_length_max_label, and
payment_reference_code_length_min_label across all supported locales.

Signed-off-by: Travis Garrison <travis@chiraqbookstore.com>

* test(config,sales): add payment reference code min/max validation tests

- Add baseLocalePayload() helper in ConfigTest for postSaveLocale tests
- Add testSaveLocale_AcceptsValidReferenceCodeMinMax and related boundary tests
- Add Sale_libPaymentTest for payment reference code validation in Sale_lib

Signed-off-by: Travis Garrison <travis@chiraqbookstore.com>

* fix(sales): add type-specific validation for amount_tendered

Gift card payments use amount_tendered as giftcard number (integer);
cash/other payments require decimal_locale format. Apply correct
validation rule per payment type instead of generic required.

Signed-off-by: Travis Garrison <travis@chiraqbookstore.com>

* fix(lang): replace self-closing </br> with <br> in all locales

Signed-off-by: Travis Garrison <travis@chiraqbookstore.com>

* fix(sales): use configurable precision in discount comparison

Replace hardcoded precision 2 with totals_decimals() when comparing
discount against item total via bccomp/bcmul, so discount validation
respects the configured decimal precision setting.

Signed-off-by: Travis Garrison <travis@chiraqbookstore.com>

* fix(lang): correct Azerbaijani translations in Config.php

Replace placeholder/mismatched strings with accurate translations:
- email_mailpath, email_smtp_pass, invoice_email_message
- number_locale_invalid/required, receipt_template
- reward_configuration, right, tax_decimals, theme

Signed-off-by: Travis Garrison <travis@chiraqbookstore.com>

* feat(sales): add reference code support to payment edit flow

- Add reference_code field to new payment row in sale edit form
- Persist reference_code on insert in Sale model
- Validate reference_code_new in Sales controller using configurable min/max length rules

Signed-off-by: Travis Garrison <travis@chiraqbookstore.com>

* feat(lang): add Georgian (ka) language stubs for Config and Sales

Signed-off-by: Travis Garrison <travis@chiraqbookstore.com>

* Match the fallback maximum reference_code length to the maximum of the field in the db

Signed-off-by: Travis Garrison <travis@chiraqbookstore.com>

* Bug fixes

- Add validation of UI settings to prevent overridden values from being passed.
- Correct maximum value in JS for payment_reference_code maximum length to 40.
- Fix bug causing copy_entire_sale() to incorrectly copy the reference code and cash_adjustment

Signed-off-by: Travis Garrison <travis@chiraqbookstore.com>

---------

Signed-off-by: Travis Garrison <travis@chiraqbookstore.com>
Co-authored-by: Travis Garrison <travis@chiraqbookstore.com>
2026-07-10 13:49:05 +04:00

190 lines
6.0 KiB
PHP

<?php
namespace Tests\Libraries;
use App\Libraries\Sale_lib;
use App\Models\Attribute;
use App\Models\Customer;
use App\Models\Dinner_table;
use App\Models\Item;
use App\Models\Item_kit_items;
use App\Models\Item_quantity;
use App\Models\Item_taxes;
use App\Models\Sale;
use App\Models\Stock_location;
use CodeIgniter\Config\Factories;
use CodeIgniter\Test\CIUnitTestCase;
use Config\OSPOS;
class Sale_libPaymentTest extends CIUnitTestCase
{
private Sale_lib $saleLib;
protected function setUp(): void
{
parent::setUp();
// Inject mock OSPOS config so Sale_lib constructor and helpers don't need real settings
$ospos = new OSPOS();
$ospos->settings = [
'cash_rounding_code' => '',
'cash_decimals' => 2,
'currency_decimals' => 2,
'tax_decimals' => 2,
'quantity_decimals' => 2,
];
Factories::injectMock('config', OSPOS::class, $ospos);
// Inject stub models so model() calls in Sale_lib::__construct() skip DB
$stubMethods = ['__construct'];
foreach ([
Attribute::class,
Customer::class,
Dinner_table::class,
Item::class,
Item_kit_items::class,
Item_quantity::class,
Item_taxes::class,
Sale::class,
Stock_location::class,
] as $modelClass) {
$mock = $this->getMockBuilder($modelClass)
->disableOriginalConstructor()
->getMock();
Factories::injectMock('models', $modelClass, $mock);
}
session()->destroy();
$this->saleLib = new Sale_lib();
}
protected function tearDown(): void
{
Factories::reset();
parent::tearDown();
}
// ========== getPayments / setPayments ==========
public function testGetPaymentsReturnsEmptyArrayInitially(): void
{
$payments = $this->saleLib->getPayments();
$this->assertIsArray($payments);
$this->assertEmpty($payments);
}
public function testSetPaymentsPersistsToSession(): void
{
$data = [
'cash' => [
'payment_type' => 'cash',
'payment_amount' => '10.00',
'cash_refund' => 0,
'cash_adjustment' => CASH_ADJUSTMENT_FALSE,
'reference_code' => null,
]
];
$this->saleLib->setPayments($data);
$this->assertSame($data, $this->saleLib->getPayments());
}
// ========== addPayment ==========
public function testAddPaymentCreatesNewEntry(): void
{
$this->saleLib->addPayment('credit', '25.00', 'ABC123');
$payments = $this->saleLib->getPayments();
$this->assertArrayHasKey('credit', $payments);
$this->assertSame('credit', $payments['credit']['payment_type']);
$this->assertSame('25.00', $payments['credit']['payment_amount']);
$this->assertSame(0, $payments['credit']['cash_refund']);
$this->assertSame(CASH_ADJUSTMENT_FALSE, $payments['credit']['cash_adjustment']);
}
public function testAddPaymentStoresReferenceCode(): void
{
$this->saleLib->addPayment('debit', '50.00', 'REF9876');
$payments = $this->saleLib->getPayments();
$this->assertSame('REF9876', $payments['debit']['reference_code']);
}
public function testAddPaymentNullReferenceCodeStoredAsNull(): void
{
$this->saleLib->addPayment('cash', '15.00');
$payments = $this->saleLib->getPayments();
$this->assertNull($payments['cash']['reference_code']);
}
public function testAddPaymentAccumulatesAmountForExistingId(): void
{
$this->saleLib->addPayment('credit', '10.00', 'REF001');
$this->saleLib->addPayment('credit', '5.00', 'REF001');
$payments = $this->saleLib->getPayments();
// bcadd strips trailing zeros: '10.00' + '5.00' = '15'
$this->assertSame('15', $payments['credit']['payment_amount']);
}
public function testAddPaymentCashAdjustmentFlagStored(): void
{
$this->saleLib->addPayment('cash_adjustment', '0.05', null, CASH_ADJUSTMENT_TRUE);
$payments = $this->saleLib->getPayments();
$this->assertSame(CASH_ADJUSTMENT_TRUE, $payments['cash_adjustment']['cash_adjustment']);
}
public function testAddPaymentMultipleDistinctTypesAllStored(): void
{
$this->saleLib->addPayment('credit', '30.00', 'REF1');
$this->saleLib->addPayment('debit', '20.00', 'REF2');
$payments = $this->saleLib->getPayments();
$this->assertCount(2, $payments);
$this->assertArrayHasKey('credit', $payments);
$this->assertArrayHasKey('debit', $payments);
}
// ========== edit_payment ==========
public function testEditPaymentUpdatesAmount(): void
{
$this->saleLib->addPayment('credit', '10.00', 'REF001');
$result = $this->saleLib->edit_payment('credit', 99.99);
$this->assertTrue($result);
$payments = $this->saleLib->getPayments();
$this->assertSame(99.99, $payments['credit']['payment_amount']);
}
public function testEditPaymentReturnsFalseForMissingId(): void
{
$result = $this->saleLib->edit_payment('nonexistent', 10.00);
$this->assertFalse($result);
}
// ========== delete_payment ==========
public function testDeletePaymentRemovesEntry(): void
{
$this->saleLib->addPayment('credit', '25.00', 'REF999');
$this->saleLib->delete_payment('credit');
$payments = $this->saleLib->getPayments();
$this->assertArrayNotHasKey('credit', $payments);
}
public function testDeletePaymentLeavesOtherEntriesIntact(): void
{
$this->saleLib->addPayment('credit', '25.00', 'REF1');
$this->saleLib->addPayment('debit', '10.00', 'REF2');
$this->saleLib->delete_payment('credit');
$payments = $this->saleLib->getPayments();
$this->assertArrayNotHasKey('credit', $payments);
$this->assertArrayHasKey('debit', $payments);
}
}