private void dgwStockEntranceList_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 nhập kho này?",
                    "Xoá phiếu nhập kho",
                     MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question);
                    if (result == DialogResult.Yes)
                    {
                        DateTime systime = BaoHienRepository.GetBaoHienDBDataContext().GetSystemDate();
                        DataGridViewRow currentRow = dgwStockEntranceList.Rows[e.RowIndex];

                        EntranceStockService entranceStockService = new EntranceStockService();
                        int id = ObjectHelper.GetValueFromAnonymousType<int>(currentRow.DataBoundItem, "Id");
                        EntranceStock es = entranceStockService.GetEntranceStock(id);

                        ProductLogService productLogService = new ProductLogService();
                        EntranceStockDetailService entranceStockDetailService = new EntranceStockDetailService();
                        List<EntranceStockDetail> details = entranceStockDetailService.SelectEntranceStockDetailByWhere(x => x.EntranceStockId == es.Id).ToList();
                        string msg = "";
                        int error = 0;
                        ProductLog pl, newpl;
                        foreach (EntranceStockDetail item in details)
                        {
                            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 đã 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;
                        }
                        try
                        {
                            foreach (EntranceStockDetail 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 = es.EntranceCode,
                                    Status = BHConstant.DEACTIVE_STATUS,
                                    Direction = BHConstant.DIRECTION_OUT,
                                    UpdatedDate = systime
                                };
                                productLogService.AddProductLog(newpl);
                            }
                            productLogService.DeactiveProductLog(es.EntranceCode);
                            if (!entranceStockService.DeleteEntranceStock(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);
                            }
                        }
                        catch { }
                        loadEntranceStockList();
                    }

                }

            }
        }
Beispiel #2
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();
                    }
                }
            }
        }
