1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-05 18:52:44 +01:00

Working on tests for line item taxes

This commit is contained in:
David Bomba 2019-09-04 15:10:10 +10:00
parent cd40ba95e6
commit bc7e09b924
7 changed files with 125 additions and 15 deletions

View File

@ -67,7 +67,9 @@ class InvoiceItemFactory
$item->custom_value2 = $faker->realText(10);
$item->custom_value3 = $faker->realText(10);
$item->custom_value4 = $faker->realText(10);
$item->tax_name1 = 'GST';
$item->tax_rate1 = '10.00';
$data[] = $item;
}

View File

@ -123,7 +123,7 @@ class InvoiceItemCalc
$key = str_replace(" ", "", $tax_name.$tax_rate);
$group_tax[$key] = ['total' => $tax_total, 'tax_name' => $tax_name . ' ' . $tax_rate];
$group_tax = [$key => ['total' => $tax_total, 'tax_name' => $tax_name . ' ' . $tax_rate.'%']];
$this->tax_collection->push(collect($group_tax));

View File

@ -18,6 +18,7 @@ use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Spatie\Browsershot\Browsershot;
use Symfony\Component\Debug\Exception\FatalThrowableError;
class CreateInvoicePdf implements ShouldQueue
{
@ -136,7 +137,7 @@ class CreateInvoicePdf implements ShouldQueue
ob_end_clean();
}
throw new \FatalThrowableError($e);
throw new FatalThrowableError($e);
}
return ob_get_clean();

View File

@ -11,6 +11,7 @@
namespace App\Models;
use App\Helpers\Invoice\InvoiceCalc;
use App\Models\Currency;
use App\Models\Filterable;
use App\Utils\Number;
@ -251,4 +252,17 @@ class Invoice extends BaseModel
return File::exists(resource_path($this->settings->design)) ? File::get(resource_path($this->settings->design)) : File::get(resource_path('views/pdf/design1.blade.php'));
}
/**
* Access the invoice calculator object
*
* @return object The invoice calculator object getters
*/
public function calc()
{
$invoice_calc = new InvoiceCalc($this, $this->client->getMergedSettings());
return $invoice_calc->build();
}
}

View File

