コード例 #1
0
    protected void btnSave_Click(object sender, EventArgs e)
    {
        bool status = false;
        InitBus();
        string err = ValidateInput();
        //Thêm khách hàng
        if (err == "")
        {
            try
            {
                if (ctBUS.GetByEmail(GetCustomerDTO().Email).Rows.Count > 0)
                {
                    CustomerID = int.Parse(ctBUS.GetByEmail(GetCustomerDTO().Email).Rows[0]["Id"].ToString());
                    ctBUS.tblCustomer_Update(GetCustomerDTO());
                }
                else
                {
                    CustomerID = ctBUS.tblCustomer_insert(GetCustomerDTO());
                }
                int GroupID = int.Parse(this.drlGroupMail.SelectedValue);
                DetailGroupDTO dgDTO = new DetailGroupDTO() { CustomerID = CustomerID, GroupID = GroupID };
                //Thêm vào nhóm mail
                if (dgBUS.GetByID(GroupID, CustomerID).Rows.Count == 0)
                {
                    dgBUS.tblDetailGroup_insert(dgDTO);
                }
                //Thêm vào hóa đơn
                if (oBUS.tblOrder_GetByID(GetOrderDTO().OrderID).Rows.Count > 0 || hdfUpdate.Value!="")
                {
                    status = GetOrderDTO().Status;
                    oBUS.tblOrder_updateStatus(GetOrderDTO().OrderID, status);
                }
                else
                {
                    oBUS.tblOrder_insert(GetOrderDTO());
                }
                // Thêm vào chi tiết hóa đơn
                DataTable tblProduct = (DataTable)Session["orderList"];
                if (tblProduct.Rows.Count > 0)
                {
                    OrderDatailDTO odDTO = new OrderDatailDTO();
                    for (int i = 0; i < tblProduct.Rows.Count; i++)
                    {
                        //Thêm từng sản phẩm vào chi tiết đơn hàng
                        odDTO.ProductID = int.Parse(tblProduct.Rows[i]["ProductID"].ToString());
                        odDTO.OrderID = txtOrderID.Text;
                        odDTO.ProductName = tblProduct.Rows[i]["ProductName"].ToString();
                        odDTO.DeliveryCode = tblProduct.Rows[i]["DeliveryCode"].ToString();
                        odDTO.UnitPrice = float.Parse(tblProduct.Rows[i]["UnitPrice"].ToString());
                        odDTO.Quantity = int.Parse(tblProduct.Rows[i]["Quantity"].ToString());
                        odDTO.Total = odDTO.Quantity * odDTO.UnitPrice;
                        odDTO.Note = tblProduct.Rows[i]["Note"].ToString();
                        odDTO.Size = "M";
                        int CateGoryID;
                        if (odBUS.tblOrderDetail_GetByID(txtOrderID.Text, odDTO.ProductID).Rows.Count > 0)
                        {
                            odBUS.tblOrderDetail_update(odDTO);
                        }
                        else
                        {
                            odBUS.tblOrderDetail_insert(odDTO);
                        }

                        if (status == true)
                        {
                            prBUS = new ProductBUS();
                            //Lấy nhóm của sản phẩm
                            CateGoryID = int.Parse(prBUS.GetByID(odDTO.ProductID).Rows[0]["Category"].ToString());
                            countBUS = new CountBuyBUS();
                            if (countBUS.GetByID(CustomerID, CateGoryID).Rows.Count > 0)
                            {
                                //Cập nhật số lần mua của khách hàng này theo nhóm sản phẩm
                                countBUS.tblCountBuy_UpdateCountBuy(CustomerID, CateGoryID);
                            }
                            else
                            {
                                //Thêm vào đếm lần mua theo nhóm
                                countBUS.tblCountBuy_insert(CustomerID, CateGoryID, 1);
                            }
                        }

                    }
                    //Cập nhật số lần mua cho khách hàng
                    if (status == true)
                    {
                        ctBUS = new CustomerBUS();
                        ctBUS.tblCustomer_UpdateCountBuy(CustomerID);
                    }
                    //Load lại thông tin khách hàng
                    Visible(false);
                    pnSuccess.Visible = true;
                    lblSuccess.Text = "Cập nhật thành công một đơn hàng";
                    LoadDefautlValue();
                    Session["orderList"] = null;
                    //Tạo mã hóa đơn tự động
                    AutoCreateID();
                }
            }
            catch (Exception ex)
            {
                Visible(false);
                pnError.Visible = true;
                lblError.Text = "Lỗi xảy ra trong quá trình nhập:" + ex.Message;
            }
        }
        else
        {
            Visible(false);
            pnError.Visible = true;
            lblError.Text = err;
        }
    }