Explorar el Código

wip: Accounting Module

3.x
wallo hace 2 años
padre
commit
ad2b86abbe

app/Actions/Banking/CreateCurrencyFromAccount.php → app/Actions/OptionAction/CreateCurrency.php Ver fichero

@@ -1,17 +1,19 @@
1 1
 <?php
2 2
 
3
-namespace App\Actions\Banking;
3
+namespace App\Actions\OptionAction;
4 4
 
5 5
 use App\Models\Setting\Currency;
6 6
 use Illuminate\Support\Facades\Auth;
7 7
 
8
-class CreateCurrencyFromAccount
8
+class CreateCurrency
9 9
 {
10 10
     public function create(string $code, string $name, string $rate): Currency
11 11
     {
12 12
         $companyId = Auth::user()->currentCompany->id;
13 13
 
14
-        $hasDefaultCurrency = Currency::where('company_id', $companyId)->where('enabled', true)->exists();
14
+        $defaultCurrency = Currency::getDefaultCurrency();
15
+
16
+        $hasDefaultCurrency = $defaultCurrency !== null;
15 17
 
16 18
         return Currency::create([
17 19
             'name' => $name,
@@ -26,4 +28,4 @@ class CreateCurrencyFromAccount
26 28
             'company_id' => $companyId,
27 29
         ]);
28 30
     }
29
-}
31
+}

+ 2
- 3
app/Filament/Pages/Companies.php Ver fichero

@@ -2,7 +2,6 @@
2 2
 
3 3
 namespace App\Filament\Pages;
4 4
 
5
-use App\Filament\Pages\Widgets\Companies\Charts\CumulativeTotal;
6 5
 use Filament\Pages\Page;
7 6
 use Illuminate\Support\Facades\Auth;
8 7
 use Wallo\FilamentCompanies\FilamentCompanies;
@@ -15,12 +14,12 @@ class Companies extends Page
15 14
 
16 15
     protected static function shouldRegisterNavigation(): bool
17 16
     {
18
-        return Auth::user()->currentCompany->name === 'ERPSAAS';
17
+        return Auth::user()->currentCompany->id === 1;
19 18
     }
20 19
 
21 20
     public function mount(): void
22 21
     {
23
-        abort_unless(Auth::user()->currentCompany->name === 'ERPSAAS', 403);
22
+        abort_unless(Auth::user()->currentCompany->id === 1, 403);
24 23
     }
25 24
 
26 25
     protected function getHeaderWidgets(): array

+ 2
- 2
app/Filament/Pages/Employees.php Ver fichero

@@ -14,12 +14,12 @@ class Employees extends Page
14 14
 
15 15
     protected static function shouldRegisterNavigation(): bool
16 16
     {
17
-        return Auth::user()->currentCompany->name === 'ERPSAAS';
17
+        return Auth::user()->currentCompany->id === 1;
18 18
     }
19 19
 
20 20
     public function mount(): void
21 21
     {
22
-        abort_unless(Auth::user()->currentCompany->name === 'ERPSAAS', 403);
22
+        abort_unless(Auth::user()->currentCompany->id === 1, 403);
23 23
     }
24 24
 
25 25
     protected function getHeaderWidgets(): array

+ 2
- 2
app/Filament/Pages/Users.php Ver fichero

@@ -14,12 +14,12 @@ class Users extends Page
14 14
 
15 15
     protected static function shouldRegisterNavigation(): bool
16 16
     {
17
-        return Auth::user()->currentCompany->name === 'ERPSAAS';
17
+        return Auth::user()->currentCompany->id === 1;
18 18
     }
19 19
 
20 20
     public function mount(): void
21 21
     {
22
-        abort_unless(Auth::user()->currentCompany->name === 'ERPSAAS', 403);
22
+        abort_unless(Auth::user()->currentCompany->id === 1, 403);
23 23
     }
24 24
 
25 25
     protected function getHeaderWidgets(): array

+ 22
- 14
app/Filament/Resources/AccountResource.php Ver fichero

@@ -2,11 +2,12 @@
2 2
 
3 3
 namespace App\Filament\Resources;
4 4
 
5
-use App\Actions\Banking\CreateCurrencyFromAccount;
5
+use App\Actions\OptionAction\CreateCurrency;
6 6
 use App\Filament\Resources\AccountResource\Pages;
