2022-07-06 12:04:59 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2022-07-06 12:04:59 +02:00
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\PurchaseOrder;
|
|
|
|
|
|
|
|
use App\Models\Product;
|
|
|
|
use App\Models\PurchaseOrder;
|
|
|
|
|
|
|
|
class PurchaseOrderInventory
|
|
|
|
{
|
|
|
|
private PurchaseOrder $purchase_order;
|
|
|
|
|
|
|
|
public function __construct(PurchaseOrder $purchase_order)
|
|
|
|
{
|
|
|
|
$this->purchase_order = $purchase_order;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$line_items = $this->purchase_order->line_items;
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
foreach ($line_items as $item) {
|
2023-08-06 09:35:19 +02:00
|
|
|
$p = Product::query()->where('product_key', $item->product_key)->where('company_id', $this->purchase_order->company_id)->first();
|
2022-07-06 12:04:59 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if (!$p) {
|
2022-07-06 12:04:59 +02:00
|
|
|
continue;
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-07-06 12:04:59 +02:00
|
|
|
|
|
|
|
$p->in_stock_quantity += $item->quantity;
|
|
|
|
$p->saveQuietly();
|
|
|
|
}
|
|
|
|
|
2022-07-07 03:20:43 +02:00
|
|
|
$this->purchase_order->status_id = PurchaseOrder::STATUS_RECEIVED;
|
|
|
|
$this->purchase_order->save();
|
|
|
|
|
2022-07-06 12:04:59 +02:00
|
|
|
return $this->purchase_order;
|
|
|
|
}
|
|
|
|
}
|