app/Plugin/ProductOption42/Resource/template/default/Product/option_js.twig line 1

Open in your IDE?
  1. {#
  2. * Plugin Name : ProductOption
  3. *
  4. * Copyright (C) BraTech Co., Ltd. All Rights Reserved.
  5. * http://www.bratech.co.jp/
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. #}
  10. {% block css %}
  11. <style type="text/css">
  12.     .datepicker-days th.dow:first-child,
  13.     .datepicker-days td:first-child {
  14.         color: #f00;
  15.     }
  16.     .datepicker-days th.dow:last-child,
  17.     .datepicker-days td:last-child {
  18.         color: #00f;
  19.     }
  20. </style>
  21. {% endblock %}
  22. {% block javascript %}
  23. <script src="{{ asset('../../plugin/ProductOption42/assets/js/jquery.plainmodal.min.js') }}"></script>
  24. <script>
  25. $(function() {
  26.     //デフォルトのデイトタイムピッカーが存在しない場合
  27.     if ($('[type="date"]').prop('type') !== 'date') {
  28.         $.getScript("{{ asset('assets/js/vendor/moment.min.js', 'admin') }}").done(function() {
  29.             $.getScript("{{ asset('assets/js/vendor/moment-with-locales.min.js', 'admin') }}").done(function() {
  30.                 $.getScript("{{ asset('assets/js/vendor/tempusdominus-bootstrap-4.min.js', 'admin') }}").done(function() {
  31.                     $('input[type=date]').datetimepicker({
  32.                         locale: '{{ eccube_config.locale }}',
  33.                         format: 'YYYY-MM-DD',
  34.                         useCurrent: false,
  35.                         buttons: {
  36.                             showToday: true,
  37.                             showClose: true
  38.                         }
  39.                     });
  40.                 });
  41.             });
  42.         });
  43.     }
  44. });
  45. var product_id;
  46. $(function() {
  47.     {% for ProductOption in ProductOptions %}
  48.     {% set Option = ProductOption.Option %}
  49.     {% set Product = ProductOption.Product %}
  50.         {% if Option.description_flg == constant('Plugin\\ProductOption42\\Entity\\OptionCategory::ON') %}
  51.             modal{{ Product.id }}_{{ Option.id }} = $('#option_description_{{ Product.id }}_{{ Option.id }}').plainModal();
  52.             $('#option_description_link_{{ Product.id }}_{{ Option.id }}').click(function() {
  53.                 modal{{ Product.id }}_{{ Option.id }}.plainModal('open');
  54.                 product_id = $(this).attr('data');
  55.                 return false;
  56.             });
  57.         {% endif %}
  58.     {% endfor %}
  59. });
  60. $(function() {
  61.     $("[id^=desc_btn_]").click(function(){
  62.         var form_id;
  63.         var id = $(this).attr('id').replace(/^desc_btn_/ig, '');
  64.         var ids = id.split('_');
  65.         if (eccube.hasOwnProperty('productsClassCategories')) {
  66.             form_id = product_id;
  67.         }else{
  68.             form_id = 1;
  69.         }
  70.         var $form = $("form[name=form"+form_id+"]");
  71.         func_submit($form,ids[0],ids[1]);
  72.         onOptionChange($form)
  73.     });
  74.     $("[name^=productoption]").change(function(){
  75.         $form = $(this).parents('form');
  76.         onOptionChange($form);
  77.     });
  78. });
  79. function func_submit($form,optionId, setValue) {
  80.     var $sele_option = $form.find("[name=productoption" + optionId + "]");
  81.     if($sele_option && $sele_option.length){
  82.         var kind = $sele_option.attr("type");
  83.         if(kind == 'radio'){
  84.             $sele_option.val([setValue]);
  85.         }else{
  86.             $sele_option.val(setValue);
  87.         }
  88.     }else{
  89.         var $sele_option = $form.find('[name="productoption' + optionId + '[]"]');
  90.         if($sele_option && $sele_option.length){
  91.             $sele_option.each(function(){
  92.                 if($(this).val() == setValue){
  93.                     $(this).prop('checked',true);
  94.                 }
  95.             });
  96.         }
  97.     }
  98.     $('#option_description_' + product_id + '_' + optionId).plainModal('close');
  99. }
  100. var optionPrice = {{ optionPrices|json_encode|raw }};
  101. var optionPoint = {{ optionPoints|json_encode|raw }};
  102. var optionMultiple = {{ optionMultiples|json_encode|raw }};
  103. var taxRules = {{ taxRules|json_encode|raw }};
  104. var default_class_id = {{ default_class_id|json_encode|raw }};
  105. function onOptionChange($form){
  106.     if(!$form.length){
  107.         return;
  108.     }
  109.     var optionPriceTotal = 0;
  110.     var optionPointTotal = 0;
  111.     var tax_rate = null;
  112.     var tax_rule = null;
  113.     var product_id = $form.find('input[name="product_id"]').val();
  114.     var $sele1 = $form.find('select[name=classcategory_id1]');
  115.     var $sele2 = $form.find('select[name=classcategory_id2]');
  116.     var classcat_id1 = $sele1.val() ? $sele1.val() : '__unselected';
  117.     var classcat_id2 = $sele2.val() ? $sele2.val() : '';
  118.     if (eccube.hasOwnProperty('productsClassCategories')) {
  119.         // 商品一覧時
  120.         classcat2 = eccube.productsClassCategories[product_id][classcat_id1]['#' + classcat_id2];
  121.     } else {
  122.         // 詳細表示時
  123.         classcat2 = eccube.classCategories[classcat_id1]['#' + classcat_id2];
  124.     }
  125.     $form.find("[id^=productoption]").each(function(){
  126.         var id = $(this).prop("id");
  127.         var name = $(this).attr("name");
  128.         var option_id = null;
  129.         if(name === undefined){
  130.             name = $(this).find("input").prop("id");
  131.         }
  132.         if(name === undefined)return;
  133.         option_id = name.replace(/productoption/ig,'');
  134.         option_id = option_id.replace(/_\d+/ig,'');
  135.         option_id = option_id.replace(/\[\]/ig,'');
  136.         if(id.match(/^productoption\d+$/)){
  137.             var kind = $(this).prop('tagName');
  138.             var type = $(this).attr('type');
  139.             var value = null;
  140.             var multi = 1;
  141.             switch(kind){
  142.                 case 'SELECT':
  143.                     value = $(this).val();
  144.                     break;
  145.                 case 'TEXTAREA':
  146.                 case 'INPUT':
  147.                     var text = $(this).val();
  148.                     if(text.length > 0){
  149.                         value = 0;
  150.                     }
  151.                     if(type == 'number'){
  152.                         if(optionMultiple[product_id][option_id]){
  153.                             multi = $(this).val();
  154.                             if(multi.length == 0)multi = 0;
  155.                         }
  156.                     }
  157.                     break;
  158.                 default:
  159.                     if($(this).find('input[type="checkbox"]').length > 0){
  160.                         value = [];
  161.                         $(this).find('input[type="checkbox"]:checked').each(function(){
  162.                             value.push($(this).val());
  163.                         });
  164.                     }else{
  165.                         value = $form.find("input[name='productoption" + option_id + "']:checked").val();
  166.                     }
  167.                     break;
  168.             }
  169.             if($.isNumeric(value)){
  170.                 if(Array.isArray(value)){
  171.                     for(var key in value){
  172.                         optionPriceTotal += parseFloat(optionPrice[product_id][option_id][value[key]]);
  173.                         optionPointTotal += parseFloat(optionPoint[product_id][option_id][value[key]]);
  174.                     }
  175.                 }else{
  176.                     optionPriceTotal += parseFloat(optionPrice[product_id][option_id][value]*multi);
  177.                     optionPointTotal += parseFloat(optionPoint[product_id][option_id][value]*multi);
  178.                 }
  179.             }
  180.         }
  181.     });
  182.     if(classcat2){
  183.         var product_class_id = classcat2.product_class_id;
  184.     }else{
  185.         var product_class_id = default_class_id[product_id];
  186.     }
  187.     var tax_rate = taxRules[product_class_id]['tax_rate'];
  188.     var tax_rule = taxRules[product_class_id]['tax_rule'];
  189.     var $option_price = $form.parent().find('#option_price_default').first();
  190.     $option_price.text(number_format(optionPriceTotal));
  191.     var $option_point = $form.parent().find('#option_price_inctax_default').first();
  192.     $option_point.text(number_format(optionPriceTotal + sfTax(optionPriceTotal, tax_rate, tax_rule)));
  193. }
  194. function number_format(num) {
  195.     return num.toString().replace(/([0-9]+?)(?=(?:[0-9]{3})+$)/g , '$1,');
  196. }
  197. function sfTax(price, tax_rate, tax_rule) {
  198.     real_tax = tax_rate / 100;
  199.     ret = price * real_tax;
  200.     tax_rule = parseInt(tax_rule);
  201.     switch (tax_rule) {
  202.         // 四捨五入
  203.         case {{ constant('Eccube\\Entity\\Master\\RoundingType::ROUND') }}:
  204.             $ret = Math.round(ret);
  205.             break;
  206.         // 切り捨て
  207.         case {{ constant('Eccube\\Entity\\Master\\RoundingType::FLOOR') }}:
  208.             $ret = Math.floor(ret);
  209.             break;
  210.         // 切り上げ
  211.         case {{ constant('Eccube\\Entity\\Master\\RoundingType::CEIL') }}:
  212.             $ret = Math.ceil(ret);
  213.             break;
  214.         // デフォルト:切り上げ
  215.         default:
  216.             $ret = Math.round(ret);
  217.             break;
  218.     }
  219.     return $ret;
  220. }
  221. </script>
  222. {% endblock %}