Ejemplo n.º 1
0
        public SalesViewModel GetSalesViewModel(int salesId)
        {
            SalesViewModel model = (from r in this._repository.Sales
                                    where r.SalesID == salesId && r.IsDeleted == false
                                    select new SalesViewModel
            {
                SalesId = salesId,
                PassengerName = r.PassengerName,
                UserId = r.UserID,
                Deposit = r.Deposit,
                NumberPassengers = r.Passengers,
                BookingDate = r.BookingDate,
                Destination_Init = r.Destination,
                Shop_Init = r.Shop,
                SaleLocation_Init = r.SaleLocation,
                Comments = r.Comments
            }).SingleOrDefault();

            var destinations = (from r in this._repository.Destinations
                                select new SelectListItem
            {
                Text = r.Name,
                Value = SqlFunctions.StringConvert((double)r.DestinationID),
                Selected = false
            });

            model.Destinations = destinations.OrderBy(c => c.Text).ToList();

            SetSelectedItem(model.Destinations, model.Destination_Init);

            model.SaleLocations = (from r in this._repository.SaleLocations
                                   select new SelectListItem
            {
                Text = r.Name,
                Value = SqlFunctions.StringConvert((double)r.SaleLocationID)
            }).OrderBy(c => c.Text).ToList();

            SetSelectedItem(model.SaleLocations, model.SaleLocation_Init);

            model.Shops = (from r in this._repository.Shops
                           select new SelectListItem
            {
                Text = r.Name,
                Value = SqlFunctions.StringConvert((double)r.ShopID)
            }).OrderBy(c => c.Text).ToList();

            SetSelectedItem(model.Shops, model.Shop_Init);

            List <SalesSupplierModel> salesSuppliers = (from r in this._repository.SalesSuppliers
                                                        where r.SalesID == salesId && r.IsDeleted == false
                                                        select new SalesSupplierModel
            {
                SourceTypeName = r.SourceType,
                SupplierName = r.Supplier,
                SaleValue = r.SaleValue
            }).ToList();

            model.SourceTypes0 = GetSourceTypeItems();
            model.SourceTypes1 = GetSourceTypeItems();
            model.SourceTypes2 = GetSourceTypeItems();
            model.SourceTypes3 = GetSourceTypeItems();
            model.SourceTypes4 = GetSourceTypeItems();
            model.SourceTypes5 = GetSourceTypeItems();
            model.SourceTypes6 = GetSourceTypeItems();
            model.SourceTypes7 = GetSourceTypeItems();
            model.SourceTypes8 = GetSourceTypeItems();
            model.SourceTypes9 = GetSourceTypeItems();

            SetSelectedItem(model.SourceTypes0, "Air");
            SetSelectedItem(model.SourceTypes1, "Cruise");
            SetSelectedItem(model.SourceTypes2, "Insurance");
            SetSelectedItem(model.SourceTypes3, "Land");

            string selectSource = "Select a Source";

            model.SourceType4_Init = selectSource;
            model.SourceType5_Init = selectSource;
            model.SourceType6_Init = selectSource;
            model.SourceType7_Init = selectSource;
            model.SourceType8_Init = selectSource;
            model.SourceType9_Init = selectSource;

            model.Suppliers0 = GetSupplierItems("Air");
            model.Suppliers1 = GetSupplierItems("Cruise");
            model.Suppliers2 = GetSupplierItems("Insurance");
            model.Suppliers3 = GetSupplierItems("Land");
            model.Suppliers4 = GetSupplierItems("Air");
            model.Suppliers5 = GetSupplierItems("Air");
            model.Suppliers6 = GetSupplierItems("Air");
            model.Suppliers7 = GetSupplierItems("Air");
            model.Suppliers8 = GetSupplierItems("Air");
            model.Suppliers9 = GetSupplierItems("Air");

            string selectSupplier = "Select a Supplier";

            model.Supplier4_Init = selectSupplier;
            model.Supplier5_Init = selectSupplier;
            model.Supplier6_Init = selectSupplier;
            model.Supplier7_Init = selectSupplier;
            model.Supplier8_Init = selectSupplier;
            model.Supplier9_Init = selectSupplier;

            for (int i = 0; i < salesSuppliers.Count; i++)
            {
                //string propertyName = "SourceType" + i.ToString() + "_Init";
                //var propInfo = model.GetType().GetProperty(propertyName);
                //if (propInfo != null)
                //{
                //    propInfo.SetValue(model, salesSuppliers[i].SourceTypeName, null);
                //}

                string propertyName = "SourceTypes" + i.ToString();
                var    propInfo     = model.GetType().GetProperty(propertyName);
                if (propInfo != null)
                {
                    List <SelectListItem> sourceList = (List <SelectListItem>)propInfo.GetValue(model, null);
                    ClearAndSetSelectedItem(sourceList, salesSuppliers[i].SourceTypeName);
                }

                propertyName = "Suppliers" + i.ToString();
                propInfo     = model.GetType().GetProperty(propertyName);
                if (propInfo != null)
                {
                    List <SelectListItem> supplierList = null;
                    if (salesSuppliers[i].SourceTypeName != "Air")
                    {
                        supplierList = GetSupplierItems(salesSuppliers[i].SourceTypeName);
                        SetSelectedItem(supplierList, salesSuppliers[i].SupplierName);
                        propInfo.SetValue(model, supplierList, null);
                    }
                    else
                    {
                        supplierList = (List <SelectListItem>)propInfo.GetValue(model, null);
                        ClearAndSetSelectedItem(supplierList, salesSuppliers[i].SupplierName);
                    }
                }

                propertyName = "SaleValue" + i.ToString();
                propInfo     = model.GetType().GetProperty(propertyName);
                if (propInfo != null)
                {
                    propInfo.SetValue(model, (decimal)salesSuppliers[i].SaleValue, null);
                }
            }

            return(model);
        }
