Files
thetool/application/WarehouseOffer/WarehouseOfferModel.php
2025-12-02 14:46:22 +00:00

145 lines
4.2 KiB
PHP

<?php
class WarehouseOfferModel extends TTCrudBaseModel {
public int $id;
public int $version;
public ?int $history_id;
public string $offerNumber;
public string $reference;
public string $customerNumber;
public string $customerName;
public ?string $contactPerson;
public ?string $contactPersonEmail; // New field
public string $customerStreet;
public string $customerCity;
public string $customerZip;
public ?string $customerVAT;
public int $editor;
public string $purpose;
public string $positions;
public string $alternativePositions;
public float $totalDiscount;
public string $paymentTerms;
public string $deliveryTerms;
public string $closingText;
public string $notes;
public string $status;
public ?int $validity; // New field
public ?int $lastSentDate; // New field
public float $totalAmount;
public int $create;
public int $createBy;
}
class WarehouseOfferClosingTextModel extends TTCrudBaseModel {
public int $id;
public string $name;
public string $text;
public int $createBy;
public int $create;
}
/**
* Model for managing journal entries related to a specific warehouse offer.
* It logs messages, file uploads, and status changes for an offer.
*/
class WarehouseOfferJournalModel extends TTCrudBaseModel
{
/**
* @var int The unique identifier for the journal entry.
*/
public int $id;
/**
* @var int The ID of the associated warehouse offer.
*/
public ?int $offerId;
/**
* @var string|null A JSON-encoded array of file IDs associated with this entry.
*/
public ?string $fileIds;
/**
* @var string|null The message or note for this journal entry.
*/
public ?string $message;
/**
* @var int|null The Unix timestamp when the entry was created.
*/
public ?int $create;
/**
* @var int|null The ID of the user who created the entry.
*/
public ?int $createBy;
/**
* @var string|null The name of the user who created the entry (not in DB, joined in search).
*/
public ?string $createByName;
/**
* Custom search method to include the creator's name in the results.
* This method is necessary because a JOIN with the Worker table is required,
* which is not supported by the generic getAll() method in the base model.
*
* @param array $filter
* @param array $orderBy
* @param null $limit
* @param null $offset
* @param false $count
* @return array|int
*/
public static function search(array $filter = [], array $orderBy = [], $limit = null, $offset = null, $count = false)
{
$db = self::getDB();
$tableName = self::getFullyQualifiedTable();
if ($count) {
$sql = "SELECT COUNT(*) as `count` FROM $tableName";
} else {
// Join with the Worker table to get the creator's name
$sql = "SELECT T.*, W.name as createByName FROM $tableName T LEFT JOIN `Worker` W ON T.createBy = W.id";
}
if (!empty($filter)) {
// getSQLFilter from the base model returns the complete WHERE clause.
// The columns in the filter are checked against the model's properties, so this is safe.
// The generated SQL (e.g., WHERE `offerId` = 123) will work because the column name is not ambiguous.
$sql .= " " . self::getSQLFilter($filter);
}
if (!$count && !empty($orderBy)) {
$order = [];
foreach ($orderBy as $key => $value) {
// We prefix the column with the table alias 'T' to avoid ambiguity in JOINs.
$order[] = "T.`$key` " . $db->real_escape_string($value);
}
$sql .= " ORDER BY " . implode(", ", $order);
}
if (!$count && $limit) {
$sql .= " LIMIT " . (int)$limit;
}
if (!$count && $offset) {
$sql .= " OFFSET " . (int)$offset;
}
$result = $db->query($sql);
if ($count) {
return $result->fetch_assoc()['count'];
}
$data = [];
while ($row = $result->fetch_assoc()) {
$data[] = new self($row);
}
return $data;
}
}