-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_data.php
More file actions
211 lines (203 loc) · 7.69 KB
/
get_data.php
File metadata and controls
211 lines (203 loc) · 7.69 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/**
* Created by PhpStorm.
* User: ilya
* Date: 10.04.2017
* Time: 18:03
*/
$func = $_GET['func'];
session_start();
if (!isset($_SESSION['login'])) {
echo 'login first';
exit;
}
$login = $_SESSION['login'];
$lastseen = $_SESSION['lastseen'];
function getVal($query){
global $db;
$data = mysql_query($query,$db) or trigger_error("Mysql error: ".mysql_error(),E_USER_ERROR);
if ($data) {
return mysql_fetch_array($data)[0];
}else{
return 0;
}
}
class mydate{
public $pdate = '';
public $year;
public $month;
public $day;
public $date;
function __construct($year,$month = 1, $day = 5){
$this->pdate = $year.'-'.$month.'-'.$day;
$this->year = $year;
$this->month = $month;
$this->day = $day;
$this->date = ['year'=>$year,'month'=>$month,'day'=>$day];
}
public function calcDate(){
$date = $this->date;
if ($date['day'] == 20){
$date['day'] = 5;
} else {
$date['day'] = 20;
if ($date['month'] == 1){
$date['year']--;
$date['month'] = 12;
}else{
$date['month']--;
}
}
return new mydate($date['year'],$date['month'],$date['day']);
}
public function changeDay($day){
$this->pdate = $this->year.'-'.$this->month.'-'.$day;
$this->day = $day;
$this->date['day'] = $day;
}
}
function svobodnie($cldate){
$sv['plan_dohod'] = getVal("SELECT sum(summa) FROM dohod_periodic WHERE den='".$cldate->day."'");
$sv['dohod'] = getVal("SELECT sum(summa) FROM dohod WHERE add_date='".$cldate->pdate."'");
$sv['ostalos'] = ostalos($cldate);
$sv['summa'] = $sv['ostalos'] + $sv['plan_dohod'] + $sv['dohod'];
$sv['rashod_periodic'] = getVal("SELECT sum(summa) FROM rashod_periodic WHERE den='".$cldate->day."'");
$sv['rashod'] = getVal("SELECT sum(summa) FROM rashod WHERE add_date='".$cldate->pdate."'");
$sv['svobodnie'] = $sv['summa'] - $sv['rashod_periodic'] - $sv['rashod'];
return $sv;
}
function ostalos($cldate){
$newdate = $cldate->calcDate();
$query = "SELECT summa FROM balance WHERE change_date <= '".$cldate->pdate."' AND change_date > '".$newdate->pdate."' ORDER BY change_date DESC LIMIT 1";
$q_data = mysql_query ($query);
if (mysql_num_rows($q_data) > 0) return mysql_fetch_array($q_data)[0];
$sv = svobodnie($newdate);
return $sv['summa'] - $sv['rashod'] - $sv['rashod_periodic'];
}
function printDohod($cldate){
global $db;
$sv = svobodnie($cldate);
$text = "<span style='text-decoration:underline;'>Доходы</span><br/>
<b>План доход: </b>".$sv['plan_dohod']."<br/>";
$res = mysql_query("SELECT * FROM dohod WHERE add_date='".$cldate->pdate."'",$db);
$i=0;
while ($data = mysql_fetch_assoc($res)){
$i++;
$text .= "<span>".$i.". ".$data['title']." ".$data['summa']." "
."<input class='button edit' actiontype='dohod' type='button' value='изменить' rid='".$data['id']."' date='".$cldate->pdate."' rtitle='".$data['title']."' rsumma='".$data['summa']."'/> "
."<input class='button remove' actiontype='dohod' type='button' value='удалить' rid='".$data['id']."' date='".$cldate->pdate."'/>"
."</span><br/>";
}
$text .= "<input type='button' value='добавить доход' class='button add' actiontype='dohod' date='".$cldate->pdate."'/><br/>
<b>Осталось с пред. месяца: </b>".$sv['ostalos']."<br/>
<b>Сумма: </b>" . $sv['summa']."<br/>
<b>Расходы: </b>".($sv['rashod']+$sv['rashod_periodic'])."<br/>
<b>Свободные деньги: </b>".$sv['svobodnie']."<br/><hr/>";
return $text;
}
function printRashod($cldate){
$text = "";
global $db;
$res = mysql_query("SELECT * FROM rashod_periodic WHERE den='".$cldate->day."'",$db);
$i = 0;
while ($data = mysql_fetch_assoc($res)){
$i++;
$text .= "$i. ".$data['title']." ".$data['summa']."<br/>";
}
$res = mysql_query("SELECT * FROM rashod WHERE add_date='".$cldate->pdate."'",$db);
while ($data = mysql_fetch_assoc($res)){
$i++;
$text .= "<span>".$i.". ".$data['title']." ".$data['summa']." ".$data['fact_date'];
if (! $data['fact']){
$text .= " <input class='button edit' type='button' actiontype='rashod' value='изменить' rid='" . $data['id'] . "' date='" . $cldate->pdate . "' rtitle='" . $data['title'] . "' rsumma='" . $data['summa'] . "'/> "
. "<input class='button remove' type='button' actiontype='rashod' value='удалить' rid='" . $data['id'] . "' date='" . $cldate->pdate . "'/> "
. "<input class='button fact' type='button' actiontype='rashod' value='зафиксировать' rid='" . $data['id'] . "' date='" . $cldate->pdate . "'/>";
}
$text .= "</span><br/>";
}
$text .= "<span><input class='button add' actiontype='rashod' type='button' date='".$cldate->pdate."' value='новый'/></span>";
return $text;
}
function printBalance($cldate){
$res = mysql_query("SELECT * FROM balance WHERE change_date <= '".$cldate->pdate."' AND change_date > '".$cldate->calcDate()->pdate."'");
if (mysql_num_rows($res) > 0) {
$text = '<b>Изменения баланса</b><br/>';
while ($data = mysql_fetch_assoc($res)){
$text .= $data['change_date']." ".$data['summa']."<br/>";
}
}else{
$text = 'Изменений баланса не было';
}
return $text;
}
function printPartMonth($cldate,$day){
$cldate->changeDay($day);
return "<div>".$cldate->month.".".$cldate->day."</div>
<div>".printBalance($cldate)."</div>
<div>".printDohod($cldate)."</div>
<div>".printRashod($cldate)."</div>";
}
function printMonth ($cldate){
?>
<div class="wrapper">
<div class="side-left">
<?php print printPartMonth($cldate,5); ?>
</div>
<div class="side-right">
<?php print printPartMonth($cldate,20); ?>
</div>
</div>
<?php
}
include 'base.php';
if ($func == 'year') {
$year = $_GET['year'];
$cldate = new mydate($year);
printMonth($cldate);
}
if ($func == 'month') {
$year = $_GET['year'];
$month = $_GET['month'];
$cldate = new mydate($year,$month);
printMonth($cldate);
}
if ($func == 'balance_change'){
$balance = mysql_real_escape_string($_GET['balance'],$db);
mysql_query("INSERT INTO balance (summa,change_date) VALUES ('$balance',now())",$db);
}
if ($func == 'add_go'){
$text = $_GET['text'];
$summa = $_GET['summa'];
$date = $_GET['date'];
$type = $_GET['actiontype'];
list ($year,$month,$day) = split("-",$date);
mysql_query("INSERT INTO ".$type." (add_date,summa,title) VALUE ('$date','$summa','$text')");
printMonth(new mydate($year,$month,$day));
}
if ($func == 'remove'){
$id = $_GET['id'];
$date = $_GET['date'];
$type = $_GET['actiontype'];
mysql_query("DELETE FROM ".$type." WHERE id=$id");
list ($year,$month,$day) = split("-",$date);
printMonth(new mydate($year,$month,$day));
}
if ($func == 'edit_go'){
$id = $_GET['id'];
$date = $_GET['date'];
$type = $_GET['actiontype'];
list ($year,$month,$day) = split("-",$date);
$text = $_GET['text'];
$summa = $_GET['summa'];
mysql_query("UPDATE ".$type." SET title='$text',summa='$summa' WHERE id=$id");
printMonth(new mydate($year,$month,$day));
}
if ($func == 'fact'){
$id = $_GET['id'];
$date = $_GET['date'];
$type = $_GET['actiontype'];
list ($year,$month,$day) = split("-",$date);
mysql_query("UPDATE ".$type." SET fact=1,fact_date=now() WHERE id=$id");
printMonth(new mydate($year,$month,$day));
}
?>