Beispiel #1
0
        /// <summary>
        /// Create, update and delete Stock Posting for one record.
        /// </summary>
        /// <param name="StockViewModel_New"></param>
        /// <param name="StockViewModel_Old"></param>
        /// <returns></returns>
        public string StockPost(StockViewModel StockViewModel_New, StockViewModel StockViewModel_Old)
        {
            string      ErrorText = "";
            StockHeader StockHeader;


            if (StockViewModel_New != null)
            {
                StockHeader = _stockHeaderService.FindByDocHeader(StockViewModel_New.DocHeaderId, StockViewModel_New.StockHeaderId, StockViewModel_New.DocTypeId, StockViewModel_New.SiteId, StockViewModel_New.DivisionId);

                if (StockViewModel_New.StockHeaderExist == 0 || StockViewModel_New.StockHeaderExist == null)
                {
                    if (StockHeader == null)
                    {
                        StockHeader H = new StockHeader();

                        H.DocHeaderId  = StockViewModel_New.DocHeaderId;
                        H.DocTypeId    = StockViewModel_New.DocTypeId;
                        H.DocDate      = StockViewModel_New.StockHeaderDocDate;
                        H.DocNo        = StockViewModel_New.DocNo;
                        H.DivisionId   = StockViewModel_New.DivisionId;
                        H.SiteId       = StockViewModel_New.SiteId;
                        H.CurrencyId   = StockViewModel_New.CurrencyId;
                        H.PersonId     = StockViewModel_New.PersonId;
                        H.ProcessId    = StockViewModel_New.HeaderProcessId;
                        H.FromGodownId = StockViewModel_New.HeaderFromGodownId;
                        H.GodownId     = StockViewModel_New.HeaderGodownId;
                        H.Remark       = StockViewModel_New.Remark;
                        H.Status       = StockViewModel_New.Status;
                        H.CreatedBy    = StockViewModel_New.CreatedBy;
                        H.CreatedDate  = StockViewModel_New.CreatedDate;
                        H.ModifiedBy   = StockViewModel_New.ModifiedBy;
                        H.ModifiedDate = StockViewModel_New.ModifiedDate;

                        _stockHeaderService.Create(H);

                        StockHeader = H;
                    }
                    else
                    {
                        StockHeader.DocHeaderId  = StockViewModel_New.DocHeaderId;
                        StockHeader.DocTypeId    = StockViewModel_New.DocTypeId;
                        StockHeader.DocDate      = StockViewModel_New.StockHeaderDocDate;
                        StockHeader.DocNo        = StockViewModel_New.DocNo;
                        StockHeader.DivisionId   = StockViewModel_New.DivisionId;
                        StockHeader.SiteId       = StockViewModel_New.SiteId;
                        StockHeader.CurrencyId   = StockViewModel_New.CurrencyId;
                        StockHeader.PersonId     = StockViewModel_New.PersonId;
                        StockHeader.ProcessId    = StockViewModel_New.HeaderProcessId;
                        StockHeader.FromGodownId = StockViewModel_New.HeaderFromGodownId;
                        StockHeader.GodownId     = StockViewModel_New.HeaderGodownId;
                        StockHeader.Remark       = StockViewModel_New.Remark;
                        StockHeader.Status       = StockViewModel_New.Status;
                        StockHeader.CreatedBy    = StockViewModel_New.CreatedBy;
                        StockHeader.CreatedDate  = StockViewModel_New.CreatedDate;
                        StockHeader.ModifiedBy   = StockViewModel_New.ModifiedBy;
                        StockHeader.ModifiedDate = StockViewModel_New.ModifiedDate;

                        _stockHeaderService.Update(StockHeader);
                    }
                }
            }
            else
            {
                StockHeader = _stockHeaderService.FindByDocHeader(StockViewModel_Old.DocHeaderId, StockViewModel_Old.StockHeaderId, StockViewModel_Old.DocTypeId, StockViewModel_Old.SiteId, StockViewModel_Old.DivisionId);
            }


            if (StockViewModel_Old != null)
            {
                Stock Stock_Old = Find(StockViewModel_Old.StockHeaderId, StockViewModel_Old.ProductId, StockViewModel_Old.StockDocDate, StockViewModel_Old.Dimension1Id, StockViewModel_Old.Dimension2Id, StockViewModel_Old.ProcessId, StockViewModel_Old.LotNo, StockViewModel_Old.GodownId, StockViewModel_Old.CostCenterId);

                if (Stock_Old != null)
                {
                    Stock_Old.Qty_Iss       = Stock_Old.Qty_Iss - StockViewModel_Old.Qty_Iss;
                    Stock_Old.Qty_Rec       = Stock_Old.Qty_Rec - StockViewModel_Old.Qty_Rec;
                    Stock_Old.Rate          = StockViewModel_Old.Rate;
                    Stock_Old.ExpiryDate    = StockViewModel_Old.ExpiryDate;
                    Stock_Old.Specification = StockViewModel_Old.Specification;

                    Update(Stock_Old);

                    //if (Stock_Old.Qty_Iss == 0 && Stock_Old.Qty_Rec == 0) { Delete(Stock_Old); }
                    //else { Update(Stock_Old); }

                    StockBalance StockBalance_Old = FindStockBalance(StockViewModel_Old.ProductId, StockViewModel_Old.Dimension1Id, StockViewModel_Old.Dimension2Id, StockViewModel_Old.ProcessId, StockViewModel_Old.LotNo, StockViewModel_Old.GodownId, StockViewModel_Old.CostCenterId);

                    if (StockBalance_Old != null)
                    {
                        StockBalance_Old.Qty = StockBalance_Old.Qty - StockViewModel_Old.Qty_Rec;
                        StockBalance_Old.Qty = StockBalance_Old.Qty + StockViewModel_Old.Qty_Iss;

                        if (StockBalance_Old.Qty == 0)
                        {
                            _stockBalanceService.Delete(StockBalance_Old);
                        }
                        else
                        {
                            _stockBalanceService.Update(StockBalance_Old);
                        }
                    }
                }
            }

            if (StockViewModel_New != null)
            {
                Stock Stock_New;

                if (StockHeader != null)
                {
                    Stock_New = Find(StockHeader.StockHeaderId, StockViewModel_New.ProductId, StockViewModel_New.StockDocDate, StockViewModel_New.Dimension1Id, StockViewModel_New.Dimension2Id, StockViewModel_New.ProcessId, StockViewModel_New.LotNo, StockViewModel_New.GodownId, StockViewModel_New.CostCenterId);
                }
                else
                {
                    Stock_New = null;
                }

                if (Stock_New == null)
                {
                    Stock L = new Stock();

                    L.DocDate       = StockViewModel_New.StockDocDate;
                    L.ProductId     = StockViewModel_New.ProductId;
                    L.ProcessId     = StockViewModel_New.ProcessId;
                    L.GodownId      = StockViewModel_New.GodownId;
                    L.LotNo         = StockViewModel_New.LotNo;
                    L.CostCenterId  = StockViewModel_New.CostCenterId;
                    L.Qty_Iss       = StockViewModel_New.Qty_Iss;
                    L.Qty_Rec       = StockViewModel_New.Qty_Rec;
                    L.Rate          = StockViewModel_New.Rate;
                    L.ExpiryDate    = StockViewModel_New.ExpiryDate;
                    L.Specification = StockViewModel_New.Specification;
                    L.Dimension1Id  = StockViewModel_New.Dimension1Id;
                    L.Dimension2Id  = StockViewModel_New.Dimension2Id;
                    L.CreatedBy     = StockViewModel_New.CreatedBy;
                    L.CreatedDate   = StockViewModel_New.CreatedDate;
                    L.ModifiedBy    = StockViewModel_New.ModifiedBy;
                    L.ModifiedDate  = StockViewModel_New.ModifiedDate;


                    if (StockHeader != null)
                    {
                        L.StockHeaderId = StockHeader.StockHeaderId;
                    }

                    Create(L);
                }
                else
                {
                    Stock_New.Qty_Iss       = Stock_New.Qty_Iss + StockViewModel_New.Qty_Iss;
                    Stock_New.Qty_Rec       = Stock_New.Qty_Rec + StockViewModel_New.Qty_Rec;
                    Stock_New.Rate          = StockViewModel_New.Rate;
                    Stock_New.ExpiryDate    = StockViewModel_New.ExpiryDate;
                    Stock_New.Specification = StockViewModel_New.Specification;
                    Stock_New.ModifiedBy    = StockViewModel_New.ModifiedBy;
                    Stock_New.ModifiedDate  = StockViewModel_New.ModifiedDate;

                    Update(Stock_New);
                }

                StockBalance StockBalance_New = FindStockBalance(StockViewModel_New.ProductId, StockViewModel_New.Dimension1Id, StockViewModel_New.Dimension2Id, StockViewModel_New.ProcessId, StockViewModel_New.LotNo, StockViewModel_New.GodownId, StockViewModel_New.CostCenterId);

                if (StockBalance_New == null)
                {
                    StockBalance Sb = new StockBalance();

                    Sb.ProductId    = StockViewModel_New.ProductId;
                    Sb.Dimension1Id = StockViewModel_New.Dimension1Id;
                    Sb.Dimension2Id = StockViewModel_New.Dimension2Id;
                    Sb.ProcessId    = StockViewModel_New.ProcessId;
                    Sb.GodownId     = StockViewModel_New.GodownId;
                    Sb.CostCenterId = StockViewModel_New.CostCenterId;
                    Sb.LotNo        = StockViewModel_New.LotNo;
                    if (StockViewModel_New.Qty_Iss != 0)
                    {
                        Sb.Qty = StockViewModel_New.Qty_Iss;
                    }
                    if (StockViewModel_New.Qty_Rec != 0)
                    {
                        Sb.Qty = StockViewModel_New.Qty_Rec;
                    }

                    _stockBalanceService.Create(Sb);
                }
                else
                {
                    StockBalance_New.Qty = StockBalance_New.Qty + StockViewModel_New.Qty_Rec;
                    StockBalance_New.Qty = StockBalance_New.Qty - StockViewModel_New.Qty_Iss;

                    _stockBalanceService.Update(StockBalance_New);
                }
            }

            return(ErrorText);
        }
        public string StockProcessPost(ref StockProcessViewModel StockProcessViewModel)
        {
            string ErrorText = "";

            if (StockProcessViewModel.StockHeaderId == 0)
            {
                StockHeader H = new StockHeader();

                H.DocHeaderId  = StockProcessViewModel.DocHeaderId;
                H.DocTypeId    = StockProcessViewModel.DocTypeId;
                H.DocDate      = StockProcessViewModel.StockHeaderDocDate;
                H.DocNo        = StockProcessViewModel.DocNo;
                H.DivisionId   = StockProcessViewModel.DivisionId;
                H.SiteId       = StockProcessViewModel.SiteId;
                H.CurrencyId   = StockProcessViewModel.CurrencyId;
                H.PersonId     = StockProcessViewModel.PersonId;
                H.ProcessId    = StockProcessViewModel.HeaderProcessId;
                H.FromGodownId = StockProcessViewModel.HeaderFromGodownId;
                H.GodownId     = StockProcessViewModel.HeaderGodownId;
                H.Remark       = StockProcessViewModel.Remark;
                H.Status       = StockProcessViewModel.Status;
                H.CreatedBy    = StockProcessViewModel.CreatedBy;
                H.CreatedDate  = StockProcessViewModel.CreatedDate;
                H.ModifiedBy   = StockProcessViewModel.ModifiedBy;
                H.ModifiedDate = StockProcessViewModel.ModifiedDate;

                _stockHeaderService.Create(H);

                StockProcessViewModel.StockHeaderId = H.StockHeaderId;
            }

            if (StockProcessViewModel.StockProcessId <= 0)
            {
                StockProcess L = new StockProcess();

                if (StockProcessViewModel.StockHeaderId != 0 && StockProcessViewModel.StockHeaderId != -1)
                {
                    L.StockHeaderId = StockProcessViewModel.StockHeaderId;
                }

                L.StockProcessId = StockProcessViewModel.StockProcessId;
                L.DocDate        = StockProcessViewModel.StockProcessDocDate;
                L.ProductId      = StockProcessViewModel.ProductId;
                L.ProcessId      = StockProcessViewModel.ProcessId;
                L.GodownId       = StockProcessViewModel.GodownId;
                L.LotNo          = StockProcessViewModel.LotNo;
                L.CostCenterId   = StockProcessViewModel.CostCenterId;
                L.Qty_Iss        = StockProcessViewModel.Qty_Iss;
                L.Qty_Rec        = StockProcessViewModel.Qty_Rec;
                L.Rate           = StockProcessViewModel.Rate;
                L.ExpiryDate     = StockProcessViewModel.ExpiryDate;
                L.Specification  = StockProcessViewModel.Specification;
                L.Dimension1Id   = StockProcessViewModel.Dimension1Id;
                L.Dimension2Id   = StockProcessViewModel.Dimension2Id;
                L.CreatedBy      = StockProcessViewModel.CreatedBy;
                L.CreatedDate    = StockProcessViewModel.CreatedDate;
                L.ModifiedBy     = StockProcessViewModel.ModifiedBy;
                L.ModifiedDate   = StockProcessViewModel.ModifiedDate;
                L.ObjectState    = Model.ObjectState.Added;
                Create(L);


                StockProcessBalance StockProcessBalance = _stockProcessBalanceService.Find(L.ProductId, L.Dimension1Id, L.Dimension2Id, L.ProcessId, L.LotNo, L.GodownId, L.CostCenterId);

                if (StockProcessBalance == null)
                {
                    StockProcessBalance StockProcessBalance_NewRecord = new StockProcessBalance();

                    StockProcessBalance_NewRecord.ProductId    = L.ProductId;
                    StockProcessBalance_NewRecord.Dimension1Id = L.Dimension1Id;
                    StockProcessBalance_NewRecord.Dimension2Id = L.Dimension2Id;
                    StockProcessBalance_NewRecord.ProcessId    = L.ProcessId;
                    StockProcessBalance_NewRecord.GodownId     = L.GodownId;
                    StockProcessBalance_NewRecord.CostCenterId = L.CostCenterId;
                    StockProcessBalance_NewRecord.LotNo        = L.LotNo;
                    if (L.Qty_Iss != 0)
                    {
                        StockProcessBalance_NewRecord.Qty = -L.Qty_Iss;
                    }
                    if (L.Qty_Rec != 0)
                    {
                        StockProcessBalance_NewRecord.Qty = L.Qty_Rec;
                    }

                    _stockProcessBalanceService.Create(StockProcessBalance_NewRecord);
                }
                else
                {
                    StockProcessBalance.Qty = StockProcessBalance.Qty - L.Qty_Iss;
                    StockProcessBalance.Qty = StockProcessBalance.Qty + L.Qty_Rec;

                    _stockProcessBalanceService.Update(StockProcessBalance);
                }

                StockProcessViewModel.StockProcessId = L.StockProcessId;
            }
            else
            {
                StockProcess L = Find(StockProcessViewModel.StockProcessId);

                //To Rollback Chenges in StockProcess Balance done by old entry.
                StockProcess Temp = new StockProcess();
                Temp.ProductId    = L.ProductId;
                Temp.Dimension1Id = L.Dimension1Id;
                Temp.Dimension2Id = L.Dimension2Id;
                Temp.ProcessId    = L.ProcessId;
                Temp.GodownId     = L.GodownId;
                Temp.CostCenterId = L.CostCenterId;
                Temp.LotNo        = L.LotNo;
                Temp.Qty_Iss      = L.Qty_Iss;
                Temp.Qty_Rec      = L.Qty_Rec;
                //new StockProcessBalanceService(_unitOfWork).UpdateStockProcessBalance(Temp);
                ///////////////////////////////////
                StockProcessBalance StockProcessBalance_Old = _stockProcessBalanceService.Find(Temp.ProductId, Temp.Dimension1Id, Temp.Dimension2Id, Temp.ProcessId, Temp.LotNo, Temp.GodownId, Temp.CostCenterId);


                L.DocDate       = StockProcessViewModel.StockProcessDocDate;
                L.ProductId     = StockProcessViewModel.ProductId;
                L.ProcessId     = StockProcessViewModel.ProcessId;
                L.GodownId      = StockProcessViewModel.GodownId;
                L.LotNo         = StockProcessViewModel.LotNo;
                L.CostCenterId  = StockProcessViewModel.CostCenterId;
                L.Qty_Iss       = StockProcessViewModel.Qty_Iss;
                L.Qty_Rec       = StockProcessViewModel.Qty_Rec;
                L.Rate          = StockProcessViewModel.Rate;
                L.ExpiryDate    = StockProcessViewModel.ExpiryDate;
                L.Specification = StockProcessViewModel.Specification;
                L.Dimension1Id  = StockProcessViewModel.Dimension1Id;
                L.Dimension2Id  = StockProcessViewModel.Dimension2Id;
                L.CreatedBy     = StockProcessViewModel.CreatedBy;
                L.CreatedDate   = StockProcessViewModel.CreatedDate;
                L.ModifiedBy    = StockProcessViewModel.ModifiedBy;
                L.ModifiedDate  = StockProcessViewModel.ModifiedDate;

                Update(L);

                //new StockProcessBalanceService(_unitOfWork).UpdateStockProcessBalance(L);


                StockProcessBalance StockProcessBalance_New = _stockProcessBalanceService.Find(L.ProductId, L.Dimension1Id, L.Dimension2Id, L.ProcessId, L.LotNo, L.GodownId, L.CostCenterId);

                if (StockProcessBalance_New != null)
                {
                    if (StockProcessBalance_Old.StockProcessBalanceId != StockProcessBalance_New.StockProcessBalanceId)
                    {
                        if (StockProcessBalance_Old != null)
                        {
                            StockProcessBalance_Old.Qty = StockProcessBalance_Old.Qty - Temp.Qty_Rec;
                            StockProcessBalance_Old.Qty = StockProcessBalance_Old.Qty + Temp.Qty_Iss;

                            _stockProcessBalanceService.Update(StockProcessBalance_New);
                        }

                        if (StockProcessBalance_New == null)
                        {
                            StockProcessBalance StockProcessBalance_NewRecord = new StockProcessBalance();

                            StockProcessBalance_NewRecord.ProductId    = L.ProductId;
                            StockProcessBalance_NewRecord.Dimension1Id = L.Dimension1Id;
                            StockProcessBalance_NewRecord.Dimension2Id = L.Dimension2Id;
                            StockProcessBalance_NewRecord.ProcessId    = L.ProcessId;
                            StockProcessBalance_NewRecord.GodownId     = L.GodownId;
                            StockProcessBalance_NewRecord.CostCenterId = L.CostCenterId;
                            StockProcessBalance_NewRecord.LotNo        = L.LotNo;
                            if (L.Qty_Iss != 0)
                            {
                                StockProcessBalance_NewRecord.Qty = -L.Qty_Iss;
                            }
                            if (L.Qty_Rec != 0)
                            {
                                StockProcessBalance_NewRecord.Qty = L.Qty_Rec;
                            }

                            _stockProcessBalanceService.Create(StockProcessBalance_NewRecord);
                        }
                        else
                        {
                            StockProcessBalance_New.Qty = StockProcessBalance_New.Qty - L.Qty_Iss;
                            StockProcessBalance_New.Qty = StockProcessBalance_New.Qty + L.Qty_Rec;

                            _stockProcessBalanceService.Update(StockProcessBalance_New);
                        }
                    }
                    else
                    {
                        StockProcessBalance_New.Qty = StockProcessBalance_New.Qty + Temp.Qty_Iss - L.Qty_Iss;
                        StockProcessBalance_New.Qty = StockProcessBalance_New.Qty - Temp.Qty_Rec + L.Qty_Rec;

                        _stockProcessBalanceService.Update(StockProcessBalance_New);
                    }
                }

                StockProcessViewModel.StockProcessId = L.StockProcessId;
            }

            return(ErrorText);
        }