190 lines
5.5 KiB
PHP
Raw Normal View History

2020-01-13 15:16:35 +03:00
<?php
use App\Events\Sale\InvoiceCreated;
use App\Events\Sale\InvoiceSent;
2020-01-23 17:54:39 +03:00
use App\Events\Sale\InvoiceViewed;
2020-01-13 15:16:35 +03:00
use App\Events\Sale\PaymentReceived;
use App\Jobs\Sale\UpdateInvoice;
use App\Models\Auth\User;
use App\Models\Common\Contact;
use App\Models\Common\Item;
use App\Models\Sale\Invoice;
2020-03-15 20:29:07 +03:00
use App\Models\Setting\Tax;
2020-01-13 15:16:35 +03:00
use Faker\Generator as Faker;
use Jenssegers\Date\Date;
$user = User::first();
$company = $user->companies()->first();
$factory->define(Invoice::class, function (Faker $faker) use ($company) {
session(['company_id' => $company->id]);
setting()->setExtraColumns(['company_id' => $company->id]);
$invoiced_at = $faker->dateTimeBetween(now()->startOfYear(), now()->endOfYear())->format('Y-m-d');
$due_at = Date::parse($invoiced_at)->addDays(setting('invoice.payment_terms'))->format('Y-m-d');
2020-01-21 19:47:15 +03:00
$types = (string) setting('contact.type.customer', 'customer');
$contacts = Contact::type(explode(',', $types))->enabled()->get();
2020-01-13 15:16:35 +03:00
if ($contacts->count()) {
$contact = $contacts->random(1)->first();
} else {
2020-03-15 20:29:07 +03:00
$contact = factory(Contact::class)->states('enabled', 'customer')->create();
2020-01-13 15:16:35 +03:00
}
2020-01-23 17:54:39 +03:00
$statuses = ['draft', 'sent', 'viewed', 'partial', 'paid'];
2020-01-13 15:16:35 +03:00
return [
'company_id' => $company->id,
'invoiced_at' => $invoiced_at,
'due_at' => $due_at,
'invoice_number' => setting('invoice.number_prefix') . $faker->randomNumber(setting('invoice.number_digit')),
2020-01-13 17:16:22 +03:00
'currency_code' => setting('default.currency'),
2020-01-13 15:16:35 +03:00
'currency_rate' => '1',
'notes' => $faker->text(5),
'category_id' => $company->categories()->type('income')->get()->random(1)->pluck('id')->first(),
'contact_id' => $contact->id,
2020-01-22 16:35:22 +03:00
'contact_name' => $contact->name,
'contact_email' => $contact->email,
2020-01-13 15:16:35 +03:00
'contact_tax_number' => $contact->tax_number,
2020-01-13 17:16:22 +03:00
'contact_phone' => $contact->phone,
'contact_address' => $contact->address,
2020-01-13 15:16:35 +03:00
'status' => $faker->randomElement($statuses),
'amount' => '0',
];
});
$factory->state(Invoice::class, 'draft', ['status' => 'draft']);
$factory->state(Invoice::class, 'sent', ['status' => 'sent']);
2020-01-23 17:54:39 +03:00
$factory->state(Invoice::class, 'viewed', ['status' => 'viewed']);
2020-01-22 16:35:22 +03:00
$factory->state(Invoice::class, 'partial', ['status' => 'partial']);
2020-01-13 15:16:35 +03:00
$factory->state(Invoice::class, 'paid', ['status' => 'paid']);
$factory->state(Invoice::class, 'recurring', function (Faker $faker) {
$frequencies = ['monthly', 'weekly'];
return [
'recurring_frequency' => 'yes',
'recurring_interval' => '1',
'recurring_custom_frequency' => $faker->randomElement($frequencies),
'recurring_count' => '1',
];
});
$factory->state(Invoice::class, 'items', function (Faker $faker) use ($company) {
session(['company_id' => $company->id]);
setting()->setExtraColumns(['company_id' => $company->id]);
2020-01-13 17:16:22 +03:00
$amount = $faker->randomFloat(2, 1, 1000);
2020-01-13 15:16:35 +03:00
2020-03-15 20:29:07 +03:00
$taxes = Tax::enabled()->get();
if ($taxes->count()) {
$tax = $taxes->random(1)->first();
} else {
$tax = factory(Tax::class)->states('enabled')->create();
}
2020-01-13 15:16:35 +03:00
$items = Item::enabled()->get();
if ($items->count()) {
$item = $items->random(1)->first();
} else {
2020-03-15 20:29:07 +03:00
$item = factory(Item::class)->states('enabled')->create();
2020-01-13 15:16:35 +03:00
}
2020-03-15 20:29:07 +03:00
$items = [
[
'name' => $item->name,
'item_id' => $item->id,
'tax_id' => [$tax->id],
'quantity' => '1',
'price' => $amount,
'currency' => setting('default.currency'),
]
];
2020-01-13 15:16:35 +03:00
return [
'items' => $items,
'recurring_frequency' => 'no',
];
});
$factory->afterCreating(Invoice::class, function ($invoice, $faker) use ($company) {
session(['company_id' => $company->id]);
setting()->setExtraColumns(['company_id' => $company->id]);
2020-01-23 17:54:39 +03:00
$init_status = $invoice->status;
2020-01-19 00:39:53 +03:00
2020-01-23 17:54:39 +03:00
$invoice->status = 'draft';
2020-01-13 15:16:35 +03:00
event(new InvoiceCreated($invoice));
2020-01-23 17:54:39 +03:00
$invoice->status = $init_status;
2020-01-13 15:16:35 +03:00
2020-01-13 17:16:22 +03:00
$amount = $faker->randomFloat(2, 1, 1000);
2020-01-13 15:16:35 +03:00
2020-03-15 20:29:07 +03:00
$taxes = Tax::enabled()->get();
if ($taxes->count()) {
$tax = $taxes->random(1)->first();
} else {
$tax = factory(Tax::class)->states('enabled')->create();
}
2020-01-13 15:16:35 +03:00
$items = Item::enabled()->get();
if ($items->count()) {
$item = $items->random(1)->first();
} else {
2020-03-15 20:29:07 +03:00
$item = factory(Item::class)->states('enabled')->create();
2020-01-13 15:16:35 +03:00
}
2020-03-15 20:29:07 +03:00
$items = [
[
'name' => $item->name,
'item_id' => $item->id,
'tax_id' => [$tax->id],
'quantity' => '1',
'price' => $amount,
'currency' => $invoice->currency_code,
]
];
2020-01-13 15:16:35 +03:00
$request = [
'items' => $items,
'recurring_frequency' => 'no',
];
$updated_invoice = dispatch_now(new UpdateInvoice($invoice, $request));
2020-01-23 17:54:39 +03:00
switch ($init_status) {
case 'sent':
2020-02-02 01:52:09 +03:00
event(new InvoiceSent($updated_invoice));
2020-01-23 17:54:39 +03:00
break;
case 'viewed':
2020-02-02 01:52:09 +03:00
$updated_invoice->status = 'sent';
event(new InvoiceViewed($updated_invoice));
$updated_invoice->status = $init_status;
2020-01-23 17:54:39 +03:00
break;
case 'partial':
case 'paid':
$payment_request = [
2020-02-02 01:52:09 +03:00
'paid_at' => $updated_invoice->due_at,
2020-01-23 17:54:39 +03:00
];
if ($init_status == 'partial') {
$payment_request['amount'] = round($amount / 3, $invoice->currency->precision);
2020-01-23 17:54:39 +03:00
}
2020-01-22 16:35:22 +03:00
2020-01-23 17:54:39 +03:00
event(new PaymentReceived($updated_invoice, $payment_request));
2020-01-22 16:35:22 +03:00
2020-01-23 17:54:39 +03:00
break;
2020-01-13 15:16:35 +03:00
}
});