price-change.js 14.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
'use strict';
var $ = require('jquery'),
    common = require('../common/common');

var ENUM = {
    vipType: {
        1: '正常折扣',
        2: '统一折扣',
        3: '无折扣',
        4: '固定折扣',
        5: '自定义折扣'
    },
    gender: {
        1: '男',
        2: '女',
        3: '通用'
    }
}


new common.dropDown({
    el: '#shop',
    ajax: 'shopsRest'
});
new common.dropDown({
    el: '#supplier',
    ajax: 'querySupplier'
});
new common.dropDown({
    el: '#brand',
    ajax: 'brand'
});
new common.dropDown({
    el: '#cate',
    ajax: 'maxSort'
});
new common.dropDown({
    el: "#jit"
});
new common.dropDown({
    el: "#status"
});
new common.dropDown({
    el: "#stock"
});
new common.dropDown({
    el: "#sex"
});

var g = new common.grid({
    el: "#basicTable",
    parms: function() {

        return {
            product_skn: common.util.__input("skn"),
            shop_id: common.util.__input("shop"),
            brand_id: common.util.__input("brand"),
            supplier_id: common.util.__input("supplier"),
            is_jit: common.util.__input("jit"),
            is_auditing: common.util.__input("status"),
            stock: common.util.__input("stock"),
            gender: common.util.__input("sex"),
            max_sort_id: common.util.__input("cate")
        };
    },
    columns: [{
        display: 'SKN',
        name: 'product_skn'
    }, {
        display: '商品信息',
        name: 'info',
        render: function(item) {
            return '<p>名称:' + item.product_name + '</p><p>品牌:' + item.brand_name + '</p><p>类目:' + item.middle_sort_name + '/' + item.small_sort_name + '</p>';
        }
    }, {
        display: '供应商/店铺',
        name: 'supplier_name',
        render: function(item) {
            var html = '';

            if (item.supplier_name) {
                html += item.supplier_name;
                if (item.shop_name) {
                    html += '/';
                }
            }

            if (item.shop_name) {
                html += item.shop_name;
            }
            return html;
        }
    }, {
        display: '性别',
        name: 'gender',
        render: function(item) {
            return ENUM.gender[item.gender];
        }
    }, {
        display: '吊牌价',
        name: 'retail_price'
    }, {
        display: '销售价',
        name: 'sales_price'
    }, {
        display: 'VIP折扣类型',
        name: 'vip_discount_type',
        render: function(item) {
            return ENUM.vipType[item.vip_discount_type];
        }
    }, {
        display: 'VIP价',
        name: 'vip_price'
    }, {
        display: '白金',
        name: 'vip3_price'
    }, {
        display: '金卡',
        name: 'vip2_price'
    }, {
        display: '银卡',
        name: 'vip1_price'
    }, {
        display: '操作信息',
        name: 'operateInfo',
        render: function(item) {
            var html = '';
            if (item.founder_name) {
                html += '<p>' + item.founder_name + '</p>';
            }

            if (item.updateTime) {
                html += '<p>' + item.updateTime + '</p>';
            }
            return html;
        }
    }, {
        display: '操作',
        render: function(item) {
            return '<a href="javascript:;" class="btn btn-info btn-xs modify-btn" data-skn="' + item.product_skn + '">修改</a>'
        }
    }]
});

g.init($("#gridurl").val());

