default.php
5 days ago
default_rules.php
5 days ago
default_rules_modal.php
5 days ago
default_tax.php
5 days ago
index.html
5 days ago
default_rules_modal.php
380 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 | $vik = VAPApplication::getInstance(); |
| 15 | |
| 16 | ?> |
| 17 | |
| 18 | <div class="inspector-form" id="inspector-tax-rule-form"> |
| 19 | |
| 20 | <?php echo $vik->bootStartTabSet('taxrule', array('active' => 'taxrule_details')); ?> |
| 21 | |
| 22 | <!-- DETAILS --> |
| 23 | |
| 24 | <?php echo $vik->bootAddTab('taxrule', 'taxrule_details', JText::translate('VAPCUSTFIELDSLEGEND1')); ?> |
| 25 | |
| 26 | <div class="inspector-fieldset"> |
| 27 | |
| 28 | <!-- NAME - Text --> |
| 29 | |
| 30 | <?php echo $vik->openControl(JText::translate('VAPMANAGEOPTION2') . '*'); ?> |
| 31 | <input type="text" id="rule_name" class="required" /> |
| 32 | <?php echo $vik->closeControl(); ?> |
| 33 | |
| 34 | <!-- MODIFIER - Select --> |
| 35 | |
| 36 | <?php |
| 37 | $options = array( |
| 38 | JHtml::fetch('select.option', 1, JText::translate('VAPTAXAPPLY_OPT1')), |
| 39 | JHtml::fetch('select.option', 2, JText::translate('VAPTAXAPPLY_OPT2')), |
| 40 | ); |
| 41 | |
| 42 | echo $vik->openControl(JText::translate('VAPTAXAPPLY')); ?> |
| 43 | <select id="rule_apply"> |
| 44 | <?php echo JHtml::fetch('select.options', $options); ?> |
| 45 | </select> |
| 46 | <?php echo $vik->closeControl(); ?> |
| 47 | |
| 48 | <!-- OPERATION - Select --> |
| 49 | |
| 50 | <?php |
| 51 | $options = array(); |
| 52 | |
| 53 | // get list of supported math operators |
| 54 | foreach (VAPTaxFactory::getMathOperators() as $value => $text) |
| 55 | { |
| 56 | $options[] = JHtml::fetch('select.option', $value, $text); |
| 57 | } |
| 58 | |
| 59 | echo $vik->openControl(JText::translate('VAPTAXMATHOP')); ?> |
| 60 | <select id="rule_operator"> |
| 61 | <?php echo JHtml::fetch('select.options', $options); ?> |
| 62 | </select> |
| 63 | <?php echo $vik->closeControl(); ?> |
| 64 | |
| 65 | <!-- AMOUNT - Number --> |
| 66 | |
| 67 | <?php echo $vik->openControl(JText::translate('VAPMANAGESUBSCR2')); ?> |
| 68 | <input type="number" id="rule_amount" step="any" min="0" /> |
| 69 | <?php echo $vik->closeControl(); ?> |
| 70 | |
| 71 | <!-- TAX CAP - Number --> |
| 72 | |
| 73 | <?php |
| 74 | $help = $vik->createPopover(array( |
| 75 | 'title' => JText::translate('VAPTAXCAP'), |
| 76 | 'content' => JText::translate('VAPTAXCAP_HELP'), |
| 77 | )); |
| 78 | |
| 79 | echo $vik->openControl(JText::translate('VAPTAXCAP') . $help); ?> |
| 80 | <div class="input-prepend currency-field"> |
| 81 | <span class="btn"><?php echo VAPFactory::getCurrency()->getSymbol(); ?></span> |
| 82 | |
| 83 | <input type="number" id="rule_cap" step="any" min="0" /> |
| 84 | </div> |
| 85 | <?php echo $vik->closeControl(); ?> |
| 86 | |
| 87 | </div> |
| 88 | |
| 89 | <?php echo $vik->bootEndTab(); ?> |
| 90 | |
| 91 | <!-- BREAKDOWN --> |
| 92 | |
| 93 | <?php echo $vik->bootAddTab('taxrule', 'taxrule_breakdown', JText::translate('VAPTAXBREAKDOWN')); ?> |
| 94 | |
| 95 | <div style="display:none;" id="tax-bd-repeat"> |
| 96 | |
| 97 | <div class="inspector-repeatable-head"> |
| 98 | <span class="tax-bd-summary"> |
| 99 | <i class="fas fa-ellipsis-v big hndl" style="margin-right: 4px;"></i> |
| 100 | |
| 101 | <span class="badge badge-info bd-name"></span> |
| 102 | <span class="badge badge-important bd-amount"></span> |
| 103 | </span> |
| 104 | |
| 105 | <span> |
| 106 | <a href="javascript: void(0);" class="tax-rule-edit-bd no-underline"> |
| 107 | <i class="fas fa-pen-square big ok"></i> |
| 108 | </a> |
| 109 | |
| 110 | <a href="javascript: void(0);" class="tax-rule-trash-bd no-underline"> |
| 111 | <i class="fas fa-minus-square big no"></i> |
| 112 | </a> |
| 113 | </span> |
| 114 | </div> |
| 115 | |
| 116 | <div class="inspector-repeatable-body"> |
| 117 | |
| 118 | <!-- NAME - Text --> |
| 119 | |
| 120 | <?php echo $vik->openControl(JText::translate('VATTAXBDLABEL')); ?> |
| 121 | <input type="text" class="rule_breakdown_name" placeholder="<?php echo $this->escape(JText::translate('VATTAXBDPLACEHOLDER')); ?>" /> |
| 122 | <?php echo $vik->closeControl(); ?> |
| 123 | |
| 124 | <!-- AMOUNT - Number --> |
| 125 | |
| 126 | <?php echo $vik->openControl(JText::translate('VAPMANAGESUBSCR2')); ?> |
| 127 | <div class="input-append"> |
| 128 | <input type="number" class="rule_breakdown_amount" /> |
| 129 | |
| 130 | <span class="btn">%</span> |
| 131 | </div> |
| 132 | <?php echo $vik->closeControl(); ?> |
| 133 | |
| 134 | <input type="hidden" class="rule_breakdown_id" /> |
| 135 | |
| 136 | </div> |
| 137 | |
| 138 | </div> |
| 139 | |
| 140 | <div class="inspector-repeatable-container" id="tax-bd-pool"> |
| 141 | |
| 142 | </div> |
| 143 | |
| 144 | <!-- ADD TIME --> |
| 145 | |
| 146 | <?php echo $vik->openControl(''); ?> |
| 147 | <button type="button" class="btn" id="add-tax-breakdown"><?php echo JText::translate('VAPADD'); ?></button> |
| 148 | <?php echo $vik->closeControl(); ?> |
| 149 | |
| 150 | <?php echo $vik->bootEndTab(); ?> |
| 151 | |
| 152 | <?php echo $vik->bootEndTabSet(); ?> |
| 153 | |
| 154 | <input type="hidden" id="rule_id" value="" /> |
| 155 | |
| 156 | </div> |
| 157 | |
| 158 | <?php |
| 159 | JText::script('VAPSYSTEMCONFIRMATIONMSG'); |
| 160 | ?> |
| 161 | |
| 162 | <script> |
| 163 | |
| 164 | jQuery(function($) { |
| 165 | $('#rule_apply, #rule_operator').select2({ |
| 166 | minimumResultsForSeach: -1, |
| 167 | allowClear: false, |
| 168 | width: '100%', |
| 169 | }); |
| 170 | |
| 171 | $('#add-tax-breakdown').on('click', () => { |
| 172 | addTaxBreakdown(); |
| 173 | }); |
| 174 | |
| 175 | $('#tax-bd-pool').sortable({ |
| 176 | items: '.inspector-repeatable', |
| 177 | revert: false, |
| 178 | axis: 'y', |
| 179 | handle: '.hndl', |
| 180 | cursor: 'move', |
| 181 | }); |
| 182 | }); |
| 183 | |
| 184 | var ruleValidator = new VikFormValidator('#inspector-tax-rule-form'); |
| 185 | |
| 186 | function fillTaxRuleForm(data) { |
| 187 | // update name |
| 188 | if (data.name === undefined) { |
| 189 | data.name = ''; |
| 190 | } |
| 191 | |
| 192 | jQuery('#rule_name').val(data.name); |
| 193 | |
| 194 | ruleValidator.unsetInvalid(jQuery('#rule_name')); |
| 195 | |
| 196 | // update modifier |
| 197 | if (data.apply === undefined) { |
| 198 | data.apply = 1; |
| 199 | } |
| 200 | |
| 201 | jQuery('#rule_apply').select2('val', data.apply); |
| 202 | |
| 203 | // update operator |
| 204 | if (data.operator === undefined) { |
| 205 | data.operator = 'add'; |
| 206 | } |
| 207 | |
| 208 | jQuery('#rule_operator').select2('val', data.operator); |
| 209 | |
| 210 | // update amount |
| 211 | if (data.amount === undefined) { |
| 212 | data.amount = 0.0; |
| 213 | } |
| 214 | |
| 215 | jQuery('#rule_amount').val(data.amount); |
| 216 | |
| 217 | // update cap |
| 218 | if (data.cap === undefined) { |
| 219 | data.cap = 0.0; |
| 220 | } |
| 221 | |
| 222 | jQuery('#rule_cap').val(data.cap); |
| 223 | |
| 224 | // populate breakdown |
| 225 | if (data.breakdown === undefined) { |
| 226 | data.breakdown = []; |
| 227 | } |
| 228 | |
| 229 | jQuery('#tax-bd-pool').html(''); |
| 230 | |
| 231 | data.breakdown.forEach((bd) => { |
| 232 | addTaxBreakdown(bd); |
| 233 | }); |
| 234 | |
| 235 | // update ID |
| 236 | jQuery('#rule_id').val(data.id); |
| 237 | } |
| 238 | |
| 239 | function getTaxRuleData() { |
| 240 | var data = {}; |
| 241 | |
| 242 | // set ID |
| 243 | data.id = jQuery('#rule_id').val(); |
| 244 | |
| 245 | // set name |
| 246 | data.name = jQuery('#rule_name').val(); |
| 247 | |
| 248 | // set modifier |
| 249 | data.apply = jQuery('#rule_apply').val(); |
| 250 | |
| 251 | // set operator |
| 252 | data.operator = jQuery('#rule_operator').val(); |
| 253 | |
| 254 | // set amount |
| 255 | data.amount = parseFloat(jQuery('#rule_amount').val()); |
| 256 | data.amount = isNaN(data.amount) ? 0.0 : data.amount; |
| 257 | |
| 258 | // set cap |
| 259 | data.cap = parseFloat(jQuery('#rule_cap').val()); |
| 260 | data.cap = isNaN(data.cap) ? 0.0 : data.cap; |
| 261 | |
| 262 | // set breakdown |
| 263 | data.breakdown = []; |
| 264 | |
| 265 | // iterate forms |
| 266 | jQuery('#tax-bd-pool .inspector-repeatable').each(function() { |
| 267 | let tmp = {}; |
| 268 | |
| 269 | // retrieve breakdown ID |
| 270 | tmp.id = parseInt(jQuery(this).find('input.rule_breakdown_id').val()); |
| 271 | |
| 272 | // retrieve breakdown name |
| 273 | tmp.name = jQuery(this).find('input.rule_breakdown_name').val(); |
| 274 | |
| 275 | // retrieve breakdown amount |
| 276 | tmp.amount = parseFloat(jQuery(this).find('input.rule_breakdown_amount').val()); |
| 277 | tmp.amount = isNaN(tmp.amount) ? 0.0 : tmp.amount; |
| 278 | |
| 279 | // register breakdown only if not empty |
| 280 | if (tmp.name.length || tmp.amount != 0) { |
| 281 | data.breakdown.push(tmp); |
| 282 | } |
| 283 | }); |
| 284 | |
| 285 | return data; |
| 286 | } |
| 287 | |
| 288 | function addTaxBreakdown(data) { |
| 289 | if (typeof data !== 'object') { |
| 290 | data = {}; |
| 291 | } |
| 292 | |
| 293 | let form = jQuery('#inspector-tax-rule-form'); |
| 294 | |
| 295 | // get repeatable form of the inspector |
| 296 | var repeatable = jQuery(form).find('#tax-bd-repeat'); |
| 297 | // clone the form |
| 298 | var clone = jQuery('<div class="inspector-repeatable"></div>') |
| 299 | .append(repeatable.clone().html()); |
| 300 | |
| 301 | let nameInput = clone.find('input.rule_breakdown_name'); |
| 302 | |
| 303 | // set up breakdown name/label |
| 304 | if (typeof data.name !== 'undefined') { |
| 305 | nameInput.val(data.name); |
| 306 | |
| 307 | // auto-collapse existing blocks |
| 308 | clone.addClass('collapsed'); |
| 309 | } |
| 310 | |
| 311 | let amountInput = clone.find('input.rule_breakdown_amount'); |
| 312 | |
| 313 | // set up breakdown amount |
| 314 | if (typeof data.amount !== 'undefined') { |
| 315 | amountInput.val(data.amount); |
| 316 | } |
| 317 | |
| 318 | let idInput = clone.find('input.rule_breakdown_id'); |
| 319 | |
| 320 | // set up breakdown ID |
| 321 | idInput.val(data.id || getIncrementalBreakdownID()); |
| 322 | |
| 323 | // refresh head every time something changes |
| 324 | jQuery(nameInput).add(amountInput).on('change', function() { |
| 325 | let amount = parseFloat(jQuery(amountInput).val()); |
| 326 | |
| 327 | if (isNaN(amount) || amount <= 0) { |
| 328 | jQuery(amountInput).val(Math.max(1, jQuery('#rule_amount').val())); |
| 329 | } |
| 330 | |
| 331 | vapRefreshSummaryBreakdown(clone); |
| 332 | }); |
| 333 | |
| 334 | // set up summary head |
| 335 | vapRefreshSummaryBreakdown(clone); |
| 336 | |
| 337 | // handle delete button |
| 338 | clone.find('.tax-rule-trash-bd').on('click', () => { |
| 339 | if (confirm(Joomla.JText._('VAPSYSTEMCONFIRMATIONMSG'))) { |
| 340 | clone.remove(); |
| 341 | } |
| 342 | }); |
| 343 | |
| 344 | // handle edit button |
| 345 | clone.find('.tax-rule-edit-bd').on('click', () => { |
| 346 | clone.toggleClass('collapsed'); |
| 347 | }); |
| 348 | |
| 349 | // append the clone to the document |
| 350 | jQuery('#tax-bd-pool').append(clone); |
| 351 | |
| 352 | // start by focusing "name" input |
| 353 | nameInput.focus(); |
| 354 | } |
| 355 | |
| 356 | function vapRefreshSummaryBreakdown(block) { |
| 357 | // extract name from block |
| 358 | let name = block.find('input.rule_breakdown_name').val(); |
| 359 | |
| 360 | // extract amount from block |
| 361 | let amount = parseFloat(block.find('input.rule_breakdown_amount').val()); |
| 362 | amount = isNaN(amount) ? 0 : amount; |
| 363 | |
| 364 | // set badge within block head |
| 365 | block.find('.tax-bd-summary').find('.bd-name').text(name); |
| 366 | block.find('.tax-bd-summary').find('.bd-amount').text(amount + '%'); |
| 367 | } |
| 368 | |
| 369 | function getIncrementalBreakdownID() { |
| 370 | let max = 0; |
| 371 | |
| 372 | jQuery('#tax-bd-pool input.rule_breakdown_id').each(function() { |
| 373 | max = Math.max(max, parseInt(jQuery(this).val())); |
| 374 | }); |
| 375 | |
| 376 | return max + 1; |
| 377 | } |
| 378 | |
| 379 | </script> |
| 380 |