PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.2
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.2
1.6.5 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 All 48 releases
fluent-cart / app / Events / Order / OrderRefund.php

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

150 lines 4.5 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 $newOrderPaymentStatus = $calculatedRefundAmount >= $order->total_paid ? Status::PAYMENT_REFUNDED : Status::PAYMENT_PARTIALLY_REFUNDED;
66
67 $newRefundAmount = $calculatedRefundAmount - $order->total_refund;
68
69 $isStatusChanged = $newOrderPaymentStatus !== $order->payment_status;
70
71 if (!$newRefundAmount) {
72 $this->listeners = [];
73 $this->willTrigger = false;
74 return;
75 }
76
77 $this->refundedItems = $refundedItems;
78 $this->refundedItemIds = $refundedItemIds;
79 $this->refundedAmount = $newRefundAmount;
80 $this->order->load('customer', 'shipping_address', 'billing_address');
81 $this->manageStock = $manageStock;
82 $this->transaction = $createdRefund;
83 $this->type = $newOrderPaymentStatus === Status::PAYMENT_REFUNDED ? 'full' : 'partial';
84 $this->customer = $order->customer;
85 }
86
87 public function toArray(): array
88 {
89 return [
90 'order' => $this->order,
91 'manage_stock' => $this->manageStock,
92 'transaction' => $this->transaction,
93 'customer' => $this->customer,
94 'refunded_item_ids' => $this->refundedItemIds,
95 'refunded_items' => $this->refundedItems,
96 'refunded_amount' => $this->refundedAmount,
97 'type' => $this->type
98 ];
99 }
100
101 public function getActivityEventModel()
102 {
103 return $this->order;
104 }
105
106 public function afterDispatch()
107 {
108 if (!$this->willTrigger) {
109 return;
110 }
111
112 $refundedItems = [];
113
114 if ($this->refundedItemIds) {
115 $refundedItems = OrderItem::query()
116 ->whereIn('id', $this->refundedItemIds)
117 ->get()
118 ->toArray();
119 }
120
121 $newRefundedItems = [];
122 if ($this->refundedItems) {
123 foreach ($this->refundedItems as $item) {
124 $orderItem = OrderItem::query()->find($item['id']);
125 $orderItem['restore_quantity'] = $item['restore_quantity'] ?? null;
126 $newRefundedItems[] = $orderItem;
127 }
128 }
129
130 $data = [
131 'order' => $this->order,
132 'refunded_items' => $refundedItems,
133 'new_refunded_items' => $this->refundedItems,
134 'refunded_amount' => $this->refundedAmount,
135 'manage_stock' => $this->manageStock,
136 'transaction' => $this->transaction,
137 'customer' => $this->customer,
138 'type' => $this->type
139 ];
140
141 do_action('fluent_cart/order_refunded', $data);
142
143 if ($this->type === 'full') {
144 do_action('fluent_cart/order_fully_refunded', $data);
145 } else {
146 do_action('fluent_cart/order_partially_refunded', $data);
147 }
148 }
149 }
150