public ActionResult ProductionEntry(int?id, int?foodMenuType, int?productionFormulaId, string type)
        {
            if (UserRolePermissionForPage.Add == true || UserRolePermissionForPage.Edit == true || UserRolePermissionForPage.View == true)
            {
                ProductionEntryModel productionEntryModel = new ProductionEntryModel();
                TempData["foodMenuType"] = foodMenuType;

                if (id > 0)
                {
                    ViewBag.ActionType   = type;
                    productionEntryModel = _iProductionEntryService.GetProductionEntryById(Convert.ToInt32(id));
                }
                else
                {
                    if (Convert.ToInt32(productionFormulaId) > 0)
                    {
                        productionEntryModel = _iProductionEntryService.GetProductionFormulaById(Convert.ToInt32(productionFormulaId));
                    }
                    productionEntryModel.ProductionDate = DateTime.UtcNow.AddMinutes(LoginInfo.Timeoffset);
                    productionEntryModel.FoodmenuType   = Convert.ToInt32(foodMenuType);
                }
                productionEntryModel.ProductionFormulaList = _iDropDownService.GetProductionFormulaList(Convert.ToInt32(foodMenuType));
                productionEntryModel.FoodMenuList          = _iDropDownService.GetFoodMenuListByFoodmenuType(3);
                productionEntryModel.IngredientList        = _iDropDownService.GetIngredientList();
                productionEntryModel.StoreList             = _iDropDownService.GetStoreList();
                ViewBag.SelectedStore = productionEntryModel.StoreList.Where(x => x.Selected == true).Select(x => x.Value).SingleOrDefault();

                return(View(productionEntryModel));
            }
            else
            {
                return(RedirectToAction("NotFound", "Error"));
            }
        }
        public JsonResult GetProductionEntryById(int id)
        {
            ProductionEntryModel productionEntryModel = new ProductionEntryModel();

            productionEntryModel = _iProductionEntryService.GetProductionEntryById(id);
            return(Json(new { productionEntryModel = productionEntryModel }));
        }
        public JsonResult GetProductionFormulaById(int id)
        {
            ProductionEntryModel productionEntryModel = new ProductionEntryModel();

            productionEntryModel = _iProductionEntryService.GetProductionFormulaById(id);
            //  productionEntryModel.ProductionDate = DateTime.UtcNow.AddMinutes(LoginInfo.Timeoffset);
            return(Json(new { productionEntryModel = productionEntryModel }));
        }
        public ProductionEntryModel GetProductionFormulaById(int id)
        {
            ProductionEntryModel productionEntryModel = new ProductionEntryModel();

            productionEntryModel = iProductionEntryRepository.GetProductionFormulaById(id);
            productionEntryModel.productionEntryFoodMenuModels   = iProductionEntryRepository.GetProductionFormulaFoodMenuDetails(id);
            productionEntryModel.productionEntryIngredientModels = iProductionEntryRepository.GetProductionFormulaIngredientDetails(id);
            return(productionEntryModel);
        }
        public ProductionEntryModel GetProductionFormulaById(int id)
        {
            ProductionEntryModel productionEntryModel = new ProductionEntryModel();

            using (SqlConnection con = new SqlConnection(_ConnectionString.Value.ConnectionString))
            {
                var query = "select PF.Id AS ProductionFormulaId,PF.FormulaName AS ProductionFormulaName,PF.[BatchSize],PF.[BatchSizeUnitId],PF.FoodmenuType,PF.IsActive,U.UnitShortName as BatchSizeUnitName from ProductionFormula PF " +
                            "Inner Join [Units] U On U.Id=PF.BatchSizeUnitId Where PF.IsDeleted=0 And PF.Id=" + id;
                productionEntryModel = con.QueryFirstOrDefault <ProductionEntryModel>(query);
            }
            return(productionEntryModel);
        }
        public ProductionEntryModel GetProductionEntryById(int id)
        {
            ProductionEntryModel productionEntryModel = new ProductionEntryModel();

            using (SqlConnection con = new SqlConnection(_ConnectionString.Value.ConnectionString))
            {
                var query = " select PE.Id, PE.ProductionFormulaId,PF.FormulaName AS ProductionFormulaName, PE.ProductionDate,PE.ActualBatchSize,PE.VariationNotes,PE.Notes,PE.Status, PF.[BatchSize],PF.[BatchSizeUnitId], " +
                            " PE.FoodmenuType,U.UnitShortName as BatchSizeUnitName,PE.AssetEventId,AE.EventName,PE.Storeid, S.StoreName " +
                            " from ProductionEntry PE Inner Join ProductionFormula PF On PF.Id=PE.ProductionFormulaId  inner join Store S on S.ID = PE.Storeid " +
                            " Inner Join [Units] U On U.Id=PF.BatchSizeUnitId  left join AssetEvent AE On AE.Id= PE.AssetEventId Where PE.IsDeleted=0 And PE.Id=" + id;
                productionEntryModel = con.QueryFirstOrDefault <ProductionEntryModel>(query);
            }
            return(productionEntryModel);
        }
        public ActionResult View(long?id)
        {
            ProductionEntryModel productionEntryModel = new ProductionEntryModel();

            if (UserRolePermissionForPage.View == true)
            {
                productionEntryModel = _iProductionEntryService.GetProductionEntryViewById(Convert.ToInt32(id));
                return(View(productionEntryModel));
            }
            else
            {
                return(RedirectToAction("NotFound", "Error"));
            }
        }
        public ActionResult ProductionEntry(ProductionEntryModel productionEntryModel)
        {
            int    result = 0;
            string productionEntryMessage = string.Empty;

            if (productionEntryModel != null)
            {
                if (productionEntryModel.productionEntryFoodMenuModels.Count > 0 && productionEntryModel.productionEntryIngredientModels.Count > 0)
                {
                    if (productionEntryModel.Id > 0)
                    {
                        if (productionEntryModel.Status == 3)
                        {
                            productionEntryModel.ProductionCompletionDate = DateTime.UtcNow.AddMinutes(LoginInfo.Timeoffset);
                        }
                        else
                        {
                            productionEntryModel.ProductionCompletionDate = DateTime.UtcNow.AddMinutes(LoginInfo.Timeoffset);
                        }
                        result = _iProductionEntryService.UpdateProductionEntry(productionEntryModel);
                        if (result > 0)
                        {
                            productionEntryMessage = _locService.GetLocalizedHtmlString("EditSuccss");
                        }
                    }
                    else
                    {
                        result = _iProductionEntryService.InsertProductionEntry(productionEntryModel);
                        if (result > 0)
                        {
                            productionEntryMessage = _locService.GetLocalizedHtmlString("SaveSuccess");
                        }
                    }
                }
                else
                {
                    productionEntryMessage = _locService.GetLocalizedHtmlString("ValidProductionFormula");
                    return(Json(new { error = true, message = productionEntryMessage, status = 201 }));
                }
            }
            else
            {
                productionEntryMessage = _locService.GetLocalizedHtmlString("ValidProductionFormula");
                return(Json(new { error = true, message = productionEntryMessage, status = 201 }));
            }
            return(Json(new { error = false, message = productionEntryMessage, status = 200 }));
        }
        public int UpdateProductionEntry(ProductionEntryModel productionEntryModel)
        {
            int result = 0, deleteFoodMenuResult = 0, deleteIngredientResult = 0, foodmenudetails = 0, ingredientdetails = 0;

            using (SqlConnection con = new SqlConnection(_ConnectionString.Value.ConnectionString))
            {
                con.Open();
                SqlTransaction sqltrans = con.BeginTransaction();
                var            query    = "Update [dbo].[ProductionEntry] set " +
                                          " ProductionDate = @ProductionDate " +
                                          ",ProductionCompletionDate = @ProductionCompletionDate " +
                                          ",ActualBatchSize = @ActualBatchSize " +
                                          ",Status = @Status " +
                                          ",VariationNotes = @VariationNotes " +
                                          ",Notes = @Notes " +
                                          ",StoreId = @StoreId " +
                                          "  ,[UserIdUpdated] = " + LoginInfo.Userid + " " +
                                          "  ,[DateUpdated]  = GetUtcDate()  where id= " + productionEntryModel.Id + ";";
                result = con.Execute(query, productionEntryModel, sqltrans, 0, System.Data.CommandType.Text);

                if (result > 0)
                {
                    if (productionEntryModel.FoodMenuDeletedId != null)
                    {
                        foreach (var item in productionEntryModel.FoodMenuDeletedId)
                        {
                            var deleteQuery = $"update ProductionEntryFoodmenu set IsDeleted = 1, UserIdDeleted = " + LoginInfo.Userid + ", DateDeleted = GetutcDate() where id = " + item + ";";
                            deleteFoodMenuResult = con.Execute(deleteQuery, null, sqltrans, 0, System.Data.CommandType.Text);
                        }
                    }


                    if (productionEntryModel.IngredientDeletedId != null)
                    {
                        foreach (var item in productionEntryModel.IngredientDeletedId)
                        {
                            var deleteQuery = $"update ProductionEntryIngredient set IsDeleted = 1, UserIdDeleted = " + LoginInfo.Userid + ", DateDeleted = GetutcDate() where id = " + item + ";";
                            deleteIngredientResult = con.Execute(deleteQuery, null, sqltrans, 0, System.Data.CommandType.Text);
                        }
                    }

                    foreach (var item in productionEntryModel.productionEntryFoodMenuModels)
                    {
                        var queryDetails = string.Empty;
                        if (item.PEFoodMenuId > 0)
                        {
                            queryDetails = "Update [dbo].[ProductionEntryFoodmenu] set " +
                                           "[FoodMenuId]		  	 = "+ item.FoodMenuId +
                                           //",[ExpectedOutput]     = " + item.ExpectedOutput +
                                           ",[AllocationOutput]     = " + item.AllocationOutput +
                                           ",[ActualOutput]     = " + item.ActualOutput +
                                           " ,[UserIdUpdated] = " + LoginInfo.Userid + "," +
                                           " [DateUpdated] = GetUTCDate() " +
                                           " where id = " + item.PEFoodMenuId + ";";
                        }
                        foodmenudetails = con.Execute(queryDetails, null, sqltrans, 0, System.Data.CommandType.Text);
                    }

                    foreach (var item in productionEntryModel.productionEntryIngredientModels)
                    {
                        var queryDetails = string.Empty;
                        if (item.PEIngredientId > 0)
                        {
                            queryDetails = "Update [dbo].[ProductionEntryIngredient] set " +
                                           "[IngredientId]		  	 = "+ item.IngredientId +
                                           //",[IngredientQty]     = " + item.IngredientQty +
                                           ",[AllocationIngredientQty]     = " + item.AllocationIngredientQty +
                                           ",[ActualIngredientQty]     = " + item.ActualIngredientQty +
                                           " ,[UserIdUpdated] = " + LoginInfo.Userid + "," +
                                           " [DateUpdated] = GetUTCDate() " +
                                           " where id = " + item.PEIngredientId + ";";
                        }
                        ingredientdetails = con.Execute(queryDetails, null, sqltrans, 0, System.Data.CommandType.Text);
                    }

                    if (foodmenudetails > 0 && ingredientdetails > 0)
                    {
                        sqltrans.Commit();
                        if (productionEntryModel.Status == 2)
                        {
                            CommonRepository commonRepository = new CommonRepository(_ConnectionString);
                            string           sResult          = commonRepository.InventoryPush("PE_Ingredient", Convert.ToInt32(productionEntryModel.Id));
                        }
                        if (productionEntryModel.Status == 3)
                        {
                            CommonRepository commonRepository = new CommonRepository(_ConnectionString);
                            string           sResult          = commonRepository.InventoryPush("PE_Food", productionEntryModel.Id);
                        }
                    }
                    else
                    {
                        sqltrans.Rollback();
                    }
                }
                else
                {
                    sqltrans.Rollback();
                }
            }
            return(result);
        }
        public int InsertProductionEntry(ProductionEntryModel productionEntryModel)
        {
            int result = 0;
            int foodMenuResult = 0, ingredientResult = 0;

            using (SqlConnection con = new SqlConnection(_ConnectionString.Value.ConnectionString))
            {
                con.Open();
                SqlTransaction sqltrans = con.BeginTransaction();

                var refNoQuery  = $"SELECT ISNULL(MAX(convert(int,ReferenceNo)),0) + 1  FROM  ProductionEntry where foodmenutype=" + productionEntryModel.FoodmenuType + " and  isdeleted = 0; ";
                var referenceNo = con.ExecuteScalar <string>(refNoQuery, null, sqltrans, 0, System.Data.CommandType.Text);

                var query = "INSERT INTO [dbo].[ProductionEntry] " +
                            "  ([ProductionFormulaId] " +
                            " ,[FoodmenuType] " +
                            " ,[ReferenceNo] " +
                            " ,[ProductionDate] " +
                            " ,[ProductionCompletionDate] " +
                            " ,[ActualBatchSize] " +
                            " ,[Status] " +
                            " ,[VariationNotes] " +
                            " ,[Notes] " +
                            " ,[StoreId] " +
                            " ,[UserIdInserted]  " +
                            " ,[DateInserted]   " +
                            " ,[IsDeleted])     " +
                            "   VALUES           " +
                            "  (@ProductionFormulaId " +
                            " ,@FoodmenuType " +
                            " ,'" + referenceNo + "' " +
                            " ,@ProductionDate " +
                            " ,GetUtcDate() " +
                            " ,@ActualBatchSize " +
                            " ,@Status " +
                            " ,@VariationNotes " +
                            " ,@Notes " +
                            " ,@StoreId, " +
                            "" + LoginInfo.Userid + "," +
                            "   GetUtcDate(),    " +
                            "   0); SELECT CAST(SCOPE_IDENTITY() as int); ";
                result = con.ExecuteScalar <int>(query, productionEntryModel, sqltrans, 0, System.Data.CommandType.Text);

                if (result > 0)
                {
                    foreach (var foodmenu in productionEntryModel.productionEntryFoodMenuModels)
                    {
                        var queryDetails = "INSERT INTO [dbo].[ProductionEntryFoodmenu]" +
                                           "  ([ProductionEntryId] " +
                                           " ,[FoodMenuId] " +
                                           " ,[ExpectedOutput] " +
                                           " ,[AllocationOutput] " +
                                           " ,[ActualOutput] " +
                                           " ,[UserIdInserted]" +
                                           " ,[DateInserted]" +
                                           " ,[IsDeleted])   " +
                                           "VALUES           " +
                                           "(" + result + "," +
                                           foodmenu.FoodMenuId + "," +
                                           foodmenu.ExpectedOutput + "," +
                                           foodmenu.AllocationOutput + "," +
                                           foodmenu.ActualOutput + "," +
                                           LoginInfo.Userid + ",GetUtcDate(),0);";
                        foodMenuResult = con.Execute(queryDetails, null, sqltrans, 0, System.Data.CommandType.Text);
                    }

                    foreach (var ingredient in productionEntryModel.productionEntryIngredientModels)
                    {
                        var queryDetails = "INSERT INTO [dbo].[ProductionEntryIngredient]" +
                                           "  ([ProductionEntryId] " +
                                           " ,[IngredientId] " +
                                           " ,[IngredientQty] " +
                                           " , [AllocationIngredientQty] " +
                                           " ,[ActualIngredientQty] " +
                                           " ,[UserIdInserted]" +
                                           " ,[DateInserted]" +
                                           " ,[IsDeleted])   " +
                                           "VALUES           " +
                                           "(" + result + "," +
                                           ingredient.IngredientId + "," +
                                           ingredient.IngredientQty + "," +
                                           ingredient.AllocationIngredientQty + "," +
                                           ingredient.ActualIngredientQty + "," +
                                           LoginInfo.Userid + ",GetUtcDate(),0);";
                        ingredientResult = con.Execute(queryDetails, null, sqltrans, 0, System.Data.CommandType.Text);
                    }

                    if (foodMenuResult > 0 && ingredientResult > 0)
                    {
                        sqltrans.Commit();
                        if (productionEntryModel.Status == 2)
                        {
                            CommonRepository commonRepository = new CommonRepository(_ConnectionString);
                            string           sResult          = commonRepository.InventoryPush("PE_Ingredient", result);
                        }
                        if (productionEntryModel.Status == 3)
                        {
                            CommonRepository commonRepository = new CommonRepository(_ConnectionString);
                            string           sResult          = commonRepository.InventoryPush("PE_Food", result);
                        }
                    }
                    else
                    {
                        sqltrans.Rollback();
                    }
                }
                else
                {
                    sqltrans.Rollback();
                }
            }
            return(result);
        }
 public int UpdateProductionEntry(ProductionEntryModel productionEntryModel)
 {
     return(iProductionEntryRepository.UpdateProductionEntry(productionEntryModel));
 }
 public int InsertProductionEntry(ProductionEntryModel productionEntryModel)
 {
     return(iProductionEntryRepository.InsertProductionEntry(productionEntryModel));
 }