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