Document multi currency and edit currency rate issue solved..

This commit is contained in:
Cüneyt Şentürk 2021-05-12 19:17:07 +03:00
parent 9bdbf19419
commit fba35ae3c5
8 changed files with 62 additions and 20 deletions

View File

@ -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)) {

View File

@ -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'));
}
}

View File

@ -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');
}
}

View File

@ -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);
}

View File

@ -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;
}
}
});

View File

@ -118,7 +118,8 @@
<td class="border-top-0 pt-0 pb-0"></td>
<td class="text-right border-top-0 border-right-0 align-middle pt-0 pb-0 pr-0">
<strong class="document-total-span">{{ trans('invoices.total') }}</strong>
{{ 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']) !!}
</td>
<td class="text-right border-top-0 long-texts pt-0 pb-0 pr-3">
<div>
@ -130,13 +131,13 @@
@stack('grand_total_td_end')
@stack('currency_conversion_td_start')
<tr id="tr-currency-conversion" class="d-none" :class="[{'d-table-row': (('{{ setting('default.currency') }}' != form.currency_code) && totals.total)}]">
<tr id="tr-currency-conversion" class="d-none" :class="[{'d-table-row': (('{{ $currency->code }}' != form.currency_code) && totals.total)}]">
<td class="border-top-0 pb-0"></td>
<td class="text-right border-top-0 border-right-0 align-middle pb-0 pr-3 pr-0" colspan="2">
<akaunting-currency-conversion
currency-conversion-text="{{ trans('currencies.conversion') }}"
:price="(totals.total / form.currency_rate).toFixed(2)"
currecy-code="{{ setting('default.currency') }}"
currecy-code="{{ $currency->code }}"
:currency-rate="form.currency_rate"
></akaunting-currency-conversion>
</td>

View File

@ -10,7 +10,7 @@
<script type="text/javascript">
var document_items = {!! $document_items !!};
var document_default_currency = '{{ setting('default.currency') }}';
var document_default_currency = '{{ $currency_code }}';
var document_currencies = {!! $currencies !!};
var document_taxes = {!! $taxes !!};
</script>

View File

@ -7,5 +7,5 @@
@endsection
@push('scripts_start')
<x-documents.script type="invoice" :items="$invoice->items()->get()" />
<x-documents.script type="invoice" :items="$invoice->items()->get()" :document="$invoice" />
@endpush