Passed
Pull Request — master (#377)
by Brian
05:01
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 = '';
18
19
	/**
20
	 * Stores the item quantity.
21
	 *
22
	 * @var int
23
	 */
24
	protected $quantity = 1;
25
26
	/**
27
	 * Is this item required?
28
	 *
29
	 * @var int
30
	 */
31
	protected $is_required = true;
32
33
	/**
34
	 * Are quantities allowed?
35
	 *
36
	 * @var int
37
	 */
38
	protected $allow_quantities = false;
39
40
	/**
41
	 * Associated invoice.
42
	 *
43
	 * @var int
44
	 */
45
	public $invoice_id = 0;
46
47
	/**
48
	 * Item discount.
49
	 *
50
	 * @var float
51
	 */
52
	public $item_discount = 0;
53
54
	/**
55
	 * Item tax.
56
	 *
57
	 * @var float
58
	 */
59
	public $item_tax = 0;
60
61
    /*
62
	|--------------------------------------------------------------------------
63
	| CRUD methods
64
	|--------------------------------------------------------------------------
65
	|
66
	| Methods which create, read, update and delete items from the object.
67
	|
68
    */
69
70
    /*
71
	|--------------------------------------------------------------------------
72
	| Getters
73
	|--------------------------------------------------------------------------
74
    */
75
76
    /**
77
	 * Get the item name.
78
	 *
79
	 * @since 1.0.19
80
	 * @param  string $context View or edit context.
81
	 * @return string
82
	 */
83
	public function get_name( $context = 'view' ) {
84
		$name = parent::get_name( $context );
85
		return $name . wpinv_get_item_suffix( $this );
86
	}
87
88
	/**
89
	 * Get the item description.
90
	 *
91
	 * @since 1.0.19
92
	 * @param  string $context View or edit context.
93
	 * @return string
94
	 */
95
	public function get_description( $context = 'view' ) {
96
97
		if ( ! empty( $this->custom_description ) ) {
98
			return $this->custom_description;
99
		}
100
101
		return parent::get_description( $context );
102
	}
103
104
	/**
105
	 * Returns the sub total.
106
	 *
107
	 * @since 1.0.19
108
	 * @param  string $context View or edit context.
109
	 * @return int
110
	 */
111
	public function get_sub_total( $context = 'view' ) {
112
		return $this->get_quantity( $context ) * $this->get_initial_price( $context );
113
	}
114
115
	/**
116
	 * Returns the recurring sub total.
117
	 *
118
	 * @since 1.0.19
119
	 * @param  string $context View or edit context.
120
	 * @return int
121
	 */
122
	public function get_recurring_sub_total( $context = 'view' ) {
123
		return $this->get_quantity( $context ) * $this->get_price( $context );
124
	}
125
126
	/**
127
	 * @deprecated
128
	 */
129
	public function get_qantity( $context = 'view' ) {
130
		return $this->get_quantity( $context );
131
	}
132
133
	/**
134
	 * Get the item quantity.
135
	 *
136
	 * @since 1.0.19
137
	 * @param  string $context View or edit context.
138
	 * @return int
139
	 */
140
	public function get_quantity( $context = 'view' ) {
141
		$quantity = (int) $this->quantity;
142
143
		if ( empty( $quantity ) || 1 > $quantity ) {
144
			$quantity = 1;
145
		}
146
147
		if ( 'view' == $context ) {
148
			return apply_filters( 'getpaid_payment_form_item_quanity', $quantity, $this );
149
		}
150
151
		return $quantity;
152
153
	}
154
155
	/**
156
	 * Returns whether or not customers can update the item quantity.
157
	 *
158
	 * @since 1.0.19
159
	 * @param  string $context View or edit context.
160
	 * @return bool
161
	 */
162
	public function get_allow_quantities( $context = 'view' ) {
163
		$allow_quantities = (bool) $this->allow_quantities;
164
165
		if ( 'view' == $context ) {
166
			return apply_filters( 'getpaid_payment_form_item_allow_quantities', $allow_quantities, $this );
167
		}
168
169
		return $allow_quantities;
170
171
	}
172
173
	/**
174
	 * Returns whether or not the item is required.
175
	 *
176
	 * @since 1.0.19
177
	 * @param  string $context View or edit context.
178
	 * @return bool
179
	 */
180
	public function get_is_required( $context = 'view' ) {
181
		$is_required = (bool) $this->is_required;
182
183
		if ( 'view' == $context ) {
184
			return apply_filters( 'getpaid_payment_form_item_is_required', $is_required, $this );
185
		}
186
187
		return $is_required;
188
189
	}
190
191
	/**
192
	 * Prepares form data for use.
193
	 *
194
	 * @since 1.0.19
195
	 * @return array
196
	 */
197
	public function prepare_data_for_use() {
198
199
		return array(
200
			'title'            => sanitize_text_field( $this->get_name() ),
201
			'id'               => $this->get_id(),
202
			'price'            => $this->get_price(),
203
			'recurring'        => $this->is_recurring(),
204
			'description'      => $this->get_description(),
205
			'allow_quantities' => $this->allows_quantities(),
206
			'required'         => $this->is_required(),
207
        );
208
	}
209
210
	/**
211
	 * Prepares form data for saving (cart_details).
212
	 *
213
	 * @since 1.0.19
214
	 * @return array
215
	 */
216
	public function prepare_data_for_saving() {
217
218
		return array(
219
			'post_id'           => $this->invoice_id,
220
			'item_id'           => $this->get_id(),
221
			'item_name'         => sanitize_text_field( $this->get_name() ),
222
			'item_description'  => $this->get_description(),
223
			'tax'               => $this->item_tax,
224
			'item_price'        => $this->get_price(),
225
			'quantity'          => (int) $this->get_quantity(),
226
			'discount'          => $this->item_discount,
227
			'subtotal'          => $this->get_sub_total(),
228
			'price'             => $this->get_sub_total() + $this->item_tax + $this->item_discount
229
        );
230
	}
231
232
    /*
233
	|--------------------------------------------------------------------------
234
	| Setters
235
	|--------------------------------------------------------------------------
236
	|
237
	| Functions for setting order data. These should not update anything in the
238
	| database itself and should only change what is stored in the class
239
	| object.
240
    */
241
242
	/**
243
	 * Set the item qantity.
244
	 *
245
	 * @since 1.0.19
246
	 * @param  int $quantity The item quantity.
247
	 */
248
	public function set_quantity( $quantity ) {
249
250
		if ( empty( $quantity ) || ! is_numeric( $quantity ) ) {
251
			$quantity = 1;
252
		}
253
254
		$this->quantity = $quantity;
255
256
	}
257
258
	/**
259
	 * Set whether or not the quantities are allowed.
260
	 *
261
	 * @since 1.0.19
262
	 * @param  bool $allow_quantities
263
	 */
264
	public function set_allow_quantities( $allow_quantities ) {
265
		$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...
266
	}
267
268
	/**
269
	 * Set whether or not the item is required.
270
	 *
271
	 * @since 1.0.19
272
	 * @param  bool $is_required
273
	 */
274
	public function set_is_required( $is_required ) {
275
		$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...
276
	}
277
278
	/**
279
	 * Sets the custom item description.
280
	 *
281
	 * @since 1.0.19
282
	 * @param  string $description
283
	 */
284
	public function set_custom_description( $description ) {
285
		$this->custom_description = $description;
286
	}
287
288
    /**
289
     * We do not want to save items to the database.
290
     * 
291
	 * @return int item id
292
     */
293
    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

293
    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...
294
        return $this->get_id();
295
	}
296
297
    /*
298
	|--------------------------------------------------------------------------
299
	| Conditionals
300
	|--------------------------------------------------------------------------
301
	|
302
	| Checks if a condition is true or false.
303
	|
304
	*/
305
306
    /**
307
	 * Checks whether the item has enabled dynamic pricing.
308
	 *
309
	 * @since 1.0.19
310
	 * @return bool
311
	 */
312
	public function is_required() {
313
        return (bool) $this->get_is_required();
314
	}
315
316
	/**
317
	 * Checks whether users can edit the quantities.
318
	 *
319
	 * @since 1.0.19
320
	 * @return bool
321
	 */
322
	public function allows_quantities() {
323
        return (bool) $this->get_allow_quantities();
324
	}
325
326
}
327