51 lines
1.7 KiB
PHP
51 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Modules\Sales\Infrastructure\Adapters;
|
|
|
|
use App\Modules\Inventory\Application\DTOs\StockUseData;
|
|
use App\Modules\Inventory\Application\UseCases\RecordStockUse;
|
|
use App\Modules\Inventory\Application\UseCases\RecordStockWriteOff;
|
|
use App\Modules\Inventory\Domain\ValueObjects\StockQuantity;
|
|
use App\Modules\Sales\Application\Contracts\OrderMaterials;
|
|
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
|
|
|
/**
|
|
* Stock used in making quick sale orders, through the Inventory module.
|
|
* Reasons are stored in Finnish, like the rest of the business records.
|
|
*/
|
|
class StockForOrders implements OrderMaterials
|
|
{
|
|
public function __construct(
|
|
private RecordStockUse $recordUse,
|
|
private RecordStockWriteOff $recordWriteOff,
|
|
) {}
|
|
|
|
public function use(int $stockItemId, int $quantityMilli, int $orderLineId, ?int $machineId, int $userId): bool
|
|
{
|
|
try {
|
|
$this->recordUse->execute(new StockUseData($stockItemId, StockQuantity::inputValue($quantityMilli), machineId: $machineId, orderLineId: $orderLineId), $userId);
|
|
} catch (DomainException) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public function writeOff(int $stockItemId, int $quantityMilli, int $orderLineId, ?int $machineId, int $userId, string $reason): bool
|
|
{
|
|
try {
|
|
$this->recordWriteOff->execute(new StockUseData(
|
|
$stockItemId,
|
|
StockQuantity::inputValue($quantityMilli),
|
|
machineId: $machineId,
|
|
orderLineId: $orderLineId,
|
|
reason: __('sales.orders.reasons.'.$reason, [], 'fi'),
|
|
), $userId);
|
|
} catch (DomainException) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|