bill factory
This commit is contained in:
121
database/factories/Bill.php
Normal file
121
database/factories/Bill.php
Normal file
@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
use App\Events\Purchase\BillCreated;
|
||||
use App\Jobs\Purchase\CreateBillHistory;
|
||||
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;
|
||||
use Faker\Generator as Faker;
|
||||
use Jenssegers\Date\Date;
|
||||
|
||||
$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');
|
||||
|
||||
$contacts = Contact::type('vendor')->enabled()->get();
|
||||
|
||||
if ($contacts->count()) {
|
||||
$contact = $contacts->random(1)->first();
|
||||
} else {
|
||||
$contact = factory(Contact::class)->states('vendor')->create();
|
||||
}
|
||||
|
||||
$statuses = ['draft', 'received'];
|
||||
|
||||
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,
|
||||
'contact_name' => $contact->name,
|
||||
'contact_email' =>$contact->email,
|
||||
'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']);
|
||||
|
||||
$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);
|
||||
|
||||
$items = Item::enabled()->get();
|
||||
|
||||
if ($items->count()) {
|
||||
$item = $items->random(1)->first();
|
||||
} else {
|
||||
$item = factory(Item::class)->create();
|
||||
}
|
||||
|
||||
$items = [['name' => $item->name, 'item_id' => $item->id, 'quantity' => '1', 'price' => $amount, 'currency' => setting('default.currency')]];
|
||||
|
||||
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]);
|
||||
|
||||
event(new BillCreated($bill));
|
||||
|
||||
if ($bill->status == 'received') {
|
||||
$bill->status = 'received';
|
||||
$bill->save();
|
||||
|
||||
dispatch_now(new CreateBillHistory($bill, 0, trans('bills.mark_received')));
|
||||
}
|
||||
|
||||
$amount = $faker->randomFloat(2, 1, 1000);
|
||||
|
||||
$items = Item::enabled()->get();
|
||||
|
||||
if ($items->count()) {
|
||||
$item = $items->random(1)->first();
|
||||
} else {
|
||||
$item = factory(Item::class)->create();
|
||||
}
|
||||
|
||||
$items = [['name' => $item->name, 'item_id' => $item->id, 'quantity' => '1', 'price' => $amount, 'currency' => $bill->currency_code]];
|
||||
|
||||
$request = [
|
||||
'items' => $items,
|
||||
'recurring_frequency' => 'no',
|
||||
];
|
||||
|
||||
$updated_bill = dispatch_now(new UpdateBill($bill, $request));
|
||||
});
|
Reference in New Issue
Block a user