GnuCash c935c2f+
Loading...
Searching...
No Matches
Public Member Functions
GncPreSplit Class Reference

Public Member Functions

 GncPreSplit (int date_format, int currency_format)
 
void set (GncTransPropType prop_type, const std::string &value)
 
void reset (GncTransPropType prop_type)
 
void add (GncTransPropType prop_type, const std::string &value)
 
void set_date_format (int date_format)
 
void set_currency_format (int currency_format)
 
void set_pre_trans (std::shared_ptr< GncPreTrans > pre_trans)
 
std::shared_ptr< GncPreTransget_pre_trans (void)
 
StrVec verify_essentials (void)
 
void create_split (std::shared_ptr< DraftTransaction > draft_trans)
 
Accountget_account ()
 
void set_account (Account *acct)
 
ErrMap errors ()
 

Detailed Description

Definition at line 221 of file gnc-imp-props-tx.hpp.

Constructor & Destructor Documentation

◆ GncPreSplit()

GncPreSplit::GncPreSplit ( int  date_format,
int  currency_format 
)
inline

Definition at line 224 of file gnc-imp-props-tx.hpp.

224 : m_date_format{date_format},
225 m_currency_format{currency_format} {};

Member Function Documentation

◆ add()

void GncPreSplit::add ( GncTransPropType  prop_type,
const std::string &  value 
)

Definition at line 572 of file gnc-imp-props-tx.cpp.

573{
574 try
575 {
576 /* Don't try to add to a property that has an error already */
577 if (m_errors.find(prop_type) != m_errors.cend())
578 return;
579
580 auto num_val = GncNumeric();
581 switch (prop_type)
582 {
583 case GncTransPropType::AMOUNT:
584 num_val = parse_monetary (value, m_currency_format); // Will throw if parsing fails
585 if (m_amount)
586 num_val += *m_amount;
587 m_amount = num_val;
588 break;
589
590 case GncTransPropType::AMOUNT_NEG:
591 num_val = parse_monetary (value, m_currency_format); // Will throw if parsing fails
592 if (m_amount_neg)
593 num_val += *m_amount_neg;
594 m_amount_neg = num_val;
595 break;
596
597 case GncTransPropType::VALUE:
598 num_val = parse_monetary (value, m_currency_format); // Will throw if parsing fails
599 if (m_value)
600 num_val += *m_value;
601 m_value = num_val;
602 break;
603
604 case GncTransPropType::VALUE_NEG:
605 num_val = parse_monetary (value, m_currency_format); // Will throw if parsing fails
606 if (m_value_neg)
607 num_val += *m_value_neg;
608 m_value_neg = num_val;
609 break;
610
611 case GncTransPropType::TAMOUNT:
612 num_val = parse_monetary (value, m_currency_format); // Will throw if parsing fails
613 if (m_tamount)
614 num_val += *m_tamount;
615 m_tamount = num_val;
616 break;
617
618 case GncTransPropType::TAMOUNT_NEG:
619 num_val = parse_monetary (value, m_currency_format); // Will throw if parsing fails
620 if (m_tamount_neg)
621 num_val += *m_tamount_neg;
622 m_tamount_neg = num_val;
623 break;
624
625 default:
626 /* Issue a warning for all other prop_types. */
627 PWARN ("%d can't be used to add values in a split", static_cast<int>(prop_type));
628 break;
629 }
630 }
631 catch (const std::exception& e)
632 {
633 auto err_str = (bl::format (std::string{_("{1}: {2}")}) %
634 std::string{_(gnc_csv_col_type_strs[prop_type])} %
635 e.what()).str();
636 m_errors.emplace(prop_type, err_str);
637 }
638}
The primary numeric class for representing amounts and values.
#define PWARN(format, args...)
Log a warning.
Definition qoflog.h:250

◆ create_split()

void GncPreSplit::create_split ( std::shared_ptr< DraftTransaction draft_trans)

Definition at line 712 of file gnc-imp-props-tx.cpp.

