HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux ip-172-31-42-149 5.15.0-1084-aws #91~20.04.1-Ubuntu SMP Fri May 2 07:00:04 UTC 2025 aarch64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/vhost/disk-apps/agile-selling-wpb/public/js/discounts.js
/**
 * metodos para administrar descuentos
 * @diegoDuque
*/

function convertTime12to24(time12h) {
  const [time, modifier] = time12h.split(' ');

  let [hours, minutes] = time.split(':');

  if (hours === '12') {
    hours = '00';
  }

  if (modifier === 'PM') {
    hours = parseInt(hours, 10) + 12;
  }

  return hours + ':' + minutes;
}

function createDiscount(id) {
console.log('discount_id: ', id);
  $('#createDiscount'+id).validator('update');
  $("#createDiscount"+id).validator('update').on('submit', function (e) {

    if (e.isDefaultPrevented()) {} else {

      e.preventDefault();
      $("#btn-create-discount"+id).addClass('displayNone');
      $("#spinnerButton"+id).removeClass('displayNone');

      if (id == 1) {
        var info = {
          "discount_id" : id,
          "name": $(" #createDiscount"+id+" input[name='name"+id+"']").val(),
          "price_discount": $(" #createDiscount"+id+" input[name='price_discount"+id+"']").val(),
          "percentage_discount": $(" #createDiscount"+id+" input[name='percentage_discount"+id+"']").val(),
          "days_register_discount_delivery": $(" #createDiscount"+id+" input[name='days_register_discount_delivery"+id+"']").val(),
          "discount_global_shipping": $(" #createDiscount"+id+" input[name='discount_global_shipping"+id+"']").val()
        };
      }else{
        if (id == 2 || id == 3 || id == 4 || id == 5 || id == 8 || id == 10) {
          var price_discount = $("#createDiscount"+id+" input[name='price_discount"+id+"']").val();
          var percentage_discount = $("#createDiscount"+id+" input[name='percentage_discount"+id+"']").val();
          var value_for_win = $(" #createDiscount"+id+" input[name='value_for_win"+id+"']").val();

          if (price_discount == '' && percentage_discount == '') {
            swal("Ok", Lang.get("messagesClient.discount_tag4"), "warning");
            $("#btn-create-discount"+id).removeClass('displayNone');
            $("#spinnerButton"+id).addClass('displayNone');
            return false;
          }

          if (id== 4) {
            var code_coupon = $(" #createDiscount"+id+" input[name='code_coupon"+id+"']").val();
          }else{
            var code_coupon = null;
          }

          if (id==5) {
            var products = JSON.parse($(" #createDiscount"+id+" input[name='arrayProducts"+id+"']").val());
            var productsGift = null;
            if (products.length == 0) {
              swal("Ok", Lang.get("messagesClient.discount_tag6"), "warning");
              $("#btn-create-discount"+id).removeClass('displayNone');
              $("#spinnerButton"+id).addClass('displayNone');
              return false;
            }
          }else{
            var products = null;
            var productsGift = null;
          }

          if (id == 10) {
            var apply_value = $(" #createDiscount"+id+" select[name='apply_value"+id+"']").val();
            var category_id = $(" #createDiscount"+id+" select[name='category_id"+id+"']").val();
          }else{
            var apply_value = null;
            var category_id = null;
          }
        }

        if (id == 6 || id == 7 || id == 9) {
          var code_coupon = null;
          var apply_value = null;
          var category_id = null;
          if (id == 6) {

            var products = JSON.parse($(" #createDiscount"+id+" input[name='arrayProducts"+id+"']").val());
            var productsGift = JSON.parse($(" #createDiscount"+id+" input[name='arrayProductsGift"+id+"']").val());
            var price_discount = null;
            var percentage_discount = null;
            var value_for_win = null;
            if (products.length == 0 || productsGift.length == 0) {
              swal("Ok", Lang.get("messagesClient.discount_tag5"), "warning");
              $("#btn-create-discount"+id).removeClass('displayNone');
              $("#spinnerButton"+id).addClass('displayNone');
              return false;
            }

          }else if(id == 7){

            var products = null;
            var productsGift = JSON.parse($(" #createDiscount"+id+" input[name='arrayProductsGift"+id+"']").val());
            var price_discount = null;
            var percentage_discount = $("#createDiscount"+id+" input[name='percentage_discount"+id+"']").val();
            var value_for_win = null;
            if (productsGift.length == 0) {
              swal("Ok", Lang.get("messagesClient.discount_tag6"), "warning");
              $("#btn-create-discount"+id).removeClass('displayNone');
              $("#spinnerButton"+id).addClass('displayNone');
              return false;
            }

          }else if (id == 9) {

            var products = null;
            var productsGift = JSON.parse($(" #createDiscount"+id+" input[name='arrayProductsGift"+id+"']").val());
            var price_discount = $("#createDiscount"+id+" input[name='price_discount"+id+"']").val();
            var percentage_discount = $("#createDiscount"+id+" input[name='percentage_discount"+id+"']").val();
            var value_for_win = $(" #createDiscount"+id+" input[name='value_for_win"+id+"']").val();

            if (price_discount == '' && percentage_discount == '') {
              swal("Ok", Lang.get("messagesClient.discount_tag4"), "warning");
              $("#btn-create-discount"+id).removeClass('displayNone');
              $("#spinnerButton"+id).addClass('displayNone');
              return false;
            }
            if (productsGift.length == 0) {
              swal("Ok", Lang.get("messagesClient.discount_tag6"), "warning");
              $("#btn-create-discount"+id).removeClass('displayNone');
              $("#spinnerButton"+id).addClass('displayNone');
              return false;
            }

          }
        }

        var info = {
          "discount_id" : id,
          "name": $(" #createDiscount"+id+" input[name='name"+id+"']").val(),
          "value_for_win": value_for_win,
          "message_to_user": $(" #createDiscount"+id+" input[name='message_to_user"+id+"']").val(),
          "when_start": $(" #createDiscount"+id+" input[name='start_date"+id+"']").val()+ ' ' + convertTime12to24($(" #createDiscount"+id+" input[name='start_hour"+id+"']").val()),
          "when_finish": $(" #createDiscount"+id+" input[name='limit_date"+id+"']").val()+ ' ' + convertTime12to24($(" #createDiscount"+id+" input[name='limit_hour"+id+"']").val()),
          "max_winners": $(" #createDiscount"+id+" input[name='max_winners"+id+"']").val(),
          "code_coupon": code_coupon,
          "price_discount": price_discount,
          "percentage_discount": percentage_discount,
          "is_multiple_redeem" : $(" #createDiscount"+id+" select[name='is_multiple_redeem"+id+"']").val(),
          "is_cumulative_another_discounts" : $(" #createDiscount"+id+" select[name='is_cumulative_another_discounts"+id+"']").val(),
          "apply_value" : apply_value,
          "category_id" : category_id,
          "products" : products,
          "productsGift" : productsGift
        };
      }
      console.log('info: ', info);

      swal(Lang.get("messagesClient.discount_tag2"),
        {
          buttons: {
            cancel: "No",
            Ok: true,
          }
        }).then((val) => {
          if (val == "Ok") {
        $.ajax({
          url: "/discounts/create",
          type: "POST",
          contentType: 'application/json',
          headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
            'Content-Type': 'application/json'
          },
          data: JSON.stringify(info),
          success: function (r) {
            if (r.r) {
                swal(r.m, {
                  icon: "success",
                  buttons: {
                    Ok: true,
                  }
                }).then((val) => {
                  if (val == "Ok") {
                    $(location).attr('href', '/discounts');
                  }
                });
            }else{
              swal(r.m, "error");
              $("#btn-create-discount"+id).removeClass('displayNone');
              $("#spinnerButton"+id).addClass('displayNone');
            }
          }, error: function(e){
            swal(Lang.get("messagesClient.discount_tag3"), "error");
            $("#btn-create-discount"+id).removeClass('displayNone');
            $("#spinnerButton"+id).addClass('displayNone');
          }
        });
      }else{
        $("#btn-create-discount"+id).removeClass('displayNone');
        $("#spinnerButton"+id).addClass('displayNone');
      }
    });

    }
  });
}

