1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 08:21:34 +02:00
invoiceninja/app/Console/Commands/CreateTestData.php

224 lines
6.4 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
namespace App\Console\Commands;
2016-05-04 13:53:27 +02:00
2017-03-09 17:07:20 +01:00
use App\Ninja\Repositories\AccountRepository;
2016-05-04 13:53:27 +02:00
use App\Ninja\Repositories\ClientRepository;
2017-01-30 20:40:43 +01:00
use App\Ninja\Repositories\ExpenseRepository;
2016-05-04 13:53:27 +02:00
use App\Ninja\Repositories\InvoiceRepository;
use App\Ninja\Repositories\PaymentRepository;
use App\Ninja\Repositories\VendorRepository;
2017-01-30 20:40:43 +01:00
use Auth;
use Faker\Factory;
use Illuminate\Console\Command;
use Utils;
2016-05-04 13:53:27 +02:00
/**
2017-01-30 20:40:43 +01:00
* Class CreateTestData.
*/
2016-05-04 13:53:27 +02:00
class CreateTestData extends Command
{
/**
* @var string
*/
2016-05-04 13:53:27 +02:00
protected $description = 'Create Test Data';
/**
* @var string
*/
2017-03-09 17:07:20 +01:00
protected $signature = 'ninja:create-test-data {count=1} {create_account=false}';
2016-05-04 13:53:27 +02:00
/**
* @var
*/
2016-05-04 13:53:27 +02:00
protected $token;
/**
* CreateTestData constructor.
2017-01-30 20:40:43 +01:00
*
* @param ClientRepository $clientRepo
* @param InvoiceRepository $invoiceRepo
* @param PaymentRepository $paymentRepo
2017-01-30 20:40:43 +01:00
* @param VendorRepository $vendorRepo
* @param ExpenseRepository $expenseRepo
2017-03-09 17:07:20 +01:00
* @param AccountRepository $accountRepo
*/
2016-05-04 13:53:27 +02:00
public function __construct(
2016-08-14 21:29:34 +02:00
ClientRepository $clientRepo,
InvoiceRepository $invoiceRepo,
2016-05-04 13:53:27 +02:00
PaymentRepository $paymentRepo,
VendorRepository $vendorRepo,
2017-03-09 17:07:20 +01:00
ExpenseRepository $expenseRepo,
AccountRepository $accountRepo)
2016-05-04 13:53:27 +02:00
{
parent::__construct();
$this->faker = Factory::create();
2016-08-14 21:29:34 +02:00
2016-05-04 13:53:27 +02:00
$this->clientRepo = $clientRepo;
$this->invoiceRepo = $invoiceRepo;
$this->paymentRepo = $paymentRepo;
$this->vendorRepo = $vendorRepo;
$this->expenseRepo = $expenseRepo;
2017-03-09 17:07:20 +01:00
$this->accountRepo = $accountRepo;
2016-05-04 13:53:27 +02:00
}
/**
* @return bool
*/
2016-05-04 13:53:27 +02:00
public function fire()
{
if (Utils::isNinjaProd()) {
return false;
}
2016-08-14 21:29:34 +02:00
2016-05-04 13:53:27 +02:00
$this->info(date('Y-m-d').' Running CreateTestData...');
$this->count = $this->argument('count');
2016-08-14 21:29:34 +02:00
2017-03-09 17:07:20 +01:00
if (filter_var($this->argument('create_account'), FILTER_VALIDATE_BOOLEAN)) {
$this->info('Creating new account...');
$account = $this->accountRepo->create(
$this->faker->firstName,
$this->faker->lastName,
$this->faker->safeEmail
);
Auth::login($account->users[0]);
} else {
$this->info('Using first account...');
Auth::loginUsingId(1);
}
2016-05-04 13:53:27 +02:00
$this->createClients();
$this->createVendors();
2016-08-14 21:29:34 +02:00
2016-05-04 13:53:27 +02:00
$this->info('Done');
}
2016-08-14 21:29:34 +02:00
2016-05-04 13:53:27 +02:00
private function createClients()
{
2017-01-30 20:40:43 +01:00
for ($i = 0; $i < $this->count; $i++) {
2016-05-04 13:53:27 +02:00
$data = [
'name' => $this->faker->name,
'address1' => $this->faker->streetAddress,
'address2' => $this->faker->secondaryAddress,
'city' => $this->faker->city,
'state' => $this->faker->state,
'postal_code' => $this->faker->postcode,
'contacts' => [[
'first_name' => $this->faker->firstName,
'last_name' => $this->faker->lastName,
2016-08-14 21:29:34 +02:00
'email' => $this->faker->safeEmail,
2016-05-04 13:53:27 +02:00
'phone' => $this->faker->phoneNumber,
2017-01-30 20:40:43 +01:00
]],
2016-05-04 13:53:27 +02:00
];
$client = $this->clientRepo->save($data);
$this->info('Client: ' . $client->name);
2016-08-14 21:29:34 +02:00
2016-05-04 13:53:27 +02:00
$this->createInvoices($client);
}
}
/**
* @param $client
*/
2016-05-04 13:53:27 +02:00
private function createInvoices($client)
2016-08-14 21:29:34 +02:00
{
2017-01-30 20:40:43 +01:00
for ($i = 0; $i < $this->count; $i++) {
2016-05-04 13:53:27 +02:00
$data = [
2017-04-09 15:03:10 +02:00
'is_public' => true,
2016-05-04 13:53:27 +02:00
'client_id' => $client->id,
2016-08-14 21:29:34 +02:00
'invoice_date_sql' => date_create()->modify(rand(-100, 100) . ' days')->format('Y-m-d'),
'due_date_sql' => date_create()->modify(rand(-100, 100) . ' days')->format('Y-m-d'),
2016-05-04 13:53:27 +02:00
'invoice_items' => [[
'product_key' => $this->faker->word,
'qty' => $this->faker->randomDigit + 1,
'cost' => $this->faker->randomFloat(2, 1, 10),
2017-01-30 20:40:43 +01:00
'notes' => $this->faker->text($this->faker->numberBetween(50, 300)),
]],
2016-05-04 13:53:27 +02:00
];
$invoice = $this->invoiceRepo->save($data);
$this->info('Invoice: ' . $invoice->invoice_number);
2016-08-14 21:29:34 +02:00
2016-05-04 13:53:27 +02:00
$this->createPayment($client, $invoice);
}
}
/**
* @param $client
2016-07-21 14:35:23 +02:00
* @param $invoice
*/
2016-07-21 14:35:23 +02:00
private function createPayment($client, $invoice)
2016-05-04 13:53:27 +02:00
{
$data = [
'invoice_id' => $invoice->id,
'client_id' => $client->id,
2016-09-11 16:30:23 +02:00
'amount' => $this->faker->randomFloat(2, 0, $invoice->amount),
'payment_date_sql' => date_create()->modify(rand(-100, 100) . ' days')->format('Y-m-d'),
2016-05-04 13:53:27 +02:00
];
2016-08-14 21:29:34 +02:00
2016-05-04 13:53:27 +02:00
$payment = $this->paymentRepo->save($data);
2016-08-14 21:29:34 +02:00
2016-05-04 13:53:27 +02:00
$this->info('Payment: ' . $payment->amount);
}
2016-08-14 21:29:34 +02:00
2016-05-04 13:53:27 +02:00
private function createVendors()
{
2017-01-30 20:40:43 +01:00
for ($i = 0; $i < $this->count; $i++) {
2016-05-04 13:53:27 +02:00
$data = [
'name' => $this->faker->name,
'address1' => $this->faker->streetAddress,
'address2' => $this->faker->secondaryAddress,
'city' => $this->faker->city,
'state' => $this->faker->state,
'postal_code' => $this->faker->postcode,
'vendor_contacts' => [[
2016-05-04 13:53:27 +02:00
'first_name' => $this->faker->firstName,
'last_name' => $this->faker->lastName,
2016-08-14 21:29:34 +02:00
'email' => $this->faker->safeEmail,
2016-05-04 13:53:27 +02:00
'phone' => $this->faker->phoneNumber,
2017-01-30 20:40:43 +01:00
]],
2016-05-04 13:53:27 +02:00
];
$vendor = $this->vendorRepo->save($data);
$this->info('Vendor: ' . $vendor->name);
2016-08-14 21:29:34 +02:00
2016-05-04 13:53:27 +02:00
$this->createExpense($vendor);
}
}
/**
2016-07-21 14:35:23 +02:00
* @param $vendor
*/
2016-07-21 14:35:23 +02:00
private function createExpense($vendor)
2016-05-04 13:53:27 +02:00
{
2017-01-30 20:40:43 +01:00
for ($i = 0; $i < $this->count; $i++) {
2016-05-04 13:53:27 +02:00
$data = [
'vendor_id' => $vendor->id,
'amount' => $this->faker->randomFloat(2, 1, 10),
'expense_date' => null,
2017-03-09 17:07:20 +01:00
'public_notes' => '',
2016-05-04 13:53:27 +02:00
];
2016-08-14 21:29:34 +02:00
2016-05-04 13:53:27 +02:00
$expense = $this->expenseRepo->save($data);
$this->info('Expense: ' . $expense->amount);
}
}
/**
* @return array
*/
2016-05-04 13:53:27 +02:00
protected function getArguments()
{
return [];
2016-05-04 13:53:27 +02:00
}
/**
* @return array
*/
2016-05-04 13:53:27 +02:00
protected function getOptions()
{
return [];
2016-05-04 13:53:27 +02:00
}
}