Exemplo n.º 1
0
 private void ChangeItemExitBatch(Exit exit)
 {
     foreach (var exitItem in exit.ExitItem)
     {
         StockBatch lastest    = db.StockBatch.Where(s => s.Id == exitItem.BatchId).OrderByDescending(s => s.DateExit).FirstOrDefault();
         StockBatch stockBatch = new StockBatch();
         stockBatch.StoreId     = exitItem.StoreId;
         stockBatch.StoreName   = exitItem.StoreName;
         stockBatch.ItemId      = exitItem.ItemId;
         stockBatch.ItemName    = exitItem.ItemName;
         stockBatch.BatchNo     = (lastest == null ? "" : lastest.BatchNo);
         stockBatch.DateProduct = exitItem.DateProduct;
         stockBatch.Guarantee   = (lastest == null ? 365 : lastest.Guarantee);
         stockBatch.InitCount   = (lastest == null ? 0 : lastest.RealCount);
         stockBatch.EnterType   = "";
         stockBatch.DateEnter   = DateTime.Now;
         stockBatch.EnterCount  = 0;
         stockBatch.ExitType    = exit.ExitType;
         stockBatch.DateExit    = DateTime.Now;
         stockBatch.ExitCount   = exitItem.QuantityReal;
         stockBatch.RealCount   = stockBatch.InitCount + stockBatch.EnterCount - stockBatch.ExitCount;
         stockBatch.TableName   = "ExitItem";
         stockBatch.RecordGuid  = exitItem.Guid;
         db.StockBatch.InsertOnSubmit(stockBatch);
     }
 }
        public async Task <Unit> Handle(UpdateStockBatchCommand request, CancellationToken cancellationToken)
        {
            StockBatch batch = await _database.StockBatch.FindAsync(request.Id);

            if (batch == null)
            {
                throw new NotFoundException(nameof(StockBatch), request.Id);
            }

            batch.AvailableFrom = request.AvailableFrom;
            batch.ExpiryDate    = request.ExpiryDate;

            if (request.Status.Trim().ToUpper() == "RECIEVED" && batch.Status.Trim().ToUpper() != "RECIEVED")
            {
                batch.Status = "Recieved";
            }
            else if (request.Status.Trim().ToUpper() == "PLANED" && batch.Status.Trim().ToUpper() == "RECIEVED")
            {
                batch.Status = "Planed";
            }

            _database.StockBatch.Update(batch);
            await _database.SaveAsync();

            return(Unit.Value);
        }
