Beispiel #1
0
        public virtual async Task <DeliveryChargeDto> CalculateAsync(int addressId, int deliveryScheduleId, CartItemDto[] items)
        {
            var meanDistance = await DeliveryChargeRepository.GetMeanDistanceAsync(addressId);

            var distanceChargeE = await AsyncExecuter
                                  .LastOrDefaultAsync(from dcR in DistanceChargeRepository
                                                      orderby dcR.From ascending
                                                      where dcR.From <= meanDistance.MeanDistance
                                                      select dcR);

            _ = distanceChargeE ?? throw new BusinessException();
            var subtotal = 0.0;

            foreach (var ci in items)
            {
                var itemE = await ItemRepository.SingleOrDefaultAsync(e => e.Id == ci.ItemID)
                            ?? throw new BusinessException();

                subtotal += itemE.ItemPrice * ci.Qty;
            }

            var subtotalPerE = await AsyncExecuter
                               .LastOrDefaultAsync(from subPR in SubtotalPercentageRepository
                                                   orderby subPR.From ascending
                                                   where subPR.From <= subtotal
                                                   select subPR);


            var itemIdsArr = items.Select(e => e.ItemID).ToArray();
            var dsArr      = await DeliveryScheduleRepository.GetCompatibleSchedules((new Guid()), addressId, itemIdsArr);

            var dsE = dsArr.SingleOrDefault(e => e.Id == deliveryScheduleId) ?? throw new BusinessException();

            float dsCost = (float)(subtotal * (dsE.CostIncreasePercentage / 100));
            var   subPer = (subtotalPerE == null)? 0 : (subtotal * ((float)subtotalPerE.Percentage / 100));

            return(new DeliveryChargeDto
            {
                DistanceChargeID = distanceChargeE.Id,
                DeliveryScheduleID = dsE.Id,
                SubtotalPercentageID = (subtotalPerE == null)? 0 : subtotalPerE.Id,
                DistanceCharge = distanceChargeE.Charge,
                IncreasedCost = dsCost,
                SubtotalPercentage = (float)subPer,
                Subtotal = (float)subtotal,
                Nettotal = (float)(subtotal + distanceChargeE.Charge + dsCost + subPer)
            });
        }
        public virtual async Task <DeliveryChargeEntity> CalculateAsync(int addressId, int deliveryScheduleId, float subtotal, bool hasPromotion = false)
        {
            var addrE = await AddressRepository.FindAsync(addressId) ?? throw new EntityNotFoundException();

            var dScheduleE = await DeliveryScheduleRepository.FindAsync(deliveryScheduleId) ?? throw new EntityNotFoundException();

            var riderEs = await RiderRepository.GetRidersForTheCity(addrE.CityID);

            var meanDistance    = GetMeanDistance(addrE, riderEs);
            var distanceChargeE = await AsyncExecutor.LastOrDefaultAsync(DistanceChargeRepository
                                                                         .Where(e => e.From < meanDistance)
                                                                         .OrderBy(e => e.From));

            var subtotalPercentageE = await AsyncExecutor.LastOrDefaultAsync(SubtotalPercentageRepository.Where(e => e.From < subtotal)
                                                                             .OrderBy(e => e.From));

            var charge = subtotal * subtotalPercentageE.Percentage;

            charge += (hasPromotion)? 0 : distanceChargeE.Charge;

            charge += charge * dScheduleE.CostIncreasePercentage;

            return(new DeliveryChargeEntity(charge, distanceChargeE.Id, subtotalPercentageE.Id, dScheduleE.Id));
        }
        public void ConfigureDlg(IUnitOfWork uow, UndeliveredOrder undelivery)
        {
            this.Sensitive = false;
            yEForUndeliveredOrder.Changed += OnUndeliveredOrderChanged;

            CanChangeProblemSource = commonServices.PermissionService.ValidateUserPresetPermission("can_change_undelivery_problem_source", commonServices.UserService.CurrentUserId);
            this.undelivery        = undelivery;
            UoW      = uow;
            oldOrder = undelivery.OldOrder;
            newOrder = undelivery.NewOrder;
            if (undelivery.Id > 0 && undelivery.InProcessAtDepartment != null)
            {
                InitialProcDepartmentName = undelivery.InProcessAtDepartment.Name;
            }
            if (undelivery.Id > 0)
            {
                foreach (GuiltyInUndelivery g in undelivery.ObservableGuilty)
                {
                    initialGuiltyList.Add(
                        new GuiltyInUndelivery {
                        Id = g.Id,
                        UndeliveredOrder = g.UndeliveredOrder,
                        GuiltySide       = g.GuiltySide,
                        GuiltyDepartment = g.GuiltyDepartment
                    }
                        );
                }
            }
            var filterOrders = new OrdersFilter(UoW);
            List <OrderStatus> hiddenStatusesList = new List <OrderStatus>();
            var grantedStatusesArray = OrderRepository.GetStatusesForOrderCancelation();

            foreach (OrderStatus status in Enum.GetValues(typeof(OrderStatus)))
            {
                if (!grantedStatusesArray.Contains(status))
                {
                    hiddenStatusesList.Add(status);
                }
            }
            filterOrders.SetAndRefilterAtOnce(x => x.HideStatuses = hiddenStatusesList.Cast <Enum>().ToArray());
            yEForUndeliveredOrder.Changed += (sender, e) => {
                oldOrder       = undelivery.OldOrder;
                lblInfo.Markup = undelivery.GetOldOrderInfo();
                if (undelivery.Id <= 0)
                {
                    undelivery.OldOrderStatus = oldOrder.OrderStatus;
                }
                routeListDoesNotExist = oldOrder != null && (undelivery.OldOrderStatus == OrderStatus.NewOrder ||
                                                             undelivery.OldOrderStatus == OrderStatus.Accepted ||
                                                             undelivery.OldOrderStatus == OrderStatus.WaitForPayment);

                guiltyInUndeliveryView.ConfigureWidget(UoW, undelivery, !routeListDoesNotExist);
                SetSensitivities();
                SetVisibilities();
                GetFines();
                RemoveItemsFromEnums();
            };
            yEForUndeliveredOrder.RepresentationModel = new OrdersVM(filterOrders);
            yEForUndeliveredOrder.Binding.AddBinding(undelivery, x => x.OldOrder, x => x.Subject).InitializeFromSource();
            yEForUndeliveredOrder.CanEditReference = ServicesConfig.CommonServices.CurrentPermissionService.ValidatePresetPermission("can_delete");

            yDateDriverCallTime.Binding.AddBinding(undelivery, t => t.DriverCallTime, w => w.DateOrNull).InitializeFromSource();
            if (undelivery.Id <= 0)
            {
                yDateDriverCallTime.DateOrNull = DateTime.Now;
            }

            yEnumCMBDriverCallPlace.ItemsEnum = typeof(DriverCallType);
            yEnumCMBDriverCallPlace.Binding.AddBinding(undelivery, p => p.DriverCallType, w => w.SelectedItem).InitializeFromSource();

            yDateDispatcherCallTime.Binding.AddBinding(undelivery, t => t.DispatcherCallTime, w => w.DateOrNull).InitializeFromSource();
            if (undelivery.Id <= 0)
            {
                yDateDispatcherCallTime.DateOrNull = DateTime.Now;
            }

            referenceNewDeliverySchedule.ItemsQuery = DeliveryScheduleRepository.AllQuery();
            referenceNewDeliverySchedule.SetObjectDisplayFunc <DeliverySchedule>(e => e.Name);
            referenceNewDeliverySchedule.Binding.AddBinding(undelivery, s => s.NewDeliverySchedule, w => w.Subject).InitializeFromSource();
            referenceNewDeliverySchedule.Sensitive = false;

            SetLabelsAcordingToNewOrder();

            yEnumCMBStatus.ItemsEnum         = typeof(UndeliveryStatus);
            yEnumCMBStatus.SelectedItem      = undelivery.UndeliveryStatus;
            yEnumCMBStatus.EnumItemSelected += (s, e) => {
                SetSensitivities();
                undelivery.SetUndeliveryStatus((UndeliveryStatus)e.SelectedItem);
            };

            yentInProcessAtDepartment.SubjectType = typeof(Subdivision);
            yentInProcessAtDepartment.Binding.AddBinding(undelivery, d => d.InProcessAtDepartment, w => w.Subject).InitializeFromSource();
            yentInProcessAtDepartment.ChangedByUser += (s, e) => {
                undelivery.AddCommentToTheField(
                    UoW,
                    CommentedFields.Reason,
                    String.Format(
                        "сменил(а) \"в работе у отдела\" \nс \"{0}\" на \"{1}\"",
                        InitialProcDepartmentName,
                        undelivery.InProcessAtDepartment.Name
                        )
                    );
            };

            if (undelivery.Id <= 0)
            {
                yentInProcessAtDepartment.Subject = SubdivisionsRepository.GetQCDepartment(UoW);
            }

            refRegisteredBy.RepresentationModel = new EmployeesVM(UoW);
            refRegisteredBy.Binding.AddBinding(undelivery, s => s.EmployeeRegistrator, w => w.Subject).InitializeFromSource();

            yEnumCMBDriverCallPlace.EnumItemSelected += CMBSelectedItemChanged;

            txtReason.Binding.AddBinding(undelivery, u => u.Reason, w => w.Buffer.Text).InitializeFromSource();

            lblInfo.Markup = undelivery.GetOldOrderInfo();

            yenumcomboboxTransferType.ItemsEnum = typeof(TransferType);
            yenumcomboboxTransferType.Binding.AddBinding(undelivery, u => u.OrderTransferType, w => w.SelectedItemOrNull).InitializeFromSource();

            comboProblemSource.SetRenderTextFunc <UndeliveryProblemSource>(k => k.GetFullName);
            comboProblemSource.Binding.AddBinding(undelivery, u => u.ProblemSourceItems, w => w.ItemsList).InitializeFromSource();
            comboProblemSource.Binding.AddBinding(undelivery, u => u.ProblemSource, w => w.SelectedItem).InitializeFromSource();
            comboProblemSource.Sensitive = CanChangeProblemSource;

            yTreeFines.ColumnsConfig = ColumnsConfigFactory.Create <FineItem>()
                                       .AddColumn("Номер").AddTextRenderer(node => node.Fine.Id.ToString())
                                       .AddColumn("Сотудники").AddTextRenderer(node => node.Employee.ShortName)
                                       .AddColumn("Сумма штрафа").AddTextRenderer(node => CurrencyWorks.GetShortCurrencyString(node.Money))
                                       .Finish();

            yenumcomboboxTransferType.Visible = undelivery?.NewOrder != null;

            undelivery.PropertyChanged += (sender, e) => {
                if (e.PropertyName != "NewOrder")
                {
                    return;
                }

                if (undelivery.NewOrder == null)
                {
                    yenumcomboboxTransferType.Visible = false;
                    undelivery.OrderTransferType      = null;
                    return;
                }

                yenumcomboboxTransferType.Visible = true;
            };

            GetFines();
            SetVisibilities();
            SetSensitivities();
        }