Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 290
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 3
zeroBSCRM_TransactionsMetaboxSetup
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
zeroBS__Metabox_Transaction
0.00% covered (danger)
0.00%
0 / 233
0.00% covered (danger)
0.00%
0 / 5
6642
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
2
 html
0.00% covered (danger)
0.00%
0 / 162
0.00% covered (danger)
0.00%
0 / 1
3192
 save_data
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 1
380
 post_save_data
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
 updateMessage
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
zeroBS__Metabox_TransactionTags
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
2
zeroBS__Metabox_TransactionActions
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 2
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
2
 html
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2/*
3 * Jetpack CRM
4 * https://jetpackcrm.com
5 * V3.0
6 *
7 * Copyright 2020 Automattic
8 *
9 * Date: 20/02/2019
10 */
11
12defined( 'ZEROBSCRM_PATH' ) || exit( 0 );
13
14/*
15======================================================
16    Init Func
17    ====================================================== */
18
19function zeroBSCRM_TransactionsMetaboxSetup() {
20
21    // main detail
22    $zeroBS__Metabox_Transaction = new zeroBS__Metabox_Transaction( __FILE__ );
23
24    // save
25    $zeroBS__Metabox_TransactionActions = new zeroBS__Metabox_TransactionActions( __FILE__ );
26
27    // Tags
28    $zeroBS__Metabox_TransactionTags = new zeroBS__Metabox_TransactionTags( __FILE__ );
29
30    // external sources
31    $zeroBS__Metabox_ExtSource = new zeroBS__Metabox_ExtSource( __FILE__, 'transaction', 'zbs-add-edit-transaction-edit' );
32}
33
34    add_action( 'admin_init', 'zeroBSCRM_TransactionsMetaboxSetup' );
35
36/*
37======================================================
38    / Init Func
39    ====================================================== */
40
41/*
42======================================================
43    Transaction Metabox
44    ====================================================== */
45
46class zeroBS__Metabox_Transaction extends zeroBS__Metabox {
47
48    // this is for catching 'new' transactions
49    private $newRecordNeedsRedir = false;
50
51    public function __construct( $plugin_file ) {
52
53        // set these
54        $this->objType         = 'transaction';
55        $this->metaboxID       = 'zerobs-transaction-edit';
56        $this->metaboxTitle    = __( 'Transaction Information', 'zero-bs-crm' ); // will be headless anyhow
57        $this->headless        = true;
58        $this->metaboxScreen   = 'zbs-add-edit-transaction-edit';
59        $this->metaboxArea     = 'normal';
60        $this->metaboxLocation = 'high';
61        $this->saveOrder       = 1;
62        $this->capabilities    = array(
63
64            'can_hide'        => false, // can be hidden
65            'areas'           => array( 'normal' ), // areas can be dragged to - normal side = only areas currently
66            'can_accept_tabs' => true,  // can/can't accept tabs onto it
67            'can_become_tab'  => false, // can be added as tab
68            'can_minimise'    => true, // can be minimised
69            'can_move'        => true, // can be moved
70
71        );
72
73        // call this
74        $this->initMetabox();
75    }
76
77    public function html( $transaction, $metabox ) {
78
79            // localise ID
80            $transactionID = -1;
81        if ( is_array( $transaction ) && isset( $transaction['id'] ) ) {
82            $transactionID = (int) $transaction['id'];
83        }
84
85            // if new + $zbsObjDataPrefill passed, use that instead of loaded trans.
86        if ( $transactionID == -1 ) {
87            global $zbsObjDataPrefill;
88            $transaction = $zbsObjDataPrefill;
89        }
90
91            global $zbs;
92
93            #} Prefill ID and OBJ are added to the #zbs_invoice to aid in prefilling the data (when drawn with JS)
94            $prefill_id = -1;
95        $prefill_obj    = -1;
96        $prefill_email  = '';
97        if ( isset( $_GET['zbsprefillcust'] ) && ! empty( $_GET['zbsprefillcust'] ) ) {
98            $prefill_id    = (int) $_GET['zbsprefillcust'];
99            $prefill_obj   = ZBS_TYPE_CONTACT;
100            $prefill_email = zeroBS_customerEmail( $prefill_id );
101        }
102        if ( isset( $_GET['zbsprefillco'] ) && ! empty( $_GET['zbsprefillco'] ) ) {
103            $prefill_id    = (int) $_GET['zbsprefillco'];
104            $prefill_obj   = ZBS_TYPE_COMPANY;
105            $prefill_email = zeroBS_companyEmail( $prefill_id );
106        }
107
108            // DAL2 legacy, patched through
109            $contactID = -1;
110        if ( is_array( $transaction ) && isset( $transaction['contact'] ) && is_array( $transaction['contact'] ) && count( $transaction['contact'] ) > 0 ) {
111            $contactID = $transaction['contact'][0]['id'];
112        }
113            $companyID = -1;
114        if ( is_array( $transaction ) && isset( $transaction['company'] ) && is_array( $transaction['company'] ) && count( $transaction['company'] ) > 0 ) {
115            $companyID = $transaction['company'][0]['id'];
116        }
117            $contactName = '';
118        $companyName     = '';
119        if ( $contactID > 0 ) {
120            $contactName = $zbs->DAL->contacts->getContactNameWithFallback( $contactID );
121        }
122        if ( empty( $contactName ) || $contactName == -1 ) {
123            $contactName = '';
124        }
125        if ( ! empty( $companyID ) && $companyID > 0 ) {
126            $companyName = $zbs->DAL->companies->getCompanyNameEtc( $companyID );
127            if ( empty( $companyName ) ) {
128                $companyName = jpcrm_label_company() . ' #' . $companyID;
129            }
130        }
131
132            // prefill if not assigned:
133        if ( $contactID == -1 && $companyID == -1 ) {
134
135            switch ( $prefill_obj ) {
136
137                case ZBS_TYPE_CONTACT:
138                    if ( $prefill_id > 0 ) {
139
140                        // dump into contactID etc.
141                        $contactID   = $prefill_id;
142                        $contactName = $zbs->DAL->contacts->getContactNameWithFallback( $contactID );
143
144                    }
145
146                    break;
147
148                case ZBS_TYPE_COMPANY:
149                    if ( $prefill_id > 0 ) {
150
151                        // dump into contactID etc.
152                        $companyID   = $prefill_id;
153                        $companyName = $zbs->DAL->companies->getCompanyNameEtc( $companyID );
154                        if ( empty( $companyName ) ) {
155                            $companyName = jpcrm_label_company() . ' #' . $companyID;
156                        }
157                    }
158
159                    break;
160
161            }
162        }
163
164        ?>
165                <script type="text/javascript">var zbscrmjs_secToken = '<?php echo esc_js( wp_create_nonce( 'zbscrmjs-ajax-nonce' ) ); ?>';</script>
166                <?php
167
168                // New transaction?
169                if ( gettype( $transaction ) != 'array' ) {
170                    echo '<input type="hidden" name="zbscrm_newtransaction" value="1" />';
171                }
172
173                ?>
174                    <div>
175                        <div class="jpcrm-form-grid" id="wptbpMetaBoxMainItem">
176                    
177                        <div class="jpcrm-form-group jpcrm-form-group-span-2">
178                            <label class="jpcrm-form-label" for="ref"><?php echo esc_html( __( 'Transaction unique ID', 'zero-bs-crm' ) ); ?>:</label>
179                            <input type="text" id="ref" name="zbst_ref" class="form-control" value="<?php echo esc_attr( isset( $transaction['ref'] ) ? $transaction['ref'] : zeroBSCRM_uniqueID() ); ?>" autocomplete="<?php echo esc_attr( jpcrm_disable_browser_autocomplete() ); ?>" /></td>
180                        </div>
181
182                <?php
183
184                // Refunds show a notice
185
186                if (
187                    // has Refund Type
188                    isset( $transaction['type'] ) && $transaction['type'] == __( 'Refund', 'zero-bs-crm' )
189                    &&
190                    // has parent ID based transaction
191                    isset( $transaction['parent'] ) && $zbs->DAL->transactions->transaction_exists( $transaction['parent'] )
192                ) {
193
194                    // get parent ref
195                    $parent_ref = $zbs->DAL->transactions->get_transaction_ref( $transaction['parent'] );
196
197                    ?>
198                        <div class="jpcrm-form-group jpcrm-form-group-span-2">
199                                <p style="text-align: center;">
200                                <?php echo esc_html( sprintf( __( 'This transaction is a refund against transaction #%s', 'zero-bs-crm' ), $parent_ref ) ); ?>
201                                    <a href="<?php echo jpcrm_esc_link( 'edit', $transaction['parent'], ZBS_TYPE_TRANSACTION ); ?>" class="ui compact teal button" style="margin-left:10px"><?php echo esc_html( __( 'View Original Transaction', 'zero-bs-crm' ) ); ?></a>
202                                </p>
203                        </div>
204                        <?php
205
206                }
207
208                // get transaction statuses
209                $available_statuses = zeroBSCRM_getTransactionsStatuses( true );
210
211                // if current status is not a valid one, add it to statuses array
212                if ( empty( $transaction['status'] ) ) {
213                    $transaction['status'] = $available_statuses[0];
214                } elseif ( ! $zbs->DAL->is_valid_obj_status( ZBS_TYPE_TRANSACTION, $transaction['status'] ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
215                    $available_statuses[] = $transaction['status'];
216                }
217
218                ?>
219                    <div class="jpcrm-form-group jpcrm-form-group-span-2">
220                        <label class="jpcrm-form-label" for="zbst_status"><?php echo esc_html( __( 'Transaction Status:', 'zero-bs-crm' ) ); ?></label>
221                        <select class="form-control" id="zbst_status" name="zbst_status">
222                        <?php
223                        foreach ( $available_statuses as $status ) {
224                            $sel = $status === $transaction['status'] ? ' selected' : '';
225                            echo '<option value="' .
226                            esc_attr( $status ) .
227                            '"' .
228                            esc_attr( $sel ) .
229                            '>' .
230                            esc_html( $status ) .
231                            '</option>';
232                        }
233                        ?>
234                        </select>
235                    </div>
236
237                    <div class="jpcrm-form-group jpcrm-form-group-span-2">
238                        <label class="jpcrm-form-label" for="title"><?php echo esc_html( __( 'Transaction Name:', 'zero-bs-crm' ) ); ?>
239                            <span class="zbs-infobox zbs-infobox-transaction"><?php echo esc_html( __( 'If possible, keep these the same if you routinely use common products here (they are used in the transaction index)', 'zero-bs-crm' ) ); ?></span>
240                        </label>
241                        <td><input id="title" type="text" name="zbst_title" value="<?php echo esc_attr( isset( $transaction['title'] ) ? $transaction['title'] : '' ); ?>" class="form-control widetext" autocomplete="<?php echo esc_attr( jpcrm_disable_browser_autocomplete() ); ?>" /></td>
242                    </div>
243
244                    <div class="jpcrm-form-group jpcrm-form-group-span-2">
245                        <label class="jpcrm-form-label" for="total"><?php echo esc_html( __( 'Transaction Value', 'zero-bs-crm' ) ); ?><?php echo ' (' . esc_html( zeroBSCRM_getCurrencyChr() ) . '):'; ?></label></th>
246                        <input class="form-control" type="text" id="total" name="zbst_total" value="<?php echo esc_attr( isset( $transaction['total'] ) ? $transaction['total'] : '0.00' ); ?>" class="form-control numbersOnly" autocomplete="<?php echo esc_attr( jpcrm_disable_browser_autocomplete() ); ?>" />
247                    </div>
248
249                    <div class="jpcrm-form-group">
250                        <label class="jpcrm-form-label">
251                        <?php echo esc_html( __( 'Transaction Date', 'zero-bs-crm' ) ); ?>:
252                            <span class="zbs-infobox zbs-infobox-transaction"><?php echo esc_html( __( 'The transaction date will default to the initial save date if left blank.', 'zero-bs-crm' ) ); ?></span>
253                        </label>
254                        <input class="form-control" type="date" name="zbst_date_datepart" id="transactionDate" value="<?php echo isset( $transaction['date'] ) ? esc_attr( jpcrm_uts_to_date_str( $transaction['date'], 'Y-m-d' ) ) : ''; ?>" autocomplete="<?php echo esc_attr( jpcrm_disable_browser_autocomplete() ); ?>" >
255                    </div>
256                    <div class="jpcrm-form-group">
257                        <label class="jpcrm-form-label">&nbsp;</label>
258                        <input class="form-control" type="time" name="zbst_date_timepart" value="<?php echo isset( $transaction['date'] ) ? esc_attr( jpcrm_uts_to_time_str( $transaction['date'], 'H:i' ) ) : ''; ?>" autocomplete="<?php echo esc_attr( jpcrm_disable_browser_autocomplete() ); ?>" >
259                    </div>
260
261                    <?php
262                    // Custom Fields
263
264                    global $zbsTransactionFields;
265
266                    // had to add this, because other fields are put out separately here
267                    // TODO resolve zbsTransactionFields global for new DAL
268                    $skipList = array( 'ref', 'customer', 'status', 'total', 'customer_name', 'date', 'currency', 'title', 'tax_rate', 'taxes', 'date_paid', 'date_completed' );
269
270                    if ( zeroBSCRM_getSetting( 'transaction_fee' ) !== 1 ) {
271                        $skipList[] = 'fee';
272                    }
273
274                    if ( zeroBSCRM_getSetting( 'transaction_net' ) !== 1 ) {
275                        $skipList[] = 'net';
276                    }
277
278                    if ( zeroBSCRM_getSetting( 'transaction_discount' ) !== 1 ) {
279                        $skipList[] = 'discount';
280                    }
281
282                    if ( zeroBSCRM_getSetting( 'transaction_tax' ) !== 1 ) {
283                        $skipList[] = 'tax';
284                    }
285
286                    // shipping?
287                    $useShipping = zeroBSCRM_getSetting( 'shippingfortransactions' );
288                    if ( $useShipping != 1 ) {
289                        $skipList[] = 'shipping';
290                        $skipList[] = 'shipping_taxes';
291                    }
292
293                    // output additional fields
294                    zeroBSCRM_html_editFields( $transaction, $zbsTransactionFields, 'zbst_', $skipList );
295
296                    // use paid/completed dates?
297                    $usePaidDates = zeroBSCRM_getSetting( 'paiddatestransaction' );
298                    if ( $usePaidDates === 1 ) {
299                        ?>
300                        <div class="jpcrm-form-group jpcrm-form-group-span-2">
301                            <label class="jpcrm-form-label">
302                                <?php echo esc_html( __( 'Date Paid', 'zero-bs-crm' ) ); ?>:
303                                <span class="zbs-infobox zbs-infobox-transaction"><?php echo esc_html( __( 'This will default to the transaction date if left blank.', 'zero-bs-crm' ) ); ?></span>
304                            </label>
305                            <div class="jpcrm-form-input-group">
306                                <input type="date" name="zbst_date_paid_datepart" value="<?php echo isset( $transaction['date_paid'] ) ? esc_attr( jpcrm_uts_to_date_str( $transaction['date_paid'], 'Y-m-d' ) ) : ''; ?>" autocomplete="<?php echo esc_attr( jpcrm_disable_browser_autocomplete() ); ?>" >
307                                @
308                                <input type="time" name="zbst_date_paid_timepart" value="<?php echo isset( $transaction['date_paid'] ) ? esc_attr( jpcrm_uts_to_time_str( $transaction['date_paid'], 'H:i' ) ) : ''; ?>" autocomplete="<?php echo esc_attr( jpcrm_disable_browser_autocomplete() ); ?>" >
309                            </div>
310                        </div>
311
312                        <div class="jpcrm-form-group jpcrm-form-group-span-2">
313                            <label class="jpcrm-form-label">
314                                <?php echo esc_html( __( 'Date Completed', 'zero-bs-crm' ) ); ?>:
315                                <span class="zbs-infobox zbs-infobox-transaction"><?php echo esc_html( __( 'This will default to the transaction date if left blank.', 'zero-bs-crm' ) ); ?></span>
316                            </label>
317                            <div class="jpcrm-form-input-group">
318                                <input type="date" name="zbst_date_completed_datepart" value="<?php echo isset( $transaction['date_completed'] ) ? esc_attr( jpcrm_uts_to_date_str( $transaction['date_completed'], 'Y-m-d' ) ) : ''; ?>" autocomplete="<?php echo esc_attr( jpcrm_disable_browser_autocomplete() ); ?>" >
319                                @
320                                <input type="time" name="zbst_date_completed_timepart" value="<?php echo isset( $transaction['date_completed'] ) ? esc_attr( jpcrm_uts_to_time_str( $transaction['date_completed'], 'H:i' ) ) : ''; ?>" autocomplete="<?php echo esc_attr( jpcrm_disable_browser_autocomplete() ); ?>" >
321                            </div>
322                        </div>
323                        <?php
324                    }
325
326                    ?>
327
328                    </div>
329                </div>
330
331                <table class="form-table wh-metatab wptbp">
332
333                    <tr><td><hr /></td></tr>
334
335                    <tr>
336                        <td>
337                            <h2 style="font-size: 20px"><i class="linkify icon"></i> <?php echo esc_html( __( 'Assign Transaction to', 'zero-bs-crm' ) ); ?></h2>
338                        </td>
339                    </tr>
340
341                    <tr class="wh-large" id="zbs-transaction-assignment-wrap">
342                        <td>
343                        <?php // hidden inputs dictating any assignment typeaheads ?>
344                            <input id="customer" name="customer" value="<?php echo esc_attr( $contactID ); ?>" class="form-control widetext" type="hidden">
345                            <input id="customer_name" name="customer_name" value="<?php echo esc_attr( $contactName ); ?>" class="form-control widetext" type="hidden">
346                            <input type="hidden" name="zbsct_company" id="zbsct_company" value="<?php echo esc_attr( $companyID ); ?>" />
347                        <?php
348                        if ( zeroBSCRM_getSetting( 'companylevelcustomers' ) != '1' ) {
349
350                            // Just contact
351                            ?>
352                                    <div id="zbs-customer-title"><label><?php echo esc_html( __( 'Contact', 'zero-bs-crm' ) ); ?></label></div>
353                            <?php
354                            echo zeroBSCRM_CustomerTypeList( 'zbscrmjs_transaction_setCustomer', $contactName, false, 'zbscrmjs_transaction_unsetCustomer' );
355
356                            // mikes inv selector
357                            ?>
358                                    <div class="assignInvToCust" style="display:none;max-width:658px;" id="invoiceSelectionTitle"><label><?php echo esc_html( __( 'Contact invoice:', 'zero-bs-crm' ) ); ?></label><span class="zbs-infobox zbs-infobox-transaction" style="margin-top:3px"><?php echo esc_html( __( 'Is this transaction a payment for an invoice? If so, enter the Invoice ID. Otherwise leave blank.', 'zero-bs-crm' ) ); ?></span></div>
359                                    <div id="invoiceFieldWrap" style="position:relative;display:none;max-width:658px" class="assignInvToCust"><input style="max-width:200px" id="invoice_id" name="invoice_id" value="<?php echo esc_attr( isset( $transaction['invoice_id'] ) ? $transaction['invoice_id'] : '' ); ?>" class="form-control" autocomplete="<?php echo esc_attr( jpcrm_disable_browser_autocomplete() ); ?>" /></div>
360                                <?php
361
362                        } else {
363
364                            // contact or co
365                            ?>
366                                    <div class="ui grid"><div class="seven wide column">
367                                        <div id="zbs-customer-title"><label>
368                                    <?php
369                                    echo esc_html( __( 'Contact', 'zero-bs-crm' ) );
370                                    ?>
371                                        </label></div>
372                                    <?php
373
374                                    // contact
375                                    echo zeroBSCRM_CustomerTypeList( 'zbscrmjs_transaction_setCustomer', $contactName, false, 'zbscrmjs_transaction_unsetCustomer' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped,WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
376
377                                    // mikes inv selector
378                                    ?>
379                                        <div class="assignInvToCust" style="display:none;max-width:658px;margin-top:21px;margin-bottom:10px;" id="invoiceSelectionTitle"><label><?php echo esc_html( __( 'Contact invoice:', 'zero-bs-crm' ) ); ?>&nbsp;</label><span class="zbs-infobox" style="margin-top:3px"><?php echo esc_html( __( 'Is this transaction a payment for an invoice? If so, enter the Invoice ID. Otherwise leave blank.', 'zero-bs-crm' ) ); ?></span></div>
380                                        <div id="invoiceFieldWrap" style="position:relative;display:none;max-width:658px" class="assignInvToCust"><input style="max-width:200px" id="invoice_id" name="invoice_id" value="<?php echo esc_attr( isset( $transaction['invoice_id'] ) ? $transaction['invoice_id'] : '' ); ?>" class="form-control" autocomplete="<?php echo esc_attr( jpcrm_disable_browser_autocomplete() ); ?>" /></div>
381
382                                                </div><div class="two wide column centered" style="text-align:center;"><?php echo esc_html( __( 'Or', 'zero-bs-crm' ) ); ?></div><div class="seven wide column"><div id="zbs-company-title"><label><?php echo esc_html( jpcrm_label_company() ); ?></label></div>
383                                        <?php
384
385                                        // company
386                                        echo zeroBSCRM_CompanyTypeList( 'zbscrmjs_transaction_setCompany', $companyName, true, 'zbscrmjs_transaction_unsetCompany' );
387
388                                        ?>
389                                        </div>
390                                    </div>
391                                    <?php
392                        }
393
394                        ?>
395
396                        </td>
397                    </tr>
398
399                </table>
400
401      
402        <script type="text/javascript">
403
404            // v3.0 Moved into it's own JS for optimal perf.
405            // js/ZeroBSCRM.admin.transactioneditor.js
406            var zeroBSCRMJS_transactionedit_lang = {
407
408                    'noinvoices': '<?php echo esc_html( zeroBSCRM_slashOut( __( 'No Invoices Found!', 'zero-bs-crm' ) ) ); ?>',
409                    'none': '<?php echo esc_html( zeroBSCRM_slashOut( __( 'None', 'zero-bs-crm' ) ) ); ?>',
410                    'view': '<?php echo esc_html( zeroBSCRM_slashOut( __( 'View', 'zero-bs-crm' ) ) ); ?>',
411                    'contact': '<?php echo esc_html( zeroBSCRM_slashOut( __( 'Contact', 'zero-bs-crm' ) ) ); ?>',
412                    'company': '<?php echo esc_html( zeroBSCRM_slashOut( jpcrm_label_company() ) ); ?>',
413                    'selectinv': '<?php echo esc_html( zeroBSCRM_slashOut( __( 'Select Invoice', 'zero-bs-crm' ) ) ); ?>',
414            }
415            var zeroBSCRMJS_transactionedit_links = {
416
417                'editinvprefix': '<?php echo jpcrm_esc_link( 'edit', -1, 'zerobs_invoice', true ); ?>',
418                'editcontactprefix': '<?php echo jpcrm_esc_link( 'edit', -1, 'zerobs_customer', true ); ?>',
419                'editcompanyprefix': '<?php echo jpcrm_esc_link( 'edit', -1, 'zerobs_company', true ); ?>',
420            }
421
422        </script>
423        <?php
424    }
425
426    public function save_data( $transactionID, $transaction ) {
427
428        if ( ! defined( 'ZBS_T_SAVED' ) ) {
429
430            define( 'ZBS_T_SAVED', 1 );
431
432            // DAL3.0+
433            global $zbs;
434
435            // check this
436            if ( empty( $transactionID ) || $transactionID < 1 ) {
437                $transactionID = -1;
438            }
439
440                // ====== DAL 3 way
441                $autoGenAutonumbers = true; // generate if not set
442                $transaction        = zeroBS_buildObjArr( $_POST, array(), 'zbst_', '', false, ZBS_TYPE_TRANSACTION, $autoGenAutonumbers );
443
444                // Use the tag-class function to retrieve any tags so we can add inline.
445                // Save tags against objid
446                $transaction['tags'] = zeroBSCRM_tags_retrieveFromPostBag( true, ZBS_TYPE_TRANSACTION );
447
448                $date                = jpcrm_datetime_post_keys_to_uts( 'zbst_date' );
449                $transaction['date'] = empty( $date ) ? time() : $date;
450
451                $date_paid                = jpcrm_datetime_post_keys_to_uts( 'zbst_date_paid' );
452                $transaction['date_paid'] = empty( $date_paid ) ? $transaction['date'] : $date_paid;
453
454                $date_completed                = jpcrm_datetime_post_keys_to_uts( 'zbst_date_completed' );
455                $transaction['date_completed'] = empty( $date_completed ) ? $transaction['date'] : $date_completed;
456
457                // currency wasn't set when storing manually too
458                $transaction['currency'] = zeroBSCRM_getCurrencyStr();
459
460                    // assignments
461                    $zbsTransactionContact    = (int) sanitize_text_field( $_POST['customer'] );
462                    $transaction['contacts']  = ( $zbsTransactionContact > 0 ) ? array( $zbsTransactionContact ) : array();
463                    $zbsTransactionCompany    = (int) sanitize_text_field( $_POST['zbsct_company'] );
464                    $transaction['companies'] = ( $zbsTransactionCompany > 0 ) ? array( $zbsTransactionCompany ) : array();
465
466                    #} Invoice allocation:
467                    $transaction['invoice_id'] = '';
468            if ( isset( $_POST['invoice_id'] ) ) {
469                $transaction['invoice_id'] = (int) sanitize_text_field( $_POST['invoice_id'] );
470            }
471
472                    // add/update
473                    $addUpdateReturn = $zbs->DAL->transactions->addUpdateTransaction(
474                        array(
475
476                            'id'            => $transactionID,
477                            'data'          => $transaction,
478                            'limitedFields' => -1,
479
480                        )
481                    );
482
483                    // Note: For NEW objs, we make sure a global is set here, that other update funcs can catch
484                    // ... so it's essential this one runs first!
485                    // this is managed in the metabox Class :)
486            if ( $transactionID == -1 && ! empty( $addUpdateReturn ) && $addUpdateReturn != -1 ) {
487
488                $transactionID = $addUpdateReturn;
489                global $zbsJustInsertedMetaboxID;
490                $zbsJustInsertedMetaboxID = $transactionID;
491
492                // set this so it redirs
493                $this->newRecordNeedsRedir = true;
494            }
495
496                    // success?
497            if ( $addUpdateReturn != -1 && $addUpdateReturn > 0 ) {
498
499                // Update Msg
500                // this adds an update message which'll go out ahead of any content
501                // This adds to metabox: $this->updateMessages['update'] = zeroBSCRM_UI2_messageHTML('info olive mini zbs-not-urgent',__('Contact Updated',"zero-bs-crm"),'','address book outline','contactUpdated');
502                // This adds to edit page
503                $this->updateMessage();
504
505                // catch any non-critical messages
506                $nonCriticalMessages = $zbs->DAL->getErrors( ZBS_TYPE_TRANSACTION );
507                if ( is_array( $nonCriticalMessages ) && count( $nonCriticalMessages ) > 0 ) {
508                    $this->dalNoticeMessage( $nonCriticalMessages );
509                }
510            } else {
511
512                // fail somehow
513                $failMessages = $zbs->DAL->getErrors( ZBS_TYPE_TRANSACTION );
514
515                // show msg (retrieved from DAL err stack)
516                if ( is_array( $failMessages ) && count( $failMessages ) > 0 ) {
517                    $this->dalErrorMessage( $failMessages );
518                } else {
519                    $this->dalErrorMessage( array( __( 'Insert/Update Failed with general error', 'zero-bs-crm' ) ) );
520                }
521
522                // pass the pre-fill:
523                global $zbsObjDataPrefill;
524                $zbsObjDataPrefill = $transaction;
525
526            }
527        }
528
529        return $transaction;
530    }
531
532    // This catches 'new' contacts + redirs to right url
533    public function post_save_data( $objID, $obj ) {
534
535        if ( $this->newRecordNeedsRedir ) {
536
537            global $zbsJustInsertedMetaboxID;
538            if ( ! empty( $zbsJustInsertedMetaboxID ) && $zbsJustInsertedMetaboxID > 0 ) {
539
540                // redir
541                wp_redirect( jpcrm_esc_link( 'edit', $zbsJustInsertedMetaboxID, $this->objType ) );
542                exit( 0 );
543
544            }
545        }
546    }
547
548    public function updateMessage() {
549
550        global $zbs;
551
552        // zbs-not-urgent means it'll auto hide after 1.5s
553        // genericified from DAL3.0
554        $msg = zeroBSCRM_UI2_messageHTML( 'info olive mini zbs-not-urgent', $zbs->DAL->typeStr( $zbs->DAL->objTypeKey( $this->objType ) ) . ' ' . __( 'Updated', 'zero-bs-crm' ), '', 'address book outline', 'contactUpdated' );
555
556        $zbs->pageMessages[] = $msg;
557    }
558}
559
560/*
561======================================================
562    / Transaction Metabox
563    ====================================================== */
564
565/*
566======================================================
567    Create Tags Box
568    ====================================================== */
569
570// phpcs:ignore
571class zeroBS__Metabox_TransactionTags extends zeroBS__Metabox_Tags {
572
573    public function __construct( $plugin_file ) { // phpcs:ignore
574        // phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
575        $this->objTypeID       = ZBS_TYPE_TRANSACTION;
576        $this->objType         = 'transaction';
577        $this->metaboxID       = 'zerobs-transaction-tags';
578        $this->metaboxTitle    = __( 'Transaction Tags', 'zero-bs-crm' );
579        $this->metaboxScreen   = 'zbs-add-edit-transaction-edit'; // we can use anything here as is now using our func
580        $this->metaboxArea     = 'side';
581        $this->metaboxLocation = 'high';
582        $this->showSuggestions = true;
583        // phpcs:enable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
584        $this->capabilities = array(
585            'can_hide'        => true, // can be hidden
586            'areas'           => array( 'side' ), // areas can be dragged to - normal side = only areas currently
587            'can_accept_tabs' => false,  // can/can't accept tabs onto it
588            'can_become_tab'  => false, // can be added as tab
589            'can_minimise'    => true, // can be minimised
590        );
591
592        // call this
593        $this->initMetabox();
594    }
595    // html + save dealt with by parent class :)
596}
597
598/*
599======================================================
600    / Create Tags Box
601    ====================================================== */
602
603/*
604======================================================
605    Transaction Actions Metabox
606    ====================================================== */
607
608// phpcs:ignore
609class zeroBS__Metabox_TransactionActions extends zeroBS__Metabox {
610    public function __construct( $plugin_file ) { // phpcs:ignore
611        // phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
612        $this->objType         = 'transaction';
613        $this->metaboxID       = 'zerobs-transaction-actions';
614        $this->metaboxTitle    = __( 'Transaction', 'zero-bs-crm' ) . ' ' . __( 'Actions', 'zero-bs-crm' ); // will be headless anyhow
615        $this->headless        = true;
616        $this->metaboxScreen   = 'zbs-add-edit-transaction-edit';
617        $this->metaboxArea     = 'side';
618        $this->metaboxLocation = 'high';
619        $this->saveOrder       = 1;
620        // phpcs:enable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
621        $this->capabilities = array(
622            'can_hide'        => false, // can be hidden
623            'areas'           => array( 'side' ), // areas can be dragged to - normal side = only areas currently
624            'can_accept_tabs' => true,  // can/can't accept tabs onto it
625            'can_become_tab'  => false, // can be added as tab
626            'can_minimise'    => true, // can be minimised
627            'can_move'        => true, // can be moved
628        );
629
630        // call this
631        $this->initMetabox();
632    }
633
634    public function html( $transaction, $metabox ) { // phpcs:ignore Squiz.Commenting.FunctionComment.Missing,VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
635        ?>
636        <div class="zbs-generic-save-wrap">
637            <div class="ui medium dividing header"><i class="save icon"></i> <?php echo esc_html( __( 'Transaction Actions', 'zero-bs-crm' ) ); ?></div>
638            <?php
639
640            // localise ID & content
641            $transaction_id = -1;
642            if ( is_array( $transaction ) && isset( $transaction['id'] ) ) {
643                $transaction_id = (int) $transaction['id'];
644            }
645
646            // if a saved post...
647            if ( $transaction_id > 0 ) {
648                ?>
649                <div class="zbs-transaction-actions-bottom zbs-objedit-actions-bottom">
650                    <button class="ui button black" type="button" id="zbs-edit-save"><?php echo esc_html( __( 'Update Transaction', 'zero-bs-crm' ) ); ?></button>
651                    <?php
652                    // for now just check if can modify, later better, granular perms.
653                    if ( zeroBSCRM_permsTransactions() ) {
654                        ?>
655                        <div id="zbs-transaction-actions-delete" class="zbs-objedit-actions-delete">
656                            <a class="submitdelete deletion" href="<?php echo jpcrm_esc_link( 'delete', $transaction_id, 'transaction' ); ?>"><?php echo esc_html( __( 'Delete Permanently', 'zero-bs-crm' ) ); ?></a>
657                        </div>
658                        <?php
659                    }
660                    ?>
661                    <div class='clear'></div>
662                </div>
663                <?php
664            } else {
665                // NEW transaction
666                ?>
667                <div class="zbs-transaction-actions-bottom zbs-objedit-actions-bottom">
668                    <button class="ui button black" type="button" id="zbs-edit-save"><?php echo esc_html( __( 'Save Transaction', 'zero-bs-crm' ) ); ?></button>
669                </div>
670                <?php
671            }
672            ?>
673        </div>
674        <?php
675    } // html
676
677    // saved via main metabox
678}
679
680/**
681 * End of Transaction Action Metabox
682 */