public ResponseModel CreateOutletSaleUIDetails(OutletSaleUIDetailsViewModel OutletSaleUIData, List <OutletSaleUIVM> productList)
        {
            using (var transaction = _db.Database.BeginTransaction())
            {
                try
                {
                    DateTime aDate = DateTime.Now;


                    InvOutletInvoiceMaster aOutletSaleInvoiceMaster = new InvOutletInvoiceMaster()
                    {
                        OutletInvoiceMasterId = OutletSaleUIData.OutletInvoiceMasterId,
                        OutletSaleInvoiceNo   = Convert.ToInt32(_invoiceNo.GetNewInvoiceNo()),
                        //OutletId = OutletSaleUIData.OutletId,
                        OutletId     = 1,
                        CustomerId   = 1, //// Now default value is 1 but after add Identity it will be >> CustomerId = OutletSaleUIData.CustomerId,
                        SalePersonId = 1, /////Now default value is 1 but after add Identity it will be >>SalePersonId = OutletSaleUIData.SalePersonId,


                        TotalItem       = OutletSaleUIData.TotalItem,
                        TotalGrandPrice = OutletSaleUIData.TotalGrandPrice,
                        VAT             = OutletSaleUIData.VAT,
                        PaymentMode     = OutletSaleUIData.PaymentMode,
                        Cash            = OutletSaleUIData.Cash,
                        Credit          = OutletSaleUIData.Credit,
                        Discount        = OutletSaleUIData.Discount,
                        Rounding        = OutletSaleUIData.Rounding,
                        PayableAmount   = OutletSaleUIData.PayableAmount,
                        PaidAmount      = OutletSaleUIData.PaidAmount,
                        DueOrRefund     = OutletSaleUIData.DueOrRefund,

                        //IsFullPaid = OutletSaleUIData.IsFullPaid,
                        IsActive  = true,
                        IsChanged = false,
                        IsDeleted = false,
                        IsEdited  = false,


                        IsReturned  = -1,////////
                        Status      = 2,
                        Note        = "Sold",
                        CreatedDate = DateTime.Now,
                    };
                    aOutletSaleInvoiceMaster.IsFullPaid = OutletSaleUIData.DueOrRefund <= 0;   //////////OR, if (OutletSaleUIData.DueOrRefund <= 0) { aOutletSaleInvoiceMaster.IsFullPaid = true; } else { aOutletSaleInvoiceMaster.IsFullPaid = false; }
                    //aOutletSaleInvoiceMaster.OutletSaleInvoiceNo = aDate.Year +''+ aDate.Month +''+ aDate.Day +''+ aOutletSaleInvoiceMaster.OutletId;
                    // var invoiceId = aDate.Year().toString().substr(2) + "" + (aDate.Month() + 1) + "" + (aDate.Day() + "" + 1);

                    if (OutletSaleUIData.OutletInvoiceMasterId == 0)
                    {
                        if ((OutletSaleUIData.Cash > 0) && (OutletSaleUIData.Credit <= 0) && (OutletSaleUIData.PaidAmount > 0))
                        {
                            aOutletSaleInvoiceMaster.PaymentMode = "Cash";
                        }
                        else if ((OutletSaleUIData.Credit > 0) && (OutletSaleUIData.Cash <= 0) && (OutletSaleUIData.PaidAmount > 0))
                        {
                            aOutletSaleInvoiceMaster.PaymentMode = "Credit";
                        }
                        else if ((OutletSaleUIData.Cash > 0) && (OutletSaleUIData.Credit > 0) && (OutletSaleUIData.PaidAmount > 0))
                        {
                            aOutletSaleInvoiceMaster.PaymentMode = "Cash & Credit";
                        }
                        else
                        {
                            aOutletSaleInvoiceMaster.PaymentMode = "None";
                        }
                        _db.InvOutletInvoiceMasters.Add(aOutletSaleInvoiceMaster);
                        _db.SaveChanges();


                        foreach (var aData in productList)
                        {
                            //break;
                            InvOutletInvoiceDetail aOutletSaleInvoiceDetails = new InvOutletInvoiceDetail()
                            {
                                OutletInvoiceMasterId = aOutletSaleInvoiceMaster.OutletInvoiceMasterId,
                                ProductId             = aData.ProductId,
                                ProductQuantity       = aData.ProductQuantity,
                                Discount = aData.Discount,
                                //DiscountPercent = aData.DiscountPercent,
                                UnitPrice   = aData.UnitPrice,
                                TotalPrice  = aData.TotalPrice,
                                IsActive    = true,
                                IsChanged   = false,
                                IsDeleted   = false,
                                IsEdited    = false,
                                IsReturned  = -1,
                                Note        = "Sold",
                                CreatedDate = DateTime.Now
                            };
                            _db.InvOutletInvoiceDetails.Add(aOutletSaleInvoiceDetails);
                            _db.SaveChanges();

                            InvOutletStock aStock = new InvOutletStock()
                            {
                                ProductId = aData.ProductId,
                                //OutletId = aData.OutletId,
                                OutletId            = aOutletSaleInvoiceMaster.OutletId,
                                IsActive            = true,
                                IsChanged           = false,
                                IsDeleted           = false,
                                IsEdited            = false,
                                IsReturned          = -1,
                                InOut               = 2,
                                Note                = "Sold",
                                OutletStockQuantity = aData.ProductQuantity,
                                CreatedDate         = DateTime.Now
                            };
                            _db.InvOutletStocks.Add(aStock);
                            _db.SaveChanges();
                        }
                        _db.SaveChanges();
                        transaction.Commit();
                        var invoiceData = _db.spSaleInvoice(aOutletSaleInvoiceMaster.OutletSaleInvoiceNo).ToList();
                        //return _aModel.Respons(true, "Sale Info Successfully Saved");
                        return(_aModel.Respons(invoiceData));
                    }

                    else if (OutletSaleUIData.OutletInvoiceMasterId > 0)
                    {
                        _db.InvOutletInvoiceMasters.Attach(aOutletSaleInvoiceMaster);
                        _db.Entry(aOutletSaleInvoiceMaster).State = EntityState.Modified;
                        _db.SaveChanges();

                        var invOutletInvoice = _db.InvOutletInvoiceDetails.Find(OutletSaleUIData.OutletInvoiceDetailsId);

                        invOutletInvoice.IsActive     = false;
                        invOutletInvoice.ModifiedDate = DateTime.Now;

                        _db.InvOutletInvoiceDetails.Attach(invOutletInvoice);
                        _db.Entry(invOutletInvoice).State = EntityState.Modified;
                        _db.SaveChanges();

                        foreach (var aData in productList)
                        {
                            InvOutletInvoiceDetail aOutletSaleInvoiceDetails = new InvOutletInvoiceDetail()
                            {
                                OutletInvoiceMasterId = aOutletSaleInvoiceMaster.OutletInvoiceMasterId,
                                ProductId             = aData.ProductId,
                                ProductQuantity       = aData.ProductQuantity,
                                CreatedDate           = DateTime.Now,
                            };
                            _db.InvOutletInvoiceDetails.Add(aOutletSaleInvoiceDetails);
                            _db.Entry(aOutletSaleInvoiceDetails).State = EntityState.Modified;
                            _db.SaveChanges();
                        }
                        transaction.Commit();
                        return(_aModel.Respons(true, "Successfully Updated  Sale Data"));
                    }
                    _db.SaveChanges();
                    transaction.Commit();
                    return(_aModel.Respons(true, "Sorry Failed to Update  Sale Data"));
                }
                catch (Exception ex)
                {
                    //_db.SaveChanges();
                    transaction.Rollback();
                    return(_aModel.Respons(true, "Sorry Some Error Happned"));
                }
            }
        }