713{
714 if (created)
715 return;
716
717
718
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;
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 {
748 if (m_tamount)
749 *tamount += *m_tamount;
750 if (m_tamount_neg)
751 *tamount -= *m_tamount_neg;
752 }
753
754
755
756
757
758
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 }
770 value = amount;
771 else if (tamount)
772 value = -*tamount;
773 else if (m_price)
774 value = amount * *m_price;
775 else
776 {
779
780 auto nprice =
782 acct_comm, trans_curr, time);
783 GncNumeric rate = nprice ? gnc_price_get_value (nprice): gnc_numeric_zero();
785 {
786
787
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
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
804
805
806
807
808
809
810 auto tvalue = -value;
814 tamount = tvalue;
815 else if (tamount)
816 ;
817 else if (m_price)
818 tamount = tvalue * m_price->inv();
819 else
820 {
823
824 auto nprice =
826 acct_comm, trans_curr, time);
827 GncNumeric rate = nprice ? gnc_price_get_value (nprice): gnc_numeric_zero();
829 {
830
831
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
850
851
852
853
854
855
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
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.
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.