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