75 lines
2.2 KiB
PHP
75 lines
2.2 KiB
PHP
<?php
|
|
|
|
class WarehouseStocktakeModel extends TTCrudBaseModel {
|
|
public int $id;
|
|
public ?string $stocktakeNumber = null;
|
|
public string $title;
|
|
public ?string $description = null;
|
|
public int $warehouseLocationId;
|
|
public string $status = 'planned';
|
|
public ?int $startedAt = null;
|
|
public ?int $completedAt = null;
|
|
public ?int $startedBy = null;
|
|
public ?int $completedBy = null;
|
|
public int $totalItems = 0;
|
|
public int $totalScannedItems = 0;
|
|
public ?string $notes = null;
|
|
public int $createBy;
|
|
public int $create;
|
|
|
|
/**
|
|
* Generate next stocktake number (ST-YYYY-NNNN)
|
|
*/
|
|
public static function generateStocktakeNumber(): string {
|
|
$year = date('Y');
|
|
$prefix = "IN{$year}-X";
|
|
|
|
$db = FronkDB::singleton();
|
|
$result = $db->query("SELECT stocktakeNumber FROM WarehouseStocktake
|
|
WHERE stocktakeNumber LIKE '{$prefix}%'
|
|
ORDER BY stocktakeNumber DESC LIMIT 1");
|
|
|
|
if ($row = $result->fetch_assoc()) {
|
|
$lastNumber = intval(substr($row['stocktakeNumber'], -6));
|
|
$nextNumber = $lastNumber + 1;
|
|
} else {
|
|
$nextNumber = 1;
|
|
}
|
|
|
|
return $prefix . str_pad((string)$nextNumber, 6, '0', STR_PAD_LEFT);
|
|
}
|
|
|
|
/**
|
|
* Get location object
|
|
*/
|
|
public function getLocation(): ?WarehouseLocationModel {
|
|
return WarehouseLocationModel::get($this->warehouseLocationId);
|
|
}
|
|
|
|
/**
|
|
* Get user who started the stocktake
|
|
*/
|
|
public function getStartedByUser(): ?UserModel {
|
|
if (!$this->startedBy) return null;
|
|
return UserModel::get($this->startedBy);
|
|
}
|
|
|
|
/**
|
|
* Get items for this stocktake
|
|
*/
|
|
public function getItems(): array {
|
|
return WarehouseStocktakeItemModel::getAll(['stocktakeId' => $this->id]);
|
|
}
|
|
|
|
/**
|
|
* Update progress counters
|
|
*/
|
|
public function updateProgress(): void {
|
|
$items = $this->getItems();
|
|
$this->totalScannedItems = count($items);
|
|
|
|
$db = FronkDB::singleton();
|
|
$db->query("UPDATE WarehouseStocktake SET totalScannedItems = {$this->totalScannedItems} WHERE id = {$this->id}");
|
|
}
|
|
}
|