AlexProfi

alitvin_js

May 30th, 2017
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. (function ($) {
  3.     'use strict';
  4.  
  5.     var deliveryTab = $('#delivery'),
  6.         formTab = $('#info'),
  7.         payTab = $('#payment'),
  8.         b2cTab = $('.js-b2c'),
  9.         $zipcode = $('#id_zipcode'),
  10.         cartTab = $('#cart');
  11.  
  12.     var $deliveryPrice = $('.delivery_price'),
  13.         $productsPrice = $('#productsPrice'),
  14.         $lastDeliveryPrice = deliveryTab.find('form input[name="last_delivery_price"]');
  15.  
  16.     $(document).bind('deliveryRecountCart', function () {
  17.         withCartTotal(function (total) {
  18.             $productsPrice.text(total);
  19.             deliveryTab.find('#id_country').trigger('change');
  20.         });
  21.     });
  22.  
  23.     $zipcode.on('keyup', function () {
  24.         load_b2cpl($(this).val());
  25.     });
  26.  
  27.     function clear_delivery_b2c() {
  28.         $('#id_code').html('');
  29.         $('#b2c_loader .radio').remove();
  30.         $('#b2c_loader .name').remove();
  31.     }
  32.  
  33.     function load_b2cpl(val) {
  34.         if($('#id_dtype').val() == 'b2cpl') {
  35.  
  36.             if(val.length > 6) {
  37.                 val = val.substr(0, 6);
  38.                 $(this).val(val);
  39.             }
  40.             if(val.length == 6) {
  41.                 check_delivery(val);
  42.             }
  43.             else {
  44.                 clear_delivery_b2c();
  45.             }
  46.         }
  47.         else {
  48.             clear_delivery_b2c();
  49.         }
  50.     }
  51.  
  52.     function check_delivery(zip) {
  53.         if(zip != '' && zip.length == 6 && parseInt(zip)) {
  54.             $('#b2c_loader').remove();
  55.             if($('.b2c_hello_msg').length > 0) {
  56.                 $('.b2c_hello_msg').remove();
  57.             }
  58.             $('#id_code').prepend('<div id="b2c_loader" class=""><div class="radio"><img src="/static/core/img/icons/loader.gif" /></div></div>');
  59.             clear_delivery_b2c();
  60.  
  61.             withCartWeight(function (weight) {
  62.                 getDeliveries(zip, weight);
  63.             });        
  64.         }
  65.  
  66.         function getDeliveries(zip, weight) {
  67.             //get deliveries
  68.             $.ajax({
  69.                 url: Urls['cart:b2c_api'](),
  70.                 data: {
  71.                     func: 'tarif',
  72.                     type: '+post',
  73.                     zip: zip,
  74.                     weight: weight
  75.                 },
  76.                 success: function (data) {
  77.                     var price = 0;
  78.                     var b2c_options = '';
  79.                     if(data.flag_delivery) {
  80.                         add_delivery(data);
  81.                         $('[name="code"] option:first').trigger('change');
  82.                         $('[name="code"] option:first').attr('selected', 'selected');
  83.                         $('[name="code"] option:first').click();
  84.                     }
  85.                     else {
  86.                         //remove old records
  87.                         clear_delivery_b2c();
  88.  
  89.                         //error message
  90.                         $('#b2c_loader').append('<div class="radio"></div><div class="name" style="color:red">B2CPL доставка не доступна по указаному почтовому индексу</div>');
  91.                     }
  92.                 }
  93.             });
  94.         }
  95.     }
  96.  
  97.  
  98.     function add_delivery(data) {
  99.         var deliveries = '';
  100.         var deliveriesGroup1 = [];
  101.         var deliveriesGroup2 = [];
  102.         var deliveriesGroup3 = [];
  103.         var deliveriesList = [];
  104.         data = replace_delimetr(data);
  105.         var deliveryWays = data.delivery_ways;
  106.         deliveriesGroup1 = deliveryWays.filter(function (item) {
  107.             if(item.Код == 'курьер') {
  108.                 return item;
  109.             }
  110.         });
  111.         deliveriesGroup2 = deliveryWays.filter(function (item) {
  112.             if(item.Код.startsWith('пр')) {
  113.                 return item;
  114.             }
  115.         });
  116.         deliveriesGroup3 = deliveryWays.filter(function (item) {
  117.             if(item.Код.startsWith('пвз')) {
  118.                 return item;
  119.             }
  120.         });
  121.  
  122.         deliveriesList = deliveriesGroup1.concat(deliveriesGroup2, deliveriesGroup3);
  123.  
  124.         var deliveryName = '',
  125.             deliveryCode = '',
  126.             deliveryAddress = '',
  127.             deliveryCost = '',
  128.             selectedFirst = false,
  129.             selectText = ' selected="selected" ';
  130.         deliveriesList.forEach(function (deliveryWay) {
  131.  
  132.             deliveryName = deliveryWay.Наименование;
  133.             deliveryCode = deliveryWay.Код;
  134.             deliveryAddress = deliveryWay.Адрес;
  135.             deliveryCost = Math.round(deliveryWay.Стоимость);
  136.  
  137.             if(deliveryName == 'пвз') {
  138.                 deliveryName = deliveryCode;
  139.             }
  140.             if(deliveryAddress != undefined && deliveryAddress != '') {
  141.                 deliveryAddress = ' Адрес: ' + deliveryAddress;
  142.             }
  143.             if(selectedFirst) {
  144.                 selectText = '';
  145.             }
  146.  
  147.             deliveries += '<option' + selectText + ' value = "' + deliveryCode + '" data-price="' + deliveryCost + '">' + deliveryCost + ' руб. ' + deliveryName + deliveryAddress + '</option>';
  148.             selectedFirst = true;
  149.         });
  150.  
  151.         clear_delivery_b2c();
  152.         $('[name="code"]').append(deliveries);
  153.     }
  154.  
  155.  
  156.     function updateTotalPrice(value) {
  157.         var $totalPriceButtons = $('.cabinet_tabs').find('.totalPrice');
  158.         $totalPriceButtons.each(function () {
  159.             $(this).text(value);
  160.         });
  161.     }
  162.  
  163.     function withCartWeight(callback) {
  164.         $.ajax({
  165.             url: Urls['cart:get_weight'](),
  166.             data: {
  167.                 dtype: $('#id_dtype').val()
  168.             },
  169.             success: function (data) {
  170.                 callback(data['weight']);
  171.             }
  172.         });
  173.     }
  174.  
  175.     function withCartTotal(callback) {
  176.         $.ajax({
  177.             url: Urls['cart:get_total'](),
  178.             success: function (data) {
  179.                 callback(data['total']);
  180.             }
  181.         });
  182.     }
Advertisement
Add Comment
Please, Sign In to add comment