2019-12-26 23:33:07 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-12-26 23:33:07 +01:00
|
|
|
*
|
|
|
|
* @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)
|
2019-12-26 23:33:07 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-12-26 23:33:07 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Jobs\Product;
|
|
|
|
|
2019-12-29 07:28:57 +01:00
|
|
|
use App\Libraries\MultiDB;
|
2019-12-26 23:33:07 +01:00
|
|
|
use App\Models\Product;
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
|
|
class UpdateOrCreateProduct implements ShouldQueue
|
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
2020-11-03 14:57:48 +01:00
|
|
|
public $products;
|
2019-12-26 23:33:07 +01:00
|
|
|
|
2020-11-03 14:57:48 +01:00
|
|
|
public $invoice;
|
2019-12-26 23:33:07 +01:00
|
|
|
|
2020-11-03 14:57:48 +01:00
|
|
|
public $company;
|
2019-12-29 07:28:57 +01:00
|
|
|
|
2023-01-19 03:20:05 +01:00
|
|
|
public $deleteWhenMissingModels = true;
|
|
|
|
|
2019-12-26 23:33:07 +01:00
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param $products
|
|
|
|
* @param $invoice
|
|
|
|
* @param $company
|
2019-12-26 23:33:07 +01:00
|
|
|
*/
|
2019-12-29 07:28:57 +01:00
|
|
|
public function __construct($products, $invoice, $company)
|
2019-12-26 23:33:07 +01:00
|
|
|
{
|
|
|
|
$this->products = $products;
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-12-26 23:33:07 +01:00
|
|
|
$this->invoice = $invoice;
|
|
|
|
|
2019-12-29 07:28:57 +01:00
|
|
|
$this->company = $company;
|
2019-12-26 23:33:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-12-26 23:33:07 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2019-12-29 07:28:57 +01:00
|
|
|
MultiDB::setDB($this->company->db);
|
2021-01-23 05:42:27 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (strval($this->invoice->client->getSetting('currency_id')) != strval($this->company->settings->currency_id)) {
|
2021-04-05 23:36:20 +02:00
|
|
|
return;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2021-01-23 05:42:27 +01:00
|
|
|
* If the invoice was generated from a Task or Expense then
|
|
|
|
* we do NOT update the product details this short block we
|
|
|
|
* check for the presence of a task_id and/or expense_id
|
2022-06-21 11:57:17 +02:00
|
|
|
*/
|
|
|
|
$expense_count = count(array_column((array) $this->products, 'expense_id'));
|
|
|
|
$task_count = count(array_column((array) $this->products, 'task_id'));
|
2021-01-23 05:42:27 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($task_count >= 1 || $expense_count >= 1) {
|
2021-01-23 05:42:27 +01:00
|
|
|
return;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-01-23 05:42:27 +01:00
|
|
|
|
2020-11-03 14:57:48 +01:00
|
|
|
//only update / create products - not tasks or gateway fees
|
|
|
|
$updateable_products = collect($this->products)->filter(function ($item) {
|
|
|
|
return $item->type_id == 1;
|
|
|
|
});
|
|
|
|
|
2023-08-07 07:30:34 +02:00
|
|
|
/** @var \App\DataMapper\InvoiceItem $item */
|
2020-11-03 14:57:48 +01:00
|
|
|
foreach ($updateable_products as $item) {
|
2020-09-06 11:38:10 +02:00
|
|
|
if (empty($item->product_key)) {
|
2020-05-12 11:56:30 +02:00
|
|
|
continue;
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-05-12 11:56:30 +02:00
|
|
|
|
2020-11-25 01:23:39 +01:00
|
|
|
$product = Product::withTrashed()->firstOrNew(['product_key' => $item->product_key, 'company_id' => $this->invoice->company->id]);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
/* If a user is using placeholders in their descriptions, do not update the products */
|
|
|
|
$string_hit = false;
|
|
|
|
|
|
|
|
foreach ([':MONTH',':YEAR',':QUARTER',':WEEK'] as $string) {
|
|
|
|
if (stripos($product->notes, $string) !== false) {
|
|
|
|
$string_hit = true;
|
2022-10-21 00:05:37 +02:00
|
|
|
}
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-10-21 00:05:37 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($string_hit) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-10-21 00:05:37 +02:00
|
|
|
|
2019-12-26 23:33:07 +01:00
|
|
|
$product->product_key = $item->product_key;
|
2019-12-29 07:28:57 +01:00
|
|
|
$product->notes = isset($item->notes) ? $item->notes : '';
|
2020-02-25 10:17:04 +01:00
|
|
|
$product->price = isset($item->cost) ? $item->cost : 0;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
|
|
|
if (! $product->id) {
|
2021-05-03 12:50:08 +02:00
|
|
|
$product->quantity = isset($item->quantity) ? $item->quantity : 0;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if (isset($item->custom_value1) && strlen($item->custom_value1) >=1) {
|
2022-12-15 02:16:50 +01:00
|
|
|
$product->custom_value1 = $item->custom_value1;
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-12-15 02:16:50 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if (isset($item->custom_value2) && strlen($item->custom_value1) >=1) {
|
2022-12-15 02:16:50 +01:00
|
|
|
$product->custom_value2 = $item->custom_value2;
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-12-15 02:16:50 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if (isset($item->custom_value3) && strlen($item->custom_value1) >=1) {
|
2022-12-15 02:16:50 +01:00
|
|
|
$product->custom_value3 = $item->custom_value3;
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-12-15 02:16:50 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if (isset($item->custom_value4) && strlen($item->custom_value1) >=1) {
|
2022-12-15 02:16:50 +01:00
|
|
|
$product->custom_value4 = $item->custom_value4;
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-12-15 02:16:50 +01:00
|
|
|
|
2019-12-26 23:33:07 +01:00
|
|
|
$product->user_id = $this->invoice->user_id;
|
|
|
|
$product->company_id = $this->invoice->company_id;
|
|
|
|
$product->project_id = $this->invoice->project_id;
|
|
|
|
$product->vendor_id = $this->invoice->vendor_id;
|
2023-01-31 15:06:21 +01:00
|
|
|
$product->saveQuietly();
|
2019-12-26 23:33:07 +01:00
|
|
|
}
|
|
|
|
}
|
2020-11-25 01:23:39 +01:00
|
|
|
|
|
|
|
public function failed($exception = null)
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
info('update create failed with = ');
|
2020-11-25 15:19:52 +01:00
|
|
|
info(print_r($exception->getMessage(), 1));
|
2020-11-25 01:23:39 +01:00
|
|
|
}
|
2019-12-26 23:33:07 +01:00
|
|
|
}
|