Update client tax data when updating the client record

This commit is contained in:
David Bomba 2023-05-16 07:43:26 +10:00
parent 44b0bcd019
commit 269fbea1f0
5 changed files with 96 additions and 75 deletions

View File

@ -152,6 +152,7 @@ class BaseRule implements RuleInterface
$this->client->saveQuietly();
nlog('Automatic tax calculations not supported for this country - defaulting to company country');
}
/** Harvest the client_region */

View File

@ -46,74 +46,6 @@ class EmailController extends BaseController
parent::__construct();
}
/**
* Returns a template filled with entity variables.
*
* @param SendEmailRequest $request
* @return Response
*
* @OA\Post(
* path="/api/v1/emails",
* operationId="sendEmailTemplate",
* tags={"emails"},
* summary="Sends an email for an entity",
* description="Sends an email for an entity",
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\RequestBody(
* description="The template subject and body",
* required=true,
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* type="object",
* @OA\Property(
* property="subject",
* description="The email subject",
* type="string",
* ),
* @OA\Property(
* property="body",
* description="The email body",
* type="string",
* ),
* @OA\Property(
* property="entity",
* description="The entity name",
* type="string",
* ),
* @OA\Property(
* property="entity_id",
* description="The entity_id",
* type="string",
* ),
* @OA\Property(
* property="template",
* description="The template required",
* type="string",
* ),
* )
* )
* ),
* @OA\Response(
* response=200,
* description="success",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/Template"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function send(SendEmailRequest $request)
{
$entity = $request->input('entity');

View File

@ -0,0 +1,78 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Jobs\Company;
use App\DataProviders\USStates;
use App\Models\Client;
use App\Models\Company;
use App\Libraries\MultiDB;
use Illuminate\Bus\Queueable;
use App\Utils\Traits\MakesHash;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class UpdateTaxData implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
use MakesHash;
public $tries = 1;
/**
* Create a new job instance.
*
* @param Client $client
* @param Company $company
*/
public function __construct(public Client $client, protected Company $company)
{
}
/**
* Execute the job.
*
*/
public function handle()
{
MultiDB::setDb($this->company->db);
if(!config('services.tax.zip_tax.key'))
return;
$tax_provider = new \App\Services\Tax\Providers\TaxProvider($this->company, $this->client);
try {
$tax_provider->updateClientTaxData();
if (!$this->client->state && $this->client->postal_code) {
$this->client->state = USStates::getState($this->client->postal_code);
$this->client->save();
}
}catch(\Exception $e){
nlog("problem getting tax data => ".$e->getMessage());
}
}
}

View File

@ -11,9 +11,10 @@
namespace App\Observers;
use App\Jobs\Util\WebhookHandler;
use App\Models\Client;
use App\Models\Webhook;
use App\Jobs\Util\WebhookHandler;
use App\Jobs\Company\UpdateTaxData;
class ClientObserver
{
@ -27,6 +28,11 @@ class ClientObserver
*/
public function created(Client $client)
{
if ($client->country_id == 840 && $client->company->calculate_taxes) {
UpdateTaxData::dispatch($client, $client->company);
}
$subscriptions = Webhook::where('company_id', $client->company_id)
->where('event_id', Webhook::EVENT_CREATE_CLIENT)
->exists();
@ -44,6 +50,11 @@ class ClientObserver
*/
public function updated(Client $client)
{
if($client->getOriginal('postal_code') != $client->postal_code && $client->country_id == 840 && $client->company->calculate_taxes)
{
UpdateTaxData::dispatch($client, $client->company);
}
$event = Webhook::EVENT_UPDATE_CLIENT;
if ($client->getOriginal('deleted_at') && !$client->deleted_at) {
@ -53,8 +64,7 @@ class ClientObserver
if ($client->is_deleted) {
$event = Webhook::EVENT_DELETE_CLIENT;
}
$subscriptions = Webhook::where('company_id', $client->company_id)
->where('event_id', $event)
->exists();

View File

@ -59,16 +59,16 @@ class TaxConfigTest extends TestCase
'address1' => '400 Evelyn Pl',
'city' => 'Beverley Hills',
'state' => '',
'postal_code' => 90210,
'postal_code' => '',
'country_id' => 840,
]);
$this->assertEquals('CA', USStates::getState('90210'));
// $this->assertEquals('CA', USStates::getState('90210'));
// $this->bootApi($client);
$this->bootApi($client);
// $this->tp->updateClientTaxData();
$this->tp->updateClientTaxData();
}