diff --git a/app/Factory/ProductFactory.php b/app/Factory/ProductFactory.php index 6852be350f19..a9078606408d 100644 --- a/app/Factory/ProductFactory.php +++ b/app/Factory/ProductFactory.php @@ -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 = ''; diff --git a/app/Helpers/Invoice/InvoiceCalc.php b/app/Helpers/Invoice/InvoiceCalc.php index 62b6bc76a4f9..9fa15042a6a2 100644 --- a/app/Helpers/Invoice/InvoiceCalc.php +++ b/app/Helpers/Invoice/InvoiceCalc.php @@ -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; diff --git a/app/Helpers/Invoice/InvoiceItemCalc.php b/app/Helpers/Invoice/InvoiceItemCalc.php index 3fc10fc0d7ce..fa5a3339ba4d 100644 --- a/app/Helpers/Invoice/InvoiceItemCalc.php +++ b/app/Helpers/Invoice/InvoiceItemCalc.php @@ -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; } diff --git a/app/Http/Requests/Product/StoreProductRequest.php b/app/Http/Requests/Product/StoreProductRequest.php index 9962712e246e..39123a998c59 100644 --- a/app/Http/Requests/Product/StoreProductRequest.php +++ b/app/Http/Requests/Product/StoreProductRequest.php @@ -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', ]; } diff --git a/app/Http/Requests/Product/UpdateProductRequest.php b/app/Http/Requests/Product/UpdateProductRequest.php index c4a22728114e..cee8945b0453 100644 --- a/app/Http/Requests/Product/UpdateProductRequest.php +++ b/app/Http/Requests/Product/UpdateProductRequest.php @@ -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', ]; } diff --git a/app/Listeners/Invoice/CreateInvoicePdf.php b/app/Listeners/Invoice/CreateInvoicePdf.php index 7fd70b729771..cbcb7825ea2b 100644 --- a/app/Listeners/Invoice/CreateInvoicePdf.php +++ b/app/Listeners/Invoice/CreateInvoicePdf.php @@ -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); diff --git a/app/Models/Product.php b/app/Models/Product.php index c06fa42140ad..36eca4d1f0df 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -31,7 +31,7 @@ class Product extends BaseModel 'notes', 'cost', 'price', - 'qty', + 'quantity', 'tax_name1', 'tax_name2', 'tax_rate1', diff --git a/app/Transformers/ProductTransformer.php b/app/Transformers/ProductTransformer.php index 3b9c814c85e3..600119227aba 100644 --- a/app/Transformers/ProductTransformer.php +++ b/app/Transformers/ProductTransformer.php @@ -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 ?: '', diff --git a/app/Utils/Number.php b/app/Utils/Number.php index 5aaa05fef8da..b822e06a589a 100644 --- a/app/Utils/Number.php +++ b/app/Utils/Number.php @@ -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); diff --git a/app/Utils/Traits/MakesInvoiceValues.php b/app/Utils/Traits/MakesInvoiceValues.php index f83052e845c6..99731f61af87 100644 --- a/app/Utils/Traits/MakesInvoiceValues.php +++ b/app/Utils/Traits/MakesInvoiceValues.php @@ -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 rows with line item + * aggregate data + */ + private function makeLineTaxes() :string + { + + $tax_map = $this->calc()->getTaxMap(); + + $data = ''; + + foreach($tax_map as $tax) + { + $data .= ''; + $data .= ''. $tax['name'] .''; + $data .= ''. Number::formatMoney($tax['total'], $this->client->currency(), $this->client->country, $this->client->getMergedSettings()) .''; + } + + return $data; + } } \ No newline at end of file diff --git a/database/factories/ProductFactory.php b/database/factories/ProductFactory.php index de46be5c4fb3..00b978e27891 100644 --- a/database/factories/ProductFactory.php +++ b/database/factories/ProductFactory.php @@ -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', diff --git a/database/migrations/2014_10_13_000000_create_users_table.php b/database/migrations/2014_10_13_000000_create_users_table.php index fb1e8f38db94..dbefcee0018b 100644 --- a/database/migrations/2014_10_13_000000_create_users_table.php +++ b/database/migrations/2014_10_13_000000_create_users_table.php @@ -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); diff --git a/docs/api.rst b/docs/api.rst index 602ee4826c63..0f07e21c02b6 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -79,7 +79,7 @@ You can also update a client by specifying a value for ‘id’. Next, here’s .. 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" diff --git a/resources/views/pdf/design1.blade.php b/resources/views/pdf/design1.blade.php index 7ac39311cba1..79152e112ac4 100644 --- a/resources/views/pdf/design1.blade.php +++ b/resources/views/pdf/design1.blade.php @@ -2,13 +2,14 @@ - A simple, clean, and responsive HTML invoice template - + $invoice_number +