Skip to content

Commit

Permalink
Refactor event listeners and services
Browse files Browse the repository at this point in the history
  • Loading branch information
Blair2004 committed Jan 8, 2024
1 parent a1f7aaf commit fea2780
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 13 deletions.
37 changes: 37 additions & 0 deletions app/Jobs/RecordTransactionForShippingJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Jobs;

use App\Models\Order;
use App\Models\OrderRefund;
use App\Services\TransactionService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class RecordTransactionForShippingJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

/**
* Create a new job instance.
*/
public function __construct( public Order $order, public OrderRefund $orderRefund )
{
//
}

/**
* Execute the job.
*/
public function handle( TransactionService $transactionService ): void
{
$transactionService->createTransactionFormRefundedOrderShipping(
order: $this->order,
orderRefund: $this->orderRefund
);
}
}
2 changes: 1 addition & 1 deletion app/Listeners/OrderAfterProductRefundedEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct()
public function handle(OrderAfterProductRefundedEvent $event)
{
Bus::chain([
new RefreshOrderJob($event->order),
// new RefreshOrderJob($event->order), this already called on OrderAfterRefundEvent
new CreateExpenseFromRefundJob(
order: $event->order,
orderProduct: $event->orderProduct,
Expand Down
2 changes: 2 additions & 0 deletions app/Listeners/OrderAfterRefundedEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Events\OrderAfterRefundedEvent;
use App\Jobs\DecreaseCustomerPurchasesJob;
use App\Jobs\RecordTransactionForShippingJob;
use App\Jobs\ReduceCashierStatsFromRefundJob;
use App\Jobs\RefreshOrderJob;
use Illuminate\Support\Facades\Bus;
Expand All @@ -28,6 +29,7 @@ public function __construct()
public function handle(OrderAfterRefundedEvent $event)
{
Bus::chain([
new RecordTransactionForShippingJob( $event->order, $event->orderRefund ),
new RefreshOrderJob($event->order),
new ReduceCashierStatsFromRefundJob($event->order, $event->orderRefund),
new DecreaseCustomerPurchasesJob($event->order->customer, $event->orderRefund->total),
Expand Down
2 changes: 1 addition & 1 deletion app/Services/OrdersService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2139,7 +2139,7 @@ public function refreshOrder(Order $order)
$order->payment_status = Order::PAYMENT_REFUNDED;
} elseif ($order->total > 0 && $totalRefunds > 0) {
$order->payment_status = Order::PAYMENT_PARTIALLY_REFUNDED;
} elseif ($order->tendered >= $order->total && $order->payments->count() > 0) {
} elseif ($order->tendered >= $order->total && $order->payments->count() > 0 && $totalRefunds == 0 ) {
$order->payment_status = Order::PAYMENT_PAID;
} elseif ((float) $order->tendered < (float) $order->total && (float) $order->tendered > 0) {
$order->payment_status = Order::PAYMENT_PARTIALLY;
Expand Down
36 changes: 36 additions & 0 deletions app/Services/TransactionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use App\Models\Order;
use App\Models\OrderProduct;
use App\Models\OrderProductRefund;
use App\Models\OrderRefund;
use App\Models\Procurement;
use App\Models\Role;
use App\Models\Transaction;
Expand Down Expand Up @@ -474,6 +475,37 @@ public function handleProcurementTransaction(Procurement $procurement)
}
}

/**
* Will record a transaction resulting from a paid order
*
* @return void
*/
public function createTransactionFormRefundedOrderShipping( Order $order, OrderRefund $orderRefund )
{
/**
* If the order shipping is greater than 0
* this means the shipping has been refunded
*/
if ( $orderRefund->shipping > 0 ) {
$transactionAccount = $this->getTransactionAccountByCode( TransactionHistory::ACCOUNT_REFUNDS );

$transaction = new Transaction;
$transaction->value = $orderRefund->shipping;
$transaction->active = true;
$transaction->operation = TransactionHistory::OPERATION_DEBIT;
$transaction->author = $orderRefund->author;
$transaction->order_id = $order->id;
$transaction->order_refund_id = $orderRefund->id;
$transaction->name = sprintf( __( 'Refund Shipping : %s' ), $order->code );
$transaction->id = 0; // this is not assigned to an existing transaction
$transaction->account = $transactionAccount;
$transaction->created_at = $orderRefund->created_at;
$transaction->updated_at = $orderRefund->updated_at;

$this->recordTransactionHistory( $transaction );
}
}

/**
* Will record a transaction for every refund performed
*
Expand Down Expand Up @@ -616,6 +648,9 @@ public function handlePaymentStatus(string $previous, string $new, Order $order)
}
}

/**
* @deprecated ?
*/
public function recomputeTransactionHistory($rangeStarts = null, $rangeEnds = null)
{
/**
Expand Down Expand Up @@ -678,6 +713,7 @@ public function getTransactionAccountByCode($type): TransactionAccount
/**
* Will process refunded orders
*
* @todo the method might no longer be in use.
* @param string $rangeStart
* @param string $rangeEnds
* @return void
Expand Down
24 changes: 13 additions & 11 deletions tests/Traits/WithOrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2222,18 +2222,19 @@ protected function attemptRefundOrder($productQuantity, $refundQuantity, $paymen
$responseData = $response->json();

$secondFetchCustomer = $firstFetchCustomer->fresh();

if ($currency->define($secondFetchCustomer->purchases_amount)
$purchaseAmount = $currency->define($secondFetchCustomer->purchases_amount)
->subtractBy($responseData[ 'data' ][ 'order' ][ 'total' ])
->getRaw() != $currency->getRaw($firstFetchCustomer->purchases_amount)) {
throw new Exception(
sprintf(
__('The purchase amount hasn\'t been updated correctly. Expected %s, got %s'),
$secondFetchCustomer->purchases_amount - (float) $responseData[ 'data' ][ 'order' ][ 'total' ],
$firstFetchCustomer->purchases_amount
)
);
}
->getRaw();

$this->assertSame(
$purchaseAmount,
$currency->getRaw($firstFetchCustomer->purchases_amount),
sprintf(
__('The purchase amount hasn\'t been updated correctly. Expected %s, got %s'),
$secondFetchCustomer->purchases_amount - (float) $responseData[ 'data' ][ 'order' ][ 'total' ],
$firstFetchCustomer->purchases_amount
)
);

/**
* We'll keep original products amounts and quantity
Expand All @@ -2253,6 +2254,7 @@ protected function attemptRefundOrder($productQuantity, $refundQuantity, $paymen
'identifier' => 'account-payment',
],
'total' => $responseData[ 'data' ][ 'order' ][ 'total' ],
'refund_shipping' => true,
'products' => $responseData[ 'data' ][ 'order' ][ 'products' ],
]);

Expand Down

0 comments on commit fea2780

Please sign in to comment.