
function show_purchase_bill(json)
{
	$("#purchase_detail").find("tr:not(:first)").remove();
	if (!json)
	{
		return;
	}	
	data = json.data;
	if(!data || data.length == 0)
	{
		return;
	}
	g_token = json.token;
	for (var i = 0; i < data.length; i++) 
	{
		var tr = "<tr>";
		if (i % 2 == 0)
		{
			tr = "<tr class='alt'>";
		}

		var td = "<td>" + (i+1) + "</td>"
				+ "<td>" + data[i]["code"] + "</td>"
				+ "<td>" + data[i]["name"] + "</td>"
				+ "<td>" + data[i]["spec"] + "</td>"
				+ "<td>" + data[i]["amount"] + "</td>"
				+ "<td>" + data[i]["unit"] + "</td>"
				+ "<td>" + data[i]["notes"] + "</td>"
				+ "</tr>";
		$('#purchase_detail').append(tr+td);
	}
}

function get_purchase_bill_json(purchase_id) 
{
	var token = get_token();
    var json = {
        "service": "web_purchase/get_purchase_bill",
		"token": token,
		"params":
		{
			"purchase_id": purchase_id,
		}
    };
    return json;
}

function get_purchase_bill(purchase_id) 
{
	$.ajax(
	{
　　　　url : g_url, 　   <!-- url  :请求地址 -->  
　　　　type : 'post',　　　　　　　　<!--type ：请求类型:post/get --> 
		xhrFields: {
                withCredentials: true // 这里设置了withCredentials
            },
		async:false,
　　　　data : JSON.stringify(get_purchase_bill_json(purchase_id)),
	
	　　success:function(data)
		{
			var json = eval('(' + data + ')');
			show_purchase_bill(json);
	　　},
		error:function (request, status, error)
		{
//			alert(error);
	　　}
	});
}

function get_purchase_auth_json(purchase_id) 
{
	var token = get_token();
	var user_name = get_user_name();
    var json = {
        "service": "web_purchase/auth_purchase",
		"token": token,
		"params":
		{
			"purchase_id": purchase_id,
			"auditor": user_name
		}
    };
    return json;
}

function do_purchase_auth(purchase_id) 
{
	if(!confirm("真的要锁定采购计划吗？锁定后将不可修改"))
	{
		return;
	}
	var user_name=get_user_name();
	$.ajax(
	{
　　　　url : g_url, 　   <!-- url  :请求地址 -->  
　　　　type : 'post',　　　　　　　　<!--type ：请求类型:post/get --> 
		xhrFields: {
                withCredentials: true // 这里设置了withCredentials
            },
		async:false,
　　　　data : JSON.stringify(get_purchase_auth_json(purchase_id)),
	
	　　success:function(data)
		{
			var json = eval('(' + data + ')');
			 $("#"+purchase_id).html("已锁定");			
			 $("#A_"+purchase_id).text(user_name);	
	　　},
		error:function (request, status, error)
		{
//			alert(error);
	　　}
	});
}

function show_purchase(json)
{
	$("#customers").find("tr:not(:first)").remove();
	if (!json)
	{
		return;
	}	
	data = json.data;
	if(!data || data.length == 0)
	{
		return;
	}
	g_token = json.token;
	for (var i = 0; i < data.length; i++) 
	{
		var tr = "<tr>";
		if (i % 2 == 0)
		{
			tr = "<tr class='alt'>";
		}
		var id = data[i]["id"];
		var cust_name = data[i]["num"];
		var status = "<span id='" + id + "'><a href='#' onclick='do_purchase_auth(" + id + ")'>锁定计划</a></span>";
		if(data[i]["status"] > 0)
		{
			status = "已锁定";
		}
		var start_time = data[i]["create_time"]; //计划时间
		var end_time = data[i]["buy_time"]; //截止时间		
		
		var warn = "";
		var percent = data[i]["progress"];
		var cur_percent = get_cur_percent(get_date(start_time), get_date(end_time));
		var dif_str = "";
		if(cur_percent <=0 || cur_percent > 100) //没开始，或已经结束
		{
			if(cur_percent > 100 && percent < 99) //没结束
			{
				warn = " class='warn' ";
			}
		}
		else
		{
			var left_days = get_days(get_cur_date(), give_time); //剩余时间
			dif = cur_percent - percent;
			if(dif >=20 || left_days <=3) //落后10%
			{
				warn = " class='warn_info' "; //提醒
			}
		}
		
		var td = "<td>" + (i+1) + "</td>"
				+ "<td><a href='#' onclick=\"show_dlg(" + id + ",'" + cust_name + "')\">" + data[i]["num"] + "</a>" + "</td>"
				+ "<td>" + data[i]["order_num"] + "</td>"
				+ "<td>" + date_part(start_time) + "</td>"
				+ "<td>" + date_part(end_time) + "</td>"
				+ "<td" + warn +">" + percent.toFixed(1) + "%</td>"
				+ "<td><span id='A_" + id + "'>" + data[i]["buyer"] + "</span></td>"
				+ "<td>" + status + "</td>"
				+ "<td>" + data[i]["memo"] + "</td>"
				+ "</tr>";
		$('#customers').append(tr+td);
	}
}

function get_sch_purchase_json() 
{
	var token = get_token();
	var val=get_opt_val("ord_status");
	var service = "web_purchase/get_purchase_list";
	var end_time = $("#e_day").val() + " 23:59:59"; //结束时间
	if(val == "0")
	{
		service = "web_purchase/get_purchase_list_unauth";
	}
    var json = {
        "service": service,
		"token": token,
		"params":
		{
			"begin_time": $("#s_day").val(),
			"end_time": end_time
		}
    };
    return json;
}

