PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.4
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.4
1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk 1.2.0 All 47 releases
fluent-cart / app / Events / Order / OrderRefund.php

OrderRefund.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.4, at app/Events/Order/OrderRefund.php

151 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Events\Order;
4
5 use FluentCart\App\Events\EventDispatcher;
6 use FluentCart\App\Helpers\Status;
7 use FluentCart\App\Listeners;
8 use FluentCart\App\Models\Customer;
9 use FluentCart\App\Models\Order;
10 use FluentCart\App\Models\OrderItem;
11 use FluentCart\App\Models\OrderTransaction;
12
13 class OrderRefund extends EventDispatcher
14 {
15 public string $hook = 'fluent_cart/order_refunded';
16 public bool $autoFireHook = false;
17
18 protected array $listeners = [
19 Listeners\Order\OrderRefunded::class,
20 // Listeners\UpdateStock::class,
21 ];
22
23 public Order $order;
24
25 public ?OrderTransaction $transaction;
26
27 /**
28 * @var int[] Array of refunded order item IDs
29 */
30 public array $refundedItemIds;
31
32 public array $refundedItems;
33
34 public bool $manageStock;
35
36 /*
37 * Refunded amount in cents
38 */
39 public int $refundedAmount;
40
41 public ?Customer $customer;
42
43 public string $type;
44
45 public bool $willTrigger = true;
46
47 /**
48 * OrderRefunded constructor.
49 *
50 * @param Order $order The refunded order
51 * @param OrderTransaction|null $transaction The related transaction, if any
52 * @param int[] $refundedItemIds Array of refunded order item IDs
53 * @param bool $manageStock Whether to manage stock
54 */
55 public function __construct(Order $order, ?OrderTransaction $createdRefund = null, array $refundedItemIds = [], bool $manageStock = false, $refundedItems = [])
56 {
57 $this->order = $order;
58
59 $calculatedRefundAmount = OrderTransaction::query()->where('order_id', $order->id)
60 ->where('transaction_type', Status::TRANSACTION_TYPE_REFUND)
61 ->where('status', Status::TRANSACTION_REFUNDED)
62 ->sum('total');
63
64
65 $netPaidTotal = $order->netAmount((int) $order->total_paid);
66 $newOrderPaymentStatus = $calculatedRefundAmount >= $netPaidTotal ? Status::PAYMENT_REFUNDED : Status::PAYMENT_PARTIALLY_REFUNDED;
67
68 $newRefundAmount = $calculatedRefundAmount - $order->total_refund;
69
70 $isStatusChanged = $newOrderPaymentStatus !== $order->payment_status;
71
72 if (!$newRefundAmount) {
73 $this->listeners = [];
74 $this->willTrigger = false;
75 return;
76 }
77
78 $this->refundedItems = $refundedItems;
79 $this->refundedItemIds = $refundedItemIds;
80 $this->refundedAmount = $newRefundAmount;
81 $this->order->load('customer', 'shipping_address', 'billing_address');
82 $this->manageStock = $manageStock;
83 $this->transaction = $createdRefund;
84 $this->type = $newOrderPaymentStatus === Status::PAYMENT_REFUNDED ? 'full' : 'partial';
85 $this->customer = $order->customer;
86 }
87
88 public function toArray(): array
89 {
90 return [
91 'order' => $this->order,
92 'manage_stock' => $this->manageStock,
93 'transaction' => $this->transaction,
94 'customer' => $this->customer,
95 'refunded_item_ids' => $this->refundedItemIds,
96 'refunded_items' => $this->refundedItems,
97 'refunded_amount' => $this->refundedAmount,
98 'type' => $this->type
99 ];
100 }
101
102 public function getActivityEventModel()
103 {
104 return $this->order;
105 }
106
107 public function afterDispatch()
108 {
109 if (!$this->willTrigger) {
110 return;
111 }
112
113 $refundedItems = [];
114
115 if ($this->refundedItemIds) {
116 $refundedItems = OrderItem::query()
117 ->whereIn('id', $this->refundedItemIds)
118 ->get()
119 ->toArray();
120 }
121
122 $newRefundedItems = [];
123 if ($this->refundedItems) {
124 foreach ($this->refundedItems as $item) {
125 $orderItem = OrderItem::query()->find($item['id']);
126 $orderItem['restore_quantity'] = $item['restore_quantity'] ?? null;
127 $newRefundedItems[] = $orderItem;
128 }
129 }
130
131 $data = [
132 'order' => $this->order,
133 'refunded_items' => $refundedItems,
134 'new_refunded_items' => $this->refundedItems,
135 'refunded_amount' => $this->refundedAmount,
136 'manage_stock' => $this->manageStock,
137 'transaction' => $this->transaction,
138 'customer' => $this->customer,
139 'type' => $this->type
140 ];
141
142 do_action('fluent_cart/order_refunded', $data);
143
144 if ($this->type === 'full') {
145 do_action('fluent_cart/order_fully_refunded', $data);
146 } else {
147 do_action('fluent_cart/order_partially_refunded', $data);
148 }
149 }
150 }
151