using events to fire updates for invoices and company ledger

This commit is contained in:
David Bomba 2019-10-01 14:27:04 +10:00
parent 2d5d9b816b
commit 4bccdae01e
8 changed files with 27 additions and 28 deletions

View File

@ -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;
}
}

View File

@ -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);
}

View File

@ -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);

View File

@ -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);

View File

@ -88,7 +88,7 @@ class UpdateInvoicePayment implements ShouldQueue
'log' => $data,
'category_id' => SystemLog::PAYMENT_RESPONSE,
'event_id' => SystemLog::PAYMENT_RECONCILIATION_FAILURE,
]
];
SystemLog::create($sl);

View File

@ -24,7 +24,6 @@ class PaymentObserver
*/
public function created(Payment $payment)
{
event(new PaymentWasCreated($payment));
}
/**

View File

@ -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;
}
}

View File

@ -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)]);
}