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
|
|
|
|
{
|
2023-09-24 22:52:14 +02:00
|
|
|
public function __construct(private PurchaseOrder $purchase_order)
|
2022-07-06 12:04:59 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2023-09-24 22:52:14 +02:00
|
|
|
$p = new Product();
|
|
|
|
$p->user_id = $this->purchase_order->user_id;
|
|
|
|
$p->company_id = $this->purchase_order->company_id;
|
|
|
|
$p->project_id = $this->purchase_order->project_id;
|
|
|
|
$p->vendor_id = $this->purchase_order->vendor_id;
|
|
|
|
$p->product_key = $item->product_key;
|
|
|
|
$p->notes = $item->notes ?? '';
|
|
|
|
$p->price = $item->cost ?? 0;
|
|
|
|
$p->quantity = $item->quantity ?? 0;
|
|
|
|
$p->custom_value1 = $item->custom_value1 ?? '';
|
|
|
|
$p->custom_value2 = $item->custom_value2 ?? '';
|
|
|
|
$p->custom_value3 = $item->custom_value3 ?? '';
|
|
|
|
$p->custom_value4 = $item->custom_value4 ?? '';
|
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;
|
|
|
|
}
|
|
|
|
}
|