Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 717d4a13

Von Moritz Bunkus vor fast 18 Jahren hinzugefügt

  • ID 717d4a1334d916fc77ba09bc17e5999ada617b21
  • Vorgänger 0246e125
  • Nachfolger 546480e0

CA.pm auf die Verwendung von parametrisierten Queries zur Vermeidung von SQL injections umgestellt.

Unterschiede anzeigen:

SL/CA.pm
36 36

  
37 37
package CA;
38 38
use Data::Dumper;
39
use SL::DBUtils;
39 40

  
40 41
sub all_accounts {
41 42
  $main::lxdebug->enter_sub();
......
47 48
  # connect to database
48 49
  my $dbh = $form->dbconnect($myconfig);
49 50

  
50
  my $query = qq|SELECT c.accno,
51
                 SUM(a.amount) AS amount
52
                 FROM chart c, acc_trans a
53
		 WHERE c.id = a.chart_id
54
		 GROUP BY c.accno|;
51
  my $query =
52
    qq|SELECT c.accno, SUM(a.amount) AS amount | .
53
    qq|FROM chart c, acc_trans a | .
54
    qq|WHERE c.id = a.chart_id | .
55
    qq|GROUP BY c.accno|;
55 56
  my $sth = $dbh->prepare($query);
56 57
  $sth->execute || $form->dberror($query);
57 58

  
......
60 61
  }
61 62
  $sth->finish;
62 63

  
63
  $query = qq{
64
    SELECT 
65
      c.accno,
66
      c.id,
67
      c.description,
68
      c.charttype,
69
      c.category,
70
      c.link,
71
      c.pos_bwa,
72
      c.pos_bilanz,
73
      c.pos_eur,
74
      c.valid_from,
75
      c.datevautomatik,
76
      comma(tk.startdate) AS startdate,
77
      comma(tk.taxkey_id) AS taxkey,
78
      comma(tx.taxdescription || to_char (tx.rate, '99V99' ) || '%') AS taxdescription,
79
      comma(tx.taxnumber) AS taxaccount,
80
      comma(tk.pos_ustva) AS tk_ustva,
81
      ( SELECT accno
82
      FROM chart c2
83
      WHERE c2.id = c.id
84
      ) AS new_account
85
    FROM chart c
86
    LEFT JOIN taxkeys tk ON (c.id = tk.chart_id)
87
    LEFT JOIN tax tx ON (tk.tax_id = tx.id)
88
    GROUP BY c.accno, c.id, c.description, c.charttype,
89
      c.category, c.link, c.pos_bwa, c.pos_bilanz, c.pos_eur, c.valid_from,      
90
      c.datevautomatik
91
    ORDER BY c.accno
92
  };
93

  
94
  $sth = $dbh->prepare($query);
95
  $sth->execute || $form->dberror($query);
96

  
64
  $query =
65
    qq!SELECT  c.accno, c.id, c.description, c.charttype, c.category, ! .
66
    qq!  c.link, c.pos_bwa, c.pos_bilanz, c.pos_eur, c.valid_from, ! .
67
    qq!  c.datevautomatik, comma(tk.startdate) AS startdate, ! .
68
    qq!  comma(tk.taxkey_id) AS taxkey, ! .
69
    qq!  comma(tx.taxdescription || to_char (tx.rate, '99V99' ) || '%') ! .
70
    qq!    AS taxdescription, ! .
71
    qq!  comma(tx.taxnumber) AS taxaccount, comma(tk.pos_ustva) ! .
72
    qq!    AS tk_ustva, ! .
73
    qq!  ( SELECT accno FROM chart c2 WHERE c2.id = c.id ) AS new_account ! .
74
    qq!FROM chart c ! .
75
    qq!LEFT JOIN taxkeys tk ON (c.id = tk.chart_id) ! .
76
    qq!LEFT JOIN tax tx ON (tk.tax_id = tx.id) ! .
77
    qq!GROUP BY c.accno, c.id, c.description, c.charttype, ! .
78
    qq!  c.category, c.link, c.pos_bwa, c.pos_bilanz, c.pos_eur, ! .
79
    qq!  c.valid_from, c.datevautomatik ! .
80
    qq!ORDER BY c.accno!;
81
  my $sth = prepare_execute_query($form, $dbh, $query);
82

  
83
  $form->{CA} = [];
