Branch data Line data Source code
1 : : /********************************************************************\
2 : : * Transaction.c -- transaction implementation *
3 : : * Copyright (C) 1997 Robin D. Clark *
4 : : * Copyright (C) 1997-2003 Linas Vepstas <linas@linas.org> *
5 : : * Copyright (C) 2000 Bill Gribble <grib@billgribble.com> *
6 : : * Copyright (c) 2006 David Hampton <hampton@employees.org> *
7 : : * *
8 : : * This program is free software; you can redistribute it and/or *
9 : : * modify it under the terms of the GNU General Public License as *
10 : : * published by the Free Software Foundation; either version 2 of *
11 : : * the License, or (at your option) any later version. *
12 : : * *
13 : : * This program is distributed in the hope that it will be useful, *
14 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 : : * GNU General Public License for more details. *
17 : : * *
18 : : * You should have received a copy of the GNU General Public License*
19 : : * along with this program; if not, contact: *
20 : : * *
21 : : * Free Software Foundation Voice: +1-617-542-5942 *
22 : : * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
23 : : * Boston, MA 02110-1301, USA gnu@gnu.org *
24 : : * *
25 : : \********************************************************************/
26 : :
27 : : #include "qofinstance.h"
28 : : #include <config.h>
29 : :
30 : : #include <platform.h>
31 : : #if PLATFORM(WINDOWS)
32 : : #include <windows.h>
33 : : #endif
34 : :
35 : : #include <glib.h>
36 : : #include <glib/gi18n.h>
37 : : #include <stdlib.h>
38 : : #include <string.h>
39 : : #include <stdint.h>
40 : : #include <time.h>
41 : : #ifdef HAVE_UNISTD_H
42 : : # include <unistd.h>
43 : : #endif
44 : :
45 : : #include "AccountP.hpp"
46 : : #include "Scrub.h"
47 : : #include "Scrub3.h"
48 : : #include "TransactionP.hpp"
49 : : #include "SplitP.hpp"
50 : : #include "TransLog.h"
51 : : #include "cap-gains.h"
52 : : #include "gnc-commodity.h"
53 : : #include "gnc-engine.h"
54 : : #include "gnc-lot.h"
55 : : #include "gnc-event.h"
56 : : #include <gnc-date.h>
57 : : #include "SchedXaction.h"
58 : : #include "gncBusiness.h"
59 : : #include <qofinstance-p.h>
60 : : #include "gncInvoice.h"
61 : : #include "gncOwner.h"
62 : :
63 : : /* Notes about xaccTransBeginEdit(), xaccTransCommitEdit(), and
64 : : * xaccTransRollback():
65 : : *
66 : : * Why use it:
67 : : *
68 : : * Data consistency: Wrapping your changes to financial data inside
69 : : * a BeginEdit/CommitEdit block allows the engine to verify that
70 : : * your changes still leave the financial objects in an internally
71 : : * consistent state. This is true even though you may make a series
72 : : * of individual changes that are not consistent by themselves. In
73 : : * this way, it's like telling the engine, "Okay, I've finished my
74 : : * edits. Please check my work."
75 : : *
76 : : * Data integrity: The other benefit of the BeginEdit/CommitEdit
77 : : * block is that it allows the engine (and the backend) to remember
78 : : * the last known correct state of your data. This allows you to
79 : : * undo any changes that you don't want to keep. In this way, it's
80 : : * like telling the engine telling the back end, "Yes, I really mean
81 : : * it. Remember this data." or "Nevermind, scratch that." The
82 : : * important feature here is that if things go bad, for whatever
83 : : * reason (e.g. the application crashed, you lost the backend), your
84 : : * data remains in the state it was in just after the previous
85 : : * xaccTransCommitEdit(). [assuming no nesting, which probably
86 : : * isn't useful outside the engine.]
87 : : *
88 : : * Note that the backend doesn't care about data consistency -
89 : : * that's the engine's job.
90 : : *
91 : : * Example Use:
92 : : *
93 : : * xaccTransBeginEdit(trans);
94 : : *
95 : : *
96 : : * split = xaccMallocSplit(book);
97 : : * xaccSplitSetAccount(split, acc);
98 : : * xaccSplitSetParent(split, trans); // Adding a new split
99 : : *
100 : : * xaccSplitSetValue(split, val); // Changing a split
101 : : *
102 : : * xaccSplitDestroy(split); // Removing a split
103 : : *
104 : : * xaccTransSetNum(trans, "501"); // Changing the trans
105 : : *
106 : : * if (really_do_it)
107 : : * xaccTransCommitEdit(trans);
108 : : * else
109 : : * xaccTransRollbackEdit(trans);
110 : : *
111 : : * How it works:
112 : : *
113 : : * Calling xaccTransBeginEdit() starts a BeginEdit/CommitEdit block.
114 : : * Inside the block any changes to the transaction or any splits in
115 : : * the transaction are considered "pending". What does that mean?
116 : : *
117 : : * In general that means that if you set and then get the
118 : : * transaction's or split's parameters inside the
119 : : * BeginEdit/CommitEdit block, you'll get the values you just set.
120 : : * However, if you change an object's many-to-one relationship with
121 : : * another object, you won't see the change from the "many" side
122 : : * until the CommitEdit. For example, if you move a split from one
123 : : * account into another, you can see the change with
124 : : * xaccSplitGetAccount(), but both Accounts' split lists won't be
125 : : * updated until the CommitEdit. Correspondingly, no signals
126 : : * (events) will be generated for those "foreign" objects, or the
127 : : * Transaction, until the CommitEdit.
128 : : *
129 : : * This behavior is important because, when we're finally ready to
130 : : * commit to the backend, we can't be 100% sure that the backend
131 : : * will still be available. We have to offer the backend all of the
132 : : * new state as if it were already "true", but we need to save all of
133 : : * the old state in case the backend won't accept our commit. If
134 : : * the backend commit fails, we have to restore all the old state.
135 : : * If the backend commit succeeds, and *only* after it succeeds, we
136 : : * can advertise the new state to the rest of the engine (and gui).
137 : : *
138 : : * Q: Who owns the ref of an added split if the Transaction is rolled
139 : : * back?
140 : : *
141 : : * A: This is a design decision. If the answer is 'the user',
142 : : * then the burden is on the api user to check the transaction after
143 : : * every commit to see if the added split is really in the
144 : : * transaction. If they don't they risk leaking the split if the
145 : : * commit was rolled back. Another design is to answer 'the engine'.
146 : : * In that case the burden is on the engine to free a newly added
147 : : * split if the commit is rolled back. Unfortunately the engine
148 : : * objects aren't ref-counted, so this is tricky.
149 : : *
150 : : * In the current implementation, the answer is 'the engine', but
151 : : * that means that you must not add the split to two different
152 : : * transactions during the begin/commit block, because if one rolls
153 : : * back, they will both think they own the split. This is one
154 : : * specific example of the general problem that the outcome of two
155 : : * parallel begin/commit edit blocks for two transactions where edits
156 : : * for both transactions involve the same splits and one or more
157 : : * edit-blocks is rolled-back, is poorly-defined.
158 : : *
159 : : *
160 : : *
161 : : * Design notes on event-generation: transaction-modified-events
162 : : * should not be generated until transaction commit or rollback
163 : : * time. They should not be generated as each field is tweaked.
164 : : * This for two reasons:
165 : : * 1) Most editing events make multiple changes to a transaction,
166 : : * which would generate a flurry of (needless) events, if they
167 : : * weren't saved up till the commit.
168 : : * 2) Technically, its incorrect to use transaction data
169 : : * until the transaction is committed. The GUI element that
170 : : * is changing the data can look at it, but all of the rest
171 : : * of the GUI should ignore the data until its committed.
172 : : */
173 : :
174 : : const char *trans_notes_str = "notes";
175 : : const char *void_reason_str = "void-reason";
176 : : const char *void_time_str = "void-time";
177 : : const char *void_former_notes_str = "void-former-notes";
178 : : const char *trans_is_closing_str = "book_closing";
179 : : const char *doclink_uri_str = "assoc_uri"; // this is the old name for the document link, kept for compatibility
180 : :
181 : : /* KVP entry for date-due value */
182 : : #define TRANS_DATE_DUE_KVP "trans-date-due"
183 : : #define TRANS_TXN_TYPE_KVP "trans-txn-type"
184 : : #define TRANS_READ_ONLY_REASON "trans-read-only"
185 : : #define TRANS_REVERSED_BY "reversed-by"
186 : : #define GNC_SX_FROM "from-sched-xaction"
187 : :
188 : : #define ISO_DATELENGTH 32 /* length of an iso 8601 date string. */
189 : :
190 : : /* This static indicates the debugging module that this .o belongs to. */
191 : : static QofLogModule log_module = GNC_MOD_ENGINE;
192 : :
193 : : enum
194 : : {
195 : : PROP_0,
196 : : PROP_CURRENCY, /* Table */
197 : : PROP_NUM, /* Table */
198 : : PROP_POST_DATE, /* Table */
199 : : PROP_ENTER_DATE, /* Table */
200 : : PROP_DESCRIPTION, /* Table */
201 : : PROP_INVOICE, /* KVP */
202 : : PROP_SX_TXN, /* KVP */
203 : : };
204 : :
205 : : void
206 : 15 : check_open (const Transaction *trans)
207 : : {
208 : 15 : if (trans && 0 >= qof_instance_get_editlevel(trans))
209 : 1 : PERR ("transaction %p not open for editing", trans);
210 : 15 : }
211 : : /********************************************************************\
212 : : \********************************************************************/
213 : : gboolean
214 : 24240 : xaccTransStillHasSplit(const Transaction *trans, const Split *s)
215 : : {
216 : 24240 : return (s && s->parent == trans && !qof_instance_get_destroying(s));
217 : : }
218 : :
219 : : /* Executes 'cmd_block' for each split currently in the transaction,
220 : : * using the in-edit state. Use the variable 's' for each split. */
221 : : #define FOR_EACH_SPLIT(trans, cmd_block) if (trans->splits) { \
222 : : GList *splits; \
223 : : for (splits = (trans)->splits; splits; splits = splits->next) { \
224 : : Split *s = GNC_SPLIT(splits->data); \
225 : : if (xaccTransStillHasSplit(trans, s)) { \
226 : : cmd_block; \
227 : : } \
228 : : } \
229 : : }
230 : :
231 : : static inline void mark_trans (Transaction *trans);
232 : 8933 : void mark_trans (Transaction *trans)
233 : : {
234 : 9711 : FOR_EACH_SPLIT(trans, mark_split(s));
235 : 8933 : }
236 : :
237 : : static inline void gen_event_trans (Transaction *trans);
238 : 4642 : void gen_event_trans (Transaction *trans)
239 : : {
240 : : GList *node;
241 : :
242 : 14146 : for (node = trans->splits; node; node = node->next)
243 : : {
244 : 9504 : Split *s = GNC_SPLIT(node->data);
245 : 9504 : Account *account = s->acc;
246 : 9504 : GNCLot *lot = s->lot;
247 : 9504 : if (account)
248 : 9486 : qof_event_gen (&account->inst, GNC_EVENT_ITEM_CHANGED, s);
249 : :
250 : 9504 : if (lot)
251 : : {
252 : : /* A change of transaction date might affect opening date of lot */
253 : 315 : qof_event_gen (QOF_INSTANCE(lot), QOF_EVENT_MODIFY, nullptr);
254 : : }
255 : : }
256 : 4642 : }
257 : :
258 : : /* GObject Initialization */
259 : 28223 : G_DEFINE_TYPE(Transaction, gnc_transaction, QOF_TYPE_INSTANCE)
260 : :
261 : : static void
262 : 8392 : gnc_transaction_init(Transaction* trans)
263 : : {
264 : 8392 : ENTER ("trans=%p", trans);
265 : : /* Fill in some sane defaults */
266 : 8392 : trans->num = CACHE_INSERT("");
267 : 8392 : trans->description = CACHE_INSERT("");
268 : 8392 : trans->common_currency = nullptr;
269 : 8392 : trans->splits = nullptr;
270 : 8392 : trans->date_entered = 0;
271 : 8392 : trans->date_posted = 0;
272 : 8392 : trans->marker = 0;
273 : 8392 : trans->orig = nullptr;
274 : 8392 : trans->txn_type = TXN_TYPE_UNCACHED;
275 : 8392 : LEAVE (" ");
276 : 8392 : }
277 : :
278 : : static void
279 : 7257 : gnc_transaction_dispose(GObject *txnp)
280 : : {
281 : 7257 : G_OBJECT_CLASS(gnc_transaction_parent_class)->dispose(txnp);
282 : 7257 : }
283 : :
284 : : static void
285 : 7256 : gnc_transaction_finalize(GObject* txnp)
286 : : {
287 : 7256 : G_OBJECT_CLASS(gnc_transaction_parent_class)->finalize(txnp);
288 : 7256 : }
289 : :
290 : : /* Note that g_value_set_object() refs the object, as does
291 : : * g_object_get(). But g_object_get() only unrefs once when it disgorges
292 : : * the object, leaving an unbalanced ref, which leaks. So instead of
293 : : * using g_value_set_object(), use g_value_take_object() which doesn't
294 : : * ref the object when used in get_property().
295 : : */
296 : : static void
297 : 248 : gnc_transaction_get_property(GObject* object,
298 : : guint prop_id,
299 : : GValue* value,
300 : : GParamSpec* pspec)
301 : : {
302 : : Transaction* tx;
303 : : Time64 time;
304 : :
305 : 248 : g_return_if_fail(GNC_IS_TRANSACTION(object));
306 : :
307 : 248 : tx = GNC_TRANSACTION(object);
308 : 248 : switch (prop_id)
309 : : {
310 : 12 : case PROP_NUM:
311 : 12 : g_value_set_string(value, tx->num);
312 : 12 : break;
313 : 19 : case PROP_DESCRIPTION:
314 : 19 : g_value_set_string(value, tx->description);
315 : 19 : break;
316 : 12 : case PROP_CURRENCY:
317 : 12 : g_value_take_object(value, tx->common_currency);
318 : 12 : break;
319 : 12 : case PROP_POST_DATE:
320 : 12 : time.t = tx->date_posted;
321 : 12 : g_value_set_boxed(value, &time);
322 : 12 : break;
323 : 12 : case PROP_ENTER_DATE:
324 : 12 : time.t = tx->date_entered;
325 : 12 : g_value_set_boxed(value, &time);
326 : 12 : break;
327 : 180 : case PROP_INVOICE:
328 : 180 : qof_instance_get_kvp (QOF_INSTANCE (tx), value, 2, GNC_INVOICE_ID, GNC_INVOICE_GUID);
329 : 180 : break;
330 : 1 : case PROP_SX_TXN:
331 : 1 : qof_instance_get_kvp (QOF_INSTANCE (tx), value, 1, GNC_SX_FROM);
332 : 1 : break;
333 : 0 : default:
334 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
335 : 0 : break;
336 : : }
337 : : }
338 : :
339 : : static void
340 : 145 : gnc_transaction_set_property(GObject* object,
341 : : guint prop_id,
342 : : const GValue* value,
343 : : GParamSpec* pspec)
344 : : {
345 : : Transaction* tx;
346 : : Time64 *t;
347 : :
348 : 145 : g_return_if_fail(GNC_IS_TRANSACTION(object));
349 : :
350 : 145 : tx = GNC_TRANSACTION(object);
351 : 145 : g_assert (qof_instance_get_editlevel(tx));
352 : :
353 : 145 : switch (prop_id)
354 : : {
355 : 13 : case PROP_NUM:
356 : 13 : xaccTransSetNum( tx, g_value_get_string(value));
357 : 13 : break;
358 : 13 : case PROP_DESCRIPTION:
359 : 13 : xaccTransSetDescription(tx, g_value_get_string(value));
360 : 13 : break;
361 : 13 : case PROP_CURRENCY:
362 : 13 : xaccTransSetCurrency(tx, GNC_COMMODITY(g_value_get_object(value)));
363 : 13 : break;
364 : 13 : case PROP_POST_DATE:
365 : 13 : t = (Time64*)g_value_get_boxed(value);
366 : 13 : xaccTransSetDatePostedSecs(tx, t->t);
367 : 13 : break;
368 : 13 : case PROP_ENTER_DATE:
369 : 13 : t = (Time64*)g_value_get_boxed(value);
370 : 13 : xaccTransSetDateEnteredSecs(tx, t->t);
371 : 13 : break;
372 : 75 : case PROP_INVOICE:
373 : 75 : qof_instance_set_kvp (QOF_INSTANCE (tx), value, 2, GNC_INVOICE_ID, GNC_INVOICE_GUID);
374 : 75 : break;
375 : 5 : case PROP_SX_TXN:
376 : 5 : qof_instance_set_kvp (QOF_INSTANCE (tx), value, 1, GNC_SX_FROM);
377 : 5 : break;
378 : 0 : default:
379 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
380 : 0 : break;
381 : : }
382 : : }
383 : :
384 : : static void
385 : 42 : gnc_transaction_class_init(TransactionClass* klass)
386 : : {
387 : 42 : GObjectClass* gobject_class = G_OBJECT_CLASS(klass);
388 : :
389 : 42 : gobject_class->dispose = gnc_transaction_dispose;
390 : 42 : gobject_class->finalize = gnc_transaction_finalize;
391 : 42 : gobject_class->set_property = gnc_transaction_set_property;
392 : 42 : gobject_class->get_property = gnc_transaction_get_property;
393 : :
394 : : g_object_class_install_property
395 : 42 : (gobject_class,
396 : : PROP_NUM,
397 : : g_param_spec_string("num",
398 : : "Transaction Number",
399 : : "The transactionNumber is an arbitrary string "
400 : : "assigned by the user. It is intended to be "
401 : : "a short 1-6 character string that is displayed "
402 : : "by the register. For checks, it is usually the "
403 : : "check number. For other types of transactions, "
404 : : "it can be any string.",
405 : : nullptr,
406 : : G_PARAM_READWRITE));
407 : :
408 : : g_object_class_install_property
409 : 42 : (gobject_class,
410 : : PROP_DESCRIPTION,
411 : : g_param_spec_string("description",
412 : : "Transaction Description",
413 : : "The transaction description is an arbitrary string "
414 : : "assigned by the user. It is usually the customer, "
415 : : "vendor or other organization associated with the "
416 : : "transaction.",
417 : : nullptr,
418 : : G_PARAM_READWRITE));
419 : :
420 : : g_object_class_install_property
421 : 42 : (gobject_class,
422 : : PROP_CURRENCY,
423 : : g_param_spec_object ("currency",
424 : : "Currency",
425 : : "The base currency for this transaction.",
426 : : GNC_TYPE_COMMODITY,
427 : : G_PARAM_READWRITE));
428 : :
429 : : g_object_class_install_property
430 : 42 : (gobject_class,
431 : : PROP_POST_DATE,
432 : : g_param_spec_boxed("post-date",
433 : : "Post Date",
434 : : "The date the transaction occurred.",
435 : : GNC_TYPE_TIME64,
436 : : G_PARAM_READWRITE));
437 : :
438 : : g_object_class_install_property
439 : 42 : (gobject_class,
440 : : PROP_ENTER_DATE,
441 : : g_param_spec_boxed("enter-date",
442 : : "Enter Date",
443 : : "The date the transaction was entered.",
444 : : GNC_TYPE_TIME64,
445 : : G_PARAM_READWRITE));
446 : :
447 : 42 : g_object_class_install_property(
448 : : gobject_class,
449 : : PROP_INVOICE,
450 : : g_param_spec_boxed("invoice",
451 : : "Invoice attached to lot",
452 : : "Used by GncInvoice",
453 : : GNC_TYPE_GUID,
454 : : G_PARAM_READWRITE));
455 : :
456 : 42 : g_object_class_install_property(
457 : : gobject_class,
458 : : PROP_SX_TXN,
459 : : g_param_spec_boxed("from-sched-xaction",
460 : : "From Scheduled Transaction",
461 : : "Used by Scheduled Transastions to record the "
462 : : "originating template transaction for created "
463 : : "transactions",
464 : : GNC_TYPE_GUID,
465 : : G_PARAM_READWRITE));
466 : 42 : }
467 : :
468 : : /********************************************************************\
469 : : * xaccInitTransaction
470 : : * Initialize a transaction structure
471 : : \********************************************************************/
472 : :
473 : : static void
474 : 3500 : xaccInitTransaction (Transaction * trans, QofBook *book)
475 : : {
476 : 3500 : ENTER ("trans=%p", trans);
477 : 3500 : qof_instance_init_data (&trans->inst, GNC_ID_TRANS, book);
478 : 3500 : LEAVE (" ");
479 : 3500 : }
480 : :
481 : : /********************************************************************\
482 : : \********************************************************************/
483 : :
484 : : Transaction *
485 : 3501 : xaccMallocTransaction (QofBook *book)
486 : : {
487 : : Transaction *trans;
488 : :
489 : 3501 : g_return_val_if_fail (book, nullptr);
490 : :
491 : 3500 : trans = GNC_TRANSACTION(g_object_new(GNC_TYPE_TRANSACTION, nullptr));
492 : 3500 : xaccInitTransaction (trans, book);
493 : 3500 : qof_event_gen (&trans->inst, QOF_EVENT_CREATE, nullptr);
494 : :
495 : 3500 : return trans;
496 : : }
497 : :
498 : : #ifdef DUMP_FUNCTIONS
499 : : /* Please don't delete this function. Although it is not called by
500 : : any other code in GnuCash, it is useful when debugging. For example
501 : : it can be called using the gdb "call" command when stopped at a
502 : : breakpoint. */
503 : : void
504 : : xaccTransDump (const Transaction *trans, const char *tag)
505 : : {
506 : : GList *node;
507 : : char datebuff[MAX_DATE_LENGTH + 1];
508 : :
509 : : printf("%s Trans %p", tag, trans);
510 : : memset(datebuff, 0, sizeof(datebuff));
511 : : qof_print_date_buff(datebuff, MAX_DATE_LENGTH, trans->date_entered);
512 : : printf(" Entered: %s\n", datebuff);
513 : : memset(datebuff, 0, sizeof(datebuff));
514 : : qof_print_date_buff(datebuff, MAX_DATE_LENGTH, trans->date_posted);
515 : : printf(" Posted: %s\n", datebuff);
516 : : printf(" Num: %s\n", trans->num ? trans->num : "(null)");
517 : : printf(" Description: %s\n",
518 : : trans->description ? trans->description : "(null)");
519 : : printf(" Currency: %s\n",
520 : : gnc_commodity_get_printname(trans->common_currency));
521 : : printf(" version: %x\n", qof_instance_get_version(trans));
522 : : printf(" version_chk: %x\n", qof_instance_get_version_check(trans));
523 : : printf(" editlevel: %x\n", qof_instance_get_editlevel(trans));
524 : : printf(" orig: %p\n", trans->orig);
525 : : printf(" idata: %x\n", qof_instance_get_idata(trans));
526 : : printf(" splits: ");
527 : : for (node = trans->splits; node; node = node->next)
528 : : {
529 : : printf("%p ", node->data);
530 : : }
531 : : printf("\n");
532 : : for (node = trans->splits; node; node = node->next)
533 : : {
534 : : xaccSplitDump(GNC_SPLIT(node->data), tag);
535 : : }
536 : : printf("\n");
537 : : }
538 : : #endif
539 : :
540 : : void
541 : 4633 : xaccTransSortSplits (Transaction *trans)
542 : : {
543 : 4633 : GList *node, *new_list = nullptr;
544 : : Split *split;
545 : :
546 : : /* first debits */
547 : 14130 : for (node = trans->splits; node; node = node->next)
548 : : {
549 : 9497 : split = GNC_SPLIT(node->data);
550 : 9497 : if (gnc_numeric_negative_p (xaccSplitGetValue(split)))
551 : 4406 : continue;
552 : 5091 : new_list = g_list_prepend (new_list, split);
553 : : }
554 : :
555 : : /* then credits */
556 : 14130 : for (node = trans->splits; node; node = node->next)
557 : : {
558 : 9497 : split = GNC_SPLIT(node->data);
559 : 9497 : if (!gnc_numeric_negative_p (xaccSplitGetValue(split)))
560 : 5091 : continue;
561 : 4406 : new_list = g_list_prepend (new_list, split);
562 : : }
563 : :
564 : : /* install newly sorted list */
565 : 4633 : g_list_free(trans->splits);
566 : 4633 : trans->splits = g_list_reverse (new_list);
567 : 4633 : }
568 : :
569 : :
570 : : /********************************************************************\
571 : : \********************************************************************/
572 : : /* This routine is not exposed externally, since it does weird things,
573 : : * like not really owning the splits correctly, and other weirdnesses.
574 : : * This routine is prone to programmer snafu if not used correctly.
575 : : * It is used only by the edit-rollback code.
576 : : */
577 : : static Transaction *
578 : 4877 : dupe_trans (const Transaction *from)
579 : : {
580 : : Transaction *to;
581 : 4877 : to = GNC_TRANSACTION(g_object_new (GNC_TYPE_TRANSACTION, nullptr));
582 : :
583 : 4877 : CACHE_REPLACE (to->num, from->num);
584 : 4877 : CACHE_REPLACE (to->description, from->description);
585 : :
586 : 4877 : to->splits = g_list_copy_deep (from->splits, (GCopyFunc)xaccDupeSplit, nullptr);
587 : 4877 : to->date_entered = from->date_entered;
588 : 4877 : to->date_posted = from->date_posted;
589 : 4877 : qof_instance_copy_version(to, from);
590 : 4877 : to->orig = nullptr;
591 : :
592 : 4877 : to->common_currency = from->common_currency;
593 : :
594 : : /* Trash the guid and entity table. We don't want to mistake
595 : : * the cloned transaction as something official. If we ever
596 : : * use this transaction, we'll have to fix this up.
597 : : */
598 : 4877 : to->inst.e_type = nullptr;
599 : 4877 : qof_instance_set_guid(to, guid_null());
600 : 4877 : qof_instance_copy_book(to, from);
601 : 4877 : qof_instance_copy_kvp (QOF_INSTANCE(to), QOF_INSTANCE(from));
602 : :
603 : 4877 : return to;
604 : : }
605 : :
606 : : /********************************************************************\
607 : : * Use this routine to externally duplicate a transaction. It creates
608 : : * a full fledged transaction with unique guid, splits, etc. and
609 : : * writes it to the database.
610 : : \********************************************************************/
611 : : static gpointer
612 : 21 : copy_split (gconstpointer from_split, gpointer to)
613 : : {
614 : 21 : auto split = xaccSplitCloneNoKvp(GNC_SPLIT(from_split));
615 : 21 : split->parent = GNC_TRANSACTION(to);
616 : 21 : return split;
617 : : }
618 : :
619 : : Transaction *
620 : 11 : xaccTransCloneNoKvp (const Transaction *from)
621 : : {
622 : : Transaction *to;
623 : :
624 : 11 : qof_event_suspend();
625 : 11 : to = GNC_TRANSACTION(g_object_new (GNC_TYPE_TRANSACTION, nullptr));
626 : :
627 : 11 : to->date_entered = from->date_entered;
628 : 11 : to->date_posted = from->date_posted;
629 : 11 : CACHE_REPLACE (to->num, from->num);
630 : 11 : CACHE_REPLACE (to->description, from->description);
631 : 11 : to->common_currency = from->common_currency;
632 : 11 : qof_instance_copy_version(to, from);
633 : 11 : qof_instance_copy_version_check(to, from);
634 : :
635 : 11 : to->orig = nullptr;
636 : :
637 : 11 : qof_instance_init_data (&to->inst, GNC_ID_TRANS,
638 : : qof_instance_get_book(from));
639 : :
640 : 11 : xaccTransBeginEdit(to);
641 : 11 : to->splits = g_list_copy_deep (from->splits, copy_split, to);
642 : 11 : qof_instance_set_dirty(QOF_INSTANCE(to));
643 : 11 : xaccTransCommitEdit(to);
644 : 11 : qof_event_resume();
645 : :
646 : 11 : return to;
647 : : }
648 : :
649 : : Transaction *
650 : 7 : xaccTransClone (const Transaction *from)
651 : : {
652 : 7 : Transaction *to = xaccTransCloneNoKvp (from);
653 : :
654 : 7 : if (g_list_length (to->splits) != g_list_length (from->splits))
655 : : {
656 : 0 : PERR ("Cloned transaction has different number of splits from original");
657 : 0 : xaccTransDestroy (to);
658 : 0 : return nullptr;
659 : : }
660 : :
661 : 7 : xaccTransBeginEdit (to);
662 : 7 : qof_instance_copy_kvp (QOF_INSTANCE (to), QOF_INSTANCE (from));
663 : :
664 : 20 : for (GList* lfrom = from->splits, *lto = to->splits; lfrom && lto;
665 : 13 : lfrom = g_list_next (lfrom), lto = g_list_next (lto))
666 : 13 : xaccSplitCopyKvp (GNC_SPLIT(lfrom->data), GNC_SPLIT(lto->data));
667 : :
668 : 7 : xaccTransCommitEdit (to);
669 : 7 : return to;
670 : : }
671 : :
672 : : /*################## Added for Reg2 #################*/
673 : :
674 : : /********************************************************************\
675 : : * Copy a transaction to the 'clipboard' transaction using
676 : : * dupe_trans. The 'clipboard' transaction must never
677 : : * be dereferenced.
678 : : \********************************************************************/
679 : 0 : Transaction * xaccTransCopyToClipBoard(const Transaction *from_trans)
680 : : {
681 : : Transaction *to_trans;
682 : :
683 : 0 : if (!from_trans)
684 : 0 : return nullptr;
685 : :
686 : 0 : to_trans = dupe_trans(from_trans);
687 : 0 : return to_trans;
688 : : }
689 : :
690 : : /********************************************************************\
691 : : * Copy a transaction to another using the function below without
692 : : * changing any account information.
693 : : \********************************************************************/
694 : : void
695 : 0 : xaccTransCopyOnto(const Transaction *from_trans, Transaction *to_trans)
696 : : {
697 : 0 : xaccTransCopyFromClipBoard(from_trans, to_trans, nullptr, nullptr, TRUE);
698 : 0 : }
699 : :
700 : : /********************************************************************\
701 : : * This function explicitly must robustly handle some unusual input.
702 : : *
703 : : * 'from_trans' may be a duped trans (see dupe_trans), so its
704 : : * splits may not really belong to the accounts that they say they do.
705 : : *
706 : : * 'from_acc' need not be a valid account. It may be an already freed
707 : : * Account. Therefore, it must not be dereferenced at all.
708 : : *
709 : : * Neither 'from_trans', nor 'from_acc', nor any of 'from's splits may
710 : : * be modified in any way.
711 : : *
712 : : * 'no_date' if TRUE will not copy the date posted.
713 : : *
714 : : * The 'to_trans' transaction will end up with valid copies of from's
715 : : * splits. In addition, the copies of any of from's splits that were
716 : : * in from_acc (or at least claimed to be) will end up in to_acc.
717 : : \********************************************************************/
718 : : void
719 : 2 : xaccTransCopyFromClipBoard(const Transaction *from_trans, Transaction *to_trans,
720 : : const Account *from_acc, Account *to_acc, gboolean no_date)
721 : : {
722 : 2 : gboolean change_accounts = FALSE;
723 : : GList *node;
724 : :
725 : 2 : if (!from_trans || !to_trans)
726 : 0 : return;
727 : :
728 : 2 : change_accounts = from_acc && GNC_IS_ACCOUNT(to_acc) && from_acc != to_acc;
729 : 2 : xaccTransBeginEdit(to_trans);
730 : :
731 : 2 : xaccTransClearSplits(to_trans);
732 : 2 : xaccTransSetCurrency(to_trans, xaccTransGetCurrency(from_trans));
733 : 2 : xaccTransSetDescription(to_trans, xaccTransGetDescription(from_trans));
734 : :
735 : 2 : if ((xaccTransGetNum(to_trans) == nullptr) || (g_strcmp0 (xaccTransGetNum(to_trans), "") == 0))
736 : 2 : xaccTransSetNum(to_trans, xaccTransGetNum(from_trans));
737 : :
738 : 2 : xaccTransSetNotes(to_trans, xaccTransGetNotes(from_trans));
739 : 2 : xaccTransSetDocLink(to_trans, xaccTransGetDocLink (from_trans));
740 : 2 : if(!no_date)
741 : : {
742 : 1 : xaccTransSetDatePostedSecs(to_trans, xaccTransRetDatePosted (from_trans));
743 : : }
744 : :
745 : : /* Each new split will be parented to 'to' */
746 : 6 : for (node = from_trans->splits; node; node = node->next)
747 : : {
748 : 4 : Split *new_split = xaccMallocSplit( qof_instance_get_book(QOF_INSTANCE(from_trans)));
749 : 4 : xaccSplitCopyOnto(GNC_SPLIT(node->data), new_split);
750 : 4 : if (change_accounts && xaccSplitGetAccount(GNC_SPLIT(node->data)) == from_acc)
751 : 2 : xaccSplitSetAccount(new_split, to_acc);
752 : 4 : xaccSplitSetParent(new_split, to_trans);
753 : : }
754 : 2 : xaccTransCommitEdit(to_trans);
755 : : }
756 : :
757 : : /*################## Added for Reg2 #################*/
758 : :
759 : : /********************************************************************\
760 : : Free the transaction.
761 : : \********************************************************************/
762 : : static void
763 : 7171 : xaccFreeTransaction (Transaction *trans)
764 : : {
765 : 7171 : if (!trans) return;
766 : :
767 : 7171 : ENTER ("(addr=%p)", trans);
768 : 7171 : if (((char *) 1) == trans->num)
769 : : {
770 : 0 : PERR ("double-free %p", trans);
771 : 0 : LEAVE (" ");
772 : 0 : return;
773 : : }
774 : :
775 : : /* free up the destination splits */
776 : 7171 : g_list_free_full (trans->splits, (GDestroyNotify)xaccFreeSplit);
777 : 7171 : trans->splits = nullptr;
778 : :
779 : : /* free up transaction strings */
780 : 7171 : CACHE_REMOVE(trans->num);
781 : 7171 : CACHE_REMOVE(trans->description);
782 : :
783 : : /* Just in case someone looks up freed memory ... */
784 : 7171 : trans->num = (char *) 1;
785 : 7171 : trans->description = nullptr;
786 : 7171 : trans->date_entered = 0;
787 : 7171 : trans->date_posted = 0;
788 : 7171 : if (trans->orig)
789 : : {
790 : 123 : xaccFreeTransaction (trans->orig);
791 : 123 : trans->orig = nullptr;
792 : : }
793 : :
794 : : /* qof_instance_release (&trans->inst); */
795 : 7171 : g_object_unref(trans);
796 : :
797 : 7171 : LEAVE ("(addr=%p)", trans);
798 : : }
799 : :
800 : : /********************************************************************
801 : : xaccTransEqual
802 : :
803 : : Compare two transactions for equality. We don't pay any attention to
804 : : rollback issues here, and we only care about equality of "permanent
805 : : fields", basically the things that would survive a file save/load
806 : : cycle.
807 : :
808 : : ********************************************************************/
809 : :
810 : : /* return 0 when splits have equal guids */
811 : : static gint
812 : 201 : compare_split_guids (gconstpointer a, gconstpointer b)
813 : : {
814 : 201 : const Split *sa = GNC_SPLIT(a);
815 : 201 : const Split *sb = GNC_SPLIT(b);
816 : :
817 : 201 : if (sa == sb) return 0;
818 : 201 : if (!sa || !sb) return 1;
819 : :
820 : 201 : return guid_compare (xaccSplitGetGUID (sa), xaccSplitGetGUID (sb));
821 : : }
822 : :
823 : : gboolean
824 : 268 : xaccTransEqual(const Transaction *ta, const Transaction *tb,
825 : : gboolean check_guids,
826 : : gboolean check_splits,
827 : : gboolean check_balances,
828 : : gboolean assume_ordered)
829 : : {
830 : : gboolean same_book;
831 : :
832 : 268 : if (!ta && !tb) return TRUE; /* Arguable. FALSE may be better. */
833 : :
834 : 267 : if (!ta || !tb)
835 : : {
836 : 4 : PINFO ("one is nullptr");
837 : 4 : return FALSE;
838 : : }
839 : :
840 : 263 : if (ta == tb) return TRUE;
841 : :
842 : 253 : same_book = qof_instance_get_book(QOF_INSTANCE(ta)) == qof_instance_get_book(QOF_INSTANCE(tb));
843 : :
844 : 253 : if (check_guids)
845 : : {
846 : 225 : if (qof_instance_guid_compare(ta, tb) != 0)
847 : : {
848 : 3 : PINFO ("GUIDs differ");
849 : 3 : return FALSE;
850 : : }
851 : : }
852 : :
853 : 250 : if (!gnc_commodity_equal(ta->common_currency, tb->common_currency))
854 : : {
855 : 1 : PINFO ("commodities differ %s vs %s",
856 : : gnc_commodity_get_unique_name (ta->common_currency),
857 : : gnc_commodity_get_unique_name (tb->common_currency));
858 : 1 : return FALSE;
859 : : }
860 : :
861 : 249 : if (ta->date_entered != tb->date_entered)
862 : : {
863 : : char buf1[100];
864 : : char buf2[100];
865 : :
866 : 1 : (void)gnc_time64_to_iso8601_buff(ta->date_entered, buf1);
867 : 1 : (void)gnc_time64_to_iso8601_buff(tb->date_entered, buf2);
868 : 1 : PINFO ("date entered differs: '%s' vs '%s'", buf1, buf2);
869 : 1 : return FALSE;
870 : : }
871 : :
872 : 248 : if (ta->date_posted != tb->date_posted)
873 : : {
874 : : char buf1[100];
875 : : char buf2[100];
876 : :
877 : 1 : (void)gnc_time64_to_iso8601_buff(ta->date_posted, buf1);
878 : 1 : (void)gnc_time64_to_iso8601_buff(tb->date_posted, buf2);
879 : 1 : PINFO ("date posted differs: '%s' vs '%s'", buf1, buf2);
880 : 1 : return FALSE;
881 : : }
882 : :
883 : : /* If the same book, since we use cached strings, we can just compare pointer
884 : : * equality for num and description
885 : : */
886 : 247 : if ((same_book && ta->num != tb->num) || (!same_book && g_strcmp0(ta->num, tb->num) != 0))
887 : : {
888 : 2 : PINFO ("num differs: %s vs %s", ta->num, tb->num);
889 : 2 : return FALSE;
890 : : }
891 : :
892 : 169 : if ((same_book && ta->description != tb->description)
893 : 414 : || (!same_book && g_strcmp0(ta->description, tb->description)))
894 : : {
895 : 2 : PINFO ("descriptions differ: %s vs %s", ta->description, tb->description);
896 : 2 : return FALSE;
897 : : }
898 : :
899 : 243 : if (qof_instance_compare_kvp (QOF_INSTANCE (ta), QOF_INSTANCE (tb)) != 0)
900 : : {
901 : : char *frame_a;
902 : : char *frame_b;
903 : :
904 : 1 : frame_a = qof_instance_kvp_as_string (QOF_INSTANCE (ta));
905 : 1 : frame_b = qof_instance_kvp_as_string (QOF_INSTANCE (tb));
906 : :
907 : :
908 : 1 : PINFO ("kvp frames differ:\n%s\n\nvs\n\n%s", frame_a, frame_b);
909 : :
910 : 1 : g_free (frame_a);
911 : 1 : g_free (frame_b);
912 : :
913 : 1 : return FALSE;
914 : : }
915 : :
916 : 242 : if (check_splits)
917 : : {
918 : 75 : if ((!ta->splits && tb->splits) || (!tb->splits && ta->splits))
919 : : {
920 : 0 : PINFO ("only one has splits");
921 : 0 : return FALSE;
922 : : }
923 : :
924 : 75 : if (ta->splits && tb->splits)
925 : : {
926 : : GList *node_a, *node_b;
927 : :
928 : 75 : for (node_a = ta->splits, node_b = tb->splits;
929 : 219 : node_a;
930 : 144 : node_a = node_a->next, node_b = node_b->next)
931 : : {
932 : 147 : Split *split_a = GNC_SPLIT(node_a->data);
933 : : Split *split_b;
934 : :
935 : : /* don't presume that the splits are in the same order */
936 : 147 : if (!assume_ordered)
937 : 134 : node_b = g_list_find_custom (tb->splits, split_a,
938 : : compare_split_guids);
939 : :
940 : 147 : if (!node_b)
941 : : {
942 : : gchar guidstr[GUID_ENCODING_LENGTH+1];
943 : 0 : guid_to_string_buff (xaccSplitGetGUID (split_a),guidstr);
944 : :
945 : 0 : PINFO ("first has split %s and second does not",guidstr);
946 : 0 : return FALSE;
947 : : }
948 : :
949 : 147 : split_b = GNC_SPLIT(node_b->data);
950 : :
951 : 147 : if (!xaccSplitEqual (split_a, split_b, check_guids, check_balances,
952 : : FALSE))
953 : : {
954 : : char str_a[GUID_ENCODING_LENGTH + 1];
955 : : char str_b[GUID_ENCODING_LENGTH + 1];
956 : :
957 : 3 : guid_to_string_buff (xaccSplitGetGUID (split_a), str_a);
958 : 3 : guid_to_string_buff (xaccSplitGetGUID (split_b), str_b);
959 : :
960 : 3 : PINFO ("splits %s and %s differ", str_a, str_b);
961 : 3 : return FALSE;
962 : : }
963 : : }
964 : :
965 : 72 : if (g_list_length (ta->splits) != g_list_length (tb->splits))
966 : : {
967 : 0 : PINFO ("different number of splits");
968 : 0 : return FALSE;
969 : : }
970 : : }
971 : : }
972 : :
973 : 239 : return TRUE;
974 : : }
975 : :
976 : : /********************************************************************\
977 : : xaccTransUseTradingAccounts
978 : :
979 : : Returns true if the transaction should include trading account splits if
980 : : it involves more than one commodity.
981 : : \********************************************************************/
982 : :
983 : 6147 : gboolean xaccTransUseTradingAccounts(const Transaction *trans)
984 : : {
985 : 6147 : return qof_book_use_trading_accounts(qof_instance_get_book (trans));
986 : : }
987 : :
988 : : /********************************************************************\
989 : : \********************************************************************/
990 : :
991 : : Transaction *
992 : 72 : xaccTransLookup (const GncGUID *guid, QofBook *book)
993 : : {
994 : : QofCollection *col;
995 : 72 : if (!guid || !book) return nullptr;
996 : 72 : col = qof_book_get_collection (book, GNC_ID_TRANS);
997 : 72 : return (Transaction *) qof_collection_lookup_entity (col, guid);
998 : : }
999 : :
1000 : : /********************************************************************\
1001 : : \********************************************************************/
1002 : :
1003 : : gnc_numeric
1004 : 3110 : xaccTransGetImbalanceValue (const Transaction * trans)
1005 : : {
1006 : 3110 : gnc_numeric imbal = gnc_numeric_zero();
1007 : 3110 : if (!trans) return imbal;
1008 : :
1009 : 3110 : ENTER("(trans=%p)", trans);
1010 : : /* Could use xaccSplitsComputeValue, except that we want to use
1011 : : GNC_HOW_DENOM_EXACT */
1012 : 9420 : FOR_EACH_SPLIT(trans, imbal =
1013 : : gnc_numeric_add(imbal, xaccSplitGetValue(s),
1014 : : GNC_DENOM_AUTO, GNC_HOW_DENOM_EXACT));
1015 : 3110 : LEAVE("(trans=%p) imbal=%s", trans, gnc_num_dbg_to_string(imbal));
1016 : 3110 : return imbal;
1017 : : }
1018 : :
1019 : : MonetaryList *
1020 : 13 : xaccTransGetImbalance (const Transaction * trans)
1021 : : {
1022 : : /* imbal_value is used if either (1) the transaction has a non currency
1023 : : split or (2) all the splits are in the same currency. If there are
1024 : : no non-currency splits and not all splits are in the same currency then
1025 : : imbal_list is used to compute the imbalance. */
1026 : 13 : MonetaryList *imbal_list = nullptr;
1027 : 13 : gnc_numeric imbal_value = gnc_numeric_zero();
1028 : : gboolean trading_accts;
1029 : :
1030 : 13 : if (!trans) return imbal_list;
1031 : :
1032 : 12 : ENTER("(trans=%p)", trans);
1033 : :
1034 : 12 : trading_accts = xaccTransUseTradingAccounts (trans);
1035 : :
1036 : : /* If using trading accounts and there is at least one split that is not
1037 : : in the transaction currency or a split that has a price or exchange
1038 : : rate other than 1, then compute the balance in each commodity in the
1039 : : transaction. Otherwise (all splits are in the transaction's currency)
1040 : : then compute the balance using the value fields.
1041 : :
1042 : : Optimize for the common case of only one currency and a balanced
1043 : : transaction. */
1044 : 50 : FOR_EACH_SPLIT(trans,
1045 : : {
1046 : : gnc_commodity *commodity;
1047 : : commodity = xaccAccountGetCommodity(xaccSplitGetAccount(s));
1048 : : if (trading_accts &&
1049 : : (imbal_list ||
1050 : : ! gnc_commodity_equiv(commodity, trans->common_currency) ||
1051 : : ! gnc_numeric_equal(xaccSplitGetAmount(s), xaccSplitGetValue(s))))
1052 : : {
1053 : : /* Need to use (or already are using) a list of imbalances in each of
1054 : : the currencies used in the transaction. */
1055 : : if (! imbal_list)
1056 : : {
1057 : : /* All previous splits have been in the transaction's common
1058 : : currency, so imbal_value is in this currency. */
1059 : : imbal_list = gnc_monetary_list_add_value(imbal_list,
1060 : : trans->common_currency,
1061 : : imbal_value);
1062 : : }
1063 : : imbal_list = gnc_monetary_list_add_value(imbal_list, commodity,
1064 : : xaccSplitGetAmount(s));
1065 : : }
1066 : :
1067 : : /* Add it to the value accumulator in case we need it. */
1068 : : imbal_value = gnc_numeric_add(imbal_value, xaccSplitGetValue(s),
1069 : : GNC_DENOM_AUTO, GNC_HOW_DENOM_EXACT);
1070 : : } );
1071 : :
1072 : :
1073 : 12 : if (!imbal_list && !gnc_numeric_zero_p(imbal_value))
1074 : : {
1075 : : /* Not balanced and no list, create one. If we found multiple currencies
1076 : : and no non-currency commodity then imbal_list will already exist and
1077 : : we won't get here. */
1078 : 1 : imbal_list = gnc_monetary_list_add_value(imbal_list,
1079 : 1 : trans->common_currency,
1080 : : imbal_value);
1081 : : }
1082 : :
1083 : : /* Delete all the zero entries from the list, perhaps leaving an
1084 : : empty list */
1085 : 12 : imbal_list = gnc_monetary_list_delete_zeros(imbal_list);
1086 : :
1087 : 12 : LEAVE("(trans=%p), imbal=%p", trans, imbal_list);
1088 : 12 : return imbal_list;
1089 : : }
1090 : :
1091 : : gboolean
1092 : 3070 : xaccTransIsBalanced (const Transaction *trans)
1093 : : {
1094 : : MonetaryList *imbal_list;
1095 : : gboolean result;
1096 : 3070 : gnc_numeric imbal = gnc_numeric_zero();
1097 : 3070 : gnc_numeric imbal_trading = gnc_numeric_zero();
1098 : :
1099 : 3070 : if (trans == nullptr) return FALSE;
1100 : :
1101 : 3069 : if (xaccTransUseTradingAccounts(trans))
1102 : : {
1103 : : /* Transaction is imbalanced if the value is imbalanced in either
1104 : : trading or non-trading splits. One can't be used to balance
1105 : : the other. */
1106 : 30 : FOR_EACH_SPLIT(trans,
1107 : : {
1108 : : Account *acc = xaccSplitGetAccount(s);
1109 : : if (!acc || xaccAccountGetType(acc) != ACCT_TYPE_TRADING)
1110 : : {
1111 : : imbal = gnc_numeric_add(imbal, xaccSplitGetValue(s),
1112 : : GNC_DENOM_AUTO, GNC_HOW_DENOM_EXACT);
1113 : : }
1114 : : else
1115 : : {
1116 : : imbal_trading = gnc_numeric_add(imbal_trading, xaccSplitGetValue(s),
1117 : : GNC_DENOM_AUTO, GNC_HOW_DENOM_EXACT);
1118 : : }
1119 : : }
1120 : : );
1121 : : }
1122 : : else
1123 : 3062 : imbal = xaccTransGetImbalanceValue(trans);
1124 : :
1125 : 3069 : if (! gnc_numeric_zero_p(imbal) || ! gnc_numeric_zero_p(imbal_trading))
1126 : 51 : return FALSE;
1127 : :
1128 : 3018 : if (!xaccTransUseTradingAccounts (trans))
1129 : 3013 : return TRUE;
1130 : :
1131 : 5 : imbal_list = xaccTransGetImbalance(trans);
1132 : 5 : result = imbal_list == nullptr;
1133 : 5 : gnc_monetary_list_free(imbal_list);
1134 : 5 : return result;
1135 : : }
1136 : :
1137 : : gnc_numeric
1138 : 4 : xaccTransGetAccountValue (const Transaction *trans,
1139 : : const Account *acc)
1140 : : {
1141 : 4 : gnc_numeric total = gnc_numeric_zero ();
1142 : 4 : if (!trans || !acc) return total;
1143 : :
1144 : 6 : FOR_EACH_SPLIT(trans, if (acc == xaccSplitGetAccount(s))
1145 : : {
1146 : : total = gnc_numeric_add (total, xaccSplitGetValue (s),
1147 : : GNC_DENOM_AUTO,
1148 : : GNC_HOW_DENOM_EXACT);
1149 : : });
1150 : 2 : return total;
1151 : : }
1152 : :
1153 : : gnc_numeric
1154 : 16 : xaccTransGetAccountAmount (const Transaction *trans, const Account *acc)
1155 : : {
1156 : 16 : gnc_numeric total = gnc_numeric_zero ();
1157 : 16 : if (!trans || !acc) return total;
1158 : :
1159 : 14 : total = gnc_numeric_convert (total, xaccAccountGetCommoditySCU (acc),
1160 : : GNC_HOW_RND_ROUND_HALF_UP);
1161 : 42 : FOR_EACH_SPLIT(trans, if (acc == xaccSplitGetAccount(s))
1162 : : total = gnc_numeric_add_fixed(
1163 : : total, xaccSplitGetAmount(s)));
1164 : 14 : return total;
1165 : : }
1166 : :
1167 : : gnc_numeric
1168 : 4 : xaccTransGetAccountConvRate(const Transaction *txn, const Account *acc)
1169 : : {
1170 : : gnc_numeric amount, value, convrate;
1171 : : GList *splits;
1172 : : Split *s;
1173 : 4 : gboolean found_acc_match = FALSE;
1174 : 4 : gnc_commodity *acc_commod = xaccAccountGetCommodity(acc);
1175 : :
1176 : : /* We need to compute the conversion rate into _this account_. So,
1177 : : * find the first split into this account, compute the conversion
1178 : : * rate (based on amount/value), and then return this conversion
1179 : : * rate.
1180 : : */
1181 : 4 : if (gnc_commodity_equal(acc_commod, xaccTransGetCurrency(txn)))
1182 : 1 : return gnc_numeric_create(1, 1);
1183 : :
1184 : 4 : for (splits = txn->splits; splits; splits = splits->next)
1185 : : {
1186 : : Account *split_acc;
1187 : : gnc_commodity *split_commod;
1188 : :
1189 : 4 : s = GNC_SPLIT(splits->data);
1190 : :
1191 : 4 : if (!xaccTransStillHasSplit(txn, s))
1192 : 0 : continue;
1193 : 4 : split_acc = xaccSplitGetAccount (s);
1194 : 4 : split_commod = xaccAccountGetCommodity (split_acc);
1195 : 5 : if (! (split_acc == acc ||
1196 : 1 : gnc_commodity_equal (split_commod, acc_commod)))
1197 : 1 : continue;
1198 : :
1199 : 3 : found_acc_match = TRUE;
1200 : 3 : amount = xaccSplitGetAmount (s);
1201 : :
1202 : : /* Ignore splits with "zero" amount */
1203 : 3 : if (gnc_numeric_zero_p (amount))
1204 : 0 : continue;
1205 : :
1206 : 3 : value = xaccSplitGetValue (s);
1207 : 3 : if (gnc_numeric_zero_p (value))
1208 : 1 : PWARN("How can amount be nonzero and value be zero?");
1209 : :
1210 : 3 : convrate = gnc_numeric_div(amount, value, GNC_DENOM_AUTO, GNC_HOW_DENOM_REDUCE);
1211 : 3 : return convrate;
1212 : : }
1213 : :
1214 : 0 : if (acc)
1215 : : {
1216 : : /* If we did find a matching account but its amount was zero,
1217 : : * then perhaps this is a "special" income/loss transaction
1218 : : */
1219 : 0 : if (found_acc_match)
1220 : 0 : return gnc_numeric_zero();
1221 : : else
1222 : 0 : PERR("Cannot convert transaction -- no splits with proper conversion ratio");
1223 : : }
1224 : 0 : return gnc_numeric_create (100, 100);
1225 : : }
1226 : :
1227 : : gnc_numeric
1228 : 4 : xaccTransGetAccountBalance (const Transaction *trans,
1229 : : const Account *account)
1230 : : {
1231 : : GList *node;
1232 : 4 : Split *last_split = nullptr;
1233 : :
1234 : : // Not really the appropriate error value.
1235 : 4 : g_return_val_if_fail(account && trans, gnc_numeric_error(GNC_ERROR_ARG));
1236 : :
1237 : 6 : for (node = trans->splits; node; node = node->next)
1238 : : {
1239 : 4 : Split *split = GNC_SPLIT(node->data);
1240 : :
1241 : 4 : if (!xaccTransStillHasSplit(trans, split))
1242 : 0 : continue;
1243 : 4 : if (xaccSplitGetAccount(split) != account)
1244 : 2 : continue;
1245 : :
1246 : 2 : if (!last_split)
1247 : : {
1248 : 2 : last_split = split;
1249 : 2 : continue;
1250 : : }
1251 : :
1252 : : /* This test needs to correspond to the comparison function used when
1253 : : sorting the splits for computing the running balance. */
1254 : 0 : if (xaccSplitOrder (last_split, split) < 0)
1255 : 0 : last_split = split;
1256 : : }
1257 : :
1258 : 2 : return xaccSplitGetBalance (last_split);
1259 : : }
1260 : :
1261 : : /********************************************************************\
1262 : : \********************************************************************/
1263 : : /* The new routine for setting the common currency */
1264 : :
1265 : : gnc_commodity *
1266 : 61641 : xaccTransGetCurrency (const Transaction *trans)
1267 : : {
1268 : 61641 : return trans ? trans->common_currency : nullptr;
1269 : : }
1270 : :
1271 : : /* Helper functions for xaccTransSetCurrency */
1272 : : static gnc_numeric
1273 : 201 : find_new_rate(Transaction *trans, gnc_commodity *curr)
1274 : : {
1275 : : GList *node;
1276 : 201 : gnc_numeric rate = gnc_numeric_zero();
1277 : 524 : for (node = trans->splits; node != nullptr; node = g_list_next (node))
1278 : : {
1279 : 389 : Split *split = GNC_SPLIT(node->data);
1280 : : gnc_commodity *split_com =
1281 : 389 : xaccAccountGetCommodity(xaccSplitGetAccount(split));
1282 : 389 : if (gnc_commodity_equal(curr, split_com))
1283 : : {
1284 : : /* This looks backwards, but the amount of the balancing transaction
1285 : : * that we're going to use it on is in the value's currency. */
1286 : 66 : rate = gnc_numeric_div(xaccSplitGetAmount(split),
1287 : : xaccSplitGetValue(split),
1288 : : GNC_DENOM_AUTO, GNC_HOW_RND_NEVER);
1289 : 66 : break;
1290 : : }
1291 : : }
1292 : 201 : return rate;
1293 : : }
1294 : :
1295 : : static void
1296 : 130 : split_set_new_value(Split* split, gnc_commodity *curr, gnc_commodity *old_curr,
1297 : : gnc_numeric rate)
1298 : : {
1299 : : gnc_commodity *split_com =
1300 : 130 : xaccAccountGetCommodity(xaccSplitGetAccount(split));
1301 : 130 : if (gnc_commodity_equal(curr, split_com))
1302 : 67 : xaccSplitSetValue(split, xaccSplitGetAmount(split));
1303 : 63 : else if (gnc_commodity_equal(old_curr, split_com))
1304 : 63 : xaccSplitSetSharePrice(split, rate);
1305 : : else
1306 : : {
1307 : 0 : gnc_numeric old_rate = gnc_numeric_div(xaccSplitGetValue(split),
1308 : : xaccSplitGetAmount(split),
1309 : : GNC_DENOM_AUTO,
1310 : : GNC_HOW_RND_NEVER);
1311 : 0 : gnc_numeric new_rate = gnc_numeric_div(old_rate, rate, GNC_DENOM_AUTO,
1312 : : GNC_HOW_RND_NEVER);
1313 : 0 : xaccSplitSetSharePrice(split, new_rate);
1314 : : }
1315 : 130 : }
1316 : :
1317 : : /**
1318 : : * Set a new currency on a transaction.
1319 : : * When we do that to a transaction with splits we need to re-value
1320 : : * all of the splits in the new currency.
1321 : : * @param trans: The transaction to change
1322 : : * @param curr: The new currency to set.
1323 : : */
1324 : : void
1325 : 3642 : xaccTransSetCurrency (Transaction *trans, gnc_commodity *curr)
1326 : : {
1327 : 3642 : if (!trans || !curr || trans->common_currency == curr) return;
1328 : :
1329 : 3444 : gnc_commodity *old_curr = trans->common_currency;
1330 : 3444 : xaccTransBeginEdit(trans);
1331 : :
1332 : 3444 : trans->common_currency = curr;
1333 : 3444 : if (old_curr != nullptr && trans->splits != nullptr)
1334 : : {
1335 : 201 : gnc_numeric rate = find_new_rate(trans, curr);
1336 : 201 : if (!gnc_numeric_zero_p (rate))
1337 : : {
1338 : 193 : FOR_EACH_SPLIT(trans, split_set_new_value(s, curr, old_curr, rate));
1339 : : }
1340 : : else
1341 : : {
1342 : 417 : FOR_EACH_SPLIT(trans, xaccSplitSetValue(s, xaccSplitGetValue(s)));
1343 : : }
1344 : : }
1345 : :
1346 : 3444 : qof_instance_set_dirty(QOF_INSTANCE(trans));
1347 : 3444 : mark_trans(trans); /* Dirty balance of every account in trans */
1348 : 3444 : xaccTransCommitEdit(trans);
1349 : : }
1350 : :
1351 : : /********************************************************************\
1352 : : \********************************************************************/
1353 : :
1354 : : void
1355 : 83544 : xaccTransBeginEdit (Transaction *trans)
1356 : : {
1357 : 83544 : if (!trans) return;
1358 : 69043 : if (!qof_begin_edit(&trans->inst)) return;
1359 : :
1360 : 7157 : if (qof_book_shutting_down(qof_instance_get_book(trans))) return;
1361 : :
1362 : 4875 : if (!qof_book_is_readonly(qof_instance_get_book(trans)))
1363 : : {
1364 : 4872 : xaccOpenLog ();
1365 : 4872 : xaccTransWriteLog (trans, 'B');
1366 : : }
1367 : :
1368 : : /* Make a clone of the transaction; we will use this
1369 : : * in case we need to roll-back the edit. */
1370 : 4875 : trans->orig = dupe_trans (trans);
1371 : : }
1372 : :
1373 : : /********************************************************************\
1374 : : \********************************************************************/
1375 : :
1376 : : void
1377 : 2418 : xaccTransDestroy (Transaction *trans)
1378 : : {
1379 : 2418 : if (!trans) return;
1380 : :
1381 : 2519 : if (!xaccTransGetReadOnly (trans) ||
1382 : 101 : qof_book_shutting_down(qof_instance_get_book(trans)))
1383 : : {
1384 : 2404 : xaccTransBeginEdit(trans);
1385 : 2404 : qof_instance_set_destroying(trans, TRUE);
1386 : 2404 : xaccTransCommitEdit(trans);
1387 : : }
1388 : : }
1389 : :
1390 : : static void
1391 : 123 : destroy_gains (Transaction *trans)
1392 : : {
1393 : : SplitList *node;
1394 : 359 : for (node = trans->splits; node; node = node->next)
1395 : : {
1396 : 236 : Split *s = GNC_SPLIT(node->data);
1397 : 236 : if (!xaccTransStillHasSplit(trans, s))
1398 : 1 : continue;
1399 : :
1400 : 235 : if (GAINS_STATUS_UNKNOWN == s->gains) xaccSplitDetermineGainStatus(s);
1401 : 235 : if (s->gains_split && (GAINS_STATUS_GAINS & s->gains_split->gains))
1402 : : {
1403 : 2 : Transaction *t = s->gains_split->parent;
1404 : 2 : xaccTransDestroy (t);
1405 : 2 : s->gains_split = nullptr;
1406 : : }
1407 : : }
1408 : 123 : }
1409 : :
1410 : : static void
1411 : 2404 : do_destroy (QofInstance* inst)
1412 : : {
1413 : 2404 : Transaction *trans{GNC_TRANSACTION (inst)};
1414 : 2404 : gboolean shutting_down = qof_book_shutting_down(qof_instance_get_book(trans));
1415 : :
1416 : : /* If there are capital-gains transactions associated with this,
1417 : : * they need to be destroyed too unless we're shutting down in
1418 : : * which case all transactions will be destroyed. */
1419 : 2404 : if (!shutting_down)
1420 : 122 : destroy_gains (trans);
1421 : :
1422 : : /* Make a log in the journal before destruction. */
1423 : 2404 : if (!shutting_down && !qof_book_is_readonly(qof_instance_get_book(trans)))
1424 : 121 : xaccTransWriteLog (trans, 'D');
1425 : :
1426 : 2404 : qof_event_gen (&trans->inst, QOF_EVENT_DESTROY, nullptr);
1427 : : /* xaccFreeTransaction will also clean up the splits but without
1428 : : * emitting GNC_EVENT_ITEM_REMOVED.
1429 : : */
1430 : 2404 : xaccTransClearSplits(trans);
1431 : 2404 : xaccFreeTransaction (trans);
1432 : 2404 : }
1433 : :
1434 : : /********************************************************************\
1435 : : \********************************************************************/
1436 : :
1437 : : /* Temporary hack for data consistency */
1438 : : static int scrub_data = 1;
1439 : 30 : void xaccEnableDataScrubbing(void)
1440 : : {
1441 : 30 : scrub_data = 1;
1442 : 30 : }
1443 : 30 : void xaccDisableDataScrubbing(void)
1444 : : {
1445 : 30 : scrub_data = 0;
1446 : 30 : }
1447 : :
1448 : : /* Check for an implicitly deleted transaction */
1449 : 7031 : static gboolean was_trans_emptied(Transaction *trans)
1450 : : {
1451 : 7036 : FOR_EACH_SPLIT(trans, return FALSE);
1452 : 5 : return TRUE;
1453 : : }
1454 : :
1455 : 1 : static void trans_on_error(QofInstance *inst, QofBackendError errcode)
1456 : : {
1457 : 1 : Transaction *trans{GNC_TRANSACTION(inst)};
1458 : :
1459 : : /* If the backend puked, then we must roll-back
1460 : : * at this point, and let the user know that we failed.
1461 : : * The GUI should check for error conditions ...
1462 : : */
1463 : 1 : if (ERR_BACKEND_MODIFIED == errcode)
1464 : : {
1465 : 1 : PWARN("Another user has modified this transaction\n"
1466 : : "\tjust a moment ago. Please look at their changes,\n"
1467 : : "\tand try again, if needed.\n");
1468 : : }
1469 : :
1470 : 1 : xaccTransRollbackEdit(trans);
1471 : 1 : gnc_engine_signal_commit_error( errcode );
1472 : 1 : }
1473 : :
1474 : 4629 : static void trans_cleanup_commit(QofInstance *inst)
1475 : : {
1476 : 4629 : Transaction *trans{GNC_TRANSACTION(inst)};
1477 : : GList *slist, *node;
1478 : :
1479 : : /* ------------------------------------------------- */
1480 : : /* Make sure all associated splits are in proper order
1481 : : * in their accounts with the correct balances. */
1482 : :
1483 : : /* Iterate over existing splits */
1484 : 4629 : slist = g_list_copy(trans->splits);
1485 : 14134 : for (node = slist; node; node = node->next)
1486 : : {
1487 : 9505 : Split *s = GNC_SPLIT(node->data);
1488 : 9505 : if (!qof_instance_is_dirty(QOF_INSTANCE(s)))
1489 : 1315 : continue;
1490 : :
1491 : 8190 : if ((s->parent != trans) || qof_instance_get_destroying(s))
1492 : : {
1493 : : /* Existing split either moved to another transaction or
1494 : : was destroyed, drop from list */
1495 : : GncEventData ed;
1496 : 19 : ed.node = trans;
1497 : 19 : ed.idx = g_list_index(trans->splits, s);
1498 : 19 : trans->splits = g_list_remove(trans->splits, s);
1499 : 19 : qof_event_gen(&s->inst, QOF_EVENT_REMOVE, &ed);
1500 : : }
1501 : :
1502 : 8190 : if (s->parent == trans)
1503 : : {
1504 : : /* Split was either added, destroyed or just changed */
1505 : 8181 : if (qof_instance_get_destroying(s))
1506 : 10 : qof_event_gen(&s->inst, QOF_EVENT_DESTROY, nullptr);
1507 : 8171 : else qof_event_gen(&s->inst, QOF_EVENT_MODIFY, nullptr);
1508 : 8181 : xaccSplitCommitEdit(s);
1509 : : }
1510 : : }
1511 : 4629 : g_list_free(slist);
1512 : :
1513 : 4629 : if (!qof_book_is_readonly(qof_instance_get_book(trans)))
1514 : 4629 : xaccTransWriteLog (trans, 'C');
1515 : :
1516 : : /* Get rid of the copy we made. We won't be rolling back,
1517 : : * so we don't need it any more. */
1518 : 4629 : PINFO ("get rid of rollback trans=%p", trans->orig);
1519 : 4629 : xaccFreeTransaction (trans->orig);
1520 : 4629 : trans->orig = nullptr;
1521 : :
1522 : : /* Sort the splits. Why do we need to do this ?? */
1523 : : /* Good question. Who knows? */
1524 : 4629 : xaccTransSortSplits(trans);
1525 : :
1526 : : /* Put back to zero. */
1527 : 4629 : qof_instance_decrease_editlevel(trans);
1528 : 4629 : g_assert(qof_instance_get_editlevel(trans) == 0);
1529 : :
1530 : 4629 : gen_event_trans (trans); //TODO: could be conditional
1531 : 4629 : qof_event_gen (&trans->inst, QOF_EVENT_MODIFY, nullptr);
1532 : 4629 : }
1533 : :
1534 : : void
1535 : 83415 : xaccTransCommitEdit (Transaction *trans)
1536 : : {
1537 : 83415 : if (!trans) return;
1538 : 68914 : ENTER ("(trans=%p)", trans);
1539 : :
1540 : 68914 : if (!qof_commit_edit (QOF_INSTANCE(trans)))
1541 : : {
1542 : 61885 : LEAVE("editlevel non-zero");
1543 : 61885 : return;
1544 : : }
1545 : :
1546 : : /* We increment this for the duration of the call
1547 : : * so other functions don't result in a recursive
1548 : : * call to xaccTransCommitEdit. */
1549 : 7029 : qof_instance_increase_editlevel(trans);
1550 : :
1551 : 7029 : if (was_trans_emptied(trans))
1552 : 4 : qof_instance_set_destroying(trans, TRUE);
1553 : :
1554 : : /* Before committing the transaction, we are going to enforce certain
1555 : : * constraints. In particular, we want to enforce the cap-gains
1556 : : * and the balanced lot constraints. These constraints might
1557 : : * change the number of splits in this transaction, and the
1558 : : * transaction itself might be deleted. This is also why
1559 : : * we can't really enforce these constraints elsewhere: they
1560 : : * can cause pointers to splits and transactions to disappear out
1561 : : * from under the holder.
1562 : : */
1563 : 10086 : if (!qof_instance_get_destroying(trans) && scrub_data &&
1564 : 3057 : !qof_book_shutting_down(xaccTransGetBook(trans)))
1565 : : {
1566 : : /* If scrubbing gains recurses through here, don't call it again. */
1567 : 3057 : scrub_data = 0;
1568 : : /* The total value of the transaction should sum to zero.
1569 : : * Call the trans scrub routine to fix it. Indirectly, this
1570 : : * routine also performs a number of other transaction fixes too.
1571 : : */
1572 : 3057 : xaccTransScrubImbalance (trans, nullptr, nullptr);
1573 : : /* Get the cap gains into a consistent state as well. */
1574 : :
1575 : : /* Lot Scrubbing is temporarily disabled. */
1576 : 3057 : if (g_getenv("GNC_AUTO_SCRUB_LOTS") != nullptr)
1577 : 0 : xaccTransScrubGains (trans, nullptr);
1578 : :
1579 : : /* Allow scrubbing in transaction commit again */
1580 : 3057 : scrub_data = 1;
1581 : : }
1582 : :
1583 : : /* Record the time of last modification */
1584 : 7029 : if (0 == trans->date_entered)
1585 : : {
1586 : 2106 : trans->date_entered = gnc_time(nullptr);
1587 : 2106 : qof_instance_set_dirty(QOF_INSTANCE(trans));
1588 : : }
1589 : :
1590 : 7029 : trans->txn_type = TXN_TYPE_UNCACHED;
1591 : 7029 : qof_commit_edit_part2(QOF_INSTANCE(trans), trans_on_error,
1592 : : trans_cleanup_commit, do_destroy);
1593 : 7029 : LEAVE ("(trans=%p)", trans);
1594 : : }
1595 : :
1596 : : /* Ughhh. The Rollback function is terribly complex, and, what's worse,
1597 : : * it only rolls back the basics. The TransCommit functions did a bunch
1598 : : * of Lot/Cap-gains scrubbing that don't get addressed/undone here, and
1599 : : * so the rollback can potentially leave a bit of a mess behind. We
1600 : : * really need a more robust undo capability. Part of the problem is
1601 : : * that the biggest user of the undo is the multi-user backend, which
1602 : : * also adds complexity.
1603 : : */
1604 : : void
1605 : 16 : xaccTransRollbackEdit (Transaction *trans)
1606 : : {
1607 : : GList *node, *onode;
1608 : : QofBackend *be;
1609 : : Transaction *orig;
1610 : : GList *slist;
1611 : : int num_preexist, i;
1612 : :
1613 : : /* FIXME: This isn't quite the right way to handle nested edits --
1614 : : * there should be a stack of transaction states that are popped off
1615 : : * and restored at each level -- but it does prevent restoring to the
1616 : : * editlevel 0 state until one is returning to editlevel 0, and
1617 : : * thereby prevents a crash caused by trans->orig getting nullptred too
1618 : : * soon.
1619 : : */
1620 : 16 : if (!qof_instance_get_editlevel (QOF_INSTANCE (trans))) return;
1621 : 15 : if (qof_instance_get_editlevel (QOF_INSTANCE (trans)) > 1) {
1622 : 2 : qof_instance_decrease_editlevel (QOF_INSTANCE (trans));
1623 : 2 : return;
1624 : : }
1625 : :
1626 : 13 : ENTER ("trans addr=%p\n", trans);
1627 : :
1628 : 13 : check_open(trans);
1629 : :
1630 : : /* copy the original values back in. */
1631 : :
1632 : 13 : orig = trans->orig;
1633 : 13 : std::swap (trans->num, orig->num);
1634 : 13 : std::swap (trans->description, orig->description);
1635 : 13 : trans->date_entered = orig->date_entered;
1636 : 13 : trans->date_posted = orig->date_posted;
1637 : 13 : std::swap (trans->common_currency, orig->common_currency);
1638 : 13 : qof_instance_swap_kvp (QOF_INSTANCE (trans), QOF_INSTANCE (orig));
1639 : :
1640 : : /* The splits at the front of trans->splits are exactly the same
1641 : : splits as in the original, but some of them may have changed, so
1642 : : we restore only those. */
1643 : : /* FIXME: Runs off the transaction's splits, so deleted splits are not
1644 : : * restored!
1645 : : */
1646 : 13 : num_preexist = g_list_length(orig->splits);
1647 : 13 : slist = g_list_copy(trans->splits);
1648 : 34 : for (i = 0, node = slist, onode = orig->splits; node;
1649 : 21 : i++, node = node->next, onode = onode ? onode->next : nullptr)
1650 : : {
1651 : 21 : Split *s = GNC_SPLIT(node->data);
1652 : :
1653 : 21 : if (!qof_instance_is_dirty(QOF_INSTANCE(s)))
1654 : 16 : continue;
1655 : :
1656 : 5 : if (i < num_preexist && onode)
1657 : : {
1658 : 2 : Split *so = GNC_SPLIT(onode->data);
1659 : :
1660 : 2 : xaccSplitRollbackEdit(s);
1661 : 2 : std::swap (s->action, so->action);
1662 : 2 : std::swap (s->memo, so->memo);
1663 : 2 : qof_instance_copy_kvp (QOF_INSTANCE (s), QOF_INSTANCE (so));
1664 : 2 : s->reconciled = so->reconciled;
1665 : 2 : s->amount = so->amount;
1666 : 2 : s->value = so->value;
1667 : 2 : s->lot = so->lot;
1668 : 2 : s->gains_split = so->gains_split;
1669 : : //SET_GAINS_A_VDIRTY(s);
1670 : 2 : s->date_reconciled = so->date_reconciled;
1671 : 2 : qof_instance_mark_clean(QOF_INSTANCE(s));
1672 : 2 : }
1673 : : else
1674 : : {
1675 : : /* Potentially added splits */
1676 : 3 : if (trans != xaccSplitGetParent(s))
1677 : : {
1678 : 0 : trans->splits = g_list_remove(trans->splits, s);
1679 : : /* New split added, but then moved to another
1680 : : transaction */
1681 : 0 : continue;
1682 : : }
1683 : 3 : xaccSplitRollbackEdit(s);
1684 : 3 : trans->splits = g_list_remove(trans->splits, s);
1685 : 3 : g_assert(trans != xaccSplitGetParent(s));
1686 : : /* NB: our memory management policy here is that a new split
1687 : : added to the transaction which is then rolled-back still
1688 : : belongs to the engine. Specifically, it's freed by the
1689 : : transaction to which it was added. Don't add the Split to
1690 : : more than one transaction during the begin/commit block! */
1691 : 3 : if (nullptr == xaccSplitGetParent(s))
1692 : : {
1693 : 3 : xaccFreeSplit(s); // a newly malloc'd split
1694 : : }
1695 : : }
1696 : : }
1697 : 13 : g_list_free(slist);
1698 : :
1699 : : // orig->splits may still have duped splits so free them
1700 : 13 : g_list_free_full (orig->splits, (GDestroyNotify)xaccFreeSplit);
1701 : 13 : orig->splits = nullptr;
1702 : :
1703 : : /* Now that the engine copy is back to its original version,
1704 : : * get the backend to fix it in the database */
1705 : 13 : be = qof_book_get_backend(qof_instance_get_book(trans));
1706 : : /** \todo Fix transrollbackedit in QOF so that rollback
1707 : : is exposed via the API. */
1708 : 13 : if (qof_backend_can_rollback (be))
1709 : : {
1710 : : QofBackendError errcode;
1711 : :
1712 : : /* clear errors */
1713 : : do
1714 : : {
1715 : 9 : errcode = qof_backend_get_error (be);
1716 : : }
1717 : 9 : while (ERR_BACKEND_NO_ERR != errcode);
1718 : :
1719 : 8 : qof_backend_rollback_instance (be, &(trans->inst));
1720 : :
1721 : 8 : errcode = qof_backend_get_error (be);
1722 : 8 : if (ERR_BACKEND_MOD_DESTROY == errcode)
1723 : : {
1724 : : /* The backend is asking us to delete this transaction.
1725 : : * This typically happens because another (remote) user
1726 : : * has deleted this transaction, and we haven't found
1727 : : * out about it until this user tried to edit it.
1728 : : */
1729 : 1 : xaccTransDestroy (trans);
1730 : 1 : do_destroy (QOF_INSTANCE(trans));
1731 : :
1732 : : /* push error back onto the stack */
1733 : 1 : qof_backend_set_error (be, errcode);
1734 : 1 : LEAVE ("deleted trans addr=%p\n", trans);
1735 : 1 : return;
1736 : : }
1737 : 7 : if (ERR_BACKEND_NO_ERR != errcode)
1738 : : {
1739 : 1 : PERR ("Rollback Failed. Ouch!");
1740 : : /* push error back onto the stack */
1741 : 1 : qof_backend_set_error (be, errcode);
1742 : : }
1743 : : }
1744 : :
1745 : 12 : if (!qof_book_is_readonly(qof_instance_get_book(trans)))
1746 : 10 : xaccTransWriteLog (trans, 'R');
1747 : :
1748 : 12 : xaccFreeTransaction (trans->orig);
1749 : :
1750 : 12 : trans->orig = nullptr;
1751 : 12 : qof_instance_set_destroying(trans, FALSE);
1752 : :
1753 : : /* Put back to zero. */
1754 : 12 : qof_instance_decrease_editlevel(trans);
1755 : : /* FIXME: The register code seems to depend on the engine to
1756 : : generate an event during rollback, even though the state is just
1757 : : reverting to what it was. */
1758 : 12 : gen_event_trans (trans);
1759 : :
1760 : 12 : LEAVE ("trans addr=%p\n", trans);
1761 : : }
1762 : :
1763 : : gboolean
1764 : 92 : xaccTransIsOpen (const Transaction *trans)
1765 : : {
1766 : 92 : return trans ? (0 < qof_instance_get_editlevel(trans)) : FALSE;
1767 : : }
1768 : :
1769 : : #define SECS_PER_DAY 86400
1770 : :
1771 : : int
1772 : 700603 : xaccTransOrder (const Transaction *ta, const Transaction *tb)
1773 : : {
1774 : 700603 : return xaccTransOrder_num_action (ta, nullptr, tb, nullptr);
1775 : : }
1776 : :
1777 : : /* Order a pair of potentially numeric string as numbers if both
1778 : : * strings begin with numbers, ordering the remainder of the string
1779 : : * lexically if the numeric parts are equal, and the whole strings
1780 : : * lexically otherwise.
1781 : : *
1782 : : * Note that this won't work well for numbers > 10^18 and that
1783 : : * negative numbers are treated as strings and will cause the pair to
1784 : : * be ordered lexically.
1785 : : */
1786 : :
1787 : : static int
1788 : 49550 : order_by_int64_or_string (const char* a, const char* b)
1789 : : {
1790 : 49550 : char *end_a = nullptr, *end_b = nullptr;
1791 : 49550 : int cmp = 0;
1792 : 49550 : uint64_t na = strtoull(a, &end_a, 10);
1793 : 49550 : uint64_t nb = strtoull(b, &end_b, 10);
1794 : 49550 : if (na && nb)
1795 : : {
1796 : 509 : if (na != nb)
1797 : 480 : return na < nb ? -1 : 1;
1798 : 29 : cmp = g_utf8_collate(end_a, end_b);
1799 : : }
1800 : : else
1801 : : {
1802 : 49041 : cmp = g_utf8_collate(a, b);
1803 : : }
1804 : 49070 : return cmp < 0 ? -1 : cmp > 0 ? 1 : 0;
1805 : : }
1806 : :
1807 : : int
1808 : 700618 : xaccTransOrder_num_action (const Transaction *ta, const char *actna,
1809 : : const Transaction *tb, const char *actnb)
1810 : : {
1811 : : const char *da, *db;
1812 : : int retval;
1813 : :
1814 : 700618 : if (ta == tb) return 0;
1815 : 691554 : if (!tb) return -1;
1816 : 691552 : if (!ta) return +1;
1817 : :
1818 : 691551 : if (ta->date_posted != tb->date_posted)
1819 : 641993 : return (ta->date_posted > tb->date_posted) - (ta->date_posted < tb->date_posted);
1820 : :
1821 : : /* Always sort closing transactions after normal transactions */
1822 : : {
1823 : 49558 : gboolean ta_is_closing = xaccTransGetIsClosingTxn (ta);
1824 : 49558 : gboolean tb_is_closing = xaccTransGetIsClosingTxn (tb);
1825 : 49558 : if (ta_is_closing != tb_is_closing)
1826 : 8 : return (ta_is_closing - tb_is_closing);
1827 : : }
1828 : :
1829 : : /* otherwise, sort on number string */
1830 : 49550 : if (actna && actnb) /* split action string, if not nullptr */
1831 : : {
1832 : 3 : retval = order_by_int64_or_string (actna, actnb);
1833 : : }
1834 : : else /* else transaction num string */
1835 : : {
1836 : 49547 : retval = order_by_int64_or_string (ta->num, tb->num);
1837 : : }
1838 : 49550 : if (retval)
1839 : 3129 : return retval;
1840 : :
1841 : 46421 : if (ta->date_entered != tb->date_entered)
1842 : 1424 : return (ta->date_entered > tb->date_entered) - (ta->date_entered < tb->date_entered);
1843 : :
1844 : : /* otherwise, sort on description string */
1845 : 44997 : da = ta->description ? ta->description : "";
1846 : 44997 : db = tb->description ? tb->description : "";
1847 : 44997 : retval = g_utf8_collate (da, db);
1848 : 44997 : if (retval)
1849 : 44765 : return retval;
1850 : :
1851 : : /* else, sort on guid - keeps sort stable. */
1852 : 232 : return qof_instance_guid_compare(ta, tb);
1853 : : }
1854 : :
1855 : : /********************************************************************\
1856 : : \********************************************************************/
1857 : :
1858 : : static void
1859 : 718 : set_kvp_string_path (Transaction *txn, const Path& path, const char *value)
1860 : : {
1861 : 718 : g_return_if_fail (GNC_IS_TRANSACTION(txn));
1862 : 718 : xaccTransBeginEdit(txn);
1863 : 1406 : auto val = value && *value ? std::make_optional<const char*>(g_strdup(value)) : std::nullopt;
1864 : 718 : qof_instance_set_path_kvp<const char*> (QOF_INSTANCE(txn), val, path);
1865 : 718 : qof_instance_set_dirty (QOF_INSTANCE(txn));
1866 : 718 : xaccTransCommitEdit(txn);
1867 : : }
1868 : :
1869 : : static const char*
1870 : 14222 : get_kvp_string_path (const Transaction *txn, const Path& path)
1871 : : {
1872 : 14222 : auto rv{qof_instance_get_path_kvp<const char*> (QOF_INSTANCE(txn), path)};
1873 : 28444 : return rv ? *rv : nullptr;
1874 : : }
1875 : :
1876 : : static inline void
1877 : 4623 : xaccTransSetDateInternal(Transaction *trans, time64 *dadate, time64 val)
1878 : : {
1879 : 4623 : xaccTransBeginEdit(trans);
1880 : :
1881 : : #if 0 /* gnc_ctime is expensive so change to 1 only if you need to debug setting
1882 : : * dates. */
1883 : : {
1884 : : time64 secs = (time64) val.tv_sec;
1885 : : gchar *tstr = gnc_ctime (&secs);
1886 : : PINFO ("addr=%p set date to %" G_GUINT64_FORMAT ".%09ld %s\n",
1887 : : trans, val.tv_sec, val.tv_nsec, tstr ? tstr : "(null)");
1888 : : g_free(tstr);
1889 : : }
1890 : : #endif
1891 : 4623 : *dadate = val;
1892 : 4623 : qof_instance_set_dirty(QOF_INSTANCE(trans));
1893 : 4623 : mark_trans(trans);
1894 : 4623 : xaccTransCommitEdit(trans);
1895 : :
1896 : : /* Because the date has changed, we need to make sure that each of
1897 : : * the splits is properly ordered in each of their accounts. We
1898 : : * could do that here, simply by reinserting each split into its
1899 : : * account. However, in some ways this is bad behaviour, and it
1900 : : * seems much better/nicer to defer that until the commit phase,
1901 : : * i.e. until the user has called the xaccTransCommitEdit()
1902 : : * routine. So, for now, we are done. */
1903 : 4623 : }
1904 : :
1905 : : static inline void
1906 : 3384 : set_gains_date_dirty (Transaction *trans)
1907 : : {
1908 : 3443 : FOR_EACH_SPLIT(trans, s->gains |= GAINS_STATUS_DATE_DIRTY);
1909 : 3384 : }
1910 : :
1911 : : void
1912 : 1175 : xaccTransSetDatePostedSecs (Transaction *trans, time64 secs)
1913 : : {
1914 : 1175 : if (!trans) return;
1915 : 1175 : xaccTransSetDateInternal(trans, &trans->date_posted, secs);
1916 : 1175 : set_gains_date_dirty(trans);
1917 : : }
1918 : :
1919 : : void
1920 : 110 : xaccTransSetDatePostedSecsNormalized (Transaction *trans, time64 time)
1921 : : {
1922 : : GDate date;
1923 : 110 : gnc_gdate_set_time64(&date, time);
1924 : 110 : xaccTransSetDatePostedGDate(trans, date);
1925 : 110 : }
1926 : :
1927 : : void
1928 : 2209 : xaccTransSetDatePostedGDate (Transaction *trans, GDate date)
1929 : : {
1930 : 2209 : if (!trans) return;
1931 : :
1932 : : /* We additionally save this date into a kvp frame to ensure in
1933 : : * the future a date which was set as *date* (without time) can
1934 : : * clearly be distinguished from the time64. */
1935 : 4418 : qof_instance_set_path_kvp<GDate> (QOF_INSTANCE(trans), date, {TRANS_DATE_POSTED});
1936 : 2209 : qof_instance_set_dirty (QOF_INSTANCE(trans));
1937 : : /* mark dirty and commit handled by SetDateInternal */
1938 : 2209 : xaccTransSetDateInternal(trans, &trans->date_posted,
1939 : : gdate_to_time64(date));
1940 : 2209 : set_gains_date_dirty (trans);
1941 : : }
1942 : :
1943 : : void
1944 : 1239 : xaccTransSetDateEnteredSecs (Transaction *trans, time64 secs)
1945 : : {
1946 : 1239 : if (!trans) return;
1947 : 1239 : xaccTransSetDateInternal(trans, &trans->date_entered, secs);
1948 : : }
1949 : :
1950 : : void
1951 : 1996 : xaccTransSetDate (Transaction *trans, int day, int mon, int year)
1952 : : {
1953 : 1996 : if (!trans) return;
1954 : : GDate date;
1955 : 1996 : g_date_clear (&date, 1);
1956 : 1996 : if (g_date_valid_dmy (day, static_cast<GDateMonth>(mon), year))
1957 : 1996 : g_date_set_dmy (&date, day, static_cast<GDateMonth>(mon), year);
1958 : : else
1959 : : {
1960 : 0 : PWARN("Attempted to set invalid date %d-%d-%d; set today's date instead.",
1961 : : year, mon, day);
1962 : 0 : gnc_gdate_set_today (&date);
1963 : : }
1964 : 1996 : xaccTransSetDatePostedGDate(trans, date);
1965 : : }
1966 : :
1967 : : void
1968 : 76 : xaccTransSetDateDue (Transaction * trans, time64 time)
1969 : : {
1970 : 76 : if (!trans) return;
1971 : 76 : xaccTransBeginEdit(trans);
1972 : 152 : qof_instance_set_path_kvp<Time64> (QOF_INSTANCE (trans), Time64{time}, {TRANS_DATE_DUE_KVP});
1973 : 76 : qof_instance_set_dirty(QOF_INSTANCE(trans));
1974 : 76 : xaccTransCommitEdit(trans);
1975 : : }
1976 : :
1977 : : void
1978 : 100 : xaccTransSetTxnType (Transaction *trans, char type)
1979 : : {
1980 : 100 : char s[2] = {type, '\0'};
1981 : 200 : set_kvp_string_path (trans, {TRANS_TXN_TYPE_KVP}, s);
1982 : 100 : }
1983 : :
1984 : 16 : void xaccTransClearReadOnly (Transaction *trans)
1985 : : {
1986 : 32 : set_kvp_string_path (trans, {TRANS_READ_ONLY_REASON}, nullptr);
1987 : 16 : }
1988 : :
1989 : : void
1990 : 181 : xaccTransSetReadOnly (Transaction *trans, const char *reason)
1991 : : {
1992 : 181 : if (trans && reason)
1993 : 540 : set_kvp_string_path (trans, {TRANS_READ_ONLY_REASON}, reason);
1994 : 181 : }
1995 : :
1996 : : /********************************************************************\
1997 : : \********************************************************************/
1998 : :
1999 : : /* QOF does not open the trans before setting a parameter,
2000 : : but the call uses check_open so we cannot use the call directly. */
2001 : : static void
2002 : 0 : qofTransSetNum (Transaction *trans, const char *xnum)
2003 : : {
2004 : 0 : if (!qof_begin_edit(&trans->inst)) return;
2005 : 0 : xaccTransSetNum(trans, xnum);
2006 : 0 : qof_commit_edit(&trans->inst);
2007 : : }
2008 : :
2009 : : void
2010 : 865 : xaccTransSetNum (Transaction *trans, const char *xnum)
2011 : : {
2012 : 865 : if (!trans || !xnum) return;
2013 : 865 : xaccTransBeginEdit(trans);
2014 : :
2015 : 865 : CACHE_REPLACE(trans->num, xnum);
2016 : 865 : qof_instance_set_dirty(QOF_INSTANCE(trans));
2017 : 865 : mark_trans(trans); /* Dirty balance of every account in trans */
2018 : 865 : xaccTransCommitEdit(trans);
2019 : : }
2020 : :
2021 : : static void
2022 : 0 : qofTransSetDescription (Transaction *trans, const char *desc)
2023 : : {
2024 : 0 : if (!qof_begin_edit(&trans->inst)) return;
2025 : 0 : xaccTransSetDescription(trans, desc);
2026 : 0 : qof_commit_edit(&trans->inst);
2027 : : }
2028 : :
2029 : : void
2030 : 3379 : xaccTransSetDescription (Transaction *trans, const char *desc)
2031 : : {
2032 : 3379 : if (!trans || !desc) return;
2033 : 3375 : xaccTransBeginEdit(trans);
2034 : :
2035 : 3375 : CACHE_REPLACE(trans->description, desc);
2036 : 3375 : qof_instance_set_dirty(QOF_INSTANCE(trans));
2037 : 3375 : xaccTransCommitEdit(trans);
2038 : : }
2039 : :
2040 : : void
2041 : 11 : xaccTransSetDocLink (Transaction *trans, const char *doclink)
2042 : : {
2043 : 11 : if (!trans || !doclink) return;
2044 : 24 : set_kvp_string_path (trans, {doclink_uri_str}, doclink);
2045 : 24 : }
2046 : :
2047 : : static void
2048 : 0 : qofTransSetNotes (Transaction *trans, const char *notes)
2049 : : {
2050 : 0 : if (!qof_begin_edit(&trans->inst)) return;
2051 : 0 : xaccTransSetNotes(trans, notes);
2052 : 0 : qof_commit_edit(&trans->inst);
2053 : : }
2054 : :
2055 : : void
2056 : 100 : xaccTransSetNotes (Transaction *trans, const char *notes)
2057 : : {
2058 : 100 : if (!trans || !notes) return;
2059 : 285 : set_kvp_string_path (trans, {trans_notes_str}, notes);
2060 : 285 : }
2061 : :
2062 : : void
2063 : 27 : xaccTransSetIsClosingTxn (Transaction *trans, gboolean is_closing)
2064 : : {
2065 : 27 : xaccTransBeginEdit(trans);
2066 : 27 : auto val = is_closing ? std::make_optional<int64_t>(1) : std::nullopt;
2067 : 81 : qof_instance_set_path_kvp<int64_t> (QOF_INSTANCE(trans), val, {trans_is_closing_str});
2068 : 27 : xaccTransCommitEdit(trans);
2069 : 81 : }
2070 : :
2071 : :
2072 : : /********************************************************************\
2073 : : \********************************************************************/
2074 : : void
2075 : 2413 : xaccTransClearSplits(Transaction* trans)
2076 : : {
2077 : 2413 : xaccTransBeginEdit(trans);
2078 : : /* We only own the splits that still think they belong to us. This is done
2079 : : in 2 steps. In the first, the splits are marked as being destroyed, but they
2080 : : are not destroyed yet. In the second, the destruction is committed which will
2081 : : do the actual destruction. If both steps are done for a split before they are
2082 : : done for the next split, then a split will still be on the split list after it
2083 : : has been freed. This can cause other parts of the code (e.g. in xaccSplitDestroy())
2084 : : to reference the split after it has been freed. */
2085 : 7340 : for (auto node = trans->splits; node; node = node->next)
2086 : : {
2087 : 4927 : auto s = GNC_SPLIT(node->data);
2088 : 4927 : if (s && s->parent == trans)
2089 : : {
2090 : 4926 : xaccSplitDestroy(s);
2091 : : }
2092 : : }
2093 : 7340 : for (auto node = trans->splits; node; node = node->next)
2094 : : {
2095 : 4927 : auto s = GNC_SPLIT(node->data);
2096 : 4927 : if (s && s->parent == trans)
2097 : : {
2098 : 4926 : xaccSplitCommitEdit(s);
2099 : : }
2100 : : }
2101 : 2413 : g_list_free (trans->splits);
2102 : 2413 : trans->splits = nullptr;
2103 : :
2104 : 2413 : xaccTransCommitEdit(trans);
2105 : 2413 : }
2106 : :
2107 : : Split *
2108 : 383 : xaccTransGetSplit (const Transaction *trans, int i)
2109 : : {
2110 : 383 : int j = 0;
2111 : 383 : if (!trans || i < 0) return nullptr;
2112 : :
2113 : 548 : FOR_EACH_SPLIT(trans, { if (i == j) return s; j++; });
2114 : 50 : return nullptr;
2115 : : }
2116 : :
2117 : : int
2118 : 4967 : xaccTransGetSplitIndex(const Transaction *trans, const Split *split)
2119 : : {
2120 : 4967 : int j = 0;
2121 : 4967 : g_return_val_if_fail(trans && split, -1);
2122 : :
2123 : 7703 : FOR_EACH_SPLIT(trans, { if (s == split) return j; j++; });
2124 : 1 : return -1;
2125 : : }
2126 : :
2127 : : SplitList *
2128 : 5847 : xaccTransGetSplitList (const Transaction *trans)
2129 : : {
2130 : 5847 : return trans ? trans->splits : nullptr;
2131 : : }
2132 : :
2133 : : SplitList *
2134 : 0 : xaccTransGetPaymentAcctSplitList (const Transaction *trans)
2135 : : {
2136 : 0 : GList *pay_splits = nullptr;
2137 : 0 : FOR_EACH_SPLIT (trans,
2138 : : const Account *account = xaccSplitGetAccount(s);
2139 : : if (account && gncBusinessIsPaymentAcctType(xaccAccountGetType(account)))
2140 : : pay_splits = g_list_prepend (pay_splits, s);
2141 : : );
2142 : :
2143 : 0 : pay_splits = g_list_reverse (pay_splits);
2144 : 0 : return pay_splits;
2145 : : }
2146 : :
2147 : : SplitList *
2148 : 9 : xaccTransGetAPARAcctSplitList (const Transaction *trans, gboolean strict)
2149 : : {
2150 : 9 : GList *apar_splits = nullptr;
2151 : 9 : if (!trans) return nullptr;
2152 : :
2153 : 28 : FOR_EACH_SPLIT (trans,
2154 : : const Account *account = xaccSplitGetAccount(s);
2155 : : if (account && xaccAccountIsAPARType(xaccAccountGetType(account)))
2156 : : {
2157 : :
2158 : : if (!strict)
2159 : : apar_splits = g_list_prepend (apar_splits, s);
2160 : : else
2161 : : {
2162 : : GncOwner owner;
2163 : : GNCLot *lot = xaccSplitGetLot(s);
2164 : : if (lot &&
2165 : : (gncInvoiceGetInvoiceFromLot (lot) ||
2166 : : gncOwnerGetOwnerFromLot (lot, &owner)))
2167 : : apar_splits = g_list_prepend (apar_splits, s);
2168 : : }
2169 : : }
2170 : : );
2171 : :
2172 : 9 : apar_splits = g_list_reverse (apar_splits);
2173 : 9 : return apar_splits;
2174 : : }
2175 : :
2176 : 0 : Split *xaccTransGetFirstPaymentAcctSplit(const Transaction *trans)
2177 : : {
2178 : 0 : FOR_EACH_SPLIT (trans,
2179 : : const Account *account = xaccSplitGetAccount(s);
2180 : : if (account && gncBusinessIsPaymentAcctType(xaccAccountGetType(account)))
2181 : : return s;
2182 : : );
2183 : :
2184 : 0 : return nullptr;
2185 : : }
2186 : :
2187 : 22 : Split *xaccTransGetFirstAPARAcctSplit (const Transaction *trans, gboolean strict)
2188 : : {
2189 : 40 : FOR_EACH_SPLIT (trans,
2190 : : const Account *account = xaccSplitGetAccount(s);
2191 : : if (account && xaccAccountIsAPARType(xaccAccountGetType(account)))
2192 : : {
2193 : : GNCLot *lot;
2194 : : GncOwner owner;
2195 : :
2196 : : if (!strict)
2197 : : return s;
2198 : :
2199 : : lot = xaccSplitGetLot(s);
2200 : : if (lot &&
2201 : : (gncInvoiceGetInvoiceFromLot (lot) ||
2202 : : gncOwnerGetOwnerFromLot (lot, &owner)))
2203 : : return s;
2204 : : }
2205 : : );
2206 : :
2207 : 0 : return nullptr;
2208 : : }
2209 : :
2210 : : int
2211 : 204 : xaccTransCountSplits (const Transaction *trans)
2212 : : {
2213 : 204 : gint i = 0;
2214 : 204 : g_return_val_if_fail (trans != nullptr, 0);
2215 : 612 : FOR_EACH_SPLIT(trans, i++);
2216 : 204 : return i;
2217 : : }
2218 : :
2219 : : const char *
2220 : 2086 : xaccTransGetNum (const Transaction *trans)
2221 : : {
2222 : 2086 : return trans ? trans->num : nullptr;
2223 : : }
2224 : :
2225 : : const char *
2226 : 4747 : xaccTransGetDescription (const Transaction *trans)
2227 : : {
2228 : 4747 : return trans ? trans->description : nullptr;
2229 : : }
2230 : :
2231 : : const char *
2232 : 28 : xaccTransGetDocLink (const Transaction *trans)
2233 : : {
2234 : 112 : return get_kvp_string_path (trans, {doclink_uri_str});
2235 : 84 : }
2236 : :
2237 : : const char *
2238 : 5675 : xaccTransGetNotes (const Transaction *trans)
2239 : : {
2240 : 22700 : return get_kvp_string_path (trans, {trans_notes_str});
2241 : 17025 : }
2242 : :
2243 : : gboolean
2244 : 199687 : xaccTransGetIsClosingTxn (const Transaction *trans)
2245 : : {
2246 : 599061 : auto rv{qof_instance_get_path_kvp<int64_t> (QOF_INSTANCE(trans), {trans_is_closing_str})};
2247 : 399374 : return rv ? *rv != 0 : FALSE;
2248 : 599061 : }
2249 : :
2250 : : /********************************************************************\
2251 : : \********************************************************************/
2252 : :
2253 : : time64
2254 : 41090 : xaccTransGetDate (const Transaction *trans)
2255 : : {
2256 : 41090 : return trans ? trans->date_posted : 0;
2257 : : }
2258 : :
2259 : : /*################## Added for Reg2 #################*/
2260 : : time64
2261 : 7 : xaccTransGetDateEntered (const Transaction *trans)
2262 : : {
2263 : 7 : return trans ? trans->date_entered : 0;
2264 : : }
2265 : : /*################## Added for Reg2 #################*/
2266 : :
2267 : : time64
2268 : 11855 : xaccTransRetDatePosted (const Transaction *trans)
2269 : : {
2270 : 11855 : return trans ? trans->date_posted : 0;
2271 : : }
2272 : :
2273 : : GDate
2274 : 2 : xaccTransGetDatePostedGDate (const Transaction *trans)
2275 : : {
2276 : : GDate result;
2277 : 2 : g_date_clear (&result, 1);
2278 : 2 : if (trans)
2279 : : {
2280 : : /* Can we look up this value in the kvp slot? If yes, use it
2281 : : * from there because it doesn't suffer from time zone
2282 : : * shifts. */
2283 : 6 : if (auto res = qof_instance_get_path_kvp<GDate> (QOF_INSTANCE(trans), {TRANS_DATE_POSTED}))
2284 : 1 : result = *res;
2285 : 2 : if (! g_date_valid (&result) || gdate_to_time64 (result) == INT64_MAX)
2286 : : {
2287 : : /* Well, this txn doesn't have a valid GDate saved in a slot.
2288 : : * time64_to_gdate() uses local time and we want UTC so we have
2289 : : * to write it out.
2290 : : */
2291 : 1 : time64 time = xaccTransGetDate(trans);
2292 : 1 : struct tm *stm = gnc_gmtime(&time);
2293 : 1 : if (stm)
2294 : : {
2295 : 1 : g_date_set_dmy(&result, stm->tm_mday,
2296 : 1 : (GDateMonth)(stm->tm_mon + 1),
2297 : 1 : stm->tm_year + 1900);
2298 : 1 : free(stm);
2299 : : }
2300 : : }
2301 : : }
2302 : 2 : return result;
2303 : : }
2304 : :
2305 : : time64
2306 : 108 : xaccTransRetDateEntered (const Transaction *trans)
2307 : : {
2308 : 108 : return trans ? trans->date_entered : 0;
2309 : : }
2310 : :
2311 : : time64
2312 : 85 : xaccTransRetDateDue(const Transaction *trans)
2313 : : {
2314 : 85 : if (!trans) return 0;
2315 : 170 : auto res = qof_instance_get_path_kvp<Time64> (QOF_INSTANCE (trans), {TRANS_DATE_DUE_KVP});
2316 : 85 : return res ? res->t : xaccTransRetDatePosted (trans);
2317 : : }
2318 : :
2319 : : char
2320 : 343 : xaccTransGetTxnType (Transaction *trans)
2321 : : {
2322 : 343 : gboolean has_nonAPAR_split = FALSE;
2323 : :
2324 : 343 : if (!trans) return TXN_TYPE_NONE;
2325 : :
2326 : 343 : if (trans->txn_type != TXN_TYPE_UNCACHED)
2327 : 268 : return trans->txn_type;
2328 : :
2329 : 75 : trans->txn_type = TXN_TYPE_NONE;
2330 : 244 : for (GList *n = xaccTransGetSplitList (trans); n; n = g_list_next (n))
2331 : : {
2332 : 169 : Account *acc = xaccSplitGetAccount (GNC_SPLIT(n->data));
2333 : :
2334 : 169 : if (!acc)
2335 : 0 : continue;
2336 : :
2337 : 169 : if (!xaccAccountIsAPARType (xaccAccountGetType (acc)))
2338 : 111 : has_nonAPAR_split = TRUE;
2339 : 58 : else if (trans->txn_type == TXN_TYPE_NONE)
2340 : : {
2341 : 55 : GNCLot *lot = xaccSplitGetLot (GNC_SPLIT(n->data));
2342 : 55 : GncInvoice *invoice = gncInvoiceGetInvoiceFromLot (lot);
2343 : : GncOwner owner;
2344 : :
2345 : 55 : if (invoice && trans == gncInvoiceGetPostedTxn (invoice))
2346 : 22 : trans->txn_type = TXN_TYPE_INVOICE;
2347 : 33 : else if (invoice || gncOwnerGetOwnerFromLot (lot, &owner))
2348 : 30 : trans->txn_type = TXN_TYPE_PAYMENT;
2349 : : }
2350 : : }
2351 : :
2352 : 75 : if (!has_nonAPAR_split && (trans->txn_type == TXN_TYPE_PAYMENT))
2353 : 2 : trans->txn_type = TXN_TYPE_LINK;
2354 : :
2355 : 75 : return trans->txn_type;
2356 : : }
2357 : :
2358 : : const char *
2359 : 2546 : xaccTransGetReadOnly (Transaction *trans)
2360 : : {
2361 : 7638 : return get_kvp_string_path (trans, {TRANS_READ_ONLY_REASON});
2362 : : }
2363 : :
2364 : : static gboolean
2365 : 0 : xaccTransIsSXTemplate (const Transaction * trans)
2366 : : {
2367 : 0 : Split *split0 = xaccTransGetSplit (trans, 0);
2368 : 0 : if (split0 != nullptr)
2369 : : {
2370 : 0 : char* formula = nullptr;
2371 : 0 : g_object_get (split0, "sx-debit-formula", &formula, nullptr);
2372 : 0 : if (formula != nullptr)
2373 : : {
2374 : 0 : g_free (formula);
2375 : 0 : return TRUE;
2376 : : }
2377 : 0 : g_object_get (split0, "sx-credit-formula", &formula, nullptr);
2378 : 0 : if (formula != nullptr)
2379 : : {
2380 : 0 : g_free (formula);
2381 : 0 : return TRUE;
2382 : : }
2383 : : }
2384 : 0 : return FALSE;
2385 : : }
2386 : :
2387 : 0 : gboolean xaccTransIsReadonlyByPostedDate(const Transaction *trans)
2388 : : {
2389 : : GDate *threshold_date;
2390 : : GDate trans_date;
2391 : 0 : const QofBook *book = xaccTransGetBook (trans);
2392 : : gboolean result;
2393 : 0 : g_assert(trans);
2394 : :
2395 : 0 : if (!qof_book_uses_autoreadonly(book))
2396 : : {
2397 : 0 : return FALSE;
2398 : : }
2399 : :
2400 : 0 : if (xaccTransIsSXTemplate (trans))
2401 : 0 : return FALSE;
2402 : :
2403 : 0 : threshold_date = qof_book_get_autoreadonly_gdate(book);
2404 : 0 : g_assert(threshold_date); // ok because we checked uses_autoreadonly before
2405 : 0 : trans_date = xaccTransGetDatePostedGDate(trans);
2406 : :
2407 : : // g_warning("there is auto-read-only with days=%d, trans_date_day=%d, threshold_date_day=%d",
2408 : : // qof_book_get_num_days_autofreeze(book),
2409 : : // g_date_get_day(&trans_date),
2410 : : // g_date_get_day(threshold_date));
2411 : :
2412 : 0 : if (g_date_compare(&trans_date, threshold_date) < 0)
2413 : : {
2414 : : //g_warning("we are auto-read-only");
2415 : 0 : result = TRUE;
2416 : : }
2417 : : else
2418 : : {
2419 : 0 : result = FALSE;
2420 : : }
2421 : 0 : g_date_free(threshold_date);
2422 : 0 : return result;
2423 : : }
2424 : :
2425 : : gboolean
2426 : 0 : xaccTransHasReconciledSplitsByAccount (const Transaction *trans,
2427 : : const Account *account)
2428 : : {
2429 : : GList *node;
2430 : :
2431 : 0 : for (node = xaccTransGetSplitList (trans); node; node = node->next)
2432 : : {
2433 : 0 : Split *split = GNC_SPLIT(node->data);
2434 : :
2435 : 0 : if (!xaccTransStillHasSplit(trans, split))
2436 : 0 : continue;
2437 : 0 : if (account && (xaccSplitGetAccount(split) != account))
2438 : 0 : continue;
2439 : :
2440 : 0 : switch (xaccSplitGetReconcile (split))
2441 : : {
2442 : 0 : case YREC:
2443 : : case FREC:
2444 : 0 : return TRUE;
2445 : :
2446 : 0 : default:
2447 : 0 : break;
2448 : : }
2449 : : }
2450 : :
2451 : 0 : return FALSE;
2452 : : }
2453 : :
2454 : : gboolean
2455 : 0 : xaccTransHasReconciledSplits (const Transaction *trans)
2456 : : {
2457 : 0 : return xaccTransHasReconciledSplitsByAccount (trans, nullptr);
2458 : : }
2459 : :
2460 : :
2461 : : gboolean
2462 : 0 : xaccTransHasSplitsInStateByAccount (const Transaction *trans,
2463 : : const char state,
2464 : : const Account *account)
2465 : : {
2466 : : GList *node;
2467 : :
2468 : 0 : for (node = xaccTransGetSplitList (trans); node; node = node->next)
2469 : : {
2470 : 0 : Split *split = GNC_SPLIT(node->data);
2471 : :
2472 : 0 : if (!xaccTransStillHasSplit(trans, split))
2473 : 0 : continue;
2474 : 0 : if (account && (xaccSplitGetAccount(split) != account))
2475 : 0 : continue;
2476 : :
2477 : 0 : if (split->reconciled == state)
2478 : 0 : return TRUE;
2479 : : }
2480 : :
2481 : 0 : return FALSE;
2482 : : }
2483 : :
2484 : : gboolean
2485 : 0 : xaccTransHasSplitsInState (const Transaction *trans, const char state)
2486 : : {
2487 : 0 : return xaccTransHasSplitsInStateByAccount (trans, state, nullptr);
2488 : : }
2489 : :
2490 : :
2491 : : /********************************************************************\
2492 : : \********************************************************************/
2493 : :
2494 : :
2495 : : /* ====================================================================== */
2496 : :
2497 : : static int
2498 : 8 : counter_thunk(Transaction *t, void *data)
2499 : : {
2500 : 8 : (*((guint*)data))++;
2501 : 8 : return 0;
2502 : : }
2503 : :
2504 : : guint
2505 : 8 : gnc_book_count_transactions(QofBook *book)
2506 : : {
2507 : 8 : guint count = 0;
2508 : 8 : xaccAccountTreeForEachTransaction(gnc_book_get_root_account(book),
2509 : : counter_thunk, (void*)&count);
2510 : 8 : return count;
2511 : : }
2512 : :
2513 : : /********************************************************************\
2514 : : \********************************************************************/
2515 : :
2516 : : void
2517 : 103 : xaccTransVoid(Transaction *trans, const char *reason)
2518 : : {
2519 : 104 : g_return_if_fail(trans && reason);
2520 : :
2521 : : /* Prevent voiding transactions that are already marked
2522 : : * read only, for example generated by the business features.
2523 : : */
2524 : 103 : if (xaccTransGetReadOnly (trans))
2525 : : {
2526 : 1 : PWARN ("Refusing to void a read-only transaction!");
2527 : 1 : return;
2528 : : }
2529 : 102 : xaccTransBeginEdit(trans);
2530 : :
2531 : 102 : char iso8601_str[ISO_DATELENGTH + 1] = "";
2532 : 102 : gnc_time64_to_iso8601_buff (gnc_time(nullptr), iso8601_str);
2533 : :
2534 : 306 : if (auto s = get_kvp_string_path (trans, {trans_notes_str}))
2535 : 3 : set_kvp_string_path (trans, {void_former_notes_str}, s);
2536 : 408 : set_kvp_string_path (trans, {trans_notes_str}, _("Voided transaction"));
2537 : 306 : set_kvp_string_path (trans, {void_reason_str}, reason);
2538 : 306 : set_kvp_string_path (trans, {void_time_str}, iso8601_str);
2539 : :
2540 : 306 : FOR_EACH_SPLIT(trans, xaccSplitVoid(s));
2541 : :
2542 : : /* Dirtying taken care of by SetReadOnly */
2543 : 102 : xaccTransSetReadOnly(trans, _("Transaction Voided"));
2544 : 102 : xaccTransCommitEdit(trans);
2545 : 1022 : }
2546 : :
2547 : : gboolean
2548 : 5859 : xaccTransGetVoidStatus(const Transaction *trans)
2549 : : {
2550 : 5859 : auto t = xaccTransGetVoidTime (trans);
2551 : 5859 : return t != INT64_MAX;
2552 : : }
2553 : :
2554 : : const char *
2555 : 6 : xaccTransGetVoidReason(const Transaction *trans)
2556 : : {
2557 : 24 : return get_kvp_string_path (trans, {void_reason_str});
2558 : 18 : }
2559 : :
2560 : : time64
2561 : 5862 : xaccTransGetVoidTime(const Transaction *tr)
2562 : : {
2563 : 17586 : auto void_str{get_kvp_string_path (tr, {void_time_str})};
2564 : 5862 : return void_str ? gnc_iso8601_to_time64_gmt (void_str) : INT64_MAX;
2565 : 17586 : }
2566 : :
2567 : : void
2568 : 3 : xaccTransUnvoid (Transaction *trans)
2569 : : {
2570 : 3 : g_return_if_fail(trans);
2571 : :
2572 : 3 : if (!xaccTransGetVoidStatus (trans))
2573 : 0 : return; /* Transaction isn't voided. Bail. */
2574 : :
2575 : 3 : xaccTransBeginEdit(trans);
2576 : :
2577 : 18 : set_kvp_string_path (trans, {trans_notes_str}, get_kvp_string_path (trans, {void_former_notes_str}));
2578 : 9 : set_kvp_string_path (trans, {void_former_notes_str}, nullptr);
2579 : 9 : set_kvp_string_path (trans, {void_reason_str}, nullptr);
2580 : 9 : set_kvp_string_path (trans, {void_time_str}, nullptr);
2581 : :
2582 : 9 : FOR_EACH_SPLIT(trans, xaccSplitUnvoid(s));
2583 : :
2584 : : /* Dirtying taken care of by ClearReadOnly */
2585 : 3 : xaccTransClearReadOnly(trans);
2586 : 3 : xaccTransCommitEdit(trans);
2587 : 36 : }
2588 : :
2589 : : Transaction *
2590 : 2 : xaccTransReverse (Transaction *orig)
2591 : : {
2592 : : Transaction *trans;
2593 : 2 : g_return_val_if_fail(orig, nullptr);
2594 : :
2595 : : /* First edit, dirty, and commit orig to ensure that any trading
2596 : : * splits are correctly balanced.
2597 : : */
2598 : 2 : xaccTransBeginEdit (orig);
2599 : 2 : qof_instance_set_dirty (QOF_INSTANCE (orig));
2600 : 2 : xaccTransCommitEdit (orig);
2601 : :
2602 : 2 : trans = xaccTransClone(orig);
2603 : 2 : g_return_val_if_fail (trans, nullptr);
2604 : 2 : xaccTransBeginEdit(trans);
2605 : :
2606 : : /* Reverse the values on each split. Clear per-split info. */
2607 : 6 : FOR_EACH_SPLIT(trans,
2608 : : {
2609 : : xaccSplitSetAmount(s, gnc_numeric_neg(xaccSplitGetAmount(s)));
2610 : : xaccSplitSetValue(s, gnc_numeric_neg(xaccSplitGetValue(s)));
2611 : : xaccSplitSetReconcile(s, NREC);
2612 : : });
2613 : :
2614 : : /* Now update the original with a pointer to the new one */
2615 : 4 : qof_instance_set_path_kvp<GncGUID*> (QOF_INSTANCE (orig), guid_copy(xaccTransGetGUID(trans)),
2616 : : {TRANS_REVERSED_BY});
2617 : :
2618 : : /* Make sure the reverse transaction is not read-only */
2619 : 2 : xaccTransClearReadOnly(trans);
2620 : :
2621 : 2 : qof_instance_set_dirty(QOF_INSTANCE(trans));
2622 : 2 : xaccTransCommitEdit(trans);
2623 : 2 : return trans;
2624 : : }
2625 : :
2626 : : Transaction *
2627 : 1 : xaccTransGetReversedBy(const Transaction *trans)
2628 : : {
2629 : 1 : g_return_val_if_fail(trans, nullptr);
2630 : 2 : auto g = qof_instance_get_path_kvp<GncGUID*> (QOF_INSTANCE(trans), {TRANS_REVERSED_BY});
2631 : 1 : return g ? xaccTransLookup (*g, qof_instance_get_book (trans)) : nullptr;
2632 : : }
2633 : :
2634 : : /* ============================================================== */
2635 : : /** The xaccTransScrubGainsDate() routine is used to keep the posted date
2636 : : * of gains splits in sync with the posted date of the transaction
2637 : : * that caused the gains.
2638 : : *
2639 : : * The posted date is kept in sync using a lazy-evaluation scheme.
2640 : : * If xaccTransactionSetDatePosted() is called, the date change is
2641 : : * accepted, and the split is marked date-dirty. If the posted date
2642 : : * is queried for (using GetDatePosted()), then the transaction is
2643 : : * evaluated. If it's a gains-transaction, then its date is copied
2644 : : * from the source transaction that created the gains.
2645 : : */
2646 : :
2647 : : static void
2648 : 3 : xaccTransScrubGainsDate (Transaction *trans)
2649 : : {
2650 : : SplitList *node;
2651 : 3 : SplitList *splits_copy = g_list_copy(trans->splits);
2652 : 9 : for (node = splits_copy; node; node = node->next)
2653 : : {
2654 : 6 : Split *s = GNC_SPLIT(node->data);
2655 : :
2656 : 6 : if (!xaccTransStillHasSplit(trans, s)) continue;
2657 : 6 : xaccSplitDetermineGainStatus(s);
2658 : :
2659 : 6 : if ((GAINS_STATUS_GAINS & s->gains) &&
2660 : 3 : s->gains_split &&
2661 : 3 : ((s->gains_split->gains & GAINS_STATUS_DATE_DIRTY) ||
2662 : 2 : (s->gains & GAINS_STATUS_DATE_DIRTY)))
2663 : : {
2664 : 2 : Transaction *source_trans = s->gains_split->parent;
2665 : 2 : s->gains &= ~GAINS_STATUS_DATE_DIRTY;
2666 : 2 : s->gains_split->gains &= ~GAINS_STATUS_DATE_DIRTY;
2667 : 2 : xaccTransSetDatePostedSecs(trans, source_trans->date_posted);
2668 : 6 : FOR_EACH_SPLIT(trans, s->gains &= ~GAINS_STATUS_DATE_DIRTY);
2669 : : }
2670 : : }
2671 : 3 : g_list_free(splits_copy);
2672 : 3 : }
2673 : :
2674 : : /* ============================================================== */
2675 : :
2676 : : void
2677 : 0 : xaccTransScrubGains (Transaction *trans, Account *gain_acc)
2678 : : {
2679 : : SplitList *node;
2680 : :
2681 : 0 : ENTER("(trans=%p)", trans);
2682 : : /* Lock down posted date, its to be synced to the posted date
2683 : : * for the source of the cap gains. */
2684 : 0 : xaccTransScrubGainsDate(trans);
2685 : :
2686 : : /* Fix up the split amount */
2687 : 0 : restart:
2688 : 0 : for (node = trans->splits; node; node = node->next)
2689 : : {
2690 : 0 : Split *s = GNC_SPLIT(node->data);
2691 : :
2692 : 0 : if (!xaccTransStillHasSplit(trans, s)) continue;
2693 : :
2694 : 0 : xaccSplitDetermineGainStatus(s);
2695 : 0 : if (s->gains & GAINS_STATUS_ADIRTY)
2696 : : {
2697 : 0 : gboolean altered = FALSE;
2698 : 0 : s->gains &= ~GAINS_STATUS_ADIRTY;
2699 : 0 : if (s->lot)
2700 : 0 : altered = xaccScrubLot(s->lot);
2701 : : else
2702 : 0 : altered = xaccSplitAssign(s);
2703 : 0 : if (altered) goto restart;
2704 : : }
2705 : : }
2706 : :
2707 : : /* Fix up gains split value */
2708 : 0 : FOR_EACH_SPLIT(trans,
2709 : : if ((s->gains & GAINS_STATUS_VDIRTY) ||
2710 : : (s->gains_split &&
2711 : : (s->gains_split->gains & GAINS_STATUS_VDIRTY)))
2712 : : xaccSplitComputeCapGains(s, gain_acc);
2713 : : );
2714 : :
2715 : 0 : LEAVE("(trans=%p)", trans);
2716 : 0 : }
2717 : :
2718 : : Split *
2719 : 79 : xaccTransFindSplitByAccount(const Transaction *trans, const Account *acc)
2720 : : {
2721 : 79 : if (!trans || !acc) return nullptr;
2722 : 212 : FOR_EACH_SPLIT(trans, if (xaccSplitGetAccount(s) == acc) return s);
2723 : 56 : return nullptr;
2724 : : }
2725 : :
2726 : : static void
2727 : 0 : record_price (Split *split,
2728 : : PriceSource source)
2729 : : {
2730 : : Transaction *trans;
2731 : : Account *account;
2732 : : QofBook* book;
2733 : : GNCPriceDB* pricedb;
2734 : : gnc_commodity* comm;
2735 : : gnc_commodity* curr;
2736 : : GNCPrice* price;
2737 : : gnc_numeric price_value, value, amount;
2738 : : int scu;
2739 : : time64 time;
2740 : : gboolean swap;
2741 : :
2742 : 0 : account = xaccSplitGetAccount (split);
2743 : 0 : if (!xaccAccountIsPriced (account))
2744 : : {
2745 : 0 : return;
2746 : : }
2747 : 0 : amount = xaccSplitGetAmount (split);
2748 : 0 : if (gnc_numeric_zero_p (amount))
2749 : : {
2750 : 0 : return;
2751 : : }
2752 : 0 : trans = xaccSplitGetParent (split);
2753 : 0 : value = gnc_numeric_div (xaccSplitGetValue (split), amount,
2754 : : GNC_DENOM_AUTO,
2755 : : GNC_HOW_DENOM_EXACT);
2756 : 0 : book = qof_instance_get_book (QOF_INSTANCE (account));
2757 : 0 : pricedb = gnc_pricedb_get_db (book);
2758 : 0 : comm = xaccAccountGetCommodity (account);
2759 : 0 : curr = xaccTransGetCurrency (trans);
2760 : 0 : scu = gnc_commodity_get_fraction (curr);
2761 : 0 : swap = FALSE;
2762 : 0 : time = xaccTransGetDate (trans);
2763 : 0 : price = gnc_pricedb_lookup_day_t64 (pricedb, comm, curr, time);
2764 : 0 : if (gnc_commodity_equiv (comm, gnc_price_get_currency (price)))
2765 : 0 : swap = TRUE;
2766 : :
2767 : 0 : if (price)
2768 : : {
2769 : 0 : PriceSource oldsource = gnc_price_get_source (price);
2770 : 0 : price_value = gnc_price_get_value (price);
2771 : 0 : if (gnc_numeric_equal (swap ? gnc_numeric_invert (value) : value,
2772 : : price_value))
2773 : : {
2774 : 0 : gnc_price_unref (price);
2775 : 0 : return;
2776 : : }
2777 : 0 : if (oldsource < source &&
2778 : 0 : !(oldsource == PRICE_SOURCE_XFER_DLG_VAL &&
2779 : : source == PRICE_SOURCE_SPLIT_REG))
2780 : : {
2781 : : /* Existing price is preferred over this one. */
2782 : 0 : gnc_price_unref (price);
2783 : 0 : return;
2784 : : }
2785 : 0 : if (swap)
2786 : : {
2787 : 0 : value = gnc_numeric_invert (value);
2788 : 0 : scu = gnc_commodity_get_fraction (comm);
2789 : : }
2790 : 0 : value = gnc_numeric_convert (value, scu * COMMODITY_DENOM_MULT,
2791 : : GNC_HOW_RND_ROUND_HALF_UP);
2792 : 0 : gnc_price_begin_edit (price);
2793 : 0 : gnc_price_set_time64 (price, time);
2794 : 0 : gnc_price_set_source (price, source);
2795 : 0 : gnc_price_set_typestr (price, PRICE_TYPE_TRN);
2796 : 0 : gnc_price_set_value (price, value);
2797 : 0 : gnc_price_commit_edit (price);
2798 : 0 : gnc_price_unref (price);
2799 : 0 : return;
2800 : : }
2801 : :
2802 : 0 : value = gnc_numeric_convert (value, scu * COMMODITY_DENOM_MULT,
2803 : : GNC_HOW_RND_ROUND_HALF_UP);
2804 : 0 : price = gnc_price_create (book);
2805 : 0 : gnc_price_begin_edit (price);
2806 : 0 : gnc_price_set_commodity (price, comm);
2807 : 0 : gnc_price_set_currency (price, curr);
2808 : 0 : gnc_price_set_time64 (price, time);
2809 : 0 : gnc_price_set_source (price, source);
2810 : 0 : gnc_price_set_typestr (price, PRICE_TYPE_TRN);
2811 : 0 : gnc_price_set_value (price, value);
2812 : 0 : gnc_pricedb_add_price (pricedb, price);
2813 : 0 : gnc_price_commit_edit (price);
2814 : : }
2815 : :
2816 : : void
2817 : 0 : xaccTransRecordPrice (Transaction *trans, PriceSource source)
2818 : : {
2819 : : /* XXX: This should have been part of xaccSplitCommitEdit. */
2820 : 0 : g_list_foreach (xaccTransGetSplitList (trans), (GFunc)record_price, (gpointer)source);
2821 : 0 : }
2822 : :
2823 : : /********************************************************************\
2824 : : \********************************************************************/
2825 : : /* QofObject function implementation */
2826 : :
2827 : : static void
2828 : 2282 : destroy_tx_on_book_close(QofInstance *ent, gpointer data)
2829 : : {
2830 : 2282 : Transaction* tx = GNC_TRANSACTION(ent);
2831 : :
2832 : 2282 : xaccTransDestroy(tx);
2833 : 2282 : }
2834 : :
2835 : : static int
2836 : 10531 : trans_reverse_order (const Transaction* a, const Transaction* b)
2837 : : {
2838 : 10531 : return xaccTransOrder (b, a);
2839 : : }
2840 : :
2841 : : /** Handles book end - frees all transactions from the book
2842 : : *
2843 : : * @param book Book being closed
2844 : : */
2845 : : static void
2846 : 177 : gnc_transaction_book_end(QofBook* book)
2847 : : {
2848 : : QofCollection *col;
2849 : :
2850 : 177 : col = qof_book_get_collection(book, GNC_ID_TRANS);
2851 : :
2852 : : // destroy all transactions from latest to earliest, because
2853 : : // accounts' splits are stored chronologically and removing from
2854 : : // the end is faster than from the middle.
2855 : 177 : qof_collection_foreach_sorted (col, destroy_tx_on_book_close, nullptr,
2856 : : (GCompareFunc)trans_reverse_order);
2857 : 177 : }
2858 : :
2859 : : #ifdef _MSC_VER
2860 : : /* MSVC compiler doesn't have C99 "designated initializers"
2861 : : * so we wrap them in a macro that is empty on MSVC. */
2862 : : # define DI(x) /* */
2863 : : #else
2864 : : # define DI(x) x
2865 : : #endif
2866 : :
2867 : : /* Hook into the QofObject registry */
2868 : : static QofObject trans_object_def =
2869 : : {
2870 : : DI(.interface_version = ) QOF_OBJECT_VERSION,
2871 : : DI(.e_type = ) GNC_ID_TRANS,
2872 : : DI(.type_label = ) "Transaction",
2873 : : DI(.create = ) (void* (*)(QofBook*))xaccMallocTransaction,
2874 : : DI(.book_begin = ) nullptr,
2875 : : DI(.book_end = ) gnc_transaction_book_end,
2876 : : DI(.is_dirty = ) qof_collection_is_dirty,
2877 : : DI(.mark_clean = ) qof_collection_mark_clean,
2878 : : DI(.foreach = ) qof_collection_foreach,
2879 : : DI(.printable = ) (const char * (*)(gpointer)) xaccTransGetDescription,
2880 : : DI(.version_cmp = ) (int (*)(gpointer, gpointer)) qof_instance_version_cmp,
2881 : : };
2882 : :
2883 : : static gboolean
2884 : 0 : trans_is_balanced_p (const Transaction *trans)
2885 : : {
2886 : 0 : return trans ? xaccTransIsBalanced(trans) : FALSE;
2887 : : }
2888 : :
2889 : 83 : gboolean xaccTransRegister (void)
2890 : : {
2891 : : static QofParam params[] =
2892 : : {
2893 : : {
2894 : : TRANS_NUM, QOF_TYPE_STRING,
2895 : : (QofAccessFunc)xaccTransGetNum,
2896 : : (QofSetterFunc)qofTransSetNum,
2897 : : qof_string_number_compare_func
2898 : : },
2899 : : {
2900 : : TRANS_DESCRIPTION, QOF_TYPE_STRING,
2901 : : (QofAccessFunc)xaccTransGetDescription,
2902 : : (QofSetterFunc)qofTransSetDescription
2903 : : },
2904 : : {
2905 : : TRANS_DATE_ENTERED, QOF_TYPE_DATE,
2906 : : (QofAccessFunc)xaccTransRetDateEntered,
2907 : : (QofSetterFunc)xaccTransSetDateEnteredSecs
2908 : : },
2909 : : {
2910 : : TRANS_DATE_POSTED, QOF_TYPE_DATE,
2911 : : (QofAccessFunc)xaccTransRetDatePosted,
2912 : : (QofSetterFunc)xaccTransSetDatePostedSecs
2913 : : },
2914 : : {
2915 : : TRANS_DATE_DUE, QOF_TYPE_DATE,
2916 : : (QofAccessFunc)xaccTransRetDateDue, nullptr
2917 : : },
2918 : : {
2919 : : TRANS_IMBALANCE, QOF_TYPE_NUMERIC,
2920 : : (QofAccessFunc)xaccTransGetImbalanceValue, nullptr
2921 : : },
2922 : : {
2923 : : TRANS_NOTES, QOF_TYPE_STRING,
2924 : : (QofAccessFunc)xaccTransGetNotes,
2925 : : (QofSetterFunc)qofTransSetNotes
2926 : : },
2927 : : {
2928 : : TRANS_DOCLINK, QOF_TYPE_STRING,
2929 : : (QofAccessFunc)xaccTransGetDocLink,
2930 : : (QofSetterFunc)xaccTransSetDocLink
2931 : : },
2932 : : {
2933 : : TRANS_IS_CLOSING, QOF_TYPE_BOOLEAN,
2934 : : (QofAccessFunc)xaccTransGetIsClosingTxn, nullptr
2935 : : },
2936 : : {
2937 : : TRANS_IS_BALANCED, QOF_TYPE_BOOLEAN,
2938 : : (QofAccessFunc)trans_is_balanced_p, nullptr
2939 : : },
2940 : : {
2941 : : TRANS_TYPE, QOF_TYPE_CHAR,
2942 : : (QofAccessFunc)xaccTransGetTxnType,
2943 : : (QofSetterFunc)xaccTransSetTxnType
2944 : : },
2945 : : {
2946 : : TRANS_VOID_STATUS, QOF_TYPE_BOOLEAN,
2947 : : (QofAccessFunc)xaccTransGetVoidStatus, nullptr
2948 : : },
2949 : : {
2950 : : TRANS_VOID_REASON, QOF_TYPE_STRING,
2951 : : (QofAccessFunc)xaccTransGetVoidReason, nullptr
2952 : : },
2953 : : {
2954 : : TRANS_VOID_TIME, QOF_TYPE_DATE,
2955 : : (QofAccessFunc)xaccTransGetVoidTime, nullptr
2956 : : },
2957 : : {
2958 : : TRANS_SPLITLIST, GNC_ID_SPLIT,
2959 : : (QofAccessFunc)xaccTransGetSplitList, nullptr
2960 : : },
2961 : : {
2962 : : QOF_PARAM_BOOK, QOF_ID_BOOK,
2963 : : (QofAccessFunc)qof_instance_get_book, nullptr
2964 : : },
2965 : : {
2966 : : QOF_PARAM_GUID, QOF_TYPE_GUID,
2967 : : (QofAccessFunc)qof_entity_get_guid, nullptr
2968 : : },
2969 : : { nullptr },
2970 : : };
2971 : :
2972 : 83 : qof_class_register (GNC_ID_TRANS, (QofSortFunc)xaccTransOrder, params);
2973 : :
2974 : 83 : return qof_object_register (&trans_object_def);
2975 : : }
2976 : :
2977 : : TransTestFunctions*
2978 : 43 : _utest_trans_fill_functions (void)
2979 : : {
2980 : 43 : TransTestFunctions *func = g_new (TransTestFunctions, 1);
2981 : :
2982 : 43 : func->mark_trans = mark_trans;
2983 : 43 : func->gen_event_trans = gen_event_trans;
2984 : 43 : func->xaccFreeTransaction = xaccFreeTransaction;
2985 : 43 : func->destroy_gains = destroy_gains;
2986 : 43 : func->do_destroy = do_destroy;
2987 : 43 : func->was_trans_emptied = was_trans_emptied;
2988 : 43 : func->trans_on_error = trans_on_error;
2989 : 43 : func->trans_cleanup_commit = trans_cleanup_commit;
2990 : 43 : func->xaccTransScrubGainsDate = xaccTransScrubGainsDate;
2991 : 43 : func->dupe_trans = dupe_trans;
2992 : 43 : return func;
2993 : : }
2994 : :
2995 : : /************************ END OF ************************************\
2996 : : \************************* FILE *************************************/
|