using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace Web.BuyOrder
{
public partial class ADDBuyOrder : System.Web.UI.Page
{
Model.BuyOrder buyOrder = new Model.BuyOrder();
Model.BuyOrderDetail buyOrderDetail = new Model.BuyOrderDetail();
BLL.B_BuyOrder buy = new BLL.B_BuyOrder();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
InitGriw();
this.txtBuyOrderid.Text = "buy" + DateTime.Now.ToString("yyyymmddhhmmss");
this.txtExecDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
}
}
#region//自定义添加列(添加临时表)
public DataTable Data
{
get { return ViewState["dt"] == null ? InitGriw() : (DataTable)ViewState["dt"]; }
set {ViewState["dt"]=value;}
}
public DataTable InitGriw()
{
DataTable dt = new DataTable();
dt.Columns.Add("商品编号", typeof(System.String));
dt.Columns.Add("商品名称", typeof(System.String));
dt.Columns.Add("供应商", typeof(System.String));
dt.Columns.Add("供应商编号", typeof(System.String));
dt.Columns.Add("数量", typeof(System.Int32));
dt.Columns.Add("单价", typeof(System.Decimal));
dt.Columns.Add("税率(%)", typeof(System.Double));
dt.Columns.Add("折扣率(%)", typeof(System.Double));
dt.Columns.Add("税额", typeof(System.Decimal));
dt.Columns.Add("折扣额", typeof(System.Decimal));
dt.Columns.Add("金额", typeof(System.Decimal));
this.Data = dt;
return dt;
}
public void bandView() //绑定
{
if (Data.Rows.Count > 0)
{
this.GridViewSave.DataSource = Data;
this.GridViewSave.DataBind();
}
else
{
this.GridViewSave.DataSource = null;
this.GridViewSave.DataBind();
}
}
public void TotalPrice()//总价
{
decimal total = 0m;
foreach (GridViewRow gr in this.GridViewSave.Rows)
{
total += Convert.ToDecimal(gr.Cells[11].Text);
}
this.txtTotalPrice.Text = total.ToString();
}
private void clear()//清空文本
{
this.ProductName.Value = "";
this.SupplierName.Value="";
this.Quantity.Value="";
this.Money.Value="";
this.TaxRate.Value="";
this.DiscountRate.Value="";
this.txtSescription.Text = "";
}
#endregion
#region//GridViewSave...的事件
protected void GridViewSave_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "color=this.style.backgroundColor;this.style.backgroundColor='#CCFFCC'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=color");
}
}
protected void GridViewSave_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Data.Rows[e.RowIndex].Delete();
bandView();
}
#endregion
#region//保存
protected void btnSave_Click(object sender, EventArgs e)
{
DataRow dr = this.Data.NewRow();
dr["商品编号"] = this.ProductID.Value;
dr["商品名称"] = this.ProductName.Value;
dr["供应商编号"] = this.SupplierID.Value;
dr["供应商"] = this.SupplierName.Value;
dr["数量"] = this.Quantity.Value;
dr["单价"] = this.Money.Value;
dr["税率(%)"] = this.TaxRate.Value;
dr["折扣率(%)"] = this.DiscountRate.Value;
dr["税额"] = Convert.ToDecimal(dr["单价"])*Convert.ToInt32(dr["数量"])*(Convert.ToDecimal(dr["税率(%)"])/100);
dr["折扣额"] = Convert.ToDecimal(dr["单价"])*Convert.ToInt32(dr["数量"])*(Convert.ToDecimal(dr["折扣率(%)"])/100);
dr["金额"] = Convert.ToDecimal(dr["单价"]) * Convert.ToInt32(dr["数量"]) + Convert.ToDecimal(dr["税额"]) - Convert.ToDecimal(dr["折扣额"]);
this.Data.Rows.Add(dr);
bandView();
decimal total = 0m;
foreach (GridViewRow gr in this.GridViewSave.Rows)
{
total += Convert.ToDecimal(gr.Cells[11].Text);
}
this.txtTotalPrice.Text = total.ToString();
clear();
}
#endregion
public static void AjaxAlert(System.Web.UI.Page page, string msg)
{
page.ClientScript.RegisterStartupScript(page.GetType(), "ajaxjs", string.Format("alert('{0}!');", msg), true);
}
#region//添加和完成所有
protected void btnAdd_Click(object sender, EventArgs e)
{
buyOrder.BuyOrderID = this.txtBuyOrderid.Text;
buyOrder.BuyOrderDate = Convert.ToDateTime( this.txtBuyOrderDate.Text);
buyOrder.BuyExecDate = Convert.ToDateTime( this.txtExecDate.Text);
buyOrder.TradeDate =Convert.ToDateTime( this.tztTradeDate.Text);
buyOrder.EmployeeID = this.EmployeeID.Value;
buyOrder.TradeAddress = this.txtTradeddress.Text;
buyOrder.SupplierID = this.SupplierID.Value;
buyOrder.TotalPrice =Convert.ToDecimal( this.txtTotalPrice.Text);
buyOrder.Auditing = 0;
if (buy.insertBuyOrder(buyOrder) != null)
{
foreach (GridViewRow gvr in GridViewSave.Rows)
{
buyOrderDetail.BuyOrderDetailID1 = "Detail" + DateTime.Now.ToString("yyyymmddhhmmss");
buyOrderDetail.BuyOrderID1 = this.txtBuyOrderid.Text;
buyOrderDetail.EmployeeID = this.EmployeeID.Value;
buyOrderDetail.BuyOrderDate1 =Convert.ToDateTime( DateTime.Now.ToString("yyyy-MM-dd"));
buyOrderDetail.SupplierID1 = gvr.Cells[4].Text.ToString();
buyOrderDetail.TotalPrice=Convert.ToDecimal(gvr.Cells[6].Text.ToString());
buyOrderDetail.Quantity=Convert.ToInt32(gvr.Cells[5].Text.ToString());
buyOrderDetail.TaxRate=Convert.ToDecimal(gvr.Cells[7].Text.ToString());
buyOrderDetail.DiscountRate=Convert.ToDecimal(gvr.Cells[8].Text.ToString());
buyOrderDetail.ProductID1 = gvr.Cells[1].Text.ToString();
buyOrderDetail.Description="";
if (BLL.B_BuyOrderDetail.insertBuyOrderDetail(buyOrderDetail)!=null)
{
AjaxAlert(this, "添加成功!");
}
this.btnAdd.Visible = false;
this.btnAll.Visible = true;
}
}
}
protected void btnAll_Click(object sender, EventArgs e)//完成所有
{
string buyOrderID = this.txtBuyOrderid.Text.ToString();
Response.Redirect("BuyOrdrDetail.aspx?BuyOrderID=" + buyOrderID + "", true);
}
#endregion
}
}