97 84
  while (my $ca = $sth->fetchrow_hashref(NAME_lc)) {
98
    $ca->{amount}           = $amount{ $ca->{accno} };
85
    $ca->{amount} = $amount{ $ca->{accno} };
99 86
    if ($ca->{amount} < 0) {
100 87
      $ca->{debit} = $ca->{amount} * -1;
101 88
    } else {
102 89
      $ca->{credit} = $ca->{amount};
103 90
    }
104
    push @{ $form->{CA} }, $ca;
91
    push(@{ $form->{CA} }, $ca);
105 92
  }
106 93

  
107 94
  $sth->finish;
......
119 106
  my $dbh = $form->dbconnect($myconfig);
120 107

  
121 108
  # get chart_id
122
  my $query = qq|SELECT c.id FROM chart c
123
                 WHERE c.accno = '$form->{accno}'|;
124
  my $sth = $dbh->prepare($query);
125
  $sth->execute || $form->dberror($query);
126

  
127
  my @id = ();
128
  while (my ($id) = $sth->fetchrow_array) {
129
    push @id, $id;
130
  }
131
  $sth->finish;
109
  my $query = qq|SELECT id FROM chart WHERE accno = ?|;
110
  my @id = selectall_array_query($form, $dbh, $query, $form->{accno});
132 111

  
133 112
  my $fromdate_where;
134 113
  my $todate_where;
135 114

  
136
  my $where = '1 = 1';
115
  my $where = qq|1 = 1|;
137 116

  
138 117
  # build WHERE clause from dates if any
139 118
  #  if ($form->{fromdate}) {
......
143 122
  #    $where .= " AND ac.transdate <= '$form->{todate}'";
144 123
  #  }
145 124

  
125
  my (@values, @where_values, @subwhere_values);
146 126
  if ($form->{fromdate}) {
147
    $fromto = " AND ac.transdate >= '$form->{fromdate}'";
148
    $subwhere .= " AND transdate >= '$form->{fromdate}'";
149
    $glwhere = " AND ac.transdate >= '$form->{fromdate}'";
127
    $where .= qq| AND ac.transdate >= ?|;
128
    $subwhere .= qq| AND transdate >= ?|;
129
    push(@where_values, conv_date($form->{fromdate}));
130
    push(@subwhere_values, conv_date($form->{fromdate}));
150 131
  }
151 132

  
152 133
  if ($form->{todate}) {
153
    $fromto   .= " AND ac.transdate <= '$form->{todate}'";
154
    $subwhere .= " AND transdate <= '$form->{todate}'";
155
    $glwhere .= " AND ac.transdate <= '$form->{todate}'";
134
    $where .= qq| AND ac.transdate <= ?|;
135
    $subwhere .= qq| AND transdate <= ?|;
136
    push(@where_values, conv_date($form->{todate}));
137
    push(@subwhere_values, conv_date($form->{todate}));
156 138
  }
157 139

  
158 140

  
159
  $where .= $fromto;
160
  $AR_PAID = "";
161
  $AP_PAID = "";
162
  $glwhere = "";    # note! gl will be aliased as "a" later...
163 141
  my $sortorder = join ', ',
164 142
    $form->sort_columns(qw(transdate reference description));
165 143
  my $false = ($myconfig->{dbdriver} eq 'Pg') ? FALSE: q|'0'|;
......
170 148
                 description => 3);
171 149
  map { $sortorder =~ s/$_/$ordinal{$_}/ } keys %ordinal;
172 150

  
173
  my ($null, $department_id) = split /--/, $form->{department};
174
  my $dpt_where;
175
  my $dpt_join;
151
  my ($null, $department_id) = split(/--/, $form->{department});
152
  my ($dpt_where, $dpt_join, @department_values);
176 153
  if ($department_id) {
177
    $dpt_join = qq|
178
                   JOIN department t ON (t.id = a.department_id)
179
		  |;
180
    $dpt_where = qq|
181
		   AND t.id = $department_id
182
		  |;
154
    $dpt_join = qq| JOIN department t ON (t.id = a.department_id) |;
155
    $dpt_where = qq| AND t.id = ? |;
156
    @department_values = ($department_id);
183 157
  }
184 158

  
185
  my $project;
159
  my ($project, @project_values);
186 160
  if ($form->{project_id}) {
187
    $project = qq|
188
                 AND ac.project_id = $form->{project_id}
189
		 |;
161
    $project = qq| AND ac.project_id = ? |;
162
    @project_values = (conv_i($form->{project_id}));
190 163
  }
