/
month_statistics.go
79 lines (63 loc) · 2.37 KB
/
month_statistics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package statistics
import (
"fmt"
"time"
"../../betypes"
"../../database"
"../../emoji"
"../../keyboards"
"../users"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)
var MonthStatsQueue = make(map[int]bool)
func MonthStatisticsHandler(bot *tgbotapi.BotAPI, update tgbotapi.Update) {
if !users.IsAdmin(update.CallbackQuery.From) {
answer := tgbotapi.NewEditMessageTextAndMarkup(update.CallbackQuery.Message.Chat.ID,
update.CallbackQuery.Message.MessageID,
emoji.NoEntry + "У вас недостаточно прав" + emoji.NoEntry,
keyboards.MainMenu)
bot.Send(answer)
return
}
MonthStatsQueue[update.CallbackQuery.From.ID] = true
answer := tgbotapi.NewMessage(update.CallbackQuery.Message.Chat.ID,
`Введите месяц в формате - "08.2020":`)
bot.DeleteMessage(tgbotapi.NewDeleteMessage(update.CallbackQuery.Message.Chat.ID,
update.CallbackQuery.Message.MessageID))
bot.Send(answer)
}
func GetMonthStatistics(bot *tgbotapi.BotAPI, update tgbotapi.Update) {
fromDate, err := time.Parse("01.2006", update.Message.Text)
if err != nil {
answer := tgbotapi.NewMessage(update.Message.Chat.ID, "*WRONG!* {"+err.Error()+"}")
answer.ReplyMarkup = keyboards.MainMenu
answer.ParseMode = "MarkDown"
bot.Send(answer)
return
}
toDate := fromDate.AddDate(0, 1, -1).Add(23 * time.Hour + 59 * time.Minute)
var products []betypes.Product
database.ProductsCollection.Find(nil).All(&products)
var totalSum float64
var totalMoney float64
var message string = " "
for index, prod := range products {
amount := 0.0
profit := 0.0
i := len(prod.Purchases) - 1
for i > -1 && prod.Purchases[i].SaleDate.After(fromDate) && prod.Purchases[i].SaleDate.Before(toDate) {
amount += prod.Purchases[i].Amount
profit = prod.Purchases[i].Amount * prod.Price - prod.Purchases[i].Amount * prod.PrimeCost
totalSum += prod.Purchases[i].Amount * prod.Price
totalMoney += prod.Purchases[i].Amount * prod.PrimeCost
i--
}
message += fmt.Sprintf("%-3d) %s %-5s(*%v*) profit(*%.2f UAH*)\n", index, prod.Name, "sold", amount, profit)
}
message += fmt.Sprintf("Total cash: *%v UAH*\nTotal profit: *%.2f UAH*", totalSum, totalSum - totalMoney)
delete(MonthStatsQueue, update.Message.From.ID)
answer := tgbotapi.NewMessage(update.Message.Chat.ID, message)
answer.ParseMode = "MarkDown"
answer.ReplyMarkup = keyboards.MainMenu
bot.Send(answer)
}