/// <summary>
 /// Insert Method for Insert tbl_MasterDetailInfo Data
 /// </summary>
 /// <param name="model"></param>
 /// <returns></returns>
 private static int InsertToMasterDetailInfo(MasterDetailInfoModel model)
 {
     try
     {
         tbl_MasterDetailInfo entity = new tbl_MasterDetailInfo()
         {
             CustomerID = model.CustomerID,
             CustomerName = model.CustomerName,
             Address = model.Address,
             LoadDate = model.LoadDate
         };
         //Add the object to Entity Set
         _db.AddTotbl_MasterDetailInfo(entity);
         //Save the Entity Set
         return _db.SaveChanges();
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
        public static JsonResult AjaxSave(MasterDetailInfoViewModel model, List<DetailInfoViewModel> modelList)
        {
            JsonResult jsonResult = new JsonResult();

            try
            {
                if (model.CustomerId != 0 && model.CustomerName != null && model.Address != null)
                {

                    if (model != null)
                    {
                        if (modelList.Any())
                        {
                            //Save MasterDetails Data

                            MasterDetailInfoModel masterDetailInfoModel = new MasterDetailInfoModel()
                            {
                                CustomerID = model.CustomerId,
                                CustomerName = model.CustomerName,
                                Address = model.Address,
                                LoadDate = DateTime.Now
                            };

                            List<DetailInfoModel> detailInfoModelList = new List<DetailInfoModel>();

                            ////Master Data Insert
                            //if (InsertToMasterDetailInfo(masterDetailInfoModel) > 0)
                            //{
                            //var lastInsetMasterDetailInfo = _db.tbl_MasterDetailInfo.FirstOrDefault(x => x.CustomerID == model.CustomerId && x.CustomerName == model.CustomerName);

                            ////Details Data Insert
                            //if (lastInsetMasterDetailInfo != null)
                            //{
                            foreach (var item in modelList)
                            {
                                DetailInfoModel detailInfoModel = new DetailInfoModel()
                                {
                                    //CustomerID = model.CustomerId,
                                    ItemSizeID = item.ItemSizeId,
                                    Quantity = item.Quantity
                                };

                                //if (InsertToDetailInfo(detailInfoModel) > 0)
                                //{
                                //    continue;
                                //}
                                //else
                                //{
                                //    break;
                                //}

                                detailInfoModelList.Add(detailInfoModel);
                            }
                            //}
                            //}

                            //Save MasterDetails Data

                            if (InsertMasterDetailInfoBySP(masterDetailInfoModel, detailInfoModelList))
                            {
                                jsonResult.msg = "Data saved successfully to server.";
                                jsonResult.status = MessageType.success.ToString();
                                return jsonResult;
                            }
                            else
                            {
                                jsonResult.msg = "Data can not saved successfully to server.";
                                jsonResult.status = MessageType.warn.ToString();
                                return jsonResult;
                            }
                        }
                        else
                        {
                            //Details Data Null Message
                            jsonResult.msg = "Details data could not found.";
                            jsonResult.status = MessageType.warn.ToString();
                            return jsonResult;
                        }
                    }
                    else
                    {
                        //Master Data Null Message
                        jsonResult.msg = "Master data could not found.";
                        jsonResult.status = MessageType.warn.ToString();
                        return jsonResult;
                    }

                }

                jsonResult.msg = "Data is required";
                jsonResult.status = MessageType.info.ToString();
                return jsonResult;

            }
            catch (Exception ex)
            {
                jsonResult.msg = ExceptionHelper.ExceptionMessageFormat(ex);
                jsonResult.status = MessageType.error.ToString();
                return jsonResult;
            }
        }
        /// <summary>
        /// Insert Master DetailInfo By SP
        /// </summary>
        /// <param name="masterDetailInfoModel"></param>
        /// <param name="detailInfoModelList"></param>
        /// <returns></returns>
        private static bool InsertMasterDetailInfoBySP(MasterDetailInfoModel masterDetailInfoModel, List<DetailInfoModel> detailInfoModelList)
        {
            bool isInsert = false;

            if (masterDetailInfoModel != null && detailInfoModelList.Any())
            {

                SqlConnection sqlConnection = null;

                try
                {

                    string dbConnectionString = ConfigurationManager.ConnectionStrings["SoftplusTextingConStr"].ConnectionString;
                    sqlConnection = new SqlConnection(dbConnectionString);

                    SqlCommand command = new SqlCommand("sp_InsertMasterDetails", sqlConnection);
                    command.CommandType = CommandType.StoredProcedure;

                    SqlParameter parmCustomerName = new SqlParameter("@customerName", SqlDbType.VarChar, 50);
                    parmCustomerName.Value = masterDetailInfoModel.CustomerName;
                    command.Parameters.Add(parmCustomerName);

                    SqlParameter parmAddress = new SqlParameter("@address", SqlDbType.VarChar, 200);
                    parmAddress.Value = masterDetailInfoModel.Address;
                    command.Parameters.Add(parmAddress);

                    SqlParameter parmLoadDate = new SqlParameter("@loadDate", SqlDbType.SmallDateTime);
                    parmLoadDate.Value = masterDetailInfoModel.LoadDate;
                    command.Parameters.Add(parmLoadDate);

                    // declare a table to store the parameter values
                    DataTable detailItems = new DataTable();
                    detailItems.Columns.Add("CustomerID", typeof(int));
                    detailItems.Columns.Add("ItemSizeID", typeof(int));
                    detailItems.Columns.Add("Quantity", typeof(double));

                    foreach (var item in detailInfoModelList)
                    {
                        detailItems.Rows.Add(new object[] { null, item.ItemSizeID, item.Quantity });
                    }

                    // add the table as a parameter to the stored procedure
                    SqlParameter paramDetailItems = command.Parameters.AddWithValue("@detailItems", detailItems);
                    paramDetailItems.SqlDbType = SqlDbType.Structured;
                    paramDetailItems.TypeName = "dbo.tbl_DetailInfoType";
                    //command.Parameters.Add(paramDetailItems);

                    SqlParameter parmResult = new SqlParameter("@result", SqlDbType.NVarChar, 100);
                    parmResult.Direction = ParameterDirection.Output;
                    command.Parameters.Add(parmResult);

                    SqlParameter parmMessage = new SqlParameter("@message", SqlDbType.NVarChar, 100);
                    parmMessage.Direction = ParameterDirection.Output;
                    command.Parameters.Add(parmMessage);

                    sqlConnection.Open();
                    command.ExecuteNonQuery();

                    string result = command.Parameters["@result"].Value.ToString();

                    if (!String.IsNullOrEmpty(result) && result == "Fail")
                    {
                        return isInsert;

                    }
                    else
                    {
                        return isInsert = true;
                    }

                }
                catch (Exception ex)
                {
                    throw;
                }
                finally
                {
                    if (sqlConnection.State == ConnectionState.Open)
                        sqlConnection.Close();

                }

            }

            return isInsert;
        }