713{
714 if (created)
715 return;
716
717 /* Gently refuse to create the split if the basics are not set correctly
718 * This should have been tested before calling this function though!
719 */
720 auto check = verify_essentials();
721 if (!check.empty())
722 {
723 auto err_msg = std::string("Not creating split because essentials not set properly:");
724 auto add_bullet_item = [](std::string& a, std::string& b)->std::string { return std::move(a) + "\n• " + b; };
725 err_msg = std::accumulate (check.begin(), check.end(), std::move (err_msg), add_bullet_item);
726 PWARN ("%s", err_msg.c_str());
727 return;
728 }
729
730 auto splits_created = 0;
731 Account *account = nullptr;
732 Account *taccount = nullptr;
733 auto amount = GncNumeric();
734
735 if (m_account)
736 account = *m_account;
737 if (m_taccount)
738 taccount = *m_taccount;
739 if (m_amount)
740 amount += *m_amount;
741 if (m_amount_neg)
742 amount -= *m_amount_neg;
743
744 std::optional<GncNumeric> tamount;
745 if (m_tamount || m_tamount_neg)
746 {
747 tamount = GncNumeric();
748 if (m_tamount)
749 *tamount += *m_tamount;
750 if (m_tamount_neg)
751 *tamount -= *m_tamount_neg;
752 }
753
754 /* Value can be calculated in several ways, depending on what
755 * data was available in the csv import file.
756 * Below code will prefer the method with the least
757 * risk on rounding errors.
758 * */
759 auto value = GncNumeric();
760 auto trans_curr = xaccTransGetCurrency(draft_trans->trans);
761 auto acct_comm = xaccAccountGetCommodity(account);
762 if (m_value || m_value_neg)
763 {
764 if (m_value)
765 value += *m_value;
766 if (m_value_neg)
767 value -= *m_value_neg;
768 }
769 else if (gnc_commodity_equiv(trans_curr, acct_comm))
770 value = amount;
771 else if (tamount)
772 value = -*tamount;
773 else if (m_price)
774 value = amount * *m_price;
775 else
776 {
777 QofBook* book = xaccTransGetBook (draft_trans->trans);
778 auto time = xaccTransRetDatePosted (draft_trans->trans);
779 /* Import data didn't specify price, let's lookup the nearest in time */
780 auto nprice =
782 acct_comm, trans_curr, time);
783 GncNumeric rate = nprice ? gnc_price_get_value (nprice): gnc_numeric_zero();
784 if (!gnc_numeric_zero_p (rate))
785 {
786 /* Found a usable price. Let's check if the conversion direction is right
787 * Reminder: value = amount * price, or amount = value / price */
788 if (gnc_commodity_equiv(gnc_price_get_currency(nprice), trans_curr))
789 value = amount * rate;
790 else
791 value = amount * rate.inv();
792 }
793 else
794 PERR("No price found, can't create this split.");
795 }
796
797 /* Add a split with the cumulative amount value. */
798 trans_add_split (draft_trans->trans, account, amount, value, m_action, m_memo, m_rec_state, m_rec_date);
799 splits_created++;
800
801 if (taccount)
802 {
803 /* If a taccount is set that forcibly means we're processing a single-line
804 * transaction. The csv importer will assume this can only create a
805 * two-split transaction, so whatever transfer data is available, the
806 * transfer split's value must balance the first split value. Remains
807 * to determine: the transfer amount. As with value above, for single
808 * currency case use transfer value. Otherwise calculate from whatever
809 * is found in the csv data preferring minimal rounding calculations. */
810 auto tvalue = -value;
811 auto trans_curr = xaccTransGetCurrency(draft_trans->trans);
812 auto acct_comm = xaccAccountGetCommodity(taccount);
813 if (gnc_commodity_equiv(trans_curr, acct_comm))
814 tamount = tvalue;
815 else if (tamount)
816 ; // Nothing to do, was already calculated
817 else if (m_price)
818 tamount = tvalue * m_price->inv();
819 else
820 {
821 QofBook* book = xaccTransGetBook (draft_trans->trans);
822 auto time = xaccTransRetDatePosted (draft_trans->trans);
823 /* Import data didn't specify price, let's lookup the nearest in time */
824 auto nprice =
826 acct_comm, trans_curr, time);
827 GncNumeric rate = nprice ? gnc_price_get_value (nprice): gnc_numeric_zero();
828 if (!gnc_numeric_zero_p (rate))
829 {
830 /* Found a usable price. Let's check if the conversion direction is right
831 * Reminder: value = amount * price, or amount = value / price */
832 if (gnc_commodity_equiv(gnc_price_get_currency(nprice), trans_curr))
833 tamount = tvalue * rate.inv();
834 else
835 tamount = tvalue * rate;
836 }
837 }
838 if (tamount)
839 {
840 trans_add_split (draft_trans->trans, taccount, *tamount, tvalue, m_taction, m_tmemo, m_trec_state, m_trec_date);
841 splits_created++;
842 }
843 else
844 PWARN("No price found, defer creation of second split to generic import matcher.");
845 }
846
847 if (splits_created == 1)
848 {
849 /* If we get here, we're either
850 * - in multi-line mode
851 * - or single-line mode but didn't have enough details to create the
852 * transfer split.
853 * For the latter we will pass what we know about the transfer split to
854 * allow the generic import matcher to ask the user for the final
855 * details before creating this split.
856 */
857 draft_trans->m_price = m_price;
858 draft_trans->m_taction = m_taction;
859 draft_trans->m_tmemo = m_tmemo;
860 draft_trans->m_tamount = tamount;
861 draft_trans->m_taccount = m_taccount;
862 draft_trans->m_trec_state = m_trec_state;
863 draft_trans->m_trec_date = m_trec_date;
864 }
865
866 created = true;
867}
GncNumeric inv() const noexcept
gnc_commodity * xaccAccountGetCommodity(const Account *acc)
Get the account's commodity
Definition Account.cpp:3408
gboolean gnc_commodity_equiv(const gnc_commodity *a, const gnc_commodity *b)
This routine returns TRUE if the two commodities are equivalent.
gnc_commodity * xaccTransGetCurrency(const Transaction *trans)
Returns the valuation commodity of this transaction.
time64 xaccTransRetDatePosted(const Transaction *trans)
Retrieve the posted date of the transaction.
#define xaccTransGetBook(X)
#define PERR(format, args...)
Log a serious error.
Definition qoflog.h:244
gboolean gnc_numeric_zero_p(gnc_numeric a)
Returns 1 if the given gnc_numeric is 0 (zero), else returns 0.
GNCPrice * gnc_pricedb_lookup_nearest_in_time64(GNCPriceDB *db, const gnc_commodity *c, const gnc_commodity *currency, time64 t)
Return the price between the two commoditiesz nearest to the given time.
GNCPriceDB * gnc_pricedb_get_db(QofBook *book)
Return the pricedb associated with the book.
STRUCTS.
QofBook reference.
Definition qofbook-p.hpp:47

