Moving accounting commands into an Intuit/ namespace, updates to intuit module

This commit is contained in:
2024-07-14 13:49:00 +10:00
parent 28aa1f9dc8
commit b486a0eac4
15 changed files with 220 additions and 316 deletions

View File

@@ -0,0 +1,58 @@
<?php
namespace App\Console\Commands\Intuit;
use Illuminate\Console\Command;
use Intuit\Jobs\AccountingCustomerUpdate;
use Intuit\Models\Customer as AccAccount;
use App\Models\{Account,ProviderOauth,User};
class AccountAdd extends Command
{
private const provider = 'intuit';
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'intuit:account:add'
.' {id : Account ID}'
.' {user? : User Email}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Add an account to quickbooks';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$uo = User::where('email',$this->argument('user') ?: config('osb.admin'))->singleOrFail();
$so = ProviderOauth::where('name',self::provider)->singleOrFail();
if (! ($to=$so->token($uo)))
abort(500,sprintf('Unknown Tokens for [%s]',$uo->email));
$o = Account::findOrFail($this->argument('id'));
$acc = new AccAccount;
$acc->PrimaryEmailAddr = (object)['Address'=>$o->user->email];
$acc->ResaleNum = $o->sid;
$acc->GivenName = $o->user->firstname;
$acc->FamilyName = $o->user->lastname;
$acc->CompanyName = $o->name;
$acc->DisplayName = $o->name;
$acc->FullyQualifiedName = $o->name;
$acc->Active = (bool)$o->active;
return AccountingCustomerUpdate::dispatchSync($to,$acc);
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace App\Console\Commands\Intuit;
use GuzzleHttp\Exception\ConnectException;
use Illuminate\Console\Command;
use Intuit\Exceptions\ConnectionIssueException;
use App\Models\{ProviderOauth,User};
class AccountGet extends Command
{
private const provider = 'intuit';
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'intuit:account:get'
.' {id : Account ID}'
.' {user? : User Email}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Get an account from quickbooks';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$uo = User::where('email',$this->argument('user') ?: config('osb.admin'))->singleOrFail();
$so = ProviderOauth::where('name',self::provider)->singleOrFail();
if (! ($to=$so->token($uo)))
abort(500,sprintf('Unknown Tokens for [%s]',$uo->email));
try {
$api = $to->API();
dump($api->getAccountQuery($this->argument('id')));
} catch (ConnectException|ConnectionIssueException $e) {
$this->error($e->getMessage());
return self::FAILURE;
}
return self::SUCCESS;
}
}

View File

@@ -0,0 +1,49 @@
<?php
namespace App\Console\Commands\Intuit;
use Illuminate\Console\Command;
use App\Models\{ProviderOauth,User};
use App\Jobs\AccountingAccountSync as Job;
/**
* Synchronise Customers with Accounts
*/
class AccountSync extends Command
{
private const provider = 'intuit';
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'intuit:account:sync'
.' {user? : User Email}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Synchronise accounts with quickbooks';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$uo = User::where('email',$this->argument('user') ?: config('osb.admin'))->singleOrFail();
$so = ProviderOauth::where('name',self::provider)->singleOrFail();
if (! ($to=$so->token($uo)))
abort(500,sprintf('Unknown Tokens for [%s]',$uo->email));
Job::dispatchSync($to);
return self::SUCCESS;
}
}

View File