Exemplo n.º 3
0
 private void ChangeItemEnterBatch(Enter enter)
 {
     foreach (var enterItem in enter.EnterItem)
     {
         StockBatch lastest    = db.StockBatch.Where(s => s.ItemId == enterItem.ItemId && s.StoreId == enterItem.StoreId && s.DateProduct == enterItem.DateProduct).OrderByDescending(s => s.DateEnter).FirstOrDefault();
         StockBatch stockBatch = new StockBatch();
         stockBatch.StoreId     = enterItem.StoreId;
         stockBatch.StoreName   = enterItem.StoreName;
         stockBatch.ItemId      = enterItem.ItemId;
         stockBatch.ItemName    = enterItem.ItemName;
         stockBatch.BatchNo     = (lastest == null ? DateTime.Now.ToString("yyyyMMddhhmmssffff") : lastest.BatchNo);
         stockBatch.DateProduct = enterItem.DateProduct;
         Item item = db.Item.Where(i => i.Id == enterItem.ItemId).FirstOrDefault();
         stockBatch.Guarantee  = item.Guarantee;
         stockBatch.InitCount  = (lastest == null ? 0 : lastest.RealCount);
         stockBatch.EnterType  = enter.EnterType;
         stockBatch.DateEnter  = DateTime.Now;
         stockBatch.EnterCount = enterItem.QuantityReal;
         stockBatch.ExitType   = "";
         stockBatch.ExitCount  = 0;
         stockBatch.DateExit   = null;
         stockBatch.RealCount  = stockBatch.InitCount + stockBatch.EnterCount - stockBatch.ExitCount;
         stockBatch.TableName  = "EnterItem";
         stockBatch.RecordGuid = enterItem.Guid;
         db.StockBatch.InsertOnSubmit(stockBatch);
     }
 }
        public async Task <uint> Handle(NewStockBatchDto request, CancellationToken cancellationToken)
        {
            var item = await _database.Item
                       .AsNoTracking()
                       .FirstOrDefaultAsync(i => i.Id == request.ItemId);

            if (item == null)
            {
                throw new NotFoundException(nameof(Item), request.ItemId);
            }

            StockBatch batch = new StockBatch()
            {
                ItemId        = request.ItemId,
                Quantity      = request.Quantity,
                UnitCost      = request.UnitCost,
                AvailableFrom = request.AvailableFrom,
                Status        = request.Status
            };

            if (request.Status.ToUpper() == "RECIEVED" || request.ArivalDate != null)
            {
                batch.ArrivalDate = (request.ArivalDate == null) ? DateTime.Now : request.ArivalDate;
            }
            batch.Source = "Manual";

            Object manufacture;

            // check if manufacture order id is defined
            if (request.ManufactureOrderId != null && request.ManufactureOrderId != 0)
            {
                manufacture = await _database.ProductionOrderList
                              .AsNoTracking()
                              .FirstOrDefaultAsync(m => m.Id == request.ManufactureOrderId);

                if (manufacture == null)
                {
                    throw new NotFoundException(nameof(ProductionOrderList), request.ManufactureOrderId);
                }
                batch.Source             = "Manufactured";
                batch.ManufactureOrderId = request.ManufactureOrderId;
            }

            Object purchaseOrder;

            // check if purchase order id is defined
            if (request.PurchaseOrderId != null && request.PurchaseOrderId != 0)
            {
                purchaseOrder = await _database.PurchaseOrder
                                .AsNoTracking()
                                .FirstOrDefaultAsync(p => p.Id == request.PurchaseOrderId);

                if (purchaseOrder == null)
                {
                    throw new NotFoundException(nameof(PurchaseOrder), request.PurchaseOrderId);
                }
                batch.Source          = "Procured";
                batch.PurchaseOrderId = request.PurchaseOrderId;
            }

            if (request.ExpiryDate != null)
            {
                batch.ExpiryDate = request.ExpiryDate;
            }
            else if (item.ShelfLife != 0 && item.ShelfLife != null)
            {
                batch.ExpiryDate = request.AvailableFrom.AddDays((double)item.ShelfLife);
            }

            if (request.StorageLocation.Count < 1)
            {
                throw new BelowRequiredMinimumItemException("Stock Batch", 1, "Storage Location");
            }

            float storedQuantity = 0;

            foreach (var data in request.StorageLocation)
            {
                storedQuantity += data.Quantity;

                var store = await _database.StorageLocation.FindAsync(data.StorageId);

                if (store == null)
                {
                    throw new NotFoundException(nameof(StorageLocations), data.StorageId);
                }

                batch.StockBatchStorage.Add(new StockBatchStorage()
                {
                    StorageId = store.Id,
                    Quantity  = data.Quantity
                });
            }

            // check if the sum of items allocated on each storage matchs the quantity stored in main
            if (request.Quantity != storedQuantity)
            {
                throw new InequalMasterDetailQuantityException("Item Batch", request.Quantity, storedQuantity);
            }

            _database.StockBatch.Add(batch);

            await _database.SaveAsync();

            return(batch.Id);
        }
 public static PurchaseOrderItemView Create(StockBatch po_item)
 {
     return(Projection.Compile().Invoke(po_item));
 }
