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();
                    }
                }
            }
        }
示例#2
0
 void LoadReport()
 {
     modeReport   = 0;
     lbTotal.Text = "(VND) 0";
     if (cbmCustomers.SelectedValue != null)
     {
         int customerId = (int)cbmCustomers.SelectedValue;
         if (customerId != 0)
         {
             SetupColumnOneCustomer();
             modeReport    = 1;
             textForPrint  = "Từ ngày " + dtpFrom.Value.ToString(BHConstant.DATE_FORMAT) + " đến ngày " + dtpTo.Value.ToString(BHConstant.DATE_FORMAT);
             customerPrint = ((Customer)cbmCustomers.SelectedItem);
             double             total = 0.0;
             CustomerLogService customerLogService = new CustomerLogService();
             customerReports = customerLogService.GetReportsOfCustomer(customerId, dtpFrom.Value,
                                                                       dtpTo.Value.AddDays(1).Date, ref total);
             dgwStockEntranceList.DataSource = customerReports;
             lbTotal.Text = Global.formatVNDCurrencyText(total.ToString());
         }
         else
         {
             SetupColumnAllCustomers();
             double total = 0.0;
             textForPrint = "Từ ngày " + dtpFrom.Value.ToString(BHConstant.DATE_FORMAT) + " đến ngày " + dtpTo.Value.ToString(BHConstant.DATE_FORMAT);
             CustomerLogService customerLogService = new CustomerLogService();
             customersReports = customerLogService.GetReportsOfCustomers(dtpFrom.Value, dtpTo.Value.AddDays(1).Date, ref total);
             dgwStockEntranceList.DataSource = customersReports;
             setColorRow(4);
             lbTotal.Text = Global.formatVNDCurrencyText(total.ToString());
         }
     }
     else
     {
         MessageBox.Show("Không đủ thông tin để lập báo cáo!");
     }
 }
 void LoadReport()
 {
     modeReport = 0;
     lbTotal.Text = "(VND) 0";
     if (cbmCustomers.SelectedValue != null)
     {
         int customerId = (int)cbmCustomers.SelectedValue;
         if (customerId != 0)
         {
             SetupColumnOneCustomer();
             modeReport = 1;
             textForPrint = "Từ ngày " + dtpFrom.Value.ToString(BHConstant.DATE_FORMAT) + " đến ngày " + dtpTo.Value.ToString(BHConstant.DATE_FORMAT); 
             customerPrint = ((Customer)cbmCustomers.SelectedItem);
             double total = 0.0;
             CustomerLogService customerLogService = new CustomerLogService();
             customerReports = customerLogService.GetReportsOfCustomer(customerId, dtpFrom.Value, 
                 dtpTo.Value.AddDays(1).Date, ref total);
             dgwStockEntranceList.DataSource = customerReports;
             lbTotal.Text = Global.formatVNDCurrencyText(total.ToString());
         }
         else
         {
             SetupColumnAllCustomers();
             double total = 0.0;
             textForPrint = "Từ ngày " + dtpFrom.Value.ToString(BHConstant.DATE_FORMAT) + " đến ngày " + dtpTo.Value.ToString(BHConstant.DATE_FORMAT);
             CustomerLogService customerLogService = new CustomerLogService();
             customersReports = customerLogService.GetReportsOfCustomers(dtpFrom.Value, dtpTo.Value.AddDays(1).Date, ref total);
             dgwStockEntranceList.DataSource = customersReports;
             setColorRow(4);
             lbTotal.Text = Global.formatVNDCurrencyText(total.ToString());
         }
     }
     else
     {
         MessageBox.Show("Không đủ thông tin để lập báo cáo!");
     }
 }
        private bool saveData()
        {
            calculateTotal();
            if (validator1.Validate() && ValidateData())
            {
                if (cbxCustomer.SelectedValue == null || cbxCustomer.SelectedIndex <= 0)
                {
                    MessageBox.Show("Bạn cần có một khách hàng cho phiếu này!");
                    return false;
                }
                DialogResult dialogResult = Preview();
                if (dialogResult == DialogResult.OK)
                {
                    XKNumber = PrintPreview.XKNumber;
                    BHNumber = PrintPreview.BHNumber;
                    double discount = 0;
                    Double.TryParse(txtDiscount.WorkingText, out discount);
                    DateTime createdDate = BaoHienRepository.GetBaoHienDBDataContext().GetSystemDate();

                    double vat = 0;
                    Double.TryParse(txtVAT.WorkingText, out vat);
                    int userId = 0;
                    if (Global.CurrentUser != null)
                    {
                        userId = Global.CurrentUser.Id;
                    }
                    else
                    {
                        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);
                        return false;
                    }
                    if (order != null)//update
                    {
                        #region Update

                        order.CustId = (int)cbxCustomer.SelectedValue;
                        order.Discount = discount;
                        order.Note = txtNote.Text;
                        order.VAT = vat;
                        order.OrderCode = txtOrderCode.Text;
                        order.Reason = txtReason.Text;
                        order.WareHouse = txtWare.Text;
                        order.Total = totalWithTax;
                        order.UpdatedDate = createdDate;

                        #region Update Order Detail

                        string msg = "";
                        int error = 0, amount_change = 0;
                        ProductLog pl, newpl;
                        OrderDetail prd;

                        // Check old data

                        OrderDetailService orderDetailService = new OrderDetailService();
                        List<OrderDetail> deleted_details = old_orderDetails.Where(x => !orderDetails.Select(y => y.ProductId.ToString() + '_' +
                            y.AttributeId.ToString() + '_' + y.UnitId.ToString()).Contains(x.ProductId.ToString() + '_' +
                            x.AttributeId.ToString() + '_' + x.UnitId.ToString())).ToList();
                        List<OrderDetail> updated_details = old_orderDetails.Where(x => orderDetails.Select(y => y.ProductId.ToString() + '_' +
                            y.AttributeId.ToString() + '_' + y.UnitId.ToString()).Contains(x.ProductId.ToString() + '_' +
                            x.AttributeId.ToString() + '_' + x.UnitId.ToString())).ToList();
                        List<OrderDetail> new_details = orderDetails.Where(x => !old_orderDetails.Select(y => y.ProductId.ToString() + '_' +
                            y.AttributeId.ToString() + '_' + y.UnitId.ToString()).Contains(x.ProductId.ToString() + '_' +
                            x.AttributeId.ToString() + '_' + x.UnitId.ToString())).ToList();
                        foreach (OrderDetail item in updated_details)
                        {
                            pl = productLogService.GetProductLog(item.ProductId, item.AttributeId, item.UnitId);
                            prd = orderDetails.Where(x => x.ProductId == item.ProductId && x.AttributeId == item.AttributeId &&
                                x.UnitId == item.UnitId).FirstOrDefault();
                            amount_change = Convert.ToInt32(prd.NumberUnit - item.NumberUnit);
                            if (amount_change > 0 && pl.AfterNumber - amount_change < 0) // Tang nguyen lieu
                            {
                                if (error == 0)
                                {
                                    msg += "Những sản phẩm sau đã bị SỬA nhưng không đảm bảo dữ liệu trong kho:\n";
                                    error = 1;
                                }
                                msg += "- " + productLogService.GetNameOfProductLog(pl) + " : " + amount_change.ToString() + "\n";
                            }
                        }
                        foreach (OrderDetail item in new_details)
                        {
                            pl = productLogService.GetProductLog(item.ProductId, item.AttributeId, item.UnitId);
                            if (pl.AfterNumber - item.NumberUnit < 0)
                            {
                                if (error < 2)
                                {
                                    msg += "Những sản phẩm sau không đủ số lượng để tạo phiếu bán hàng:\n";
                                    error = 2;
                                }
                                msg += "- " + productLogService.GetNameOfProductLog(pl) + " còn : " + pl.AfterNumber + "\n";
                            }
                        }

                        if (error > 0)
                        {
                            MessageBox.Show(msg, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return false;
                        }

                        foreach (OrderDetail item in deleted_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.ACTIVE_STATUS,
                                Direction = BHConstant.DIRECTION_IN,
                                UpdatedDate = createdDate
                            };
                            productLogService.AddProductLog(newpl);
                        }
                        foreach (OrderDetail od in orderDetails)
                        {
                            od.OrderId = order.Id;
                            if (od.ProductId > 0 && od.AttributeId > 0 && od.UnitId > 0)
                            {
                                totalCommission += od.Commission;
                                OrderDetail tmp_ode = old_orderDetails.Where(x => x.ProductId == od.ProductId &&
                                    x.AttributeId == od.AttributeId && x.UnitId == od.UnitId && x.OrderId == order.Id).FirstOrDefault();
                                if (tmp_ode != null)
                                {
                                    double amount = tmp_ode.NumberUnit - od.NumberUnit;
                                    bool ret = orderDetailService.UpdateOrderDetail(od);
                                    //Save in Production Log
                                    if (amount != 0)
                                    {
                                        pl = productLogService.GetProductLog(od.ProductId, od.AttributeId, od.UnitId);
                                        newpl = new ProductLog()
                                        {
                                            ProductId = od.ProductId,
                                            AttributeId = od.AttributeId,
                                            UnitId = od.UnitId,
                                            BeforeNumber = pl.AfterNumber,
                                            Amount = Math.Abs(amount),
                                            AfterNumber = pl.AfterNumber + amount,
                                            RecordCode = order.OrderCode,
                                            Status = BHConstant.ACTIVE_STATUS,
                                            Direction = amount > 0 ? BHConstant.DIRECTION_IN : BHConstant.DIRECTION_OUT,
                                            UpdatedDate = createdDate
                                        };
                                        productLogService.AddProductLog(newpl);
                                    }
                                }
                                else
                                {
                                    bool ret = (od.Id != null && od.Id > 0) ? orderDetailService.UpdateOrderDetail(od) 
                                        : orderDetailService.AddOrderDetail(od);
                                    //Save in Production Log
                                    pl = productLogService.GetProductLog(od.ProductId, od.AttributeId, od.UnitId);
                                    newpl = new ProductLog()
                                    {
                                        ProductId = od.ProductId,
                                        AttributeId = od.AttributeId,
                                        UnitId = od.UnitId,
                                        BeforeNumber = pl.AfterNumber,
                                        Amount = od.NumberUnit,
                                        AfterNumber = pl.AfterNumber - od.NumberUnit,
                                        RecordCode = order.OrderCode,
                                        Status = BHConstant.ACTIVE_STATUS,
                                        Direction = BHConstant.DIRECTION_OUT,
                                        UpdatedDate = createdDate
                                    };
                                    productLogService.AddProductLog(newpl);
                                }
                            }
                        }

                        OrderService orderService = new OrderService();
                        bool result = orderService.UpdateOrder(order);

                        #endregion

                        #region KH & NV

                        CustomerLogService cls = new CustomerLogService();
                        CustomerLog newest = cls.GetCustomerLog(order.OrderCode);
                        if (newest != null)
                        {
                            newest.Amount = totalWithTax;
                            cls.UpdateCustomerLog(newest);
                        }

                        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)
                            {
                                order_el.Amount = totalCommission;
                                els.UpdateEmployeeLog(order_el);
                            }
                        }

                        #endregion

                        if (result)
                        {
                            MessageBox.Show("Phiếu bán hàng đã được cập nhật thành công");
                            this.Close();
                            return true;
                        }
                        else
                        {
                            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);
                            return false;
                        }

                        #endregion
                    }
                    else//add new
                    {
                        #region Create New

                        ProductLog pl, newpl;
                        string msg = "";
                        int error = 0;
                        foreach (OrderDetail item in orderDetails)
                        {
                            pl = productLogService.GetProductLog(item.ProductId, item.AttributeId, item.UnitId);
                            if (pl.AfterNumber - item.NumberUnit < 0)
                            {
                                if (error == 0)
                                {
                                    msg += "Những sản phẩm sau không đủ số lượng để tạo phiếu bán hàng:\n";
                                    error = 1;
                                }
                                msg += "- " + productLogService.GetNameOfProductLog(pl) + " còn : " + pl.AfterNumber + "\n";
                            }
                        }

                        if (error > 0)
                        {
                            MessageBox.Show(msg, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return false;
                        }

                        SeedService ss = new SeedService();
                        order = new Order
                        {
                            CustId = cbxCustomer.SelectedValue != null ? (int)cbxCustomer.SelectedValue : 0,
                            Discount = discount,
                            Note = txtNote.Text,
                            VAT = vat,
                            OrderCode = ss.AddSeedID(BHConstant.PREFIX_FOR_ORDER),
                            CreatedDate = createdDate,
                            UserId = userId,
                            Reason = txtReason.Text,
                            WareHouse = txtWare.Text,
                            Total = totalWithTax
                        };
                        OrderService orderService = new OrderService();
                        bool result = orderService.AddOrder(order);
                        long newOrderId = -1;
                        try
                        {
                            newOrderId = order.Id;// BaoHienRepository.GetMaxId<Order>();
                        }
                        catch
                        {
                            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);
                            return false;
                        }

                        #region New Order Detail

                        OrderDetailService orderDetailService = new OrderDetailService();
                        foreach (OrderDetail od in orderDetails)
                        {
                            if (od.ProductId > 0 && od.AttributeId > 0 && od.UnitId > 0)
                            {
                                od.OrderId = (int)newOrderId;
                                bool ret = orderDetailService.AddOrderDetail(od);
                                totalCommission += od.Commission;

                                //Save in Production Log
                                pl = productLogService.GetProductLog(od.ProductId, od.AttributeId, od.UnitId);
                                newpl = new ProductLog()
                                {
                                    ProductId = od.ProductId,
                                    AttributeId = od.AttributeId,
                                    UnitId = od.UnitId,
                                    BeforeNumber = pl.AfterNumber,
                                    Amount = od.NumberUnit,
                                    AfterNumber = pl.AfterNumber - od.NumberUnit,
                                    RecordCode = order.OrderCode,
                                    Status = BHConstant.ACTIVE_STATUS,
                                    Direction = BHConstant.DIRECTION_OUT,
                                    UpdatedDate = createdDate
                                };
                                productLogService.AddProductLog(newpl);
                            }
                        }

                        #endregion

                        #region KH & NV

                        CustomerLogService cls = new CustomerLogService();
                        CustomerLog cl = new CustomerLog
                        {
                            CustomerId = order.CustId,
                            RecordCode = order.OrderCode,
                            Amount = totalWithTax,
                            Direction = BHConstant.DIRECTION_OUT,
                            CreatedDate = createdDate
                        };
                        result = cls.AddCustomerLog(cl);

                        int salerId = (int)order.Customer.SalerId;
                        if (salerId > 0)
                        {
                            EmployeeLogService els = new EmployeeLogService();
                            EmployeeLog newel = new EmployeeLog
                            {
                                EmployeeId = salerId,
                                RecordCode = order.OrderCode,
                                Amount = totalCommission,
                                CreatedDate = createdDate
                            };
                            result = els.AddEmployeeLog(newel);
                        }

                        #endregion

                        if (result)
                        {
                            MessageBox.Show("Phiếu bán hàng đã được tạo thành công");
                            this.Close();
                            return true;
                        }
                        else
                        {
                            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);
                            return false;
                        }
                        #endregion
                    }
                }
                return false;
            }
            else
            {
                MessageBox.Show("Vui lòng kiểm tra các thông tin cần thiết!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }
        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();
            }
        }
