Exemplo n.º 1
0
        public DTOCreateShipmentViewModel CreateShipment(DTOShipmentsViewModel dtoShipmentsViewModel)
        {
            var userCurrent = Database.GetManager(dtoShipmentsViewModel.Login);
            DTOCreateShipmentViewModel dtoCreateShipmentViewModel = new DTOCreateShipmentViewModel();

            if (userCurrent != null)
            {
                Shipment shipment = new Shipment
                {
                    ShipmentDate = dtoShipmentsViewModel.ShipmentDate,
                    Company      = dtoShipmentsViewModel.Company,
                    City         = dtoShipmentsViewModel.City,
                    Country      = dtoShipmentsViewModel.Country,
                    Quantity     = dtoShipmentsViewModel.Quantity,
                    Sum          = dtoShipmentsViewModel.Sum
                };

                // If first manager`s shipment.
                if (Database.Shipments.GetShipment(dtoShipmentsViewModel.Login) == null)
                {
                    return(CreateShipmentExtension(shipment, userCurrent, dtoCreateShipmentViewModel));
                }

                // If first manager`s shipment on current month and year.
                if (Database.Shipments.GetShipment(dtoShipmentsViewModel.Login, dtoShipmentsViewModel.ShipmentDate.Year, dtoShipmentsViewModel.ShipmentDate.Month) == null)
                {
                    var maxDateShipment = Database.Shipments.MaxDateShipment(dtoShipmentsViewModel.Login);

                    var averageSumLastMonth = Database.Shipments.AverageSum(dtoShipmentsViewModel.Login, maxDateShipment.Year, maxDateShipment.Month);

                    if (dtoShipmentsViewModel.Sum > averageSumLastMonth + 500)
                    {
                        dtoCreateShipmentViewModel.Responce = "Sum must be no more " + (averageSumLastMonth + 500) + " (average Sum of last month where were shipments for the current manager)+500.";
                        return(dtoCreateShipmentViewModel);
                    }
                    return(CreateShipmentExtension(shipment, userCurrent, dtoCreateShipmentViewModel));
                }

                // Sum< average sum on current month for current manager +500
                var averageSumCurrentMonth = Database.Shipments.AverageSum(dtoShipmentsViewModel.Login, dtoShipmentsViewModel.ShipmentDate.Year, dtoShipmentsViewModel.ShipmentDate.Month);

                if (dtoShipmentsViewModel.Sum > averageSumCurrentMonth + 500)
                {
                    dtoCreateShipmentViewModel.Responce = "Sum must be no more " + (averageSumCurrentMonth + 500) + " (average Sum of current month for the current manager)+500";
                    return(dtoCreateShipmentViewModel);
                }
                return(CreateShipmentExtension(shipment, userCurrent, dtoCreateShipmentViewModel));
            }
            dtoCreateShipmentViewModel.Responce = "Close this window and reset program.";
            return(dtoCreateShipmentViewModel);
        }
Exemplo n.º 2
0
        private DTOCreateShipmentViewModel CreateShipmentExtension(Shipment shipment, Manager userCurrent, DTOCreateShipmentViewModel dtoCreateShipmentViewModel)
        {
            var shipmentCurrent = Database.Shipments.CreateShipment(shipment, userCurrent);

            dtoCreateShipmentViewModel.Id          = shipmentCurrent.Id;
            dtoCreateShipmentViewModel.SurnameName = shipmentCurrent.SurnameName;
            return(dtoCreateShipmentViewModel);
        }