Ejemplo n.º 2
0
        public ActionResult EditPageSubmit(FormCollection collection)
        {
            ViewBag.Message = "<font color='red'>There is some error in your order!</font>";

            var updatedModel = new SalesViewModel();

            if (ModelState.IsValid)
            {
                if (TryUpdateModel(updatedModel, null, null, new[] { "Destinations", "SaleLocations", "Shops", "SourceTypes", "Suppliers" }))
                {
                    updatedModel.CreatedDateTime = DateTime.Now;
                    updatedModel.UpdatedDateTime = DateTime.Now;

                    string destination  = this._salesBL.GetDestinationByName(Convert.ToInt32(updatedModel.Destination));
                    string shop         = this._salesBL.GetShopByName(Convert.ToInt32(updatedModel.Shop));
                    string saleLocation = this._salesBL.GetSaleLocationByName(Convert.ToInt32(updatedModel.SaleLocation));

                    List <SalesSupplierModel> salesList = new List <SalesSupplierModel>();

                    for (int i = 0; i < Constant.NumberSalesSupplier; i++)
                    {
                        string propertyOfSaleValue = "SaleValue" + i.ToString();
                        var    propInfoOfSaleValue = updatedModel.GetType().GetProperty(propertyOfSaleValue);
                        if (propInfoOfSaleValue != null)
                        {
                            decimal?saleValue = (decimal?)propInfoOfSaleValue.GetValue(updatedModel, null);
                            if (saleValue == null)
                            {
                                continue;
                            }

                            string propertyOfSourceType = "SourceType" + i.ToString();
                            string propValOfSourceType  = (string)updatedModel.GetType().GetProperty(propertyOfSourceType).GetValue(updatedModel, null);

                            string propertyOfSupplier = "Supplier" + i.ToString();
                            string propValOfSupplier  = (string)updatedModel.GetType().GetProperty(propertyOfSupplier).GetValue(updatedModel, null);

                            this._salesBL.AddToSalesList(salesList, Convert.ToInt32(propValOfSourceType), Convert.ToInt32(propValOfSupplier), saleValue);
                        }
                    }

                    if (salesList.Count == 0)
                    {
                        ViewBag.Message = "<font color='red'>At least one of the Source and Suppliers line must be entered! (You must enter 'Sale Value'.)</font>";
                        return(View());
                    }

                    int status = 0;
                    if (updatedModel.SalesId == 0)
                    {
                        status = this._salesBL.AddSalesAndSalesSupplier(
                            updatedModel.UserId,
                            shop,
                            updatedModel.CreatedDateTime,
                            updatedModel.BookingDate,
                            updatedModel.PassengerName,
                            destination,
                            updatedModel.Deposit,
                            saleLocation,
                            updatedModel.NumberPassengers,
                            updatedModel.Comments,
                            salesList);

                        if (status > 0)
                        {
                            ViewBag.Message = "<font color='blue'>Your order has been entered successfully!</font>";
                        }
                    }
                    else
                    {
                        status = this._salesBL.UpdateSalesAndSalesSupplier(
                            updatedModel.SalesId,
                            updatedModel.UserId,
                            shop,
                            updatedModel.UpdatedDateTime,
                            updatedModel.BookingDate,
                            updatedModel.PassengerName,
                            destination,
                            updatedModel.Deposit,
                            saleLocation,
                            updatedModel.NumberPassengers,
                            updatedModel.Comments,
                            salesList);

                        if (status > 0)
                        {
                            ViewBag.Message = "<font color='blue'>Your order has been updated successfully!</font>";
                        }
                    }
                }
                else
                {
                    ViewBag.Message = "<font color='red'>" + string.Join("<br/>", ModelState.Values
                                                                         .SelectMany(v => v.Errors)
                                                                         .Select(e => e.ErrorMessage)) + "</font>";

                    //return View("~/Views/Home/EditPage.aspx");
                }
            }

            return(View());
        }