1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 21:52:35 +01:00
invoiceninja/app/Services/PurchaseOrder/PurchaseOrderInventory.php

56 lines
1.8 KiB
PHP
Raw Normal View History

<?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)
*
* @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)
{
}
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();
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
}
$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();
return $this->purchase_order;
}
}