| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 | 
							- <?php
 - 
 - namespace App\Listeners;
 - 
 - use App\Enums\CategoryType;
 - use App\Enums\DiscountType;
 - use App\Enums\TaxType;
 - use App\Events\CompanyDefaultEvent;
 - use App\Models\Setting\CompanyDefault;
 - use Illuminate\Support\Facades\DB;
 - 
 - class SyncWithCompanyDefaults
 - {
 -     /**
 -      * Create the event listener.
 -      */
 -     public function __construct()
 -     {
 -         //
 -     }
 - 
 -     /**
 -      * Handle the event.
 -      */
 -     public function handle(CompanyDefaultEvent $event): void
 -     {
 -         DB::transaction(function () use ($event) {
 -             $this->syncWithCompanyDefaults($event);
 -         }, 5);
 -     }
 - 
 -     private function syncWithCompanyDefaults($event): void
 -     {
 -         $model = $event->model;
 - 
 -         if (! $model->getAttribute('enabled') || ! auth()->check() || ! auth()->user()->currentCompany) {
 -             return;
 -         }
 - 
 -         $companyId = auth()->user()->currentCompany->id;
 - 
 -         if (! $companyId) {
 -             return;
 -         }
 - 
 -         $this->updateCompanyDefaults($model, $companyId);
 -     }
 - 
 -     private function updateCompanyDefaults($model, $companyId): void
 -     {
 -         $modelName = class_basename($model);
 -         $type = $model->getAttribute('type');
 - 
 -         $default = CompanyDefault::firstOrNew([
 -             'company_id' => $companyId,
 -         ]);
 - 
 -         match ($modelName) {
 -             'Discount' => $this->handleDiscount($default, $type, $model->getKey()),
 -             'Tax' => $this->handleTax($default, $type, $model->getKey()),
 -             'Category' => $this->handleCategory($default, $type, $model->getKey()),
 -             'Currency' => $default->currency_code = $model->getAttribute('code'),
 -             'Account' => $default->account_id = $model->getKey(),
 -             default => null,
 -         };
 - 
 -         $default->save();
 -     }
 - 
 -     private function handleDiscount($default, $type, $key): void
 -     {
 -         if (! in_array($type, [DiscountType::Sales, DiscountType::Purchase], true)) {
 -             return;
 -         }
 - 
 -         match (true) {
 -             $type === DiscountType::Sales => $default->sales_discount_id = $key,
 -             $type === DiscountType::Purchase => $default->purchase_discount_id = $key,
 -         };
 -     }
 - 
 -     private function handleTax($default, $type, $key): void
 -     {
 -         if (! in_array($type, [TaxType::Sales, TaxType::Purchase], true)) {
 -             return;
 -         }
 - 
 -         match (true) {
 -             $type === TaxType::Sales => $default->sales_tax_id = $key,
 -             $type === TaxType::Purchase => $default->purchase_tax_id = $key,
 -         };
 -     }
 - 
 -     private function handleCategory($default, $type, $key): void
 -     {
 -         if (! in_array($type, [CategoryType::Income, CategoryType::Expense], true)) {
 -             return;
 -         }
 - 
 -         match (true) {
 -             $type === CategoryType::Income => $default->income_category_id = $key,
 -             $type === CategoryType::Expense => $default->expense_category_id = $key,
 -         };
 -     }
 - }
 
 
  |