Example #1
0
        public async Task HandleAsync(UserMessageInfo userMessageInfo, int amount, string caption)
        {
            BillingPeriod lastBillingPeriod = await _billingPeriodService.GetLastByGroupIdAsync(userMessageInfo.Group.Id);

            if (lastBillingPeriod == null)
            {
                await _messenger.SendMessageAsync(userMessageInfo, "В группе ещё не начат ни один расчётный период", true);

                return;
            }

            var newReceipt = await _receiptService.AddAsync(new Receipt
            {
                BillingPeriodId = lastBillingPeriod.Id,
                TotalAmount     = amount,
                Status          = ReceiptStatus.NewManual,
                Comment         = caption
            });

            IMenu menu = _menuProvider.GetMenu(userMessageInfo, new AddReceiptQueryData
            {
                MenuType            = MenuType.NewReceiptSelectCustomer,
                ReceiptId           = newReceipt.Id,
                SelectedCustomerId  = null,
                SelectedConsumerIds = new long[0],
                TargetId            = null,
                Version             = AddReceiptQueryData.ServerVersion,
            });
            await _messenger.SendMessageAsync(userMessageInfo, Resources.SelectCustomer, true, menu);
        }
Example #2
0
        public async Task HandleAsync(UserMessageInfo userMessageInfo)
        {
            ReceiptMainInfo data = userMessageInfo.Message.ReceiptInfo;

            if (data == null)
            {
                await Messenger.SendMessageAsync(userMessageInfo, Resources.ParsePhotoError, true);

                Logger.Trace(Resources.ParsePhotoError);
                return;
            }

            BillingPeriod lastBillingPeriod = await _billingPeriodService.GetLastByGroupIdAsync(userMessageInfo.Group.Id);

            if (lastBillingPeriod == null)
            {
                await Messenger.SendMessageAsync(userMessageInfo, Resources.BillingPeriodNotCreated, true);

                return;
            }

            var receipt = new Receipt
            {
                BillingPeriodId = lastBillingPeriod.Id,
                TotalAmount     = data.TotalAmount,
                FiscalSign      = data.FiscalSign,
                FiscalDocument  = data.FiscalDocument,
                FiscalNumber    = data.FiscalNumber,
                PurchaseTime    = data.PurchaseTime,
                Status          = ReceiptStatus.New,
                Comment         = "Чек",
            };

            if (await ReceiptService.IsReceiptExists(receipt))
            {
                await Messenger.SendMessageAsync(userMessageInfo, Resources.ReceiptAlredyAdded, true);

                return;
            }

            var newReceipt = await ReceiptService.AddAsync(receipt);

            IMenu menu = MenuProvider.GetMenu(userMessageInfo, new AddReceiptQueryData
            {
                MenuType            = MenuType.NewReceiptSelectCustomer,
                ReceiptId           = newReceipt.Id,
                SelectedCustomerId  = null,
                SelectedConsumerIds = new long[0],
                TargetId            = null,
                Version             = AddReceiptQueryData.ServerVersion,
            });
            await Messenger.SendMessageAsync(userMessageInfo, Resources.SelectCustomer, true, menu);
        }
Example #3
0
        public async Task HandleAsync(UserMessageInfo userMessageInfo)
        {
            var currentBilling = await _billingPeriodService.GetLastByGroupIdAsync(userMessageInfo.Group.Id);

            var debts = await _mainLogicService.CalculatePeriodCurrentDebts(currentBilling.Id);

            string[] debtsMessages = debts.Select(x =>
            {
                string from = userMessageInfo.Customers.First(c => c.Id == x.FromId).Caption;
                string to   = userMessageInfo.Customers.First(c => c.Id == x.ToId).Caption;
                return($"* {from} должен {to}: {(int) x.Amount}р.");
            }).ToArray();

            await _messenger.SendMessageAsync(userMessageInfo, $"Промежуточный итог долгов на период с {currentBilling.PeriodBegin}:\n{string.Join("\n", debtsMessages)}", true);
        }
