default.php
6 days ago
default_details.php
6 days ago
default_details_appointments.php
6 days ago
default_details_appointments_modal.php
6 days ago
default_details_appointments_option_modal.php
6 days ago
default_details_appointments_table.php
6 days ago
default_details_billing.php
6 days ago
default_details_order.php
6 days ago
default_details_order_custmail.php
6 days ago
default_fields.php
6 days ago
default_orderstatus.php
6 days ago
default_orderstatus_history.php
6 days ago
default_orderstatus_manage.php
6 days ago
default_usernotes.php
6 days ago
default_usernotes_main.php
6 days ago
default_usernotes_sidebar.php
6 days ago
index.html
6 days ago
default_details_appointments.php
241 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments |
| 4 | * @subpackage core |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. |
| 7 | * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 | * @link https://vikwp.com |
| 9 | */ |
| 10 | |
| 11 | // No direct access |
| 12 | defined('ABSPATH') or die('No script kiddies please!'); |
| 13 | |
| 14 | ?> |
| 15 | |
| 16 | <div class="order-appointments-table" id="order-appointments-table"> |
| 17 | |
| 18 | <?php echo $this->loadTemplate('details_appointments_table'); ?> |
| 19 | |
| 20 | </div> |
| 21 | |
| 22 | <script> |
| 23 | var SELECTED_OPTION = null; |
| 24 | var OPTIONS_COUNT = <?php echo count($this->reservation->options); ?>; |
| 25 | var TMP_COSTS = {}; |
| 26 | |
| 27 | jQuery(function($) { |
| 28 | // fill the form before showing the inspector |
| 29 | $('#order-app-inspector').on('inspector.show', () => { |
| 30 | initAppointmentData(); |
| 31 | |
| 32 | // save current costs of the service |
| 33 | TMP_COSTS = { |
| 34 | net: parseFloat($('input[name="service_net"]').val()), |
| 35 | tax: parseFloat($('input[name="service_tax"]').val()), |
| 36 | gross: parseFloat($('input[name="service_gross"]').val()), |
| 37 | }; |
| 38 | }); |
| 39 | |
| 40 | // apply the changes |
| 41 | $('#order-app-inspector').on('inspector.save', function() { |
| 42 | // disable button |
| 43 | $('#order-app-inspector button[data-role="save"]').prop('disabled', true); |
| 44 | |
| 45 | // get saved record |
| 46 | commitAppointmentChanges().then((data) => { |
| 47 | // refresh appointment row |
| 48 | vapRefreshAppointmentRow($('#' + SELECTED_OPTION), data); |
| 49 | |
| 50 | // refresh total prices |
| 51 | vapRefreshTotals(data); |
| 52 | |
| 53 | // auto-close on save |
| 54 | $(this).inspector('dismiss'); |
| 55 | }).catch((err) => { |
| 56 | // the callback performs an AJAX request to retrieve |
| 57 | // all the costs of the saved service and, since it |
| 58 | // may fail, we need to catch the exception thrown |
| 59 | // and display the error message |
| 60 | alert(err); |
| 61 | }).finally(() => { |
| 62 | // re-enable button |
| 63 | $('#order-app-inspector button[data-role="save"]').prop('disabled', false); |
| 64 | }); |
| 65 | }); |
| 66 | |
| 67 | // open inspector for new options |
| 68 | $('#add-app-option').on('click', () => { |
| 69 | vapOpenAppOptionCard(); |
| 70 | }); |
| 71 | |
| 72 | // fill the form before showing the inspector |
| 73 | $('#order-app-opt-inspector').on('inspector.show', () => { |
| 74 | var json = []; |
| 75 | |
| 76 | // fetch JSON data |
| 77 | if (SELECTED_OPTION) { |
| 78 | var fieldset = $('#' + SELECTED_OPTION); |
| 79 | |
| 80 | json = fieldset.find('input[name="option_json[]"]').val(); |
| 81 | |
| 82 | try { |
| 83 | json = JSON.parse(json); |
| 84 | } catch (err) { |
| 85 | json = {}; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // save current costs of the service |
| 90 | TMP_COSTS = { |
| 91 | net: json.net ? parseFloat(json.net) : 0, |
| 92 | tax: json.tax ? parseFloat(json.tax) : 0, |
| 93 | gross: json.gross ? parseFloat(json.gross) : 0, |
| 94 | }; |
| 95 | |
| 96 | if (json.id === undefined) { |
| 97 | // creating new record, hide delete button |
| 98 | $(this).find('[data-role="delete"]').hide(); |
| 99 | } else { |
| 100 | // editing existing record, show delete button |
| 101 | $(this).find('[data-role="delete"]').show(); |
| 102 | } |
| 103 | |
| 104 | fillAppOptionForm(json); |
| 105 | }); |
| 106 | |
| 107 | // apply the changes |
| 108 | $('#order-app-opt-inspector').on('inspector.save', function() { |
| 109 | // validate form |
| 110 | if (!appOptValidator.validate()) { |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | // disable button |
| 115 | $('#order-app-opt-inspector button[data-role="save"]').prop('disabled', true); |
| 116 | |
| 117 | // get saved record |
| 118 | getAppOptionData().then((data) => { |
| 119 | var fieldset; |
| 120 | |
| 121 | if (SELECTED_OPTION) { |
| 122 | fieldset = $('#' + SELECTED_OPTION); |
| 123 | } else { |
| 124 | fieldset = vapAddAppOptionCard(data); |
| 125 | } |
| 126 | |
| 127 | if (fieldset.length == 0) { |
| 128 | // an error occurred, abort |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | // save JSON data |
| 133 | fieldset.find('input[name="option_json[]"]').val(JSON.stringify(data)); |
| 134 | |
| 135 | // refresh card details |
| 136 | vapRefreshAppOptionCard(fieldset, data); |
| 137 | |
| 138 | // refresh total prices |
| 139 | vapRefreshTotals(data); |
| 140 | |
| 141 | // auto-close on save |
| 142 | $(this).inspector('dismiss'); |
| 143 | }).catch((err) => { |
| 144 | // the callback performs an AJAX request to retrieve |
| 145 | // all the costs of the option to save/add and, since |
| 146 | // it may fail, we need to catch the exception thrown |
| 147 | // and display the error message |
| 148 | alert(err); |
| 149 | }).finally(() => { |
| 150 | // re-enable button |
| 151 | $('#order-app-opt-inspector button[data-role="save"]').prop('disabled', false); |
| 152 | }); |
| 153 | }); |
| 154 | |
| 155 | // delete the record |
| 156 | $('#order-app-opt-inspector').on('inspector.delete', function() { |
| 157 | var fieldset = $('#' + SELECTED_OPTION); |
| 158 | |
| 159 | if (fieldset.length == 0) { |
| 160 | // record not found |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | // get existing record |
| 165 | var json = fieldset.find('input[name="option_json[]"]').val(); |
| 166 | |
| 167 | try { |
| 168 | json = JSON.parse(json); |
| 169 | } catch (err) { |
| 170 | json = {}; |
| 171 | } |
| 172 | |
| 173 | if (json.id) { |
| 174 | // commit record delete |
| 175 | $('#adminForm').append('<input type="hidden" name="option_deleted[]" value="' + json.id + '" />'); |
| 176 | } |
| 177 | |
| 178 | // save current costs of the option |
| 179 | TMP_COSTS = { |
| 180 | net: json.net ? parseFloat(json.net) : 0, |
| 181 | tax: json.tax ? parseFloat(json.tax) : 0, |
| 182 | gross: json.gross ? parseFloat(json.gross) : 0, |
| 183 | }; |
| 184 | |
| 185 | // refresh total prices |
| 186 | vapRefreshTotals({net: 0, tax: 0, gross: 0}); |
| 187 | |
| 188 | // auto delete fieldset |
| 189 | fieldset.remove(); |
| 190 | |
| 191 | // auto-close on delete |
| 192 | $(this).inspector('dismiss'); |
| 193 | }); |
| 194 | }); |
| 195 | |
| 196 | function vapOpenAppointmentCard(index) { |
| 197 | SELECTED_OPTION = 'order-app-fieldset-' + index; |
| 198 | |
| 199 | // open inspector |
| 200 | vapOpenInspector('order-app-inspector'); |
| 201 | } |
| 202 | |
| 203 | function vapOpenAppOptionCard(index) { |
| 204 | if (typeof index !== 'undefined') { |
| 205 | SELECTED_OPTION = 'order-app-opt-fieldset-' + index; |
| 206 | } else { |
| 207 | SELECTED_OPTION = null; |
| 208 | } |
| 209 | |
| 210 | // open inspector |
| 211 | vapOpenInspector('order-app-opt-inspector'); |
| 212 | } |
| 213 | |
| 214 | function vapRefreshTotals(data) { |
| 215 | // get totals |
| 216 | let totals = getInputTotals(); |
| 217 | |
| 218 | // get currency helper |
| 219 | currency = VAPCurrency.getInstance(); |
| 220 | |
| 221 | // add new total net |
| 222 | totals.total_net = currency.sum(totals.total_net, data.net); |
| 223 | // subtract previous one |
| 224 | totals.total_net = currency.diff(totals.total_net, TMP_COSTS.net); |
| 225 | |
| 226 | // add new total tax |
| 227 | totals.total_tax = currency.sum(totals.total_tax, data.tax); |
| 228 | // subtract previous one |
| 229 | totals.total_tax = currency.diff(totals.total_tax, TMP_COSTS.tax); |
| 230 | |
| 231 | // add new total gross |
| 232 | totals.total_cost = currency.sum(totals.total_cost, data.gross); |
| 233 | // subtract previous one |
| 234 | totals.total_cost = currency.diff(totals.total_cost, TMP_COSTS.gross); |
| 235 | |
| 236 | // commit changes |
| 237 | updateInputTotals(totals); |
| 238 | } |
| 239 | |
| 240 | </script> |
| 241 |