Skip to content

Commit fea2780

Browse files
committed
Refactor event listeners and services
1 parent a1f7aaf commit fea2780

File tree

6 files changed

+90
-13
lines changed

6 files changed

+90
-13
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace App\Jobs;
4+
5+
use App\Models\Order;
6+
use App\Models\OrderRefund;
7+
use App\Services\TransactionService;
8+
use Illuminate\Bus\Queueable;
9+
use Illuminate\Contracts\Queue\ShouldBeUnique;
10+
use Illuminate\Contracts\Queue\ShouldQueue;
11+
use Illuminate\Foundation\Bus\Dispatchable;
12+
use Illuminate\Queue\InteractsWithQueue;
13+
use Illuminate\Queue\SerializesModels;
14+
15+
class RecordTransactionForShippingJob implements ShouldQueue
16+
{
17+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
18+
19+
/**
20+
* Create a new job instance.
21+
*/
22+
public function __construct( public Order $order, public OrderRefund $orderRefund )
23+
{
24+
//
25+
}
26+
27+
/**
28+
* Execute the job.
29+
*/
30+
public function handle( TransactionService $transactionService ): void
31+
{
32+
$transactionService->createTransactionFormRefundedOrderShipping(
33+
order: $this->order,
34+
orderRefund: $this->orderRefund
35+
);
36+
}
37+
}

app/Listeners/OrderAfterProductRefundedEventListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function __construct()
2727
public function handle(OrderAfterProductRefundedEvent $event)
2828
{
2929
Bus::chain([
30-
new RefreshOrderJob($event->order),
30+
// new RefreshOrderJob($event->order), this already called on OrderAfterRefundEvent
3131
new CreateExpenseFromRefundJob(
3232
order: $event->order,
3333
orderProduct: $event->orderProduct,

app/Listeners/OrderAfterRefundedEventListener.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Events\OrderAfterRefundedEvent;
66
use App\Jobs\DecreaseCustomerPurchasesJob;
7+
use App\Jobs\RecordTransactionForShippingJob;
78
use App\Jobs\ReduceCashierStatsFromRefundJob;
89
use App\Jobs\RefreshOrderJob;
910
use Illuminate\Support\Facades\Bus;
@@ -28,6 +29,7 @@ public function __construct()
2829
public function handle(OrderAfterRefundedEvent $event)
2930
{
3031
Bus::chain([
32+
new RecordTransactionForShippingJob( $event->order, $event->orderRefund ),
3133
new RefreshOrderJob($event->order),
3234
new ReduceCashierStatsFromRefundJob($event->order, $event->orderRefund),
3335
new DecreaseCustomerPurchasesJob($event->order->customer, $event->orderRefund->total),

app/Services/OrdersService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2139,7 +2139,7 @@ public function refreshOrder(Order $order)
21392139
$order->payment_status = Order::PAYMENT_REFUNDED;
21402140
} elseif ($order->total > 0 && $totalRefunds > 0) {
21412141
$order->payment_status = Order::PAYMENT_PARTIALLY_REFUNDED;
2142-
} elseif ($order->tendered >= $order->total && $order->payments->count() > 0) {
2142+
} elseif ($order->tendered >= $order->total && $order->payments->count() > 0 && $totalRefunds == 0 ) {
21432143
$order->payment_status = Order::PAYMENT_PAID;
21442144
} elseif ((float) $order->tendered < (float) $order->total && (float) $order->tendered > 0) {
21452145
$order->payment_status = Order::PAYMENT_PARTIALLY;

app/Services/TransactionService.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use App\Models\Order;
1717
use App\Models\OrderProduct;
1818
use App\Models\OrderProductRefund;
19+
use App\Models\OrderRefund;
1920
use App\Models\Procurement;
2021
use App\Models\Role;
2122
use App\Models\Transaction;
@@ -474,6 +475,37 @@ public function handleProcurementTransaction(Procurement $procurement)
474475
}
475476
}
476477

478+
/**
479+
* Will record a transaction resulting from a paid order
480+
*
481+
* @return void
482+
*/
483+
public function createTransactionFormRefundedOrderShipping( Order $order, OrderRefund $orderRefund )
484+
{
485+
/**
486+
* If the order shipping is greater than 0
487+
* this means the shipping has been refunded
488+
*/
489+
if ( $orderRefund->shipping > 0 ) {
490+
$transactionAccount = $this->getTransactionAccountByCode( TransactionHistory::ACCOUNT_REFUNDS );
491+
492+
$transaction = new Transaction;
493+
$transaction->value = $orderRefund->shipping;
494+
$transaction->active = true;
495+
$transaction->operation = TransactionHistory::OPERATION_DEBIT;
496+
$transaction->author = $orderRefund->author;
497+
$transaction->order_id = $order->id;
498+
$transaction->order_refund_id = $orderRefund->id;
499+
$transaction->name = sprintf( __( 'Refund Shipping : %s' ), $order->code );
500+
$transaction->id = 0; // this is not assigned to an existing transaction
501+
$transaction->account = $transactionAccount;
502+
$transaction->created_at = $orderRefund->created_at;
503+
$transaction->updated_at = $orderRefund->updated_at;
504+
505+
$this->recordTransactionHistory( $transaction );
506+
}
507+
}
508+
477509
/**
478510
* Will record a transaction for every refund performed
479511
*
@@ -616,6 +648,9 @@ public function handlePaymentStatus(string $previous, string $new, Order $order)
616648
}
617649
}
618650

651+
/**
652+
* @deprecated ?
653+
*/
619654
public function recomputeTransactionHistory($rangeStarts = null, $rangeEnds = null)
620655
{
621656
/**
@@ -678,6 +713,7 @@ public function getTransactionAccountByCode($type): TransactionAccount
678713
/**
679714
* Will process refunded orders
680715
*
716+
* @todo the method might no longer be in use.
681717
* @param string $rangeStart
682718
* @param string $rangeEnds
683719
* @return void

tests/Traits/WithOrderTest.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2222,18 +2222,19 @@ protected function attemptRefundOrder($productQuantity, $refundQuantity, $paymen
22222222
$responseData = $response->json();
22232223

22242224
$secondFetchCustomer = $firstFetchCustomer->fresh();
2225-
2226-
if ($currency->define($secondFetchCustomer->purchases_amount)
2225+
$purchaseAmount = $currency->define($secondFetchCustomer->purchases_amount)
22272226
->subtractBy($responseData[ 'data' ][ 'order' ][ 'total' ])
2228-
->getRaw() != $currency->getRaw($firstFetchCustomer->purchases_amount)) {
2229-
throw new Exception(
2230-
sprintf(
2231-
__('The purchase amount hasn\'t been updated correctly. Expected %s, got %s'),
2232-
$secondFetchCustomer->purchases_amount - (float) $responseData[ 'data' ][ 'order' ][ 'total' ],
2233-
$firstFetchCustomer->purchases_amount
2234-
)
2235-
);
2236-
}
2227+
->getRaw();
2228+
2229+
$this->assertSame(
2230+
$purchaseAmount,
2231+
$currency->getRaw($firstFetchCustomer->purchases_amount),
2232+
sprintf(
2233+
__('The purchase amount hasn\'t been updated correctly. Expected %s, got %s'),
2234+
$secondFetchCustomer->purchases_amount - (float) $responseData[ 'data' ][ 'order' ][ 'total' ],
2235+
$firstFetchCustomer->purchases_amount
2236+
)
2237+
);
22372238

22382239
/**
22392240
* We'll keep original products amounts and quantity
@@ -2253,6 +2254,7 @@ protected function attemptRefundOrder($productQuantity, $refundQuantity, $paymen
22532254
'identifier' => 'account-payment',
22542255
],
22552256
'total' => $responseData[ 'data' ][ 'order' ][ 'total' ],
2257+
'refund_shipping' => true,
22562258
'products' => $responseData[ 'data' ][ 'order' ][ 'products' ],
22572259
]);
22582260

0 commit comments

Comments
 (0)