X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FAM.pm;h=a2e201f61149afe801583e0ee474346c51b50dab;hb=baf92f533975d1224700a85d0b8ededd8246d09b;hp=81e463b0fcdd6c346fe4a8813d355560c405c7eb;hpb=4dbb09950c9f5596646537c12d991c99086fe7c1;p=kivitendo-erp.git diff --git a/SL/AM.pm b/SL/AM.pm index 81e463b0f..a2e201f61 100644 --- a/SL/AM.pm +++ b/SL/AM.pm @@ -37,32 +37,34 @@ package AM; +use Data::Dumper; + sub get_account { $main::lxdebug->enter_sub(); my ($self, $myconfig, $form) = @_; - + $form->{id} = "NULL" unless ($form->{id}); + # connect to database my $dbh = $form->dbconnect($myconfig); my $query = qq|SELECT c.accno, c.description, c.charttype, c.gifi_accno, - c.category, c.link, c.taxkey_id, c.pos_ustva, c.pos_bwa, c.pos_bilanz,c.pos_eur + c.category, c.link, c.taxkey_id, c.pos_ustva, c.pos_bwa, c.pos_bilanz,c.pos_eur, c.new_chart_id, c.valid_from FROM chart c WHERE c.id = $form->{id}|; - + my $sth = $dbh->prepare($query); $sth->execute || $form->dberror($query); my $ref = $sth->fetchrow_hashref(NAME_lc); - + foreach my $key (keys %$ref) { $form->{"$key"} = $ref->{"$key"}; } $sth->finish; - # get default accounts $query = qq|SELECT inventory_accno_id, income_accno_id, expense_accno_id FROM defaults|; @@ -76,7 +78,7 @@ sub get_account { $sth->finish; # get taxkeys and description - $query = qq|SELECT taxkey, taxdescription + $query = qq|SELECT taxkey, taxdescription FROM tax|; $sth = $dbh->prepare($query); $sth->execute || $form->dberror($query); @@ -88,7 +90,23 @@ sub get_account { } $sth->finish; + if ($form->{id}) { + $where = " WHERE link='$form->{link}'"; + + + # get new accounts + $query = qq|SELECT id, accno,description + FROM chart $where|; + $sth = $dbh->prepare($query); + $sth->execute || $form->dberror($query); + + while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { + push @{ $form->{NEWACCOUNT} }, $ref; + } + + $sth->finish; + } # check if we have any transactions $query = qq|SELECT a.trans_id FROM acc_trans a WHERE a.chart_id = $form->{id}|; @@ -99,12 +117,26 @@ sub get_account { $form->{orphaned} = !$form->{orphaned}; $sth->finish; + # check if new account is active + $form->{new_chart_valid} = 0; + if ($form->{new_chart_id}) { + $query = qq|SELECT current_date-valid_from FROM chart + WHERE id = $form->{id}|; + $sth = $dbh->prepare($query); + $sth->execute || $form->dberror($query); + + my ($count) = $sth->fetchrow_array; + if ($count >=0) { + $form->{new_chart_valid} = 1; + } + $sth->finish; + } + $dbh->disconnect; $main::lxdebug->leave_sub(); } - sub save_account { $main::lxdebug->enter_sub(); @@ -115,28 +147,21 @@ sub save_account { # sanity check, can't have AR with AR_... if ($form->{AR} || $form->{AP} || $form->{IC}) { - 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); + 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); } - + $form->{link} = ""; - foreach my $item ($form->{AR}, - $form->{AR_amount}, - $form->{AR_tax}, - $form->{AR_paid}, - $form->{AP}, - $form->{AP_amount}, - $form->{AP_tax}, - $form->{AP_paid}, - $form->{IC}, - $form->{IC_sale}, - $form->{IC_cogs}, - $form->{IC_taxpart}, - $form->{IC_income}, - $form->{IC_expense}, - $form->{IC_taxservice}, - $form->{CT_tax} - ) { - $form->{link} .= "${item}:" if ($item); + foreach my $item ($form->{AR}, $form->{AR_amount}, + $form->{AR_tax}, $form->{AR_paid}, + $form->{AP}, $form->{AP_amount}, + $form->{AP_tax}, $form->{AP_paid}, + $form->{IC}, $form->{IC_sale}, + $form->{IC_cogs}, $form->{IC_taxpart}, + $form->{IC_income}, $form->{IC_expense}, + $form->{IC_taxservice}, $form->{CT_tax} + ) { + $form->{link} .= "${item}:" if ($item); } chop $form->{link}; @@ -145,16 +170,19 @@ sub save_account { # strip blanks from accno map { $form->{$_} =~ s/ //g; } qw(accno); - + my ($query, $sth); - - if ($form->{id}eq "NULL") { - $form->{id}=""; - } - - map({ $form->{$_} = "NULL" unless ($form->{$_}); } qw(pos_ustva pos_bwa pos_bilanz pos_eur)); - if ($form->{id}) { + if ($form->{id} eq "NULL") { + $form->{id} = ""; + } + + map({ $form->{$_} = "NULL" unless ($form->{$_}); } + qw(pos_ustva pos_bwa pos_bilanz pos_eur new_chart_id)); + + $form->{valid_from} = ($form->{valid_from}) ? "'$form->{valid_from}'" : "NULL"; + + if ($form->{id} && $form->{orphaned}) { $query = qq|UPDATE chart SET accno = '$form->{accno}', description = '$form->{description}', @@ -166,24 +194,31 @@ sub save_account { pos_ustva = $form->{pos_ustva}, pos_bwa = $form->{pos_bwa}, pos_bilanz = $form->{pos_bilanz}, - pos_eur = $form->{pos_eur} + pos_eur = $form->{pos_eur}, + new_chart_id = $form->{new_chart_id}, + valid_from = $form->{valid_from} + WHERE id = $form->{id}|; + } elsif ($form->{id} && !$form->{new_chart_valid}) { + $query = qq|UPDATE chart SET + new_chart_id = $form->{new_chart_id}, + valid_from = $form->{valid_from} WHERE id = $form->{id}|; } else { - - $query = qq|INSERT INTO chart - (accno, description, charttype, gifi_accno, category, link, taxkey_id, pos_ustva, pos_bwa, pos_bilanz,pos_eur) + + $query = qq|INSERT INTO chart + (accno, description, charttype, gifi_accno, category, link, taxkey_id, pos_ustva, pos_bwa, pos_bilanz,pos_eur, new_chart_id, valid_from) VALUES ('$form->{accno}', '$form->{description}', '$form->{charttype}', '$form->{gifi_accno}', - '$form->{category}', '$form->{link}', $form->{taxkey_id}, $form->{pos_ustva}, $form->{pos_bwa}, $form->{pos_bilanz}, $form->{pos_eur})|; + '$form->{category}', '$form->{link}', $form->{taxkey_id}, $form->{pos_ustva}, $form->{pos_bwa}, $form->{pos_bilanz}, $form->{pos_eur}, $form->{new_chart_id}, $form->{valid_from})|; } $dbh->do($query) || $form->dberror($query); - if ($form->{IC_taxpart} || $form->{IC_taxservice} || $form->{CT_tax}) { my $chart_id = $form->{id}; - + unless ($form->{id}) { + # get id from chart $query = qq|SELECT c.id FROM chart c @@ -194,7 +229,7 @@ sub save_account { ($chart_id) = $sth->fetchrow_array; $sth->finish; } - + # add account if it doesn't exist in tax $query = qq|SELECT t.chart_id FROM tax t @@ -204,7 +239,7 @@ sub save_account { my ($tax_id) = $sth->fetchrow_array; $sth->finish; - + # add tax if it doesn't exist unless ($tax_id) { $query = qq|INSERT INTO tax (chart_id, rate) @@ -212,6 +247,7 @@ sub save_account { $dbh->do($query) || $form->dberror($query); } } else { + # remove tax if ($form->{id}) { $query = qq|DELETE FROM tax @@ -220,7 +256,6 @@ sub save_account { } } - # commit my $rc = $dbh->commit; $dbh->disconnect; @@ -230,8 +265,6 @@ sub save_account { return $rc; } - - sub delete_account { $main::lxdebug->enter_sub(); @@ -253,7 +286,6 @@ sub delete_account { } $sth->finish; - # delete chart of account record $query = qq|DELETE FROM chart WHERE id = $form->{id}|; @@ -261,23 +293,23 @@ sub delete_account { # set inventory_accno_id, income_accno_id, expense_accno_id to defaults $query = qq|UPDATE parts - SET inventory_accno_id = + SET inventory_accno_id = (SELECT inventory_accno_id FROM defaults) WHERE inventory_accno_id = $form->{id}|; $dbh->do($query) || $form->dberror($query); - + $query = qq|UPDATE parts SET income_accno_id = (SELECT income_accno_id FROM defaults) WHERE income_accno_id = $form->{id}|; $dbh->do($query) || $form->dberror($query); - + $query = qq|UPDATE parts SET expense_accno_id = (SELECT expense_accno_id FROM defaults) WHERE expense_accno_id = $form->{id}|; $dbh->do($query) || $form->dberror($query); - + foreach my $table (qw(partstax customertax vendortax tax)) { $query = qq|DELETE FROM $table WHERE chart_id = $form->{id}|; @@ -293,12 +325,11 @@ sub delete_account { return $rc; } - sub gifi_accounts { $main::lxdebug->enter_sub(); my ($self, $myconfig, $form) = @_; - + # connect to database my $dbh = $form->dbconnect($myconfig); @@ -315,12 +346,10 @@ sub gifi_accounts { $sth->finish; $dbh->disconnect; - + $main::lxdebug->leave_sub(); } - - sub get_gifi { $main::lxdebug->enter_sub(); @@ -328,7 +357,7 @@ sub get_gifi { # connect to database my $dbh = $form->dbconnect($myconfig); - + my $query = qq|SELECT g.accno, g.description FROM gifi g WHERE g.accno = '$form->{accno}'|; @@ -336,7 +365,7 @@ sub get_gifi { $sth->execute || $form->dberror($query); my $ref = $sth->fetchrow_hashref(NAME_lc); - + map { $form->{$_} = $ref->{$_} } keys %$ref; $sth->finish; @@ -358,15 +387,14 @@ sub get_gifi { $main::lxdebug->leave_sub(); } - sub save_gifi { $main::lxdebug->enter_sub(); my ($self, $myconfig, $form) = @_; - + # connect to database my $dbh = $form->dbconnect($myconfig); - + $form->{description} =~ s/\'/\'\'/g; # id is the old account number! @@ -376,42 +404,40 @@ sub save_gifi { description = '$form->{description}' WHERE accno = '$form->{id}'|; } else { - $query = qq|INSERT INTO gifi + $query = qq|INSERT INTO gifi (accno, description) VALUES ('$form->{accno}', '$form->{description}')|; } $dbh->do($query) || $form->dberror($query); - + $dbh->disconnect; $main::lxdebug->leave_sub(); } - sub delete_gifi { $main::lxdebug->enter_sub(); my ($self, $myconfig, $form) = @_; - + # connect to database my $dbh = $form->dbconnect($myconfig); - + # id is the old account number! $query = qq|DELETE FROM gifi WHERE accno = '$form->{id}'|; $dbh->do($query) || $form->dberror($query); - + $dbh->disconnect; $main::lxdebug->leave_sub(); } - sub warehouses { $main::lxdebug->enter_sub(); my ($self, $myconfig, $form) = @_; - + # connect to database my $dbh = $form->dbconnect($myconfig); @@ -428,12 +454,10 @@ sub warehouses { $sth->finish; $dbh->disconnect; - + $main::lxdebug->leave_sub(); } - - sub get_warehouse { $main::lxdebug->enter_sub(); @@ -441,7 +465,7 @@ sub get_warehouse { # connect to database my $dbh = $form->dbconnect($myconfig); - + my $query = qq|SELECT w.description FROM warehouse w WHERE w.id = $form->{id}|; @@ -449,7 +473,7 @@ sub get_warehouse { $sth->execute || $form->dberror($query); my $ref = $sth->fetchrow_hashref(NAME_lc); - + map { $form->{$_} = $ref->{$_} } keys %$ref; $sth->finish; @@ -469,15 +493,14 @@ sub get_warehouse { $main::lxdebug->leave_sub(); } - sub save_warehouse { $main::lxdebug->enter_sub(); my ($self, $myconfig, $form) = @_; - + # connect to database my $dbh = $form->dbconnect($myconfig); - + $form->{description} =~ s/\'/\'\'/g; if ($form->{id}) { @@ -490,37 +513,34 @@ sub save_warehouse { VALUES ('$form->{description}')|; } $dbh->do($query) || $form->dberror($query); - + $dbh->disconnect; $main::lxdebug->leave_sub(); } - sub delete_warehouse { $main::lxdebug->enter_sub(); my ($self, $myconfig, $form) = @_; - + # connect to database my $dbh = $form->dbconnect($myconfig); - + $query = qq|DELETE FROM warehouse WHERE id = $form->{id}|; $dbh->do($query) || $form->dberror($query); - + $dbh->disconnect; $main::lxdebug->leave_sub(); } - - sub departments { $main::lxdebug->enter_sub(); my ($self, $myconfig, $form) = @_; - + # connect to database my $dbh = $form->dbconnect($myconfig); @@ -537,12 +557,10 @@ sub departments { $sth->finish; $dbh->disconnect; - + $main::lxdebug->leave_sub(); } - - sub get_department { $main::lxdebug->enter_sub(); @@ -550,7 +568,7 @@ sub get_department { # connect to database my $dbh = $form->dbconnect($myconfig); - + my $query = qq|SELECT d.description, d.role FROM department d WHERE d.id = $form->{id}|; @@ -558,7 +576,7 @@ sub get_department { $sth->execute || $form->dberror($query); my $ref = $sth->fetchrow_hashref(NAME_lc); - + map { $form->{$_} = $ref->{$_} } keys %$ref; $sth->finish; @@ -578,15 +596,14 @@ sub get_department { $main::lxdebug->leave_sub(); } - sub save_department { $main::lxdebug->enter_sub(); my ($self, $myconfig, $form) = @_; - + # connect to database my $dbh = $form->dbconnect($myconfig); - + $form->{description} =~ s/\'/\'\'/g; if ($form->{id}) { @@ -595,46 +612,44 @@ sub save_department { role = '$form->{role}' WHERE id = $form->{id}|; } else { - $query = qq|INSERT INTO department + $query = qq|INSERT INTO department (description, role) VALUES ('$form->{description}', '$form->{role}')|; } $dbh->do($query) || $form->dberror($query); - + $dbh->disconnect; $main::lxdebug->leave_sub(); } - sub delete_department { $main::lxdebug->enter_sub(); my ($self, $myconfig, $form) = @_; - + # connect to database my $dbh = $form->dbconnect($myconfig); - + $query = qq|DELETE FROM department WHERE id = $form->{id}|; $dbh->do($query) || $form->dberror($query); - + $dbh->disconnect; $main::lxdebug->leave_sub(); } - -sub business { +sub lead { $main::lxdebug->enter_sub(); my ($self, $myconfig, $form) = @_; - + # connect to database my $dbh = $form->dbconnect($myconfig); - my $query = qq|SELECT id, description, discount, customernumberinit, salesman - FROM business + my $query = qq|SELECT id, lead + FROM leads ORDER BY 2|; $sth = $dbh->prepare($query); @@ -646,28 +661,27 @@ sub business { $sth->finish; $dbh->disconnect; - + $main::lxdebug->leave_sub(); } - - -sub get_business { +sub get_lead { $main::lxdebug->enter_sub(); my ($self, $myconfig, $form) = @_; # connect to database my $dbh = $form->dbconnect($myconfig); - - my $query = qq|SELECT b.description, b.discount, b.customernumberinit, b.salesman - FROM business b - WHERE b.id = $form->{id}|; + + my $query = + qq|SELECT l.id, l.lead + FROM leads l + WHERE l.id = $form->{id}|; my $sth = $dbh->prepare($query); $sth->execute || $form->dberror($query); my $ref = $sth->fetchrow_hashref(NAME_lc); - + map { $form->{$_} = $ref->{$_} } keys %$ref; $sth->finish; @@ -677,69 +691,61 @@ sub get_business { $main::lxdebug->leave_sub(); } - -sub save_business { +sub save_lead { $main::lxdebug->enter_sub(); my ($self, $myconfig, $form) = @_; - + # connect to database my $dbh = $form->dbconnect($myconfig); - - $form->{description} =~ s/\'/\'\'/g; - $form->{discount} /= 100; - $form->{salesman} *= 1; - + + $form->{lead} =~ s/\'/\'\'/g; + # id is the old record if ($form->{id}) { - $query = qq|UPDATE business SET - description = '$form->{description}', - discount = $form->{discount}, - customernumberinit = '$form->{customernumberinit}', - salesman = '$form->{salesman}' + $query = qq|UPDATE leads SET + lead = '$form->{description}' WHERE id = $form->{id}|; } else { - $query = qq|INSERT INTO business - (description, discount, customernumberinit, salesman) - VALUES ('$form->{description}', $form->{discount}, '$form->{customernumberinit}', '$form->{salesman}')|; + $query = qq|INSERT INTO leads + (lead) + VALUES ('$form->{description}')|; } $dbh->do($query) || $form->dberror($query); - + $dbh->disconnect; $main::lxdebug->leave_sub(); } - -sub delete_business { +sub delete_lead { $main::lxdebug->enter_sub(); my ($self, $myconfig, $form) = @_; - + # connect to database my $dbh = $form->dbconnect($myconfig); - - $query = qq|DELETE FROM business + + $query = qq|DELETE FROM leads WHERE id = $form->{id}|; $dbh->do($query) || $form->dberror($query); - + $dbh->disconnect; $main::lxdebug->leave_sub(); } - -sub sic { +sub business { $main::lxdebug->enter_sub(); my ($self, $myconfig, $form) = @_; - + # connect to database my $dbh = $form->dbconnect($myconfig); - my $query = qq|SELECT code, sictype, description - FROM sic - ORDER BY code|; + my $query = qq|SELECT id, description, discount, customernumberinit, salesman + FROM business + ORDER BY 2|; $sth = $dbh->prepare($query); $sth->execute || $form->dberror($query); @@ -750,28 +756,27 @@ sub sic { $sth->finish; $dbh->disconnect; - + $main::lxdebug->leave_sub(); } - - -sub get_sic { +sub get_business { $main::lxdebug->enter_sub(); my ($self, $myconfig, $form) = @_; # connect to database my $dbh = $form->dbconnect($myconfig); - - my $query = qq|SELECT s.code, s.sictype, s.description - FROM sic s - WHERE s.code = '$form->{code}'|; + + my $query = + qq|SELECT b.description, b.discount, b.customernumberinit, b.salesman + FROM business b + WHERE b.id = $form->{id}|; my $sth = $dbh->prepare($query); $sth->execute || $form->dberror($query); my $ref = $sth->fetchrow_hashref(NAME_lc); - + map { $form->{$_} = $ref->{$_} } keys %$ref; $sth->finish; @@ -781,61 +786,721 @@ sub get_sic { $main::lxdebug->leave_sub(); } - -sub save_sic { +sub save_business { $main::lxdebug->enter_sub(); my ($self, $myconfig, $form) = @_; - + # connect to database my $dbh = $form->dbconnect($myconfig); - - $form->{code} =~ s/\'/\'\'/g; + $form->{description} =~ s/\'/\'\'/g; - - # if there is an id + $form->{discount} /= 100; + $form->{salesman} *= 1; + + # id is the old record if ($form->{id}) { - $query = qq|UPDATE sic SET - code = '$form->{code}', - sictype = '$form->{sictype}', - description = '$form->{description}' - WHERE code = '$form->{id}'|; + $query = qq|UPDATE business SET + description = '$form->{description}', + discount = $form->{discount}, + customernumberinit = '$form->{customernumberinit}', + salesman = '$form->{salesman}' + WHERE id = $form->{id}|; } else { - $query = qq|INSERT INTO sic - (code, sictype, description) - VALUES ('$form->{code}', '$form->{sictype}', '$form->{description}')|; + $query = qq|INSERT INTO business + (description, discount, customernumberinit, salesman) + VALUES ('$form->{description}', $form->{discount}, '$form->{customernumberinit}', '$form->{salesman}')|; } $dbh->do($query) || $form->dberror($query); - + $dbh->disconnect; $main::lxdebug->leave_sub(); } - -sub delete_sic { +sub delete_business { $main::lxdebug->enter_sub(); my ($self, $myconfig, $form) = @_; - + # connect to database my $dbh = $form->dbconnect($myconfig); - - $query = qq|DELETE FROM sic - WHERE code = '$form->{code}'|; + + $query = qq|DELETE FROM business + WHERE id = $form->{id}|; $dbh->do($query) || $form->dberror($query); - + $dbh->disconnect; $main::lxdebug->leave_sub(); } -sub load_template { +sub language { $main::lxdebug->enter_sub(); - my ($self, $form) = @_; - + my ($self, $myconfig, $form) = @_; + + # connect to database + my $dbh = $form->dbconnect($myconfig); + + my $query = qq|SELECT id, description, template_code, article_code + FROM language + ORDER BY 2|; + + $sth = $dbh->prepare($query); + $sth->execute || $form->dberror($query); + + while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { + push @{ $form->{ALL} }, $ref; + } + + $sth->finish; + $dbh->disconnect; + + $main::lxdebug->leave_sub(); +} + +sub get_language { + $main::lxdebug->enter_sub(); + + my ($self, $myconfig, $form) = @_; + + # connect to database + my $dbh = $form->dbconnect($myconfig); + + my $query = + qq|SELECT l.description, l.template_code, l.article_code + FROM language l + WHERE l.id = $form->{id}|; + my $sth = $dbh->prepare($query); + $sth->execute || $form->dberror($query); + + my $ref = $sth->fetchrow_hashref(NAME_lc); + + map { $form->{$_} = $ref->{$_} } keys %$ref; + + $sth->finish; + + $dbh->disconnect; + + $main::lxdebug->leave_sub(); +} + +sub save_language { + $main::lxdebug->enter_sub(); + + my ($self, $myconfig, $form) = @_; + + # connect to database + my $dbh = $form->dbconnect($myconfig); + + $form->{description} =~ s/\'/\'\'/g; + $form->{article_code} =~ s/\'/\'\'/g; + $form->{template_code} =~ s/\'/\'\'/g; + + + # id is the old record + if ($form->{id}) { + $query = qq|UPDATE language SET + description = '$form->{description}', + template_code = '$form->{template_code}', + article_code = '$form->{article_code}' + WHERE id = $form->{id}|; + } else { + $query = qq|INSERT INTO language + (description, template_code, article_code) + VALUES ('$form->{description}', '$form->{template_code}', '$form->{article_code}')|; + } + $dbh->do($query) || $form->dberror($query); + + $dbh->disconnect; + + $main::lxdebug->leave_sub(); +} + +sub delete_language { + $main::lxdebug->enter_sub(); + + my ($self, $myconfig, $form) = @_; + + # connect to database + my $dbh = $form->dbconnect($myconfig); + + $query = qq|DELETE FROM language + WHERE id = $form->{id}|; + $dbh->do($query) || $form->dberror($query); + + $dbh->disconnect; + + $main::lxdebug->leave_sub(); +} + + +sub buchungsgruppe { + $main::lxdebug->enter_sub(); + + my ($self, $myconfig, $form) = @_; + + # connect to database + my $dbh = $form->dbconnect($myconfig); + + 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 + FROM buchungsgruppen + ORDER BY id|; + + $sth = $dbh->prepare($query); + $sth->execute || $form->dberror($query); + + while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { + push @{ $form->{ALL} }, $ref; + } + + $sth->finish; + $dbh->disconnect; + + $main::lxdebug->leave_sub(); +} + +sub get_buchungsgruppe { + $main::lxdebug->enter_sub(); + + my ($self, $myconfig, $form) = @_; + + # connect to database + my $dbh = $form->dbconnect($myconfig); + + if ($form->{id}) { + my $query = + 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 + FROM buchungsgruppen + WHERE id = $form->{id}|; + my $sth = $dbh->prepare($query); + $sth->execute || $form->dberror($query); + + my $ref = $sth->fetchrow_hashref(NAME_lc); + + map { $form->{$_} = $ref->{$_} } keys %$ref; + + $sth->finish; + + my $query = + qq|SELECT count(id) as anzahl + FROM parts + WHERE buchungsgruppen_id = $form->{id}|; + my $sth = $dbh->prepare($query); + $sth->execute || $form->dberror($query); + + my $ref = $sth->fetchrow_hashref(NAME_lc); + if (!$ref->{anzahl}) { + $form->{orphaned} = 1; + } + $sth->finish; + + } + my $module = "IC"; + $query = qq|SELECT c.accno, c.description, c.link, c.id, + d.inventory_accno_id, d.income_accno_id, d.expense_accno_id + FROM chart c, defaults d + WHERE c.link LIKE '%$module%' + ORDER BY c.accno|; + + + my $sth = $dbh->prepare($query); + $sth->execute || $form->dberror($query); + while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { + foreach my $key (split /:/, $ref->{link}) { + if ($key =~ /$module/) { + if ( ($ref->{id} eq $ref->{inventory_accno_id}) + || ($ref->{id} eq $ref->{income_accno_id}) + || ($ref->{id} eq $ref->{expense_accno_id})) { + push @{ $form->{"${module}_links"}{$key} }, + { accno => $ref->{accno}, + description => $ref->{description}, + selected => "selected", + id => $ref->{id} }; + } else { + push @{ $form->{"${module}_links"}{$key} }, + { accno => $ref->{accno}, + description => $ref->{description}, + selected => "", + id => $ref->{id} }; + } + } + } + } + $sth->finish; + + + $dbh->disconnect; + + $main::lxdebug->leave_sub(); +} + +sub save_buchungsgruppe { + $main::lxdebug->enter_sub(); + + my ($self, $myconfig, $form) = @_; + + # connect to database + my $dbh = $form->dbconnect($myconfig); + + $form->{description} =~ s/\'/\'\'/g; + + + # id is the old record + if ($form->{id}) { + $query = qq|UPDATE buchungsgruppen SET + description = '$form->{description}', + inventory_accno_id = '$form->{inventory_accno_id}', + income_accno_id_0 = '$form->{income_accno_id_0}', + expense_accno_id_0 = '$form->{expense_accno_id_0}', + income_accno_id_1 = '$form->{income_accno_id_1}', + expense_accno_id_1 = '$form->{expense_accno_id_1}', + income_accno_id_2 = '$form->{income_accno_id_2}', + expense_accno_id_2 = '$form->{expense_accno_id_2}', + income_accno_id_3 = '$form->{income_accno_id_3}', + expense_accno_id_3 = '$form->{expense_accno_id_3}' + WHERE id = $form->{id}|; + } else { + $query = qq|INSERT INTO buchungsgruppen + (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) + 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}')|; + } + $dbh->do($query) || $form->dberror($query); + + $dbh->disconnect; + + $main::lxdebug->leave_sub(); +} + +sub delete_buchungsgruppe { + $main::lxdebug->enter_sub(); + + my ($self, $myconfig, $form) = @_; + + # connect to database + my $dbh = $form->dbconnect($myconfig); + + $query = qq|DELETE FROM buchungsgruppe + WHERE id = $form->{id}|; + $dbh->do($query) || $form->dberror($query); + + $dbh->disconnect; + + $main::lxdebug->leave_sub(); +} + +sub printer { + $main::lxdebug->enter_sub(); + + my ($self, $myconfig, $form) = @_; + + # connect to database + my $dbh = $form->dbconnect($myconfig); + + my $query = qq|SELECT id, printer_description, template_code, printer_command + FROM printers + ORDER BY 2|; + + $sth = $dbh->prepare($query); + $sth->execute || $form->dberror($query); + + while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { + push @{ $form->{ALL} }, $ref; + } + + $sth->finish; + $dbh->disconnect; + + $main::lxdebug->leave_sub(); +} + +sub get_printer { + $main::lxdebug->enter_sub(); + + my ($self, $myconfig, $form) = @_; + + # connect to database + my $dbh = $form->dbconnect($myconfig); + + my $query = + qq|SELECT p.printer_description, p.template_code, p.printer_command + FROM printers p + WHERE p.id = $form->{id}|; + my $sth = $dbh->prepare($query); + $sth->execute || $form->dberror($query); + + my $ref = $sth->fetchrow_hashref(NAME_lc); + + map { $form->{$_} = $ref->{$_} } keys %$ref; + + $sth->finish; + + $dbh->disconnect; + + $main::lxdebug->leave_sub(); +} + +sub save_printer { + $main::lxdebug->enter_sub(); + + my ($self, $myconfig, $form) = @_; + + # connect to database + my $dbh = $form->dbconnect($myconfig); + + $form->{printer_description} =~ s/\'/\'\'/g; + $form->{printer_command} =~ s/\'/\'\'/g; + $form->{template_code} =~ s/\'/\'\'/g; + + + # id is the old record + if ($form->{id}) { + $query = qq|UPDATE printers SET + printer_description = '$form->{printer_description}', + template_code = '$form->{template_code}', + printer_command = '$form->{printer_command}' + WHERE id = $form->{id}|; + } else { + $query = qq|INSERT INTO printers + (printer_description, template_code, printer_command) + VALUES ('$form->{printer_description}', '$form->{template_code}', '$form->{printer_command}')|; + } + $dbh->do($query) || $form->dberror($query); + + $dbh->disconnect; + + $main::lxdebug->leave_sub(); +} + +sub delete_printer { + $main::lxdebug->enter_sub(); + + my ($self, $myconfig, $form) = @_; + + # connect to database + my $dbh = $form->dbconnect($myconfig); + + $query = qq|DELETE FROM printers + WHERE id = $form->{id}|; + $dbh->do($query) || $form->dberror($query); + + $dbh->disconnect; + + $main::lxdebug->leave_sub(); +} + +sub adr { + $main::lxdebug->enter_sub(); + + my ($self, $myconfig, $form) = @_; + + # connect to database + my $dbh = $form->dbconnect($myconfig); + + my $query = qq|SELECT id, adr_description, adr_code + FROM adr + ORDER BY adr_code|; + + $sth = $dbh->prepare($query); + $sth->execute || $form->dberror($query); + + while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { + push @{ $form->{ALL} }, $ref; + } + + $sth->finish; + $dbh->disconnect; + + $main::lxdebug->leave_sub(); +} + +sub get_adr { + $main::lxdebug->enter_sub(); + + my ($self, $myconfig, $form) = @_; + + # connect to database + my $dbh = $form->dbconnect($myconfig); + + my $query = + qq|SELECT a.adr_description, a.adr_code + FROM adr a + WHERE a.id = $form->{id}|; + my $sth = $dbh->prepare($query); + $sth->execute || $form->dberror($query); + + my $ref = $sth->fetchrow_hashref(NAME_lc); + + map { $form->{$_} = $ref->{$_} } keys %$ref; + + $sth->finish; + + $dbh->disconnect; + + $main::lxdebug->leave_sub(); +} + +sub save_adr { + $main::lxdebug->enter_sub(); + + my ($self, $myconfig, $form) = @_; + + # connect to database + my $dbh = $form->dbconnect($myconfig); + + $form->{adr_description} =~ s/\'/\'\'/g; + $form->{adr_code} =~ s/\'/\'\'/g; + + + # id is the old record + if ($form->{id}) { + $query = qq|UPDATE adr SET + adr_description = '$form->{adr_description}', + adr_code = '$form->{adr_code}' + WHERE id = $form->{id}|; + } else { + $query = qq|INSERT INTO adr + (adr_description, adr_code) + VALUES ('$form->{adr_description}', '$form->{adr_code}')|; + } + $dbh->do($query) || $form->dberror($query); + + $dbh->disconnect; + + $main::lxdebug->leave_sub(); +} + +sub delete_adr { + $main::lxdebug->enter_sub(); + + my ($self, $myconfig, $form) = @_; + + # connect to database + my $dbh = $form->dbconnect($myconfig); + + $query = qq|DELETE FROM adr + WHERE id = $form->{id}|; + $dbh->do($query) || $form->dberror($query); + + $dbh->disconnect; + + $main::lxdebug->leave_sub(); +} + +sub payment { + $main::lxdebug->enter_sub(); + + my ($self, $myconfig, $form) = @_; + + # connect to database + my $dbh = $form->dbconnect($myconfig); + + my $query = qq|SELECT * + FROM payment_terms + ORDER BY id|; + + $sth = $dbh->prepare($query); + $sth->execute || $form->dberror($query); + + while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { + $ref->{percent_skonto} = $form->format_amount($myconfig,($ref->{percent_skonto} * 100)); + push @{ $form->{ALL} }, $ref; + } + + $sth->finish; + $dbh->disconnect; + + $main::lxdebug->leave_sub(); +} + +sub get_payment { + $main::lxdebug->enter_sub(); + + my ($self, $myconfig, $form) = @_; + + # connect to database + my $dbh = $form->dbconnect($myconfig); + + my $query = + qq|SELECT * + FROM payment_terms + WHERE id = $form->{id}|; + my $sth = $dbh->prepare($query); + $sth->execute || $form->dberror($query); + + my $ref = $sth->fetchrow_hashref(NAME_lc); + $ref->{percent_skonto} = $form->format_amount($myconfig,($ref->{percent_skonto} * 100)); + + map { $form->{$_} = $ref->{$_} } keys %$ref; + + $sth->finish; + + $dbh->disconnect; + + $main::lxdebug->leave_sub(); +} + +sub save_payment { + $main::lxdebug->enter_sub(); + + my ($self, $myconfig, $form) = @_; + + # connect to database + my $dbh = $form->dbconnect($myconfig); + + $form->{description} =~ s/\'/\'\'/g; + $form->{description_long} =~ s/\'/\'\'/g; + $percentskonto = $form->parse_amount($myconfig, $form->{percent_skonto}) /100; + $form->{ranking} *= 1; + $form->{terms_netto} *= 1; + $form->{terms_skonto} *= 1; + $form->{percent_skonto} *= 1; + + + + # id is the old record + if ($form->{id}) { + $query = qq|UPDATE payment_terms SET + description = '$form->{description}', + ranking = $form->{ranking}, + description_long = '$form->{description_long}', + terms_netto = $form->{terms_netto}, + terms_skonto = $form->{terms_skonto}, + percent_skonto = $percentskonto + WHERE id = $form->{id}|; + } else { + $query = qq|INSERT INTO payment_terms + (description, ranking, description_long, terms_netto, terms_skonto, percent_skonto) + VALUES ('$form->{description}', $form->{ranking}, '$form->{description_long}', $form->{terms_netto}, $form->{terms_skonto}, $percentskonto)|; + } + $dbh->do($query) || $form->dberror($query); + + $dbh->disconnect; + + $main::lxdebug->leave_sub(); +} + +sub delete_payment { + $main::lxdebug->enter_sub(); + + my ($self, $myconfig, $form) = @_; + + # connect to database + my $dbh = $form->dbconnect($myconfig); + + $query = qq|DELETE FROM payment_terms + WHERE id = $form->{id}|; + $dbh->do($query) || $form->dberror($query); + + $dbh->disconnect; + + $main::lxdebug->leave_sub(); +} + +sub sic { + $main::lxdebug->enter_sub(); + + my ($self, $myconfig, $form) = @_; + + # connect to database + my $dbh = $form->dbconnect($myconfig); + + my $query = qq|SELECT code, sictype, description + FROM sic + ORDER BY code|; + + $sth = $dbh->prepare($query); + $sth->execute || $form->dberror($query); + + while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { + push @{ $form->{ALL} }, $ref; + } + + $sth->finish; + $dbh->disconnect; + + $main::lxdebug->leave_sub(); +} + +sub get_sic { + $main::lxdebug->enter_sub(); + + my ($self, $myconfig, $form) = @_; + + # connect to database + my $dbh = $form->dbconnect($myconfig); + + my $query = qq|SELECT s.code, s.sictype, s.description + FROM sic s + WHERE s.code = '$form->{code}'|; + my $sth = $dbh->prepare($query); + $sth->execute || $form->dberror($query); + + my $ref = $sth->fetchrow_hashref(NAME_lc); + + map { $form->{$_} = $ref->{$_} } keys %$ref; + + $sth->finish; + + $dbh->disconnect; + + $main::lxdebug->leave_sub(); +} + +sub save_sic { + $main::lxdebug->enter_sub(); + + my ($self, $myconfig, $form) = @_; + + # connect to database + my $dbh = $form->dbconnect($myconfig); + + $form->{code} =~ s/\'/\'\'/g; + $form->{description} =~ s/\'/\'\'/g; + + # if there is an id + if ($form->{id}) { + $query = qq|UPDATE sic SET + code = '$form->{code}', + sictype = '$form->{sictype}', + description = '$form->{description}' + WHERE code = '$form->{id}'|; + } else { + $query = qq|INSERT INTO sic + (code, sictype, description) + VALUES ('$form->{code}', '$form->{sictype}', '$form->{description}')|; + } + $dbh->do($query) || $form->dberror($query); + + $dbh->disconnect; + + $main::lxdebug->leave_sub(); +} + +sub delete_sic { + $main::lxdebug->enter_sub(); + + my ($self, $myconfig, $form) = @_; + + # connect to database + my $dbh = $form->dbconnect($myconfig); + + $query = qq|DELETE FROM sic + WHERE code = '$form->{code}'|; + $dbh->do($query) || $form->dberror($query); + + $dbh->disconnect; + + $main::lxdebug->leave_sub(); +} + +sub load_template { + $main::lxdebug->enter_sub(); + + my ($self, $form) = @_; + open(TEMPLATE, "$form->{file}") or $form->error("$form->{file} : $!"); while (