function search_purchase() 
{
	$.ajax(
	{
　　　　url : g_url, 　   <!-- url  :请求地址 -->  
　　　　type : 'post',　　　　　　　　<!--type ：请求类型:post/get --> 
		xhrFields: {
                withCredentials: true // 这里设置了withCredentials
            },
		async:false,
　　　　data : JSON.stringify(get_sch_purchase_json()),
	
	　　success:function(data)
		{
			var json = eval('(' + data + ')');
			show_purchase(json);
			
	　　},
		error:function (request, status, error)
		{
//			alert(error);
	　　}
	});
}
function show_rpt(json)
{
	var opt=get_opt_val("ord_status");	
	if (!json)
	{
		return;
	}	
	data = json.data;
	
	g_token = json.token;
	
	$("#customers").empty();
	
	var tr = "", sum_fld = "", sum_title="",chart_title="";
	if(opt == "0")
	{
		chart_title="采购金额统计";
		sum_fld = "t_je";
		sum_title = "合计金额";
		tr = "<tr>"
			+"<th width='70px'>序号</th>"
			+"<th width='80px'>代码</th>"
			+"<th width='180px'>供应商名称</th>";
	}
	else
	{
		chart_title="采购数量统计";
		sum_title = "合计数量";
		sum_fld = "t_amount";
		tr = "<tr>"
			+"<th width='70px'>序号</th>"
			+"<th width='100px'>物料代码</th>"
			+"<th width='150px'>物料名称</th>"
			+"<th width='180px'>规格型号</th>"
			+"<th width='70px'>单位</th>";
	}
	tr += 
		 "<th width='60px'>1月</th>"
		+"<th width='60px'>2月</th>"
		+"<th width='60px'>3月</th>"
		+"<th width='60px'>4月</th>"
		+"<th width='60px'>5月</th>"
		+"<th width='60px'>6月</th>"
		+"<th width='60px'>7月</th>"
		+"<th width='60px'>8月</th>"
		+"<th width='60px'>9月</th>"
		+"<th width='60px'>10月</th>"
		+"<th width='60px'>11月</th>"
		+"<th width='60px'>12月</th>"
		+"<th width='80px'>" + sum_title + "</th>"
		+"</tr>";
	
	$('#customers').append(tr);
	
	if(!data || data.length == 0)
	{
		$('#customers').append("<tr><td colspan='18' align='center'>没有数据</td></tr>");
		rpt_chart("rpt_chart", chart_title, new Array(0,0,0,0,0,0,0,0,0,0,0,0));
		return;
	}
	
	for (var i = 0; i < data.length; i++) 
	{
		tr = "<tr>";
		if (i % 2 == 0)
		{
			tr = "<tr class='alt'>";
		}
		if(opt == "0")
		{
			tr += "<td>" + (i+1) + "</td>"
				+ "<td>" + data[i]["code"] + "</td>"
				+ "<td>" + data[i]["ent_name"] + "</td>";
		}
		else
		{
			tr += "<td>" + (i+1) + "</td>"
				+ "<td>" + data[i]["code"] + "</td>"
				+ "<td>" + data[i]["name"] + "</td>"
				+ "<td>" + data[i]["spec"] + "</td>"
				+ "<td>" + data[i]["unit"] + "</td>";
		}
				
		tr += "<td>" + data[i]["m1"] + "</td>"
			+ "<td>" + data[i]["m2"] + "</td>"
			+ "<td>" + data[i]["m3"] + "</td>"
			+ "<td>" + data[i]["m4"] + "</td>"
			+ "<td>" + data[i]["m5"] + "</td>"
			+ "<td>" + data[i]["m6"] + "</td>"
			+ "<td>" + data[i]["m7"] + "</td>"
			+ "<td>" + data[i]["m8"] + "</td>"
			+ "<td>" + data[i]["m9"] + "</td>"
			+ "<td>" + data[i]["m10"] + "</td>"
			+ "<td>" + data[i]["m11"] + "</td>"
			+ "<td>" + data[i]["m12"] + "</td>"
			+ "<td>" + data[i][sum_fld] + "</td>"
			+ "</tr>";
		$('#customers').append(tr);
	}
	var sum_data;
	if(opt == "0")
	{
		sum_data = sum_cols("customers", 3, 13);
	}
	else
	{
		sum_data = sum_cols("customers", 5, 13);
	}
	rpt_chart("rpt_chart", chart_title, sum_data);
}

function get_rpt_purchase_json() 
{
	var token = get_token();

	var val=get_opt_val("ord_status");	
	var year = $("#rpt_year").val();
	
	if(val == "0")
	{
	    var json = {
			"service": "web_purchase/rtp_supply",
			"token": token,
			"params":
			{
				"year": year
			}
		}
		return json;
	}
	else
	{
		var json = {
			"service": "web_purchase/rpt_mat",
			"token": token,
			"params":
			{
				"year": year,
				"min_price":$("#min_price").val(),
				"max_price":$("#max_price").val()
			}
		}
		return json;
    }
}

function rpt_purchase() 
{
	$.ajax(
	{
　　　　url : g_url, 　   <!-- url  :请求地址 -->  
　　　　type : 'post',　　　　　　　　<!--type ：请求类型:post/get --> 
		xhrFields: {
                withCredentials: true // 这里设置了withCredentials
            },
		async:false,
　　　　data : JSON.stringify(get_rpt_purchase_json()),
	
	　　success:function(data)
		{
			var json = eval('(' + data + ')');
			show_rpt(json);
			
	　　},
		error:function (request, status, error)
		{
//			alert(error);
	　　}
	});
}

