351 lines
9.9 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;
2020-12-24 01:28:38 +03:00
use App\Abstracts\Factory as AbstractFactory;
use App\Events\Document\DocumentCancelled;
use App\Events\Document\DocumentCreated;
use App\Events\Document\DocumentReceived;
use App\Events\Document\DocumentSent;
use App\Events\Document\DocumentViewed;
use App\Events\Document\PaymentReceived;
use App\Jobs\Document\UpdateDocument;
2020-01-13 15:16:35 +03:00
use App\Models\Common\Contact;
use App\Models\Common\Item;
2020-12-24 01:28:38 +03:00
use App\Models\Document\Document as Model;
2020-03-15 20:29:07 +03:00
use App\Models\Setting\Tax;
2022-06-01 10:15:55 +03:00
use App\Traits\Documents;
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-12-24 01:28:38 +03:00
use Illuminate\Database\Eloquent\Factories\Factory;
2020-01-13 15:16:35 +03:00
2020-12-24 01:28:38 +03:00
class Document extends AbstractFactory
2020-10-14 17:07:59 +03:00
{
2022-06-01 10:15:55 +03:00
use Documents;
2020-10-14 17:07:59 +03:00
/**
* 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()
{
2020-12-24 01:28:38 +03:00
$issued_at = $this->faker->dateTimeBetween(now()->startOfYear(), now()->endOfYear())->format('Y-m-d H:i:s');
2022-06-01 10:15:55 +03:00
$due_at = Date::parse($issued_at)->addDays($this->faker->randomNumber(2))->format('Y-m-d H:i:s');
2020-10-14 17:07:59 +03:00
return [
'company_id' => $this->company->id,
2020-12-24 01:28:38 +03:00
'issued_at' => $issued_at,
2020-10-14 17:07:59 +03:00
'due_at' => $due_at,
'currency_code' => setting('default.currency'),
'currency_rate' => '1',
'notes' => $this->faker->text(5),
'amount' => '0',
2021-09-10 00:31:39 +03:00
'created_from' => 'core::factory',
2020-10-14 17:07:59 +03:00
];
}
2020-12-24 01:28:38 +03:00
/**
* Indicate that the model type is invoice.
*/
public function invoice(): Factory
{
2022-06-01 10:15:55 +03:00
$contacts = Contact::customer()->enabled()->get();
2020-12-24 01:28:38 +03:00
2022-06-01 10:15:55 +03:00
if ($contacts->count()) {
$contact = $contacts->random(1)->first();
} else {
$contact = Contact::factory()->customer()->enabled()->create();
}
2020-12-24 01:28:38 +03:00
2022-06-01 10:15:55 +03:00
$statuses = ['draft', 'sent', 'viewed', 'partial', 'paid', 'cancelled'];
return $this->state([
'type' => Model::INVOICE_TYPE,
'document_number' => $this->getNextDocumentNumber(Model::INVOICE_TYPE),
'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),
]);
2020-12-24 01:28:38 +03:00
}
/**
* Indicate that the model type is bill.
*/
public function bill(): Factory
{
2022-06-01 10:15:55 +03:00
$contacts = Contact::vendor()->enabled()->get();
2020-12-24 01:28:38 +03:00
2022-06-01 10:15:55 +03:00
if ($contacts->count()) {
$contact = $contacts->random(1)->first();
} else {
$contact = Contact::factory()->vendor()->enabled()->create();
}
2020-12-24 01:28:38 +03:00
2022-06-01 10:15:55 +03:00
$statuses = ['draft', 'received', 'partial', 'paid', 'cancelled'];
return $this->state([
'type' => Model::BILL_TYPE,
'document_number' => $this->getNextDocumentNumber(Model::BILL_TYPE),
'category_id' => $this->company->categories()->expense()->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),
]);
2020-12-24 01:28:38 +03:00
}
2020-10-14 17:07:59 +03:00
/**
* 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-12-24 01:28:38 +03:00
/**
* Indicate that the model status is received.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
public function received()
{
return $this->state([
2021-09-06 17:00:11 +03:00
'status' => 'received',
]);
2020-12-24 01:28:38 +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()
{
2022-06-01 10:15:55 +03:00
$type = $this->getRawAttribute('type') . '-recurring';
2020-10-15 00:08:41 +03:00
return $this->state([
2022-06-01 10:15:55 +03:00
'type' => $type,
'document_number' => $this->getNextDocumentNumber($type),
'recurring_started_at' => $this->getRawAttribute('issued_at'),
2021-10-09 18:34:37 +03:00
'recurring_frequency' => 'daily',
2020-10-15 00:08:41 +03:00
'recurring_interval' => '1',
2022-06-01 10:15:55 +03:00
'recurring_limit_count' => '7',
2020-10-15 00:08:41 +03:00
]);
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()
{
2022-06-01 10:15:55 +03:00
$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();
}
$items = Item::enabled()->get();
if ($items->count()) {
$item = $items->random(1)->first();
} else {
$item = Item::factory()->enabled()->create();
}
$items = [
[
'type' => $this->getRawAttribute('type'),
'name' => $item->name,
'description' => $this->faker->text,
'item_id' => $item->id,
'tax_ids' => [$tax->id],
'quantity' => '1',
'price' => $amount,
'currency' => setting('default.currency'),
],
];
2020-01-22 16:35:22 +03:00
2022-06-01 10:15:55 +03:00
return $this->state([
'items' => $items,
'recurring_frequency' => 'no',
]);
2020-10-14 17:07:59 +03:00
}
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()
{
2020-12-24 01:28:38 +03:00
return $this->afterCreating(function (Model $document) {
2020-11-18 14:30:17 +03:00
Overrider::load('currencies');
2020-12-24 01:28:38 +03:00
$init_status = $document->status;
2020-03-28 17:54:36 +03:00
2020-12-24 01:28:38 +03:00
$document->status = 'draft';
event(new DocumentCreated($document, request()));
2020-12-24 01:28:38 +03:00
$document->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 = [
[
2022-06-01 10:15:55 +03:00
'type' => $document->type,
2020-10-14 17:07:59 +03:00
'name' => $item->name,
2020-12-24 01:28:38 +03:00
'description' => $this->faker->text,
2020-10-14 17:07:59 +03:00
'item_id' => $item->id,
2020-12-24 01:28:38 +03:00
'tax_ids' => [$tax->id],
2020-10-14 17:07:59 +03:00
'quantity' => '1',
'price' => $amount,
2020-12-24 01:28:38 +03:00
'currency' => $document->currency_code,
2021-09-06 17:00:11 +03:00
],
2020-10-14 17:07:59 +03:00
];
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-12-24 01:28:38 +03:00
$updated_document = $this->dispatch(new UpdateDocument($document, $request));
2020-03-15 20:29:07 +03:00
2020-10-14 17:07:59 +03:00
switch ($init_status) {
2020-12-24 01:28:38 +03:00
case 'received':
event(new DocumentReceived($updated_document));
break;
2020-10-14 17:07:59 +03:00
case 'sent':
2020-12-24 01:28:38 +03:00
event(new DocumentSent($updated_document));
2020-01-13 15:16:35 +03:00
2020-10-14 17:07:59 +03:00
break;
case 'viewed':
2020-12-24 01:28:38 +03:00
$updated_document->status = 'sent';
event(new DocumentViewed($updated_document));
$updated_document->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 = [
2020-12-24 01:28:38 +03:00
'paid_at' => $updated_document->due_at,
2022-06-01 10:15:55 +03:00
'type' => config('type.document.' . $document->type . '.transaction_type'),
2020-10-14 17:07:59 +03:00
];
2020-01-23 17:54:39 +03:00
2020-12-24 01:28:38 +03:00
if ($init_status === 'partial') {
$payment_request['amount'] = (int) round($amount / 3, $document->currency->precision);
2020-10-14 17:07:59 +03:00
}
2020-01-22 16:35:22 +03:00
2020-12-24 01:28:38 +03:00
event(new PaymentReceived($updated_document, $payment_request));
2020-01-22 16:35:22 +03:00
2020-10-14 17:07:59 +03:00
break;
case 'cancelled':
2020-12-24 01:28:38 +03:00
event(new DocumentCancelled($updated_document));
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
}