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