Exemplo n.º 6
0
        public async Task <List <StockBatch> > FetchBatch(List <string> symbols)
        {
            //Error checking for an empty list
            if (symbols.Count() == 0)
            {
                return(null);
            }
            //Capitalize every symbol
            symbols = symbols.ConvertAll(symbol => symbol.ToUpper());

            //Check if the batch request is over 100
            //split it up into chunks if it is
            List <StockBatch> batch = new List <StockBatch>();

            if (symbols.Count() > 100)
            {
                int requests = (symbols.Count() / 100) + 1;
                //For every 100 symbols, do a separate API request
                for (int i = 1; i <= requests; i++)
                {
                    //Check if it's the last request of the batch
                    //If so then a limit must be placed on the "GetRange" function
                    int max;
                    if (i != requests)
                    {
                        max = 100;
                    }
                    else
                    {
                        max = (symbols.Count() - (i * 100 - 100));
                    }
                    string responseBody = await httpClient.GetStringAsync("stock/market/batch?symbols=" + String.Join(",", symbols.GetRange(i * 100 - 100, max)) +
                                                                          "&types=price,previous");

                    //Get only the data needed to return
                    var tempList = JObject.Parse(responseBody);
                    for (int j = 0; j < max; j++)
                    {
                        StockBatch x = new StockBatch();
                        x.symbol = symbols.ElementAt(j + (i * 100 - 100));
                        //Price and changePercent error checking
                        if ((string)tempList[x.symbol]["price"] == null || (string)tempList[x.symbol]["previous"]["changePercent"] == null)
                        {
                            continue;
                        }
                        x.price         = (decimal)tempList[x.symbol]["price"];
                        x.changePercent = (decimal)tempList[x.symbol]["previous"]["changePercent"];
                        batch.Add(x);
                    }
                }
                return(batch);
            }

            //When request is 100 or less stocks
            var response = await httpClient.GetStringAsync("stock/market/batch?symbols=" + string.Join(",", symbols) + "&types=price,previous");

            //Get only the required data fields and return a list of that
            var list = JObject.Parse(response);

            for (int i = 0; i < symbols.Count(); i++)
            {
                StockBatch x = new StockBatch();
                x.symbol = symbols.ElementAt(i);
                //Price and changePercent error checking
                if ((string)list[x.symbol]["price"] == null || (string)list[x.symbol]["previous"]["changePercent"] == null)
                {
                    continue;
                }
                x.price         = (decimal)list[x.symbol]["price"];
                x.changePercent = (decimal)list[x.symbol]["previous"]["changePercent"];
                batch.Add(x);
            }

            return(batch);
        }
