2023-05-10 03:59:42 +00:00
|
|
|
<?php
|
|
|
|
|
2024-07-14 03:49:00 +00:00
|
|
|
namespace App\Console\Commands\Intuit;
|
2023-05-10 03:59:42 +00:00
|
|
|
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
|
2024-07-14 03:49:00 +00:00
|
|
|
use App\Models\{Product,ProviderOauth,User};
|
2023-05-10 03:59:42 +00:00
|
|
|
use App\Jobs\AccountingItemSync as Job;
|
|
|
|
|
2024-07-14 03:49:00 +00:00
|
|
|
class ItemList extends Command
|
2023-05-10 03:59:42 +00:00
|
|
|
{
|
2024-07-14 03:49:00 +00:00
|
|
|
private const provider = 'intuit';
|
|
|
|
|
2023-05-10 03:59:42 +00:00
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $signature = 'accounting:item:list'
|
2024-07-14 03:49:00 +00:00
|
|
|
.' {user? : User Email}';
|
2023-05-10 03:59:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description = 'Synchronise items with accounting system';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2024-07-14 03:49:00 +00:00
|
|
|
$uo = User::where('email',$this->argument('user') ?: config('osb.admin'))->singleOrFail();
|
2023-05-10 03:59:42 +00:00
|
|
|
|
2024-07-14 03:49:00 +00:00
|
|
|
$so = ProviderOauth::where('name',self::provider)->singleOrFail();
|
2023-05-10 03:59:42 +00:00
|
|
|
if (($x=$so->tokens->where('user_id',$uo->id))->count() !== 1)
|
|
|
|
abort(500,sprintf('Unknown Tokens for [%s]',$uo->email));
|
|
|
|
|
|
|
|
$to = $x->pop();
|
|
|
|
|
|
|
|
// Current Products used by services
|
|
|
|
$products = Product::select(['products.*'])
|
|
|
|
->distinct('products.id')
|
|
|
|
->join('services',['services.product_id'=>'products.id'])
|
|
|
|
->where('services.active',TRUE)
|
|
|
|
->get();
|
|
|
|
|
|
|
|
$api = $so->API($to,TRUE); // @todo Remove TRUE
|
|
|
|
|
|
|
|
$acc = $api->getItems()->pluck('pid','FullyQualifiedName');
|
|
|
|
|
|
|
|
foreach ($products as $po) {
|
|
|
|
if (! $po->accounting)
|
|
|
|
$this->error(sprintf('Product [%d](%s) doesnt have accounting set',$po->id,$po->name));
|
|
|
|
|
|
|
|
elseif ($acc->has($po->accounting) === FALSE)
|
|
|
|
$this->error(sprintf('Product [%d](%s) accounting [%s] doesnt exist?',$po->id,$po->name,$po->accounting));
|
|
|
|
|
|
|
|
else
|
|
|
|
$this->info(sprintf('Product [%d](%s) set to accounting [%s]',$po->id,$po->name,$po->accounting));
|
|
|
|
}
|
2024-07-14 03:49:00 +00:00
|
|
|
|
|
|
|
return self::SUCCESS;
|
2023-05-10 03:59:42 +00:00
|
|
|
}
|
|
|
|
}
|