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 #======================================================================
41 $main::lxdebug->enter_sub();
43 my ($self, $myconfig, $form) = @_;
45 $form->{id} = "NULL" unless ($form->{id});
48 my $dbh = $form->dbconnect($myconfig);
50 my $query = qq|SELECT c.accno, c.description, c.charttype, c.gifi_accno,
51 c.category, c.link, c.taxkey_id, c.pos_ustva, c.pos_bwa, c.pos_bilanz,c.pos_eur
53 WHERE c.id = $form->{id}|;
55 my $sth = $dbh->prepare($query);
56 $sth->execute || $form->dberror($query);
58 my $ref = $sth->fetchrow_hashref(NAME_lc);
60 foreach my $key (keys %$ref) {
61 $form->{"$key"} = $ref->{"$key"};
66 # get default accounts
67 $query = qq|SELECT inventory_accno_id, income_accno_id, expense_accno_id
69 $sth = $dbh->prepare($query);
70 $sth->execute || $form->dberror($query);
72 $ref = $sth->fetchrow_hashref(NAME_lc);
74 map { $form->{$_} = $ref->{$_} } keys %ref;
78 # get taxkeys and description
79 $query = qq|SELECT taxkey, taxdescription
81 $sth = $dbh->prepare($query);
82 $sth->execute || $form->dberror($query);
84 $ref = $sth->fetchrow_hashref(NAME_lc);
86 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
87 push @{ $form->{TAXKEY} }, $ref;
92 # check if we have any transactions
93 $query = qq|SELECT a.trans_id FROM acc_trans a
94 WHERE a.chart_id = $form->{id}|;
95 $sth = $dbh->prepare($query);
96 $sth->execute || $form->dberror($query);
98 ($form->{orphaned}) = $sth->fetchrow_array;
99 $form->{orphaned} = !$form->{orphaned};
104 $main::lxdebug->leave_sub();
108 $main::lxdebug->enter_sub();
110 my ($self, $myconfig, $form) = @_;
112 # connect to database, turn off AutoCommit
113 my $dbh = $form->dbconnect_noauto($myconfig);
115 # sanity check, can't have AR with AR_...
116 if ($form->{AR} || $form->{AP} || $form->{IC}) {
117 map { delete $form->{$_} }
118 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);
122 foreach my $item ($form->{AR}, $form->{AR_amount},
123 $form->{AR_tax}, $form->{AR_paid},
124 $form->{AP}, $form->{AP_amount},
125 $form->{AP_tax}, $form->{AP_paid},
126 $form->{IC}, $form->{IC_sale},
127 $form->{IC_cogs}, $form->{IC_taxpart},
128 $form->{IC_income}, $form->{IC_expense},
129 $form->{IC_taxservice}, $form->{CT_tax}
131 $form->{link} .= "${item}:" if ($item);
135 # if we have an id then replace the old record
136 $form->{description} =~ s/\'/\'\'/g;
138 # strip blanks from accno
139 map { $form->{$_} =~ s/ //g; } qw(accno);
143 if ($form->{id} eq "NULL") {
147 map({ $form->{$_} = "NULL" unless ($form->{$_}); }
148 qw(pos_ustva pos_bwa pos_bilanz pos_eur));
151 $query = qq|UPDATE chart SET
152 accno = '$form->{accno}',
153 description = '$form->{description}',
154 charttype = '$form->{charttype}',
155 gifi_accno = '$form->{gifi_accno}',
156 category = '$form->{category}',
157 link = '$form->{link}',
158 taxkey_id = $form->{taxkey_id},
159 pos_ustva = $form->{pos_ustva},
160 pos_bwa = $form->{pos_bwa},
161 pos_bilanz = $form->{pos_bilanz},
162 pos_eur = $form->{pos_eur}
163 WHERE id = $form->{id}|;
166 $query = qq|INSERT INTO chart
167 (accno, description, charttype, gifi_accno, category, link, taxkey_id, pos_ustva, pos_bwa, pos_bilanz,pos_eur)
168 VALUES ('$form->{accno}', '$form->{description}',
169 '$form->{charttype}', '$form->{gifi_accno}',
170 '$form->{category}', '$form->{link}', $form->{taxkey_id}, $form->{pos_ustva}, $form->{pos_bwa}, $form->{pos_bilanz}, $form->{pos_eur})|;
172 $dbh->do($query) || $form->dberror($query);
174 if ($form->{IC_taxpart} || $form->{IC_taxservice} || $form->{CT_tax}) {
176 my $chart_id = $form->{id};
178 unless ($form->{id}) {
181 $query = qq|SELECT c.id
183 WHERE c.accno = '$form->{accno}'|;
184 $sth = $dbh->prepare($query);
185 $sth->execute || $form->dberror($query);
187 ($chart_id) = $sth->fetchrow_array;
191 # add account if it doesn't exist in tax
192 $query = qq|SELECT t.chart_id
194 WHERE t.chart_id = $chart_id|;
195 $sth = $dbh->prepare($query);
196 $sth->execute || $form->dberror($query);
198 my ($tax_id) = $sth->fetchrow_array;
201 # add tax if it doesn't exist
203 $query = qq|INSERT INTO tax (chart_id, rate)
204 VALUES ($chart_id, 0)|;
205 $dbh->do($query) || $form->dberror($query);
211 $query = qq|DELETE FROM tax
212 WHERE chart_id = $form->{id}|;
213 $dbh->do($query) || $form->dberror($query);
218 my $rc = $dbh->commit;
221 $main::lxdebug->leave_sub();
227 $main::lxdebug->enter_sub();
229 my ($self, $myconfig, $form) = @_;
231 # connect to database, turn off AutoCommit
232 my $dbh = $form->dbconnect_noauto($myconfig);
234 my $query = qq|SELECT count(*) FROM acc_trans a
235 WHERE a.chart_id = $form->{id}|;
236 my $sth = $dbh->prepare($query);
237 $sth->execute || $form->dberror($query);
239 if ($sth->fetchrow_array) {
242 $main::lxdebug->leave_sub();
247 # delete chart of account record
248 $query = qq|DELETE FROM chart
249 WHERE id = $form->{id}|;
250 $dbh->do($query) || $form->dberror($query);
252 # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
253 $query = qq|UPDATE parts
254 SET inventory_accno_id =
255 (SELECT inventory_accno_id FROM defaults)
256 WHERE inventory_accno_id = $form->{id}|;
257 $dbh->do($query) || $form->dberror($query);
259 $query = qq|UPDATE parts
260 SET income_accno_id =
261 (SELECT income_accno_id FROM defaults)
262 WHERE income_accno_id = $form->{id}|;
263 $dbh->do($query) || $form->dberror($query);
265 $query = qq|UPDATE parts
266 SET expense_accno_id =
267 (SELECT expense_accno_id FROM defaults)
268 WHERE expense_accno_id = $form->{id}|;
269 $dbh->do($query) || $form->dberror($query);
271 foreach my $table (qw(partstax customertax vendortax tax)) {
272 $query = qq|DELETE FROM $table
273 WHERE chart_id = $form->{id}|;
274 $dbh->do($query) || $form->dberror($query);
277 # commit and redirect
278 my $rc = $dbh->commit;
281 $main::lxdebug->leave_sub();
287 $main::lxdebug->enter_sub();
289 my ($self, $myconfig, $form) = @_;
291 # connect to database
292 my $dbh = $form->dbconnect($myconfig);
294 my $query = qq|SELECT accno, description
298 $sth = $dbh->prepare($query);
299 $sth->execute || $form->dberror($query);
301 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
302 push @{ $form->{ALL} }, $ref;
308 $main::lxdebug->leave_sub();
312 $main::lxdebug->enter_sub();
314 my ($self, $myconfig, $form) = @_;
316 # connect to database
317 my $dbh = $form->dbconnect($myconfig);
319 my $query = qq|SELECT g.accno, g.description
321 WHERE g.accno = '$form->{accno}'|;
322 my $sth = $dbh->prepare($query);
323 $sth->execute || $form->dberror($query);
325 my $ref = $sth->fetchrow_hashref(NAME_lc);
327 map { $form->{$_} = $ref->{$_} } keys %$ref;
331 # check for transactions
332 $query = qq|SELECT count(*) FROM acc_trans a, chart c, gifi g
333 WHERE c.gifi_accno = g.accno
334 AND a.chart_id = c.id
335 AND g.accno = '$form->{accno}'|;
336 $sth = $dbh->prepare($query);
337 $sth->execute || $form->dberror($query);
339 ($form->{orphaned}) = $sth->fetchrow_array;
341 $form->{orphaned} = !$form->{orphaned};
345 $main::lxdebug->leave_sub();
349 $main::lxdebug->enter_sub();
351 my ($self, $myconfig, $form) = @_;
353 # connect to database
354 my $dbh = $form->dbconnect($myconfig);
356 $form->{description} =~ s/\'/\'\'/g;
358 # id is the old account number!
360 $query = qq|UPDATE gifi SET
361 accno = '$form->{accno}',
362 description = '$form->{description}'
363 WHERE accno = '$form->{id}'|;
365 $query = qq|INSERT INTO gifi
367 VALUES ('$form->{accno}', '$form->{description}')|;
369 $dbh->do($query) || $form->dberror($query);
373 $main::lxdebug->leave_sub();
377 $main::lxdebug->enter_sub();
379 my ($self, $myconfig, $form) = @_;
381 # connect to database
382 my $dbh = $form->dbconnect($myconfig);
384 # id is the old account number!
385 $query = qq|DELETE FROM gifi
386 WHERE accno = '$form->{id}'|;
387 $dbh->do($query) || $form->dberror($query);
391 $main::lxdebug->leave_sub();
395 $main::lxdebug->enter_sub();
397 my ($self, $myconfig, $form) = @_;
399 # connect to database
400 my $dbh = $form->dbconnect($myconfig);
402 my $query = qq|SELECT id, description
406 $sth = $dbh->prepare($query);
407 $sth->execute || $form->dberror($query);
409 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
410 push @{ $form->{ALL} }, $ref;
416 $main::lxdebug->leave_sub();
420 $main::lxdebug->enter_sub();
422 my ($self, $myconfig, $form) = @_;
424 # connect to database
425 my $dbh = $form->dbconnect($myconfig);
427 my $query = qq|SELECT w.description
429 WHERE w.id = $form->{id}|;
430 my $sth = $dbh->prepare($query);
431 $sth->execute || $form->dberror($query);
433 my $ref = $sth->fetchrow_hashref(NAME_lc);
435 map { $form->{$_} = $ref->{$_} } keys %$ref;
439 # see if it is in use
440 $query = qq|SELECT count(*) FROM inventory i
441 WHERE i.warehouse_id = $form->{id}|;
442 $sth = $dbh->prepare($query);
443 $sth->execute || $form->dberror($query);
445 ($form->{orphaned}) = $sth->fetchrow_array;
446 $form->{orphaned} = !$form->{orphaned};
451 $main::lxdebug->leave_sub();
455 $main::lxdebug->enter_sub();
457 my ($self, $myconfig, $form) = @_;
459 # connect to database
460 my $dbh = $form->dbconnect($myconfig);
462 $form->{description} =~ s/\'/\'\'/g;
465 $query = qq|UPDATE warehouse SET
466 description = '$form->{description}'
467 WHERE id = $form->{id}|;
469 $query = qq|INSERT INTO warehouse
471 VALUES ('$form->{description}')|;
473 $dbh->do($query) || $form->dberror($query);
477 $main::lxdebug->leave_sub();
480 sub delete_warehouse {
481 $main::lxdebug->enter_sub();
483 my ($self, $myconfig, $form) = @_;
485 # connect to database
486 my $dbh = $form->dbconnect($myconfig);
488 $query = qq|DELETE FROM warehouse
489 WHERE id = $form->{id}|;
490 $dbh->do($query) || $form->dberror($query);
494 $main::lxdebug->leave_sub();
498 $main::lxdebug->enter_sub();
500 my ($self, $myconfig, $form) = @_;
502 # connect to database
503 my $dbh = $form->dbconnect($myconfig);
505 my $query = qq|SELECT d.id, d.description, d.role
509 $sth = $dbh->prepare($query);
510 $sth->execute || $form->dberror($query);
512 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
513 push @{ $form->{ALL} }, $ref;
519 $main::lxdebug->leave_sub();
523 $main::lxdebug->enter_sub();
525 my ($self, $myconfig, $form) = @_;
527 # connect to database
528 my $dbh = $form->dbconnect($myconfig);
530 my $query = qq|SELECT d.description, d.role
532 WHERE d.id = $form->{id}|;
533 my $sth = $dbh->prepare($query);
534 $sth->execute || $form->dberror($query);
536 my $ref = $sth->fetchrow_hashref(NAME_lc);
538 map { $form->{$_} = $ref->{$_} } keys %$ref;
542 # see if it is in use
543 $query = qq|SELECT count(*) FROM dpt_trans d
544 WHERE d.department_id = $form->{id}|;
545 $sth = $dbh->prepare($query);
546 $sth->execute || $form->dberror($query);
548 ($form->{orphaned}) = $sth->fetchrow_array;
549 $form->{orphaned} = !$form->{orphaned};
554 $main::lxdebug->leave_sub();
557 sub save_department {
558 $main::lxdebug->enter_sub();
560 my ($self, $myconfig, $form) = @_;
562 # connect to database
563 my $dbh = $form->dbconnect($myconfig);
565 $form->{description} =~ s/\'/\'\'/g;
568 $query = qq|UPDATE department SET
569 description = '$form->{description}',
570 role = '$form->{role}'
571 WHERE id = $form->{id}|;
573 $query = qq|INSERT INTO department
575 VALUES ('$form->{description}', '$form->{role}')|;
577 $dbh->do($query) || $form->dberror($query);
581 $main::lxdebug->leave_sub();
584 sub delete_department {
585 $main::lxdebug->enter_sub();
587 my ($self, $myconfig, $form) = @_;
589 # connect to database
590 my $dbh = $form->dbconnect($myconfig);
592 $query = qq|DELETE FROM department
593 WHERE id = $form->{id}|;
594 $dbh->do($query) || $form->dberror($query);
598 $main::lxdebug->leave_sub();
602 $main::lxdebug->enter_sub();
604 my ($self, $myconfig, $form) = @_;
606 # connect to database
607 my $dbh = $form->dbconnect($myconfig);
609 my $query = qq|SELECT id, description, discount, customernumberinit, salesman
613 $sth = $dbh->prepare($query);
614 $sth->execute || $form->dberror($query);
616 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
617 push @{ $form->{ALL} }, $ref;
623 $main::lxdebug->leave_sub();
627 $main::lxdebug->enter_sub();
629 my ($self, $myconfig, $form) = @_;
631 # connect to database
632 my $dbh = $form->dbconnect($myconfig);
635 qq|SELECT b.description, b.discount, b.customernumberinit, b.salesman
637 WHERE b.id = $form->{id}|;
638 my $sth = $dbh->prepare($query);
639 $sth->execute || $form->dberror($query);
641 my $ref = $sth->fetchrow_hashref(NAME_lc);
643 map { $form->{$_} = $ref->{$_} } keys %$ref;
649 $main::lxdebug->leave_sub();
653 $main::lxdebug->enter_sub();
655 my ($self, $myconfig, $form) = @_;
657 # connect to database
658 my $dbh = $form->dbconnect($myconfig);
660 $form->{description} =~ s/\'/\'\'/g;
661 $form->{discount} /= 100;
662 $form->{salesman} *= 1;
664 # id is the old record
666 $query = qq|UPDATE business SET
667 description = '$form->{description}',
668 discount = $form->{discount},
669 customernumberinit = '$form->{customernumberinit}',
670 salesman = '$form->{salesman}'
671 WHERE id = $form->{id}|;
673 $query = qq|INSERT INTO business
674 (description, discount, customernumberinit, salesman)
675 VALUES ('$form->{description}', $form->{discount}, '$form->{customernumberinit}', '$form->{salesman}')|;
677 $dbh->do($query) || $form->dberror($query);
681 $main::lxdebug->leave_sub();
684 sub delete_business {
685 $main::lxdebug->enter_sub();
687 my ($self, $myconfig, $form) = @_;
689 # connect to database
690 my $dbh = $form->dbconnect($myconfig);
692 $query = qq|DELETE FROM business
693 WHERE id = $form->{id}|;
694 $dbh->do($query) || $form->dberror($query);
698 $main::lxdebug->leave_sub();
702 $main::lxdebug->enter_sub();
704 my ($self, $myconfig, $form) = @_;
706 # connect to database
707 my $dbh = $form->dbconnect($myconfig);
709 my $query = qq|SELECT code, sictype, description
713 $sth = $dbh->prepare($query);
714 $sth->execute || $form->dberror($query);
716 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
717 push @{ $form->{ALL} }, $ref;
723 $main::lxdebug->leave_sub();
727 $main::lxdebug->enter_sub();
729 my ($self, $myconfig, $form) = @_;
731 # connect to database
732 my $dbh = $form->dbconnect($myconfig);
734 my $query = qq|SELECT s.code, s.sictype, s.description
736 WHERE s.code = '$form->{code}'|;
737 my $sth = $dbh->prepare($query);
738 $sth->execute || $form->dberror($query);
740 my $ref = $sth->fetchrow_hashref(NAME_lc);
742 map { $form->{$_} = $ref->{$_} } keys %$ref;
748 $main::lxdebug->leave_sub();
752 $main::lxdebug->enter_sub();
754 my ($self, $myconfig, $form) = @_;
756 # connect to database
757 my $dbh = $form->dbconnect($myconfig);
759 $form->{code} =~ s/\'/\'\'/g;
760 $form->{description} =~ s/\'/\'\'/g;
764 $query = qq|UPDATE sic SET
765 code = '$form->{code}',
766 sictype = '$form->{sictype}',
767 description = '$form->{description}'
768 WHERE code = '$form->{id}'|;
770 $query = qq|INSERT INTO sic
771 (code, sictype, description)
772 VALUES ('$form->{code}', '$form->{sictype}', '$form->{description}')|;
774 $dbh->do($query) || $form->dberror($query);
778 $main::lxdebug->leave_sub();
782 $main::lxdebug->enter_sub();
784 my ($self, $myconfig, $form) = @_;
786 # connect to database
787 my $dbh = $form->dbconnect($myconfig);
789 $query = qq|DELETE FROM sic
790 WHERE code = '$form->{code}'|;
791 $dbh->do($query) || $form->dberror($query);
795 $main::lxdebug->leave_sub();
799 $main::lxdebug->enter_sub();
801 my ($self, $form) = @_;
803 open(TEMPLATE, "$form->{file}") or $form->error("$form->{file} : $!");
811 $main::lxdebug->leave_sub();
815 $main::lxdebug->enter_sub();
817 my ($self, $form) = @_;
819 open(TEMPLATE, ">$form->{file}") or $form->error("$form->{file} : $!");
822 $form->{body} =~ s/\r\n/\n/g;
823 print TEMPLATE $form->{body};
827 $main::lxdebug->leave_sub();
830 sub save_preferences {
831 $main::lxdebug->enter_sub();
833 my ($self, $myconfig, $form, $memberfile, $userspath, $webdav) = @_;
835 map { ($form->{$_}) = split /--/, $form->{$_} }
836 qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno);
839 $form->{curr} =~ s/ //g;
840 map { push(@a, uc pack "A3", $_) if $_ } split /:/, $form->{curr};
841 $form->{curr} = join ':', @a;
843 # connect to database
844 my $dbh = $form->dbconnect_noauto($myconfig);
846 # these defaults are database wide
847 # user specific variables are in myconfig
849 my $query = qq|UPDATE defaults SET
851 (SELECT c.id FROM chart c
852 WHERE c.accno = '$form->{inventory_accno}'),
854 (SELECT c.id FROM chart c
855 WHERE c.accno = '$form->{income_accno}'),
857 (SELECT c.id FROM chart c
858 WHERE c.accno = '$form->{expense_accno}'),
860 (SELECT c.id FROM chart c
861 WHERE c.accno = '$form->{fxgain_accno}'),
863 (SELECT c.id FROM chart c
864 WHERE c.accno = '$form->{fxloss_accno}'),
865 invnumber = '$form->{invnumber}',
866 sonumber = '$form->{sonumber}',
867 ponumber = '$form->{ponumber}',
868 sqnumber = '$form->{sqnumber}',
869 rfqnumber = '$form->{rfqnumber}',
870 customernumber = '$form->{customernumber}',
871 vendornumber = '$form->{vendornumber}',
872 articlenumber = '$form->{articlenumber}',
873 servicenumber = '$form->{servicenumber}',
874 yearend = '$form->{yearend}',
875 curr = '$form->{curr}',
876 weightunit = '$form->{weightunit}',
877 businessnumber = '$form->{businessnumber}'
879 $dbh->do($query) || $form->dberror($query);
882 my $name = $form->{name};
883 $name =~ s/\'/\'\'/g;
884 $query = qq|UPDATE employee
886 WHERE login = '$form->{login}'|;
887 $dbh->do($query) || $form->dberror($query);
889 foreach my $item (split / /, $form->{taxaccounts}) {
890 $query = qq|UPDATE tax
891 SET rate = | . ($form->{$item} / 100) . qq|,
892 taxnumber = '$form->{"taxnumber_$item"}'
893 WHERE chart_id = $item|;
894 $dbh->do($query) || $form->dberror($query);
897 my $rc = $dbh->commit;
900 # save first currency in myconfig
901 $form->{currency} = substr($form->{curr}, 0, 3);
903 my $myconfig = new User "$memberfile", "$form->{login}";
905 foreach my $item (keys %$form) {
906 $myconfig->{$item} = $form->{$item};
909 $myconfig->save_member($memberfile, $userspath);
913 qw(angebote bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen);
914 foreach $directory (@webdavdirs) {
915 $file = "webdav/" . $directory . "/webdav-user";
916 if ($myconfig->{$directory}) {
917 open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
919 ($login, $password) = split(/:/, $_);
920 if ($login ne $form->{login}) {
925 open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
926 $newfile .= $myconfig->{login} . ":" . $myconfig->{password} . "\n";
927 print(HTACCESS $newfile);
930 $form->{$directory} = 0;
931 open(HTACCESS, "$file") or die "cannot open webdav-user $!\n";
933 ($login, $password) = split(/:/, $_);
934 if ($login ne $form->{login}) {
939 open(HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
940 print(HTACCESS $newfile);
946 $main::lxdebug->leave_sub();
951 sub defaultaccounts {
952 $main::lxdebug->enter_sub();
954 my ($self, $myconfig, $form) = @_;
956 # connect to database
957 my $dbh = $form->dbconnect($myconfig);
959 # get defaults from defaults table
960 my $query = qq|SELECT * FROM defaults|;
961 my $sth = $dbh->prepare($query);
962 $sth->execute || $form->dberror($query);
964 $form->{defaults} = $sth->fetchrow_hashref(NAME_lc);
965 $form->{defaults}{IC} = $form->{defaults}{inventory_accno_id};
966 $form->{defaults}{IC_income} = $form->{defaults}{income_accno_id};
967 $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
968 $form->{defaults}{FX_gain} = $form->{defaults}{fxgain_accno_id};
969 $form->{defaults}{FX_loss} = $form->{defaults}{fxloss_accno_id};
973 $query = qq|SELECT c.id, c.accno, c.description, c.link
975 WHERE c.link LIKE '%IC%'
977 $sth = $dbh->prepare($query);
978 $sth->execute || $self->dberror($query);
980 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
981 foreach my $key (split(/:/, $ref->{link})) {
984 if ($key =~ /cogs/) {
985 $nkey = "IC_expense";
987 if ($key =~ /sale/) {
990 %{ $form->{IC}{$nkey}{ $ref->{accno} } } = (
992 description => $ref->{description}
999 $query = qq|SELECT c.id, c.accno, c.description
1001 WHERE c.category = 'I'
1002 AND c.charttype = 'A'
1004 $sth = $dbh->prepare($query);
1005 $sth->execute || $self->dberror($query);
1007 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1008 %{ $form->{IC}{FX_gain}{ $ref->{accno} } } = (
1010 description => $ref->{description}
1015 $query = qq|SELECT c.id, c.accno, c.description
1017 WHERE c.category = 'E'
1018 AND c.charttype = 'A'
1020 $sth = $dbh->prepare($query);
1021 $sth->execute || $self->dberror($query);
1023 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1024 %{ $form->{IC}{FX_loss}{ $ref->{accno} } } = (
1026 description => $ref->{description}
1031 # now get the tax rates and numbers
1032 $query = qq|SELECT c.id, c.accno, c.description,
1033 t.rate * 100 AS rate, t.taxnumber
1035 WHERE c.id = t.chart_id|;
1037 $sth = $dbh->prepare($query);
1038 $sth->execute || $form->dberror($query);
1040 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1041 $form->{taxrates}{ $ref->{accno} }{id} = $ref->{id};
1042 $form->{taxrates}{ $ref->{accno} }{description} = $ref->{description};
1043 $form->{taxrates}{ $ref->{accno} }{taxnumber} = $ref->{taxnumber}
1044 if $ref->{taxnumber};
1045 $form->{taxrates}{ $ref->{accno} }{rate} = $ref->{rate} if $ref->{rate};
1051 $main::lxdebug->leave_sub();
1055 $main::lxdebug->enter_sub();
1057 my ($self, $myconfig, $form, $userspath) = @_;
1061 my $boundary = time;
1063 "$userspath/$boundary.$myconfig->{dbname}-$form->{dbversion}.sql";
1064 my $out = $form->{OUT};
1065 $form->{OUT} = ">$tmpfile";
1067 if ($form->{media} eq 'email') {
1072 $mail->{to} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
1073 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
1075 "Lx-Office Backup / $myconfig->{dbname}-$form->{dbversion}.sql";
1076 @{ $mail->{attachments} } = ($tmpfile);
1077 $mail->{version} = $form->{version};
1078 $mail->{fileid} = "$boundary.";
1080 $myconfig->{signature} =~ s/\\n/\r\n/g;
1081 $mail->{message} = "--\n$myconfig->{signature}";
1085 open(OUT, "$form->{OUT}") or $form->error("$form->{OUT} : $!");
1087 # get sequences, functions and triggers
1088 open(FH, "sql/lx-office.sql") or $form->error("sql/lx-office.sql : $!");
1101 # Remove DOS and Unix style line endings.
1104 # ignore comments or empty lines
1105 next if /^(--.*|\s+)$/;
1107 for (my $i = 0; $i < length($_); $i++) {
1108 my $char = substr($_, $i, 1);
1110 # Are we inside a string?
1112 if ($char eq $quote_chars[-1]) {
1118 if (($char eq "'") || ($char eq "\"")) {
1119 push(@quote_chars, $char);
1121 } elsif ($char eq ";") {
1123 # Query is complete. Check for triggers and functions.
1124 if ($query =~ /^create\s+function\s+\"?(\w+)\"?/i) {
1125 push(@functions, $query);
1127 } elsif ($query =~ /^create\s+trigger\s+\"?(\w+)\"?/i) {
1128 push(@triggers, $query);
1130 } elsif ($query =~ /^create\s+sequence\s+\"?(\w+)\"?/i) {
1131 push(@sequences, $1);
1133 } elsif ($query =~ /^create\s+table\s+\"?(\w+)\"?/i) {
1134 $tablespecs{$1} = $query;
1136 } elsif ($query =~ /^create\s+index\s+\"?(\w+)\"?/i) {
1137 push(@indices, $query);
1151 # connect to database
1152 my $dbh = $form->dbconnect($myconfig);
1154 # get all the tables
1155 my @tables = $dbh->tables('', '', 'customer', '', { noprefix => 0 });
1157 my $today = scalar localtime;
1159 $myconfig->{dbhost} = 'localhost' unless $myconfig->{dbhost};
1161 print OUT qq|-- Lx-Office Backup
1162 -- Dataset: $myconfig->{dbname}
1163 -- Version: $form->{dbversion}
1164 -- Host: $myconfig->{dbhost}
1165 -- Login: $form->{login}
1166 -- User: $myconfig->{name}
1170 $myconfig->{dboptions};
1174 print OUT "-- DROP Sequences\n";
1176 foreach $item (@sequences) {
1177 print OUT qq|DROP SEQUENCE $item;\n|;
1180 print OUT "-- DROP Triggers\n";
1182 foreach $item (@triggers) {
1183 if ($item =~ /^create\s+trigger\s+\"?(\w+)\"?\s+.*on\s+\"?(\w+)\"?\s+/i) {
1184 print OUT qq|DROP TRIGGER "$1" ON "$2";\n|;
1188 print OUT "-- DROP Functions\n";
1190 foreach $item (@functions) {
1191 if ($item =~ /^create\s+function\s+\"?(\w+)\"?/i) {
1192 print OUT qq|DROP FUNCTION "$1" ();\n|;
1196 foreach $table (@tables) {
1197 if (!($table =~ /^sql_.*/)) {
1198 my $query = qq|SELECT * FROM $table|;
1200 my $sth = $dbh->prepare($query);
1201 $sth->execute || $form->dberror($query);
1203 $query = "INSERT INTO $table (";
1204 map { $query .= qq|$sth->{NAME}->[$_],| }
1205 (0 .. $sth->{NUM_OF_FIELDS} - 1);
1208 $query .= ") VALUES";
1210 if ($tablespecs{$table}) {
1212 print(OUT "DROP TABLE $table;\n");
1213 print(OUT $tablespecs{$table}, ";\n");
1216 print(OUT "DELETE FROM $table;\n");
1218 while (my @arr = $sth->fetchrow_array) {
1221 foreach my $item (@arr) {
1222 if (defined $item) {
1223 $item =~ s/\'/\'\'/g;
1224 $fields .= qq|'$item',|;
1233 print OUT qq|$query $fields;\n|;
1240 # create indices, sequences, functions and triggers
1242 print(OUT "-- CREATE Indices\n");
1243 map({ print(OUT "$_;\n"); } @indices);
1245 print OUT "-- CREATE Sequences\n";
1246 foreach $item (@sequences) {
1247 $query = qq|SELECT last_value FROM $item|;
1248 $sth = $dbh->prepare($query);
1249 $sth->execute || $form->dberror($query);
1250 my ($id) = $sth->fetchrow_array;
1254 CREATE SEQUENCE $item START $id;
1258 print OUT "-- CREATE Functions\n";
1261 map { print(OUT $_, ";\n"); } @functions;
1263 print OUT "-- CREATE Triggers\n";
1266 map { print(OUT $_, ";\n"); } @triggers;
1273 my @args = ("gzip", "$tmpfile");
1274 system(@args) == 0 or $form->error("$args[0] : $?");
1278 if ($form->{media} eq 'email') {
1279 @{ $mail->{attachments} } = ($tmpfile);
1280 $err = $mail->send($out);
1283 if ($form->{media} eq 'file') {
1285 open(IN, "$tmpfile") or $form->error("$tmpfile : $!");
1286 open(OUT, ">-") or $form->error("STDOUT : $!");
1288 print OUT qq|Content-Type: application/x-tar-gzip;
1289 Content-Disposition: attachment; filename="$myconfig->{dbname}-$form->{dbversion}.sql.gz"
1304 $main::lxdebug->leave_sub();
1308 $main::lxdebug->enter_sub();
1310 my ($self, $myconfig, $form) = @_;
1312 my $dbh = $form->dbconnect($myconfig);
1314 my $query = qq|SELECT closedto, revtrans FROM defaults|;
1315 my $sth = $dbh->prepare($query);
1316 $sth->execute || $form->dberror($query);
1318 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
1324 $main::lxdebug->leave_sub();
1328 $main::lxdebug->enter_sub();
1330 my ($self, $myconfig, $form) = @_;
1332 my $dbh = $form->dbconnect($myconfig);
1334 if ($form->{revtrans}) {
1336 $query = qq|UPDATE defaults SET closedto = NULL,
1339 if ($form->{closedto}) {
1341 $query = qq|UPDATE defaults SET closedto = '$form->{closedto}',
1345 $query = qq|UPDATE defaults SET closedto = NULL,
1350 # set close in defaults
1351 $dbh->do($query) || $form->dberror($query);
1355 $main::lxdebug->leave_sub();