bill factory
This commit is contained in:
parent
57cdfe96b6
commit
b100bb69ab
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));
|
||||
});
|
@ -3,6 +3,7 @@
|
||||
namespace Tests\Feature\Purchases;
|
||||
|
||||
use App\Jobs\Purchase\CreateBill;
|
||||
use App\Models\Purchase\Bill;
|
||||
use Tests\Feature\FeatureTestCase;
|
||||
|
||||
class BillsTest extends FeatureTestCase
|
||||
@ -26,7 +27,7 @@ class BillsTest extends FeatureTestCase
|
||||
public function testItShouldCreateBill()
|
||||
{
|
||||
$this->loginAs()
|
||||
->post(route('bills.store'), $this->getBillRequest())
|
||||
->post(route('bills.store'), $this->getRequest())
|
||||
->assertStatus(200);
|
||||
|
||||
$this->assertFlashLevel('success');
|
||||
@ -35,7 +36,7 @@ class BillsTest extends FeatureTestCase
|
||||
public function testItShouldCreateBillWithRecurring()
|
||||
{
|
||||
$this->loginAs()
|
||||
->post(route('bills.store'), $this->getBillRequest(true))
|
||||
->post(route('bills.store'), $this->getRequest(true))
|
||||
->assertStatus(200);
|
||||
|
||||
$this->assertFlashLevel('success');
|
||||
@ -43,7 +44,7 @@ class BillsTest extends FeatureTestCase
|
||||
|
||||
public function testItShouldSeeBillUpdatePage()
|
||||
{
|
||||
$bill = $this->dispatch(new CreateBill($this->getBillRequest()));
|
||||
$bill = $this->dispatch(new CreateBill($this->getRequest()));
|
||||
|
||||
$this->loginAs()
|
||||
->get(route('bills.edit', $bill->id))
|
||||
@ -53,7 +54,7 @@ class BillsTest extends FeatureTestCase
|
||||
|
||||
public function testItShouldUpdateBill()
|
||||
{
|
||||
$request = $this->getBillRequest();
|
||||
$request = $this->getRequest();
|
||||
|
||||
$bill = $this->dispatch(new CreateBill($request));
|
||||
|
||||
@ -61,14 +62,15 @@ class BillsTest extends FeatureTestCase
|
||||
|
||||
$this->loginAs()
|
||||
->patch(route('bills.update', $bill->id), $request)
|
||||
->assertStatus(200);
|
||||
->assertStatus(200)
|
||||
->assertSee($request['contact_name']);
|
||||
|
||||
$this->assertFlashLevel('success');
|
||||
}
|
||||
|
||||
public function testItShouldDeleteBill()
|
||||
{
|
||||
$bill = $this->dispatch(new CreateBill($this->getBillRequest()));
|
||||
$bill = $this->dispatch(new CreateBill($this->getRequest()));
|
||||
|
||||
$this->loginAs()
|
||||
->delete(route('bills.destroy', $bill->id))
|
||||
@ -77,42 +79,12 @@ class BillsTest extends FeatureTestCase
|
||||
$this->assertFlashLevel('success');
|
||||
}
|
||||
|
||||
private function getBillRequest($recurring = false)
|
||||
public function getRequest($recurring = false)
|
||||
{
|
||||
$amount = $this->faker->randomFloat(2, 2);
|
||||
$factory = factory(Bill::class);
|
||||
|
||||
$items = [['name' => $this->faker->text(5), 'item_id' => null, 'quantity' => '1', 'price' => $amount, 'currency' => 'USD', 'tax_id' => null]];
|
||||
$recurring ? $factory->states('items', 'recurring') : $factory->states('items');
|
||||
|
||||
$data = [
|
||||
'company_id' => $this->company->id,
|
||||
'billed_at' => $this->faker->date(),
|
||||
'due_at' => $this->faker->date(),
|
||||
'bill_number' => '1',
|
||||
'order_number' => '1',
|
||||
'currency_code' => setting('default.currency', 'USD'),
|
||||
'currency_rate' => '1',
|
||||
'items' => $items,
|
||||
'discount' => '0',
|
||||
'notes' => $this->faker->text(5),
|
||||
'category_id' => $this->company->categories()->type('expense')->pluck('id')->first(),
|
||||
'recurring_frequency' => 'no',
|
||||
'contact_id' => '0',
|
||||
'contact_name' => $this->faker->name,
|
||||
'contact_email' =>$this->faker->email,
|
||||
'contact_tax_number' => null,
|
||||
'contact_phone' => null,
|
||||
'contact_address' => $this->faker->address,
|
||||
'status' => 'draft',
|
||||
'amount' => $amount,
|
||||
];
|
||||
|
||||
if ($recurring) {
|
||||
$data['recurring_frequency'] = 'yes';
|
||||
$data['recurring_interval'] = '1';
|
||||
$data['recurring_custom_frequency'] = $this->faker->randomElement(['monthly', 'weekly']);
|
||||
$data['recurring_count'] = '1';
|
||||
}
|
||||
|
||||
return $data;
|
||||
return $factory->raw();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user