◆ errors()

ErrMap GncPreSplit::errors ( void  )

Definition at line 869 of file gnc-imp-props-tx.cpp.

870{
871 return m_errors;
872}

◆ get_account()

Account * GncPreSplit::get_account ( )
inline

Definition at line 236 of file gnc-imp-props-tx.hpp.

236{ if (m_account) return *m_account; else return nullptr; }

◆ get_pre_trans()

std::shared_ptr< GncPreTrans > GncPreSplit::get_pre_trans ( void  )
inline

Definition at line 232 of file gnc-imp-props-tx.hpp.

232{ return m_pre_trans; }

◆ reset()

void GncPreSplit::reset ( GncTransPropType  prop_type)

Definition at line 564 of file gnc-imp-props-tx.cpp.

565{
566 set (prop_type, std::string());
567 // Set with an empty string will effectively clear the property
568 // but can also set an error for the property. Clear that error here.
569 m_errors.erase(prop_type);
570}

◆ set()

void GncPreSplit::set ( GncTransPropType  prop_type,
const std::string &  value 
)

Definition at line 426 of file gnc-imp-props-tx.cpp.

427{
428 try
429 {
430 // Drop any existing error for the prop_type we're about to set
431 m_errors.erase(prop_type);
432
433 Account *acct = nullptr;
434 switch (prop_type)
435 {
436 case GncTransPropType::ACTION:
437 m_action.reset();
438 if (!value.empty())
439 m_action = value;
440 break;
441
442 case GncTransPropType::TACTION:
443 m_taction.reset();
444 if (!value.empty())
445 m_taction = value;
446 break;
447
448 case GncTransPropType::ACCOUNT:
449 m_account.reset();
450 if (value.empty())
451 throw std::invalid_argument (_("Account value can't be empty."));
452 if ((acct = gnc_account_imap_find_any (gnc_get_current_book(), IMAP_CAT_CSV, value.c_str())) ||
453 (acct = gnc_account_lookup_by_full_name (gnc_get_current_root_account(), value.c_str())))
454 m_account = acct;
455 else
456 throw std::invalid_argument (_("Account value can't be mapped back to an account."));
457 break;
458
459 case GncTransPropType::TACCOUNT:
460 m_taccount.reset();
461 if (value.empty())
462 throw std::invalid_argument (_("Transfer account value can't be empty."));
463
464 if ((acct = gnc_account_imap_find_any (gnc_get_current_book(), IMAP_CAT_CSV,value.c_str())) ||
465 (acct = gnc_account_lookup_by_full_name (gnc_get_current_root_account(), value.c_str())))
466 m_taccount = acct;
467 else
468 throw std::invalid_argument (_("Transfer account value can't be mapped back to an account."));
469 break;
470
471 case GncTransPropType::MEMO:
472 m_memo.reset();
473 if (!value.empty())
474 m_memo = value;
475 break;
476
477 case GncTransPropType::TMEMO:
478 m_tmemo.reset();
479 if (!value.empty())
480 m_tmemo = value;
481 break;
482
483 case GncTransPropType::AMOUNT:
484 m_amount.reset();
485 m_amount = parse_monetary (value, m_currency_format); // Will throw if parsing fails
486 break;
487
488 case GncTransPropType::AMOUNT_NEG:
489 m_amount_neg.reset();
490 m_amount_neg = parse_monetary (value, m_currency_format); // Will throw if parsing fails
491 break;
492
493 case GncTransPropType::VALUE:
494 m_value.reset();
495 m_value = parse_monetary (value, m_currency_format); // Will throw if parsing fails
496 break;
497
498 case GncTransPropType::VALUE_NEG:
499 m_value_neg.reset();
500 m_value_neg = parse_monetary (value, m_currency_format); // Will throw if parsing fails
501 break;
502
503 case GncTransPropType::TAMOUNT:
504 m_tamount.reset();
505 m_tamount = parse_monetary (value, m_currency_format); // Will throw if parsing fails
506 break;
507
508 case GncTransPropType::TAMOUNT_NEG:
509 m_tamount_neg.reset();
510 m_tamount_neg = parse_monetary (value, m_currency_format); // Will throw if parsing fails
511 break;
512
513 case GncTransPropType::PRICE:
514 /* Note while a price is not stricly a currency, it will likely use
515 * the same decimal point as currencies in the csv file, so parse
516 * using the same parser */
517 m_price.reset();
518 m_price = parse_monetary (value, m_currency_format); // Will throw if parsing fails
519 break;
520
521 case GncTransPropType::REC_STATE:
522 m_rec_state.reset();
523 m_rec_state = parse_reconciled (value); // Throws if parsing fails
524 break;
525
526 case GncTransPropType::TREC_STATE:
527 m_trec_state.reset();
528 m_trec_state = parse_reconciled (value); // Throws if parsing fails
529 break;
530
531 case GncTransPropType::REC_DATE:
532 m_rec_date.reset();
533 if (!value.empty())
534 m_rec_date = GncDate (value,
535 GncDate::c_formats[m_date_format].m_fmt); // Throws if parsing fails
536 break;
537
538 case GncTransPropType::TREC_DATE:
539 m_trec_date.reset();
540 if (!value.empty())
541 m_trec_date = GncDate (value,
542 GncDate::c_formats[m_date_format].m_fmt); // Throws if parsing fails
543 break;
544
545 default:
546 /* Issue a warning for all other prop_types. */
547 PWARN ("%d is an invalid property for a split", static_cast<int>(prop_type));
548 break;
549 }
550 }
551 catch (const std::exception& e)
552 {
553 auto err_str = (bl::format (std::string{_("{1}: {2}")}) %
554 std::string{_(gnc_csv_col_type_strs[prop_type])} %
555 e.what()).str();
556 m_errors.emplace(prop_type, err_str);
557 }
558
559 /* Extra currency related postprocessing for account type */
560 if (prop_type == GncTransPropType::ACCOUNT)
561 UpdateCrossSplitCounters();
562}
GnuCash Date class.
static const std::vector< GncDateFormat > c_formats
A vector with all the date formats supported by the string constructor.
Account * gnc_account_lookup_by_full_name(const Account *any_acc, const gchar *name)
The gnc_account_lookup_full_name() subroutine works like gnc_account_lookup_by_name,...
Definition Account.cpp:3163

