The company form currency field support all currencies and create automatic new currency!
This commit is contained in:
		| @@ -10,6 +10,7 @@ use App\Jobs\Common\UpdateCompany; | |||||||
| use App\Models\Common\Company; | use App\Models\Common\Company; | ||||||
| use App\Traits\Uploads; | use App\Traits\Uploads; | ||||||
| use App\Traits\Users; | use App\Traits\Users; | ||||||
|  | use Akaunting\Money\Currency as MoneyCurrency; | ||||||
|  |  | ||||||
| class Companies extends Controller | class Companies extends Controller | ||||||
| { | { | ||||||
| @@ -44,7 +45,15 @@ class Companies extends Controller | |||||||
|      */ |      */ | ||||||
|     public function create() |     public function create() | ||||||
|     { |     { | ||||||
|         return view('common.companies.create'); |         $money_currencies = MoneyCurrency::getCurrencies(); | ||||||
|  |  | ||||||
|  |         $currencies = []; | ||||||
|  |  | ||||||
|  |         foreach ($money_currencies as $key => $item) { | ||||||
|  |             $currencies[$key] = $item['name']; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         return view('common.companies.create', compact('currencies')); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
| @@ -92,7 +101,15 @@ class Companies extends Controller | |||||||
|             return redirect()->route('companies.index'); |             return redirect()->route('companies.index'); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         return view('common.companies.edit', compact('company')); |         $money_currencies = MoneyCurrency::getCurrencies(); | ||||||
|  |  | ||||||
|  |         $currencies = []; | ||||||
|  |  | ||||||
|  |         foreach ($money_currencies as $key => $item) { | ||||||
|  |             $currencies[$key] = $item['name']; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         return view('common.companies.edit', compact('company', 'currencies')); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|   | |||||||
| @@ -8,8 +8,12 @@ use App\Events\Common\CompanyCreating; | |||||||
| use App\Interfaces\Job\HasOwner; | use App\Interfaces\Job\HasOwner; | ||||||
| use App\Interfaces\Job\HasSource; | use App\Interfaces\Job\HasSource; | ||||||
| use App\Interfaces\Job\ShouldCreate; | use App\Interfaces\Job\ShouldCreate; | ||||||
|  | use App\Models\Banking\Account; | ||||||
| use App\Models\Common\Company; | use App\Models\Common\Company; | ||||||
|  | use App\Models\Setting\Currency; | ||||||
|  | use Akaunting\Money\Currency as MoneyCurrency; | ||||||
| use Illuminate\Support\Facades\Artisan; | use Illuminate\Support\Facades\Artisan; | ||||||
|  | use OutOfBoundsException; | ||||||
|  |  | ||||||
| class CreateCompany extends Job implements HasOwner, HasSource, ShouldCreate | class CreateCompany extends Job implements HasOwner, HasSource, ShouldCreate | ||||||
| { | { | ||||||
| @@ -26,6 +30,8 @@ class CreateCompany extends Job implements HasOwner, HasSource, ShouldCreate | |||||||
|  |  | ||||||
|             $this->callSeeds(); |             $this->callSeeds(); | ||||||
|  |  | ||||||
|  |             $this->updateCurrency(); | ||||||
|  |  | ||||||
|             $this->updateSettings(); |             $this->updateSettings(); | ||||||
|         }); |         }); | ||||||
|  |  | ||||||
| @@ -99,4 +105,42 @@ class CreateCompany extends Job implements HasOwner, HasSource, ShouldCreate | |||||||
|  |  | ||||||
|         setting()->save(); |         setting()->save(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     protected function updateCurrency() | ||||||
|  |     { | ||||||
|  |         $currency_code = $this->request->get('currency'); | ||||||
|  |  | ||||||
|  |         if ($currency_code == 'USD') { | ||||||
|  |             return; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         $currency = Currency::where('company_id', $this->model->id) | ||||||
|  |                             ->where('code', $currency_code) | ||||||
|  |                             ->first(); | ||||||
|  |  | ||||||
|  |         if ($currency) { | ||||||
|  |             $currency->rate = '1'; | ||||||
|  |             $currency->enabled = '1'; | ||||||
|  |  | ||||||
|  |             $currency->save(); | ||||||
|  |         } else { | ||||||
|  |             try { | ||||||
|  |                 $data = (new MoneyCurrency($currency_code))->toArray()[$currency_code]; | ||||||
|  |                 $data['rate'] = '1'; | ||||||
|  |                 $data['enabled'] = '1'; | ||||||
|  |                 $data['company_id'] = $this->model->id; | ||||||
|  |                 $data['code'] = $currency_code; | ||||||
|  |                 $data['created_from'] = 'core::ui'; | ||||||
|  |                 $data['created_by'] = user_id(); | ||||||
|  |  | ||||||
|  |                 $currency = Currency::create($data); | ||||||
|  |             } catch (OutOfBoundsException $e) { | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         $account = Account::where('company_id', $this->model->id)->first(); | ||||||
|  |  | ||||||
|  |         $account->currency_code = $currency_code; | ||||||
|  |         $account->save(); | ||||||
|  |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -7,7 +7,10 @@ use App\Events\Common\CompanyUpdated; | |||||||
| use App\Events\Common\CompanyUpdating; | use App\Events\Common\CompanyUpdating; | ||||||
| use App\Interfaces\Job\ShouldUpdate; | use App\Interfaces\Job\ShouldUpdate; | ||||||
| use App\Models\Common\Company; | use App\Models\Common\Company; | ||||||
|  | use App\Models\Setting\Currency; | ||||||
| use App\Traits\Users; | use App\Traits\Users; | ||||||
|  | use Akaunting\Money\Currency as MoneyCurrency; | ||||||
|  | use OutOfBoundsException; | ||||||
|  |  | ||||||
| class UpdateCompany extends Job implements ShouldUpdate | class UpdateCompany extends Job implements ShouldUpdate | ||||||
| { | { | ||||||
| @@ -86,6 +89,8 @@ class UpdateCompany extends Job implements ShouldUpdate | |||||||
|             } |             } | ||||||
|  |  | ||||||
|             setting()->save(); |             setting()->save(); | ||||||
|  |  | ||||||
|  |             $this->updateCurrency(); | ||||||
|         }); |         }); | ||||||
|  |  | ||||||
|         event(new CompanyUpdated($this->model, $this->request)); |         event(new CompanyUpdated($this->model, $this->request)); | ||||||
| @@ -116,4 +121,37 @@ class UpdateCompany extends Job implements ShouldUpdate | |||||||
|             throw new \Exception($message); |             throw new \Exception($message); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     protected function updateCurrency() | ||||||
|  |     { | ||||||
|  |         $currency_code = $this->request->get('currency'); | ||||||
|  |  | ||||||
|  |         if (empty($currency_code)) { | ||||||
|  |             return; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         $currency = Currency::where('company_id', $this->model->id) | ||||||
|  |                             ->where('code', $currency_code) | ||||||
|  |                             ->first(); | ||||||
|  |  | ||||||
|  |         if ($currency) { | ||||||
|  |             $currency->rate = '1'; | ||||||
|  |             $currency->enabled = '1'; | ||||||
|  |  | ||||||
|  |             $currency->save(); | ||||||
|  |         } else { | ||||||
|  |             try { | ||||||
|  |                 $data = (new MoneyCurrency($currency_code))->toArray()[$currency_code]; | ||||||
|  |                 $data['rate'] = '1'; | ||||||
|  |                 $data['enabled'] = '1'; | ||||||
|  |                 $data['company_id'] = $this->model->id; | ||||||
|  |                 $data['code'] = $currency_code; | ||||||
|  |                 $data['created_from'] = 'core::ui'; | ||||||
|  |                 $data['created_by'] = user_id(); | ||||||
|  |  | ||||||
|  |                 $currency = Currency::create($data); | ||||||
|  |             } catch (OutOfBoundsException $e) { | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -35,6 +35,10 @@ class Currency extends Form | |||||||
|  |  | ||||||
|         $this->currencies = Model::enabled()->orderBy('name')->pluck('name', 'code'); |         $this->currencies = Model::enabled()->orderBy('name')->pluck('name', 'code'); | ||||||
|  |  | ||||||
|  |         if (! empty($this->options)) { | ||||||
|  |             $this->currencies = $this->options; | ||||||
|  |         } | ||||||
|  |  | ||||||
|         $currency_id = old('currency.id', old('currency_id', null)); |         $currency_id = old('currency.id', old('currency_id', null)); | ||||||
|  |  | ||||||
|         if (! empty($currency_id)) { |         if (! empty($currency_id)) { | ||||||
|   | |||||||
| @@ -16,7 +16,7 @@ | |||||||
|  |  | ||||||
|                         <x-form.group.email name="email" label="{{ trans('general.email') }}" /> |                         <x-form.group.email name="email" label="{{ trans('general.email') }}" /> | ||||||
|  |  | ||||||
|                         <x-form.group.currency name="currency" /> |                         <x-form.group.currency name="currency" :options="$currencies" without-add-new /> | ||||||
|  |  | ||||||
|                         <x-form.group.country /> |                         <x-form.group.country /> | ||||||
|                     </x-slot> |                     </x-slot> | ||||||
|   | |||||||
| @@ -14,7 +14,7 @@ | |||||||
|  |  | ||||||
|                         <x-form.group.email name="email" label="{{ trans('general.email') }}" /> |                         <x-form.group.email name="email" label="{{ trans('general.email') }}" /> | ||||||
|  |  | ||||||
|                         <x-form.group.currency name="currency" selected="{{ ! empty($company->currency) ? $company->currency : config('setting.fallback.default.currency') }}" /> |                         <x-form.group.currency name="currency" :options="$currencies" selected="{{ ! empty($company->currency) ? $company->currency : config('setting.fallback.default.currency') }}" without-add-new /> | ||||||
|  |  | ||||||
|                         <x-form.group.country /> |                         <x-form.group.country /> | ||||||
|                     </x-slot> |                     </x-slot> | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user