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});
47 my $dbh = $form->dbconnect($myconfig);
49 my $query = qq|SELECT c.accno, c.description, c.charttype, c.gifi_accno,
50 c.category, c.link, c.taxkey_id, c.pos_ustva, c.pos_bwa, c.pos_bilanz,c.pos_eur
52 WHERE c.id = $form->{id}|;
54 my $sth = $dbh->prepare($query);
55 $sth->execute || $form->dberror($query);
57 my $ref = $sth->fetchrow_hashref(NAME_lc);
59 foreach my $key (keys %$ref) {
60 $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();
109 $main::lxdebug->enter_sub();
111 my ($self, $myconfig, $form) = @_;
113 # connect to database, turn off AutoCommit
114 my $dbh = $form->dbconnect_noauto($myconfig);
116 # sanity check, can't have AR with AR_...
117 if ($form->{AR} || $form->{AP} || $form->{IC}) {
118 map { delete $form->{$_} } 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},
136 $form->{IC_taxservice},
139 $form->{link} .= "${item}:" if ($item);
143 # if we have an id then replace the old record
144 $form->{description} =~ s/\'/\'\'/g;
146 # strip blanks from accno
147 map { $form->{$_} =~ s/ //g; } qw(accno);
151 if ($form->{id}eq "NULL") {
155 map({ $form->{$_} = "NULL" unless ($form->{$_}); } qw(pos_ustva pos_bwa pos_bilanz pos_eur));
158 $query = qq|UPDATE chart SET
159 accno = '$form->{accno}',
160 description = '$form->{description}',
161 charttype = '$form->{charttype}',
162 gifi_accno = '$form->{gifi_accno}',
163 category = '$form->{category}',
164 link = '$form->{link}',
165 taxkey_id = $form->{taxkey_id},
166 pos_ustva = $form->{pos_ustva},
167 pos_bwa = $form->{pos_bwa},
168 pos_bilanz = $form->{pos_bilanz},
169 pos_eur = $form->{pos_eur}
170 WHERE id = $form->{id}|;
173 $query = qq|INSERT INTO chart
174 (accno, description, charttype, gifi_accno, category, link, taxkey_id, pos_ustva, pos_bwa, pos_bilanz,pos_eur)
175 VALUES ('$form->{accno}', '$form->{description}',
176 '$form->{charttype}', '$form->{gifi_accno}',
177 '$form->{category}', '$form->{link}', $form->{taxkey_id}, $form->{pos_ustva}, $form->{pos_bwa}, $form->{pos_bilanz}, $form->{pos_eur})|;
179 $dbh->do($query) || $form->dberror($query);
182 if ($form->{IC_taxpart} || $form->{IC_taxservice} || $form->{CT_tax}) {
184 my $chart_id = $form->{id};
186 unless ($form->{id}) {
188 $query = qq|SELECT c.id
190 WHERE c.accno = '$form->{accno}'|;
191 $sth = $dbh->prepare($query);
192 $sth->execute || $form->dberror($query);
194 ($chart_id) = $sth->fetchrow_array;
198 # add account if it doesn't exist in tax
199 $query = qq|SELECT t.chart_id
201 WHERE t.chart_id = $chart_id|;
202 $sth = $dbh->prepare($query);
203 $sth->execute || $form->dberror($query);
205 my ($tax_id) = $sth->fetchrow_array;
208 # add tax if it doesn't exist
210 $query = qq|INSERT INTO tax (chart_id, rate)
211 VALUES ($chart_id, 0)|;
212 $dbh->do($query) || $form->dberror($query);
217 $query = qq|DELETE FROM tax
218 WHERE chart_id = $form->{id}|;
219 $dbh->do($query) || $form->dberror($query);
225 my $rc = $dbh->commit;
228 $main::lxdebug->leave_sub();
236 $main::lxdebug->enter_sub();
238 my ($self, $myconfig, $form) = @_;
240 # connect to database, turn off AutoCommit
241 my $dbh = $form->dbconnect_noauto($myconfig);
243 my $query = qq|SELECT count(*) FROM acc_trans a
244 WHERE a.chart_id = $form->{id}|;
245 my $sth = $dbh->prepare($query);
246 $sth->execute || $form->dberror($query);
248 if ($sth->fetchrow_array) {
251 $main::lxdebug->leave_sub();
257 # delete chart of account record
258 $query = qq|DELETE FROM chart
259 WHERE id = $form->{id}|;
260 $dbh->do($query) || $form->dberror($query);
262 # set inventory_accno_id, income_accno_id, expense_accno_id to defaults
263 $query = qq|UPDATE parts
264 SET inventory_accno_id =
265 (SELECT inventory_accno_id FROM defaults)
266 WHERE inventory_accno_id = $form->{id}|;
267 $dbh->do($query) || $form->dberror($query);
269 $query = qq|UPDATE parts
270 SET income_accno_id =
271 (SELECT income_accno_id FROM defaults)
272 WHERE income_accno_id = $form->{id}|;
273 $dbh->do($query) || $form->dberror($query);
275 $query = qq|UPDATE parts
276 SET expense_accno_id =
277 (SELECT expense_accno_id FROM defaults)
278 WHERE expense_accno_id = $form->{id}|;
279 $dbh->do($query) || $form->dberror($query);
281 foreach my $table (qw(partstax customertax vendortax tax)) {
282 $query = qq|DELETE FROM $table
283 WHERE chart_id = $form->{id}|;
284 $dbh->do($query) || $form->dberror($query);
287 # commit and redirect
288 my $rc = $dbh->commit;
291 $main::lxdebug->leave_sub();
298 $main::lxdebug->enter_sub();
300 my ($self, $myconfig, $form) = @_;
302 # connect to database
303 my $dbh = $form->dbconnect($myconfig);
305 my $query = qq|SELECT accno, description
309 $sth = $dbh->prepare($query);
310 $sth->execute || $form->dberror($query);
312 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
313 push @{ $form->{ALL} }, $ref;
319 $main::lxdebug->leave_sub();
325 $main::lxdebug->enter_sub();
327 my ($self, $myconfig, $form) = @_;
329 # connect to database
330 my $dbh = $form->dbconnect($myconfig);
332 my $query = qq|SELECT g.accno, g.description
334 WHERE g.accno = '$form->{accno}'|;
335 my $sth = $dbh->prepare($query);
336 $sth->execute || $form->dberror($query);
338 my $ref = $sth->fetchrow_hashref(NAME_lc);
340 map { $form->{$_} = $ref->{$_} } keys %$ref;
344 # check for transactions
345 $query = qq|SELECT count(*) FROM acc_trans a, chart c, gifi g
346 WHERE c.gifi_accno = g.accno
347 AND a.chart_id = c.id
348 AND g.accno = '$form->{accno}'|;
349 $sth = $dbh->prepare($query);
350 $sth->execute || $form->dberror($query);
352 ($form->{orphaned}) = $sth->fetchrow_array;
354 $form->{orphaned} = !$form->{orphaned};
358 $main::lxdebug->leave_sub();
363 $main::lxdebug->enter_sub();
365 my ($self, $myconfig, $form) = @_;
367 # connect to database
368 my $dbh = $form->dbconnect($myconfig);
370 $form->{description} =~ s/\'/\'\'/g;
372 # id is the old account number!
374 $query = qq|UPDATE gifi SET
375 accno = '$form->{accno}',
376 description = '$form->{description}'
377 WHERE accno = '$form->{id}'|;
379 $query = qq|INSERT INTO gifi
381 VALUES ('$form->{accno}', '$form->{description}')|;
383 $dbh->do($query) || $form->dberror($query);
387 $main::lxdebug->leave_sub();
392 $main::lxdebug->enter_sub();
394 my ($self, $myconfig, $form) = @_;
396 # connect to database
397 my $dbh = $form->dbconnect($myconfig);
399 # id is the old account number!
400 $query = qq|DELETE FROM gifi
401 WHERE accno = '$form->{id}'|;
402 $dbh->do($query) || $form->dberror($query);
406 $main::lxdebug->leave_sub();
411 $main::lxdebug->enter_sub();
413 my ($self, $myconfig, $form) = @_;
415 # connect to database
416 my $dbh = $form->dbconnect($myconfig);
418 my $query = qq|SELECT id, description
422 $sth = $dbh->prepare($query);
423 $sth->execute || $form->dberror($query);
425 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
426 push @{ $form->{ALL} }, $ref;
432 $main::lxdebug->leave_sub();
438 $main::lxdebug->enter_sub();
440 my ($self, $myconfig, $form) = @_;
442 # connect to database
443 my $dbh = $form->dbconnect($myconfig);
445 my $query = qq|SELECT w.description
447 WHERE w.id = $form->{id}|;
448 my $sth = $dbh->prepare($query);
449 $sth->execute || $form->dberror($query);
451 my $ref = $sth->fetchrow_hashref(NAME_lc);
453 map { $form->{$_} = $ref->{$_} } keys %$ref;
457 # see if it is in use
458 $query = qq|SELECT count(*) FROM inventory i
459 WHERE i.warehouse_id = $form->{id}|;
460 $sth = $dbh->prepare($query);
461 $sth->execute || $form->dberror($query);
463 ($form->{orphaned}) = $sth->fetchrow_array;
464 $form->{orphaned} = !$form->{orphaned};
469 $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 $form->{description} =~ s/\'/\'\'/g;
484 $query = qq|UPDATE warehouse SET
485 description = '$form->{description}'
486 WHERE id = $form->{id}|;
488 $query = qq|INSERT INTO warehouse
490 VALUES ('$form->{description}')|;
492 $dbh->do($query) || $form->dberror($query);
496 $main::lxdebug->leave_sub();
500 sub delete_warehouse {
501 $main::lxdebug->enter_sub();
503 my ($self, $myconfig, $form) = @_;
505 # connect to database
506 my $dbh = $form->dbconnect($myconfig);
508 $query = qq|DELETE FROM warehouse
509 WHERE id = $form->{id}|;
510 $dbh->do($query) || $form->dberror($query);
514 $main::lxdebug->leave_sub();
520 $main::lxdebug->enter_sub();
522 my ($self, $myconfig, $form) = @_;
524 # connect to database
525 my $dbh = $form->dbconnect($myconfig);
527 my $query = qq|SELECT d.id, d.description, d.role
531 $sth = $dbh->prepare($query);
532 $sth->execute || $form->dberror($query);
534 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
535 push @{ $form->{ALL} }, $ref;
541 $main::lxdebug->leave_sub();
547 $main::lxdebug->enter_sub();
549 my ($self, $myconfig, $form) = @_;
551 # connect to database
552 my $dbh = $form->dbconnect($myconfig);
554 my $query = qq|SELECT d.description, d.role
556 WHERE d.id = $form->{id}|;
557 my $sth = $dbh->prepare($query);
558 $sth->execute || $form->dberror($query);
560 my $ref = $sth->fetchrow_hashref(NAME_lc);
562 map { $form->{$_} = $ref->{$_} } keys %$ref;
566 # see if it is in use
567 $query = qq|SELECT count(*) FROM dpt_trans d
568 WHERE d.department_id = $form->{id}|;
569 $sth = $dbh->prepare($query);
570 $sth->execute || $form->dberror($query);
572 ($form->{orphaned}) = $sth->fetchrow_array;
573 $form->{orphaned} = !$form->{orphaned};
578 $main::lxdebug->leave_sub();
582 sub save_department {
583 $main::lxdebug->enter_sub();
585 my ($self, $myconfig, $form) = @_;
587 # connect to database
588 my $dbh = $form->dbconnect($myconfig);
590 $form->{description} =~ s/\'/\'\'/g;
593 $query = qq|UPDATE department SET
594 description = '$form->{description}',
595 role = '$form->{role}'
596 WHERE id = $form->{id}|;
598 $query = qq|INSERT INTO department
600 VALUES ('$form->{description}', '$form->{role}')|;
602 $dbh->do($query) || $form->dberror($query);
606 $main::lxdebug->leave_sub();
610 sub delete_department {
611 $main::lxdebug->enter_sub();
613 my ($self, $myconfig, $form) = @_;
615 # connect to database
616 my $dbh = $form->dbconnect($myconfig);
618 $query = qq|DELETE FROM department
619 WHERE id = $form->{id}|;
620 $dbh->do($query) || $form->dberror($query);
624 $main::lxdebug->leave_sub();
629 $main::lxdebug->enter_sub();
631 my ($self, $myconfig, $form) = @_;
633 # connect to database
634 my $dbh = $form->dbconnect($myconfig);
636 my $query = qq|SELECT id, description, discount, customernumberinit, salesman
640 $sth = $dbh->prepare($query);
641 $sth->execute || $form->dberror($query);
643 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
644 push @{ $form->{ALL} }, $ref;
650 $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 b.description, b.discount, b.customernumberinit, b.salesman
665 WHERE b.id = $form->{id}|;
666 my $sth = $dbh->prepare($query);
667 $sth->execute || $form->dberror($query);
669 my $ref = $sth->fetchrow_hashref(NAME_lc);
671 map { $form->{$_} = $ref->{$_} } keys %$ref;
677 $main::lxdebug->leave_sub();
682 $main::lxdebug->enter_sub();
684 my ($self, $myconfig, $form) = @_;
686 # connect to database
687 my $dbh = $form->dbconnect($myconfig);
689 $form->{description} =~ s/\'/\'\'/g;
690 $form->{discount} /= 100;
691 $form->{salesman} *= 1;
693 # id is the old record
695 $query = qq|UPDATE business SET
696 description = '$form->{description}',
697 discount = $form->{discount},
698 customernumberinit = '$form->{customernumberinit}',
699 salesman = '$form->{salesman}'
700 WHERE id = $form->{id}|;
702 $query = qq|INSERT INTO business
703 (description, discount, customernumberinit, salesman)
704 VALUES ('$form->{description}', $form->{discount}, '$form->{customernumberinit}', '$form->{salesman}')|;
706 $dbh->do($query) || $form->dberror($query);
710 $main::lxdebug->leave_sub();
714 sub delete_business {
715 $main::lxdebug->enter_sub();
717 my ($self, $myconfig, $form) = @_;
719 # connect to database
720 my $dbh = $form->dbconnect($myconfig);
722 $query = qq|DELETE FROM business
723 WHERE id = $form->{id}|;
724 $dbh->do($query) || $form->dberror($query);
728 $main::lxdebug->leave_sub();
733 $main::lxdebug->enter_sub();
735 my ($self, $myconfig, $form) = @_;
737 # connect to database
738 my $dbh = $form->dbconnect($myconfig);
740 my $query = qq|SELECT code, sictype, description
744 $sth = $dbh->prepare($query);
745 $sth->execute || $form->dberror($query);
747 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
748 push @{ $form->{ALL} }, $ref;
754 $main::lxdebug->leave_sub();
760 $main::lxdebug->enter_sub();
762 my ($self, $myconfig, $form) = @_;
764 # connect to database
765 my $dbh = $form->dbconnect($myconfig);
767 my $query = qq|SELECT s.code, s.sictype, s.description
769 WHERE s.code = '$form->{code}'|;
770 my $sth = $dbh->prepare($query);
771 $sth->execute || $form->dberror($query);
773 my $ref = $sth->fetchrow_hashref(NAME_lc);
775 map { $form->{$_} = $ref->{$_} } keys %$ref;
781 $main::lxdebug->leave_sub();
786 $main::lxdebug->enter_sub();
788 my ($self, $myconfig, $form) = @_;
790 # connect to database
791 my $dbh = $form->dbconnect($myconfig);
793 $form->{code} =~ s/\'/\'\'/g;
794 $form->{description} =~ s/\'/\'\'/g;
798 $query = qq|UPDATE sic SET
799 code = '$form->{code}',
800 sictype = '$form->{sictype}',
801 description = '$form->{description}'
802 WHERE code = '$form->{id}'|;
804 $query = qq|INSERT INTO sic
805 (code, sictype, description)
806 VALUES ('$form->{code}', '$form->{sictype}', '$form->{description}')|;
808 $dbh->do($query) || $form->dberror($query);
812 $main::lxdebug->leave_sub();
817 $main::lxdebug->enter_sub();
819 my ($self, $myconfig, $form) = @_;
821 # connect to database
822 my $dbh = $form->dbconnect($myconfig);
824 $query = qq|DELETE FROM sic
825 WHERE code = '$form->{code}'|;
826 $dbh->do($query) || $form->dberror($query);
830 $main::lxdebug->leave_sub();
835 $main::lxdebug->enter_sub();
837 my ($self, $form) = @_;
839 open(TEMPLATE, "$form->{file}") or $form->error("$form->{file} : $!");
847 $main::lxdebug->leave_sub();
852 $main::lxdebug->enter_sub();
854 my ($self, $form) = @_;
856 open(TEMPLATE, ">$form->{file}") or $form->error("$form->{file} : $!");
859 $form->{body} =~ s/\r\n/\n/g;
860 print TEMPLATE $form->{body};
864 $main::lxdebug->leave_sub();
869 sub save_preferences {
870 $main::lxdebug->enter_sub();
872 my ($self, $myconfig, $form, $memberfile, $userspath, $webdav) = @_;
874 map { ($form->{$_}) = split /--/, $form->{$_} } qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno);
877 $form->{curr} =~ s/ //g;
878 map { push(@a, uc pack "A3", $_) if $_ } split /:/, $form->{curr};
879 $form->{curr} = join ':', @a;
881 # connect to database
882 my $dbh = $form->dbconnect_noauto($myconfig);
884 # these defaults are database wide
885 # user specific variables are in myconfig
887 my $query = qq|UPDATE defaults SET
889 (SELECT c.id FROM chart c
890 WHERE c.accno = '$form->{inventory_accno}'),
892 (SELECT c.id FROM chart c
893 WHERE c.accno = '$form->{income_accno}'),
895 (SELECT c.id FROM chart c
896 WHERE c.accno = '$form->{expense_accno}'),
898 (SELECT c.id FROM chart c
899 WHERE c.accno = '$form->{fxgain_accno}'),
901 (SELECT c.id FROM chart c
902 WHERE c.accno = '$form->{fxloss_accno}'),
903 invnumber = '$form->{invnumber}',
904 sonumber = '$form->{sonumber}',
905 ponumber = '$form->{ponumber}',
906 sqnumber = '$form->{sqnumber}',
907 rfqnumber = '$form->{rfqnumber}',
908 customernumber = '$form->{customernumber}',
909 vendornumber = '$form->{vendornumber}',
910 articlenumber = '$form->{articlenumber}',
911 servicenumber = '$form->{servicenumber}',
912 yearend = '$form->{yearend}',
913 curr = '$form->{curr}',
914 weightunit = '$form->{weightunit}',
915 businessnumber = '$form->{businessnumber}'
917 $dbh->do($query) || $form->dberror($query);
920 my $name = $form->{name};
921 $name =~ s/\'/\'\'/g;
922 $query = qq|UPDATE employee
924 WHERE login = '$form->{login}'|;
925 $dbh->do($query) || $form->dberror($query);
927 foreach my $item (split / /, $form->{taxaccounts}) {
928 $query = qq|UPDATE tax
929 SET rate = |.($form->{$item} / 100).qq|,
930 taxnumber = '$form->{"taxnumber_$item"}'
931 WHERE chart_id = $item|;
932 $dbh->do($query) || $form->dberror($query);
935 my $rc = $dbh->commit;
938 # save first currency in myconfig
939 $form->{currency} = substr($form->{curr},0,3);
941 my $myconfig = new User "$memberfile", "$form->{login}";
943 foreach my $item (keys %$form) {
944 $myconfig->{$item} = $form->{$item};
947 $myconfig->save_member($memberfile, $userspath);
950 @webdavdirs = qw(angebote bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen);
951 foreach $directory (@webdavdirs) {
952 $file = "webdav/".$directory."/webdav-user";
953 if ($myconfig->{$directory}) {
954 open (HTACCESS, "$file") or die "cannot open webdav-user $!\n";
956 ($login,$password) = split(/:/, $_);
957 if ($login ne $form->{login}) {
962 open (HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
963 $newfile .= $myconfig->{login}.":".$myconfig->{password}."\n";
964 print (HTACCESS $newfile);
968 $form->{$directory} = 0;
969 open (HTACCESS, "$file") or die "cannot open webdav-user $!\n";
971 ($login,$password) = split(/:/, $_);
972 if ($login ne $form->{login}) {
977 open (HTACCESS, "> $file") or die "cannot open webdav-user $!\n";
978 print (HTACCESS $newfile);
984 $main::lxdebug->leave_sub();
990 sub defaultaccounts {
991 $main::lxdebug->enter_sub();
993 my ($self, $myconfig, $form) = @_;
995 # connect to database
996 my $dbh = $form->dbconnect($myconfig);
998 # get defaults from defaults table
999 my $query = qq|SELECT * FROM defaults|;
1000 my $sth = $dbh->prepare($query);
1001 $sth->execute || $form->dberror($query);
1003 $form->{defaults} = $sth->fetchrow_hashref(NAME_lc);
1004 $form->{defaults}{IC} = $form->{defaults}{inventory_accno_id};
1005 $form->{defaults}{IC_income} = $form->{defaults}{income_accno_id};
1006 $form->{defaults}{IC_expense} = $form->{defaults}{expense_accno_id};
1007 $form->{defaults}{FX_gain} = $form->{defaults}{fxgain_accno_id};
1008 $form->{defaults}{FX_loss} = $form->{defaults}{fxloss_accno_id};
1014 $query = qq|SELECT c.id, c.accno, c.description, c.link
1016 WHERE c.link LIKE '%IC%'
1018 $sth = $dbh->prepare($query);
1019 $sth->execute || $self->dberror($query);
1021 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1022 foreach my $key (split(/:/, $ref->{link})) {
1025 if ($key =~ /cogs/) {
1026 $nkey = "IC_expense";
1028 if ($key =~ /sale/) {
1029 $nkey = "IC_income";
1031 %{ $form->{IC}{$nkey}{$ref->{accno}} } = ( id => $ref->{id},
1032 description => $ref->{description} );
1039 $query = qq|SELECT c.id, c.accno, c.description
1041 WHERE c.category = 'I'
1042 AND c.charttype = 'A'
1044 $sth = $dbh->prepare($query);
1045 $sth->execute || $self->dberror($query);
1047 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1048 %{ $form->{IC}{FX_gain}{$ref->{accno}} } = ( id => $ref->{id},
1049 description => $ref->{description} );
1053 $query = qq|SELECT c.id, c.accno, c.description
1055 WHERE c.category = 'E'
1056 AND c.charttype = 'A'
1058 $sth = $dbh->prepare($query);
1059 $sth->execute || $self->dberror($query);
1061 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1062 %{ $form->{IC}{FX_loss}{$ref->{accno}} } = ( id => $ref->{id},
1063 description => $ref->{description} );
1068 # now get the tax rates and numbers
1069 $query = qq|SELECT c.id, c.accno, c.description,
1070 t.rate * 100 AS rate, t.taxnumber
1072 WHERE c.id = t.chart_id|;
1074 $sth = $dbh->prepare($query);
1075 $sth->execute || $form->dberror($query);
1077 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1078 $form->{taxrates}{$ref->{accno}}{id} = $ref->{id};
1079 $form->{taxrates}{$ref->{accno}}{description} = $ref->{description};
1080 $form->{taxrates}{$ref->{accno}}{taxnumber} = $ref->{taxnumber} if $ref->{taxnumber};
1081 $form->{taxrates}{$ref->{accno}}{rate} = $ref->{rate} if $ref->{rate};
1087 $main::lxdebug->leave_sub();
1092 $main::lxdebug->enter_sub();
1094 my ($self, $myconfig, $form, $userspath) = @_;
1098 my $boundary = time;
1099 my $tmpfile = "$userspath/$boundary.$myconfig->{dbname}-$form->{dbversion}.sql";
1100 my $out = $form->{OUT};
1101 $form->{OUT} = ">$tmpfile";
1103 if ($form->{media} eq 'email') {
1108 $mail->{to} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
1109 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
1110 $mail->{subject} = "Lx-Office Backup / $myconfig->{dbname}-$form->{dbversion}.sql";
1111 @{ $mail->{attachments} } = ($tmpfile);
1112 $mail->{version} = $form->{version};
1113 $mail->{fileid} = "$boundary.";
1115 $myconfig->{signature} =~ s/\\n/\r\n/g;
1116 $mail->{message} = "--\n$myconfig->{signature}";
1121 open(OUT, "$form->{OUT}") or $form->error("$form->{OUT} : $!");
1123 # get sequences, functions and triggers
1124 open(FH, "sql/lx-office.sql") or $form->error("sql/lx-office.sql : $!");
1136 # Remove DOS and Unix style line endings.
1139 # ignore comments or empty lines
1140 next if /^(--.*|\s+)$/;
1142 for (my $i = 0; $i < length($_); $i++) {
1143 my $char = substr($_, $i, 1);
1145 # Are we inside a string?
1147 if ($char eq $quote_chars[-1]) {
1153 if (($char eq "'") || ($char eq "\"")) {
1154 push(@quote_chars, $char);
1156 } elsif ($char eq ";") {
1157 # Query is complete. Check for triggers and functions.
1158 if ($query =~ /^create\s+function\s+\"?(\w+)\"?/i) {
1159 push(@functions, $query);
1161 } elsif ($query =~ /^create\s+trigger\s+\"?(\w+)\"?/i) {
1162 push(@triggers, $query);
1164 } elsif ($query =~ /^create\s+sequence\s+\"?(\w+)\"?/i) {
1165 push(@sequences, $1);
1167 } elsif ($query =~ /^create\s+table\s+\"?(\w+)\"?/i) {
1168 $tablespecs{$1} = $query;
1170 } elsif ($query =~ /^create\s+index\s+\"?(\w+)\"?/i) {
1171 push(@indices, $query);
1186 # connect to database
1187 my $dbh = $form->dbconnect($myconfig);
1189 # get all the tables
1190 my @tables = $dbh->tables( '', '', 'customer', '', {noprefix => 0});
1192 my $today = scalar localtime;
1194 $myconfig->{dbhost} = 'localhost' unless $myconfig->{dbhost};
1196 print OUT qq|-- Lx-Office Backup
1197 -- Dataset: $myconfig->{dbname}
1198 -- Version: $form->{dbversion}
1199 -- Host: $myconfig->{dbhost}
1200 -- Login: $form->{login}
1201 -- User: $myconfig->{name}
1205 $myconfig->{dboptions};
1209 print OUT "-- DROP Sequences\n";
1211 foreach $item (@sequences) {
1212 print OUT qq|DROP SEQUENCE $item;\n|;
1215 print OUT "-- DROP Triggers\n";
1217 foreach $item (@triggers) {
1218 if ($item =~ /^create\s+trigger\s+\"?(\w+)\"?\s+.*on\s+\"?(\w+)\"?\s+/i) {
1219 print OUT qq|DROP TRIGGER "$1" ON "$2";\n|;
1223 print OUT "-- DROP Functions\n";
1225 foreach $item (@functions) {
1226 if ($item =~ /^create\s+function\s+\"?(\w+)\"?/i) {
1227 print OUT qq|DROP FUNCTION "$1" ();\n|;
1231 foreach $table (@tables) {
1232 if (!($table =~ /^sql_.*/)) {
1233 my $query = qq|SELECT * FROM $table|;
1235 my $sth = $dbh->prepare($query);
1236 $sth->execute || $form->dberror($query);
1238 $query = "INSERT INTO $table (";
1239 map { $query .= qq|$sth->{NAME}->[$_],| } (0 .. $sth->{NUM_OF_FIELDS} - 1);
1242 $query .= ") VALUES";
1244 if ($tablespecs{$table}) {
1246 print(OUT "DROP TABLE $table;\n");
1247 print(OUT $tablespecs{$table}, ";\n");
1250 print(OUT "DELETE FROM $table;\n");
1252 while (my @arr = $sth->fetchrow_array) {
1255 foreach my $item (@arr) {
1256 if (defined $item) {
1257 $item =~ s/\'/\'\'/g;
1258 $fields .= qq|'$item',|;
1267 print OUT qq|$query $fields;\n|;
1275 # create indices, sequences, functions and triggers
1277 print(OUT "-- CREATE Indices\n");
1278 map({ print(OUT "$_;\n"); } @indices);
1280 print OUT "-- CREATE Sequences\n";
1281 foreach $item (@sequences) {
1282 $query = qq|SELECT last_value FROM $item|;
1283 $sth = $dbh->prepare($query);
1284 $sth->execute || $form->dberror($query);
1285 my ($id) = $sth->fetchrow_array;
1289 CREATE SEQUENCE $item START $id;
1293 print OUT "-- CREATE Functions\n";
1296 map { print(OUT $_, ";\n"); } @functions;
1298 print OUT "-- CREATE Triggers\n";
1301 map { print(OUT $_, ";\n"); } @triggers;
1309 my @args = ("gzip", "$tmpfile");
1310 system(@args) == 0 or $form->error("$args[0] : $?");
1314 if ($form->{media} eq 'email') {
1315 @{ $mail->{attachments} } = ($tmpfile);
1316 $err = $mail->send($out);
1320 if ($form->{media} eq 'file') {
1322 open(IN, "$tmpfile") or $form->error("$tmpfile : $!");
1323 open(OUT, ">-") or $form->error("STDOUT : $!");
1325 print OUT qq|Content-Type: application/x-tar-gzip;
1326 Content-Disposition: attachment; filename="$myconfig->{dbname}-$form->{dbversion}.sql.gz"
1341 $main::lxdebug->leave_sub();
1346 $main::lxdebug->enter_sub();
1348 my ($self, $myconfig, $form) = @_;
1350 my $dbh = $form->dbconnect($myconfig);
1352 my $query = qq|SELECT closedto, revtrans FROM defaults|;
1353 my $sth = $dbh->prepare($query);
1354 $sth->execute || $form->dberror($query);
1356 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
1362 $main::lxdebug->leave_sub();
1367 $main::lxdebug->enter_sub();
1369 my ($self, $myconfig, $form) = @_;
1371 my $dbh = $form->dbconnect($myconfig);
1373 if ($form->{revtrans}) {
1375 $query = qq|UPDATE defaults SET closedto = NULL,
1378 if ($form->{closedto}) {
1380 $query = qq|UPDATE defaults SET closedto = '$form->{closedto}',
1384 $query = qq|UPDATE defaults SET closedto = NULL,
1389 # set close in defaults
1390 $dbh->do($query) || $form->dberror($query);
1394 $main::lxdebug->leave_sub();