示例#7
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (validator1.Validate() && cbxCustomer.SelectedValue != null && cbxCustomer.SelectedIndex > 0)
            {
                BillService billService = new BillService();
                if (bill == null)
                {
                    double amount    = 0;
                    string amountStr = string.IsNullOrEmpty(txtAmount.WorkingText) ? txtAmount.Text : txtAmount.WorkingText;
                    double.TryParse(amountStr, out amount);
                    DateTime systime = BaoHienRepository.GetBaoHienDBDataContext().GetSystemDate();
                    int      userId  = 0;
                    if (Global.CurrentUser != null)
                    {
                        userId = Global.CurrentUser.Id;
                    }
                    else
                    {
                        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);
                        return;
                    }

                    SeedService ss = new SeedService();
                    bill = new Bill
                    {
                        BillCode    = ss.AddSeedID(BHConstant.PREFIX_FOR_BILLING),
                        Note        = txtNote.Text,
                        CreatedDate = systime,
                        Amount      = amount,
                        CustId      = cbxCustomer.SelectedValue != null ? (int)cbxCustomer.SelectedValue : 0,
                        UserId      = userId
                    };

                    bool result            = billService.AddBill(bill);
                    CustomerLogService cls = new CustomerLogService();
                    CustomerLog        cl  = new CustomerLog
                    {
                        CustomerId  = bill.CustId,
                        RecordCode  = bill.BillCode,
                        Amount      = bill.Amount,
                        Direction   = BHConstant.DIRECTION_IN,
                        CreatedDate = systime
                    };
                    result = cls.AddCustomerLog(cl);
                    if (result)
                    {
                        MessageBox.Show("Phiếu thanh toán đã được thêm!");
                    }
                    else
                    {
                        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);
                        return;
                    }
                }
                if (this.CallFromUserControll != null && this.CallFromUserControll is BillList)
                {
                    ((BillList)this.CallFromUserControll).loadBillList();
                }
                this.Close();
                return;
            }
            MessageBox.Show("Vui lòng kiểm tra các thông tin cần thiết!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
        }
        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 btnSave_Click(object sender, EventArgs e)
        {
            if (validator1.Validate() && cbxCustomer.SelectedValue != null && cbxCustomer.SelectedIndex > 0)
            {
                BillService billService = new BillService();
                if (bill == null)
                {
                    double amount = 0;
                    string amountStr = string.IsNullOrEmpty(txtAmount.WorkingText) ? txtAmount.Text : txtAmount.WorkingText;
                    double.TryParse(amountStr, out amount);
                    DateTime systime = BaoHienRepository.GetBaoHienDBDataContext().GetSystemDate();
                    int userId = 0;
                    if (Global.CurrentUser != null)
                    {
                        userId = Global.CurrentUser.Id;
                    }
                    else
                    {
                        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);
                        return;
                    }
                    
                    SeedService ss = new SeedService();
                    bill = new Bill
                    {
                        BillCode = ss.AddSeedID(BHConstant.PREFIX_FOR_BILLING),
                        Note = txtNote.Text,
                        CreatedDate = systime,
                        Amount = amount,
                        CustId = cbxCustomer.SelectedValue != null ? (int)cbxCustomer.SelectedValue : 0,
                        UserId = userId
                    };

                    bool result = billService.AddBill(bill);
                    CustomerLogService cls = new CustomerLogService();
                    CustomerLog cl = new CustomerLog
                    {
                        CustomerId = bill.CustId,
                        RecordCode = bill.BillCode,
                        Amount = bill.Amount,
                        Direction = BHConstant.DIRECTION_IN,
                        CreatedDate = systime
                    };
                    result = cls.AddCustomerLog(cl);
                    if (result)
                    {
                        MessageBox.Show("Phiếu thanh toán đã được thêm!");
                    }
                    else
                    {
                        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);
                        return;
                    }
                }
                if (this.CallFromUserControll != null && this.CallFromUserControll is BillList)
                {
                    ((BillList)this.CallFromUserControll).loadBillList();
                }
                this.Close();
                return;
            }
            MessageBox.Show("Vui lòng kiểm tra các thông tin cần thiết!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;  
        }