Completed
Push — master ( 43d636...8512bc )
by Brian
20s queued 17s
created

WPInv_Meta_Box_Details::resend_invoice()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 7
eloc 21
c 1
b 0
f 1
nc 4
nop 1
dl 0
loc 31
rs 8.6506
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_Details {
8
    public static function output( $post ) {
9
        global $wpinv_euvat;
10
11
        $currency_symbol    = wpinv_currency_symbol();
0 ignored issues
show
Unused Code introduced by
The assignment to $currency_symbol is dead and can be removed.
Loading history...
12
        $statuses           = wpinv_get_invoice_statuses( true );
13
14
        $post_id            = !empty( $post->ID ) ? $post->ID : 0;
15
        $invoice            = new WPInv_Invoice( $post_id );
16
        $status             = $invoice->get_status( false ); // Current status    
17
        $discount           = $invoice->get_discount();
18
        $discount_code      = $discount > 0 ? $invoice->get_discount_code() : '';
19
        $invoice_number     = $invoice->get_number();
20
        $taxable            = $invoice->is_taxable();
21
22
        $date_created       = $invoice->get_created_date();
23
        $datetime_created   = strtotime( $date_created );
24
        $date_created       = $date_created != '' && $date_created != '0000-00-00 00:00:00' ? date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $datetime_created ) : '';
0 ignored issues
show
Bug introduced by
Are you sure get_option('time_format') of type false|mixed can be used in concatenation? ( Ignorable by Annotation )

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

24
        $date_created       = $date_created != '' && $date_created != '0000-00-00 00:00:00' ? date_i18n( get_option( 'date_format' ) . ' ' . /** @scrutinizer ignore-type */ get_option( 'time_format' ), $datetime_created ) : '';
Loading history...
Bug introduced by
Are you sure get_option('date_format') of type false|mixed can be used in concatenation? ( Ignorable by Annotation )

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

24
        $date_created       = $date_created != '' && $date_created != '0000-00-00 00:00:00' ? date_i18n( /** @scrutinizer ignore-type */ get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $datetime_created ) : '';
Loading history...
Unused Code introduced by
The assignment to $date_created is dead and can be removed.
Loading history...
25
        $date_completed     = $invoice->get_completed_date();
26
        $date_completed     = $date_completed != '' && $date_completed != '0000-00-00 00:00:00' ? date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), strtotime( $date_completed ) ) : 'n/a';
27
        $title['status'] = __( 'Invoice Status:', 'invoicing' );
0 ignored issues
show
Comprehensibility Best Practice introduced by
$title was never initialized. Although not strictly required by PHP, it is generally a good practice to add $title = array(); before regardless.
Loading history...
28
        $title['number'] = __( 'Invoice Number:', 'invoicing' );
29
        $mail_notice = esc_attr__( 'After saving invoice, this will send a copy of the invoice to the user&#8217;s email address.', 'invoicing' );
30
31
        $title = apply_filters('wpinv_details_metabox_titles', $title, $invoice);
32
        $statuses = apply_filters('wpinv_invoice_statuses', $statuses, $invoice);
33
        $mail_notice = apply_filters('wpinv_metabox_mail_notice', $mail_notice, $invoice);
34
        $post_obj = get_post_type_object($invoice->post_type);
35
        ?>
36
<div class="gdmbx2-wrap form-table">
37
    <div class="gdmbx2-metabox gdmbx-field-list" id="gdmbx2-metabox-wpinv_details">
38
39
        <div class="gdmbx-row gdmbx-type-text gdmbx2-id-wpinv-date-created table-layout">
40
            <div class="gdmbx-th"><label for="wpinv_date_created"><?php _e( 'Date Created:', 'invoicing' );?></label></div>
41
            <div class="gdmbx-td">
42
                <input type="datetime-local" value="<?php echo esc_attr( date( 'Y-m-d\TH:i:s', $datetime_created ) );?>" id="wpinv_date_created" name="date_created" class="regular-text">
43
            </div>
44
        </div>
