public void loadProductionRequestList()
 {
     ProductionRequestService productionRequestService = new ProductionRequestService();
     productionRequests = productionRequestService.GetProductionRequests();
     if (productionRequests != null)
     {
         productionRequestInTotal.Text = productionRequests.Count.ToString();
     }
     
     dgwRequestList.AutoGenerateColumns = false;
     setUpDataGrid(productionRequests);           
 }
        private void dgwStockEntranceList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (modeReport == 1)
            {
                DataGridViewRow currentRow = dgwStockEntranceList.Rows[e.RowIndex];
                string          RecordCode = ObjectHelper.GetValueFromAnonymousType <string>(currentRow.DataBoundItem, "RecordCode");
                string          prefix     = RecordCode.Substring(0, 2);
                switch (prefix)
                {
                case BHConstant.PREFIX_FOR_ORDER:
                {
                    OrderService orderService = new OrderService();
                    Order        order        = orderService.GetOrders().Where(o => o.OrderCode == RecordCode).FirstOrDefault();
                    if (order != null)
                    {
                        AddOrder frmAddOrder = new AddOrder();
                        frmAddOrder.loadDataForEditOrder(order.Id);

                        frmAddOrder.CallFromUserControll = this;
                        frmAddOrder.ShowDialog();
                    }
                } break;

                case BHConstant.PREFIX_FOR_ENTRANCE:
                {
                    EntranceStockService stockService = new EntranceStockService();
                    EntranceStock        stock        = stockService.GetEntranceStocks().Where(r => r.EntranceCode == RecordCode).FirstOrDefault();
                    if (stock != null)
                    {
                        AddEntranceStock frmAddEntranceStock = new AddEntranceStock();
                        frmAddEntranceStock.loadDataForEditEntranceStock(stock.Id);

                        frmAddEntranceStock.CallFromUserControll = this;
                        frmAddEntranceStock.ShowDialog();
                    }
                } break;

                case BHConstant.PREFIX_FOR_PRODUCTION:
                {
                    ProductionRequestService requestService = new ProductionRequestService();
                    ProductionRequest        request        = requestService.GetProductionRequests().Where(r => r.ReqCode == RecordCode).FirstOrDefault();
                    if (request != null)
                    {
                        AddProductionRequest addProductionRequest = new AddProductionRequest();
                        addProductionRequest.loadDataForEditProductRequest(request.Id);

                        addProductionRequest.CallFromUserControll = this;
                        addProductionRequest.ShowDialog();
                    }
                } break;
                }
            }
        }
示例#3
0
        public void loadProductionRequestList()
        {
            ProductionRequestService productionRequestService = new ProductionRequestService();

            productionRequests = productionRequestService.GetProductionRequests();
            if (productionRequests != null)
            {
                productionRequestInTotal.Text = productionRequests.Count.ToString();
            }

            dgwRequestList.AutoGenerateColumns = false;
            setUpDataGrid(productionRequests);
        }
