Beispiel #1
0
        public bool Insert(StockTransferModels model, ref string msg)
        {
            bool result = true;

            using (NuWebContext cxt = new NuWebContext())
            {
                using (var transaction = cxt.Database.BeginTransaction())
                {
                    try
                    {
                        ResultModels resultModels = new ResultModels();

                        I_Stock_Transfer item            = new I_Stock_Transfer();
                        string           StockTransferId = Guid.NewGuid().ToString();

                        item.Id = StockTransferId;
                        item.StockTransferNo = CommonHelper.GetGenNo(Commons.ETableZipCode.StockTransfer, model.IssueStoreId);

                        item.IssueStoreId   = model.IssueStoreId;
                        item.ReceiveStoreId = model.ReceiveStoreId;

                        item.RequestBy   = model.RequestBy;
                        item.RequestDate = model.RequestDate;

                        item.IssueBy   = model.IssueBy;
                        item.IssueDate = model.IssueDate;

                        item.ReceiveBy   = model.ReceiveBy;
                        item.ReceiveDate = model.ReceiveDate;

                        item.IsActive = model.IsActive;

                        List <I_Stock_Transfer_Detail> listInsert = new List <I_Stock_Transfer_Detail>();
                        I_Stock_Transfer_Detail        itemDetail = null;
                        //for stock
                        List <InventoryTransferModels> lstInventory = new List <InventoryTransferModels>();
                        InventoryTransferModels        inventory    = null;
                        foreach (var STDetailItem in model.ListItem)
                        {
                            itemDetail = new I_Stock_Transfer_Detail();

                            itemDetail.Id = Guid.NewGuid().ToString();
                            itemDetail.StockTransferId = StockTransferId;
                            itemDetail.IngredientId    = STDetailItem.IngredientId;
                            itemDetail.RequestQty      = STDetailItem.RequestQty;
                            itemDetail.ReceiveQty      = STDetailItem.ReceiveQty;
                            itemDetail.IssueQty        = STDetailItem.IssueQty;
                            itemDetail.UOMId           = STDetailItem.UOMId;
                            itemDetail.ReceiveBaseQty  = (STDetailItem.ReceiveQty * STDetailItem.Rate);
                            itemDetail.IssueBaseQty    = (STDetailItem.IssueQty * STDetailItem.Rate);

                            listInsert.Add(itemDetail);

                            inventory = new InventoryTransferModels();
                            inventory.IssueStoreId   = model.IssueStoreId;
                            inventory.ReceiveStoreId = model.ReceiveStoreId;
                            inventory.IngredientId   = STDetailItem.IngredientId;
                            inventory.IssueQty       = (STDetailItem.IssueQty * STDetailItem.Rate);
                            inventory.ReceiveQty     = (STDetailItem.ReceiveQty * STDetailItem.Rate);

                            inventory.Price = 0;

                            lstInventory.Add(inventory);
                        }

                        cxt.I_Stock_Transfer.Add(item);
                        cxt.I_Stock_Transfer_Detail.AddRange(listInsert);
                        cxt.SaveChanges();
                        transaction.Commit();

                        //Update inventory
                        _inventoryFactory.UpdateInventoryForTransfer(lstInventory, StockTransferId, ref resultModels);
                        _logger.Info(string.Format("UpdateInventoryForTransfer: [{0}] - [{1}] - [{2}] ", resultModels.IsOk, StockTransferId, resultModels.Message));
                    }
                    catch (Exception ex)
                    {
                        _logger.Error(ex);
                        result = false;
                        transaction.Rollback();
                    }
                    finally
                    {
                        if (cxt != null)
                        {
                            cxt.Dispose();
                        }
                    }
                }
            }
            return(result);
        }