d7373d78bf56bd4600a978dd8baa73cd273b87f4
[kivitendo-erp.git] / bin / mozilla / am.pl
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) 1998-2002
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
20 #
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #======================================================================
29 #
30 # administration
31 #
32 #======================================================================
33
34 use SL::AM;
35 use SL::CA;
36 use SL::Form;
37 use SL::User;
38
39 use Data::Dumper;
40
41 1;
42
43 # end of main
44
45 sub add    { &{"add_$form->{type}"} }
46 sub edit   { &{"edit_$form->{type}"} }
47 sub save   { &{"save_$form->{type}"} }
48 sub delete { &{"delete_$form->{type}"} }
49
50 sub add_account {
51   $lxdebug->enter_sub();
52
53   $form->{title}     = "Add";
54   $form->{charttype} = "A";
55   AM->get_account(\%myconfig, \%$form);
56
57   $form->{callback} =
58     "$form->{script}?action=list_account&path=$form->{path}&login=$form->{login}&password=$form->{password}"
59     unless $form->{callback};
60
61   &account_header;
62   &form_footer;
63
64   $lxdebug->leave_sub();
65 }
66
67 sub edit_account {
68   $lxdebug->enter_sub();
69
70   $form->{title} = "Edit";
71   AM->get_account(\%myconfig, \%$form);
72
73   foreach my $item (split(/:/, $form->{link})) {
74     $form->{$item} = "checked";
75   }
76
77   &account_header;
78   &form_footer;
79
80   $lxdebug->leave_sub();
81 }
82
83 sub account_header {
84   $lxdebug->enter_sub();
85
86   $form->{title} = $locale->text("$form->{title} Account");
87
88   $checked{ $form->{charttype} } = "checked";
89   $checked{"$form->{category}_"} = "checked";
90   $checked{CT_tax} = ($form->{CT_tax}) ? "" : "checked";
91
92   $form->{description} =~ s/\"/"/g;
93
94   if (@{ $form->{TAXKEY} }) {
95     $form->{selecttaxkey} = "<option value=0>Keine Steuer 0%\n";
96     foreach $item (@{ $form->{TAXKEY} }) {
97       if ($item->{taxkey} == $form->{taxkey_id}) {
98         $form->{selecttaxkey} .=
99           "<option value=$item->{taxkey} selected>$item->{taxdescription}\n";
100       } else {
101         $form->{selecttaxkey} .=
102           "<option value=$item->{taxkey}>$item->{taxdescription}\n";
103       }
104
105     }
106   }
107
108   $taxkey = qq|
109               <tr>
110                 <th align=right>| . $locale->text('Steuersatz') . qq|</th>
111                 <td><select name=taxkey_id>$form->{selecttaxkey}</select></td>
112                 <input type=hidden name=selecttaxkey value="$form->{selecttaxkey}">
113               </tr>|;
114
115   if (@{ $form->{NEWACCOUNT} }) {
116     if (!$form->{new_chart_valid}) {
117       $form->{selectnewaccount} = "<option value=></option>";
118     }
119     foreach $item (@{ $form->{NEWACCOUNT} }) {
120       if ($item->{id} == $form->{new_chart_id}) {
121         $form->{selectnewaccount} .=
122           "<option value=$item->{id} selected>$item->{accno}--$item->{description}</option>";
123       } elsif (!$form->{new_chart_valid}) {
124         $form->{selectnewaccount} .=
125           "<option value=$item->{id}>$item->{accno}--$item->{description}</option>";
126       }
127
128     }
129   }
130
131   $newaccount = qq|
132               <tr>
133                 <td colspan=2>
134                   <table>
135                     <tr>
136                       <th align=right>| . $locale->text('Folgekonto') . qq|</th>
137                       <td><select name=new_chart_id>$form->{selectnewaccount}</select></td>
138                       <th align=right>| . $locale->text('Gültig ab') . qq|</th>
139                       <td><input name=valid_from value="$form->{valid_from}"></td>
140                     </tr>
141                   </table>
142                 </td>
143               </tr>|;
144
145   $form->{selectustva} = "<option>\n";
146
147   %ustva = (35  => $locale->text('UStVA-Nr. 35'),
148             36  => $locale->text('UStVA-Nr. 36'),
149             39  => $locale->text('UStVA-Nr. 39'),
150             41  => $locale->text('UStVA-Nr. 41'),
151             42  => $locale->text('UStVA-Nr. 42'),
152             43  => $locale->text('UStVA-Nr. 43'),
153             44  => $locale->text('UStVA-Nr. 44'),
154             45  => $locale->text('UStVA-Nr. 45'),
155             48  => $locale->text('UStVA-Nr. 48'),
156             49  => $locale->text('UStVA-Nr. 49'),
157             51  => $locale->text('UStVA-Nr. 51 left'),
158             511 => $locale->text('UStVA-Nr. 51 right'),
159             52  => $locale->text('UStVA-Nr. 52'),
160             53  => $locale->text('UStVA-Nr. 53'),
161             59  => $locale->text('UStVA-Nr. 59'),
162             60  => $locale->text('UStVA-Nr. 60'),
163             61  => $locale->text('UStVA-Nr. 61'),
164             62  => $locale->text('UStVA-Nr. 62'),
165             63  => $locale->text('UStVA-Nr. 63'),
166             64  => $locale->text('UStVA-Nr. 64'),
167             65  => $locale->text('UStVA-Nr. 65'),
168             66  => $locale->text('UStVA-Nr. 66'),
169             67  => $locale->text('UStVA-Nr. 67'),
170             69  => $locale->text('UStVA-Nr. 69'),
171             73  => $locale->text('UStVA-Nr. 73'),
172             74  => $locale->text('UStVA-Nr. 74'),
173             76  => $locale->text('UStVA-Nr. 76'),
174             77  => $locale->text('UStVA-Nr. 77'),
175             80  => $locale->text('UStVA-Nr. 80'),
176             84  => $locale->text('UStVA-Nr. 84'),
177             85  => $locale->text('UStVA-Nr. 85'),
178             86  => $locale->text('UStVA-Nr. 86 left'),
179             861 => $locale->text('UStVA-Nr. 86 right'),
180             91  => $locale->text('UStVA-Nr. 91'),
181             93  => $locale->text('UStVA-Nr. 93 left'),
182             931 => $locale->text('UStVA-Nr. 93 right'),
183             94  => $locale->text('UStVA-Nr. 94'),
184             95  => $locale->text('UStVA-Nr. 95'),
185             96  => $locale->text('UStVA-Nr. 96'),
186             97  => $locale->text('UStVA-Nr. 97 links'),
187             971 => $locale->text('UStVA-Nr. 97 rechts'),
188             98  => $locale->text('UStVA-Nr. 98'));
189
190   foreach $item (sort({ $a cmp $b } keys %ustva)) {
191     if ($item == $form->{pos_ustva}) {
192       $form->{selectustva} .= "<option value=$item selected>$ustva{$item}\n";
193     } else {
194       $form->{selectustva} .= "<option value=$item>$ustva{$item}\n";
195     }
196
197   }
198
199   $ustva = qq|
200               <tr>
201                 <th align=right>| . $locale->text('Umsatzsteuervoranmeldung') . qq|</th>
202                 <td><select name=pos_ustva>$form->{selectustva}</select></td>
203                 <input type=hidden name=selectustva value="$form->{selectustva}">
204               </tr>|;
205
206   $form->{selecteur} = "<option>\n";
207   %eur = (1  => "Umsatzerlöse",
208           2  => "sonstige Erlöse",
209           3  => "Privatanteile",
210           4  => "Zinserträge",
211           5  => "Ausserordentliche Erträge",
212           6  => "Vereinnahmte Umsatzst.",
213           7  => "Umsatzsteuererstattungen",
214           8  => "Wareneingänge",
215           9  => "Löhne und Gehälter",
216           10 => "Gesetzl. sozialer Aufw.",
217           11 => "Mieten",
218           12 => "Gas, Strom, Wasser",
219           13 => "Instandhaltung",
220           14 => "Steuern, Versich., Beiträge",
221           15 => "Kfz-Steuern",
222           16 => "Kfz-Versicherungen",
223           17 => "Sonst. Fahrtkosten",
224           18 => "Werbe- und Reisekosten",
225           19 => "Instandhaltung u. Werkzeuge",
226           20 => "Fachzeitschriften, Bücher",
227           21 => "Miete für Einrichtungen",
228           22 => "Rechts- und Beratungskosten",
229           23 => "Bürobedarf, Porto, Telefon",
230           24 => "Sonstige Aufwendungen",
231           25 => "Abschreibungen auf Anlagever.",
232           26 => "Abschreibungen auf GWG",
233           27 => "Vorsteuer",
234           28 => "Umsatzsteuerzahlungen",
235           29 => "Zinsaufwand",
236           30 => "Ausserordentlicher Aufwand",
237           31 => "Betriebliche Steuern");
238   foreach $item (sort({ $a <=> $b } keys(%eur))) {
239     if ($item == $form->{pos_eur}) {
240       $form->{selecteur} .= "<option value=$item selected>$eur{$item}\n";
241     } else {
242       $form->{selecteur} .= "<option value=$item>$eur{$item}\n";
243     }
244
245   }
246
247   $eur = qq|
248               <tr>
249                 <th align=right>| . $locale->text('EUER') . qq|</th>
250                 <td><select name=pos_eur>$form->{selecteur}</select></td>
251                 <input type=hidden name=selecteur value="$form->{selecteur}">
252               </tr>|;
253
254   $form->{selectbwa} = "<option>\n";
255
256   %bwapos = (1  => 'Umsatzerlöse',
257              2  => 'Best.Verdg.FE/UE',
258              3  => 'Aktiv.Eigenleistung',
259              4  => 'Mat./Wareneinkauf',
260              5  => 'So.betr.Erlöse',
261              10 => 'Personalkosten',
262              11 => 'Raumkosten',
263              12 => 'Betriebl.Steuern',
264              13 => 'Vers./Beiträge',
265              14 => 'Kfz.Kosten o.St.',
266              15 => 'Werbe-Reisek.',
267              16 => 'Kosten Warenabgabe',
268              17 => 'Abschreibungen',
269              18 => 'Rep./instandhlt.',
270              19 => 'Übrige Steuern',
271              20 => 'Sonst.Kosten',
272              30 => 'Zinsauwand',
273              31 => 'Sonst.neutr.Aufw.',
274              32 => 'Zinserträge',
275              33 => 'Sonst.neutr.Ertrag',
276              34 => 'Verr.kalk.Kosten',
277              35 => 'Steuern Eink.u.Ertr.');
278   foreach $item (sort({ $a <=> $b } keys %bwapos)) {
279     if ($item == $form->{pos_bwa}) {
280       $form->{selectbwa} .= "<option value=$item selected>$bwapos{$item}\n";
281     } else {
282       $form->{selectbwa} .= "<option value=$item>$bwapos{$item}\n";
283     }
284
285   }
286
287   $bwa = qq|
288               <tr>
289                 <th align=right>| . $locale->text('BWA') . qq|</th>
290                 <td><select name=pos_bwa>$form->{selectbwa}</select></td>
291                 <input type=hidden name=selectbwa value="$form->{selectbwa}">
292               </tr>|;
293
294 # Entfernt bis es ordentlich umgesetzt wird (hli) 30.03.2006
295 #  $form->{selectbilanz} = "<option>\n";
296 #  foreach $item ((1, 2, 3, 4)) {
297 #    if ($item == $form->{pos_bilanz}) {
298 #      $form->{selectbilanz} .= "<option value=$item selected>$item\n";
299 #    } else {
300 #      $form->{selectbilanz} .= "<option value=$item>$item\n";
301 #    }
302 #
303 #  }
304 #
305 #  $bilanz = qq|
306 #             <tr>
307 #               <th align=right>| . $locale->text('Bilanz') . qq|</th>
308 #               <td><select name=pos_bilanz>$form->{selectbilanz}</select></td>
309 #               <input type=hidden name=selectbilanz value="$form->{selectbilanz}">
310 #             </tr>|;
311
312   # this is for our parser only!
313   # type=submit $locale->text('Add Account')
314   # type=submit $locale->text('Edit Account')
315   $form->{type} = "account";
316
317   $form->header;
318
319   print qq|
320 <body>
321
322 <form method=post action=$form->{script}>
323
324 <input type=hidden name=id value=$form->{id}>
325 <input type=hidden name=type value=account>
326 <input type=hidden name=orphaned value=$form->{orphaned}>
327 <input type=hidden name=new_chart_valid value=$form->{new_chart_valid}>
328
329 <input type=hidden name=inventory_accno_id value=$form->{inventory_accno_id}>
330 <input type=hidden name=income_accno_id value=$form->{income_accno_id}>
331 <input type=hidden name=expense_accno_id value=$form->{expense_accno_id}>
332 <input type=hidden name=fxgain_accno_id value=$form->{fxgain_accno_id}>
333 <input type=hidden name=fxloss_accno_id value=$form->{fxloss_accno_id}>
334
335 <table border=0 width=100%>
336   <tr>
337     <th class=listtop>$form->{title}</th>
338   </tr>
339   <tr height="5"></tr>
340   <tr valign=top>
341     <td>
342       <table>
343         <tr>
344           <th align=right>| . $locale->text('Account Number') . qq|</th>
345           <td><input name=accno size=20 value=$form->{accno}></td>
346         </tr>
347         <tr>
348           <th align=right>| . $locale->text('Description') . qq|</th>
349           <td><input name=description size=40 value="$form->{description}"></td>
350         </tr>
351         <tr>
352           <th align=right>| . $locale->text('Account Type') . qq|</th>
353           <td>
354             <table>
355               <tr valign=top>
356                 <td><input name=category type=radio class=radio value=A $checked{A_}>&nbsp;|
357     . $locale->text('Asset') . qq|\n<br>
358                 <input name=category type=radio class=radio value=L $checked{L_}>&nbsp;|
359     . $locale->text('Liability') . qq|\n<br>
360                 <input name=category type=radio class=radio value=Q $checked{Q_}>&nbsp;|
361     . $locale->text('Equity') . qq|\n<br>
362                 <input name=category type=radio class=radio value=I $checked{I_}>&nbsp;|
363     . $locale->text('Revenue') . qq|\n<br>
364                 <input name=category type=radio class=radio value=E $checked{E_}>&nbsp;|
365     . $locale->text('Expense') . qq|</td>
366                 <td width=50>&nbsp;</td>
367                 <td>
368                 <input name=charttype type=radio class=radio value="H" $checked{H}>&nbsp;|
369     . $locale->text('Heading') . qq|<br>
370                 <input name=charttype type=radio class=radio value="A" $checked{A}>&nbsp;|
371     . $locale->text('Account') . qq|</td>
372               </tr>
373             </table>
374           </td>
375         </tr>
376 |;
377
378   if ($form->{charttype} eq "A") {
379     print qq|
380         <tr>
381           <td colspan=2>
382             <table>
383               <tr>
384                 <th align=left>|
385       . $locale->text('Is this a summary account to record') . qq|</th>
386                 <td>
387                 <input name=AR type=checkbox class=checkbox value=AR $form->{AR}>&nbsp;|
388       . $locale->text('AR')
389       . qq|&nbsp;<input name=AP type=checkbox class=checkbox value=AP $form->{AP}>&nbsp;|
390       . $locale->text('AP')
391       . qq|&nbsp;<input name=IC type=checkbox class=checkbox value=IC $form->{IC}>&nbsp;|
392       . $locale->text('Inventory')
393       . qq|</td>
394               </tr>
395             </table>
396           </td>
397         </tr>
398         <tr>
399           <th colspan=2>| . $locale->text('Include in drop-down menus') . qq|</th>
400         </tr>
401         <tr valign=top>
402           <td colspan=2>
403             <table width=100%>
404               <tr>
405                 <th align=left>| . $locale->text('Receivables') . qq|</th>
406                 <th align=left>| . $locale->text('Payables') . qq|</th>
407                 <th align=left>| . $locale->text('Parts Inventory') . qq|</th>
408                 <th align=left>| . $locale->text('Service Items') . qq|</th>
409               </tr>
410               <tr>
411                 <td>
412                 <input name=AR_amount type=checkbox class=checkbox value=AR_amount $form->{AR_amount}>&nbsp;|
413       . $locale->text('Revenue') . qq|\n<br>
414                 <input name=AR_paid type=checkbox class=checkbox value=AR_paid $form->{AR_paid}>&nbsp;|
415       . $locale->text('Receipt') . qq|\n<br>
416                 <input name=AR_tax type=checkbox class=checkbox value=AR_tax $form->{AR_tax}>&nbsp;|
417       . $locale->text('Tax') . qq|
418                 </td>
419                 <td>
420                 <input name=AP_amount type=checkbox class=checkbox value=AP_amount $form->{AP_amount}>&nbsp;|
421       . $locale->text('Expense/Asset') . qq|\n<br>
422                 <input name=AP_paid type=checkbox class=checkbox value=AP_paid $form->{AP_paid}>&nbsp;|
423       . $locale->text('Payment') . qq|\n<br>
424                 <input name=AP_tax type=checkbox class=checkbox value=AP_tax $form->{AP_tax}>&nbsp;|
425       . $locale->text('Tax') . qq|
426                 </td>
427                 <td>
428                 <input name=IC_sale type=checkbox class=checkbox value=IC_sale $form->{IC_sale}>&nbsp;|
429       . $locale->text('Revenue') . qq|\n<br>
430                 <input name=IC_cogs type=checkbox class=checkbox value=IC_cogs $form->{IC_cogs}>&nbsp;|
431       . $locale->text('Expense') . qq|\n<br>
432                 <input name=IC_taxpart type=checkbox class=checkbox value=IC_taxpart $form->{IC_taxpart}>&nbsp;|
433       . $locale->text('Tax') . qq|
434                 </td>
435                 <td>
436                 <input name=IC_income type=checkbox class=checkbox value=IC_income $form->{IC_income}>&nbsp;|
437       . $locale->text('Revenue') . qq|\n<br>
438                 <input name=IC_expense type=checkbox class=checkbox value=IC_expense $form->{IC_expense}>&nbsp;|
439       . $locale->text('Expense') . qq|\n<br>
440                 <input name=IC_taxservice type=checkbox class=checkbox value=IC_taxservice $form->{IC_taxservice}>&nbsp;|
441       . $locale->text('Tax') . qq|
442                 </td>
443               </tr>
444             </table>
445           </td>
446         </tr>
447 |;
448   }
449
450   print qq|
451         $taxkey
452         $ustva
453         $eur
454         $bwa
455         $bilanz
456       </table>
457     </td>
458   </tr>
459   $newaccount
460   <tr>
461     <td><hr size=3 noshade></td>
462   </tr>
463 </table>
464 |;
465
466   $lxdebug->leave_sub();
467 }
468
469 sub form_footer {
470   $lxdebug->enter_sub();
471
472   print qq|
473
474 <input name=callback type=hidden value="$form->{callback}">
475
476 <input type=hidden name=path value=$form->{path}>
477 <input type=hidden name=login value=$form->{login}>
478 <input type=hidden name=password value=$form->{password}>
479
480 <br>|;
481   if ((!$form->{id}) || ($form->{id} && $form->{orphaned}) || (($form->{type} eq "account") && (!$form->{new_chart_valid}))) {
482     print qq|
483 <input type=submit class=submit name=action value="|
484     . $locale->text('Save') . qq|">
485 |;
486 }
487
488   if ($form->{id} && $form->{orphaned}) {
489     print qq|<input type=submit class=submit name=action value="|
490       . $locale->text('Delete') . qq|">|;
491   }
492
493   if ($form->{menubar}) {
494     require "$form->{path}/menu.pl";
495     &menubar;
496   }
497
498   print qq|
499 </form>
500
501 </body>
502 </html>
503 |;
504
505   $lxdebug->leave_sub();
506 }
507
508 sub save_account {
509   $lxdebug->enter_sub();
510
511   $form->isblank("accno",    $locale->text('Account Number missing!'));
512   $form->isblank("category", $locale->text('Account Type missing!'));
513
514   $form->redirect($locale->text('Account saved!'))
515     if (AM->save_account(\%myconfig, \%$form));
516   $form->error($locale->text('Cannot save account!'));
517
518   $lxdebug->leave_sub();
519 }
520
521 sub list_account {
522   $lxdebug->enter_sub();
523
524   CA->all_accounts(\%myconfig, \%$form);
525
526   $form->{title} = $locale->text('Chart of Accounts');
527
528   # construct callback
529   $callback =
530     "$form->{script}?action=list_account&path=$form->{path}&login=$form->{login}&password=$form->{password}";
531
532   @column_index = qw(accno gifi_accno description debit credit link);
533
534   $column_header{accno} = qq|<th>| . $locale->text('Account') . qq|</a></th>|;
535   $column_header{gifi_accno} =
536     qq|<th>| . $locale->text('GIFI') . qq|</a></th>|;
537   $column_header{description} =
538     qq|<th>| . $locale->text('Description') . qq|</a></th>|;
539   $column_header{debit}  = qq|<th>| . $locale->text('Debit') . qq|</a></th>|;
540   $column_header{credit} = qq|<th>| . $locale->text('Credit') . qq|</a></th>|;
541   $column_header{link}   = qq|<th>| . $locale->text('Link') . qq|</a></th>|;
542
543   $form->header;
544   $colspan = $#column_index + 1;
545
546   print qq|
547 <body>
548
549 <table width=100%>
550   <tr>
551     <th class=listtop colspan=$colspan>$form->{title}</th>
552   </tr>
553   <tr height=5></tr>
554   <tr class=listheading>
555 |;
556
557   map { print "$column_header{$_}\n" } @column_index;
558
559   print qq|
560 </tr>
561 |;
562
563   # escape callback
564   $callback = $form->escape($callback);
565
566   foreach $ca (@{ $form->{CA} }) {
567
568     $ca->{debit}  = "&nbsp;";
569     $ca->{credit} = "&nbsp;";
570
571     if ($ca->{amount} > 0) {
572       $ca->{credit} =
573         $form->format_amount(\%myconfig, $ca->{amount}, 2, "&nbsp;");
574     }
575     if ($ca->{amount} < 0) {
576       $ca->{debit} =
577         $form->format_amount(\%myconfig, -$ca->{amount}, 2, "&nbsp;");
578     }
579
580     $ca->{link} =~ s/:/<br>/og;
581
582     if ($ca->{charttype} eq "H") {
583       print qq|<tr class=listheading>|;
584
585       $column_data{accno} =
586         qq|<th><a href=$form->{script}?action=edit_account&id=$ca->{id}&path=$form->{path}&login=$form->{login}&password=$form->{password}&callback=$callback>$ca->{accno}</a></th>|;
587       $column_data{gifi_accno} =
588         qq|<th><a href=$form->{script}?action=edit_gifi&accno=$ca->{gifi_accno}&path=$form->{path}&login=$form->{login}&password=$form->{password}&callback=$callback>$ca->{gifi_accno}</a>&nbsp;</th>|;
589       $column_data{description} = qq|<th>$ca->{description}&nbsp;</th>|;
590       $column_data{debit}       = qq|<th>&nbsp;</th>|;
591       $column_data{credit}      = qq| <th>&nbsp;</th>|;
592       $column_data{link}        = qq|<th>&nbsp;</th>|;
593
594     } else {
595       $i++;
596       $i %= 2;
597       print qq|
598 <tr valign=top class=listrow$i>|;
599       $column_data{accno} =
600         qq|<td><a href=$form->{script}?action=edit_account&id=$ca->{id}&path=$form->{path}&login=$form->{login}&password=$form->{password}&callback=$callback>$ca->{accno}</a></td>|;
601       $column_data{gifi_accno} =
602         qq|<td><a href=$form->{script}?action=edit_gifi&accno=$ca->{gifi_accno}&path=$form->{path}&login=$form->{login}&password=$form->{password}&callback=$callback>$ca->{gifi_accno}</a>&nbsp;</td>|;
603       $column_data{description} = qq|<td>$ca->{description}&nbsp;</td>|;
604       $column_data{debit}       = qq|<td align=right>$ca->{debit}</td>|;
605       $column_data{credit}      = qq|<td align=right>$ca->{credit}</td>|;
606       $column_data{link}        = qq|<td>$ca->{link}&nbsp;</td>|;
607
608     }
609
610     map { print "$column_data{$_}\n" } @column_index;
611
612     print "</tr>\n";
613   }
614
615   print qq|
616   <tr><td colspan=$colspan><hr size=3 noshade></td></tr>
617 </table>
618
619 </body>
620 </html>
621 |;
622
623   $lxdebug->leave_sub();
624 }
625
626 sub delete_account {
627   $lxdebug->enter_sub();
628
629   $form->{title} = $locale->text('Delete Account');
630
631   foreach $id (
632     qw(inventory_accno_id income_accno_id expense_accno_id fxgain_accno_id fxloss_accno_id)
633     ) {
634     if ($form->{id} == $form->{$id}) {
635       $form->error($locale->text('Cannot delete default account!'));
636     }
637   }
638
639   $form->redirect($locale->text('Account deleted!'))
640     if (AM->delete_account(\%myconfig, \%$form));
641   $form->error($locale->text('Cannot delete account!'));
642
643   $lxdebug->leave_sub();
644 }
645
646 sub list_gifi {
647   $lxdebug->enter_sub();
648
649   @{ $form->{fields} } = (accno, description);
650   $form->{table}     = "gifi";
651   $form->{sortorder} = "accno";
652
653   AM->gifi_accounts(\%myconfig, \%$form);
654
655   $form->{title} = $locale->text('GIFI');
656
657   # construct callback
658   $callback =
659     "$form->{script}?action=list_gifi&path=$form->{path}&login=$form->{login}&password=$form->{password}";
660
661   @column_index = qw(accno description);
662
663   $column_header{accno} = qq|<th>| . $locale->text('GIFI') . qq|</a></th>|;
664   $column_header{description} =
665     qq|<th>| . $locale->text('Description') . qq|</a></th>|;
666
667   $form->header;
668   $colspan = $#column_index + 1;
669
670   print qq|
671 <body>
672
673 <table width=100%>
674   <tr>
675     <th class=listtop colspan=$colspan>$form->{title}</th>
676   </tr>
677   <tr height="5"></tr>
678   <tr class=listheading>
679 |;
680
681   map { print "$column_header{$_}\n" } @column_index;
682
683   print qq|
684 </tr>
685 |;
686
687   # escape callback
688   $callback = $form->escape($callback);
689
690   foreach $ca (@{ $form->{ALL} }) {
691
692     $i++;
693     $i %= 2;
694
695     print qq|
696 <tr valign=top class=listrow$i>|;
697
698     $column_data{accno} =
699       qq|<td><a href=$form->{script}?action=edit_gifi&coa=1&accno=$ca->{accno}&path=$form->{path}&login=$form->{login}&password=$form->{password}&callback=$callback>$ca->{accno}</td>|;
700     $column_data{description} = qq|<td>$ca->{description}&nbsp;</td>|;
701
702     map { print "$column_data{$_}\n" } @column_index;
703
704     print "</tr>\n";
705   }
706
707   print qq|
708   <tr>
709     <td colspan=$colspan><hr size=3 noshade></td>
710   </tr>
711 </table>
712
713 </body>
714 </html>
715 |;
716
717   $lxdebug->leave_sub();
718 }
719
720 sub add_gifi {
721   $lxdebug->enter_sub();
722
723   $form->{title} = "Add";
724
725   # construct callback
726   $form->{callback} =
727     "$form->{script}?action=list_gifi&path=$form->{path}&login=$form->{login}&password=$form->{password}";
728
729   $form->{coa} = 1;
730
731   &gifi_header;
732   &gifi_footer;
733
734   $lxdebug->leave_sub();
735 }
736
737 sub edit_gifi {
738   $lxdebug->enter_sub();
739
740   $form->{title} = "Edit";
741
742   AM->get_gifi(\%myconfig, \%$form);
743
744   &gifi_header;
745   &gifi_footer;
746
747   $lxdebug->leave_sub();
748 }
749
750 sub gifi_header {
751   $lxdebug->enter_sub();
752
753   $form->{title} = $locale->text("$form->{title} GIFI");
754
755   # $locale->text('Add GIFI')
756   # $locale->text('Edit GIFI')
757
758   $form->{description} =~ s/\"/&quot;/g;
759
760   $form->header;
761
762   print qq|
763 <body>
764
765 <form method=post action=$form->{script}>
766
767 <input type=hidden name=id value=$form->{accno}>
768 <input type=hidden name=type value=gifi>
769
770 <table width=100%>
771   <tr>
772     <th class=listtop>$form->{title}</th>
773   </tr>
774   <tr height="5"></tr>
775   <tr>
776     <td>
777       <table>
778         <tr>
779           <th align=right>| . $locale->text('GIFI') . qq|</th>
780           <td><input name=accno size=20 value=$form->{accno}></td>
781         </tr>
782         <tr>
783           <th align=right>| . $locale->text('Description') . qq|</th>
784           <td><input name=description size=60 value="$form->{description}"></td>
785         </tr>
786       </table>
787     </td>
788   </tr>
789   <tr>
790     <td colspan=2><hr size=3 noshade></td>
791   </tr>
792 </table>
793 |;
794
795   $lxdebug->leave_sub();
796 }
797
798 sub gifi_footer {
799   $lxdebug->enter_sub();
800
801   print qq|
802
803 <input name=callback type=hidden value="$form->{callback}">
804
805 <input type=hidden name=path value=$form->{path}>
806 <input type=hidden name=login value=$form->{login}>
807 <input type=hidden name=password value=$form->{password}>
808
809 <br><input type=submit class=submit name=action value="|
810     . $locale->text('Save') . qq|">|;
811
812   if ($form->{coa}) {
813     print qq|
814 <input type=submit class=submit name=action value="|
815       . $locale->text('Copy to COA') . qq|">
816 |;
817
818     if ($form->{accno} && $form->{orphaned}) {
819       print qq|<input type=submit class=submit name=action value="|
820         . $locale->text('Delete') . qq|">|;
821     }
822   }
823
824   if ($form->{menubar}) {
825     require "$form->{path}/menu.pl";
826     &menubar;
827   }
828
829   print qq|
830   </form>
831
832 </body>
833 </html>
834 |;
835
836   $lxdebug->leave_sub();
837 }
838
839 sub save_gifi {
840   $lxdebug->enter_sub();
841
842   $form->isblank("accno", $locale->text('GIFI missing!'));
843   AM->save_gifi(\%myconfig, \%$form);
844   $form->redirect($locale->text('GIFI saved!'));
845
846   $lxdebug->leave_sub();
847 }
848
849 sub copy_to_coa {
850   $lxdebug->enter_sub();
851
852   $form->isblank("accno", $locale->text('GIFI missing!'));
853
854   AM->save_gifi(\%myconfig, \%$form);
855
856   delete $form->{id};
857   $form->{gifi_accno} = $form->{accno};
858   $form->{title}      = "Add";
859   $form->{charttype}  = "A";
860
861   &account_header;
862   &form_footer;
863
864   $lxdebug->leave_sub();
865 }
866
867 sub delete_gifi {
868   $lxdebug->enter_sub();
869
870   AM->delete_gifi(\%myconfig, \%$form);
871   $form->redirect($locale->text('GIFI deleted!'));
872
873   $lxdebug->leave_sub();
874 }
875
876 sub add_department {
877   $lxdebug->enter_sub();
878
879   $form->{title} = "Add";
880   $form->{role}  = "P";
881
882   $form->{callback} =
883     "$form->{script}?action=add_department&path=$form->{path}&login=$form->{login}&password=$form->{password}"
884     unless $form->{callback};
885
886   &department_header;
887   &form_footer;
888
889   $lxdebug->leave_sub();
890 }
891
892 sub edit_department {
893   $lxdebug->enter_sub();
894
895   $form->{title} = "Edit";
896
897   AM->get_department(\%myconfig, \%$form);
898
899   &department_header;
900   &form_footer;
901
902   $lxdebug->leave_sub();
903 }
904
905 sub list_department {
906   $lxdebug->enter_sub();
907
908   AM->departments(\%myconfig, \%$form);
909
910   $form->{callback} =
911     "$form->{script}?action=list_department&path=$form->{path}&login=$form->{login}&password=$form->{password}";
912
913   $callback = $form->escape($form->{callback});
914
915   $form->{title} = $locale->text('Departments');
916
917   @column_index = qw(description cost profit);
918
919   $column_header{description} =
920       qq|<th class=listheading width=90%>|
921     . $locale->text('Description')
922     . qq|</th>|;
923   $column_header{cost} =
924       qq|<th class=listheading nowrap>|
925     . $locale->text('Cost Center')
926     . qq|</th>|;
927   $column_header{profit} =
928       qq|<th class=listheading nowrap>|
929     . $locale->text('Profit Center')
930     . qq|</th>|;
931
932   $form->header;
933
934   print qq|
935 <body>
936
937 <table width=100%>
938   <tr>
939     <th class=listtop>$form->{title}</th>
940   </tr>
941   <tr height="5"></tr>
942   <tr>
943     <td>
944       <table width=100%>
945         <tr class=listheading>
946 |;
947
948   map { print "$column_header{$_}\n" } @column_index;
949
950   print qq|
951         </tr>
952 |;
953
954   foreach $ref (@{ $form->{ALL} }) {
955
956     $i++;
957     $i %= 2;
958
959     print qq|
960         <tr valign=top class=listrow$i>
961 |;
962
963     $costcenter   = ($ref->{role} eq "C") ? "X" : "";
964     $profitcenter = ($ref->{role} eq "P") ? "X" : "";
965
966     $column_data{description} =
967       qq|<td><a href=$form->{script}?action=edit_department&id=$ref->{id}&path=$form->{path}&login=$form->{login}&password=$form->{password}&callback=$callback>$ref->{description}</td>|;
968     $column_data{cost}   = qq|<td align=center>$costcenter</td>|;
969     $column_data{profit} = qq|<td align=center>$profitcenter</td>|;
970
971     map { print "$column_data{$_}\n" } @column_index;
972
973     print qq|
974         </tr>
975 |;
976   }
977
978   print qq|
979       </table>
980     </td>
981   </tr>
982   <tr>
983   <td><hr size=3 noshade></td>
984   </tr>
985 </table>
986
987 <br>
988 <form method=post action=$form->{script}>
989
990 <input name=callback type=hidden value="$form->{callback}">
991
992 <input type=hidden name=type value=department>
993
994 <input type=hidden name=path value=$form->{path}>
995 <input type=hidden name=login value=$form->{login}>
996 <input type=hidden name=password value=$form->{password}>
997
998 <input class=submit type=submit name=action value="|
999     . $locale->text('Add') . qq|">|;
1000
1001   if ($form->{menubar}) {
1002     require "$form->{path}/menu.pl";
1003     &menubar;
1004   }
1005
1006   print qq|
1007   </form>
1008
1009   </body>
1010   </html>
1011 |;
1012
1013   $lxdebug->leave_sub();
1014 }
1015
1016 sub department_header {
1017   $lxdebug->enter_sub();
1018
1019   $form->{title} = $locale->text("$form->{title} Department");
1020
1021   # $locale->text('Add Department')
1022   # $locale->text('Edit Department')
1023
1024   $form->{description} =~ s/\"/&quot;/g;
1025
1026   if (($rows = $form->numtextrows($form->{description}, 60)) > 1) {
1027     $description =
1028       qq|<textarea name="description" rows=$rows cols=60 wrap=soft>$form->{description}</textarea>|;
1029   } else {
1030     $description =
1031       qq|<input name=description size=60 value="$form->{description}">|;
1032   }
1033
1034   $costcenter   = "checked" if $form->{role} eq "C";
1035   $profitcenter = "checked" if $form->{role} eq "P";
1036
1037   $form->header;
1038
1039   print qq|
1040 <body>
1041
1042 <form method=post action=$form->{script}>
1043
1044 <input type=hidden name=id value=$form->{id}>
1045 <input type=hidden name=type value=department>
1046
1047 <table width=100%>
1048   <tr>
1049     <th class=listtop colspan=2>$form->{title}</th>
1050   </tr>
1051   <tr height="5"></tr>
1052   <tr>
1053     <th align=right>| . $locale->text('Description') . qq|</th>
1054     <td>$description</td>
1055   </tr>
1056   <tr>
1057     <td></td>
1058     <td><input type=radio style=radio name=role value="C" $costcenter> |
1059     . $locale->text('Cost Center') . qq|
1060         <input type=radio style=radio name=role value="P" $profitcenter> |
1061     . $locale->text('Profit Center') . qq|
1062     </td>
1063   <tr>
1064     <td colspan=2><hr size=3 noshade></td>
1065   </tr>
1066 </table>
1067 |;
1068
1069   $lxdebug->leave_sub();
1070 }
1071
1072 sub save_department {
1073   $lxdebug->enter_sub();
1074
1075   $form->isblank("description", $locale->text('Description missing!'));
1076   AM->save_department(\%myconfig, \%$form);
1077   $form->redirect($locale->text('Department saved!'));
1078
1079   $lxdebug->leave_sub();
1080 }
1081
1082 sub delete_department {
1083   $lxdebug->enter_sub();
1084
1085   AM->delete_department(\%myconfig, \%$form);
1086   $form->redirect($locale->text('Department deleted!'));
1087
1088   $lxdebug->leave_sub();
1089 }
1090
1091 sub add_lead {
1092   $lxdebug->enter_sub();
1093
1094   $form->{title} = "Add";
1095
1096   $form->{callback} =
1097     "$form->{script}?action=add_lead&path=$form->{path}&login=$form->{login}&password=$form->{password}"
1098     unless $form->{callback};
1099
1100   &lead_header;
1101   &form_footer;
1102
1103   $lxdebug->leave_sub();
1104 }
1105
1106 sub edit_lead {
1107   $lxdebug->enter_sub();
1108
1109   $form->{title} = "Edit";
1110
1111   AM->get_lead(\%myconfig, \%$form);
1112
1113   &lead_header;
1114
1115   $form->{orphaned} = 1;
1116   &form_footer;
1117
1118   $lxdebug->leave_sub();
1119 }
1120
1121 sub list_lead {
1122   $lxdebug->enter_sub();
1123
1124   AM->lead(\%myconfig, \%$form);
1125
1126   $form->{callback} =
1127     "$form->{script}?action=list_lead&path=$form->{path}&login=$form->{login}&password=$form->{password}";
1128
1129   $callback = $form->escape($form->{callback});
1130
1131   $form->{title} = $locale->text('Lead');
1132
1133   @column_index = qw(description cost profit);
1134
1135   $column_header{description} =
1136       qq|<th class=listheading width=100%>|
1137     . $locale->text('Description')
1138     . qq|</th>|;
1139
1140   $form->header;
1141
1142   print qq|
1143 <body>
1144
1145 <table width=100%>
1146   <tr>
1147     <th class=listtop>$form->{title}</th>
1148   </tr>
1149   <tr height="5"></tr>
1150   <tr class=listheading>
1151 |;
1152
1153   map { print "$column_header{$_}\n" } @column_index;
1154
1155   print qq|
1156         </tr>
1157 |;
1158
1159   foreach $ref (@{ $form->{ALL} }) {
1160
1161     $i++;
1162     $i %= 2;
1163
1164     print qq|
1165         <tr valign=top class=listrow$i>
1166 |;
1167
1168         $lead = $ref->{lead};
1169         
1170     $column_data{description} =
1171       qq|<td><a href=$form->{script}?action=edit_lead&id=$ref->{id}&path=$form->{path}&login=$form->{login}&password=$form->{password}&callback=$callback>$ref->{lead}</td>|;
1172
1173     map { print "$column_data{$_}\n" } @column_index;
1174
1175     print qq|
1176         </tr>
1177 |;
1178   }
1179
1180   print qq|
1181   <tr>
1182   <td><hr size=3 noshade></td>
1183   </tr>
1184 </table>
1185
1186 <br>
1187 <form method=post action=$form->{script}>
1188
1189 <input name=callback type=hidden value="$form->{callback}">
1190
1191 <input type=hidden name=type value=lead>
1192
1193 <input type=hidden name=path value=$form->{path}>
1194 <input type=hidden name=login value=$form->{login}>
1195 <input type=hidden name=password value=$form->{password}>
1196
1197 <input class=submit type=submit name=action value="|
1198     . $locale->text('Add') . qq|">|;
1199
1200   if ($form->{menubar}) {
1201     require "$form->{path}/menu.pl";
1202     &menubar;
1203   }
1204
1205   print qq|
1206   </form>
1207
1208   </body>
1209   </html>
1210 |;
1211
1212   $lxdebug->leave_sub();
1213 }
1214
1215 sub lead_header {
1216   $lxdebug->enter_sub();
1217
1218   $form->{title} = $locale->text("$form->{title} Lead");
1219
1220   # $locale->text('Add Lead')
1221   # $locale->text('Edit Lead')
1222
1223   $form->{description} =~ s/\"/&quot;/g;
1224
1225   $description =
1226       qq|<input name=description size=50 value="$form->{lead}">|;
1227
1228   $form->header;
1229
1230   print qq|
1231 <body>
1232
1233 <form method=post action=$form->{script}>
1234
1235 <input type=hidden name=id value=$form->{id}>
1236 <input type=hidden name=type value=lead>
1237
1238 <table width=100%>
1239   <tr>
1240     <th class=listtop colspan=2>$form->{title}</th>
1241   </tr>
1242   <tr height="5"></tr>
1243   <tr>
1244     <th align=right>| . $locale->text('Description') . qq|</th>
1245     <td>$description</td>
1246   </tr>
1247     <td colspan=2><hr size=3 noshade></td>
1248   </tr>
1249 </table>
1250 |;
1251
1252   $lxdebug->leave_sub();
1253 }
1254
1255 sub save_lead {
1256   $lxdebug->enter_sub();
1257
1258   $form->isblank("description", $locale->text('Description missing!'));
1259   AM->save_lead(\%myconfig, \%$form);
1260   $form->redirect($locale->text('lead saved!'));
1261
1262   $lxdebug->leave_sub();
1263 }
1264
1265 sub delete_lead {
1266   $lxdebug->enter_sub();
1267
1268   AM->delete_lead(\%myconfig, \%$form);
1269   $form->redirect($locale->text('lead deleted!'));
1270
1271   $lxdebug->leave_sub();
1272 }
1273
1274 sub add_business {
1275   $lxdebug->enter_sub();
1276
1277   $form->{title} = "Add";
1278
1279   $form->{callback} =
1280     "$form->{script}?action=add_business&path=$form->{path}&login=$form->{login}&password=$form->{password}"
1281     unless $form->{callback};
1282
1283   &business_header;
1284   &form_footer;
1285
1286   $lxdebug->leave_sub();
1287 }
1288
1289 sub edit_business {
1290   $lxdebug->enter_sub();
1291
1292   $form->{title} = "Edit";
1293
1294   AM->get_business(\%myconfig, \%$form);
1295
1296   &business_header;
1297
1298   $form->{orphaned} = 1;
1299   &form_footer;
1300
1301   $lxdebug->leave_sub();
1302 }
1303
1304 sub list_business {
1305   $lxdebug->enter_sub();
1306
1307   AM->business(\%myconfig, \%$form);
1308
1309   $form->{callback} =
1310     "$form->{script}?action=list_business&path=$form->{path}&login=$form->{login}&password=$form->{password}";
1311
1312   $callback = $form->escape($form->{callback});
1313
1314   $form->{title} = $locale->text('Type of Business');
1315
1316   @column_index = qw(description discount customernumberinit);
1317
1318   $column_header{description} =
1319       qq|<th class=listheading width=60%>|
1320     . $locale->text('Description')
1321     . qq|</th>|;
1322   $column_header{discount} =
1323       qq|<th class=listheading width=10%>|
1324     . $locale->text('Discount')
1325     . qq| %</th>|;
1326   $column_header{customernumberinit} =
1327       qq|<th class=listheading>|
1328     . $locale->text('Customernumberinit')
1329     . qq|</th>|;
1330
1331   $form->header;
1332
1333   print qq|
1334 <body>
1335
1336 <table width=100%>
1337   <tr>
1338     <th class=listtop>$form->{title}</th>
1339   </tr>
1340   <tr height="5"></tr>
1341   <tr>
1342     <td>
1343       <table width=100%>
1344         <tr class=listheading>
1345 |;
1346
1347   map { print "$column_header{$_}\n" } @column_index;
1348
1349   print qq|
1350         </tr>
1351 |;
1352
1353   foreach $ref (@{ $form->{ALL} }) {
1354
1355     $i++;
1356     $i %= 2;
1357
1358     print qq|
1359         <tr valign=top class=listrow$i>
1360 |;
1361
1362     $discount =
1363       $form->format_amount(\%myconfig, $ref->{discount} * 100, 1, "&nbsp");
1364     $description =
1365       ($ref->{salesman})
1366       ? "<b>$ref->{description}</b>"
1367       : "$ref->{description}";
1368     $column_data{description} =
1369       qq|<td><a href=$form->{script}?action=edit_business&id=$ref->{id}&path=$form->{path}&login=$form->{login}&password=$form->{password}&callback=$callback>$description</td>|;
1370     $column_data{discount}           = qq|<td align=right>$discount</td>|;
1371     $column_data{customernumberinit} =
1372       qq|<td align=right>$ref->{customernumberinit}</td>|;
1373
1374     map { print "$column_data{$_}\n" } @column_index;
1375
1376     print qq|
1377         </tr>
1378 |;
1379   }
1380
1381   print qq|
1382       </table>
1383     </td>
1384   </tr>
1385   <tr>
1386   <td><hr size=3 noshade></td>
1387   </tr>
1388 </table>
1389
1390 <br>
1391 <form method=post action=$form->{script}>
1392
1393 <input name=callback type=hidden value="$form->{callback}">
1394
1395 <input type=hidden name=type value=business>
1396
1397 <input type=hidden name=path value=$form->{path}>
1398 <input type=hidden name=login value=$form->{login}>
1399 <input type=hidden name=password value=$form->{password}>
1400
1401 <input class=submit type=submit name=action value="|
1402     . $locale->text('Add') . qq|">|;
1403
1404   if ($form->{menubar}) {
1405     require "$form->{path}/menu.pl";
1406     &menubar;
1407   }
1408
1409   print qq|
1410
1411   </form>
1412
1413   </body>
1414   </html>
1415 |;
1416
1417   $lxdebug->leave_sub();
1418 }
1419
1420 sub business_header {
1421   $lxdebug->enter_sub();
1422
1423   $form->{title}    = $locale->text("$form->{title} Business");
1424   $form->{salesman} = "checked" if $form->{salesman};
1425
1426   # $locale->text('Add Business')
1427   # $locale->text('Edit Business')
1428
1429   $form->{description} =~ s/\"/&quot;/g;
1430   $form->{discount} =
1431     $form->format_amount(\%myconfig, $form->{discount} * 100);
1432
1433   $form->header;
1434
1435   print qq|
1436 <body>
1437
1438 <form method=post action=$form->{script}>
1439
1440 <input type=hidden name=id value=$form->{id}>
1441 <input type=hidden name=type value=business>
1442
1443 <table width=100%>
1444   <tr>
1445     <th class=listtop colspan=2>$form->{title}</th>
1446   </tr>
1447   <tr height="5"></tr>
1448   <tr>
1449     <th align=right>| . $locale->text('Type of Business') . qq|</th>
1450     <td><input name=description size=30 value="$form->{description}"></td>
1451   <tr>
1452   <tr>
1453     <th align=right>| . $locale->text('Discount') . qq| %</th>
1454     <td><input name=discount size=5 value=$form->{discount}></td>
1455   </tr>
1456   <tr>
1457     <th align=right>| . $locale->text('Customernumberinit') . qq|</th>
1458     <td><input name=customernumberinit size=10 value=$form->{customernumberinit}></td>
1459   </tr>
1460   <tr>
1461     <td align=right>| . $locale->text('Salesman') . qq|</td>
1462     <td><input name=salesman class=checkbox type=checkbox value=1 $form->{salesman}></td>
1463   </tr>
1464   <td colspan=2><hr size=3 noshade></td>
1465   </tr>
1466 </table>
1467 |;
1468
1469   $lxdebug->leave_sub();
1470 }
1471
1472 sub save_business {
1473   $lxdebug->enter_sub();
1474
1475   $form->isblank("description", $locale->text('Description missing!'));
1476   AM->save_business(\%myconfig, \%$form);
1477   $form->redirect($locale->text('Business saved!'));
1478
1479   $lxdebug->leave_sub();
1480 }
1481
1482 sub delete_business {
1483   $lxdebug->enter_sub();
1484
1485   AM->delete_business(\%myconfig, \%$form);
1486   $form->redirect($locale->text('Business deleted!'));
1487
1488   $lxdebug->leave_sub();
1489 }
1490
1491 sub add_language {
1492   $lxdebug->enter_sub();
1493
1494   $form->{title} = "Add";
1495
1496   $form->{callback} =
1497     "$form->{script}?action=add_language&path=$form->{path}&login=$form->{login}&password=$form->{password}"
1498     unless $form->{callback};
1499
1500   &language_header;
1501   &form_footer;
1502
1503   $lxdebug->leave_sub();
1504 }
1505
1506 sub edit_language {
1507   $lxdebug->enter_sub();
1508
1509   $form->{title} = "Edit";
1510
1511   AM->get_language(\%myconfig, \%$form);
1512
1513   &language_header;
1514
1515   $form->{orphaned} = 1;
1516   &form_footer;
1517
1518   $lxdebug->leave_sub();
1519 }
1520
1521 sub list_language {
1522   $lxdebug->enter_sub();
1523
1524   AM->language(\%myconfig, \%$form);
1525
1526   $form->{callback} =
1527     "$form->{script}?action=list_language&path=$form->{path}&login=$form->{login}&password=$form->{password}";
1528
1529   $callback = $form->escape($form->{callback});
1530
1531   $form->{title} = $locale->text('Languages');
1532
1533   @column_index = qw(description template_code article_code);
1534
1535   $column_header{description} =
1536       qq|<th class=listheading width=60%>|
1537     . $locale->text('Description')
1538     . qq|</th>|;
1539   $column_header{template_code} =
1540       qq|<th class=listheading width=10%>|
1541     . $locale->text('Template Code')
1542     . qq|</th>|;
1543   $column_header{article_code} =
1544       qq|<th class=listheading>|
1545     . $locale->text('Article Code')
1546     . qq|</th>|;
1547
1548   $form->header;
1549
1550   print qq|
1551 <body>
1552
1553 <table width=100%>
1554   <tr>
1555     <th class=listtop>$form->{title}</th>
1556   </tr>
1557   <tr height="5"></tr>
1558   <tr>
1559     <td>
1560       <table width=100%>
1561         <tr class=listheading>
1562 |;
1563
1564   map { print "$column_header{$_}\n" } @column_index;
1565
1566   print qq|
1567         </tr>
1568 |;
1569
1570   foreach $ref (@{ $form->{ALL} }) {
1571
1572     $i++;
1573     $i %= 2;
1574
1575     print qq|
1576         <tr valign=top class=listrow$i>
1577 |;
1578
1579
1580     $column_data{description} =
1581       qq|<td><a href=$form->{script}?action=edit_language&id=$ref->{id}&path=$form->{path}&login=$form->{login}&password=$form->{password}&callback=$callback>$ref->{description}</td>|;
1582     $column_data{template_code}           = qq|<td align=right>$ref->{template_code}</td>|;
1583     $column_data{article_code} =
1584       qq|<td align=right>$ref->{article_code}</td>|;
1585
1586     map { print "$column_data{$_}\n" } @column_index;
1587
1588     print qq|
1589         </tr>
1590 |;
1591   }
1592
1593   print qq|
1594       </table>
1595     </td>
1596   </tr>
1597   <tr>
1598   <td><hr size=3 noshade></td>
1599   </tr>
1600 </table>
1601
1602 <br>
1603 <form method=post action=$form->{script}>
1604
1605 <input name=callback type=hidden value="$form->{callback}">
1606
1607 <input type=hidden name=type value=language>
1608
1609 <input type=hidden name=path value=$form->{path}>
1610 <input type=hidden name=login value=$form->{login}>
1611 <input type=hidden name=password value=$form->{password}>
1612
1613 <input class=submit type=submit name=action value="|
1614     . $locale->text('Add') . qq|">|;
1615
1616   if ($form->{menubar}) {
1617     require "$form->{path}/menu.pl";
1618     &menubar;
1619   }
1620
1621   print qq|
1622
1623   </form>
1624
1625   </body>
1626   </html>
1627 |;
1628
1629   $lxdebug->leave_sub();
1630 }
1631
1632 sub language_header {
1633   $lxdebug->enter_sub();
1634
1635   $form->{title}    = $locale->text("$form->{title} Language");
1636
1637   # $locale->text('Add Language')
1638   # $locale->text('Edit Language')
1639
1640   $form->{description} =~ s/\"/&quot;/g;
1641   $form->{template_code} =~ s/\"/&quot;/g;
1642   $form->{article_code} =~ s/\"/&quot;/g;
1643
1644
1645   $form->header;
1646
1647   print qq|
1648 <body>
1649
1650 <form method=post action=$form->{script}>
1651
1652 <input type=hidden name=id value=$form->{id}>
1653 <input type=hidden name=type value=language>
1654
1655 <table width=100%>
1656   <tr>
1657     <th class=listtop colspan=2>$form->{title}</th>
1658   </tr>
1659   <tr height="5"></tr>
1660   <tr>
1661     <th align=right>| . $locale->text('Language') . qq|</th>
1662     <td><input name=description size=30 value="$form->{description}"></td>
1663   <tr>
1664   <tr>
1665     <th align=right>| . $locale->text('Template Code') . qq|</th>
1666     <td><input name=template_code size=5 value=$form->{template_code}></td>
1667   </tr>
1668   <tr>
1669     <th align=right>| . $locale->text('Article Code') . qq|</th>
1670     <td><input name=article_code size=10 value=$form->{article_code}></td>
1671   </tr>
1672   <td colspan=2><hr size=3 noshade></td>
1673   </tr>
1674 </table>
1675 |;
1676
1677   $lxdebug->leave_sub();
1678 }
1679
1680 sub save_language {
1681   $lxdebug->enter_sub();
1682
1683   $form->isblank("description", $locale->text('Language missing!'));
1684   $form->isblank("template_code", $locale->text('Template Code missing!'));
1685   $form->isblank("article_code", $locale->text('Article Code missing!'));
1686   AM->save_language(\%myconfig, \%$form);
1687   $form->redirect($locale->text('Language saved!'));
1688
1689   $lxdebug->leave_sub();
1690 }
1691
1692 sub delete_language {
1693   $lxdebug->enter_sub();
1694
1695   AM->delete_language(\%myconfig, \%$form);
1696   $form->redirect($locale->text('Language deleted!'));
1697
1698   $lxdebug->leave_sub();
1699 }
1700
1701
1702 sub add_buchungsgruppe {
1703   $lxdebug->enter_sub();
1704
1705   # $locale->text("Add Buchungsgruppe")
1706   # $locale->text("Edit Buchungsgruppe")
1707   $form->{title} = "Add";
1708
1709   $form->{callback} =
1710     "$form->{script}?action=add_buchungsgruppe&path=$form->{path}&login=$form->{login}&password=$form->{password}"
1711     unless $form->{callback};
1712   AM->get_buchungsgruppe(\%myconfig, \%$form);
1713
1714   &buchungsgruppe_header;
1715   &form_footer;
1716
1717   $lxdebug->leave_sub();
1718 }
1719
1720 sub edit_buchungsgruppe {
1721   $lxdebug->enter_sub();
1722
1723   $form->{title} = "Edit";
1724
1725   AM->get_buchungsgruppe(\%myconfig, \%$form);
1726
1727   &buchungsgruppe_header;
1728
1729   &form_footer;
1730
1731   $lxdebug->leave_sub();
1732 }
1733
1734 sub list_buchungsgruppe {
1735   $lxdebug->enter_sub();
1736
1737   AM->buchungsgruppe(\%myconfig, \%$form);
1738
1739   $form->{callback} =
1740     "$form->{script}?action=list_buchungsgruppe&path=$form->{path}&login=$form->{login}&password=$form->{password}";
1741
1742   $callback = $form->escape($form->{callback});
1743
1744   $form->{title} = $locale->text('Buchungsgruppen');
1745
1746   @column_index = qw(description inventory_accno income_accno_0 expense_accno_0 income_accno_1 expense_accno_1 income_accno_2 expense_accno_2 income_accno_3 expense_accno_3 );
1747
1748   $column_header{description} =
1749       qq|<th class=listheading width=60%>|
1750     . $locale->text('Description')
1751     . qq|</th>|;
1752   $column_header{inventory_accno} =
1753       qq|<th class=listheading width=10%>|
1754     . $locale->text('Bestandskonto')
1755     . qq|</th>|;
1756   $column_header{income_accno_0} =
1757       qq|<th class=listheading>|
1758     . $locale->text('Erlöse Inland')
1759     . qq|</th>|;
1760   $column_header{expense_accno_0} =
1761       qq|<th class=listheading>|
1762     . $locale->text('Aufwand Inland')
1763     . qq|</th>|;
1764   $column_header{income_accno_1} =
1765       qq|<th class=listheading>|
1766     . $locale->text('Erlöse EU o. UStId')
1767     . qq|</th>|;
1768   $column_header{expense_accno_1} =
1769       qq|<th class=listheading>|
1770     . $locale->text('Aufwand EU o. UStId')
1771     . qq|</th>|;
1772   $column_header{income_accno_2} =
1773       qq|<th class=listheading>|
1774     . $locale->text('Erlöse EU m. UStId')
1775     . qq|</th>|;
1776   $column_header{expense_accno_2} =
1777       qq|<th class=listheading>|
1778     . $locale->text('Aufwand EU m. UStId')
1779     . qq|</th>|;
1780   $column_header{income_accno_3} =
1781       qq|<th class=listheading>|
1782     . $locale->text('Erlöse Ausland')
1783     . qq|</th>|;
1784   $column_header{expense_accno_3} =
1785       qq|<th class=listheading>|
1786     . $locale->text('Aufwand Ausland')
1787     . qq|</th>|;
1788   $form->header;
1789
1790   print qq|
1791 <body>
1792
1793 <table width=100%>
1794   <tr>
1795     <th class=listtop>$form->{title}</th>
1796   </tr>
1797   <tr height="5"></tr>
1798   <tr>
1799     <td>
1800       <table width=100%>
1801         <tr class=listheading>
1802 |;
1803
1804   map { print "$column_header{$_}\n" } @column_index;
1805
1806   print qq|
1807         </tr>
1808 |;
1809
1810   foreach $ref (@{ $form->{ALL} }) {
1811
1812     $i++;
1813     $i %= 2;
1814
1815     print qq|
1816         <tr valign=top class=listrow$i>
1817 |;
1818
1819
1820     $column_data{description} =
1821       qq|<td><a href=$form->{script}?action=edit_buchungsgruppe&id=$ref->{id}&path=$form->{path}&login=$form->{login}&password=$form->{password}&callback=$callback>$ref->{description}</td>|;
1822     $column_data{inventory_accno}           = qq|<td align=right>$ref->{inventory_accno}</td>|;
1823     $column_data{income_accno_0} =
1824       qq|<td align=right>$ref->{income_accno_0}</td>|;
1825     $column_data{expense_accno_0}           = qq|<td align=right>$ref->{expense_accno_0}</td>|;
1826     $column_data{income_accno_1} =
1827       qq|<td align=right>$ref->{income_accno_1}</td>|;
1828     $column_data{expense_accno_1}           = qq|<td align=right>$ref->{expense_accno_1}</td>|;
1829     $column_data{income_accno_2} =
1830       qq|<td align=right>$ref->{income_accno_2}</td>|;
1831     $column_data{expense_accno_2}           = qq|<td align=right>$ref->{expense_accno_2}</td>|;
1832     $column_data{income_accno_3} =
1833       qq|<td align=right>$ref->{income_accno_3}</td>|;
1834     $column_data{expense_accno_3}           = qq|<td align=right>$ref->{expense_accno_3}</td>|;
1835
1836     map { print "$column_data{$_}\n" } @column_index;
1837
1838     print qq|
1839         </tr>
1840 |;
1841   }
1842
1843   print qq|
1844       </table>
1845     </td>
1846   </tr>
1847   <tr>
1848   <td><hr size=3 noshade></td>
1849   </tr>
1850 </table>
1851
1852 <br>
1853 <form method=post action=$form->{script}>
1854
1855 <input name=callback type=hidden value="$form->{callback}">
1856
1857 <input type=hidden name=type value=buchungsgruppe>
1858
1859 <input type=hidden name=path value=$form->{path}>
1860 <input type=hidden name=login value=$form->{login}>
1861 <input type=hidden name=password value=$form->{password}>
1862
1863 <input class=submit type=submit name=action value="|
1864     . $locale->text('Add') . qq|">|;
1865
1866   if ($form->{menubar}) {
1867     require "$form->{path}/menu.pl";
1868     &menubar;
1869   }
1870
1871   print qq|
1872
1873   </form>
1874
1875   </body>
1876   </html>
1877 |;
1878
1879   $lxdebug->leave_sub();
1880 }
1881
1882 sub buchungsgruppe_header {
1883   $lxdebug->enter_sub();
1884
1885   $form->{title}    = $locale->text("$form->{title} Buchungsgruppe");
1886
1887   # $locale->text('Buchungsgruppe hinzufügen')
1888   # $locale->text('Buchungsgruppe bearbeiten')
1889
1890   $form->{description} =~ s/\"/&quot;/g;
1891
1892   # build the popup menus
1893   $form->{taxaccounts} = "";
1894   foreach $key (keys %{ $form->{IC_links} }) {
1895     foreach $ref (@{ $form->{IC_links}{$key} }) {
1896
1897       # if this is a tax field
1898       if ($key =~ /IC_tax/) {
1899         if ($key =~ /$item/) {
1900           $form->{taxaccounts} .= "$ref->{accno} ";
1901           $form->{"IC_tax_$ref->{accno}_description"} =
1902             "$ref->{accno}--$ref->{description}";
1903
1904           if ($form->{id}) {
1905             if ($form->{amount}{ $ref->{accno} }) {
1906               $form->{"IC_tax_$ref->{accno}"} = "checked";
1907             }
1908           } else {
1909             $form->{"IC_tax_$ref->{accno}"} = "checked";
1910           }
1911         }
1912       } else {
1913
1914         $form->{"select$key"} .=
1915           "<option value=$ref->{id} $ref->{selected}>$ref->{accno}--$ref->{description}\n";
1916         if (($key eq "IC") && ($ref->{selected} eq "selected")) {
1917           $form->{IC_default} = $ref->{id};
1918         }
1919         if ($form->{amount}{$key} eq $ref->{accno}) {
1920           $form->{$key} = "$ref->{accno}--$ref->{description}";
1921         }
1922
1923       }
1924     }
1925   }
1926   $form->{selectIC_income}  = $form->{selectIC_sale};
1927   $form->{selectIC_expense} = $form->{selectIC_cogs};
1928   $form->{IC_income}        = $form->{IC_sale};
1929   $form->{IC_expense}       = $form->{IC_cogs};
1930
1931   if ($form->{id}) {
1932     $form->{selectIC} =~ s/selected//g;
1933     $form->{selectIC} =~ s/ value=\$form->{inventory_accno_id}/  value=\$form->{inventory_accno_id} selected/;
1934     $form->{selectIC_income} =~ s/selected//g;
1935     $form->{selectIC_income} =~ s/ value=$form->{income_accno_id_0}/  value=$form->{income_accno_id_0} selected/;
1936     $form->{selectIC_expense} =~ s/selected//g;
1937     $form->{selectIC_expense} =~ s/ value=$form->{expense_accno_id_0}/  value=$form->{expense_accno_id_0} selected/;
1938   }
1939
1940   if (!$eur) {
1941     $linkaccounts = qq|
1942                <tr>
1943                 <th align=right>| . $locale->text('Inventory') . qq|</th>
1944                 <td><select name=inventory_accno_id>$form->{selectIC}</select></td>
1945                 <input name=selectIC type=hidden value="$form->{selectIC}">
1946               </tr>|;
1947   } else {
1948     $linkaccounts = qq|
1949                 <input type=hidden name=inventory_accno_id value=1>|;
1950   }   
1951
1952
1953     $linkaccounts .= qq|
1954               <tr>
1955                 <th align=right>| . $locale->text('Erlöse Inland') . qq|</th>
1956                 <td><select name=income_accno_id_0>$form->{selectIC_income}</select></td>
1957               </tr>
1958               <tr>
1959                 <th align=right>| . $locale->text('Aufwand Inland') . qq|</th>
1960                 <td><select name=expense_accno_id_0>$form->{selectIC_expense}</select></td>
1961               </tr>|;
1962   if ($form->{id}) {
1963     $form->{selectIC_income} =~ s/selected//g;
1964     $form->{selectIC_income} =~ s/ value=$form->{income_accno_id_1}/  value=$form->{income_accno_id_1} selected/;
1965     $form->{selectIC_expense} =~ s/selected//g;
1966     $form->{selectIC_expense} =~ s/ value=$form->{expense_accno_id_1}/  value=$form->{expense_accno_id_1} selected/;
1967   }
1968   $linkaccounts .= qq|        <tr>
1969                 <th align=right>| . $locale->text('Erlöse EU m. UStId') . qq|</th>
1970                 <td><select name=income_accno_id_1>$form->{selectIC_income}</select></td>
1971               </tr>
1972               <tr>
1973                 <th align=right>| . $locale->text('Aufwand EU m UStId') . qq|</th>
1974                 <td><select name=expense_accno_id_1>$form->{selectIC_expense}</select></td>
1975               </tr>|;
1976
1977   if ($form->{id}) {
1978     $form->{selectIC_income} =~ s/selected//g;
1979     $form->{selectIC_income} =~ s/ value=$form->{income_accno_id_2}/  value=$form->{income_accno_id_2} selected/;
1980     $form->{selectIC_expense} =~ s/selected//g;
1981     $form->{selectIC_expense} =~ s/ value=$form->{expense_accno_id_2}/  value=$form->{expense_accno_id_2} selected/;
1982   }
1983
1984   $linkaccounts .= qq|        <tr>
1985                 <th align=right>| . $locale->text('Erlöse EU o. UStId') . qq|</th>
1986                 <td><select name=income_accno_id_2>$form->{selectIC_income}</select></td>
1987               </tr>
1988               <tr>
1989                 <th align=right>| . $locale->text('Aufwand EU o. UStId') . qq|</th>
1990                 <td><select name=expense_accno_id_2>$form->{selectIC_expense}</select></td>
1991               </tr>|;
1992
1993   if ($form->{id}) {
1994     $form->{selectIC_income} =~ s/selected//g;
1995     $form->{selectIC_income} =~ s/ value=$form->{income_accno_id_3}/  value=$form->{income_accno_id_3} selected/;
1996     $form->{selectIC_expense} =~ s/selected//g;
1997     $form->{selectIC_expense} =~ s/ value=$form->{expense_accno_id_3}/  value=$form->{expense_accno_id_3} selected/;
1998   }
1999
2000   $linkaccounts .= qq|        <tr>
2001                 <th align=right>| . $locale->text('Erlöse Ausland') . qq|</th>
2002                 <td><select name=income_accno_id_3>$form->{selectIC_income}</select></td>
2003               </tr>
2004               <tr>
2005                 <th align=right>| . $locale->text('Aufwand Ausland') . qq|</th>
2006                 <td><select name=expense_accno_id_3>$form->{selectIC_expense}</select></td>
2007               </tr>
2008 |;
2009
2010
2011   $form->header;
2012
2013   print qq|
2014 <body>
2015
2016 <form method=post action=$form->{script}>
2017
2018 <input type=hidden name=id value=$form->{id}>
2019 <input type=hidden name=type value=buchungsgruppe>
2020
2021 <table width=100%>
2022   <tr>
2023     <th class=listtop colspan=2>$form->{title}</th>
2024   </tr>
2025   <tr height="5"></tr>
2026   <tr>
2027     <th align=right>| . $locale->text('Buchungsgruppe') . qq|</th>
2028     <td><input name=description size=30 value="$form->{description}"></td>
2029   <tr>
2030   $linkaccounts
2031   <td colspan=2><hr size=3 noshade></td>
2032   </tr>
2033 </table>
2034 |;
2035
2036   $lxdebug->leave_sub();
2037 }
2038
2039 sub save_buchungsgruppe {
2040   $lxdebug->enter_sub();
2041
2042   $form->isblank("description", $locale->text('Description missing!'));
2043
2044   AM->save_buchungsgruppe(\%myconfig, \%$form);
2045   $form->redirect($locale->text('Buchungsgruppe gespeichert!'));
2046
2047   $lxdebug->leave_sub();
2048 }
2049
2050 sub delete_buchungsgruppe {
2051   $lxdebug->enter_sub();
2052
2053   AM->delete_buchungsgruppe(\%myconfig, \%$form);
2054   $form->redirect($locale->text('Buchungsgruppe gelöscht!'));
2055
2056   $lxdebug->leave_sub();
2057 }
2058
2059
2060 sub add_printer {
2061   $lxdebug->enter_sub();
2062
2063   $form->{title} = "Add";
2064
2065   $form->{callback} =
2066     "$form->{script}?action=add_printer&path=$form->{path}&login=$form->{login}&password=$form->{password}"
2067     unless $form->{callback};
2068
2069   &printer_header;
2070   &form_footer;
2071
2072   $lxdebug->leave_sub();
2073 }
2074
2075 sub edit_printer {
2076   $lxdebug->enter_sub();
2077
2078   $form->{title} = "Edit";
2079
2080   AM->get_printer(\%myconfig, \%$form);
2081
2082   &printer_header;
2083
2084   $form->{orphaned} = 1;
2085   &form_footer;
2086
2087   $lxdebug->leave_sub();
2088 }
2089
2090 sub list_printer {
2091   $lxdebug->enter_sub();
2092
2093   AM->printer(\%myconfig, \%$form);
2094
2095   $form->{callback} =
2096     "$form->{script}?action=list_printer&path=$form->{path}&login=$form->{login}&password=$form->{password}";
2097
2098   $callback = $form->escape($form->{callback});
2099
2100   $form->{title} = $locale->text('Printer');
2101
2102   @column_index = qw(printer_description printer_command template_code);
2103
2104   $column_header{printer_description} =
2105       qq|<th class=listheading width=60%>|
2106     . $locale->text('Printer Description')
2107     . qq|</th>|;
2108   $column_header{printer_command} =
2109       qq|<th class=listheading width=10%>|
2110     . $locale->text('Printer Command')
2111     . qq|</th>|;
2112   $column_header{template_code} =
2113       qq|<th class=listheading>|
2114     . $locale->text('Template Code')
2115     . qq|</th>|;
2116
2117   $form->header;
2118
2119   print qq|
2120 <body>
2121
2122 <table width=100%>
2123   <tr>
2124     <th class=listtop>$form->{title}</th>
2125   </tr>
2126   <tr height="5"></tr>
2127   <tr>
2128     <td>
2129       <table width=100%>
2130         <tr class=listheading>
2131 |;
2132
2133   map { print "$column_header{$_}\n" } @column_index;
2134
2135   print qq|
2136         </tr>
2137 |;
2138
2139   foreach $ref (@{ $form->{ALL} }) {
2140
2141     $i++;
2142     $i %= 2;
2143
2144     print qq|
2145         <tr valign=top class=listrow$i>
2146 |;
2147
2148
2149     $column_data{printer_description} =
2150       qq|<td><a href=$form->{script}?action=edit_printer&id=$ref->{id}&path=$form->{path}&login=$form->{login}&password=$form->{password}&callback=$callback>$ref->{printer_description}</td>|;
2151     $column_data{printer_command}           = qq|<td align=right>$ref->{printer_command}</td>|;
2152     $column_data{template_code} =
2153       qq|<td align=right>$ref->{template_code}</td>|;
2154
2155     map { print "$column_data{$_}\n" } @column_index;
2156
2157     print qq|
2158         </tr>
2159 |;
2160   }
2161
2162   print qq|
2163       </table>
2164     </td>
2165   </tr>
2166   <tr>
2167   <td><hr size=3 noshade></td>
2168   </tr>
2169 </table>
2170
2171 <br>
2172 <form method=post action=$form->{script}>
2173
2174 <input name=callback type=hidden value="$form->{callback}">
2175
2176 <input type=hidden name=type value=printer>
2177
2178 <input type=hidden name=path value=$form->{path}>
2179 <input type=hidden name=login value=$form->{login}>
2180 <input type=hidden name=password value=$form->{password}>
2181
2182 <input class=submit type=submit name=action value="|
2183     . $locale->text('Add') . qq|">|;
2184
2185   if ($form->{menubar}) {
2186     require "$form->{path}/menu.pl";
2187     &menubar;
2188   }
2189
2190   print qq|
2191
2192   </form>
2193
2194   </body>
2195   </html>
2196 |;
2197
2198   $lxdebug->leave_sub();
2199 }
2200
2201 sub printer_header {
2202   $lxdebug->enter_sub();
2203
2204   $form->{title}    = $locale->text("$form->{title} Printer");
2205
2206   # $locale->text('Add Printer')
2207   # $locale->text('Edit Printer')
2208
2209   $form->{printer_description} =~ s/\"/&quot;/g;
2210   $form->{template_code} =~ s/\"/&quot;/g;
2211   $form->{printer_command} =~ s/\"/&quot;/g;
2212
2213
2214   $form->header;
2215
2216   print qq|
2217 <body>
2218
2219 <form method=post action=$form->{script}>
2220
2221 <input type=hidden name=id value=$form->{id}>
2222 <input type=hidden name=type value=printer>
2223
2224 <table width=100%>
2225   <tr>
2226     <th class=listtop colspan=2>$form->{title}</th>
2227   </tr>
2228   <tr height="5"></tr>
2229   <tr>
2230     <th align=right>| . $locale->text('Printer') . qq|</th>
2231     <td><input name=printer_description size=30 value="$form->{printer_description}"></td>
2232   <tr>
2233   <tr>
2234     <th align=right>| . $locale->text('Printer Command') . qq|</th>
2235     <td><input name=printer_command size=30 value="$form->{printer_command}"></td>
2236   </tr>
2237   <tr>
2238     <th align=right>| . $locale->text('Template Code') . qq|</th>
2239     <td><input name=template_code size=5 value="$form->{template_code}"></td>
2240   </tr>
2241   <td colspan=2><hr size=3 noshade></td>
2242   </tr>
2243 </table>
2244 |;
2245
2246   $lxdebug->leave_sub();
2247 }
2248
2249 sub save_printer {
2250   $lxdebug->enter_sub();
2251
2252   $form->isblank("printer_description", $locale->text('Description missing!'));
2253   $form->isblank("printer_command", $locale->text('Printer Command missing!'));
2254   AM->save_printer(\%myconfig, \%$form);
2255   $form->redirect($locale->text('Printer saved!'));
2256
2257   $lxdebug->leave_sub();
2258 }
2259
2260 sub delete_printer {
2261   $lxdebug->enter_sub();
2262
2263   AM->delete_printer(\%myconfig, \%$form);
2264   $form->redirect($locale->text('Printer deleted!'));
2265
2266   $lxdebug->leave_sub();
2267 }
2268
2269
2270 sub add_adr {
2271   $lxdebug->enter_sub();
2272
2273   $form->{title} = "Add";
2274
2275   $form->{callback} =
2276     "$form->{script}?action=add_adr&path=$form->{path}&login=$form->{login}&password=$form->{password}"
2277     unless $form->{callback};
2278
2279   &adr_header;
2280   &form_footer;
2281
2282   $lxdebug->leave_sub();
2283 }
2284
2285 sub edit_adr {
2286   $lxdebug->enter_sub();
2287
2288   $form->{title} = "Edit";
2289
2290   AM->get_adr(\%myconfig, \%$form);
2291
2292   &adr_header;
2293
2294   $form->{orphaned} = 1;
2295   &form_footer;
2296
2297   $lxdebug->leave_sub();
2298 }
2299
2300 sub list_adr {
2301   $lxdebug->enter_sub();
2302
2303   AM->adr(\%myconfig, \%$form);
2304
2305   $form->{callback} =
2306     "$form->{script}?action=list_adr&path=$form->{path}&login=$form->{login}&password=$form->{password}";
2307
2308   $callback = $form->escape($form->{callback});
2309
2310   $form->{title} = $locale->text('ADR');
2311
2312   @column_index = qw(adr_code adr_description);
2313
2314   $column_header{adr_description} =
2315       qq|<th class=listheading width=60%>|
2316     . $locale->text('ADR Description')
2317     . qq|</th>|;
2318   $column_header{adr_code} =
2319       qq|<th class=listheading width=10%>|
2320     . $locale->text('ADR Code')
2321     . qq|</th>|;
2322
2323   $form->header;
2324
2325   print qq|
2326 <body>
2327
2328 <table width=100%>
2329   <tr>
2330     <th class=listtop>$form->{title}</th>
2331   </tr>
2332   <tr height="5"></tr>
2333   <tr>
2334     <td>
2335       <table width=100%>
2336         <tr class=listheading>
2337 |;
2338
2339   map { print "$column_header{$_}\n" } @column_index;
2340
2341   print qq|
2342         </tr>
2343 |;
2344
2345   foreach $ref (@{ $form->{ALL} }) {
2346
2347     $i++;
2348     $i %= 2;
2349
2350     print qq|
2351         <tr valign=top class=listrow$i>
2352 |;
2353
2354
2355     $column_data{adr_code} =
2356       qq|<td><a href=$form->{script}?action=edit_adr&id=$ref->{id}&path=$form->{path}&login=$form->{login}&password=$form->{password}&callback=$callback>$ref->{adr_code}</td>|;
2357     $column_data{adr_description}           = qq|<td align=left>$ref->{adr_description}</td>|;
2358
2359
2360     map { print "$column_data{$_}\n" } @column_index;
2361
2362     print qq|
2363         </tr>
2364 |;
2365   }
2366
2367   print qq|
2368       </table>
2369     </td>
2370   </tr>
2371   <tr>
2372   <td><hr size=3 noshade></td>
2373   </tr>
2374 </table>
2375
2376 <br>
2377 <form method=post action=$form->{script}>
2378
2379 <input name=callback type=hidden value="$form->{callback}">
2380
2381 <input type=hidden name=type value=adr>
2382
2383 <input type=hidden name=path value=$form->{path}>
2384 <input type=hidden name=login value=$form->{login}>
2385 <input type=hidden name=password value=$form->{password}>
2386
2387 <input class=submit type=submit name=action value="|
2388     . $locale->text('Add') . qq|">|;
2389
2390   if ($form->{menubar}) {
2391     require "$form->{path}/menu.pl";
2392     &menubar;
2393   }
2394
2395   print qq|
2396
2397   </form>
2398
2399   </body>
2400   </html>
2401 |;
2402
2403   $lxdebug->leave_sub();
2404 }
2405
2406 sub adr_header {
2407   $lxdebug->enter_sub();
2408
2409   $form->{title}    = $locale->text("$form->{title} ADR");
2410
2411   # $locale->text('Add ADR')
2412   # $locale->text('Edit ADR')
2413
2414   $form->{adr_description} =~ s/\"/&quot;/g;
2415   $form->{adr_code} =~ s/\"/&quot;/g;
2416
2417
2418   $form->header;
2419
2420   print qq|
2421 <body>
2422
2423 <form method=post action=$form->{script}>
2424
2425 <input type=hidden name=id value=$form->{id}>
2426 <input type=hidden name=type value=adr>
2427
2428 <table width=100%>
2429   <tr>
2430     <th class=listtop colspan=2>$form->{title}</th>
2431   </tr>
2432   <tr height="5"></tr>
2433   <tr>
2434     <th align=right>| . $locale->text('ADR Code') . qq|</th>
2435     <td><input name=adr_code size=30 value="$form->{adr_code}"></td>
2436   <tr>
2437   <tr>
2438     <th align=right>| . $locale->text('ADR Description') . qq|</th>
2439     <td><input name=adr_description size=60 value="$form->{adr_description}"></td>
2440   </tr>
2441   <td colspan=2><hr size=3 noshade></td>
2442   </tr>
2443 </table>
2444 |;
2445
2446   $lxdebug->leave_sub();
2447 }
2448
2449 sub save_adr {
2450   $lxdebug->enter_sub();
2451
2452   $form->isblank("adr_description", $locale->text('ADR Description missing!'));
2453   $form->isblank("adr_code", $locale->text('ADR Code missing!'));
2454   AM->save_adr(\%myconfig, \%$form);
2455   $form->redirect($locale->text('ADR saved!'));
2456
2457   $lxdebug->leave_sub();
2458 }
2459
2460
2461 sub delete_adr {
2462   $lxdebug->enter_sub();
2463
2464   AM->delete_adr(\%myconfig, \%$form);
2465   $form->redirect($locale->text('ADR deleted!'));
2466
2467   $lxdebug->leave_sub();
2468 }
2469
2470
2471 sub add_payment {
2472   $lxdebug->enter_sub();
2473
2474   $form->{title} = "Add";
2475
2476   $form->{callback} =
2477     "$form->{script}?action=add_payment&path=$form->{path}&login=$form->{login}&password=$form->{password}"
2478     unless $form->{callback};
2479
2480   &payment_header;
2481   &form_footer;
2482
2483   $lxdebug->leave_sub();
2484 }
2485
2486 sub edit_payment {
2487   $lxdebug->enter_sub();
2488
2489   $form->{title} = "Edit";
2490
2491   AM->get_payment(\%myconfig, \%$form);
2492
2493   &payment_header;
2494
2495   $form->{orphaned} = 1;
2496   &form_footer;
2497
2498   $lxdebug->leave_sub();
2499 }
2500
2501 sub list_payment {
2502   $lxdebug->enter_sub();
2503
2504   AM->payment(\%myconfig, \%$form);
2505
2506   $form->{callback} =
2507     "$form->{script}?action=list_payment&path=$form->{path}&login=$form->{login}&password=$form->{password}";
2508
2509   $callback = $form->escape($form->{callback});
2510
2511   $form->{title} = $locale->text('Payment Terms');
2512
2513   @column_index = qw(description description_long terms_netto terms_skonto percent_skonto);
2514
2515   $column_header{description} =
2516       qq|<th class=listheading>|
2517     . $locale->text('Description')
2518     . qq|</th>|;
2519   $column_header{description_long} =
2520       qq|<th class=listheading>|
2521     . $locale->text('Long Description')
2522     . qq|</th>|;
2523   $column_header{terms_netto} =
2524       qq|<th class=listheading>|
2525     . $locale->text('Netto Terms')
2526     . qq|</th>|;
2527   $column_header{terms_skonto} =
2528       qq|<th class=listheading>|
2529     . $locale->text('Skonto Terms')
2530     . qq|</th>|;
2531   $column_header{percent_skonto} =
2532       qq|<th class=listheading>|
2533     . $locale->text('Skonto')
2534     . qq| %</th>|;
2535
2536   $form->header;
2537
2538   print qq|
2539 <body>
2540
2541 <table width=100%>
2542   <tr>
2543     <th class=listtop>$form->{title}</th>
2544   </tr>
2545   <tr height="5"></tr>
2546   <tr>
2547     <td>
2548       <table width=100%>
2549         <tr class=listheading>
2550 |;
2551
2552   map { print "$column_header{$_}\n" } @column_index;
2553
2554   print qq|
2555         </tr>
2556 |;
2557
2558   foreach $ref (@{ $form->{ALL} }) {
2559
2560     $i++;
2561     $i %= 2;
2562
2563     print qq|
2564         <tr valign=top class=listrow$i>
2565 |;
2566
2567
2568     $column_data{description} =
2569       qq|<td><a href=$form->{script}?action=edit_payment&id=$ref->{id}&path=$form->{path}&login=$form->{login}&password=$form->{password}&callback=$callback>$ref->{description}</td>|;
2570     $column_data{description_long}           = qq|<td align=right>$ref->{description_long}</td>|;
2571     $column_data{terms_netto} =
2572       qq|<td align=right>$ref->{terms_netto}</td>|;
2573     $column_data{terms_skonto} =
2574       qq|<td align=right>$ref->{terms_skonto}</td>|;
2575     $column_data{percent_skonto} =
2576       qq|<td align=right>$ref->{percent_skonto} %</td>|;
2577     map { print "$column_data{$_}\n" } @column_index;
2578
2579     print qq|
2580         </tr>
2581 |;
2582   }
2583
2584   print qq|
2585       </table>
2586     </td>
2587   </tr>
2588   <tr>
2589   <td><hr size=3 noshade></td>
2590   </tr>
2591 </table>
2592
2593 <br>
2594 <form method=post action=$form->{script}>
2595
2596 <input name=callback type=hidden value="$form->{callback}">
2597
2598 <input type=hidden name=type value=business>
2599
2600 <input type=hidden name=path value=$form->{path}>
2601 <input type=hidden name=login value=$form->{login}>
2602 <input type=hidden name=password value=$form->{password}>
2603
2604 <input class=submit type=submit name=action value="|
2605     . $locale->text('Add') . qq|">|;
2606
2607   if ($form->{menubar}) {
2608     require "$form->{path}/menu.pl";
2609     &menubar;
2610   }
2611
2612   print qq|
2613
2614   </form>
2615
2616   </body>
2617   </html>
2618 |;
2619
2620   $lxdebug->leave_sub();
2621 }
2622
2623 sub payment_header {
2624   $lxdebug->enter_sub();
2625
2626   $form->{title}    = $locale->text("$form->{title} Payment Terms");
2627
2628   # $locale->text('Add Payment Terms')
2629   # $locale->text('Edit Payment Terms')
2630
2631   $form->{description} =~ s/\"/&quot;/g;
2632
2633
2634
2635   $form->header;
2636
2637   print qq|
2638 <body>
2639
2640 <form method=post action=$form->{script}>
2641
2642 <input type=hidden name=id value=$form->{id}>
2643 <input type=hidden name=type value=payment>
2644
2645 <table width=100%>
2646   <tr>
2647     <th class=listtop colspan=2>$form->{title}</th>
2648   </tr>
2649   <tr height="5"></tr>
2650   <tr>
2651     <th align=right>| . $locale->text('Description') . qq|</th>
2652     <td><input name=description size=30 value="$form->{description}"></td>
2653   <tr>
2654   <tr>
2655     <th align=right>| . $locale->text('Ranking') . qq|</th>
2656     <td><input name=ranking size=30 value="$form->{ranking}"></td>
2657   <tr>
2658   <tr>
2659     <th align=right>| . $locale->text('Long Description') . qq|</th>
2660     <td><input name=description_long size=50 value="$form->{description_long}"></td>
2661   </tr>
2662   <tr>
2663     <th align=right>| . $locale->text('Netto Terms') . qq|</th>
2664     <td><input name=terms_netto size=10 value="$form->{terms_netto}"></td>
2665   </tr>
2666   <tr>
2667     <th align=right>| . $locale->text('Skonto Terms') . qq|</th>
2668     <td><input name=terms_skonto size=10 value="$form->{terms_skonto}"></td>
2669   </tr>  
2670   <tr>
2671     <th align=right>| . $locale->text('Skonto') . qq| %</th>
2672     <td><input name=percent_skonto size=10 value="$form->{percent_skonto}"></td>
2673   </tr> 
2674   <td colspan=2><hr size=3 noshade></td>
2675   </tr>
2676 </table>
2677 |;
2678
2679   $lxdebug->leave_sub();
2680 }
2681
2682 sub save_payment {
2683   $lxdebug->enter_sub();
2684
2685   $form->isblank("description", $locale->text('Language missing!'));
2686   AM->save_payment(\%myconfig, \%$form);
2687   $form->redirect($locale->text('Payment Terms saved!'));
2688
2689   $lxdebug->leave_sub();
2690 }
2691
2692 sub delete_payment {
2693   $lxdebug->enter_sub();
2694
2695   AM->delete_payment(\%myconfig, \%$form);
2696   $form->redirect($locale->text('Payment terms deleted!'));
2697
2698   $lxdebug->leave_sub();
2699 }
2700
2701 sub add_sic {
2702   $lxdebug->enter_sub();
2703
2704   $form->{title} = "Add";
2705
2706   $form->{callback} =
2707     "$form->{script}?action=add_sic&path=$form->{path}&login=$form->{login}&password=$form->{password}"
2708     unless $form->{callback};
2709
2710   &sic_header;
2711   &form_footer;
2712
2713   $lxdebug->leave_sub();
2714 }
2715
2716 sub edit_sic {
2717   $lxdebug->enter_sub();
2718
2719   $form->{title} = "Edit";
2720
2721   AM->get_sic(\%myconfig, \%$form);
2722
2723   &sic_header;
2724
2725   $form->{orphaned} = 1;
2726   &form_footer;
2727
2728   $lxdebug->leave_sub();
2729 }
2730
2731 sub list_sic {
2732   $lxdebug->enter_sub();
2733
2734   AM->sic(\%myconfig, \%$form);
2735
2736   $form->{callback} =
2737     "$form->{script}?action=list_sic&path=$form->{path}&login=$form->{login}&password=$form->{password}";
2738
2739   $callback = $form->escape($form->{callback});
2740
2741   $form->{title} = $locale->text('Standard Industrial Codes');
2742
2743   @column_index = qw(code description);
2744
2745   $column_header{code} =
2746     qq|<th class=listheading>| . $locale->text('Code') . qq|</th>|;
2747   $column_header{description} =
2748     qq|<th class=listheading>| . $locale->text('Description') . qq|</th>|;
2749
2750   $form->header;
2751
2752   print qq|
2753 <body>
2754
2755 <table width=100%>
2756   <tr>
2757     <th class=listtop>$form->{title}</th>
2758   </tr>
2759   <tr height="5"></tr>
2760   <tr>
2761     <td>
2762       <table width=100%>
2763         <tr class=listheading>
2764 |;
2765
2766   map { print "$column_header{$_}\n" } @column_index;
2767
2768   print qq|
2769         </tr>
2770 |;
2771
2772   foreach $ref (@{ $form->{ALL} }) {
2773
2774     $i++;
2775     $i %= 2;
2776
2777     if ($ref->{sictype} eq 'H') {
2778       print qq|
2779         <tr valign=top class=listheading>
2780 |;
2781       $column_data{code} =
2782         qq|<th><a href=$form->{script}?action=edit_sic&code=$ref->{code}&path=$form->{path}&login=$form->{login}&password=$form->{password}&callback=$callback>$ref->{code}</th>|;
2783       $column_data{description} = qq|<th>$ref->{description}</th>|;
2784
2785     } else {
2786       print qq|
2787         <tr valign=top class=listrow$i>
2788 |;
2789
2790       $column_data{code} =
2791         qq|<td><a href=$form->{script}?action=edit_sic&code=$ref->{code}&path=$form->{path}&login=$form->{login}&password=$form->{password}&callback=$callback>$ref->{code}</td>|;
2792       $column_data{description} = qq|<td>$ref->{description}</td>|;
2793
2794     }
2795
2796     map { print "$column_data{$_}\n" } @column_index;
2797
2798     print qq|
2799         </tr>
2800 |;
2801   }
2802
2803   print qq|
2804       </table>
2805     </td>
2806   </tr>
2807   <tr>
2808   <td><hr size=3 noshade></td>
2809   </tr>
2810 </table>
2811
2812 <br>
2813 <form method=post action=$form->{script}>
2814
2815 <input name=callback type=hidden value="$form->{callback}">
2816
2817 <input type=hidden name=type value=sic>
2818
2819 <input type=hidden name=path value=$form->{path}>
2820 <input type=hidden name=login value=$form->{login}>
2821 <input type=hidden name=password value=$form->{password}>
2822
2823 <input class=submit type=submit name=action value="|
2824     . $locale->text('Add') . qq|">|;
2825
2826   if ($form->{menubar}) {
2827     require "$form->{path}/menu.pl";
2828     &menubar;
2829   }
2830
2831   print qq|
2832   </form>
2833
2834   </body>
2835   </html>
2836 |;
2837
2838   $lxdebug->leave_sub();
2839 }
2840
2841 sub sic_header {
2842   $lxdebug->enter_sub();
2843
2844   $form->{title} = $locale->text("$form->{title} SIC");
2845
2846   # $locale->text('Add SIC')
2847   # $locale->text('Edit SIC')
2848
2849   $form->{code}        =~ s/\"/&quot;/g;
2850   $form->{description} =~ s/\"/&quot;/g;
2851
2852   $checked = ($form->{sictype} eq 'H') ? "checked" : "";
2853
2854   $form->header;
2855
2856   print qq|
2857 <body>
2858
2859 <form method=post action=$form->{script}>
2860
2861 <input type=hidden name=type value=sic>
2862 <input type=hidden name=id value=$form->{code}>
2863
2864 <table width=100%>
2865   <tr>
2866     <th class=listtop colspan=2>$form->{title}</th>
2867   </tr>
2868   <tr height="5"></tr>
2869   <tr>
2870     <th align=right>| . $locale->text('Code') . qq|</th>
2871     <td><input name=code size=10 value=$form->{code}></td>
2872   <tr>
2873   <tr>
2874     <td></td>
2875     <th align=left><input name=sictype type=checkbox style=checkbox value="H" $checked> |
2876     . $locale->text('Heading') . qq|</th>
2877   <tr>
2878   <tr>
2879     <th align=right>| . $locale->text('Description') . qq|</th>
2880     <td><input name=description size=60 value="$form->{description}"></td>
2881   </tr>
2882     <td colspan=2><hr size=3 noshade></td>
2883   </tr>
2884 </table>
2885 |;
2886
2887   $lxdebug->leave_sub();
2888 }
2889
2890 sub save_sic {
2891   $lxdebug->enter_sub();
2892
2893   $form->isblank("code",        $locale->text('Code missing!'));
2894   $form->isblank("description", $locale->text('Description missing!'));
2895   AM->save_sic(\%myconfig, \%$form);
2896   $form->redirect($locale->text('SIC saved!'));
2897
2898   $lxdebug->leave_sub();
2899 }
2900
2901 sub delete_sic {
2902   $lxdebug->enter_sub();
2903
2904   AM->delete_sic(\%myconfig, \%$form);
2905   $form->redirect($locale->text('SIC deleted!'));
2906
2907   $lxdebug->leave_sub();
2908 }
2909
2910 sub display_stylesheet {
2911   $lxdebug->enter_sub();
2912
2913   $form->{file} = "css/$myconfig{stylesheet}";
2914   &display_form;
2915
2916   $lxdebug->leave_sub();
2917 }
2918
2919 sub display_form {
2920   $lxdebug->enter_sub();
2921
2922   $form->{file} =~ s/^(.:)*?\/|\.\.\///g;
2923   $form->{file} =~ s/^\/*//g;
2924   $form->{file} =~ s/$userspath//;
2925
2926   $form->error("$!: $form->{file}") unless -f $form->{file};
2927
2928   AM->load_template(\%$form);
2929
2930   $form->{title} = $form->{file};
2931
2932   # if it is anything but html
2933   if ($form->{file} !~ /\.html$/) {
2934     $form->{body} = "<pre>\n$form->{body}\n</pre>";
2935   }
2936
2937   $form->header;
2938
2939   print qq|
2940 <body>
2941
2942 $form->{body}
2943
2944 <form method=post action=$form->{script}>
2945
2946 <input name=file type=hidden value=$form->{file}>
2947 <input name=type type=hidden value=template>
2948
2949 <input type=hidden name=path value=$form->{path}>
2950 <input type=hidden name=login value=$form->{login}>
2951 <input type=hidden name=password value=$form->{password}>
2952
2953 <input name=action type=submit class=submit value="|
2954     . $locale->text('Edit') . qq|">|;
2955
2956   if ($form->{menubar}) {
2957     require "$form->{path}/menu.pl";
2958     &menubar;
2959   }
2960
2961   print qq|
2962   </form>
2963
2964 </body>
2965 </html>
2966 |;
2967
2968   $lxdebug->leave_sub();
2969 }
2970
2971 sub edit_template {
2972   $lxdebug->enter_sub();
2973
2974   AM->load_template(\%$form);
2975
2976   $form->{title} = $locale->text('Edit Template');
2977
2978   # convert &nbsp to &amp;nbsp;
2979   $form->{body} =~ s/&nbsp;/&amp;nbsp;/gi;
2980
2981   $form->header;
2982
2983   print qq|
2984 <body>
2985
2986 <form method=post action=$form->{script}>
2987
2988 <input name=file type=hidden value=$form->{file}>
2989 <input name=type type=hidden value=template>
2990
2991 <input type=hidden name=path value=$form->{path}>
2992 <input type=hidden name=login value=$form->{login}>
2993 <input type=hidden name=password value=$form->{password}>
2994
2995 <input name=callback type=hidden value="$form->{script}?action=display_form&file=$form->{file}&path=$form->{path}&login=$form->{login}&password=$form->{password}">
2996
2997 <textarea name=body rows=25 cols=70>
2998 $form->{body}
2999 </textarea>
3000
3001 <br>
3002 <input type=submit class=submit name=action value="|
3003     . $locale->text('Save') . qq|">|;
3004
3005   if ($form->{menubar}) {
3006     require "$form->{path}/menu.pl";
3007     &menubar;
3008   }
3009
3010   print q|
3011   </form>
3012
3013
3014 </body>
3015 </html>
3016 |;
3017
3018   $lxdebug->leave_sub();
3019 }
3020
3021 sub save_template {
3022   $lxdebug->enter_sub();
3023
3024   AM->save_template(\%$form);
3025   $form->redirect($locale->text('Template saved!'));
3026
3027   $lxdebug->leave_sub();
3028 }
3029
3030 sub config {
3031   $lxdebug->enter_sub();
3032
3033   # get defaults for account numbers and last numbers
3034   AM->defaultaccounts(\%myconfig, \%$form);
3035
3036   foreach $item (qw(mm-dd-yy mm/dd/yy dd-mm-yy dd/mm/yy dd.mm.yy yyyy-mm-dd)) {
3037     $dateformat .=
3038       ($item eq $myconfig{dateformat})
3039       ? "<option selected>$item\n"
3040       : "<option>$item\n";
3041   }
3042
3043   foreach $item (qw(1,000.00 1000.00 1.000,00 1000,00)) {
3044     $numberformat .=
3045       ($item eq $myconfig{numberformat})
3046       ? "<option selected>$item\n"
3047       : "<option>$item\n";
3048   }
3049
3050   foreach $item (qw(name company address signature)) {
3051     $myconfig{$item} =~ s/\"/&quot;/g;
3052   }
3053
3054   foreach $item (qw(address signature)) {
3055     $myconfig{$item} =~ s/\\n/\r\n/g;
3056   }
3057
3058   %countrycodes = User->country_codes;
3059   $countrycodes = '';
3060   foreach $key (sort { $countrycodes{$a} cmp $countrycodes{$b} }
3061                 keys %countrycodes
3062     ) {
3063     $countrycodes .=
3064       ($myconfig{countrycode} eq $key)
3065       ? "<option selected value=$key>$countrycodes{$key}\n"
3066       : "<option value=$key>$countrycodes{$key}\n";
3067   }
3068   $countrycodes = "<option>American English\n$countrycodes";
3069
3070   # use an other input number format than output numberformat
3071   # look at Form.pm, sub parse_amount
3072   my $in_numberformat = '';
3073   $text1 = qq|value="0">| . $locale->text('equal Outputformat');
3074   $text2 = qq|value="1">| . $locale->text('1000,00 or 1000.00');
3075   @in_nf = ($text1, $text2);
3076   foreach $item (@in_nf) {
3077     $in_numberformat .=
3078       (substr($item, 7, 1) eq $myconfig{in_numberformat})
3079       ? "<option selected $item\n"
3080       : "<option $item\n";
3081   }
3082
3083   foreach $key (keys %{ $form->{IC} }) {
3084     foreach $accno (sort keys %{ $form->{IC}{$key} }) {
3085       $myconfig{$key} .=
3086         ($form->{IC}{$key}{$accno}{id} == $form->{defaults}{$key})
3087         ? "<option selected>$accno--$form->{IC}{$key}{$accno}{description}\n"
3088         : "<option>$accno--$form->{IC}{$key}{$accno}{description}\n";
3089     }
3090   }
3091
3092   opendir CSS, "css/.";
3093   @all = grep /.*\.css$/, readdir CSS;
3094   closedir CSS;
3095
3096   foreach $item (@all) {
3097     if ($item eq $myconfig{stylesheet}) {
3098       $selectstylesheet .= qq|<option selected>$item\n|;
3099     } else {
3100       $selectstylesheet .= qq|<option>$item\n|;
3101     }
3102   }
3103   $selectstylesheet .= "<option>\n";
3104
3105   $form->{title} = $locale->text('Edit Preferences for') . qq| $form->{login}|;
3106
3107   $form->header;
3108
3109   if ($myconfig{menustyle} eq "old") { $oldS = "checked"; }
3110   else { $newS = "checked"; }
3111
3112   print qq|
3113 <body>
3114
3115 <form method=post action=$form->{script}>
3116
3117 <input type=hidden name=old_password value=$myconfig{password}>
3118 <input type=hidden name=type value=preferences>
3119 <input type=hidden name=role value=$myconfig{role}>
3120
3121 <table width=100%>
3122   <tr><th class=listtop>$form->{title}</th></tr>
3123   <tr>
3124     <td>
3125       <table>
3126         <tr>
3127           <th align=right>| . $locale->text('Name') . qq|</th>
3128           <td><input name=name size=15 value="$myconfig{name}"></td>
3129         </tr>
3130         <tr>
3131           <th align=right>| . $locale->text('Password') . qq|</th>
3132           <td><input type=password name=new_password size=10 value=$myconfig{password}></td>
3133         </tr>
3134         <tr>
3135           <th align=right>| . $locale->text('E-mail') . qq|</th>
3136           <td><input name=email size=30 value="$myconfig{email}"></td>
3137         </tr>
3138         <tr valign=top>
3139           <th align=right>| . $locale->text('Signature') . qq|</th>
3140           <td><textarea name=signature rows=3 cols=50>$myconfig{signature}</textarea></td>
3141         </tr>
3142         <tr>
3143           <th align=right>| . $locale->text('Phone') . qq|</th>
3144           <td><input name=tel size=14 value="$myconfig{tel}"></td>
3145         </tr>
3146         <tr>
3147           <th align=right>| . $locale->text('Fax') . qq|</th>
3148           <td><input name=fax size=14 value="$myconfig{fax}"></td>
3149         </tr>
3150         <tr>
3151           <th align=right>| . $locale->text('Company') . qq|</th>
3152           <td><input name=company size=30 value="$myconfig{company}"></td>
3153         </tr>
3154         <tr valign=top>
3155           <th align=right>| . $locale->text('Address') . qq|</th>
3156           <td><textarea name=address rows=4 cols=50>$myconfig{address}</textarea></td>
3157         </tr>
3158         <tr>
3159           <th align=right>| . $locale->text('Date Format') . qq|</th>
3160           <td><select name=dateformat>$dateformat</select></td>
3161         </tr>
3162         <tr>
3163           <th align=right>| . $locale->text('Output Number Format') . qq|</th>
3164           <td><select name=numberformat>$numberformat</select></td>
3165         </tr>
3166         <tr>
3167           <th align=right>| . $locale->text('Input Number Format') . qq|</th>
3168           <td><select name=in_numberformat>$in_numberformat</select></td>
3169         </tr>
3170
3171         <tr>
3172           <th align=right>| . $locale->text('Dropdown Limit') . qq|</th>
3173           <td><input name=vclimit size=10 value="$myconfig{vclimit}"></td>
3174         </tr>
3175         <tr>
3176           <th align=right>| . $locale->text('Language') . qq|</th>
3177           <td><select name=countrycode>$countrycodes</select></td>
3178         </tr>
3179         <tr>
3180           <th align=right>| . $locale->text('Stylesheet') . qq|</th>
3181           <td><select name=usestylesheet>$selectstylesheet</select></td>
3182         </tr>
3183         <tr>
3184           <th align=right>| . $locale->text('Setup Menu') . qq|</th>
3185           <td><input name=menustyle type=radio class=radio value=neu $newS>&nbsp;New
3186                   <input name=menustyle type=radio class=radio value=old $oldS>&nbsp;Old</td>
3187         </tr>   
3188         <input name=printer type=hidden value="$myconfig{printer}">
3189         <tr class=listheading>
3190           <th colspan=2>&nbsp;</th>
3191         </tr>
3192         <tr>
3193           <th align=right>| . $locale->text('Business Number') . qq|</th>
3194           <td><input name=businessnumber size=25 value="$myconfig{businessnumber}"></td>
3195         </tr>
3196         <tr>
3197           <td colspan=2>
3198             <table width=100%>
3199               <tr>
3200                 <th align=right>| . $locale->text('Year End') . qq| (mm/dd)</th>
3201                 <td><input name=yearend size=5 value=$form->{defaults}{yearend}></td>
3202                 <th align=right>| . $locale->text('Weight Unit') . qq|</th>
3203                 <td><input name=weightunit size=5 value="$form->{defaults}{weightunit}"></td>
3204               </tr>
3205             </table>
3206           </td>
3207         </tr>
3208         <tr class=listheading>
3209           <th colspan=2>|
3210     . $locale->text('Last Numbers & Default Accounts') . qq|</th>
3211         </tr>
3212         <tr>
3213           <td colspan=2>
3214             <table width=100%>
3215               <tr>
3216                 <th align=right nowrap>| . $locale->text('Inventory Account') . qq|</th>
3217                 <td><select name=inventory_accno>$myconfig{IC}</select></td>
3218               </tr>
3219               <tr>
3220                 <th align=right nowrap>| . $locale->text('Revenue Account') . qq|</th>
3221                 <td><select name=income_accno>$myconfig{IC_income}</select></td>
3222               </tr>
3223               <tr>
3224                 <th align=right nowrap>| . $locale->text('Expense Account') . qq|</th>
3225                 <td><select name=expense_accno>$myconfig{IC_expense}</select></td>
3226               </tr>
3227               <tr>
3228                 <th align=right nowrap>| . $locale->text('Foreign Exchange Gain') . qq|</th>
3229                 <td><select name=fxgain_accno>$myconfig{FX_gain}</select></td>
3230               </tr>
3231               <tr>
3232                 <th align=right nowrap>| . $locale->text('Foreign Exchange Loss') . qq|</th>
3233                 <td><select name=fxloss_accno>$myconfig{FX_loss}</select></td>
3234               </tr>
3235               <tr>
3236                 <td colspan=2>|
3237     . $locale->text(
3238     'Enter up to 3 letters separated by a colon (i.e CAD:USD:EUR) for your native and foreign currencies'
3239     )
3240     . qq|<br><input name=curr size=40 value="$form->{defaults}{curr}"></td>
3241               </tr>
3242             </table>
3243           </td>
3244          </tr>
3245          <tr>
3246            <td colspan=2>
3247              <table width=100%>
3248               <tr>
3249                 <th align=right nowrap>| . $locale->text('Last Invoice Number') . qq|</th>
3250                 <td><input name=invnumber size=10 value=$form->{defaults}{invnumber}></td>
3251                 <th align=right nowrap>|
3252     . $locale->text('Last Customer Number') . qq|</th>
3253                 <td><input name=customernumber size=10 value=$form->{defaults}{customernumber}></td>
3254               </tr>
3255               <tr>
3256                 <th align=right nowrap>|
3257     . $locale->text('Last Credit Note Number') . qq|</th>
3258                 <td><input name=cnnumber size=10 value=$form->{defaults}{cnnumber}></td>
3259                 <th align=right nowrap>|
3260     . $locale->text('Last Vendor Number') . qq|</th>
3261                 <td><input name=vendornumber size=10 value=$form->{defaults}{vendornumber}></td>
3262               </tr>
3263               <tr>
3264                 <th align=right nowrap>|
3265     . $locale->text('Last Sales Order Number') . qq|</th>
3266                 <td><input name=sonumber size=10 value=$form->{defaults}{sonumber}></td>
3267               </tr>
3268               <tr>
3269                 <th align=right nowrap>|
3270     . $locale->text('Last Purchase Order Number') . qq|</th>
3271                 <td><input name=ponumber size=10 value=$form->{defaults}{ponumber}></td>
3272                 <th align=right nowrap>|
3273     . $locale->text('Last Article Number') . qq|</th>
3274                 <td><input name=articlenumber size=10 value=$form->{defaults}{articlenumber}></td>
3275               </tr>
3276               <tr>
3277                 <th align=right nowrap>|
3278     . $locale->text('Last Sales Quotation Number') . qq|</th>
3279                 <td><input name=sqnumber size=10 value=$form->{defaults}{sqnumber}></td>
3280                 <th align=right nowrap>|
3281     . $locale->text('Last Service Number') . qq|</th>
3282                 <td><input name=servicenumber size=10 value=$form->{defaults}{servicenumber}></td>
3283               </tr>
3284               <tr>
3285                 <th align=right nowrap>| . $locale->text('Last RFQ Number') . qq|</th>
3286                 <td><input name=rfqnumber size=10 value=$form->{defaults}{rfqnumber}></td>
3287                 <th align=right nowrap></th>
3288                 <td></td>
3289               </tr>
3290             </table>
3291           </td>
3292         </tr>
3293         <tr class=listheading>
3294           <th colspan=2>| . $locale->text('Tax Accounts') . qq|</th>
3295         </tr>
3296         <tr>
3297           <td colspan=2>
3298             <table>
3299               <tr>
3300                 <th>&nbsp;</th>
3301                 <th>| . $locale->text('Rate') . qq| (%)</th>
3302                 <th>| . $locale->text('Number') . qq|</th>
3303               </tr>
3304 |;
3305
3306   foreach $accno (sort keys %{ $form->{taxrates} }) {
3307     print qq|
3308               <tr>
3309                 <th align=right>$form->{taxrates}{$accno}{description}</th>
3310                 <td><input name=$form->{taxrates}{$accno}{id} size=6 value=$form->{taxrates}{$accno}{rate}></td>
3311                 <td><input name="taxnumber_$form->{taxrates}{$accno}{id}" value="$form->{taxrates}{$accno}{taxnumber}"></td>
3312               </tr>
3313 |;
3314     $form->{taxaccounts} .= "$form->{taxrates}{$accno}{id} ";
3315   }
3316
3317   chop $form->{taxaccounts};
3318
3319   print qq|
3320 <input name=taxaccounts type=hidden value="$form->{taxaccounts}">
3321
3322             </table>
3323           </td>
3324         </tr>
3325       </table>
3326     </td>
3327   </tr>
3328   <tr>
3329     <td><hr size=3 noshade></td>
3330   </tr>
3331 </table>
3332
3333 <input type=hidden name=path value=$form->{path}>
3334 <input type=hidden name=login value=$form->{login}>
3335 <input type=hidden name=password value=$form->{password}>
3336
3337 <br>
3338 <input type=submit class=submit name=action value="|
3339     . $locale->text('Save') . qq|">|;
3340
3341   if ($form->{menubar}) {
3342     require "$form->{path}/menu.pl";
3343     &menubar;
3344   }
3345
3346   print qq|
3347   </form>
3348
3349 </body>
3350 </html>
3351 |;
3352
3353   $lxdebug->leave_sub();
3354 }
3355
3356 sub save_preferences {
3357   $lxdebug->enter_sub();
3358
3359   $form->{stylesheet} = $form->{usestylesheet};
3360
3361   $form->redirect($locale->text('Preferences saved!'))
3362     if (
3363      AM->save_preferences(\%myconfig, \%$form, $memberfile, $userspath, $webdav
3364      ));
3365   $form->error($locale->text('Cannot save preferences!'));
3366
3367   $lxdebug->leave_sub();
3368 }
3369
3370 sub backup {
3371   $lxdebug->enter_sub();
3372
3373   if ($form->{media} eq 'email') {
3374     $form->error($locale->text('No email address for') . " $myconfig{name}")
3375       unless ($myconfig{email});
3376
3377     $form->{OUT} = "$sendmail";
3378
3379   }
3380
3381   AM->backup(\%myconfig, \%$form, $userspath);
3382
3383   if ($form->{media} eq 'email') {
3384     $form->redirect($locale->text('Backup sent to') . qq| $myconfig{email}|);
3385   }
3386
3387   $lxdebug->leave_sub();
3388 }
3389
3390 sub audit_control {
3391   $lxdebug->enter_sub();
3392
3393   $form->{title} = $locale->text('Audit Control');
3394
3395   AM->closedto(\%myconfig, \%$form);
3396
3397   if ($form->{revtrans}) {
3398     $checked{Y} = "checked";
3399   } else {
3400     $checked{N} = "checked";
3401   }
3402
3403   $form->header;
3404
3405   print qq|
3406 <body>
3407
3408 <form method=post action=$form->{script}>
3409
3410 <input type=hidden name=path value=$form->{path}>
3411 <input type=hidden name=login value=$form->{login}>
3412 <input type=hidden name=password value=$form->{password}>
3413
3414 <table width=100%>
3415   <tr><th class=listtop>$form->{title}</th></tr>
3416   <tr height="5"></tr>
3417   <tr>
3418     <td>
3419       <table>
3420         <tr>
3421           <td>|
3422     . $locale->text('Enforce transaction reversal for all dates') . qq|</th>
3423           <td><input name=revtrans class=radio type=radio value="1" $checked{Y}> |
3424     . $locale->text('Yes')
3425     . qq| <input name=revtrans class=radio type=radio value="0" $checked{N}> |
3426     . $locale->text('No')
3427     . qq|</td>
3428         </tr>
3429         <tr>
3430           <th>| . $locale->text('Close Books up to') . qq|</th>
3431           <td><input name=closedto size=11 title="$myconfig{dateformat}" value=$form->{closedto}></td>
3432         </tr>
3433       </table>
3434     </td>
3435   </tr>
3436 </table>
3437
3438 <hr size=3 noshade>
3439
3440 <br>
3441 <input type=hidden name=nextsub value=doclose>
3442
3443 <input type=submit class=submit name=action value="|
3444     . $locale->text('Continue') . qq|">
3445
3446 </form>
3447
3448 </body>
3449 </html>
3450 |;
3451
3452   $lxdebug->leave_sub();
3453 }
3454
3455 sub doclose {
3456   $lxdebug->enter_sub();
3457
3458   AM->closebooks(\%myconfig, \%$form);
3459
3460   if ($form->{revtrans}) {
3461     $form->redirect(
3462                  $locale->text('Transaction reversal enforced for all dates'));
3463   } else {
3464     if ($form->{closedto}) {
3465       $form->redirect(
3466                      $locale->text('Transaction reversal enforced up to') . " "
3467                        . $locale->date(\%myconfig, $form->{closedto}, 1));
3468     } else {
3469       $form->redirect($locale->text('Books are open'));
3470     }
3471   }
3472
3473   $lxdebug->leave_sub();
3474 }
3475
3476 sub add_warehouse {
3477   $lxdebug->enter_sub();
3478
3479   $form->{title} = "Add";
3480
3481   $form->{callback} =
3482     "$form->{script}?action=add_warehouse&path=$form->{path}&login=$form->{login}&password=$form->{password}"
3483     unless $form->{callback};
3484
3485   &warehouse_header;
3486   &form_footer;
3487
3488   $lxdebug->leave_sub();
3489 }
3490
3491 sub edit_warehouse {
3492   $lxdebug->enter_sub();
3493
3494   $form->{title} = "Edit";
3495
3496   AM->get_warehouse(\%myconfig, \%$form);
3497
3498   &warehouse_header;
3499   &form_footer;
3500
3501   $lxdebug->leave_sub();
3502 }
3503
3504 sub list_warehouse {
3505   $lxdebug->enter_sub();
3506
3507   AM->warehouses(\%myconfig, \%$form);
3508
3509   $form->{callback} =
3510     "$form->{script}?action=list_warehouse&path=$form->{path}&login=$form->{login}&password=$form->{password}";
3511
3512   $callback = $form->escape($form->{callback});
3513
3514   $form->{title} = $locale->text('Warehouses');
3515
3516   @column_index = qw(description);
3517
3518   $column_header{description} =
3519       qq|<th class=listheading width=100%>|
3520     . $locale->text('Description')
3521     . qq|</th>|;
3522
3523   $form->header;
3524
3525   print qq|
3526 <body>
3527
3528 <table width=100%>
3529   <tr>
3530     <th class=listtop>$form->{title}</th>
3531   </tr>
3532   <tr height="5"></tr>
3533   <tr>
3534     <td>
3535       <table width=100%>
3536         <tr class=listheading>
3537 |;
3538
3539   map { print "$column_header{$_}\n" } @column_index;
3540
3541   print qq|
3542         </tr>
3543 |;
3544
3545   foreach $ref (@{ $form->{ALL} }) {
3546
3547     $i++;
3548     $i %= 2;
3549
3550     print qq|
3551         <tr valign=top class=listrow$i>
3552 |;
3553
3554     $column_data{description} =
3555       qq|<td><a href=$form->{script}?action=edit_warehouse&id=$ref->{id}&path=$form->{path}&login=$form->{login}&password=$form->{password}&callback=$callback>$ref->{description}</td>|;
3556
3557     map { print "$column_data{$_}\n" } @column_index;
3558
3559     print qq|
3560         </tr>
3561 |;
3562   }
3563
3564   print qq|
3565       </table>
3566     </td>
3567   </tr>
3568   <tr>
3569   <td><hr size=3 noshade></td>
3570   </tr>
3571 </table>
3572
3573 <br>
3574 <form method=post action=$form->{script}>
3575
3576 <input name=callback type=hidden value="$form->{callback}">
3577
3578 <input type=hidden name=type value=warehouse>
3579
3580 <input type=hidden name=path value=$form->{path}>
3581 <input type=hidden name=login value=$form->{login}>
3582 <input type=hidden name=password value=$form->{password}>
3583
3584 <input class=submit type=submit name=action value="|
3585     . $locale->text('Add') . qq|">|;
3586
3587   if ($form->{menubar}) {
3588     require "$form->{path}/menu.pl";
3589     &menubar;
3590   }
3591
3592   print qq|
3593   </form>
3594
3595   </body>
3596   </html>
3597 |;
3598
3599   $lxdebug->leave_sub();
3600 }
3601
3602 sub warehouse_header {
3603   $lxdebug->enter_sub();
3604
3605   $form->{title} = $locale->text("$form->{title} Warehouse");
3606
3607   # $locale->text('Add Warehouse')
3608   # $locale->text('Edit Warehouse')
3609
3610   $form->{description} =~ s/\"/&quot;/g;
3611
3612   if (($rows = $form->numtextrows($form->{description}, 60)) > 1) {
3613     $description =
3614       qq|<textarea name="description" rows=$rows cols=60 wrap=soft>$form->{description}</textarea>|;
3615   } else {
3616     $description =
3617       qq|<input name=description size=60 value="$form->{description}">|;
3618   }
3619
3620   $form->header;
3621
3622   print qq|
3623 <body>
3624
3625 <form method=post action=$form->{script}>
3626
3627 <input type=hidden name=id value=$form->{id}>
3628 <input type=hidden name=type value=warehouse>
3629
3630 <table width=100%>
3631   <tr>
3632     <th class=listtop colspan=2>$form->{title}</th>
3633   </tr>
3634   <tr height="5"></tr>
3635   <tr>
3636     <th align=right>| . $locale->text('Description') . qq|</th>
3637     <td>$description</td>
3638   </tr>
3639   <tr>
3640     <td colspan=2><hr size=3 noshade></td>
3641   </tr>
3642 </table>
3643 |;
3644
3645   $lxdebug->leave_sub();
3646 }
3647
3648 sub save_warehouse {
3649   $lxdebug->enter_sub();
3650
3651   $form->isblank("description", $locale->text('Description missing!'));
3652   AM->save_warehouse(\%myconfig, \%$form);
3653   $form->redirect($locale->text('Warehouse saved!'));
3654
3655   $lxdebug->leave_sub();
3656 }
3657
3658 sub delete_warehouse {
3659   $lxdebug->enter_sub();
3660
3661   AM->delete_warehouse(\%myconfig, \%$form);
3662   $form->redirect($locale->text('Warehouse deleted!'));
3663
3664   $lxdebug->leave_sub();
3665 }
3666
3667 sub continue {
3668   $lxdebug->enter_sub();
3669
3670   &{ $form->{nextsub} };
3671
3672   $lxdebug->leave_sub();
3673 }
3674
3675 sub edit_units {
3676   $lxdebug->enter_sub();
3677
3678   $units = AM->retrieve_units(\%myconfig, $form, $form->{"unit_type"}, "resolved_");
3679   AM->units_in_use(\%myconfig, $form, $units);
3680   map({ $units->{$_}->{"BASE_UNIT_DDBOX"} = AM->unit_select_data($units, $units->{$_}->{"base_unit"}, 1); } keys(%{$units}));
3681
3682   @unit_list = ();
3683   foreach $name (sort({ lc($a) cmp lc($b) } grep({ !$units->{$_}->{"base_unit"} } keys(%{$units})))) {
3684     map({ push(@unit_list, $units->{$_}); }
3685         sort({ ($units->{$a}->{"resolved_factor"} * 1) <=> ($units->{$b}->{"resolved_factor"} * 1) }
3686              grep({ $units->{$_}->{"resolved_base_unit"} eq $name } keys(%{$units}))));
3687   }
3688   map({ $_->{"factor"} = $form->format_amount(\%myconfig, $_->{"factor"}, 5) if ($_->{"factor"}); } @unit_list);
3689
3690   $units = AM->retrieve_units(\%myconfig, $form, $form->{"unit_type"});
3691   $ddbox = AM->unit_select_data($units, undef, 1);
3692
3693   $form->{"title"} = sprintf($locale->text("Add and edit %s"), $form->{"unit_type"} eq "dimension" ? $locale->text("dimension units") : $locale->text("service units"));
3694   $form->header();
3695   print($form->parse_html_template("am/edit_units", { "UNITS" => \@unit_list, "NEW_BASE_UNIT_DDBOX" => $ddbox }));
3696
3697   $lxdebug->leave_sub();
3698 }
3699
3700 sub add_unit {
3701   $lxdebug->enter_sub();
3702
3703   $form->isblank("new_name", $locale->text("The name is missing."));
3704   $units = AM->retrieve_units(\%myconfig, $form, $form->{"unit_type"});
3705   $form->show_generic_error($locale->text("A unit with this name does already exist.")) if ($units->{$form->{"new_name"}});
3706
3707   my ($base_unit, $factor);
3708   if ($form->{"new_base_unit"}) {
3709     $form->show_generic_error($locale->text("The base unit does not exist.")) unless (defined($units->{$form->{"new_base_unit"}}));
3710
3711     $form->isblank("new_factor", $locale->text("The factor is missing."));
3712     $factor = $form->parse_amount(\%myconfig, $form->{"new_factor"});
3713     $form->show_generic_error($locale->text("The factor is missing.")) unless ($factor);
3714     $base_unit = $form->{"new_base_unit"};
3715   }
3716
3717   AM->add_unit(\%myconfig, $form, $form->{"new_name"}, $base_unit, $factor, $form->{"unit_type"});
3718
3719   $form->{"saved_message"} = $locale->text("The unit has been saved.");
3720
3721   edit_units();
3722
3723   $lxdebug->leave_sub();
3724 }
3725
3726 sub save_unit {
3727   $lxdebug->enter_sub();
3728
3729   $old_units = AM->retrieve_units(\%myconfig, $form, $form->{"unit_type"}, "resolved_");
3730   AM->units_in_use(\%myconfig, $form, $old_units);
3731
3732   $new_units = {};
3733   @delete_units = ();
3734   foreach $i (1..($form->{"rowcount"} * 1)) {
3735     $old_unit = $old_units->{$form->{"old_name_$i"}};
3736     if (!$old_unit) {
3737       $form->show_generic_error(sprintf($locale->text("The unit in row %d has been deleted in the meantime."), $i));
3738     }
3739
3740     if ($form->{"unchangeable_$i"}) {
3741       $new_units->{$form->{"old_name_$i"}} = $old_units->{$form->{"old_name_$i"}};
3742       $new_units->{$form->{"old_name_$i"}}->{"unchanged_unit"} = 1;
3743       next;
3744     }
3745
3746     if ($old_unit->{"in_use"}) {
3747       $form->show_generic_error(sprintf($locale->text("The unit in row %d has been used in the meantime and cannot be changed anymore."), $i));
3748     }
3749
3750     if ($form->{"delete_$i"}) {
3751       push(@delete_units, $old_unit->{"name"});
3752       next;
3753     }
3754
3755     $form->isblank("name_$i", sprintf($locale->text("The name is missing in row %d."), $i));
3756
3757     $form->show_generic_error(sprintf($locale->text("The name in row %d has already been used before."), $i)) if ($new_units->{$form->{"name_$i"}});
3758     my %h = map({ $_ => $form->{"${_}_$i"} } qw(name base_unit factor old_name));
3759     $new_units->{$form->{"name_$i"}} = \%h;
3760     $new_units->{$form->{"name_$i"}}->{"row"} = $i;
3761   }
3762
3763   foreach $unit (values(%{$new_units})) {
3764     next unless ($unit->{"old_name"});
3765     if ($unit->{"base_unit"}) {
3766       $form->show_generic_error(sprintf($locale->text("The base unit does not exist or it is about to be deleted in row %d."), $unit->{"row"}))
3767         unless (defined($new_units->{$unit->{"base_unit"}}));
3768       $unit->{"factor"} = $form->parse_amount(\%myconfig, $unit->{"factor"});
3769       $form->show_generic_error(sprintf($locale->text("The factor is missing in row %d."), $unit->{"row"})) unless ($unit->{"factor"} >= 1.0);
3770     } else {
3771       $unit->{"base_unit"} = undef;
3772       $unit->{"factor"} = undef;
3773     }
3774   }
3775
3776   foreach $unit (values(%{$new_units})) {
3777     next if ($unit->{"unchanged_unit"});
3778
3779     map({ $_->{"seen"} = 0; } values(%{$new_units}));
3780     $new_unit = $unit;
3781     while ($new_unit->{"base_unit"}) {
3782       $new_unit->{"seen"} = 1;
3783       $new_unit = $new_units->{$new_unit->{"base_unit"}};
3784       if ($new_unit->{"seen"}) {
3785         $form->show_generic_error(sprintf($locale->text("The base unit relations must not contain loops (e.g. by saying that unit A's base unit is B, " .
3786                                                         "B's base unit is C and C's base unit is A) in row %d."), $unit->{"row"}));
3787       }
3788     }
3789   }
3790
3791   AM->save_units(\%myconfig, $form, $form->{"unit_type"}, $new_units, \@delete_units);
3792
3793   $form->{"saved_message"} = $locale->text("The units have been saved.");
3794
3795   edit_units();
3796
3797   $lxdebug->leave_sub();
3798 }