45
46
        <?php if ( $invoice->post_type == 'wpi_invoice' && wpinv_get_option( 'overdue_active' ) && ( $invoice->needs_payment() || $invoice->has_status( array( 'auto-draft', 'draft' ) ) ) ) { ?>
47
        <div class="gdmbx-row gdmbx-type-select gdmbx2-id-wpinv-date-overdue">
48
            <div class="gdmbx-th"><label for="wpinv_due_date"><?php _e( 'Due Date:', 'invoicing' );?></label></div>
49
            <div class="gdmbx-td">
50
                <input type="text" placeholder="<?php esc_attr_e( 'Y-m-d', 'invoicing' );?>" value="<?php echo esc_attr( $invoice->get_due_date() );?>" id="wpinv_due_date" name="wpinv_due_date" class="regular-text wpiDatepicker" data-minDate="<?php echo esc_attr( date_i18n( 'Y-m-d', $datetime_created ) );?>" data-dateFormat="yy-mm-dd">
51
                <p class="wpi-meta-row wpi-meta-desc"><?php _e( 'Leave blank to disable sending auto reminder for this invoice.', 'invoicing' );?></p>
52
            </div>
53
        </div>
54
        <?php } ?>
55
        <?php do_action( 'wpinv_meta_box_details_after_due_date', $post_id ); ?>
56
        <?php if ( $date_completed && $date_completed != 'n/a' ) { ?>
57
        <div class="gdmbx-row gdmbx-type-select gdmbx2-id-wpinv-date-completed">
58
            <div class="gdmbx-th"><label><?php _e( 'Payment Date:', 'invoicing' );?></label></div>
59
            <div class="gdmbx-td"><?php echo $date_completed;?></div>
60
        </div>
61
        <?php } ?>
62
        <?php $is_viewed = wpinv_is_invoice_viewed( $post_id ); ?>
63
        <div class="gdmbx-row gdmbx-type-select gdmbx2-id-wpinv-customer-viewed">
64
            <div class="gdmbx-th"><label><?php _e( 'Viewed by Customer:', 'invoicing' );?></label></div>
65
            <div class="gdmbx-td"><?php ( 1 == $is_viewed ) ? _e( 'Yes', 'invoicing' ) : _e( 'No', 'invoicing' ); ?></div>
66
        </div>
67
        <div class="gdmbx-row gdmbx-type-select gdmbx2-id-wpinv-status">
68
            <div class="gdmbx-th"><label for="wpinv_status"><?php echo $title['status']; ?></label></div>
69
            <div class="gdmbx-td">
70
                <select required="required" id="wpinv_status" name="wpinv_status" class="gdmbx2_select wpi_select2">
71
                    <?php foreach ( $statuses as $value => $label ) { ?>
72
                    <option value="<?php echo $value;?>" <?php selected( $status, $value );?>><?php echo $label;?></option>
73
                    <?php } ?>
74
                </select>
75
            </div>
76
        </div>
77
78
        <div class="gdmbx-row gdmbx-type-text gdmbx2-id-wpinv-number table-layout">
79
            <div class="gdmbx-th"><label for="wpinv_number"><?php echo $title['number']; ?></label></div>
80
            <div class="gdmbx-td">
81
                <input type="text" value="<?php echo esc_attr( $invoice_number );?>" id="wpinv_number" name="wpinv_number" class="regular-text" readonly>
82
            </div>
83
        </div>
84
        <?php do_action( 'wpinv_meta_box_details_inner', $post_id );
85
        $disable_discount = apply_filters('wpinv_disable_apply_discount', false, $invoice, $post_id);
86
        ?>
87
88
        <?php if ( $wpinv_euvat->allow_vat_rules() && ! ( $invoice->is_paid() || $invoice->is_refunded() ) ) { ?>
89
        <div class="gdmbx-row gdmbx-type-select gdmbx2-id-wpinv-taxable">
90
            <div class="gdmbx-th">
91
                <label for="wpinv_taxable">
92
                    <input type="checkbox" name="disable_taxes" value="1" <?php checked( $taxable, false ); ?> id="wpinv_taxable">
93
                    <?php _e( 'Disable taxes', 'invoicing' ); ?>
94
                </label>
95
            </div>
96
        </div>
97
        <?php } ?>
98
99
        <?php if ( !( $is_paid = ( $invoice->is_paid() || $invoice->is_refunded() ) ) && !$disable_discount || $discount_code ) { ?>
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: (! $is_paid = $invoice->...ount) || $discount_code, Probably Intended Meaning: ! $is_paid = $invoice->i...ount || $discount_code)
Loading history...
100
        <div class="gdmbx-row gdmbx-type-text gdmbx2-id-wpinv-discount-code table-layout">
101
            <div class="gdmbx-th"><label for="wpinv_discount_code"><?php _e( 'Discount Code:', 'invoicing' );?></label></div>
102
            <div class="gdmbx-td">
