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 #======================================================================
43 $main::lxdebug->enter_sub();
45 my ($self, $myconfig, $form) = @_;
47 $form->{id} = "NULL" unless ($form->{id});
50 my $dbh = $form->dbconnect($myconfig);
51 my $query = qq§SELECT c.accno, c.description, c.charttype, c.gifi_accno,
52 c.category,c.link, tk.taxkey_id, tk.pos_ustva, tk.tax_id,tk.tax_id||'--'||tk.taxkey_id AS tax, tk.startdate, c.pos_bilanz, c.pos_eur, c.new_chart_id, c.valid_from, c.pos_bwa
53 FROM chart c LEFT JOIN taxkeys tk
54 ON (c.id=tk.chart_id AND tk.id = (SELECT id from taxkeys where taxkeys.chart_id =c.id AND startdate<=current_date ORDER BY startdate desc LIMIT 1))
55 WHERE c.id = $form->{id}§;
58 my $sth = $dbh->prepare($query);
59 $sth->execute || $form->dberror($query);
61 my $ref = $sth->fetchrow_hashref(NAME_lc);
63 foreach my $key (keys %$ref) {
64 $form->{"$key"} = $ref->{"$key"};
69 # get default accounts
70 $query = qq|SELECT inventory_accno_id, income_accno_id, expense_accno_id
72 $sth = $dbh->prepare($query);
73 $sth->execute || $form->dberror($query);
75 $ref = $sth->fetchrow_hashref(NAME_lc);
77 map { $form->{$_} = $ref->{$_} } keys %ref;
81 # get taxkeys and description
82 $query = qq§SELECT id, taxkey,id||'--'||taxkey AS tax, taxdescription
83 FROM tax ORDER BY taxkey§;
84 $sth = $dbh->prepare($query);
85 $sth->execute || $form->dberror($query);
89 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
90 push @{ $form->{TAXKEY} }, $ref;
96 $where = " WHERE link='$form->{link}'";
100 $query = qq|SELECT id, accno,description
102 $sth = $dbh->prepare($query);
103 $sth->execute || $form->dberror($query);
105 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
106 push @{ $form->{NEWACCOUNT} }, $ref;
111 # check if we have any transactions
112 $query = qq|SELECT a.trans_id FROM acc_trans a
113 WHERE a.chart_id = $form->{id}|;
114 $sth = $dbh->prepare($query);
115 $sth->execute || $form->dberror($query);
117 ($form->{orphaned}) = $sth->fetchrow_array;
118 $form->{orphaned} = !$form->{orphaned};
121 # check if new account is active
122 $form->{new_chart_valid} = 0;
123 if ($form->{new_chart_id}) {
124 $query = qq|SELECT current_date-valid_from FROM chart
125 WHERE id = $form->{id}|;
126 $sth = $dbh->prepare($query);
127 $sth->execute || $form->dberror($query);
129 my ($count) = $sth->fetchrow_array;
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 # if we have an id then replace the old record
170 $form->{description} =~ s/\'/\'\'/g;
172 # strip blanks from accno
173 map { $form->{$_} =~ s/ //g; } qw(accno);
177 if ($form->{id} eq "NULL") {
181 map({ $form->{$_} = "NULL" unless ($form->{$_}); }
182 qw(pos_ustva pos_bwa pos_bilanz pos_eur new_chart_id));
183 my($tax_id, $taxkey) = split /--/, $form->{tax};
184 $form->{valid_from} = ($form->{valid_from}) ? "'$form->{valid_from}'" : "NULL";
185 my $startdate = ($form->{startdate}) ? "'$form->{startdate}'" : "'1970-01-01'";
186 if ($form->{id} && $form->{orphaned}) {
187 $query = qq|UPDATE chart SET
188 accno = '$form->{accno}',
189 description = '$form->{description}',
190 charttype = '$form->{charttype}',
191 gifi_accno = '$form->{gifi_accno}',
192 category = '$form->{category}',
193 link = '$form->{link}',
195 pos_ustva = $form->{pos_ustva},
196 pos_bwa = $form->{pos_bwa},
197 pos_bilanz = $form->{pos_bilanz},
198 pos_eur = $form->{pos_eur},
199 new_chart_id = $form->{new_chart_id},
200 valid_from = $form->{valid_from}
201 WHERE id = $form->{id}|;
202 } elsif ($form->{id} && !$form->{new_chart_valid}) {
203 $query = qq|UPDATE chart SET
204 new_chart_id = $form->{new_chart_id},
205 valid_from = $form->{valid_from}
206 WHERE id = $form->{id}|;
209 $query = qq|INSERT INTO chart
210 (accno, description, charttype, gifi_accno, category, link, taxkey_id, pos_ustva, pos_bwa, pos_bilanz,pos_eur, new_chart_id, valid_from)
211 VALUES ('$form->{accno}', '$form->{description}',
212 '$form->{charttype}', '$form->{gifi_accno}',
213 '$form->{category}', '$form->{link}', $taxkey, $form->{pos_ustva}, $form->{pos_bwa}, $form->{pos_bilanz}, $form->{pos_eur}, $form->{new_chart_id}, $form->{valid_from})|;
215 $dbh->do($query) || $form->dberror($query);
219 $query = qq|INSERT INTO taxkeys (chart_id,tax_id,taxkey_id, pos_ustva, startdate) VALUES ((SELECT id FROM chart where accno='$form->{accno}'), $tax_id, $taxkey,$form->{pos_ustva}, $startdate)|;
220 $dbh->do($query) || $form->dberror($query);
222 $query = qq|DELETE FROM taxkeys WHERE chart_id=$form->{id} AND tax_id=$tax_id|;
223 $dbh->do($query) || $form->dberror($query);
224 $query = qq|INSERT INTO taxkeys (chart_id,tax_id,taxkey_id, pos_ustva, startdate) VALUES ($form->{id}, $tax_id, $taxkey,$form->{pos_ustva}, $startdate)|;
225 $dbh->do($query) || $form->dberror($query);
228 # if ($form->{IC_taxpart} || $form->{IC_taxservice} || $form->{CT_tax}) {
230 # my $chart_id = $form->{id};
232 # unless ($form->{id}) {
234 # # get id from chart
235 # $query = qq|SELECT c.id
237 # WHERE c.accno = '$form->{accno}'|;
238 # $sth = $dbh->prepare($query);
239 # $sth->execute || $form->dberror($query);
241 # ($chart_id) = $sth->fetchrow_array;
245 # # add account if it doesn't exist in tax
246 # $query = qq|SELECT t.chart_id
248 # WHERE t.chart_id = $chart_id|;
249 # $sth = $dbh->prepare($query);
250 # $sth->execute || $form->dberror($query);
252 # my ($tax_id) = $sth->fetchrow_array;
255 # # add tax if it doesn't exist
257 # $query = qq|INSERT INTO tax (chart_id, rate)
258 # VALUES ($chart_id, 0)|;
259 # $dbh->do($query) || $form->dberror($query);
265 # $query = qq|DELETE FROM tax
266 # WHERE chart_id = $form->{id}|;
267 # $dbh->do($query) || $form->dberror($query);
272 my $rc = $dbh->commit;
275 $main::lxdebug->leave_sub();
281 $main::lxdebug->enter_sub();
283 my ($self, $myconfig, $form) = @_;
285 # connect to database, turn off AutoCommit
286 my $dbh = $form->dbconnect_noauto($myconfig);
288 my $query = qq|SELECT count(*) FROM acc_trans a
289 WHERE a.chart_id = $form->{id}|;
290 my $sth = $dbh->prepare($query);
291 $sth->execute || $form->dberror($query);
293 if ($sth->fetchrow_array) {
296 $main::lxdebug->leave_sub();
301 # delete chart of account record
302 $query = qq|DELETE FROM chart
303 WHERE id = $form->{id}|;
304 $dbh->do($query) || $form->dberror($query);
306 # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
307 $query = qq|UPDATE parts
308 SET inventory_accno_id =
309 (SELECT inventory_accno_id FROM defaults)
310 WHERE inventory_accno_id = $form->{id}|;
311 $dbh->do($query) || $form->dberror($query);
313 $query = qq|UPDATE parts
314 SET income_accno_id =
315 (SELECT income_accno_id FROM defaults)
316 WHERE income_accno_id = $form->{id}|;
317 $dbh->do($query) || $form->dberror($query);
319 $query = qq|UPDATE parts
320 SET expense_accno_id =
321 (SELECT expense_accno_id FROM defaults)
322 WHERE expense_accno_id = $form->{id}|;
323 $dbh->do($query) || $form->dberror($query);
325 foreach my $table (qw(partstax customertax vendortax tax)) {
326 $query = qq|DELETE FROM $table
327 WHERE chart_id = $form->{id}|;
328 $dbh->do($query) || $form->dberror($query);
331 # commit and redirect
332 my $rc = $dbh->commit;
335 $main::lxdebug->leave_sub();
341 $main::lxdebug->enter_sub();
343 my ($self, $myconfig, $form) = @_;
345 # connect to database
346 my $dbh = $form->dbconnect($myconfig);
348 my $query = qq|SELECT accno, description
352 $sth = $dbh->prepare($query);
353 $sth->execute || $form->dberror($query);
355 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
356 push @{ $form->{ALL} }, $ref;
362 $main::lxdebug->leave_sub();
366 $main::lxdebug->enter_sub();
368 my ($self, $myconfig, $form) = @_;
370 # connect to database
371 my $dbh = $form->dbconnect($myconfig);
373 my $query = qq|SELECT g.accno, g.description
375 WHERE g.accno = '$form->{accno}'|;
376 my $sth = $dbh->prepare($query);
377 $sth->execute || $form->dberror($query);
379 my $ref = $sth->fetchrow_hashref(NAME_lc);
381 map { $form->{$_} = $ref->{$_} } keys %$ref;
385 # check for transactions
386 $query = qq|SELECT count(*) FROM acc_trans a, chart c, gifi g
387 WHERE c.gifi_accno = g.accno
388 AND a.chart_id = c.id
389 AND g.accno = '$form->{accno}'|;
390 $sth = $dbh->prepare($query);
391 $sth->execute || $form->dberror($query);
393 ($form->{orphaned}) = $sth->fetchrow_array;
395 $form->{orphaned} = !$form->{orphaned};
399 $main::lxdebug->leave_sub();
403 $main::lxdebug->enter_sub();
405 my ($self, $myconfig, $form) = @_;
407 # connect to database
408 my $dbh = $form->dbconnect($myconfig);
410 $form->{description} =~ s/\'/\'\'/g;
412 # id is the old account number!
414 $query = qq|UPDATE gifi SET
415 accno = '$form->{accno}',
416 description = '$form->{description}'
417 WHERE accno = '$form->{id}'|;
419 $query = qq|INSERT INTO gifi
421 VALUES ('$form->{accno}', '$form->{description}')|;
423 $dbh->do($query) || $form->dberror($query);
427 $main::lxdebug->leave_sub();
431 $main::lxdebug->enter_sub();
433 my ($self, $myconfig, $form) = @_;
435 # connect to database
436 my $dbh = $form->dbconnect($myconfig);
438 # id is the old account number!
439 $query = qq|DELETE FROM gifi
440 WHERE accno = '$form->{id}'|;
441 $dbh->do($query) || $form->dberror($query);
445 $main::lxdebug->leave_sub();
449 $main::lxdebug->enter_sub();
451 my ($self, $myconfig, $form) = @_;
453 # connect to database
454 my $dbh = $form->dbconnect($myconfig);
456 my $query = qq|SELECT id, description
460 $sth = $dbh->prepare($query);
461 $sth->execute || $form->dberror($query);
463 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
464 push @{ $form->{ALL} }, $ref;
470 $main::lxdebug->leave_sub();
474 $main::lxdebug->enter_sub();
476 my ($self, $myconfig, $form) = @_;
478 # connect to database
479 my $dbh = $form->dbconnect($myconfig);
481 my $query = qq|SELECT w.description
483 WHERE w.id = $form->{id}|;
484 my $sth = $dbh->prepare($query);
485 $sth->execute || $form->dberror($query);
487 my $ref = $sth->fetchrow_hashref(NAME_lc);
489 map { $form->{$_} = $ref->{$_} } keys %$ref;
493 # see if it is in use
494 $query = qq|SELECT count(*) FROM inventory i
495 WHERE i.warehouse_id = $form->{id}|;
496 $sth = $dbh->prepare($query);
497 $sth->execute || $form->dberror($query);
499 ($form->{orphaned}) = $sth->fetchrow_array;
500 $form->{orphaned} = !$form->{orphaned};
505 $main::lxdebug->leave_sub();
509 $main::lxdebug->enter_sub();
511 my ($self, $myconfig, $form) = @_;
513 # connect to database
514 my $dbh = $form->dbconnect($myconfig);
516 $form->{description} =~ s/\'/\'\'/g;
519 $query = qq|UPDATE warehouse SET
520 description = '$form->{description}'
521 WHERE id = $form->{id}|;
523 $query = qq|INSERT INTO warehouse
525 VALUES ('$form->{description}')|;
527 $dbh->do($query) || $form->dberror($query);
531 $main::lxdebug->leave_sub();
534 sub delete_warehouse {
535 $main::lxdebug->enter_sub();
537 my ($self, $myconfig, $form) = @_;
539 # connect to database
540 my $dbh = $form->dbconnect($myconfig);
542 $query = qq|DELETE FROM warehouse
543 WHERE id = $form->{id}|;
544 $dbh->do($query) || $form->dberror($query);
548 $main::lxdebug->leave_sub();
552 $main::lxdebug->enter_sub();
554 my ($self, $myconfig, $form) = @_;
556 # connect to database
557 my $dbh = $form->dbconnect($myconfig);
559 my $query = qq|SELECT d.id, d.description, d.role
563 $sth = $dbh->prepare($query);
564 $sth->execute || $form->dberror($query);
566 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
567 push @{ $form->{ALL} }, $ref;
573 $main::lxdebug->leave_sub();
577 $main::lxdebug->enter_sub();
579 my ($self, $myconfig, $form) = @_;
581 # connect to database
582 my $dbh = $form->dbconnect($myconfig);
584 my $query = qq|SELECT d.description, d.role
586 WHERE d.id = $form->{id}|;
587 my $sth = $dbh->prepare($query);
588 $sth->execute || $form->dberror($query);
590 my $ref = $sth->fetchrow_hashref(NAME_lc);
592 map { $form->{$_} = $ref->{$_} } keys %$ref;
596 # see if it is in use
597 $query = qq|SELECT count(*) FROM dpt_trans d
598 WHERE d.department_id = $form->{id}|;
599 $sth = $dbh->prepare($query);
600 $sth->execute || $form->dberror($query);
602 ($form->{orphaned}) = $sth->fetchrow_array;
603 $form->{orphaned} = !$form->{orphaned};
608 $main::lxdebug->leave_sub();
611 sub save_department {
612 $main::lxdebug->enter_sub();
614 my ($self, $myconfig, $form) = @_;
616 # connect to database
617 my $dbh = $form->dbconnect($myconfig);
619 $form->{description} =~ s/\'/\'\'/g;
622 $query = qq|UPDATE department SET
623 description = '$form->{description}',
624 role = '$form->{role}'
625 WHERE id = $form->{id}|;
627 $query = qq|INSERT INTO department
629 VALUES ('$form->{description}', '$form->{role}')|;
631 $dbh->do($query) || $form->dberror($query);
635 $main::lxdebug->leave_sub();
638 sub delete_department {
639 $main::lxdebug->enter_sub();
641 my ($self, $myconfig, $form) = @_;
643 # connect to database
644 my $dbh = $form->dbconnect($myconfig);
646 $query = qq|DELETE FROM department
647 WHERE id = $form->{id}|;
648 $dbh->do($query) || $form->dberror($query);
652 $main::lxdebug->leave_sub();
656 $main::lxdebug->enter_sub();
658 my ($self, $myconfig, $form) = @_;
660 # connect to database
661 my $dbh = $form->dbconnect($myconfig);
663 my $query = qq|SELECT id, lead
667 $sth = $dbh->prepare($query);
668 $sth->execute || $form->dberror($query);
670 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
671 push @{ $form->{ALL} }, $ref;
677 $main::lxdebug->leave_sub();
681 $main::lxdebug->enter_sub();
683 my ($self, $myconfig, $form) = @_;
685 # connect to database
686 my $dbh = $form->dbconnect($myconfig);
689 qq|SELECT l.id, l.lead
691 WHERE l.id = $form->{id}|;
692 my $sth = $dbh->prepare($query);
693 $sth->execute || $form->dberror($query);
695 my $ref = $sth->fetchrow_hashref(NAME_lc);
697 map { $form->{$_} = $ref->{$_} } keys %$ref;
703 $main::lxdebug->leave_sub();
707 $main::lxdebug->enter_sub();
709 my ($self, $myconfig, $form) = @_;
711 # connect to database
712 my $dbh = $form->dbconnect($myconfig);
714 $form->{lead} =~ s/\'/\'\'/g;
716 # id is the old record
718 $query = qq|UPDATE leads SET
719 lead = '$form->{description}'
720 WHERE id = $form->{id}|;
722 $query = qq|INSERT INTO leads
724 VALUES ('$form->{description}')|;
726 $dbh->do($query) || $form->dberror($query);
730 $main::lxdebug->leave_sub();
734 $main::lxdebug->enter_sub();
736 my ($self, $myconfig, $form) = @_;
738 # connect to database
739 my $dbh = $form->dbconnect($myconfig);
741 $query = qq|DELETE FROM leads
742 WHERE id = $form->{id}|;
743 $dbh->do($query) || $form->dberror($query);
747 $main::lxdebug->leave_sub();
751 $main::lxdebug->enter_sub();
753 my ($self, $myconfig, $form) = @_;
755 # connect to database
756 my $dbh = $form->dbconnect($myconfig);
758 my $query = qq|SELECT id, description, discount, customernumberinit, salesman
762 $sth = $dbh->prepare($query);
763 $sth->execute || $form->dberror($query);
765 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
766 push @{ $form->{ALL} }, $ref;
772 $main::lxdebug->leave_sub();
776 $main::lxdebug->enter_sub();
778 my ($self, $myconfig, $form) = @_;
780 # connect to database
781 my $dbh = $form->dbconnect($myconfig);
784 qq|SELECT b.description, b.discount, b.customernumberinit, b.salesman
786 WHERE b.id = $form->{id}|;
787 my $sth = $dbh->prepare($query);
788 $sth->execute || $form->dberror($query);
790 my $ref = $sth->fetchrow_hashref(NAME_lc);
792 map { $form->{$_} = $ref->{$_} } keys %$ref;
798 $main::lxdebug->leave_sub();
802 $main::lxdebug->enter_sub();
804 my ($self, $myconfig, $form) = @_;
806 # connect to database
807 my $dbh = $form->dbconnect($myconfig);
809 $form->{description} =~ s/\'/\'\'/g;
810 $form->{discount} /= 100;
811 $form->{salesman} *= 1;
813 # id is the old record
815 $query = qq|UPDATE business SET
816 description = '$form->{description}',
817 discount = $form->{discount},
818 customernumberinit = '$form->{customernumberinit}',
819 salesman = '$form->{salesman}'
820 WHERE id = $form->{id}|;
822 $query = qq|INSERT INTO business
823 (description, discount, customernumberinit, salesman)
824 VALUES ('$form->{description}', $form->{discount}, '$form->{customernumberinit}', '$form->{salesman}')|;
826 $dbh->do($query) || $form->dberror($query);
830 $main::lxdebug->leave_sub();
833 sub delete_business {
834 $main::lxdebug->enter_sub();
836 my ($self, $myconfig, $form) = @_;
838 # connect to database
839 my $dbh = $form->dbconnect($myconfig);
841 $query = qq|DELETE FROM business
842 WHERE id = $form->{id}|;
843 $dbh->do($query) || $form->dberror($query);
847 $main::lxdebug->leave_sub();
852 $main::lxdebug->enter_sub();
854 my ($self, $myconfig, $form, $return_list) = @_;
856 # connect to database
857 my $dbh = $form->dbconnect($myconfig);
860 "SELECT id, description, template_code, article_code, " .
861 " output_numberformat, output_dateformat, output_longdates " .
862 "FROM language ORDER BY description";
864 $sth = $dbh->prepare($query);
865 $sth->execute || $form->dberror($query);
869 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
870 push(@{ $ary }, $ref);
876 $main::lxdebug->leave_sub();
886 $main::lxdebug->enter_sub();
888 my ($self, $myconfig, $form) = @_;
890 # connect to database
891 my $dbh = $form->dbconnect($myconfig);
894 "SELECT description, template_code, article_code, " .
895 " output_numberformat, output_dateformat, output_longdates " .
896 "FROM language WHERE id = ?";
897 my $sth = $dbh->prepare($query);
898 $sth->execute($form->{"id"}) || $form->dberror($query . " ($form->{id})");
900 my $ref = $sth->fetchrow_hashref(NAME_lc);
902 map { $form->{$_} = $ref->{$_} } keys %$ref;
908 $main::lxdebug->leave_sub();
911 sub get_language_details {
912 $main::lxdebug->enter_sub();
914 my ($self, $myconfig, $form, $id) = @_;
916 # connect to database
917 my $dbh = $form->dbconnect($myconfig);
920 "SELECT template_code, " .
921 " output_numberformat, output_dateformat, output_longdates " .
922 "FROM language WHERE id = ?";
923 my @res = $dbh->selectrow_array($query, undef, $id);
926 $main::lxdebug->leave_sub();
932 $main::lxdebug->enter_sub();
934 my ($self, $myconfig, $form) = @_;
936 # connect to database
937 my $dbh = $form->dbconnect($myconfig);
938 my (@values, $query);
940 map({ push(@values, $form->{$_}); }
941 qw(description template_code article_code
942 output_numberformat output_dateformat output_longdates));
944 # id is the old record
947 "UPDATE language SET " .
948 " description = ?, template_code = ?, article_code = ?, " .
949 " output_numberformat = ?, output_dateformat = ?, " .
950 " output_longdates = ? " .
952 push(@values, $form->{id});
955 "INSERT INTO language (" .
956 " description, template_code, article_code, " .
957 " output_numberformat, output_dateformat, output_longdates" .
958 ") VALUES (?, ?, ?, ?, ?, ?)";
960 $dbh->do($query, undef, @values) ||
961 $form->dberror($query . " (" . join(", ", @values) . ")");
965 $main::lxdebug->leave_sub();
968 sub delete_language {
969 $main::lxdebug->enter_sub();
971 my ($self, $myconfig, $form) = @_;
973 # connect to database
974 my $dbh = $form->dbconnect_noauto($myconfig);
976 my $query = "DELETE FROM units_language WHERE language_id = ?";
977 $dbh->do($query, undef, $form->{"id"}) ||
978 $form->dberror($query . " ($form->{id})");
980 $query = "DELETE FROM language WHERE id = ?";
981 $dbh->do($query, undef, $form->{"id"}) ||
982 $form->dberror($query . " ($form->{id})");
987 $main::lxdebug->leave_sub();
992 $main::lxdebug->enter_sub();
994 my ($self, $myconfig, $form) = @_;
996 # connect to database
997 my $dbh = $form->dbconnect($myconfig);
999 my $query = qq|SELECT id, description, inventory_accno_id, (select accno from chart where id=inventory_accno_id) as inventory_accno, income_accno_id_0, (select accno from chart where id=income_accno_id_0) as income_accno_0, expense_accno_id_0, (select accno from chart where id=expense_accno_id_0) as expense_accno_0, income_accno_id_1, (select accno from chart where id=income_accno_id_1) as income_accno_1, expense_accno_id_1, (select accno from chart where id=expense_accno_id_1) as expense_accno_1, income_accno_id_2, (select accno from chart where id=income_accno_id_2) as income_accno_2, expense_accno_id_2, (select accno from chart where id=expense_accno_id_2) as expense_accno_2, income_accno_id_3, (select accno from chart where id=income_accno_id_3) as income_accno_3, expense_accno_id_3, (select accno from chart where id=expense_accno_id_3) as expense_accno_3
1000 FROM buchungsgruppen
1003 $sth = $dbh->prepare($query);
1004 $sth->execute || $form->dberror($query);
1006 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1007 push @{ $form->{ALL} }, $ref;
1013 $main::lxdebug->leave_sub();
1016 sub get_buchungsgruppe {
1017 $main::lxdebug->enter_sub();
1019 my ($self, $myconfig, $form) = @_;
1021 # connect to database
1022 my $dbh = $form->dbconnect($myconfig);
1026 qq|SELECT description, inventory_accno_id, (select accno from chart where id=inventory_accno_id) as inventory_accno, income_accno_id_0, (select accno from chart where id=income_accno_id_0) as income_accno_0, expense_accno_id_0, (select accno from chart where id=expense_accno_id_0) as expense_accno_0, income_accno_id_1, (select accno from chart where id=income_accno_id_1) as income_accno_1, expense_accno_id_1, (select accno from chart where id=expense_accno_id_1) as expense_accno_1, income_accno_id_2, (select accno from chart where id=income_accno_id_2) as income_accno_2, expense_accno_id_2, (select accno from chart where id=expense_accno_id_2) as expense_accno_2, income_accno_id_3, (select accno from chart where id=income_accno_id_3) as income_accno_3, expense_accno_id_3, (select accno from chart where id=expense_accno_id_3) as expense_accno_3
1027 FROM buchungsgruppen
1028 WHERE id = $form->{id}|;
1029 my $sth = $dbh->prepare($query);
1030 $sth->execute || $form->dberror($query);
1032 my $ref = $sth->fetchrow_hashref(NAME_lc);
1034 map { $form->{$_} = $ref->{$_} } keys %$ref;
1039 qq|SELECT count(id) as anzahl
1041 WHERE buchungsgruppen_id = $form->{id}|;
1042 my $sth = $dbh->prepare($query);
1043 $sth->execute || $form->dberror($query);
1045 my $ref = $sth->fetchrow_hashref(NAME_lc);
1046 if (!$ref->{anzahl}) {
1047 $form->{orphaned} = 1;
1053 $query = "SELECT inventory_accno_id, income_accno_id, expense_accno_id ".
1055 ($form->{"std_inventory_accno_id"}, $form->{"std_income_accno_id"},
1056 $form->{"std_expense_accno_id"}) = $dbh->selectrow_array($query);
1059 $query = qq|SELECT c.accno, c.description, c.link, c.id,
1060 d.inventory_accno_id, d.income_accno_id, d.expense_accno_id
1061 FROM chart c, defaults d
1062 WHERE c.link LIKE '%$module%'
1066 my $sth = $dbh->prepare($query);
1067 $sth->execute || $form->dberror($query);
1068 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1069 foreach my $key (split(/:/, $ref->{link})) {
1070 if (!$form->{"std_inventory_accno_id"} && ($key eq "IC")) {
1071 $form->{"std_inventory_accno_id"} = $ref->{"id"};
1073 if ($key =~ /$module/) {
1074 if ( ($ref->{id} eq $ref->{inventory_accno_id})
1075 || ($ref->{id} eq $ref->{income_accno_id})
1076 || ($ref->{id} eq $ref->{expense_accno_id})) {
1077 push @{ $form->{"${module}_links"}{$key} },
1078 { accno => $ref->{accno},
1079 description => $ref->{description},
1080 selected => "selected",
1083 push @{ $form->{"${module}_links"}{$key} },
1084 { accno => $ref->{accno},
1085 description => $ref->{description},
1097 $main::lxdebug->leave_sub();
1100 sub save_buchungsgruppe {
1101 $main::lxdebug->enter_sub();
1103 my ($self, $myconfig, $form) = @_;
1105 # connect to database
1106 my $dbh = $form->dbconnect($myconfig);
1108 $form->{description} =~ s/\'/\'\'/g;
1111 # id is the old record
1113 $query = qq|UPDATE buchungsgruppen SET
1114 description = '$form->{description}',
1115 inventory_accno_id = '$form->{inventory_accno_id}',
1116 income_accno_id_0 = '$form->{income_accno_id_0}',
1117 expense_accno_id_0 = '$form->{expense_accno_id_0}',
1118 income_accno_id_1 = '$form->{income_accno_id_1}',
1119 expense_accno_id_1 = '$form->{expense_accno_id_1}',
1120 income_accno_id_2 = '$form->{income_accno_id_2}',
1121 expense_accno_id_2 = '$form->{expense_accno_id_2}',
1122 income_accno_id_3 = '$form->{income_accno_id_3}',
1123 expense_accno_id_3 = '$form->{expense_accno_id_3}'
1124 WHERE id = $form->{id}|;
1126 $query = qq|INSERT INTO buchungsgruppen
1127 (description, inventory_accno_id, income_accno_id_0, expense_accno_id_0, income_accno_id_1, expense_accno_id_1, income_accno_id_2, expense_accno_id_2, income_accno_id_3, expense_accno_id_3)
1128 VALUES ('$form->{description}', '$form->{inventory_accno_id}', '$form->{income_accno_id_0}', '$form->{expense_accno_id_0}', '$form->{income_accno_id_1}', '$form->{expense_accno_id_1}', '$form->{income_accno_id_2}', '$form->{expense_accno_id_2}', '$form->{income_accno_id_3}', '$form->{expense_accno_id_3}')|;
1130 $dbh->do($query) || $form->dberror($query);
1134 $main::lxdebug->leave_sub();
1137 sub delete_buchungsgruppe {
1138 $main::lxdebug->enter_sub();
1140 my ($self, $myconfig, $form) = @_;
1142 # connect to database
1143 my $dbh = $form->dbconnect($myconfig);
1145 $query = qq|DELETE FROM buchungsgruppen
1146 WHERE id = $form->{id}|;
1147 $dbh->do($query) || $form->dberror($query);
1151 $main::lxdebug->leave_sub();
1155 $main::lxdebug->enter_sub();
1157 my ($self, $myconfig, $form) = @_;
1159 # connect to database
1160 my $dbh = $form->dbconnect($myconfig);
1162 my $query = qq|SELECT id, printer_description, template_code, printer_command
1166 $sth = $dbh->prepare($query);
1167 $sth->execute || $form->dberror($query);
1169 $form->{"ALL"} = [];
1170 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1171 push @{ $form->{ALL} }, $ref;
1177 $main::lxdebug->leave_sub();
1181 $main::lxdebug->enter_sub();
1183 my ($self, $myconfig, $form) = @_;
1185 # connect to database
1186 my $dbh = $form->dbconnect($myconfig);
1189 qq|SELECT p.printer_description, p.template_code, p.printer_command
1191 WHERE p.id = $form->{id}|;
1192 my $sth = $dbh->prepare($query);
1193 $sth->execute || $form->dberror($query);
1195 my $ref = $sth->fetchrow_hashref(NAME_lc);
1197 map { $form->{$_} = $ref->{$_} } keys %$ref;
1203 $main::lxdebug->leave_sub();
1207 $main::lxdebug->enter_sub();
1209 my ($self, $myconfig, $form) = @_;
1211 # connect to database
1212 my $dbh = $form->dbconnect($myconfig);
1214 $form->{printer_description} =~ s/\'/\'\'/g;
1215 $form->{printer_command} =~ s/\'/\'\'/g;
1216 $form->{template_code} =~ s/\'/\'\'/g;
1219 # id is the old record
1221 $query = qq|UPDATE printers SET
1222 printer_description = '$form->{printer_description}',
1223 template_code = '$form->{template_code}',
1224 printer_command = '$form->{printer_command}'
1225 WHERE id = $form->{id}|;
1227 $query = qq|INSERT INTO printers
1228 (printer_description, template_code, printer_command)
1229 VALUES ('$form->{printer_description}', '$form->{template_code}', '$form->{printer_command}')|;
1231 $dbh->do($query) || $form->dberror($query);
1235 $main::lxdebug->leave_sub();
1238 sub delete_printer {
1239 $main::lxdebug->enter_sub();
1241 my ($self, $myconfig, $form) = @_;
1243 # connect to database
1244 my $dbh = $form->dbconnect($myconfig);
1246 $query = qq|DELETE FROM printers
1247 WHERE id = $form->{id}|;
1248 $dbh->do($query) || $form->dberror($query);
1252 $main::lxdebug->leave_sub();
1256 $main::lxdebug->enter_sub();
1258 my ($self, $myconfig, $form) = @_;
1260 # connect to database
1261 my $dbh = $form->dbconnect($myconfig);
1263 my $query = qq|SELECT *
1267 $sth = $dbh->prepare($query);
1268 $sth->execute || $form->dberror($query);
1270 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1271 $ref->{percent_skonto} = $form->format_amount($myconfig,($ref->{percent_skonto} * 100));
1272 push @{ $form->{ALL} }, $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);
1292 WHERE id = $form->{id}|;
1293 my $sth = $dbh->prepare($query);
1294 $sth->execute || $form->dberror($query);
1296 my $ref = $sth->fetchrow_hashref(NAME_lc);
1297 $ref->{percent_skonto} = $form->format_amount($myconfig,($ref->{percent_skonto} * 100));
1299 map { $form->{$_} = $ref->{$_} } keys %$ref;
1305 $main::lxdebug->leave_sub();
1309 $main::lxdebug->enter_sub();
1311 my ($self, $myconfig, $form) = @_;
1313 # connect to database
1314 my $dbh = $form->dbconnect($myconfig);
1316 $form->{description} =~ s/\'/\'\'/g;
1317 $form->{description_long} =~ s/\'/\'\'/g;
1318 $percentskonto = $form->parse_amount($myconfig, $form->{percent_skonto}) /100;
1319 $form->{ranking} *= 1;
1320 $form->{terms_netto} *= 1;
1321 $form->{terms_skonto} *= 1;
1322 $form->{percent_skonto} *= 1;
1326 # id is the old record
1328 $query = qq|UPDATE payment_terms SET
1329 description = '$form->{description}',
1330 ranking = $form->{ranking},
1331 description_long = '$form->{description_long}',
1332 terms_netto = $form->{terms_netto},
1333 terms_skonto = $form->{terms_skonto},
1334 percent_skonto = $percentskonto
1335 WHERE id = $form->{id}|;
1337 $query = qq|INSERT INTO payment_terms
1338 (description, ranking, description_long, terms_netto, terms_skonto, percent_skonto)
1339 VALUES ('$form->{description}', $form->{ranking}, '$form->{description_long}', $form->{terms_netto}, $form->{terms_skonto}, $percentskonto)|;
1341 $dbh->do($query) || $form->dberror($query);
1345 $main::lxdebug->leave_sub();
1348 sub delete_payment {
1349 $main::lxdebug->enter_sub();
1351 my ($self, $myconfig, $form) = @_;
1353 # connect to database
1354 my $dbh = $form->dbconnect($myconfig);
1356 $query = qq|DELETE FROM payment_terms
1357 WHERE id = $form->{id}|;
1358 $dbh->do($query) || $form->dberror($query);
1362 $main::lxdebug->leave_sub();
1366 $main::lxdebug->enter_sub();
1368 my ($self, $myconfig, $form) = @_;
1370 # connect to database
1371 my $dbh = $form->dbconnect($myconfig);
1373 my $query = qq|SELECT code, sictype, description
1377 $sth = $dbh->prepare($query);
1378 $sth->execute || $form->dberror($query);
1380 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1381 push @{ $form->{ALL} }, $ref;
1387 $main::lxdebug->leave_sub();
1391 $main::lxdebug->enter_sub();
1393 my ($self, $myconfig, $form) = @_;
1395 # connect to database
1396 my $dbh = $form->dbconnect($myconfig);
1398 my $query = qq|SELECT s.code, s.sictype, s.description
1400 WHERE s.code = '$form->{code}'|;
1401 my $sth = $dbh->prepare($query);
1402 $sth->execute || $form->dberror($query);
1404 my $ref = $sth->fetchrow_hashref(NAME_lc);
1406 map { $form->{$_} = $ref->{$_} } keys %$ref;
1412 $main::lxdebug->leave_sub();
1416 $main::lxdebug->enter_sub();
1418 my ($self, $myconfig, $form) = @_;
1420 # connect to database
1421 my $dbh = $form->dbconnect($myconfig);
1423 $form->{code} =~ s/\'/\'\'/g;
1424 $form->{description} =~ s/\'/\'\'/g;
1428 $query = qq|UPDATE sic SET
1429 code = '$form->{code}',
1430 sictype = '$form->{sictype}',
1431 description = '$form->{description}'
1432 WHERE code = '$form->{id}'|;
1434 $query = qq|INSERT INTO sic
1435 (code, sictype, description)
1436 VALUES ('$form->{code}', '$form->{sictype}', '$form->{description}')|;
1438 $dbh->do($query) || $form->dberror($query);
1442 $main::lxdebug->leave_sub();
1446 $main::lxdebug->enter_sub();
1448 my ($self, $myconfig, $form) = @_;
1450 # connect to database
1451 my $dbh = $form->dbconnect($myconfig);
1453 $query = qq|DELETE FROM sic
1454 WHERE code = '$form->{code}'|;
1455 $dbh->do($query) || $form->dberror($query);
1459 $main::lxdebug->leave_sub();
1463 $main::lxdebug->enter_sub();
1465 my ($self, $form) = @_;
1467 open(TEMPLATE, "$form->{file}") or $form->error("$form->{file} : $!");
1469 while (<TEMPLATE>) {
1470 $form->{body} .= $_;
1475 $main::lxdebug->leave_sub();
1479 $main::lxdebug->enter_sub();
1481 my ($self, $form) = @_;
1483 open(TEMPLATE, ">$form->{file}") or $form->error("$form->{file} : $!");
1486 $form->{body} =~ s/\r\n/\n/g;
1487 print TEMPLATE $form->{body};
1491 $main::lxdebug->leave_sub();
1494 sub save_preferences {
1495 $main::lxdebug->enter_sub();
1497 my ($self, $myconfig, $form, $memberfile, $userspath, $webdav) = @_;
1499 map { ($form->{$_}) = split(/--/, $form->{$_}) }
1500 qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno);
1503 $form->{curr} =~ s/ //g;
1504 map { push(@a, uc pack "A3", $_) if $_ } split(/:/, $form->{curr});
1505 $form->{curr} = join ':', @a;
1507 # connect to database
1508 my $dbh = $form->dbconnect_noauto($myconfig);
1510 # these defaults are database wide
1511 # user specific variables are in myconfig
1513 my $query = qq|UPDATE defaults SET
1514 inventory_accno_id =
1515 (SELECT c.id FROM chart c
1516 WHERE c.accno = '$form->{inventory_accno}'),
1518 (SELECT c.id FROM chart c
1519 WHERE c.accno = '$form->{income_accno}'),
1521 (SELECT c.id FROM chart c
1522 WHERE c.accno = '$form->{expense_accno}'),
1524 (SELECT c.id FROM chart c
1525 WHERE c.accno = '$form->{fxgain_accno}'),
1527 (SELECT c.id FROM chart c
1528 WHERE c.accno = '$form->{fxloss_accno}'),
1529 invnumber = '$form->{invnumber}',
1530 cnnumber = '$form->{cnnumber}',
1531 sonumber = '$form->{sonumber}',
1532 ponumber = '$form->{ponumber}',
1533 sqnumber = '$form->{sqnumber}',
1534 rfqnumber = '$form->{rfqnumber}',
1535 customernumber = '$form->{customernumber}',
1536 vendornumber = '$form->{vendornumber}',
1537 articlenumber = '$form->{articlenumber}',
1538 servicenumber = '$form->{servicenumber}',
1539 yearend = '$form->{yearend}',
1540 curr = '$form->{curr}',
1541 businessnumber = '$form->{businessnumber}'
1543 $dbh->do($query) || $form->dberror($query);
1546 my $name = $form->{name};
1547 $name =~ s/\'/\'\'/g;
1548 $query = qq|UPDATE employee
1550 WHERE login = '$form->{login}'|;
1551 $dbh->do($query) || $form->dberror($query);
1553 # foreach my $item (split(/ /, $form->{taxaccounts})) {
1554 # $query = qq|UPDATE tax
1555 # SET rate = | . ($form->{$item} / 100) . qq|,
1556 # taxnumber = '$form->{"taxnumber_$item"}'
1557 # WHERE chart_id = $item|;
1558 # $dbh->do($query) || $form->dberror($query);
1561 my $rc = $dbh->commit;
1564 # save first currency in myconfig
1565 $form->{currency} = substr($form->{curr}, 0, 3);
1567 my $myconfig = new User "$memberfile", "$form->{login}";
1569 foreach my $item (keys %$form) {
1570 $myconfig->{$item} = $form->{$item};
1573 $myconfig->save_member($memberfile, $userspath);
1577 qw(angebote bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen);
1578 foreach $directory (@webdavdirs) {
1579 $file = "webdav/" . $directory . "/webdav-user";
1580 if ($myconfig->{$directory}) {
1581 open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1582 while (<HTACCESS>) {
1583 ($login, $password) = split(/:/, $_);
1584 if ($login ne $form->{login}) {
1589 open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1590 $newfile .= $myconfig->{login} . ":" . $myconfig->{password} . "\n";
1591 print(HTACCESS $newfile);
1594 $form->{$directory} = 0;
1595 open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
1596 while (<HTACCESS>) {
1597 ($login, $password) = split(/:/, $_);
1598 if ($login ne $form->{login}) {
1603 open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
1604 print(HTACCESS $newfile);
1610 $main::lxdebug->leave_sub();
1615 sub defaultaccounts {
1616 $main::lxdebug->enter_sub();
1618 my ($self, $myconfig, $form) = @_;
1620 # connect to database
1621 my $dbh = $form->dbconnect($myconfig);
1623 # get defaults from defaults table
1624 my $query = qq|SELECT * FROM defaults|;
1625 my $sth = $dbh->prepare($query);
1626 $sth->execute || $form->dberror($query);
1628 $form->{defaults} = $sth->fetchrow_hashref(NAME_lc);
1629 $form->{defaults}{IC} = $form->{defaults}{inventory_accno_id};
1630 $form->{defaults}{IC_income} = $form->{defaults}{income_accno_id};
1631 $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
1632 $form->{defaults}{FX_gain} = $form->{defaults}{fxgain_accno_id};
1633 $form->{defaults}{FX_loss} = $form->{defaults}{fxloss_accno_id};
1637 $query = qq|SELECT c.id, c.accno, c.description, c.link
1639 WHERE c.link LIKE '%IC%'
1641 $sth = $dbh->prepare($query);
1642 $sth->execute || $self->dberror($query);
1644 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1645 foreach my $key (split(/:/, $ref->{link})) {
1648 if ($key =~ /cogs/) {
1649 $nkey = "IC_expense";
1651 if ($key =~ /sale/) {
1652 $nkey = "IC_income";
1654 %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
1656 description => $ref->{description}
1663 $query = qq|SELECT c.id, c.accno, c.description
1665 WHERE c.category = 'I'
1666 AND c.charttype = 'A'
1668 $sth = $dbh->prepare($query);
1669 $sth->execute || $self->dberror($query);
1671 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1672 %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
1674 description => $ref->{description}
1679 $query = qq|SELECT c.id, c.accno, c.description
1681 WHERE c.category = 'E'
1682 AND c.charttype = 'A'
1684 $sth = $dbh->prepare($query);
1685 $sth->execute || $self->dberror($query);
1687 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1688 %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
1690 description => $ref->{description}
1695 # now get the tax rates and numbers
1696 $query = qq|SELECT c.id, c.accno, c.description,
1697 t.rate * 100 AS rate, t.taxnumber
1699 WHERE c.id = t.chart_id|;
1701 $sth = $dbh->prepare($query);
1702 $sth->execute || $form->dberror($query);
1704 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1705 $form->{taxrates}{ $ref->{accno} }{id} = $ref->{id};
1706 $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
1707 $form->{taxrates}{ $ref->{accno} }{taxnumber} = $ref->{taxnumber}
1708 if $ref->{taxnumber};
1709 $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
1715 $main::lxdebug->leave_sub();
1719 $main::lxdebug->enter_sub();
1721 my ($self, $myconfig, $form, $userspath) = @_;
1725 my $boundary = time;
1727 "$userspath/$boundary.$myconfig->{dbname}-$form->{dbversion}.sql";
1728 my $out = $form->{OUT};
1729 $form->{OUT} = ">$tmpfile";
1731 if ($form->{media} eq 'email') {
1736 $mail->{to} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
1737 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
1739 "Lx-Office Backup / $myconfig->{dbname}-$form->{dbversion}.sql";
1740 @{ $mail->{attachments} } = ($tmpfile);
1741 $mail->{version} = $form->{version};
1742 $mail->{fileid} = "$boundary.";
1744 $myconfig->{signature} =~ s/\\n/\r\n/g;
1745 $mail->{message} = "--\n$myconfig->{signature}";
1749 open(OUT, "$form->{OUT}") or $form->error("$form->{OUT} : $!");
1751 # get sequences, functions and triggers
1752 open(FH, "sql/lx-office.sql") or $form->error("sql/lx-office.sql : $!");
1765 # Remove DOS and Unix style line endings.
1768 # ignore comments or empty lines
1769 next if /^(--.*|\s+)$/;
1771 for (my $i = 0; $i < length($_); $i++) {
1772 my $char = substr($_, $i, 1);
1774 # Are we inside a string?
1776 if ($char eq $quote_chars[-1]) {
1782 if (($char eq "'") || ($char eq "\"")) {
1783 push(@quote_chars, $char);
1785 } elsif ($char eq ";") {
1787 # Query is complete. Check for triggers and functions.
1788 if ($query =~ /^create\s+function\s+\"?(\w+)\"?/i) {
1789 push(@functions, $query);
1791 } elsif ($query =~ /^create\s+trigger\s+\"?(\w+)\"?/i) {
1792 push(@triggers, $query);
1794 } elsif ($query =~ /^create\s+sequence\s+\"?(\w+)\"?/i) {
1795 push(@sequences, $1);
1797 } elsif ($query =~ /^create\s+table\s+\"?(\w+)\"?/i) {
1798 $tablespecs{$1} = $query;
1800 } elsif ($query =~ /^create\s+index\s+\"?(\w+)\"?/i) {
1801 push(@indices, $query);
1815 # connect to database
1816 my $dbh = $form->dbconnect($myconfig);
1818 # get all the tables
1819 my @tables = $dbh->tables('', '', 'customer', '', { noprefix => 0 });
1821 my $today = scalar localtime;
1823 $myconfig->{dbhost} = 'localhost' unless $myconfig->{dbhost};
1825 print OUT qq|-- Lx-Office Backup
1826 -- Dataset: $myconfig->{dbname}
1827 -- Version: $form->{dbversion}
1828 -- Host: $myconfig->{dbhost}
1829 -- Login: $form->{login}
1830 -- User: $myconfig->{name}
1834 $myconfig->{dboptions};
1838 print OUT "-- DROP Sequences\n";
1840 foreach $item (@sequences) {
1841 print OUT qq|DROP SEQUENCE $item;\n|;
1844 print OUT "-- DROP Triggers\n";
1846 foreach $item (@triggers) {
1847 if ($item =~ /^create\s+trigger\s+\"?(\w+)\"?\s+.*on\s+\"?(\w+)\"?\s+/i) {
1848 print OUT qq|DROP TRIGGER "$1" ON "$2";\n|;
1852 print OUT "-- DROP Functions\n";
1854 foreach $item (@functions) {
1855 if ($item =~ /^create\s+function\s+\"?(\w+)\"?/i) {
1856 print OUT qq|DROP FUNCTION "$1" ();\n|;
1860 foreach $table (@tables) {
1861 if (!($table =~ /^sql_.*/)) {
1862 my $query = qq|SELECT * FROM $table|;
1864 my $sth = $dbh->prepare($query);
1865 $sth->execute || $form->dberror($query);
1867 $query = "INSERT INTO $table (";
1868 map { $query .= qq|$sth->{NAME}->[$_],| }
1869 (0 .. $sth->{NUM_OF_FIELDS} - 1);
1872 $query .= ") VALUES";
1874 if ($tablespecs{$table}) {
1876 print(OUT "DROP TABLE $table;\n");
1877 print(OUT $tablespecs{$table}, ";\n");
1880 print(OUT "DELETE FROM $table;\n");
1882 while (my @arr = $sth->fetchrow_array) {
1885 foreach my $item (@arr) {
1886 if (defined $item) {
1887 $item =~ s/\'/\'\'/g;
1888 $fields .= qq|'$item',|;
1897 print OUT qq|$query $fields;\n|;
1904 # create indices, sequences, functions and triggers
1906 print(OUT "-- CREATE Indices\n");
1907 map({ print(OUT "$_;\n"); } @indices);
1909 print OUT "-- CREATE Sequences\n";
1910 foreach $item (@sequences) {
1911 $query = qq|SELECT last_value FROM $item|;
1912 $sth = $dbh->prepare($query);
1913 $sth->execute || $form->dberror($query);
1914 my ($id) = $sth->fetchrow_array;
1918 CREATE SEQUENCE $item START $id;
1922 print OUT "-- CREATE Functions\n";
1925 map { print(OUT $_, ";\n"); } @functions;
1927 print OUT "-- CREATE Triggers\n";
1930 map { print(OUT $_, ";\n"); } @triggers;
1937 my @args = ("gzip", "$tmpfile");
1938 system(@args) == 0 or $form->error("$args[0] : $?");
1942 if ($form->{media} eq 'email') {
1943 @{ $mail->{attachments} } = ($tmpfile);
1944 $err = $mail->send($out);
1947 if ($form->{media} eq 'file') {
1949 open(IN, "$tmpfile") or $form->error("$tmpfile : $!");
1950 open(OUT, ">-") or $form->error("STDOUT : $!");
1952 print OUT qq|Content-Type: application/x-tar-gzip;
1953 Content-Disposition: attachment; filename="$myconfig->{dbname}-$form->{dbversion}.sql.gz"
1968 $main::lxdebug->leave_sub();
1972 $main::lxdebug->enter_sub();
1974 my ($self, $myconfig, $form) = @_;
1976 my $dbh = $form->dbconnect($myconfig);
1978 my $query = qq|SELECT closedto, revtrans FROM defaults|;
1979 my $sth = $dbh->prepare($query);
1980 $sth->execute || $form->dberror($query);
1982 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
1988 $main::lxdebug->leave_sub();
1992 $main::lxdebug->enter_sub();
1994 my ($self, $myconfig, $form) = @_;
1996 my $dbh = $form->dbconnect($myconfig);
1998 if ($form->{revtrans}) {
2000 $query = qq|UPDATE defaults SET closedto = NULL,
2002 } elsif ($form->{closedto}) {
2004 $query = qq|UPDATE defaults SET closedto = '$form->{closedto}',
2008 $query = qq|UPDATE defaults SET closedto = NULL,
2012 # set close in defaults
2013 $dbh->do($query) || $form->dberror($query);
2017 $main::lxdebug->leave_sub();
2021 my ($self, $units, $unit_name, $factor) = @_;
2023 $factor = 1 unless ($factor);
2025 my $unit = $units->{$unit_name};
2027 if (!defined($unit) || !$unit->{"base_unit"} ||
2028 ($unit_name eq $unit->{"base_unit"})) {
2029 return ($unit_name, $factor);
2032 return AM->get_base_unit($units, $unit->{"base_unit"}, $factor * $unit->{"factor"});
2035 sub retrieve_units {
2036 $main::lxdebug->enter_sub();
2038 my ($self, $myconfig, $form, $type, $prefix) = @_;
2040 my $dbh = $form->dbconnect($myconfig);
2042 my $query = "SELECT *, base_unit AS original_base_unit FROM units";
2045 $query .= " WHERE (type = ?)";
2049 my $sth = $dbh->prepare($query);
2050 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
2053 while (my $ref = $sth->fetchrow_hashref()) {
2054 $units->{$ref->{"name"}} = $ref;
2058 my $query_lang = "SELECT id, template_code FROM language ORDER BY description";
2059 $sth = $dbh->prepare($query_lang);
2060 $sth->execute() || $form->dberror($query_lang);
2062 while ($ref = $sth->fetchrow_hashref()) {
2063 push(@languages, $ref);
2067 $query_lang = "SELECT ul.localized, ul.localized_plural, l.id, l.template_code " .
2068 "FROM units_language ul " .
2069 "LEFT JOIN language l ON ul.language_id = l.id " .
2070 "WHERE ul.unit = ?";
2071 $sth = $dbh->prepare($query_lang);
2073 foreach my $unit (values(%{$units})) {
2074 ($unit->{"${prefix}base_unit"}, $unit->{"${prefix}factor"}) = AM->get_base_unit($units, $unit->{"name"});
2076 $unit->{"LANGUAGES"} = {};
2077 foreach my $lang (@languages) {
2078 $unit->{"LANGUAGES"}->{$lang->{"template_code"}} = { "template_code" => $lang->{"template_code"} };
2081 $sth->execute($unit->{"name"}) || $form->dberror($query_lang . " (" . $unit->{"name"} . ")");
2082 while ($ref = $sth->fetchrow_hashref()) {
2083 map({ $unit->{"LANGUAGES"}->{$ref->{"template_code"}}->{$_} = $ref->{$_} } keys(%{$ref}));
2090 $main::lxdebug->leave_sub();
2095 sub translate_units {
2096 $main::lxdebug->enter_sub();
2098 my ($self, $form, $template_code, $unit, $amount) = @_;
2100 my $units = $self->retrieve_units(\%main::myconfig, $form);
2102 my $h = $units->{$unit}->{"LANGUAGES"}->{$template_code};
2103 my $new_unit = $unit;
2105 if (($amount != 1) && $h->{"localized_plural"}) {
2106 $new_unit = $h->{"localized_plural"};
2107 } elsif ($h->{"localized"}) {
2108 $new_unit = $h->{"localized"};
2112 $main::lxdebug->leave_sub();
2118 $main::lxdebug->enter_sub();
2120 my ($self, $myconfig, $form, $units) = @_;
2122 my $dbh = $form->dbconnect($myconfig);
2124 foreach my $unit (values(%{$units})) {
2125 my $base_unit = $unit->{"original_base_unit"};
2126 while ($base_unit) {
2127 $units->{$base_unit}->{"DEPENDING_UNITS"} = [] unless ($units->{$base_unit}->{"DEPENDING_UNITS"});
2128 push(@{$units->{$base_unit}->{"DEPENDING_UNITS"}}, $unit->{"name"});
2129 $base_unit = $units->{$base_unit}->{"original_base_unit"};
2133 foreach my $unit (values(%{$units})) {
2134 $unit->{"in_use"} = 0;
2135 map({ $_ = $dbh->quote($_); } @{$unit->{"DEPENDING_UNITS"}});
2137 foreach my $table (qw(parts invoice orderitems)) {
2138 my $query = "SELECT COUNT(*) FROM $table WHERE unit ";
2140 if (0 == scalar(@{$unit->{"DEPENDING_UNITS"}})) {
2141 $query .= "= " . $dbh->quote($unit->{"name"});
2143 $query .= "IN (" . $dbh->quote($unit->{"name"}) . "," . join(",", @{$unit->{"DEPENDING_UNITS"}}) . ")";
2146 my ($count) = $dbh->selectrow_array($query);
2147 $form->dberror($query) if ($dbh->err);
2150 $unit->{"in_use"} = 1;
2158 $main::lxdebug->leave_sub();
2161 sub unit_select_data {
2162 $main::lxdebug->enter_sub();
2164 my ($self, $units, $selected, $empty_entry) = @_;
2169 push(@{$select}, { "name" => "", "base_unit" => "", "factor" => "", "selected" => "" });
2172 foreach my $unit (sort({ lc($a) cmp lc($b) } keys(%{$units}))) {
2173 push(@{$select}, { "name" => $unit,
2174 "base_unit" => $units->{$unit}->{"base_unit"},
2175 "factor" => $units->{$unit}->{"factor"},
2176 "selected" => ($unit eq $selected) ? "selected" : "" });
2179 $main::lxdebug->leave_sub();
2184 sub unit_select_html {
2185 $main::lxdebug->enter_sub();
2187 my ($self, $units, $name, $selected, $convertible_into) = @_;
2189 my $select = "<select name=${name}>";
2191 foreach my $unit (sort({ lc($a) cmp lc($b) } keys(%{$units}))) {
2192 if (!$convertible_into ||
2193 ($units->{$convertible_into} &&
2194 ($units->{$convertible_into}->{"base_unit"} eq $units->{$unit}->{"base_unit"}))) {
2195 $select .= "<option" . (($unit eq $selected) ? " selected" : "") . ">${unit}</option>";
2198 $select .= "</select>";
2200 $main::lxdebug->leave_sub();
2206 $main::lxdebug->enter_sub();
2208 my ($self, $myconfig, $form, $name, $base_unit, $factor, $type, $languages) = @_;
2210 my $dbh = $form->dbconnect_noauto($myconfig);
2212 my $query = "INSERT INTO units (name, base_unit, factor, type) VALUES (?, ?, ?, ?)";
2213 $dbh->do($query, undef, $name, $base_unit, $factor, $type) || $form->dberror($query . " ($name, $base_unit, $factor, $type)");
2216 $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
2217 my $sth = $dbh->prepare($query);
2218 foreach my $lang (@{$languages}) {
2219 my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
2220 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
2228 $main::lxdebug->leave_sub();
2232 $main::lxdebug->enter_sub();
2234 my ($self, $myconfig, $form, $type, $units, $delete_units) = @_;
2236 my $dbh = $form->dbconnect_noauto($myconfig);
2238 my ($base_unit, $unit, $sth, $query);
2240 $query = "DELETE FROM units_language";
2241 $dbh->do($query) || $form->dberror($query);
2243 if ($delete_units && (0 != scalar(@{$delete_units}))) {
2244 $query = "DELETE FROM units WHERE name IN (";
2245 map({ $query .= "?," } @{$delete_units});
2246 substr($query, -1, 1) = ")";
2247 $dbh->do($query, undef, @{$delete_units}) ||
2248 $form->dberror($query . " (" . join(", ", @{$delete_units}) . ")");
2251 $query = "UPDATE units SET name = ?, base_unit = ?, factor = ? WHERE name = ?";
2252 $sth = $dbh->prepare($query);
2254 my $query_lang = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
2255 my $sth_lang = $dbh->prepare($query_lang);
2257 foreach $unit (values(%{$units})) {
2258 $unit->{"depth"} = 0;
2259 my $base_unit = $unit;
2260 while ($base_unit->{"base_unit"}) {
2262 $base_unit = $units->{$base_unit->{"base_unit"}};
2266 foreach $unit (sort({ $a->{"depth"} <=> $b->{"depth"} } values(%{$units}))) {
2267 if ($unit->{"LANGUAGES"}) {
2268 foreach my $lang (@{$unit->{"LANGUAGES"}}) {
2269 next unless ($lang->{"id"} && $lang->{"localized"});
2270 my @values = ($unit->{"name"}, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
2271 $sth_lang->execute(@values) || $form->dberror($query_lang . " (" . join(", ", @values) . ")");
2275 next if ($unit->{"unchanged_unit"});
2277 my @values = ($unit->{"name"}, $unit->{"base_unit"}, $unit->{"factor"}, $unit->{"old_name"});
2278 $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
2282 $sth_lang->finish();
2286 $main::lxdebug->leave_sub();