283 lines
7.8 KiB
PHP
Raw Normal View History

2020-01-13 15:16:35 +03:00
<?php
2020-10-14 17:07:59 +03:00
namespace Database\Factories;
use App\Abstracts\Factory;
2020-03-28 17:54:36 +03:00
use App\Events\Sale\InvoiceCancelled;
2020-01-13 15:16:35 +03:00
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\Common\Contact;
use App\Models\Common\Item;
2020-10-14 17:07:59 +03:00
use App\Models\Sale\Invoice as Model;
2020-03-15 20:29:07 +03:00
use App\Models\Setting\Tax;
2020-03-25 20:21:42 +03:00
use App\Utilities\Date;
2020-11-18 14:30:17 +03:00
use App\Utilities\Overrider;
2020-01-13 15:16:35 +03:00
2020-10-14 17:07:59 +03:00
class Invoice extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Model::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
$invoiced_at = $this->faker->dateTimeBetween(now()->startOfYear(), now()->endOfYear())->format('Y-m-d H:i:s');
$due_at = Date::parse($invoiced_at)->addDays(setting('invoice.payment_terms'))->format('Y-m-d H:i:s');
$contacts = Contact::customer()->enabled()->get();
if ($contacts->count()) {
$contact = $contacts->random(1)->first();
} else {
$contact = Contact::factory()->customer()->enabled()->create();
}
$statuses = ['draft', 'sent', 'viewed', 'partial', 'paid', 'cancelled'];
return [
'company_id' => $this->company->id,
'invoiced_at' => $invoiced_at,
'due_at' => $due_at,
'invoice_number' => setting('invoice.number_prefix') . $this->faker->randomNumber(setting('invoice.number_digit')),
'currency_code' => setting('default.currency'),
'currency_rate' => '1',
'notes' => $this->faker->text(5),
'category_id' => $this->company->categories()->income()->get()->random(1)->pluck('id')->first(),
'contact_id' => $contact->id,
'contact_name' => $contact->name,
'contact_email' => $contact->email,
'contact_tax_number' => $contact->tax_number,
'contact_phone' => $contact->phone,
'contact_address' => $contact->address,
'status' => $this->faker->randomElement($statuses),
'amount' => '0',
];
}
/**
* Indicate that the model status is draft.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
public function draft()
{
2020-10-15 00:08:41 +03:00
return $this->state([
'status' => 'draft',
]);
2020-10-14 17:07:59 +03:00
}
2020-01-13 15:16:35 +03:00
2020-10-14 17:07:59 +03:00
/**
* Indicate that the model status is sent.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
public function sent()
{
2020-10-15 00:08:41 +03:00
return $this->state([
'status' => 'sent',
]);
2020-10-14 17:07:59 +03:00
}
2020-01-13 15:16:35 +03:00
2020-10-14 17:07:59 +03:00
/**
* Indicate that the model status is viewed.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
public function viewed()
{
2020-10-15 00:08:41 +03:00
return $this->state([
'status' => 'viewed',
]);
2020-10-14 17:07:59 +03:00
}
2020-01-13 15:16:35 +03:00
2020-10-14 17:07:59 +03:00
/**
* Indicate that the model status is partial.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
public function partial()
{
2020-10-15 00:08:41 +03:00
return $this->state([
'status' => 'partial',
]);
2020-10-14 17:07:59 +03:00
}
2020-01-13 15:16:35 +03:00
2020-10-14 17:07:59 +03:00
/**
* Indicate that the model status is paid.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
public function paid()
{
2020-10-15 00:08:41 +03:00
return $this->state([
'status' => 'paid',
]);
2020-01-13 15:16:35 +03:00
}
2020-10-14 17:07:59 +03:00
/**
* Indicate that the model status is cancelled.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
public function cancelled()
{
2020-10-15 00:08:41 +03:00
return $this->state([
'status' => 'cancelled',
]);
2020-10-14 17:07:59 +03:00
}
2020-01-13 15:16:35 +03:00
2020-10-14 17:07:59 +03:00
/**
* Indicate that the model is recurring.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
public function recurring()
{
2020-10-15 00:08:41 +03:00
return $this->state([
'recurring_frequency' => 'yes',
'recurring_interval' => '1',
'recurring_custom_frequency' => $this->faker->randomElement(['monthly', 'weekly']),
'recurring_count' => '1',
]);
2020-10-14 17:07:59 +03:00
}
2020-01-13 15:16:35 +03:00
2020-10-14 17:07:59 +03:00
/**
* Indicate that the model has items.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
public function items()
{
return $this->state(function (array $attributes) {
$amount = $this->faker->randomFloat(2, 1, 1000);
$taxes = Tax::enabled()->get();
if ($taxes->count()) {
$tax = $taxes->random(1)->first();
} else {
$tax = Tax::factory()->enabled()->create();
}
2020-01-13 15:16:35 +03:00
2020-10-14 17:07:59 +03:00
$items = Item::enabled()->get();
2020-01-13 15:16:35 +03:00
2020-10-14 17:07:59 +03:00
if ($items->count()) {
$item = $items->random(1)->first();
} else {
$item = Item::factory()->enabled()->create();
}
2020-01-23 17:54:39 +03:00
2020-10-14 17:07:59 +03:00
$items = [
[
'name' => $item->name,
'item_id' => $item->id,
'tax_id' => [$tax->id],
'quantity' => '1',
'price' => $amount,
'currency' => setting('default.currency'),
]
];
2020-01-22 16:35:22 +03:00
2020-10-14 17:07:59 +03:00
return [
'items' => $items,
'recurring_frequency' => 'no',
];
});
}
2020-01-13 15:16:35 +03:00
2020-10-14 17:07:59 +03:00
/**
* Configure the model factory.
*
* @return $this
*/
public function configure()
{
return $this->afterCreating(function (Model $invoice) {
2020-11-18 14:30:17 +03:00
Overrider::load('currencies');
2020-10-14 17:07:59 +03:00
$init_status = $invoice->status;
2020-03-28 17:54:36 +03:00
2020-10-14 17:07:59 +03:00
$invoice->status = 'draft';
event(new InvoiceCreated($invoice));
$invoice->status = $init_status;
2020-01-13 15:16:35 +03:00
2020-10-14 17:07:59 +03:00
$amount = $this->faker->randomFloat(2, 1, 1000);
2020-01-13 15:16:35 +03:00
2020-10-14 17:07:59 +03:00
$taxes = Tax::enabled()->get();
2020-01-13 15:16:35 +03:00
2020-10-14 17:07:59 +03:00
if ($taxes->count()) {
$tax = $taxes->random(1)->first();
} else {
$tax = Tax::factory()->enabled()->create();
}
2020-01-13 15:16:35 +03:00
2020-10-14 17:07:59 +03:00
$items = Item::enabled()->get();
2020-03-15 20:29:07 +03:00
2020-10-14 17:07:59 +03:00
if ($items->count()) {
$item = $items->random(1)->first();
} else {
$item = Item::factory()->enabled()->create();
}
2020-03-15 20:29:07 +03:00
2020-10-14 17:07:59 +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
2020-10-14 17:07:59 +03:00
$request = [
'items' => $items,
'recurring_frequency' => 'no',
];
2020-01-13 15:16:35 +03:00
2020-10-14 17:07:59 +03:00
$updated_invoice = $this->dispatch(new UpdateInvoice($invoice, $request));
2020-03-15 20:29:07 +03:00
2020-10-14 17:07:59 +03:00
switch ($init_status) {
case 'sent':
event(new InvoiceSent($updated_invoice));
2020-01-13 15:16:35 +03:00
2020-10-14 17:07:59 +03:00
break;
case 'viewed':
$updated_invoice->status = 'sent';
event(new InvoiceViewed($updated_invoice));
$updated_invoice->status = $init_status;
2020-01-13 15:16:35 +03:00
2020-10-14 17:07:59 +03:00
break;
case 'partial':
case 'paid':
$payment_request = [
'paid_at' => $updated_invoice->due_at,
];
2020-01-23 17:54:39 +03:00
2020-10-14 17:07:59 +03:00
if ($init_status == 'partial') {
$payment_request['amount'] = (int) round($amount / 3, $invoice->currency->precision);
}
2020-01-22 16:35:22 +03:00
2020-10-14 17:07:59 +03:00
event(new PaymentReceived($updated_invoice, $payment_request));
2020-01-22 16:35:22 +03:00
2020-10-14 17:07:59 +03:00
break;
case 'cancelled':
event(new InvoiceCancelled($updated_invoice));
2020-03-28 17:54:36 +03:00
2020-10-14 17:07:59 +03:00
break;
}
});
2020-01-13 15:16:35 +03:00
}
2020-10-14 17:07:59 +03:00
}