Alle Dateien durch Perltidy laufen lassen. Die verwendeten Optionen sind am Ende...
[kivitendo-erp.git] / SL / OE.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) 1999-2003
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 # Order entry module
32 # Quotation
33 #======================================================================
34
35 package OE;
36
37 sub transactions {
38   $main::lxdebug->enter_sub();
39
40   my ($self, $myconfig, $form) = @_;
41
42   # connect to database
43   my $dbh = $form->dbconnect($myconfig);
44
45   my $query;
46   my $ordnumber = 'ordnumber';
47   my $quotation = '0';
48   my ($null, $department_id) = split /--/, $form->{department};
49
50   my $department = " AND o.department_id = $department_id" if $department_id;
51
52   my $rate = ($form->{vc} eq 'customer') ? 'buy' : 'sell';
53
54   if ($form->{type} =~ /_quotation$/) {
55     $quotation = '1';
56     $ordnumber = 'quonumber';
57   }
58
59   my $number = $form->like(lc $form->{$ordnumber});
60   my $name   = $form->like(lc $form->{ $form->{vc} });
61
62   my $query = qq|SELECT o.id, o.ordnumber, o.transdate, o.reqdate,
63                  o.amount, ct.name, o.netamount, o.$form->{vc}_id,
64                  ex.$rate AS exchangerate,
65                  o.closed, o.quonumber, o.shippingpoint, o.shipvia,
66                  e.name AS employee
67                  FROM oe o
68                  JOIN $form->{vc} ct ON (o.$form->{vc}_id = ct.id)
69                  LEFT JOIN employee e ON (o.employee_id = e.id)
70                  LEFT JOIN exchangerate ex ON (ex.curr = o.curr
71                                                AND ex.transdate = o.transdate)
72                  WHERE o.quotation = '$quotation'
73                  $department|;
74
75   # build query if type eq (ship|receive)_order
76   if ($form->{type} =~ /(ship|receive)_order/) {
77     my ($warehouse, $warehouse_id) = split /--/, $form->{warehouse};
78
79     $query = qq|SELECT DISTINCT ON (o.id) o.id, o.ordnumber, o.transdate,
80                  o.reqdate, o.amount, ct.name, o.netamount, o.$form->{vc}_id,
81                  ex.$rate AS exchangerate,
82                  o.closed, o.quonumber, o.shippingpoint, o.shipvia,
83                  e.name AS employee
84                  FROM oe o
85                  JOIN $form->{vc} ct ON (o.$form->{vc}_id = ct.id)
86                  JOIN orderitems oi ON (oi.trans_id = o.id)
87                  JOIN parts p ON (p.id = oi.parts_id)|;
88
89     if ($warehouse_id && $form->{type} eq 'ship_order') {
90       $query .= qq|
91                  JOIN inventory i ON (oi.parts_id = i.parts_id)
92                  |;
93     }
94
95     $query .= qq|
96                  LEFT JOIN employee e ON (o.employee_id = e.id)
97                  LEFT JOIN exchangerate ex ON (ex.curr = o.curr
98                                                AND ex.transdate = o.transdate)
99                  WHERE o.quotation = '0'
100                  AND (p.inventory_accno_id > 0 OR p.assembly = '1')
101                  AND oi.qty <> oi.ship
102                  $department|;
103
104     if ($warehouse_id && $form->{type} eq 'ship_order') {
105       $query .= qq|
106                  AND i.warehouse_id = $warehouse_id
107                  AND i.qty >= (oi.qty - oi.ship)
108                  |;
109     }
110
111   }
112
113   if ($form->{"$form->{vc}_id"}) {
114     $query .= qq| AND o.$form->{vc}_id = $form->{"$form->{vc}_id"}|;
115   } else {
116     if ($form->{ $form->{vc} }) {
117       $query .= " AND lower(ct.name) LIKE '$name'";
118     }
119   }
120   if (!$form->{open} && !$form->{closed}) {
121     $query .= " AND o.id = 0";
122   } elsif (!($form->{open} && $form->{closed})) {
123     $query .= ($form->{open}) ? " AND o.closed = '0'" : " AND o.closed = '1'";
124   }
125
126   my $sortorder = join ', ',
127     ("o.id", $form->sort_columns(transdate, $ordnumber, name));
128   $sortorder = $form->{sort} unless $sortorder;
129
130   $query .= " AND lower($ordnumber) LIKE '$number'" if $form->{$ordnumber};
131   $query .= " AND o.transdate >= '$form->{transdatefrom}'"
132     if $form->{transdatefrom};
133   $query .= " AND o.transdate <= '$form->{transdateto}'"
134     if $form->{transdateto};
135   $query .= " ORDER by $sortorder";
136
137   my $sth = $dbh->prepare($query);
138   $sth->execute || $form->dberror($query);
139
140   my %id = ();
141   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
142     $ref->{exchangerate} = 1 unless $ref->{exchangerate};
143     push @{ $form->{OE} }, $ref if $ref->{id} != $id{ $ref->{id} };
144     $id{ $ref->{id} } = $ref->{id};
145   }
146
147   $sth->finish;
148   $dbh->disconnect;
149
150   $main::lxdebug->leave_sub();
151 }
152
153 sub save {
154   $main::lxdebug->enter_sub();
155
156   my ($self, $myconfig, $form) = @_;
157
158   # connect to database, turn off autocommit
159   my $dbh = $form->dbconnect_noauto($myconfig);
160
161   my ($query, $sth, $null);
162   my $exchangerate = 0;
163
164   ($null, $form->{employee_id}) = split /--/, $form->{employee};
165   unless ($form->{employee_id}) {
166     $form->get_employee($dbh);
167   }
168
169   ($null, $form->{contact_id}) = split /--/, $form->{contact};
170   $form->{contact_id} *= 1;
171
172   my $ml = ($form->{type} eq 'sales_order') ? 1 : -1;
173
174   if ($form->{id}) {
175
176     &adj_onhand($dbh, $form, $ml) if $form->{type} =~ /_order$/;
177
178     $query = qq|DELETE FROM orderitems
179                 WHERE trans_id = $form->{id}|;
180     $dbh->do($query) || $form->dberror($query);
181
182     $query = qq|DELETE FROM shipto
183                 WHERE trans_id = $form->{id}|;
184     $dbh->do($query) || $form->dberror($query);
185
186   } else {
187
188     my $uid = rand() . time;
189
190     $uid .= $form->{login};
191
192     $uid = substr($uid, 2, 75);
193
194     $query = qq|INSERT INTO oe (ordnumber, employee_id)
195                 VALUES ('$uid', $form->{employee_id})|;
196     $dbh->do($query) || $form->dberror($query);
197
198     $query = qq|SELECT o.id FROM oe o
199                 WHERE o.ordnumber = '$uid'|;
200     $sth = $dbh->prepare($query);
201     $sth->execute || $form->dberror($query);
202
203     ($form->{id}) = $sth->fetchrow_array;
204     $sth->finish;
205   }
206
207   map { $form->{$_} =~ s/\'/\'\'/g }
208     qw(ordnumber quonumber shippingpoint shipvia notes intnotes message);
209
210   my $amount;
211   my $linetotal;
212   my $discount;
213   my $project_id;
214   my $reqdate;
215   my $taxrate;
216   my $taxamount;
217   my $fxsellprice;
218   my %taxbase;
219   my @taxaccounts;
220   my %taxaccounts;
221   my $netamount = 0;
222
223   for my $i (1 .. $form->{rowcount}) {
224
225     map {
226       $form->{"${_}_$i"} = $form->parse_amount($myconfig, $form->{"${_}_$i"})
227     } qw(qty ship);
228
229     if ($form->{"qty_$i"}) {
230
231       map { $form->{"${_}_$i"} =~ s/\'/\'\'/g }
232         qw(partnumber description unit);
233
234       # set values to 0 if nothing entered
235       $form->{"discount_$i"} =
236         $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100;
237
238       $form->{"sellprice_$i"} =
239         $form->parse_amount($myconfig, $form->{"sellprice_$i"});
240       $fxsellprice = $form->{"sellprice_$i"};
241
242       my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
243       $dec = length $dec;
244       my $decimalplaces = ($dec > 2) ? $dec : 2;
245
246       $discount =
247         $form->round_amount($form->{"sellprice_$i"} * $form->{"discount_$i"},
248                             $decimalplaces);
249       $form->{"sellprice_$i"} =
250         $form->round_amount($form->{"sellprice_$i"} - $discount,
251                             $decimalplaces);
252
253       $form->{"inventory_accno_$i"} *= 1;
254       $form->{"expense_accno_$i"}   *= 1;
255
256       $linetotal =
257         $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2);
258
259       @taxaccounts = split / /, $form->{"taxaccounts_$i"};
260       $taxrate     = 0;
261       $taxdiff     = 0;
262
263       map { $taxrate += $form->{"${_}_rate"} } @taxaccounts;
264
265       if ($form->{taxincluded}) {
266         $taxamount = $linetotal * $taxrate / (1 + $taxrate);
267         $taxbase   = $linetotal - $taxamount;
268
269         # we are not keeping a natural price, do not round
270         $form->{"sellprice_$i"} =
271           $form->{"sellprice_$i"} * (1 / (1 + $taxrate));
272       } else {
273         $taxamount = $linetotal * $taxrate;
274         $taxbase   = $linetotal;
275       }
276
277       if ($form->round_amount($taxrate, 7) == 0) {
278         if ($form->{taxincluded}) {
279           foreach $item (@taxaccounts) {
280             $taxamount =
281               $form->round_amount($linetotal * $form->{"${item}_rate"} /
282                                     (1 + abs($form->{"${item}_rate"})),
283                                   2);
284
285             $taxaccounts{$item} += $taxamount;
286             $taxdiff            += $taxamount;
287
288             $taxbase{$item} += $taxbase;
289           }
290           $taxaccounts{ $taxaccounts[0] } += $taxdiff;
291         } else {
292           foreach $item (@taxaccounts) {
293             $taxaccounts{$item} += $linetotal * $form->{"${item}_rate"};
294             $taxbase{$item}     += $taxbase;
295           }
296         }
297       } else {
298         foreach $item (@taxaccounts) {
299           $taxaccounts{$item} +=
300             $taxamount * $form->{"${item}_rate"} / $taxrate;
301           $taxbase{$item} += $taxbase;
302         }
303       }
304
305       $netamount += $form->{"sellprice_$i"} * $form->{"qty_$i"};
306
307       $project_id = 'NULL';
308       if ($form->{"projectnumber_$i"}) {
309         $project_id = $form->{"projectnumber_$i"};
310       }
311       $reqdate =
312         ($form->{"reqdate_$i"}) ? qq|'$form->{"reqdate_$i"}'| : "NULL";
313
314       # save detail record in orderitems table
315       $query = qq|INSERT INTO orderitems (|;
316       $query .= "id, " if $form->{"orderitems_id_$i"};
317       $query .= qq|trans_id, parts_id, description, qty, sellprice, discount,
318                    unit, reqdate, project_id, serialnumber, ship)
319                    VALUES (|;
320       $query .= qq|$form->{"orderitems_id_$i"},|
321         if $form->{"orderitems_id_$i"};
322       $query .= qq|$form->{id}, $form->{"id_$i"},
323                    '$form->{"description_$i"}', $form->{"qty_$i"},
324                    $fxsellprice, $form->{"discount_$i"},
325                    '$form->{"unit_$i"}', $reqdate, (SELECT id from project where projectnumber = '$project_id'),
326                    '$form->{"serialnumber_$i"}', $form->{"ship_$i"})|;
327       $dbh->do($query) || $form->dberror($query);
328
329       $form->{"sellprice_$i"} = $fxsellprice;
330       $form->{"discount_$i"} *= 100;
331     }
332   }
333
334   # set values which could be empty
335   map { $form->{$_} *= 1 }
336     qw(vendor_id customer_id taxincluded closed quotation);
337
338   $reqdate = ($form->{reqdate}) ? qq|'$form->{reqdate}'| : "NULL";
339
340   # add up the tax
341   my $tax = 0;
342   map { $tax += $form->round_amount($taxaccounts{$_}, 2) } keys %taxaccounts;
343
344   $amount = $form->round_amount($netamount + $tax, 2);
345   $netamount = $form->round_amount($netamount, 2);
346
347   if ($form->{currency} eq $form->{defaultcurrency}) {
348     $form->{exchangerate} = 1;
349   } else {
350     $exchangerate =
351       $form->check_exchangerate($myconfig,
352                                 $form->{currency},
353                                 $form->{transdate},
354                                 ($form->{vc} eq 'customer') ? 'buy' : 'sell');
355   }
356
357   $form->{exchangerate} =
358     ($exchangerate)
359     ? $exchangerate
360     : $form->parse_amount($myconfig, $form->{exchangerate});
361
362   my $quotation;
363
364   # fill in subject if there is none
365   if ($form->{type} =~ /_order$/) {
366     $quotation = '0';
367     $form->{subject} = qq|$form->{label} $form->{ordnumber}|
368       unless $form->{subject};
369   } else {
370     $quotation = '1';
371     $form->{subject} = qq|$form->{label} $form->{quonumber}|
372       unless $form->{subject};
373   }
374
375   # if there is a message stuff it into the intnotes
376   my $cc  = "Cc: $form->{cc}\\r\n"   if $form->{cc};
377   my $bcc = "Bcc: $form->{bcc}\\r\n" if $form->{bcc};
378   my $now = scalar localtime;
379   $form->{intnotes} .= qq|\r
380 \r| if $form->{intnotes};
381
382   $form->{intnotes} .= qq|[email]\r
383 Date: $now
384 To: $form->{email}\r
385 $cc${bcc}Subject: $form->{subject}\r
386 \r
387 Message: $form->{message}\r| if $form->{message};
388
389   ($null, $form->{department_id}) = split(/--/, $form->{department});
390   $form->{department_id} *= 1;
391
392   # save OE record
393   $query = qq|UPDATE oe set
394               ordnumber = '$form->{ordnumber}',
395               quonumber = '$form->{quonumber}',
396               cusordnumber = '$form->{cusordnumber}',
397               transdate = '$form->{transdate}',
398               vendor_id = $form->{vendor_id},
399               customer_id = $form->{customer_id},
400               amount = $amount,
401               netamount = $netamount,
402               reqdate = $reqdate,
403               taxincluded = '$form->{taxincluded}',
404               shippingpoint = '$form->{shippingpoint}',
405               shipvia = '$form->{shipvia}',
406               notes = '$form->{notes}',
407               intnotes = '$form->{intnotes}',
408               curr = '$form->{currency}',
409               closed = '$form->{closed}',
410               quotation = '$quotation',
411               department_id = $form->{department_id},
412               employee_id = $form->{employee_id},
413               cp_id = $form->{contact_id}
414               WHERE id = $form->{id}|;
415   $dbh->do($query) || $form->dberror($query);
416
417   $form->{ordtotal} = $amount;
418
419   if ($form->{webdav}) {
420     &webdav_folder($myconfig, $form);
421   }
422
423   # add shipto
424   $form->{name} = $form->{ $form->{vc} };
425   $form->{name} =~ s/--$form->{"$form->{vc}_id"}//;
426   $form->add_shipto($dbh, $form->{id});
427
428   # save printed, emailed, queued
429   $form->save_status($dbh);
430
431   if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
432     if ($form->{vc} eq 'customer') {
433       $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate},
434                                  $form->{exchangerate}, 0);
435     }
436     if ($form->{vc} eq 'vendor') {
437       $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate},
438                                  0, $form->{exchangerate});
439     }
440   }
441
442   if ($form->{type} =~ /_order$/) {
443
444     # adjust onhand
445     &adj_onhand($dbh, $form, $ml * -1);
446     &adj_inventory($dbh, $myconfig, $form);
447   }
448
449   my $rc = $dbh->commit;
450   $dbh->disconnect;
451
452   $main::lxdebug->leave_sub();
453
454   return $rc;
455 }
456
457 sub delete {
458   $main::lxdebug->enter_sub();
459
460   my ($self, $myconfig, $form, $spool) = @_;
461
462   # connect to database
463   my $dbh = $form->dbconnect_noauto($myconfig);
464
465   # delete spool files
466   my $query = qq|SELECT s.spoolfile FROM status s
467                  WHERE s.trans_id = $form->{id}|;
468   $sth = $dbh->prepare($query);
469   $sth->execute || $self->dberror($query);
470
471   my $spoolfile;
472   my @spoolfiles = ();
473
474   while (($spoolfile) = $sth->fetchrow_array) {
475     push @spoolfiles, $spoolfile;
476   }
477   $sth->finish;
478
479   $query = qq|SELECT o.parts_id, o.ship FROM orderitems o
480               WHERE o.trans_id = $form->{id}|;
481   $sth = $dbh->prepare($query);
482   $sth->execute || $self->dberror($query);
483
484   while (my ($id, $ship) = $sth->fetchrow_array) {
485     $form->update_balance($dbh, "parts", "onhand", qq|id = $id|, $ship * -1);
486   }
487   $sth->finish;
488
489   # delete inventory
490   $query = qq|DELETE FROM inventory
491               WHERE oe_id = $form->{id}|;
492   $dbh->do($query) || $form->dberror($query);
493
494   # delete status entries
495   $query = qq|DELETE FROM status
496               WHERE trans_id = $form->{id}|;
497   $dbh->do($query) || $form->dberror($query);
498
499   # delete OE record
500   $query = qq|DELETE FROM oe
501               WHERE id = $form->{id}|;
502   $dbh->do($query) || $form->dberror($query);
503
504   # delete individual entries
505   $query = qq|DELETE FROM orderitems
506               WHERE trans_id = $form->{id}|;
507   $dbh->do($query) || $form->dberror($query);
508
509   $query = qq|DELETE FROM shipto
510               WHERE trans_id = $form->{id}|;
511   $dbh->do($query) || $form->dberror($query);
512
513   my $rc = $dbh->commit;
514   $dbh->disconnect;
515
516   if ($rc) {
517     foreach $spoolfile (@spoolfiles) {
518       unlink "$spool/$spoolfile" if $spoolfile;
519     }
520   }
521
522   $main::lxdebug->leave_sub();
523
524   return $rc;
525 }
526
527 sub retrieve {
528   $main::lxdebug->enter_sub();
529
530   my ($self, $myconfig, $form) = @_;
531
532   # connect to database
533   my $dbh = $form->dbconnect_noauto($myconfig);
534
535   my $query;
536
537   if ($form->{id}) {
538
539     # get default accounts and last order number
540     $query = qq|SELECT (SELECT c.accno FROM chart c
541                         WHERE d.inventory_accno_id = c.id) AS inventory_accno,
542                        (SELECT c.accno FROM chart c
543                         WHERE d.income_accno_id = c.id) AS income_accno,
544                        (SELECT c.accno FROM chart c
545                         WHERE d.expense_accno_id = c.id) AS expense_accno,
546                        (SELECT c.accno FROM chart c
547                         WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
548                        (SELECT c.accno FROM chart c
549                         WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
550                 d.curr AS currencies
551                 FROM defaults d|;
552   } else {
553     $query = qq|SELECT (SELECT c.accno FROM chart c
554                         WHERE d.inventory_accno_id = c.id) AS inventory_accno,
555                        (SELECT c.accno FROM chart c
556                         WHERE d.income_accno_id = c.id) AS income_accno,
557                        (SELECT c.accno FROM chart c
558                         WHERE d.expense_accno_id = c.id) AS expense_accno,
559                        (SELECT c.accno FROM chart c
560                         WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
561                        (SELECT c.accno FROM chart c
562                         WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
563                 d.curr AS currencies,
564                 current_date AS transdate, current_date AS reqdate
565                 FROM defaults d|;
566   }
567   my $sth = $dbh->prepare($query);
568   $sth->execute || $form->dberror($query);
569
570   my $ref = $sth->fetchrow_hashref(NAME_lc);
571   map { $form->{$_} = $ref->{$_} } keys %$ref;
572   $sth->finish;
573
574   ($form->{currency}) = split /:/, $form->{currencies};
575
576   if ($form->{id}) {
577
578     # retrieve order
579     $query = qq|SELECT o.cp_id,o.ordnumber, o.transdate, o.reqdate,
580                 o.taxincluded, o.shippingpoint, o.shipvia, o.notes, o.intnotes,
581                 o.curr AS currency, e.name AS employee, o.employee_id,
582                 o.$form->{vc}_id, cv.name AS $form->{vc}, o.amount AS invtotal,
583                 o.closed, o.reqdate, o.quonumber, o.department_id, o.cusordnumber,
584                 d.description AS department
585                 FROM oe o
586                 JOIN $form->{vc} cv ON (o.$form->{vc}_id = cv.id)
587                 LEFT JOIN employee e ON (o.employee_id = e.id)
588                 LEFT JOIN department d ON (o.department_id = d.id)
589                 WHERE o.id = $form->{id}|;
590     $sth = $dbh->prepare($query);
591     $sth->execute || $form->dberror($query);
592
593     $ref = $sth->fetchrow_hashref(NAME_lc);
594     map { $form->{$_} = $ref->{$_} } keys %$ref;
595     $sth->finish;
596
597     $query = qq|SELECT s.* FROM shipto s
598                 WHERE s.trans_id = $form->{id}|;
599     $sth = $dbh->prepare($query);
600     $sth->execute || $form->dberror($query);
601
602     $ref = $sth->fetchrow_hashref(NAME_lc);
603     map { $form->{$_} = $ref->{$_} } keys %$ref;
604     $sth->finish;
605
606     # get printed, emailed and queued
607     $query = qq|SELECT s.printed, s.emailed, s.spoolfile, s.formname
608                 FROM status s
609                 WHERE s.trans_id = $form->{id}|;
610     $sth = $dbh->prepare($query);
611     $sth->execute || $form->dberror($query);
612
613     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
614       $form->{printed} .= "$ref->{formname} " if $ref->{printed};
615       $form->{emailed} .= "$ref->{formname} " if $ref->{emailed};
616       $form->{queued} .= "$ref->{formname} $ref->{spoolfile} "
617         if $ref->{spoolfile};
618     }
619     $sth->finish;
620     map { $form->{$_} =~ s/ +$//g } qw(printed emailed queued);
621
622     my %oid = ('Pg'     => 'oid',
623                'Oracle' => 'rowid');
624
625     # retrieve individual items
626     $query = qq|SELECT o.id AS orderitems_id,
627                 c1.accno AS inventory_accno,
628                 c2.accno AS income_accno,
629                 c3.accno AS expense_accno,
630                 p.partnumber, p.assembly, o.description, o.qty,
631                 o.sellprice, o.parts_id AS id, o.unit, o.discount, p.bin, p.notes AS partnotes,
632                 o.reqdate, o.project_id, o.serialnumber, o.ship,
633                 pr.projectnumber,
634                 pg.partsgroup
635                 FROM orderitems o
636                 JOIN parts p ON (o.parts_id = p.id)
637                 LEFT JOIN chart c1 ON (p.inventory_accno_id = c1.id)
638                 LEFT JOIN chart c2 ON (p.income_accno_id = c2.id)
639                 LEFT JOIN chart c3 ON (p.expense_accno_id = c3.id)
640                 LEFT JOIN project pr ON (o.project_id = pr.id)
641                 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
642                 WHERE o.trans_id = $form->{id}
643                 ORDER BY o.$oid{$myconfig->{dbdriver}}|;
644     $sth = $dbh->prepare($query);
645     $sth->execute || $form->dberror($query);
646
647     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
648
649       #set expense_accno=inventory_accno if they are different => bilanz
650       $vendor_accno =
651         ($ref->{expense_accno} != $ref->{inventory_accno})
652         ? $ref->{inventory_accno}
653         : $ref->{expense_accno};
654
655       # get tax rates and description
656       $accno_id =
657         ($form->{vc} eq "customer") ? $ref->{income_accno} : $vendor_accno;
658       $query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
659                  FROM chart c, tax t
660                  WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
661                  ORDER BY accno|;
662       $stw = $dbh->prepare($query);
663       $stw->execute || $form->dberror($query);
664       $ref->{taxaccounts} = "";
665       while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
666
667         #    if ($customertax{$ref->{accno}}) {
668         $ref->{taxaccounts} .= "$ptr->{accno} ";
669         if (!($form->{taxaccounts} =~ /$ptr->{accno}/)) {
670           $form->{"$ptr->{accno}_rate"}        = $ptr->{rate};
671           $form->{"$ptr->{accno}_description"} = $ptr->{description};
672           $form->{"$ptr->{accno}_taxnumber"}   = $ptr->{taxnumber};
673           $form->{taxaccounts} .= "$ptr->{accno} ";
674         }
675
676       }
677
678       chop $ref->{taxaccounts};
679       push @{ $form->{form_details} }, $ref;
680       $stw->finish;
681     }
682     $sth->finish;
683
684   } else {
685
686     # get last name used
687     $form->lastname_used($dbh, $myconfig, $form->{vc})
688       unless $form->{"$form->{vc}_id"};
689
690   }
691
692   $form->{exchangerate} =
693     $form->get_exchangerate($dbh, $form->{currency}, $form->{transdate},
694                             ($form->{vc} eq 'customer') ? "buy" : "sell");
695
696   if ($form->{webdav}) {
697     &webdav_folder($myconfig, $form);
698   }
699
700   my $rc = $dbh->commit;
701   $dbh->disconnect;
702
703   $main::lxdebug->leave_sub();
704
705   return $rc;
706 }
707
708 sub order_details {
709   $main::lxdebug->enter_sub();
710
711   my ($self, $myconfig, $form) = @_;
712
713   # connect to database
714   my $dbh = $form->dbconnect($myconfig);
715   my $query;
716   my $sth;
717
718   my $item;
719   my $i;
720   my @partsgroup = ();
721   my $partsgroup;
722   my %oid = ('Pg'     => 'oid',
723              'Oracle' => 'rowid');
724
725   # sort items by partsgroup
726   for $i (1 .. $form->{rowcount}) {
727     $partsgroup = "";
728     if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
729       $form->format_string("partsgroup_$i");
730       $partsgroup = $form->{"partsgroup_$i"};
731     }
732     push @partsgroup, [$i, $partsgroup];
733   }
734
735   # if there is a warehouse limit picking
736   if ($form->{warehouse_id} && $form->{formname} =~ /(pick|packing)_list/) {
737
738     # run query to check for inventory
739     $query = qq|SELECT sum(i.qty) AS qty
740                 FROM inventory i
741                 WHERE i.parts_id = ?
742                 AND i.warehouse_id = ?|;
743     $sth = $dbh->prepare($query) || $form->dberror($query);
744
745     for $i (1 .. $form->{rowcount}) {
746       $sth->execute($form->{"id_$i"}, $form->{warehouse_id}) || $form->dberror;
747
748       ($qty) = $sth->fetchrow_array;
749       $sth->finish;
750
751       $form->{"qty_$i"} = 0 if $qty == 0;
752
753       if ($form->parse_amount($myconfig, $form->{"ship_$i"}) > $qty) {
754         $form->{"ship_$i"} = $form->format_amount($myconfig, $qty);
755       }
756     }
757   }
758
759   my $sameitem = "";
760   foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) {
761     $i = $item->[0];
762
763     if ($item->[1] ne $sameitem) {
764       push(@{ $form->{description} }, qq|$item->[1]|);
765       $sameitem = $item->[1];
766
767       map { push(@{ $form->{$_} }, "") }
768         qw(runningnumber number qty ship unit bin partnotes serialnumber reqdate sellprice listprice netprice discount linetotal);
769     }
770
771     $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
772
773     if ($form->{"qty_$i"} != 0) {
774
775       # add number, description and qty to $form->{number}, ....
776       push(@{ $form->{runningnumber} }, $i);
777       push(@{ $form->{number} },        qq|$form->{"partnumber_$i"}|);
778       push(@{ $form->{description} },   qq|$form->{"description_$i"}|);
779       push(@{ $form->{qty} },
780            $form->format_amount($myconfig, $form->{"qty_$i"}));
781       push(@{ $form->{ship} },
782            $form->format_amount($myconfig, $form->{"ship_$i"}));
783       push(@{ $form->{unit} },         qq|$form->{"unit_$i"}|);
784       push(@{ $form->{bin} },          qq|$form->{"bin_$i"}|);
785       push(@{ $form->{"partnotes"} },  qq|$form->{"partnotes_$i"}|);
786       push(@{ $form->{serialnumber} }, qq|$form->{"serialnumber_$i"}|);
787       push(@{ $form->{reqdate} },      qq|$form->{"reqdate_$i"}|);
788
789       push(@{ $form->{sellprice} }, $form->{"sellprice_$i"});
790
791       push(@{ $form->{listprice} }, $form->{"listprice_$i"});
792
793       my $sellprice = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
794       my ($dec) = ($sellprice =~ /\.(\d+)/);
795       $dec = length $dec;
796       my $decimalplaces = ($dec > 2) ? $dec : 2;
797
798       my $discount =
799         $form->round_amount(
800                             $sellprice * $form->parse_amount($myconfig,
801                                                  $form->{"discount_$i"}) / 100,
802                             $decimalplaces);
803
804       # keep a netprice as well, (sellprice - discount)
805       $form->{"netprice_$i"} = $sellprice - $discount;
806
807       my $linetotal =
808         $form->round_amount($form->{"qty_$i"} * $form->{"netprice_$i"}, 2);
809
810       push(@{ $form->{netprice} },
811            ($form->{"netprice_$i"} != 0)
812            ? $form->format_amount(
813                                  $myconfig, $form->{"netprice_$i"},
814                                  $decimalplaces
815              )
816            : " ");
817
818       $discount =
819         ($discount != 0)
820         ? $form->format_amount($myconfig, $discount * -1, $decimalplaces)
821         : " ";
822       $linetotal = ($linetotal != 0) ? $linetotal : " ";
823
824       push(@{ $form->{discount} }, $discount);
825
826       $form->{ordtotal} += $linetotal;
827
828       push(@{ $form->{linetotal} },
829            $form->format_amount($myconfig, $linetotal, 2));
830
831       my ($taxamount, $taxbase);
832       my $taxrate = 0;
833
834       map { $taxrate += $form->{"${_}_rate"} } split / /,
835         $form->{"taxaccounts_$i"};
836
837       if ($form->{taxincluded}) {
838
839         # calculate tax
840         $taxamount = $linetotal * $taxrate / (1 + $taxrate);
841         $taxbase = $linetotal / (1 + $taxrate);
842       } else {
843         $taxamount = $linetotal * $taxrate;
844         $taxbase   = $linetotal;
845       }
846
847       if ($taxamount != 0) {
848         foreach my $item (split / /, $form->{"taxaccounts_$i"}) {
849           $taxaccounts{$item} +=
850             $taxamount * $form->{"${item}_rate"} / $taxrate;
851           $taxbase{$item} += $taxbase;
852         }
853       }
854
855       if ($form->{"assembly_$i"}) {
856         $sameitem = "";
857
858         # get parts and push them onto the stack
859         my $sortorder = "";
860         if ($form->{groupitems}) {
861           $sortorder =
862             qq|ORDER BY pg.partsgroup, a.$oid{$myconfig->{dbdriver}}|;
863         } else {
864           $sortorder = qq|ORDER BY a.$oid{$myconfig->{dbdriver}}|;
865         }
866
867         $query = qq|SELECT p.partnumber, p.description, p.unit, a.qty,
868                     pg.partsgroup
869                     FROM assembly a
870                     JOIN parts p ON (a.parts_id = p.id)
871                     LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
872                     WHERE a.bom = '1'
873                     AND a.id = '$form->{"id_$i"}'
874                     $sortorder|;
875         $sth = $dbh->prepare($query);
876         $sth->execute || $form->dberror($query);
877
878         while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
879           if ($form->{groupitems} && $ref->{partsgroup} ne $sameitem) {
880             map { push(@{ $form->{$_} }, "") }
881               qw(runningnumber ship bin serialnumber number unit bin qty reqdate sellprice listprice netprice discount linetotal);
882             $sameitem = ($ref->{partsgroup}) ? $ref->{partsgroup} : "--";
883             push(@{ $form->{description} }, $sameitem);
884           }
885
886           push(@{ $form->{description} },
887                $form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}
888                  )
889                  . qq|, $ref->{partnumber}, $ref->{description}|);
890
891           map { push(@{ $form->{$_} }, "") }
892             qw(number unit qty runningnumber ship bin serialnumber reqdate sellprice listprice netprice discount linetotal);
893
894         }
895         $sth->finish;
896       }
897
898     }
899   }
900
901   my $tax = 0;
902   foreach $item (sort keys %taxaccounts) {
903     if ($form->round_amount($taxaccounts{$item}, 2) != 0) {
904       push(@{ $form->{taxbase} },
905            $form->format_amount($myconfig, $taxbase{$item}, 2));
906
907       $tax += $taxamount = $form->round_amount($taxaccounts{$item}, 2);
908
909       push(@{ $form->{tax} }, $form->format_amount($myconfig, $taxamount, 2));
910       push(@{ $form->{taxdescription} }, $form->{"${item}_description"});
911       push(@{ $form->{taxrate} },
912            $form->format_amount($myconfig, $form->{"${item}_rate"} * 100));
913       push(@{ $form->{taxnumber} }, $form->{"${item}_taxnumber"});
914     }
915   }
916
917   $form->{subtotal} = $form->format_amount($myconfig, $form->{ordtotal}, 2);
918   $form->{ordtotal} =
919     ($form->{taxincluded}) ? $form->{ordtotal} : $form->{ordtotal} + $tax;
920
921   # format amounts
922   $form->{quototal} = $form->{ordtotal} =
923     $form->format_amount($myconfig, $form->{ordtotal}, 2);
924
925   # myconfig variables
926   map { $form->{$_} = $myconfig->{$_} }
927     (qw(company address tel fax signature businessnumber));
928   $form->{username} = $myconfig->{name};
929
930   $dbh->disconnect;
931
932   $main::lxdebug->leave_sub();
933 }
934
935 sub project_description {
936   $main::lxdebug->enter_sub();
937
938   my ($self, $dbh, $id) = @_;
939
940   my $query = qq|SELECT p.description
941                  FROM project p
942                  WHERE p.id = $id|;
943   my $sth = $dbh->prepare($query);
944   $sth->execute || $form->dberror($query);
945
946   ($_) = $sth->fetchrow_array;
947
948   $sth->finish;
949
950   $main::lxdebug->leave_sub();
951
952   return $_;
953 }
954
955 sub get_warehouses {
956   $main::lxdebug->enter_sub();
957
958   my ($self, $myconfig, $form) = @_;
959
960   my $dbh = $form->dbconnect($myconfig);
961
962   # setup warehouses
963   my $query = qq|SELECT id, description
964                  FROM warehouse|;
965
966   my $sth = $dbh->prepare($query);
967   $sth->execute || $form->dberror($query);
968
969   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
970     push @{ $form->{all_warehouses} }, $ref;
971   }
972   $sth->finish;
973
974   $dbh->disconnect;
975
976   $main::lxdebug->leave_sub();
977 }
978
979 sub save_inventory {
980   $main::lxdebug->enter_sub();
981
982   my ($self, $myconfig, $form) = @_;
983
984   my ($null, $warehouse_id) = split /--/, $form->{warehouse};
985   $warehouse_id *= 1;
986
987   my $employee_id;
988   ($null, $employee_id) = split /--/, $form->{employee};
989
990   my $ml = ($form->{type} eq 'ship_order') ? -1 : 1;
991
992   my $dbh = $form->dbconnect_noauto($myconfig);
993   my $sth;
994   my $wth;
995   my $serialnumber;
996   my $ship;
997
998   $query = qq|SELECT o.serialnumber, o.ship
999               FROM orderitems o
1000               WHERE o.trans_id = ?
1001               AND o.id = ?
1002               FOR UPDATE|;
1003   $sth = $dbh->prepare($query) || $form->dberror($query);
1004
1005   $query = qq|SELECT sum(i.qty)
1006               FROM inventory i
1007               WHERE i.parts_id = ?
1008               AND i.warehouse_id = ?|;
1009   $wth = $dbh->prepare($query) || $form->dberror($query);
1010
1011   for my $i (1 .. $form->{rowcount} - 1) {
1012
1013     $ship =
1014       (abs($form->{"ship_$i"}) > abs($form->{"qty_$i"}))
1015       ? $form->{"qty_$i"}
1016       : $form->{"ship_$i"};
1017
1018     if ($warehouse_id && $form->{type} eq 'ship_order') {
1019
1020       $wth->execute($form->{"id_$i"}, $warehouse_id) || $form->dberror;
1021
1022       ($qty) = $wth->fetchrow_array;
1023       $wth->finish;
1024
1025       if ($ship > $qty) {
1026         $ship = $qty;
1027       }
1028     }
1029
1030     if ($ship != 0) {
1031
1032       $ship *= $ml;
1033       $query = qq|INSERT INTO inventory (parts_id, warehouse_id,
1034                   qty, oe_id, orderitems_id, shippingdate, employee_id)
1035                   VALUES ($form->{"id_$i"}, $warehouse_id,
1036                   $ship, $form->{"id"},
1037                   $form->{"orderitems_id_$i"}, '$form->{shippingdate}',
1038                   $employee_id)|;
1039       $dbh->do($query) || $form->dberror($query);
1040
1041       # add serialnumber, ship to orderitems
1042       $sth->execute($form->{id}, $form->{"orderitems_id_$i"})
1043         || $form->dberror;
1044       ($serialnumber, $ship) = $sth->fetchrow_array;
1045       $sth->finish;
1046
1047       $serialnumber .= " " if $serialnumber;
1048       $serialnumber .= qq|$form->{"serialnumber_$i"}|;
1049       $ship += $form->{"ship_$i"};
1050
1051       $query = qq|UPDATE orderitems SET
1052                   serialnumber = '$serialnumber',
1053                   ship = $ship
1054                   WHERE trans_id = $form->{id}
1055                   AND id = $form->{"orderitems_id_$i"}|;
1056       $dbh->do($query) || $form->dberror($query);
1057
1058       # update order with ship via
1059       $query = qq|UPDATE oe SET
1060                   shippingpoint = '$form->{shippingpoint}',
1061                   shipvia = '$form->{shipvia}'
1062                   WHERE id = $form->{id}|;
1063       $dbh->do($query) || $form->dberror($query);
1064
1065       # update onhand for parts
1066       $form->update_balance($dbh, "parts", "onhand",
1067                             qq|id = $form->{"id_$i"}|,
1068                             $form->{"ship_$i"} * $ml);
1069
1070     }
1071   }
1072
1073   my $rc = $dbh->commit;
1074   $dbh->disconnect;
1075
1076   $main::lxdebug->leave_sub();
1077
1078   return $rc;
1079 }
1080
1081 sub adj_onhand {
1082   $main::lxdebug->enter_sub();
1083
1084   my ($dbh, $form, $ml) = @_;
1085
1086   my $query = qq|SELECT oi.parts_id, oi.ship, p.inventory_accno_id, p.assembly
1087                  FROM orderitems oi
1088                  JOIN parts p ON (p.id = oi.parts_id)
1089                  WHERE oi.trans_id = $form->{id}|;
1090   my $sth = $dbh->prepare($query);
1091   $sth->execute || $form->dberror($query);
1092
1093   $query = qq|SELECT sum(p.inventory_accno_id)
1094               FROM parts p
1095               JOIN assembly a ON (a.parts_id = p.id)
1096               WHERE a.id = ?|;
1097   my $ath = $dbh->prepare($query) || $form->dberror($query);
1098
1099   my $ispa;
1100
1101   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1102
1103     if ($ref->{inventory_accno_id} || $ref->{assembly}) {
1104
1105       # do not update if assembly consists of all services
1106       if ($ref->{assembly}) {
1107         $ath->execute($ref->{parts_id}) || $form->dberror($query);
1108
1109         ($ispa) = $sth->fetchrow_array;
1110         $ath->finish;
1111
1112         next unless $ispa;
1113
1114       }
1115
1116       # adjust onhand in parts table
1117       $form->update_balance($dbh, "parts", "onhand",
1118                             qq|id = $ref->{parts_id}|,
1119                             $ref->{ship} * $ml);
1120     }
1121   }
1122
1123   $sth->finish;
1124
1125   $main::lxdebug->leave_sub();
1126 }
1127
1128 sub adj_inventory {
1129   $main::lxdebug->enter_sub();
1130
1131   my ($dbh, $myconfig, $form) = @_;
1132
1133   my %oid = ('Pg'     => 'oid',
1134              'Oracle' => 'rowid');
1135
1136   # increase/reduce qty in inventory table
1137   my $query = qq|SELECT oi.id, oi.parts_id, oi.ship
1138                  FROM orderitems oi
1139                  WHERE oi.trans_id = $form->{id}|;
1140   my $sth = $dbh->prepare($query);
1141   $sth->execute || $form->dberror($query);
1142
1143   $query = qq|SELECT $oid{$myconfig->{dbdriver}} AS oid, qty,
1144                      (SELECT SUM(qty) FROM inventory
1145                       WHERE oe_id = $form->{id}
1146                       AND orderitems_id = ?) AS total
1147               FROM inventory
1148               WHERE oe_id = $form->{id}
1149               AND orderitems_id = ?|;
1150   my $ith = $dbh->prepare($query) || $form->dberror($query);
1151
1152   my $qty;
1153   my $ml = ($form->{type} =~ /(ship|sales)_order/) ? -1 : 1;
1154
1155   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1156
1157     $ith->execute($ref->{id}, $ref->{id}) || $form->dberror($query);
1158
1159     while (my $inv = $ith->fetchrow_hashref(NAME_lc)) {
1160
1161       if (($qty = (($inv->{total} * $ml) - $ref->{ship})) >= 0) {
1162         $qty = $inv->{qty} if ($qty > ($inv->{qty} * $ml));
1163
1164         $form->update_balance($dbh, "inventory", "qty",
1165                               qq|$oid{$myconfig->{dbdriver}} = $inv->{oid}|,
1166                               $qty * -1 * $ml);
1167       }
1168     }
1169     $ith->finish;
1170
1171   }
1172   $sth->finish;
1173
1174   # delete inventory entries if qty = 0
1175   $query = qq|DELETE FROM inventory
1176               WHERE oe_id = $form->{id}
1177               AND qty = 0|;
1178   $dbh->do($query) || $form->dberror($query);
1179
1180   $main::lxdebug->leave_sub();
1181 }
1182
1183 sub get_inventory {
1184   $main::lxdebug->enter_sub();
1185
1186   my ($self, $myconfig, $form) = @_;
1187
1188   my ($null, $warehouse_id) = split /--/, $form->{warehouse};
1189   $warehouse_id *= 1;
1190
1191   my $dbh = $form->dbconnect($myconfig);
1192
1193   my $query = qq|SELECT p.id, p.partnumber, p.description, p.onhand,
1194                  pg.partsgroup
1195                  FROM parts p
1196                  LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1197                  WHERE p.onhand > 0|;
1198
1199   if ($form->{partnumber}) {
1200     $var = $form->like(lc $form->{partnumber});
1201     $query .= "
1202                  AND lower(p.partnumber) LIKE '$var'";
1203   }
1204   if ($form->{description}) {
1205     $var = $form->like(lc $form->{description});
1206     $query .= "
1207                  AND lower(p.description) LIKE '$var'";
1208   }
1209   if ($form->{partsgroup}) {
1210     $var = $form->like(lc $form->{partsgroup});
1211     $query .= "
1212                  AND lower(pg.partsgroup) LIKE '$var'";
1213   }
1214
1215   $sth = $dbh->prepare($query);
1216   $sth->execute || $form->dberror($query);
1217
1218   $query = qq|SELECT sum(i.qty), w.description, w.id
1219               FROM inventory i
1220               LEFT JOIN warehouse w ON (w.id = i.warehouse_id)
1221               WHERE i.parts_id = ?
1222               AND NOT i.warehouse_id = $warehouse_id
1223               GROUP BY w.description, w.id|;
1224   $wth = $dbh->prepare($query) || $form->dberror($query);
1225
1226   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1227
1228     $wth->execute($ref->{id}) || $form->dberror;
1229
1230     while (($qty, $warehouse, $warehouse_id) = $wth->fetchrow_array) {
1231       push @{ $form->{all_inventory} },
1232         { 'id'           => $ref->{id},
1233           'partnumber'   => $ref->{partnumber},
1234           'description'  => $ref->{description},
1235           'partsgroup'   => $ref->{partsgroup},
1236           'qty'          => $qty,
1237           'warehouse_id' => $warehouse_id,
1238           'warehouse'    => $warehouse }
1239         if $qty > 0;
1240     }
1241     $wth->finish;
1242   }
1243   $sth->finish;
1244
1245   $dbh->disconnect;
1246
1247   # sort inventory
1248   @{ $form->{all_inventory} } =
1249     sort { $a->{ $form->{sort} } cmp $b->{ $form->{sort} } }
1250     @{ $form->{all_inventory} };
1251
1252   $main::lxdebug->leave_sub();
1253
1254   return @{ $form->{all_inventory} };
1255 }
1256
1257 sub transfer {
1258   $main::lxdebug->enter_sub();
1259
1260   my ($self, $myconfig, $form) = @_;
1261
1262   my $dbh = $form->dbconnect_noauto($myconfig);
1263
1264   my $query = qq|INSERT INTO inventory
1265                  (warehouse_id, parts_id, qty, shippingdate, employee_id)
1266                  VALUES (?, ?, ?, ?, ?)|;
1267   $sth = $dbh->prepare($query) || $form->dberror($query);
1268
1269   $form->get_employee($dbh);
1270
1271   my @a = localtime;
1272   $a[5] += 1900;
1273   $a[4]++;
1274   $shippingdate = "$a[5]-$a[4]-$a[3]";
1275
1276   for my $i (1 .. $form->{rowcount}) {
1277     $qty = $form->parse_amount($myconfig, $form->{"transfer_$i"});
1278
1279     $qty = $form->{"qty_$i"} if ($qty > $form->{"qty_$i"});
1280
1281     if ($qty) {
1282
1283       # to warehouse
1284       $sth->execute($form->{warehouse_id}, $form->{"id_$i"}, $qty,
1285                     $shippingdate, $form->{employee_id})
1286         || $form->dberror;
1287
1288       $sth->finish;
1289
1290       # from warehouse
1291       $sth->execute($form->{"warehouse_id_$i"},
1292                     $form->{"id_$i"}, $qty * -1, $shippingdate,
1293                     $form->{employee_id})
1294         || $form->dberror;
1295
1296       $sth->finish;
1297     }
1298   }
1299
1300   my $rc = $dbh->commit;
1301   $dbh->disconnect;
1302
1303   $main::lxdebug->leave_sub();
1304
1305   return $rc;
1306 }
1307
1308 sub webdav_folder {
1309   $main::lxdebug->enter_sub();
1310
1311   my ($myconfig, $form) = @_;
1312
1313 SWITCH: {
1314     $path = "webdav/angebote/" . $form->{quonumber}, last SWITCH
1315       if ($form->{type} eq "sales_quotation");
1316     $path = "webdav/bestellungen/" . $form->{ordnumber}, last SWITCH
1317       if ($form->{type} eq "sales_order");
1318     $path = "webdav/anfragen/" . $form->{quonumber}, last SWITCH
1319       if ($form->{type} eq "request_quotation");
1320     $path = "webdav/lieferantenbestellungen/" . $form->{ordnumber}, last SWITCH
1321       if ($form->{type} eq "purchase_order");
1322   }
1323
1324   if (!-d $path) {
1325     mkdir($path, 0770) or die "can't make directory $!\n";
1326   } else {
1327     if ($form->{id}) {
1328       @files = <$path/*>;
1329       foreach $file (@files) {
1330         $file =~ /\/([^\/]*)$/;
1331         $fname = $1;
1332         $ENV{'SCRIPT_NAME'} =~ /\/([^\/]*)\//;
1333         $lxerp = $1;
1334         $link  = "http://" . $ENV{'SERVER_NAME'} . "/" . $lxerp . "/" . $file;
1335         $form->{WEBDAV}{$fname} = $link;
1336       }
1337     }
1338   }
1339
1340   $main::lxdebug->leave_sub();
1341 }
1342 1;
1343