X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FRP.pm;h=365d6e0235d4c2dd81f733efc731d2db2840f5fd;hb=a2aca2e1d8136681d2a95fefd240d509581c8bc8;hp=19af9d8a744eb948e166979a4219a3d1c114f288;hpb=064a0998401fc3c208fd2df315e2150ccac90488;p=kivitendo-erp.git diff --git a/SL/RP.pm b/SL/RP.pm index 19af9d8a7..365d6e023 100644 --- a/SL/RP.pm +++ b/SL/RP.pm @@ -803,33 +803,46 @@ sub trial_balance { $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}, 'NULL') . qq|) |; } + my $acc_cash_where = ""; + my $ar_cash_where = ""; + my $ap_cash_where = ""; + + + if ($form->{method} eq "cash") { + $acc_cash_where = qq| AND (ac.trans_id IN (SELECT id FROM ar WHERE datepaid>='$form->{fromdate}' AND datepaid<='$form->{todate}' UNION SELECT id FROM ap WHERE datepaid>='$form->{fromdate}' AND datepaid<='$form->{todate}' UNION SELECT id FROM gl WHERE transdate>='$form->{fromdate}' AND transdate<='$form->{todate}')) |; + $ar_ap_cash_where = qq| AND (a.datepaid>='$form->{fromdate}' AND a.datepaid<='$form->{todate}') |; + } + # get beginning balances - if ($form->{fromdate}) { - $query = - qq|SELECT c.accno, c.category, SUM(ac.amount) AS amount, c.description - FROM acc_trans ac - JOIN chart c ON (ac.chart_id = c.id) - $dpt_join - WHERE (ac.transdate < ?) - $dpt_where - $project - GROUP BY c.accno, c.category, c.description |; + $query = + qq|SELECT c.accno, c.category, SUM(ac.amount) AS amount, c.description + FROM acc_trans ac + LEFT JOIN chart c ON (ac.chart_id = c.id) + $dpt_join + WHERE ((select date_trunc('year', ac.transdate::date)) = (select date_trunc('year', ?::date))) AND ac.ob_transaction $acc_cash_where + $dpt_where + $project + GROUP BY c.accno, c.category, c.description |; - $sth = prepare_execute_query($form, $dbh, $query, $form->{fromdate}); + $sth = prepare_execute_query($form, $dbh, $query, $form->{fromdate}); - while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { - $balance{ $ref->{accno} } = $ref->{amount}; + while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { - if ($ref->{amount} != 0 && $form->{all_accounts}) { - $trb{ $ref->{accno} }{description} = $ref->{description}; - $trb{ $ref->{accno} }{charttype} = 'A'; - $trb{ $ref->{accno} }{category} = $ref->{category}; + if ($ref->{amount} != 0 || $form->{all_accounts}) { + $trb{ $ref->{accno} }{description} = $ref->{description}; + $trb{ $ref->{accno} }{charttype} = 'A'; + + if ($ref->{amount} > 0) { + $trb{ $ref->{accno} }{haben_eb} = $ref->{amount}; + } else { + $trb{ $ref->{accno} }{soll_eb} = $ref->{amount} * -1; } - + $trb{ $ref->{accno} }{category} = $ref->{category}; } - $sth->finish; } + $sth->finish; + # get headings $query = @@ -851,6 +864,8 @@ sub trial_balance { $sth->finish; $where = " 1 = 1 "; + $saldowhere = " 1 = 1 "; + $sumwhere = " 1 = 1 "; my $tofrom; if ($form->{fromdate} || $form->{todate}) { @@ -858,19 +873,27 @@ sub trial_balance { my $fromdate = conv_dateq($form->{fromdate}); $tofrom .= " AND (ac.transdate >= $fromdate)"; $subwhere .= " AND (transdate >= $fromdate)"; + $sumsubwhere .= " AND (transdate >= (select date_trunc('year', date $fromdate))) "; + $saldosubwhere .= " AND transdate>=(select date_trunc('year', date $fromdate)) "; $invwhere .= " AND (a.transdate >= $fromdate)"; + $glsaldowhere .= " AND ac.transdate>=(select date_trunc('year', date $fromdate)) "; $glwhere = " AND (ac.transdate >= $fromdate)"; + $glsumwhere = " AND (ac.transdate >= (select date_trunc('year', date $fromdate))) "; } if ($form->{todate}) { my $todate = conv_dateq($form->{todate}); $tofrom .= " AND (ac.transdate <= $todate)"; $invwhere .= " AND (a.transdate <= $todate)"; + $saldosubwhere .= " AND (transdate <= $todate)"; + $sumsubwhere .= " AND (transdate <= $todate)"; $subwhere .= " AND (transdate <= $todate)"; $glwhere .= " AND (ac.transdate <= $todate)"; - } + $glsumwhere .= " AND (ac.transdate <= $todate) "; + $glsaldowhere .= " AND (ac.transdate <= $todate) "; + } } - if ($form->{eur}) { + if ($form->{method} eq "cash") { $where .= qq| AND ((ac.trans_id IN (SELECT id from ar) AND ac.trans_id IN @@ -897,8 +920,63 @@ sub trial_balance { (ac.trans_id in (SELECT id from gl) $glwhere) )|; + $saldowhere .= +qq| AND ((ac.trans_id IN (SELECT id from ar) AND + ac.trans_id IN + ( + SELECT trans_id + FROM acc_trans + JOIN chart ON (chart_id = id) + WHERE (link LIKE '%AR_paid%') + $saldosubwhere + ) + ) + OR + (ac.trans_id in (SELECT id from ap) AND + ac.trans_id IN + ( + SELECT trans_id + FROM acc_trans + JOIN chart ON (chart_id = id) + WHERE (link LIKE '%AP_paid%') + $saldosubwhere + ) + ) + OR + (ac.trans_id in (SELECT id from gl) + $glsaldowhere) + )|; + $sumwhere .= +qq| AND ((ac.trans_id IN (SELECT id from ar) AND + ac.trans_id IN + ( + SELECT trans_id + FROM acc_trans + JOIN chart ON (chart_id = id) + WHERE (link LIKE '%AR_paid%') + $sumsubwhere + ) + ) + OR + (ac.trans_id in (SELECT id from ap) AND + ac.trans_id IN + ( + SELECT trans_id + FROM acc_trans + JOIN chart ON (chart_id = id) + WHERE (link LIKE '%AP_paid%') + $sumsubwhere + ) + ) + OR + (ac.trans_id in (SELECT id from gl) + $glsumwhere) + )|; + } else { - $where .= $tofrom; + $where .= $tofrom . " AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)"; + $saldowhere .= $glsaldowhere . " AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)"; + $sumwhere .= $glsumwhere . " AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)"; } $query = qq| @@ -979,7 +1057,46 @@ sub trial_balance { $dpt_where $project AND ac.amount > 0 - AND c.accno = ?) AS credit |; + AND c.accno = ?) AS credit, + (SELECT SUM(ac.amount) + FROM acc_trans ac + JOIN chart c ON (ac.chart_id = c.id) + $dpt_join + WHERE $saldowhere + $dpt_where + $project + AND c.accno = ?) AS saldo, + + (SELECT SUM(ac.amount) + FROM acc_trans ac + JOIN chart c ON (ac.chart_id = c.id) + $dpt_join + WHERE $sumwhere + $dpt_where + $project + AND amount > 0 + AND c.accno = ?) AS sum_credit, + + (SELECT SUM(ac.amount) + FROM acc_trans ac + JOIN chart c ON (ac.chart_id = c.id) + $dpt_join + WHERE $sumwhere + $dpt_where + $project + AND amount < 0 + AND c.accno = ?) AS sum_debit, + + (SELECT max(ac.transdate) FROM acc_trans ac + JOIN chart c ON (ac.chart_id = c.id) + $dpt_join + WHERE $where + $dpt_where + $project + AND c.accno = ?) AS last_transaction + + + |; $drcr = prepare_query($form, $dbh, $q_drcr); @@ -1007,50 +1124,109 @@ sub trial_balance { WHERE $invwhere $dpt_where $project - AND c.accno = ?) AS credit |; + AND c.accno = ?) AS credit, + + (SELECT SUM(ac.amount) + FROM acc_trans ac + JOIN chart c ON (ac.chart_id = c.id) + $dpt_join + WHERE $saldowhere + $dpt_where + $project + AND c.accno = ?) AS saldo, + + (SELECT SUM(ac.amount) + FROM acc_trans ac + JOIN chart c ON (ac.chart_id = c.id) + $dpt_join + WHERE $sumwhere + $dpt_where + $project + AND amount > 0 + AND c.accno = ?) AS sum_credit, + + (SELECT SUM(ac.amount) + FROM acc_trans ac + JOIN chart c ON (ac.chart_id = c.id) + $dpt_join + WHERE $sumwhere + $dpt_where + $project + AND amount < 0 + AND c.accno = ?) AS sum_debit, + + + (SELECT max(ac.transdate) FROM acc_trans ac + JOIN chart c ON (ac.chart_id = c.id) + $dpt_join + WHERE $where + $dpt_where + $project + AND c.accno = ?) AS last_transaction + |; $project_drcr = prepare_query($form, $dbh, $q_project_drcr); } - my ($debit, $credit); + + my ($debit, $credit, $saldo, $soll_saldo, $haben_saldo,$soll_kummuliert, $haben_kummuliert, $last_transaction); foreach my $accno (sort keys %trb) { $ref = {}; $ref->{accno} = $accno; map { $ref->{$_} = $trb{$accno}{$_} } - qw(description category charttype amount); + qw(description category charttype amount soll_eb haben_eb); $ref->{balance} = $form->round_amount($balance{ $ref->{accno} }, 2); if ($trb{$accno}{charttype} eq 'A') { # get DR/CR - do_statement($form, $drcr, $q_drcr, $ref->{accno}, $ref->{accno}); + do_statement($form, $drcr, $q_drcr, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}); - ($debit, $credit) = (0, 0); - while (($debit, $credit) = $drcr->fetchrow_array) { + ($debit, $credit, $saldo, $haben_saldo, $soll_saldo, $soll_kumuliert, $haben_kumuliert) = (0, 0, 0, 0, 0, 0, 0); + $last_transaction = ""; + while (($debit, $credit, $saldo, $haben_kumuliert, $soll_kumuliert, $last_transaction) = $drcr->fetchrow_array) { $ref->{debit} += $debit; $ref->{credit} += $credit; + if ($saldo >= 0) { + $ref->{haben_saldo} += $saldo; + } else { + $ref->{soll_saldo} += $saldo * -1; + } + $ref->{last_transaction} = $last_transaction; + $ref->{soll_kumuliert} = $soll_kumuliert * -1; + $ref->{haben_kumuliert} = $haben_kumuliert; } $drcr->finish; if ($form->{project_id}) { # get DR/CR - do_statement($form, $project_drcr, $q_project_drcr, $ref->{accno}, $ref->{accno}); + do_statement($form, $project_drcr, $q_project_drcr, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}); ($debit, $credit) = (0, 0); - while (($debit, $credit) = $project_drcr->fetchrow_array) { + while (($debit, $credit, $saldo, $haben_kumuliert, $soll_kumuliert, $last_transaction) = $project_drcr->fetchrow_array) { $ref->{debit} += $debit; $ref->{credit} += $credit; + if ($saldo >= 0) { + $ref->{haben_saldo} += $saldo; + } else { + $ref->{soll_saldo} += $saldo * -1; + } + $ref->{soll_kumuliert} += $soll_kumuliert * -1; + $ref->{haben_kumuliert} += $haben_kumuliert; } $project_drcr->finish; } $ref->{debit} = $form->round_amount($ref->{debit}, 2); $ref->{credit} = $form->round_amount($ref->{credit}, 2); - + $ref->{haben_saldo} = $form->round_amount($ref->{haben_saldo}, 2); + $ref->{soll_saldo} = $form->round_amount($ref->{soll_saldo}, 2); + $ref->{haben_kumuliert} = $form->round_amount($ref->{haben_kumuliert}, 2); + $ref->{soll_kumuliert} = $form->round_amount($ref->{soll_kumuliert}, 2); } # add subtotal @@ -1059,6 +1235,10 @@ sub trial_balance { if ($accno) { $trb{$accno}{debit} += $ref->{debit}; $trb{$accno}{credit} += $ref->{credit}; + $trb{$accno}{soll_saldo} += $ref->{soll_saldo}; + $trb{$accno}{haben_saldo} += $ref->{haben_saldo}; + $trb{$accno}{soll_kumuliert} += $ref->{soll_kumuliert}; + $trb{$accno}{haben_kumuliert} += $ref->{haben_kumuliert}; } push @{ $form->{TB} }, $ref; @@ -1073,7 +1253,10 @@ sub trial_balance { if ($accno eq $ref->{accno}) { $ref->{debit} = $trb{$accno}{debit}; $ref->{credit} = $trb{$accno}{credit}; - } + $ref->{soll_saldo} = $trb{$accno}{soll_saldo}; + $ref->{haben_saldo} = $trb{$accno}{haben_saldo}; + $ref->{soll_kumuliert} = $trb{$accno}{soll_kumuliert}; + $ref->{haben_kumuliert} = $trb{$accno}{haben_kumuliert}; } } } @@ -1101,23 +1284,28 @@ sub aging { # connect to database my $dbh = $form->dbconnect($myconfig); - my ($invoice, $arap, $buysell, $ct, $ct_id); + my ($invoice, $arap, $buysell, $ct, $ct_id, $ml); if ($form->{ct} eq "customer") { $invoice = "is"; $arap = "ar"; $buysell = "buy"; $ct = "customer"; + $ml = -1; } else { $invoice = "ir"; $arap = "ap"; $buysell = "sell"; $ct = "vendor"; + $ml = 1; } $ct_id = "${ct}_id"; $form->{todate} = $form->current_date($myconfig) unless ($form->{todate}); my $todate = conv_dateq($form->{todate}); + my $fromdate = conv_dateq($form->{fromdate}); + + my $fromwhere = ($form->{fromdate} ne "") ? " AND (transdate >= (date $fromdate)) " : ""; my $where = " 1 = 1 "; my ($name, $null); @@ -1142,84 +1330,18 @@ sub aging { street, zipcode, city, country, contact, email, phone as customerphone, fax as customerfax, ${ct}number, "invnumber", "transdate", - (amount - paid) as "c0", 0.00 as "c30", 0.00 as "c60", 0.00 as "c90", + (amount - COALESCE((SELECT sum(amount)*$ml FROM acc_trans LEFT JOIN chart ON (acc_trans.chart_id=chart.id) WHERE link ilike '%paid%' AND acc_trans.trans_id=${arap}.id AND acc_trans.transdate <= (date $todate)),0)) as "open", "amount", "duedate", invoice, ${arap}.id, (SELECT $buysell FROM exchangerate WHERE (${arap}.curr = exchangerate.curr) AND (exchangerate.transdate = ${arap}.transdate)) AS exchangerate FROM ${arap}, ${ct} - WHERE (paid != amount) - AND (${arap}.storno IS FALSE) - AND (${arap}.${ct}_id = ${ct}.id) - AND (${ct}.id = ?) - AND (transdate <= (date $todate - interval '0 days')) - AND (transdate >= (date $todate - interval '30 days')) - - UNION - - -- between 31-60 days - - SELECT ${ct}.id AS ctid, ${ct}.name, - street, zipcode, city, country, contact, email, - phone as customerphone, fax as customerfax, ${ct}number, - "invnumber", "transdate", - 0.00 as "c0", (amount - paid) as "c30", 0.00 as "c60", 0.00 as "c90", - "duedate", invoice, ${arap}.id, - (SELECT $buysell - FROM exchangerate - WHERE (${arap}.curr = exchangerate.curr) - AND (exchangerate.transdate = ${arap}.transdate)) AS exchangerate - FROM ${arap}, ${ct} - WHERE (paid != amount) - AND (${arap}.storno IS FALSE) - AND (${arap}.${ct}_id = ${ct}.id) - AND (${ct}.id = ?) - AND (transdate < (date $todate - interval '30 days')) - AND (transdate >= (date $todate - interval '60 days')) - - UNION - - -- between 61-90 days - - SELECT ${ct}.id AS ctid, ${ct}.name, - street, zipcode, city, country, contact, email, - phone as customerphone, fax as customerfax, ${ct}number, - "invnumber", "transdate", - 0.00 as "c0", 0.00 as "c30", (amount - paid) as "c60", 0.00 as "c90", - "duedate", invoice, ${arap}.id, - (SELECT $buysell - FROM exchangerate - WHERE (${arap}.curr = exchangerate.curr) - AND (exchangerate.transdate = ${arap}.transdate)) AS exchangerate - FROM ${arap}, ${ct} - WHERE (paid != amount) - AND (${arap}.storno IS FALSE) - AND (${arap}.${ct}_id = ${ct}.id) - AND (${ct}.id = ?) - AND (transdate < (date $todate - interval '60 days')) - AND (transdate >= (date $todate - interval '90 days')) - - UNION - - -- over 90 days - - SELECT ${ct}.id AS ctid, ${ct}.name, - street, zipcode, city, country, contact, email, - phone as customerphone, fax as customerfax, ${ct}number, - "invnumber", "transdate", - 0.00 as "c0", 0.00 as "c30", 0.00 as "c60", (amount - paid) as "c90", - "duedate", invoice, ${arap}.id, - (SELECT $buysell - FROM exchangerate - WHERE (${arap}.curr = exchangerate.curr) - AND (exchangerate.transdate = ${arap}.transdate)) AS exchangerate - FROM ${arap}, ${ct} - WHERE (paid != amount) + WHERE ((paid != amount) OR (datepaid > (date $todate) AND datepaid is not null)) AND (${arap}.storno IS FALSE) AND (${arap}.${ct}_id = ${ct}.id) AND (${ct}.id = ?) - AND (transdate < (date $todate - interval '90 days')) + AND (transdate <= (date $todate) $fromwhere ) ORDER BY ctid, transdate, invnumber |; @@ -1232,8 +1354,8 @@ sub aging { $dpt_join WHERE $where AND (a.${ct_id} = ct.id) - AND (a.paid != a.amount) - AND (a.transdate <= $todate) + AND ((a.paid != a.amount) OR ((a.datepaid > $todate) AND (datepaid is NOT NULL))) + AND (a.transdate <= $todate $fromwhere) ORDER BY ct.name|; my $sth = prepare_execute_query($form, $dbh, $query); @@ -1241,7 +1363,7 @@ sub aging { $form->{AG} = []; # for each company that has some stuff outstanding while (my ($id) = $sth->fetchrow_array) { - do_statement($form, $sth_details, $q_details, $id, $id, $id, $id); + do_statement($form, $sth_details, $q_details, $id); while (my $ref = $sth_details->fetchrow_hashref(NAME_lc)) { $ref->{module} = ($ref->{invoice}) ? $invoice : $arap;