103
                <input type="text" value="<?php echo esc_attr( $discount_code ); ?>" id="wpinv_discount" class="medium-text" <?php echo ( $discount_code ? 'readonly' : '' ); ?> /><?php if ( !$is_paid && !$disable_discount ) { ?><input value="<?php echo esc_attr_e( 'Apply', 'invoicing' ); ?>" class="button button-small button-primary <?php echo ( $discount_code ? 'wpi-hide' : 'wpi-inlineb' ); ?>" id="wpinv-apply-code" type="button" /><input value="<?php echo esc_attr_e( 'Remove', 'invoicing' ); ?>" class="button button-small button-primary <?php echo ( $discount_code ? 'wpi-inlineb' : 'wpi-hide' ); ?>" id="wpinv-remove-code" type="button" /><?php } ?>
0 ignored issues
show
Bug introduced by
Are you sure the usage of esc_attr_e('Remove', 'invoicing') is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug introduced by
Are you sure the usage of esc_attr_e('Apply', 'invoicing') is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
104
            </div>
105
        </div>
106
        <?php } ?>
107
    </div>
108
</div>
109
<div class="gdmbx-row gdmbx-type-text gdmbx-wpinv-save-send table-layout">
110
    <p class="wpi-meta-row wpi-save-send"><label for="wpi_save_send"><?php echo sprintf(__( 'Send %s:', 'invoicing' ),$post_obj->labels->singular_name) ; ?></label>
111
        <select id="wpi_save_send" name="wpi_save_send" class="wpi_select2">
112
            <option value="1"><?php _e( 'Yes', 'invoicing' ); ?></option>
113
            <option value="" selected="selected"><?php _e( 'No', 'invoicing' ); ?></option>
114
        </select>
115
    </p>
116
    <p class="wpi-meta-row wpi-send-info"><?php echo $mail_notice; ?></p>
117
</div>
118
<?php wp_nonce_field( 'wpinv_details', 'wpinv_details_nonce' ) ;?>
119
        <?php
120
    }
121
    
122
    public static function resend_invoice( $post ) {
123
        global $wpi_mb_invoice;
124
        
125
        if ( empty( $wpi_mb_invoice ) ) {
126
            return;
127
        }
128
        
129
        $text = array(
130
            'message'       => esc_attr__( 'This will send a copy of the invoice to the customer&#8217;s email address.', 'invoicing' ),
131
            'button_text'   =>  __( 'Resend Invoice', 'invoicing' ),
132
        );
133
            
134
        $text = apply_filters('wpinv_resend_invoice_metabox_text', $text);
135
        do_action( 'wpinv_metabox_resend_invoice_before', $wpi_mb_invoice );
136
        
137
        if ( $email = $wpi_mb_invoice->get_email() ) {
0 ignored issues
show
Unused Code introduced by
The assignment to $email is dead and can be removed.
Loading history...
138
            $email_actions = array();
139
            $email_actions['email_url']      = remove_query_arg( 'wpinv-message', add_query_arg( array( 'wpi_action' => 'send_invoice', 'invoice_id' => $post->ID ) ) );
140
            $email_actions['reminder_url']   = add_query_arg( array( 'wpi_action' => 'send_reminder', 'invoice_id' => $post->ID ) );
141
            
142
            $email_actions = apply_filters('wpinv_resend_invoice_email_actions', $email_actions );
143
        ?>
144
        <p class="wpi-meta-row wpi-resend-info"><?php echo $text['message']; ?></p>
145
        <p class="wpi-meta-row wpi-resend-email"><a href="<?php echo esc_url( $email_actions['email_url'] ); ?>" class="button button-secondary"><?php echo $text['button_text']; ?></a></p>
146
        <?php if ( wpinv_get_option( 'overdue_active' ) && "wpi_invoice" === $wpi_mb_invoice->post_type && $wpi_mb_invoice->needs_payment() && ( $due_date = $wpi_mb_invoice->get_due_date() ) ) { ?>
0 ignored issues
show
Unused Code introduced by
The assignment to $due_date is dead and can be removed.
Loading history...
147
        <p class="wpi-meta-row wpi-send-reminder"><a title="<?php esc_attr_e( 'Send overdue reminder notification to customer', 'invoicing' ); ?>" href="<?php echo esc_url( $email_actions['reminder_url'] ); ?>" class="button button-secondary"><?php esc_attr_e( 'Send Reminder', 'invoicing' ); ?></a></p>
148
        <?php } ?>
149
        <?php
150
        }
151
        
152
        do_action( 'wpinv_metabox_resend_invoice_after', $wpi_mb_invoice );
153
    }