Example #4
0
        public async Task <ClosingPeriodResult> CloseCurrentAndOpenNewPeriod(long groupId)
        {
            DateTime now = DateTime.Now;

            // Закрываем старый период.
            BillingPeriod lastBillingPeriod = await _billingPeriodService.GetLastByGroupIdAsync(groupId);

            if (lastBillingPeriod != null)
            {
                lastBillingPeriod.PeriodEnd = now;
                await _billingPeriodService.UpdateAsync(lastBillingPeriod);
            }

            // Создаём новый период.
            BillingPeriod newBillingPeriod = await _billingPeriodService.AddAsync(new BillingPeriod
            {
                GroupId     = groupId,
                PeriodBegin = now,
                PeriodEnd   = null
            });

            // Рассчитываем затраты за предыдущий период.
            MoneyOperation[] debts = null;
            if (lastBillingPeriod != null)
            {
                // Записываем задолжности за предыдущий расчётный период.
                MoneyOperationShortInfo[] debtsShortInfo = await CalculatePeriodCurrentDebts(lastBillingPeriod.Id);

                debts = debtsShortInfo.Select(x => new MoneyOperation
                {
                    Amount          = (int)x.Amount,
                    BillingPeriodId = newBillingPeriod.Id,
                    Comment         = "Долг с предыдущего периода",
                    CustomerFromId  = x.FromId,
                    CustomerToId    = x.ToId,
                    OperationType   = MoneyOperationType.Debt
                }).ToArray();
                await _moneyOperationService.AddAsync(debts);
            }

            return(new ClosingPeriodResult
            {
                Debts = debts ?? new MoneyOperation[0],
                PreviousPeriod = lastBillingPeriod,
                NewPeriod = newBillingPeriod
            });
        }
