1 /*******************************************************
2  Copyright (C) 2013,2014 James Higley
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  ********************************************************/
18 
19 #include "Model_Budgetsplittransaction.h"
20 
Model_Budgetsplittransaction()21 Model_Budgetsplittransaction::Model_Budgetsplittransaction()
22 : Model<DB_Table_BUDGETSPLITTRANSACTIONS_V1>()
23 {
24 }
25 
~Model_Budgetsplittransaction()26 Model_Budgetsplittransaction::~Model_Budgetsplittransaction()
27 {
28 }
29 
30 /**
31 * Initialize the global Model_Budgetsplittransaction table.
32 * Reset the Model_Budgetsplittransaction table or create the table if it does not exist.
33 */
instance(wxSQLite3Database * db)34 Model_Budgetsplittransaction& Model_Budgetsplittransaction::instance(wxSQLite3Database* db)
35 {
36     Model_Budgetsplittransaction& ins = Singleton<Model_Budgetsplittransaction>::instance();
37     ins.db_ = db;
38     ins.destroy_cache();
39     ins.ensure(db);
40 
41     return ins;
42 }
43 
44 /** Return the static instance of Model_Budgetsplittransaction table */
instance()45 Model_Budgetsplittransaction& Model_Budgetsplittransaction::instance()
46 {
47     return Singleton<Model_Budgetsplittransaction>::instance();
48 }
49 
get_total(const Data_Set & rows)50 double Model_Budgetsplittransaction::get_total(const Data_Set& rows)
51 {
52     double total = 0.0;
53     for (auto& r : rows) total += r.SPLITTRANSAMOUNT;
54 
55     return total;
56 }
57 
get_all()58 std::map<int, Model_Budgetsplittransaction::Data_Set> Model_Budgetsplittransaction::get_all()
59 {
60     std::map<int, Model_Budgetsplittransaction::Data_Set> data;
61     for (const auto & split : instance().all())
62     {
63         data[split.TRANSID].push_back(split);
64     }
65 
66     return data;
67 }
68 
update(const Data_Set & rows,int transactionID)69 int Model_Budgetsplittransaction::update(const Data_Set& rows, int transactionID)
70 {
71 
72     Data_Set split = instance().find(TRANSID(transactionID));
73     for (const auto& split_item : split)
74     {
75         instance().remove(split_item.SPLITTRANSID);
76     }
77 
78     if (!rows.empty())
79     {
80         Data_Set split_items;
81         for (const auto &item : rows)
82         {
83             Data *split_item = instance().create();
84             split_item->TRANSID = transactionID;
85             split_item->SPLITTRANSAMOUNT = item.SPLITTRANSAMOUNT;
86             split_item->CATEGID = item.CATEGID;
87             split_item->SUBCATEGID = item.SUBCATEGID;
88             split_items.push_back(*split_item);
89         }
90         instance().save(split_items);
91     }
92     return rows.size();
93 }
94