154
    
155
    public static function subscriptions( $post ) {
156
        $invoice = wpinv_get_invoice( $post->ID );
157
158
        if ( ! empty( $invoice ) && $invoice->is_recurring() && $invoice->is_parent() ) {
159
            $subscription = wpinv_get_subscription( $invoice );
160
161
            if ( empty( $subscription ) ) {
162
                ?>
163
                <p class="wpi-meta-row"><?php echo wp_sprintf( __( 'New Subscription will be created when customer will checkout and pay the invoice. Go to: %sSubscriptions%s', 'invoicing' ), '<a href="' . admin_url( 'admin.php?page=wpinv-subscriptions' ).'">', '</a>' ); ?></p>
164
                <?php
165
                return;
166
            }
167
            $frequency = WPInv_Subscriptions::wpinv_get_pretty_subscription_frequency( $subscription->period, $subscription->frequency );
168
            $billing = wpinv_price(wpinv_format_amount( $subscription->recurring_amount ), wpinv_get_invoice_currency_code( $subscription->parent_payment_id ) ) . ' / ' . $frequency;
169
            $initial = wpinv_price(wpinv_format_amount( $subscription->initial_amount ), wpinv_get_invoice_currency_code( $subscription->parent_payment_id ) );
170
            $payments = $subscription->get_child_payments();
171
            ?>
172
            <p class="wpi-meta-row wpi-sub-label <?php echo 'status-' . $subscription->status; ?>"><?php _e('Recurring Payment', 'invoicing'); ?></p>
173
            <?php if ( ! empty( $subscription ) && ! empty( $subscription->id ) ) { ?>
174
                <p class="wpi-meta-row wpi-sub-id">
175
                    <label><?php _e( 'Subscription ID:', 'invoicing' ); ?> </label><a href="<?php echo esc_url( admin_url( 'admin.php?page=wpinv-subscriptions&id=' . $subscription->id ) ); ?>" title="<?php esc_attr_e( 'View or edit subscription', 'invoicing' ); ?>" target="_blank"><?php echo $subscription->id; ?></a></p>
176
            <?php } ?>
177
            <p class="wpi-meta-row wpi-bill-cycle">
178
                <label><?php _e( 'Billing Cycle:', 'invoicing'); ?> </label><?php printf( _x( '%s then %s', 'Initial subscription amount then billing cycle and amount', 'invoicing' ), $initial, $billing ); ?>
179
            </p>
180
            <p class="wpi-meta-row wpi-billed-times">
181
                <label><?php _e( 'Times Billed:', 'invoicing' ); ?> </label><?php echo $subscription->get_times_billed() . ' / ' . ( ( $subscription->bill_times == 0 ) ? 'Until Cancelled' : $subscription->bill_times ); ?>
182
            </p>
183
            <p class="wpi-meta-row wpi-start-date">
184
                <label><?php _e( 'Start Date:', 'invoicing' ); ?> </label><?php echo date_i18n( get_option( 'date_format' ), strtotime( $subscription->created, current_time( 'timestamp' ) ) ); ?>
0 ignored issues
show
Bug introduced by
It seems like get_option('date_format') can also be of type false; however, parameter $format of date_i18n() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

184
                <label><?php _e( 'Start Date:', 'invoicing' ); ?> </label><?php echo date_i18n( /** @scrutinizer ignore-type */ get_option( 'date_format' ), strtotime( $subscription->created, current_time( 'timestamp' ) ) ); ?>
Loading history...
185
            </p>
186
            <p class="wpi-meta-row wpi-end-date">
187
                <label><?php echo ( 'trialling' == $subscription->status ? __( 'Trialling Until:', 'invoicing' ) : __( 'Expiration Date:', 'invoicing' ) ); ?> </label><?php echo date_i18n( get_option( 'date_format' ), strtotime( $subscription->expiration, current_time( 'timestamp' ) ) ); ?>
188
            </p>
189
            <?php if ( $subscription->status ) { ?>
190
                <p class="wpi-meta-row wpi-sub-status">
191
                    <label><?php _e( 'Subscription Status:', 'invoicing'); ?> </label><?php echo $subscription->get_status_label(); ?>
192
                </p>
193
            <?php } ?>
194
            <?php if ( !empty( $payments ) ) { ?>
195
                <p><strong><?php _e( 'Renewal Payments:', 'invoicing' ); ?></strong></p>
196
                <ul id="wpi-sub-payments">
197
                <?php foreach ( $payments as $payment ) {
198
                    $invoice_id = $payment->ID;
199
                    ?>
200
                    <li>
201
                        <a href="<?php echo esc_url( get_edit_post_link( $invoice_id ) ); ?>"><?php echo wpinv_get_invoice_number( $invoice_id ); ?></a>&nbsp;&ndash;&nbsp;
202
                        <span><?php echo wpinv_get_invoice_date( $invoice_id ); ?>&nbsp;&ndash;&nbsp;</span>
203
                        <span><?php echo wpinv_payment_total( $invoice_id, true ); ?></span>
204
                    </li>
205
                <?php } ?>
206
                </ul>
207
            <?php }
208
        }
209
    }
