Cleaning up Tests

This commit is contained in:
David Bomba 2019-09-04 22:01:19 +10:00
parent 0051145d2e
commit 43dc9158f6
17 changed files with 138 additions and 93 deletions

View File

@ -25,7 +25,7 @@ class ProductFactory
$product->notes = '';
$product->cost = 0;
$product->price = 0;
$product->qty = 0;
$product->quantity = 0;
$product->tax_name1 = '';
$product->tax_rate1 = 0;
$product->tax_name2 = '';

View File

@ -245,9 +245,38 @@ class InvoiceCalc
return $this;
}
/**
* Sums and reduces the line item taxes
*
* @return array The array of tax names and tax totals
*/
public function getTaxMap()
{
return $this->tax_map;
$keys = $this->tax_map->collapse()->pluck('key')->unique();
$values = $this->tax_map->collapse();
$tax_array = [];
foreach($keys as $key)
{
$tax_name = $values->filter(function ($value, $k) use($key){
return $value['key'] == $key;
})->pluck('tax_name')->first();
$total_line_tax = $values->filter(function ($value, $k) use($key){
return $value['key'] == $key;
})->sum('total');
$tax_array[] = ['name' => $tax_name, 'total' => $total_line_tax];
}
return $tax_array;
}
public function setTaxMap($value)
@ -281,6 +310,11 @@ class InvoiceCalc
return $this;
}
public function getTotalLineTaxes()
{
}
public function getTotal()
{
return $this->total;

View File

@ -14,6 +14,7 @@ namespace App\Helpers\Invoice;
use App\Models\Invoice;
use App\Utils\Traits\NumberFormatter;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
class InvoiceItemCalc
{
@ -179,7 +180,7 @@ class InvoiceItemCalc
}
public function getGroupedTaxes()
{
{
return $this->tax_collection;
}

View File

@ -33,7 +33,7 @@ class StoreProductRequest extends Request
'product_key' => 'required|unique:products,product_key,null,null,company_id,'.auth()->user()->companyId(),
'cost' => 'numeric',
'price' => 'numeric',
'qty' => 'numeric',
'quantity' => 'numeric',
];
}

View File

@ -38,7 +38,7 @@ class UpdateProductRequest extends Request
'product_key' => 'unique:products,product_key,'.$this->product->id.',id,company_id,'.auth()->user()->companyId(),
'cost' => 'numeric',
'price' => 'numeric',
'qty' => 'numeric',
'quantity' => 'numeric',
];
}

View File

@ -119,7 +119,7 @@ class CreateInvoicePdf implements ShouldQueue
$data['__env'] = app(\Illuminate\View\Factory::class);
$php = Blade::compileString($string);
//Log::error($php);
$obLevel = ob_get_level();
ob_start();
extract($data, EXTR_SKIP);

View File

@ -31,7 +31,7 @@ class Product extends BaseModel
'notes',
'cost',
'price',
'qty',
'quantity',
'tax_name1',
'tax_name2',
'tax_rate1',

View File

