Loading...
Searching...
No Matches
post.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2003-2023, 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
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
64 xact_t * xact; // only set for posts of regular xacts
66
67 amount_t amount; // can be null until finalization
74
75private:
76
77 optional<string> _payee;
78
79public:
80
84 {
85 TRACE_CTOR(post_t, "account_t *, flags_t");
86 }
88 const amount_t& _amount,
92 {
93 TRACE_CTOR(post_t, "account_t *, amount_t, flags_t, optional<string>");
94 }
109 virtual ~post_t() {
111 }
112
113 virtual string description() {
114 if (pos) {
115 std::ostringstream buf;
116 buf << _f("posting at line %1%") % pos->beg_line;
117 return buf.str();
118 } else {
119 return string(_("generated posting"));
120 }
121 }
122
123 virtual bool has_tag(const string& tag,
124 bool inherit = true) const;
125 virtual bool has_tag(const mask_t& tag_mask,
127 bool inherit = true) const;
128
129 virtual optional<value_t> get_tag(const string& tag,
130 bool inherit = true) const;
133 bool inherit = true) const;
134
135 virtual date_t value_date() const;
136 virtual date_t date() const;
137 virtual date_t primary_date() const;
138 virtual optional<date_t> aux_date() const;
139
140 string payee_from_tag() const;
141 string payee() const;
142 void set_payee(const string& payee) {
143 _payee = payee;
144 }
145
146 bool must_balance() const {
148 }
149
151 const string& name);
152
154
155 std::size_t xact_id() const;
156 std::size_t account_id() const;
157
158 virtual void copy_details(const item_t& item) {
159 const post_t& post(dynamic_cast<const post_t&>(item));
160 xdata_ = post.xdata_;
162 }
163
164 bool valid() const;
165
166 struct xdata_t : public supports_flags<uint_least16_t>
167 {
168#define POST_EXT_RECEIVED 0x0001
169#define POST_EXT_HANDLED 0x0002
170#define POST_EXT_DISPLAYED 0x0004
171#define POST_EXT_DIRECT_AMT 0x0008
172#define POST_EXT_SORT_CALC 0x0010
173#define POST_EXT_COMPOUND 0x0020
174#define POST_EXT_VISITED 0x0040
175#define POST_EXT_MATCHES 0x0080
176#define POST_EXT_CONSIDERED 0x0100
177
181 std::size_t count;
186
187 std::list<sort_value_t> sort_values;
188
190 : supports_flags<uint_least16_t>(), count(0), account(NULL) {
192 }
208 };
209
210 // This variable holds optional "extended data" which is usually produced
211 // only during reporting, and only for the posting set being reported.
212 // It's a memory-saving measure to delay allocation until the last possible
213 // moment.
215
216 bool has_xdata() const {
217 return static_cast<bool>(xdata_);
218 }
219 void clear_xdata() {
220 xdata_ = none;
221 }
223 if (! xdata_)
224 xdata_ = xdata_t();
225 return *xdata_;
226 }
227 const xdata_t& xdata() const {
228 return const_cast<post_t *>(this)->xdata();
229 }
230
232 const optional<expr_t&>& expr = none) const;
233
235
237 if (xdata_)
238 if (account_t * acct = xdata_->account)
239 return acct;
241 return account;
242 }
243
244 const account_t * reported_account() const {
245 return const_cast<post_t *>(this)->reported_account();
246 }
247
248 friend class xact_t;
249
251 {
252 bool operator()(const post_t * left, const post_t * right) const {
253 gregorian::date_duration duration =
254 left->primary_date() - right->primary_date();
255 if (duration.days() == 0) {
256 return ((left->pos ? left->pos->sequence : 0) <
257 (right->pos ? right->pos->sequence : 0));
258 } else {
259 return duration.days() < 0;
260 }
261 }
262 };
263};
264
265class journal_t;
267void put_post(property_tree::ptree& pt, const post_t& post);
268
269} // namespace ledger
#define TRACE_DTOR(cls)
Definition utils.h:144
#define TRACE_CTOR(cls, args)
Definition utils.h:143
#define assert(x)
Definition utils.h:92
#define ITEM_NORMAL
Definition item.h:85
#define POST_VIRTUAL
Definition post.h:54
#define POST_MUST_BALANCE
Definition post.h:55
void put_post(property_tree::ptree &pt, const post_t &post)
gregorian::date date
Definition utils.h:64
boost::gregorian::date date_t
Definition times.h:60
std::string string
Definition utils.h:59
T & downcast(U &object)
Definition utils.h:468
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
optional< position_t > pos
Definition item.h:101
optional< expr_t > amount_expr
Definition post.h:68
virtual void copy_details(const item_t &item)
Definition post.h:158
virtual ~post_t()
Definition post.h:109
virtual optional< date_t > aux_date() const
post_t(account_t *_account=NULL, flags_t _flags=0x00)
Definition post.h:81
const xdata_t & xdata() const
Definition post.h:227
const account_t * reported_account() const
Definition post.h:244
amount_t resolve_expr(scope_t &scope, expr_t &expr)
optional< datetime_t > checkout
Definition post.h:73
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:222
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:87
virtual optional< value_t > get_tag(const string &tag, bool inherit=true) const
void set_payee(const string &payee)
Definition post.h:142
std::size_t account_id() const
void set_reported_account(account_t *account)
virtual string description()
Definition post.h:113
void clear_xdata()
Definition post.h:219
post_t(const post_t &post)
Definition post.h:95
optional< amount_t > assigned_amount
Definition post.h:71
bool must_balance() const
Definition post.h:146
optional< xdata_t > xdata_
Definition post.h:214
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:236
amount_t amount
Definition post.h:67
void add_to_value(value_t &value, const optional< expr_t & > &expr=none) const
optional< amount_t > cost
Definition post.h:69
virtual bool has_tag(const string &tag, bool inherit=true) const
bool valid() const
optional< datetime_t > checkin
Definition post.h:72
optional< amount_t > given_cost
Definition post.h:70
virtual date_t date() const
string payee() const
xact_t * xact
Definition post.h:64
string payee_from_tag() const
account_t * account
Definition post.h:65
bool has_xdata() const
Definition post.h:216
value_t visited_value
Definition post.h:178
value_t compound_value
Definition post.h:179
account_t * account
Definition post.h:185
xdata_t(const xdata_t &other)
Definition post.h:193
datetime_t datetime
Definition post.h:184
std::size_t count
Definition post.h:181
std::list< sort_value_t > sort_values
Definition post.h:187
bool operator()(const post_t *left, const post_t *right) const
Definition post.h:252
Dynamic type representing various numeric types.
Definition value.h:83