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