1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #======================================================================
33 # DS. 2000-07-04 Created
35 #======================================================================
42 $main::lxdebug->enter_sub();
44 my ($self, $myconfig, $form) = @_;
49 my $dbh = $form->dbconnect($myconfig);
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 | .
56 my $sth = $dbh->prepare($query);
57 $sth->execute || $form->dberror($query);
59 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
60 $amount{ $ref->{accno} } = $ref->{amount};
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) ! .
73 qq! ( SELECT accno FROM chart c2 WHERE c2.id = c.id ) AS new_account ! .
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 ! .
81 my $sth = prepare_execute_query($form, $dbh, $query);
84 while (my $ca = $sth->fetchrow_hashref(NAME_lc)) {
85 $ca->{amount} = $amount{ $ca->{accno} };
86 if ($ca->{amount} < 0) {
87 $ca->{debit} = $ca->{amount} * -1;
89 $ca->{credit} = $ca->{amount};
91 push(@{ $form->{CA} }, $ca);
97 $main::lxdebug->leave_sub();
100 sub all_transactions {
101 $main::lxdebug->enter_sub();
103 my ($self, $myconfig, $form) = @_;
105 # connect to database
106 my $dbh = $form->dbconnect($myconfig);
109 my $query = qq|SELECT id FROM chart WHERE accno = ?|;
110 my @id = selectall_array_query($form, $dbh, $query, $form->{accno});
115 my $where = qq|1 = 1|;
117 # build WHERE clause from dates if any
118 # if ($form->{fromdate}) {
119 # $where .= " AND ac.transdate >= '$form->{fromdate}'";
121 # if ($form->{todate}) {
122 # $where .= " AND ac.transdate <= '$form->{todate}'";
125 my (@values, @where_values, @subwhere_values);
126 if ($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}));
133 if ($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}));
141 my $sortorder = join ', ',
142 $form->sort_columns(qw(transdate reference description));
143 my $false = ($myconfig->{dbdriver} eq 'Pg') ? FALSE: q|'0'|;
145 # Oracle workaround, use ordinal positions
146 my %ordinal = (transdate => 4,
149 map { $sortorder =~ s/$_/$ordinal{$_}/ } keys %ordinal;
151 my ($null, $department_id) = split(/--/, $form->{department});
152 my ($dpt_where, $dpt_join, @department_values);
153 if ($department_id) {
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);
159 my ($project, @project_values);
160 if ($form->{project_id}) {
161 $project = qq| AND ac.project_id = ? |;
162 @project_values = (conv_i($form->{project_id}));
165 if ($form->{accno}) {
167 # get category for account
168 $query = qq|SELECT category FROM chart WHERE accno = ?|;
169 ($form->{category}) = selectrow_query($form, $dbh, $query, $form->{accno});
171 if ($form->{fromdate}) {
172 # get beginning balance
174 qq|SELECT SUM(ac.amount) | .
175 qq|FROM acc_trans ac | .
176 qq|JOIN chart c ON (ac.chart_id = c.id) | .
178 qq|WHERE c.accno = ? | .
179 qq|AND ac.transdate < ? | .
182 @values = ($form->{accno}, conv_date($form->{fromdate}),
183 @department_values, @project_values);
185 if ($form->{project_id}) {
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) | .
195 qq|WHERE c.accno = ? | .
196 qq| AND a.transdate < ? | .
197 qq| AND c.category = 'I' | .
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) | .
209 qq|WHERE c.accno = ? | .
210 qq| AND a.transdate < ? | .
211 qq| AND c.category = 'E' | .
216 $form->{accno}, conv_date($form->{transdate}),
217 @department_values, @project_values,
218 $form->{accno}, conv_date($form->{transdate}),
219 @department_values, @project_values);
222 ($form->{balance}) = selectrow_query($form, $dbh, $query, @values);
230 foreach my $id (@id) {
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.
241 # get all transactions
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 | .
248 qq|WHERE | . $where . $dpt_where . $project .
249 qq| AND ac.chart_id = ? | .
250 qq| AND ac.trans_id = a.id | .
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 | .
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 | .
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 | .
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 |;
275 @where_values, @department_values, @project_values, $id,
276 @where_values, @department_values, @project_values, $id,
277 @where_values, @department_values, @project_values, $id);
279 $union = qq|UNION ALL |;
281 if ($form->{project_id}) {
283 $fromdate_where =~ s/ac\./a\./;
284 $todate_where =~ s/ac\./a\./;
289 qq|SELECT a.id, a.invnumber, c.name, a.transdate, | .
290 qq| a.invoice, ac.qty * ac.sellprice AS sellprice, 'ar' as module | .
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) | .
296 qq|WHERE p.income_accno_id = ? | .
304 qq|SELECT a.id, a.invnumber, v.name, a.transdate, | .
305 qq| a.invoice, ac.qty * ac.sellprice AS sellprice, 'ap' as module | .
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) | .
311 qq|WHERE p.expense_accno_id = ? | .
318 $id, @department_values, @project_values,
319 $id, @department_values, @project_values);
321 $fromdate_where =~ s/a\./ac\./;
322 $todate_where =~ s/a\./ac\./;
326 $union = qq|UNION ALL|;
329 $query .= qq|ORDER BY | . $sortorder;
330 $sth = prepare_execute_query($form, $dbh, $query, @values);
333 while (my $ca = $sth->fetchrow_hashref(NAME_lc)) {
335 if ($ca->{module} eq "ap") {
336 $ca->{module} = ($ca->{invoice}) ? 'ir' : 'ap';
340 if ($ca->{module} eq "ar") {
341 $ca->{module} = ($ca->{invoice}) ? 'is' : 'ar';
344 if ($ca->{amount} < 0) {
345 $ca->{debit} = $ca->{amount} * -1;
348 $ca->{credit} = $ca->{amount};
352 push(@{ $form->{CA} }, $ca);
359 $main::lxdebug->leave_sub();