d6ccfe2dfac35cda3e8dd42393d1444a0c1c2c77
[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   $form->{"inventory_accno_id"} = $form->{"std_inventory_accno_id"};
1784   for (my $i = 0; 4 > $i; $i++) {
1785     map({ $form->{"${_}_accno_id_$i"} = $form->{"std_${_}_accno_id"}; }
1786         qw(income expense));
1787   }
1788
1789   &buchungsgruppe_header;
1790   &form_footer;
1791
1792   $lxdebug->leave_sub();
1793 }
1794
1795 sub edit_buchungsgruppe {
1796   $lxdebug->enter_sub();
1797
1798   $form->{title} = "Edit";
1799
1800   AM->get_buchungsgruppe(\%myconfig, \%$form);
1801
1802   &buchungsgruppe_header;
1803
1804   &form_footer;
1805
1806   $lxdebug->leave_sub();
1807 }
1808
1809 sub list_buchungsgruppe {
1810   $lxdebug->enter_sub();
1811
1812   AM->buchungsgruppe(\%myconfig, \%$form);
1813
1814   $form->{callback} =
1815     "$form->{script}?action=list_buchungsgruppe&path=$form->{path}&login=$form->{login}&password=$form->{password}";
1816
1817   $callback = $form->escape($form->{callback});
1818
1819   $form->{title} = $locale->text('Buchungsgruppen');
1820
1821   @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 );
1822
1823   $column_header{description} =
1824       qq|<th class=listheading width=60%>|
1825     . $locale->text('Description')
1826     . qq|</th>|;
1827   $column_header{inventory_accno} =
1828       qq|<th class=listheading width=10%>|
1829     . $locale->text('Bestandskonto')
1830     . qq|</th>|;
1831   $column_header{income_accno_0} =
1832       qq|<th class=listheading>|
1833     . $locale->text('Erlöse Inland')
1834     . qq|</th>|;
1835   $column_header{expense_accno_0} =
1836       qq|<th class=listheading>|
1837     . $locale->text('Aufwand Inland')
1838     . qq|</th>|;
1839   $column_header{income_accno_1} =
1840       qq|<th class=listheading>|
1841     . $locale->text('Erlöse EU m. UStId')
1842     . qq|</th>|;
1843   $column_header{expense_accno_1} =
1844       qq|<th class=listheading>|
1845     . $locale->text('Aufwand EU m. UStId')
1846     . qq|</th>|;
1847   $column_header{income_accno_2} =
1848       qq|<th class=listheading>|
1849     . $locale->text('Erlöse EU o. UStId')
1850     . qq|</th>|;
1851   $column_header{expense_accno_2} =
1852       qq|<th class=listheading>|
1853     . $locale->text('Aufwand EU o. UStId')
1854     . qq|</th>|;
1855   $column_header{income_accno_3} =
1856       qq|<th class=listheading>|
1857     . $locale->text('Erlöse Ausland')
1858     . qq|</th>|;
1859   $column_header{expense_accno_3} =
1860       qq|<th class=listheading>|
1861     . $locale->text('Aufwand Ausland')
1862     . qq|</th>|;
1863   $form->header;
1864
1865   print qq|
1866 <body>
1867
1868 <table width=100%>
1869   <tr>
1870     <th class=listtop>$form->{title}</th>
1871   </tr>
1872   <tr height="5"></tr>
1873   <tr>
1874     <td>
1875       <table width=100%>
1876         <tr class=listheading>
1877 |;
1878
1879   map { print "$column_header{$_}\n" } @column_index;
1880
1881   print qq|
1882         </tr>
1883 |;
1884
1885   foreach $ref (@{ $form->{ALL} }) {
1886
1887     $i++;
1888     $i %= 2;
1889
1890     print qq|
1891         <tr valign=top class=listrow$i>
1892 |;
1893
1894
1895     $column_data{description} =
1896       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>|;
1897     $column_data{inventory_accno}           = qq|<td align=right>$ref->{inventory_accno}</td>|;
1898     $column_data{income_accno_0} =
1899       qq|<td align=right>$ref->{income_accno_0}</td>|;
1900     $column_data{expense_accno_0}           = qq|<td align=right>$ref->{expense_accno_0}</td>|;
1901     $column_data{income_accno_1} =
1902       qq|<td align=right>$ref->{income_accno_1}</td>|;
1903     $column_data{expense_accno_1}           = qq|<td align=right>$ref->{expense_accno_1}</td>|;
1904     $column_data{income_accno_2} =
1905       qq|<td align=right>$ref->{income_accno_2}</td>|;
1906     $column_data{expense_accno_2}           = qq|<td align=right>$ref->{expense_accno_2}</td>|;
1907     $column_data{income_accno_3} =
1908       qq|<td align=right>$ref->{income_accno_3}</td>|;
1909     $column_data{expense_accno_3}           = qq|<td align=right>$ref->{expense_accno_3}</td>|;
1910
1911     map { print "$column_data{$_}\n" } @column_index;
1912
1913     print qq|
1914         </tr>
1915 |;
1916   }
1917
1918   print qq|
1919       </table>
1920     </td>
1921   </tr>
1922   <tr>
1923   <td><hr size=3 noshade></td>
1924   </tr>
1925 </table>
1926
1927 <br>
1928 <form method=post action=$form->{script}>
1929
1930 <input name=callback type=hidden value="$form->{callback}">
1931
1932 <input type=hidden name=type value=buchungsgruppe>
1933
1934 <input type=hidden name=path value=$form->{path}>
1935 <input type=hidden name=login value=$form->{login}>
1936 <input type=hidden name=password value=$form->{password}>
1937
1938 <input class=submit type=submit name=action value="|
1939     . $locale->text('Add') . qq|">|;
1940
1941   if ($form->{menubar}) {
1942     require "$form->{path}/menu.pl";
1943     &menubar;
1944   }
1945
1946   print qq|
1947
1948   </form>
1949
1950   </body>
1951   </html>
1952 |;
1953
1954   $lxdebug->leave_sub();
1955 }
1956
1957 sub buchungsgruppe_header {
1958   $lxdebug->enter_sub();
1959
1960   $form->{title}    = $locale->text("$form->{title} Buchungsgruppe");
1961
1962   # $locale->text('Buchungsgruppe hinzufügen')
1963   # $locale->text('Buchungsgruppe bearbeiten')
1964
1965   my ($acc_inventory, $acc_income, $acc_expense) = ({}, {}, {});
1966   my %acc_type_map = (
1967     "IC" => $acc_inventory,
1968     "IC_income" => $acc_income,
1969     "IC_sale" => $acc_income,
1970     "IC_expense" => $acc_expense,
1971     "IC_cogs" => $acc_expense,
1972     );
1973
1974   foreach $key (keys(%acc_type_map)) {
1975     foreach $ref (@{ $form->{IC_links}{$key} }) {
1976       $acc_type_map{$key}->{$ref->{"id"}} = $ref;
1977     }
1978   }
1979
1980   foreach my $type (qw(IC IC_income IC_expense)) {
1981     $form->{"select$type"} =
1982       join("",
1983            map({ "<option value=$_->{id} $_->{selected}>" .
1984                    "$_->{accno}--" . H($_->{description}) . "</option>" }
1985                sort({ $a->{"accno"} cmp $b->{"accno"} }
1986                     values(%{$acc_type_map{$type}}))));
1987   }
1988
1989   if ($form->{id}) {
1990     $form->{selectIC} =~ s/selected//g;
1991     $form->{selectIC} =~ s/ value=$form->{inventory_accno_id}/  value=$form->{inventory_accno_id} selected/;
1992     $form->{selectIC_income} =~ s/selected//g;
1993     $form->{selectIC_income} =~ s/ value=$form->{income_accno_id_0}/  value=$form->{income_accno_id_0} selected/;
1994     $form->{selectIC_expense} =~ s/selected//g;
1995     $form->{selectIC_expense} =~ s/ value=$form->{expense_accno_id_0}/  value=$form->{expense_accno_id_0} selected/;
1996   }
1997
1998   if (!$eur) {
1999     $linkaccounts = qq|
2000                <tr>
2001                 <th align=right>| . $locale->text('Inventory') . qq|</th>
2002                 <td><select name=inventory_accno_id>$form->{selectIC}</select></td>
2003                 <input name=selectIC type=hidden value="$form->{selectIC}">
2004               </tr>|;
2005   } else {
2006     $linkaccounts = qq|
2007                 <input type=hidden name=inventory_accno_id value=$form->{inventory_accno_id}>|;
2008   }
2009
2010
2011   $linkaccounts .= qq|
2012               <tr>
2013                 <th align=right>| . $locale->text('Erlöse Inland') . qq|</th>
2014                 <td><select name=income_accno_id_0>$form->{selectIC_income}</select></td>
2015               </tr>
2016               <tr>
2017                 <th align=right>| . $locale->text('Aufwand Inland') . qq|</th>
2018                 <td><select name=expense_accno_id_0>$form->{selectIC_expense}</select></td>
2019               </tr>|;
2020   if ($form->{id}) {
2021     $form->{selectIC_income} =~ s/selected//g;
2022     $form->{selectIC_income} =~ s/ value=$form->{income_accno_id_1}/  value=$form->{income_accno_id_1} selected/;
2023     $form->{selectIC_expense} =~ s/selected//g;
2024     $form->{selectIC_expense} =~ s/ value=$form->{expense_accno_id_1}/  value=$form->{expense_accno_id_1} selected/;
2025   }
2026   $linkaccounts .= qq|        <tr>
2027                 <th align=right>| . $locale->text('Erlöse EU m. UStId') . qq|</th>
2028                 <td><select name=income_accno_id_1>$form->{selectIC_income}</select></td>
2029               </tr>
2030               <tr>
2031                 <th align=right>| . $locale->text('Aufwand EU m UStId') . qq|</th>
2032                 <td><select name=expense_accno_id_1>$form->{selectIC_expense}</select></td>
2033               </tr>|;
2034
2035   if ($form->{id}) {
2036     $form->{selectIC_income} =~ s/selected//g;
2037     $form->{selectIC_income} =~ s/ value=$form->{income_accno_id_2}/  value=$form->{income_accno_id_2} selected/;
2038     $form->{selectIC_expense} =~ s/selected//g;
2039     $form->{selectIC_expense} =~ s/ value=$form->{expense_accno_id_2}/  value=$form->{expense_accno_id_2} selected/;
2040   }
2041
2042   $linkaccounts .= qq|        <tr>
2043                 <th align=right>| . $locale->text('Erlöse EU o. UStId') . qq|</th>
2044                 <td><select name=income_accno_id_2>$form->{selectIC_income}</select></td>
2045               </tr>
2046               <tr>
2047                 <th align=right>| . $locale->text('Aufwand EU o. UStId') . qq|</th>
2048                 <td><select name=expense_accno_id_2>$form->{selectIC_expense}</select></td>
2049               </tr>|;
2050
2051   if ($form->{id}) {
2052     $form->{selectIC_income} =~ s/selected//g;
2053     $form->{selectIC_income} =~ s/ value=$form->{income_accno_id_3}/  value=$form->{income_accno_id_3} selected/;
2054     $form->{selectIC_expense} =~ s/selected//g;
2055     $form->{selectIC_expense} =~ s/ value=$form->{expense_accno_id_3}/  value=$form->{expense_accno_id_3} selected/;
2056   }
2057
2058   $linkaccounts .= qq|        <tr>
2059                 <th align=right>| . $locale->text('Erlöse Ausland') . qq|</th>
2060                 <td><select name=income_accno_id_3>$form->{selectIC_income}</select></td>
2061               </tr>
2062               <tr>
2063                 <th align=right>| . $locale->text('Aufwand Ausland') . qq|</th>
2064                 <td><select name=expense_accno_id_3>$form->{selectIC_expense}</select></td>
2065               </tr>
2066 |;
2067
2068
2069   $form->header;
2070
2071   print qq|
2072 <body>
2073
2074 <form method=post action=$form->{script}>
2075
2076 <input type=hidden name=id value=$form->{id}>
2077 <input type=hidden name=type value=buchungsgruppe>
2078
2079 <table width=100%>
2080   <tr>
2081     <th class=listtop colspan=2>$form->{title}</th>
2082   </tr>
2083   <tr height="5"></tr>
2084   <tr>
2085     <th align=right>| . $locale->text('Buchungsgruppe') . qq|</th>
2086     <td><input name=description size=30 value="| . $form->quote($form->{description}) . qq|"></td>
2087   <tr>
2088   $linkaccounts
2089   <td colspan=2><hr size=3 noshade></td>
2090   </tr>
2091 </table>
2092 |;
2093
2094   $lxdebug->leave_sub();
2095 }
2096
2097 sub save_buchungsgruppe {
2098   $lxdebug->enter_sub();
2099
2100   $form->isblank("description", $locale->text('Description missing!'));
2101
2102   AM->save_buchungsgruppe(\%myconfig, \%$form);
2103   $form->redirect($locale->text('Buchungsgruppe gespeichert!'));
2104
2105   $lxdebug->leave_sub();
2106 }
2107
2108 sub delete_buchungsgruppe {
2109   $lxdebug->enter_sub();
2110
2111   AM->delete_buchungsgruppe(\%myconfig, \%$form);
2112   $form->redirect($locale->text('Buchungsgruppe gelöscht!'));
2113
2114   $lxdebug->leave_sub();
2115 }
2116
2117
2118 sub add_printer {
2119   $lxdebug->enter_sub();
2120
2121   $form->{title} = "Add";
2122
2123   $form->{callback} =
2124     "$form->{script}?action=add_printer&path=$form->{path}&login=$form->{login}&password=$form->{password}"
2125     unless $form->{callback};
2126
2127   &printer_header;
2128   &form_footer;
2129
2130   $lxdebug->leave_sub();
2131 }
2132
2133 sub edit_printer {
2134   $lxdebug->enter_sub();
2135
2136   $form->{title} = "Edit";
2137
2138   AM->get_printer(\%myconfig, \%$form);
2139
2140   &printer_header;
2141
2142   $form->{orphaned} = 1;
2143   &form_footer;
2144
2145   $lxdebug->leave_sub();
2146 }
2147
2148 sub list_printer {
2149   $lxdebug->enter_sub();
2150
2151   AM->printer(\%myconfig, \%$form);
2152
2153   $form->{callback} =
2154     "$form->{script}?action=list_printer&path=$form->{path}&login=$form->{login}&password=$form->{password}";
2155
2156   $callback = $form->escape($form->{callback});
2157
2158   $form->{title} = $locale->text('Printer');
2159
2160   @column_index = qw(printer_description printer_command template_code);
2161
2162   $column_header{printer_description} =
2163       qq|<th class=listheading width=60%>|
2164     . $locale->text('Printer Description')
2165     . qq|</th>|;
2166   $column_header{printer_command} =
2167       qq|<th class=listheading width=10%>|
2168     . $locale->text('Printer Command')
2169     . qq|</th>|;
2170   $column_header{template_code} =
2171       qq|<th class=listheading>|
2172     . $locale->text('Template Code')
2173     . qq|</th>|;
2174
2175   $form->header;
2176
2177   print qq|
2178 <body>
2179
2180 <table width=100%>
2181   <tr>
2182     <th class=listtop>$form->{title}</th>
2183   </tr>
2184   <tr height="5"></tr>
2185   <tr>
2186     <td>
2187       <table width=100%>
2188         <tr class=listheading>
2189 |;
2190
2191   map { print "$column_header{$_}\n" } @column_index;
2192
2193   print qq|
2194         </tr>
2195 |;
2196
2197   foreach $ref (@{ $form->{ALL} }) {
2198
2199     $i++;
2200     $i %= 2;
2201
2202     print qq|
2203         <tr valign=top class=listrow$i>
2204 |;
2205
2206
2207     $column_data{printer_description} =
2208       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>|;
2209     $column_data{printer_command}           = qq|<td align=right>$ref->{printer_command}</td>|;
2210     $column_data{template_code} =
2211       qq|<td align=right>$ref->{template_code}</td>|;
2212
2213     map { print "$column_data{$_}\n" } @column_index;
2214
2215     print qq|
2216         </tr>
2217 |;
2218   }
2219
2220   print qq|
2221       </table>
2222     </td>
2223   </tr>
2224   <tr>
2225   <td><hr size=3 noshade></td>
2226   </tr>
2227 </table>
2228
2229 <br>
2230 <form method=post action=$form->{script}>
2231
2232 <input name=callback type=hidden value="$form->{callback}">
2233
2234 <input type=hidden name=type value=printer>
2235
2236 <input type=hidden name=path value=$form->{path}>
2237 <input type=hidden name=login value=$form->{login}>
2238 <input type=hidden name=password value=$form->{password}>
2239
2240 <input class=submit type=submit name=action value="|
2241     . $locale->text('Add') . qq|">|;
2242
2243   if ($form->{menubar}) {
2244     require "$form->{path}/menu.pl";
2245     &menubar;
2246   }
2247
2248   print qq|
2249
2250   </form>
2251
2252   </body>
2253   </html>
2254 |;
2255
2256   $lxdebug->leave_sub();
2257 }
2258
2259 sub printer_header {
2260   $lxdebug->enter_sub();
2261
2262   $form->{title}    = $locale->text("$form->{title} Printer");
2263
2264   # $locale->text('Add Printer')
2265   # $locale->text('Edit Printer')
2266
2267   $form->{printer_description} =~ s/\"/&quot;/g;
2268   $form->{template_code} =~ s/\"/&quot;/g;
2269   $form->{printer_command} =~ s/\"/&quot;/g;
2270
2271
2272   $form->header;
2273
2274   print qq|
2275 <body>
2276
2277 <form method=post action=$form->{script}>
2278
2279 <input type=hidden name=id value=$form->{id}>
2280 <input type=hidden name=type value=printer>
2281
2282 <table width=100%>
2283   <tr>
2284     <th class=listtop colspan=2>$form->{title}</th>
2285   </tr>
2286   <tr height="5"></tr>
2287   <tr>
2288     <th align=right>| . $locale->text('Printer') . qq|</th>
2289     <td><input name=printer_description size=30 value="$form->{printer_description}"></td>
2290   <tr>
2291   <tr>
2292     <th align=right>| . $locale->text('Printer Command') . qq|</th>
2293     <td><input name=printer_command size=30 value="$form->{printer_command}"></td>
2294   </tr>
2295   <tr>
2296     <th align=right>| . $locale->text('Template Code') . qq|</th>
2297     <td><input name=template_code size=5 value="$form->{template_code}"></td>
2298   </tr>
2299   <td colspan=2><hr size=3 noshade></td>
2300   </tr>
2301 </table>
2302 |;
2303
2304   $lxdebug->leave_sub();
2305 }
2306
2307 sub save_printer {
2308   $lxdebug->enter_sub();
2309
2310   $form->isblank("printer_description", $locale->text('Description missing!'));
2311   $form->isblank("printer_command", $locale->text('Printer Command missing!'));
2312   AM->save_printer(\%myconfig, \%$form);
2313   $form->redirect($locale->text('Printer saved!'));
2314
2315   $lxdebug->leave_sub();
2316 }
2317
2318 sub delete_printer {
2319   $lxdebug->enter_sub();
2320
2321   AM->delete_printer(\%myconfig, \%$form);
2322   $form->redirect($locale->text('Printer deleted!'));
2323
2324   $lxdebug->leave_sub();
2325 }
2326
2327
2328 sub add_payment {
2329   $lxdebug->enter_sub();
2330
2331   $form->{title} = "Add";
2332
2333   $form->{callback} =
2334     "$form->{script}?action=add_payment&path=$form->{path}&login=$form->{login}&password=$form->{password}"
2335     unless $form->{callback};
2336
2337   $form->{terms_netto} = 0;
2338   $form->{terms_skonto} = 0;
2339   $form->{percent_skonto} = 0;
2340   &payment_header;
2341   &form_footer;
2342
2343   $lxdebug->leave_sub();
2344 }
2345
2346 sub edit_payment {
2347   $lxdebug->enter_sub();
2348
2349   $form->{title} = "Edit";
2350
2351   AM->get_payment(\%myconfig, \%$form);
2352
2353   &payment_header;
2354
2355   $form->{orphaned} = 1;
2356   &form_footer;
2357
2358   $lxdebug->leave_sub();
2359 }
2360
2361 sub list_payment {
2362   $lxdebug->enter_sub();
2363
2364   AM->payment(\%myconfig, \%$form);
2365
2366   $form->{callback} =
2367     "$form->{script}?action=list_payment&path=$form->{path}&login=$form->{login}&password=$form->{password}";
2368
2369   $callback = $form->escape($form->{callback});
2370
2371   $form->{title} = $locale->text('Payment Terms');
2372
2373   @column_index = qw(description description_long terms_netto terms_skonto percent_skonto);
2374
2375   $column_header{description} =
2376       qq|<th class=listheading>|
2377     . $locale->text('Description')
2378     . qq|</th>|;
2379   $column_header{description_long} =
2380       qq|<th class=listheading>|
2381     . $locale->text('Long Description')
2382     . qq|</th>|;
2383   $column_header{terms_netto} =
2384       qq|<th class=listheading>|
2385     . $locale->text('Netto Terms')
2386     . qq|</th>|;
2387   $column_header{terms_skonto} =
2388       qq|<th class=listheading>|
2389     . $locale->text('Skonto Terms')
2390     . qq|</th>|;
2391   $column_header{percent_skonto} =
2392       qq|<th class=listheading>|
2393     . $locale->text('Skonto')
2394     . qq| %</th>|;
2395
2396   $form->header;
2397
2398   print qq|
2399 <body>
2400
2401 <table width=100%>
2402   <tr>
2403     <th class=listtop>$form->{title}</th>
2404   </tr>
2405   <tr height="5"></tr>
2406   <tr>
2407     <td>
2408       <table width=100%>
2409         <tr class=listheading>
2410 |;
2411
2412   map { print "$column_header{$_}\n" } @column_index;
2413
2414   print qq|
2415         </tr>
2416 |;
2417
2418   foreach $ref (@{ $form->{ALL} }) {
2419
2420     $i++;
2421     $i %= 2;
2422
2423     print qq|
2424         <tr valign=top class=listrow$i>
2425 |;
2426
2427
2428     $column_data{description} =
2429       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>|;
2430     $column_data{description_long}           = qq|<td align=right>$ref->{description_long}</td>|;
2431     $column_data{terms_netto} =
2432       qq|<td align=right>$ref->{terms_netto}</td>|;
2433     $column_data{terms_skonto} =
2434       qq|<td align=right>$ref->{terms_skonto}</td>|;
2435     $column_data{percent_skonto} =
2436       qq|<td align=right>$ref->{percent_skonto} %</td>|;
2437     map { print "$column_data{$_}\n" } @column_index;
2438
2439     print qq|
2440         </tr>
2441 |;
2442   }
2443
2444   print qq|
2445       </table>
2446     </td>
2447   </tr>
2448   <tr>
2449   <td><hr size=3 noshade></td>
2450   </tr>
2451 </table>
2452
2453 <br>
2454 <form method=post action=$form->{script}>
2455
2456 <input name=callback type=hidden value="$form->{callback}">
2457
2458 <input type=hidden name=type value=payment>
2459
2460 <input type=hidden name=path value=$form->{path}>
2461 <input type=hidden name=login value=$form->{login}>
2462 <input type=hidden name=password value=$form->{password}>
2463
2464 <input class=submit type=submit name=action value="|
2465     . $locale->text('Add') . qq|">|;
2466
2467   if ($form->{menubar}) {
2468     require "$form->{path}/menu.pl";
2469     &menubar;
2470   }
2471
2472   print qq|
2473
2474   </form>
2475
2476   </body>
2477   </html>
2478 |;
2479
2480   $lxdebug->leave_sub();
2481 }
2482
2483 sub payment_header {
2484   $lxdebug->enter_sub();
2485
2486   $form->{title}    = $locale->text("$form->{title} Payment Terms");
2487
2488   # $locale->text('Add Payment Terms')
2489   # $locale->text('Edit Payment Terms')
2490
2491   $form->{description} =~ s/\"/&quot;/g;
2492
2493
2494
2495   $form->header;
2496
2497   print qq|
2498 <body>
2499
2500 <form method=post action=$form->{script}>
2501
2502 <input type=hidden name=id value=$form->{id}>
2503 <input type=hidden name=type value=payment>
2504
2505 <table width=100%>
2506   <tr>
2507     <th class=listtop colspan=2>$form->{title}</th>
2508   </tr>
2509   <tr height="5"></tr>
2510   <tr>
2511     <th align=right>| . $locale->text('Description') . qq|</th>
2512     <td><input name=description size=30 value="$form->{description}"></td>
2513   <tr>
2514   <tr>
2515     <th align=right>| . $locale->text('Long Description') . qq|</th>
2516     <td><input name=description_long size=50 value="$form->{description_long}"></td>
2517   </tr>
2518   <tr>
2519     <th align=right>| . $locale->text('Netto Terms') . qq|</th>
2520     <td><input name=terms_netto size=10 value="$form->{terms_netto}"></td>
2521   </tr>
2522   <tr>
2523     <th align=right>| . $locale->text('Skonto Terms') . qq|</th>
2524     <td><input name=terms_skonto size=10 value="$form->{terms_skonto}"></td>
2525   </tr>  
2526   <tr>
2527     <th align=right>| . $locale->text('Skonto') . qq| %</th>
2528     <td><input name=percent_skonto size=10 value="$form->{percent_skonto}"></td>
2529   </tr> 
2530   <td colspan=2><hr size=3 noshade></td>
2531   </tr>
2532 </table>
2533 |;
2534
2535   $lxdebug->leave_sub();
2536 }
2537
2538 sub save_payment {
2539   $lxdebug->enter_sub();
2540
2541   $form->isblank("description", $locale->text('Language missing!'));
2542   AM->save_payment(\%myconfig, \%$form);
2543   $form->redirect($locale->text('Payment Terms saved!'));
2544
2545   $lxdebug->leave_sub();
2546 }
2547
2548 sub delete_payment {
2549   $lxdebug->enter_sub();
2550
2551   AM->delete_payment(\%myconfig, \%$form);
2552   $form->redirect($locale->text('Payment terms deleted!'));
2553
2554   $lxdebug->leave_sub();
2555 }
2556
2557 sub add_sic {
2558   $lxdebug->enter_sub();
2559
2560   $form->{title} = "Add";
2561
2562   $form->{callback} =
2563     "$form->{script}?action=add_sic&path=$form->{path}&login=$form->{login}&password=$form->{password}"
2564     unless $form->{callback};
2565
2566   &sic_header;
2567   &form_footer;
2568
2569   $lxdebug->leave_sub();
2570 }
2571
2572 sub edit_sic {
2573   $lxdebug->enter_sub();
2574
2575   $form->{title} = "Edit";
2576
2577   AM->get_sic(\%myconfig, \%$form);
2578
2579   &sic_header;
2580
2581   $form->{orphaned} = 1;
2582   &form_footer;
2583
2584   $lxdebug->leave_sub();
2585 }
2586
2587 sub list_sic {
2588   $lxdebug->enter_sub();
2589
2590   AM->sic(\%myconfig, \%$form);
2591
2592   $form->{callback} =
2593     "$form->{script}?action=list_sic&path=$form->{path}&login=$form->{login}&password=$form->{password}";
2594
2595   $callback = $form->escape($form->{callback});
2596
2597   $form->{title} = $locale->text('Standard Industrial Codes');
2598
2599   @column_index = qw(code description);
2600
2601   $column_header{code} =
2602     qq|<th class=listheading>| . $locale->text('Code') . qq|</th>|;
2603   $column_header{description} =
2604     qq|<th class=listheading>| . $locale->text('Description') . qq|</th>|;
2605
2606   $form->header;
2607
2608   print qq|
2609 <body>
2610
2611 <table width=100%>
2612   <tr>
2613     <th class=listtop>$form->{title}</th>
2614   </tr>
2615   <tr height="5"></tr>
2616   <tr>
2617     <td>
2618       <table width=100%>
2619         <tr class=listheading>
2620 |;
2621
2622   map { print "$column_header{$_}\n" } @column_index;
2623
2624   print qq|
2625         </tr>
2626 |;
2627
2628   foreach $ref (@{ $form->{ALL} }) {
2629
2630     $i++;
2631     $i %= 2;
2632
2633     if ($ref->{sictype} eq 'H') {
2634       print qq|
2635         <tr valign=top class=listheading>
2636 |;
2637       $column_data{code} =
2638         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>|;
2639       $column_data{description} = qq|<th>$ref->{description}</th>|;
2640
2641     } else {
2642       print qq|
2643         <tr valign=top class=listrow$i>
2644 |;
2645
2646       $column_data{code} =
2647         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>|;
2648       $column_data{description} = qq|<td>$ref->{description}</td>|;
2649
2650     }
2651
2652     map { print "$column_data{$_}\n" } @column_index;
2653
2654     print qq|
2655         </tr>
2656 |;
2657   }
2658
2659   print qq|
2660       </table>
2661     </td>
2662   </tr>
2663   <tr>
2664   <td><hr size=3 noshade></td>
2665   </tr>
2666 </table>
2667
2668 <br>
2669 <form method=post action=$form->{script}>
2670
2671 <input name=callback type=hidden value="$form->{callback}">
2672
2673 <input type=hidden name=type value=sic>
2674
2675 <input type=hidden name=path value=$form->{path}>
2676 <input type=hidden name=login value=$form->{login}>
2677 <input type=hidden name=password value=$form->{password}>
2678
2679 <input class=submit type=submit name=action value="|
2680     . $locale->text('Add') . qq|">|;
2681
2682   if ($form->{menubar}) {
2683     require "$form->{path}/menu.pl";
2684     &menubar;
2685   }
2686
2687   print qq|
2688   </form>
2689
2690   </body>
2691   </html>
2692 |;
2693
2694   $lxdebug->leave_sub();
2695 }
2696
2697 sub sic_header {
2698   $lxdebug->enter_sub();
2699
2700   $form->{title} = $locale->text("$form->{title} SIC");
2701
2702   # $locale->text('Add SIC')
2703   # $locale->text('Edit SIC')
2704
2705   $form->{code}        =~ s/\"/&quot;/g;
2706   $form->{description} =~ s/\"/&quot;/g;
2707
2708   $checked = ($form->{sictype} eq 'H') ? "checked" : "";
2709
2710   $form->header;
2711
2712   print qq|
2713 <body>
2714
2715 <form method=post action=$form->{script}>
2716
2717 <input type=hidden name=type value=sic>
2718 <input type=hidden name=id value=$form->{code}>
2719
2720 <table width=100%>
2721   <tr>
2722     <th class=listtop colspan=2>$form->{title}</th>
2723   </tr>
2724   <tr height="5"></tr>
2725   <tr>
2726     <th align=right>| . $locale->text('Code') . qq|</th>
2727     <td><input name=code size=10 value=$form->{code}></td>
2728   <tr>
2729   <tr>
2730     <td></td>
2731     <th align=left><input name=sictype type=checkbox style=checkbox value="H" $checked> |
2732     . $locale->text('Heading') . qq|</th>
2733   <tr>
2734   <tr>
2735     <th align=right>| . $locale->text('Description') . qq|</th>
2736     <td><input name=description size=60 value="$form->{description}"></td>
2737   </tr>
2738     <td colspan=2><hr size=3 noshade></td>
2739   </tr>
2740 </table>
2741 |;
2742
2743   $lxdebug->leave_sub();
2744 }
2745
2746 sub save_sic {
2747   $lxdebug->enter_sub();
2748
2749   $form->isblank("code",        $locale->text('Code missing!'));
2750   $form->isblank("description", $locale->text('Description missing!'));
2751   AM->save_sic(\%myconfig, \%$form);
2752   $form->redirect($locale->text('SIC saved!'));
2753
2754   $lxdebug->leave_sub();
2755 }
2756
2757 sub delete_sic {
2758   $lxdebug->enter_sub();
2759
2760   AM->delete_sic(\%myconfig, \%$form);
2761   $form->redirect($locale->text('SIC deleted!'));
2762
2763   $lxdebug->leave_sub();
2764 }
2765
2766 sub display_stylesheet {
2767   $lxdebug->enter_sub();
2768
2769   $form->{file} = "css/$myconfig{stylesheet}";
2770   &display_form;
2771
2772   $lxdebug->leave_sub();
2773 }
2774
2775 sub display_form {
2776   $lxdebug->enter_sub();
2777
2778   $form->{file} =~ s/^(.:)*?\/|\.\.\///g;
2779   $form->{file} =~ s/^\/*//g;
2780   $form->{file} =~ s/$userspath//;
2781
2782   $form->error("$!: $form->{file}") unless -f $form->{file};
2783
2784   AM->load_template(\%$form);
2785
2786   $form->{title} = $form->{file};
2787
2788   # if it is anything but html
2789   if ($form->{file} !~ /\.html$/) {
2790     $form->{body} = "<pre>\n$form->{body}\n</pre>";
2791   }
2792
2793   $form->header;
2794
2795   print qq|
2796 <body>
2797
2798 $form->{body}
2799
2800 <form method=post action=$form->{script}>
2801
2802 <input name=file type=hidden value=$form->{file}>
2803 <input name=type type=hidden value=template>
2804
2805 <input type=hidden name=path value=$form->{path}>
2806 <input type=hidden name=login value=$form->{login}>
2807 <input type=hidden name=password value=$form->{password}>
2808
2809 <input name=action type=submit class=submit value="|
2810     . $locale->text('Edit') . qq|">|;
2811
2812   if ($form->{menubar}) {
2813     require "$form->{path}/menu.pl";
2814     &menubar;
2815   }
2816
2817   print qq|
2818   </form>
2819
2820 </body>
2821 </html>
2822 |;
2823
2824   $lxdebug->leave_sub();
2825 }
2826
2827 sub edit_template {
2828   $lxdebug->enter_sub();
2829
2830   AM->load_template(\%$form);
2831
2832   $form->{title} = $locale->text('Edit Template');
2833
2834   # convert &nbsp to &amp;nbsp;
2835   $form->{body} =~ s/&nbsp;/&amp;nbsp;/gi;
2836
2837   $form->header;
2838
2839   print qq|
2840 <body>
2841
2842 <form method=post action=$form->{script}>
2843
2844 <input name=file type=hidden value=$form->{file}>
2845 <input name=type type=hidden value=template>
2846
2847 <input type=hidden name=path value=$form->{path}>
2848 <input type=hidden name=login value=$form->{login}>
2849 <input type=hidden name=password value=$form->{password}>
2850
2851 <input name=callback type=hidden value="$form->{script}?action=display_form&file=$form->{file}&path=$form->{path}&login=$form->{login}&password=$form->{password}">
2852
2853 <textarea name=body rows=25 cols=70>
2854 $form->{body}
2855 </textarea>
2856
2857 <br>
2858 <input type=submit class=submit name=action value="|
2859     . $locale->text('Save') . qq|">|;
2860
2861   if ($form->{menubar}) {
2862     require "$form->{path}/menu.pl";
2863     &menubar;
2864   }
2865
2866   print q|
2867   </form>
2868
2869
2870 </body>
2871 </html>
2872 |;
2873
2874   $lxdebug->leave_sub();
2875 }
2876
2877 sub save_template {
2878   $lxdebug->enter_sub();
2879
2880   AM->save_template(\%$form);
2881   $form->redirect($locale->text('Template saved!'));
2882
2883   $lxdebug->leave_sub();
2884 }
2885
2886 sub config {
2887   $lxdebug->enter_sub();
2888
2889   # get defaults for account numbers and last numbers
2890   AM->defaultaccounts(\%myconfig, \%$form);
2891
2892   foreach $item (qw(mm-dd-yy mm/dd/yy dd-mm-yy dd/mm/yy dd.mm.yy yyyy-mm-dd)) {
2893     $dateformat .=
2894       ($item eq $myconfig{dateformat})
2895       ? "<option selected>$item\n"
2896       : "<option>$item\n";
2897   }
2898
2899   foreach $item (qw(1,000.00 1000.00 1.000,00 1000,00)) {
2900     $numberformat .=
2901       ($item eq $myconfig{numberformat})
2902       ? "<option selected>$item\n"
2903       : "<option>$item\n";
2904   }
2905
2906   foreach $item (qw(name company address signature)) {
2907     $myconfig{$item} =~ s/\"/&quot;/g;
2908   }
2909
2910   foreach $item (qw(address signature)) {
2911     $myconfig{$item} =~ s/\\n/\r\n/g;
2912   }
2913
2914   @formats = ();
2915   if ($opendocument_templates && $openofficeorg_writer_bin &&
2916       $xvfb_bin && (-x $openofficeorg_writer_bin) && (-x $xvfb_bin)) {
2917     push(@formats, { "name" => $locale->text("PDF (OpenDocument/OASIS)"),
2918                      "value" => "opendocument_pdf" });
2919   }
2920   if ($latex_templates) {
2921     push(@formats, { "name" => $locale->text("PDF"), "value" => "pdf" });
2922   }
2923   push(@formats, { "name" => "HTML", "value" => "html" });
2924   if ($latex_templates) {
2925     push(@formats, { "name" => $locale->text("Postscript"),
2926                      "value" => "postscript" });
2927   }
2928   if ($opendocument_templates) {
2929     push(@formats, { "name" => $locale->text("OpenDocument/OASIS"),
2930                      "value" => "opendocument" });
2931   }
2932
2933   if (!$myconfig{"template_format"}) {
2934     $myconfig{"template_format"} = "pdf";
2935   }
2936   $template_format = "";
2937   foreach $item (@formats) {
2938     $template_format .=
2939       "<option value=\"$item->{value}\"" .
2940       ($item->{"value"} eq $myconfig{"template_format"} ?
2941        " selected" : "") .
2942        ">" . H($item->{"name"}) . "</option>";
2943   }
2944
2945   %countrycodes = User->country_codes;
2946   $countrycodes = '';
2947   foreach $key (sort { $countrycodes{$a} cmp $countrycodes{$b} }
2948                 keys %countrycodes
2949     ) {
2950     $countrycodes .=
2951       ($myconfig{countrycode} eq $key)
2952       ? "<option selected value=$key>$countrycodes{$key}\n"
2953       : "<option value=$key>$countrycodes{$key}\n";
2954   }
2955   $countrycodes = "<option>American English\n$countrycodes";
2956
2957   # use an other input number format than output numberformat
2958   # look at Form.pm, sub parse_amount
2959   my $in_numberformat = '';
2960   $text1 = qq|value="0">| . $locale->text('equal Outputformat');
2961   $text2 = qq|value="1">| . $locale->text('1000,00 or 1000.00');
2962   @in_nf = ($text1, $text2);
2963   foreach $item (@in_nf) {
2964     $in_numberformat .=
2965       (substr($item, 7, 1) eq $myconfig{in_numberformat})
2966       ? "<option selected $item\n"
2967       : "<option $item\n";
2968   }
2969
2970   foreach $key (keys %{ $form->{IC} }) {
2971     foreach $accno (sort keys %{ $form->{IC}{$key} }) {
2972       $myconfig{$key} .=
2973         ($form->{IC}{$key}{$accno}{id} == $form->{defaults}{$key})
2974         ? "<option selected>$accno--$form->{IC}{$key}{$accno}{description}\n"
2975         : "<option>$accno--$form->{IC}{$key}{$accno}{description}\n";
2976     }
2977   }
2978
2979   opendir CSS, "css/.";
2980   @all = grep /.*\.css$/, readdir CSS;
2981   closedir CSS;
2982
2983   foreach $item (@all) {
2984     if ($item eq $myconfig{stylesheet}) {
2985       $selectstylesheet .= qq|<option selected>$item\n|;
2986     } else {
2987       $selectstylesheet .= qq|<option>$item\n|;
2988     }
2989   }
2990   $selectstylesheet .= "<option>\n";
2991
2992   $form->{title} = $locale->text('Edit Preferences for') . qq| $form->{login}|;
2993
2994   $form->header;
2995
2996   if ($myconfig{menustyle} eq "old") {
2997     $menustyle_old = "checked";
2998   } elsif ($myconfig{menustyle} eq "neu") {
2999     $menustyle_neu = "checked";
3000   } elsif ($myconfig{menustyle} eq "v3") {
3001     $menustyle_v3 = "checked";
3002   }
3003
3004   my ($show_form_details, $hide_form_details);
3005   $myconfig{"show_form_details"} = 1
3006     unless (defined($myconfig{"show_form_details"}));
3007   $show_form_details = "checked" if ($myconfig{"show_form_details"});
3008   $hide_form_details = "checked" unless ($myconfig{"show_form_details"});
3009
3010   print qq|
3011 <body>
3012
3013 <form method=post action=$form->{script}>
3014
3015 <input type=hidden name=old_password value=$myconfig{password}>
3016 <input type=hidden name=type value=preferences>
3017 <input type=hidden name=role value=$myconfig{role}>
3018
3019 <table width=100%>
3020   <tr><th class=listtop>$form->{title}</th></tr>
3021   <tr>
3022     <td>
3023       <table>
3024         <tr>
3025           <th align=right>| . $locale->text('Name') . qq|</th>
3026           <td><input name=name size=15 value="$myconfig{name}"></td>
3027         </tr>
3028         <tr>
3029           <th align=right>| . $locale->text('Password') . qq|</th>
3030           <td><input type=password name=new_password size=10 value=$myconfig{password}></td>
3031         </tr>
3032         <tr>
3033           <th align=right>| . $locale->text('E-mail') . qq|</th>
3034           <td><input name=email size=30 value="$myconfig{email}"></td>
3035         </tr>
3036         <tr valign=top>
3037           <th align=right>| . $locale->text('Signature') . qq|</th>
3038           <td><textarea name=signature rows=3 cols=50>$myconfig{signature}</textarea></td>
3039         </tr>
3040         <tr>
3041           <th align=right>| . $locale->text('Phone') . qq|</th>
3042           <td><input name=tel size=14 value="$myconfig{tel}"></td>
3043         </tr>
3044         <tr>
3045           <th align=right>| . $locale->text('Fax') . qq|</th>
3046           <td><input name=fax size=14 value="$myconfig{fax}"></td>
3047         </tr>
3048         <tr>
3049           <th align=right>| . $locale->text('Company') . qq|</th>
3050           <td><input name=company size=30 value="$myconfig{company}"></td>
3051         </tr>
3052         <tr valign=top>
3053           <th align=right>| . $locale->text('Address') . qq|</th>
3054           <td><textarea name=address rows=4 cols=50>$myconfig{address}</textarea></td>
3055         </tr>
3056         <tr>
3057           <th align=right>| . $locale->text('Date Format') . qq|</th>
3058           <td><select name=dateformat>$dateformat</select></td>
3059         </tr>
3060         <tr>
3061           <th align=right>| . $locale->text('Output Number Format') . qq|</th>
3062           <td><select name=numberformat>$numberformat</select></td>
3063         </tr>
3064         <tr>
3065           <th align=right>| . $locale->text('Input Number Format') . qq|</th>
3066           <td><select name=in_numberformat>$in_numberformat</select></td>
3067         </tr>
3068
3069         <tr>
3070           <th align=right>| . $locale->text('Dropdown Limit') . qq|</th>
3071           <td><input name=vclimit size=10 value="$myconfig{vclimit}"></td>
3072         </tr>
3073         <tr>
3074           <th align=right>| . $locale->text('Language') . qq|</th>
3075           <td><select name=countrycode>$countrycodes</select></td>
3076         </tr>
3077         <tr>
3078           <th align=right>| . $locale->text('Stylesheet') . qq|</th>
3079           <td><select name=usestylesheet>$selectstylesheet</select></td>
3080         </tr>
3081         <tr>
3082           <th align=right>| . $locale->text('Setup Menu') . qq|</th>
3083           <td><input name=menustyle type=radio class=radio value=v3 $menustyle_v3>&nbsp;| .
3084     $locale->text("Top (CSS)") . qq|
3085           <input name=menustyle type=radio class=radio value=neu $menustyle_neu>&nbsp;| .
3086     $locale->text("Top (Javascript)") . qq|
3087     <input name=menustyle type=radio class=radio value=old $menustyle_old>&nbsp;| .
3088     $locale->text("Old (on the side)") . qq|</td>
3089   </tr>
3090   <tr>
3091     <th align=right>| . $locale->text('Form details (second row)') . qq|</th>
3092     <td><input type="radio" id="rad_show_form_details" name="show_form_details" value="1" $show_form_details>&nbsp;
3093     <label for="rad_show_form_details">| . $locale->text('Show by default') . qq|</label>
3094     <input type="radio" id="rad_hide_form_details" name="show_form_details" value="0" $hide_form_details>&nbsp;
3095     <label for="rad_hide_form_details">| . $locale->text('Hide by default') . qq|</label></td>
3096         </tr>
3097         <input name=printer type=hidden value="$myconfig{printer}">
3098         <tr class=listheading>
3099           <th colspan=2>| . $locale->text("Print options") . qq|</th>
3100         </tr>
3101         <tr>
3102           <th align=right>| . $locale->text('Default template format') . qq|</th>
3103           <td><select name="template_format">$template_format</select></td>
3104         </tr>
3105         <tr>
3106           <th align=right>| . $locale->text('Number of copies') . qq|</th>
3107           <td><input name="copies" size="10" value="| .
3108     $form->quote($myconfig{"copies"}) . qq|"></td>
3109         </tr>
3110
3111
3112         <tr class=listheading>
3113           <th colspan=2>&nbsp;</th>
3114         </tr>
3115         <tr>
3116           <th align=right>| . $locale->text('Business Number') . qq|</th>
3117           <td><input name=businessnumber size=25 value="$myconfig{businessnumber}"></td>
3118         </tr>
3119         <tr>
3120                 <th align=right>| . $locale->text('Year End') . qq| (mm/dd)</th>
3121                 <td><input name=yearend size=5 value=$form->{defaults}{yearend}></td>
3122         </tr>
3123         <tr class=listheading>
3124           <th colspan=2>|
3125     . $locale->text('Last Numbers & Default Accounts') . qq|</th>
3126         </tr>
3127         <tr>
3128           <td colspan=2>
3129             <table width=100%>
3130               <tr>
3131                 <th align=right nowrap>| . $locale->text('Inventory Account') . qq|</th>
3132                 <td><select name=inventory_accno>$myconfig{IC}</select></td>
3133               </tr>
3134               <tr>
3135                 <th align=right nowrap>| . $locale->text('Revenue Account') . qq|</th>
3136                 <td><select name=income_accno>$myconfig{IC_income}</select></td>
3137               </tr>
3138               <tr>
3139                 <th align=right nowrap>| . $locale->text('Expense Account') . qq|</th>
3140                 <td><select name=expense_accno>$myconfig{IC_expense}</select></td>
3141               </tr>
3142               <tr>
3143                 <th align=right nowrap>| . $locale->text('Foreign Exchange Gain') . qq|</th>
3144                 <td><select name=fxgain_accno>$myconfig{FX_gain}</select></td>
3145               </tr>
3146               <tr>
3147                 <th align=right nowrap>| . $locale->text('Foreign Exchange Loss') . qq|</th>
3148                 <td><select name=fxloss_accno>$myconfig{FX_loss}</select></td>
3149               </tr>
3150               <tr>
3151                 <td colspan=2>|
3152     . $locale->text(
3153     'Enter up to 3 letters separated by a colon (i.e CAD:USD:EUR) for your native and foreign currencies'
3154     )
3155     . qq|<br><input name=curr size=40 value="$form->{defaults}{curr}"></td>
3156               </tr>
3157             </table>
3158           </td>
3159          </tr>
3160          <tr>
3161            <td colspan=2>
3162              <table width=100%>
3163               <tr>
3164                 <th align=right nowrap>| . $locale->text('Last Invoice Number') . qq|</th>
3165                 <td><input name=invnumber size=10 value=$form->{defaults}{invnumber}></td>
3166                 <th align=right nowrap>|
3167     . $locale->text('Last Customer Number') . qq|</th>
3168                 <td><input name=customernumber size=10 value=$form->{defaults}{customernumber}></td>
3169               </tr>
3170               <tr>
3171                 <th align=right nowrap>|
3172     . $locale->text('Last Credit Note Number') . qq|</th>
3173                 <td><input name=cnnumber size=10 value=$form->{defaults}{cnnumber}></td>
3174                 <th align=right nowrap>|
3175     . $locale->text('Last Vendor Number') . qq|</th>
3176                 <td><input name=vendornumber size=10 value=$form->{defaults}{vendornumber}></td>
3177               </tr>
3178               <tr>
3179                 <th align=right nowrap>|
3180     . $locale->text('Last Sales Order Number') . qq|</th>
3181                 <td><input name=sonumber size=10 value=$form->{defaults}{sonumber}></td>
3182               </tr>
3183               <tr>
3184                 <th align=right nowrap>|
3185     . $locale->text('Last Purchase Order Number') . qq|</th>
3186                 <td><input name=ponumber size=10 value=$form->{defaults}{ponumber}></td>
3187                 <th align=right nowrap>|
3188     . $locale->text('Last Article Number') . qq|</th>
3189                 <td><input name=articlenumber size=10 value=$form->{defaults}{articlenumber}></td>
3190               </tr>
3191               <tr>
3192                 <th align=right nowrap>|
3193     . $locale->text('Last Sales Quotation Number') . qq|</th>
3194                 <td><input name=sqnumber size=10 value=$form->{defaults}{sqnumber}></td>
3195                 <th align=right nowrap>|
3196     . $locale->text('Last Service Number') . qq|</th>
3197                 <td><input name=servicenumber size=10 value=$form->{defaults}{servicenumber}></td>
3198               </tr>
3199               <tr>
3200                 <th align=right nowrap>| . $locale->text('Last RFQ Number') . qq|</th>
3201                 <td><input name=rfqnumber size=10 value=$form->{defaults}{rfqnumber}></td>
3202                 <th align=right nowrap></th>
3203                 <td></td>
3204               </tr>
3205             </table>
3206           </td>
3207         </tr>|;
3208 #       <tr class=listheading>
3209 #         <th colspan=2>| . $locale->text('Tax Accounts') . qq|</th>
3210 #       </tr>
3211 #       <tr>
3212 #         <td colspan=2>
3213 #           <table>
3214 #             <tr>
3215 #               <th>&nbsp;</th>
3216 #               <th>| . $locale->text('Rate') . qq| (%)</th>
3217 #               <th>| . $locale->text('Number') . qq|</th>
3218 #             </tr>
3219 # |;
3220
3221 #   foreach $accno (sort keys %{ $form->{taxrates} }) {
3222 #     print qq|
3223 #               <tr>
3224 #               <th align=right>$form->{taxrates}{$accno}{description}</th>
3225 #               <td><input name=$form->{taxrates}{$accno}{id} size=6 value=$form->{taxrates}{$accno}{rate}></td>
3226 #               <td><input name="taxnumber_$form->{taxrates}{$accno}{id}" value="$form->{taxrates}{$accno}{taxnumber}"></td>
3227 #             </tr>
3228 # |;
3229 #     $form->{taxaccounts} .= "$form->{taxrates}{$accno}{id} ";
3230 #   }
3231
3232 #   chop $form->{taxaccounts};
3233
3234 #   print qq|
3235 # <input name=taxaccounts type=hidden value="$form->{taxaccounts}">
3236
3237 #             </table>
3238 #         </td>
3239 #       </tr>
3240 print qq|      </table>
3241     </td>
3242   </tr>
3243   <tr>
3244     <td><hr size=3 noshade></td>
3245   </tr>
3246 </table>
3247
3248 <input type=hidden name=path value=$form->{path}>
3249 <input type=hidden name=login value=$form->{login}>
3250 <input type=hidden name=password value=$form->{password}>
3251
3252 <br>
3253 <input type=submit class=submit name=action value="|
3254     . $locale->text('Save') . qq|">|;
3255
3256   if ($form->{menubar}) {
3257     require "$form->{path}/menu.pl";
3258     &menubar;
3259   }
3260
3261   print qq|
3262   </form>
3263
3264 </body>
3265 </html>
3266 |;
3267
3268   $lxdebug->leave_sub();
3269 }
3270
3271 sub save_preferences {
3272   $lxdebug->enter_sub();
3273
3274   $form->{stylesheet} = $form->{usestylesheet};
3275
3276   $form->redirect($locale->text('Preferences saved!'))
3277     if (
3278      AM->save_preferences(\%myconfig, \%$form, $memberfile, $userspath, $webdav
3279      ));
3280   $form->error($locale->text('Cannot save preferences!'));
3281
3282   $lxdebug->leave_sub();
3283 }
3284
3285 sub backup {
3286   $lxdebug->enter_sub();
3287
3288   if ($form->{media} eq 'email') {
3289     $form->error($locale->text('No email address for') . " $myconfig{name}")
3290       unless ($myconfig{email});
3291
3292     $form->{OUT} = "$sendmail";
3293
3294   }
3295
3296   AM->backup(\%myconfig, \%$form, $userspath);
3297
3298   if ($form->{media} eq 'email') {
3299     $form->redirect($locale->text('Backup sent to') . qq| $myconfig{email}|);
3300   }
3301
3302   $lxdebug->leave_sub();
3303 }
3304
3305 sub audit_control {
3306   $lxdebug->enter_sub();
3307
3308   $form->{title} = $locale->text('Audit Control');
3309
3310   AM->closedto(\%myconfig, \%$form);
3311
3312   if ($form->{revtrans}) {
3313     $checked{Y} = "checked";
3314   } else {
3315     $checked{N} = "checked";
3316   }
3317
3318   $form->header;
3319
3320   print qq|
3321 <body>
3322
3323 <form method=post action=$form->{script}>
3324
3325 <input type=hidden name=path value=$form->{path}>
3326 <input type=hidden name=login value=$form->{login}>
3327 <input type=hidden name=password value=$form->{password}>
3328
3329 <table width=100%>
3330   <tr><th class=listtop>$form->{title}</th></tr>
3331   <tr height="5"></tr>
3332   <tr>
3333     <td>
3334       <table>
3335         <tr>
3336           <td>|
3337     . $locale->text('Enforce transaction reversal for all dates') . qq|</th>
3338           <td><input name=revtrans class=radio type=radio value="1" $checked{Y}> |
3339     . $locale->text('Yes')
3340     . qq| <input name=revtrans class=radio type=radio value="0" $checked{N}> |
3341     . $locale->text('No')
3342     . qq|</td>
3343         </tr>
3344         <tr>
3345           <th>| . $locale->text('Close Books up to') . qq|</th>
3346           <td><input name=closedto size=11 title="$myconfig{dateformat}" value=$form->{closedto}></td>
3347         </tr>
3348       </table>
3349     </td>
3350   </tr>
3351 </table>
3352
3353 <hr size=3 noshade>
3354
3355 <br>
3356 <input type=hidden name=nextsub value=doclose>
3357
3358 <input type=submit class=submit name=action value="|
3359     . $locale->text('Continue') . qq|">
3360
3361 </form>
3362
3363 </body>
3364 </html>
3365 |;
3366
3367   $lxdebug->leave_sub();
3368 }
3369
3370 sub doclose {
3371   $lxdebug->enter_sub();
3372
3373   AM->closebooks(\%myconfig, \%$form);
3374
3375   if ($form->{revtrans}) {
3376     $form->redirect(
3377                  $locale->text('Transaction reversal enforced for all dates'));
3378   } else {
3379     if ($form->{closedto}) {
3380       $form->redirect(
3381                      $locale->text('Transaction reversal enforced up to') . " "
3382                        . $locale->date(\%myconfig, $form->{closedto}, 1));
3383     } else {
3384       $form->redirect($locale->text('Books are open'));
3385     }
3386   }
3387
3388   $lxdebug->leave_sub();
3389 }
3390
3391 sub add_warehouse {
3392   $lxdebug->enter_sub();
3393
3394   $form->{title} = "Add";
3395
3396   $form->{callback} =
3397     "$form->{script}?action=add_warehouse&path=$form->{path}&login=$form->{login}&password=$form->{password}"
3398     unless $form->{callback};
3399
3400   &warehouse_header;
3401   &form_footer;
3402
3403   $lxdebug->leave_sub();
3404 }
3405
3406 sub edit_warehouse {
3407   $lxdebug->enter_sub();
3408
3409   $form->{title} = "Edit";
3410
3411   AM->get_warehouse(\%myconfig, \%$form);
3412
3413   &warehouse_header;
3414   &form_footer;
3415
3416   $lxdebug->leave_sub();
3417 }
3418
3419 sub list_warehouse {
3420   $lxdebug->enter_sub();
3421
3422   AM->warehouses(\%myconfig, \%$form);
3423
3424   $form->{callback} =
3425     "$form->{script}?action=list_warehouse&path=$form->{path}&login=$form->{login}&password=$form->{password}";
3426
3427   $callback = $form->escape($form->{callback});
3428
3429   $form->{title} = $locale->text('Warehouses');
3430
3431   @column_index = qw(description);
3432
3433   $column_header{description} =
3434       qq|<th class=listheading width=100%>|
3435     . $locale->text('Description')
3436     . qq|</th>|;
3437
3438   $form->header;
3439
3440   print qq|
3441 <body>
3442
3443 <table width=100%>
3444   <tr>
3445     <th class=listtop>$form->{title}</th>
3446   </tr>
3447   <tr height="5"></tr>
3448   <tr>
3449     <td>
3450       <table width=100%>
3451         <tr class=listheading>
3452 |;
3453
3454   map { print "$column_header{$_}\n" } @column_index;
3455
3456   print qq|
3457         </tr>
3458 |;
3459
3460   foreach $ref (@{ $form->{ALL} }) {
3461
3462     $i++;
3463     $i %= 2;
3464
3465     print qq|
3466         <tr valign=top class=listrow$i>
3467 |;
3468
3469     $column_data{description} =
3470       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>|;
3471
3472     map { print "$column_data{$_}\n" } @column_index;
3473
3474     print qq|
3475         </tr>
3476 |;
3477   }
3478
3479   print qq|
3480       </table>
3481     </td>
3482   </tr>
3483   <tr>
3484   <td><hr size=3 noshade></td>
3485   </tr>
3486 </table>
3487
3488 <br>
3489 <form method=post action=$form->{script}>
3490
3491 <input name=callback type=hidden value="$form->{callback}">
3492
3493 <input type=hidden name=type value=warehouse>
3494
3495 <input type=hidden name=path value=$form->{path}>
3496 <input type=hidden name=login value=$form->{login}>
3497 <input type=hidden name=password value=$form->{password}>
3498
3499 <input class=submit type=submit name=action value="|
3500     . $locale->text('Add') . qq|">|;
3501
3502   if ($form->{menubar}) {
3503     require "$form->{path}/menu.pl";
3504     &menubar;
3505   }
3506
3507   print qq|
3508   </form>
3509
3510   </body>
3511   </html>
3512 |;
3513
3514   $lxdebug->leave_sub();
3515 }
3516
3517 sub warehouse_header {
3518   $lxdebug->enter_sub();
3519
3520   $form->{title} = $locale->text("$form->{title} Warehouse");
3521
3522   # $locale->text('Add Warehouse')
3523   # $locale->text('Edit Warehouse')
3524
3525   $form->{description} =~ s/\"/&quot;/g;
3526
3527   if (($rows = $form->numtextrows($form->{description}, 60)) > 1) {
3528     $description =
3529       qq|<textarea name="description" rows=$rows cols=60 wrap=soft>$form->{description}</textarea>|;
3530   } else {
3531     $description =
3532       qq|<input name=description size=60 value="$form->{description}">|;
3533   }
3534
3535   $form->header;
3536
3537   print qq|
3538 <body>
3539
3540 <form method=post action=$form->{script}>
3541
3542 <input type=hidden name=id value=$form->{id}>
3543 <input type=hidden name=type value=warehouse>
3544
3545 <table width=100%>
3546   <tr>
3547     <th class=listtop colspan=2>$form->{title}</th>
3548   </tr>
3549   <tr height="5"></tr>
3550   <tr>
3551     <th align=right>| . $locale->text('Description') . qq|</th>
3552     <td>$description</td>
3553   </tr>
3554   <tr>
3555     <td colspan=2><hr size=3 noshade></td>
3556   </tr>
3557 </table>
3558 |;
3559
3560   $lxdebug->leave_sub();
3561 }
3562
3563 sub save_warehouse {
3564   $lxdebug->enter_sub();
3565
3566   $form->isblank("description", $locale->text('Description missing!'));
3567   AM->save_warehouse(\%myconfig, \%$form);
3568   $form->redirect($locale->text('Warehouse saved!'));
3569
3570   $lxdebug->leave_sub();
3571 }
3572
3573 sub delete_warehouse {
3574   $lxdebug->enter_sub();
3575
3576   AM->delete_warehouse(\%myconfig, \%$form);
3577   $form->redirect($locale->text('Warehouse deleted!'));
3578
3579   $lxdebug->leave_sub();
3580 }
3581
3582 sub continue {
3583   $lxdebug->enter_sub();
3584
3585   &{ $form->{nextsub} };
3586
3587   $lxdebug->leave_sub();
3588 }
3589
3590 sub edit_units {
3591   $lxdebug->enter_sub();
3592
3593   $units = AM->retrieve_units(\%myconfig, $form, $form->{"unit_type"}, "resolved_");
3594   AM->units_in_use(\%myconfig, $form, $units);
3595   map({ $units->{$_}->{"BASE_UNIT_DDBOX"} = AM->unit_select_data($units, $units->{$_}->{"base_unit"}, 1); } keys(%{$units}));
3596
3597   @languages = AM->language(\%myconfig, $form, 1);
3598
3599   @unit_list = ();
3600   foreach $name (sort({ lc($a) cmp lc($b) } grep({ !$units->{$_}->{"base_unit"} } keys(%{$units})))) {
3601     map({ push(@unit_list, $units->{$_}); }
3602         sort({ ($units->{$a}->{"resolved_factor"} * 1) <=> ($units->{$b}->{"resolved_factor"} * 1) }
3603              grep({ $units->{$_}->{"resolved_base_unit"} eq $name } keys(%{$units}))));
3604   }
3605   my $i = 1;
3606   foreach (@unit_list) {
3607     $_->{"factor"} = $form->format_amount(\%myconfig, $_->{"factor"}, 5) if ($_->{"factor"});
3608     $_->{"UNITLANGUAGES"} = [];
3609     foreach my $lang (@languages) {
3610       push(@{ $_->{"UNITLANGUAGES"} },
3611            { "idx" => $i,
3612              "unit" => $_->{"name"},
3613              "language_id" => $lang->{"id"},
3614              "localized" => $_->{"LANGUAGES"}->{$lang->{"template_code"}}->{"localized"},
3615              "localized_plural" => $_->{"LANGUAGES"}->{$lang->{"template_code"}}->{"localized_plural"},
3616            });
3617     }
3618     $i++;
3619   }
3620
3621   $units = AM->retrieve_units(\%myconfig, $form, $form->{"unit_type"});
3622   $ddbox = AM->unit_select_data($units, undef, 1);
3623
3624   $form->{"title"} = sprintf($locale->text("Add and edit %s"), $form->{"unit_type"} eq "dimension" ? $locale->text("dimension units") : $locale->text("service units"));
3625   $form->header();
3626   print($form->parse_html_template("am/edit_units",
3627                                    { "UNITS" => \@unit_list,
3628                                      "NEW_BASE_UNIT_DDBOX" => $ddbox,
3629                                      "LANGUAGES" => \@languages }));
3630
3631   $lxdebug->leave_sub();
3632 }
3633
3634 sub add_unit {
3635   $lxdebug->enter_sub();
3636
3637   $form->isblank("new_name", $locale->text("The name is missing."));
3638   $units = AM->retrieve_units(\%myconfig, $form, $form->{"unit_type"});
3639   $all_units = AM->retrieve_units(\%myconfig, $form);
3640   $form->show_generic_error($locale->text("A unit with this name does already exist.")) if ($all_units->{$form->{"new_name"}});
3641
3642   my ($base_unit, $factor);
3643   if ($form->{"new_base_unit"}) {
3644     $form->show_generic_error($locale->text("The base unit does not exist.")) unless (defined($units->{$form->{"new_base_unit"}}));
3645
3646     $form->isblank("new_factor", $locale->text("The factor is missing."));
3647     $factor = $form->parse_amount(\%myconfig, $form->{"new_factor"});
3648     $form->show_generic_error($locale->text("The factor is missing.")) unless ($factor);
3649     $base_unit = $form->{"new_base_unit"};
3650   }
3651
3652   my @languages;
3653   foreach my $lang (AM->language(\%myconfig, $form, 1)) {
3654     next unless ($form->{"new_localized_$lang->{id}"} || $form->{"new_localized_plural_$lang->{id}"});
3655     push(@languages, { "id" => $lang->{"id"},
3656                        "localized" => $form->{"new_localized_$lang->{id}"},
3657                        "localized_plural" => $form->{"new_localized_plural_$lang->{id}"},
3658          });
3659   }
3660
3661   AM->add_unit(\%myconfig, $form, $form->{"new_name"}, $base_unit, $factor, $form->{"unit_type"}, \@languages);
3662
3663   $form->{"saved_message"} = $locale->text("The unit has been saved.");
3664
3665   edit_units();
3666
3667   $lxdebug->leave_sub();
3668 }
3669
3670 sub set_unit_languages {
3671   $lxdebug->enter_sub();
3672
3673   my ($unit, $languages, $idx) = @_;
3674
3675   $unit->{"LANGUAGES"} = [];
3676
3677   foreach my $lang (@{$languages}) {
3678     push(@{ $unit->{"LANGUAGES"} },
3679          { "id" => $lang->{"id"},
3680            "localized" => $form->{"localized_${idx}_$lang->{id}"},
3681            "localized_plural" => $form->{"localized_plural_${idx}_$lang->{id}"},
3682          });
3683   }
3684
3685   $lxdebug->leave_sub();
3686 }
3687
3688 sub save_unit {
3689   $lxdebug->enter_sub();
3690
3691   $old_units = AM->retrieve_units(\%myconfig, $form, $form->{"unit_type"}, "resolved_");
3692   AM->units_in_use(\%myconfig, $form, $old_units);
3693
3694   @languages = AM->language(\%myconfig, $form, 1);
3695
3696   $new_units = {};
3697   @delete_units = ();
3698   foreach $i (1..($form->{"rowcount"} * 1)) {
3699     $old_unit = $old_units->{$form->{"old_name_$i"}};
3700     if (!$old_unit) {
3701       $form->show_generic_error(sprintf($locale->text("The unit in row %d has been deleted in the meantime."), $i));
3702     }
3703
3704     if ($form->{"unchangeable_$i"}) {
3705       $new_units->{$form->{"old_name_$i"}} = $old_units->{$form->{"old_name_$i"}};
3706       $new_units->{$form->{"old_name_$i"}}->{"unchanged_unit"} = 1;
3707       set_unit_languages($new_units->{$form->{"old_name_$i"}}, \@languages, $i);
3708       next;
3709     }
3710
3711     if ($old_unit->{"in_use"}) {
3712       $form->show_generic_error(sprintf($locale->text("The unit in row %d has been used in the meantime and cannot be changed anymore."), $i));
3713     }
3714
3715     if ($form->{"delete_$i"}) {
3716       push(@delete_units, $old_unit->{"name"});
3717       next;
3718     }
3719
3720     $form->isblank("name_$i", sprintf($locale->text("The name is missing in row %d."), $i));
3721
3722     $form->show_generic_error(sprintf($locale->text("The name in row %d has already been used before."), $i)) if ($new_units->{$form->{"name_$i"}});
3723     my %h = map({ $_ => $form->{"${_}_$i"} } qw(name base_unit factor old_name));
3724     $new_units->{$form->{"name_$i"}} = \%h;
3725     $new_units->{$form->{"name_$i"}}->{"row"} = $i;
3726     set_unit_languages($new_units->{$form->{"old_name_$i"}}, \@languages, $i);
3727   }
3728
3729   foreach $unit (values(%{$new_units})) {
3730     next unless ($unit->{"old_name"});
3731     if ($unit->{"base_unit"}) {
3732       $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"}))
3733         unless (defined($new_units->{$unit->{"base_unit"}}));
3734       $unit->{"factor"} = $form->parse_amount(\%myconfig, $unit->{"factor"});
3735       $form->show_generic_error(sprintf($locale->text("The factor is missing in row %d."), $unit->{"row"})) unless ($unit->{"factor"} >= 1.0);
3736     } else {
3737       $unit->{"base_unit"} = undef;
3738       $unit->{"factor"} = undef;
3739     }
3740   }
3741
3742   foreach $unit (values(%{$new_units})) {
3743     next if ($unit->{"unchanged_unit"});
3744
3745     map({ $_->{"seen"} = 0; } values(%{$new_units}));
3746     $new_unit = $unit;
3747     while ($new_unit->{"base_unit"}) {
3748       $new_unit->{"seen"} = 1;
3749       $new_unit = $new_units->{$new_unit->{"base_unit"}};
3750       if ($new_unit->{"seen"}) {
3751         $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, " .
3752                                                         "B's base unit is C and C's base unit is A) in row %d."), $unit->{"row"}));
3753       }
3754     }
3755   }
3756
3757   AM->save_units(\%myconfig, $form, $form->{"unit_type"}, $new_units, \@delete_units);
3758
3759   $form->{"saved_message"} = $locale->text("The units have been saved.");
3760
3761   edit_units();
3762
3763   $lxdebug->leave_sub();
3764 }