Skip to content

Commit

Permalink
invoice and bill currency issue some fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Batuhan Baş committed Aug 29, 2018
1 parent fa6a5d7 commit 51503e6
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 54 deletions.
4 changes: 2 additions & 2 deletions app/Http/Controllers/Common/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@ public function autocomplete()
$item_tax_price = ($item->sale_price / 100) * $tax->rate;
}

$item->sale_price = $this->convertPrice($item->sale_price, $currency_code, $currency->rate);
$item->purchase_price = $this->convertPrice($item->purchase_price, $currency_code, $currency->rate);
//$item->sale_price = $this->convertPrice($item->sale_price, $currency_code, $currency->rate);
//$item->purchase_price = $this->convertPrice($item->purchase_price, $currency_code, $currency->rate);

switch ($type) {
case 'bill':
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Expenses/Bills.php
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ public function addItem(ItemRequest $request)
// it should be integer for amount mask
$currency->precision = (int) $currency->precision;

$html = view('expenses.bills.item', compact('item_row', 'taxes'))->render();
$html = view('expenses.bills.item', compact('item_row', 'taxes', 'currency'))->render();

return response()->json([
'success' => true,
Expand Down
20 changes: 11 additions & 9 deletions app/Http/Controllers/Incomes/Invoices.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ public function show(Invoice $invoice)
foreach ($invoice->payments as $item) {
$default_amount = $item->amount;

if ($invoice->currency_code != $item->currency_code) {
if ($invoice->currency_code == $item->currency_code) {
$amount = (double)$default_amount;
} else {
$default_amount_model = new InvoicePayment();

$default_amount_model->default_currency_code = $invoice->currency_code;
Expand All @@ -85,16 +87,16 @@ public function show(Invoice $invoice)
$default_amount_model->currency_rate = $_currencies[$item->currency_code];

$default_amount = (double) $default_amount_model->getDivideConvertedAmount();
}

$convert_amount = new InvoicePayment();
$convert_amount = new InvoicePayment();

$convert_amount->default_currency_code = $item->currency_code;
$convert_amount->amount = $default_amount;
$convert_amount->currency_code = $invoice->currency_code;
$convert_amount->currency_rate = $_currencies[$invoice->currency_code];
$convert_amount->default_currency_code = $item->currency_code;
$convert_amount->amount = $default_amount;
$convert_amount->currency_code = $invoice->currency_code;
$convert_amount->currency_rate = $_currencies[$invoice->currency_code];

$amount = (double) $convert_amount->getDynamicConvertedAmount();
$amount = (double) $convert_amount->getDynamicConvertedAmount();
}

$paid += $amount;
}
Expand Down Expand Up @@ -892,7 +894,7 @@ public function addItem(ItemRequest $request)
// it should be integer for amount mask
$currency->precision = (int) $currency->precision;

$html = view('incomes.invoices.item', compact('item_row', 'taxes'))->render();
$html = view('incomes.invoices.item', compact('item_row', 'taxes', 'currency'))->render();

return response()->json([
'success' => true,
Expand Down
58 changes: 31 additions & 27 deletions app/Http/Controllers/Modals/InvoicePayments.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ public function create(Invoice $invoice)

$currencies = Currency::enabled()->orderBy('name')->pluck('name', 'code')->toArray();

$currency = Currency::where('code', setting('general.default_currency'))->first();

$account_currency_code = Account::where('id', setting('general.default_account'))->pluck('currency_code')->first();

$currency = Currency::where('code', $account_currency_code)->first();

$payment_methods = Modules::getPaymentMethods();

$invoice->paid = $this->getPaid($invoice);
$paid = $this->getPaid($invoice);

// Get Invoice Totals
foreach ($invoice->totals as $invoice_total) {
Expand Down Expand Up @@ -88,7 +88,9 @@ public function store(Invoice $invoice, Request $request)

$default_amount = (double) $request['amount'];

if ($invoice->currency_code != $request['currency_code']) {
if ($invoice->currency_code == $request['currency_code']) {
$amount = $default_amount;
} else {
$default_amount_model = new InvoicePayment();

$default_amount_model->default_currency_code = $invoice->currency_code;
Expand All @@ -97,16 +99,16 @@ public function store(Invoice $invoice, Request $request)
$default_amount_model->currency_rate = $currencies[$request['currency_code']];

$default_amount = (double) $default_amount_model->getDivideConvertedAmount();
}

$convert_amount = new InvoicePayment();
$convert_amount = new InvoicePayment();

$convert_amount->default_currency_code = $request['currency_code'];
$convert_amount->amount = $default_amount;
$convert_amount->currency_code = $invoice->currency_code;
$convert_amount->currency_rate = $currencies[$invoice->currency_code];
$convert_amount->default_currency_code = $request['currency_code'];
$convert_amount->amount = $default_amount;
$convert_amount->currency_code = $invoice->currency_code;
$convert_amount->currency_rate = $currencies[$invoice->currency_code];

$amount = (double) $convert_amount->getDynamicConvertedAmount();
$amount = (double) $convert_amount->getDynamicConvertedAmount();
}

if ($invoice->payments()->count()) {
$total_amount -= $this->getPaid($invoice);
Expand Down Expand Up @@ -134,18 +136,18 @@ public function store(Invoice $invoice, Request $request)
$error_amount_model->currency_rate = $currencies[$invoice->currency_code];

$error_amount = (double) $error_amount_model->getDivideConvertedAmount();
}

$convert_amount = new InvoicePayment();
$convert_amount = new InvoicePayment();

$convert_amount->default_currency_code = $invoice->currency_code;
$convert_amount->amount = $error_amount;
$convert_amount->currency_code = $request['currency_code'];
$convert_amount->currency_rate = $currencies[$request['currency_code']];
$convert_amount->default_currency_code = $invoice->currency_code;
$convert_amount->amount = $error_amount;
$convert_amount->currency_code = $request['currency_code'];
$convert_amount->currency_rate = $currencies[$request['currency_code']];

$error_amount = (double) $convert_amount->getDynamicConvertedAmount();
$error_amount = (double) $convert_amount->getDynamicConvertedAmount();
}

$message = trans('messages.error.over_payment', ['amount' => money($error_amount, $request['currency_code'], true)]);
$message = trans('messages.error.over_payment', ['amount' => money($error_amount, $request['currency_code'],true)]);

return response()->json([
'success' => false,
Expand Down Expand Up @@ -217,7 +219,9 @@ protected function getPaid($invoice)
foreach ($invoice->payments as $item) {
$default_amount = $item->amount;

if ($invoice->currency_code != $item->currency_code) {
if ($invoice->currency_code == $item->currency_code) {
$amount = (double) $default_amount;
} else {
$default_amount_model = new InvoicePayment();

$default_amount_model->default_currency_code = $invoice->currency_code;
Expand All @@ -226,16 +230,16 @@ protected function getPaid($invoice)
$default_amount_model->currency_rate = $_currencies[$item->currency_code];

$default_amount = (double) $default_amount_model->getDivideConvertedAmount();
}

$convert_amount = new InvoicePayment();
$convert_amount = new InvoicePayment();

$convert_amount->default_currency_code = $item->currency_code;
$convert_amount->amount = $default_amount;
$convert_amount->currency_code = $invoice->currency_code;
$convert_amount->currency_rate = $_currencies[$invoice->currency_code];
$convert_amount->default_currency_code = $item->currency_code;
$convert_amount->amount = $default_amount;
$convert_amount->currency_code = $invoice->currency_code;
$convert_amount->currency_rate = $_currencies[$invoice->currency_code];

$amount = (double) $convert_amount->getDynamicConvertedAmount();
$amount = (double) $convert_amount->getDynamicConvertedAmount();
}

$paid += $amount;
}
Expand Down
6 changes: 5 additions & 1 deletion app/Http/Middleware/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ public function handle($request, Closure $next)
if (isset($bill_number) || isset($invoice_number) || !empty($items)) {
if (!empty($items)) {
foreach ($items as $key => $item) {
$items[$key]['price'] = money($item['price'], $currency_code)->getAmount();
if (isset($item['currency']) && $item['currency'] != $currency_code) {
$items[$key]['price'] = money($item['price'], $item['currency'])->getAmount();
} else {
$items[$key]['price'] = money($item['price'], $currency_code)->getAmount();
}
}

$request->request->set('item', $items);
Expand Down
27 changes: 26 additions & 1 deletion resources/views/expenses/bills/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,13 @@
var item_row = '{{ $item_row }}';
$(document).on('click', '#button-add-item', function (e) {
var currency_code = $('#currency_code').val();
$.ajax({
url: '{{ url("expenses/bills/addItem") }}',
type: 'GET',
dataType: 'JSON',
data: {item_row: item_row, currency_code : $('#currency_code').val()},
data: {item_row: item_row, currency_code : currency_code},
success: function(json) {
if (json['success']) {
$('#items tbody #addItem').before(json['html']);
Expand Down Expand Up @@ -285,6 +287,7 @@
$('#item-tax-' + item_id).val(data.tax_id);
// This event Select2 Stylesheet
$('#item-price-' + item_id).trigger('focusout');
$('#item-tax-' + item_id).trigger('change');
$('#item-total-' + item_id).html(data.total);
Expand Down Expand Up @@ -412,6 +415,28 @@ function totalItem() {
$('#discount-total').html(data.discount_total);
$('#tax-total').html(data.tax_total);
$('#grand-total').html(data.grand_total);
$('.input-price').each(function(){
input_price_id = $(this).attr('id');
input_currency_id = input_price_id.replace('price', 'currency');
$('#' + input_currency_id).val(data.currency_code);
amount = $(this).maskMoney('unmasked')[0];
$(this).maskMoney({
thousands : data.thousands_separator,
decimal : data.decimal_mark,
precision : data.precision,
allowZero : true,
prefix : (data.symbol_first) ? data.symbol : '',
suffix : (data.symbol_first) ? '' : data.symbol
});
$(this).val(amount);
$(this).trigger('focusout');
});
}
}
});
Expand Down
29 changes: 27 additions & 2 deletions resources/views/expenses/bills/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,13 @@
var item_row = '{{ $item_row }}';
$(document).on('click', '#button-add-item', function (e) {
var currency_code = $('#currency_code').val();
$.ajax({
url: '{{ url("expenses/bills/addItem") }}',
type: 'GET',
dataType: 'JSON',
data: {item_row: item_row},
data: {item_row: item_row, currency_code : currency_code},
success: function(json) {
if (json['success']) {
$('#items tbody #addItem').before(json['html']);
Expand Down Expand Up @@ -293,6 +295,7 @@
$('#item-tax-' + item_id).val(data.tax_id);
// This event Select2 Stylesheet
$('#item-price-' + item_id).trigger('focusout');
$('#item-tax-' + item_id).trigger('change');
$('#item-total-' + item_id).html(data.total);
Expand Down Expand Up @@ -406,7 +409,7 @@ function totalItem() {
url: '{{ url("common/items/totalItem") }}',
type: 'POST',
dataType: 'JSON',
data: $('#currency_code, #discount input[type=\'number\'], #items input[type=\'text\'],#items input[type=\'number\'],#items input[type=\'hidden\'], #items textarea, #items select'),
data: $('#currency_code, #discount input[type=\'number\'], #items input[type=\'text\'], #items input[type=\'number\'], #items input[type=\'hidden\'], #items textarea, #items select'),
headers: { 'X-CSRF-TOKEN': '{{ csrf_token() }}' },
success: function(data) {
if (data) {
Expand All @@ -420,6 +423,28 @@ function totalItem() {
$('#discount-total').html(data.discount_total);
$('#tax-total').html(data.tax_total);
$('#grand-total').html(data.grand_total);
$('.input-price').each(function(){
input_price_id = $(this).attr('id');
input_currency_id = input_price_id.replace('price', 'currency');
$('#' + input_currency_id).val(data.currency_code);
amount = $(this).maskMoney('unmasked')[0];
$(this).maskMoney({
thousands : data.thousands_separator,
decimal : data.decimal_mark,
precision : data.precision,
allowZero : true,
prefix : (data.symbol_first) ? data.symbol : '',
suffix : (data.symbol_first) ? '' : data.symbol
});
$(this).val(amount);
$(this).trigger('focusout');
});
}
}
});
Expand Down
1 change: 1 addition & 0 deletions resources/views/expenses/bills/item.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<td>
@stack('price_input_start')
<input value="{{ empty($item) ? '' : $item->price }}" class="form-control text-right" required="required" name="item[{{ $item_row }}][price]" type="text" id="item-price-{{ $item_row }}">
<input value="{{ $currency->code }}" name="item[{{ $item_row }}][currency]" type="hidden" id="item-currency-{{ $item_row }}">
@stack('price_input_end')
</td>
@stack('price_td_end')
Expand Down
21 changes: 13 additions & 8 deletions resources/views/incomes/invoices/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,13 @@
var item_row = '{{ $item_row }}';
$(document).on('click', '#button-add-item', function (e) {
var currency_code = $('#currency_code').val();
$.ajax({
url: '{{ url("incomes/invoices/addItem") }}',
type: 'GET',
dataType: 'JSON',
data: {item_row: item_row, currency_code : $('#currency_code').val()},
data: {item_row: item_row, currency_code : currency_code},
success: function(json) {
if (json['success']) {
$('#items tbody #addItem').before(json['html']);
Expand Down Expand Up @@ -371,6 +373,11 @@
$('#currency_rate').val(data.currency_rate);
$('.input-price').each(function(){
input_price_id = $(this).attr('id');
input_currency_id = input_price_id.replace('price', 'currency');
$('#' + input_currency_id).val(data.currency_code);
amount = $(this).maskMoney('unmasked')[0];
$(this).maskMoney({
Expand Down Expand Up @@ -401,13 +408,6 @@ function totalItem() {
dataType: 'JSON',
data: $('#currency_code, #discount input[type=\'number\'], #items input[type=\'text\'],#items input[type=\'number\'],#items input[type=\'hidden\'], #items textarea, #items select'),
headers: { 'X-CSRF-TOKEN': '{{ csrf_token() }}' },
before: function () {
$('.input-price').each(function(){
amount = $(this).maskMoney('unmasked')[0];
$(this).val(amount);
});
},
success: function(data) {
if (data) {
$.each( data.items, function( key, value ) {
Expand All @@ -422,6 +422,11 @@ function totalItem() {
$('#grand-total').html(data.grand_total);
$('.input-price').each(function(){
input_price_id = $(this).attr('id');
input_currency_id = input_price_id.replace('price', 'currency');
$('#' + input_currency_id).val(data.currency_code);
amount = $(this).maskMoney('unmasked')[0];
$(this).maskMoney({
Expand Down

0 comments on commit 51503e6

Please sign in to comment.