Passed
Push — master ( ee81c6...9867ed )
by Brian
19:07 queued 03:15
created

WPInv_Meta_Box_Items::vat_rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 19
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 19
rs 9.6333
1
<?php
2
// MUST have WordPress.
3
if ( !defined( 'WPINC' ) ) {
4
    exit( 'Do NOT access this file directly: ' . basename( __FILE__ ) );
5
}
6
7
class WPInv_Meta_Box_Items {
8
    public static function output( $post ) {
9
        global $wpinv_euvat, $ajax_cart_details;
10
11
        $post_id            = !empty( $post->ID ) ? $post->ID : 0;
12
        $invoice            = new WPInv_Invoice( $post_id );
13
        $ajax_cart_details  = $invoice->get_cart_details();
14
        $subtotal           = $invoice->get_subtotal( true );
15
        $discount_raw       = $invoice->get_discount();
16
        $discount           = wpinv_price( $discount_raw, $invoice->get_currency() );
0 ignored issues
show
Unused Code introduced by
The assignment to $discount is dead and can be removed.
Loading history...
17
        $discounts          = $discount_raw > 0 ? $invoice->get_discounts() : '';
0 ignored issues
show
Unused Code introduced by
The assignment to $discounts is dead and can be removed.
Loading history...
18
        $tax                = $invoice->get_tax( true );
19
        $total              = $invoice->get_total( true );
20
        $item_quantities    = wpinv_item_quantities_enabled();
21
        $use_taxes          = wpinv_use_taxes();
22
        if ( !$use_taxes && (float)$invoice->get_tax() > 0 ) {
23
            $use_taxes = true;
24
        }
25
        $item_types         = apply_filters( 'wpinv_item_types_for_quick_add_item', wpinv_get_item_types(), $post );
26
        $is_recurring       = $invoice->is_recurring();
27
        $post_type_object   = get_post_type_object($invoice->post_type);
28
        $type_title         = $post_type_object->labels->singular_name;
29
30
        $cols = 5;
31
        if ( $item_quantities ) {
32
            $cols++;
33
        }
34
        if ( $use_taxes ) {
35
            $cols++;
36
        }
37
        $class = '';
38
        if ( $invoice->is_paid() ) {
39
            $class .= ' wpinv-paid';
40
        }
41
        if ( $invoice->is_refunded() ) {
42
            $class .= ' wpinv-refunded';
43
        }
44
        if ( $is_recurring ) {
45
            $class .= ' wpi-recurring';
46
        }
47
        ?>
48
        <div class="wpinv-items-wrap<?php echo $class; ?>" id="wpinv_items_wrap" data-status="<?php echo $invoice->status; ?>">
49
            <table id="wpinv_items" class="wpinv-items" cellspacing="0" cellpadding="0">
50
                <thead>
51
                    <tr>
52
                        <th class="id"><?php _e( 'ID', 'invoicing' );?></th>
53
                        <th class="title"><?php _e( 'Item', 'invoicing' );?></th>
54
                        <th class="price"><?php _e( 'Price', 'invoicing' );?></th>
55
                        <?php if ( $item_quantities ) { ?>
56
                        <th class="qty"><?php _e( 'Qty', 'invoicing' );?></th>
57
                        <?php } ?>
58
                        <th class="total"><?php _e( 'Total', 'invoicing' );?></th>
59
                        <?php if ( $use_taxes ) { ?>
60
                        <th class="tax"><?php _e( 'Tax (%)', 'invoicing' );?></th>
61
                        <?php } ?>
62
                        <th class="action"></th>
63
                    </tr>
64
                </thead>
65
                <tbody class="wpinv-line-items">
66
                    <?php echo wpinv_admin_get_line_items( $invoice ); ?>
67
                </tbody>
68
                <tfoot class="wpinv-totals">
69
                    <tr>
70
                        <td colspan="<?php echo $cols; ?>" style="padding:0;border:0">
71
                            <div id="wpinv-quick-add">
72
                                <table cellspacing="0" cellpadding="0">
73
                                    <tr>
74
                                        <td class="id">
75
                                        </td>
76
                                        <td class="title">
77
                                            <input type="text" class="regular-text" placeholder="<?php _e( 'Item Name', 'invoicing' ); ?>" value="" name="_wpinv_quick[name]">
78
                                            <?php if ( $wpinv_euvat->allow_vat_rules() ) { ?>
79
                                            <div class="wp-clearfix">
80
                                                <label class="wpi-vat-rule">
81
                                                    <span class="title"><?php _e( 'VAT rule type', 'invoicing' );?></span>
82
                                                    <span class="input-text-wrap">
83
                                                        <?php echo wpinv_html_select( array(
84
                                                            'options'          => $wpinv_euvat->get_rules(),
85
                                                            'name'             => '_wpinv_quick[vat_rule]',
86
                                                            'id'               => '_wpinv_quick_vat_rule',
87
                                                            'show_option_all'  => false,
88
                                                            'show_option_none' => false,
89
                                                            'class'            => 'gdmbx2-text-medium wpinv-quick-vat-rule wpi_select2',
90
                                                        ) ); ?>
91
                                                    </span>
92
                                                </label>
93
                                            </div>
94
                                            <?php } if ( $wpinv_euvat->allow_vat_classes() ) { ?>
95
                                            <div class="wp-clearfix">
96
                                                <label class="wpi-vat-class">
97
                                                    <span class="title"><?php _e( 'VAT class', 'invoicing' );?></span>
98
                                                    <span class="input-text-wrap">
99
                                                        <?php echo wpinv_html_select( array(
100
                                                            'options'          => $wpinv_euvat->get_all_classes(),
101
                                                            'name'             => '_wpinv_quick[vat_class]',
102
                                                            'id'               => '_wpinv_quick_vat_class',
103
                                                            'show_option_all'  => false,
104
                                                            'show_option_none' => false,
105
                                                            'class'            => 'gdmbx2-text-medium wpinv-quick-vat-class wpi_select2',
106
                                                        ) ); ?>
107
                                                    </span>
108
                                                </label>
109
                                            </div>
110
                                            <?php } ?>
111
                                            <div class="wp-clearfix">
112
                                                <label class="wpi-item-type">
113
                                                    <span class="title"><?php _e( 'Item type', 'invoicing' );?></span>
114
                                                    <span class="input-text-wrap">
115
                                                        <?php echo wpinv_html_select( array(
116
                                                            'options'          => $item_types,
117
                                                            'name'             => '_wpinv_quick[type]',
118
                                                            'id'               => '_wpinv_quick_type',
119
                                                            'selected'         => 'custom',
120
                                                            'show_option_all'  => false,
121
                                                            'show_option_none' => false,
122
                                                            'class'            => 'gdmbx2-text-medium wpinv-quick-type wpi_select2',
123
                                                        ) ); ?>
124
                                                    </span>
125
                                                </label>
126
                                            </div>
127
128
                                            <div class="wp-clearfix">
129
                                                <?php 
130
                                                    echo wpinv_html_textarea( array(
131
                                                        'name'  => '_wpinv_quick[excerpt]',
132
                                                        'id'    => '_wpinv_quick_excerpt',
133
                                                        'value' => '',
134
                                                        'class' => 'large-text',
135
                                                        'label' => __( 'Item description', 'invoicing' ),
136
                                                    ) ); 
137
                                                ?>
138
                                            </div>
139
140
                                            <div class="wp-clearfix">
141
                                                <label class="wpi-item-actions">
142
                                                    <span class="input-text-wrap">
143
                                                        <input type="button" value="<?php esc_attr_e( 'Add', 'invoicing' ); ?>" class="button button-primary" id="wpinv-save-item"><input type="button" value="Cancel" class="button button-secondary" id="wpinv-cancel-item">
144
                                                    </span>
145
                                                </label>
146
                                            </div>
147
                                        </td>
148
                                        <td class="price"><input type="text" placeholder="0.00" class="wpi-field-price wpi-price" name="_wpinv_quick[price]" /></td>
149
                                        <?php if ( $item_quantities ) { ?>
150
                                        <td class="qty"><input type="number" class="small-text" step="1" min="1" value="1" name="_wpinv_quick[qty]" /></td>
151
                                        <?php } ?>
152
                                        <td class="total"></td>
153
                                        <?php if ( $use_taxes ) { ?>
154
                                        <td class="tax"></td>
155
                                        <?php } ?>
156
                                        <td class="action"></td>
157
                                    </tr>
158
                                </table>
159
                            </div>
160
                        </td>
161
                    </tr>
162
                    <tr class="clear">
163
                        <td colspan="<?php echo $cols; ?>"></td>
164
                    </tr>
165
                    <tr class="totals">
166
                        <td colspan="<?php echo ( $cols - 4 ); ?>"></td>
167
                        <td colspan="4">
168
                            <table cellspacing="0" cellpadding="0">
169
                                <tr class="subtotal">
170
                                    <td class="name"><?php _e( 'Sub Total:', 'invoicing' );?></td>
171
                                    <td class="total"><?php echo $subtotal;?></td>
172
                                    <td class="action"></td>
173
                                </tr>
174
                                <tr class="discount">
175
                                    <td class="name"><?php wpinv_get_discount_label( wpinv_discount_code( $invoice->ID ) ); ?>:</td>
176
                                    <td class="total"><?php echo wpinv_discount( $invoice->ID, true, true ); ?></td>
177
                                    <td class="action"></td>
178
                                </tr>
179
                                <?php if ( $use_taxes ) { ?>
180
                                <tr class="tax">
181
                                    <td class="name"><?php _e( 'Tax:', 'invoicing' );?></td>
182
                                    <td class="total"><?php echo $tax;?></td>
183
                                    <td class="action"></td>
184
                                </tr>
185
                                <?php } ?>
186
                                <tr class="total">
187
                                    <td class="name"><?php echo apply_filters( 'wpinv_invoice_items_total_label', __( 'Invoice Total:', 'invoicing' ), $invoice );?></td>
188
                                    <td class="total"><?php echo $total;?></td>
189
                                    <td class="action"></td>
190
                                </tr>
191
                            </table>
192
                        </td>
193
                    </tr>
194
                </tfoot>
195
            </table>
196
            <div class="wpinv-actions">
197
                <?php ob_start(); ?>
198
                <?php
199
                    if ( !$invoice->is_paid() && !$invoice->is_refunded() ) {
200
                        if ( !$invoice->is_recurring() ) {
201
                            echo wpinv_item_dropdown( array(
202
                                'name'             => 'wpinv_invoice_item',
203
                                'id'               => 'wpinv_invoice_item',
204
                                'show_recurring'   => true,
205
                                'class'            => 'wpi_select2',
206
                            ) );
207
                    ?>
208
                <input type="button" value="<?php echo sprintf(esc_attr__( 'Add item to %s', 'invoicing'), $type_title); ?>" class="button button-primary" id="wpinv-add-item"><input type="button" value="<?php esc_attr_e( 'Create new item', 'invoicing' );?>" class="button button-primary" id="wpinv-new-item"><?php } ?><input type="button" value="<?php esc_attr_e( 'Recalculate Totals', 'invoicing' );?>" class="button button-primary wpinv-flr" id="wpinv-recalc-totals">
209
                    <?php } ?>
210
                <?php do_action( 'wpinv_invoice_items_actions', $invoice ); ?>
211
                <?php $item_actions = ob_get_clean(); echo apply_filters( 'wpinv_invoice_items_actions_content', $item_actions, $invoice, $post ); ?>
212
            </div>
213
        </div>
214
        <?php
215
    }
216
217
    public static function prices( $post ) {        
218
        $symbol         = wpinv_currency_symbol();
219
        $position       = wpinv_currency_position();
220
        $item           = new WPInv_Item( $post->ID );
221
222
        $price                = $item->get_price();
223
        $is_dynamic_pricing   = $item->get_is_dynamic_pricing();
224
        $minimum_price        = $item->get_minimum_price();
225
        $is_recurring         = $item->is_recurring();
226
        $period               = $item->get_recurring_period();
227
        $interval             = absint( $item->get_recurring_interval() );
228
        $times                = absint( $item->get_recurring_limit() );
229
        $free_trial           = $item->has_free_trial();
230
        $trial_interval       = $item->get_trial_interval();
231
        $trial_period         = $item->get_trial_period();
232
233
        $intervals            = array();
234
        for ( $i = 1; $i <= 90; $i++ ) {
235
            $intervals[$i] = $i;
236
        }
237
238
        $interval       = $interval > 0 ? $interval : 1;
239
240
        $class = $is_recurring ? 'wpinv-recurring-y' : 'wpinv-recurring-n';
241
242
        $minimum_price_style = 'margin-left: 24px;';
243
        if(! $is_dynamic_pricing ) {
244
            $minimum_price_style .= 'display: none;';
245
        }
246
247
        ?>
248
        <p class="wpinv-row-prices"><?php echo ( $position != 'right' ? $symbol . '&nbsp;' : '' );?><input type="text" maxlength="12" placeholder="<?php echo wpinv_sanitize_amount( 0 ); ?>" value="<?php echo $price;?>" id="wpinv_item_price" name="wpinv_item_price" class="medium-text wpi-field-price wpi-price" <?php disabled( $item->is_editable(), false ); ?> /><?php echo ( $position == 'right' ? '&nbsp;' . $symbol : '' );?><input type="hidden" name="wpinv_vat_meta_box_nonce" value="<?php echo wp_create_nonce( 'wpinv_item_meta_box_save' ) ;?>" />
249
        <?php do_action( 'wpinv_prices_metabox_price', $item ); ?>
250
        </p>
251
252
    <?php if( $item->supports_dynamic_pricing() ) { ?>
253
254
        <p class="wpinv-row-name-your-price">
255
            <label>
256
                <input type="checkbox" name="wpinv_name_your_price" id="wpinv_name_your_price" value="1" <?php checked( 1, $is_dynamic_pricing ); ?> />
257
                <?php echo apply_filters( 'wpinv_name_your_price_toggle_text', __( 'User can set a custom price', 'invoicing' ) ); ?>
258
            </label>
259
            <?php do_action( 'wpinv_prices_metabox_name_your_price_field', $item ); ?>
260
        </p>
261
262
        <p class="wpinv-row-minimum-price" style="<?php echo $minimum_price_style; ?>">
263
            <label>
264
                <?php _e( 'Minimum Price', 'invoicing' ); ?>
265
                <?php echo ( $position != 'right' ? $symbol . '&nbsp;' : '' );?><input type="text" maxlength="12" placeholder="<?php echo wpinv_sanitize_amount( 0 ); ?>" value="<?php echo $minimum_price;?>" id="wpinv_minimum_price" name="wpinv_minimum_price" class="medium-text wpi-field-price" <?php disabled( $item->is_editable(), false ); ?> /><?php echo ( $position == 'right' ? '&nbsp;' . $symbol : '' );?>
266
            </label>
267
268
            <?php do_action( 'wpinv_prices_metabox_minimum_price_field', $item ); ?>
269
        </p>
270
271
    <?php } ?>
272
273
        <p class="wpinv-row-is-recurring">
274
            <label for="wpinv_is_recurring">
275
                <input type="checkbox" name="wpinv_is_recurring" id="wpinv_is_recurring" value="1" <?php checked( 1, $is_recurring ); ?> />
276
                <?php echo apply_filters( 'wpinv_is_recurring_toggle_text', __( 'Is Recurring Item?', 'invoicing' ) ); ?>
277
            </label>
278
            <?php do_action( 'wpinv_prices_metabox_is_recurring_field', $item ); ?>
279
        </p>
280
        <p class="wpinv-row-recurring-fields <?php echo $class;?>">
281
            <label class="wpinv-period" for="wpinv_recurring_period"><?php _e( 'Recurring', 'invoicing' );?> <select class="wpinv-select wpi_select2" id="wpinv_recurring_period" name="wpinv_recurring_period"><option value="D" data-text="<?php esc_attr_e( 'day(s)', 'invoicing' ); ?>" <?php selected( 'D', $period );?>><?php _e( 'Daily', 'invoicing' ); ?></option><option value="W" data-text="<?php esc_attr_e( 'week(s)', 'invoicing' ); ?>" <?php selected( 'W', $period );?>><?php _e( 'Weekly', 'invoicing' ); ?></option><option value="M" data-text="<?php esc_attr_e( 'month(s)', 'invoicing' ); ?>" <?php selected( 'M', $period );?>><?php _e( 'Monthly', 'invoicing' ); ?></option><option value="Y" data-text="<?php esc_attr_e( 'year(s)', 'invoicing' ); ?>" <?php selected( 'Y', $period );?>><?php _e( 'Yearly', 'invoicing' ); ?></option></select></label>
282
            <label class="wpinv-interval" for="wpinv_recurring_interval"> <?php _e( 'at every', 'invoicing' );?> <?php echo wpinv_html_select( array(
283
                'options'          => $intervals,
284
                'name'             => 'wpinv_recurring_interval',
285
                'id'               => 'wpinv_recurring_interval',
286
                'selected'         => $interval,
287
                'show_option_all'  => false,
288
                'show_option_none' => false,
289
                'class'            => 'wpi_select2',
290
            ) ); ?> <span id="wpinv_interval_text"><?php _e( 'day(s)', 'invoicing' );?></span></label>
291
            <label class="wpinv-times" for="wpinv_recurring_limit"> <?php _e( 'for', 'invoicing' );?> <input class="small-text" type="number" value="<?php echo $times;?>" size="4" id="wpinv_recurring_limit" name="wpinv_recurring_limit" step="1" min="0"> <?php _e( 'time(s) <i>(select 0 for recurring forever until cancelled</i>)', 'invoicing' );?></label>
292
            <span class="clear wpi-trial-clr"></span>
293
            <label class="wpinv-free-trial" for="wpinv_free_trial">
294
                <input type="checkbox" name="wpinv_free_trial" id="wpinv_free_trial" value="1" <?php checked( true, (bool)$free_trial ); ?> /> 
295
                <?php echo __( 'Offer free trial for', 'invoicing' ); ?>
296
            </label>
297
            <label class="wpinv-trial-interval" for="wpinv_trial_interval">
298
                <input class="small-text" type="number" value="<?php echo $trial_interval;?>" size="4" id="wpinv_trial_interval" name="wpinv_trial_interval" step="1" min="1"> <select class="wpinv-select wpi_select2" id="wpinv_trial_period" name="wpinv_trial_period"><option value="D" <?php selected( 'D', $trial_period );?>><?php _e( 'day(s)', 'invoicing' ); ?></option><option value="W" <?php selected( 'W', $trial_period );?>><?php _e( 'week(s)', 'invoicing' ); ?></option><option value="M" <?php selected( 'M', $trial_period );?>><?php _e( 'month(s)', 'invoicing' ); ?></option><option value="Y" <?php selected( 'Y', $trial_period );?>><?php _e( 'year(s)', 'invoicing' ); ?></option></select>
299
            </label>
300
            <?php do_action( 'wpinv_prices_metabox_recurring_fields', $item ); ?>
301
        </p>
302
        <input type="hidden" id="_wpi_current_type" value="<?php echo wpinv_get_item_type( $post->ID ); ?>" />
0 ignored issues
show
Bug introduced by
Are you sure wpinv_get_item_type($post->ID) of type false|mixed can be used in echo? ( Ignorable by Annotation )

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

302
        <input type="hidden" id="_wpi_current_type" value="<?php echo /** @scrutinizer ignore-type */ wpinv_get_item_type( $post->ID ); ?>" />
Loading history...
303
        <?php do_action( 'wpinv_item_price_field', $post->ID ); ?>
304
        <?php
305
    }
306
307
    public static function vat_rules( $post ) {
308
        global $wpinv_euvat;
309
310
        $rule_type = $wpinv_euvat->get_item_rule( $post->ID );
311
        ?>
312
        <p><label for="wpinv_vat_rules"><strong><?php _e( 'Select how VAT rules will be applied:', 'invoicing' );?></strong></label>&nbsp;&nbsp;&nbsp;
313
        <?php echo wpinv_html_select( array(
314
                    'options'          => $wpinv_euvat->get_rules(),
315
                    'name'             => 'wpinv_vat_rules',
316
                    'id'               => 'wpinv_vat_rules',
317
                    'selected'         => $rule_type,
318
                    'show_option_all'  => false,
319
                    'show_option_none' => false,
320
                    'class'            => 'gdmbx2-text-medium wpinv-vat-rules wpi_select2',
321
                ) ); ?>
322
        </p>
323
        <p class="wpi-m0"><?php _e( 'When you select physical product rules, only consumers and businesses in your country will be charged VAT.  The VAT rate used will be the rate in your country.', 'invoicing' ); ?></p>
324
        <p class="wpi-m0"><?php _e( 'If you select Digital product rules, VAT will be charged at the rate that applies in the country of the consumer.  Only businesses in your country will be charged VAT.', 'invoicing' ); ?></p>
325
        <p class="wpi-m0"><?php _e( 'Select "Tax-Free" if you do not want to charge VAT for this item.', 'invoicing' ); ?></p>
326
        <?php
327
    }
328
329
    public static function vat_classes( $post ) {
330
        global $wpinv_euvat;
331
        
332
        $vat_class = $wpinv_euvat->get_item_class( $post->ID );
333
        ?>
334
        <p><?php echo wpinv_html_select( array(
335
                    'options'          => $wpinv_euvat->get_all_classes(),
336
                    'name'             => 'wpinv_vat_class',
337
                    'id'               => 'wpinv_vat_class',
338
                    'selected'         => $vat_class,
339
                    'show_option_all'  => false,
340
                    'show_option_none' => false,
341
                    'class'            => 'gdmbx2-text-medium wpinv-vat-class wpi_select2',
342
                ) ); ?>
343
        </p>
344
        <p class="wpi-m0"><?php _e( 'Select the VAT rate class to use for this invoice item.', 'invoicing' ); ?></p>
345
        <?php
346
    }
347
348
    public static function item_info( $post ) {
349
        $item_type = wpinv_get_item_type( $post->ID );
350
        do_action( 'wpinv_item_info_metabox_before', $post );
351
        ?>
352
        <p><label for="wpinv_item_type"><strong><?php _e( 'Type:', 'invoicing' );?></strong></label>&nbsp;&nbsp;&nbsp;
353
        <?php echo wpinv_html_select( array(
354
                    'options'          => wpinv_get_item_types(),
355
                    'name'             => 'wpinv_item_type',
356
                    'id'               => 'wpinv_item_type',
357
                    'selected'         => $item_type,
358
                    'show_option_all'  => false,
359
                    'show_option_none' => false,
360
                    'class'            => 'gdmbx2-text-medium wpinv-item-type',
361
                ) ); ?>
362
        </p>
363
        <p class="wpi-m0"><?php _e( 'Select item type.', 'invoicing' );?><br><?php _e( '<b>Standard:</b> Standard item type', 'invoicing' );?><br><?php _e( '<b>Fee:</b> Like Registration Fee, Sign up Fee etc.', 'invoicing' );?></p>
364
        <?php
365
        do_action( 'wpinv_item_info_metabox_after', $post );
366
    }
367
368
    public static function meta_values( $post ) {
369
        $meta_keys = apply_filters( 'wpinv_show_meta_values_for_keys', array(
370
            'type',
371
            'custom_id'
372
        ) );
373
374
        if ( empty( $meta_keys ) ) {
375
            return;
376
        }
377
378
        do_action( 'wpinv_meta_values_metabox_before', $post );
379
380
        foreach ( $meta_keys as $meta_key ) {
381
            ?>
382
            <p class="wpi-mtb05"><label><strong><?php echo $meta_key; ?></strong>: <?php echo get_post_meta( $post->ID, '_wpinv_' . $meta_key, true ); ?></label></p>
0 ignored issues
show
Bug introduced by
Are you sure get_post_meta($post->ID,...nv_' . $meta_key, true) of type false|mixed|string can be used in echo? ( Ignorable by Annotation )

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

382
            <p class="wpi-mtb05"><label><strong><?php echo $meta_key; ?></strong>: <?php echo /** @scrutinizer ignore-type */ get_post_meta( $post->ID, '_wpinv_' . $meta_key, true ); ?></label></p>
Loading history...
383
            <?php 
384
        }
385
386
        do_action( 'wpinv_meta_values_metabox_after', $post );
387
    }
388
389
    public static function save( $post_id, $data, $post ) {
0 ignored issues
show
Unused Code introduced by
The parameter $post 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

389
    public static function save( $post_id, $data, /** @scrutinizer ignore-unused */ $post ) {

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...
390
        $invoice        = new WPInv_Invoice( $post_id );
391
392
        // Billing
393
        $first_name     = sanitize_text_field( $data['wpinv_first_name'] );
394
        $last_name      = sanitize_text_field( $data['wpinv_last_name'] );
395
        $company        = sanitize_text_field( $data['wpinv_company'] );
396
        $vat_number     = sanitize_text_field( $data['wpinv_vat_number'] );
397
        $phone          = sanitize_text_field( $data['wpinv_phone'] );
398
        $address        = sanitize_text_field( $data['wpinv_address'] );
399
        $city           = sanitize_text_field( $data['wpinv_city'] );
400
        $zip            = sanitize_text_field( $data['wpinv_zip'] );
401
        $country        = sanitize_text_field( $data['wpinv_country'] );
402
        $state          = sanitize_text_field( $data['wpinv_state'] );
403
404
        // Details
405
        $status         = sanitize_text_field( $data['wpinv_status'] );
406
        $old_status     = !empty( $data['original_post_status'] ) ? sanitize_text_field( $data['original_post_status'] ) : $status;
0 ignored issues
show
Unused Code introduced by
The assignment to $old_status is dead and can be removed.
Loading history...
407
        $number         = sanitize_text_field( $data['wpinv_number'] );
0 ignored issues
show
Unused Code introduced by
The assignment to $number is dead and can be removed.
Loading history...
408
        $due_date       = isset( $data['wpinv_due_date'] ) ? sanitize_text_field( $data['wpinv_due_date'] ) : '';
409
        $date_created   = isset( $data['date_created'] ) ? sanitize_text_field( $data['date_created'] ) : '';
410
        //$discounts      = sanitize_text_field( $data['wpinv_discounts'] );
411
        //$discount       = sanitize_text_field( $data['wpinv_discount'] );
412
413
        $disable_taxes = 0;
414
415
        if ( ! empty( $data['disable_taxes'] ) ) {
416
            $disable_taxes = 1;
417
        }
418
419
        $ip             = $invoice->get_ip() ? $invoice->get_ip() : wpinv_get_ip();
420
421
        $invoice->set( 'due_date', $due_date );
422
        $invoice->set( 'first_name', $first_name );
423
        $invoice->set( 'last_name', $last_name );
424
        $invoice->set( 'company', $company );
425
        $invoice->set( 'vat_number', $vat_number );
426
        $invoice->set( 'phone', $phone );
427
        $invoice->set( 'address', $address );
428
        $invoice->set( 'city', $city );
429
        $invoice->set( 'zip', $zip );
430
        $invoice->set( 'country', $country );
431
        $invoice->set( 'state', $state );
432
        $invoice->set( 'status', $status );
433
        $invoice->set( 'set', $status );
434
        //$invoice->set( 'number', $number );
435
        //$invoice->set( 'discounts', $discounts );
436
        //$invoice->set( 'discount', $discount );
437
        $invoice->set( 'disable_taxes', $disable_taxes );
438
        $invoice->set( 'ip', $ip );
439
        $invoice->old_status = $_POST['original_post_status'];
440
        
441
        $currency = $invoice->get_currency();
442
        if ( ! empty( $data['wpinv_currency'] ) ) {
443
            $currency = sanitize_text_field( $data['wpinv_currency'] );
444
        }
445
446
        if ( empty( $currency ) ) {
447
            $currency = wpinv_get_currency();
448
        }
449
450
        if ( ! $invoice->is_paid() ) {
451
            $invoice->currency = $currency;
452
        }
453
454
        if ( !empty( $data['wpinv_gateway'] ) ) {
455
            $invoice->set( 'gateway', sanitize_text_field( $data['wpinv_gateway'] ) );
456
        }
457
        $saved = $invoice->save();
458
459
        if ( ! empty( $date_created ) && strtotime( $date_created, current_time( 'timestamp' ) ) ) {
460
461
            $time = strtotime( $date_created, current_time( 'timestamp' ) );
462
            $date = date( 'Y-m-d H:i:s', $time );
463
            $date_gmt = get_gmt_from_date( $date );
464
465
            wp_update_post(
466
                array(
467
                    'ID'            => $invoice->ID,
468
                    'post_date'     => $date,
469
                    'post_date_gmt' => $date_gmt,
470
                    'edit_date'     => true,
471
                )
472
            );
473
474
            $invoice->date = $date;
475
        }
476
477
        // Check for payment notes
478
        if ( !empty( $data['invoice_note'] ) ) {
479
            $note               = wp_kses( $data['invoice_note'], array() );
480
            $note_type          = sanitize_text_field( $data['invoice_note_type'] );
481
            $is_customer_note   = $note_type == 'customer' ? 1 : 0;
482
483
            wpinv_insert_payment_note( $invoice->ID, $note, $is_customer_note );
484
        }
485
486
        // Update user address if empty.
487
        if ( $saved && !empty( $invoice ) ) {
488
            if ( $user_id = $invoice->get_user_id() ) {
489
                $user_address = wpinv_get_user_address( $user_id, false );
490
491
                if (empty($user_address['first_name'])) {
492
                    update_user_meta( $user_id, '_wpinv_first_name', $first_name );
493
                    update_user_meta( $user_id, '_wpinv_last_name', $last_name );
494
                } else if (empty($user_address['last_name']) && $user_address['first_name'] == $first_name) {
495
                    update_user_meta( $user_id, '_wpinv_last_name', $last_name );
496
                }
497
498
                if (empty($user_address['address']) || empty($user_address['city']) || empty($user_address['state']) || empty($user_address['country'])) {
499
                    update_user_meta( $user_id, '_wpinv_address', $address );
500
                    update_user_meta( $user_id, '_wpinv_city', $city );
501
                    update_user_meta( $user_id, '_wpinv_state', $state );
502
                    update_user_meta( $user_id, '_wpinv_country', $country );
503
                    update_user_meta( $user_id, '_wpinv_zip', $zip );
504
                    update_user_meta( $user_id, '_wpinv_phone', $phone );
505
                }
506
            }
507
508
            do_action( 'wpinv_invoice_metabox_saved', $invoice );
509
        }
510
511
        return $saved;
512
    }
513
}
514