forked from condo4/carbudget
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcost.cpp
More file actions
125 lines (114 loc) · 3.12 KB
/
cost.cpp
File metadata and controls
125 lines (114 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* CarBudget, Sailfish application to manage car cost
*
* Copyright (C) 2014 Fabien Proriol
*
* This file is part of CarBudget.
*
* CarBudget is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* CarBudget is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the GNU General Public License for more details. You should have received a copy of the GNU
* General Public License along with CarBudget. If not, see <http://www.gnu.org/licenses/>.
*
* Authors: Fabien Proriol
*/
#include "cost.h"
#include <QDebug>
#include <car.h>
Cost::Cost(Car *parent) :
CarEvent(parent),
_description(""),
_cost(0)
{
}
Cost::Cost(QDate date, unsigned int distance, QString desc, double cost, unsigned int id, Car *parent):
CarEvent(date, distance, id, parent),
_description(desc),
_cost(cost)
{
}
QString Cost::description() const
{
return _description;
}
void Cost::setDescription(QString desc)
{
_description = desc;
emit descriptionChanged();
}
double Cost::cost() const
{
return _cost;
}
void Cost::setCost(double cost)
{
_cost = cost;
emit costChanged();
}
void Cost::save()
{
if(_eventid == 0)
{
_eventid = saveevent();
if(_eventid)
{
QSqlQuery query(_car->db);
QString sql = QString("INSERT INTO CostList (event,cost,desc) VALUES(%1,%2,'%3')").arg(_eventid).arg(_cost).arg(_description);
if(query.exec(sql))
{
qDebug() << "Create Cost in database with id " << _eventid;
_car->db.commit();
}
else _eventid = 0;
}
if(_eventid == 0)
{
qDebug() << "Error during Create Cost in database";
_car->db.rollback();
}
}
else
{
if(saveevent())
{
QSqlQuery query(_car->db);
QString sql = QString("UPDATE CostList SET cost=%1, desc='%2' WHERE event=%3;").arg(_cost).arg(_description).arg(_eventid);
if(query.exec(sql))
{
qDebug() << "Update Cost in database with id " << _eventid;
_car->db.commit();
}
else
{
qDebug() << "Error during Update Cost in database";
qDebug() << query.lastError();
}
}
else
{
qDebug() << "Error during Update Cost in database";
}
}
}
void Cost::remove()
{
QSqlQuery query(_car->db);
QString sql = QString("DELETE FROM CostList WHERE event=%1;").arg(_eventid);
if(query.exec(sql))
{
if(delevent())
{
qDebug() << "DELETE Cost in database with id " << _eventid;
_car->db.commit();
return;
}
}
qDebug() << "Error during DELETE Cost in database";
qDebug() << query.lastError();
_car->db.rollback();
}