/**审核通过**/
var id=0;
var callbackreload=function(){window.location.reload();};
function audit(){
    if(!id || id==0 || id=='' || id==null){return false;}
    var url=APP+"Shop/InvoicingOrder/audit";
    var query=new Object();
    query.id=id;
    $.ajax({
        type: "get",
        url: url,
        data: query,
        success: function(data){
            if(data.status==1){
                $.showSuccess(data.msg,1,'hc',callbackreload,callbackreload);
            }else{
                $.showErr(data.msg,1,'hc',callbackreload,callbackreload);
            }
        }
    });
}

//改变商品进货数量
function change_num(obj){
    var num = parseFloat($(obj).val());//数量
    if (isNaN(num) || num <= 0) {num = 1;}
    //var stock = parseInt($(obj).parent().next().html());
    //if(stock <= 0) {
    //    $.showErr('没有库存,确认删除', 0, 'hc',function(){
    //        var del=$(obj).parent().parent().find('td a.del');
    //        delCurrentRows(del);
    //    });
    //}else {
    //    if (stock < num) {num=stock;$.showErr('库存不足', 0, 'hc');}
    if(num.toString().indexOf(".")>-1){
        if(num.toString().split(".")[1].length>3){$.showErr('商品数量不能超过三位小数',1,'hc');}
    }
    $(obj).val(parseFloat(num.toFixed(3)));
    var priceobj = $(obj).parent().next().find("input[name='supply_price[]']");
    var price = parseFloat(priceobj.val());//本店价
    var sum_price = parseFloat(num * price).toFixed(2);
    $(obj).parent().nextAll('.money').html(foramtmoney(sum_price));
    //}
    sum_money();//计算合计
}
//改变商品单价
function change_price(obj){
        var price=parseFloat($(obj).val());
    $(obj).val(parseFloat(price.toFixed(2)));
    var num=parseFloat($(obj).parent().prev().find("input[name='freight_num[]']").val());
    var money=parseFloat(price*num).toFixed(2);
    $(obj).parent().nextAll('.money').html(foramtmoney(money));
    sum_money();
}
//商品总数
function sum_count(is_return){
    var count = 0;
    $("input[name='freight_num[]']").each(function(){
        count= parseFloat(count) + parseFloat($(this).val());
    });
    if(is_return==true){return count;}
}
//合计
function sum_money(is_return){
   var sumprice = 0;
    $("input[name='freight_num[]']").each(function(){
        var count=parseFloat($(this).val());
        var money=parseFloat($(this).parent().next().find("input[name='supply_price[]']").val());
        sumprice= sumprice= parseFloat(sumprice) + parseFloat(count*money);
    });
    if(is_return==true){return sumprice;}
    else{$("#sum_money").html("订单总金额："+foramtmoney(sumprice));}
    
}
/**确认配货**/
function confirm_order(){
    if(!id || id==0 || id=='' || id==null){return false;}
    $("input[name='id']").val(id);
    var count=sum_count(true);
    var all_money=sum_money(true);
    $("input[name='count']").val(count);
    $("input[name='all_money']").val(all_money);
    var url=APP+"Shop/InvoicingOrder/confirm_order";
    var query=$("form[name='myform']").serialize();
    $.ajax({
        type: "post",
        url: url,
        data: query,
        success: function(data){
            if(data.status==1){
                $.showSuccess(data.msg,1,'hc',callbackreload,callbackreload);
            }else{
                $.showErr(data.msg,1,'hc',callbackreload,callbackreload);
            }
        }
    });
}
function cancel_order(type){
    if(!id || id==0 || id=='' || id==null){return false;}
    $("input[name='id']").val(id);
	// alert(type);
    var query=new Object();
    query.order_id=id;
	query.type=type;
    var url=APP+"Shop/InvoicingOrder/cancel_order";
    $.ajax({
        type: "post",
        url: url,
        data: query,
        success: function(data){
            if(data.status==1){
                $.showSuccess(data.msg,1,'hc',callbackreload,callbackreload);
            }else{
                $.showErr(data.msg,1,'hc',callbackreload,callbackreload);
            }
            
        }
    });
}
function separate(){
    if(!id || id==0 || id=='' || id==null){return false;}
    $("input[name='id']").val(id);
    var query=new Object();
    query=$("form[name='myform']").serialize();
    query.id=id;
    var url=APP+"Shop/InvoicingOrder/separate";
    $.ajax({
        type: "post",
        url: url,
        data: query,
        success: function(data){
            if(data.status==1){
                $.showSuccess(data.msg,1,'hc',function(){
                    window.location.href=data.url;
                },function(){
                    window.location.href=data.url;
                });
                
            }else{
                $.showErr(data.msg,1,'hc');
            }
        }
    });
}
function change_supplier(){
    var send_shop_id=$("select[name='send_shop_id']").val();
    if(send_shop_id==0){
        $(".show_shop").hide();
        $(".show_supplier").show();
    }else{
        $(".show_shop").show();
        $(".show_supplier").hide();
    }
}
window.onload = function(){
    var send_shop_id=$("select[name='send_shop_id']").val();
    var is_transfer=$("input[name='is_transfer']").val();
    if(send_shop_id>0 && is_transfer==0){
        $(".show_shop").show();
        $(".show_supplier").hide();
    } else if(is_transfer==1) {
        $(".show_shop").show();
        $(".show_supplier").hide();
    }else{
        $(".show_shop").hide();
        $(".show_supplier").show();
    }
}

function differ_confirm(differ_type){
    if(!id || id==0 || id=='' || id==null){return false;}
    $("input[name='id']").val(id);
    var query=new Object();
    query.id=id;
    query.differ_type=differ_type;
    var url=APP+"Shop/InvoicingOrder/differ_confirm";
    $.ajax({
        type: "post",
        url: url,
        data: query,
        success: function(data){
            if(data.status==1){
                $.showSuccess(data.msg,1,'hc',function(){
                    window.location.href=data.url;
                },function(){
                    window.location.href=data.url;
                });

            }else{
                $.showErr(data.msg,1,'hc');
            }
        }
    });
}
//格式化价格
function foramtmoney(num,len) {
    len = len > 0 && len <= 20 ? len : 2;
    var result = parseFloat(num);
    num=isNaN(result)?0:result;
    var numpow=Math.pow(10,len);
    result = Math.round(num * numpow) / numpow;//round四舍五入 ceil进一 floor舍去
    var s_x = result.toString();
    var pos_decimal = s_x.indexOf('.');
    if (pos_decimal < 0) {
       pos_decimal = s_x.length;
       s_x += '.';
    }
    while (s_x.length <= pos_decimal + len) {s_x += '0';}
    return s_x;
}