private void dgwBillingList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (sender is DataGridView)
            {
                DataGridViewCell cell = ((DataGridView)sender).CurrentCell;
                if (cell.ColumnIndex == ((DataGridView)sender).ColumnCount - 1)
                {
                    DialogResult result = MessageBox.Show("Bạn có muốn xóa phiếu thanh toán?",
                                                          "Xoá phiếu thanh toán",
                                                          MessageBoxButtons.YesNo,
                                                          MessageBoxIcon.Question);
                    if (result == DialogResult.Yes)
                    {
                        DataGridViewRow currentRow = dgwBillingList.Rows[e.RowIndex];

                        BillService        billService = new BillService();
                        int                id          = ObjectHelper.GetValueFromAnonymousType <int>(currentRow.DataBoundItem, "Id");
                        Bill               bill        = billService.GetBill(id);
                        CustomerLogService cls         = new CustomerLogService();
                        CustomerLog        cl          = cls.SelectCustomerLogByWhere(x => x.RecordCode == bill.BillCode).FirstOrDefault();
                        bool               kq          = true;
                        if (cl != null)
                        {
                            cls.DeleteCustomerLog(cl.Id);
                        }

                        if (!billService.DeleteBill(id) && kq)
                        {
                            MessageBox.Show("Hiện tại hệ thống đang có lỗi. Vui lòng thử lại sau!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        loadBillList();
                    }
                }
            }
        }
        private void dgwBillingList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (sender is DataGridView)
            {
                DataGridViewCell cell = ((DataGridView)sender).CurrentCell;
                if (cell.ColumnIndex == ((DataGridView)sender).ColumnCount - 1)
                {
                    DialogResult result = MessageBox.Show("Bạn có muốn xóa phiếu thanh toán?",
                    "Xoá phiếu thanh toán",
                     MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question);
                    if (result == DialogResult.Yes)
                    {
                        DataGridViewRow currentRow = dgwBillingList.Rows[e.RowIndex];

                        BillService billService = new BillService();
                        int id = ObjectHelper.GetValueFromAnonymousType<int>(currentRow.DataBoundItem, "Id");
                        Bill bill = billService.GetBill(id);
                        CustomerLogService cls = new CustomerLogService();
                        CustomerLog cl = cls.SelectCustomerLogByWhere(x => x.RecordCode == bill.BillCode).FirstOrDefault();
                        bool kq = true;
                        if(cl != null)
                            cls.DeleteCustomerLog(cl.Id);

                        if (!billService.DeleteBill(id) && kq)
                        {
                            MessageBox.Show("Hiện tại hệ thống đang có lỗi. Vui lòng thử lại sau!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        loadBillList();
                    }

                }

            }
        }
        private void DeleteOrder(DataGridViewCellEventArgs e)
        {
            DialogResult result = MessageBox.Show("Bạn muốn xóa đơn hàng này?", "Xoá đơn hàng", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result == DialogResult.Yes)
            {
                DataGridViewRow currentRow = dgwOrderList.Rows[e.RowIndex];

                OrderService orderService = new OrderService();
                int          id           = ObjectHelper.GetValueFromAnonymousType <int>(currentRow.DataBoundItem, "Id");
                Order        order        = orderService.GetOrder(id);
                DateTime     systime      = BaoHienRepository.GetBaoHienDBDataContext().GetSystemDate();

                #region CustomerLog

                CustomerLogService cls = new CustomerLogService();
                CustomerLog        cl  = cls.GetCustomerLog(order.OrderCode);
                bool kq = true;
                if (cl != null)
                {
                    kq = cls.DeleteCustomerLog(cl.Id);
                }

                #endregion

                #region ProductLog

                ProductLogService  productLogService = new ProductLogService();
                OrderDetailService orderDetailService = new OrderDetailService();
                List <OrderDetail> details = orderDetailService.SelectOrderDetailByWhere(x => x.OrderId == order.Id).ToList();
                ProductLog         pl, newpl;
                foreach (OrderDetail item in details)
                {
                    pl    = productLogService.GetProductLog(item.ProductId, item.AttributeId, item.UnitId);
                    newpl = new ProductLog()
                    {
                        ProductId    = item.ProductId,
                        AttributeId  = item.AttributeId,
                        UnitId       = item.UnitId,
                        BeforeNumber = pl.AfterNumber,
                        Amount       = item.NumberUnit,
                        AfterNumber  = pl.AfterNumber + item.NumberUnit,
                        RecordCode   = order.OrderCode,
                        Status       = BHConstant.DEACTIVE_STATUS,
                        Direction    = BHConstant.DIRECTION_IN,
                        UpdatedDate  = systime
                    };
                    productLogService.AddProductLog(newpl);
                }
                productLogService.DeactiveProductLog(order.OrderCode);

                #endregion

                #region EmployeeLog

                int salerId = (int)order.Customer.SalerId;
                if (salerId > 0)
                {
                    EmployeeLogService els      = new EmployeeLogService();
                    EmployeeLog        order_el = els.SelectEmployeeLogByWhere(x => x.RecordCode == order.OrderCode).FirstOrDefault();
                    if (order_el != null)
                    {
                        els.DeleteEmployeeLog(order_el.Id);
                    }
                }

                #endregion

                if (!orderService.DeleteOrder(id) && kq)
                {
                    MessageBox.Show("Hiện tại hệ thống đang có lỗi. Vui lòng thử lại sau!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                loadOrderList();
            }
        }
        private void DeleteOrder(DataGridViewCellEventArgs e)
        {
            DialogResult result = MessageBox.Show("Bạn muốn xóa đơn hàng này?", "Xoá đơn hàng", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                DataGridViewRow currentRow = dgwOrderList.Rows[e.RowIndex];

                OrderService orderService = new OrderService();
                int id = ObjectHelper.GetValueFromAnonymousType<int>(currentRow.DataBoundItem, "Id");
                Order order = orderService.GetOrder(id);
                DateTime systime = BaoHienRepository.GetBaoHienDBDataContext().GetSystemDate();

                #region CustomerLog

                CustomerLogService cls = new CustomerLogService();
                CustomerLog cl = cls.GetCustomerLog(order.OrderCode);
                bool kq = true;
                if (cl != null)
                    kq = cls.DeleteCustomerLog(cl.Id);

                #endregion

                #region ProductLog

                ProductLogService productLogService = new ProductLogService();
                OrderDetailService orderDetailService = new OrderDetailService();
                List<OrderDetail> details = orderDetailService.SelectOrderDetailByWhere(x => x.OrderId == order.Id).ToList();
                ProductLog pl, newpl;
                foreach (OrderDetail item in details)
                {
                    pl = productLogService.GetProductLog(item.ProductId, item.AttributeId, item.UnitId);
                    newpl = new ProductLog()
                    {
                        ProductId = item.ProductId,
                        AttributeId = item.AttributeId,
                        UnitId = item.UnitId,
                        BeforeNumber = pl.AfterNumber,
                        Amount = item.NumberUnit,
                        AfterNumber = pl.AfterNumber + item.NumberUnit,
                        RecordCode = order.OrderCode,
                        Status = BHConstant.DEACTIVE_STATUS,
                        Direction = BHConstant.DIRECTION_IN,
                        UpdatedDate = systime
                    };
                    productLogService.AddProductLog(newpl);
                }
                productLogService.DeactiveProductLog(order.OrderCode);

                #endregion

                #region EmployeeLog

                int salerId = (int)order.Customer.SalerId;
                if (salerId > 0)
                {
                    EmployeeLogService els = new EmployeeLogService(); 
                    EmployeeLog order_el = els.SelectEmployeeLogByWhere(x => x.RecordCode == order.OrderCode).FirstOrDefault();
                    if(order_el != null)
                        els.DeleteEmployeeLog(order_el.Id);
                }

                #endregion

                if (!orderService.DeleteOrder(id) && kq)
                {
                    MessageBox.Show("Hiện tại hệ thống đang có lỗi. Vui lòng thử lại sau!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                loadOrderList();
            }
        }