wpinv_get_discount()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Contains discount functions.
4
 *
5
 * @since 1.0.0
6
 * @package Invoicing
7
 */
8
9
defined( 'ABSPATH' ) || exit;
10
11
/**
12
 * Returns an array of discount type.
13
 *
14
 * @return array
15
 */
16
function wpinv_get_discount_types() {
17
    return apply_filters(
18
        'wpinv_discount_types',
19
        array(
20
            'percent' => __( 'Percentage', 'invoicing' ),
21
            'flat'    => __( 'Flat Amount', 'invoicing' ),
22
        )
23
    );
24
}
25
26
/**
27
 * Returns the name of a discount type.
28
 *
29
 * @return string
30
 */
31
function wpinv_get_discount_type_name( $type = '' ) {
32
    $types = wpinv_get_discount_types();
33
    return isset( $types[ $type ] ) ? $types[ $type ] : $type;
34
}
35
36
/**
37
 * Deletes a discount via the admin page.
38
 *
39
 */
40
function wpinv_delete_discount( $data ) {
41
42
    $discount = new WPInv_Discount( absint( $data['discount'] ) );
43
    $discount->delete( true );
44
45
}
46
add_action( 'getpaid_authenticated_admin_action_delete_discount', 'wpinv_delete_discount' );
47
48
/**
49
 * Deactivates a discount via the admin page.
50
 */
51
function wpinv_activate_discount( $data ) {
52
53
    $discount = new WPInv_Discount( absint( $data['discount'] ) );
54
    $discount->set_status( 'publish' );
55
    $discount->save();
56
57
}
58
add_action( 'getpaid_authenticated_admin_action_activate_discount', 'wpinv_activate_discount' );
59
60
/**
61
 * Activates a discount via the admin page.
62
 */
63
function wpinv_deactivate_discount( $data ) {
64
65
    $discount = new WPInv_Discount( absint( $data['discount'] ) );
66
    $discount->set_status( 'pending' );
67
    $discount->save();
68
69
}
70
add_action( 'getpaid_authenticated_admin_action_deactivate_discount', 'wpinv_deactivate_discount' );
71
72
/**
73
 * Fetches a discount object.
74
 *
75
 * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code.
76
 * @since 1.0.15
77
 * @return WPInv_Discount
78
 */
79
function wpinv_get_discount( $discount ) {
80
    return new WPInv_Discount( $discount );
81
}
82
83
/**
84
 * Fetches a discount object.
85
 *
86
 * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code.
87
 * @since 1.0.15
88
 * @return WPInv_Discount
89
 */
90
function wpinv_get_discount_obj( $discount = 0 ) {
91
    return new WPInv_Discount( $discount );
92
}
93
94
/**
95
 * Fetch a discount from the db/cache using a given field.
96
 *
97
 * @param string $deprecated deprecated
98
 * @param string|int $value The field value
99
 * @return bool|WPInv_Discount
100
 */
101
function wpinv_get_discount_by( $deprecated = null, $value = '' ) {
102
    $discount = new WPInv_Discount( $value );
103
104
    if ( $discount->get_id() != 0 ) {
105
        return $discount;
106
    }
107
108
    return false;
109
}
110
111
/**
112
 * Returns an array discount statuses.
113
 *
114
 * @return array
115
 */
116
function wpinv_get_discount_statuses() {
117
118
    return array(
119
        'expired'  => __( 'Expired', 'invoicing' ),
120
        'publish'  => __( 'Active', 'invoicing' ),
121
        'inactive' => __( 'Inactive', 'invoicing' ),
122
    );
123
124
}
125
126
/**
127
 * Retrieves an invoice status label.
128
 */
129
function wpinv_discount_status( $status ) {
130
    $statuses = wpinv_get_discount_statuses();
131
    return isset( $statuses[ $status ] ) ? $statuses[ $status ] : __( 'Inactive', 'invoicing' );
132
}
133
134
/**
135
 * Checks if a discount is recurring.
136
 *
137
 * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code.
138
 * @param int|array|string|WPInv_Discount $code discount data, object, ID or code.
139
 * @return bool
140
 */
141
function wpinv_discount_is_recurring( $discount = 0, $code = 0 ) {
142
143
    if ( ! empty( $discount ) ) {
144
        $discount    = wpinv_get_discount_obj( $discount );
145
    } else {
146
        $discount    = wpinv_get_discount_obj( $code );
147
    }
148
149
    return $discount->get_is_recurring();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $discount->get_is_recurring() also could return the type integer|string which is incompatible with the documented return type boolean.
Loading history...
150
}
151
152
/**
153
 * Calculates a discount code's amount.
154
 *
155
 * Ensure that the discount exists and has been validated before calling this method.
156
 *
157
 * @param WPInv_Invoice|GetPaid_Payment_Form_Submission $invoice
158
 * @param WPInv_Discount $discount
159
 * @return array
160
 */
161
function getpaid_calculate_invoice_discount( $invoice, $discount ) {
162
163
	$initial_discount   = 0;
164
	$recurring_discount = 0;
165
166
	foreach ( $invoice->get_items() as $item ) {
167
168
		// Abort if it is not valid for this item.
169
		if ( ! $discount->is_valid_for_items( array( $item->get_id() ) ) ) {
170
			continue;
171
		}
172
173
		// Calculate the initial amount...
174
		$item_discount           = $discount->get_discounted_amount( $item->get_sub_total() );
175
		$recurring_item_discount = 0;
176
177
		// ... and maybe the recurring amount.
178
		if ( $item->is_recurring() && $discount->is_recurring() ) {
179
			$recurring_item_discount = $discount->get_discounted_amount( $item->get_recurring_sub_total() );
180
		}
181
182
		// Discount should not exceed discounted amount.
183
		if ( ! $discount->is_type( 'percent' ) ) {
184
185
			if ( ( $initial_discount + $item_discount ) > $discount->get_amount() ) {
186
				$item_discount = $discount->get_amount() - $initial_discount;
187
			}
188
189
			if ( ( $recurring_discount + $recurring_item_discount ) > $discount->get_amount() ) {
190
				$recurring_item_discount = $discount->get_amount() - $recurring_discount;
191
			}
192
        }
193
194
		$initial_discount             += $item_discount;
195
		$recurring_discount           += $recurring_item_discount;
196
		$item->item_discount           = $item_discount;
197
		$item->recurring_item_discount = $recurring_item_discount;
198
199
	}
200
201
	return array(
202
		'name'               => 'discount_code',
203
		'discount_code'      => $discount->get_code(),
204
		'initial_discount'   => $initial_discount,
205
		'recurring_discount' => $recurring_discount,
206
	);
207
208
}
209
210
/**
211
 * Checks if we have an active discount.
212
 *
213
 * @return bool
214
 */
215
function getpaid_has_published_discount() {
216
217
    $discounts = get_posts(
218
        array(
219
            'post_type'   => 'wpi_discount',
220
            'numberposts' => 1,
221
            'fields'      => array( 'ids' ),
222
        )
223
    );
224
225
    return ! empty( $discounts );
226
227
}
228