191 164

  
192 165
  if ($form->{accno}) {
193 166

  
194 167
    # get category for account
195
    $query = qq|SELECT c.category
196
                FROM chart c
197
		WHERE c.accno = '$form->{accno}'|;
198

  
199
    $sth = $dbh->prepare($query);
200

  
201
    $sth->execute || $form->dberror($query);
202
    ($form->{category}) = $sth->fetchrow_array;
203
    $sth->finish;
168
    $query = qq|SELECT category FROM chart WHERE accno = ?|;
169
    ($form->{category}) = selectrow_query($form, $dbh, $query, $form->{accno});
204 170

  
205 171
    if ($form->{fromdate}) {
206

  
207 172
      # get beginning balance
208
      $query = qq|SELECT SUM(ac.amount)
209
		  FROM acc_trans ac
210
		  JOIN chart c ON (ac.chart_id = c.id)
211
		  $dpt_join
212
		  WHERE c.accno = '$form->{accno}'
213
		  AND ac.transdate < '$form->{fromdate}'
214
		  $dpt_where
215
		  $project
216
		  |;
173
      $query =
174
        qq|SELECT SUM(ac.amount) | .
175
        qq|FROM acc_trans ac | .
176
        qq|JOIN chart c ON (ac.chart_id = c.id) | .
177
        $dpt_join .
178
        qq|WHERE c.accno = ? | .
179
        qq|AND ac.transdate < ? | .
180
        $dpt_where .
181
        $project;
182
      @values = ($form->{accno}, conv_date($form->{fromdate}),
183
                 @department_values, @project_values);
217 184

  
218 185
      if ($form->{project_id}) {
219

  
220
        $query .= qq|
221

  
222
	       UNION
223

  
224
	          SELECT SUM(ac.qty * ac.sellprice)
225
		  FROM invoice ac
226
		  JOIN ar a ON (ac.trans_id = a.id)
227
		  JOIN parts p ON (ac.parts_id = p.id)
228
		  JOIN chart c ON (p.income_accno_id = c.id)
229
		  $dpt_join
230
		  WHERE c.accno = '$form->{accno}'
231
		  AND a.transdate < '$form->{fromdate}'
232
		  AND c.category = 'I'
233
		  $dpt_where
234
		  $project
235

  
236
	       UNION
237

  
238
	          SELECT SUM(ac.qty * ac.sellprice)
239
		  FROM invoice ac
240
		  JOIN ap a ON (ac.trans_id = a.id)
241
		  JOIN parts p ON (ac.parts_id = p.id)
242
		  JOIN chart c ON (p.expense_accno_id = c.id)
243
		  $dpt_join
244
		  WHERE c.accno = '$form->{accno}'
245
		  AND a.transdate < '$form->{fromdate}'
246
		  AND c.category = 'E'
247
		  $dpt_where
248
		  $project
249
		  |;
250

  
186
        $query .=
187
          qq|UNION | .
188

  
189
          qq|SELECT SUM(ac.qty * ac.sellprice) | .
190
          qq|FROM invoice ac | .
191
          qq|JOIN ar a ON (ac.trans_id = a.id) | .
192
          qq|JOIN parts p ON (ac.parts_id = p.id) | .
193
          qq|JOIN chart c ON (p.income_accno_id = c.id) | .
194
          $dpt_join .
195
          qq|WHERE c.accno = ? | .
196
          qq|  AND a.transdate < ? | .
197
          qq|  AND c.category = 'I' | .
198
          $dpt_where .
199
          $project .
200

  
201
          qq|UNION | .
202

  
203
          qq|SELECT SUM(ac.qty * ac.sellprice) | .
204
          qq|FROM invoice ac | .
205
          qq|JOIN ap a ON (ac.trans_id = a.id) | .
206
          qq|JOIN parts p ON (ac.parts_id = p.id) | .
207
          qq|JOIN chart c ON (p.expense_accno_id = c.id) | .
208
          $dpt_join .
209
          qq|WHERE c.accno = ? | .
210
          qq|  AND a.transdate < ? | .
211
          qq|  AND c.category = 'E' | .
212
          $dpt_where .
213
          $project;
214

  
215
        push(@values,
216
             $form->{accno}, conv_date($form->{transdate}),
217
             @department_values, @project_values,
218
             $form->{accno}, conv_date($form->{transdate}),
219
             @department_values, @project_values);
251 220
      }
252 221

  
253
      $sth = $dbh->prepare($query);
254

  
255
      $sth->execute || $form->dberror($query);
256
      ($form->{balance}) = $sth->fetchrow_array;
257
      $sth->finish;
222
      ($form->{balance}) = selectrow_query($form, $dbh, $query, @values);
258 223
    }
259 224
  }
