2024-09-22 11:27:34 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\Quickbooks\Models;
|
|
|
|
|
2024-09-23 04:43:43 +02:00
|
|
|
use Carbon\Carbon;
|
2024-09-22 11:27:34 +02:00
|
|
|
use App\Models\Product;
|
2024-09-23 04:43:43 +02:00
|
|
|
use App\DataMapper\ProductSync;
|
2024-09-22 11:27:34 +02:00
|
|
|
use App\Factory\ProductFactory;
|
2024-09-22 23:27:28 +02:00
|
|
|
use App\Interfaces\SyncInterface;
|
2024-09-23 04:43:43 +02:00
|
|
|
use App\Services\Quickbooks\QuickbooksService;
|
|
|
|
use App\Services\Quickbooks\Transformers\ProductTransformer;
|
2024-09-22 11:27:34 +02:00
|
|
|
|
|
|
|
|
2024-09-22 23:27:28 +02:00
|
|
|
class QbProduct implements SyncInterface
|
2024-09-22 11:27:34 +02:00
|
|
|
{
|
2024-09-23 04:43:43 +02:00
|
|
|
protected ProductTransformer $product_transformer;
|
|
|
|
|
2024-09-22 11:27:34 +02:00
|
|
|
public function __construct(public QuickbooksService $service)
|
|
|
|
{
|
2024-09-23 04:43:43 +02:00
|
|
|
|
|
|
|
$this->product_transformer = new ProductTransformer($service->company);
|
|
|
|
|
2024-09-22 11:27:34 +02:00
|
|
|
}
|
|
|
|
|
2024-09-22 23:27:28 +02:00
|
|
|
public function find(string $id): mixed
|
2024-09-22 11:27:34 +02:00
|
|
|
{
|
|
|
|
return $this->service->sdk->FindById('Item', $id);
|
|
|
|
}
|
|
|
|
|
2024-09-22 23:27:28 +02:00
|
|
|
public function syncToNinja(array $records): void
|
2024-09-22 11:27:34 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
foreach ($records as $record) {
|
|
|
|
|
2024-09-23 04:43:43 +02:00
|
|
|
$ninja_data = $this->product_transformer->qbToNinja($record);
|
2024-09-22 11:27:34 +02:00
|
|
|
|
|
|
|
if ($product = $this->findProduct($ninja_data['id'])) {
|
|
|
|
$product->fill($ninja_data);
|
|
|
|
$product->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-09-22 23:27:28 +02:00
|
|
|
public function syncToForeign(array $records): void
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-09-22 11:27:34 +02:00
|
|
|
private function findProduct(string $key): ?Product
|
|
|
|
{
|
|
|
|
$search = Product::query()
|
|
|
|
->withTrashed()
|
|
|
|
->where('company_id', $this->service->company->id)
|
|
|
|
->where('sync->qb_id', $key);
|
|
|
|
|
|
|
|
if($search->count() == 0) {
|
|
|
|
|
|
|
|
$product = ProductFactory::create($this->service->company->id, $this->service->company->owner()->id);
|
|
|
|
|
|
|
|
$sync = new ProductSync();
|
|
|
|
$sync->qb_id = $key;
|
|
|
|
$product->sync = $sync;
|
|
|
|
|
|
|
|
return $product;
|
|
|
|
|
|
|
|
} elseif($search->count() == 1) {
|
2024-09-24 06:49:19 +02:00
|
|
|
return $this->service->syncable('product', \App\Enum\SyncDirection::PULL) ? $search->first() : null;
|
2024-09-22 11:27:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
2024-09-23 04:43:43 +02:00
|
|
|
}
|
|
|
|
|
2024-09-23 04:50:19 +02:00
|
|
|
public function sync(string $id, string $last_updated): void
|
2024-09-23 04:43:43 +02:00
|
|
|
{
|
|
|
|
$qb_record = $this->find($id);
|
|
|
|
|
2024-09-24 06:15:11 +02:00
|
|
|
if($this->service->syncable('product', \App\Enum\SyncDirection::PULL) && $ninja_record = $this->findProduct($id))
|
2024-09-23 04:43:43 +02:00
|
|
|
{
|
|
|
|
|
2024-09-23 04:50:19 +02:00
|
|
|
if(Carbon::parse($last_updated) > Carbon::parse($ninja_record->updated_at))
|
2024-09-23 04:43:43 +02:00
|
|
|
{
|
2024-09-23 04:50:19 +02:00
|
|
|
$ninja_data = $this->product_transformer->qbToNinja($qb_record);
|
2024-09-23 04:43:43 +02:00
|
|
|
|
|
|
|
$ninja_record->fill($ninja_data);
|
|
|
|
$ninja_record->save();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2024-09-22 11:27:34 +02:00
|
|
|
|
|
|
|
}
|
2024-09-23 05:36:12 +02:00
|
|
|
}
|