◆ set_account()

void GncPreSplit::set_account ( Account acct)

Definition at line 875 of file gnc-imp-props-tx.cpp.

876{
877 if (acct)
878 m_account = acct;
879 else
880 m_account.reset();
881
882 UpdateCrossSplitCounters();
883}

◆ set_currency_format()

void GncPreSplit::set_currency_format ( int  currency_format)
inline

Definition at line 230 of file gnc-imp-props-tx.hpp.

230{ m_currency_format = currency_format; }

◆ set_date_format()

void GncPreSplit::set_date_format ( int  date_format)
inline

Definition at line 229 of file gnc-imp-props-tx.hpp.

229{ m_date_format = date_format ;}

◆ set_pre_trans()

void GncPreSplit::set_pre_trans ( std::shared_ptr< GncPreTrans pre_trans)
inline

Definition at line 231 of file gnc-imp-props-tx.hpp.

231{ m_pre_trans = pre_trans; }

◆ verify_essentials()

StrVec GncPreSplit::verify_essentials ( void  )

Definition at line 640 of file gnc-imp-props-tx.cpp.

641{
642 auto err_msg = StrVec();
643 /* Make sure this split has the minimum required set of properties defined. */
644 if (!m_amount && !m_amount_neg)
645 err_msg.emplace_back (_("No amount or negated amount column."));
646
647 if (m_rec_state && *m_rec_state == YREC && !m_rec_date)
648 err_msg.emplace_back (_("Split is reconciled but reconcile date column is missing or invalid."));
649
650 if (m_trec_state && *m_trec_state == YREC && !m_trec_date)
651 err_msg.emplace_back (_("Transfer split is reconciled but transfer reconcile date column is missing or invalid."));
652
653
654 /* When current account selections imply multi-currency
655 * transactions, we require extra columns to ensure each split is
656 * fully defined.
657 * Note this check only involves splits created by the csv importer
658 * code. The generic import matcher may add a balancing split
659 * optionally using Transfer <something> properties. The generic
660 * import matcher has its own tools to balance that split so
661 * we won't concern ourselves with that one here.
662 */
663 if (m_pre_trans->is_multi_currency())
664 {
665 if (m_pre_trans->m_multi_split && !m_price && !m_value && !m_value_neg)
666 err_msg.emplace_back( _("Choice of accounts makes this a multi-currency transaction but price or (negated) value column is missing or invalid."));
667 else if (!m_pre_trans->m_multi_split &&
668 !m_price && !m_value && !m_value_neg && !m_tamount && !m_tamount_neg )
669 err_msg.emplace_back( _("Choice of accounts makes this a multi-currency transaction but price, (negated) value or (negated) transfer amount column is missing or invalid."));
670 }
671
672 return err_msg;
673}
#define YREC
The Split has been reconciled.
Definition Split.h:74

The documentation for this class was generated from the following files: