Subversion Repositories sql-ledger-old

Rev

Rev 114 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 finnarne 1
#=====================================================================
2
# SQL-Ledger Accounting
3
# Copyright (C) 2001
4
#
5
#  Author: Dieter Simader
6
#   Email: dsimader@sql-ledger.org
7
#     Web: http://www.sql-ledger.org
8
#
9
#  Contributors:
10
#
11
# This program is free software; you can redistribute it and/or modify
12
# it under the terms of the GNU General Public License as published by
13
# the Free Software Foundation; either version 2 of the License, or
14
# (at your option) any later version.
15
#
16
# This program is distributed in the hope that it will be useful,
17
# but WITHOUT ANY WARRANTY; without even the implied warranty of
18
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
# GNU General Public License for more details.
20
# You should have received a copy of the GNU General Public License
21
# along with this program; if not, write to the Free Software
22
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23
#======================================================================
24
#
25
# Accounts Receivable module backend routines
26
#
27
#======================================================================
28
 
29
package AR;
30
 
31
 
32
sub post_transaction {
33
  my ($self, $myconfig, $form) = @_;
34
 
35
  my ($null, $taxrate, $amount, $tax, $diff);
36
  my $exchangerate = 0;
37
  my $i;
38
 
39
  # split and store id numbers in link accounts
40
  map { ($form->{AR}{"amount_$_"}) = split(/--/, $form->{"AR_amount_$_"}) } (1 .. $form->{rowcount});
41
  ($form->{AR}{receivables}) = split(/--/, $form->{AR});
42
 
43
  if ($form->{currency} eq $form->{defaultcurrency}) {
44
    $form->{exchangerate} = 1;
45
  } else {
46
    $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{transdate}, 'buy');
47
  }
48
 
49
  $form->{exchangerate} = ($exchangerate) ? $exchangerate : $form->parse_amount($myconfig, $form->{exchangerate});
50
 
51
  for $i (1 .. $form->{rowcount}) {
52
    $form->{"amount_$i"} = $form->round_amount($form->parse_amount($myconfig, $form->{"amount_$i"}) * $form->{exchangerate}, 2);
53
    $amount += $form->{"amount_$i"};
54
  }
55
 
56
  # this is for ar
57
  $form->{amount} = $amount;
58
 
59
  # taxincluded doesn't make sense if there is no amount
60
  $form->{taxincluded} = 0 if ($form->{amount} == 0);
61
 
62
  foreach my $item (split / /, $form->{taxaccounts}) {
63
    $form->{AR}{"tax_$item"} = $item;
64
 
65
    $amount = $form->round_amount($form->parse_amount($myconfig, $form->{"tax_$item"}), 2);
66
 
67
    $form->{"tax_$item"} = $form->round_amount($amount * $form->{exchangerate}, 2);
68
    $form->{total_tax} += $form->{"tax_$item"};
69
 
70
  }
71
 
72
  # adjust paidaccounts if there is no date in the last row
73
  $form->{paidaccounts}-- unless ($form->{"datepaid_$form->{paidaccounts}"});
74
 
75
  $form->{invpaid} = 0;
76
  # add payments
77
  for $i (1 .. $form->{paidaccounts}) {
78
    $form->{"paid_$i"} = $form->round_amount($form->parse_amount($myconfig, $form->{"paid_$i"}), 2);
79
 
80
    $form->{invpaid} += $form->{"paid_$i"};
81
    $form->{datepaid} = $form->{"datepaid_$i"};
82
 
83
    # reverse payment
84
    $form->{"paid_$i"} *= -1;
85
 
86
  }
87
 
88
  $form->{invpaid} = $form->round_amount($form->{invpaid} * $form->{exchangerate}, 2);
89
 
90
  if ($form->{taxincluded} *= 1) {
91
    for $i (1 .. $form->{rowcount}) {
92
      $tax = $form->{total_tax} * $form->{"amount_$i"} / $form->{amount};
93
      $amount = $form->{"amount_$i"} - $tax;
94
      $form->{"amount_$i"} = $form->round_amount($amount, 2);
95
      $diff += $amount - $form->{"amount_$i"};
96
    }
97
 
98
    $form->{amount} -= $form->{total_tax};
99
    # deduct difference from amount_1
100
    $form->{amount_1} += $form->round_amount($diff, 2);
101
  }
102
 
103
  # store invoice total, this goes into ar table
104
  $form->{invtotal} = $form->{amount} + $form->{total_tax};
105
 
106
  # connect to database
107
  my $dbh = $form->dbconnect_noauto($myconfig);
108
 
109
  my ($query, $sth);
110
 
111
  # if we have an id delete old records
112
  if ($form->{id}) {
113
 
114
    # delete detail records
115
    $query = qq|DELETE FROM acc_trans WHERE trans_id = $form->{id}|;
116
    $dbh->do($query) || $form->dberror($query);
117
 
118
  } else {
119
    my $uid = time;
120
    $uid .= $form->{login};
121
 
122
    $query = qq|INSERT INTO ar (invnumber, employee_id)
123
                VALUES ('$uid', (SELECT id FROM employee
124
                                 WHERE login = '$form->{login}') )|;
125
    $dbh->do($query) || $form->dberror($query);
126
 
127
    $query = qq|SELECT id FROM ar
128
                WHERE invnumber = '$uid'|;
129
    $sth = $dbh->prepare($query);
130
    $sth->execute || $form->dberror($query);
131
 
132
    ($form->{id}) = $sth->fetchrow_array;
133
    $sth->finish;
134
 
135
  }
136
 
137
  # escape '
138
  $form->{notes} =~ s/'/''/g;
139
 
140
  # record last payment date in ar table
141
  $form->{datepaid} = $form->{transdate} unless $form->{datepaid};
142
  my $datepaid = ($form->{invpaid} != 0) ? qq|'$form->{datepaid}'| : 'NULL';
143
 
144
  $query = qq|UPDATE ar set
145
              invnumber = '$form->{invnumber}',
146
              ordnumber = '$form->{ordnumber}',
147
              transdate = '$form->{transdate}',
148
              customer_id = $form->{customer_id},
149
              taxincluded = '$form->{taxincluded}',
150
              amount = $form->{invtotal},
151
              duedate = '$form->{duedate}',
152
              paid = $form->{invpaid},
153
              datepaid = $datepaid,
154
              netamount = $form->{amount},
155
              curr = '$form->{currency}',
156
              notes = '$form->{notes}'
157
              WHERE id = $form->{id}|;
158
  $dbh->do($query) || $form->dberror($query);
159
 
160
 
161
  # amount for AR account
162
  $form->{receivables} = $form->round_amount($form->{invtotal} * -1, 2);
163
 
164
 
165
  # update exchangerate
166
  if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
167
    $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, $form->{exchangerate}, 0);
168
  }
169
 
170
  # add individual transactions for AR, amount and taxes
171
  foreach my $item (keys %{ $form->{AR} }) {
114 finnarne 172
 
2 finnarne 173
    if ($form->{$item} != 0) {
174
      $project_id = 'NULL';
175
      if ($item =~ /amount_/) {
114 finnarne 176
        if ($form->{"projectnumber_$'"}) {
177
 
178
          $form->{"projectnumber_$'"} =~ s/'/''/g;
179
 
180
          $project_id = qq|(SELECT id
181
                            FROM project
182
                            WHERE projectnumber = '$form->{"projectnumber_$'"}')|;
2 finnarne 183
        }
184
      }
185
 
186
      # insert detail records in acc_trans
187
      $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
188
                                         project_id)
189
                  VALUES ($form->{id}, (SELECT id FROM chart
190
                                        WHERE accno = '$form->{AR}{$item}'),
191
                  $form->{$item}, '$form->{transdate}', $project_id)|;
192
      $dbh->do($query) || $form->dberror($query);
193
    }
194
  }
195
 
196
  # if there is no amount but a payment record a receivables
197
  if ($form->{amount} == 0 && $form->{invtotal} == 0) {
198
    $form->{receivables} = $form->{invpaid} * -1;
199
  }
200
 
201
  # add paid transactions
