Passed
Push — master ( 9f8a09...a2648f )
by Brian
144:35 queued 63:58
created

WPInv_Meta_Box_Items::vat_classes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 16
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 16
rs 9.7333
1
<?php
2
3
/**
4
 * Item Data
5
 *
6
 * Display the item data meta box.
7
 *
8
 */
9
10
if ( ! defined( 'ABSPATH' ) ) {
11
	exit; // Exit if accessed directly
12
}
13
14
/**
15
 * WPInv_Meta_Box_Items Class.
16
 */
17
class WPInv_Meta_Box_Items {
18
19
    /**
20
	 * Output the metabox.
21
	 *
22
	 * @param WP_Post $post
23
	 */
24
    public static function output( $post ) {
25
        global $wpinv_euvat, $ajax_cart_details;
26
27
        $post_id            = !empty( $post->ID ) ? $post->ID : 0;
28
        $invoice            = new WPInv_Invoice( $post_id );
29
        $ajax_cart_details  = $invoice->get_cart_details();
30
        $subtotal           = $invoice->get_subtotal( true );
31
        $discount_raw       = $invoice->get_discount();
32
        $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...
33
        $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...
34
        $tax                = $invoice->get_tax( true );
35
        $total              = $invoice->get_total( true );
36
        $item_quantities    = wpinv_item_quantities_enabled();
37
        $use_taxes          = wpinv_use_taxes();
38
        if ( !$use_taxes && (float)$invoice->get_tax() > 0 ) {
39
            $use_taxes = true;
40
        }
41
        $item_types         = apply_filters( 'wpinv_item_types_for_quick_add_item', wpinv_get_item_types(), $post );
42
        $is_recurring       = $invoice->is_recurring();
43
        $post_type_object   = get_post_type_object($invoice->post_type);
44
        $type_title         = $post_type_object->labels->singular_name;
45
46
        $cols = 5;
47
        if ( $item_quantities ) {
48
            $cols++;
49
        }
50
        if ( $use_taxes ) {
51
            $cols++;
52
        }
53
        $class = '';
54
        if ( $invoice->is_paid() ) {
55
            $class .= ' wpinv-paid';
56
        }
57
        if ( $invoice->is_refunded() ) {
58
            $class .= ' wpinv-refunded';
59
        }
60
        if ( $is_recurring ) {
61
            $class .= ' wpi-recurring';
62
        }
63
        ?>
64
        <div class="wpinv-items-wrap<?php echo $class; ?>" id="wpinv_items_wrap" data-status="<?php echo $invoice->status; ?>">
65
            <table id="wpinv_items" class="wpinv-items" cellspacing="0" cellpadding="0">
66
                <thead>
67
                    <tr>
68
                        <th class="id"><?php _e( 'ID', 'invoicing' );?></th>
69
                        <th class="title"><?php _e( 'Item', 'invoicing' );?></th>
70
                        <th class="price"><?php _e( 'Price', 'invoicing' );?></th>
71
                        <?php if ( $item_quantities ) { ?>
72
                        <th class="qty"><?php _e( 'Qty', 'invoicing' );?></th>
73
                        <?php } ?>
74
                        <th class="total"><?php _e( 'Total', 'invoicing' );?></th>
75
                        <?php if ( $use_taxes ) { ?>
76
                        <th class="tax"><?php _e( 'Tax (%)', 'invoicing' );?></th>
77
                        <?php } ?>
78
                        <th class="action"></th>
79
                    </tr>
80
                </thead>
81
                <tbody class="wpinv-line-items">
82
                    <?php echo wpinv_admin_get_line_items( $invoice ); ?>
83
                </tbody>
84
                <tfoot class="wpinv-totals">
85
                    <tr>
86
                        <td colspan="<?php echo $cols; ?>" style="padding:0;border:0">
87
                            <div id="wpinv-quick-add">
88
                                <table cellspacing="0" cellpadding="0">
89
                                    <tr>
90
                                        <td class="id">
91
                                        </td>
92
                                        <td class="title">
93
                                            <input type="text" class="regular-text" placeholder="<?php _e( 'Item Name', 'invoicing' ); ?>" value="" name="_wpinv_quick[name]">
94
                                            <?php if ( $wpinv_euvat->allow_vat_rules() ) { ?>
95
                                            <div class="wp-clearfix">
96
                                                <label class="wpi-vat-rule">
97
                                                    <span class="title"><?php _e( 'VAT rule type', 'invoicing' );?></span>
98
                                                    <span class="input-text-wrap">
99
                                                        <?php echo wpinv_html_select( array(
100
                                                            'options'          => $wpinv_euvat->get_rules(),
101
                                                            'name'             => '_wpinv_quick[vat_rule]',
102
                                                            'id'               => '_wpinv_quick_vat_rule',
103
                                                            'show_option_all'  => false,
104
                                                            'show_option_none' => false,
105
                                                            'class'            => 'gdmbx2-text-medium wpinv-quick-vat-rule wpi_select2',
106
                                                        ) ); ?>
107
                                                    </span>
108
                                                </label>
109
                                            </div>
110
                                            <?php } if ( $wpinv_euvat->allow_vat_classes() ) { ?>
111
                                            <div class="wp-clearfix">
112
                                                <label class="wpi-vat-class">
113
                                                    <span class="title"><?php _e( 'VAT class', 'invoicing' );?></span>
114
                                                    <span class="input-text-wrap">
115
                                                        <?php echo wpinv_html_select( array(
116
                                                            'options'          => $wpinv_euvat->get_all_classes(),
117
                                                            'name'             => '_wpinv_quick[vat_class]',
118
                                                            'id'               => '_wpinv_quick_vat_class',
119
                                                            'show_option_all'  => false,
120
                                                            'show_option_none' => false,
121
                                                            'class'            => 'gdmbx2-text-medium wpinv-quick-vat-class wpi_select2',
122
                                                        ) ); ?>
123
                                                    </span>
124
                                                </label>
125
                                            </div>
126
                                            <?php } ?>
127
                                            <div class="wp-clearfix">
128
                                                <label class="wpi-item-type">
129
                                                    <span class="title"><?php _e( 'Item type', 'invoicing' );?></span>
130
                                                    <span class="input-text-wrap">
131
                                                        <?php echo wpinv_html_select( array(
132
                                                            'options'          => $item_types,
133
                                                            'name'             => '_wpinv_quick[type]',
134
                                                            'id'               => '_wpinv_quick_type',
135
                                                            'selected'         => 'custom',
136
                                                            'show_option_all'  => false,
137
                                                            'show_option_none' => false,
138
                                                            'class'            => 'gdmbx2-text-medium wpinv-quick-type wpi_select2',
139
                                                        ) ); ?>
140
                                                    </span>
141
                                                </label>
142
                                            </div>
143
144
                                            <div class="wp-clearfix">
145
                                                <?php 
146
                                                    echo wpinv_html_textarea( array(
147
                                                        'name'  => '_wpinv_quick[excerpt]',
148
                                                        'id'    => '_wpinv_quick_excerpt',
149
                                                        'value' => '',
150
                                                        'class' => 'large-text',
151
                                                        'label' => __( 'Item description', 'invoicing' ),
152
                                                    ) ); 
153
                                                ?>
154
                                            </div>
155
156
                                            <div class="wp-clearfix">
157
                                                <label class="wpi-item-actions">
158
                                                    <span class="input-text-wrap">
159
                                                        <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">
160
                                                    </span>
161
                                                </label>
162
                                            </div>
163
                                        </td>
164
                                        <td class="price"><input type="text" placeholder="0.00" class="wpi-field-price wpi-price" name="_wpinv_quick[price]" /></td>
165
                                        <?php if ( $item_quantities ) { ?>
166
                                        <td class="qty"><input type="number" class="small-text" step="1" min="1" value="1" name="_wpinv_quick[qty]" /></td>
167
                                        <?php } ?>
168
                                        <td class="total"></td>
169
                                        <?php if ( $use_taxes ) { ?>
170
                                        <td class="tax"></td>
171
                                        <?php } ?>
172
                                        <td class="action"></td>
173
                                    </tr>
174
                                </table>
175
                            </div>
176
                        </td>
177
                    </tr>
178
                    <tr class="clear">
179
                        <td colspan="<?php echo $cols; ?>"></td>
180
                    </tr>
181
                    <tr class="totals">
182
                        <td colspan="<?php echo ( $cols - 4 ); ?>"></td>
183
                        <td colspan="4">
184
                            <table cellspacing="0" cellpadding="0">
185
                                <tr class="subtotal">
186
                                    <td class="name"><?php _e( 'Sub Total:', 'invoicing' );?></td>
187
                                    <td class="total"><?php echo $subtotal;?></td>
188
                                    <td class="action"></td>
189
                                </tr>
190
                                <tr class="discount">
191
                                    <td class="name"><?php wpinv_get_discount_label( wpinv_discount_code( $invoice->ID ) ); ?>:</td>
192
                                    <td class="total"><?php echo wpinv_discount( $invoice->ID, true, true ); ?></td>
193
                                    <td class="action"></td>
194
                                </tr>
195
                                <?php if ( $use_taxes ) { ?>
196
                                <tr class="tax">
197
                                    <td class="name"><?php _e( 'Tax:', 'invoicing' );?></td>
198
                                    <td class="total"><?php echo $tax;?></td>
199
                                    <td class="action"></td>
200
                                </tr>
201
                                <?php } ?>
202
                                <tr class="total">
203
                                    <td class="name"><?php echo apply_filters( 'wpinv_invoice_items_total_label', __( 'Invoice Total:', 'invoicing' ), $invoice );?></td>
204
                                    <td class="total"><?php echo $total;?></td>
205
                                    <td class="action"></td>
206
                                </tr>
207
                            </table>
208
                        </td>
209
                    </tr>
210
                </tfoot>
211
            </table>
212
            <div class="wpinv-actions">
213
                <?php ob_start(); ?>
214
                <?php
215
                    if ( !$invoice->is_paid() && !$invoice->is_refunded() ) {
216
                        if ( !$invoice->is_recurring() ) {
217
                            echo wpinv_item_dropdown( array(
218
                                'name'             => 'wpinv_invoice_item',
219
                                'id'               => 'wpinv_invoice_item',
220
                                'show_recurring'   => true,
221
                                'class'            => 'wpi_select2',
222
                            ) );
223
                    ?>
224
                <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">
225
                    <?php } ?>
226
                <?php do_action( 'wpinv_invoice_items_actions', $invoice ); ?>
227
                <?php $item_actions = ob_get_clean(); echo apply_filters( 'wpinv_invoice_items_actions_content', $item_actions, $invoice, $post ); ?>
228
            </div>
229
        </div>
230
        <?php
231
    }
232
233
    public static function vat_rules( $post ) {
234
        global $wpinv_euvat;
235
236
        $rule_type = $wpinv_euvat->get_item_rule( $post->ID );
237
        ?>
238
        <p><label for="wpinv_vat_rules"><strong><?php _e( 'Select how VAT rules will be applied:', 'invoicing' );?></strong></label>&nbsp;&nbsp;&nbsp;
239
        <?php echo wpinv_html_select( array(
240
                    'options'          => $wpinv_euvat->get_rules(),
241
                    'name'             => 'wpinv_vat_rules',
242
                    'id'               => 'wpinv_vat_rules',
243
                    'selected'         => $rule_type,
244
                    'show_option_all'  => false,
245
                    'show_option_none' => false,
246
                    'class'            => 'gdmbx2-text-medium wpinv-vat-rules wpi_select2',
247
                ) ); ?>
248
        </p>
249
        <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>
250
        <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>
251
        <p class="wpi-m0"><?php _e( 'Select "Tax-Free" if you do not want to charge VAT for this item.', 'invoicing' ); ?></p>
252
        <?php
253
    }
254
255
    public static function vat_classes( $post ) {
256
        global $wpinv_euvat;
257
        
258
        $vat_class = $wpinv_euvat->get_item_class( $post->ID );
259
        ?>
260
        <p><?php echo wpinv_html_select( array(
261
                    'options'          => $wpinv_euvat->get_all_classes(),
262
                    'name'             => 'wpinv_vat_class',
263
                    'id'               => 'wpinv_vat_class',
264
                    'selected'         => $vat_class,
265
                    'show_option_all'  => false,
266
                    'show_option_none' => false,
267
                    'class'            => 'gdmbx2-text-medium wpinv-vat-class wpi_select2',
268
                ) ); ?>
269
        </p>
270
        <p class="wpi-m0"><?php _e( 'Select the VAT rate class to use for this invoice item.', 'invoicing' ); ?></p>
271
        <?php
272
    }
273
274
    public static function item_info( $post ) {
275
        $item_type = wpinv_get_item_type( $post->ID );
276
        do_action( 'wpinv_item_info_metabox_before', $post );
277
        ?>
278
        <p><label for="wpinv_item_type"><strong><?php _e( 'Type:', 'invoicing' );?></strong></label>&nbsp;&nbsp;&nbsp;
279
        <?php echo wpinv_html_select( array(
280
                    'options'          => wpinv_get_item_types(),
281
                    'name'             => 'wpinv_item_type',
282
                    'id'               => 'wpinv_item_type',
283
                    'selected'         => $item_type,
284
                    'show_option_all'  => false,
285
                    'show_option_none' => false,
286
                    'class'            => 'gdmbx2-text-medium wpinv-item-type',
287
                ) ); ?>
288
        </p>
289
        <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>
290
        <?php
291
        do_action( 'wpinv_item_info_metabox_after', $post );
292
    }
293
294
    public static function meta_values( $post ) {
295
        $meta_keys = apply_filters( 'wpinv_show_meta_values_for_keys', array(
296
            'type',
297
            'custom_id'
298
        ) );
299
300
        if ( empty( $meta_keys ) ) {
301
            return;
302
        }
303
304
        do_action( 'wpinv_meta_values_metabox_before', $post );
305
306
        foreach ( $meta_keys as $meta_key ) {
307
            ?>
308
            <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

308
            <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...
309
            <?php 
310
        }
311
312
        do_action( 'wpinv_meta_values_metabox_after', $post );
313
    }
314
315
    /**
316
     * Display the items buy now shortcode.
317
     */
318
    public static function shortcode( $post ) {
319
320
        if ( ! is_numeric( $post ) ) {
321
            $post = $post->ID;
322
        }
323
324
        echo "<input type='text' style='min-width: 100%; font-size: small;' value='[getpaid item=$post]' disabled>";
325
    }
326
327
    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

327
    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...
328
        $invoice        = new WPInv_Invoice( $post_id );
329
330
        // Billing
331
        $first_name     = sanitize_text_field( $data['wpinv_first_name'] );
332
        $last_name      = sanitize_text_field( $data['wpinv_last_name'] );
333
        $company        = sanitize_text_field( $data['wpinv_company'] );
334
        $vat_number     = sanitize_text_field( $data['wpinv_vat_number'] );
335
        $phone          = sanitize_text_field( $data['wpinv_phone'] );
336
        $address        = sanitize_text_field( $data['wpinv_address'] );
337
        $city           = sanitize_text_field( $data['wpinv_city'] );
338
        $zip            = sanitize_text_field( $data['wpinv_zip'] );
339
        $country        = sanitize_text_field( $data['wpinv_country'] );
340
        $state          = sanitize_text_field( $data['wpinv_state'] );
341
342
        // Details
343
        $status         = sanitize_text_field( $data['wpinv_status'] );
344
        $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...
345
        $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...
346
        $due_date       = isset( $data['wpinv_due_date'] ) ? sanitize_text_field( $data['wpinv_due_date'] ) : '';
347
        $date_created   = isset( $data['date_created'] ) ? sanitize_text_field( $data['date_created'] ) : '';
348
        //$discounts      = sanitize_text_field( $data['wpinv_discounts'] );
349
        //$discount       = sanitize_text_field( $data['wpinv_discount'] );
350
351
        $disable_taxes = 0;
352
353
        if ( ! empty( $data['disable_taxes'] ) ) {
354
            $disable_taxes = 1;
355
        }
356
357
        $ip             = $invoice->get_ip() ? $invoice->get_ip() : wpinv_get_ip();
358
359
        $invoice->set( 'due_date', $due_date );
360
        $invoice->set( 'first_name', $first_name );
361
        $invoice->set( 'last_name', $last_name );
362
        $invoice->set( 'company', $company );
363
        $invoice->set( 'vat_number', $vat_number );
364
        $invoice->set( 'phone', $phone );
365
        $invoice->set( 'address', $address );
366
        $invoice->set( 'city', $city );
367
        $invoice->set( 'zip', $zip );
368
        $invoice->set( 'country', $country );
369
        $invoice->set( 'state', $state );
370
        $invoice->set( 'status', $status );
371
        $invoice->set( 'set', $status );
372
        //$invoice->set( 'number', $number );
373
        //$invoice->set( 'discounts', $discounts );
374
        //$invoice->set( 'discount', $discount );
375
        $invoice->set( 'disable_taxes', $disable_taxes );
376
        $invoice->set( 'ip', $ip );
377
        $invoice->old_status = $_POST['original_post_status'];
378
        
379
        $currency = $invoice->get_currency();
380
        if ( ! empty( $data['wpinv_currency'] ) ) {
381
            $currency = sanitize_text_field( $data['wpinv_currency'] );
382
        }
383
384
        if ( empty( $currency ) ) {
385
            $currency = wpinv_get_currency();
386
        }
387
388
        if ( ! $invoice->is_paid() ) {
389
            $invoice->currency = $currency;
390
        }
391
392
        if ( !empty( $data['wpinv_gateway'] ) ) {
393
            $invoice->set( 'gateway', sanitize_text_field( $data['wpinv_gateway'] ) );
394
        }
395
        $saved = $invoice->save();
396
397
        $emails = '';
398
        if ( ! empty( $data['wpinv_cc'] ) ) {
399
            $emails = wpinv_clean( $data['wpinv_cc'] );
400
        }
401
        update_post_meta( $invoice->ID, 'wpinv_email_cc', $emails );
402
403
        if ( ! empty( $date_created ) && strtotime( $date_created, current_time( 'timestamp' ) ) ) {
404
405
            $time = strtotime( $date_created, current_time( 'timestamp' ) );
406
            $date = date( 'Y-m-d H:i:s', $time );
407
            $date_gmt = get_gmt_from_date( $date );
408
409
            wp_update_post(
410
                array(
411
                    'ID'            => $invoice->ID,
412
                    'post_date'     => $date,
413
                    'post_date_gmt' => $date_gmt,
414
                    'edit_date'     => true,
415
                )
416
            );
417
418
            $invoice->date = $date;
419
        }
420
421
        // Check for payment notes
422
        if ( !empty( $data['invoice_note'] ) ) {
423
            $note               = wp_kses( $data['invoice_note'], array() );
424
            $note_type          = sanitize_text_field( $data['invoice_note_type'] );
425
            $is_customer_note   = $note_type == 'customer' ? 1 : 0;
426
427
            wpinv_insert_payment_note( $invoice->ID, $note, $is_customer_note );
428
        }
429
430
        // Update user address if empty.
431
        if ( $saved && !empty( $invoice ) ) {
432
            if ( $user_id = $invoice->get_user_id() ) {
433
                $user_address = wpinv_get_user_address( $user_id, false );
434
435
                if (empty($user_address['first_name'])) {
436
                    update_user_meta( $user_id, '_wpinv_first_name', $first_name );
437
                    update_user_meta( $user_id, '_wpinv_last_name', $last_name );
438
                } else if (empty($user_address['last_name']) && $user_address['first_name'] == $first_name) {
439
                    update_user_meta( $user_id, '_wpinv_last_name', $last_name );
440
                }
441
442
                if (empty($user_address['address']) || empty($user_address['city']) || empty($user_address['state']) || empty($user_address['country'])) {
443
                    update_user_meta( $user_id, '_wpinv_address', $address );
444
                    update_user_meta( $user_id, '_wpinv_city', $city );
445
                    update_user_meta( $user_id, '_wpinv_state', $state );
446
                    update_user_meta( $user_id, '_wpinv_country', $country );
447
                    update_user_meta( $user_id, '_wpinv_zip', $zip );
448
                    update_user_meta( $user_id, '_wpinv_phone', $phone );
449
                }
450
            }
451
452
            do_action( 'wpinv_invoice_metabox_saved', $invoice );
453
        }
454
455
        return $saved;
456
    }
457
}
458