@ -163,30 +163,31 @@ trait MakesInvoiceValues
$data['$due_date'] = $this->due_date;
$data['$invoice_number'] = $this->invoice_number;
$data['$po_number'] = $this->po_number;
// $data['$discount'] = ;
// $data['$taxes'] = ;
$data['$discount'] = $this->calc()->getTotalDiscount();
$data['$taxes'] = $this->calc()->getTotalTaxes();
$data['$line_taxes'] = $this->calc()->getTaxMap();
// $data['$tax'] = ;
// $data['$item'] = ;
// $data['$description'] = ;
// $data['$unit_cost'] = ;
// $data['$quantity'] = ;
// $data['$line_total'] = ;
// $data['$subtotal'] = ;
// $data['$paid_to_date'] = ;
$data['$subtotal'] = Number::formatMoney($this->calc()->getSubTotal(), $this->client->currency(), $this->client->country, $this->client->settings);
$data['$balance_due'] = Number::formatMoney($this->balance, $this->client->currency(), $this->client->country, $this->client->settings);
$data['$partial_due'] = Number::formatMoney($this->partial, $this->client->currency(), $this->client->country, $this->client->settings);
$data['$total'] = Number::formatMoney($this->calc()->getTotal(), $this->client->currency(), $this->client->country, $this->client->settings);
$data['$balance'] = Number::formatMoney($this->calc()->getBalance(), $this->client->currency(), $this->client->country, $this->client->settings);
$data['$terms'] = $this->terms;
// $data['$your_invoice'] = ;
// $data['$quote'] = ;
// $data['$your_quote'] = ;
// $data['$quote_date'] = ;
// $data['$quote_number'] = ;
$data['$total'] = Number::formatMoney($this->amount, $this->client->currency(), $this->client->country, $this->client->settings);
// $data['$invoice_issued_to'] = ;
// $data['$quote_issued_to'] = ;
// $data['$rate'] = ;
// $data['$hours'] = ;
// $data['$balance'] = ;
// $data['$from'] = ;
// $data['$to'] = ;
// $data['$invoice_to'] = ;
@ -303,8 +304,8 @@ trait MakesInvoiceValues
*/
private function transformColumnsForHeader(array $columns) :array
{
$columns = array_intersect(self::$master_columns, $columns);
$pre_columns = $columns;
$columns = array_intersect($columns, self::$master_columns);
return str_replace([
'tax_name1',
@ -329,7 +330,7 @@ trait MakesInvoiceValues
private function transformColumnsForLineItems(array $columns) :array
{
/* Removes any invalid columns the user has entered. */
$columns = array_intersect(self::$master_columns, $columns);
$columns = array_intersect($columns, self::$master_columns);
return str_replace([
'custom_invoice_label1',

View File

@ -168,17 +168,47 @@
custom_label3 ( will show as the following parameter as its value -> custom_invoice_value3 )
custom_label4 ( will show as the following parameter as its value -> custom_invoice_value4 )
--}}
{!! $invoice->table(['product_key', 'notes', 'cost','quantity', 'line_total']) !!}
{!! $invoice->table(['product_key', 'notes', 'cost','quantity', 'discount', 'tax_name1', 'line_total']) !!}
<table>
<tr class="total">
<tr class="subtotal">
<td></td>
<td>
Total: $385.00
$subtotal_label: $subtotal
</td>
</tr>
<tr class="taxes">
<td></td>
<td>
$taxes_label: $taxes
</td>
</tr>
<tr class="line_taxes">
<td>
$taxes_label: $line_taxes
</td>
</tr>
<tr class="discount">
<td></td>
<td>
$discount_label: $discount
</td>
</tr>
<tr class="total">
<td></td>
<td>
$total_label: $total
</td>
</tr>
<tr class="balance">
<td></td>
<td>
$balance_label: $balance
</td>
</tr>
</table>
</div>
</body>

View File

@ -31,6 +31,68 @@ class MakesInvoiceValuesTest extends TestCase
$this->assertFalse(in_array("custom_invoice_value1", $columns));
}
public function testFilteringItemTaxes()
{
$taxes = collect();
$tax_name = "GST";
$tax_rate = "10.00";
$key = str_replace(" ", "", $tax_name.$tax_rate);
$group_tax = collect(['key' => $key, 'total' => 20, 'tax_name' => $tax_name . ' ' . $tax_rate.'%']);
$taxes->push($group_tax);
$group_tax = collect(['key' => $key, 'total' => 30, 'tax_name' => $tax_name . ' ' . $tax_rate.'%']);
$taxes->push($group_tax);
$group_tax = collect(['key' => $key, 'total' => 30, 'tax_name' => $tax_name . ' ' . $tax_rate.'%']);
$taxes->push($group_tax);
$group_tax = collect(['key' => $key, 'total' => 20, 'tax_name' => $tax_name . ' ' . $tax_rate.'%']);
$taxes->push($group_tax);
$group_tax = collect(['key' => 'VAT', 'total' => 20, 'tax_name' => 'VAT' . ' ' . '17.5%']);
$taxes->push($group_tax);
$this->assertEquals(5, $taxes->count());
$keys = $taxes->pluck('key')->unique();
$this->assertEquals("GST10.00", $keys->first());
$tax_array = [];
foreach($keys as $key)
{
$tax_name = $taxes->filter(function ($value, $k) use($key){
return $value['key'] == $key;
})->pluck('tax_name')->first();
$total_line_tax = $taxes->filter(function ($value, $k) use($key){
return $value['key'] == $key;
})->sum('total');
$tax_array[] = ['name' => $tax_name, 'total' => $total_line_tax];
}
$this->assertEquals("GST10.00", print_r($tax_array));
// $this->assertEquals("GST10.00", $keys->keys());
foreach($keys as $key)
{
$this->assertEquals("GST10.00", $key);
$tax_group = $taxes->groupBy($key);
$this->assertEquals('hi', $tax_group);
$tax_group->sum('total');
$this->assertEquals(100, $tax_group->sum('total'));
}
}
}