Passed
Pull Request — master (#377)
by Brian
04:53
created

GetPaid_Meta_Box_Invoice_Items   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 276
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 135
c 1
b 0
f 0
dl 0
loc 276
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C output() 0 269 8
1
<?php
2
3
/**
4
 * Invoice Items
5
 *
6
 * Display the invoice items meta box.
7
 *
8
 */
9
10
if ( ! defined( 'ABSPATH' ) ) {
11
	exit; // Exit if accessed directly
12
}
13
14
/**
15
 * GetPaid_Meta_Box_Invoice_Items Class.
16
 */
17
class GetPaid_Meta_Box_Invoice_Items {
18
19
    /**
20
	 * Output the metabox.
21
	 *
22
	 * @param WP_Post $post
23
	 */
24
    public static function output( $post ) {
25
26
        // Prepare the invoice.
27
        $invoice = new WPInv_Invoice( $post );
28
29
        // Invoice items.
30
        $items = $invoice->get_items();
31
32
        // New item url.
33
        $new_item = admin_url( 'post-new.php?post_type=wpi_item' );
34
35
        // Totals.
36
        $totals = array(
37
38
            'subtotal'  => array(
39
                'label' => __( 'Items Subtotal', 'invoicing' ),
40
                'value' => wpinv_price( wpinv_format_amount( $invoice->get_subtotal() ) ),
41
            ),
42
43
            'discount'  => array(
44
                'label' => __( 'Total Discount', 'invoicing' ),
45
                'value' => wpinv_price( wpinv_format_amount( $invoice->get_total_discount() ) ),
46
            ),
47
48
            'tax'       => array(
49
                'label' => __( 'Total Tax', 'invoicing' ),
50
                'value' => wpinv_price( wpinv_format_amount( $invoice->get_total_tax() ) ),
51
            ),
52
53
            'total'     => array(
54
                'label' => __( 'Invoice Total', 'invoicing' ),
55
                'value' => wpinv_price( wpinv_format_amount( $invoice->get_total() ) ),
56
            )
57
        );
58
59
        if ( ! wpinv_use_taxes() ) {
60
            unset( $totals['tax'] );
61
        }
62
        ?>
63
64
        <style>
65
            #poststuff .input-group-text,
66
            #poststuff .form-control {
67
                border-color: #7e8993;
68
            }
69
70
            #wpinv-details label {
71
                margin-bottom: 3px;
72
                font-weight: 600;
73
            }
74
        </style>
75
76
                <div class="bsui getpaid-invoice-items-inner <?php echo sanitize_html_class( $invoice->get_template( 'edit' ) ); ?> <?php echo empty( $items ) ? 'no-items' : 'has-items'; ?> <?php echo $invoice->is_paid() || $invoice->is_refunded() ? 'not-editable' : 'editable'; ?>" style="margin-top: 1.5rem">
77
78
                    <?php if ( ! $invoice->is_paid() && ! $invoice->is_refunded() ) : ?>
79
                        <?php do_action( 'wpinv_meta_box_before_invoice_template_row', $invoice->get_id() ); ?>
80
81
                        <div class="row">
82
                            <div class="col-12 col-sm-6">
83
                                <?php
84
                                    echo aui()->select(
85
                                        array(
86
                                            'id'          => 'wpinv_template',
87
                                            'name'        => 'wpinv_template',
88
                                            'label'       => __( 'Template', 'invoicing' ),
89
                                            'label_type'  => 'vertical',
90
                                            'placeholder' => __( 'Choose a template', 'invoicing' ),
91
                                            'class'       => 'form-control-sm',
92
                                            'value'       => $invoice->get_template( 'edit' ),
93
                                            'options'     => array(
94
                                                'quantity' => __( 'Quantity', 'invoicing' ),
95
                                                'hours'    => __( 'Hours', 'invoicing' ),
96
                                                'amount'   => __( 'Amount Only', 'invoicing' ),
97
                                            ),
98
                                            'data-allow-clear' => 'false',
99
                                            'select2'          => true,
100
                                        )
101
                                    );
102
                                ?>
103
                            </div>
104
                            <div class="col-12 col-sm-6">
105
                                <?php
106
107
                                    // Set currency.
108
                                    echo aui()->select(
109
                                        array(
110
                                            'id'          => 'wpinv_currency',
111
                                            'name'        => 'wpinv_currency',
112
                                            'label'       => __( 'Currency', 'invoicing' ),
113
                                            'label_type'  => 'vertical',
114
                                            'placeholder' => __( 'Select Invoice Currency', 'invoicing' ),
115
                                            'class'       => 'form-control-sm',
116
                                            'value'       => $invoice->get_currency( 'edit' ),
117
                                            'required'    => false,
118
                                            'data-allow-clear' => 'false',
119
                                            'select2'          => true,
120
                                            'options'     => wpinv_get_currencies(),
121
                                        )
122
                                    );
123
124
                                ?>
125
                            </div>
126
                        </div>
127
128
                        <?php do_action( 'wpinv_meta_box_invoice_template_row', $invoice->get_id() ); ?>
129
                    <?php endif; ?>
130
131
                    <table cellpadding="0" cellspacing="0" class="getpaid_invoice_items">
132
                        <thead>
133
                            <tr>
134
                                <th class="getpaid-item" colspan="2"><?php _e( 'Item', 'invoicing' ) ?></th>
135
                                <th class="getpaid-quantity hide-if-amount text-right">
136
                                    <span class="getpaid-hide-if-hours"><?php _e( 'Quantity', 'invoicing' ) ?></span>
137
                                    <span class="getpaid-hide-if-quantity"><?php _e( 'Hours', 'invoicing' ) ?></span>
138
                                </th>
139
                                <th class="getpaid-price hide-if-amount text-right">
140
                                    <span class="getpaid-hide-if-hours"><?php _e( 'Price', 'invoicing' ) ?></span>
141
                                    <span class="getpaid-hide-if-quantity"><?php _e( 'Rate', 'invoicing' ) ?></span>
142
                                </th>
143
                                <th class="getpaid-item-subtotal text-right">
144
                                    <span class="getpaid-hide-if-hours getpaid-hide-if-quantity"><?php _e( 'Amount', 'invoicing' ) ?></span>
145
                                    <span class="hide-if-amount"><?php _e( 'Total', 'invoicing' ) ?></span>
146
                                </th>
147
                                <th class="getpaid-item-actions hide-if-not-editable" width="70px">&nbsp;</th>
148
                            </tr>
149
                        </thead>
150
		                <tbody class="getpaid_invoice_line_items">
151
                            <tr class="hide-if-has-items hide-if-not-editable">
152
                                <td colspan="2" class="pt-4 pb-4">
153
                                    <button type="button" class="button button-primary add-invoice-item" data-toggle="modal" data-target="#getpaid-add-items-to-invoice"><?php _e( 'Add Existing Items', 'invoicing' ) ?></button>
154
	                                <a href="<?php echo esc_url( $new_item ); ?>" target="_blank" class="button button-secondary"><?php _e( 'Create New Item', 'invoicing' ) ?></a>
155
                                </td>
156
                                <td class="hide-if-amount">&nbsp;</th>
157
                                <td class="hide-if-amount">&nbsp;</th>
158
                                <td>&nbsp;</th>
159
                                <td width="1%">&nbsp;</th>
160
                            </tr>
161
                            <tr class="getpaid-invoice-item-template d-none">
162
                                <td class="getpaid-item" colspan="2">
163
                                    <span class='item-name'></span>
164
                                    <small class="form-text text-muted item-description"></small>
165
                                </td>
166
                                <td class="getpaid-quantity hide-if-amount text-right item-quantity"></td>
167
                                <td class="getpaid-price hide-if-amount text-right item-price"></td>
168
                                <td class="getpaid-item-subtotal text-right">
169
                                    <span class="getpaid-hide-if-hours getpaid-hide-if-quantity item-price"></span>
170
                                    <span class="hide-if-amount item-total"></span>
171
                                </td>
172
                                <td class="getpaid-item-actions hide-if-not-editable" width="70px">
173
                                    <span class="dashicons dashicons-edit"></span>
174
                                    <span class="dashicons dashicons-trash"></span>
175
                                </td>
176
                            </tr>
177
178
                        </tbody>
179
                    </table>
180
181
                    <div class="getpaid-invoice-totals-row">
182
                        <div class="row">
183
                            <div class="col-12 col-sm-6 offset-sm-6">
184
                                <table class="getpaid-invoice-totals text-right w-100">
185
                                    <tbody>
186
                                        <?php foreach ( apply_filters( 'getpaid_invoice_subtotal_rows', $totals, $invoice ) as $key => $data ) : ?>
187
                                            <tr class="getpaid-totals-<?php echo sanitize_html_class( $key ); ?>">
188
                                                <td class="label"><?php echo sanitize_text_field( $data['label'] ) ?>:</td>
189
                                                <td width="1%"></td>
190
                                                <td class="value"><?php echo sanitize_text_field( $data['value'] ) ?></td>
191
                                            </tr>
192
                                        <?php endforeach; ?>
193
                                    </tbody>
194
                                </table>
195
                            </div>
196
                        </div>
197
                    </div>
198
199
                    <!-- Actions -->
200
                    <div class="getpaid-invoice-item-actions hide-if-no-items hide-if-not-editable">
201
                        <div class="row">
202
                            <div class="text-left col-12 col-sm-8">
203
                                <button type="button" class="button add-invoice-item" data-toggle="modal" data-target="#getpaid-add-items-to-invoice"><?php _e( 'Add Existing Item', 'invoicing' ) ?></button>
204
                                <a href="<?php echo esc_url( $new_item ); ?>" target="_blank" class="button button-secondary"><?php _e( 'Create New Item', 'invoicing' ) ?></a>
205
                                <?php do_action( 'getpaid-invoice-items-actions', $invoice ); ?>
206
                            </div>
207
                            <div class="text-right col-12 col-sm-4">
208
                                <button type="button" class="button button-primary recalculate-totals-button"><?php _e( 'Recalculate Totals', 'invoicing' ) ?></button>
209
                            </div>
210
                        </div>
211
                    </div>
212
213
                    <div class="getpaid-invoice-item-actions hide-if-editable">
214
                        <p class="description m-2 text-right text-muted"><?php _e( 'This invoice is no longer editable', 'invoicing' ); ?></p>
215
                    </div>
216
217
                    <!-- Add items to an invoice -->
218
                    <div class="modal fade" id="getpaid-add-items-to-invoice" tabindex="-1" role="dialog" aria-labelledby="getpaid-add-item-to-invoice-label" aria-hidden="true">
219
                        <div class="modal-dialog modal-dialog-centered" role="document">
220
                            <div class="modal-content">
221
                                <div class="modal-header">
222
                                    <h5 class="modal-title" id="getpaid-add-item-to-invoice-label"><?php _e( "Add Item(s)", 'invoicing' ); ?></h5>
223
                                    <button type="button" class="close" data-dismiss="modal" aria-label="<?php _e( "Close", 'invoicing' ); ?>">
224
                                        <span aria-hidden="true">&times;</span>
225
                                    </button>
226
                                </div>
227
                                <div class="modal-body">
228
                                    <table class="widefat">
229
                                        <thead>
230
                                            <tr>
231
                                                <th class="pl-0 text-left"><?php _e( 'Item', 'invoicing' ) ?></th>
232
                                                <th class="pr-0 text-right hide-if-amount">
233
                                                    <span class="getpaid-hide-if-hours"><?php _e( 'Quantity', 'invoicing' ) ?></span>
234
                                                    <span class="getpaid-hide-if-quantity"><?php _e( 'Hours', 'invoicing' ) ?></span>
235
                                                </th>
236
                                            </tr>
237
                                        </thead>
238
										<tbody>
239
								            <tr>
240
									            <td class="pl-0 text-left">
241
                                                    <select class="getpaid-item-search regular-text" data-placeholder="<?php esc_attr_e( 'Search for an item…', 'invoicing' ); ?>"></select>
242
                                                </td>
243
									            <td class="pr-0 text-right hide-if-amount">
244
                                                    <input type="number" class="w100" step="1" min="1" autocomplete="off" value="1" placeholder="1">
245
                                                </td>
246
                                            </tr>
247
							            </tbody>
248
						            </table>
249
                                </div>
250
                                <div class="modal-footer">
251
                                    <button type="button" class="btn btn-secondary getpaid-cancel" data-dismiss="modal"><?php _e( 'Cancel', 'invoicing' ); ?></button>
252
                                    <button type="button" class="btn btn-primary getpaid-add" data-dismiss="modal"><?php _e( 'Add', 'invoicing' ); ?></button>
253
                                </div>
254
                            </div>
255
                        </div>
256
                    </div>
257
258
                    <!-- Edit invoice item -->
259
                    <div class="modal fade" id="getpaid-edit-invoice-item" tabindex="-1" role="dialog" aria-labelledby="getpaid-edit-invoice-item-label" aria-hidden="true">
260
                        <div class="modal-dialog modal-dialog-centered" role="document">
261
                            <div class="modal-content">
262
                                <div class="modal-header">
263
                                    <h5 class="modal-title" id="getpaid-edit-invoice-item-label"><?php _e( "Edit Item", 'invoicing' ); ?></h5>
264
                                    <button type="button" class="close" data-dismiss="modal" aria-label="<?php _e( "Close", 'invoicing' ); ?>">
265
                                        <span aria-hidden="true">&times;</span>
266
                                    </button>
267
                                </div>
268
                                <div class="modal-body">
269
                                    <div class="getpaid-edit-item-div">
270
                                        <input type="hidden" name="id" class="form-control form-control-sm item-id">
271
                                        <label class="form-group w-100">
272
                                            <span><?php _e( 'Name', 'invoicing' ); ?></span>
273
                                            <input type="text" name="name" placeholder="<?php esc_attr_e( 'Item Name', 'invoicing' ); ?>" class="form-control form-control-sm item-name">
274
                                        </label>
275
                                        <label class="form-group w-100">
276
                                            <span class="getpaid-hide-if-hours getpaid-hide-if-quantity item-price"><?php _e( 'Amount', 'invoicing' ); ?></span>
277
                                            <span class="hide-if-amount"><?php _e( 'Price', 'invoicing' ); ?></span>
278
                                            <input type="text" name="price" placeholder="<?php wpinv_sanitize_amount( 0 ); ?>" class="form-control form-control-sm item-price">
279
                                        </label>
280
                                        <label class="form-group w-100 hide-if-amount">
281
                                            <span><?php _e( 'Quantity', 'invoicing' ); ?></span>
282
                                            <input type="number" name="quantity" placeholder="1" class="form-control form-control-sm item-quantity">
283
                                        </label>
284
                                        <label class="form-group w-100">
285
                                            <span><?php _e( 'Item Description', 'invoicing' ); ?></span>
286
                                            <textarea name="description" placeholder="<?php esc_attr_e( 'Enter a description for this item', 'invoicing' ); ?>" class="form-control item-description"></textarea>
287
                                        </label>
288
                                    </div>
289
                                </div>
290
                                <div class="modal-footer">
291
                                    <button type="button" class="btn btn-secondary getpaid-cancel" data-dismiss="modal"><?php _e( 'Cancel', 'invoicing' ); ?></button>
292
                                    <button type="button" class="btn btn-primary getpaid-save" data-dismiss="modal"><?php _e( 'Save', 'invoicing' ); ?></button>
293
                                </div>
294
                            </div>
295
                        </div>
296
                    </div>
297
                </div>
298
299
        <?php
300
    }
301
}
302