260 225

  
261 226
  $query = "";
262 227
  my $union = "";
228
  @values = ();
263 229

  
264 230
  foreach my $id (@id) {
265 231

  
266
    # NOTE:
267
    #  Postgres is really picky about the order of implicit CROSS JOINs with ','
268
    #  if you alias the  tables and want to use the alias later in another JOIN.
269
    #  the alias you want to use has to be the most recent in the list, otherwise
270
    #  Postgres will overwrite the alias internally and complain.
271
    #  For this reason, in the next 3 SELECTs, the 'a' alias is last in the list.
272
    #  Don't change this, and if you do, substitute the ',' with CROSS JOIN
273
    #  ... that also works.
232
    # NOTE: Postgres is really picky about the order of implicit CROSS
233
    #  JOINs with ',' if you alias the tables and want to use the
234
    #  alias later in another JOIN.  the alias you want to use has to
235
    #  be the most recent in the list, otherwise Postgres will
236
    #  overwrite the alias internally and complain.  For this reason,
237
    #  in the next 3 SELECTs, the 'a' alias is last in the list.
238
    #  Don't change this, and if you do, substitute the ',' with CROSS
239
    #  JOIN ... that also works.
274 240

  
275 241
    # get all transactions
276
    $query .= qq|$union
277
      SELECT a.id, a.reference, a.description, ac.transdate,
278
	     $false AS invoice, ac.amount, 'gl' as module
279
		FROM acc_trans ac, gl a $dpt_join
280
		WHERE $where
281
		$glwhere
282
		$dpt_where
283
		$project
284
		AND ac.chart_id = $id
285
		AND ac.trans_id = a.id
286
      UNION
287
      SELECT a.id, a.invnumber, c.name, ac.transdate,
288
	     a.invoice, ac.amount, 'ar' as module
289
		FROM acc_trans ac, customer c, ar a $dpt_join
290
		WHERE $where
291
		$dpt_where
292
		$project
293
		AND ac.chart_id = $id
294
		AND ac.trans_id = a.id
295
                $AR_PAID
296
		AND a.customer_id = c.id
297
      UNION
298
      SELECT a.id, a.invnumber, v.name, ac.transdate,
299
	     a.invoice, ac.amount, 'ap' as module
300
		FROM acc_trans ac, vendor v, ap a $dpt_join
301
		WHERE $where
302
		$dpt_where
303
		$project
304
		AND ac.chart_id = $id
305
		AND ac.trans_id = a.id
306
		$AP_PAID
307
		AND a.vendor_id = v.id
308
		|;
309
    $union = qq|
310
      UNION ALL
311
      |;
242
    $query .=
243
      $union .
244
      qq|SELECT a.id, a.reference, a.description, ac.transdate, | .
245
      qq|  $false AS invoice, ac.amount, 'gl' as module | .
246
      qq|FROM acc_trans ac, gl a | .
247
      $dpt_join .
248
      qq|WHERE | . $where . $dpt_where . $project .
249
      qq|  AND ac.chart_id = ? | .
250
      qq|  AND ac.trans_id = a.id | .
251

  
252
      qq|UNION | .
253

  
254
      qq|SELECT a.id, a.invnumber, c.name, ac.transdate, | .
255
      qq|  a.invoice, ac.amount, 'ar' as module | .
256
      qq|FROM acc_trans ac, customer c, ar a | .
257
      $dpt_join .
258
      qq|WHERE | . $where . $dpt_where . $project .
259
      qq|  AND ac.chart_id = ? | .
260
      qq|  AND ac.trans_id = a.id | .
261
      qq|  AND a.customer_id = c.id | .
262

  
263
      qq|UNION | .
264

  
265
      qq|SELECT a.id, a.invnumber, v.name, ac.transdate, | .
266
      qq|  a.invoice, ac.amount, 'ap' as module | .
267
      qq|FROM acc_trans ac, vendor v, ap a | .
268
      $dpt_join .
269
      qq|WHERE | . $where . $dpt_where . $project .
270
      qq|  AND ac.chart_id = ? | .
271
      qq|  AND ac.trans_id = a.id | .
272
      qq|  AND a.vendor_id = v.id |;
273

  
274
    push(@values,
275
         @where_values, @department_values, @project_values, $id,
276
         @where_values, @department_values, @project_values, $id,
277
         @where_values, @department_values, @project_values, $id);
278

  
279
    $union = qq|UNION ALL |;
312 280

  
313 281
    if ($form->{project_id}) {
314 282

  
315 283
      $fromdate_where =~ s/ac\./a\./;
316 284
      $todate_where   =~ s/ac\./a\./;
317 285

  
318
      $query .= qq|
319

  
320
             UNION ALL
321

  
322
                 SELECT a.id, a.invnumber, c.name, a.transdate,
323
	         a.invoice, ac.qty * ac.sellprice AS sellprice, 'ar' as module
324
		 FROM ar a
325
		 JOIN invoice ac ON (ac.trans_id = a.id)
326
		 JOIN parts p ON (ac.parts_id = p.id)
327
		 JOIN customer c ON (a.customer_id = c.id)
328
		 $dpt_join
329
		 WHERE p.income_accno_id = $id
330
		 $fromdate_where
331
		 $todate_where
332
		 $dpt_where
333
		 $project
334

  
335
             UNION ALL
336

  
337
                 SELECT a.id, a.invnumber, v.name, a.transdate,
338
	         a.invoice, ac.qty * ac.sellprice AS sellprice, 'ap' as module
339
		 FROM ap a
340
		 JOIN invoice ac ON (ac.trans_id = a.id)
341
		 JOIN parts p ON (ac.parts_id = p.id)
342
		 JOIN vendor v ON (a.vendor_id = v.id)
343
		 $dpt_join
344
		 WHERE p.expense_accno_id = $id
345
		 $fromdate_where
346
		 $todate_where
347
		 $dpt_where
348
		 $project
349
		 |;
286
      $query .=
287
        qq|UNION ALL | .
288

  
289
        qq|SELECT a.id, a.invnumber, c.name, a.transdate, | .
290
        qq|  a.invoice, ac.qty * ac.sellprice AS sellprice, 'ar' as module | .
291
        qq|FROM ar a | .
292
        qq|JOIN invoice ac ON (ac.trans_id = a.id) | .
293
        qq|JOIN parts p ON (ac.parts_id = p.id) | .
294
        qq|JOIN customer c ON (a.customer_id = c.id) | .
295
        $dpt_join .
296
        qq|WHERE p.income_accno_id = ? | .
297
        $fromdate_where .
298
        $todate_where .
299
        $dpt_where .
300
        $project .
301

  
302
        qq|UNION ALL | .
303

  
304
        qq|SELECT a.id, a.invnumber, v.name, a.transdate, | .
305
        qq|  a.invoice, ac.qty * ac.sellprice AS sellprice, 'ap' as module | .
306
        qq|FROM ap a | .
307
        qq|JOIN invoice ac ON (ac.trans_id = a.id) | .
308
        qq|JOIN parts p ON (ac.parts_id = p.id) | .
309
        qq|JOIN vendor v ON (a.vendor_id = v.id) | .
310
        $dpt_join .
311
        qq|WHERE p.expense_accno_id = ? | .
312
        $fromdate_where .
313
        $todate_where .
314
        $dpt_where .
315
        $project;
316

  
317
      push(@values,
318
           $id, @department_values, @project_values,
319
           $id, @department_values, @project_values);
350 320

  
351 321
      $fromdate_where =~ s/a\./ac\./;
352 322
      $todate_where   =~ s/a\./ac\./;
353 323

  
354 324
    }
355 325

  
356
    $union = qq|
357
             UNION ALL
358
                 |;
326
    $union = qq|UNION ALL|;
359 327
  }
360 328

  
361
  $query .= qq|
362
      ORDER BY $sortorder|;
363

  
364
  $sth = $dbh->prepare($query);
365
  $sth->execute || $form->dberror($query);
329
  $query .= qq|ORDER BY | . $sortorder;
330
  $sth = prepare_execute_query($form, $dbh, $query, @values);
366 331

  
332
  $form->{CA} = [];
367 333
  while (my $ca = $sth->fetchrow_hashref(NAME_lc)) {
368

  
369
    # gl
370
    if ($ca->{module} eq "gl") {
371
      $ca->{module} = "gl";
372
    }
373

  
374 334
    # ap
375 335
    if ($ca->{module} eq "ap") {
376 336
      $ca->{module} = ($ca->{invoice}) ? 'ir' : 'ap';
......
389 349
      $ca->{debit}  = 0;
390 350
    }
391 351

  
392
    push @{ $form->{CA} }, $ca;
352
    push(@{ $form->{CA} }, $ca);
393 353

  
394 354
  }
395 355

  

Auch abrufbar als: Unified diff