Inuit sync of tax, product accounting, accounts and invoices

This commit is contained in:
Deon George
2023-05-12 20:09:51 +10:00
parent e2d8f8a096
commit ad2f6f3a7f
22 changed files with 707 additions and 144 deletions

View File

@@ -209,6 +209,13 @@ class Invoice extends Model implements IDs
return $this->hasMany(PaymentItem::class);
}
public function providers()
{
return $this->belongsToMany(ProviderOauth::class,'invoice__provider')
->where('invoice__provider.site_id',$this->site_id)
->withPivot('ref','synctoken','created_at','updated_at');
}
/* SCOPES */
/**

View File

@@ -12,4 +12,11 @@ class InvoiceItemTax extends Model
protected $table = 'invoice_item_taxes';
public $timestamps = FALSE;
/* RELATIONS */
public function tax()
{
return $this->belongsTo(Tax::class);
}
}

View File

@@ -16,7 +16,7 @@ use Leenooks\Traits\ScopeActive;
use App\Http\Controllers\AccountingController;
use App\Interfaces\{IDs,ProductItem};
use App\Traits\{ProductDetails,SiteID};
use App\Traits\{ProductDetails,ProviderRef,SiteID};
/**
* Class Product
@@ -66,7 +66,7 @@ use App\Traits\{ProductDetails,SiteID};
*/
class Product extends Model implements IDs
{
use Compoships,HasFactory,SiteID,ProductDetails,ScopeActive;
use Compoships,HasFactory,SiteID,ProductDetails,ScopeActive,ProviderRef;
protected $casts = [
'pricing'=>'collection',
@@ -106,6 +106,13 @@ class Product extends Model implements IDs
/* RELATIONS */
public function providers()
{
return $this->belongsToMany(ProviderOauth::class,'product__provider')
->where('product__provider.site_id',$this->site_id)
->withPivot('ref');
}
/**
* Which services are configured with this product
*

View File

@@ -2,6 +2,7 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use App\Traits\SiteID;
@@ -23,10 +24,17 @@ class ProviderOauth extends Model
->withPivot('ref','synctoken','created_at','updated_at');
}
public function products()
public function invoices()
{
return $this->belongsToMany(Product::class,'product__provider')
->where('product__provider.site_id',$this->site_id)
return $this->belongsToMany(Invoice::class,'invoice__provider')
->where('invoice__provider.site_id',$this->site_id)
->withPivot('ref','synctoken','created_at','updated_at');
}
public function taxes()
{
return $this->belongsToMany(Tax::class,'tax__provider')
->where('tax__provider.site_id',$this->site_id)
->withPivot('ref','synctoken','created_at','updated_at');
}
@@ -42,23 +50,30 @@ class ProviderOauth extends Model
/* METHODS */
/**
* @return string|null
*/
public function api_class(): ?string
{
return config('services.provider.'.strtolower($this->name).'.api');
}
public function API(ProviderToken $o,bool $tryprod=FALSE): mixed
/**
* Return a list of the provider OAUTH details
*/
public static function providers(): Collection
{
return ($this->api_class() && $o->access_token) ? new ($this->api_class())($o,$tryprod) : NULL;
return (new self)::whereIn('name',array_keys(config('services.provider')))->get();
}
/**
* Do we have API details for this supplier
* Return the token object for a specific user
*
* @return bool
* @param User $uo
* @return ProviderToken|null
*/
public function hasAPIdetails(): bool
public function token(User $uo): ?ProviderToken
{
return $this->api_class() && $this->access_token && (! $this->hasAccessTokenExpired());
return (($x=$this->tokens->where('user_id',$uo->id))->count() === 1) ? $x->pop() : NULL;
}
}

View File

@@ -15,10 +15,38 @@ class ProviderToken extends ProviderTokenBase
'refresh_token_expires_at',
];
protected $with = ['provider'];
/* RELATIONS */
public function provider()
{
return $this->belongsTo(ProviderOauth::class,'provider_oauth_id');
}
/* METHODS */
/**
* Return an API object to interact with this provider
*
* @return mixed
* @throws \Exception
*/
public function API(): mixed
{
if (! $this->provider->api_class() || ! $this->access_token)
throw new \Exception(sprintf('No API details for [%s]',$this->provider->name));
return new ($this->provider->api_class())($this);
}
/**
* Do we have API details for this provider
*
* @return bool
*/
public function hasAPIdetails(): bool
{
return $this->provider->api_class() && $this->access_token && (! $this->hasAccessTokenExpired());
}
}

View File

@@ -5,8 +5,12 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use App\Traits\ProviderRef;
class Tax extends Model
{
use ProviderRef;
public $timestamps = FALSE;
/* RELATIONS */
@@ -16,6 +20,12 @@ class Tax extends Model
return $this->belongsTo(Country::class);
}
public function providers()
{
return $this->belongsToMany(ProviderOauth::class,'tax__provider')
->withPivot('ref','synctoken','created_at','updated_at');
}
/* METHODS */
/**