X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FRP.pm;h=d9a09eec50c2511df43b3e9ed51f11ed938746d8;hb=1410996dd10264247f118f1ac6f3a8daaf5e55e6;hp=1355cc83e1ecd2826b5e5c4e809c8ca1a8beab32;hpb=c9a7e79e10591ae95b1523f2c7f249eae8439580;p=kivitendo-erp.git diff --git a/SL/RP.pm b/SL/RP.pm index 1355cc83e..d9a09eec5 100644 --- a/SL/RP.pm +++ b/SL/RP.pm @@ -609,7 +609,7 @@ sub get_accounts_g { if ($form->{method} eq 'cash') { $subwhere .= " AND (transdate >= $fromdate)"; $glwhere = " AND (ac.transdate >= $fromdate)"; - $prwhere = " AND (ar.transdate >= $fromdate)"; + $prwhere = " AND (a.transdate >= $fromdate)"; } else { $where .= " AND (ac.transdate >= $fromdate)"; } @@ -619,7 +619,7 @@ sub get_accounts_g { $todate = conv_dateq($todate); $subwhere .= " AND (transdate <= $todate)"; $where .= " AND (ac.transdate <= $todate)"; - $prwhere .= " AND (ar.transdate <= $todate)"; + $prwhere .= " AND (a.transdate <= $todate)"; } if ($department_id) { @@ -642,7 +642,7 @@ sub get_accounts_g { WHERE $where $dpt_where AND ac.trans_id IN ( SELECT trans_id FROM acc_trans JOIN chart ON (chart_id = id) WHERE (link LIKE '%AR_paid%') $subwhere) $project - GROUP BY c.$category + GROUP BY c.$category UNION @@ -654,7 +654,7 @@ sub get_accounts_g { WHERE $where $dpt_where AND ac.trans_id IN ( SELECT trans_id FROM acc_trans JOIN chart ON (chart_id = id) WHERE (link LIKE '%AP_paid%') $subwhere) $project - GROUP BY c.$category + GROUP BY c.$category UNION @@ -663,16 +663,14 @@ sub get_accounts_g { JOIN chart c ON (c.id = ac.chart_id) JOIN gl a ON (a.id = ac.trans_id) $dpt_join - WHERE $where $dpt_where $glwhere + WHERE $where $dpt_where $glwhere AND NOT ((c.link = 'AR') OR (c.link = 'AP')) $project - - $project_union - GROUP BY c.$category + GROUP BY c.$category |; if ($form->{project_id}) { - $project_union = qq| + $query .= qq| UNION SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category @@ -684,7 +682,7 @@ sub get_accounts_g { WHERE (c.category = 'I') $prwhere $dpt_where AND ac.trans_id IN ( SELECT trans_id FROM acc_trans JOIN chart ON (chart_id = id) WHERE (link LIKE '%AR_paid%') $subwhere) $project - GROUP BY c.$category + GROUP BY c.$category UNION @@ -697,7 +695,7 @@ sub get_accounts_g { WHERE (c.category = 'E') $prwhere $dpt_where AND ac.trans_id IN ( SELECT trans_id FROM acc_trans JOIN chart ON (chart_id = id) WHERE (link LIKE '%AP_paid%') $subwhere) $project - GROUP BY c.$category + GROUP BY c.$category |; } @@ -803,13 +801,23 @@ 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 $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) + LEFT JOIN chart c ON (ac.chart_id = c.id) $dpt_join - WHERE (ac.transdate < (select date_trunc('year', ?::date))) + 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 |; @@ -821,12 +829,11 @@ sub trial_balance { if ($ref->{amount} != 0 || $form->{all_accounts}) { $trb{ $ref->{accno} }{description} = $ref->{description}; $trb{ $ref->{accno} }{charttype} = 'A'; - if ($ref->{category} ne "I" && $ref->{category} ne "E") { - if ($ref->{amount} > 0) { - $trb{ $ref->{accno} }{haben_eb} = $ref->{amount}; - } else { - $trb{ $ref->{accno} }{soll_eb} = $ref->{amount} * -1; - } + + 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}; } @@ -865,9 +872,9 @@ sub trial_balance { $tofrom .= " AND (ac.transdate >= $fromdate)"; $subwhere .= " AND (transdate >= $fromdate)"; $sumsubwhere .= " AND (transdate >= (select date_trunc('year', date $fromdate))) "; - $saldosubwhere .= " AND (((c.category='I' OR c.category='E') AND transdate>=(select date_trunc('year', date $fromdate))) OR (c.category NOT IN ('I', 'E'))) "; + $saldosubwhere .= " AND transdate>=(select date_trunc('year', date $fromdate)) "; $invwhere .= " AND (a.transdate >= $fromdate)"; - $glsaldowhere .= " AND (((c.category='I' OR c.category='E') AND ac.transdate>=(select date_trunc('year', date $fromdate))) OR (c.category NOT IN ('I', 'E'))) "; + $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))) "; } @@ -911,7 +918,7 @@ sub trial_balance { (ac.trans_id in (SELECT id from gl) $glwhere) )|; - $saldowhere .= + $saldowhere .= qq| AND ((ac.trans_id IN (SELECT id from ar) AND ac.trans_id IN ( @@ -937,7 +944,7 @@ qq| AND ((ac.trans_id IN (SELECT id from ar) AND (ac.trans_id in (SELECT id from gl) $glsaldowhere) )|; - $sumwhere .= + $sumwhere .= qq| AND ((ac.trans_id IN (SELECT id from ar) AND ac.trans_id IN ( @@ -965,9 +972,9 @@ qq| AND ((ac.trans_id IN (SELECT id from ar) AND )|; } else { - $where .= $tofrom; - $saldowhere .= $glsaldowhere; - $sumwhere .= $glsumwhere; + $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| @@ -1159,6 +1166,7 @@ qq| AND ((ac.trans_id IN (SELECT id from ar) AND $project_drcr = prepare_query($form, $dbh, $q_project_drcr); } + my ($debit, $credit, $saldo, $soll_saldo, $haben_saldo,$soll_kummuliert, $haben_kummuliert, $last_transaction); foreach my $accno (sort keys %trb) { @@ -1293,6 +1301,9 @@ sub aging { $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); @@ -1328,7 +1339,7 @@ sub aging { AND (${arap}.storno IS FALSE) AND (${arap}.${ct}_id = ${ct}.id) AND (${ct}.id = ?) - AND (transdate <= (date $todate)) + AND (transdate <= (date $todate) $fromwhere ) ORDER BY ctid, transdate, invnumber |; @@ -1341,8 +1352,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); @@ -1619,8 +1630,32 @@ sub payments { $where .= " AND (ac.memo ILIKE " . $dbh->quote('%' . $form->{memo} . '%') . ") "; } - my $sortorder = join(', ', qw(name invnumber ordnumber transdate source)); - $sortorder = $form->{sort} if ($form->{sort} && grep({ $_ eq $form->{sort} } qw(transdate invnumber name source memo))); + my %sort_columns = ( + 'transdate' => [ qw(transdate lower_invnumber lower_name) ], + 'invnumber' => [ qw(lower_invnumber lower_name transdate) ], + 'name' => [ qw(lower_name transdate) ], + 'source' => [ qw(lower_source) ], + 'memo' => [ qw(lower_memo) ], + ); + my %lowered_columns = ( + 'invnumber' => { 'gl' => 'g.reference', 'arap' => 'a.invnumber', }, + 'memo' => { 'gl' => 'ac.memo', 'arap' => 'ac.memo', }, + 'source' => { 'gl' => 'ac.source', 'arap' => 'ac.source', }, + 'name' => { 'gl' => 'g.description', 'arap' => 'c.name', }, + ); + + my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC'; + my $sortkey = $sort_columns{$form->{sort}} ? $form->{sort} : 'transdate'; + my $sortorder = join ', ', map { "$_ $sortdir" } @{ $sort_columns{$sortkey} }; + + + my %columns_for_sorting = ( 'gl' => '', 'arap' => '', ); + foreach my $spec (@{ $sort_columns{$sortkey} }) { + next if ($spec !~ m/^lower_(.*)$/); + + my $column = $1; + map { $columns_for_sorting{$_} .= sprintf(', lower(%s) AS lower_%s', $lowered_columns{$column}->{$_}, $column) } qw(gl arap); + } $query = qq|SELECT id, accno, description FROM chart WHERE accno = ?|; my $sth = prepare_query($form, $dbh, $query); @@ -1629,6 +1664,7 @@ sub payments { qq|SELECT c.name, a.invnumber, a.ordnumber, ac.transdate, ac.amount * $ml AS paid, ac.source, a.invoice, a.id, ac.memo, '${arap}' AS module + $columns_for_sorting{arap} FROM acc_trans ac JOIN $arap a ON (ac.trans_id = a.id) JOIN $table c ON (c.id = a.${table}_id) @@ -1642,6 +1678,7 @@ sub payments { SELECT g.description, g.reference, NULL AS ordnumber, ac.transdate, ac.amount * $ml AS paid, ac.source, '0' as invoice, g.id, ac.memo, 'gl' AS module + $columns_for_sorting{gl} FROM acc_trans ac JOIN gl g ON (g.id = ac.trans_id) $dpt_join