@ -56,15 +56,6 @@ class ProductTransformer extends EntityTransformer
return $this->includeItem($product->company, $transformer, Company::class);
}
/**
* @SWG\Property(property="id", type="integer", example=1, readOnly=true)
* @SWG\Property(property="product_key", type="string", example="Item")
* @SWG\Property(property="notes", type="string", example="Notes...")
* @SWG\Property(property="cost", type="number", format="float", example=10.00)
* @SWG\Property(property="qty", type="number", format="float", example=1)
* @SWG\Property(property="updated_at", type="integer", example=1451160233, readOnly=true)
* @SWG\Property(property="archived_at", type="integer", example=1451160233, readOnly=true)
*/
public function transform(Product $product)
{
return [
@ -72,7 +63,7 @@ class ProductTransformer extends EntityTransformer
'product_key' => $product->product_key,
'notes' => $product->notes,
'cost' => (float) $product->cost,
'qty' => (float) ($product->qty ?: 0.0),
'quantity' => (float) ($product->quantity ?: 0.0),
'tax_name1' => $product->tax_name1 ?: '',
'tax_rate1' => (float) $product->tax_rate1,
'tax_name2' => $product->tax_name2 ?: '',

View File

@ -77,13 +77,11 @@ class Number
$value = number_format($value, $precision, $decimal, $thousand);
$symbol = $currency->symbol;
if ($settings->show_currency_code == "TRUE") {
Log::error('in code regardless');
return "{$value} {$code}";
} elseif ($swapSymbol) {
return "{$value} " . trim($symbol);
} elseif ($settings->show_currency_symbol === "TRUE") {
} elseif ($settings->show_currency_symbol == "TRUE") {
return "{$symbol}{$value}";
} else {
return self::formatValue($value, $currency);

View File

@ -164,8 +164,7 @@ trait MakesInvoiceValues
$data['$invoice_number'] = $this->invoice_number;
$data['$po_number'] = $this->po_number;
$data['$discount'] = $this->calc()->getTotalDiscount();
$data['$taxes'] = $this->calc()->getTotalTaxes();
$data['$line_taxes'] = $this->calc()->getTaxMap();
$data['$line_taxes'] = $this->makeLineTaxes();
// $data['$tax'] = ;
// $data['$item'] = ;
// $data['$description'] = ;
@ -173,11 +172,12 @@ trait MakesInvoiceValues
// $data['$quantity'] = ;
// $data['$line_total'] = ;
// $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['$subtotal'] = $subtotal = $this->calc()->getSubTotal(); Number::formatMoney($subtotal, $this->client->currency(), $this->client->country, $this->client->getMergedSettings());
$data['$balance_due'] = Number::formatMoney($this->balance, $this->client->currency(), $this->client->country, $this->client->getMergedSettings());
$data['$partial_due'] = Number::formatMoney($this->partial, $this->client->currency(), $this->client->country, $this->client->getMergedSettings());
$data['$total'] = $total = $this->calc()->getTotal(); Number::formatMoney($total, $this->client->currency(), $this->client->country, $this->client->getMergedSettings());
$data['$balance'] = $balance = $this->calc()->getBalance(); Number::formatMoney($balance, $this->client->currency(), $this->client->country, $this->client->getMergedSettings());
$data['$taxes'] = $taxes = $this->calc()->getTotalTaxes(); Number::formatMoney($taxes, $this->client->currency(), $this->client->country, $this->client->getMergedSettings());
$data['$terms'] = $this->terms;
// $data['$your_invoice'] = ;
// $data['$quote'] = ;
@ -380,4 +380,30 @@ trait MakesInvoiceValues
return $items;
}
/**
* Due to the way we are compiling the blade template we
* have no ability to iterate, so in the case
* of line taxes where there are multiple rows,
* we use this function to format a section of rows
*
* @return string a collection of <tr> rows with line item
* aggregate data
*/
private function makeLineTaxes() :string
{
$tax_map = $this->calc()->getTaxMap();
$data = '';
foreach($tax_map as $tax)
{
$data .= '<tr class="line_taxes">';
$data .= '<td>'. $tax['name'] .'</td>';
$data .= '<td>'. Number::formatMoney($tax['total'], $this->client->currency(), $this->client->country, $this->client->getMergedSettings()) .'</td></tr>';
}
return $data;
}
}

View File

@ -8,7 +8,7 @@ $factory->define(App\Models\Product::class, function (Faker $faker) {
'notes' => $faker->text(20),
'cost' => $faker->numberBetween(1,1000),
'price' => $faker->numberBetween(1,1000),
'qty' => $faker->numberBetween(1,100),
'quantity' => $faker->numberBetween(1,100),
'tax_name1' => 'GST',
'tax_rate1' => 10,
'tax_name2' => 'VAT',

View File

@ -657,7 +657,7 @@ class CreateUsersTable extends Migration
$t->text('notes');
$t->decimal('cost', 13, 2);
$t->decimal('price', 13, 2);
$t->decimal('qty', 13, 2)->nullable();
$t->decimal('quantity', 13, 2)->nullable();
$t->string('tax_name1')->nullable();
$t->decimal('tax_rate1', 13, 3);

View File

@ -79,7 +79,7 @@ You can also update a client by specifying a value for id. Next, heres
.. code-block:: shell
curl -X POST ninja.test/api/v1/invoices -H "Content-Type:application/json" \
-d '{"client_id":"1", "invoice_items":[{"product_key": "ITEM", "notes":"Test", "cost":10, "qty":1}]}' \
-d '{"client_id":"1", "invoice_items":[{"product_key": "ITEM", "notes":"Test", "cost":10, "quantity":1}]}' \
-H "X-Ninja-Token: TOKEN" \
-H "X-API-SECRET: SECRET"

View File

@ -2,13 +2,14 @@
<html>
<head>
<meta charset="utf-8">
<title>A simple, clean, and responsive HTML invoice template</title>
<title>$invoice_number</title>
<!--<link href="{{asset('/vendors/css/bootstrap.min.css') }}" rel="stylesheet">
<link href="{{asset('/vendors/css/coreui.min.css') }}" rel="stylesheet">-->
<style>
html {
-webkit-print-color-adjust: exact;
}
html {
-webkit-print-color-adjust: exact;
}
.invoice-box {
max-width: 800px;
margin: auto;
@ -32,8 +33,8 @@
vertical-align: top;
}
.invoice-box table tr td:nth-child(2) {
text-align: right;
.invoice-box table tr td {
text-align: center;
}
.invoice-box table tr.top table td {
@ -69,10 +70,13 @@
}
.invoice-box table tr.total td:nth-child(2) {
border-top: 2px solid #eee;
border-top: 0px solid #eee;
font-weight: bold;
}
table.totals tr td {
text-align: right;
}
@media only screen and (max-width: 600px) {
.invoice-box table tr.top table td {
width: 100%;
@ -105,7 +109,7 @@
<body>
<div class="invoice-box">
<table cellpadding="0" cellspacing="0">
<table>
<tr class="top">
<td colspan="2">
<table>
@ -170,46 +174,37 @@
--}}
{!! $invoice->table(['product_key', 'notes', 'cost','quantity', 'discount', 'tax_name1', 'line_total']) !!}
<table>
<tr class="subtotal">
<td></td>
<td>
$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>
<div class="d-flex justify-content-end">
<table class="totals">
<tr class="subtotal">
<td>$subtotal_label:</td>
<td>$subtotal</td>
</tr>
<tr class="taxes">
<td>$taxes_label:</td>
<td>$taxes</td>
</tr>
<tr class="balance">
<td></td>
<td>
$balance_label: $balance
</td>
</tr>
{{-- line_taxes html is populated server side, with a class of line_items, you can customise your CSS here to override the defaults--}}
$line_taxes
</table>
<tr class="discount">
<td>$discount_label:</td>
<td>$discount</td>
</tr>
<tr class="total">
<td>$total_label:</td>
<td>$total</td>
</tr>
<tr class="balance">
<td>$balance_label:</td>
<td>$balance</td>
</tr>
</table>
</div>
</div>
</body>
</html>

View File

@ -46,7 +46,7 @@ class MarkInvoicePaidTest extends TestCase
foreach($invoice->payments as $payment) {
//Log::error($payment);
$this->assertEquals(10, $payment->amount);
$this->assertEquals($this->invoice->amount, $payment->amount);
}
$this->assertEquals(Invoice::STATUS_PAID, $invoice->status_id);

View File

@ -25,7 +25,7 @@ class InvoiceItemTest extends TestCase
public function testInvoiceItemTotalSimple()
{
$item = InvoiceItemFactory::create();
$item->qty = 1;
$item->quantity = 1;
$item->cost =10;
$item->is_amount_discount = true;
@ -42,7 +42,7 @@ class InvoiceItemTest extends TestCase
public function testInvoiceItemTotalSimpleWithDiscount()
{
$item = InvoiceItemFactory::create();
$item->qty = 1;
$item->quantity = 1;
$item->cost =10;
$item->is_amount_discount = true;
$item->discount = 2;
@ -59,7 +59,7 @@ class InvoiceItemTest extends TestCase
public function testInvoiceItemTotalSimpleWithDiscountWithPrecision()
{
$item = InvoiceItemFactory::create();
$item->qty = 1;
$item->quantity = 1;
$item->cost =10;
$item->is_amount_discount = true;
$item->discount = 2.521254522145214511;
@ -77,7 +77,7 @@ class InvoiceItemTest extends TestCase
public function testInvoiceItemTotalSimpleWithDiscountWithPrecisionWithSingleInclusiveTax()
{
$item = InvoiceItemFactory::create();
$item->qty = 1;
$item->quantity = 1;
$item->cost =10;
$item->is_amount_discount = true;
$item->discount = 2.521254522145214511;
@ -96,7 +96,7 @@ class InvoiceItemTest extends TestCase
public function testInvoiceItemTotalSimpleWithDiscountWithPrecisionWithSingleExclusiveTax()
{
$item = InvoiceItemFactory::create();
$item->qty = 1;
$item->quantity = 1;
$item->cost =10;
$item->is_amount_discount = true;
$item->discount = 2.521254522145214511;
@ -115,7 +115,7 @@ class InvoiceItemTest extends TestCase
public function testInvoiceItemTotalSimpleWithDiscountWithPrecisionWithDoubleInclusiveTax()
{
$item = InvoiceItemFactory::create();
$item->qty = 1;
$item->quantity = 1;
$item->cost =10;
$item->is_amount_discount = true;
$item->discount = 2.521254522145214511;
@ -135,7 +135,7 @@ class InvoiceItemTest extends TestCase
public function testInvoiceItemTotalSimpleWithDiscountWithPrecisionWithDoubleExclusiveTax()
{
$item = InvoiceItemFactory::create();
$item->qty = 1;
$item->quantity = 1;
$item->cost =10;
$item->is_amount_discount = true;
$item->discount = 2.521254522145214511;

View File

@ -47,13 +47,13 @@ class InvoiceTest extends TestCase
$line_items = [];
$item = InvoiceItemFactory::create();
$item->qty = 1;
$item->quantity = 1;
$item->cost =10;
$line_items[] = $item;
$item = InvoiceItemFactory::create();
$item->qty = 1;
$item->quantity = 1;
$item->cost =10;
$line_items[] = $item;
@ -156,7 +156,7 @@ class InvoiceTest extends TestCase
$line_items = [];
$item = InvoiceItemFactory::create();
$item->qty = 1;
$item->quantity = 1;
$item->cost =10;
$item->tax_rate1 = 10;
$item->tax_name1 = 10;
@ -164,7 +164,7 @@ class InvoiceTest extends TestCase
$line_items[] = $item;
$item = InvoiceItemFactory::create();
$item->qty = 1;
$item->quantity = 1;
$item->cost =10;
$item->tax_rate1 = 10;
$item->tax_name1 = 10;
@ -184,7 +184,7 @@ class InvoiceTest extends TestCase
$this->assertEquals($this->invoice_calc->getTotal(), 20);
$this->assertEquals($this->invoice_calc->getBalance(), 20);
$this->assertEquals($this->invoice_calc->getTotalTaxes(), 0);
$this->assertEquals($this->invoice_calc->getTaxMap()->count(), 2);
$this->assertEquals(count($this->invoice_calc->getTaxMap()), 1);
}
public function testLineItemTaxRatesExclusiveTaxes()
@ -193,7 +193,7 @@ class InvoiceTest extends TestCase
$line_items = [];
$item = InvoiceItemFactory::create();
$item->qty = 1;
$item->quantity = 1;
$item->cost =10;
$item->tax_rate1 = 10;
$item->tax_name1 = 10;
@ -201,7 +201,7 @@ class InvoiceTest extends TestCase
$line_items[] = $item;
$item = InvoiceItemFactory::create();
$item->qty = 1;
$item->quantity = 1;
$item->cost =10;
$item->tax_rate1 = 10;
$item->tax_name1 = 10;
@ -223,7 +223,7 @@ class InvoiceTest extends TestCase
$this->assertEquals($this->invoice_calc->getTotal(), 26);
$this->assertEquals($this->invoice_calc->getBalance(), 26);
$this->assertEquals($this->invoice_calc->getTotalTaxes(), 6);
$this->assertEquals($this->invoice_calc->getTaxMap()->count(), 2);
$this->assertEquals(count($this->invoice_calc->getTaxMap()), 1);
}
}