202
  for my $i (1 .. $form->{paidaccounts}) {
203
    if ($form->{"paid_$i"} != 0) {
204
 
205
       ($form->{AR}{"paid_$i"}) = split(/--/, $form->{"AR_paid_$i"});
206
      $form->{"datepaid_$i"} = $form->{transdate} unless ($form->{"datepaid_$i"});
207
 
208
      $exchangerate = 0;
209
      if ($form->{currency} eq $form->{defaultcurrency}) {
210
        $form->{"exchangerate_$i"} = 1;
211
      } else {
212
        $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
213
 
214
        $form->{"exchangerate_$i"} = ($exchangerate) ? $exchangerate : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
215
      }
216
 
217
 
218
      # if there is no amount and invtotal is zero there is no exchangerate
219
      if ($form->{amount} == 0 && $form->{invtotal} == 0) {
220
        $form->{exchangerate} = $form->{"exchangerate_$i"};
221
      }
222
 
223
      # receivables amount
224
      $amount = $form->round_amount($form->{"paid_$i"} * $form->{exchangerate} * -1, 2);
225
 
226
      if ($form->{receivables} != 0) {
227
        # add receivable
228
        $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
229
                    transdate)
230
                    VALUES ($form->{id},
231
                           (SELECT id FROM chart
232
                            WHERE accno = '$form->{AR}{receivables}'),
233
                    $amount, '$form->{"datepaid_$i"}')|;
234
        $dbh->do($query) || $form->dberror($query);
235
      }
236
      $form->{receivables} = $amount;
237
 
238
      # add payment
239
      $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
240
                  transdate, source)
241
                  VALUES ($form->{id},
242
                         (SELECT id FROM chart
243
                          WHERE accno = '$form->{AR}{"paid_$i"}'),
244
                  $form->{"paid_$i"}, '$form->{"datepaid_$i"}',
245
                  '$form->{"source_$i"}')|;
246
      $dbh->do($query) || $form->dberror($query);
247
 
248
 
249
      # exchangerate difference for payment
250
      $amount = $form->round_amount($form->{"paid_$i"} * ($form->{"exchangerate_$i"} - 1), 2);
251
 
252
      if ($amount != 0) {
253
        $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
254
                    transdate, fx_transaction, cleared)
255
                    VALUES ($form->{id},
256
                           (SELECT id FROM chart
257
                            WHERE accno = '$form->{AR}{"paid_$i"}'),
258
                    $amount, '$form->{"datepaid_$i"}', '1', '0')|;
259
        $dbh->do($query) || $form->dberror($query);
260
      }
261
 
262
      # exchangerate gain/loss
263
      $amount = $form->round_amount($form->{"paid_$i"} * ($form->{exchangerate} - $form->{"exchangerate_$i"}), 2);
264
 
265
      if ($amount != 0) {
266
        $accno = ($amount > 0) ? $form->{fxgain_accno} : $form->{fxloss_accno};
267
        $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
268
                    transdate, fx_transaction, cleared)
269
                    VALUES ($form->{id}, (SELECT id FROM chart
270
                                          WHERE accno = '$accno'),
271
                    $amount, '$form->{"datepaid_$i"}', '1', '0')|;
272
        $dbh->do($query) || $form->dberror($query);
273
      }
274
 
275
      # update exchangerate record
276
      if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
277
        $form->update_exchangerate($dbh, $form->{currency}, $form->{"datepaid_$i"}, $form->{"exchangerate_$i"}, 0);
278
      }
279
    }
280
  }
281
 
282
 
283
  my $rc = $dbh->commit;
284
  $dbh->disconnect;
285
 
286
  $rc;
287
 
288
}
289
 
290
 
291
 
292
sub delete_transaction {
293
  my ($self, $myconfig, $form) = @_;
294
 
295
  # connect to database, turn AutoCommit off
296
  my $dbh = $form->dbconnect_noauto($myconfig);
297
 
298
  # check for other foreign currency transactions
299
  $form->delete_exchangerate($dbh) if ($form->{currency} ne $form->{defaultcurrency});
300
 
301
  my $query = qq|DELETE FROM ar WHERE id = $form->{id}|;
302
  $dbh->do($query) || $form->dberror($query);
303
 
304
  $query = qq|DELETE FROM acc_trans WHERE trans_id = $form->{id}|;
305
  $dbh->do($query) || $form->dberror($query);
306
 
307
  # commit
308
  my $rc = $dbh->commit;
309
  $dbh->disconnect;
310
 
311
  $rc;
312
 
313
}
314
 
315
 
316
 