7 7
 use App\Filament\Resources\AccountResource\RelationManagers;
8 8
 use App\Models\Banking\Account;
9 9
 use App\Models\Setting\Currency;
10
+use App\Services\CurrencyService;
10 11
 use Closure;
11 12
 use Exception;
12 13
 use Filament\Forms;
@@ -33,7 +34,8 @@ class AccountResource extends Resource
33 34
 
34 35
     public static function getEloquentQuery(): Builder
35 36
     {
36
-        return parent::getEloquentQuery()->where('company_id', Auth::user()->currentCompany->id);
37
+        return parent::getEloquentQuery()
38
+            ->where('company_id', Auth::user()->currentCompany->id);
37 39
     }
38 40
 
39 41
     public static function form(Form $form): Form
@@ -89,9 +91,24 @@ class AccountResource extends Resource
89 91
                                             ->options(Currency::getCurrencyCodes())
90 92
                                             ->reactive()
91 93
                                             ->afterStateUpdated(static function (callable $set, $state) {
94
+                                                if ($state === null) {
95
+                                                    return;
96
+                                                }
97
+
92 98
                                                 $code = $state;
93
-                                                $name = config("money.{$code}.name");
94
-                                                $set('currency.name', $name);
99
+                                                $currencyConfig = config("money.{$code}", []);
100
+                                                $currencyService = app(CurrencyService::class);
101
+
102
+                                                $defaultCurrency = Currency::getDefaultCurrency();
103
+
104
+                                                $rate = 1;
105
+
106
+                                                if ($defaultCurrency !== null) {
107
+                                                    $rate = $currencyService->getCachedExchangeRate($defaultCurrency, $code);
108
+                                                }
109
+
110
+                                                $set('currency.name', $currencyConfig['name'] ?? '');
111
+                                                $set('currency.rate', $rate);
95 112
                                             })
96 113
                                             ->required(),
97 114
                                         Forms\Components\TextInput::make('currency.name')
@@ -101,15 +118,6 @@ class AccountResource extends Resource
101 118
                                         Forms\Components\TextInput::make('currency.rate')
102 119
                                             ->label('Rate')
103 120
                                             ->numeric()
104
-                                            ->mask(static fn (Forms\Components\TextInput\Mask $mask) => $mask
105
-                                                ->numeric()
106
-                                                ->decimalPlaces(4)
107
-                                                ->signed(false)
108
-                                                ->padFractionalZeros(false)
109
-                                                ->normalizeZeros(false)
110
-                                                ->minValue(0.0001)
111
-                                                ->maxValue(999999.9999)
112
-                                                ->lazyPlaceholder(false))
113 121
                                             ->required(),
114 122
                                     ])->createOptionAction(static function (Forms\Components\Actions\Action $action) {
115 123
                                         return $action
@@ -122,7 +130,7 @@ class AccountResource extends Resource
122 130
                                                     $name = $data['currency']['name'];
123 131
                                                     $rate = $data['currency']['rate'];
124 132
 
125
-                                                    return (new CreateCurrencyFromAccount())->create($code, $name, $rate);
133
+                                                    return (new CreateCurrency())->create($code, $name, $rate);
126 134
                                                 });
127 135
                                             });
128 136
                                     }),

+ 8
- 0
app/Filament/Resources/CategoryResource.php Ver fichero

@@ -4,6 +4,8 @@ namespace App\Filament\Resources;
4 4
 
5 5
 use App\Filament\Resources\CategoryResource\Pages;
6 6
 use App\Filament\Resources\CategoryResource\RelationManagers;
7
+use Illuminate\Database\Eloquent\Builder;
8
+use Illuminate\Support\Facades\Auth;
7 9
 use Wallo\FilamentSelectify\Components\ToggleButton;
8 10
 use App\Models\Setting\Category;
9 11
 use Exception;
@@ -23,6 +25,12 @@ class CategoryResource extends Resource
23 25
 
24 26
     protected static ?string $navigationGroup = 'Settings';
25 27
 
28
+    public static function getEloquentQuery(): Builder
29
+    {
30
+        return parent::getEloquentQuery()
31
+            ->where('company_id', Auth::user()->currentCompany->id);
32
+    }
33
+
26 34
     public static function form(Form $form): Form
