COOPENOMICS  v1
Кооперативная Экономика
edubridge.hpp
См. документацию.
1#pragma once
2
3#include <optional>
4#include <string>
5
6#include <eosio/asset.hpp>
7#include <eosio/crypto.hpp>
8#include <eosio/eosio.hpp>
9
10#include "../../consts.hpp"
11#include "../../domain/table_ledger2_userwallets.hpp"
12#include "../../domain/table_ledger2_wallet.hpp"
13#include "../../domain/table_edubridge_subscriptions.hpp"
14#include "../../domain/table_edubridge_courses.hpp"
15#include "../../domain/table_edubridge_terms.hpp"
16#include "../../domain/table_edubridge_rids.hpp"
17#include "../../domain/table_edubridge_contracts.hpp"
18#include "../ledger2/ledger2.hpp"
19#include "../utils.hpp"
20
32namespace Edubridge {
33
34using namespace eosio;
35
36// ── Доступный баланс пайщика по кошельку ledger2 ─────────────────────────
37//
38// Аналог `Marketplace::get_user_wallet_balance` — вынесен отдельно, чтобы не
39// тянуть в edubridge заголовки «Стола заказов».
40
42 eosio::asset available = eosio::asset(0, _root_govern_symbol);
43 eosio::asset blocked = eosio::asset(0, _root_govern_symbol);
44 bool exists = false;
45};
46
47inline UserWalletAvailable get_user_wallet_balance(eosio::name coopname,
48 eosio::name wallet_id,
49 eosio::name username) {
50 userwallets_index user_wallets(_ledger2, coopname.value);
51 auto idx = user_wallets.get_index<"byuserwallet"_n>();
52 auto it = idx.find(combine_ids(wallet_id.value, username.value));
53 if (it == idx.end()) {
54 return UserWalletAvailable{};
55 }
56 return UserWalletAvailable{ it->available, it->blocked, true };
57}
58
59// ── Поиск анкеров процессов ──────────────────────────────────────────────
60
62inline edu_subscriptions_index::const_iterator
63get_subscription_or_fail(edu_subscriptions_index& subs, const checksum256& sub_hash) {
64 auto idx = subs.get_index<"byhash"_n>();
65 auto it = idx.find(sub_hash);
66 eosio::check(it != idx.end(), "EDUBRIDGE_SUBSCRIPTION_NOT_FOUND: Подписка с указанным hash не найдена");
67 return subs.find(it->id);
68}
69
71inline edu_rids_index::const_iterator
72get_rid_or_fail(edu_rids_index& rids, const checksum256& rid_hash) {
73 auto idx = rids.get_index<"byhash"_n>();
74 auto it = idx.find(rid_hash);
75 eosio::check(it != idx.end(), "EDUBRIDGE_RID_NOT_FOUND: Заявление о паевом взносе РИД с указанным hash не найдено");
76 return rids.find(it->id);
77}
78
80inline std::optional<edu_contract> get_contract(eosio::name coopname, eosio::name username) {
81 edu_contracts_index contracts(_edubridge, coopname.value);
82 auto idx = contracts.get_index<"byusername"_n>();
83 auto it = idx.find(username.value);
84 if (it == idx.end()) return std::nullopt;
85 return *it;
86}
87
89inline edu_contract get_active_contract_or_fail(eosio::name coopname, eosio::name username) {
90 auto contract = get_contract(coopname, username);
91 eosio::check(contract.has_value(), "EDUBRIDGE_CONTRACT_NOT_SIGNED: Преподаватель не подписал договор участия в хозяйственной деятельности");
92 eosio::check(contract->status == ContractStatus::ACTIVE,
93 "EDUBRIDGE_CONTRACT_NOT_APPROVED: Договор участия в хозяйственной деятельности ещё не подписан председателем совета");
94 return *contract;
95}
96
97// ── Учёт средств курса ───────────────────────────────────────────────────
98
100inline eosio::asset get_course_reserve(eosio::name coopname, uint64_t course_id) {
101 edu_courses_index courses(_edubridge, coopname.value);
102 auto it = courses.find(course_id);
103 return it == courses.end() ? eosio::asset(0, _root_govern_symbol) : it->reserve;
104}
105
108template <typename Fn>
109inline void update_course(eosio::name coopname, uint64_t course_id, Fn&& change) {
110 edu_courses_index courses(_edubridge, coopname.value);
111 auto it = courses.find(course_id);
112 if (it == courses.end()) {
113 courses.emplace(RamPayer::of(courses, coopname), [&](auto& c) {
114 const eosio::asset zero(0, _root_govern_symbol);
115 c.course_id = course_id;
116 c.collected = zero;
117 c.reserve = zero;
118 c.settled = zero;
119 change(c);
120 });
121 } else {
122 courses.modify(it, RamPayer::of(courses, coopname), [&](auto& c) { change(c); });
123 }
124}
125
127inline eosio::asset get_coop_wallet_available(eosio::name coopname, eosio::name wallet_id) {
128 wallets2_index wallets(_ledger2, coopname.value);
129 auto it = wallets.find(wallet_id.value);
130 return it == wallets.end() ? eosio::asset(0, _root_govern_symbol) : it->available;
131}
132
134inline void check_money(const eosio::asset& amount, const char* what) {
135 eosio::check(amount.is_valid() && amount.amount > 0,
136 std::string{"EDUBRIDGE_AMOUNT_NOT_POSITIVE: "} + what + " должна быть больше нуля");
137 eosio::check(amount.symbol == _root_govern_symbol,
138 std::string{"EDUBRIDGE_SYMBOL_INVALID: Некорректный символ валюты: "} + what);
139}
140
147namespace Memo {
148
149 inline std::string get_convert_to_member_memo() {
150 return "Конвертация паевого взноса в членский взнос по ЦПП «Образование» по заявлению пайщика";
151 }
152
153 inline std::string get_collect_fee_memo() {
154 return "Членский взнос за курс по ЦПП «Образование» в распоряжение кооператива";
155 }
156
157 inline std::string get_lock_fee_memo() {
158 return "Удержание членского взноса до конца гарантийного срока курса по ЦПП «Образование»";
159 }
160
161 inline std::string get_unlock_fee_memo() {
162 return "Разблокировка членского взноса по ЦПП «Образование»: гарантийный срок истёк либо подписка закрыта";
163 }
164
165 inline std::string get_allot_reserve_memo() {
166 return "Резерв выплат преподавателям из членского взноса за курс по ЦПП «Образование»";
167 }
168
169 inline std::string get_free_reserve_memo() {
170 return "Высвобождение резерва выплат преподавателям: подписка закрыта, занятия не состоятся";
171 }
172
173 inline std::string get_rate_gap_memo() {
174 return "Разница между плановой ставкой курса и ставкой преподавателя за проведённое занятие — на кошелёк программы";
175 }
176
177 inline std::string get_settle_reserve_memo(uint64_t rid_id) {
178 return "Расчёт с преподавателем за счёт резерва программы по заявлению № " + std::to_string(rid_id);
179 }
180
181 inline std::string get_refund_memo() {
182 return "Возврат членского взноса за курс по ЦПП «Образование»";
183 }
184
185 inline std::string get_return_to_share_memo() {
186 return "Возврат членского взноса по ЦПП «Образование» в паевой взнос пайщика";
187 }
188
189 inline std::string get_hold_rid_memo(uint64_t rid_id) {
190 return "Приём материалов занятия № " + std::to_string(rid_id) +
191 " на ответственное хранение по акту преподавателя";
192 }
193
194 inline std::string get_settle_rid_memo(uint64_t rid_id) {
195 return "Паевой взнос результатом интеллектуальной деятельности по заявлению № " +
196 std::to_string(rid_id) + " и решению совета";
197 }
198
199 inline std::string get_withdraw_share_memo() {
200 return "Трансляция паевого взноса из ЦПП «Образование» в ЦПП «Цифровой Кошелёк» по заявлению пайщика";
201 }
202
203 inline std::string get_release_rid_memo(uint64_t rid_id, const std::string& reason) {
204 return "Снятие материалов занятия № " + std::to_string(rid_id) +
205 " с ответственного хранения: " + reason;
206 }
207
208 inline std::string get_accept_rid_memo(uint64_t rid_id) {
209 return "Приём результата интеллектуальной деятельности преподавателя в паевой фонд по заявлению № " +
210 std::to_string(rid_id) + " и решению совета";
211 }
212
213} // namespace Memo
214
215} // namespace Edubridge
static constexpr eosio::symbol _root_govern_symbol
Definition: consts.hpp:257
static constexpr eosio::name _edubridge
Definition: consts.hpp:204
static constexpr eosio::name _ledger2
Definition: consts.hpp:202
contract
Definition: eosio.msig_tests.cpp:977
share_type amount
Definition: eosio.token_tests.cpp:174
constexpr eosio::name ACTIVE
подписан обеими сторонами
Definition: table_edubridge_contracts.hpp:31
std::string get_free_reserve_memo()
Definition: edubridge.hpp:169
std::string get_refund_memo()
Definition: edubridge.hpp:181
std::string get_rate_gap_memo()
Definition: edubridge.hpp:173
std::string get_accept_rid_memo(uint64_t rid_id)
Definition: edubridge.hpp:208
std::string get_settle_rid_memo(uint64_t rid_id)
Definition: edubridge.hpp:194
std::string get_unlock_fee_memo()
Definition: edubridge.hpp:161
std::string get_hold_rid_memo(uint64_t rid_id)
Definition: edubridge.hpp:189
std::string get_allot_reserve_memo()
Definition: edubridge.hpp:165
std::string get_return_to_share_memo()
Definition: edubridge.hpp:185
std::string get_withdraw_share_memo()
Definition: edubridge.hpp:199
std::string get_collect_fee_memo()
Definition: edubridge.hpp:153
std::string get_lock_fee_memo()
Definition: edubridge.hpp:157
std::string get_settle_reserve_memo(uint64_t rid_id)
Definition: edubridge.hpp:177
std::string get_convert_to_member_memo()
Definition: edubridge.hpp:149
std::string get_release_rid_memo(uint64_t rid_id, const std::string &reason)
Definition: edubridge.hpp:203
Definition: economy.hpp:25
std::optional< edu_contract > get_contract(eosio::name coopname, eosio::name username)
Договор УХД преподавателя по username; nullopt, если не подписывал.
Definition: edubridge.hpp:80
eosio::asset get_course_reserve(eosio::name coopname, uint64_t course_id)
Остаток резерва выплат преподавателям курса; ноль, если движений по курсу ещё не было.
Definition: edubridge.hpp:100
UserWalletAvailable get_user_wallet_balance(eosio::name coopname, eosio::name wallet_id, eosio::name username)
Definition: edubridge.hpp:47
eosio::multi_index< "edusubs"_n, edu_subscription, eosio::indexed_by<"byhash"_n, eosio::const_mem_fun< edu_subscription, checksum256, &edu_subscription::by_hash > >, eosio::indexed_by<"byusername"_n, eosio::const_mem_fun< edu_subscription, uint64_t, &edu_subscription::by_username > >, eosio::indexed_by<"bypaiduntil"_n, eosio::const_mem_fun< edu_subscription, uint64_t, &edu_subscription::by_paid_until > > > edu_subscriptions_index
Definition: table_edubridge_subscriptions.hpp:159
eosio::multi_index<"educourses"_n, edu_course > edu_courses_index
Definition: table_edubridge_courses.hpp:38
void check_money(const eosio::asset &amount, const char *what)
Валидация денежной суммы контракта: корректный asset, > 0, символ кооператива.
Definition: edubridge.hpp:134
edu_contract get_active_contract_or_fail(eosio::name coopname, eosio::name username)
Действующий (подписанный обеими сторонами) договор УХД; бросает, если его нет.
Definition: edubridge.hpp:89
void update_course(eosio::name coopname, uint64_t course_id, Fn &&change)
Definition: edubridge.hpp:109
eosio::multi_index< "edurids"_n, edu_rid, eosio::indexed_by<"byhash"_n, eosio::const_mem_fun< edu_rid, checksum256, &edu_rid::by_hash > >, eosio::indexed_by<"byusername"_n, eosio::const_mem_fun< edu_rid, uint64_t, &edu_rid::by_username > > > edu_rids_index
Definition: table_edubridge_rids.hpp:78
edu_rids_index::const_iterator get_rid_or_fail(edu_rids_index &rids, const checksum256 &rid_hash)
Заявление о РИД по rid_hash; бросает, если не найдено.
Definition: edubridge.hpp:72
eosio::multi_index< "educontracts"_n, edu_contract, eosio::indexed_by<"byusername"_n, eosio::const_mem_fun< edu_contract, uint64_t, &edu_contract::by_username > >, eosio::indexed_by<"byhash"_n, eosio::const_mem_fun< edu_contract, checksum256, &edu_contract::by_hash > > > edu_contracts_index
Definition: table_edubridge_contracts.hpp:51
eosio::asset get_coop_wallet_available(eosio::name coopname, eosio::name wallet_id)
Доступный остаток кооперативного кошелька программы; ноль, если кошелёк ещё не заведён.
Definition: edubridge.hpp:127
edu_subscriptions_index::const_iterator get_subscription_or_fail(edu_subscriptions_index &subs, const checksum256 &sub_hash)
Подписка по sub_hash; бросает, если не найдена.
Definition: edubridge.hpp:63
eosio::name of(const Table &table, eosio::name coopname)
Definition: ram_payer.hpp:78
Definition: eosio.msig.hpp:34
Definition: edubridge.hpp:41
eosio::asset blocked
Definition: edubridge.hpp:43
eosio::asset available
Definition: edubridge.hpp:42
bool exists
Definition: edubridge.hpp:44
Definition: table_edubridge_contracts.hpp:34
eosio::multi_index< "userwallets"_n, userwallet, eosio::indexed_by<"byuserwallet"_n, eosio::const_mem_fun< userwallet, uint128_t, &userwallet::by_userwallet > >, eosio::indexed_by<"byuser"_n, eosio::const_mem_fun< userwallet, uint64_t, &userwallet::by_user > >, eosio::indexed_by<"bywallet"_n, eosio::const_mem_fun< userwallet, uint64_t, &userwallet::by_wallet > > > userwallets_index
Definition: table_ledger2_userwallets.hpp:48
eosio::multi_index<"wallets"_n, wallet2 > wallets2_index
Definition: table_ledger2_wallet.hpp:40
static uint128_t combine_ids(const uint64_t &x, const uint64_t &y)
Definition: utils.hpp:7