Invoice Item Array factory

This commit is contained in:
David Bomba 2019-09-04 08:16:53 +10:00
parent f9a5bd3444
commit ea07174df6
3 changed files with 71 additions and 10 deletions

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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' => '',
];
});