diff --git a/app/Factory/InvoiceItemFactory.php b/app/Factory/InvoiceItemFactory.php index d30f89bb19a4..2396e3df154f 100644 --- a/app/Factory/InvoiceItemFactory.php +++ b/app/Factory/InvoiceItemFactory.php @@ -12,6 +12,7 @@ namespace App\Factory; use Illuminate\Support\Carbon; +//use Faker\Generator as Faker; class InvoiceItemFactory { @@ -39,4 +40,38 @@ class InvoiceItemFactory return $item; } + + /** + * Generates an array of dummy data for invoice items + * @param int $items Number of line items to create + * @return array array of objects + */ + public static function generate(int $items = 1) :array + { + $faker = \Faker\Factory::create(); + + $data = []; + + for($x=0; $x<$items; $x++) + { + + $item = self::create(); + $item->quantity = $faker->numberBetween(1,10); + $item->cost = $faker->randomFloat(2, 1, 1000); + $item->line_total = $item->quantity * $item->cost; + $item->is_amount_discount = $faker->boolean(); + $item->discount = $faker->numberBetween(1,10); + $item->notes = $faker->realText(20); + $item->product_key = $faker->word(); + $item->custom_value1 = $faker->realText(10); + $item->custom_value2 = $faker->realText(10); + $item->custom_value3 = $faker->realText(10); + $item->custom_value4 = $faker->realText(10); + + $data[] = $item; + } + + return $data; + } + } diff --git a/app/Utils/Traits/MakesInvoiceValues.php b/app/Utils/Traits/MakesInvoiceValues.php index 20709e7e4368..c5cd80214a9d 100644 --- a/app/Utils/Traits/MakesInvoiceValues.php +++ b/app/Utils/Traits/MakesInvoiceValues.php @@ -262,19 +262,25 @@ trait MakesInvoiceValues */ private function transformColumns(array $columns) :array { - return str_replace(['custom_invoice_label1', + + return str_replace([ + 'custom_invoice_label1', 'custom_invoice_label2', 'custom_invoice_label3', 'custom_invoice_label4', 'tax_name1', - 'tax_name2'], - ['custom_invoice_value1', - 'custom_invoice_value2', - 'custom_invoice_value3', - 'custom_invoice_value4', - 'tax_rate1', - 'tax_rate2'], - $columns); + 'tax_name2' + ], + [ + 'custom_invoice_value1', + 'custom_invoice_value2', + 'custom_invoice_value3', + 'custom_invoice_value4', + 'tax_rate1', + 'tax_rate2' + ], + $columns); + } /** @@ -285,5 +291,24 @@ trait MakesInvoiceValues private function transformLineItems(array $items) :array { + foreach($items as $item) + { + + $item->cost = Number::formatMoney($item->cost, $this->client->currency(), $this->client->country, $this->client->getMergedSettings); + $item->line_total = Number::formatMoney($item->line_total, $this->client->currency(), $this->client->country, $this->client->getMergedSettings); + + if(isset($item->discount) && $item->discount > 0) + { + + if($item->is_amount_discount) + $item->discount = Number::formatMoney($item->discount, $this->client->currency(), $this->client->country, $this->client->getMergedSettings); + else + $item->discount = $item->discount . '%'; + } + + } + + + return $items; } } \ No newline at end of file diff --git a/database/factories/InvoiceFactory.php b/database/factories/InvoiceFactory.php index ddcc7e586386..0a1a75b7bf0d 100644 --- a/database/factories/InvoiceFactory.php +++ b/database/factories/InvoiceFactory.php @@ -2,6 +2,7 @@ use App\DataMapper\ClientSettings; use App\DataMapper\CompanySettings; +use App\Factory\InvoiceItemFactory; use Faker\Generator as Faker; $factory->define(App\Models\Invoice::class, function (Faker $faker) { @@ -22,7 +23,7 @@ $factory->define(App\Models\Invoice::class, function (Faker $faker) { 'po_number' => $faker->text(10), 'invoice_date' => $faker->date(), 'due_date' => $faker->date(), - 'line_items' => false, + 'line_items' => InvoiceItemFactory::generate(5), 'backup' => '', ]; }); \ No newline at end of file