Alle Dateien durch Perltidy laufen lassen. Die verwendeten Optionen sind am Ende...
[kivitendo-erp.git] / SL / IR.pm
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (C) 2001
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #  Contributors:
16 #
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.
21 #
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 #======================================================================
30 #
31 # Inventory received module
32 #
33 #======================================================================
34
35 package IR;
36
37 sub post_invoice {
38   $main::lxdebug->enter_sub();
39
40   my ($self, $myconfig, $form) = @_;
41
42   # connect to database, turn off autocommit
43   my $dbh = $form->dbconnect_noauto($myconfig);
44
45   my ($query, $sth, $null, $project_id);
46   my $exchangerate = 0;
47   my $allocated;
48   my $taxrate;
49   my $taxamount;
50   my $taxdiff;
51   my $item;
52
53   if ($form->{id}) {
54
55     &reverse_invoice($dbh, $form);
56
57   } else {
58     my $uid = rand() . time;
59
60     $uid .= $form->{login};
61
62     $uid = substr($uid, 2, 75);
63
64     $query = qq|INSERT INTO ap (invnumber, employee_id)
65                 VALUES ('$uid', (SELECT e.id FROM employee e
66                                  WHERE e.login = '$form->{login}'))|;
67     $dbh->do($query) || $form->dberror($query);
68
69     $query = qq|SELECT a.id FROM ap a
70                 WHERE a.invnumber = '$uid'|;
71     $sth = $dbh->prepare($query);
72     $sth->execute || $form->dberror($query);
73
74     ($form->{id}) = $sth->fetchrow_array;
75     $sth->finish;
76   }
77
78   ($null, $form->{contact_id}) = split /--/, $form->{contact};
79   $form->{contact_id} *= 1;
80
81   map { $form->{$_} =~ s/\'/\'\'/g } qw(invnumber ordnumber quonumber);
82
83   my ($amount, $linetotal, $lastinventoryaccno, $lastexpenseaccno);
84   my ($netamount, $invoicediff, $expensediff) = (0, 0, 0);
85
86   if ($form->{currency} eq $form->{defaultcurrency}) {
87     $form->{exchangerate} = 1;
88   } else {
89     $exchangerate =
90       $form->check_exchangerate($myconfig, $form->{currency},
91                                 $form->{transdate}, 'sell');
92   }
93
94   $form->{exchangerate} =
95     ($exchangerate)
96     ? $exchangerate
97     : $form->parse_amount($myconfig, $form->{exchangerate});
98
99   for my $i (1 .. $form->{rowcount}) {
100     $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
101
102     if ($form->{"qty_$i"} != 0) {
103
104       map { $form->{"${_}_$i"} =~ s/\'/\'\'/g }
105         qw(partnumber description unit);
106
107       @taxaccounts = split / /, $form->{"taxaccounts_$i"};
108       $taxdiff     = 0;
109       $allocated   = 0;
110       $taxrate     = 0;
111
112       $form->{"sellprice_$i"} =
113         $form->parse_amount($myconfig, $form->{"sellprice_$i"});
114       my $fxsellprice = $form->{"sellprice_$i"};
115
116       my ($dec) = ($fxsellprice =~ /\.(\d+)/);
117       $dec = length $dec;
118       my $decimalplaces = ($dec > 2) ? $dec : 2;
119
120       map { $taxrate += $form->{"${_}_rate"} } @taxaccounts;
121
122       if ($form->{"inventory_accno_$i"}) {
123
124         $linetotal =
125           $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2);
126
127         if ($form->{taxincluded}) {
128           $taxamount = $linetotal * ($taxrate / (1 + $taxrate));
129           $form->{"sellprice_$i"} =
130             $form->{"sellprice_$i"} * (1 / (1 + $taxrate));
131         } else {
132           $taxamount = $linetotal * $taxrate;
133         }
134
135         $netamount += $linetotal;
136
137         if ($form->round_amount($taxrate, 7) == 0) {
138           if ($form->{taxincluded}) {
139             foreach $item (@taxaccounts) {
140               $taxamount =
141                 $form->round_amount($linetotal * $form->{"${item}_rate"} /
142                                       (1 + abs($form->{"${item}_rate"})),
143                                     2);
144               $taxdiff += $taxamount;
145               $form->{amount}{ $form->{id} }{$item} -= $taxamount;
146             }
147             $form->{amount}{ $form->{id} }{ $taxaccounts[0] } += $taxdiff;
148           } else {
149             map {
150               $form->{amount}{ $form->{id} }{$_} -=
151                 $linetotal * $form->{"${_}_rate"}
152             } @taxaccounts;
153           }
154         } else {
155           map {
156             $form->{amount}{ $form->{id} }{$_} -=
157               $taxamount * $form->{"${_}_rate"} / $taxrate
158           } @taxaccounts;
159         }
160
161         # add purchase to inventory, this one is without the tax!
162         $amount =
163           $form->{"sellprice_$i"} * $form->{"qty_$i"} * $form->{exchangerate};
164         $linetotal =
165           $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2) *
166           $form->{exchangerate};
167         $linetotal = $form->round_amount($linetotal, 2);
168
169         # this is the difference for the inventory
170         $invoicediff += ($amount - $linetotal);
171
172         $form->{amount}{ $form->{id} }{ $form->{"inventory_accno_$i"} } -=
173           $linetotal;
174
175         # adjust and round sellprice
176         $form->{"sellprice_$i"} =
177           $form->round_amount($form->{"sellprice_$i"} * $form->{exchangerate},
178                               $decimalplaces);
179
180         # update parts table
181         $query = qq|UPDATE parts SET
182                     lastcost = $form->{"sellprice_$i"}
183                     WHERE id = $form->{"id_$i"}|;
184
185         $dbh->do($query) || $form->dberror($query);
186
187         $form->update_balance($dbh, "parts", "onhand",
188                               qq|id = $form->{"id_$i"}|,
189                               $form->{"qty_$i"})
190           unless $form->{shipped};
191
192         # check if we sold the item already and
193         # make an entry for the expense and inventory
194         $query = qq|SELECT i.id, i.qty, i.allocated, i.trans_id,
195                     p.inventory_accno_id, p.expense_accno_id, a.transdate
196                     FROM invoice i, ar a, parts p
197                     WHERE i.parts_id = p.id
198                     AND i.parts_id = $form->{"id_$i"}
199                     AND (i.qty + i.allocated) > 0
200                     AND i.trans_id = a.id
201                     ORDER BY transdate|;
202         $sth = $dbh->prepare($query);
203         $sth->execute || $form->dberror($query);
204
205         my $totalqty = $form->{"qty_$i"};
206
207         while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
208
209           my $qty = $ref->{qty} + $ref->{allocated};
210
211           if (($qty - $totalqty) > 0) {
212             $qty = $totalqty;
213           }
214
215           $linetotal = $form->round_amount($form->{"sellprice_$i"} * $qty, 2);
216
217           if ($ref->{allocated} < 0) {
218
219             # we have an entry for it already, adjust amount
220             $form->update_balance(
221               $dbh,
222               "acc_trans",
223               "amount",
224               qq|trans_id = $ref->{trans_id} AND chart_id = $ref->{inventory_accno_id} AND transdate = '$ref->{transdate}'|,
225               $linetotal);
226
227             $form->update_balance(
228               $dbh,
229               "acc_trans",
230               "amount",
231               qq|trans_id = $ref->{trans_id} AND chart_id = $ref->{expense_accno_id} AND transdate = '$ref->{transdate}'|,
232               $linetotal * -1);
233
234           } else {
235
236             # add entry for inventory, this one is for the sold item
237             if ($linetotal != 0) {
238               $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, 
239                           transdate)
240                           VALUES ($ref->{trans_id}, $ref->{inventory_accno_id},
241                           $linetotal, '$ref->{transdate}')|;
242               $dbh->do($query) || $form->dberror($query);
243
244               # add expense
245               $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, 
246                           transdate, taxkey)
247                           VALUES ($ref->{trans_id}, $ref->{expense_accno_id},
248                           | . ($linetotal * -1) . qq|, '$ref->{transdate}', 
249                           (SELECT taxkey from tax WHERE chart_id = $ref->{expense_accno_id}))|;
250               $dbh->do($query) || $form->dberror($query);
251             }
252           }
253
254           # update allocated for sold item
255           $form->update_balance($dbh, "invoice", "allocated",
256                                 qq|id = $ref->{id}|,
257                                 $qty * -1);
258
259           $allocated += $qty;
260
261           last if (($totalqty -= $qty) <= 0);
262         }
263
264         $sth->finish;
265
266         $lastinventoryaccno = $form->{"inventory_accno_$i"};
267
268       } else {
269
270         $linetotal =
271           $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2);
272
273         if ($form->{taxincluded}) {
274           $taxamount = $linetotal * ($taxrate / (1 + $taxrate));
275
276           $form->{"sellprice_$i"} =
277             $form->{"sellprice_$i"} * (1 / (1 + $taxrate));
278         } else {
279           $taxamount = $linetotal * $taxrate;
280         }
281
282         $netamount += $linetotal;
283
284         if ($form->round_amount($taxrate, 7) == 0) {
285           if ($form->{taxincluded}) {
286             foreach $item (@taxaccounts) {
287               $taxamount =
288                 $linetotal * $form->{"${item}_rate"} /
289                 (1 + abs($form->{"${item}_rate"}));
290               $totaltax += $taxamount;
291               $form->{amount}{ $form->{id} }{$item} -= $taxamount;
292             }
293           } else {
294             map {
295               $form->{amount}{ $form->{id} }{$_} -=
296                 $linetotal * $form->{"${_}_rate"}
297             } @taxaccounts;
298           }
299         } else {
300           map {
301             $form->{amount}{ $form->{id} }{$_} -=
302               $taxamount * $form->{"${_}_rate"} / $taxrate
303           } @taxaccounts;
304         }
305
306         $amount =
307           $form->{"sellprice_$i"} * $form->{"qty_$i"} * $form->{exchangerate};
308         $linetotal =
309           $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2) *
310           $form->{exchangerate};
311         $linetotal = $form->round_amount($linetotal, 2);
312
313         # this is the difference for expense
314         $expensediff += ($amount - $linetotal);
315
316         # add amount to expense
317         $form->{amount}{ $form->{id} }{ $form->{"expense_accno_$i"} } -=
318           $linetotal;
319
320         $lastexpenseaccno = $form->{"expense_accno_$i"};
321
322         # adjust and round sellprice
323         $form->{"sellprice_$i"} =
324           $form->round_amount($form->{"sellprice_$i"} * $form->{exchangerate},
325                               $decimalplaces);
326
327         # update lastcost
328         $query = qq|UPDATE parts SET
329                     lastcost = $form->{"sellprice_$i"}
330                     WHERE id = $form->{"id_$i"}|;
331
332         $dbh->do($query) || $form->dberror($query);
333
334       }
335
336       $project_id = 'NULL';
337       if ($form->{"projectnumber_$i"}) {
338         $project_id = $form->{"projectnumber_$i"};
339       }
340       print(STDERR $project_id, " Project_id\n");
341       $deliverydate =
342         ($form->{"deliverydate_$i"})
343         ? qq|'$form->{"deliverydate_$i"}'|
344         : "NULL";
345
346       # save detail record in invoice table
347       $query = qq|INSERT INTO invoice (trans_id, parts_id, description, qty,
348                   sellprice, fxsellprice, allocated, unit, deliverydate,
349                   project_id, serialnumber)
350                   VALUES ($form->{id}, $form->{"id_$i"},
351                   '$form->{"description_$i"}', | . ($form->{"qty_$i"} * -1) . qq|,
352                   $form->{"sellprice_$i"}, $fxsellprice, $allocated,
353                   '$form->{"unit_$i"}', $deliverydate, (SELECT id FROM project WHERE projectnumber = '$project_id'),
354                   '$form->{"serialnumber_$i"}')|;
355       $dbh->do($query) || $form->dberror($query);
356       print(STDERR $query, "\n\n");
357     }
358   }
359
360   $form->{datepaid} = $form->{invdate};
361
362   # all amounts are in natural state, netamount includes the taxes
363   # if tax is included, netamount is rounded to 2 decimal places,
364   # taxes are not
365
366   # total payments
367   for my $i (1 .. $form->{paidaccounts}) {
368     $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"});
369     $form->{paid} += $form->{"paid_$i"};
370     $form->{datepaid} = $form->{"datepaid_$i"} if ($form->{"datepaid_$i"});
371   }
372
373   my ($tax, $paiddiff) = (0, 0);
374
375   $netamount = $form->round_amount($netamount, 2);
376
377   # figure out rounding errors for amount paid and total amount
378   if ($form->{taxincluded}) {
379
380     $amount    = $form->round_amount($netamount * $form->{exchangerate}, 2);
381     $paiddiff  = $amount - $netamount * $form->{exchangerate};
382     $netamount = $amount;
383
384     foreach $item (split / /, $form->{taxaccounts}) {
385       $amount = $form->{amount}{ $form->{id} }{$item} * $form->{exchangerate};
386       $form->{amount}{ $form->{id} }{$item} = $form->round_amount($amount, 2);
387       $amount = $form->{amount}{ $form->{id} }{$item} * -1;
388       $tax += $amount;
389       $netamount -= $amount;
390     }
391
392     $invoicediff += $paiddiff;
393     $expensediff += $paiddiff;
394
395     ######## this only applies to tax included
396     if ($lastinventoryaccno) {
397       $form->{amount}{ $form->{id} }{$lastinventoryaccno} -= $invoicediff;
398     }
399     if ($lastexpenseaccno) {
400       $form->{amount}{ $form->{id} }{$lastexpenseaccno} -= $expensediff;
401     }
402
403   } else {
404     $amount    = $form->round_amount($netamount * $form->{exchangerate}, 2);
405     $paiddiff  = $amount - $netamount * $form->{exchangerate};
406     $netamount = $amount;
407     foreach my $item (split / /, $form->{taxaccounts}) {
408       $form->{amount}{ $form->{id} }{$item} =
409         $form->round_amount($form->{amount}{ $form->{id} }{$item}, 2);
410       $amount =
411         $form->round_amount(
412             $form->{amount}{ $form->{id} }{$item} * $form->{exchangerate} * -1,
413             2);
414       $paiddiff +=
415         $amount - $form->{amount}{ $form->{id} }{$item} *
416         $form->{exchangerate} * -1;
417       $form->{amount}{ $form->{id} }{$item} =
418         $form->round_amount($amount * -1, 2);
419       $amount = $form->{amount}{ $form->{id} }{$item} * -1;
420       $tax += $amount;
421     }
422   }
423
424   $form->{amount}{ $form->{id} }{ $form->{AP} } = $netamount + $tax;
425
426   if ($form->{paid} != 0) {
427     $form->{paid} =
428       $form->round_amount($form->{paid} * $form->{exchangerate} + $paiddiff,
429                           2);
430   }
431
432   # update exchangerate
433   if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
434     $form->update_exchangerate($dbh, $form->{currency}, $form->{invdate}, 0,
435                                $form->{exchangerate});
436   }
437
438   # record acc_trans transactions
439   foreach my $trans_id (keys %{ $form->{amount} }) {
440     foreach my $accno (keys %{ $form->{amount}{$trans_id} }) {
441       if (
442           ($form->{amount}{$trans_id}{$accno} =
443            $form->round_amount($form->{amount}{$trans_id}{$accno}, 2)
444           ) != 0
445         ) {
446         $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, 
447                     transdate, taxkey)
448                     VALUES ($trans_id, (SELECT c.id FROM chart c
449                                          WHERE c.accno = '$accno'),
450                     $form->{amount}{$trans_id}{$accno}, '$form->{invdate}',
451                     (SELECT taxkey_id  FROM chart WHERE accno = '$accno'))|;
452         $dbh->do($query) || $form->dberror($query);
453       }
454     }
455   }
456
457   # deduct payment differences from paiddiff
458   for my $i (1 .. $form->{paidaccounts}) {
459     if ($form->{"paid_$i"} != 0) {
460       $amount =
461         $form->round_amount($form->{"paid_$i"} * $form->{exchangerate}, 2);
462       $paiddiff -= $amount - $form->{"paid_$i"} * $form->{exchangerate};
463     }
464   }
465
466   # force AP entry if 0
467   $form->{amount}{ $form->{id} }{ $form->{AP} } = $form->{paid}
468     if ($form->{amount}{ $form->{id} }{ $form->{AP} } == 0);
469
470   # record payments and offsetting AP
471   for my $i (1 .. $form->{paidaccounts}) {
472
473     if ($form->{"paid_$i"} != 0) {
474       my ($accno) = split /--/, $form->{"AP_paid_$i"};
475       $form->{"datepaid_$i"} = $form->{invdate}
476         unless ($form->{"datepaid_$i"});
477       $form->{datepaid} = $form->{"datepaid_$i"};
478
479       $amount = (
480                  $form->round_amount(
481                       $form->{"paid_$i"} * $form->{exchangerate} + $paiddiff, 2
482                  )
483       ) * -1;
484
485       # record AP
486
487       if ($form->{amount}{ $form->{id} }{ $form->{AP} } != 0) {
488         $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
489                     transdate)
490                     VALUES ($form->{id}, (SELECT c.id FROM chart c
491                                         WHERE c.accno = '$form->{AP}'),
492                     $amount, '$form->{"datepaid_$i"}')|;
493         $dbh->do($query) || $form->dberror($query);
494       }
495
496       # record payment
497
498       $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
499                   source, memo)
500                   VALUES ($form->{id}, (SELECT c.id FROM chart c
501                                       WHERE c.accno = '$accno'),
502                   $form->{"paid_$i"}, '$form->{"datepaid_$i"}',
503                   '$form->{"source_$i"}', '$form->{"memo_$i"}')|;
504       $dbh->do($query) || $form->dberror($query);
505
506       $exchangerate = 0;
507
508       if ($form->{currency} eq $form->{defaultcurrency}) {
509         $form->{"exchangerate_$i"} = 1;
510       } else {
511         $exchangerate =
512           $form->check_exchangerate($myconfig, $form->{currency},
513                                     $form->{"datepaid_$i"}, 'sell');
514
515         $form->{"exchangerate_$i"} =
516           ($exchangerate)
517           ? $exchangerate
518           : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
519       }
520
521       # exchangerate difference
522       $form->{fx}{$accno}{ $form->{"datepaid_$i"} } +=
523         $form->{"paid_$i"} * ($form->{"exchangerate_$i"} - 1) + $paiddiff;
524
525       # gain/loss
526       $amount =
527         ($form->{"paid_$i"} * $form->{exchangerate}) -
528         ($form->{"paid_$i"} * $form->{"exchangerate_$i"});
529       if ($amount > 0) {
530         $form->{fx}{ $form->{fxgain_accno} }{ $form->{"datepaid_$i"} } +=
531           $amount;
532       } else {
533         $form->{fx}{ $form->{fxloss_accno} }{ $form->{"datepaid_$i"} } +=
534           $amount;
535       }
536
537       $paiddiff = 0;
538
539       # update exchange rate
540       if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
541         $form->update_exchangerate($dbh, $form->{currency},
542                                    $form->{"datepaid_$i"},
543                                    0, $form->{"exchangerate_$i"});
544       }
545     }
546   }
547
548   # record exchange rate differences and gains/losses
549   foreach my $accno (keys %{ $form->{fx} }) {
550     foreach my $transdate (keys %{ $form->{fx}{$accno} }) {
551       if (
552           ($form->{fx}{$accno}{$transdate} =
553            $form->round_amount($form->{fx}{$accno}{$transdate}, 2)
554           ) != 0
555         ) {
556
557         $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
558                     transdate, cleared, fx_transaction)
559                     VALUES ($form->{id}, (SELECT c.id FROM chart c
560                                         WHERE c.accno = '$accno'),
561                     $form->{fx}{$accno}{$transdate}, '$transdate', '0', '1')|;
562         $dbh->do($query) || $form->dberror($query);
563       }
564     }
565   }
566
567   $amount = $netamount + $tax;
568
569   # set values which could be empty
570   $form->{taxincluded} *= 1;
571   my $datepaid = ($form->{paid})    ? qq|'$form->{datepaid}'| : "NULL";
572   my $duedate  = ($form->{duedate}) ? qq|'$form->{duedate}'|  : "NULL";
573
574   ($null, $form->{department_id}) = split(/--/, $form->{department});
575   $form->{department_id} *= 1;
576
577   $form->{invnumber} = $form->{id} unless $form->{invnumber};
578
579   # save AP record
580   $query = qq|UPDATE ap set
581               invnumber = '$form->{invnumber}',
582               ordnumber = '$form->{ordnumber}',
583               quonumber = '$form->{quonumber}',
584               transdate = '$form->{invdate}',
585               vendor_id = $form->{vendor_id},
586               amount = $amount,
587               netamount = $netamount,
588               paid = $form->{paid},
589               datepaid = $datepaid,
590               duedate = $duedate,
591               invoice = '1',
592               taxincluded = '$form->{taxincluded}',
593               notes = '$form->{notes}',
594               intnotes = '$form->{intnotes}',
595               curr = '$form->{currency}',
596               department_id = $form->{department_id},
597               cp_id = $form->{contact_id}
598               WHERE id = $form->{id}|;
599   $dbh->do($query) || $form->dberror($query);
600
601   # add shipto
602   $form->{name} = $form->{vendor};
603   $form->{name} =~ s/--$form->{vendor_id}//;
604   $form->add_shipto($dbh, $form->{id});
605
606   # delete zero entries
607   $query = qq|DELETE FROM acc_trans
608               WHERE amount = 0|;
609   $dbh->do($query) || $form->dberror($query);
610
611   if ($form->{webdav}) {
612     &webdav_folder($myconfig, $form);
613   }
614
615   my $rc = $dbh->commit;
616   $dbh->disconnect;
617
618   $main::lxdebug->leave_sub();
619
620   return $rc;
621 }
622
623 sub reverse_invoice {
624   $main::lxdebug->enter_sub();
625
626   my ($dbh, $form) = @_;
627
628   # reverse inventory items
629   my $query = qq|SELECT i.parts_id, p.inventory_accno_id, p.expense_accno_id,
630                  i.qty, i.allocated, i.sellprice
631                  FROM invoice i, parts p
632                  WHERE i.parts_id = p.id
633                  AND i.trans_id = $form->{id}|;
634   my $sth = $dbh->prepare($query);
635   $sth->execute || $form->dberror($query);
636
637   my $netamount = 0;
638
639   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
640     $netamount += $form->round_amount($ref->{sellprice} * $ref->{qty} * -1, 2);
641
642     if ($ref->{inventory_accno_id}) {
643
644       # update onhand
645       $form->update_balance($dbh, "parts", "onhand", qq|id = $ref->{parts_id}|,
646                             $ref->{qty});
647
648       # if $ref->{allocated} > 0 than we sold that many items
649       if ($ref->{allocated} > 0) {
650
651         # get references for sold items
652         $query = qq|SELECT i.id, i.trans_id, i.allocated, a.transdate
653                     FROM invoice i, ar a
654                     WHERE i.parts_id = $ref->{parts_id}
655                     AND i.allocated < 0
656                     AND i.trans_id = a.id
657                     ORDER BY transdate DESC|;
658         my $sth = $dbh->prepare($query);
659         $sth->execute || $form->dberror($query);
660
661         while (my $pthref = $sth->fetchrow_hashref(NAME_lc)) {
662           my $qty = $ref->{allocated};
663           if (($ref->{allocated} + $pthref->{allocated}) > 0) {
664             $qty = $pthref->{allocated} * -1;
665           }
666
667           my $amount = $form->round_amount($ref->{sellprice} * $qty, 2);
668
669           #adjust allocated
670           $form->update_balance($dbh, "invoice", "allocated",
671                                 qq|id = $pthref->{id}|, $qty);
672
673           $form->update_balance(
674             $dbh,
675             "acc_trans",
676             "amount",
677             qq|trans_id = $pthref->{trans_id} AND chart_id = $ref->{expense_accno_id} AND transdate = '$pthref->{transdate}'|,
678             $amount);
679
680           $form->update_balance(
681             $dbh,
682             "acc_trans",
683             "amount",
684             qq|trans_id = $pthref->{trans_id} AND chart_id = $ref->{inventory_accno_id} AND transdate = '$pthref->{transdate}'|,
685             $amount * -1);
686
687           last if (($ref->{allocated} -= $qty) <= 0);
688         }
689         $sth->finish;
690       }
691     }
692   }
693   $sth->finish;
694
695   # delete acc_trans
696   $query = qq|DELETE FROM acc_trans
697               WHERE trans_id = $form->{id}|;
698   $dbh->do($query) || $form->dberror($query);
699
700   # delete invoice entries
701   $query = qq|DELETE FROM invoice
702               WHERE trans_id = $form->{id}|;
703   $dbh->do($query) || $form->dberror($query);
704
705   $query = qq|DELETE FROM shipto
706               WHERE trans_id = $form->{id}|;
707   $dbh->do($query) || $form->dberror($query);
708
709   $main::lxdebug->leave_sub();
710 }
711
712 sub delete_invoice {
713   $main::lxdebug->enter_sub();
714
715   my ($self, $myconfig, $form) = @_;
716
717   # connect to database
718   my $dbh = $form->dbconnect_noauto($myconfig);
719
720   &reverse_invoice($dbh, $form);
721
722   # delete zero entries
723   my $query = qq|DELETE FROM acc_trans
724                  WHERE amount = 0|;
725   $dbh->do($query) || $form->dberror($query);
726
727   # delete AP record
728   my $query = qq|DELETE FROM ap
729                  WHERE id = $form->{id}|;
730   $dbh->do($query) || $form->dberror($query);
731
732   my $rc = $dbh->commit;
733   $dbh->disconnect;
734
735   $main::lxdebug->leave_sub();
736
737   return $rc;
738 }
739
740 sub retrieve_invoice {
741   $main::lxdebug->enter_sub();
742
743   my ($self, $myconfig, $form) = @_;
744
745   # connect to database
746   my $dbh = $form->dbconnect_noauto($myconfig);
747
748   my $query;
749
750   if ($form->{id}) {
751
752     # get default accounts and last invoice number
753     $query = qq|SELECT (SELECT c.accno FROM chart c
754                         WHERE d.inventory_accno_id = c.id) AS inventory_accno,
755                        (SELECT c.accno FROM chart c
756                         WHERE d.income_accno_id = c.id) AS income_accno,
757                        (SELECT c.accno FROM chart c
758                         WHERE d.expense_accno_id = c.id) AS expense_accno,
759                        (SELECT c.accno FROM chart c
760                         WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
761                        (SELECT c.accno FROM chart c
762                         WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
763                 d.curr AS currencies
764                 FROM defaults d|;
765   } else {
766     $query = qq|SELECT (SELECT c.accno FROM chart c
767                         WHERE d.inventory_accno_id = c.id) AS inventory_accno,
768                        (SELECT c.accno FROM chart c
769                         WHERE d.income_accno_id = c.id) AS income_accno,
770                        (SELECT c.accno FROM chart c
771                         WHERE d.expense_accno_id = c.id) AS expense_accno,
772                        (SELECT c.accno FROM chart c
773                         WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
774                        (SELECT c.accno FROM chart c
775                         WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
776                 d.curr AS currencies,
777                 current_date AS invdate
778                 FROM defaults d|;
779   }
780   my $sth = $dbh->prepare($query);
781   $sth->execute || $form->dberror($query);
782
783   my $ref = $sth->fetchrow_hashref(NAME_lc);
784   map { $form->{$_} = $ref->{$_} } keys %$ref;
785   $sth->finish;
786
787   if ($form->{id}) {
788
789     # retrieve invoice
790     $query = qq|SELECT a.cp_id, a.invnumber, a.transdate AS invdate, a.duedate,
791                 a.ordnumber, a.quonumber, a.paid, a.taxincluded, a.notes,
792                 a.intnotes, a.curr AS currency
793                 FROM ap a
794                 WHERE a.id = $form->{id}|;
795     $sth = $dbh->prepare($query);
796     $sth->execute || $form->dberror($query);
797
798     $ref = $sth->fetchrow_hashref(NAME_lc);
799     map { $form->{$_} = $ref->{$_} } keys %$ref;
800     $sth->finish;
801
802     $form->{exchangerate} =
803       $form->get_exchangerate($dbh, $form->{currency}, $form->{invdate},
804                               "sell");
805
806     # get shipto
807     $query = qq|SELECT s.* FROM shipto s
808                 WHERE s.trans_id = $form->{id}|;
809     $sth = $dbh->prepare($query);
810     $sth->execute || $form->dberror($query);
811
812     $ref = $sth->fetchrow_hashref(NAME_lc);
813     map { $form->{$_} = $ref->{$_} } keys %$ref;
814     $sth->finish;
815
816     # retrieve individual items
817     $query = qq|SELECT c1.accno AS inventory_accno,
818                        c2.accno AS income_accno,
819                        c3.accno AS expense_accno,
820                 p.partnumber, i.description, i.qty, i.fxsellprice AS sellprice,
821                 i.parts_id AS id, i.unit, p.bin, i.deliverydate,
822                 pr.projectnumber,
823                 i.project_id, i.serialnumber,
824                 pg.partsgroup
825                 FROM invoice i
826                 JOIN parts p ON (i.parts_id = p.id)
827                 LEFT JOIN chart c1 ON (p.inventory_accno_id = c1.id)
828                 LEFT JOIN chart c2 ON (p.income_accno_id = c2.id)
829                 LEFT JOIN chart c3 ON (p.expense_accno_id = c3.id)
830                 LEFT JOIN project pr ON (i.project_id = pr.id)
831                 LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
832                 WHERE i.trans_id = $form->{id}
833                 ORDER BY i.id|;
834     $sth = $dbh->prepare($query);
835     $sth->execute || $form->dberror($query);
836
837     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
838
839       #set expense_accno=inventory_accno if they are different => bilanz
840       $vendor_accno =
841         ($ref->{expense_accno} != $ref->{inventory_accno})
842         ? $ref->{inventory_accno}
843         : $ref->{expense_accno};
844       $vendor_accno =
845         ($ref->{inventory_accno})
846         ? $ref->{inventory_accno}
847         : $ref->{expense_accno};
848
849       # get tax rates and description
850       $accno_id =
851         ($form->{vc} eq "customer") ? $ref->{income_accno} : $vendor_accno;
852       $query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
853                  FROM chart c, tax t
854                  WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
855                  ORDER BY accno|;
856       $stw = $dbh->prepare($query);
857       $stw->execute || $form->dberror($query);
858       $ref->{taxaccounts} = "";
859       while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
860
861         #    if ($customertax{$ref->{accno}}) {
862         $ref->{taxaccounts} .= "$ptr->{accno} ";
863         if (!($form->{taxaccounts} =~ /$ptr->{accno}/)) {
864           $form->{"$ptr->{accno}_rate"}        = $ptr->{rate};
865           $form->{"$ptr->{accno}_description"} = $ptr->{description};
866           $form->{"$ptr->{accno}_taxnumber"}   = $ptr->{taxnumber};
867           $form->{taxaccounts} .= "$ptr->{accno} ";
868         }
869
870       }
871
872       chop $ref->{taxaccounts};
873       push @{ $form->{invoice_details} }, $ref;
874       $stw->finish;
875     }
876     $sth->finish;
877
878     if ($form->{webdav}) {
879       &webdav_folder($myconfig, $form);
880     }
881
882   }
883
884   my $rc = $dbh->commit;
885   $dbh->disconnect;
886
887   $main::lxdebug->leave_sub();
888
889   return $rc;
890 }
891
892 sub get_vendor {
893   $main::lxdebug->enter_sub();
894
895   my ($self, $myconfig, $form) = @_;
896
897   # connect to database
898   my $dbh = $form->dbconnect($myconfig);
899
900   my $dateformat = $myconfig->{dateformat};
901   $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
902
903   my $duedate =
904     ($form->{invdate})
905     ? "to_date('$form->{invdate}', '$dateformat')"
906     : "current_date";
907
908   $form->{vendor_id} *= 1;
909
910   # get vendor
911   my $query = qq|SELECT v.name AS vendor, v.creditlimit, v.terms,
912                  v.email, v.cc, v.bcc, v.language,
913                  v.street, v.zipcode, v.city, v.country,
914                  $duedate + v.terms AS duedate, v.notes AS intnotes
915                  FROM vendor v
916                  WHERE v.id = $form->{vendor_id}|;
917   my $sth = $dbh->prepare($query);
918   $sth->execute || $form->dberror($query);
919
920   $ref = $sth->fetchrow_hashref(NAME_lc);
921   map { $form->{$_} = $ref->{$_} } keys %$ref;
922   $sth->finish;
923
924   $form->{creditremaining} = $form->{creditlimit};
925   $query = qq|SELECT SUM(a.amount - a.paid)
926               FROM ap a
927               WHERE a.vendor_id = $form->{vendor_id}|;
928   $sth = $dbh->prepare($query);
929   $sth->execute || $form->dberror($query);
930
931   ($form->{creditremaining}) -= $sth->fetchrow_array;
932
933   $sth->finish;
934
935   $query = qq|SELECT o.amount,
936                 (SELECT e.sell FROM exchangerate e
937                  WHERE e.curr = o.curr
938                  AND e.transdate = o.transdate)
939               FROM oe o
940               WHERE o.vendor_id = $form->{vendor_id}
941               AND o.quotation = '0'
942               AND o.closed = '0'|;
943   $sth = $dbh->prepare($query);
944   $sth->execute || $form->dberror($query);
945
946   while (my ($amount, $exch) = $sth->fetchrow_array) {
947     $exch = 1 unless $exch;
948     $form->{creditremaining} -= $amount * $exch;
949   }
950   $sth->finish;
951
952   $form->get_contacts($dbh, $form->{vendor_id});
953
954   ($null, $form->{cp_id}) = split /--/, $form->{contact};
955
956   # get contact if selected
957   if ($form->{contact} ne "--" && $form->{contact} ne "") {
958     $form->get_contact($dbh, $form->{cp_id});
959   }
960
961   # get shipto if we do not convert an order or invoice
962   if (!$form->{shipto}) {
963     map { delete $form->{$_} }
964       qw(shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax shiptoemail);
965
966     $query = qq|SELECT s.* FROM shipto s
967                 WHERE s.trans_id = $form->{vendor_id}|;
968     $sth = $dbh->prepare($query);
969     $sth->execute || $form->dberror($query);
970
971     $ref = $sth->fetchrow_hashref(NAME_lc);
972     map { $form->{$_} = $ref->{$_} } keys %$ref;
973     $sth->finish;
974   }
975
976   # get taxes for vendor
977   $query = qq|SELECT c.accno
978               FROM chart c
979               JOIN vendortax v ON (v.chart_id = c.id)
980               WHERE v.vendor_id = $form->{vendor_id}|;
981   $sth = $dbh->prepare($query);
982   $sth->execute || $form->dberror($query);
983
984   my $vendortax = ();
985   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
986     $vendortax{ $ref->{accno} } = 1;
987   }
988   $sth->finish;
989
990   if (!$form->{id} && $form->{type} !~ /_(order|quotation)/) {
991
992     # setup last accounts used
993     $query = qq|SELECT c.accno, c.description, c.link, c.category
994                 FROM chart c
995                 JOIN acc_trans ac ON (ac.chart_id = c.id)
996                 JOIN ap a ON (a.id = ac.trans_id)
997                 WHERE a.vendor_id = $form->{vendor_id}
998                 AND NOT (c.link LIKE '%_tax%' OR c.link LIKE '%_paid%')
999                 AND a.id IN (SELECT max(a2.id) FROM ap a2
1000                              WHERE a2.vendor_id = $form->{vendor_id})|;
1001     $sth = $dbh->prepare($query);
1002     $sth->execute || $form->dberror($query);
1003
1004     my $i = 0;
1005     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1006       if ($ref->{category} eq 'E') {
1007         $i++;
1008         $form->{"AP_amount_$i"} = "$ref->{accno}--$ref->{description}";
1009       }
1010       if ($ref->{category} eq 'L') {
1011         $form->{APselected} = $form->{AP_1} =
1012           "$ref->{accno}--$ref->{description}";
1013       }
1014     }
1015     $sth->finish;
1016     $form->{rowcount} = $i if ($i && !$form->{type});
1017   }
1018
1019   $dbh->disconnect;
1020
1021   $main::lxdebug->leave_sub();
1022 }
1023
1024 sub retrieve_item {
1025   $main::lxdebug->enter_sub();
1026
1027   my ($self, $myconfig, $form) = @_;
1028
1029   my $i = $form->{rowcount};
1030
1031   # don't include assemblies or obsolete parts
1032   my $where = "NOT p.assembly = '1' AND NOT p.obsolete = '1'";
1033
1034   if ($form->{"partnumber_$i"}) {
1035     my $partnumber = $form->like(lc $form->{"partnumber_$i"});
1036     $where .= " AND lower(p.partnumber) LIKE '$partnumber'";
1037   }
1038
1039   if ($form->{"description_$i"}) {
1040     my $description = $form->like(lc $form->{"description_$i"});
1041     $where .= " AND lower(p.description) LIKE '$description'";
1042   }
1043
1044   if ($form->{"partsgroup_$i"}) {
1045     my $partsgroup = $form->like(lc $form->{"partsgroup_$i"});
1046     $where .= " AND lower(pg.partsgroup) LIKE '$partsgroup'";
1047   }
1048
1049   if ($form->{"description_$i"}) {
1050     $where .= " ORDER BY p.description";
1051   } else {
1052     $where .= " ORDER BY p.partnumber";
1053   }
1054
1055   # connect to database
1056   my $dbh = $form->dbconnect($myconfig);
1057
1058   my $query = qq|SELECT p.id, p.partnumber, p.description,
1059                  c1.accno AS inventory_accno,
1060                  c2.accno AS income_accno,
1061                  c3.accno AS expense_accno,
1062                  pg.partsgroup,
1063                  p.lastcost AS sellprice, p.unit, p.bin, p.onhand, p.notes AS partnotes
1064                  FROM parts p
1065                  LEFT JOIN chart c1 ON (p.inventory_accno_id = c1.id)
1066                  LEFT JOIN chart c2 ON (p.income_accno_id = c2.id)
1067                  LEFT JOIN chart c3 ON (p.expense_accno_id = c3.id)
1068                  LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
1069                  WHERE $where|;
1070   my $sth = $dbh->prepare($query);
1071   $sth->execute || $form->dberror($query);
1072
1073   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1074
1075     #set expense_accno=inventory_accno if they are different => bilanz
1076     $vendor_accno =
1077       ($ref->{expense_accno} != $ref->{inventory_accno})
1078       ? $ref->{inventory_accno}
1079       : $ref->{expense_accno};
1080     $vendor_accno =
1081       ($ref->{inventory_accno})
1082       ? $ref->{inventory_accno}
1083       : $ref->{expense_accno};
1084
1085     # get tax rates and description
1086     $accno_id =
1087       ($form->{vc} eq "customer") ? $ref->{income_accno} : $vendor_accno;
1088     $query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
1089               FROM chart c, tax t
1090               WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
1091               ORDER BY c.accno|;
1092     $stw = $dbh->prepare($query);
1093     $stw->execute || $form->dberror($query);
1094
1095     $ref->{taxaccounts} = "";
1096     while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
1097
1098       #    if ($customertax{$ref->{accno}}) {
1099       $ref->{taxaccounts} .= "$ptr->{accno} ";
1100       if (!($form->{taxaccounts} =~ /$ptr->{accno}/)) {
1101         $form->{"$ptr->{accno}_rate"}        = $ptr->{rate};
1102         $form->{"$ptr->{accno}_description"} = $ptr->{description};
1103         $form->{"$ptr->{accno}_taxnumber"}   = $ptr->{taxnumber};
1104         $form->{taxaccounts} .= "$ptr->{accno} ";
1105       }
1106
1107     }
1108
1109     $stw->finish;
1110     chop $ref->{taxaccounts};
1111
1112     push @{ $form->{item_list} }, $ref;
1113
1114   }
1115
1116   $sth->finish;
1117   $dbh->disconnect;
1118
1119   $main::lxdebug->leave_sub();
1120 }
1121
1122 sub vendor_details {
1123   $main::lxdebug->enter_sub();
1124
1125   my ($self, $myconfig, $form) = @_;
1126
1127   # connect to database
1128   my $dbh = $form->dbconnect($myconfig);
1129
1130   # get contact id, set it if nessessary
1131   ($null, $form->{cp_id}) = split /--/, $form->{contact};
1132
1133   $contact = "";
1134   if ($form->{cp_id}) {
1135     $contact = "and cp.cp_id = $form->{cp_id}";
1136   }
1137
1138   $taxincluded = $form->{taxincluded};
1139
1140   # get rest for the vendor
1141   # fax and phone and email as vendor*
1142   my $query =
1143     qq|SELECT ct.*, cp.*, ct.notes as vendornotes, phone as vendorphone, fax as vendorfax, email as vendoremail
1144                  FROM vendor ct
1145                  LEFT JOIN contacts cp on ct.id = cp.cp_cv_id
1146                  WHERE ct.id = $form->{vendor_id}  $contact order by cp.cp_id limit 1|;
1147   my $sth = $dbh->prepare($query);
1148   $sth->execute || $form->dberror($query);
1149
1150   $ref = $sth->fetchrow_hashref(NAME_lc);
1151   map { $form->{$_} = $ref->{$_} } keys %$ref;
1152
1153   $form->{taxincluded} = $taxincluded;
1154
1155   $sth->finish;
1156   $dbh->disconnect;
1157
1158   $main::lxdebug->leave_sub();
1159 }
1160
1161 sub item_links {
1162   $main::lxdebug->enter_sub();
1163
1164   my ($self, $myconfig, $form) = @_;
1165
1166   # connect to database
1167   my $dbh = $form->dbconnect($myconfig);
1168
1169   my $query = qq|SELECT c.accno, c.description, c.link
1170                  FROM chart c
1171                  WHERE c.link LIKE '%IC%'
1172                  ORDER BY c.accno|;
1173   my $sth = $dbh->prepare($query);
1174   $sth->execute || $form->dberror($query);
1175
1176   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1177     foreach my $key (split(/:/, $ref->{link})) {
1178       if ($key =~ /IC/) {
1179         push @{ $form->{IC_links}{$key} },
1180           { accno       => $ref->{accno},
1181             description => $ref->{description} };
1182       }
1183     }
1184   }
1185
1186   $sth->finish;
1187   $main::lxdebug->leave_sub();
1188 }
1189
1190 sub webdav_folder {
1191   $main::lxdebug->enter_sub();
1192
1193   my ($myconfig, $form) = @_;
1194
1195 SWITCH: {
1196     $path = "webdav/rechnungen/" . $form->{invnumber}, last SWITCH
1197       if ($form->{vc} eq "customer");
1198     $path = "webdav/einkaufsrechnungen/" . $form->{invnumber}, last SWITCH
1199       if ($form->{vc} eq "vendor");
1200   }
1201
1202   if (!-d $path) {
1203     mkdir($path, 0770) or die "can't make directory $!\n";
1204   } else {
1205     if ($form->{id}) {
1206       @files = <$path/*>;
1207       foreach $file (@files) {
1208
1209         $file =~ /\/([^\/]*)$/;
1210         $fname = $1;
1211         $ENV{'SCRIPT_NAME'} =~ /\/([^\/]*)\//;
1212         $lxerp = $1;
1213         $link  = "http://" . $ENV{'SERVER_NAME'} . "/" . $lxerp . "/" . $file;
1214         $form->{WEBDAV}{$fname} = $link;
1215       }
1216     }
1217   }
1218
1219   $main::lxdebug->leave_sub();
1220 }
1221
1222 1;
1223