Beispiel #3
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this.validator1.Validate() && ValidateData())
            {
                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;
                }
                if (entranceStock != null && isUpdating)//update
                {
                    #region Fix Update

                    string              msg = "";
                    int                 error = 0, amount_change = 0;
                    ProductLog          pl, newpl;
                    EntranceStockDetail esd;
                    // Check update old data
                    List <EntranceStockDetail> deleted_details = old_details.Where(x => !entranceStockDetails.Select(y => y.ProductId.ToString() + '_' +
                                                                                                                     y.AttributeId.ToString() + '_' + y.UnitId.ToString()).Contains(x.ProductId.ToString() + '_' +
                                                                                                                                                                                    x.AttributeId.ToString() + '_' + x.UnitId.ToString())).ToList();
                    foreach (EntranceStockDetail item in deleted_details)
                    {
                        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 đã 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";
                        }
                    }

                    List <EntranceStockDetail> updated_details = old_details.Where(x => entranceStockDetails.Select(y => y.ProductId.ToString() + '_' +
                                                                                                                    y.AttributeId.ToString() + '_' + y.UnitId.ToString()).Contains(x.ProductId.ToString() + '_' +
                                                                                                                                                                                   x.AttributeId.ToString() + '_' + x.UnitId.ToString())).ToList();
                    foreach (EntranceStockDetail item in updated_details)
                    {
                        pl  = productLogService.GetProductLog(item.ProductId, item.AttributeId, item.UnitId);
                        esd = entranceStockDetails.Where(x => x.ProductId == item.ProductId && x.AttributeId == item.AttributeId &&
                                                         x.UnitId == item.UnitId).FirstOrDefault();
                        amount_change = Convert.ToInt32(esd.NumberUnit - item.NumberUnit);
                        if (amount_change < 0 && pl.AfterNumber + amount_change < 0) // Giam so luong nhap
                        {
                            if (error < 2)
                            {
                                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 = 2;
                            }
                            msg += "- " + productLogService.GetNameOfProductLog(pl) + " : " + amount_change.ToString() + "\n";
                        }
                    }

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

                    try
                    {
                        entranceStock.UserId      = userId;
                        entranceStock.Note        = txtNote.Text;
                        entranceStock.UpdatedDate = systime;

                        EntranceStockDetailService entranceStockDetailService = new EntranceStockDetailService();
                        foreach (EntranceStockDetail 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   = entranceStock.EntranceCode,
                                Status       = BHConstant.ACTIVE_STATUS,
                                Direction    = BHConstant.DIRECTION_OUT,
                                UpdatedDate  = systime
                            };
                            productLogService.AddProductLog(newpl);
                        }
                        foreach (EntranceStockDetail od in entranceStockDetails)
                        {
                            od.EntranceStockId = entranceStock.Id;
                            if (od.ProductId > 0 && od.AttributeId > 0 && od.UnitId > 0)
                            {
                                EntranceStockDetail tmp_ode = old_details.Where(x => x.ProductId == od.ProductId &&
                                                                                x.AttributeId == od.AttributeId && x.UnitId == od.UnitId && x.EntranceStockId == entranceStock.Id).FirstOrDefault();
                                if (tmp_ode != null)
                                {
                                    double amount = od.NumberUnit - tmp_ode.NumberUnit;
                                    entranceStockDetailService.UpdateEntranceStockDetail(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   = entranceStock.EntranceCode,
                                            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) ? entranceStockDetailService.UpdateEntranceStockDetail(od)
                                        : entranceStockDetailService.AddEntranceStockDetail(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   = entranceStock.EntranceCode,
                                        Status       = BHConstant.ACTIVE_STATUS,
                                        Direction    = BHConstant.DIRECTION_IN,
                                        UpdatedDate  = systime
                                    };
                                    productLogService.AddProductLog(newpl);
                                }
                            }
                        }

                        EntranceStockService entranceStockService = new EntranceStockService();
                        bool result = entranceStockService.UpdateEntranceStock(entranceStock);

                        if (result)
                        {
                            MessageBox.Show("Phiếu nhập kho đã đượ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();
                    }
                    catch { }
                    #endregion
                }
                else//add new
                {
                    #region New

                    SeedService ss = new SeedService();
                    entranceStock = new EntranceStock
                    {
                        EntranceCode = ss.AddSeedID(BHConstant.PREFIX_FOR_ENTRANCE),
                        UserId       = userId,
                        CreatedDate  = systime,
                        Note         = txtNote.Text
                    };
                    EntranceStockService entranceStockService = new EntranceStockService();
                    bool result     = entranceStockService.AddEntranceStock(entranceStock);
                    long newOrderId = BaoHienRepository.GetMaxId <EntranceStock>();
                    EntranceStockDetailService entranceStockDetailService = new EntranceStockDetailService();
                    foreach (EntranceStockDetail od in entranceStockDetails)
                    {
                        if (od.ProductId > 0 && od.AttributeId > 0 && od.UnitId > 0)
                        {
                            od.EntranceStockId = (int)newOrderId;
                            bool ret = entranceStockDetailService.AddEntranceStockDetail(od);
                            if (!ret)
                            {
                                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;
                            }

                            //Save in Product Log
                            ProductLog pl    = productLogService.GetProductLog(od.ProductId, od.AttributeId, od.UnitId);
                            ProductLog newpl = new ProductLog()
                            {
                                ProductId    = od.ProductId,
                                AttributeId  = od.AttributeId,
                                UnitId       = od.UnitId,
                                BeforeNumber = pl.AfterNumber,
                                Amount       = od.NumberUnit,
                                AfterNumber  = pl.AfterNumber + od.NumberUnit,
                                RecordCode   = entranceStock.EntranceCode,
                                Status       = BHConstant.ACTIVE_STATUS,
                                Direction    = BHConstant.DIRECTION_IN,
                                UpdatedDate  = systime
                            };
                            productLogService.AddProductLog(newpl);
                        }
                    }
                    if (result)
                    {
                        MessageBox.Show("Phiếu nhập kho đã được tạo 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
                }
            }
        }
        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_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 nhập kho này?",
                                                          "Xoá phiếu nhập kho",
                                                          MessageBoxButtons.YesNo,
                                                          MessageBoxIcon.Question);
                    if (result == DialogResult.Yes)
                    {
                        DateTime        systime    = BaoHienRepository.GetBaoHienDBDataContext().GetSystemDate();
                        DataGridViewRow currentRow = dgwStockEntranceList.Rows[e.RowIndex];

                        EntranceStockService entranceStockService = new EntranceStockService();
                        int           id = ObjectHelper.GetValueFromAnonymousType <int>(currentRow.DataBoundItem, "Id");
                        EntranceStock es = entranceStockService.GetEntranceStock(id);

                        ProductLogService          productLogService          = new ProductLogService();
                        EntranceStockDetailService entranceStockDetailService = new EntranceStockDetailService();
                        List <EntranceStockDetail> details = entranceStockDetailService.SelectEntranceStockDetailByWhere(x => x.EntranceStockId == es.Id).ToList();
                        string     msg = "";
                        int        error = 0;
                        ProductLog pl, newpl;
                        foreach (EntranceStockDetail item in details)
                        {
                            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 đã 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;
                        }
                        try
                        {
                            foreach (EntranceStockDetail 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   = es.EntranceCode,
                                    Status       = BHConstant.DEACTIVE_STATUS,
                                    Direction    = BHConstant.DIRECTION_OUT,
                                    UpdatedDate  = systime
                                };
                                productLogService.AddProductLog(newpl);
                            }
                            productLogService.DeactiveProductLog(es.EntranceCode);
                            if (!entranceStockService.DeleteEntranceStock(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);
                            }
                        }
                        catch { }
                        loadEntranceStockList();
                    }
                }
            }
        }