317
sub ar_transactions {
318
  my ($self, $myconfig, $form) = @_;
319
 
320
  # connect to database
321
  my $dbh = $form->dbconnect($myconfig);
322
 
114 finnarne 323
  my $paid = "a.paid";
324
 
325
  if ($form->{outstanding}) {
326
    $paid = qq|SELECT SUM(ac.amount) * -1
327
               FROM acc_trans ac
328
               JOIN chart c ON (c.id = ac.chart_id)
329
               WHERE ac.trans_id = a.id
330
               AND (c.link LIKE '%AR_paid%' OR c.link = '')|;
331
    $paid .= qq|
332
               AND ac.transdate <= '$form->{transdateto}'| if $form->{transdateto};
333
  }
334
 
2 finnarne 335
  my $query = qq|SELECT a.id, a.invnumber, a.ordnumber, a.transdate,
114 finnarne 336
                 a.duedate, a.netamount, a.amount, ($paid) AS paid,
337
                 c.name, a.invoice, a.datepaid, a.terms,
338
                 a.notes, a.shippingpoint, a.till, e.name AS employee
339
                 FROM ar a
340
                 JOIN customer c ON (a.customer_id = c.id)
341
                 LEFT JOIN employee e ON (a.employee_id = e.id)
342
                 WHERE 1=1|;
343
 
344
  my %ordinal = ( 'transdate' => 4,
345
                  'invnumber' => 2,
346
                  'name' => 9,
347
                  'till' => 15,
348
                  'employee' => 16
349
                );
2 finnarne 350
 
114 finnarne 351
  my @a = (transdate, invnumber, name);
352
  push @a, "employee" if $form->{l_employee};
353
  my $sortorder = join ',', $form->sort_columns(@a);
354
  map { $sortorder =~ s/$_/$ordinal{$_}/ } keys %ordinal;
355
  $sortorder = $form->{sort} unless $sortorder;
356
 
357
 
2 finnarne 358
  if ($form->{customer_id}) {
359
    $query .= " AND a.customer_id = $form->{customer_id}";
360
  } else {
361
    if ($form->{customer}) {
362
      my $customer = $form->like(lc $form->{customer});
363
      $query .= " AND lower(c.name) LIKE '$customer'";
364
    }
365
  }
366
  if ($form->{invnumber}) {
367
    my $invnumber = $form->like(lc $form->{invnumber});
368
    $query .= " AND lower(a.invnumber) LIKE '$invnumber'";
114 finnarne 369
    $form->{open} = $form->{closed} = 0;
2 finnarne 370
  }
371
  if ($form->{ordnumber}) {
372
    my $ordnumber = $form->like(lc $form->{ordnumber});
373
    $query .= " AND lower(a.ordnumber) LIKE '$ordnumber'";
114 finnarne 374
    $form->{open} = $form->{closed} = 0;
2 finnarne 375
  }
376
  if ($form->{notes}) {
377
    my $notes = $form->like(lc $form->{notes});
378
    $query .= " AND lower(a.notes) LIKE '$notes'";
114 finnarne 379
    $form->{open} = $form->{closed} = 0;
2 finnarne 380
  }
381
 
382
  $query .= " AND a.transdate >= '$form->{transdatefrom}'" if $form->{transdatefrom};
383
  $query .= " AND a.transdate <= '$form->{transdateto}'" if $form->{transdateto};
384
  if ($form->{open} || $form->{closed}) {
385
    unless ($form->{open} && $form->{closed}) {
114 finnarne 386
      $query .= " AND a.amount != a.paid" if ($form->{open});
387
      $query .= " AND a.amount = a.paid" if ($form->{closed});
2 finnarne 388
    }
389
  }
390
 
114 finnarne 391
 
392
  if ($form->{till}) {
393
    $query .= " AND a.invoice = '1'
394
                AND NOT a.till IS NULL";
395
    if (!$myconfig->{admin}) {
396
      $query .= " AND e.login = '$form->{login}'";
397
    }
398
  }
399
 
400
 
401
  if ($form->{AR}) {
402
    my ($accno) = split /--/, $form->{AR};
403
    $query .= qq|
404
                AND a.id IN (SELECT ac.trans_id
405
                             FROM acc_trans ac
406
                             JOIN chart c ON (c.id = ac.chart_id)
407
                             WHERE a.id = ac.trans_id
408
                             AND c.accno = '$accno')
409
                |;
410
  }
411
 
2 finnarne 412
 
413
  $query .= " ORDER by $sortorder";
114 finnarne 414
 
2 finnarne 415
  my $sth = $dbh->prepare($query);
416
  $sth->execute || $form->dberror($query);
417
 
114 finnarne 418
  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
419
    next if $form->{outstanding} && $ref->{amount} == $ref->{paid};
420
    push @{ $form->{transactions} }, $ref;
2 finnarne 421
  }
422
 
423
  $sth->finish;
424
  $dbh->disconnect;
425
 
426
}
427
 
428
 
429
1;
430