Reworking taxes

This commit is contained in:
Hillel Coren 2016-03-29 17:30:28 +03:00
parent 3e9802502c
commit f91f32323f
9 changed files with 63 additions and 50 deletions

View File

@ -194,7 +194,7 @@ class InvoiceController extends BaseController
'isRecurring' => $invoice->is_recurring,
'actions' => $actions,
'lastSent' => $lastSent);
$data = array_merge($data, self::getViewModel());
$data = array_merge($data, self::getViewModel($invoice));
if ($clone) {
$data['formIsChanged'] = true;
@ -261,7 +261,7 @@ class InvoiceController extends BaseController
'url' => 'invoices',
'title' => trans('texts.new_invoice'),
];
$data = array_merge($data, self::getViewModel());
$data = array_merge($data, self::getViewModel($invoice));
return View::make('invoices.edit', $data);
}
@ -271,7 +271,7 @@ class InvoiceController extends BaseController
return self::create($clientPublicId, true);
}
private static function getViewModel()
private static function getViewModel($invoice)
{
$recurringHelp = '';
foreach (preg_split("/((\r?\n)|(\r\n?))/", trans('texts.recurring_help')) as $line) {
@ -332,17 +332,37 @@ class InvoiceController extends BaseController
}
}
// Tax rate options
// Tax rate $options
$account = Auth::user()->account;
$rates = TaxRate::scope()->orderBy('name')->get();
$options = [];
$defaultTax = false;
foreach ($rates as $rate) {
$options[$rate->rate . ' ' . $rate->name] = $rate->name . ' ' . ($rate->rate+0) . '%';
// load default invoice tax
if ($rate->id == $account->default_tax_rate_id) {
$defaultTax = $rate;
}
}
// Check for any taxes which have been deleted
if ($invoice->exists) {
foreach ($invoice->getTaxes() as $key => $rate) {
if (isset($options[$key])) {
continue;
}
$options[$key] = $rate['name'] . ' ' . $rate['rate'] . '%';
}
}
return [
'data' => Input::old('data'),
'account' => Auth::user()->account->load('country'),
'products' => Product::scope()->with('default_tax_rate')->orderBy('product_key')->get(),
'taxRates' => $options,
'taxRateOptions' => $options,
'defaultTax' => $defaultTax,
'currencies' => Cache::get('currencies'),
'languages' => Cache::get('languages'),
'sizes' => Cache::get('sizes'),

View File

@ -853,9 +853,9 @@ class Invoice extends EntityModel implements BalanceAffecting
$taxAmount = round($taxAmount, 2);
if ($taxAmount) {
$taxes[$this->tax_name.$this->tax_rate] = [
$taxes[$this->tax_rate . ' ' . $this->tax_name] = [
'name' => $this->tax_name,
'rate' => $this->tax_rate,
'rate' => $this->tax_rate+0,
'amount' => $taxAmount,
'paid' => round($this->getAmountPaid($calculatePaid) / $this->amount * $taxAmount, 2)
];
@ -872,7 +872,7 @@ class Invoice extends EntityModel implements BalanceAffecting
$taxAmount = round($taxAmount, 2);
if ($taxAmount) {
$key = $invoiceItem->tax_name.$invoiceItem->tax_rate;
$key = $invoiceItem->tax_rate . ' ' . $invoiceItem->tax_name;
if ( ! isset($taxes[$key])) {
$taxes[$key] = [
@ -884,7 +884,7 @@ class Invoice extends EntityModel implements BalanceAffecting
$taxes[$key]['amount'] += $taxAmount;
$taxes[$key]['paid'] += $this->amount && $taxAmount ? round($this->getAmountPaid($calculatePaid) / $this->amount * $taxAmount, 2) : 0;
$taxes[$key]['name'] = $invoiceItem->tax_name;
$taxes[$key]['rate'] = $invoiceItem->tax_rate;
$taxes[$key]['rate'] = $invoiceItem->tax_rate+0;
}
}

View File

@ -1,5 +1,6 @@
<?php namespace App\Models;
use Auth;
use Illuminate\Database\Eloquent\SoftDeletes;
class TaxRate extends EntityModel

File diff suppressed because one or more lines are too long

View File

@ -465,7 +465,7 @@ NINJA.subtotals = function(invoice, hideBalance)
}
}
if (invoice.tax && invoice.tax.name || invoice.tax_name) {
if (invoice.tax_amount) {
var taxStr = invoice.tax_name + ' ' + (invoice.tax_rate*1).toString() + '%';
data.push([{text: taxStr}, {text: formatMoneyInvoice(invoice.tax_amount, invoice)}]);
}

View File

@ -612,11 +612,7 @@ function calculateAmounts(invoice) {
invoice.has_product_key = true;
}
// the object structure differs if it's read from the db or created by knockoutJS
if (item.tax && parseFloat(item.tax.rate)) {
taxRate = parseFloat(item.tax.rate);
taxName = item.tax.name;
} else if (item.tax_rate && parseFloat(item.tax_rate)) {
if (item.tax_rate && parseFloat(item.tax_rate)) {
taxRate = parseFloat(item.tax_rate);
taxName = item.tax_name;
}
@ -632,7 +628,7 @@ function calculateAmounts(invoice) {
}
var taxAmount = roundToTwo(lineTotal * taxRate / 100);
if (taxRate) {
if (taxAmount) {
var key = taxName + taxRate;
if (taxes.hasOwnProperty(key)) {
taxes[key].amount += taxAmount;
@ -641,7 +637,7 @@ function calculateAmounts(invoice) {
}
}
if ((item.tax && item.tax.name) || item.tax_name) {
if (item.tax_name) {
hasTaxes = true;
}
}
@ -667,9 +663,7 @@ function calculateAmounts(invoice) {
}
var tax = 0;
if (invoice.tax && parseFloat(invoice.tax.rate)) {
tax = parseFloat(invoice.tax.rate);
} else if (invoice.tax_rate && parseFloat(invoice.tax_rate)) {
if (invoice.tax_rate && parseFloat(invoice.tax_rate)) {
tax = parseFloat(invoice.tax_rate);
}
@ -708,9 +702,7 @@ function calculateAmounts(invoice) {
function getInvoiceTaxRate(invoice) {
var tax = 0;
if (invoice.tax && parseFloat(invoice.tax.rate)) {
tax = parseFloat(invoice.tax.rate);
} else if (invoice.tax_rate && parseFloat(invoice.tax_rate)) {
if (invoice.tax_rate && parseFloat(invoice.tax_rate)) {
tax = parseFloat(invoice.tax_rate);
}
return tax;

View File

@ -1102,7 +1102,7 @@ $LANG = array(
'email_documents_header' => 'Documents:',
'email_documents_example_1' => 'Widgets Receipt.pdf',
'email_documents_example_2' => 'Final Deliverable.zip',
'invoice_documents' => 'Attached Documents',
'invoice_documents' => 'Documents',
'expense_documents' => 'Attached Documents',
'invoice_embed_documents' => 'Embed Documents',
'invoice_embed_documents_help' => 'Include attached images in the invoice.',

View File

@ -244,7 +244,8 @@
</td>
<td style="display:none;" data-bind="visible: $root.invoice_item_taxes.show">
{!! Former::select('')
->options($taxRates)
->addOption('', '')
->options($taxRateOptions)
->data_bind('value: tax')
->raw() !!}
<input type="text" data-bind="value: tax_name, attr: {name: 'invoice_items[' + $index() + '][tax_name]'}" style="display:none">
@ -384,7 +385,8 @@
@endif
<td style="min-width:120px">
{!! Former::select('')
->options($taxRates)
->addOption('', '')
->options($taxRateOptions)
->data_bind('value: tax')
->raw() !!}
<input type="text" name="tax_name" data-bind="value: tax_name" style="display:none">
@ -722,7 +724,6 @@
var invoiceDesigns = {!! $invoiceDesigns !!};
var invoiceFonts = {!! $invoiceFonts !!};
$(function() {
// create client dictionary
for (var i=0; i<clients.length; i++) {
@ -767,8 +768,10 @@
model.invoice().custom_taxes1({{ $account->custom_invoice_taxes1 ? 'true' : 'false' }});
model.invoice().custom_taxes2({{ $account->custom_invoice_taxes2 ? 'true' : 'false' }});
// set the default account tax rate
@if ($account->invoice_taxes && $account->default_tax_rate_id)
//model.invoice().tax(model.getTaxRateById({{ $account->default_tax_rate ? $account->default_tax_rate->public_id : '' }}));
@if ($account->invoice_taxes && ! empty($defaultTax))
var defaultTax = {!! $defaultTax !!};
model.invoice().tax_rate(defaultTax.rate);
model.invoice().tax_name(defaultTax.name);
@endif
@endif

View File

@ -794,7 +794,9 @@ ko.bindingHandlers.typeahead = {
}
@if ($account->invoice_item_taxes)
if (datum.default_tax_rate) {
//model.tax(self.model.getTaxRateById(datum.default_tax_rate.public_id));
model.tax_rate(datum.default_tax_rate.rate);
model.tax_name(datum.default_tax_rate.name);
model.tax(datum.default_tax_rate.rate + ' ' + datum.default_tax_rate.name);
}
@endif
@endif