function clickEditDiscount(value){
  $(location).attr('href', '/discounts/edit/' + value);
}

function editDiscount() {
  $('#editDiscount').validator('update');
  $("#editDiscount").validator('update').on('submit', function (e) {
    if (e.isDefaultPrevented()) {} else {
      e.preventDefault();
      $("#btn-edit-discount").addClass('displayNone');
      $("#spinnerButton").removeClass('displayNone');

      var discount_id = $("#btn-edit-discount").attr("data-id");
      var type_discount_id = $("#btn-edit-discount").attr("type-id");
      console.log('discount_id: ', discount_id);
      console.log('type_discount_id: ', type_discount_id);

      if (type_discount_id == 1) {
        var info = {
          "discount_id" : discount_id,
          "type_discount_id" : type_discount_id,
          "name": $(" #editDiscount input[name='name']").val(),
          "price_discount": $(" #editDiscount input[name='price_discount']").val(),
          "percentage_discount": $(" #editDiscount input[name='percentage_discount']").val(),
          "days_register_discount_delivery": $(" #editDiscount input[name='days_register_discount_delivery']").val(),
          "discount_global_shipping": $(" #editDiscount input[name='discount_global_shipping']").val()
        };
      }else{
        if(value_for_win = $(" #editDiscount input[name='value_for_win']").val()){}
        else{
          value_for_win = null;
        }
        if(price_discount = $("#editDiscount input[name='price_discount']").val()){}
        else{
          price_discount = null;
        }
        if(percentage_discount = $("#editDiscount input[name='percentage_discount']").val()){}
        else{
          percentage_discount = null;
        }
        if (code_coupon = $(" #editDiscount input[name='code_coupon']").val()) {}
        else {
          code_coupon = null;
        }
        if (apply_value = $(" #editDiscount select[name='apply_value']").val()) {}
        else {
          apply_value = null;
        }
        if (category_id = $(" #editDiscount select[name='category_id']").val()) {}
        else {
          category_id = null;
        }

        if($(" #editDiscount input[name='arrayProductsRequirement']").val()){
          products = JSON.parse($(" #editDiscount input[name='arrayProductsRequirement']").val());
        }else{
          products = null;
        }
        if($(" #editDiscount input[name='arrayProductsGift']").val()){
          productsGift = JSON.parse($(" #editDiscount input[name='arrayProductsGift']").val());
        }else{
          productsGift = null;
        }

        if (type_discount_id == 5 && products.length == 0 || type_discount_id == 6 && products.length == 0) {
          swal("Ok", Lang.get("messagesClient.discount_tag6"), "warning");
          $("#btn-edit-discount").removeClass('displayNone');
          $("#spinnerButton").addClass('displayNone');
          return false;
        }

        if (type_discount_id == 6 && productsGift.length == 0 || type_discount_id == 7 && productsGift.length == 0 || type_discount_id == 9 && productsGift.length == 0) {
          swal("Ok", Lang.get("messagesClient.discount_tag7"), "warning");
          $("#btn-edit-discount").removeClass('displayNone');
          $("#spinnerButton").addClass('displayNone');
          return false;
        }

        var info = {
          "discount_id" : discount_id,
          "type_discount_id" : type_discount_id,
          "name": $(" #editDiscount input[name='name']").val(),
          "value_for_win": value_for_win,
          "message_to_user": $(" #editDiscount input[name='message_to_user']").val(),
          "when_start": $(" #editDiscount input[name='start_date']").val()+ ' ' + convertTime12to24($(" #editDiscount input[name='start_hour']").val()),
          "when_finish": $(" #editDiscount input[name='limit_date']").val()+ ' ' + convertTime12to24($(" #editDiscount input[name='limit_hour']").val()),
          "max_winners": $(" #editDiscount input[name='max_winners']").val(),
          "code_coupon": code_coupon,
          "price_discount": price_discount,
          "percentage_discount": percentage_discount,
          "is_multiple_redeem" : $(" #editDiscount select[name='is_multiple_redeem']").val(),
          "is_cumulative_another_discounts" : $(" #editDiscount select[name='is_cumulative_another_discounts']").val(),
          "apply_value" : apply_value,
          "category_id" : category_id,
          "products" : products,
          "productsGift" : productsGift
        }

      }
      console.log('info: ', info);

        swal(Lang.get("messagesClient.discount_tag8"),
        {

          buttons: {
            cancel: "No",
            Ok: true,

          }
        }).then((val) => {
          if (val == "Ok") {
        $.ajax({
          url: "/discounts/update",
          type: "PUT",
          contentType: 'application/json',
          headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
            'Content-Type': 'application/json'
          },
          data: JSON.stringify(info),
          success: function (r) {
            if (r.r) {
                swal(r.m, {
                  icon: "success",
                  buttons: {
                    Ok: true,
                  }
                }).then((val) => {
                  if (val == "Ok") {
                    $(location).attr('href', '/discounts');
                  }
                });
            }else{
              swal(r.m, "error");
              $("#btn-edit-discount").removeClass('displayNone');
              $("#spinnerButton").addClass('displayNone');
            }
          }, error: function(e){
            swal(Lang.get("messagesClient.discount_tag9"), "error");
            $("#btn-edit-discount").removeClass('displayNone');
            $("#spinnerButton").addClass('displayNone');
          }
        });
      }else{
        $("#btn-edit-discount").removeClass('displayNone');
        $("#spinnerButton").addClass('displayNone');
      }
    });

    }
  });

}

/**Activar/Desactivar Discount usando switch*/
function chkDiscount(value) {
   var state;
   if($('#Checkactive'+value).is(':checked')){
     state = 1;
   }else{
     state = 0;
   }

   var info = {
     "id": value,
     "state": state
   };

   $.ajax({
   url: "/discounts/active",
   type: "POST",
   contentType: 'application/json',
   headers: {
     'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
     'Content-Type': 'application/json'
   },
   data: JSON.stringify(info),
   success: function (r) {
     if (r.r) {
       swal("Ok", r.m);
     } else {
       swal("Error",r.m, "error");
     }
   },error: function(e){
     swal("Error", Lang.get("messagesClient.banner_tag7"), "error");
   }
  });
}

function clickInfoDiscount(discount_id){
  $(location).attr('href', '/discounts/info/' + discount_id);
}