27 35
     {
28 36
         return $form

+ 2
- 2
app/Filament/Resources/CurrencyResource.php Ver fichero

@@ -18,7 +18,6 @@ use Filament\Tables;
18 18
 use Illuminate\Database\Eloquent\Builder;
19 19
 use Illuminate\Support\Collection;
20 20
 use Illuminate\Support\Facades\Auth;
21
-use Illuminate\Support\Facades\Cache;
22 21
 use Wallo\FilamentSelectify\Components\ToggleButton;
23 22
 
24 23
 class CurrencyResource extends Resource
@@ -31,7 +30,8 @@ class CurrencyResource extends Resource
31 30
 
32 31
     public static function getEloquentQuery(): Builder
33 32
     {
34
-        return parent::getEloquentQuery()->where('company_id', Auth::user()->currentCompany->id);
33
+        return parent::getEloquentQuery()
34
+            ->where('company_id', Auth::user()->currentCompany->id);
35 35
     }
36 36
 
37 37
     public static function form(Form $form): Form

+ 21
- 14
app/Filament/Resources/CustomerResource.php Ver fichero

@@ -2,10 +2,11 @@
2 2
 
3 3
 namespace App\Filament\Resources;
4 4
 
5
-use App\Actions\Banking\CreateCurrencyFromAccount;
5
+use App\Actions\OptionAction\CreateCurrency;
6 6
 use App\Filament\Resources\CustomerResource\Pages;
7 7
 use App\Filament\Resources\CustomerResource\RelationManagers;
8 8
 use App\Models\Setting\Currency;
9
+use App\Services\CurrencyService;
9 10
 use Wallo\FilamentSelectify\Components\ButtonGroup;
10 11
 use App\Models\Contact;
11 12
 use Filament\Forms;
@@ -101,9 +102,24 @@ class CustomerResource extends Resource
101 102
                                     ->options(Currency::getCurrencyCodes())
102 103
                                     ->reactive()
103 104
                                     ->afterStateUpdated(static function (callable $set, $state) {
105
+                                        if ($state === null) {
106
+                                            return;
107
+                                        }
108
+
104 109
                                         $code = $state;
105
-                                        $name = config("money.{$code}.name");
106
-                                        $set('currency.name', $name);
110
+                                        $currencyConfig = config("money.{$code}", []);
111
+                                        $currencyService = app(CurrencyService::class);
112
+
113
+                                        $defaultCurrency = Currency::getDefaultCurrency();
114
+
115
+                                        $rate = 1;
116
+
117
+                                        if ($defaultCurrency !== null) {
118
+                                            $rate = $currencyService->getCachedExchangeRate($defaultCurrency, $code);
119
+                                        }
120
+
121
+                                        $set('currency.name', $currencyConfig['name'] ?? '');
122
+                                        $set('currency.rate', $rate);
107 123
                                     })
108 124
                                     ->required(),
109 125
                                 Forms\Components\TextInput::make('currency.name')
@@ -113,15 +129,6 @@ class CustomerResource extends Resource
113 129
                                 Forms\Components\TextInput::make('currency.rate')
114 130
                                     ->label('Rate')
115 131
                                     ->numeric()
116
-                                    ->mask(static fn (Forms\Components\TextInput\Mask $mask) => $mask
117
-                                        ->numeric()
118
-                                        ->decimalPlaces(4)
119
-                                        ->signed(false)
120
-                                        ->padFractionalZeros(false)
121
-                                        ->normalizeZeros(false)
122
-                                        ->minValue(0.0001)
123
-                                        ->maxValue(999999.9999)
124
-                                        ->lazyPlaceholder(false))
125 132
                                     ->required(),
126 133
                             ])->createOptionAction(static function (Forms\Components\Actions\Action $action) {
127 134
                                 return $action
@@ -134,11 +141,11 @@ class CustomerResource extends Resource
134 141
                                             $name = $data['currency']['name'];
135 142
                                             $rate = $data['currency']['rate'];
136 143
 
137
-                                            return (new CreateCurrencyFromAccount())->create($code, $name, $rate);
144
+                                            return (new CreateCurrency())->create($code, $name, $rate);
138 145
                                         });
139 146
                                     });
140 147
                             }),
