From 4bccdae01e565c77c2961b4197bbc46ca9a64c41 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 1 Oct 2019 14:27:04 +1000 Subject: [PATCH] using events to fire updates for invoices and company ledger --- app/Events/Payment/PaymentWasCreated.php | 10 ++++----- app/Jobs/Invoice/ApplyPaymentToInvoice.php | 2 +- app/Jobs/Invoice/MarkInvoicePaid.php | 21 ++++++------------- .../Activity/PaymentCreatedActivity.php | 9 ++++---- .../Invoice/UpdateInvoicePayment.php | 2 +- app/Observers/PaymentObserver.php | 1 - app/PaymentDrivers/BasePaymentDriver.php | 6 ++++++ .../PayPalExpressPaymentDriver.php | 4 +++- 8 files changed, 27 insertions(+), 28 deletions(-) diff --git a/app/Events/Payment/PaymentWasCreated.php b/app/Events/Payment/PaymentWasCreated.php index 1d08c27e91d2..e1d8366defdf 100644 --- a/app/Events/Payment/PaymentWasCreated.php +++ b/app/Events/Payment/PaymentWasCreated.php @@ -22,17 +22,17 @@ class PaymentWasCreated use SerializesModels; /** - * @var array $data + * @var array $payment */ - public $data; + public $payment; /** * Create a new event instance. * - * @param array $data + * @param Payment $payment */ - public function __construct(array $data) + public function __construct(Payment $payment) { - $this->data = $data; + $this->payment = $payment; } } diff --git a/app/Jobs/Invoice/ApplyPaymentToInvoice.php b/app/Jobs/Invoice/ApplyPaymentToInvoice.php index 9f4609b0aa73..890ec3a4111e 100644 --- a/app/Jobs/Invoice/ApplyPaymentToInvoice.php +++ b/app/Jobs/Invoice/ApplyPaymentToInvoice.php @@ -86,7 +86,7 @@ class ApplyPaymentToInvoice implements ShouldQueue if(!$this->invoice->due_date) - $this->invoice->due_date = Carbon::now()->addDays(PaymentTerm::find($this->invoice->settings->payment_terms)->num_days)->format($client->date_format()); + $this->invoice->due_date = Carbon::now()->addDays(PaymentTerm::find($this->invoice->settings->payment_terms)->num_days); } diff --git a/app/Jobs/Invoice/MarkInvoicePaid.php b/app/Jobs/Invoice/MarkInvoicePaid.php index 0e2f5cc4aef0..fdf069549ab0 100644 --- a/app/Jobs/Invoice/MarkInvoicePaid.php +++ b/app/Jobs/Invoice/MarkInvoicePaid.php @@ -9,7 +9,7 @@ * @license https://opensource.org/licenses/AAL */ -namespace App\Jobs\Invoice; +namespace Jobs\Invoice; use App\Events\Payment\PaymentWasCreated; use App\Factory\PaymentFactory; @@ -57,23 +57,14 @@ class MarkInvoicePaid implements ShouldQueue $payment->amount = $this->invoice->balance; $payment->status_id = Payment::STATUS_COMPLETED; $payment->client_id = $this->invoice->client_id; - + /* Create a payment relationship to the invoice entity */ + $payment->save(); + $payment->invoices()->save($this->invoice); $payment->save(); - /* Create a payment relationship to the invoice entity */ - $payment->invoices()->save($this->invoice); - - $data = [ - 'payment_id' => $payment->id, - 'invoice_ids' => [ - $this->invoice->id - ] - ]; - - event(new PaymentWasCreated($data)); - /* Update Invoice balance */ - $invoice = ApplyPaymentToInvoice::dispatchNow($payment, $this->invoice); + //$invoice = ApplyPaymentToInvoice::dispatchNow($payment, $this->invoice); + event(new PaymentWasCreated($payment)); UpdateCompanyLedgerWithPayment::dispatchNow($payment, $payment->amount); diff --git a/app/Listeners/Activity/PaymentCreatedActivity.php b/app/Listeners/Activity/PaymentCreatedActivity.php index 7773b7faa217..c14a0fd9e046 100644 --- a/app/Listeners/Activity/PaymentCreatedActivity.php +++ b/app/Listeners/Activity/PaymentCreatedActivity.php @@ -39,12 +39,12 @@ class PaymentCreatedActivity implements ShouldQueue */ public function handle($event) { - $data = $event->data; + $payment = $event->payment; - $payment = Payment::find($data['payment_id']); - - $invoices = Invoice::find($data['invoice_ids']); + $invoices = $payment->invoices; + \Log::error($invoices->count()); + $fields = new \stdClass; $fields->payment_id = $payment->id; @@ -55,6 +55,7 @@ class PaymentCreatedActivity implements ShouldQueue foreach($invoices as $invoice) //todo we may need to add additional logic if in the future we apply payments to other entity Types, not just invoices { + $fields->invoice_id = $invoice->id; $this->activityRepo->save($fields, $invoice); diff --git a/app/Listeners/Invoice/UpdateInvoicePayment.php b/app/Listeners/Invoice/UpdateInvoicePayment.php index f26597c80ca5..a0b0fbfb8c40 100644 --- a/app/Listeners/Invoice/UpdateInvoicePayment.php +++ b/app/Listeners/Invoice/UpdateInvoicePayment.php @@ -88,7 +88,7 @@ class UpdateInvoicePayment implements ShouldQueue 'log' => $data, 'category_id' => SystemLog::PAYMENT_RESPONSE, 'event_id' => SystemLog::PAYMENT_RECONCILIATION_FAILURE, - ] + ]; SystemLog::create($sl); diff --git a/app/Observers/PaymentObserver.php b/app/Observers/PaymentObserver.php index beaaaf7b3bb9..16ad071d9310 100644 --- a/app/Observers/PaymentObserver.php +++ b/app/Observers/PaymentObserver.php @@ -24,7 +24,6 @@ class PaymentObserver */ public function created(Payment $payment) { - event(new PaymentWasCreated($payment)); } /** diff --git a/app/PaymentDrivers/BasePaymentDriver.php b/app/PaymentDrivers/BasePaymentDriver.php index b0b4bee7293e..662fb3f53b9f 100644 --- a/app/PaymentDrivers/BasePaymentDriver.php +++ b/app/PaymentDrivers/BasePaymentDriver.php @@ -240,6 +240,7 @@ class BasePaymentDriver $payment->company_gateway_id = $this->company_gateway->id; $payment->status_id = Payment::STATUS_COMPLETED; $payment->payment_date = Carbon::now(); + return $payment; } @@ -251,9 +252,14 @@ class BasePaymentDriver ->whereClientId($this->client->id) ->get(); + \Log::error($hashed_ids); + \Log::error($invoices->count()); + $payment->invoices()->sync($invoices); $payment->save(); + \Log::error(print_r($payment,1)); + return $payment; } } \ No newline at end of file diff --git a/app/PaymentDrivers/PayPalExpressPaymentDriver.php b/app/PaymentDrivers/PayPalExpressPaymentDriver.php index b9f3ab1a6363..27dee0cf635a 100644 --- a/app/PaymentDrivers/PayPalExpressPaymentDriver.php +++ b/app/PaymentDrivers/PayPalExpressPaymentDriver.php @@ -11,6 +11,7 @@ namespace App\PaymentDrivers; +use App\Events\Payment\PaymentWasCreated; use App\Models\ClientGatewayToken; use App\Models\GatewayType; use App\Models\PaymentType; @@ -105,11 +106,12 @@ class PayPalExpressPaymentDriver extends BasePaymentDriver throw new Exception($response->getMessage()); } -\Log::error($response->getData()); $payment = $this->createPayment($response->getData()); $this->attachInvoices($payment, $request->input('hashed_ids')); + event(new PaymentWasCreated($payment)); + return redirect()->route('client.payments.show', ['payment'=>$this->encodePrimaryKey($payment->id)]); }