1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #======================================================================
31 # Administration module
36 #======================================================================
44 $main::lxdebug->enter_sub();
46 my ($self, $myconfig, $form) = @_;
49 my $dbh = $form->dbconnect($myconfig);
51 qq!SELECT c.accno, c.description, c.charttype, c.gifi_accno, c.category,! .
52 qq! c.link, c.pos_bilanz, c.pos_eur, c.new_chart_id, c.valid_from, ! .
54 qq! tk.taxkey_id, tk.pos_ustva, tk.tax_id, ! .
55 qq! tk.tax_id || '--' || tk.taxkey_id AS tax, tk.startdate ! .
57 qq!LEFT JOIN taxkeys tk ! .
58 qq!ON (c.id=tk.chart_id AND tk.id = ! .
59 qq! (SELECT id FROM taxkeys ! .
60 qq! WHERE taxkeys.chart_id = c.id AND startdate <= current_date ! .
61 qq! ORDER BY startdate DESC LIMIT 1)) ! .
64 my $sth = $dbh->prepare($query);
65 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
67 my $ref = $sth->fetchrow_hashref(NAME_lc);
69 foreach my $key (keys %$ref) {
70 $form->{"$key"} = $ref->{"$key"};
75 # get default accounts
76 $query = qq|SELECT inventory_accno_id, income_accno_id, expense_accno_id
78 $sth = $dbh->prepare($query);
79 $sth->execute || $form->dberror($query);
81 $ref = $sth->fetchrow_hashref(NAME_lc);
83 map { $form->{$_} = $ref->{$_} } keys %ref;
87 # get taxkeys and description
88 $query = qq§SELECT id, taxkey,id||'--'||taxkey AS tax, taxdescription
89 FROM tax ORDER BY taxkey§;
90 $sth = $dbh->prepare($query);
91 $sth->execute || $form->dberror($query);
95 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
96 push @{ $form->{TAXKEY} }, $ref;
102 $query = qq|SELECT id, accno,description
103 FROM chart WHERE link = ?|;
104 $sth = $dbh->prepare($query);
105 $sth->execute($form->{link}) || $form->dberror($query . " ($form->{link})");
107 $form->{NEWACCOUNT} = [];
108 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
109 push @{ $form->{NEWACCOUNT} }, $ref;
114 # check if we have any transactions
115 $query = qq|SELECT a.trans_id FROM acc_trans a
116 WHERE a.chart_id = ?|;
117 $sth = $dbh->prepare($query);
118 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
120 ($form->{orphaned}) = $sth->fetchrow_array;
121 $form->{orphaned} = !$form->{orphaned};
124 # check if new account is active
125 $form->{new_chart_valid} = 0;
126 if ($form->{new_chart_id}) {
127 $query = qq|SELECT current_date-valid_from FROM chart
129 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
131 $form->{new_chart_valid} = 1;
138 $main::lxdebug->leave_sub();
142 $main::lxdebug->enter_sub();
144 my ($self, $myconfig, $form) = @_;
146 # connect to database, turn off AutoCommit
147 my $dbh = $form->dbconnect_noauto($myconfig);
149 # sanity check, can't have AR with AR_...
150 if ($form->{AR} || $form->{AP} || $form->{IC}) {
151 map { delete $form->{$_} }
152 qw(AR_amount AR_tax AR_paid AP_amount AP_tax AP_paid IC_sale IC_cogs IC_taxpart IC_income IC_expense IC_taxservice CT_tax);
156 foreach my $item ($form->{AR}, $form->{AR_amount},
157 $form->{AR_tax}, $form->{AR_paid},
158 $form->{AP}, $form->{AP_amount},
159 $form->{AP_tax}, $form->{AP_paid},
160 $form->{IC}, $form->{IC_sale},
161 $form->{IC_cogs}, $form->{IC_taxpart},
162 $form->{IC_income}, $form->{IC_expense},
163 $form->{IC_taxservice}, $form->{CT_tax}
165 $form->{link} .= "${item}:" if ($item);
169 # strip blanks from accno
170 map { $form->{$_} =~ s/ //g; } qw(accno);
174 if ($form->{id} eq "NULL") {
180 my ($tax_id, $taxkey) = split(/--/, $form->{tax});
181 my $startdate = $form->{startdate} ? $form->{startdate} : "1970-01-01";
183 if ($form->{id} && $form->{orphaned}) {
184 $query = qq|UPDATE chart SET
185 accno = ?, description = ?, charttype = ?,
186 gifi_accno = ?, category = ?, link = ?,
188 pos_ustva = ?, pos_bwa = ?, pos_bilanz = ?,
189 pos_eur = ?, new_chart_id = ?, valid_from = ?
191 @values = ($form->{accno}, $form->{description}, $form->{charttype},
192 $form->{gifi_accno}, $form->{category}, $form->{link},
194 conv_i($form->{pos_ustva}), conv_i($form->{pos_bwa}),
195 conv_i($form->{pos_bilanz}), conv_i($form->{pos_eur}),
196 conv_i($form->{new_chart_id}),
197 conv_date($form->{valid_from}),
200 } elsif ($form->{id} && !$form->{new_chart_valid}) {
201 $query = qq|UPDATE chart SET new_chart_id = ?, valid_from = ?
203 @values = (conv_i($form->{new_chart_id}), conv_date($form->{valid_from}),
206 $query = qq|INSERT INTO chart
207 (accno, description, charttype,
208 gifi_accno, category, link,
210 pos_ustva, pos_bwa, pos_bilanz, pos_eur,
211 new_chart_id, valid_from)
212 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
213 @values = ($form->{accno}, $form->{description}, $form->{charttype},
214 $form->{gifi_accno}, $form->{category}, $form->{link},
216 conv_i($form->{pos_ustva}), conv_i($form->{pos_bwa}),
217 conv_i($form->{pos_bilanz}), conv_i($form->{pos_eur}),
218 conv_i($form->{new_chart_id}),
219 conv_date($form->{valid_from}));
222 do_query($form, $dbh, $query, @values);
227 qq|INSERT INTO taxkeys | .
228 qq|(chart_id, tax_id, taxkey_id, pos_ustva, startdate) | .
229 qq|VALUES ((SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?)|;
230 do_query($form, $dbh, $query,
231 $form->{accno}, conv_i($tax_id), conv_i($taxkey),
232 conv_i($form->{pos_ustva}), conv_date($startdate));
235 $query = qq|DELETE FROM taxkeys WHERE chart_id = ? AND tax_id = ?|;
236 do_query($form, $dbh, $query, $form->{id}, conv_i($tax_id));
239 qq|INSERT INTO taxkeys | .
240 qq|(chart_id, tax_id, taxkey_id, pos_ustva, startdate) | .
241 qq|VALUES (?, ?, ?, ?, ?)|;
242 do_query($form, $dbh, $query,
243 $form->{id}, conv_i($tax_id), conv_i($taxkey),
244 conv_i($form->{pos_ustva}), conv_date($startdate));
248 my $rc = $dbh->commit;
251 $main::lxdebug->leave_sub();
257 $main::lxdebug->enter_sub();
259 my ($self, $myconfig, $form) = @_;
261 # connect to database, turn off AutoCommit
262 my $dbh = $form->dbconnect_noauto($myconfig);
264 my $query = qq|SELECT count(*) FROM acc_trans a
265 WHERE a.chart_id = ?|;
266 my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
270 $main::lxdebug->leave_sub();
274 # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
275 foreach my $type (qw(inventory income expense)) {
278 qq|SET ${type}_accno_id = (SELECT ${type}_accno_id FROM defaults) | .
279 qq|WHERE ${type}_accno_id = ?|;
280 do_query($form, $dbh, $query, $form->{id});
283 foreach my $table (qw(partstax customertax vendortax tax)) {
284 $query = qq|DELETE FROM $table
286 do_query($form, $dbh, $query, $form->{id});
289 # delete chart of account record
290 $query = qq|DELETE FROM chart
292 do_query($form, $dbh, $query, $form->{id});
294 # commit and redirect
295 my $rc = $dbh->commit;
298 $main::lxdebug->leave_sub();
304 $main::lxdebug->enter_sub();
306 my ($self, $myconfig, $form) = @_;
308 # connect to database
309 my $dbh = $form->dbconnect($myconfig);
311 my $query = qq|SELECT d.id, d.description, d.role
315 $sth = $dbh->prepare($query);
316 $sth->execute || $form->dberror($query);
318 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
319 push @{ $form->{ALL} }, $ref;
325 $main::lxdebug->leave_sub();
329 $main::lxdebug->enter_sub();
331 my ($self, $myconfig, $form) = @_;
333 # connect to database
334 my $dbh = $form->dbconnect($myconfig);
336 my $query = qq|SELECT d.description, d.role
338 WHERE d.id = $form->{id}|;
339 my $sth = $dbh->prepare($query);
340 $sth->execute || $form->dberror($query);
342 my $ref = $sth->fetchrow_hashref(NAME_lc);
344 map { $form->{$_} = $ref->{$_} } keys %$ref;
348 # see if it is in use
349 $query = qq|SELECT count(*) FROM dpt_trans d
350 WHERE d.department_id = $form->{id}|;
351 $sth = $dbh->prepare($query);
352 $sth->execute || $form->dberror($query);
354 ($form->{orphaned}) = $sth->fetchrow_array;
355 $form->{orphaned} = !$form->{orphaned};
360 $main::lxdebug->leave_sub();
363 sub save_department {
364 $main::lxdebug->enter_sub();
366 my ($self, $myconfig, $form) = @_;
368 # connect to database
369 my $dbh = $form->dbconnect($myconfig);
371 $form->{description} =~ s/\'/\'\'/g;
374 $query = qq|UPDATE department SET
375 description = '$form->{description}',
376 role = '$form->{role}'
377 WHERE id = $form->{id}|;
379 $query = qq|INSERT INTO department
381 VALUES ('$form->{description}', '$form->{role}')|;
383 $dbh->do($query) || $form->dberror($query);
387 $main::lxdebug->leave_sub();
390 sub delete_department {
391 $main::lxdebug->enter_sub();
393 my ($self, $myconfig, $form) = @_;
395 # connect to database
396 my $dbh = $form->dbconnect($myconfig);
398 $query = qq|DELETE FROM department
399 WHERE id = $form->{id}|;
400 $dbh->do($query) || $form->dberror($query);
404 $main::lxdebug->leave_sub();
408 $main::lxdebug->enter_sub();
410 my ($self, $myconfig, $form) = @_;
412 # connect to database
413 my $dbh = $form->dbconnect($myconfig);
415 my $query = qq|SELECT id, lead
419 $sth = $dbh->prepare($query);
420 $sth->execute || $form->dberror($query);
422 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
423 push @{ $form->{ALL} }, $ref;
429 $main::lxdebug->leave_sub();
433 $main::lxdebug->enter_sub();
435 my ($self, $myconfig, $form) = @_;
437 # connect to database
438 my $dbh = $form->dbconnect($myconfig);
441 qq|SELECT l.id, l.lead
443 WHERE l.id = $form->{id}|;
444 my $sth = $dbh->prepare($query);
445 $sth->execute || $form->dberror($query);
447 my $ref = $sth->fetchrow_hashref(NAME_lc);
449 map { $form->{$_} = $ref->{$_} } keys %$ref;
455 $main::lxdebug->leave_sub();
459 $main::lxdebug->enter_sub();
461 my ($self, $myconfig, $form) = @_;
463 # connect to database
464 my $dbh = $form->dbconnect($myconfig);
466 $form->{lead} =~ s/\'/\'\'/g;
468 # id is the old record
470 $query = qq|UPDATE leads SET
471 lead = '$form->{description}'
472 WHERE id = $form->{id}|;
474 $query = qq|INSERT INTO leads
476 VALUES ('$form->{description}')|;
478 $dbh->do($query) || $form->dberror($query);
482 $main::lxdebug->leave_sub();
486 $main::lxdebug->enter_sub();
488 my ($self, $myconfig, $form) = @_;
490 # connect to database
491 my $dbh = $form->dbconnect($myconfig);
493 $query = qq|DELETE FROM leads
494 WHERE id = $form->{id}|;
495 $dbh->do($query) || $form->dberror($query);
499 $main::lxdebug->leave_sub();
503 $main::lxdebug->enter_sub();
505 my ($self, $myconfig, $form) = @_;
507 # connect to database
508 my $dbh = $form->dbconnect($myconfig);
510 my $query = qq|SELECT id, description, discount, customernumberinit, salesman
514 $sth = $dbh->prepare($query);
515 $sth->execute || $form->dberror($query);
517 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
518 push @{ $form->{ALL} }, $ref;
524 $main::lxdebug->leave_sub();
528 $main::lxdebug->enter_sub();
530 my ($self, $myconfig, $form) = @_;
532 # connect to database
533 my $dbh = $form->dbconnect($myconfig);
536 qq|SELECT b.description, b.discount, b.customernumberinit, b.salesman
538 WHERE b.id = $form->{id}|;
539 my $sth = $dbh->prepare($query);
540 $sth->execute || $form->dberror($query);
542 my $ref = $sth->fetchrow_hashref(NAME_lc);
544 map { $form->{$_} = $ref->{$_} } keys %$ref;
550 $main::lxdebug->leave_sub();
554 $main::lxdebug->enter_sub();
556 my ($self, $myconfig, $form) = @_;
558 # connect to database
559 my $dbh = $form->dbconnect($myconfig);
561 $form->{description} =~ s/\'/\'\'/g;
562 $form->{discount} /= 100;
563 $form->{salesman} *= 1;
565 # id is the old record
567 $query = qq|UPDATE business SET
568 description = '$form->{description}',
569 discount = $form->{discount},
570 customernumberinit = '$form->{customernumberinit}',
571 salesman = '$form->{salesman}'
572 WHERE id = $form->{id}|;
574 $query = qq|INSERT INTO business
575 (description, discount, customernumberinit, salesman)
576 VALUES ('$form->{description}', $form->{discount}, '$form->{customernumberinit}', '$form->{salesman}')|;
578 $dbh->do($query) || $form->dberror($query);
582 $main::lxdebug->leave_sub();
585 sub delete_business {
586 $main::lxdebug->enter_sub();
588 my ($self, $myconfig, $form) = @_;
590 # connect to database
591 my $dbh = $form->dbconnect($myconfig);
593 $query = qq|DELETE FROM business
594 WHERE id = $form->{id}|;
595 $dbh->do($query) || $form->dberror($query);
599 $main::lxdebug->leave_sub();
604 $main::lxdebug->enter_sub();
606 my ($self, $myconfig, $form, $return_list) = @_;
608 # connect to database
609 my $dbh = $form->dbconnect($myconfig);
612 "SELECT id, description, template_code, article_code, " .
613 " output_numberformat, output_dateformat, output_longdates " .
614 "FROM language ORDER BY description";
616 $sth = $dbh->prepare($query);
617 $sth->execute || $form->dberror($query);
621 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
622 push(@{ $ary }, $ref);
628 $main::lxdebug->leave_sub();
638 $main::lxdebug->enter_sub();
640 my ($self, $myconfig, $form) = @_;
642 # connect to database
643 my $dbh = $form->dbconnect($myconfig);
646 "SELECT description, template_code, article_code, " .
647 " output_numberformat, output_dateformat, output_longdates " .
648 "FROM language WHERE id = ?";
649 my $sth = $dbh->prepare($query);
650 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
652 my $ref = $sth->fetchrow_hashref(NAME_lc);
654 map { $form->{$_} = $ref->{$_} } keys %$ref;
660 $main::lxdebug->leave_sub();
663 sub get_language_details {
664 $main::lxdebug->enter_sub();
666 my ($self, $myconfig, $form, $id) = @_;
668 # connect to database
669 my $dbh = $form->dbconnect($myconfig);
672 "SELECT template_code, " .
673 " output_numberformat, output_dateformat, output_longdates " .
674 "FROM language WHERE id = ?";
675 my @res = $dbh->selectrow_array($query, undef, $id);
678 $main::lxdebug->leave_sub();
684 $main::lxdebug->enter_sub();
686 my ($self, $myconfig, $form) = @_;
688 # connect to database
689 my $dbh = $form->dbconnect($myconfig);
690 my (@values, $query);
692 map({ push(@values, $form->{$_}); }
693 qw(description template_code article_code
694 output_numberformat output_dateformat output_longdates));
696 # id is the old record
699 "UPDATE language SET " .
700 " description = ?, template_code = ?, article_code = ?, " .
701 " output_numberformat = ?, output_dateformat = ?, " .
702 " output_longdates = ? " .
704 push(@values, $form->{id});
707 "INSERT INTO language (" .
708 " description, template_code, article_code, " .
709 " output_numberformat, output_dateformat, output_longdates" .
710 ") VALUES (?, ?, ?, ?, ?, ?)";
712 $dbh->do($query, undef, @values) ||
713 $form->dberror($query . " (" . join(", ", @values) . ")");
717 $main::lxdebug->leave_sub();
720 sub delete_language {
721 $main::lxdebug->enter_sub();
723 my ($self, $myconfig, $form) = @_;
725 # connect to database
726 my $dbh = $form->dbconnect_noauto($myconfig);
728 foreach my $table (qw(translation_payment_terms units_language)) {
729 my $query = qq|DELETE FROM $table WHERE language_id = ?|;
730 do_query($form, $dbh, $query, $form->{"id"});
733 $query = "DELETE FROM language WHERE id = ?";
734 do_query($form, $dbh, $query, $form->{"id"});
739 $main::lxdebug->leave_sub();
744 $main::lxdebug->enter_sub();
746 my ($self, $myconfig, $form) = @_;
748 # connect to database
749 my $dbh = $form->dbconnect($myconfig);
751 my $query = qq|SELECT id, description,
753 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
755 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
757 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
759 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
761 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
763 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
765 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
767 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
769 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
773 $sth = $dbh->prepare($query);
774 $sth->execute || $form->dberror($query);
777 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
778 push @{ $form->{ALL} }, $ref;
784 $main::lxdebug->leave_sub();
787 sub get_buchungsgruppe {
788 $main::lxdebug->enter_sub();
790 my ($self, $myconfig, $form) = @_;
792 # connect to database
793 my $dbh = $form->dbconnect($myconfig);
797 qq|SELECT description, inventory_accno_id,
798 (SELECT accno FROM chart WHERE id = inventory_accno_id) AS inventory_accno,
800 (SELECT accno FROM chart WHERE id = income_accno_id_0) AS income_accno_0,
802 (SELECT accno FROM chart WHERE id = expense_accno_id_0) AS expense_accno_0,
804 (SELECT accno FROM chart WHERE id = income_accno_id_1) AS income_accno_1,
806 (SELECT accno FROM chart WHERE id = expense_accno_id_1) AS expense_accno_1,
808 (SELECT accno FROM chart WHERE id = income_accno_id_2) AS income_accno_2,
810 (select accno FROM chart WHERE id = expense_accno_id_2) AS expense_accno_2,
812 (SELECT accno FROM chart WHERE id = income_accno_id_3) AS income_accno_3,
814 (SELECT accno FROM chart WHERE id = expense_accno_id_3) AS expense_accno_3
817 my $sth = $dbh->prepare($query);
818 $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})");
820 my $ref = $sth->fetchrow_hashref(NAME_lc);
822 map { $form->{$_} = $ref->{$_} } keys %$ref;
827 qq|SELECT count(id) = 0 AS orphaned
829 WHERE buchungsgruppen_id = ?|;
830 ($form->{orphaned}) = $dbh->selectrow_array($query, undef, $form->{id});
831 $form->dberror($query . " ($form->{id})") if ($dbh->err);
834 $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
836 ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
837 $form->{"std_expense_accno_id"}) = $dbh->selectrow_array($query);
840 $query = qq|SELECT c.accno, c.description, c.link, c.id,
841 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
842 FROM chart c, defaults d
843 WHERE c.link LIKE '%$module%'
847 my $sth = $dbh->prepare($query);
848 $sth->execute || $form->dberror($query);
849 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
850 foreach my $key (split(/:/, $ref->{link})) {
851 if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
852 $form->{"std_inventory_accno_id"} = $ref->{"id"};
854 if ($key =~ /$module/) {
855 if ( ($ref->{id} eq $ref->{inventory_accno_id})
856 || ($ref->{id} eq $ref->{income_accno_id})
857 || ($ref->{id} eq $ref->{expense_accno_id})) {
858 push @{ $form->{"${module}_links"}{$key} },
859 { accno => $ref->{accno},
860 description => $ref->{description},
861 selected => "selected",
864 push @{ $form->{"${module}_links"}{$key} },
865 { accno => $ref->{accno},
866 description => $ref->{description},
878 $main::lxdebug->leave_sub();
881 sub save_buchungsgruppe {
882 $main::lxdebug->enter_sub();
884 my ($self, $myconfig, $form) = @_;
886 # connect to database
887 my $dbh = $form->dbconnect($myconfig);
889 my @values = ($form->{description}, $form->{inventory_accno_id},
890 $form->{income_accno_id_0}, $form->{expense_accno_id_0},
891 $form->{income_accno_id_1}, $form->{expense_accno_id_1},
892 $form->{income_accno_id_2}, $form->{expense_accno_id_2},
893 $form->{income_accno_id_3}, $form->{expense_accno_id_3});
897 # id is the old record
899 $query = qq|UPDATE buchungsgruppen SET
900 description = ?, inventory_accno_id = ?,
901 income_accno_id_0 = ?, expense_accno_id_0 = ?,
902 income_accno_id_1 = ?, expense_accno_id_1 = ?,
903 income_accno_id_2 = ?, expense_accno_id_2 = ?,
904 income_accno_id_3 = ?, expense_accno_id_3 = ?
906 push(@values, $form->{id});
908 $query = qq|SELECT COALESCE(MAX(sortkey) + 1, 1) FROM buchungsgruppen|;
909 my ($sortkey) = $dbh->selectrow_array($query);
910 $form->dberror($query) if ($dbh->err);
911 push(@values, $sortkey);
912 $query = qq|INSERT INTO buchungsgruppen
913 (description, inventory_accno_id,
914 income_accno_id_0, expense_accno_id_0,
915 income_accno_id_1, expense_accno_id_1,
916 income_accno_id_2, expense_accno_id_2,
917 income_accno_id_3, expense_accno_id_3,
919 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
921 do_query($form, $dbh, $query, @values);
925 $main::lxdebug->leave_sub();
928 sub delete_buchungsgruppe {
929 $main::lxdebug->enter_sub();
931 my ($self, $myconfig, $form) = @_;
933 # connect to database
934 my $dbh = $form->dbconnect($myconfig);
936 $query = qq|DELETE FROM buchungsgruppen WHERE id = ?|;
937 do_query($form, $dbh, $query, $form->{id});
941 $main::lxdebug->leave_sub();
945 $main::lxdebug->enter_sub();
947 my ($self, $myconfig, $form, $table) = @_;
949 # connect to database
950 my $dbh = $form->dbconnect_noauto($myconfig);
954 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey1,
955 (SELECT sortkey FROM $table WHERE id = ?) AS sortkey2|;
956 my @values = ($form->{"id1"}, $form->{"id2"});
957 my @sortkeys = selectrow_query($form, $dbh, $query, @values);
958 $main::lxdebug->dump(0, "v", \@values);
959 $main::lxdebug->dump(0, "s", \@sortkeys);
961 $query = qq|UPDATE $table SET sortkey = ? WHERE id = ?|;
962 my $sth = $dbh->prepare($query);
963 $sth->execute($sortkeys[1], $form->{"id1"}) ||
964 $form->dberror($query . " ($sortkeys[1], $form->{id1})");
965 $sth->execute($sortkeys[0], $form->{"id2"}) ||
966 $form->dberror($query . " ($sortkeys[0], $form->{id2})");
972 $main::lxdebug->leave_sub();
976 $main::lxdebug->enter_sub();
978 my ($self, $myconfig, $form) = @_;
980 # connect to database
981 my $dbh = $form->dbconnect($myconfig);
983 my $query = qq|SELECT id, printer_description, template_code, printer_command
987 $sth = $dbh->prepare($query);
988 $sth->execute || $form->dberror($query);
991 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
992 push @{ $form->{ALL} }, $ref;
998 $main::lxdebug->leave_sub();
1002 $main::lxdebug->enter_sub();
1004 my ($self, $myconfig, $form) = @_;
1006 # connect to database
1007 my $dbh = $form->dbconnect($myconfig);
1010 qq|SELECT p.printer_description, p.template_code, p.printer_command
1012 WHERE p.id = $form->{id}|;
1013 my $sth = $dbh->prepare($query);
1014 $sth->execute || $form->dberror($query);
1016 my $ref = $sth->fetchrow_hashref(NAME_lc);
1018 map { $form->{$_} = $ref->{$_} } keys %$ref;
1024 $main::lxdebug->leave_sub();
1028 $main::lxdebug->enter_sub();
1030 my ($self, $myconfig, $form) = @_;
1032 # connect to database
1033 my $dbh = $form->dbconnect($myconfig);
1035 $form->{printer_description} =~ s/\'/\'\'/g;
1036 $form->{printer_command} =~ s/\'/\'\'/g;
1037 $form->{template_code} =~ s/\'/\'\'/g;
1040 # id is the old record
1042 $query = qq|UPDATE printers SET
1043 printer_description = '$form->{printer_description}',
1044 template_code = '$form->{template_code}',
1045 printer_command = '$form->{printer_command}'
1046 WHERE id = $form->{id}|;
1048 $query = qq|INSERT INTO printers
1049 (printer_description, template_code, printer_command)
1050 VALUES ('$form->{printer_description}', '$form->{template_code}', '$form->{printer_command}')|;
1052 $dbh->do($query) || $form->dberror($query);
1056 $main::lxdebug->leave_sub();
1059 sub delete_printer {
1060 $main::lxdebug->enter_sub();
1062 my ($self, $myconfig, $form) = @_;
1064 # connect to database
1065 my $dbh = $form->dbconnect($myconfig);
1067 $query = qq|DELETE FROM printers
1068 WHERE id = $form->{id}|;
1069 $dbh->do($query) || $form->dberror($query);
1073 $main::lxdebug->leave_sub();
1077 $main::lxdebug->enter_sub();
1079 my ($self, $myconfig, $form) = @_;
1081 # connect to database
1082 my $dbh = $form->dbconnect($myconfig);
1084 my $query = qq|SELECT * FROM payment_terms ORDER BY sortkey|;
1086 $sth = $dbh->prepare($query);
1087 $sth->execute || $form->dberror($query);
1090 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1091 push @{ $form->{ALL} }, $ref;
1097 $main::lxdebug->leave_sub();
1101 $main::lxdebug->enter_sub();
1103 my ($self, $myconfig, $form) = @_;
1105 # connect to database
1106 my $dbh = $form->dbconnect($myconfig);
1108 my $query = qq|SELECT * FROM payment_terms WHERE id = ?|;
1109 my $sth = $dbh->prepare($query);
1110 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
1112 my $ref = $sth->fetchrow_hashref(NAME_lc);
1113 map { $form->{$_} = $ref->{$_} } keys %$ref;
1117 qq|SELECT t.language_id, t.description_long, l.description AS language | .
1118 qq|FROM translation_payment_terms t | .
1119 qq|LEFT JOIN language l ON t.language_id = l.id | .
1120 qq|WHERE t.payment_terms_id = ? | .
1122 qq|SELECT l.id AS language_id, NULL AS description_long, | .
1123 qq|l.description AS language | .
1124 qq|FROM language l|;
1125 $sth = $dbh->prepare($query);
1126 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
1129 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1130 $mapping{ $ref->{"language_id"} } = $ref
1131 unless (defined($mapping{ $ref->{"language_id"} }));
1135 $form->{"TRANSLATION"} = [sort({ $a->{"language"} cmp $b->{"language"} }
1140 $main::lxdebug->leave_sub();
1144 $main::lxdebug->enter_sub();
1146 my ($self, $myconfig, $form) = @_;
1148 # connect to database
1149 my $dbh = $form->dbconnect_noauto($myconfig);
1154 $query = qq|SELECT nextval('id'), COALESCE(MAX(sortkey) + 1, 1) | .
1155 qq|FROM payment_terms|;
1157 ($form->{id}, $sortkey) = selectrow_query($form, $dbh, $query);
1159 $query = qq|INSERT INTO payment_terms (id, sortkey) VALUES (?, ?)|;
1160 do_query($form, $dbh, $query, $form->{id}, $sortkey);
1164 qq|DELETE FROM translation_payment_terms | .
1165 qq|WHERE payment_terms_id = ?|;
1166 do_query($form, $dbh, $query, $form->{"id"});
1169 $query = qq|UPDATE payment_terms SET
1170 description = ?, description_long = ?,
1172 terms_netto = ?, terms_skonto = ?,
1175 my @values = ($form->{description}, $form->{description_long},
1176 $form->{ranking} * 1,
1177 $form->{terms_netto} * 1, $form->{terms_skonto} * 1,
1178 $form->{percent_skonto} * 1,
1180 do_query($form, $dbh, $query, @values);
1182 $query = qq|SELECT id FROM language|;
1184 my $sth = $dbh->prepare($query);
1185 $sth->execute() || $form->dberror($query);
1187 while (my ($id) = $sth->fetchrow_array()) {
1188 push(@language_ids, $id);
1193 qq|INSERT INTO translation_payment_terms | .
1194 qq|(language_id, payment_terms_id, description_long) | .
1195 qq|VALUES (?, ?, ?)|;
1196 $sth = $dbh->prepare($query);
1198 foreach my $language_id (@language_ids) {
1199 do_statement($form, $sth, $query, $language_id, $form->{"id"},
1200 $form->{"description_long_${language_id}"});
1207 $main::lxdebug->leave_sub();
1210 sub delete_payment {
1211 $main::lxdebug->enter_sub();
1213 my ($self, $myconfig, $form) = @_;
1215 # connect to database
1216 my $dbh = $form->dbconnect_noauto($myconfig);
1219 qq|DELETE FROM translation_payment_terms WHERE payment_terms_id = ?|;
1220 do_query($form, $dbh, $query, $form->{"id"});
1222 $query = qq|DELETE FROM payment_terms WHERE id = ?|;
1223 do_query($form, $dbh, $query, $form->{"id"});
1228 $main::lxdebug->leave_sub();
1232 $main::lxdebug->enter_sub();
1234 my ($self, $myconfig, $form) = @_;
1236 # connect to database
1237 my $dbh = $form->dbconnect($myconfig);
1239 my $query = qq|SELECT code, sictype, description
1243 $sth = $dbh->prepare($query);
1244 $sth->execute || $form->dberror($query);
1246 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1247 push @{ $form->{ALL} }, $ref;
1253 $main::lxdebug->leave_sub();
1257 $main::lxdebug->enter_sub();
1259 my ($self, $myconfig, $form) = @_;
1261 # connect to database
1262 my $dbh = $form->dbconnect($myconfig);
1264 my $query = qq|SELECT s.code, s.sictype, s.description
1266 WHERE s.code = '$form->{code}'|;
1267 my $sth = $dbh->prepare($query);
1268 $sth->execute || $form->dberror($query);
1270 my $ref = $sth->fetchrow_hashref(NAME_lc);
1272 map { $form->{$_} = $ref->{$_} } keys %$ref;
1278 $main::lxdebug->leave_sub();
1282 $main::lxdebug->enter_sub();
1284 my ($self, $myconfig, $form) = @_;
1286 # connect to database
1287 my $dbh = $form->dbconnect($myconfig);
1289 $form->{code} =~ s/\'/\'\'/g;
1290 $form->{description} =~ s/\'/\'\'/g;
1294 $query = qq|UPDATE sic SET
1295 code = '$form->{code}',
1296 sictype = '$form->{sictype}',
1297 description = '$form->{description}'
1298 WHERE code = '$form->{id}'|;
1300 $query = qq|INSERT INTO sic
1301 (code, sictype, description)
1302 VALUES ('$form->{code}', '$form->{sictype}', '$form->{description}')|;
1304 $dbh->do($query) || $form->dberror($query);
1308 $main::lxdebug->leave_sub();
1312 $main::lxdebug->enter_sub();
1314 my ($self, $myconfig, $form) = @_;
1316 # connect to database
1317 my $dbh = $form->dbconnect($myconfig);
1319 $query = qq|DELETE FROM sic
1320 WHERE code = '$form->{code}'|;
1321 $dbh->do($query) || $form->dberror($query);
1325 $main::lxdebug->leave_sub();
1329 $main::lxdebug->enter_sub();
1331 my ($self, $form) = @_;
1333 open(TEMPLATE, "$form->{file}") or $form->error("$form->{file} : $!");
1335 while (<TEMPLATE>) {
1336 $form->{body} .= $_;
1341 $main::lxdebug->leave_sub();
1345 $main::lxdebug->enter_sub();
1347 my ($self, $form) = @_;
1349 open(TEMPLATE, ">$form->{file}") or $form->error("$form->{file} : $!");
1352 $form->{body} =~ s/\r\n/\n/g;
1353 print TEMPLATE $form->{body};
1357 $main::lxdebug->leave_sub();
1360 sub save_preferences {
1361 $main::lxdebug->enter_sub();
1363 my ($self, $myconfig, $form, $memberfile, $userspath, $webdav) = @_;
1365 map { ($form->{$_}) = split(/--/, $form->{$_}) }
1366 qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno);
1369 $form->{curr} =~ s/ //g;
1370 map { push(@a, uc pack "A3", $_) if $_ } split(/:/, $form->{curr});
1371 $form->{curr} = join ':', @a;
1373 # connect to database
1374 my $dbh = $form->dbconnect_noauto($myconfig);
1376 # these defaults are database wide
1377 # user specific variables are in myconfig
1379 my $query = qq|UPDATE defaults SET
1380 inventory_accno_id =
1381 (SELECT c.id FROM chart c
1382 WHERE c.accno = '$form->{inventory_accno}'),
1384 (SELECT c.id FROM chart c
1385 WHERE c.accno = '$form->{income_accno}'),
1387 (SELECT c.id FROM chart c
1388 WHERE c.accno = '$form->{expense_accno}'),
1390 (SELECT c.id FROM chart c
1391 WHERE c.accno = '$form->{fxgain_accno}'),
1393 (SELECT c.id FROM chart c
1394 WHERE c.accno = '$form->{fxloss_accno}'),
1395 invnumber = '$form->{invnumber}',
1396 cnnumber = '$form->{cnnumber}',
1397 sonumber = '$form->{sonumber}',
1398 ponumber = '$form->{ponumber}',
1399 sqnumber = '$form->{sqnumber}',
1400 rfqnumber = '$form->{rfqnumber}',
1401 customernumber = '$form->{customernumber}',
1402 vendornumber = '$form->{vendornumber}',
1403 articlenumber = '$form->{articlenumber}',
1404 servicenumber = '$form->{servicenumber}',
1405 yearend = '$form->{yearend}',
1406 curr = '$form->{curr}',
1407 businessnumber = '$form->{businessnumber}'
1409 $dbh->do($query) || $form->dberror($query);
1412 my $name = $form->{name};
1413 $name =~ s/\'/\'\'/g;
1414 $query = qq|UPDATE employee
1416 WHERE login = '$form->{login}'|;
1417 $dbh->do($query) || $form->dberror($query);
1419 # foreach my $item (split(/ /, $form->{taxaccounts})) {
1420 # $query = qq|UPDATE tax
1421 # SET rate = | . ($form->{$item} / 100) . qq|,
1422 # taxnumber = '$form->{"taxnumber_$item"}'
1423 # WHERE chart_id = $item|;
1424 # $dbh->do($query) || $form->dberror($query);
1427 my $rc = $dbh->commit;
1430 # save first currency in myconfig
1431 $form->{currency} = substr($form->{curr}, 0, 3);
1433 my $myconfig = new User "$memberfile", "$form->{login}";
1435 foreach my $item (keys %$form) {
1436 $myconfig->{$item} = $form->{$item};
1439 $myconfig->save_member($memberfile, $userspath);
1443 qw(angebote bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen);
1444 foreach $directory (@webdavdirs) {
1445 $file = "webdav/" . $directory . "/webdav-user";
1446 if ($myconfig->{$directory}) {
1447 open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1448 while (<HTACCESS>) {
1449 ($login, $password) = split(/:/, $_);
1450 if ($login ne $form->{login}) {
1455 open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1456 $newfile .= $myconfig->{login} . ":" . $myconfig->{password} . "\n";
1457 print(HTACCESS $newfile);
1460 $form->{$directory} = 0;
1461 open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1462 while (<HTACCESS>) {
1463 ($login, $password) = split(/:/, $_);
1464 if ($login ne $form->{login}) {
1469 open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1470 print(HTACCESS $newfile);
1476 $main::lxdebug->leave_sub();
1481 sub defaultaccounts {
1482 $main::lxdebug->enter_sub();
1484 my ($self, $myconfig, $form) = @_;
1486 # connect to database
1487 my $dbh = $form->dbconnect($myconfig);
1489 # get defaults from defaults table
1490 my $query = qq|SELECT * FROM defaults|;
1491 my $sth = $dbh->prepare($query);
1492 $sth->execute || $form->dberror($query);
1494 $form->{defaults} = $sth->fetchrow_hashref(NAME_lc);
1495 $form->{defaults}{IC} = $form->{defaults}{inventory_accno_id};
1496 $form->{defaults}{IC_income} = $form->{defaults}{income_accno_id};
1497 $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
1498 $form->{defaults}{FX_gain} = $form->{defaults}{fxgain_accno_id};
1499 $form->{defaults}{FX_loss} = $form->{defaults}{fxloss_accno_id};
1503 $query = qq|SELECT c.id, c.accno, c.description, c.link
1505 WHERE c.link LIKE '%IC%'
1507 $sth = $dbh->prepare($query);
1508 $sth->execute || $self->dberror($query);
1510 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1511 foreach my $key (split(/:/, $ref->{link})) {
1514 if ($key =~ /cogs/) {
1515 $nkey = "IC_expense";
1517 if ($key =~ /sale/) {
1518 $nkey = "IC_income";
1520 %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
1522 description => $ref->{description}
1529 $query = qq|SELECT c.id, c.accno, c.description
1531 WHERE c.category = 'I'
1532 AND c.charttype = 'A'
1534 $sth = $dbh->prepare($query);
1535 $sth->execute || $self->dberror($query);
1537 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1538 %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
1540 description => $ref->{description}
1545 $query = qq|SELECT c.id, c.accno, c.description
1547 WHERE c.category = 'E'
1548 AND c.charttype = 'A'
1550 $sth = $dbh->prepare($query);
1551 $sth->execute || $self->dberror($query);
1553 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1554 %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
1556 description => $ref->{description}
1561 # now get the tax rates and numbers
1562 $query = qq|SELECT c.id, c.accno, c.description,
1563 t.rate * 100 AS rate, t.taxnumber
1565 WHERE c.id = t.chart_id|;
1567 $sth = $dbh->prepare($query);
1568 $sth->execute || $form->dberror($query);
1570 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1571 $form->{taxrates}{ $ref->{accno} }{id} = $ref->{id};
1572 $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
1573 $form->{taxrates}{ $ref->{accno} }{taxnumber} = $ref->{taxnumber}
1574 if $ref->{taxnumber};
1575 $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
1581 $main::lxdebug->leave_sub();
1585 $main::lxdebug->enter_sub();
1587 my ($self, $myconfig, $form, $userspath) = @_;
1591 my $boundary = time;
1593 "$userspath/$boundary.$myconfig->{dbname}-$form->{dbversion}.sql";
1594 my $out = $form->{OUT};
1595 $form->{OUT} = ">$tmpfile";
1597 if ($form->{media} eq 'email') {
1602 $mail->{to} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
1603 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
1605 "Lx-Office Backup / $myconfig->{dbname}-$form->{dbversion}.sql";
1606 @{ $mail->{attachments} } = ($tmpfile);
1607 $mail->{version} = $form->{version};
1608 $mail->{fileid} = "$boundary.";
1610 $myconfig->{signature} =~ s/\\n/\r\n/g;
1611 $mail->{message} = "--\n$myconfig->{signature}";
1615 open(OUT, "$form->{OUT}") or $form->error("$form->{OUT} : $!");
1617 # get sequences, functions and triggers
1618 open(FH, "sql/lx-office.sql") or $form->error("sql/lx-office.sql : $!");
1631 # Remove DOS and Unix style line endings.
1634 # ignore comments or empty lines
1635 next if /^(--.*|\s+)$/;
1637 for (my $i = 0; $i < length($_); $i++) {
1638 my $char = substr($_, $i, 1);
1640 # Are we inside a string?
1642 if ($char eq $quote_chars[-1]) {
1648 if (($char eq "'") || ($char eq "\"")) {
1649 push(@quote_chars, $char);
1651 } elsif ($char eq ";") {
1653 # Query is complete. Check for triggers and functions.
1654 if ($query =~ /^create\s+function\s+\"?(\w+)\"?/i) {
1655 push(@functions, $query);
1657 } elsif ($query =~ /^create\s+trigger\s+\"?(\w+)\"?/i) {
1658 push(@triggers, $query);
1660 } elsif ($query =~ /^create\s+sequence\s+\"?(\w+)\"?/i) {
1661 push(@sequences, $1);
1663 } elsif ($query =~ /^create\s+table\s+\"?(\w+)\"?/i) {
1664 $tablespecs{$1} = $query;
1666 } elsif ($query =~ /^create\s+index\s+\"?(\w+)\"?/i) {
1667 push(@indices, $query);
1681 # connect to database
1682 my $dbh = $form->dbconnect($myconfig);
1684 # get all the tables
1685 my @tables = $dbh->tables('', '', 'customer', '', { noprefix => 0 });
1687 my $today = scalar localtime;
1689 $myconfig->{dbhost} = 'localhost' unless $myconfig->{dbhost};
1691 print OUT qq|-- Lx-Office Backup
1692 -- Dataset: $myconfig->{dbname}
1693 -- Version: $form->{dbversion}
1694 -- Host: $myconfig->{dbhost}
1695 -- Login: $form->{login}
1696 -- User: $myconfig->{name}
1700 $myconfig->{dboptions};
1704 print OUT "-- DROP Sequences\n";
1706 foreach $item (@sequences) {
1707 print OUT qq|DROP SEQUENCE $item;\n|;
1710 print OUT "-- DROP Triggers\n";
1712 foreach $item (@triggers) {
1713 if ($item =~ /^create\s+trigger\s+\"?(\w+)\"?\s+.*on\s+\"?(\w+)\"?\s+/i) {
1714 print OUT qq|DROP TRIGGER "$1" ON "$2";\n|;
1718 print OUT "-- DROP Functions\n";
1720 foreach $item (@functions) {
1721 if ($item =~ /^create\s+function\s+\"?(\w+)\"?/i) {
1722 print OUT qq|DROP FUNCTION "$1" ();\n|;
1726 foreach $table (@tables) {
1727 if (!($table =~ /^sql_.*/)) {
1728 my $query = qq|SELECT * FROM $table|;
1730 my $sth = $dbh->prepare($query);
1731 $sth->execute || $form->dberror($query);
1733 $query = "INSERT INTO $table (";
1734 map { $query .= qq|$sth->{NAME}->[$_],| }
1735 (0 .. $sth->{NUM_OF_FIELDS} - 1);
1738 $query .= ") VALUES";
1740 if ($tablespecs{$table}) {
1742 print(OUT "DROP TABLE $table;\n");
1743 print(OUT $tablespecs{$table}, ";\n");
1746 print(OUT "DELETE FROM $table;\n");
1748 while (my @arr = $sth->fetchrow_array) {
1751 foreach my $item (@arr) {
1752 if (defined $item) {
1753 $item =~ s/\'/\'\'/g;
1754 $fields .= qq|'$item',|;
1763 print OUT qq|$query $fields;\n|;
1770 # create indices, sequences, functions and triggers
1772 print(OUT "-- CREATE Indices\n");
1773 map({ print(OUT "$_;\n"); } @indices);
1775 print OUT "-- CREATE Sequences\n";
1776 foreach $item (@sequences) {
1777 $query = qq|SELECT last_value FROM $item|;
1778 $sth = $dbh->prepare($query);
1779 $sth->execute || $form->dberror($query);
1780 my ($id) = $sth->fetchrow_array;
1784 CREATE SEQUENCE $item START $id;
1788 print OUT "-- CREATE Functions\n";
1791 map { print(OUT $_, ";\n"); } @functions;
1793 print OUT "-- CREATE Triggers\n";
1796 map { print(OUT $_, ";\n"); } @triggers;
1803 my @args = ("gzip", "$tmpfile");
1804 system(@args) == 0 or $form->error("$args[0] : $?");
1808 if ($form->{media} eq 'email') {
1809 @{ $mail->{attachments} } = ($tmpfile);
1810 $err = $mail->send($out);
1813 if ($form->{media} eq 'file') {
1815 open(IN, "$tmpfile") or $form->error("$tmpfile : $!");
1816 open(OUT, ">-") or $form->error("STDOUT : $!");
1818 print OUT qq|Content-Type: application/x-tar-gzip;
1819 Content-Disposition: attachment; filename="$myconfig->{dbname}-$form->{dbversion}.sql.gz"
1834 $main::lxdebug->leave_sub();
1838 $main::lxdebug->enter_sub();
1840 my ($self, $myconfig, $form) = @_;
1842 my $dbh = $form->dbconnect($myconfig);
1844 my $query = qq|SELECT closedto, revtrans FROM defaults|;
1845 my $sth = $dbh->prepare($query);
1846 $sth->execute || $form->dberror($query);
1848 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
1854 $main::lxdebug->leave_sub();
1858 $main::lxdebug->enter_sub();
1860 my ($self, $myconfig, $form) = @_;
1862 my $dbh = $form->dbconnect($myconfig);
1864 if ($form->{revtrans}) {
1866 $query = qq|UPDATE defaults SET closedto = NULL,
1868 } elsif ($form->{closedto}) {
1870 $query = qq|UPDATE defaults SET closedto = '$form->{closedto}',
1874 $query = qq|UPDATE defaults SET closedto = NULL,
1878 # set close in defaults
1879 $dbh->do($query) || $form->dberror($query);
1883 $main::lxdebug->leave_sub();
1887 my ($self, $units, $unit_name, $factor) = @_;
1889 $factor = 1 unless ($factor);
1891 my $unit = $units->{$unit_name};
1893 if (!defined($unit) || !$unit->{"base_unit"} ||
1894 ($unit_name eq $unit->{"base_unit"})) {
1895 return ($unit_name, $factor);
1898 return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
1901 sub retrieve_units {
1902 $main::lxdebug->enter_sub();
1904 my ($self, $myconfig, $form, $type, $prefix) = @_;
1906 my $dbh = $form->dbconnect($myconfig);
1908 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
1911 $query .= " WHERE (type = ?)";
1915 my $sth = $dbh->prepare($query);
1916 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1919 while (my $ref = $sth->fetchrow_hashref()) {
1920 $units->{$ref->{"name"}} = $ref;
1924 my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
1925 $sth = $dbh->prepare($query_lang);
1926 $sth->execute() || $form->dberror($query_lang);
1928 while ($ref = $sth->fetchrow_hashref()) {
1929 push(@languages, $ref);
1933 $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
1934 "FROM units_language ul " .
1935 "LEFT JOIN language l ON ul.language_id = l.id " .
1936 "WHERE ul.unit = ?";
1937 $sth = $dbh->prepare($query_lang);
1939 foreach my $unit (values(%{$units})) {
1940 ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
1942 $unit->{"LANGUAGES"} = {};
1943 foreach my $lang (@languages) {
1944 $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
1947 $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
1948 while ($ref = $sth->fetchrow_hashref()) {
1949 map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
1956 $main::lxdebug->leave_sub();
1961 sub translate_units {
1962 $main::lxdebug->enter_sub();
1964 my ($self, $form, $template_code, $unit, $amount) = @_;
1966 my $units = $self->retrieve_units(\%main::myconfig, $form);
1968 my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
1969 my $new_unit = $unit;
1971 if (($amount != 1) && $h->{"localized_plural"}) {
1972 $new_unit = $h->{"localized_plural"};
1973 } elsif ($h->{"localized"}) {
1974 $new_unit = $h->{"localized"};
1978 $main::lxdebug->leave_sub();
1984 $main::lxdebug->enter_sub();
1986 my ($self, $myconfig, $form, $units) = @_;
1988 my $dbh = $form->dbconnect($myconfig);
1990 foreach my $unit (values(%{$units})) {
1991 my $base_unit = $unit->{"original_base_unit"};
1992 while ($base_unit) {
1993 $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
1994 push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
1995 $base_unit = $units->{$base_unit}->{"original_base_unit"};
1999 foreach my $unit (values(%{$units})) {
2000 $unit->{"in_use"} = 0;
2001 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
2003 foreach my $table (qw(parts invoice orderitems)) {
2004 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
2006 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
2007 $query .= "= " . $dbh->quote($unit->{"name"});
2009 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," . join(",", @{$unit->{"DEPENDING_UNITS"}}) . ")";
2012 my ($count) = $dbh->selectrow_array($query);
2013 $form->dberror($query) if ($dbh->err);
2016 $unit->{"in_use"} = 1;
2024 $main::lxdebug->leave_sub();
2027 sub unit_select_data {
2028 $main::lxdebug->enter_sub();
2030 my ($self, $units, $selected, $empty_entry) = @_;
2035 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
2038 foreach my $unit (sort({ $a->{"sortkey"} <=> $b->{"sortkey"} } keys(%{$units}))) {
2039 push(@{$select}, { "name" => $unit,
2040 "base_unit" => $units->{$unit}->{"base_unit"},
2041 "factor" => $units->{$unit}->{"factor"},
2042 "selected" => ($unit eq $selected) ? "selected" : "" });
2045 $main::lxdebug->leave_sub();
2050 sub unit_select_html {
2051 $main::lxdebug->enter_sub();
2053 my ($self, $units, $name, $selected, $convertible_into) = @_;
2055 my $select = "<select name=${name}>";
2057 foreach my $unit (sort({ $a->{"sortkey"} <=> $b->{"sortkey"} } keys(%{$units}))) {
2058 if (!$convertible_into ||
2059 ($units->{$convertible_into} &&
2060 ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
2061 $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
2064 $select .= "</select>";
2066 $main::lxdebug->leave_sub();
2072 $main::lxdebug->enter_sub();
2074 my ($self, $myconfig, $form, $name, $base_unit, $factor, $type, $languages) = @_;
2076 my $dbh = $form->dbconnect_noauto($myconfig);
2078 my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
2079 my ($sortkey) = selectrow_query($form, $dbh, $query);
2081 $query = "INSERT INTO units (name, base_unit, factor, type, sortkey) " .
2082 "VALUES (?, ?, ?, ?, ?)";
2083 do_query($form, $dbh, $query, $name, $base_unit, $factor, $type, $sortkey);
2086 $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
2087 my $sth = $dbh->prepare($query);
2088 foreach my $lang (@{$languages}) {
2089 my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
2090 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
2098 $main::lxdebug->leave_sub();
2102 $main::lxdebug->enter_sub();
2104 my ($self, $myconfig, $form, $type, $units, $delete_units) = @_;
2106 my $dbh = $form->dbconnect_noauto($myconfig);
2108 my ($base_unit, $unit, $sth, $query);
2110 $query = "DELETE FROM units_language";
2111 $dbh->do($query) || $form->dberror($query);
2113 if ($delete_units && (0 != scalar(@{$delete_units}))) {
2114 $query = "DELETE FROM units WHERE name IN (";
2115 map({ $query .= "?," } @{$delete_units});
2116 substr($query, -1, 1) = ")";
2117 $dbh->do($query, undef, @{$delete_units}) ||
2118 $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
2121 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
2122 $sth = $dbh->prepare($query);
2124 my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
2125 my $sth_lang = $dbh->prepare($query_lang);
2127 foreach $unit (values(%{$units})) {
2128 $unit->{"depth"} = 0;
2129 my $base_unit = $unit;
2130 while ($base_unit->{"base_unit"}) {
2132 $base_unit = $units->{$base_unit->{"base_unit"}};
2136 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
2137 if ($unit->{"LANGUAGES"}) {
2138 foreach my $lang (@{$unit->{"LANGUAGES"}}) {
2139 next unless ($lang->{"id"} && $lang->{"localized"});
2140 my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
2141 $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
2145 next if ($unit->{"unchanged_unit"});
2147 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
2148 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
2152 $sth_lang->finish();
2156 $main::lxdebug->leave_sub();
2160 $main::lxdebug->enter_sub();
2162 my ($self, $myconfig, $form, $dir, $name_1, $unit_type) = @_;
2164 my $dbh = $form->dbconnect_noauto($myconfig);
2168 $query = qq|SELECT sortkey FROM units WHERE name = ?|;
2169 my ($sortkey_1) = selectrow_query($form, $dbh, $query, $name_1);
2172 qq|SELECT sortkey FROM units | .
2173 qq|WHERE sortkey | . ($dir eq "down" ? ">" : "<") . qq| ? AND type = ? | .
2174 qq|ORDER BY sortkey | . ($dir eq "down" ? "ASC" : "DESC") . qq| LIMIT 1|;
2175 my ($sortkey_2) = selectrow_query($form, $dbh, $query, $sortkey_1, $unit_type);
2177 if (defined($sortkey_1)) {
2178 $query = qq|SELECT name FROM units WHERE sortkey = ${sortkey_2}|;
2179 my ($name_2) = selectrow_query($form, $dbh, $query);
2181 if (defined($name_2)) {
2182 $query = qq|UPDATE units SET sortkey = ? WHERE name = ?|;
2183 my $sth = $dbh->prepare($query);
2185 do_statement($form, $sth, $query, $sortkey_1, $name_2);
2186 do_statement($form, $sth, $query, $sortkey_2, $name_1);
2193 $main::lxdebug->leave_sub();