1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Services/PurchaseOrder/PurchaseOrderInventory.php
2023-02-16 12:36:09 +11:00

47 lines
1.1 KiB
PHP

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
* @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;
foreach ($line_items as $item) {
$p = Product::where('product_key', $item->product_key)->where('company_id', $this->purchase_order->company_id)->first();
if (!$p) {
continue;
}
$p->in_stock_quantity += $item->quantity;
$p->saveQuietly();
}
$this->purchase_order->status_id = PurchaseOrder::STATUS_RECEIVED;
$this->purchase_order->save();
return $this->purchase_order;
}
}