Integration with Intuit - get accounting details for products

This commit is contained in:
Deon George 2023-05-10 12:59:42 +09:00
parent dde11f73f5
commit 17ebbb71e8
8 changed files with 147 additions and 17 deletions

View File

@ -0,0 +1,71 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;
use App\Models\{Product, ProviderOauth, Site, User};
use App\Jobs\AccountingItemSync as Job;
class AccountingItemList extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'accounting:item:list'
.' {siteid : Site ID}'
.' {provider : Provider Name}'
.' {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()
{
$site = Site::findOrFail($this->argument('siteid'));
Config::set('site',$site);
$so = ProviderOauth::where('name',$this->argument('provider'))->singleOrFail();
$uo = User::where('email',$this->argument('user'))->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));
}
}
}

View File

@ -0,0 +1,48 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use App\Models\ProviderOauth;
use App\Models\User;
class AccountingController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
/**
* Query the accounting system and get a valid list of accounting codes
*
* @param string $provider
* @return Collection
*/
public static function list(string $provider): Collection
{
$so = ProviderOauth::where('name',$provider)->singleOrFail();
// @todo This should be a variable
$uo = User::findOrFail(1);
if (($x=$so->tokens->where('user_id',$uo->id))->count() !== 1)
abort(500,sprintf('Unknown Tokens for [%s]',$uo->email));
$to = $x->pop();
$api = $so->API($to,TRUE); // @todo Remove TRUE
return $api->getItems()
->pluck('pid','FullyQualifiedName')
->transform(function($item,$value) { return ['id'=>$item,'value'=>$value]; })
->values();
}
public function webhook(Request $request)
{
Log::channel('webhook')->debug('Webhook event',['request'=>$request]);
}
}

View File

@ -10,9 +10,4 @@ class WelcomeController extends Controller
public function home() { public function home() {
return view('welcome.home'); return view('welcome.home');
} }
public function webhook(Request $request)
{
Log::channel('webhook')->debug('Webhook event',['request'=>$request]);
}
} }

View File

@ -30,11 +30,11 @@ class ProductAddEdit extends FormRequest
return [ return [
'translate.name_short' => 'required|string|min:2|max:100', 'translate.name_short' => 'required|string|min:2|max:100',
'translate.name_detail' => 'required|string|min:2|max:100', 'translate.name_detail' => 'required|string|min:2|max:100',
'translate.description' => 'required|string|min:2|max:255', 'translate.description' => 'required|string|min:2|max:65535',
'active' => 'sometimes|accepted', 'active' => 'sometimes|accepted',
'model' => 'sometimes|string', // @todo Check that it is a valid model type 'model' => 'sometimes|string', // @todo Check that it is a valid model type
'model_id' => 'sometimes|int', // @todo Check that it is a valid model type 'model_id' => 'sometimes|int', // @todo Check that it is a valid model type
'accounting' => 'nullable|string', 'accounting' => 'nullable|string', // @todo Validate that the value is in the accounting system
'pricing' => 'required|array', // @todo Validate the elements in the pricing 'pricing' => 'required|array', // @todo Validate the elements in the pricing
]; ];
} }

View File

@ -14,6 +14,7 @@ use Illuminate\Support\Facades\File;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Leenooks\Traits\ScopeActive; use Leenooks\Traits\ScopeActive;
use App\Http\Controllers\AccountingController;
use App\Interfaces\{IDs,ProductItem}; use App\Interfaces\{IDs,ProductItem};
use App\Traits\{ProductDetails,SiteID}; use App\Traits\{ProductDetails,SiteID};
@ -358,6 +359,11 @@ class Product extends Model implements IDs
return round($price,2); return round($price,2);
} }
public function accounting(string $provider): Collection
{
return AccountingController::list($provider);
}
/** /**
* Return the charge from the pricing table for the specific time period and group * Return the charge from the pricing table for the specific time period and group
* *

View File

@ -18,8 +18,15 @@ class ProviderOauth extends Model
public function accounts() public function accounts()
{ {
return $this->belongsToMany(Account::class,'account_provider') return $this->belongsToMany(Account::class,'account__provider')
->where('account_provider.site_id',$this->site_id) ->where('account__provider.site_id',$this->site_id)
->withPivot('ref','synctoken','created_at','updated_at');
}
public function products()
{
return $this->belongsToMany(Product::class,'product__provider')
->where('product__provider.site_id',$this->site_id)
->withPivot('ref','synctoken','created_at','updated_at'); ->withPivot('ref','synctoken','created_at','updated_at');
} }

View File

@ -105,13 +105,14 @@
<div class="row"> <div class="row">
<!-- Accounting --> <!-- Accounting -->
<div class="col-12"> <div class="col-12">
@include('adminlte::widget.form_text',[ @include('adminlte::widget.form_select',[
'label'=>'Accounting', 'label'=>'Accounting',
'icon'=>'fas fa-calculator', 'icon'=>'fas fa-calculator',
'id'=>'accounting', 'id'=>'accounting',
'old'=>'accounting', 'old'=>'accounting',
'name'=>'accounting', 'name'=>'accounting',
'value'=>$o->accounting ?? '', 'value'=>$o->accounting,
'options'=>$o->accounting('intuit'),
]) ])
</div> </div>
</div> </div>
@ -256,7 +257,7 @@
// Find the setup input and toggle it // Find the setup input and toggle it
input = $('#'+input.attr('id').replace('base','setup')+''); input = $('#'+input.attr('id').replace('base','setup')+'');
input.prop('disabled',(i,v)=>!v); input.prop('disabled',(i,v)=>!v);
}) });
}); });
</script> </script>
@append @append

View File

@ -1,10 +1,10 @@
<?php <?php
use App\Http\Controllers\{AdminController, use App\Http\Controllers\{AccountingController,
AdminController,
CheckoutController, CheckoutController,
ProductController, ProductController,
ResellerServicesController, ResellerServicesController};
WelcomeController};
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
@ -34,7 +34,9 @@ Route::group(['middleware'=>['auth:api','role:reseller']], function() {
Route::group(['middleware'=>'auth:api'], function() { Route::group(['middleware'=>'auth:api'], function() {
Route::post('/u/checkout/fee/{o}',[CheckoutController::class,'fee']) Route::post('/u/checkout/fee/{o}',[CheckoutController::class,'fee'])
->where('o','[0-9]+'); ->where('o','[0-9]+');
Route::any('/intuit/accounting/list',[AccountingController::class,'list']);
}); });
Route::any('/intuit/webhook',[WelcomeController::class,'webhook']) // @todo Take the specific 'intuit' out of this url, to enable other accounting methods
->where('o','[0-9]+'); Route::any('/intuit/webhook',[AccountingController::class,'webhook']);