mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-11-04 02:37:34 -05:00 
			
		
		
		
	Invoice Item Array factory
This commit is contained in:
		
							parent
							
								
									f9a5bd3444
								
							
						
					
					
						commit
						ea07174df6
					
				@ -12,6 +12,7 @@
 | 
				
			|||||||
namespace App\Factory;
 | 
					namespace App\Factory;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use Illuminate\Support\Carbon;
 | 
					use Illuminate\Support\Carbon;
 | 
				
			||||||
 | 
					//use Faker\Generator as Faker;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class InvoiceItemFactory
 | 
					class InvoiceItemFactory
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
@ -39,4 +40,38 @@ class InvoiceItemFactory
 | 
				
			|||||||
		return $item;
 | 
							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;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -262,19 +262,25 @@ trait MakesInvoiceValues
 | 
				
			|||||||
     */
 | 
					     */
 | 
				
			||||||
    private function transformColumns(array $columns) :array
 | 
					    private function transformColumns(array $columns) :array
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
    	return str_replace(['custom_invoice_label1', 
 | 
					    
 | 
				
			||||||
 | 
					    	return str_replace([
 | 
				
			||||||
 | 
					                'custom_invoice_label1', 
 | 
				
			||||||
    			'custom_invoice_label2', 
 | 
					    			'custom_invoice_label2', 
 | 
				
			||||||
    			'custom_invoice_label3',
 | 
					    			'custom_invoice_label3',
 | 
				
			||||||
    			'custom_invoice_label4',
 | 
					    			'custom_invoice_label4',
 | 
				
			||||||
    			'tax_name1',
 | 
					    			'tax_name1',
 | 
				
			||||||
    			'tax_name2'], 
 | 
					    			'tax_name2'
 | 
				
			||||||
    			['custom_invoice_value1',
 | 
					            ], 
 | 
				
			||||||
    			'custom_invoice_value2',
 | 
								[
 | 
				
			||||||
    			'custom_invoice_value3',
 | 
					                'custom_invoice_value1',
 | 
				
			||||||
    			'custom_invoice_value4',
 | 
					                'custom_invoice_value2',
 | 
				
			||||||
    			'tax_rate1',
 | 
					                'custom_invoice_value3',
 | 
				
			||||||
    			'tax_rate2'], 
 | 
					                'custom_invoice_value4',
 | 
				
			||||||
    			$columns);
 | 
					                'tax_rate1',
 | 
				
			||||||
 | 
					                'tax_rate2'
 | 
				
			||||||
 | 
					            ], 
 | 
				
			||||||
 | 
								$columns);
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
@ -285,5 +291,24 @@ trait MakesInvoiceValues
 | 
				
			|||||||
    private function transformLineItems(array $items) :array
 | 
					    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;    
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -2,6 +2,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
use App\DataMapper\ClientSettings;
 | 
					use App\DataMapper\ClientSettings;
 | 
				
			||||||
use App\DataMapper\CompanySettings;
 | 
					use App\DataMapper\CompanySettings;
 | 
				
			||||||
 | 
					use App\Factory\InvoiceItemFactory;
 | 
				
			||||||
use Faker\Generator as Faker;
 | 
					use Faker\Generator as Faker;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
$factory->define(App\Models\Invoice::class, function (Faker $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),
 | 
							'po_number' => $faker->text(10),
 | 
				
			||||||
		'invoice_date' => $faker->date(),
 | 
							'invoice_date' => $faker->date(),
 | 
				
			||||||
		'due_date' => $faker->date(),
 | 
							'due_date' => $faker->date(),
 | 
				
			||||||
		'line_items' => false,
 | 
							'line_items' => InvoiceItemFactory::generate(5),
 | 
				
			||||||
		'backup' => '', 
 | 
							'backup' => '', 
 | 
				
			||||||
    ];
 | 
					    ];
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user