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

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