diff --git a/app/Abstracts/View/Components/DocumentForm.php b/app/Abstracts/View/Components/DocumentForm.php index 8d0bf785d..74f378427 100644 --- a/app/Abstracts/View/Components/DocumentForm.php +++ b/app/Abstracts/View/Components/DocumentForm.php @@ -5,6 +5,7 @@ namespace App\Abstracts\View\Components; use App\Abstracts\View\Components\Document as Base; use App\Models\Common\Contact; use App\Models\Document\Document; +use App\Models\Setting\Currency; use App\Traits\Documents; use Date; use Illuminate\Support\Str; @@ -17,6 +18,12 @@ abstract class DocumentForm extends Base public $document; + public $currencies; + + public $currency; + + public $currency_code; + /** Advanced Component Start */ /** @var string */ public $categoryType; @@ -205,7 +212,7 @@ abstract class DocumentForm extends Base * @return void */ public function __construct( - $type, $document = false, + $type, $document = false, $currencies = false, $currency = false, $currency_code = false, /** Advanced Component Start */ string $categoryType = '', bool $hideRecurring = false, bool $hideCategory = false, bool $hideAttachment = false, /** Advanced Component End */ @@ -235,6 +242,9 @@ abstract class DocumentForm extends Base ) { $this->type = $type; $this->document = $document; + $this->currencies = $this->getCurrencies($currencies); + $this->currency = $this->getCurrency($document, $currency, $currency_code); + $this->currency_code = $this->currency->code; /** Advanced Component Start */ $this->categoryType = $this->getCategoryType($type, $categoryType); @@ -314,6 +324,32 @@ abstract class DocumentForm extends Base /** Items Component End */ } + protected function getCurrencies($currencies) + { + if (!empty($currencies)) { + return $currencies; + } + + return Currency::enabled()->pluck('name', 'code'); + } + + protected function getCurrency($document, $currency, $currency_code) + { + if (!empty($currency)) { + return $currency; + } + + if (!empty($currency_code)) { + return Currency::where('code', $currency_code)->first(); + } + + if (!empty($document)) { + return Currency::where('code', $document->currency_code)->first(); + } + + return Currency::where('code', setting('default.currency'))->first(); + } + protected function getRouteStore($type, $routeStore) { if (!empty($routeStore)) { diff --git a/app/View/Components/Documents/Form/Items.php b/app/View/Components/Documents/Form/Items.php index a1f99be46..6dcb915b4 100644 --- a/app/View/Components/Documents/Form/Items.php +++ b/app/View/Components/Documents/Form/Items.php @@ -3,7 +3,6 @@ namespace App\View\Components\Documents\Form; use App\Abstracts\View\Components\DocumentForm as Component; -use App\Models\Setting\Currency; use App\Models\Setting\Tax; class Items extends Component @@ -15,10 +14,8 @@ class Items extends Component */ public function render() { - $currency = Currency::where('code', setting('default.currency'))->first(); - $taxes = Tax::enabled()->orderBy('name')->get(); - return view('components.documents.form.items', compact('currency', 'taxes')); + return view('components.documents.form.items', compact('taxes')); } } diff --git a/app/View/Components/Documents/Form/Totals.php b/app/View/Components/Documents/Form/Totals.php index c897797b9..36c9e00e5 100644 --- a/app/View/Components/Documents/Form/Totals.php +++ b/app/View/Components/Documents/Form/Totals.php @@ -3,7 +3,6 @@ namespace App\View\Components\Documents\Form; use App\Abstracts\View\Components\DocumentForm as Component; -use App\Models\Setting\Currency; class Totals extends Component { @@ -14,9 +13,6 @@ class Totals extends Component */ public function render() { - $currencies = Currency::enabled()->pluck('name', 'code'); - $currency = Currency::where('code', setting('default.currency'))->first(); - - return view('components.documents.form.totals', compact('currencies', 'currency')); + return view('components.documents.form.totals'); } } diff --git a/app/View/Components/Documents/Script.php b/app/View/Components/Documents/Script.php index 705927b7c..c5028b24b 100644 --- a/app/View/Components/Documents/Script.php +++ b/app/View/Components/Documents/Script.php @@ -18,10 +18,14 @@ class Script extends Component /** @var string */ public $version; + public $document; + public $items; public $currencies; + public $currency_code; + public $taxes; /** @@ -29,13 +33,15 @@ class Script extends Component * * @return void */ - public function __construct(string $type = '', string $scriptFile = '', string $version = '', $items = [], $currencies = [], $taxes = []) + public function __construct(string $type = '', string $scriptFile = '', string $version = '', $document = false, $items = [], $currencies = [], $taxes = []) { $this->type = $type; $this->scriptFile = ($scriptFile) ? $scriptFile : 'public/js/common/documents.js'; $this->version = $this->getVersion($version); + $this->document = $document; $this->items = $items; $this->currencies = $this->getCurrencies($currencies); + $this->currency_code = ($document) ? $document->currency_code : setting('default.currency'); $this->taxes = $this->getTaxes($taxes); } diff --git a/resources/assets/js/views/common/documents.js b/resources/assets/js/views/common/documents.js index a992bb86e..7f8200a5d 100644 --- a/resources/assets/js/views/common/documents.js +++ b/resources/assets/js/views/common/documents.js @@ -61,10 +61,6 @@ const app = new Vue({ if ((document.getElementById('items') != null) && (document.getElementById('items').rows)) { this.colspan = document.getElementById("items").rows[0].cells.length - 1; } - - if (document_currencies) { - this.currencies = document_currencies; - } }, methods: { @@ -521,6 +517,11 @@ const app = new Vue({ // Change currency get money onChangeCurrency(currency_code) { + if (this.edit.status && this.edit.currency <= 3) { + this.edit.currency++; + return; + } + if (!this.currencies.length) { let currency_promise = Promise.resolve(window.axios.get((url + '/settings/currencies'))); @@ -550,6 +551,7 @@ const app = new Vue({ if (typeof document_items !== 'undefined' && document_items) { this.edit.status = true; + this.edit.currency = 1; document_items.forEach(function(item) { // form set item @@ -626,5 +628,9 @@ const app = new Vue({ } this.page_loaded = true; + + if (document_currencies) { + this.currencies = document_currencies; + } } }); diff --git a/resources/views/components/documents/form/totals.blade.php b/resources/views/components/documents/form/totals.blade.php index d9434f995..ebeaf2849 100644 --- a/resources/views/components/documents/form/totals.blade.php +++ b/resources/views/components/documents/form/totals.blade.php @@ -118,7 +118,8 @@ {{ trans('invoices.total') }} - {{ Form::selectGroup('currency_code', '', 'exchange-alt', $currencies, setting('default.currency'), ['required' => 'required', 'model' => 'form.currency_code', 'change' => 'onChangeCurrency'], 'document-total-currency') }} + {{ Form::selectGroup('currency_code', '', 'exchange-alt', $currencies, $currency->code, ['required' => 'required', 'model' => 'form.currency_code', 'change' => 'onChangeCurrency'], 'document-total-currency') }} + {!! Form::hidden('currency_rate', (!empty($document)) ? $document->currency_rate : $currency->rate, ['id' => 'currency_rate', 'class' => 'form-control', 'required' => 'required']) !!}
@@ -130,13 +131,13 @@ @stack('grand_total_td_end') @stack('currency_conversion_td_start') - + diff --git a/resources/views/components/documents/script.blade.php b/resources/views/components/documents/script.blade.php index 3904335bc..3042802d1 100644 --- a/resources/views/components/documents/script.blade.php +++ b/resources/views/components/documents/script.blade.php @@ -10,7 +10,7 @@ diff --git a/resources/views/sales/invoices/edit.blade.php b/resources/views/sales/invoices/edit.blade.php index 6d999e2af..2c6fc46b0 100644 --- a/resources/views/sales/invoices/edit.blade.php +++ b/resources/views/sales/invoices/edit.blade.php @@ -7,5 +7,5 @@ @endsection @push('scripts_start') - + @endpush