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