//单个变价
$('#basicTable').on('click', '.modify-btn', function() {
    var skn = $(this).data('skn'),
        that = this,
        $detail = $(that).parents('tr').next('.product-detail');

    if ($detail.size() == 0) {
        $(that).addClass('disabled');
    }

    if ($(that).data('detail')) {
        $detail.toggle();
        return;
    }



    $.ajax({
        type: 'POST',
        url: '/goods/getPrice',
        dataType: 'json',
        data: {
            param: skn
        }
    }).then(function(res) {
        var data = res.data.data;
        $(that).removeClass('disabled');

        $(that).data('detail', true).parents('tr')
            .after($(common.util.__template($("#template").html(), data)))
            .next('.product-detail').find('.skn-value').text(skn);

        var $wrap = $(that).parents('tr').next('.product-detail'),
            $price = $wrap.find('.sale-price'),
            $select = $wrap.find('.discount-select'),
            $vipPrice = $wrap.find('.vip-price'),
            $vip3Price = $wrap.find('.vip3-price'),
            $vip2Price = $wrap.find('.vip2-price'),
            $vip1Price = $wrap.find('.vip1-price'),
            $submitBtn = $wrap.find('.sure-modify');

        $select.val(data.vip_discount_type);

        var returnCoin = data.return_coin;  //返回yoho币
        var retailPrice = data.retail_price;  //吊牌价
        var salePrice = data.sales_price;  //销售价
        var discountType = data.vip_discount_type;  //折扣类型

        console.log("返回Yoho币数量:" + returnCoin);

        if(data.return_coin>0){
            $vipPrice.attr('disabled', true);
        }

        vipPrice();
        if(returnCoin > 0){  //yoho币已设置
            if(retailPrice == salePrice && discountType == 3){
                //销售价不可修改,折扣类型不能修改
                $price.attr('disabled', true);
                $select.attr('disabled', true).css("background","rgb(235, 235, 228)");
            }
        }

        $select.on('change', function() {
            vipPrice();
        });

        $vipPrice.on('blur', function() {
            vipPrice();
        });

        $price.on('blur', function() {
            vipPrice();
        });

        $submitBtn.on('click', function() {
            if(checkPriceModify(returnCoin, retailPrice, $price.val(), $select.val())){
                common.util.__ajax({
                    url: '/goods/updatePrice',
                    data: {
                        product_skn: skn,
                        sales_price: $price.val(),
                        vip_discount_type: $select.val(),
                        vip_price: $vipPrice.val(),
                        vip1_price: $vip1Price.val(),
                        vip2_price: $vip2Price.val(),
                        vip3_price: $vip3Price.val()
                    }
                }, function(res) {
                    if (res.code == 200) {
                        g.reload();
                    }
                })
            }
        });

        function vipPrice() {
            var price = $price.val(),
                vipPrice = $vipPrice.val(),
                selectVal = $select.val() - 1,
                priceArr = [];

            priceArr.push([price * 0.95, price * 0.9, price * 0.88]);
            priceArr.push([price * 0.95, price * 0.95, price * 0.95]);
            priceArr.push([price, price, price]);
            priceArr.push([$vipPrice.val(), $vipPrice.val(), $vipPrice.val()]);

            if (selectVal == 4) {
                $vip1Price.removeAttr('disabled');
                $vip2Price.removeAttr('disabled');
                $vip3Price.removeAttr('disabled');
            } else {
                $vip1Price.attr('disabled', true).val(Number(priceArr[selectVal][0]).toFixed(2));
                $vip2Price.attr('disabled', true).val(Number(priceArr[selectVal][1]).toFixed(2));
                $vip3Price.attr('disabled', true).val(Number(priceArr[selectVal][2]).toFixed(2));
            }
        }

        //修改校验
        function checkPriceModify(returnCoin, retailPrice, salePrice, discountType){
            if(returnCoin > 0){
                if(retailPrice != salePrice){
                    common.util.__tip("已设置Yoho币,销售价只能修改为吊牌价!");
                    return false;
                }
                if(discountType != 3){
                    common.util.__tip("已设置Yoho币,VIP折扣类型设置必须为:无折扣");
                    return false;
                }
            }
            return true;
        }
    });
});

//筛选
$("#filter-btn").click(function() {
    g.reload(1);
});

//回车筛选
$('#skn').on('keyup', function(event) {
    if (event.keyCode == 13) {
        $("#filter-btn").trigger('click');
    }
});

//全部
$("#all-btn").click(function() {
    location.href = '';
});

//批量导入按钮点击事件
$('#import-btn').on('click', function() {
    $('#basicTable').hide();
    $('#priceChangeSearch').hide();
    $('.bulk-import').show();
});

//返回列表
$('#return-btn').on('click', function() {
    $('.bulk-import').hide();
    $('#basicTable').show();
    $('#priceChangeSearch').show();
});

//批量导入
var successList = null,
    $error = $('#error-msg'),
    loadModal = null;
$('#upload-input').after('<div class="file-name btn btn-default">请选择文件</div>');
common.edit.ajaxfileupload("#upload-input", {
    params: {
        type: "price",
        __type: "batch-import"
    },
    onStart: function() {
        loadModal = common.dialog.load();
        $('.file-name').text($('#upload-input').val().replace("C:\\fakepath\\", ""));
    },
    onComplete: function(response) {
        console.log(response);
        if (loadModal) {
            loadModal.close();
        }
        if (response.code == 200) {
            var data = response.data,
                li = '';

            $('#priceTable').show();
            $('.success-wrap').hide();
            successList = data.successList;
            $.each(data.failFileReason, function(i, value) {
                li += '<li>' + value + '</li>';
            });
            $error.empty().html(li);
            p.init(successList);
        } else {
            common.util.__tip(response.message, 'warning');
        }
    }
});

