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