Files
opensourcepos/tests/Models/ReceivingTest.php
objecttothis c365e6e6dd feat(jobs): add batch-level import context and queue pause support
- Add import_batch_contexts table plus ImportBatchContext model so
  per-batch data (e.g. Items CSV attribute definitions) is stored
  once and referenced by context_id, instead of duplicated into
  every queued row's job payload.
- Extend queue_jobs, queue_jobs_failed, and new queue_paused_jobs
  tables with batch_id/context_id columns and FKs (ON DELETE
  RESTRICT) back to import_batch_contexts, so context rows cannot
  be purged while any job still references them.
- Introduce ImportBatchService.pushRow() to centralize queuing a
  batch row with batch/context stamping and priority; update
  Customers and Items controllers to use it instead of calling
  importBatch->create() and queue->push() separately.
- Add queue_paused_jobs table and Jobs::postPauseJobs()/pauseQueues()
  to move pending jobs out of queue_jobs into a paused table per
  queue (all core queues or a selected subset), leaving in-flight
  jobs untouched (issue #3833 pause/resume design).
- Change Jobs::getEditJob()/postSaveJob() routes to accept separate
  source/id path segments instead of a combined uid string.
- Add test coverage for ImportBatchContext, ImportBatchService, and
  ImportBatch (including purge-retains-while-referenced guards).

Signed-off-by: objecttothis <17935339+objecttothis@users.noreply.github.com>
2026-10-07 17:55:22 +04:00

109 lines
3.2 KiB
PHP

<?php
namespace Tests\Models;
use App\Models\Receiving;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use Tests\Support\EmployeeFixtureTrait;
use Tests\Support\ItemFixtureTrait;
/**
* Regression tests: Receiving::delete_value() must
* correctly reverse the stock quantity change it applied via
* Item_quantity::changeQuantity(), using the same atomic upsert as the
* sale-checkout and sale-cancel paths.
*/
class ReceivingTest extends CIUnitTestCase
{
use DatabaseTestTrait;
use EmployeeFixtureTrait;
use ItemFixtureTrait;
protected $migrate = true;
protected $migrateOnce = true;
protected $refresh = true;
protected $namespace = 'App';
private const LOCATION_ID = 1;
protected function setUp(): void
{
parent::setUp();
}
protected function createSupplier(): int
{
$db = \Config\Database::connect();
$db->table('people')->insert([
'first_name' => 'Test',
'last_name' => 'Supplier',
'phone_number' => '555-0400',
'email' => 'supplier-' . uniqid() . '@test.com',
'address_1' => '',
'address_2' => '',
'city' => '',
'state' => '',
'zip' => '',
'country' => '',
'comments' => '',
]);
$personId = (int) $db->insertID();
$db->table('suppliers')->insert([
'person_id' => $personId,
'deleted' => 0,
]);
return $personId;
}
protected function buildReceivingLine(int $itemId, float $quantity, float $costPrice): array
{
return [
0 => [
'item_id' => $itemId,
'line' => 0,
'description' => 'Test Item',
'serialnumber' => '',
'quantity' => $quantity,
'receiving_quantity' => 1,
'discount' => 0,
'discount_type' => 0,
'price' => $costPrice,
'item_location' => self::LOCATION_ID,
],
];
}
public function testDeleteValueRestoresStockAfterReceiving(): void
{
$employeeId = $this->createEmployee();
$supplierId = $this->createSupplier();
$itemId = $this->createTestItem(HAS_STOCK);
// Deliberately no pre-existing item_quantities row for this item/location.
$receivingModel = model(Receiving::class);
$items = $this->buildReceivingLine($itemId, 5, 1.00);
$receivingId = $receivingModel->save_value(
$items,
$supplierId,
$employeeId,
'test receiving',
'REF-' . uniqid(),
'Cash'
);
$this->assertGreaterThan(0, $receivingId);
$this->assertEqualsWithDelta(5.0, $this->getItemQuantity($itemId, self::LOCATION_ID), 0.001);
$deleteResult = $receivingModel->delete_value($receivingId, $employeeId, true);
$this->assertTrue($deleteResult);
$this->assertEqualsWithDelta(0.0, $this->getItemQuantity($itemId, self::LOCATION_ID), 0.001);
}
}