Loading...
Searching...
No Matches
post.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2003-2025, John Wiegley. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of New Artisans LLC nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
35
42#pragma once
43
44#include "item.h"
45
46namespace ledger {
47
48class xact_t;
49class account_t;
50
51class post_t : public item_t
52{
53public:
54#define POST_VIRTUAL 0x0010 // the account was specified with (parens)
55#define POST_MUST_BALANCE 0x0020 // posting must balance in the transaction
56#define POST_CALCULATED 0x0040 // posting's amount was calculated
57#define POST_COST_CALCULATED 0x0080 // posting's cost was calculated
58#define POST_COST_IN_FULL 0x0100 // cost specified using @@
59#define POST_COST_FIXATED 0x0200 // cost is fixed using = indicator
60#define POST_COST_VIRTUAL 0x0400 // cost is virtualized: (@)
61#define POST_ANONYMIZED 0x0800 // a temporary, anonymous posting
62#define POST_DEFERRED 0x1000 // the account was specified with <angles>
63#define POST_IS_TIMELOG 0x2000 // the posting is a timelog entry
64
65 xact_t * xact; // only set for posts of regular xacts
67
68 amount_t amount; // can be null until finalization
69 optional<expr_t> amount_expr;
70 optional<amount_t> cost;
71 optional<amount_t> given_cost;
72 optional<amount_t> assigned_amount;
73 optional<datetime_t> checkin;
74 optional<datetime_t> checkout;
75
76private:
77
78 optional<string> _payee;
79
80public:
81
82 post_t(account_t * _account = NULL,
84 : item_t(_flags), xact(NULL), account(_account)
85 {
86 TRACE_CTOR(post_t, "account_t *, flags_t");
87 }
88 post_t(account_t * _account,
89 const amount_t& _amount,
91 const optional<string>& _note = none)
92 : item_t(_flags, _note), xact(NULL), account(_account), amount(_amount)
93 {
94 TRACE_CTOR(post_t, "account_t *, amount_t, flags_t, optional<string>");
95 }
96 post_t(const post_t& post)
97 : item_t(post),
98 xact(post.xact),
99 account(post.account),
100 amount(post.amount),
101 cost(post.cost),
103 checkin(post.checkin),
104 checkout(post.checkout),
105 xdata_(post.xdata_)
106 {
107 copy_details(post);
108 TRACE_CTOR(post_t, "copy");
109 }
110 virtual ~post_t() {
112 }
113
114 virtual string description() {
115 if (pos) {
116 std::ostringstream buf;
117 buf << _f("posting at line %1%") % pos->beg_line;
118 return buf.str();
119 } else {
120 return string(_("generated posting"));
121 }
122 }
123
124 virtual bool has_tag(const string& tag,
125 bool inherit = true) const;
126 virtual bool has_tag(const mask_t& tag_mask,
127 const optional<mask_t>& value_mask = none,
128 bool inherit = true) const;
129
130 virtual optional<value_t> get_tag(const string& tag,
131 bool inherit = true) const;
132 virtual optional<value_t> get_tag(const mask_t& tag_mask,
133 const optional<mask_t>& value_mask = none,
134 bool inherit = true) const;
135
136 virtual date_t value_date() const;
137 virtual date_t date() const;
138 virtual date_t primary_date() const;
139 virtual optional<date_t> aux_date() const;
140
141 string payee_from_tag() const;
142 string payee() const;
143 void set_payee(const string& payee) {
144 _payee = payee;
145 }
146
150
152 const string& name);
153
155
156 std::size_t xact_id() const;
157 std::size_t account_id() const;
158
159 virtual void copy_details(const item_t& item) {
160 const post_t& post(dynamic_cast<const post_t&>(item));
161 xdata_ = post.xdata_;
163 }
164
165 bool valid() const;
166
167 struct xdata_t : public supports_flags<uint_least16_t>
168 {
169#define POST_EXT_RECEIVED 0x0001
170#define POST_EXT_HANDLED 0x0002
171#define POST_EXT_DISPLAYED 0x0004
172#define POST_EXT_DIRECT_AMT 0x0008
173#define POST_EXT_SORT_CALC 0x0010
174#define POST_EXT_COMPOUND 0x0020
175#define POST_EXT_VISITED 0x0040
176#define POST_EXT_MATCHES 0x0080
177#define POST_EXT_CONSIDERED 0x0100
178
182 std::size_t count;
187
188 std::list<sort_value_t> sort_values;
189
191 : supports_flags<uint_least16_t>(), count(0), account(NULL) {
193 }
194 xdata_t(const xdata_t& other)
195 : supports_flags<uint_least16_t>(other.flags()),
198 total(other.total),
199 count(other.count),
200 date(other.date),
201 account(other.account),
203 {
205 }
206 ~xdata_t() throw() {
208 }
209 };
210
211 // This variable holds optional "extended data" which is usually produced
212 // only during reporting, and only for the posting set being reported.
213 // It's a memory-saving measure to delay allocation until the last possible
214 // moment.
215 mutable optional<xdata_t> xdata_;
216
217 bool has_xdata() const {
218 return static_cast<bool>(xdata_);
219 }
220 void clear_xdata() {
221 xdata_ = none;
222 }
224 if (! xdata_)
225 xdata_ = xdata_t();
226 return *xdata_;
227 }
228 const xdata_t& xdata() const {
229 return const_cast<post_t *>(this)->xdata();
230 }
231
233 const optional<expr_t&>& expr = none) const;
234
236
238 if (xdata_)
239 if (account_t * acct = xdata_->account)
240 return acct;
242 return account;
243 }
244
245 const account_t * reported_account() const {
246 return const_cast<post_t *>(this)->reported_account();
247 }
248
249 friend class xact_t;
250
252 {
253 bool operator()(const post_t * left, const post_t * right) const {
254 gregorian::date_duration duration =
255 left->primary_date() - right->primary_date();
256 if (duration.days() == 0) {
257 return ((left->pos ? left->pos->sequence : 0) <
258 (right->pos ? right->pos->sequence : 0));
259 } else {
260 return duration.days() < 0;
261 }
262 }
263 };
264};
265
266class journal_t;
267void extend_post(post_t& post, journal_t& journal);
268void put_post(property_tree::ptree& pt, const post_t& post);
269
270} // namespace ledger
#define POST_VIRTUAL
Definition post.h:54
#define POST_IS_TIMELOG
Definition post.h:63
#define POST_MUST_BALANCE
Definition post.h:55
#define ITEM_NORMAL
Definition item.h:85
#define TRACE_DTOR(cls)
Definition utils.h:144
#define TRACE_CTOR(cls, args)
Definition utils.h:143
#define assert(x)
Definition utils.h:92
void put_post(property_tree::ptree &pt, const post_t &post)
boost::gregorian::date date_t
Definition times.h:60
std::string string
Definition utils.h:59
void extend_post(post_t &post, journal_t &journal)
boost::posix_time::ptime datetime_t
Definition times.h:53
Encapsulate infinite-precision commoditized amounts.
Definition amount.h:96
intrusive_ptr< op_t > ptr_op_t
Definition expr.h:57
bool has_flags(const flags_t arg) const
Definition flags.h:77
virtual void copy_details(const item_t &item)
Definition item.h:118
item_t(flags_t _flags=0x00, const optional< string > &_note=none)
Definition item.h:104
optional< position_t > pos
Definition item.h:101
optional< expr_t > amount_expr
Definition post.h:69
virtual void copy_details(const item_t &item)
Definition post.h:159
virtual ~post_t()
Definition post.h:110
virtual optional< date_t > aux_date() const
post_t(account_t *_account=NULL, flags_t _flags=0x00)
Definition post.h:82
const xdata_t & xdata() const
Definition post.h:228
const account_t * reported_account() const
Definition post.h:245
amount_t resolve_expr(scope_t &scope, expr_t &expr)
optional< datetime_t > checkout
Definition post.h:74
std::size_t xact_id() const
virtual bool has_tag(const mask_t &tag_mask, const optional< mask_t > &value_mask=none, bool inherit=true) const
xdata_t & xdata()
Definition post.h:223
virtual date_t primary_date() const
virtual date_t value_date() const
post_t(account_t *_account, const amount_t &_amount, flags_t _flags=0x00, const optional< string > &_note=none)
Definition post.h:88
virtual optional< value_t > get_tag(const string &tag, bool inherit=true) const
void set_payee(const string &payee)
Definition post.h:143
std::size_t account_id() const
void set_reported_account(account_t *account)
virtual string description()
Definition post.h:114
void clear_xdata()
Definition post.h:220
post_t(const post_t &post)
Definition post.h:96
optional< amount_t > assigned_amount
Definition post.h:72
bool must_balance() const
Definition post.h:147
optional< xdata_t > xdata_
Definition post.h:215
virtual optional< value_t > get_tag(const mask_t &tag_mask, const optional< mask_t > &value_mask=none, bool inherit=true) const
virtual expr_t::ptr_op_t lookup(const symbol_t::kind_t kind, const string &name)
account_t * reported_account()
Definition post.h:237
amount_t amount
Definition post.h:68
void add_to_value(value_t &value, const optional< expr_t & > &expr=none) const
optional< amount_t > cost
Definition post.h:70
virtual bool has_tag(const string &tag, bool inherit=true) const
bool valid() const
optional< datetime_t > checkin
Definition post.h:73
friend class xact_t
Definition post.h:249
optional< amount_t > given_cost
Definition post.h:71
virtual date_t date() const
string payee() const
xact_t * xact
Definition post.h:65
string payee_from_tag() const
account_t * account
Definition post.h:66
bool has_xdata() const
Definition post.h:217
value_t visited_value
Definition post.h:179
value_t compound_value
Definition post.h:180
account_t * account
Definition post.h:186
xdata_t(const xdata_t &other)
Definition post.h:194
datetime_t datetime
Definition post.h:185
std::size_t count
Definition post.h:182
std::list< sort_value_t > sort_values
Definition post.h:188
bool operator()(const post_t *left, const post_t *right) const
Definition post.h:253
Dynamic type representing various numeric types.
Definition value.h:86