188 lines
5.3 KiB
PHP
Raw Normal View History

2020-01-13 17:22:31 +03:00
<?php
2020-03-28 17:54:36 +03:00
use App\Events\Purchase\BillCancelled;
2020-01-13 17:22:31 +03:00
use App\Events\Purchase\BillCreated;
2020-01-23 17:57:28 +03:00
use App\Events\Purchase\BillReceived;
2020-01-19 00:25:46 +03:00
use App\Jobs\Banking\CreateDocumentTransaction;
2020-01-13 17:22:31 +03:00
use App\Jobs\Purchase\UpdateBill;
use App\Models\Auth\User;
use App\Models\Common\Contact;
use App\Models\Common\Item;
use App\Models\Purchase\Bill;
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;
use Faker\Generator as Faker;
2020-01-13 17:22:31 +03:00
$user = User::first();
$company = $user->companies()->first();
$factory->define(Bill::class, function (Faker $faker) use ($company) {
session(['company_id' => $company->id]);
setting()->setExtraColumns(['company_id' => $company->id]);
$billed_at = $faker->dateTimeBetween(now()->startOfYear(), now()->endOfYear())->format('Y-m-d');
$due_at = Date::parse($billed_at)->addDays(10)->format('Y-m-d');
2020-01-21 19:47:15 +03:00
$types = (string) setting('contact.type.vendor', 'vendor');
$contacts = Contact::type(explode(',', $types))->enabled()->get();
2020-01-13 17:22:31 +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', 'vendor')->create();
2020-01-13 17:22:31 +03:00
}
2020-03-28 17:54:36 +03:00
$statuses = ['draft', 'received', 'partial', 'paid', 'cancelled'];
2020-01-13 17:22:31 +03:00
return [
'company_id' => $company->id,
'billed_at' => $billed_at,
'due_at' => $due_at,
'bill_number' => (string) $faker->randomNumber(5),
'currency_code' => setting('default.currency'),
'currency_rate' => '1',
'notes' => $faker->text(5),
'category_id' => $company->categories()->type('expense')->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 17:22:31 +03:00
'contact_tax_number' => $contact->tax_number,
'contact_phone' => $contact->phone,
'contact_address' => $contact->address,
'status' => $faker->randomElement($statuses),
'amount' => '0',
];
});
$factory->state(Bill::class, 'draft', ['status' => 'draft']);
$factory->state(Bill::class, 'received', ['status' => 'received']);
2020-01-22 16:35:22 +03:00
$factory->state(Bill::class, 'partial', ['status' => 'partial']);
2020-01-19 00:25:46 +03:00
$factory->state(Bill::class, 'paid', ['status' => 'paid']);
2020-03-28 17:54:36 +03:00
$factory->state(Bill::class, 'cancelled', ['status' => 'cancelled']);
2020-01-13 17:22:31 +03:00
$factory->state(Bill::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(Bill::class, 'items', function (Faker $faker) use ($company) {
session(['company_id' => $company->id]);
setting()->setExtraColumns(['company_id' => $company->id]);
$amount = $faker->randomFloat(2, 1, 1000);
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 17:22:31 +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 17:22:31 +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 17:22:31 +03:00
return [
'items' => $items,
'recurring_frequency' => 'no',
];
});
$factory->afterCreating(Bill::class, function ($bill, $faker) use ($company) {
session(['company_id' => $company->id]);
setting()->setExtraColumns(['company_id' => $company->id]);
2020-01-23 17:57:28 +03:00
$init_status = $bill->status;
2020-01-19 00:25:46 +03:00
2020-01-23 17:57:28 +03:00
$bill->status = 'draft';
2020-01-13 17:22:31 +03:00
event(new BillCreated($bill));
2020-01-23 17:57:28 +03:00
$bill->status = $init_status;
2020-01-13 17:22:31 +03:00
$amount = $faker->randomFloat(2, 1, 1000);
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 17:22:31 +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 17:22:31 +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' => $bill->currency_code,
]
];
2020-01-13 17:22:31 +03:00
$request = [
'items' => $items,
'recurring_frequency' => 'no',
];
$updated_bill = dispatch_now(new UpdateBill($bill, $request));
2020-01-19 00:25:46 +03:00
2020-01-23 17:57:28 +03:00
switch ($init_status) {
case 'received':
2020-02-02 01:52:09 +03:00
event(new BillReceived($updated_bill));
2020-01-23 17:57:28 +03:00
break;
case 'partial':
case 'paid':
$payment_request = [
2020-02-02 01:52:09 +03:00
'paid_at' => $updated_bill->due_at,
2020-01-23 17:57:28 +03:00
];
if ($init_status == 'partial') {
$payment_request['amount'] = round($amount / 3, $bill->currency->precision);
2020-01-23 17:57:28 +03:00
}
2020-01-22 16:35:22 +03:00
2020-01-23 17:57:28 +03:00
$transaction = dispatch_now(new CreateDocumentTransaction($updated_bill, $payment_request));
2020-01-22 16:35:22 +03:00
2020-03-28 17:54:36 +03:00
break;
case 'cancelled':
event(new BillCancelled($updated_bill));
2020-01-23 17:57:28 +03:00
break;
2020-01-19 00:25:46 +03:00
}
2020-01-13 17:22:31 +03:00
});