Revision e5e96630
Von Kivitendo Admin vor fast 9 Jahren hinzugefügt
SL/AM.pm | ||
---|---|---|
53 | 53 |
sub get_account { |
54 | 54 |
$main::lxdebug->enter_sub(); |
55 | 55 |
|
56 |
my ($self, $myconfig, $form) = @_; |
|
57 |
|
|
58 |
|
|
59 |
my $chart_obj = SL::DB::Manager::Chart->find_by(id => $form->{id}) || die "Can't open chart"; |
|
60 |
|
|
61 |
# connect to database |
|
62 |
my $dbh = $form->dbconnect($myconfig); |
|
63 |
my $query = qq{ |
|
64 |
SELECT c.accno, c.description, c.charttype, c.category, |
|
65 |
c.link, c.pos_bilanz, c.pos_eur, c.pos_er, c.new_chart_id, c.valid_from, |
|
66 |
c.pos_bwa, datevautomatik, |
|
67 |
tk.taxkey_id, tk.pos_ustva, tk.tax_id, |
|
68 |
tk.tax_id || '--' || tk.taxkey_id AS tax, tk.startdate |
|
69 |
FROM chart c |
|
70 |
LEFT JOIN taxkeys tk |
|
71 |
ON (c.id=tk.chart_id AND tk.id = |
|
72 |
(SELECT id FROM taxkeys |
|
73 |
WHERE taxkeys.chart_id = c.id AND startdate <= current_date |
|
74 |
ORDER BY startdate DESC LIMIT 1)) |
|
75 |
WHERE c.id = ? |
|
76 |
}; |
|
77 |
|
|
78 |
|
|
79 |
$main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query"); |
|
80 |
my $sth = $dbh->prepare($query); |
|
81 |
$sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})"); |
|
82 |
|
|
83 |
my $ref = $sth->fetchrow_hashref("NAME_lc"); |
|
56 |
# fetch chart-related data and set form fields |
|
57 |
# get_account is called by add_account in am.pl |
|
58 |
# always sets $form->{TAXKEY} and default_accounts |
|
59 |
# loads chart data when $form->{id} is passed |
|
84 | 60 |
|
85 |
foreach my $key (keys %$ref) { |
|
86 |
$form->{"$key"} = $ref->{"$key"}; |
|
87 |
} |
|
88 |
|
|
89 |
$sth->finish; |
|
61 |
my ($self, $myconfig, $form) = @_; |
|
90 | 62 |
|
91 | 63 |
# get default accounts |
92 |
$query = qq|SELECT inventory_accno_id, income_accno_id, expense_accno_id |
|
93 |
FROM defaults|; |
|
94 |
$main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query"); |
|
95 |
$sth = $dbh->prepare($query); |
|
96 |
$sth->execute || $form->dberror($query); |
|
97 |
|
|
98 |
$ref = $sth->fetchrow_hashref("NAME_lc"); |
|
99 |
|
|
100 |
map { $form->{$_} = $ref->{$_} } keys %{ $ref }; |
|
101 |
|
|
102 |
$sth->finish; |
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
# get taxkeys and description |
|
107 |
$query = qq{ |
|
108 |
SELECT |
|
109 |
id, |
|
110 |
(SELECT accno FROM chart WHERE id=tax.chart_id) AS chart_accno, |
|
111 |
taxkey, |
|
112 |
id||'--'||taxkey AS tax, |
|
113 |
taxdescription, |
|
114 |
rate |
|
115 |
FROM tax ORDER BY taxkey |
|
116 |
}; |
|
117 |
$main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query"); |
|
118 |
$sth = $dbh->prepare($query); |
|
119 |
$sth->execute || $form->dberror($query); |
|
64 |
map { $form->{$_} = $::instance_conf->{$_} } qw(inventory_accno_id income_accno_id expense_accno_id); |
|
120 | 65 |
|
66 |
require SL::DB::Tax; |
|
67 |
my $taxes = SL::DB::Manager::Tax->get_all( with_objects => ['chart'] , sort_by => 'taxkey' ); |
|
121 | 68 |
$form->{TAXKEY} = []; |
69 |
foreach my $tk ( @{$taxes} ) { |
|
70 |
push @{ $form->{TAXKEY} }, { id => $tk->id, |
|
71 |
chart_accno => $tk->chart_id ? $tk->chart->accno : undef, |
|
72 |
taxkey => $tk->taxkey, |
|
73 |
tax => $tk->id . '--' . $tk->taxkey, |
|
74 |
rate => $tk->rate |
|
75 |
}; |
|
76 |
}; |
|
122 | 77 |
|
123 |
while (my $ref = $sth->fetchrow_hashref("NAME_lc")) { |
|
124 |
push @{ $form->{TAXKEY} }, $ref; |
|
125 |
} |
|
126 |
|
|
127 |
$sth->finish; |
|
128 | 78 |
if ($form->{id}) { |
129 |
# get new accounts |
|
130 |
$query = qq|SELECT id, accno,description |
|
131 |
FROM chart |
|
132 |
WHERE link = ? |
|
133 |
ORDER BY accno|; |
|
134 |
$main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query"); |
|
135 |
$sth = $dbh->prepare($query); |
|
136 |
$sth->execute($form->{link}) || $form->dberror($query . " ($form->{link})"); |
|
137 | 79 |
|
138 |
$form->{NEWACCOUNT} = []; |
|
139 |
while (my $ref = $sth->fetchrow_hashref("NAME_lc")) { |
|
140 |
push @{ $form->{NEWACCOUNT} }, $ref; |
|
80 |
my $chart_obj = SL::DB::Manager::Chart->find_by(id => $form->{id}) || die "Can't open chart"; |
|
81 |
|
|
82 |
my @chart_fields = qw(accno description charttype category link pos_bilanz |
|
83 |
pos_eur pos_er new_chart_id valid_from pos_bwa datevautomatik); |
|
84 |
foreach my $cf ( @chart_fields ) { |
|
85 |
$form->{"$cf"} = $chart_obj->$cf; |
|
141 | 86 |
} |
142 | 87 |
|
143 |
$sth->finish; |
|
88 |
my $active_taxkey = $chart_obj->get_active_taxkey; |
|
89 |
$form->{$_} = $active_taxkey->$_ foreach qw(taxkey_id pos_ustva tax_id startdate); |
|
90 |
$form->{tax} = $active_taxkey->tax_id . '--' . $active_taxkey->taxkey_id; |
|
144 | 91 |
|
145 |
# get the taxkeys of account |
|
146 |
|
|
147 |
$query = qq{ |
|
148 |
SELECT |
|
149 |
tk.id, |
|
150 |
tk.chart_id, |
|
151 |
c.accno, |
|
152 |
tk.tax_id, |
|
153 |
t.taxdescription, |
|
154 |
t.rate, |
|
155 |
tk.taxkey_id, |
|
156 |
tk.pos_ustva, |
|
157 |
tk.startdate |
|
158 |
FROM taxkeys tk |
|
159 |
LEFT JOIN tax t ON (t.id = tk.tax_id) |
|
160 |
LEFT JOIN chart c ON (c.id = t.chart_id) |
|
161 |
|
|
162 |
WHERE tk.chart_id = ? |
|
163 |
ORDER BY startdate DESC |
|
164 |
}; |
|
165 |
$main::lxdebug->message(LXDebug->QUERY(), "\$query=\n $query"); |
|
166 |
$sth = $dbh->prepare($query); |
|
92 |
# check if there are any transactions for this chart |
|
93 |
$form->{orphaned} = $chart_obj->has_transaction ? 0 : 1; |
|
167 | 94 |
|
168 |
$sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})"); |
|
95 |
# check if new account is active |
|
96 |
# The old sql query was broken since at least 2006 and always returned 0 |
|
97 |
$form->{new_chart_valid} = $chart_obj->new_chart_valid; |
|
169 | 98 |
|
99 |
# get the taxkeys of the account |
|
170 | 100 |
$form->{ACCOUNT_TAXKEYS} = []; |
171 |
|
|
172 |
while (my $ref = $sth->fetchrow_hashref("NAME_lc")) { |
|
173 |
push @{ $form->{ACCOUNT_TAXKEYS} }, $ref; |
|
101 |
foreach my $taxkey ( @{ $chart_obj->taxkeys } ) { |
|
102 |
push @{ $form->{ACCOUNT_TAXKEYS} }, { id => $taxkey->id, |
|
103 |
chart_id => $taxkey->chart_id, |
|
104 |
tax_id => $taxkey->tax_id, |
|
105 |
taxkey_id => $taxkey->taxkey_id, |
|
106 |
pos_ustva => $taxkey->pos_ustva, |
|
107 |
startdate => $taxkey->startdate->to_kivitendo, |
|
108 |
taxdescription => $taxkey->tax->taxdescription, |
|
109 |
rate => $taxkey->tax->rate, |
|
110 |
accno => $chart_obj->accno, |
|
111 |
}; |
|
174 | 112 |
} |
175 | 113 |
|
176 |
$sth->finish; |
|
114 |
# get new accounts (Folgekonto). Find all charts with the same link |
|
115 |
$form->{NEWACCOUNT} = $chart_obj->db->dbh->selectall_arrayref('select id, accno,description from chart where link = ? order by accno', {Slice => {}}, $chart_obj->link); |
|
177 | 116 |
|
178 |
} |
|
179 |
|
|
180 |
# check if there any transactions for this chart |
|
181 |
$form->{orphaned} = $chart_obj->has_transaction ? 0 : 1; |
|
182 |
|
|
183 |
# check if new account is active |
|
184 |
# The old sql query was broken since at least 2006 and always returned 0 |
|
185 |
$form->{new_chart_valid} = $chart_obj->new_chart_valid; |
|
186 |
|
|
187 |
$dbh->disconnect; |
|
117 |
} else { # set to orphaned for new charts, so chart_type can be changed (needed by $AccountIsPosted) |
|
118 |
$form->{orphaned} = 1; |
|
119 |
}; |
|
188 | 120 |
|
189 | 121 |
$main::lxdebug->leave_sub(); |
190 | 122 |
} |
Auch abrufbar als: Unified diff
Konten neu anlegen repariert
get_account wurde nicht nur für das Laden bestehender Konten verwendet,
sondern auch für das Füllen von anderen form-Variablen, z.B. für
Steuerschlüssel.
Bei der Gelegenheit auch etwas Rose geübt und refactored.