var isFirst = false,
    tableHtml = '';
var p = new common.grid({
    el: "#priceTable",
    columns: [{
        display: 'SKN',
        render: function(item) {
            return item.newProductPriceBo.product_skn;
        }
    }, {
        display: '吊牌价',
        render: function(item) {
            return item.newProductPriceBo.retail_price + '<p style="color: #ccc;">' + item.oldProductPriceBo.retail_price + '</p>';
        }
    }, {
        display: '销售价',
        render: function(item) {
            var newPrice = item.newProductPriceBo,
                newPriceHtml = newPrice.sales_price;

            if (newPrice.sales_price < (newPrice.retail_price / 2)) {
                newPriceHtml = '<p style="color: red">' + newPrice.sales_price + '</p>';
            }

            return newPriceHtml + '<p style="color: #ccc;">' + item.oldProductPriceBo.sales_price + '</p>';
        }
    }, {
        display: 'VIP折扣类型',
        render: function(item) {
            return ENUM.vipType[item.newProductPriceBo.vip_discount_type] +
                '<p style="color: #ccc;">' + ENUM.vipType[item.oldProductPriceBo.vip_discount_type] + '</p>';
        }
    }, {
        display: 'VIP价',
        render: function(item) {
            var newPrice = item.newProductPriceBo,
                newPriceHtml = '';

            if (newPrice.vip_price) {
                newPriceHtml = newPrice.vip_price;

                if (newPrice.vip_price < (newPrice.retail_price / 2)) {
                    newPriceHtml = '<p style="color: red">' + newPrice.vip_price + '</p>';
                }
            } else {
                newPriceHtml = '-'
            }
            return newPriceHtml + '<p style="color: #ccc;">' + item.oldProductPriceBo.vip_price + '</p>';
        }
    }, {
        display: '白金价',
        render: function(item) {
            var newPrice = item.newProductPriceBo,
                newPriceHtml = newPrice.vip3_price;

            if (newPrice.vip3_price < (newPrice.retail_price / 2)) {
                newPriceHtml = '<p style="color: red">' + newPrice.vip3_price + '</p>';
            }

            return newPriceHtml + '<p style="color: #ccc;">' + item.oldProductPriceBo.vip3_price + '</p>';
        }
    }, {
        display: '金卡价',
        render: function(item) {
            var newPrice = item.newProductPriceBo,
                newPriceHtml = newPrice.vip2_price;

            if (newPrice.vip2_price < (newPrice.retail_price / 2)) {
                newPriceHtml = '<p style="color: red">' + newPrice.vip2_price + '</p>';
            }

            return newPriceHtml + '<p style="color: #ccc;">' + item.oldProductPriceBo.vip2_price + '</p>';
        }
    }, {
        display: '银卡价',
        render: function(item) {
            var newPrice = item.newProductPriceBo,
                newPriceHtml = newPrice.vip1_price;

            if (newPrice.vip1_price < (newPrice.retail_price / 2)) {
                newPriceHtml = '<p style="color: red">' + newPrice.vip1_price + '</p>';
            }

            return newPriceHtml + '<p style="color: #ccc;">' + item.oldProductPriceBo.vip2_price + '</p>';
        }
    }, {
        display: '操作',
        render: function(item) {
            return '<a class="btn btn-info delete-btn" data-index="' + item.__index + '" href="javascript:;">删除</a>';
        }
    }]
});

//全部删除按钮
$('#delete-all').on('click', function() {
    $('#priceTable').empty();
    $error.empty();
    successList.length = 0;
})

//批量变价列表单个删除
$('#priceTable').on('click', '.delete-btn', function() {
    //var index = p.rows[$(this).data('index')];
    successList.splice($(this).data('index'), 1);
    p.reload();
});

//确定变价
var path = ''; //确定变价后返回的excel名
$('#sure-change').on('click', function() {
    var arr = [],
        data = '';

    if (!successList || successList.length == 0) {
        alert('请上传批量变价文件');
        return;
    }
    console.log(successList);
    $.each(successList, function(i, value) {
        arr.push(value.newProductPriceBo);
    });

    data = JSON.stringify(arr);
    console.log(data);
    common.util.__ajax({
        url: '/goods/batchUpdatePrice',
        data: {
            batchList: data
        }
    }, function(res) {
        $('#priceTable').hide();
        $('.success-wrap').show();
        path = res.data;
        successList.length = 0;
    });
});

$('#download-btn').on('click', function() {
    location.href = $(this).data('domain') + '/batch/download?path=' + path;
});