示例#4
0
        private void btnSearch_Click(object sender, EventArgs e)
        {
            ProductionRequestSearchCriteria productionRequestSearchCriteria = new ProductionRequestSearchCriteria
            {
                CodeRequest = txtCode.Text != null?txtCode.Text.ToLower() : "",
                                  UserId = (cbmUsers.SelectedValue != null && cbmUsers.SelectedIndex != 0) ? (int?)cbmUsers.SelectedValue : (int?)null,
                                  From   = dtpFrom.Value != null ? dtpFrom.Value : (DateTime?)null,
                                  To     = dtpTo.Value != null?dtpTo.Value.AddDays(1).Date : (DateTime?)null,
            };
            ProductionRequestService productionRequestService = new ProductionRequestService();

            productionRequests = productionRequestService.SearchingProductionRequest(productionRequestSearchCriteria);
            if (productionRequests == null)
            {
                productionRequests = new List <ProductionRequest>();
            }
            setUpDataGrid(productionRequests);
        }
 public void loadDataForEditProductRequest(int productionRequestId)
 {
     this.Text = "Chỉnh sửa phiếu sản xuất";
     isUpdating = true;
     ProductionRequestService prs = new ProductionRequestService();
     productionRequest = prs.GetProductionRequest(productionRequestId);
     old_marterial_details = new List<ProductionRequestDetail>();
     old_production_details = new List<ProductionRequestDetail>();
     if (productionRequest != null)
     {
         ProductionRequestDetailService prds = new ProductionRequestDetailService();
         productionRequestDetailInProductions = new BindingList<ProductionRequestDetail>(prds.GetProductionRequestDetails()
             .Where(p => p.ProductionRequestId == productionRequestId && p.Direction == BHConstant.PRODUCTION_REQUEST_DETAIL_OUT).ToList());
         foreach (ProductionRequestDetail od in productionRequestDetailInProductions)
         {
             old_production_details.Add(new ProductionRequestDetail
             {
                 AttributeId = od.AttributeId,
                 ProductId = od.ProductId,
                 UnitId = od.UnitId,
                 ProductionRequestId = od.ProductionRequestId,
                 NumberUnit = od.NumberUnit
             });
         }
         productionRequestDetailInMaterials = new BindingList<ProductionRequestDetail>(prds.GetProductionRequestDetails()
             .Where(p => p.ProductionRequestId == productionRequestId && p.Direction == BHConstant.PRODUCTION_REQUEST_DETAIL_IN).ToList());
         foreach (ProductionRequestDetail od in productionRequestDetailInMaterials)
         {
             old_marterial_details.Add(new ProductionRequestDetail
             {
                 AttributeId = od.AttributeId,
                 ProductId = od.ProductId,
                 UnitId = od.UnitId,
                 ProductionRequestId = od.ProductionRequestId,
                 NumberUnit = od.NumberUnit
             });
         }
     }            
 }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this.validator1.Validate() && ValidateData())
            {
                ProductionRequestService prs = new ProductionRequestService();
                ProductionRequestDetailService productionRequestDetailService = new ProductionRequestDetailService();
                DateTime systime = BaoHienRepository.GetBaoHienDBDataContext().GetSystemDate();
                if (productionRequest != null)
                {
                    #region Fix Update
                        
                    productionRequest.UpdatedDate = systime;
                    productionRequest.Note = txtNote.Text;

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

                    #region Check old data

                    // Material
                    List<ProductionRequestDetail> deleted_material_details = old_marterial_details.Where(x => !productionRequestDetailInMaterials.Select(y => y.ProductId.ToString() + '_' +
                        y.AttributeId.ToString() + '_' + y.UnitId.ToString()).Contains(x.ProductId.ToString() + '_' +
                        x.AttributeId.ToString() + '_' + x.UnitId.ToString())).ToList();
                    List<ProductionRequestDetail> updated_material_details = old_marterial_details.Where(x => productionRequestDetailInMaterials.Select(y => y.ProductId.ToString() + '_' +
                        y.AttributeId.ToString() + '_' + y.UnitId.ToString()).Contains(x.ProductId.ToString() + '_' +
                        x.AttributeId.ToString() + '_' + x.UnitId.ToString())).ToList();
                    List<ProductionRequestDetail> new_material_details = productionRequestDetailInMaterials.Where(x => !old_marterial_details.Select(y => y.ProductId.ToString() + '_' +
                        y.AttributeId.ToString() + '_' + y.UnitId.ToString()).Contains(x.ProductId.ToString() + '_' +
                        x.AttributeId.ToString() + '_' + x.UnitId.ToString())).ToList();
                    foreach (ProductionRequestDetail item in updated_material_details)
                    {
                        pl = productLogService.GetProductLog(item.ProductId, item.AttributeId, item.UnitId);
                        prd = productionRequestDetailInMaterials.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 += "Phần NGUYÊN LIỆU\n";
                                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 (ProductionRequestDetail item in new_material_details)
                    {
                        pl = productLogService.GetProductLog(item.ProductId, item.AttributeId, item.UnitId);
                        if (pl.AfterNumber - item.NumberUnit < 0)
                        {
                            if (error < 2)
                            {
                                msg += "Phần NGUYÊN LIỆU\n";
                                msg += "Những sản phẩm sau không đủ số lượng để tạo phiếu sản xuất:\n";
                                error = 2;
                            }
                            msg += "- " + productLogService.GetNameOfProductLog(pl) + " còn : " + pl.AfterNumber + "\n";
                        }
                    }

                    // Production
                    List<ProductionRequestDetail> deleted_production_details = old_production_details.Where(x => !productionRequestDetailInProductions.Select(y => y.ProductId.ToString() + '_' +
                        y.AttributeId.ToString() + '_' + y.UnitId.ToString()).Contains(x.ProductId.ToString() + '_' +
                        x.AttributeId.ToString() + '_' + x.UnitId.ToString())).ToList();
                    List<ProductionRequestDetail> updated_production_details = old_production_details.Where(x => productionRequestDetailInProductions.Select(y => y.ProductId.ToString() + '_' +
                        y.AttributeId.ToString() + '_' + y.UnitId.ToString()).Contains(x.ProductId.ToString() + '_' +
                        x.AttributeId.ToString() + '_' + x.UnitId.ToString())).ToList();
                    foreach (ProductionRequestDetail item in deleted_production_details)
                    {
                        pl = productLogService.GetProductLog(item.ProductId, item.AttributeId, item.UnitId);
                        if (pl.AfterNumber - item.NumberUnit < 0)
                        {
                            if (error < 3)
                            {
                                msg += "Phần THÀNH PHẨM\n";
                                msg += "Những sản phẩm sau đã bị XÓA nhưng không đảm bảo dữ liệu trong kho:\n";
                                error = 3;
                            }
                            msg += "- " + productLogService.GetNameOfProductLog(pl) + " : " + item.NumberUnit + "\n";
                        }
                    }
                    foreach (ProductionRequestDetail item in updated_production_details)
                    {
                        pl = productLogService.GetProductLog(item.ProductId, item.AttributeId, item.UnitId);
                        prd = productionRequestDetailInProductions.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) // Giam so luong thanh pham
                        {
                            if (error < 4)
                            {
                                msg += "Phần THÀNH PHẨM\n";
                                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 = 4;
                            }
                            msg += "- " + productLogService.GetNameOfProductLog(pl) + " : " + amount_change.ToString() + "\n";
                        }
                    }

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

                    #endregion

                    #region Update Marterial

                    foreach (ProductionRequestDetail item in deleted_material_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 = productionRequest.ReqCode,
                            Status = BHConstant.ACTIVE_STATUS,
                            Direction = BHConstant.DIRECTION_IN,
                            UpdatedDate = systime
                        };
                        productLogService.AddProductLog(newpl);
                    }
                    foreach (ProductionRequestDetail od in productionRequestDetailInMaterials)
                    {
                        od.ProductionRequestId = productionRequest.Id;
                        od.Direction = BHConstant.DIRECTION_OUT;
                        if (od.ProductId > 0 && od.AttributeId > 0 && od.UnitId > 0)
                        {
                            ProductionRequestDetail tmp_ode = old_marterial_details.Where(x => x.ProductId == od.ProductId &&
                                x.AttributeId == od.AttributeId && x.UnitId == od.UnitId && x.ProductionRequestId == productionRequest.Id).FirstOrDefault();
                            if (tmp_ode != null)
                            {
                                double amount = tmp_ode.NumberUnit - od.NumberUnit;
                                productionRequestDetailService.UpdateProductionRequestDetail(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 = productionRequest.ReqCode,
                                        Status = BHConstant.ACTIVE_STATUS,
                                        Direction = amount > 0 ? BHConstant.DIRECTION_IN : BHConstant.DIRECTION_OUT,
                                        UpdatedDate = systime
                                    };
                                    productLogService.AddProductLog(newpl);
                                }
                            }
                            else
                            {
                                bool ret = (od.Id != null && od.Id > 0) ? productionRequestDetailService.UpdateProductionRequestDetail(od) 
                                    : productionRequestDetailService.AddProductionRequestDetail(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 = productionRequest.ReqCode,
                                    Status = BHConstant.ACTIVE_STATUS,
                                    Direction = BHConstant.DIRECTION_OUT,
                                    UpdatedDate = systime
                                };
                                productLogService.AddProductLog(newpl);
                            }
                        }
                    }

                    #endregion

                    #region Update Production

                    foreach (ProductionRequestDetail item in deleted_production_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 = productionRequest.ReqCode,
                            Status = BHConstant.ACTIVE_STATUS,
                            Direction = BHConstant.DIRECTION_OUT,
                            UpdatedDate = systime
                        };
                        productLogService.AddProductLog(newpl);
                    }
                    foreach (ProductionRequestDetail od in productionRequestDetailInProductions)
                    {
                        od.ProductionRequestId = productionRequest.Id;
                        od.Direction = BHConstant.DIRECTION_IN;
                        if (od.ProductId > 0 && od.AttributeId > 0 && od.UnitId > 0)
                        {
                            ProductionRequestDetail tmp_ode = old_production_details.Where(x => x.ProductId == od.ProductId &&
                                x.AttributeId == od.AttributeId && x.UnitId == od.UnitId && x.ProductionRequestId == productionRequest.Id).FirstOrDefault();
                            if (tmp_ode != null)
                            {
                                double amount = od.NumberUnit - tmp_ode.NumberUnit;
                                productionRequestDetailService.UpdateProductionRequestDetail(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 = productionRequest.ReqCode,
                                        Status = BHConstant.ACTIVE_STATUS,
                                        Direction = amount > 0 ? BHConstant.DIRECTION_IN : BHConstant.DIRECTION_OUT,
                                        UpdatedDate = systime
                                    };
                                    productLogService.AddProductLog(newpl);
                                }
                            }
                            else
                            {
                                bool ret = (od.Id != null && od.Id > 0) ? productionRequestDetailService.UpdateProductionRequestDetail(od) 
                                    : productionRequestDetailService.AddProductionRequestDetail(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 = productionRequest.ReqCode,
                                    Status = BHConstant.ACTIVE_STATUS,
                                    Direction = BHConstant.DIRECTION_IN,
                                    UpdatedDate = systime
                                };
                                productLogService.AddProductLog(newpl);
                            }
                        }
                    }

                    #endregion

                    bool result = prs.UpdateProductionRequest(productionRequest);
                    if (result)
                        MessageBox.Show("Phiếu sản xuất đã được cập nhật thành công");
                    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);
                    this.Close();

                    #endregion
                }
                else
                {
                    #region New

                    int userId = 0;
                    ProductLog pl, newpl;
                    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;
                    }

                    string msg = "";
                    int error = 0;
                    foreach (ProductionRequestDetail prd in productionRequestDetailInMaterials)
                    {
                        pl = productLogService.GetProductLog(prd.ProductId, prd.AttributeId, prd.UnitId);
                        if (pl.AfterNumber - prd.NumberUnit < 0)
                        {
                            if (error == 0)
                            {
                                msg += "Những sản phẩm sau bên phần nguyên liệu không đủ số lượng để tạo phiếu sản xuất:\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;
                    }

                    SeedService ss = new SeedService();
                    productionRequest = new ProductionRequest
                    {
                        Note = txtNote.Text,
                        ReqCode = ss.AddSeedID(BHConstant.PREFIX_FOR_PRODUCTION),
                        CreatedDate = systime,
                        UserId = userId,
                    };

                    bool result = prs.AddProductionRequest(productionRequest);
                    if (result)
                    {
                        long newProductionRequestId = BaoHienRepository.GetMaxId<ProductionRequest>();
                        try
                        {
                            foreach (ProductionRequestDetail prd in productionRequestDetailInProductions)
                            {
                                prd.Direction = BHConstant.DIRECTION_IN;
                                if (prd.ProductId > 0 && prd.AttributeId > 0 && prd.UnitId > 0)
                                {
                                    prd.ProductionRequestId = (int)newProductionRequestId;
                                    prd.Direction = BHConstant.DIRECTION_IN;
                                    productionRequestDetailService.AddProductionRequestDetail(prd);
                                    //Save in Production Log
                                    pl = productLogService.GetProductLog(prd.ProductId, prd.AttributeId, prd.UnitId);
                                    newpl = new ProductLog()
                                    {
                                        ProductId = prd.ProductId,
                                        AttributeId = prd.AttributeId,
                                        UnitId = prd.UnitId,
                                        BeforeNumber = pl.AfterNumber,
                                        Amount = prd.NumberUnit,
                                        AfterNumber = pl.AfterNumber + prd.NumberUnit,
                                        RecordCode = productionRequest.ReqCode,
                                        Status = BHConstant.ACTIVE_STATUS,
                                        Direction = BHConstant.DIRECTION_IN,
                                        UpdatedDate = systime
                                    };
                                    productLogService.AddProductLog(newpl);
                                }
                            }

                            foreach (ProductionRequestDetail prd in productionRequestDetailInMaterials)
                            {
                                prd.Direction = BHConstant.DIRECTION_OUT;
                                if (prd.ProductId > 0 && prd.AttributeId > 0 && prd.UnitId > 0)
                                {
                                    prd.ProductionRequestId = (int)newProductionRequestId;
                                    prd.Direction = BHConstant.DIRECTION_OUT;
                                    productionRequestDetailService.AddProductionRequestDetail(prd);

                                    //Save in Product Log
                                    pl = productLogService.GetProductLog(prd.ProductId, prd.AttributeId, prd.UnitId);
                                    newpl = new ProductLog()
                                    {
                                        ProductId = prd.ProductId,
                                        AttributeId = prd.AttributeId,
                                        UnitId = prd.UnitId,
                                        BeforeNumber = pl.AfterNumber,
                                        Amount = prd.NumberUnit,
                                        AfterNumber = pl.AfterNumber - prd.NumberUnit,
                                        RecordCode = productionRequest.ReqCode,
                                        Status = BHConstant.ACTIVE_STATUS,
                                        Direction = BHConstant.DIRECTION_OUT,
                                        UpdatedDate = systime
                                    };
                                    productLogService.AddProductLog(newpl);
                                }
                            }
                        }
                        catch { }
                        MessageBox.Show("Phiếu sản xuất đã được tạo thành công");
                        this.Close();
                    }
                    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;
                    }

                    #endregion
                }
            } 
        }