210
    
211
    public static function renewals( $post ) {
212
        $invoice = wpinv_get_invoice( $post->ID );
213
        
214
        if ( wpinv_is_subscription_payment( $invoice ) ) {
215
            $parent_url = get_edit_post_link( $invoice->parent_invoice );
216
            $parent_id  = wpinv_get_invoice_number( $invoice->parent_invoice );
217
            $subscription = wpinv_get_subscription( $invoice );
218
        ?>
219
        <?php if ( ! empty( $subscription ) ) { ?><p class="wpi-meta-row wpi-sub-id"><label><?php _e('Subscription ID:', 'invoicing'); ?> </label><a href="<?php echo esc_url( admin_url( 'admin.php?page=wpinv-subscriptions&id=' . $subscription->id ) ); ?>" title="<?php esc_attr_e( 'View or edit subscription', 'invoicing' ); ?>" target="_blank"><?php echo $subscription->id; ?></a></p><?php } ?>
220
        <p class="wpi-meta-row wpi-parent-id"><label><?php _e( 'Parent Invoice:', 'invoicing' );?> </label><a href="<?php echo esc_url( $parent_url ); ?>"><?php echo $parent_id; ?></a></p>
221
        <?php
222
        }
223
    }
224
    
225
    public static function payment_meta( $post ) {
226
        global $wpi_mb_invoice;
227
228
        $set_dateway = empty( $wpi_mb_invoice->gateway ) ? true : false;
229
        if ( !$set_dateway && !$wpi_mb_invoice->get_meta( '_wpinv_checkout', true ) && !$wpi_mb_invoice->is_paid() && !$wpi_mb_invoice->is_refunded() ) {
230
            $set_dateway = true;
231
        }
232
233
        ?>
234
        <p class="wpi-meta-row">
235
        <?php if ( $set_dateway ) { $gateways = wpinv_get_enabled_payment_gateways( true ); ?>
236
            <label for="wpinv_gateway"><?php _e( 'Gateway:', 'invoicing' ) ; ?></label>
237
            <select required="required" id="wpinv_gateway" class="wpi_select2" name="wpinv_gateway">
238
                <?php foreach ( $gateways as $name => $gateway ) {
239
                    if ( $wpi_mb_invoice->is_recurring() && !wpinv_gateway_support_subscription( $name ) ) {
240
                        continue;
241
                    }
242
                    ?>
243
                <option value="<?php echo $name;?>" <?php selected( $wpi_mb_invoice->gateway, $name );?>><?php echo !empty( $gateway['admin_label'] ) ? $gateway['admin_label'] : $gateway['checkout_label']; ?></option>
244
                <?php } ?>
245
            </select>
246
        <?php } else { 
247
            echo wp_sprintf( __( '<label>Gateway:</label> %s', 'invoicing' ), wpinv_get_gateway_admin_label( $wpi_mb_invoice->gateway ) );
248
        } ?>
249
        </p>
250
        <?php if ( $key = $wpi_mb_invoice->get_key() ) { ?>
251
        <p class="wpi-meta-row"><?php echo wp_sprintf( __( '<label>Key:</label> %s', 'invoicing' ), $key ); ?></p>
252
        <?php } ?>
253
        <?php if ( $wpi_mb_invoice->is_paid() || $wpi_mb_invoice->is_refunded() ) { ?>
254
        <p class="wpi-meta-row"><?php echo wp_sprintf( __( '<label>Transaction ID:</label> %s', 'invoicing' ), wpinv_payment_link_transaction_id( $wpi_mb_invoice ) ); ?></p>
255
        <?php } ?>
256
        <?php
257
    }
258
}
259