Exemplo n.º 7
0
        public ActionResult SaveProductReceived(StockReceivedVM S)
        {
            var w = (from y in db.UserLogins
                     where y.UserID.ToString() == User.Identity.Name
                     select new { y.WorkStationID }).FirstOrDefault();
            var    wn     = db.Warehouses.Where(x => x.WarehouseID == w.WorkStationID).FirstOrDefault();
            string s1     = w.WorkStationID.ToString();
            string s2     = string.Concat(s1 + "0000000");
            int    Trno   = Convert.ToInt32(s2);
            bool   status = false;
            string mes    = "";
            int    v;
            int    PIDMax;
            // var PTID = db.ProductTransactions.Max(p => p.TransactionNo);
            var PTID = (from x in db.ProductTransactions where x.WarehouseID == wn.WarehouseID select x.TransactionNo).DefaultIfEmpty(Trno).Max();

            PIDMax = (PTID + 1);
            v      = PIDMax;

            try
            {
                if (ModelState.IsValid)
                {
                    using (PEPSIEntities dc = new PEPSIEntities())
                    {
                        ProductTransaction dm = new ProductTransaction();
                        {
                            // Here Id   Primary Key of DB table auto increase
                            dm.TransactionTypeID = 3;
                            dm.TransactionNo     = PIDMax;
                            dm.WarehouseID       = S.WarehouseID;
                            dm.FromWarehouse     = S.FromWarehouse;
                            //dm.ToWarehouse = S.WarehouseID;
                            dm.TransactionDate = DateTime.Today;
                            dm.ReferenceNo     = S.ReferenceNo;
                            dm.Status          = "A";
                            //cuser = User.Identity.Name
                            dm.CreatedBy     = User.Identity.Name;
                            dm.CreatedDate   = DateTime.Now;
                            dm.StoreLocation = S.StoreLocation;
                        }
                        dc.ProductTransactions.Add(dm);
                        foreach (var i in S.ProdTrDtl)
                        {
                            ProductTransactionDetail dmd = new ProductTransactionDetail();
                            dmd.ProductID   = i.ProductID; // Not Null
                            dmd.WarehouseID = S.WarehouseID;
                            //dmd.WarehouseID = (int)S.FromWarehouse;
                            dmd.TransactionNo = PIDMax;
                            // dmd.PlantNo =
                            dmd.LineNoPlant            = i.LineNoPlant;
                            dmd.BatchNo                = i.BatchNo;
                            dmd.ExpiryDate             = i.ExpiryDate;
                            dmd.ManufactureDate        = i.ManufactureDate;
                            dmd.Quantity               = (i.Quantity);
                            dmd.EmptyBottleQuantity    = (i.EmptyBottleQuantity);
                            dmd.PlasticBoxQuantity     = i.PlasticBoxQuantity;
                            dmd.BurstBottleQuantity    = (i.BurstBottleQuantity);
                            dmd.BreakageBottleQuantity = (i.BreakageBottleQuantity);
                            //TCases = TCases + i.Quantity;
                            dc.ProductTransactionDetails.Add(dmd);
                            // For Batch
                            StockBatch sb = new StockBatch();
                            sb.ProductID             = i.ProductID;
                            sb.WarehouseID           = (int)S.WarehouseID;
                            sb.BatchNo               = (int)i.BatchNo;
                            sb.ManufacturDate        = (DateTime)i.ManufactureDate;
                            sb.ExpiryDate            = (DateTime)i.ExpiryDate;
                            sb.PlantLineNo           = (int)i.LineNoPlant;
                            sb.PlantNo               = i.PlantNo;
                            sb.IssueQuantity         = i.Quantity;
                            sb.ReceivedQty           = i.Quantity;
                            sb.Status                = "A";
                            sb.WarehouseLocationCode = S.StoreLocation;
                            dc.StockBatches.Add(sb);
                            // Add stock batch detail for transaction history
                            StockBatchDetail sbd = new StockBatchDetail();
                            sbd.WarehouseID            = S.WarehouseID;
                            sbd.BatchNo                = (int)i.BatchNo;
                            sbd.ProductID              = i.ProductID;
                            sbd.TransactionNo          = PIDMax;
                            sbd.Quantity               = i.Quantity;
                            sbd.BurstBottleQuantity    = i.BurstBottleQuantity;
                            sbd.BreakageBottleQuantity = i.BreakageBottleQuantity;
                            sbd.PlasticBoxQuantity     = (int)i.PlasticBoxQuantity;
                            sbd.TransactionDate        = DateTime.Today;
                            sbd.TransactionType        = "Transfer Received";
                            sbd.CreateBy               = User.Identity.Name;
                            sbd.CreateDate             = DateTime.Now;
                            dc.StockBatchDetails.Add(sbd);
                        }
                        foreach (var i in S.ProdTrDtl)
                        {
                            var CF = (from x in dc.Products where x.ProductID == i.ProductID select x).FirstOrDefault();
                            try
                            {
                                var V = (from x in db.ProductBalances where x.ProductID == i.ProductID && x.WarehouseID == S.ToWarehouse select x).FirstOrDefault();
                                //Add Stock

                                var productupdate = dc.spProductBalanceUpdate(i.ProductID,
                                                                              S.WarehouseID,
                                                                              (int)i.Quantity, //change
                                                                              i.BurstBottleQuantity,
                                                                              i.BreakageBottleQuantity,
                                                                              (int)i.PlasticBoxQuantity,
                                                                              i.EmptyBottleQuantity);
                            }
                            catch (Exception ex)
                            {
                                throw ex;
                            }
                        }
                        var pt = dc.ProductTransactions.Where(x => x.WarehouseID == S.FromWarehouse && x.TransactionNo == S.TransactionNo && x.TransactionTypeID == S.TransactionTypeID).FirstOrDefault();

                        pt.UpdatedBy   = User.Identity.Name;
                        pt.UpdatedDate = DateTime.Now;
                        dc.SaveChanges();
                        status = true;
                        dc.Dispose();
                    }
                }
                else
                {
                    status = false;
                }
                return(new JsonResult {
                    Data = new { status = status, mes = mes, v = v }
                });
            }
            catch (Exception ex)
            {
                return(Json(new { status = "error", message = "Transaction No  Not Found" }));
                //  throw ex;
            }
        }
