Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

SyncWithCompanyDefaults.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace App\Listeners;
  3. use App\Enums\CategoryType;
  4. use App\Enums\DiscountType;
  5. use App\Enums\TaxType;
  6. use App\Events\CompanyDefaultEvent;
  7. use App\Models\Setting\CompanyDefault;
  8. use Illuminate\Support\Facades\DB;
  9. class SyncWithCompanyDefaults
  10. {
  11. /**
  12. * Create the event listener.
  13. */
  14. public function __construct()
  15. {
  16. //
  17. }
  18. /**
  19. * Handle the event.
  20. */
  21. public function handle(CompanyDefaultEvent $event): void
  22. {
  23. DB::transaction(function () use ($event) {
  24. $this->syncWithCompanyDefaults($event);
  25. }, 5);
  26. }
  27. private function syncWithCompanyDefaults($event): void
  28. {
  29. $model = $event->model;
  30. if (! $model->getAttribute('enabled') || ! auth()->check() || ! auth()->user()->currentCompany) {
  31. return;
  32. }
  33. $companyId = auth()->user()->currentCompany->id;
  34. if (! $companyId) {
  35. return;
  36. }
  37. $this->updateCompanyDefaults($model, $companyId);
  38. }
  39. private function updateCompanyDefaults($model, $companyId): void
  40. {
  41. $modelName = class_basename($model);
  42. $type = $model->getAttribute('type');
  43. $default = CompanyDefault::firstOrNew([
  44. 'company_id' => $companyId,
  45. ]);
  46. match ($modelName) {
  47. 'Discount' => $this->handleDiscount($default, $type, $model->getKey()),
  48. 'Tax' => $this->handleTax($default, $type, $model->getKey()),
  49. 'Category' => $this->handleCategory($default, $type, $model->getKey()),
  50. 'Currency' => $default->currency_code = $model->getAttribute('code'),
  51. 'BankAccount' => $default->bank_account_id = $model->getKey(),
  52. default => null,
  53. };
  54. $default->save();
  55. }
  56. private function handleDiscount($default, $type, $key): void
  57. {
  58. if (! in_array($type, [DiscountType::Sales, DiscountType::Purchase], true)) {
  59. return;
  60. }
  61. match (true) {
  62. $type === DiscountType::Sales => $default->sales_discount_id = $key,
  63. $type === DiscountType::Purchase => $default->purchase_discount_id = $key,
  64. };
  65. }
  66. private function handleTax($default, $type, $key): void
  67. {
  68. if (! in_array($type, [TaxType::Sales, TaxType::Purchase], true)) {
  69. return;
  70. }
  71. match (true) {
  72. $type === TaxType::Sales => $default->sales_tax_id = $key,
  73. $type === TaxType::Purchase => $default->purchase_tax_id = $key,
  74. };
  75. }
  76. private function handleCategory($default, $type, $key): void
  77. {
  78. if (! in_array($type, [CategoryType::Income, CategoryType::Expense], true)) {
  79. return;
  80. }
  81. match (true) {
  82. $type === CategoryType::Income => $default->income_category_id = $key,
  83. $type === CategoryType::Expense => $default->expense_category_id = $key,
  84. };
  85. }
  86. }