Files
opensourcepos/tests/Models/ReceivingTest.php
T
objecttothis c004a3f412 feat(items): add server-side "export all" download for items table
Add ability to export the items manage-table results (respecting
current search/filters/sort) to downloadable files in multiple
formats, instead of only the on-screen page.

- app/Controllers/Items.php: new getExportAll(format) endpoint that
  re-runs the current search/filter query, builds raw export rows via
  getItemExportRow(), times db/build/write phases for debug logging,
  and streams back either a PDF response or a generic file download.
- app/Helpers/export_helper.php (new): shared writer dispatch
  (generateExportFile) supporting csv, txt, json, xml, sql, xlsx and
  pdf formats, with per-format value coercion (currency/quantity/
  numeric columns), XLSX temp-file caching with stale-file sweeping,
  and SQL INSERT generation keyed off the target table name.
- app/Helpers/tabular_helper.php: add itemExportHeaders() (manage
  table columns minus UI-only icon columns) and getItemExportRow()
  (unformatted raw values for export consumers); rename
  expand_attribute_values() to expandAttributeValues() and add a
  $raw flag to skip locale decimal formatting for exports.
- app/Controllers/Reports.php: update call sites for the
  expandAttributeValues() rename.
- app/Config/Autoload.php: autoload new 'export' helper.
- app/Config/Mimes.php: register .sql mime type for export downloads.
- app/Controllers/Items.php: getSearch() now defaults missing
  'search' param to '' instead of null.

Signed-off-by: objecttothis <17935339+objecttothis@users.noreply.github.com>
2026-10-13 15:17:53 +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 = false;
protected $namespace = null;
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);
}
}