Exemplo n.º 8
0
        public ActionResult SaveData(StockVM S)
        {
            var w = (from y in db.UserLogins
                     where y.UserID.ToString() == User.Identity.Name
                     select new { y.WorkStationID }).FirstOrDefault();
            var wn = db.Warehouses.Where(x => x.WarehouseID == w.WorkStationID).FirstOrDefault();

            //  var PTID = (from x in db.OrderBankSummaries select x.SalesOrderNO).DefaultIfEmpty(610000000).Max();
            //  PIDMax = (PTID + 1);
            string s1      = w.WorkStationID.ToString();
            string s2      = string.Concat(s1 + "0000000");
            string s3      = string.Concat(s1 + Convert.ToString(DateTime.Now.Year) + "000");
            string plantfl = wn.PlantFlag;
            int    batchno = Convert.ToInt32(s3);
            int    Trno    = Convert.ToInt32(s2);
            bool   status  = true;
            string mes     = "";
            int    PIDMax;
            var    PTID = (from x in db.ProductTransactions where x.WarehouseID == wn.WarehouseID select x.TransactionNo).DefaultIfEmpty(Trno).Max();

            PIDMax = (PTID + 1);
            int v = PIDMax;

            if (ModelState.IsValid)
            {
                try
                {
                    using (PEPSIEntities dc = new PEPSIEntities())
                    {
                        CommonChk          ck = new CommonChk();
                        ProductTransaction dm = new ProductTransaction();
                        {
                            // Here Id   Primary Key of DB table auto increase
                            //var PTID = db.ProductTransactions.DefaultIfEmpty(100).Max(p => p.TransactionNo);
                            dm.TransactionTypeID = S.TransactionTypeID;
                            dm.TransactionNo     = PIDMax;//
                            dm.WarehouseID       = S.WarehouseID;
                            dm.FromWarehouse     = S.WarehouseID;
                            dm.ToWarehouse       = S.ToWarehouse;
                            dm.TransactionDate   = DateTime.Today;
                            dm.ReferenceNo       = S.ReferenceNo;
                            dm.Remarks           = S.Remarks;
                            if (S.TransactionTypeID != 1)
                            {
                                if (S.TransactionTypeID == 1)
                                {
                                    dm.Status = "T";
                                }
                                if (S.TransactionTypeID == 6)
                                {
                                    dm.Status = "D";
                                }
                                if (S.TransactionTypeID == 10)
                                {
                                    dm.Status = "B";
                                }
                                if (S.TransactionTypeID == 11)
                                {
                                    dm.Status = "W";
                                }
                            }
                            else
                            {
                                dm.Status = "A";
                            }
                            //cuser = User.Identity.Name
                            dm.CreatedBy     = User.Identity.Name;
                            dm.CreatedDate   = DateTime.Now;
                            dm.StoreLocation = S.StoreLocation;
                        }
                        dc.ProductTransactions.Add(dm);
                        // Transfer Note
                        if (S.TransactionTypeID == 2)
                        {
                            ProductTransferNote ptn = new ProductTransferNote();
                            {
                                var PTNoteID = (from x in db.ProductTransferNotes where x.WarehouseID == w.WorkStationID select x.SLNo).DefaultIfEmpty(1).Max();
                                int TrNoteId = Convert.ToInt32((PTNoteID)) + 1;
                                ptn.WarehouseID   = S.WarehouseID;
                                ptn.TransactionNo = PIDMax;
                                ptn.VehicleID     = S.VehicleID;
                                if (S.VehicleID == 1)
                                {
                                    ptn.DriverID = 0;
                                }
                                else
                                {
                                    ptn.DriverID = S.DriverID;
                                }
                                ptn.Remarks    = S.Remarks;
                                ptn.SLNo       = TrNoteId;
                                ptn.CreateBy   = User.Identity.Name;
                                ptn.CreateDate = DateTime.Now;
                            }
                            dc.ProductTransferNotes.Add(ptn);
                        }
                        // Agency Fare Amount
                        if (S.VehicleID == 1 && S.TransactionTypeID == 2)
                        {
                            TransportAgencyandFareSetup tf = new TransportAgencyandFareSetup();


                            {
                                tf.WarehouseID   = S.WarehouseID;
                                tf.TAID          = (int)S.TAID;
                                tf.ChallanNumber = PIDMax;
                                tf.ChallanDate   = DateTime.Today;
                                tf.CustomerName  = ck.GetCustomerName((int)S.ToWarehouse, "N");
                                tf.Address       = ck.GetCustomerName((int)S.ToWarehouse, "N");
                                tf.VechileNo     = S.VehicleID.ToString();
                                tf.FareAmnt      = (decimal)S.FareAmnt;
                                tf.Remarks       = "Hired Truck";
                                tf.TotalCases    = S.TotCase;
                                tf.Status        = "No";
                                tf.EnteredBy     = User.Identity.Name;
                                tf.EnteredDate   = System.DateTime.Now;
                            }
                            dc.TransportAgencyandFareSetups.Add(tf);
                        }

                        if (S.TransactionTypeID == 2)
                        {
                            foreach (var j in S.ProdTrDtl)
                            {
                                //checking  stock balace
                                if (ck.stockchkcs(j.ProductID, S.WarehouseID, (int)j.Quantity) == true)
                                {
                                    //status = false;
                                }
                                else
                                {
                                    status = false;
                                };
                            }
                            ;
                        }
                        if (status == true)
                        {
                            foreach (var i in S.ProdTrDtl)
                            {
                                var CF    = (from x in dc.Products where x.ProductID == i.ProductID select x).FirstOrDefault();
                                var batch = (from x in dc.StockBatches where x.ProductID == i.ProductID && x.WarehouseID == S.WarehouseID select x.BatchNo).DefaultIfEmpty(batchno).Max() + 1;
                                ProductTransactionDetail dmd = new ProductTransactionDetail();
                                dmd.ProductID     = i.ProductID; // Not Null
                                dmd.WarehouseID   = S.WarehouseID;
                                dmd.TransactionNo = PIDMax;
                                dmd.LineNoPlant   = i.LineNoPlant;
                                if (S.TransactionTypeID != 1)
                                {
                                    dmd.BatchNo = i.BatchNo;
                                }
                                else
                                {
                                    dmd.BatchNo = batch;
                                }

                                if (plantfl == "Y")
                                {
                                    int plant = (int)wn.PlantNo;
                                    dmd.PlantNo = plant;
                                }
                                dmd.ManufactureDate = i.ManufactureDate;
                                //DateTime d = Convert.ToDateTime(i.ManufactureDate);
                                if (CF.Expirydays == null)
                                {
                                    dmd.ExpiryDate = i.ManufactureDate;
                                }
                                else
                                {
                                    dmd.ExpiryDate = Convert.ToDateTime(i.ManufactureDate).AddDays((int)CF.Expirydays);// + 10;//  (int)CF.Expirydays;  //i.ExpiryDate;
                                }
                                dmd.Quantity               = (i.Quantity);
                                dmd.EmptyBottleQuantity    = (i.EmptyBottleQuantity);
                                dmd.PlasticBoxQuantity     = i.PlasticBoxQuantity;
                                dmd.BurstBottleQuantity    = (i.BurstBottleQuantity);
                                dmd.BreakageBottleQuantity = (i.BreakageBottleQuantity);
                                //TCases = TCases + i.Quantity;
                                dc.ProductTransactionDetails.Add(dmd);
                                // For Batch
                                StockBatch sb = new StockBatch();
                                sb.ProductID   = i.ProductID;
                                sb.WarehouseID = S.WarehouseID;
                                //sb.BatchNo = batch;

                                if (S.TransactionTypeID != 1)
                                {
                                    var t = db.StockBatches.Where(x => x.WarehouseID == S.WarehouseID && x.ProductID == i.ProductID && x.BatchNo == i.BatchNo).FirstOrDefault();
                                    sb.BatchRefNo     = t.BatchRefNo;
                                    sb.ManufacturDate = t.ManufacturDate;
                                    sb.ExpiryDate     = t.ExpiryDate;
                                    sb.PlantLineNo    = t.PlantLineNo;
                                    sb.BatchNo        = (int)i.BatchNo; // Add
                                    sb.PlantNo        = t.PlantNo;
                                    if (S.TransactionTypeID == 2)
                                    {
                                        sb.Status = "T";
                                    }
                                    if (S.TransactionTypeID == 6)
                                    {
                                        sb.Status = "D";
                                    }
                                    if (S.TransactionTypeID == 10)
                                    {
                                        sb.Status = "B";
                                    }
                                    if (S.TransactionTypeID == 11)
                                    {
                                        sb.Status = "W";
                                    }
                                }
                                else
                                {
                                    sb.ManufacturDate        = (DateTime)i.ManufactureDate;
                                    sb.ExpiryDate            = Convert.ToDateTime(i.ManufactureDate).AddDays((int)CF.Expirydays); //(DateTime)i.ExpiryDate;
                                    sb.PlantLineNo           = (int)i.LineNoPlant;
                                    sb.PlantNo               = i.PlantNo;
                                    sb.BatchRefNo            = i.BatchNo;
                                    sb.BatchNo               = batch; // Add
                                    sb.WarehouseLocationCode = S.StoreLocation;
                                    sb.Status = "A";
                                }
                                sb.IssueQuantity = (decimal)i.Quantity;
                                sb.ReceivedQty   = i.Quantity;
                                dc.StockBatches.Add(sb);
                                // Add stock batch detail for transaction history
                                StockBatchDetail sbd = new StockBatchDetail();
                                sbd.WarehouseID            = S.WarehouseID;
                                sbd.ProductID              = i.ProductID;
                                sbd.TransactionNo          = PIDMax;
                                sbd.Quantity               = i.Quantity;
                                sbd.BurstBottleQuantity    = i.BurstBottleQuantity;
                                sbd.BreakageBottleQuantity = i.BreakageBottleQuantity;
                                sbd.PlasticBoxQuantity     = (int)i.PlasticBoxQuantity;
                                sbd.TransactionDate        = DateTime.Today;
                                sbd.CreateBy               = User.Identity.Name;
                                sbd.CreateDate             = DateTime.Now;
                                if (S.TransactionTypeID == 2)
                                {
                                    sbd.BatchNo         = i.BatchNo;
                                    sbd.TransactionType = "Product Transfer";
                                }
                                else if (S.TransactionTypeID == 10)
                                {
                                    sbd.BatchNo         = i.BatchNo;
                                    sbd.TransactionType = "Warehouse Burst";
                                }
                                else if (S.TransactionTypeID == 11)
                                {
                                    sbd.BatchNo         = i.BatchNo;
                                    sbd.TransactionType = "Write Off";
                                }
                                else if (S.TransactionTypeID == 6)
                                {
                                    sbd.BatchNo         = i.BatchNo;
                                    sbd.TransactionType = "Adjustment";
                                }
                                else
                                {
                                    sbd.TransactionType = "Product Received";
                                    sbd.BatchNo         = batch;
                                }
                                dc.StockBatchDetails.Add(sbd);
                            }
                        }

                        //db.SaveChanges();
                        // dc.SaveChanges();
                        foreach (var i in S.ProdTrDtl)
                        {
                            var CF = (from x in dc.Products where x.ProductID == i.ProductID select x).FirstOrDefault();
                            try
                            {
                                if (S.TransactionTypeID == 1)
                                {
                                    var productupdate = dc.spProductBalanceUpdate(i.ProductID,
                                                                                  S.WarehouseID,
                                                                                  (int)i.Quantity, // change
                                                                                  i.BurstBottleQuantity,
                                                                                  i.BreakageBottleQuantity,
                                                                                  (int)i.PlasticBoxQuantity, //change
                                                                                  i.EmptyBottleQuantity);
                                }

                                if (S.TransactionTypeID != 1 && status == true)
                                {
                                    //Deduct Stock
                                    var productupdate = dc.spProductBalanceDecrease(i.ProductID,
                                                                                    S.WarehouseID,
                                                                                    (int)i.Quantity, //change
                                                                                    i.BurstBottleQuantity,
                                                                                    i.BreakageBottleQuantity,
                                                                                    (int)i.PlasticBoxQuantity,
                                                                                    i.EmptyBottleQuantity);
                                }
                            }
                            catch (Exception ex)
                            {
                                throw ex;
                            }
                        }
                        if (status == true)
                        {
                            dc.SaveChanges();
                            status = true;
                            dc.Dispose();
                        }
                    }
                    // return new JsonResult { Data = new { status = status, mes = mes } };
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            else
            {
                status = false;
            }
            return(new JsonResult {
                Data = new { status = status, mes = mes, v = v }
            });                                                                        //, v = v } };
        }
Exemplo n.º 9
0
        public async Task <uint> Handle(CreatePurchaseRecievingCommand request, CancellationToken cancellationToken)
        {
            PurchaseOrder purchaseOrder = await _database.PurchaseOrder
                                          .Include(p => p.StockBatch)
                                          .Where(p => p.Id == request.PurchaseOrderId)
                                          .FirstOrDefaultAsync();

            if (purchaseOrder == null)
            {
                throw new NotFoundException(nameof(PurchaseOrder), request.PurchaseOrderId);
            }

            foreach (var item in request.PurchaseOrderItems)
            {
                StockBatch lot = purchaseOrder.StockBatch.FirstOrDefault(l => l.Id == item.LotId);

                if (lot == null)
                {
                    throw new NotFoundException("Batch ", item.LotId);
                }

                //if recieved amount is equal to total recieved quantity
                if (item.Quantity == lot.Quantity)
                {
                    lot.Status           = "Recieved";
                    lot.ArrivalDate      = DateTime.Now;
                    purchaseOrder.Status = "Recieved";

                    // if recieved amount is less than total lot quantity
                }
                else if (item.Quantity < lot.Quantity && item.Quantity >= 1)
                {
                    var difference = lot.Quantity - item.Quantity;

                    var storages = await _database.StockBatch
                                   .Include(s => s.Item)
                                   .Include(s => s.StockBatchStorage)
                                   .Where(s => s.Id == lot.Id)
                                   .FirstOrDefaultAsync();

                    await _Mediator.Send(new CreateStockBatchCommand()
                    {
                        PurchaseOrderId = storages.PurchaseOrderId,
                        ItemId          = storages.ItemId,
                        Quantity        = difference,
                        Status          = "Planed",
                        UnitCost        = storages.UnitCost,
                        AvailableFrom   = storages.AvailableFrom,
                        StorageLocation = new List <NewStockBatchStorageDto> ()
                        {
                            new NewStockBatchStorageDto()
                            {
                                Quantity  = difference,
                                StorageId = storages.Item.DefaultStorageId
                            }
                        }
                    });

                    storages.Quantity    = storages.Quantity - difference;
                    storages.Status      = "Recieved";
                    storages.ArrivalDate = DateTime.Now;

                    foreach (var location in storages.StockBatchStorage)
                    {
                        if (location.Quantity > difference)
                        {
                            location.Quantity = location.Quantity - difference;
                            break;
                        }

                        difference = difference - location.Quantity;

                        _database.StockBatchStorage.Remove(location);

                        if (difference == 0)
                        {
                            break;
                        }
                    }

                    _database.StockBatch.Update(storages);
                }

                _database.PurchaseOrder.Update(purchaseOrder);
            }

            await _database.SaveAsync();

            return(request.PurchaseOrderId);
        }