PluginProbe
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More / 2.1.0
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More v2.1.0
2.3.0 2.2.0 2.1.1 2.1.0 2.0.0 1.10.0 1.9.1 1.9.0 1.2.1 1.2.2 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 All 59 releases
storeengine / includes / classes / refund.php

refund.php in StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More 2.1.0, at includes/classes/refund.php

285 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace StoreEngine\Classes;
4
5 use StoreEngine\Classes\Exceptions\StoreEngineException;
6 use StoreEngine\Classes\OrderStatus\OrderStatus;
7 use StoreEngine\Utils\Formatting;
8 use StoreEngine\Utils\Helper;
9
10 /**
11 * @see \WC_Order_Refund
12 * @see \WC_Order_Refund_Data_Store_CPT
13 */
14 class Refund extends AbstractOrder {
15
16 /**
17 * This is the name of this object type.
18 *
19 * @var string
20 */
21 protected string $object_type = 'refund_order';
22
23 /**
24 * Order meta-data with key => value.
25 *
26 * @var array
27 *
28 * @TODO move to extra.
29 * this is a placeholder for meta-data objects not k->v pair.
30 */
31 protected array $extra_data = [
32 //'amount' => '',
33 'reason' => '',
34 'refunded_by' => 0,
35 'refunded_payment' => false,
36 ];
37
38 protected bool $allow_trash = false;
39
40 public function __construct( $read = 0 ) {
41 $this->internal_meta_keys[] = '_reason';
42 $this->internal_meta_keys[] = '_refunded_by';
43 $this->internal_meta_keys[] = '_refunded_payment';
44 $this->meta_key_to_props['_reason'] = 'reason';
45 $this->meta_key_to_props['_refunded_by'] = 'refunded_by';
46 $this->meta_key_to_props['_refunded_payment'] = 'refunded_payment';
47
48 unset( $this->data['payment_method'], $this->data['payment_method_title'] );
49
50 parent::__construct( $read );
51 }
52
53 protected function read_db_data( $value, string $field = 'id' ): array {
54 return array_merge(
55 parent::read_db_data( $value, $field ),
56 [
57 'reason' => $this->get_metadata( '_reason' ),
58 'refunded_by' => $this->get_metadata( '_refunded_by' ),
59 'refunded_payment' => $this->get_metadata( '_refunded_payment' ),
60 ]
61 );
62 }
63
64 /**
65 * Get status - always completed for refunds.
66 *
67 * @param string $context What the value is for. Valid values are view and edit.
68 *
69 * @return string
70 */
71 public function get_status( string $context = 'view' ): ?string { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
72 return OrderStatus::COMPLETED;
73 }
74
75 /**
76 * Get refunded amount.
77 *
78 * @param string $context What the value is for. Valid values are view and edit.
79 *
80 * @return float|string
81 */
82 public function get_amount( string $context = 'view' ) {
83 return $this->get_total_amount( $context );
84 }
85
86 public function get_total_amount( string $context = 'view' ) {
87 return $this->get_total( $context );
88 }
89
90 public function get_tax_amount( string $context = 'view' ) {
91 return $this->get_total_tax( $context );
92 }
93
94
95 /**
96 * Get refund reason.
97 *
98 * @param string $context What the value is for. Valid values are view and edit.
99 *
100 * @return string
101 */
102 public function get_reason( string $context = 'view' ): string {
103 return (string) $this->get_prop( 'reason', $context );
104 }
105
106 /**
107 * Get ID of user who did the refund.
108 *
109 * @param string $context What the value is for. Valid values are view and edit.
110 *
111 * @return int
112 */
113 public function get_refunded_by( string $context = 'view' ): int {
114 return (int) $this->get_prop( 'refunded_by', $context );
115 }
116
117 protected ?Customer $user = null;
118
119 public function get_refunded_by_user() {
120 if ( ! $this->user || ( is_a( $this->user, '\StoreEngine\Classes\Customer' ) && $this->user->get_id() !== $this->get_refunded_by() ) ) {
121 $this->user = Helper::get_customer( $this->get_refunded_by() );
122 }
123
124 return $this->user;
125 }
126
127 /**
128 * Return if the payment was refunded via API.
129 *
130 * @param string $context What the value is for. Valid values are view and edit.
131 *
132 * @return bool
133 */
134 public function get_refunded_payment( string $context = 'view' ): bool {
135 return Formatting::string_to_bool( $this->get_prop( 'refunded_payment', $context ) );
136 }
137
138 /**
139 * Get formatted refunded amount.
140 *
141 * @return string
142 */
143 public function get_formatted_refund_amount(): string {
144 return apply_filters( 'storeengine/formatted_refund_amount', Formatting::price( $this->get_amount(), [ 'currency' => $this->get_currency() ] ), $this );
145 }
146
147 /**
148 * Set refunded amount.
149 *
150 * @param string|float|int $value Value to set.
151 */
152 public function set_amount( $value ) {
153 $this->set_total_amount( $value );
154 }
155
156 public function set_total_amount( $amount ) {
157 $this->set_total( $amount );
158 }
159
160 /**
161 * Set refund reason.
162 *
163 * @param string $value Value to set.
164 */
165 public function set_reason( string $value ) {
166 $this->set_prop( 'reason', $value );
167 }
168
169 /**
170 * Set refunded by.
171 *
172 * @param int|string $value Value to set.
173 */
174 public function set_refunded_by( $value ) {
175 $this->set_prop( 'refunded_by', absint( $value ) );
176 }
177
178 /**
179 * Set if the payment was refunded via API.
180 *
181 * @param bool|string $value Value to set. (yes|no|1|0|true|false)
182 */
183 public function set_refunded_payment( $value ) {
184 $this->set_prop( 'refunded_payment', Formatting::string_to_bool( $value ) );
185 }
186
187 protected function query(): ?string {
188 global $wpdb;
189
190 return "
191 SELECT
192 o.id as o_id,
193 o.parent_order_id as parent_order_id,
194 o.*,
195 p.id as operational_id,
196 p.*
197 FROM {$wpdb->prefix}storeengine_orders o
198 LEFT JOIN {$wpdb->prefix}storeengine_order_operational_data p ON p.order_id = o.id
199 LEFT JOIN {$wpdb->prefix}storeengine_orders_meta m ON m.order_id = o.id
200 ";
201 }
202
203 protected function prepare_for_db( string $context = 'create' ): array {
204 $data = [];
205 $format = [];
206
207 $props = [
208 'status',
209 'currency',
210 'type',
211 'tax_amount',
212 'total_amount',
213 'customer_id',
214 'date_created_gmt',
215 'date_updated_gmt',
216 'parent_order_id',
217 ];
218
219 if ( 'create' === $context ) {
220 $this->set_date_prop( 'date_created_gmt', current_time( 'mysql', 1 ) );
221 }
222
223 // Always set.
224 $this->set_date_prop( 'date_updated_gmt', current_time( 'mysql', 1 ) );
225
226 foreach ( $props as $prop ) {
227 $value = $this->{"get_$prop"}( 'edit' );
228
229 if ( $value && is_a( $value, StoreengineDatetime::class ) ) {
230 $value = $this->prepare_date_for_db( $value );
231 }
232
233 $format[] = $this->predict_format( $prop, $value );
234 $data[ $prop ] = $value;
235 }
236
237 return [
238 'data' => apply_filters( 'storeengine/' . $this->object_type . '/db/' . $context, $data, $this ),
239 'format' => $format,
240 ];
241 }
242
243 protected function prepare_operational_data_for_db( string $context = 'create' ): array {
244 $data = [];
245 $format = [];
246
247 $props = [
248 'storeengine_version' => 'version',
249 'prices_include_tax' => 'prices_include_tax',
250 'coupon_usages_are_counted' => 'coupon_usages_are_counted',
251 'download_permission_granted' => 'download_permission_granted',
252 'cart_hash' => 'cart_hash',
253 'new_order_email_sent' => 'new_order_email_sent',
254 'order_key' => 'order_key',
255 'date_paid_gmt' => 'date_paid_gmt',
256 'date_completed_gmt' => 'date_completed_gmt',
257 'shipping_tax_amount' => 'shipping_tax_amount',
258 'shipping_total_amount' => 'shipping_total_amount',
259 'discount_tax_amount' => 'discount_tax_amount',
260 'discount_total_amount' => 'discount_total_amount',
261 'recorded_sales' => 'recorded_sales',
262 ];
263
264 if ( 'create' === $context ) {
265 $props['order_id'] = 'id';
266 }
267
268 foreach ( $props as $key => $prop ) {
269 $value = $this->{"get_$prop"}( 'edit' );
270
271 if ( $value && is_a( $value, StoreengineDatetime::class ) ) {
272 $value = $this->prepare_date_for_db( $value );
273 }
274
275 $format[] = $this->predict_format( $key, $value );
276 $data[ $key ] = $value;
277 }
278
279 return [
280 'data' => apply_filters( 'storeengine/' . $this->object_type . '_operational_data/db/' . $context, $data, $this ),
281 'format' => $format,
282 ];
283 }
284 }
285