@@ -0,0 +1,109 @@
<?php
namespace App\Console\Commands\Intuit;
use Illuminate\Console\Command;
use Intuit\Jobs\AccountingInvoiceUpdate;
use Intuit\Models\Invoice as AccInvoice;
use App\Models\{Invoice,ProviderOauth,User};
class InvoiceAdd extends Command
{
private const provider = 'intuit';
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'accounting:invoice:add'
.' {id : Invoice ID}'
.' {user? : User Email}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Add an invoice to the accounting provider';
/**
* Execute the console command.
*
* @return int
* @throws \Exception
*/
public function handle()
{
$uo = User::where('email',$this->argument('user') ?: config('osb.admin'))->singleOrFail();
$so = ProviderOauth::where('name',self::provider)->singleOrFail();
if (! ($to=$so->token($uo)))
abort(500,sprintf('Unknown Tokens for [%s]',$uo->email));
$o = Invoice::findOrFail($this->argument('id'));
// Check the customer exists
if ($o->account->providers->where('pivot.provider_oauth_id',$so->id)->count() !== 1)
throw new \Exception(sprintf('Account [%d] for Invoice [%d] not defined',$o->account_id,$o->id));
$ao = $o->account->providers->where('pivot.provider_oauth_id',$so->id)->pop();
// Some validation
if (! $ao->pivot->ref) {
$this->error(sprintf('Accounting not defined for account [%d]',$o->account_id));
exit(1);
}
$acc = new AccInvoice;
$acc->CustomerRef = (object)['value'=>$ao->pivot->ref];
$acc->DocNumber = $o->lid;
$acc->TxnDate = $o->created_at->format('Y-m-d');
$acc->DueDate = $o->due_at->format('Y-m-d');
$lines = collect();
$c = 0;
// @todo Group these by ItemRef and the Same Unit Price and Description, so that we can then use quantity to represent the number of them.
foreach ($o->items->groupBy(function($item) use ($so) {
return sprintf('%s.%s.%s.%s',$item->item_type_name,$item->price_base,$item->product->provider_ref($so),$item->taxes->pluck('description')->join('|'));
}) as $os)
{
$key = $os->first();
// Some validation
if (! ($ref=$key->product->provider_ref($so))) {
$this->error(sprintf('Accounting not defined in product [%d]',$key->product_id));
return self::FAILURE;
}
if ($key->taxes->count() !== 1) {
$this->error(sprintf('Cannot handle when there is not just 1 tax line [%d]',$key->id));
return self::FAILURE;
}
$c++;
$line = new \stdClass;
$line->Id = $c;
$line->DetailType = 'SalesItemLineDetail';
$line->Description = $key->item_type_name;
$line->SalesItemLineDetail = (object)[
'Qty' => $os->sum('quantity'),
'UnitPrice' => $key->price_base,
'ItemRef' => ['value'=>$ref],
// @todo It is assumed there is only 1 tax category
'TaxCodeRef' => ['value'=>$key->taxes->first()->tax->provider_ref($so)],
];
$line->Amount = $os->sum('quantity')*$key->price_base;
$lines->push($line);
}
$acc->Line = $lines;
return AccountingInvoiceUpdate::dispatchSync($to,$acc);
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace App\Console\Commands\Intuit;
use GuzzleHttp\Exception\ConnectException;
use Illuminate\Console\Command;
use Intuit\Exceptions\ConnectionIssueException;
use App\Models\{ProviderOauth,User};
class InvoiceGet extends Command
{
private const provider = 'intuit';
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'intuit:invoice:get'
.' {id : Invoice ID}'
.' {user? : User Email}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Get an invoice from the quickbooks';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$uo = User::where('email',$this->argument('user') ?: config('osb.admin'))->singleOrFail();
$so = ProviderOauth::where('name',self::provider)->singleOrFail();
if (! ($to=$so->token($uo)))
abort(500,sprintf('Unknown Tokens for [%s]',$uo->email));
try {
$api = $to->API();
dump($api->getInvoiceQuery($this->argument('id')));
} catch (ConnectException|ConnectionIssueException $e) {
$this->error($e->getMessage());
return self::FAILURE;
}
return self::SUCCESS;
}
}

View File

@@ -0,0 +1,68 @@
<?php
namespace App\Console\Commands\Intuit;
use Illuminate\Console\Command;
use App\Models\{Product,ProviderOauth,User};
use App\Jobs\AccountingItemSync as Job;
class ItemList extends Command
{
private const provider = 'intuit';
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'accounting:item:list'
.' {user? : User Email}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Synchronise items with accounting system';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$uo = User::where('email',$this->argument('user') ?: config('osb.admin'))->singleOrFail();
$so = ProviderOauth::where('name',self::provider)->singleOrFail();
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));
}
return self::SUCCESS;
}
}

View File

@@ -0,0 +1,61 @@
<?php
namespace App\Console\Commands\Intuit;
use GuzzleHttp\Exception\ConnectException;
use Illuminate\Console\Command;
use Intuit\Exceptions\ConnectionIssueException;
use App\Jobs\AccountingPaymentSync as Job;
use App\Models\{ProviderOauth,User};
class PaymentGet extends Command
{
private const provider = 'intuit';
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'accounting:payment:get'
.' {id : Payment ID}'
.' {user? : User Email}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Get a payment from the accounting provider';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$uo = User::where('email',$this->argument('user') ?: config('osb.admin'))->singleOrFail();
$so = ProviderOauth::where('name',self::provider)->singleOrFail();
if (! ($to=$so->token($uo)))
abort(500,sprintf('Unknown Tokens for [%s]',$uo->email));
try {
$api = $to->API();
$acc = $api->getPayment($this->argument('id'));
dump($acc);
} catch (ConnectException|ConnectionIssueException $e) {
$this->error($e->getMessage());
return Command::FAILURE;
}
if ($acc)
Job::dispatchSync($to,$acc);
return self::SUCCESS;
}
}

View File

@@ -0,0 +1,48 @@
<?php
namespace App\Console\Commands\Intuit;
use Illuminate\Console\Command;
use App\Models\{ProviderOauth,User};
use App\Jobs\AccountingPaymentSync as Job;
class PaymentSync extends Command
{
private const provider = 'intuit';
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'accounting:payment:sync'
.' {user? : User Email}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Synchronise payments with accounting system';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$uo = User::where('email',$this->argument('user') ?: config('osb.admin'))->singleOrFail();
$so = ProviderOauth::where('name',self::provider)->singleOrFail();
if (! ($to=$so->token($uo)))
abort(500,sprintf('Unknown Tokens for [%s]',$uo->email));
$api = $to->API();
foreach ($api->getPayments() as $acc)
Job::dispatchSync($to,$acc);
return self::SUCCESS;
}
}

View File

@@ -0,0 +1,49 @@
<?php
namespace App\Console\Commands\Intuit;
use Illuminate\Console\Command;
use App\Models\{ProviderOauth,User};
use App\Jobs\AccountingTaxSync as Job;
/**
* Synchronise TAX ids with our taxes.
*/
class TaxSync extends Command
{
private const provider = 'intuit';
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'accounting:tax:sync'
.' {user? : User Email}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Synchronise taxes with accounting system';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$uo = User::where('email',$this->argument('user') ?: config('osb.admin'))->singleOrFail();
$so = ProviderOauth::where('name',self::provider)->singleOrFail();
if (! ($to=$so->token($uo)))
abort(500,sprintf('Unknown Tokens for [%s]',$uo->email));
Job::dispatchSync($to);
return self::SUCCESS;
}
}