Example #5
0
        private async Task HandleCommandMessageAsync(UserMessageInfo userMessageInfo)
        {
            // Todo Проверка №3. Надо перенести в одно место.
            switch (userMessageInfo.Message.QueryData.MenuType)
            {
            case MenuType.NewReceiptSelectCustomer:
            {
                var data = (AddReceiptQueryData)userMessageInfo.Message.QueryData;
                if (data.TargetId == data.SelectedCustomerId)
                {
                    break;
                }

                data.SelectedCustomerId = data.TargetId;

                IMenu menu = _menuProvider.GetMenu(userMessageInfo, data);
                await _messenger.EditMessageAsync(userMessageInfo, Resources.SelectCustomer, menu);

                break;
            }

            case MenuType.NewReceiptSelectConsumers:
            {
                var data = (AddReceiptQueryData)userMessageInfo.Message.QueryData;
                var selectedConsumers = new HashSet <long>(data.SelectedConsumerIds ?? new long[0]);
                if (data.TargetId != null)
                {
                    if (selectedConsumers.Contains(data.TargetId.Value))
                    {
                        selectedConsumers.Remove(data.TargetId.Value);
                    }
                    else
                    {
                        selectedConsumers.Add(data.TargetId.Value);
                    }
                }

                selectedConsumers        = new HashSet <long>(selectedConsumers.Distinct());
                data.SelectedConsumerIds = selectedConsumers.ToArray();

                IMenu menu = _menuProvider.GetMenu(userMessageInfo, data);
                await _messenger.EditMessageAsync(userMessageInfo, Resources.SelectConsumers, menu);

                break;
            }

            case MenuType.NewReceiptAdd:
            {
                var    data         = (AddReceiptQueryData)userMessageInfo.Message.QueryData;
                string customerText = userMessageInfo.Customers
                                      .FirstOrDefault(x => data.SelectedCustomerId.Value == x.Id).Caption;
                string[] consumerTexts = userMessageInfo.Customers
                                         .Where(x => data.SelectedCustomerId.Value == x.Id || data.SelectedConsumerIds.Contains(x.Id))
                                         .Select(x => x.Caption).ToArray();
                Receipt receipt = await _receiptService.GetAsync(data.ReceiptId);

                await _messenger.EditMessageAsync(userMessageInfo, Resources.InProgress);

                if (receipt.Status == ReceiptStatus.New || receipt.Status == ReceiptStatus.NewManual)
                {
                    receipt.Status = receipt.Status == ReceiptStatus.New
                            ? ReceiptStatus.Filled
                            : ReceiptStatus.Manual;
                    receipt.CustomerId   = data.SelectedCustomerId;
                    receipt.PurchaseTime = DateTime.Now;
                    await _receiptService.SetCustomersToReceiptAsync(receipt.Id, data.SelectedConsumerIds);

                    await _receiptService.UpdateAsync(receipt);

                    var msgText = string.Format(Resources.NewReceiptAddedInfo, customerText, receipt.TotalAmount, string.Join(", ", consumerTexts));
                    await _messenger.EditMessageAsync(userMessageInfo, msgText);

                    _logger.Info(msgText);
                }

                break;
            }

            case MenuType.NewReceiptCancel:
            {
                var data = (AddReceiptQueryData)userMessageInfo.Message.QueryData;
                await _messenger.EditMessageAsync(userMessageInfo, Resources.NewReceiptCancel);

                Receipt receipt = await _receiptService.GetAsync(data.ReceiptId);

                receipt.Status = ReceiptStatus.Deleted;
                await _receiptService.UpdateAsync(receipt);

                break;
            }

            case MenuType.MoneyTransferSelectFrom:
            {
                var   data = (MoneyTransferQueryData)userMessageInfo.Message.QueryData;
                IMenu menu = _menuProvider.GetMenu(userMessageInfo, data);
                await _messenger.EditMessageAsync(userMessageInfo, Resources.MoneyTransferSelectFrom, menu);

                break;
            }

            case MenuType.MoneyTransferSelectTo:
            {
                var   data = (MoneyTransferQueryData)userMessageInfo.Message.QueryData;
                IMenu menu = _menuProvider.GetMenu(userMessageInfo, data);
                await _messenger.EditMessageAsync(userMessageInfo, Resources.MoneyTransferSelectTo, menu);

                break;
            }

            case MenuType.MoneyTransferAdd:
            {
                var    data         = (MoneyTransferQueryData)userMessageInfo.Message.QueryData;
                string customerFrom = userMessageInfo.Customers.FirstOrDefault(x => data.CustomerFromId == x.Id)?.Caption ?? $"Id: {data.CustomerFromId}";
                string customerTo   = userMessageInfo.Customers.FirstOrDefault(x => data.CustomerToId == x.Id)?.Caption ?? $"Id: {data.CustomerToId}";
                await _messenger.EditMessageAsync(userMessageInfo, Resources.InProgress);

                var lastBillingPeriod = await _billingPeriodService.GetLastByGroupIdAsync(userMessageInfo.Group.Id);

                if (lastBillingPeriod == null)
                {
                    await _messenger.EditMessageAsync(userMessageInfo, Resources.BillingPeriodNotCreated);

                    break;
                }

                await _moneyOperationService.AddAsync(new MoneyOperation
                    {
                        Amount          = data.Amount,
                        Comment         = data.Caption,
                        CustomerFromId  = data.CustomerFromId.Value,
                        CustomerToId    = data.CustomerToId.Value,
                        OperationType   = MoneyOperationType.Transfer,
                        BillingPeriodId = lastBillingPeriod.Id
                    });

                var msgText = string.Format(Resources.MoneyTransferSuccess, customerFrom, customerTo, data.Amount);
                await _messenger.EditMessageAsync(userMessageInfo, msgText);

                _logger.Info(msgText.Replace("\n", ""));
                break;
            }

            case MenuType.MoneyTransferCancel:
            {
                await _messenger.EditMessageAsync(userMessageInfo, Resources.MoneyTransferCanceled);

                break;
            }
            }
        }