Passed
Push — master ( cb75a2...6929e7 )
by Brian
05:54 queued 11s
created

GetPaid_Form_Item::get_recurring_sub_total()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
nop 1
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
	exit;
4
}
5
6
/**
7
 * Form Item Class
8
 *
9
 */
10
class GetPaid_Form_Item  extends WPInv_Item {
11
12
    /**
13
	 * Stores a custom description for the item.
14
	 *
15
	 * @var string
16
	 */
17
	protected $custom_description = null;
18
19
	/**
20
	 * Stores the item quantity.
21
	 *
22
	 * @var int
23
	 */
24
	protected $quantity = 1;
25
26
	/**
27
	 * Stores the item meta.
28
	 *
29
	 * @var array
30
	 */
31
	protected $meta = array();
32
33
	/**
34
	 * Is this item required?
35
	 *
36
	 * @var int
37
	 */
38
	protected $is_required = true;
39
40
	/**
41
	 * Are quantities allowed?
42
	 *
43
	 * @var int
44
	 */
45
	protected $allow_quantities = false;
46
47
	/**
48
	 * Associated invoice.
49
	 *
50
	 * @var int
51
	 */
52
	public $invoice_id = 0;
53
54
	/**
55
	 * Item discount.
56
	 *
57
	 * @var float
58
	 */
59
	public $item_discount = 0;
60
61
	/**
62
	 * Item tax.
63
	 *
64
	 * @var float
65
	 */
66
	public $item_tax = 0;
67
68
    /*
69
	|--------------------------------------------------------------------------
70
	| CRUD methods
71
	|--------------------------------------------------------------------------
72
	|
73
	| Methods which create, read, update and delete items from the object.
74
	|
75
    */
76
77
    /*
78
	|--------------------------------------------------------------------------
79
	| Getters
80
	|--------------------------------------------------------------------------
81
    */
82
83
    /**
84
	 * Get the item name.
85
	 *
86
	 * @since 1.0.19
87
	 * @param  string $context View or edit context.
88
	 * @return string
89
	 */
90
	public function get_name( $context = 'view' ) {
91
		$name = parent::get_name( $context );
92
		return $name . wpinv_get_item_suffix( $this );
93
	}
94
95
	/**
96
	 * Get the item name without a suffix.
97
	 *
98
	 * @since 1.0.19
99
	 * @param  string $context View or edit context.
100
	 * @return string
101
	 */
102
	public function get_raw_name( $context = 'view' ) {
103
		return parent::get_name( $context );
104
	}
105
106
	/**
107
	 * Get the item description.
108
	 *
109
	 * @since 1.0.19
110
	 * @param  string $context View or edit context.
111
	 * @return string
112
	 */
113
	public function get_description( $context = 'view' ) {
114
115
		if ( isset( $this->custom_description ) ) {
116
			return $this->custom_description;
117
		}
118
119
		return parent::get_description( $context );
120
	}
121
122
	/**
123
	 * Returns the sub total.
124
	 *
125
	 * @since 1.0.19
126
	 * @param  string $context View or edit context.
127
	 * @return int
128
	 */
129
	public function get_sub_total( $context = 'view' ) {
130
		return $this->get_quantity( $context ) * $this->get_initial_price( $context );
131
	}
132
133
	/**
134
	 * Returns the recurring sub total.
135
	 *
136
	 * @since 1.0.19
137
	 * @param  string $context View or edit context.
138
	 * @return int
139
	 */
140
	public function get_recurring_sub_total( $context = 'view' ) {
141
		return $this->get_quantity( $context ) * $this->get_price( $context );
142
	}
143
144
	/**
145
	 * @deprecated
146
	 */
147
	public function get_qantity( $context = 'view' ) {
148
		return $this->get_quantity( $context );
149
	}
150
151
	/**
152
	 * Get the item quantity.
153
	 *
154
	 * @since 1.0.19
155
	 * @param  string $context View or edit context.
156
	 * @return int
157
	 */
158
	public function get_quantity( $context = 'view' ) {
159
		$quantity = (int) $this->quantity;
160
161
		if ( empty( $quantity ) || 1 > $quantity ) {
162
			$quantity = 1;
163
		}
164
165
		if ( 'view' == $context ) {
166
			return apply_filters( 'getpaid_payment_form_item_quantity', $quantity, $this );
167
		}
168
169
		return $quantity;
170
171
	}
172
173
	/**
174
	 * Get the item meta data.
175
	 *
176
	 * @since 1.0.19
177
	 * @param  string $context View or edit context.
178
	 * @return meta
0 ignored issues
show
Bug introduced by
The type meta was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
179
	 */
180
	public function get_item_meta( $context = 'view' ) {
181
		$meta = $this->meta;
182
183
		if ( 'view' == $context ) {
184
			return apply_filters( 'getpaid_payment_form_item_meta', $meta, $this );
0 ignored issues
show
Bug Best Practice introduced by
The expression return apply_filters('ge...em_meta', $meta, $this) also could return the type array which is incompatible with the documented return type meta.
Loading history...
185
		}
186
187
		return $meta;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $meta returns the type array which is incompatible with the documented return type meta.
Loading history...
188
189
	}
190
191
	/**
192
	 * Returns whether or not customers can update the item quantity.
193
	 *
194
	 * @since 1.0.19
195
	 * @param  string $context View or edit context.
196
	 * @return bool
197
	 */
198
	public function get_allow_quantities( $context = 'view' ) {
199
		$allow_quantities = (bool) $this->allow_quantities;
200
201
		if ( 'view' == $context ) {
202
			return apply_filters( 'getpaid_payment_form_item_allow_quantities', $allow_quantities, $this );
203
		}
204
205
		return $allow_quantities;
206
207
	}
208
209
	/**
210
	 * Returns whether or not the item is required.
211
	 *
212
	 * @since 1.0.19
213
	 * @param  string $context View or edit context.
214
	 * @return bool
215
	 */
216
	public function get_is_required( $context = 'view' ) {
217
		$is_required = (bool) $this->is_required;
218
219
		if ( 'view' == $context ) {
220
			return apply_filters( 'getpaid_payment_form_item_is_required', $is_required, $this );
221
		}
222
223
		return $is_required;
224
225
	}
226
227
	/**
228
	 * Prepares form data for use.
229
	 *
230
	 * @since 1.0.19
231
	 * @return array
232
	 */
233
	public function prepare_data_for_use() {
234
235
		return array(
236
			'title'            => sanitize_text_field( $this->get_name() ),
237
			'id'               => $this->get_id(),
238
			'price'            => $this->get_price(),
239
			'recurring'        => $this->is_recurring(),
240
			'description'      => $this->get_description(),
241
			'allow_quantities' => $this->allows_quantities(),
242
			'required'         => $this->is_required(),
243
		);
244
245
	}
246
247
	/**
248
	 * Prepares form data for ajax use.
249
	 *
250
	 * @since 1.0.19
251
	 * @return array
252
	 */
253
	public function prepare_data_for_invoice_edit_ajax() {
254
255
		return array(
256
			'id'     => $this->get_id(),
257
			'texts'  => array(
258
				'item-name'        => sanitize_text_field( $this->get_name() ),
259
				'item-description' => wp_kses_post( $this->get_description() ),
260
				'item-quantity'    => absint( $this->get_quantity() ),
261
				'item-price'       => wpinv_price( wpinv_format_amount ( $this->get_price() ) ),
262
				'item-total'       => wpinv_price( wpinv_format_amount( $this->get_sub_total() ) ),
263
			),
264
			'inputs' => array(
265
				'item-id'          => $this->get_id(),
266
				'item-name'        => sanitize_text_field( $this->get_name() ),
267
				'item-description' => wp_kses_post( $this->get_description() ),
268
				'item-quantity'    => absint( $this->get_quantity() ),
269
				'item-price'       => $this->get_price(),
270
			)
271
		);
272
273
	}
274
275
	/**
276
	 * Prepares form data for saving (cart_details).
277
	 *
278
	 * @since 1.0.19
279
	 * @return array
280
	 */
281
	public function prepare_data_for_saving() {
282
283
		return array(
284
			'post_id'           => $this->invoice_id,
285
			'item_id'           => $this->get_id(),
286
			'item_name'         => sanitize_text_field( $this->get_name() ),
287
			'item_description'  => $this->get_description(),
288
			'tax'               => $this->item_tax,
289
			'item_price'        => $this->get_price(),
290
			'quantity'          => (int) $this->get_quantity(),
291
			'discount'          => $this->item_discount,
292
			'subtotal'          => $this->get_sub_total(),
293
			'price'             => $this->get_sub_total() + $this->item_tax + $this->item_discount,
294
			'meta'              => $this->get_item_meta(),
295
        );
296
	}
297
298
    /*
299
	|--------------------------------------------------------------------------
300
	| Setters
301
	|--------------------------------------------------------------------------
302
	|
303
	| Functions for setting order data. These should not update anything in the
304
	| database itself and should only change what is stored in the class
305
	| object.
306
    */
307
308
	/**
309
	 * Set the item qantity.
310
	 *
311
	 * @since 1.0.19
312
	 * @param  int $quantity The item quantity.
313
	 */
314
	public function set_quantity( $quantity ) {
315
316
		if ( empty( $quantity ) || ! is_numeric( $quantity ) ) {
317
			$quantity = 1;
318
		}
319
320
		$this->quantity = (int) $quantity;
321
322
	}
323
324
	/**
325
	 * Set the item meta data.
326
	 *
327
	 * @since 1.0.19
328
	 * @param  array $meta The item meta data.
329
	 */
330
	public function set_item_meta( $meta ) {
331
		$this->meta = maybe_unserialize( $meta );
0 ignored issues
show
Bug introduced by
$meta of type array is incompatible with the type string expected by parameter $data of maybe_unserialize(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

331
		$this->meta = maybe_unserialize( /** @scrutinizer ignore-type */ $meta );
Loading history...
332
	}
333
334
	/**
335
	 * Set whether or not the quantities are allowed.
336
	 *
337
	 * @since 1.0.19
338
	 * @param  bool $allow_quantities
339
	 */
340
	public function set_allow_quantities( $allow_quantities ) {
341
		$this->allow_quantities = (bool) $allow_quantities;
0 ignored issues
show
Documentation Bug introduced by
The property $allow_quantities was declared of type integer, but (bool)$allow_quantities is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
342
	}
343
344
	/**
345
	 * Set whether or not the item is required.
346
	 *
347
	 * @since 1.0.19
348
	 * @param  bool $is_required
349
	 */
350
	public function set_is_required( $is_required ) {
351
		$this->is_required = (bool) $is_required;
0 ignored issues
show
Documentation Bug introduced by
The property $is_required was declared of type integer, but (bool)$is_required is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
352
	}
353
354
	/**
355
	 * Sets the custom item description.
356
	 *
357
	 * @since 1.0.19
358
	 * @param  string $description
359
	 */
360
	public function set_custom_description( $description ) {
361
		$this->custom_description = $description;
362
	}
363
364
    /**
365
     * We do not want to save items to the database.
366
     * 
367
	 * @return int item id
368
     */
369
    public function save( $data = array() ) {
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

369
    public function save( /** @scrutinizer ignore-unused */ $data = array() ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
370
        return $this->get_id();
371
	}
372
373
    /*
374
	|--------------------------------------------------------------------------
375
	| Conditionals
376
	|--------------------------------------------------------------------------
377
	|
378
	| Checks if a condition is true or false.
379
	|
380
	*/
381
382
    /**
383
	 * Checks whether the item has enabled dynamic pricing.
384
	 *
385
	 * @since 1.0.19
386
	 * @return bool
387
	 */
388
	public function is_required() {
389
        return (bool) $this->get_is_required();
390
	}
391
392
	/**
393
	 * Checks whether users can edit the quantities.
394
	 *
395
	 * @since 1.0.19
396
	 * @return bool
397
	 */
398
	public function allows_quantities() {
399
        return (bool) $this->get_allow_quantities();
400
	}
401
402
}
403