public JsonResult UpdateFactoryOrder(IEnumerable <VMDiscountAdjustmentBuyerPrev> DiscountPrev, IEnumerable <VMDiscountAdjustmentBuyerAdj> DiscountAdj, IEnumerable <VMDiscountAdjustmentBuyerDet> AdjustmentDetails, VMDiscountAdjustmentBuyerMas DiscountMas, int[] deletedPrevItems, int[] deletedAdjItems)
        {
            var result = new
            {
                flag    = false,
                message = "Error occured. !",
                Id      = 0
            };

            try
            {
                var OpDate = DateTime.Now;
                using (var dbContextTransaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        var OrderMS = db.DiscountAdjustmentBuyerMas.Find(DiscountMas.Id);

                        if (OrderMS == null)
                        {
                            result = new
                            {
                                flag    = false,
                                message = "Saving failed !",
                                Id      = 0
                            };

                            return(Json(result, JsonRequestBehavior.AllowGet));
                        }

                        db.Entry(OrderMS).State = EntityState.Modified;
                        db.SaveChanges();

                        Dictionary <int, int> dictionary =
                            new Dictionary <int, int>();

                        foreach (var item in DiscountPrev)
                        {
                            var OrderPrev = new DiscountAdjustmentBuyerPrev()
                            {
                                Id = item.Id,
                                DiscountAdjustmentBuyerMasId = DiscountMas.Id,
                                BuyerOrderMasId = item.BuyerOrderMasId,
                            };



                            // dictionary.Add(item.DelivOrderDetTempId, OrderPrev.Id);

                            db.Entry(OrderPrev).State = OrderPrev.Id == 0 ?
                                                        EntityState.Added :
                                                        EntityState.Modified;
                            db.SaveChanges();
                        }

                        foreach (var item in DiscountAdj)
                        {
                            var OrderAdj = new DiscountAdjustmentBuyerAdj()
                            {
                                Id = item.Id,
                                DiscountAdjustmentBuyerMasId = DiscountMas.Id,
                                BuyerOrderMasId = item.BuyerOrderMasId,
                            };



                            db.Entry(OrderAdj).State = OrderAdj.Id == 0 ?
                                                       EntityState.Added :
                                                       EntityState.Modified;


                            db.SaveChanges();
                            dictionary.Add(item.TempOrderIdAdj, OrderAdj.Id);
                        }
                        //---- shipment data

                        //    var slno = 1;

                        if (AdjustmentDetails != null)
                        {
                            foreach (var i in AdjustmentDetails)
                            {
                                //var delivDetailAdj = new DiscountAdjustmentBuyerDet()
                                //{
                                //    Id = i.Id,
                                //    DiscountAdjustmentBuyerAdjId = item.Id,
                                //    AdjustmentAmt = i.AdjustmentAmt,
                                //    ExFactoryShipDetId = i.ExFactoryShipDetId,
                                //};


                                ////  db.DiscountAdjustmentFactoryDet.Add(delivDetailAdj);

                                //db.Entry(delivDetailAdj).State = delivDetailAdj.Id == 0 ?
                                //                      EntityState.Added :
                                //                      EntityState.Modified;
                                //db.SaveChanges();

                                if (i.Id == 0)
                                {
                                    var delivDetailAdj = new DiscountAdjustmentBuyerDet()
                                    {
                                        Id = i.Id,
                                        //DiscountAdjustmentFactoryAdjId = item.DiscountAdjustmentFactoryMasId,
                                        DiscountAdjustmentBuyerAdjId = dictionary[i.DelivOrderDetTempIdAdj],
                                        AdjustmentAmt      = i.AdjustmentAmt,
                                        ExFactoryShipDetId = i.ExFactoryShipDetId,
                                    };
                                    db.DiscountAdjustmentBuyerDet.Add(delivDetailAdj);
                                    db.SaveChanges();
                                }
                                else
                                {
                                    var det = db.DiscountAdjustmentBuyerDet.Find(i.Id);
                                    det.AdjustmentAmt = i.AdjustmentAmt;

                                    db.SaveChanges();
                                }
                            }
                        }


                        if (deletedPrevItems != null)
                        {
                            foreach (var item in deletedPrevItems)
                            {
                                var delPrev = db.DiscountAdjustmentBuyerPrev.Find(item);
                                db.DiscountAdjustmentBuyerPrev.Remove(delPrev);
                                db.SaveChanges();
                            }
                        }


                        if (deletedAdjItems != null)
                        {
                            foreach (var item in deletedAdjItems)
                            {
                                //---- if order detail deleted without manual deletion of shipment
                                // ---- find those shipment and delete first

                                var discountDets = db.DiscountAdjustmentBuyerDet.Where(x => x.DiscountAdjustmentBuyerAdjId == item);

                                if (discountDets != null)
                                {
                                    db.DiscountAdjustmentBuyerDet.RemoveRange(discountDets);
                                }

                                var delAdj = db.DiscountAdjustmentBuyerAdj.Find(item);
                                db.DiscountAdjustmentBuyerAdj.Remove(delAdj);
                                db.SaveChanges();
                            }
                        }


                        dbContextTransaction.Commit();

                        result = new
                        {
                            flag    = true,
                            message = "Updated successfully!!",
                            Id      = DiscountMas.Id
                        };

                        Success("Record updated successfully.", true);
                    }
                    catch (Exception ex)
                    {
                        dbContextTransaction.Rollback();

                        result = new
                        {
                            flag    = false,
                            message = ex.Message,
                            Id      = 0
                        };
                    }
                }
            }
            catch (Exception ex)
            {
                result = new
                {
                    flag    = false,
                    message = ex.Message,
                    Id      = 0
                };
            }


            return(Json(result, JsonRequestBehavior.AllowGet));
        }
        public JsonResult SaveExFactory(IEnumerable <VMDiscountAdjustmentBuyerPrev> DiscountPrev, IEnumerable <VMDiscountAdjustmentBuyerAdj> DiscountAdj, IEnumerable <VMDiscountAdjustmentBuyerDet> AdjustmentDetails, VMDiscountAdjustmentBuyerMas DiscountMas)
        {
            var storeDetailDetailId = new List <int>();

            int[] detdetList = storeDetailDetailId.ToArray();

            var storeDetailId = new List <int>();

            int[] detList = storeDetailId.ToArray();

            var result = new
            {
                flag                = false,
                message             = "Error occured. !",
                Id                  = 0,
                storeDetailDetailId = 0,
                storeDetailId       = 0
            };

            try
            {
                var OpDate = DateTime.Now;
                using (var dbContextTransaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        var OrderM = new DiscountAdjustmentBuyerMas()
                        {
                            Id          = 0,
                            BuyerInfoId = DiscountMas.BuyerInfoId,
                            SupplierId  = DiscountMas.SupplierId,
                            DateAdj     = DiscountMas.DateAdj,
                            IsAuth      = DiscountMas.IsAuth,
                            OpBy        = 1,
                            OpOn        = OpDate,
                            AuthBy      = 1,
                            AuthOn      = OpDate
                        };

                        db.DiscountAdjustmentBuyerMas.Add(OrderM);
                        db.SaveChanges();

                        Dictionary <int, int> dictionary = new Dictionary <int, int>();

                        if (DiscountPrev != null)
                        {
                            foreach (var ship in DiscountPrev)
                            {
                                var deliv2 = new DiscountAdjustmentBuyerPrev()
                                {
                                    Id = 0,
                                    DiscountAdjustmentBuyerMasId = OrderM.Id,
                                    BuyerOrderMasId = ship.BuyerOrderMasId
                                };


                                db.DiscountAdjustmentBuyerPrev.Add(deliv2);
                                db.SaveChanges();
                            }
                        }


                        if (DiscountAdj != null)
                        {
                            foreach (var ship in DiscountAdj)
                            {
                                var deliv = new DiscountAdjustmentBuyerAdj()
                                {
                                    Id = 0,
                                    DiscountAdjustmentBuyerMasId = OrderM.Id,
                                    BuyerOrderMasId = ship.BuyerOrderMasId
                                };


                                db.DiscountAdjustmentBuyerAdj.Add(deliv);
                                db.SaveChanges();

                                var ExDetId = deliv.Id;

                                storeDetailId.Add(deliv.Id);
                                //Detailcounter++;
                                dictionary.Add(ship.TempOrderIdAdj, deliv.Id);
                            }

                            if (AdjustmentDetails != null)
                            {
                                foreach (var item in AdjustmentDetails)
                                {
                                    var OrderD = new DiscountAdjustmentBuyerDet()
                                    {
                                        Id            = 0,
                                        AdjustmentAmt = item.AdjustmentAmt,
                                        DiscountAdjustmentBuyerAdjId = dictionary[item.DelivOrderDetTempIdAdj],
                                        ExFactoryShipDetId           = item.ExFactoryShipDetId
                                    };

                                    db.DiscountAdjustmentBuyerDet.Add(OrderD);
                                    db.SaveChanges();

                                    var ExDetId = OrderD.Id;

                                    //storeDetailId.Add(OrderD.Id);
                                    //Detailcounter++;
                                    // dictionary.Add(item.TempOrderId, OrderD.Id);
                                }
                            }
                        }



                        dbContextTransaction.Commit();

                        result = new
                        {
                            flag                = true,
                            message             = "Saving successful!!!",
                            Id                  = OrderM.Id,
                            storeDetailDetailId = 0,
                            storeDetailId       = 0
                        };

                        Success("Record saved successfully.", true);
                    }
                    catch (Exception ex)
                    {
                        dbContextTransaction.Rollback();

                        result = new
                        {
                            flag                = false,
                            message             = ex.Message,
                            Id                  = 0,
                            storeDetailDetailId = 0,
                            storeDetailId       = 0
                        };
                    }
                }
            }
            catch (Exception ex)
            {
                result = new
                {
                    flag                = false,
                    message             = ex.Message,
                    Id                  = 0,
                    storeDetailDetailId = 0,
                    storeDetailId       = 0
                };
            }


            return(Json(result, JsonRequestBehavior.AllowGet));
        }