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
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//                              Næ§@@@ÑÉ©
//                        æ@@@@@@@@@@@@@@@@@@
//                    Ñ@@@@?.?@@@@@@@@@@@@@@@@@@@N
//                 ¶@@@@@?^%@@.=@@@@@@@@@@@@@@@@@@@@
//               N@@@@@@@?^@@@»^@@@@@@@@@@@@@@@@@@@@@@
//               @@@@@@@@?^@@@».............?@@@@@@@@@É
//              Ñ@@@@@@@@?^@@@@@@@@@@@@@@@@@@'?@@@@@@@@Ñ
//              @@@@@@@@@?^@@@»..............»@@@@@@@@@@
//              @@@@@@@@@?^@@@»^@@@@@@@@@@@@@@@@@@@@@@@@
//              @@@@@@@@@?^ë@@&.@@@@@@@@@@@@@@@@@@@@@@@@
//               @@@@@@@@?^´@@@o.%@@@@@@@@@@@@@@@@@@@@©
//                @@@@@@@?.´@@@@@ë.........*.±@@@@@@@æ
//                 @@@@@@@@?´.I@@@@@@@@@@@@@@.&@@@@@N
//                  N@@@@@@@@@@ë.*=????????=?@@@@@Ñ
//                    @@@@@@@@@@@@@@@@@@@@@@@@@@@¶
//                        É@@@@@@@@@@@@@@@@Ѷ
//                             Næ§@@@ÑÉ©

// Copyright 2020 Chris D'Costa
// This file is part of Totem Live Accounting.
// Authors:
// - Félix Daudré-Vignier   email: felix@totemaccounting.com
// - Chris D'Costa          email: chris.dcosta@totemaccounting.com

// Totem is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Totem is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Totem.  If not, see <http://www.gnu.org/licenses/>.

mod chart_of_accounts;
// pub use chart_of_accounts::{Ledger, {CurrentAssets, Sales, OperatingExpenses, _0030_, B,A,P,I,X,Cogs,Commissions,_0009_}};
pub use chart_of_accounts::{Ledger, *};
    
use crate::LedgerBalance;
use frame_support::{dispatch::EncodeLike, pallet_prelude::*};
use scale_info::TypeInfo;
use sp_runtime::traits::Member;
use sp_std::prelude::*;

/// Main Totem accounting trait.
pub trait Posting<AccountId, Hash, BlockNumber, CoinAmount> {
    type PostingIndex: Member + Copy + Into<u128> + Encode + Decode + Eq;

    fn handle_multiposting_amounts(
        keys: &[Record<AccountId, Hash, BlockNumber>],
    ) -> DispatchResultWithPostInfo;

    fn account_for_simple_transfer(
        from: AccountId,
        to: AccountId,
        amount: CoinAmount,
    ) -> DispatchResultWithPostInfo;

    fn account_for_fees(fee: CoinAmount, payer: AccountId) -> DispatchResultWithPostInfo;
    fn account_for_burnt_fees(fee: CoinAmount, loser: AccountId) -> DispatchResultWithPostInfo;
    fn distribute_fees_rewards(fee: CoinAmount, author: AccountId) -> DispatchResultWithPostInfo;

    fn get_escrow_account() -> AccountId;
    
    fn get_netfees_account() -> AccountId;

    fn get_pseudo_random_hash(s: AccountId, r: AccountId) -> Hash;
}

/// Debit or Credit Indicator
/// Debit and Credit balances are account specific - see chart of accounts.
#[derive(Clone, Decode, Encode, Copy, TypeInfo)]
#[scale_info(capture_docs = "always")]
pub enum Indicator {
    /// Debit
    Debit = 0,
    /// Credit
    Credit = 1,
}

#[derive(Clone, Decode, Encode, TypeInfo)]
pub struct Record<AccountId, Hash, BlockNumber> {
    pub primary_party: AccountId,
    pub counterparty: AccountId,
    pub ledger: Ledger,
    pub amount: LedgerBalance,
    pub debit_credit: Indicator,
    pub reference_hash: Hash,
    pub changed_on_blocknumber: BlockNumber,
    pub applicable_period_blocknumber: BlockNumber,
}

// Implementations

impl EncodeLike<Indicator> for bool {}

impl Indicator {
    pub fn reverse(self) -> Self {
        match self {
            Self::Debit => Self::Credit,
            Self::Credit => Self::Debit,
        }
    }
}

#[cfg(any(test, feature = "mock"))]
impl<AccountId, Hash, BlockNumber, CoinAmount> Posting<AccountId, Hash, BlockNumber, CoinAmount>
    for ()
{
    type PostingIndex = u128;

    fn handle_multiposting_amounts(
        _fwd: &[Record<AccountId, Hash, BlockNumber>],
    ) -> DispatchResultWithPostInfo {
        unimplemented!("Used as a mock, shouldn't be called")
    }

    fn account_for_simple_transfer(
        _from: AccountId,
        _to: AccountId,
        _amount: CoinAmount,
    ) -> DispatchResultWithPostInfo {
        unimplemented!("Used as a mock, shouldn't be called")
    }

    fn account_for_fees(_f: CoinAmount, _p: AccountId) -> DispatchResultWithPostInfo {
        unimplemented!("Used as a mock, shouldn't be called")
    }

    fn account_for_burnt_fees(_f: CoinAmount, _p: AccountId) -> DispatchResultWithPostInfo {
        unimplemented!("Used as a mock, shouldn't be called")
    }
    
    fn distribute_fees_rewards(_f: CoinAmount, _p: AccountId) -> DispatchResultWithPostInfo {
        unimplemented!("Used as a mock, shouldn't be called")
    }

    fn account_for_burnt_fees(_f: CoinAmount, _p: AccountId) -> DispatchResultWithPostInfo {
        unimplemented!("Used as a mock, shouldn't be called")
    }

    fn get_escrow_account() -> AccountId {
        unimplemented!("Used as a mock, shouldn't be called")
    }

    fn get_netfees_account() -> AccountId {
        unimplemented!("Used as a mock, shouldn't be called")
    }

    fn get_pseudo_random_hash(_s: AccountId, _r: AccountId) -> Hash {
        unimplemented!("Used as a mock, shouldn't be called")
    }
}