1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00
invoiceninja/app/Console/Commands/CreateTestData.php

315 lines
9.2 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-09-27 13:48:41 +02:00
use App\Ninja\Repositories\TaskRepository;
use App\Ninja\Repositories\ProjectRepository;
2017-07-09 19:38:24 +02:00
use App\Models\Client;
use App\Models\TaxRate;
use App\Models\Project;
use App\Models\ExpenseCategory;
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-05-01 19:15:33 +02:00
protected $signature = 'ninja:create-test-data {count=1} {create_account=false} {--database}';
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-09-27 13:48:41 +02:00
* @param TaskRepository $taskRepo
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,
2017-09-27 13:48:41 +02:00
TaskRepository $taskRepo,
ProjectRepository $projectRepo,
2017-03-09 17:07:20 +01:00
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-09-27 13:48:41 +02:00
$this->taskRepo = $taskRepo;
$this->projectRepo = $projectRepo;
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()) {
2017-05-01 19:15:33 +02:00
$this->info('Unable to run in production');
2016-05-04 13:53:27 +02:00
return false;
}
2016-08-14 21:29:34 +02:00
2017-10-24 09:59:26 +02:00
$this->info(date('r').' Running CreateTestData...');
2016-05-04 13:53:27 +02:00
$this->count = $this->argument('count');
2016-08-14 21:29:34 +02:00
2017-05-01 14:17:52 +02:00
if ($database = $this->option('database')) {
config(['database.default' => $database]);
}
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();
2017-07-09 19:38:24 +02:00
$this->createOtherObjects();
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);
2017-09-13 10:56:26 +02:00
$this->createInvoices($client, true);
2017-09-27 13:48:41 +02:00
$this->createTasks($client);
2016-05-04 13:53:27 +02:00
}
}
/**
* @param $client
*/
2017-09-13 10:56:26 +02:00
private function createInvoices($client, $isQuote = false)
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,
2017-09-13 10:56:26 +02:00
'is_quote' => $isQuote,
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
2017-09-14 10:58:18 +02:00
if (! $isQuote) {
$this->createPayment($client, $invoice);
}
2016-05-04 13:53:27 +02:00
}
}
/**
* @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
2017-09-27 13:48:41 +02:00
private function createTasks($client)
{
$data = [
'client_id' => $client->id,
2017-09-27 14:26:55 +02:00
'name' => $this->faker->sentence(3),
2017-09-27 13:48:41 +02:00
];
$project = $this->projectRepo->save($data);
for ($i = 0; $i < $this->count; $i++) {
$startTime = date_create()->modify(rand(-100, 100) . ' days')->format('U');
$endTime = $startTime + (60 * 60 * 2);
$timeLog = "[[{$startTime},{$endTime}]]";
$data = [
'client_id' => $client->id,
'project_id' => $project->id,
'description' => $this->faker->text($this->faker->numberBetween(50, 300)),
'time_log' => $timeLog,
];
$this->taskRepo->save(false, $data);
}
}
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),
2017-09-13 10:56:26 +02:00
'expense_date' => date_create()->modify(rand(-100, 100) . ' days')->format('Y-m-d'),
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);
}
}
2017-07-09 19:38:24 +02:00
private function createOtherObjects()
{
$this->createTaxRate('Tax 1', 10, 1);
$this->createTaxRate('Tax 2', 20, 2);
$this->createCategory('Category 1', 1);
$this->createCategory('Category 1', 2);
$this->createProject('Project 1', 1);
$this->createProject('Project 2', 2);
}
private function createTaxRate($name, $rate, $publicId)
{
$taxRate = new TaxRate();
$taxRate->name = $name;
$taxRate->rate = $rate;
$taxRate->account_id = 1;
$taxRate->user_id = 1;
$taxRate->public_id = $publicId;
$taxRate->save();
}
private function createCategory($name, $publicId)
{
$category = new ExpenseCategory();
$category->name = $name;
$category->account_id = 1;
$category->user_id = 1;
$category->public_id = $publicId;
$category->save();
}
private function createProject($name, $publicId)
{
$project = new Project();
$project->name = $name;
$project->account_id = 1;
$project->client_id = 1;
$project->user_id = 1;
$project->public_id = $publicId;
$project->save();
}
/**
* @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()
{
2017-05-01 19:15:33 +02:00
return [];
2016-05-04 13:53:27 +02:00
}
}