fix inventory
This commit is contained in:
@@ -6,11 +6,17 @@ use App\Models\PurchaseOrder;
|
||||
use App\Models\PurchaseOrderItem;
|
||||
use App\Models\Supply;
|
||||
use App\Models\SupplyTransaction;
|
||||
use App\Services\Inventory\InventoryMovementService;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use RuntimeException;
|
||||
|
||||
class PurchaseOrderReceiveService
|
||||
{
|
||||
public function __construct(
|
||||
private InventoryMovementService $inventoryMovementService
|
||||
) {
|
||||
}
|
||||
|
||||
public function receive(int $poId, array $received, string $issuedBy): array
|
||||
{
|
||||
$po = PurchaseOrder::query()->find($poId);
|
||||
@@ -51,24 +57,36 @@ class PurchaseOrderReceiveService
|
||||
$poItem->received_qty = (int) $poItem->received_qty + $toReceive;
|
||||
$poItem->save();
|
||||
|
||||
// Update supply qty_on_hand (legacy support)
|
||||
$supply = Supply::query()->find($poItem->supply_id);
|
||||
if (!$supply) {
|
||||
$completed = false;
|
||||
continue;
|
||||
if ($supply) {
|
||||
$supply->qty_on_hand = (int) $supply->qty_on_hand + $toReceive;
|
||||
$supply->save();
|
||||
|
||||
SupplyTransaction::query()->create([
|
||||
'supply_id' => $supply->id,
|
||||
'type' => 'in',
|
||||
'quantity' => $toReceive,
|
||||
'ref' => 'PO ' . ($po->po_number ?? $po->id),
|
||||
'issued_to' => 'Inventory',
|
||||
'issued_by' => $issuedBy,
|
||||
'notes' => 'Received against PO',
|
||||
]);
|
||||
}
|
||||
|
||||
$supply->qty_on_hand = (int) $supply->qty_on_hand + $toReceive;
|
||||
$supply->save();
|
||||
|
||||
SupplyTransaction::query()->create([
|
||||
'supply_id' => $supply->id,
|
||||
'type' => 'in',
|
||||
'quantity' => $toReceive,
|
||||
'ref' => 'PO ' . ($po->po_number ?? $po->id),
|
||||
'issued_to' => 'Inventory',
|
||||
'issued_by' => $issuedBy,
|
||||
'notes' => 'Received against PO',
|
||||
]);
|
||||
// Create inventory movement if the PO item is linked to an inventory item
|
||||
$inventoryItemId = $poItem->inventory_item_id;
|
||||
if ($inventoryItemId) {
|
||||
$performedBy = is_numeric($issuedBy) ? (int) $issuedBy : null;
|
||||
$this->inventoryMovementService->recordMovement(
|
||||
itemId: (int) $inventoryItemId,
|
||||
qtyChange: $toReceive,
|
||||
type: 'in',
|
||||
reason: 'Purchase order received',
|
||||
note: 'PO ' . ($po->po_number ?? $po->id),
|
||||
performedBy: $performedBy
|
||||
);
|
||||
}
|
||||
|
||||
if ($poItem->received_qty < $poItem->quantity) {
|
||||
$completed = false;
|
||||
|
||||
Reference in New Issue
Block a user