Revision 936f6a7f
Von Moritz Bunkus vor mehr als 17 Jahren hinzugefügt
SL/RP.pm | ||
---|---|---|
34 | 34 |
|
35 | 35 |
package RP; |
36 | 36 |
|
37 |
use SL::DBUtils; |
|
38 |
|
|
37 | 39 |
sub balance_sheet { |
38 | 40 |
$main::lxdebug->enter_sub(); |
39 | 41 |
|
... | ... | |
47 | 49 |
|
48 | 50 |
# if there are any dates construct a where |
49 | 51 |
if ($form->{asofdate}) { |
50 |
|
|
51 |
$form->{this_period} = "$form->{asofdate}"; |
|
52 |
$form->{period} = "$form->{asofdate}"; |
|
53 |
|
|
52 |
$form->{period} = $form->{this_period} = conv_dateq($form->{asofdate}); |
|
54 | 53 |
} |
55 | 54 |
|
56 | 55 |
$form->{decimalplaces} *= 1; |
... | ... | |
65 | 64 |
&get_accounts($dbh, $last_period, "", $form->{compareasofdate}, |
66 | 65 |
$form, \@categories); |
67 | 66 |
|
68 |
$form->{last_period} = "$form->{compareasofdate}";
|
|
67 |
$form->{last_period} = conv_dateq($form->{compareasofdate});
|
|
69 | 68 |
|
70 | 69 |
} |
71 | 70 |
|
... | ... | |
94 | 93 |
'ml' => 1 |
95 | 94 |
}); |
96 | 95 |
|
97 |
foreach $category (grep { !/C/ } @categories) { |
|
96 |
foreach my $category (grep { !/C/ } @categories) {
|
|
98 | 97 |
|
99 | 98 |
foreach $key (sort keys %{ $form->{$category} }) { |
100 | 99 |
|
... | ... | |
325 | 324 |
my $glwhere = ""; |
326 | 325 |
my $subwhere = ""; |
327 | 326 |
my $item; |
327 |
my $sth; |
|
328 | 328 |
|
329 |
my $category = "AND ("; |
|
330 |
foreach $item (@{$categories}) { |
|
331 |
$category .= qq|c.category = '$item' OR |; |
|
332 |
} |
|
333 |
$category =~ s/OR $/\)/; |
|
329 |
my $category = qq| AND (| . join(" OR ", map({ "(c.category = " . $dbh->quote($_) . ")" } @{$categories})) . qq|) |; |
|
334 | 330 |
|
335 | 331 |
# get headings |
336 |
$query = qq|SELECT c.accno, c.description, c.category |
|
337 |
FROM chart c |
|
338 |
WHERE c.charttype = 'H' |
|
339 |
$category |
|
340 |
ORDER by c.accno|; |
|
332 |
$query = |
|
333 |
qq|SELECT c.accno, c.description, c.category |
|
334 |
FROM chart c |
|
335 |
WHERE (c.charttype = 'H') |
|
336 |
$category |
|
337 |
ORDER by c.accno|; |
|
341 | 338 |
|
342 |
$sth = $dbh->prepare($query); |
|
343 |
$sth->execute || $form->dberror($query); |
|
339 |
$sth = prepare_execute_query($form, $dbh, $query); |
|
344 | 340 |
|
345 | 341 |
my @headingaccounts = (); |
346 | 342 |
while ($ref = $sth->fetchrow_hashref(NAME_lc)) { |
... | ... | |
355 | 351 |
$sth->finish; |
356 | 352 |
|
357 | 353 |
if ($fromdate) { |
354 |
$fromdate = conv_dateq($fromdate); |
|
358 | 355 |
if ($form->{method} eq 'cash') { |
359 |
$subwhere .= " AND transdate >= '$fromdate'";
|
|
360 |
$glwhere = " AND ac.transdate >= '$fromdate'";
|
|
356 |
$subwhere .= " AND (transdate >= $fromdate)";
|
|
357 |
$glwhere = " AND (ac.transdate >= $fromdate)";
|
|
361 | 358 |
} else { |
362 |
$where .= " AND ac.transdate >= '$fromdate'";
|
|
359 |
$where .= " AND (ac.transdate >= $fromdate)";
|
|
363 | 360 |
} |
364 | 361 |
} |
365 | 362 |
|
366 | 363 |
if ($todate) { |
367 |
$where .= " AND ac.transdate <= '$todate'"; |
|
368 |
$subwhere .= " AND transdate <= '$todate'"; |
|
364 |
$todate = conv_dateq($todate); |
|
365 |
$where .= " AND (ac.transdate <= $todate)"; |
|
366 |
$subwhere .= " AND (transdate <= $todate)"; |
|
369 | 367 |
} |
370 | 368 |
|
371 | 369 |
if ($department_id) { |
372 |
$dpt_join = qq| |
|
373 |
JOIN department t ON (a.department_id = t.id) |
|
374 |
|; |
|
375 |
$dpt_where = qq| |
|
376 |
AND t.id = $department_id |
|
377 |
|; |
|
370 |
$dpt_join = qq| JOIN department t ON (a.department_id = t.id) |; |
|
371 |
$dpt_where = qq| AND (t.id = | . conv_i($department_id, 'NULL') . qq|)|; |
|
378 | 372 |
} |
379 | 373 |
|
380 | 374 |
if ($form->{project_id}) { |
381 |
$project = qq| |
|
382 |
AND ac.project_id = $form->{project_id} |
|
383 |
|; |
|
375 |
$project = qq| AND (ac.project_id = | . conv_i($form->{project_id}, 'NULL') . qq|) |; |
|
384 | 376 |
} |
385 | 377 |
|
386 |
{ # standard account |
|
387 |
|
|
388 |
if ($form->{method} eq 'cash') { |
|
389 |
|
|
390 |
$query = qq| |
|
391 |
|
|
392 |
SELECT c.accno, sum(ac.amount) AS amount, |
|
393 |
c.description, c.category |
|
394 |
FROM acc_trans ac |
|
395 |
JOIN chart c ON (c.id = ac.chart_id) |
|
396 |
JOIN ar a ON (a.id = ac.trans_id) |
|
397 |
$dpt_join |
|
398 |
WHERE $where |
|
399 |
$dpt_where |
|
400 |
$category |
|
401 |
AND ac.trans_id IN |
|
402 |
( |
|
403 |
SELECT trans_id |
|
404 |
FROM acc_trans |
|
405 |
JOIN chart ON (chart_id = id) |
|
406 |
WHERE link LIKE '%AR_paid%' |
|
407 |
$subwhere |
|
408 |
) |
|
409 |
|
|
410 |
$project |
|
411 |
GROUP BY c.accno, c.description, c.category |
|
412 |
|
|
413 |
UNION ALL |
|
414 |
|
|
415 |
SELECT c.accno, sum(ac.amount) AS amount, |
|
416 |
c.description, c.category |
|
417 |
FROM acc_trans ac |
|
418 |
JOIN chart c ON (c.id = ac.chart_id) |
|
419 |
JOIN ap a ON (a.id = ac.trans_id) |
|
420 |
$dpt_join |
|
421 |
WHERE $where |
|
422 |
$dpt_where |
|
423 |
$category |
|
424 |
AND ac.trans_id IN |
|
425 |
( |
|
426 |
SELECT trans_id |
|
427 |
FROM acc_trans |
|
428 |
JOIN chart ON (chart_id = id) |
|
429 |
WHERE link LIKE '%AP_paid%' |
|
430 |
$subwhere |
|
431 |
) |
|
432 |
|
|
433 |
$project |
|
434 |
GROUP BY c.accno, c.description, c.category |
|
435 |
|
|
436 |
UNION ALL |
|
437 |
|
|
438 |
SELECT c.accno, sum(ac.amount) AS amount, |
|
439 |
c.description, c.category |
|
440 |
FROM acc_trans ac |
|
441 |
JOIN chart c ON (c.id = ac.chart_id) |
|
442 |
JOIN gl a ON (a.id = ac.trans_id) |
|
443 |
$dpt_join |
|
444 |
WHERE $where |
|
445 |
$glwhere |
|
446 |
$dpt_from |
|
447 |
$category |
|
448 |
AND NOT (c.link = 'AR' OR c.link = 'AP') |
|
449 |
$project |
|
450 |
GROUP BY c.accno, c.description, c.category |
|
451 |
|; |
|
452 |
|
|
453 |
if ($form->{project_id}) { |
|
454 |
|
|
455 |
$query .= qq| |
|
456 |
|
|
457 |
UNION ALL |
|
458 |
|
|
459 |
SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount, |
|
460 |
c.description AS description, c.category |
|
461 |
FROM invoice ac |
|
462 |
JOIN ar a ON (a.id = ac.trans_id) |
|
463 |
JOIN parts p ON (ac.parts_id = p.id) |
|
464 |
JOIN chart c on (p.income_accno_id = c.id) |
|
465 |
$dpt_join |
|
466 |
-- use transdate from subwhere |
|
467 |
WHERE 1 = 1 $subwhere |
|
468 |
AND c.category = 'I' |
|
469 |
$dpt_where |
|
470 |
AND ac.trans_id IN |
|
471 |
( |
|
472 |
SELECT trans_id |
|
473 |
FROM acc_trans |
|
474 |
JOIN chart ON (chart_id = id) |
|
475 |
WHERE link LIKE '%AR_paid%' |
|
476 |
$subwhere |
|
477 |
) |
|
478 |
|
|
479 |
$project |
|
480 |
GROUP BY c.accno, c.description, c.category |
|
481 |
|
|
482 |
UNION ALL |
|
483 |
|
|
484 |
SELECT c.accno AS accno, SUM(ac.sellprice) AS amount, |
|
485 |
c.description AS description, c.category |
|
486 |
FROM invoice ac |
|
487 |
JOIN ap a ON (a.id = ac.trans_id) |
|
488 |
JOIN parts p ON (ac.parts_id = p.id) |
|
489 |
JOIN chart c on (p.expense_accno_id = c.id) |
|
490 |
$dpt_join |
|
491 |
WHERE 1 = 1 $subwhere |
|
492 |
AND c.category = 'E' |
|
493 |
$dpt_where |
|
494 |
AND ac.trans_id IN |
|
495 |
( |
|
496 |
SELECT trans_id |
|
497 |
FROM acc_trans |
|
498 |
JOIN chart ON (chart_id = id) |
|
499 |
WHERE link LIKE '%AP_paid%' |
|
500 |
$subwhere |
|
501 |
) |
|
502 |
|
|
503 |
$project |
|
504 |
GROUP BY c.accno, c.description, c.category |
|
505 |
|; |
|
506 |
} |
|
507 |
|
|
508 |
} else { |
|
509 |
|
|
510 |
if ($department_id) { |
|
511 |
$dpt_join = qq| |
|
512 |
JOIN dpt_trans t ON (t.trans_id = ac.trans_id) |
|
513 |
|; |
|
514 |
$dpt_where = qq| |
|
515 |
AND t.department_id = $department_id |
|
516 |
|; |
|
517 |
} |
|
518 |
|
|
519 |
$query = qq| |
|
378 |
if ($form->{method} eq 'cash') { |
|
379 |
$query = |
|
380 |
qq|SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category |
|
381 |
FROM acc_trans ac |
|
382 |
JOIN chart c ON (c.id = ac.chart_id) |
|
383 |
JOIN ar a ON (a.id = ac.trans_id) |
|
384 |
$dpt_join |
|
385 |
WHERE $where |
|
386 |
$dpt_where |
|
387 |
$category |
|
388 |
AND ac.trans_id IN |
|
389 |
( |
|
390 |
SELECT trans_id |
|
391 |
FROM acc_trans |
|
392 |
JOIN chart ON (chart_id = id) |
|
393 |
WHERE (link LIKE '%AR_paid%') |
|
394 |
$subwhere |
|
395 |
) |
|
396 |
$project |
|
397 |
GROUP BY c.accno, c.description, c.category |
|
398 |
|
|
399 |
UNION ALL |
|
400 |
|
|
401 |
SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category |
|
402 |
FROM acc_trans ac |
|
403 |
JOIN chart c ON (c.id = ac.chart_id) |
|
404 |
JOIN ap a ON (a.id = ac.trans_id) |
|
405 |
$dpt_join |
|
406 |
WHERE $where |
|
407 |
$dpt_where |
|
408 |
$category |
|
409 |
AND ac.trans_id IN |
|
410 |
( |
|
411 |
SELECT trans_id |
|
412 |
FROM acc_trans |
|
413 |
JOIN chart ON (chart_id = id) |
|
414 |
WHERE (link LIKE '%AP_paid%') |
|
415 |
$subwhere |
|
416 |
) |
|
417 |
$project |
|
418 |
GROUP BY c.accno, c.description, c.category |
|
419 |
|
|
420 |
UNION ALL |
|
421 |
|
|
422 |
SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category |
|
423 |
FROM acc_trans ac |
|
424 |
JOIN chart c ON (c.id = ac.chart_id) |
|
425 |
JOIN gl a ON (a.id = ac.trans_id) |
|
426 |
$dpt_join |
|
427 |
WHERE $where |
|
428 |
$glwhere |
|
429 |
$dpt_from |
|
430 |
$category |
|
431 |
AND NOT ((c.link = 'AR') OR (c.link = 'AP')) |
|
432 |
$project |
|
433 |
GROUP BY c.accno, c.description, c.category |; |
|
520 | 434 |
|
521 |
SELECT c.accno, sum(ac.amount) AS amount, |
|
522 |
c.description, c.category |
|
523 |
FROM acc_trans ac |
|
524 |
JOIN chart c ON (c.id = ac.chart_id) |
|
525 |
$dpt_join |
|
526 |
WHERE $where |
|
527 |
$dpt_where |
|
528 |
$category |
|
529 |
$project |
|
530 |
GROUP BY c.accno, c.description, c.category |
|
531 |
|; |
|
435 |
if ($form->{project_id}) { |
|
436 |
$query .= |
|
437 |
qq| |
|
438 |
UNION ALL |
|
439 |
|
|
440 |
SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount, c.description AS description, c.category |
|
441 |
FROM invoice ac |
|
442 |
JOIN ar a ON (a.id = ac.trans_id) |
|
443 |
JOIN parts p ON (ac.parts_id = p.id) |
|
444 |
JOIN chart c on (p.income_accno_id = c.id) |
|
445 |
$dpt_join |
|
446 |
-- use transdate from subwhere |
|
447 |
WHERE (c.category = 'I') |
|
448 |
$subwhere |
|
449 |
$dpt_where |
|
450 |
AND ac.trans_id IN |
|
451 |
( |
|
452 |
SELECT trans_id |
|
453 |
FROM acc_trans |
|
454 |
JOIN chart ON (chart_id = id) |
|
455 |
WHERE (link LIKE '%AR_paid%') |
|
456 |
$subwhere |
|
457 |
) |
|
458 |
$project |
|
459 |
GROUP BY c.accno, c.description, c.category |
|
460 |
|
|
461 |
UNION ALL |
|
462 |
|
|
463 |
SELECT c.accno AS accno, SUM(ac.sellprice) AS amount, c.description AS description, c.category |
|
464 |
FROM invoice ac |
|
465 |
JOIN ap a ON (a.id = ac.trans_id) |
|
466 |
JOIN parts p ON (ac.parts_id = p.id) |
|
467 |
JOIN chart c on (p.expense_accno_id = c.id) |
|
468 |
$dpt_join |
|
469 |
WHERE (c.category = 'E') |
|
470 |
$subwhere |
|
471 |
$dpt_where |
|
472 |
AND ac.trans_id IN |
|
473 |
( |
|
474 |
SELECT trans_id |
|
475 |
FROM acc_trans |
|
476 |
JOIN chart ON (chart_id = id) |
|
477 |
WHERE link LIKE '%AP_paid%' |
|
478 |
$subwhere |
|
479 |
) |
|
480 |
$project |
|
481 |
GROUP BY c.accno, c.description, c.category |; |
|
482 |
} |
|
532 | 483 |
|
533 |
if ($form->{project_id}) { |
|
484 |
} else { # if ($form->{method} eq 'cash') |
|
485 |
if ($department_id) { |
|
486 |
$dpt_join = qq| JOIN dpt_trans t ON (t.trans_id = ac.trans_id) |; |
|
487 |
$dpt_where = qq| AND t.department_id = $department_id |; |
|
488 |
} |
|
534 | 489 |
|
535 |
$query .= qq| |
|
536 |
|
|
537 |
UNION ALL |
|
538 |
|
|
539 |
SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount, |
|
540 |
c.description AS description, c.category |
|
541 |
FROM invoice ac |
|
542 |
JOIN ar a ON (a.id = ac.trans_id) |
|
543 |
JOIN parts p ON (ac.parts_id = p.id) |
|
544 |
JOIN chart c on (p.income_accno_id = c.id) |
|
545 |
$dpt_join |
|
546 |
-- use transdate from subwhere |
|
547 |
WHERE 1 = 1 $subwhere |
|
548 |
AND c.category = 'I' |
|
549 |
$dpt_where |
|
550 |
$project |
|
551 |
GROUP BY c.accno, c.description, c.category |
|
552 |
|
|
553 |
UNION ALL |
|
554 |
|
|
555 |
SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) * -1 AS amount, |
|
556 |
c.description AS description, c.category |
|
557 |
FROM invoice ac |
|
558 |
JOIN ap a ON (a.id = ac.trans_id) |
|
559 |
JOIN parts p ON (ac.parts_id = p.id) |
|
560 |
JOIN chart c on (p.expense_accno_id = c.id) |
|
561 |
$dpt_join |
|
562 |
WHERE 1 = 1 $subwhere |
|
563 |
AND c.category = 'E' |
|
564 |
$dpt_where |
|
565 |
$project |
|
566 |
GROUP BY c.accno, c.description, c.category |
|
567 |
|; |
|
490 |
$query = qq| |
|
491 |
SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category |
|
492 |
FROM acc_trans ac |
|
493 |
JOIN chart c ON (c.id = ac.chart_id) |
|
494 |
$dpt_join |
|
495 |
WHERE $where |
|
496 |
$dpt_where |
|
497 |
$category |
|
498 |
$project |
|
499 |
GROUP BY c.accno, c.description, c.category |; |
|
568 | 500 |
|
569 |
} |
|
501 |
if ($form->{project_id}) { |
|
502 |
$query .= qq| |
|
503 |
UNION ALL |
|
504 |
|
|
505 |
SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount, c.description AS description, c.category |
|
506 |
FROM invoice ac |
|
507 |
JOIN ar a ON (a.id = ac.trans_id) |
|
508 |
JOIN parts p ON (ac.parts_id = p.id) |
|
509 |
JOIN chart c on (p.income_accno_id = c.id) |
|
510 |
$dpt_join |
|
511 |
-- use transdate from subwhere |
|
512 |
WHERE (c.category = 'I') |
|
513 |
$subwhere |
|
514 |
$dpt_where |
|
515 |
$project |
|
516 |
GROUP BY c.accno, c.description, c.category |
|
517 |
|
|
518 |
UNION ALL |
|
519 |
|
|
520 |
SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) * -1 AS amount, c.description AS description, c.category |
|
521 |
FROM invoice ac |
|
522 |
JOIN ap a ON (a.id = ac.trans_id) |
|
523 |
JOIN parts p ON (ac.parts_id = p.id) |
|
524 |
JOIN chart c on (p.expense_accno_id = c.id) |
|
525 |
$dpt_join |
|
526 |
WHERE (c.category = 'E') |
|
527 |
$subwhere |
|
528 |
$dpt_where |
|
529 |
$project |
|
530 |
GROUP BY c.accno, c.description, c.category |; |
|
570 | 531 |
} |
571 | 532 |
} |
572 | 533 |
|
... | ... | |
574 | 535 |
my $accno; |
575 | 536 |
my $ref; |
576 | 537 |
|
577 |
my $sth = $dbh->prepare($query); |
|
578 |
$sth->execute || $form->dberror($query); |
|
538 |
my $sth = prepare_execute_query($form, $dbh, $query); |
|
579 | 539 |
|
580 | 540 |
while ($ref = $sth->fetchrow_hashref(NAME_lc)) { |
581 | 541 |
|
... | ... | |
644 | 604 |
my $item; |
645 | 605 |
|
646 | 606 |
if ($fromdate) { |
607 |
$fromdate = conv_dateq($fromdate); |
|
647 | 608 |
if ($form->{method} eq 'cash') { |
648 |
$subwhere .= " AND transdate >= '$fromdate'";
|
|
649 |
$glwhere = " AND ac.transdate >= '$fromdate'";
|
|
650 |
$prwhere = " AND ar.transdate >= '$fromdate'";
|
|
609 |
$subwhere .= " AND (transdate >= $fromdate)";
|
|
610 |
$glwhere = " AND (ac.transdate >= $fromdate)";
|
|
611 |
$prwhere = " AND (ar.transdate >= $fromdate)";
|
|
651 | 612 |
} else { |
652 |
$where .= " AND ac.transdate >= '$fromdate'";
|
|
613 |
$where .= " AND (ac.transdate >= $fromdate)";
|
|
653 | 614 |
} |
654 | 615 |
} |
655 | 616 |
|
656 | 617 |
if ($todate) { |
657 |
$where .= " AND ac.transdate <= '$todate'"; |
|
658 |
$subwhere .= " AND transdate <= '$todate'"; |
|
659 |
$prwhere .= " AND ar.transdate <= '$todate'"; |
|
618 |
$todate = conv_dateq($todate); |
|
619 |
$where .= " AND (ac.transdate <= $todate)"; |
|
620 |
$subwhere .= " AND (transdate <= $todate)"; |
|
621 |
$prwhere .= " AND (ar.transdate <= $todate)"; |
|
660 | 622 |
} |
661 | 623 |
|
662 | 624 |
if ($department_id) { |
663 |
$dpt_join = qq| |
|
664 |
JOIN department t ON (a.department_id = t.id) |
|
665 |
|; |
|
666 |
$dpt_where = qq| |
|
667 |
AND t.id = $department_id |
|
668 |
|; |
|
625 |
$dpt_join = qq| JOIN department t ON (a.department_id = t.id) |; |
|
626 |
$dpt_where = qq| AND (t.id = | . conv_i($department_id, 'NULL') . qq|) |; |
|
669 | 627 |
} |
670 | 628 |
|
671 | 629 |
if ($form->{project_id}) { |
672 |
$project = qq| |
|
673 |
AND ac.project_id = $form->{project_id} |
|
674 |
|; |
|
630 |
$project = qq| AND (ac.project_id = | . conv_i($form->{project_id}) . qq|) |; |
|
675 | 631 |
} |
676 | 632 |
|
677 | 633 |
if ($form->{method} eq 'cash') { |
678 |
|
|
679 |
$query = qq| |
|
680 |
|
|
681 |
SELECT sum(ac.amount) AS amount, |
|
682 |
c.$category |
|
683 |
FROM acc_trans ac |
|
684 |
JOIN chart c ON (c.id = ac.chart_id) |
|
685 |
JOIN ar a ON (a.id = ac.trans_id) |
|
686 |
$dpt_join |
|
687 |
WHERE $where |
|
688 |
$dpt_where |
|
689 |
AND ac.trans_id IN |
|
690 |
( |
|
691 |
SELECT trans_id |
|
692 |
FROM acc_trans |
|
693 |
JOIN chart ON (chart_id = id) |
|
694 |
WHERE link LIKE '%AR_paid%' |
|
695 |
$subwhere |
|
696 |
) |
|
697 |
|
|
698 |
$project |
|
699 |
GROUP BY c.$category |
|
700 |
|
|
701 |
UNION |
|
702 |
|
|
703 |
SELECT sum(ac.amount) AS amount, |
|
704 |
c.$category |
|
705 |
FROM acc_trans ac |
|
706 |
JOIN chart c ON (c.id = ac.chart_id) |
|
707 |
JOIN ap a ON (a.id = ac.trans_id) |
|
708 |
$dpt_join |
|
709 |
WHERE $where |
|
710 |
$dpt_where |
|
711 |
AND ac.trans_id IN |
|
712 |
( |
|
713 |
SELECT trans_id |
|
714 |
FROM acc_trans |
|
715 |
JOIN chart ON (chart_id = id) |
|
716 |
WHERE link LIKE '%AP_paid%' |
|
717 |
$subwhere |
|
718 |
) |
|
719 |
|
|
720 |
$project |
|
721 |
GROUP BY c.$category |
|
722 |
|
|
723 |
UNION |
|
724 |
|
|
725 |
SELECT sum(ac.amount) AS amount, |
|
726 |
c.$category |
|
727 |
FROM acc_trans ac |
|
728 |
JOIN chart c ON (c.id = ac.chart_id) |
|
729 |
JOIN gl a ON (a.id = ac.trans_id) |
|
730 |
$dpt_join |
|
731 |
WHERE $where |
|
732 |
$glwhere |
|
733 |
$dpt_from |
|
734 |
AND NOT (c.link = 'AR' OR c.link = 'AP') |
|
735 |
$project |
|
736 |
GROUP BY c.$category |
|
737 |
|; |
|
634 |
$query = |
|
635 |
qq|SELECT sum(ac.amount) AS amount, c.$category |
|
636 |
FROM acc_trans ac |
|
637 |
JOIN chart c ON (c.id = ac.chart_id) |
|
638 |
JOIN ar a ON (a.id = ac.trans_id) |
|
639 |
$dpt_join |
|
640 |
WHERE $where |
|
641 |
$dpt_where |
|
642 |
AND ac.trans_id IN |
|
643 |
( |
|
644 |
SELECT trans_id |
|
645 |
FROM acc_trans |
|
646 |
JOIN chart ON (chart_id = id) |
|
647 |
WHERE (link LIKE '%AR_paid%') |
|
648 |
$subwhere |
|
649 |
) |
|
650 |
$project |
|
651 |
GROUP BY c.$category |
|
652 |
|
|
653 |
UNION |
|
654 |
|
|
655 |
SELECT sum(ac.amount) AS amount, c.$category |
|
656 |
FROM acc_trans ac |
|
657 |
JOIN chart c ON (c.id = ac.chart_id) |
|
658 |
JOIN ap a ON (a.id = ac.trans_id) |
|
659 |
$dpt_join |
|
660 |
WHERE $where |
|
661 |
$dpt_where |
|
662 |
AND ac.trans_id IN |
|
663 |
( |
|
664 |
SELECT trans_id |
|
665 |
FROM acc_trans |
|
666 |
JOIN chart ON (chart_id = id) |
|
667 |
WHERE (link LIKE '%AP_paid%') |
|
668 |
$subwhere |
|
669 |
) |
|
670 |
$project |
|
671 |
GROUP BY c.$category |
|
672 |
|
|
673 |
UNION |
|
674 |
|
|
675 |
SELECT sum(ac.amount) AS amount, c.$category |
|
676 |
FROM acc_trans ac |
|
677 |
JOIN chart c ON (c.id = ac.chart_id) |
|
678 |
JOIN gl a ON (a.id = ac.trans_id) |
|
679 |
$dpt_join |
|
680 |
WHERE $where |
|
681 |
$glwhere |
|
682 |
$dpt_from |
|
683 |
AND NOT ((c.link = 'AR') OR (c.link = 'AP')) |
|
684 |
$project |
|
685 |
GROUP BY c.$category |; |
|
738 | 686 |
|
739 | 687 |
if ($form->{project_id}) { |
740 |
|
|
741 | 688 |
$query .= qq| |
742 |
|
|
743 |
UNION |
|
744 |
|
|
745 |
SELECT SUM(ac.sellprice * ac.qty) AS amount, |
|
746 |
c.$category |
|
747 |
FROM invoice ac |
|
748 |
JOIN ar a ON (a.id = ac.trans_id) |
|
749 |
JOIN parts p ON (ac.parts_id = p.id) |
|
750 |
JOIN chart c on (p.income_accno_id = c.id) |
|
751 |
$dpt_join |
|
752 |
WHERE 1 = 1 $prwhere |
|
753 |
AND c.category = 'I' |
|
754 |
$dpt_where |
|
755 |
AND ac.trans_id IN |
|
756 |
( |
|
757 |
SELECT trans_id |
|
758 |
FROM acc_trans |
|
759 |
JOIN chart ON (chart_id = id) |
|
760 |
WHERE link LIKE '%AR_paid%' |
|
761 |
$subwhere |
|
762 |
) |
|
763 |
|
|
764 |
$project |
|
765 |
GROUP BY c.$category |
|
766 |
|
|
767 |
UNION |
|
768 |
|
|
769 |
SELECT SUM(ac.sellprice) AS amount, |
|
770 |
c.$category |
|
771 |
FROM invoice ac |
|
772 |
JOIN ap a ON (a.id = ac.trans_id) |
|
773 |
JOIN parts p ON (ac.parts_id = p.id) |
|
774 |
JOIN chart c on (p.expense_accno_id = c.id) |
|
775 |
$dpt_join |
|
776 |
WHERE 1 = 1 $prwhere |
|
777 |
AND c.category = 'E' |
|
778 |
$dpt_where |
|
779 |
AND ac.trans_id IN |
|
780 |
( |
|
781 |
SELECT trans_id |
|
782 |
FROM acc_trans |
|
783 |
JOIN chart ON (chart_id = id) |
|
784 |
WHERE link LIKE '%AP_paid%' |
|
785 |
$subwhere |
|
786 |
) |
|
787 |
|
|
788 |
$project |
|
789 |
GROUP BY c.$category |
|
790 |
|; |
|
689 |
UNION |
|
690 |
|
|
691 |
SELECT SUM(ac.sellprice * ac.qty) AS amount, c.$category |
|
692 |
FROM invoice ac |
|
693 |
JOIN ar a ON (a.id = ac.trans_id) |
|
694 |
JOIN parts p ON (ac.parts_id = p.id) |
|
695 |
JOIN chart c on (p.income_accno_id = c.id) |
|
696 |
$dpt_join |
|
697 |
WHERE (c.category = 'I') |
|
698 |
$prwhere |
|
699 |
$dpt_where |
|
700 |
AND ac.trans_id IN |
|
701 |
( |
|
702 |
SELECT trans_id |
|
703 |
FROM acc_trans |
|
704 |
JOIN chart ON (chart_id = id) |
|
705 |
WHERE (link LIKE '%AR_paid%') |
|
706 |
$subwhere |
|
707 |
) |
|
708 |
$project |
|
709 |
GROUP BY c.$category |
|
710 |
|
|
711 |
UNION |
|
712 |
|
|
713 |
SELECT SUM(ac.sellprice) AS amount, c.$category |
|
714 |
FROM invoice ac |
|
715 |
JOIN ap a ON (a.id = ac.trans_id) |
|
716 |
JOIN parts p ON (ac.parts_id = p.id) |
|
717 |
JOIN chart c on (p.expense_accno_id = c.id) |
|
718 |
$dpt_join |
|
719 |
WHERE (c.category = 'E') $prwhere |
|
720 |
$dpt_where |
|
721 |
AND ac.trans_id IN |
|
722 |
( |
|
723 |
SELECT trans_id |
|
724 |
FROM acc_trans |
|
725 |
JOIN chart ON (chart_id = id) |
|
726 |
WHERE (link LIKE '%AP_paid%') |
|
727 |
$subwhere |
|
728 |
) |
|
729 |
|
|
730 |
$project |
|
731 |
GROUP BY c.$category |; |
|
791 | 732 |
} |
792 | 733 |
|
793 |
} else { |
|
794 |
|
|
734 |
} else { # if ($form->{method} eq 'cash') |
|
795 | 735 |
if ($department_id) { |
796 |
$dpt_join = qq| |
|
797 |
JOIN dpt_trans t ON (t.trans_id = ac.trans_id) |
|
798 |
|; |
|
799 |
$dpt_where = qq| |
|
800 |
AND t.department_id = $department_id |
|
801 |
|; |
|
736 |
$dpt_join = qq| JOIN dpt_trans t ON (t.trans_id = ac.trans_id) |; |
|
737 |
$dpt_where = qq| AND (t.department_id = | . conv_i($department_id, 'NULL') . qq|) |; |
|
802 | 738 |
} |
803 | 739 |
|
804 | 740 |
$query = qq| |
805 |
|
|
806 |
SELECT sum(ac.amount) AS amount, |
|
807 |
c.$category |
|
808 |
FROM acc_trans ac |
|
809 |
JOIN chart c ON (c.id = ac.chart_id) |
|
810 |
$dpt_join |
|
811 |
WHERE $where |
|
812 |
$dpt_where |
|
813 |
$project |
|
814 |
GROUP BY c.$category |
|
815 |
|; |
|
741 |
SELECT sum(ac.amount) AS amount, c.$category |
|
742 |
FROM acc_trans ac |
|
743 |
JOIN chart c ON (c.id = ac.chart_id) |
|
744 |
$dpt_join |
|
745 |
WHERE $where |
|
746 |
$dpt_where |
|
747 |
$project |
|
748 |
GROUP BY c.$category |; |
|
816 | 749 |
|
817 | 750 |
if ($form->{project_id}) { |
818 |
|
|
819 | 751 |
$query .= qq| |
752 |
UNION |
|
820 | 753 |
|
821 |
UNION |
|
822 |
|
|
823 |
SELECT SUM(ac.sellprice * ac.qty) AS amount, |
|
824 |
c.$category |
|
825 |
FROM invoice ac |
|
826 |
JOIN ar a ON (a.id = ac.trans_id) |
|
827 |
JOIN parts p ON (ac.parts_id = p.id) |
|
828 |
JOIN chart c on (p.income_accno_id = c.id) |
|
829 |
$dpt_join |
|
830 |
WHERE 1 = 1 $prwhere |
|
831 |
AND c.category = 'I' |
|
832 |
$dpt_where |
|
833 |
$project |
|
834 |
GROUP BY c.$category |
|
835 |
|
|
836 |
UNION |
|
837 |
|
|
838 |
SELECT SUM(ac.sellprice * ac.qty) * -1 AS amount, |
|
839 |
c.$category |
|
840 |
FROM invoice ac |
|
841 |
JOIN ap a ON (a.id = ac.trans_id) |
|
842 |
JOIN parts p ON (ac.parts_id = p.id) |
|
843 |
JOIN chart c on (p.expense_accno_id = c.id) |
|
844 |
$dpt_join |
|
845 |
WHERE 1 = 1 $prwhere |
|
846 |
AND c.category = 'E' |
|
847 |
$dpt_where |
|
848 |
$project |
|
849 |
GROUP BY c.$category |
|
850 |
|; |
|
754 |
SELECT SUM(ac.sellprice * ac.qty) AS amount, c.$category |
|
755 |
FROM invoice ac |
|
756 |
JOIN ar a ON (a.id = ac.trans_id) |
|
757 |
JOIN parts p ON (ac.parts_id = p.id) |
|
758 |
JOIN chart c on (p.income_accno_id = c.id) |
|
759 |
$dpt_join |
|
760 |
WHERE (c.category = 'I') |
|
761 |
$prwhere |
|
762 |
$dpt_where |
|
763 |
$project |
|
764 |
GROUP BY c.$category |
|
851 | 765 |
|
766 |
UNION |
|
767 |
|
|
768 |
SELECT SUM(ac.sellprice * ac.qty) * -1 AS amount, c.$category |
|
769 |
FROM invoice ac |
|
770 |
JOIN ap a ON (a.id = ac.trans_id) |
|
771 |
JOIN parts p ON (ac.parts_id = p.id) |
|
772 |
JOIN chart c on (p.expense_accno_id = c.id) |
|
773 |
$dpt_join |
|
774 |
WHERE (c.category = 'E') |
|
775 |
$prwhere |
|
776 |
$dpt_where |
|
777 |
$project |
|
778 |
GROUP BY c.$category |; |
|
852 | 779 |
} |
853 | 780 |
} |
854 | 781 |
|
... | ... | |
857 | 784 |
my $ref; |
858 | 785 |
|
859 | 786 |
#print $query; |
860 |
my $sth = $dbh->prepare($query); |
|
861 |
$sth->execute || $form->dberror($query); |
|
787 |
my $sth = prepare_execute_query($form, $dbh, $query); |
|
862 | 788 |
|
863 | 789 |
while ($ref = $sth->fetchrow_hashref(NAME_lc)) { |
864 | 790 |
if ($ref->{amount} < 0) { |
... | ... | |
899 | 825 |
my $invwhere = $where; |
900 | 826 |
|
901 | 827 |
if ($department_id) { |
902 |
$dpt_join = qq| |
|
903 |
JOIN dpt_trans t ON (ac.trans_id = t.trans_id) |
|
904 |
|; |
|
905 |
$dpt_where = qq| |
|
906 |
AND t.department_id = $department_id |
|
907 |
|; |
|
828 |
$dpt_join = qq| JOIN dpt_trans t ON (ac.trans_id = t.trans_id) |; |
|
829 |
$dpt_where = qq| AND (t.department_id = | . conv_i($department_id, 'NULL') . qq|) |; |
|
908 | 830 |
} |
909 | 831 |
|
910 | 832 |
# project_id only applies to getting transactions |
... | ... | |
912 | 834 |
# but we use the same function to collect information |
913 | 835 |
|
914 | 836 |
if ($form->{project_id}) { |
915 |
$project = qq| |
|
916 |
AND ac.project_id = $form->{project_id} |
|
917 |
|; |
|
837 |
$project = qq| AND ac.project_id = | . conv_i($form->{project_id}, 'NULL') . qq|) |; |
|
918 | 838 |
} |
919 | 839 |
|
920 | 840 |
# get beginning balances |
921 | 841 |
if ($form->{fromdate}) { |
922 |
|
|
923 |
$query = qq|SELECT c.accno, c.category, SUM(ac.amount) AS amount, |
|
924 |
c.description |
|
925 |
FROM acc_trans ac |
|
926 |
JOIN chart c ON (ac.chart_id = c.id) |
|
927 |
$dpt_join |
|
928 |
WHERE ac.transdate < '$form->{fromdate}' |
|
929 |
$dpt_where |
|
930 |
$project |
|
931 |
GROUP BY c.accno, c.category, c.description |
|
932 |
|; |
|
933 |
|
|
934 |
$sth = $dbh->prepare($query); |
|
935 |
$sth->execute || $form->dberror($query); |
|
842 |
$query = |
|
843 |
qq|SELECT c.accno, c.category, SUM(ac.amount) AS amount, c.description |
|
844 |
FROM acc_trans ac |
|
845 |
JOIN chart c ON (ac.chart_id = c.id) |
|
846 |
$dpt_join |
|
847 |
WHERE (ac.transdate < ?) |
|
848 |
$dpt_where |
|
849 |
$project |
|
850 |
GROUP BY c.accno, c.category, c.description |; |
|
851 |
|
|
852 |
$sth = prepare_execute_query($form, $dbh, $query, $form->{fromdate}); |
|
936 | 853 |
|
937 | 854 |
while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { |
938 | 855 |
$balance{ $ref->{accno} } = $ref->{amount}; |
... | ... | |
949 | 866 |
} |
950 | 867 |
|
951 | 868 |
# get headings |
952 |
$query = qq|SELECT c.accno, c.description, c.category |
|
953 |
FROM chart c |
|
954 |
WHERE c.charttype = 'H' |
|
955 |
ORDER by c.accno|; |
|
869 |
$query = |
|
870 |
qq|SELECT c.accno, c.description, c.category |
|
871 |
FROM chart c |
|
872 |
WHERE c.charttype = 'H' |
|
873 |
ORDER by c.accno|; |
|
956 | 874 |
|
957 |
$sth = $dbh->prepare($query); |
|
958 |
$sth->execute || $form->dberror($query); |
|
875 |
$sth = prepare_execute_query($form, $dbh, $query); |
|
959 | 876 |
|
960 | 877 |
while ($ref = $sth->fetchrow_hashref(NAME_lc)) { |
961 | 878 |
$trb{ $ref->{accno} }{description} = $ref->{description}; |
... | ... | |
968 | 885 |
$sth->finish; |
969 | 886 |
|
970 | 887 |
$where = " 1 = 1 "; |
888 |
my $tofrom; |
|
971 | 889 |
|
972 | 890 |
if ($form->{fromdate} || $form->{todate}) { |
973 | 891 |
if ($form->{fromdate}) { |
974 |
$tofrom .= " AND ac.transdate >= '$form->{fromdate}'"; |
|
975 |
$subwhere .= " AND transdate >= '$form->{fromdate}'"; |
|
976 |
$invwhere .= " AND a.transdate >= '$form->{fromdate}'"; |
|
977 |
$glwhere = " AND ac.transdate >= '$form->{fromdate}'"; |
|
892 |
my $fromdate = conv_dateq($form->{fromdate}); |
|
893 |
$tofrom .= " AND (ac.transdate >= $fromdate)"; |
|
894 |
$subwhere .= " AND (transdate >= $fromdate)"; |
|
895 |
$invwhere .= " AND (a.transdate >= $fromdate)"; |
|
896 |
$glwhere = " AND (ac.transdate >= $fromdate)"; |
|
978 | 897 |
} |
979 | 898 |
if ($form->{todate}) { |
980 |
$tofrom .= " AND ac.transdate <= '$form->{todate}'"; |
|
981 |
$invwhere .= " AND a.transdate <= '$form->{todate}'"; |
|
982 |
$subwhere .= " AND transdate <= '$form->{todate}'"; |
|
983 |
$glwhere .= " AND ac.transdate <= '$form->{todate}'"; |
|
899 |
my $todate = conv_dateq($form->{todate}); |
|
900 |
$tofrom .= " AND (ac.transdate <= $todate)"; |
|
901 |
$invwhere .= " AND (a.transdate <= $todate)"; |
|
902 |
$subwhere .= " AND (transdate <= $todate)"; |
|
903 |
$glwhere .= " AND (ac.transdate <= $todate)"; |
|
984 | 904 |
} |
985 | 905 |
} |
906 |
|
|
986 | 907 |
if ($form->{eur}) { |
987 |
$where .= qq| AND ((ac.trans_id in (SELECT id from ar) |
|
988 |
AND ac.trans_id IN |
|
989 |
( |
|
990 |
SELECT trans_id |
|
991 |
FROM acc_trans |
|
992 |
JOIN chart ON (chart_id = id) |
|
993 |
WHERE link LIKE '%AR_paid%' |
|
994 |
$subwhere |
|
995 |
)) OR (ac.trans_id in (SELECT id from ap) |
|
996 |
AND ac.trans_id IN |
|
997 |
( |
|
998 |
SELECT trans_id |
|
999 |
FROM acc_trans |
|
1000 |
JOIN chart ON (chart_id = id) |
|
1001 |
WHERE link LIKE '%AP_paid%' |
|
1002 |
$subwhere |
|
1003 |
)) OR (ac.trans_id in (SELECT id from gl) |
|
1004 |
$glwhere))|; |
|
908 |
$where .= |
|
909 |
qq| AND ((ac.trans_id IN (SELECT id from ar) AND |
|
910 |
ac.trans_id IN |
|
911 |
( |
|
912 |
SELECT trans_id |
|
913 |
FROM acc_trans |
|
914 |
JOIN chart ON (chart_id = id) |
|
915 |
WHERE (link LIKE '%AR_paid%') |
|
916 |
$subwhere |
|
917 |
) |
|
918 |
) |
|
919 |
OR |
|
920 |
(ac.trans_id in (SELECT id from ap) AND |
|
921 |
ac.trans_id IN |
|
922 |
( |
|
923 |
SELECT trans_id |
|
924 |
FROM acc_trans |
|
925 |
JOIN chart ON (chart_id = id) |
|
926 |
WHERE (link LIKE '%AP_paid%') |
|
927 |
$subwhere |
|
928 |
) |
|
929 |
) |
|
930 |
OR |
|
931 |
(ac.trans_id in (SELECT id from gl) |
|
932 |
$glwhere) |
|
933 |
)|; |
|
1005 | 934 |
} else { |
1006 | 935 |
$where .= $tofrom; |
1007 | 936 |
} |
1008 | 937 |
|
1009 |
{ |
|
1010 |
|
|
1011 |
$query = qq|SELECT c.accno, c.description, c.category, |
|
1012 |
SUM(ac.amount) AS amount |
|
1013 |
FROM acc_trans ac |
|
1014 |
JOIN chart c ON (c.id = ac.chart_id) |
|
1015 |
$dpt_join |
|
1016 |
WHERE $where |
|
1017 |
$dpt_where |
|
1018 |
$project |
|
1019 |
GROUP BY c.accno, c.description, c.category |
|
1020 |
|; |
|
1021 |
|
|
1022 |
if ($form->{project_id}) { |
|
1023 |
|
|
1024 |
$query .= qq| |
|
1025 |
|
|
1026 |
-- add project transactions from invoice |
|
1027 |
|
|
1028 |
UNION ALL |
|
1029 |
|
|
1030 |
SELECT c.accno, c.description, c.category, |
|
1031 |
SUM(ac.sellprice * ac.qty) AS amount |
|
1032 |
FROM invoice ac |
|
1033 |
JOIN ar a ON (ac.trans_id = a.id) |
|
1034 |
JOIN parts p ON (ac.parts_id = p.id) |
|
1035 |
JOIN chart c ON (p.income_accno_id = c.id) |
|
1036 |
$dpt_join |
|
1037 |
WHERE $invwhere |
|
1038 |
$dpt_where |
|
1039 |
$project |
|
1040 |
GROUP BY c.accno, c.description, c.category |
|
1041 |
|
|
1042 |
UNION ALL |
|
1043 |
|
|
1044 |
SELECT c.accno, c.description, c.category, |
|
1045 |
SUM(ac.sellprice * ac.qty) * -1 AS amount |
|
1046 |
FROM invoice ac |
|
1047 |
JOIN ap a ON (ac.trans_id = a.id) |
|
1048 |
JOIN parts p ON (ac.parts_id = p.id) |
|
1049 |
JOIN chart c ON (p.expense_accno_id = c.id) |
|
1050 |
$dpt_join |
|
1051 |
WHERE $invwhere |
|
1052 |
$dpt_where |
|
1053 |
$project |
|
1054 |
GROUP BY c.accno, c.description, c.category |
|
1055 |
|; |
|
1056 |
} |
|
1057 |
|
|
1058 |
$query .= qq| |
|
1059 |
ORDER BY accno|; |
|
1060 |
|
|
1061 |
} |
|
1062 |
|
|
1063 |
$sth = $dbh->prepare($query); |
|
1064 |
$sth->execute || $form->dberror($query); |
|
1065 |
|
|
1066 |
# prepare query for each account |
|
1067 |
$query = qq|SELECT (SELECT SUM(ac.amount) * -1 |
|
1068 |
FROM acc_trans ac |
|
1069 |
JOIN chart c ON (c.id = ac.chart_id) |
|
1070 |
$dpt_join |
|
1071 |
WHERE $where |
|
1072 |
$dpt_where |
|
1073 |
$project |
|
1074 |
AND ac.amount < 0 |
|
1075 |
AND c.accno = ?) AS debit, |
|
1076 |
|
|
1077 |
(SELECT SUM(ac.amount) |
|
1078 |
FROM acc_trans ac |
|
1079 |
JOIN chart c ON (c.id = ac.chart_id) |
|
1080 |
$dpt_join |
|
1081 |
WHERE $where |
|
1082 |
$dpt_where |
|
1083 |
$project |
|
1084 |
AND ac.amount > 0 |
|
1085 |
AND c.accno = ?) AS credit |
|
1086 |
|; |
|
1087 |
|
|
1088 |
$drcr = $dbh->prepare($query); |
|
938 |
$query = qq| |
|
939 |
SELECT c.accno, c.description, c.category, SUM(ac.amount) AS amount |
|
940 |
FROM acc_trans ac |
|
941 |
JOIN chart c ON (c.id = ac.chart_id) |
|
942 |
$dpt_join |
|
943 |
WHERE $where |
|
944 |
$dpt_where |
|
945 |
$project |
|
946 |
GROUP BY c.accno, c.description, c.category |; |
|
1089 | 947 |
|
1090 | 948 |
if ($form->{project_id}) { |
949 |
$query .= qq| |
|
950 |
-- add project transactions from invoice |
|
951 |
|
|
952 |
UNION ALL |
|
953 |
|
|
954 |
SELECT c.accno, c.description, c.category, SUM(ac.sellprice * ac.qty) AS amount |
|
955 |
FROM invoice ac |
|
956 |
JOIN ar a ON (ac.trans_id = a.id) |
|
957 |
JOIN parts p ON (ac.parts_id = p.id) |
|
958 |
JOIN chart c ON (p.income_accno_id = c.id) |
|
959 |
$dpt_join |
|
960 |
WHERE $invwhere |
|
961 |
$dpt_where |
|
962 |
$project |
|
963 |
GROUP BY c.accno, c.description, c.category |
|
964 |
|
|
965 |
UNION ALL |
|
966 |
|
|
967 |
SELECT c.accno, c.description, c.category, SUM(ac.sellprice * ac.qty) * -1 AS amount |
|
968 |
FROM invoice ac |
|
969 |
JOIN ap a ON (ac.trans_id = a.id) |
|
970 |
JOIN parts p ON (ac.parts_id = p.id) |
|
971 |
JOIN chart c ON (p.expense_accno_id = c.id) |
|
972 |
$dpt_join |
|
973 |
WHERE $invwhere |
|
974 |
$dpt_where |
|
975 |
$project |
|
976 |
GROUP BY c.accno, c.description, c.category |
|
977 |
|; |
|
978 |
} |
|
1091 | 979 |
|
1092 |
# prepare query for each account |
|
1093 |
$query = qq|SELECT (SELECT SUM(ac.sellprice * ac.qty) * -1 |
|
1094 |
FROM invoice ac |
|
1095 |
JOIN parts p ON (ac.parts_id = p.id) |
|
1096 |
JOIN ap a ON (ac.trans_id = a.id) |
|
1097 |
JOIN chart c ON (p.expense_accno_id = c.id) |
|
1098 |
$dpt_join |
|
1099 |
WHERE $invwhere |
|
1100 |
$dpt_where |
|
1101 |
$project |
|
1102 |
AND c.accno = ?) AS debit, |
|
1103 |
|
|
1104 |
(SELECT SUM(ac.sellprice * ac.qty) |
|
1105 |
FROM invoice ac |
|
1106 |
JOIN parts p ON (ac.parts_id = p.id) |
|
1107 |
JOIN ar a ON (ac.trans_id = a.id) |
|
1108 |
JOIN chart c ON (p.income_accno_id = c.id) |
|
1109 |
$dpt_join |
|
1110 |
WHERE $invwhere |
|
1111 |
$dpt_where |
|
1112 |
$project |
|
1113 |
AND c.accno = ?) AS credit |
|
1114 |
|; |
|
1115 |
|
|
1116 |
$project_drcr = $dbh->prepare($query); |
|
980 |
$query .= qq| ORDER BY accno|; |
|
1117 | 981 |
|
1118 |
}
|
|
982 |
$sth = prepare_execute_query($form, $dbh, $query);
|
|
1119 | 983 |
|
1120 | 984 |
# calculate the debit and credit in the period |
1121 | 985 |
while ($ref = $sth->fetchrow_hashref(NAME_lc)) { |
... | ... | |
1126 | 990 |
} |
1127 | 991 |
$sth->finish; |
1128 | 992 |
|
993 |
# prepare query for each account |
|
994 |
my ($q_drcr, $drcr, $q_project_drcr, $project_drcr); |
|
995 |
|
|
996 |
$q_drcr = |
|
997 |
qq|SELECT |
|
998 |
(SELECT SUM(ac.amount) * -1 |
|
999 |
FROM acc_trans ac |
|
1000 |
JOIN chart c ON (c.id = ac.chart_id) |
|
1001 |
$dpt_join |
|
1002 |
WHERE $where |
|
1003 |
$dpt_where |
|
1004 |
$project |
|
1005 |
AND (ac.amount < 0) |
|
1006 |
AND (c.accno = ?)) AS debit, |
|
1007 |
|
|
1008 |
(SELECT SUM(ac.amount) |
|
1009 |
FROM acc_trans ac |
|
1010 |
JOIN chart c ON (c.id = ac.chart_id) |
|
1011 |
$dpt_join |
|
1012 |
WHERE $where |
|
1013 |
$dpt_where |
|
1014 |
$project |
|
1015 |
AND ac.amount > 0 |
|
1016 |
AND c.accno = ?) AS credit |; |
|
1017 |
$drcr = prepare_query($form, $dbh, $q_drcr); |
|
1018 |
|
|
1019 |
if ($form->{project_id}) { |
|
1020 |
# prepare query for each account |
|
1021 |
$q_project_drcr = |
|
1022 |
qq|SELECT |
|
1023 |
(SELECT SUM(ac.sellprice * ac.qty) * -1 |
|
1024 |
FROM invoice ac |
|
1025 |
JOIN parts p ON (ac.parts_id = p.id) |
|
1026 |
JOIN ap a ON (ac.trans_id = a.id) |
|
1027 |
JOIN chart c ON (p.expense_accno_id = c.id) |
|
1028 |
$dpt_join |
|
1029 |
WHERE $invwhere |
|
1030 |
$dpt_where |
|
1031 |
$project |
|
1032 |
AND c.accno = ?) AS debit, |
|
1033 |
|
|
1034 |
(SELECT SUM(ac.sellprice * ac.qty) |
|
1035 |
FROM invoice ac |
|
1036 |
JOIN parts p ON (ac.parts_id = p.id) |
|
1037 |
JOIN ar a ON (ac.trans_id = a.id) |
|
1038 |
JOIN chart c ON (p.income_accno_id = c.id) |
|
1039 |
$dpt_join |
|
1040 |
WHERE $invwhere |
|
1041 |
$dpt_where |
|
1042 |
$project |
|
1043 |
AND c.accno = ?) AS credit |; |
|
1044 |
|
|
1045 |
$project_drcr = prepare_query($form, $dbh, $q_project_drcr); |
|
1046 |
} |
|
1047 |
|
|
1129 | 1048 |
my ($debit, $credit); |
1130 | 1049 |
|
1131 | 1050 |
foreach my $accno (sort keys %trb) { |
1132 |
$ref = ();
|
|
1051 |
$ref = {};
|
|
1133 | 1052 |
|
1134 | 1053 |
$ref->{accno} = $accno; |
1135 | 1054 |
map { $ref->{$_} = $trb{$accno}{$_} } |
... | ... | |
1140 | 1059 |
if ($trb{$accno}{charttype} eq 'A') { |
1141 | 1060 |
|
1142 | 1061 |
# get DR/CR |
1143 |
$drcr->execute($ref->{accno}, $ref->{accno}) || $form->dberror($query);
|
|
1062 |
do_statement($form, $drcr, $q_drcr, $ref->{accno}, $ref->{accno});
|
|
1144 | 1063 |
|
1145 | 1064 |
($debit, $credit) = (0, 0); |
1146 | 1065 |
while (($debit, $credit) = $drcr->fetchrow_array) { |
... | ... | |
1152 | 1071 |
if ($form->{project_id}) { |
1153 | 1072 |
|
1154 | 1073 |
# get DR/CR |
1155 |
$project_drcr->execute($ref->{accno}, $ref->{accno}) |
|
1156 |
|| $form->dberror($query); |
|
1074 |
do_statement($form, $project_drcr, $q_project_drcr, $ref->{accno}, $ref->{accno}); |
|
1157 | 1075 |
|
1158 | 1076 |
($debit, $credit) = (0, 0); |
1159 | 1077 |
while (($debit, $credit) = $project_drcr->fetchrow_array) { |
... | ... | |
1198 | 1116 |
sub get_storno { |
1199 | 1117 |
$main::lxdebug->enter_sub(); |
1200 | 1118 |
my ($self, $dbh, $form) = @_; |
1201 |
my $query = qq|SELECT invnumber FROM $form->{arap} WHERE invnumber LIKE "Storno zu "|; |
|
1119 |
my $arap = $form->{arap} eq "ar" ? "ar" : "ap"; |
|
1120 |
my $query = qq|SELECT invnumber FROM $arap WHERE invnumber LIKE "Storno zu "|; |
|
1202 | 1121 |
my $sth = $dbh->prepare($query); |
1203 | 1122 |
while(my $ref = $sth->fetchrow_hashref()) { |
1204 |
$ref->{invnumer} =~ s/Storno zu //g;
|
|
1123 |
$ref->{invnumer} =~ s/Storno zu //g; |
|
1205 | 1124 |
$form->{storno}{$ref->{invnumber}} = 1; |
1206 | 1125 |
} |
1207 | 1126 |
$main::lxdebug->leave_sub(); |
... | ... | |
1214 | 1133 |
|
1215 | 1134 |
# connect to database |
1216 | 1135 |
my $dbh = $form->dbconnect($myconfig); |
1217 |
my $invoice = ($form->{arap} eq 'ar') ? 'is' : 'ir'; |
|
1136 |
|
|
1137 |
my ($invoice, $arap, $buysell, $ct, $ct_id); |
|
1138 |
|
|
1139 |
if ($form->{ct} eq "customer") { |
|
1140 |
$invoice = "is"; |
|
1141 |
$arap = "ar"; |
|
1142 |
$buysell = "buy"; |
|
1143 |
$ct = "customer"; |
|
1144 |
} else { |
|
1145 |
$invoice = "ir"; |
|
1146 |
$arap = "ap"; |
|
1147 |
$buysell = "sell"; |
|
1148 |
$ct = "vendor"; |
|
1149 |
} |
|
1150 |
$ct_id = "${ct}_id"; |
|
1218 | 1151 |
|
1219 | 1152 |
$form->{todate} = $form->current_date($myconfig) unless ($form->{todate}); |
1153 |
my $todate = conv_dateq($form->{todate}); |
|
1220 | 1154 |
|
1221 | 1155 |
my $where = " 1 = 1 "; |
1222 | 1156 |
my ($name, $null); |
1223 | 1157 |
|
1224 |
if ($form->{"$form->{ct}_id"}) { |
|
1225 |
$where .= qq| AND ct.id = $form->{"$form->{ct}_id"}|; |
|
1226 |
} else { |
|
1227 |
if ($form->{ $form->{ct} }) { |
|
1228 |
$name = $form->like(lc $form->{ $form->{ct} }); |
|
1229 |
$where .= qq| AND lower(ct.name) LIKE '$name'| if $form->{ $form->{ct} }; |
|
1230 |
} |
|
1158 |
if ($form->{$ct_id}) { |
|
1159 |
$where .= qq| AND (ct.id = | . conv_i($form->{$ct_id}) . qq|)|; |
|
1160 |
} elsif ($form->{ $form->{ct} }) { |
|
1161 |
$where .= qq| AND (ct.name ILIKE | . $dbh->quote('%' . $form->{$ct} . '%') . qq|)|; |
|
1231 | 1162 |
} |
1232 | 1163 |
|
1233 | 1164 |
my $dpt_join; |
1234 | 1165 |
if ($form->{department}) { |
1235 | 1166 |
($null, $department_id) = split /--/, $form->{department}; |
1236 |
$dpt_join = qq| |
|
1237 |
JOIN department d ON (a.department_id = d.id) |
|
1238 |
|; |
|
1239 |
|
|
1240 |
$where .= qq| AND a.department_id = $department_id|; |
|
1241 |
} |
|
1167 |
$dpt_join = qq| JOIN department d ON (a.department_id = d.id) |; |
|
1168 |
$where .= qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|)|; |
|
1169 |
} |
|
1170 |
|
|
1171 |
my $q_details = qq| |
|
1172 |
-- between 0-30 days |
|
1173 |
|
|
1174 |
SELECT ${ct}.id AS ctid, ${ct}.name, |
|
1175 |
street, zipcode, city, country, contact, email, |
|
1176 |
phone as customerphone, fax as customerfax, ${ct}number, |
|
1177 |
"invnumber", "transdate", |
|
1178 |
(amount - paid) as "c0", 0.00 as "c30", 0.00 as "c60", 0.00 as "c90", |
|
1179 |
"duedate", invoice, ${arap}.id, |
|
1180 |
(SELECT $buysell |
|
1181 |
FROM exchangerate |
|
1182 |
WHERE (${arap}.curr = exchangerate.curr) |
|
1183 |
AND (exchangerate.transdate = ${arap}.transdate)) AS exchangerate |
|
1184 |
FROM ${arap}, ${ct} |
|
1185 |
WHERE (paid != amount) |
|
1186 |
AND (${arap}.storno IS FALSE) |
|
1187 |
AND (${arap}.${ct}_id = ${ct}.id) |
|
1188 |
AND (${ct}.id = ?) |
|
1189 |
AND (transdate <= (date $todate - interval '0 days')) |
|
1190 |
AND (transdate >= (date $todate - interval '30 days')) |
|
1191 |
|
|
1192 |
UNION |
|
1193 |
|
|
1194 |
-- between 31-60 days |
|
1195 |
|
|
1196 |
SELECT ${ct}.id AS ctid, ${ct}.name, |
|
1197 |
street, zipcode, city, country, contact, email, |
|
1198 |
phone as customerphone, fax as customerfax, ${ct}number, |
|
1199 |
"invnumber", "transdate", |
|
1200 |
0.00 as "c0", (amount - paid) as "c30", 0.00 as "c60", 0.00 as "c90", |
|
1201 |
"duedate", invoice, ${arap}.id, |
|
1202 |
(SELECT $buysell |
|
1203 |
FROM exchangerate |
|
1204 |
WHERE (${arap}.curr = exchangerate.curr) |
|
1205 |
AND (exchangerate.transdate = ${arap}.transdate)) AS exchangerate |
|
1206 |
FROM ${arap}, ${ct} |
|
1207 |
WHERE (paid != amount) |
|
1208 |
AND (${arap}.storno IS FALSE) |
|
1209 |
AND (${arap}.${ct}_id = ${ct}.id) |
|
1210 |
AND (${ct}.id = ?) |
|
1211 |
AND (transdate < (date $todate - interval '30 days')) |
|
1212 |
AND (transdate >= (date $todate - interval '60 days')) |
|
1213 |
|
|
1214 |
UNION |
|
1215 |
|
|
1216 |
-- between 61-90 days |
|
1217 |
|
|
1218 |
SELECT ${ct}.id AS ctid, ${ct}.name, |
|
1219 |
street, zipcode, city, country, contact, email, |
|
1220 |
phone as customerphone, fax as customerfax, ${ct}number, |
|
1221 |
"invnumber", "transdate", |
|
1222 |
0.00 as "c0", 0.00 as "c30", (amount - paid) as "c60", 0.00 as "c90", |
|
1223 |
"duedate", invoice, ${arap}.id, |
|
1224 |
(SELECT $buysell |
|
1225 |
FROM exchangerate |
|
1226 |
WHERE (${arap}.curr = exchangerate.curr) |
|
1227 |
AND (exchangerate.transdate = ${arap}.transdate)) AS exchangerate |
|
1228 |
FROM ${arap}, ${ct} |
|
1229 |
WHERE (paid != amount) |
|
1230 |
AND (${arap}.storno IS FALSE) |
|
1231 |
AND (${arap}.${ct}_id = ${ct}.id) |
|
1232 |
AND (${ct}.id = ?) |
|
1233 |
AND (transdate < (date $todate - interval '60 days')) |
|
1234 |
AND (transdate >= (date $todate - interval '90 days')) |
|
1235 |
|
|
1236 |
UNION |
|
1237 |
|
|
1238 |
-- over 90 days |
|
1239 |
|
|
1240 |
SELECT ${ct}.id AS ctid, ${ct}.name, |
|
1241 |
street, zipcode, city, country, contact, email, |
|
1242 |
phone as customerphone, fax as customerfax, ${ct}number, |
|
1243 |
"invnumber", "transdate", |
|
1244 |
0.00 as "c0", 0.00 as "c30", 0.00 as "c60", (amount - paid) as "c90", |
|
1245 |
"duedate", invoice, ${arap}.id, |
|
1246 |
(SELECT $buysell |
|
1247 |
FROM exchangerate |
|
1248 |
WHERE (${arap}.curr = exchangerate.curr) |
|
1249 |
AND (exchangerate.transdate = ${arap}.transdate)) AS exchangerate |
|
1250 |
FROM ${arap}, ${ct} |
|
1251 |
WHERE (paid != amount) |
|
1252 |
AND (${arap}.storno IS FALSE) |
|
1253 |
AND (${arap}.${ct}_id = ${ct}.id) |
|
1254 |
AND (${ct}.id = ?) |
|
1255 |
AND (transdate < (date $todate - interval '90 days')) |
|
1256 |
|
|
1257 |
ORDER BY ctid, transdate, invnumber |; |
|
1258 |
|
|
1259 |
my $sth_details = prepare_query($form, $dbh, $q_details); |
|
1242 | 1260 |
|
1243 | 1261 |
# select outstanding vendors or customers, depends on $ct |
1244 |
my $query = qq|SELECT DISTINCT ct.id, ct.name |
|
1245 |
FROM $form->{ct} ct, $form->{arap} a |
|
1246 |
$dpt_join |
|
1247 |
WHERE $where |
|
1248 |
AND a.$form->{ct}_id = ct.id |
|
1249 |
AND a.paid != a.amount |
|
1250 |
AND (a.transdate <= '$form->{todate}') |
|
1251 |
ORDER BY ct.name|; |
|
1252 |
|
|
1253 |
my $sth = $dbh->prepare($query); |
|
1254 |
$sth->execute || $form->dberror; |
|
1255 |
|
|
1256 |
my $buysell = ($form->{arap} eq 'ar') ? 'buy' : 'sell'; |
|
1257 |
|
|
1262 |
my $query = |
|
1263 |
qq|SELECT DISTINCT ct.id, ct.name |
|
1264 |
FROM $ct ct, $arap a |
|
1265 |
$dpt_join |
|
1266 |
WHERE $where |
|
1267 |
AND (a.${ct_id} = ct.id) |
|
1268 |
AND (a.paid != a.amount) |
|
1269 |
AND (a.transdate <= $todate) |
|
1270 |
ORDER BY ct.name|; |
|
1271 |
|
|
1272 |
my $sth = prepare_execute_query($form, $dbh, $query); |
|
1273 |
|
|
1274 |
$form->{AG} = []; |
|
1258 | 1275 |
# for each company that has some stuff outstanding |
1259 | 1276 |
while (my ($id) = $sth->fetchrow_array) { |
1277 |
do_statement($form, $sth_details, $q_details, $id, $id, $id, $id); |
|
1260 | 1278 |
|
1261 |
$query = qq| |
|
1262 |
|
|
1263 |
-- between 0-30 days |
|
1264 |
|
|
1265 |
SELECT $form->{ct}.id AS ctid, $form->{ct}.name, |
|
1266 |
street, zipcode, city, country, contact, email, |
|
1267 |
phone as customerphone, fax as customerfax, $form->{ct}number, |
|
1268 |
"invnumber", "transdate", |
|
1269 |
(amount - paid) as "c0", 0.00 as "c30", 0.00 as "c60", 0.00 as "c90", |
|
1270 |
"duedate", invoice, $form->{arap}.id, |
|
1271 |
(SELECT $buysell FROM exchangerate |
|
1272 |
WHERE $form->{arap}.curr = exchangerate.curr |
|
1273 |
AND exchangerate.transdate = $form->{arap}.transdate) AS exchangerate |
|
1274 |
FROM $form->{arap}, $form->{ct} |
|
1275 |
WHERE paid != amount |
|
1276 |
AND $form->{arap}.storno IS FALSE |
|
1277 |
AND $form->{arap}.$form->{ct}_id = $form->{ct}.id |
|
1278 |
AND $form->{ct}.id = $id |
|
1279 |
AND ( |
|
1280 |
transdate <= (date '$form->{todate}' - interval '0 days') |
|
1281 |
AND transdate >= (date '$form->{todate}' - interval '30 days') |
|
1282 |
) |
|
1283 |
|
|
1284 |
UNION |
|
1285 |
|
|
1286 |
-- between 31-60 days |
|
1287 |
|
|
1288 |
SELECT $form->{ct}.id AS ctid, $form->{ct}.name, |
|
1289 |
street, zipcode, city, country, contact, email, |
|
1290 |
phone as customerphone, fax as customerfax, $form->{ct}number, |
|
1291 |
"invnumber", "transdate", |
|
1292 |
0.00 as "c0", (amount - paid) as "c30", 0.00 as "c60", 0.00 as "c90", |
|
1293 |
"duedate", invoice, $form->{arap}.id, |
|
1294 |
(SELECT $buysell FROM exchangerate |
|
1295 |
WHERE $form->{arap}.curr = exchangerate.curr |
|
1296 |
AND exchangerate.transdate = $form->{arap}.transdate) AS exchangerate |
|
1297 |
FROM $form->{arap}, $form->{ct} |
|
1298 |
WHERE paid != amount |
|
1299 |
AND $form->{arap}.storno IS FALSE |
|
1300 |
AND $form->{arap}.$form->{ct}_id = $form->{ct}.id |
|
1301 |
AND $form->{ct}.id = $id |
|
1302 |
AND ( |
|
1303 |
transdate < (date '$form->{todate}' - interval '30 days') |
|
1304 |
AND transdate >= (date '$form->{todate}' - interval '60 days') |
|
1305 |
) |
|
1306 |
|
|
1307 |
UNION |
|
1308 |
|
|
1309 |
-- between 61-90 days |
|
1310 |
|
|
1311 |
SELECT $form->{ct}.id AS ctid, $form->{ct}.name, |
|
1312 |
street, zipcode, city, country, contact, email, |
|
1313 |
phone as customerphone, fax as customerfax, $form->{ct}number, |
|
1314 |
"invnumber", "transdate", |
|
1315 |
0.00 as "c0", 0.00 as "c30", (amount - paid) as "c60", 0.00 as "c90", |
|
1316 |
"duedate", invoice, $form->{arap}.id, |
|
1317 |
(SELECT $buysell FROM exchangerate |
|
1318 |
WHERE $form->{arap}.curr = exchangerate.curr |
|
1319 |
AND exchangerate.transdate = $form->{arap}.transdate) AS exchangerate |
|
1320 |
FROM $form->{arap}, $form->{ct} |
|
1321 |
WHERE paid != amount |
|
1322 |
AND $form->{arap}.storno IS FALSE |
|
1323 |
AND $form->{arap}.$form->{ct}_id = $form->{ct}.id |
|
1324 |
AND $form->{ct}.id = $id |
|
1325 |
AND ( |
|
1326 |
transdate < (date '$form->{todate}' - interval '60 days') |
|
1327 |
AND transdate >= (date '$form->{todate}' - interval '90 days') |
|
1328 |
) |
|
1329 |
|
|
1330 |
UNION |
|
1331 |
|
|
1332 |
-- over 90 days |
|
1333 |
|
|
1334 |
SELECT $form->{ct}.id AS ctid, $form->{ct}.name, |
|
1335 |
street, zipcode, city, country, contact, email, |
|
1336 |
phone as customerphone, fax as customerfax, $form->{ct}number, |
|
1337 |
"invnumber", "transdate", |
|
1338 |
0.00 as "c0", 0.00 as "c30", 0.00 as "c60", (amount - paid) as "c90", |
|
1339 |
"duedate", invoice, $form->{arap}.id, |
|
1340 |
(SELECT $buysell FROM exchangerate |
|
1341 |
WHERE $form->{arap}.curr = exchangerate.curr |
|
1342 |
AND exchangerate.transdate = $form->{arap}.transdate) AS exchangerate |
|
1343 |
FROM $form->{arap}, $form->{ct} |
Auch abrufbar als: Unified diff
Umstellung von RP.pm auf die Verwendung von parametrisierten Queries bzw. Verwendung von gequoteten Variablen zur Vermeidung von SQL injections.