示例#7
0
        private void dgwRequestList_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 sản xuât này?",
                                                          "Xoá phiếu sản xuât",
                                                          MessageBoxButtons.YesNo,
                                                          MessageBoxIcon.Question);
                    if (result == DialogResult.Yes)
                    {
                        DataGridViewRow currentRow = dgwRequestList.Rows[e.RowIndex];

                        ProductionRequestService       productionRequestService       = new ProductionRequestService();
                        ProductionRequestDetailService productionRequestDetailService = new ProductionRequestDetailService();
                        int id = ObjectHelper.GetValueFromAnonymousType <int>(currentRow.DataBoundItem, "Id");
                        ProductionRequest pr = productionRequestService.GetProductionRequest(id);
                        List <ProductionRequestDetail> productionRequestDetails = productionRequestDetailService.GetProductionRequestDetails().Where(p => p.ProductionRequestId == id).ToList();
                        bool     ret     = false;
                        DateTime systime = BaoHienRepository.GetBaoHienDBDataContext().GetSystemDate();

                        ProductLogService productLogService = new ProductLogService();
                        string            msg = "";
                        int        error = 0, amount = 0;
                        ProductLog pl, newpl;
                        foreach (ProductionRequestDetail item in productionRequestDetails)
                        {
                            pl     = productLogService.GetProductLog(item.ProductId, item.AttributeId, item.UnitId);
                            amount = (item.Direction == BHConstant.DIRECTION_OUT) ? -item.NumberUnit : item.NumberUnit;
                            if (pl.AfterNumber - amount < 0)
                            {
                                if (error == 0)
                                {
                                    msg  += "Những sản phẩm sau đã bị XÓA nhưng không đảm bảo dữ liệu trong kho:\n";
                                    error = 1;
                                }
                                msg += "- " + productLogService.GetNameOfProductLog(pl) + " : " + item.NumberUnit + "\n";
                            }
                        }
                        if (error > 0)
                        {
                            MessageBox.Show(msg, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                        foreach (ProductionRequestDetail item in productionRequestDetails)
                        {
                            pl     = productLogService.GetProductLog(item.ProductId, item.AttributeId, item.UnitId);
                            amount = (item.Direction == BHConstant.DIRECTION_OUT) ? -item.NumberUnit : item.NumberUnit;
                            newpl  = new ProductLog()
                            {
                                ProductId    = item.ProductId,
                                AttributeId  = item.AttributeId,
                                UnitId       = item.UnitId,
                                BeforeNumber = pl.AfterNumber,
                                Amount       = item.NumberUnit,
                                AfterNumber  = pl.AfterNumber - amount,
                                RecordCode   = pr.ReqCode,
                                Status       = BHConstant.DEACTIVE_STATUS,
                                Direction    = !item.Direction,
                                UpdatedDate  = systime
                            };
                            productLogService.AddProductLog(newpl);
                        }
                        productLogService.DeactiveProductLog(pr.ReqCode);
                        if (!productionRequestService.DeleteProductionRequest(id))
                        {
                            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);
                        }
                        loadProductionRequestList();
                    }
                }
            }
        }
 private void btnSearch_Click(object sender, EventArgs e)
 {
     ProductionRequestSearchCriteria productionRequestSearchCriteria = new ProductionRequestSearchCriteria
     {
         CodeRequest = txtCode.Text != null ? txtCode.Text.ToLower() : "",
         UserId = (cbmUsers.SelectedValue != null && cbmUsers.SelectedIndex != 0) ? (int?)cbmUsers.SelectedValue : (int?)null,
         From = dtpFrom.Value != null ? dtpFrom.Value : (DateTime?)null,
         To = dtpTo.Value != null ? dtpTo.Value.AddDays(1).Date : (DateTime?)null,
     };
     ProductionRequestService productionRequestService = new ProductionRequestService();
     productionRequests = productionRequestService.SearchingProductionRequest(productionRequestSearchCriteria);
     if (productionRequests == null)
     {
         productionRequests = new List<ProductionRequest>();
     }
     setUpDataGrid(productionRequests);
 }
        private void dgwRequestList_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 sản xuât này?",
                    "Xoá phiếu sản xuât",
                     MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question);
                    if (result == DialogResult.Yes)
                    {
                        DataGridViewRow currentRow = dgwRequestList.Rows[e.RowIndex];

                        ProductionRequestService productionRequestService = new ProductionRequestService();
                        ProductionRequestDetailService productionRequestDetailService = new ProductionRequestDetailService();
                        int id = ObjectHelper.GetValueFromAnonymousType<int>(currentRow.DataBoundItem, "Id");
                        ProductionRequest pr = productionRequestService.GetProductionRequest(id);
                        List<ProductionRequestDetail> productionRequestDetails = productionRequestDetailService.GetProductionRequestDetails().Where(p => p.ProductionRequestId == id).ToList();
                        bool ret = false;
                        DateTime systime = BaoHienRepository.GetBaoHienDBDataContext().GetSystemDate();

                        ProductLogService productLogService = new ProductLogService();
                        string msg = "";
                        int error = 0, amount = 0;
                        ProductLog pl, newpl;
                        foreach (ProductionRequestDetail item in productionRequestDetails)
                        {
                            pl = productLogService.GetProductLog(item.ProductId, item.AttributeId, item.UnitId);
                            amount = (item.Direction == BHConstant.DIRECTION_OUT) ? -item.NumberUnit : item.NumberUnit;
                            if (pl.AfterNumber - amount < 0)
                            {
                                if (error == 0)
                                {
                                    msg += "Những sản phẩm sau đã bị XÓA nhưng không đảm bảo dữ liệu trong kho:\n";
                                    error = 1;
                                }
                                msg += "- " + productLogService.GetNameOfProductLog(pl) + " : " + item.NumberUnit + "\n";
                            }
                        }
                        if (error > 0)
                        {
                            MessageBox.Show(msg, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                        foreach (ProductionRequestDetail item in productionRequestDetails)
                        {
                            pl = productLogService.GetProductLog(item.ProductId, item.AttributeId, item.UnitId);
                            amount = (item.Direction == BHConstant.DIRECTION_OUT) ? -item.NumberUnit : item.NumberUnit;
                            newpl = new ProductLog()
                            {
                                ProductId = item.ProductId,
                                AttributeId = item.AttributeId,
                                UnitId = item.UnitId,
                                BeforeNumber = pl.AfterNumber,
                                Amount = item.NumberUnit,
                                AfterNumber = pl.AfterNumber - amount,
                                RecordCode = pr.ReqCode,
                                Status = BHConstant.DEACTIVE_STATUS,
                                Direction = !item.Direction,
                                UpdatedDate = systime
                            };
                            productLogService.AddProductLog(newpl);
                        }
                        productLogService.DeactiveProductLog(pr.ReqCode);
                        if (!productionRequestService.DeleteProductionRequest(id))
                        {
                            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);
                        }
                        loadProductionRequestList();
                    }

                }

            }
        }
        private void dgwStockEntranceList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (modeReport == 1)
            {
                DataGridViewRow currentRow = dgwStockEntranceList.Rows[e.RowIndex];
                string RecordCode = ObjectHelper.GetValueFromAnonymousType<string>(currentRow.DataBoundItem, "RecordCode");
                string prefix = RecordCode.Substring(0, 2);
                switch (prefix)
                {
                    case BHConstant.PREFIX_FOR_ORDER:
                        {
                            OrderService orderService = new OrderService();
                            Order order = orderService.GetOrders().Where(o => o.OrderCode == RecordCode).FirstOrDefault();
                            if (order != null)
                            {
                                AddOrder frmAddOrder = new AddOrder();
                                frmAddOrder.loadDataForEditOrder(order.Id);

                                frmAddOrder.CallFromUserControll = this;
                                frmAddOrder.ShowDialog();
                            }
                        } break;
                    case BHConstant.PREFIX_FOR_ENTRANCE:
                        {
                            EntranceStockService stockService = new EntranceStockService();
                            EntranceStock stock = stockService.GetEntranceStocks().Where(r => r.EntranceCode == RecordCode).FirstOrDefault();
                            if (stock != null)
                            {
                                AddEntranceStock frmAddEntranceStock = new AddEntranceStock();
                                frmAddEntranceStock.loadDataForEditEntranceStock(stock.Id);

                                frmAddEntranceStock.CallFromUserControll = this;
                                frmAddEntranceStock.ShowDialog();
                            }
                        } break;
                    case BHConstant.PREFIX_FOR_PRODUCTION:
                        {
                            ProductionRequestService requestService = new ProductionRequestService();
                            ProductionRequest request = requestService.GetProductionRequests().Where(r => r.ReqCode == RecordCode).FirstOrDefault();
                            if (request != null)
                            {
                                AddProductionRequest addProductionRequest = new AddProductionRequest();
                                addProductionRequest.loadDataForEditProductRequest(request.Id);

                                addProductionRequest.CallFromUserControll = this;
                                addProductionRequest.ShowDialog();
                            }
                        } break;
                }
            }
        }