From 8ddb877dae6dd6c7f37e02ba74b0d9cb7b857584 Mon Sep 17 00:00:00 2001 From: denisdulici Date: Thu, 23 Jan 2020 17:57:28 +0300 Subject: [PATCH] added bill received event --- app/Events/Purchase/BillReceived.php | 22 ++++++++++++ app/Http/Controllers/Purchases/Bills.php | 13 +------ app/Listeners/Purchase/MarkBillReceived.php | 29 +++++++++++++++ app/Providers/Event.php | 3 ++ database/factories/Bill.php | 40 ++++++++++----------- 5 files changed, 75 insertions(+), 32 deletions(-) create mode 100644 app/Events/Purchase/BillReceived.php create mode 100644 app/Listeners/Purchase/MarkBillReceived.php diff --git a/app/Events/Purchase/BillReceived.php b/app/Events/Purchase/BillReceived.php new file mode 100644 index 000000000..197de704d --- /dev/null +++ b/app/Events/Purchase/BillReceived.php @@ -0,0 +1,22 @@ +bill = $bill; + } +} diff --git a/app/Http/Controllers/Purchases/Bills.php b/app/Http/Controllers/Purchases/Bills.php index 3def477d5..ad6e93240 100644 --- a/app/Http/Controllers/Purchases/Bills.php +++ b/app/Http/Controllers/Purchases/Bills.php @@ -16,7 +16,6 @@ use App\Models\Banking\Account; use App\Models\Common\Contact; use App\Models\Common\Item; use App\Models\Purchase\Bill; -use App\Models\Purchase\BillHistory; use App\Models\Setting\Category; use App\Models\Setting\Currency; use App\Models\Setting\Tax; @@ -267,17 +266,7 @@ class Bills extends Controller */ public function markReceived(Bill $bill) { - $bill->status = 'received'; - $bill->save(); - - // Add bill history - BillHistory::create([ - 'company_id' => $bill->company_id, - 'bill_id' => $bill->id, - 'status' => 'received', - 'notify' => 0, - 'description' => trans('bills.mark_received'), - ]); + event(new \App\Events\Purchase\BillReceived($bill)); $message = trans('bills.messages.received'); diff --git a/app/Listeners/Purchase/MarkBillReceived.php b/app/Listeners/Purchase/MarkBillReceived.php new file mode 100644 index 000000000..ce611107d --- /dev/null +++ b/app/Listeners/Purchase/MarkBillReceived.php @@ -0,0 +1,29 @@ +bill->status != 'partial') { + $event->bill->status = 'received'; + + $event->bill->save(); + } + + $this->dispatch(new CreateBillHistory($event->bill, 0, trans('bills.mark_received'))); + } +} diff --git a/app/Providers/Event.php b/app/Providers/Event.php index 36631b491..9f15efc33 100644 --- a/app/Providers/Event.php +++ b/app/Providers/Event.php @@ -25,6 +25,9 @@ class Event extends Provider 'App\Events\Purchase\BillCreated' => [ 'App\Listeners\Purchase\CreateBillCreatedHistory', ], + 'App\Events\Purchase\BillReceived' => [ + 'App\Listeners\Purchase\MarkBillReceived', + ], 'App\Events\Purchase\BillRecurring' => [ 'App\Listeners\Purchase\SendBillRecurringNotification', ], diff --git a/database/factories/Bill.php b/database/factories/Bill.php index 552a72ee5..beb45e9ba 100644 --- a/database/factories/Bill.php +++ b/database/factories/Bill.php @@ -1,8 +1,8 @@ afterCreating(Bill::class, function ($bill, $faker) use ($company) { session(['company_id' => $company->id]); setting()->setExtraColumns(['company_id' => $company->id]); - $status = $bill->status; + $init_status = $bill->status; + $bill->status = 'draft'; - event(new BillCreated($bill)); - - $bill->status = $status; - - if ($bill->status == 'received') { - $bill->status = 'received'; - $bill->save(); - - dispatch_now(new CreateBillHistory($bill, 0, trans('bills.mark_received'))); - } + $bill->status = $init_status; $amount = $faker->randomFloat(2, 1, 1000); @@ -131,15 +123,23 @@ $factory->afterCreating(Bill::class, function ($bill, $faker) use ($company) { $updated_bill = dispatch_now(new UpdateBill($bill, $request)); - if (in_array($bill->status, ['partial', 'paid'])) { - $payment_request = [ - 'paid_at' => $bill->due_at, - ]; + switch ($init_status) { + case 'received': + event(new BillReceived($bill)); - if ($bill->status == 'partial') { - $payment_request['amount'] = (double) $amount / 2; - } + break; + case 'partial': + case 'paid': + $payment_request = [ + 'paid_at' => $bill->due_at, + ]; - $transaction = dispatch_now(new CreateDocumentTransaction($updated_bill, $payment_request)); + if ($init_status == 'partial') { + $payment_request['amount'] = (double) $amount / 3; + } + + $transaction = dispatch_now(new CreateDocumentTransaction($updated_bill, $payment_request)); + + break; } });