141
-                ])->columns(2),
148
+                    ])->columns(),
142 149
                 Forms\Components\Section::make('Address')
143 150
                     ->schema([
144 151
                         Forms\Components\TextInput::make('address')

+ 7
- 6
app/Filament/Resources/DiscountResource.php Ver fichero

@@ -5,7 +5,6 @@ namespace App\Filament\Resources;
5 5
 use App\Filament\Resources\DiscountResource\Pages;
6 6
 use App\Filament\Resources\DiscountResource\RelationManagers;
7 7
 use App\Models\Setting\Discount;
8
-use App\Models\Setting\Tax;
9 8
 use Filament\Forms;
10 9
 use Filament\Forms\Components\TextInput\Mask;
11 10
 use Filament\Resources\Form;
@@ -13,7 +12,7 @@ use Filament\Resources\Resource;
13 12
 use Filament\Resources\Table;
14 13
 use Filament\Tables;
15 14
 use Illuminate\Database\Eloquent\Builder;
16
-use Illuminate\Database\Eloquent\SoftDeletingScope;
15
+use Illuminate\Support\Facades\Auth;
17 16
 use Wallo\FilamentSelectify\Components\ToggleButton;
18 17
 
19 18
 class DiscountResource extends Resource
@@ -24,6 +23,12 @@ class DiscountResource extends Resource
24 23
 
25 24
     protected static ?string $navigationGroup = 'Settings';
26 25
 
26
+    public static function getEloquentQuery(): Builder
27
+    {
28
+        return parent::getEloquentQuery()
29
+            ->where('company_id', Auth::user()->currentCompany->id);
30
+    }
31
+
27 32
     public static function form(Form $form): Form
28 33
     {
29 34
         return $form
@@ -128,10 +133,6 @@ class DiscountResource extends Resource
128 133
                     ->formatStateUsing(static function (Discount $record) {
129 134
                         $rate = $record->rate;
130 135
 
131
-                        if (str_contains($rate, '.')) {
132
-                            $rate = rtrim(rtrim($rate, '0'), '.');
133
-                        }
134
-
135 136
                         return $rate . ($record->computation === 'percentage' ? '%' : null);
136 137
                     })
137 138
                     ->searchable()

+ 8
- 6
app/Filament/Resources/TaxResource.php Ver fichero

@@ -4,9 +4,7 @@ namespace App\Filament\Resources;
4 4
 
5 5
 use App\Filament\Resources\TaxResource\Pages;
6 6
 use App\Filament\Resources\TaxResource\RelationManagers;
7
-use App\Models\Setting\Category;
8 7
 use App\Models\Setting\Tax;
9
-use Closure;
10 8
 use Exception;
11 9
 use Filament\Forms;
12 10
 use Filament\Forms\Components\TextInput\Mask;
@@ -15,7 +13,9 @@ use Filament\Resources\Form;
15 13
 use Filament\Resources\Resource;
16 14
 use Filament\Resources\Table;
17 15
 use Filament\Tables;
16
+use Illuminate\Database\Eloquent\Builder;
18 17
 use Illuminate\Support\Collection;
18
+use Illuminate\Support\Facades\Auth;
19 19
 use Wallo\FilamentSelectify\Components\ToggleButton;
20 20
 
21 21
 class TaxResource extends Resource
@@ -26,6 +26,12 @@ class TaxResource extends Resource
26 26
 
27 27
     protected static ?string $navigationGroup = 'Settings';
28 28
 
29
+    public static function getEloquentQuery(): Builder
30
+    {
31
+        return parent::getEloquentQuery()
32
+            ->where('company_id', Auth::user()->currentCompany->id);
33
+    }
34
+
29 35
     public static function form(Form $form): Form
30 36
     {
31 37
         return $form
@@ -109,10 +115,6 @@ class TaxResource extends Resource
109 115
                     ->formatStateUsing(static function (Tax $record) {
110 116
                         $rate = $record->rate;
111 117
 
112
-                        if (str_contains($rate, '.')) {
113
-                            $rate = rtrim(rtrim($rate, '0'), '.');
114
-                        }
115
-
116 118
                         return $rate . ($record->computation === 'percentage' || $record->computation === 'compound' ? '%' : null);
117 119
                     })
118 120
                     ->searchable()

Loading…
Cancelar
Guardar