Revision f36eb9fc
Von Moritz Bunkus vor mehr als 17 Jahren hinzugefügt
SL/AR.pm | ||
---|---|---|
36 | 36 |
|
37 | 37 |
use Data::Dumper; |
38 | 38 |
use SL::DBUtils; |
39 |
use SL::MoreCommon; |
|
39 | 40 |
|
40 | 41 |
sub post_transaction { |
41 | 42 |
$main::lxdebug->enter_sub(); |
42 | 43 |
|
43 |
my ($self, $myconfig, $form) = @_; |
|
44 |
my ($self, $myconfig, $form, $provided_dbh, $payments_only) = @_;
|
|
44 | 45 |
|
45 | 46 |
my ($query, $sth, $null, $taxrate, $amount, $tax); |
46 | 47 |
my $exchangerate = 0; |
... | ... | |
48 | 49 |
|
49 | 50 |
my @values; |
50 | 51 |
|
51 |
my $dbh = $form->dbconnect_noauto($myconfig); |
|
52 |
my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect_noauto($myconfig);
|
|
52 | 53 |
|
53 | 54 |
# set exchangerate |
54 | 55 |
$form->{exchangerate} = ($form->{currency} eq $form->{defaultcurrency}) ? 1 : |
... | ... | |
82 | 83 |
$query = qq|SELECT c.accno, t.taxkey, t.rate FROM tax t LEFT JOIN chart c ON (c.id = t.chart_id) WHERE t.id = ? ORDER BY c.accno|; |
83 | 84 |
($form->{AR_amounts}{"tax_$i"}, $form->{"taxkey_$i"}, $form->{"taxrate_$i"}) = selectrow_query($form, $dbh, $query, $form->{"tax_id_$i"}); |
84 | 85 |
|
85 |
$form->{AR_amounts}{"tax_$i"}{taxkey} = $form->{"taxkey_$i"}; |
|
86 |
$form->{AR_amounts}{"tax_$i"}{taxkey} = $form->{"taxkey_$i"};
|
|
86 | 87 |
$form->{AR_amounts}{"amounts_$i"}{taxkey} = $form->{"taxkey_$i"}; |
87 | 88 |
|
88 | 89 |
if ($form->{taxincluded} *= 1) { |
... | ... | |
118 | 119 |
$form->get_employee($dbh) unless $form->{employee_id}; |
119 | 120 |
|
120 | 121 |
# if we have an id delete old records else make one |
121 |
if ($form->{id}) { |
|
122 |
# delete detail records |
|
123 |
$query = qq|DELETE FROM acc_trans WHERE trans_id = ?|; |
|
124 |
do_query($form, $dbh, $query, $form->{id}); |
|
125 |
} else { |
|
126 |
$query = qq|SELECT nextval('glid')|; |
|
127 |
($form->{id}) = selectrow_query($form, $dbh, $query); |
|
128 |
$query = qq|INSERT INTO ar (id, invnumber, employee_id) VALUES (?, 'dummy', ?)|; |
|
129 |
do_query($form, $dbh, $query, $form->{id}, $form->{employee_id}); |
|
130 |
$form->{invnumber} = $form->update_defaults($myconfig, "invnumber", $dbh) unless $form->{invnumber}; |
|
122 |
if (!$payments_only) { |
|
123 |
if ($form->{id}) { |
|
124 |
# delete detail records |
|
125 |
$query = qq|DELETE FROM acc_trans WHERE trans_id = ?|; |
|
126 |
do_query($form, $dbh, $query, $form->{id}); |
|
127 |
|
|
128 |
} else { |
|
129 |
$query = qq|SELECT nextval('glid')|; |
|
130 |
($form->{id}) = selectrow_query($form, $dbh, $query); |
|
131 |
$query = qq|INSERT INTO ar (id, invnumber, employee_id) VALUES (?, 'dummy', ?)|; |
|
132 |
do_query($form, $dbh, $query, $form->{id}, $form->{employee_id}); |
|
133 |
$form->{invnumber} = $form->update_defaults($myconfig, "invnumber", $dbh) unless $form->{invnumber}; |
|
134 |
} |
|
131 | 135 |
} |
132 | 136 |
|
133 | 137 |
# update department |
... | ... | |
138 | 142 |
$form->{datepaid} ||= $form->{transdate} ; |
139 | 143 |
my $datepaid = ($form->{paid} != 0) ? $form->{datepaid} : undef; |
140 | 144 |
|
141 |
$query = |
|
142 |
qq|UPDATE ar set |
|
143 |
invnumber = ?, ordnumber = ?, transdate = ?, customer_id = ?, |
|
144 |
taxincluded = ?, amount = ?, duedate = ?, paid = ?, datepaid = ?, |
|
145 |
netamount = ?, curr = ?, notes = ?, department_id = ?, |
|
146 |
employee_id = ?, storno = ?, storno_id = ? |
|
147 |
WHERE id = ?|; |
|
148 |
my @values = ($form->{invnumber}, $form->{ordnumber}, conv_date($form->{transdate}), conv_i($form->{customer_id}), $form->{taxincluded} ? 't' : 'f', $form->{amount}, |
|
149 |
conv_date($form->{duedate}), $form->{paid}, conv_date($datepaid), $form->{netamount}, $form->{currency}, $form->{notes}, conv_i($form->{department_id}), |
|
150 |
conv_i($form->{employee_id}), $form->{storno} ? 't' : 'f', $form->{storno_id}, conv_i($form->{id})); |
|
151 |
do_query($form, $dbh, $query, @values); |
|
152 |
|
|
153 | 145 |
# amount for AR account |
154 | 146 |
$form->{receivables} = $form->round_amount($form->{amount}, 2) * -1; |
155 | 147 |
|
... | ... | |
157 | 149 |
$form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, $form->{exchangerate}, 0) |
158 | 150 |
if ($form->{currency} ne $form->{defaultcurrency}) && $form->check_exchangerate($myconfig, $form->{currency}, $form->{transdate}, 'buy'); |
159 | 151 |
|
152 |
if (!$payments_only) { |
|
153 |
$query = |
|
154 |
qq|UPDATE ar set |
|
155 |
invnumber = ?, ordnumber = ?, transdate = ?, customer_id = ?, |
|
156 |
taxincluded = ?, amount = ?, duedate = ?, paid = ?, datepaid = ?, |
|
157 |
netamount = ?, curr = ?, notes = ?, department_id = ?, |
|
158 |
employee_id = ?, storno = ?, storno_id = ? |
|
159 |
WHERE id = ?|; |
|
160 |
my @values = ($form->{invnumber}, $form->{ordnumber}, conv_date($form->{transdate}), conv_i($form->{customer_id}), $form->{taxincluded} ? 't' : 'f', $form->{amount}, |
|
161 |
conv_date($form->{duedate}), $form->{paid}, conv_date($datepaid), $form->{netamount}, $form->{currency}, $form->{notes}, conv_i($form->{department_id}), |
|
162 |
conv_i($form->{employee_id}), $form->{storno} ? 't' : 'f', $form->{storno_id}, conv_i($form->{id})); |
|
163 |
do_query($form, $dbh, $query, @values); |
|
164 |
|
|
165 |
# add individual transactions for AR, amount and taxes |
|
166 |
for $i (1 .. $form->{rowcount}) { |
|
167 |
if ($form->{"amount_$i"} != 0) { |
|
168 |
my $project_id = conv_i($form->{"project_id_$i"}); |
|
169 |
$taxkey = $form->{AR_amounts}{"amounts_$i"}{taxkey}; |
|
160 | 170 |
|
161 |
# add individual transactions for AR, amount and taxes |
|
162 |
for $i (1 .. $form->{rowcount}) { |
|
163 |
if ($form->{"amount_$i"} != 0) { |
|
164 |
my $project_id = conv_i($form->{"project_id_$i"}); |
|
165 |
$taxkey = $form->{AR_amounts}{"amounts_$i"}{taxkey}; |
|
166 |
|
|
167 |
# insert detail records in acc_trans |
|
168 |
$query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey) |
|
169 |
VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), ?, ?, ?, ?)|; |
|
170 |
@values = (conv_i($form->{id}), conv_i($form->{AR_amounts}{"amount_$i"}), conv_i($form->{"amount_$i"}), conv_date($form->{transdate}), $project_id, conv_i($taxkey)); |
|
171 |
do_query($form, $dbh, $query, @values); |
|
172 |
|
|
173 |
if ($form->{"tax_$i"} != 0) { |
|
174 | 171 |
# insert detail records in acc_trans |
175 | 172 |
$query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey) |
176 | 173 |
VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), ?, ?, ?, ?)|; |
177 |
@values = (conv_i($form->{id}), conv_i($form->{AR_amounts}{"tax_$i"}), conv_i($form->{"tax_$i"}), conv_date($form->{transdate}), $project_id, conv_i($taxkey));
|
|
174 |
@values = (conv_i($form->{id}), conv_i($form->{AR_amounts}{"amount_$i"}), conv_i($form->{"amount_$i"}), conv_date($form->{transdate}), $project_id, conv_i($taxkey));
|
|
178 | 175 |
do_query($form, $dbh, $query, @values); |
176 |
|
|
177 |
if ($form->{"tax_$i"} != 0) { |
|
178 |
# insert detail records in acc_trans |
|
179 |
$query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey) |
|
180 |
VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), ?, ?, ?, ?)|; |
|
181 |
@values = (conv_i($form->{id}), conv_i($form->{AR_amounts}{"tax_$i"}), conv_i($form->{"tax_$i"}), conv_date($form->{transdate}), $project_id, conv_i($taxkey)); |
|
182 |
do_query($form, $dbh, $query, @values); |
|
183 |
} |
|
179 | 184 |
} |
180 | 185 |
} |
181 |
} |
|
182 | 186 |
|
183 |
# add recievables |
|
184 |
$query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey) |
|
185 |
VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, (SELECT taxkey_id FROM chart WHERE accno = ?))|; |
|
186 |
@values = (conv_i($form->{id}), $form->{AR_amounts}{receivables}, conv_i($form->{receivables}), conv_date($form->{transdate}), $form->{AR_amounts}{receivables}); |
|
187 |
do_query($form, $dbh, $query, @values); |
|
187 |
# add recievables |
|
188 |
$query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey) |
|
189 |
VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, (SELECT taxkey_id FROM chart WHERE accno = ?))|; |
|
190 |
@values = (conv_i($form->{id}), $form->{AR_amounts}{receivables}, conv_i($form->{receivables}), conv_date($form->{transdate}), $form->{AR_amounts}{receivables}); |
|
191 |
do_query($form, $dbh, $query, @values); |
|
192 |
} |
|
188 | 193 |
|
189 | 194 |
# add paid transactions |
190 | 195 |
for my $i (1 .. $form->{paidaccounts}) { |
... | ... | |
207 | 212 |
# receivables amount |
208 | 213 |
$amount = $form->round_amount($form->{"paid_$i"} * $form->{exchangerate}, 2); |
209 | 214 |
|
210 |
if ($form->{receivables} != 0) {
|
|
215 |
if ($amount != 0) {
|
|
211 | 216 |
# add receivable |
212 | 217 |
$query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey) |
213 | 218 |
VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, (SELECT taxkey_id FROM chart WHERE accno = ?))|; |
214 | 219 |
@values = (conv_i($form->{id}), $form->{AR}{receivables}, $amount, conv_date($form->{"datepaid_$i"}), $project_id, $form->{AR}{receivables}); |
215 | 220 |
do_query($form, $dbh, $query, @values); |
216 | 221 |
} |
217 |
$form->{receivables} = $amount; |
|
218 | 222 |
|
219 | 223 |
if ($form->{"paid_$i"} != 0) { |
220 | 224 |
my $project_id = conv_i($form->{"paid_project_id_$i"}); |
... | ... | |
253 | 257 |
} |
254 | 258 |
} |
255 | 259 |
|
256 |
my $rc = $dbh->commit; |
|
257 |
$dbh->disconnect; |
|
260 |
my $rc = 1; |
|
261 |
if (!$provided_dbh) { |
|
262 |
$rc = $dbh->commit(); |
|
263 |
$dbh->disconnect(); |
|
264 |
} |
|
258 | 265 |
|
259 | 266 |
$main::lxdebug->leave_sub() and return $rc; |
260 | 267 |
} |
261 | 268 |
|
269 |
sub _delete_payments { |
|
270 |
$main::lxdebug->enter_sub(); |
|
271 |
|
|
272 |
my ($self, $form, $dbh) = @_; |
|
273 |
|
|
274 |
my @delete_oids; |
|
275 |
|
|
276 |
# Delete old payment entries from acc_trans. |
|
277 |
my $query = |
|
278 |
qq|SELECT oid |
|
279 |
FROM acc_trans |
|
280 |
WHERE (trans_id = ?) AND fx_transaction |
|
281 |
|
|
282 |
UNION |
|
283 |
|
|
284 |
SELECT at.oid |
|
285 |
FROM acc_trans at |
|
286 |
LEFT JOIN chart c ON (at.chart_id = c.id) |
|
287 |
WHERE (trans_id = ?) AND (c.link LIKE '%AR_paid%')|; |
|
288 |
push @delete_oids, selectall_array_query($form, $dbh, $query, conv_i($form->{id}), conv_i($form->{id})); |
|
289 |
|
|
290 |
$query = |
|
291 |
qq|SELECT at.oid |
|
292 |
FROM acc_trans at |
|
293 |
LEFT JOIN chart c ON (at.chart_id = c.id) |
|
294 |
WHERE (trans_id = ?) |
|
295 |
AND ((c.link = 'AR') OR (c.link LIKE '%:AR') OR (c.link LIKE 'AR:%')) |
|
296 |
ORDER BY at.oid |
|
297 |
OFFSET 1|; |
|
298 |
push @delete_oids, selectall_array_query($form, $dbh, $query, conv_i($form->{id})); |
|
299 |
|
|
300 |
if (@delete_oids) { |
|
301 |
$query = qq|DELETE FROM acc_trans WHERE oid IN (| . join(", ", @delete_oids) . qq|)|; |
|
302 |
do_query($form, $dbh, $query); |
|
303 |
} |
|
304 |
|
|
305 |
$main::lxdebug->leave_sub(); |
|
306 |
} |
|
307 |
|
|
262 | 308 |
sub post_payment { |
263 | 309 |
$main::lxdebug->enter_sub(); |
264 | 310 |
|
... | ... | |
267 | 313 |
# connect to database, turn off autocommit |
268 | 314 |
my $dbh = $form->dbconnect_noauto($myconfig); |
269 | 315 |
|
270 |
$form->{datepaid} = $form->{transdate};
|
|
316 |
my (%payments, $old_form, $row, $item, $query, %keep_vars);
|
|
271 | 317 |
|
272 |
# total payments, don't move we need it here |
|
273 |
for my $i (1 .. $form->{paidaccounts}) { |
|
274 |
if ($form->{type} eq "credit_note") { |
|
275 |
$form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"}) * -1; |
|
276 |
} else { |
|
277 |
$form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"}); |
|
278 |
} |
|
279 |
$form->{paid} += $form->{"paid_$i"}; |
|
280 |
$form->{datepaid} = $form->{"datepaid_$i"} if ($form->{"datepaid_$i"}); |
|
281 |
} |
|
318 |
$old_form = save_form(); |
|
282 | 319 |
|
283 |
$form->{exchangerate} = |
|
284 |
$form->get_exchangerate($dbh, $form->{currency}, $form->{transdate}, |
|
285 |
"buy"); |
|
320 |
# Delete all entries in acc_trans from prior payments. |
|
321 |
$self->_delete_payments($form, $dbh); |
|
286 | 322 |
|
287 |
my $accno_ar = $form->{ARselected}; |
|
323 |
# Save the new payments the user made before cleaning up $form. |
|
324 |
my $payments_re = '^datepaid_\d+$|^memo_\d+$|^source_\d+$|^exchangerate_\d+$|^paid_\d+$|^paid_project_id_\d+$|^AR_paid_\d+$|^paidaccounts$'; |
|
325 |
map { $payments{$_} = $form->{$_} } grep m/$payments_re/, keys %{ $form }; |
|
288 | 326 |
|
289 |
# record payments and offsetting AR |
|
290 |
for my $i (1 .. $form->{paidaccounts}) { |
|
327 |
# Clean up $form so that old content won't tamper the results. |
|
328 |
%keep_vars = map { $_, 1 } qw(login password id); |
|
329 |
map { delete $form->{$_} unless $keep_vars{$_} } keys %{ $form }; |
|
291 | 330 |
|
292 |
if ($form->{"paid_$i"} != 0) {
|
|
293 |
my $project_id = conv_i($form->{"paid_project_id_$i"});
|
|
331 |
# Retrieve the invoice from the database.
|
|
332 |
$form->create_links('AR', $myconfig, 'customer', $dbh);
|
|
294 | 333 |
|
295 |
my ($accno) = split /--/, $form->{"AR_paid_$i"}; |
|
296 |
$form->{"datepaid_$i"} = $form->{transdate} |
|
297 |
unless ($form->{"datepaid_$i"}); |
|
298 |
$form->{datepaid} = $form->{"datepaid_$i"}; |
|
334 |
# Restore the payment options from the user input. |
|
335 |
map { $form->{$_} = $payments{$_} } keys %payments; |
|
299 | 336 |
|
300 |
$exchangerate = 0; |
|
301 |
if (($form->{currency} eq $form->{defaultcurrency}) || ($form->{defaultcurrency} eq "")) { |
|
302 |
$form->{"exchangerate_$i"} = 1; |
|
303 |
} else { |
|
304 |
$exchangerate = |
|
305 |
$form->check_exchangerate($myconfig, $form->{currency}, |
|
306 |
$form->{"datepaid_$i"}, 'buy'); |
|
307 |
|
|
308 |
$form->{"exchangerate_$i"} = |
|
309 |
($exchangerate) |
|
310 |
? $exchangerate |
|
311 |
: $form->parse_amount($myconfig, $form->{"exchangerate_$i"}); |
|
312 |
} |
|
337 |
# Set up the content of $form in the way that AR::post_transaction() expects. |
|
313 | 338 |
|
314 |
# record AR |
|
315 |
$amount = |
|
316 |
$form->round_amount($form->{"paid_$i"} * $form->{"exchangerate"}, |
|
317 |
2); |
|
318 |
|
|
319 |
|
|
320 |
$query = |
|
321 |
qq|DELETE FROM acc_trans | . |
|
322 |
qq|WHERE trans_id = ? AND amount = ? AND transdate = ? AND | . |
|
323 |
qq| chart_id = (SELECT c.id FROM chart c WHERE c.accno = ?)|; |
|
324 |
@values = (conv_i($form->{id}), $amount, |
|
325 |
conv_date($form->{"datepaid_$i"}), $accno_ar); |
|
326 |
do_query($form, $dbh, $query, @values); |
|
327 |
|
|
328 |
$query = |
|
329 |
qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey) | . |
|
330 |
qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, | . |
|
331 |
qq| (SELECT taxkey_id FROM chart WHERE accno = ?))|; |
|
332 |
@values = (conv_i($form->{id}), $accno_ar, conv_i($amount), |
|
333 |
conv_date($form->{"datepaid_$i"}), $project_id, $accno_ar); |
|
334 |
do_query($form, $dbh, $query, @values); |
|
335 |
|
|
336 |
# record payment |
|
337 |
$form->{"paid_$i"} *= -1; |
|
338 |
|
|
339 |
$query = |
|
340 |
qq|DELETE FROM acc_trans | . |
|
341 |
qq|WHERE trans_id = ? AND | . |
|
342 |
qq| chart_id = (SELECT c.id FROM chart c WHERE c.accno = ?) AND | . |
|
343 |
qq| amount = ? AND transdate = ? AND source = ? AND memo = ?|; |
|
344 |
@values = (conv_i($form->{id}), $accno, conv_i($form->{"paid_$i"}), |
|
345 |
conv_date($form->{"datepaid_$i"}), |
|
346 |
$form->{"source_$i"}, $form->{"memo_$i"}); |
|
347 |
do_query($form, $dbh, $query, @values); |
|
348 |
|
|
349 |
$query = |
|
350 |
qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, source, memo, project_id, taxkey) | . |
|
351 |
qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?, ?, | . |
|
352 |
qq| (SELECT taxkey_id FROM chart WHERE accno = ?))|; |
|
353 |
@values = (conv_i($form->{id}), $accno, conv_i($form->{"paid_$i"}), |
|
354 |
conv_date($form->{"datepaid_$i"}), |
|
355 |
$form->{"source_$i"}, $form->{"memo_$i"}, $project_id, $accno); |
|
356 |
do_query($form, $dbh, $query, @values); |
|
357 |
|
|
358 |
# gain/loss |
|
359 |
$amount = |
|
360 |
$form->{"paid_$i"} * $form->{exchangerate} - $form->{"paid_$i"} * |
|
361 |
$form->{"exchangerate_$i"}; |
|
362 |
if ($amount > 0) { |
|
363 |
$form->{fx}{ $form->{fxgain_accno} }{ $form->{"datepaid_$i"} } += |
|
364 |
$amount; |
|
365 |
} else { |
|
366 |
$form->{fx}{ $form->{fxloss_accno} }{ $form->{"datepaid_$i"} } += |
|
367 |
$amount; |
|
368 |
} |
|
339 |
$self->setup_form($form); |
|
369 | 340 |
|
370 |
# update exchange rate |
|
371 |
if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) { |
|
372 |
$form->update_exchangerate($dbh, $form->{currency}, |
|
373 |
$form->{"datepaid_$i"}, |
|
374 |
$form->{"exchangerate_$i"}, 0); |
|
375 |
} |
|
376 |
} |
|
377 |
} |
|
341 |
($form->{defaultcurrency}) = selectrow_query($form, $dbh, qq|SELECT curr FROM defaults|); |
|
342 |
$form->{defaultcurrency} = (split m/:/, $form->{defaultcurrency})[0]; |
|
378 | 343 |
|
379 |
# record exchange rate differences and gains/losses |
|
380 |
foreach my $accno (keys %{ $form->{fx} }) { |
|
381 |
foreach my $transdate (keys %{ $form->{fx}{$accno} }) { |
|
382 |
if ( |
|
383 |
($form->{fx}{$accno}{$transdate} = |
|
384 |
$form->round_amount($form->{fx}{$accno}{$transdate}, 2) |
|
385 |
) != 0 |
|
386 |
) { |
|
387 |
$query = |
|
388 |
qq|DELETE FROM acc_trans | . |
|
389 |
qq|WHERE trans_id = ? AND | . |
|
390 |
qq| chart_id = (SELECT c.id FROM chart c WHERE c.accno = ?) AND | . |
|
391 |
qq| amount = ? AND transdate = ? AND cleared = 'f' AND fx_transaction = 't'|; |
|
392 |
@values = (conv_i($form->{id}), $accno, |
|
393 |
conv_i($form->{fx}{$accno}{$transdate}), |
|
394 |
conv_date($transdate)); |
|
395 |
do_query($form, $dbh, $query, @values); |
|
344 |
$form->{exchangerate} = $form->format_amount($myconfig, $form->{exchangerate}); |
|
396 | 345 |
|
397 |
$query = |
|
398 |
qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, cleared, fx_transaction, project_id, taxkey) | . |
|
399 |
qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, 'f', 't', ?, | . |
|
400 |
qq| (SELECT taxkey_id FROM chart WHERE accno = ?))|; |
|
401 |
@values = (conv_i($form->{id}), $accno, |
|
402 |
$form->{fx}{$accno}{$transdate}, |
|
403 |
conv_date($transdate), $project_id, |
|
404 |
$form->{fx}{$accno}{$transdate}); |
|
405 |
do_query($form, $dbh, $query, @values); |
|
406 |
} |
|
407 |
} |
|
408 |
} |
|
409 |
my $datepaid = ($form->{paid}) ? $form->{datepaid} : "NULL"; |
|
346 |
# Get the AR accno (which is normally done by Form::create_links()). |
|
347 |
$query = |
|
348 |
qq|SELECT c.accno |
|
349 |
FROM acc_trans at |
|
350 |
LEFT JOIN chart c ON (at.chart_id = c.id) |
|
351 |
WHERE (trans_id = ?) |
|
352 |
AND ((c.link = 'AR') OR (c.link LIKE '%:AR') OR (c.link LIKE 'AR:%')) |
|
353 |
ORDER BY at.oid |
|
354 |
LIMIT 1|; |
|
410 | 355 |
|
411 |
# save AR record |
|
412 |
my $query = |
|
413 |
qq|UPDATE ar set paid = ?, datepaid = ? WHERE id = ?|; |
|
414 |
@values = (conv_i($form->{paid}), conv_date($datepaid), conv_i($form->{id})); |
|
415 |
do_query($form, $dbh, $query, @values); |
|
356 |
($form->{ARselected}) = selectfirst_array_query($form, $dbh, $query, conv_i($form->{id})); |
|
416 | 357 |
|
417 |
my $rc = $dbh->commit; |
|
418 |
$dbh->disconnect; |
|
358 |
# Post the new payments. |
|
359 |
$self->post_transaction($myconfig, $form, $dbh, 1); |
|
360 |
|
|
361 |
restore_form($old_form); |
|
362 |
|
|
363 |
my $rc = $dbh->commit(); |
|
364 |
$dbh->disconnect(); |
|
419 | 365 |
|
420 | 366 |
$main::lxdebug->leave_sub(); |
421 | 367 |
|
... | ... | |
558 | 504 |
$main::lxdebug->leave_sub(); |
559 | 505 |
} |
560 | 506 |
|
507 |
sub setup_form { |
|
508 |
$main::lxdebug->enter_sub(); |
|
509 |
|
|
510 |
my ($self, $form) = @_; |
|
511 |
|
|
512 |
my ($exchangerate, $key, $akey, $i, $j, $k, $index, $taxamount, $totaltax, $taxrate, $diff); |
|
513 |
|
|
514 |
# forex |
|
515 |
$form->{forex} = $form->{exchangerate}; |
|
516 |
$exchangerate = $form->{exchangerate} ? $form->{exchangerate} : 1; |
|
517 |
|
|
518 |
foreach $key (keys %{ $form->{AR_links} }) { |
|
519 |
# if there is a value we have an old entry |
|
520 |
$j = 0; |
|
521 |
$k = 0; |
|
522 |
|
|
523 |
for $i (1 .. scalar @{ $form->{acc_trans}{$key} }) { |
|
524 |
if ($key eq "AR_paid") { |
|
525 |
$j++; |
|
526 |
$form->{"AR_paid_$j"} = $form->{acc_trans}{$key}->[$i-1]->{accno}; |
|
527 |
|
|
528 |
# reverse paid |
|
529 |
$form->{"paid_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1; |
|
530 |
$form->{"datepaid_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{transdate}; |
|
531 |
$form->{"source_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{source}; |
|
532 |
$form->{"memo_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{memo}; |
|
533 |
$form->{"forex_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{exchangerate}; |
|
534 |
$form->{"exchangerate_$i"} = $form->{"forex_$j"}; |
|
535 |
$form->{"paid_project_id_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{project_id}; |
|
536 |
$form->{paidaccounts}++; |
|
537 |
|
|
538 |
} else { |
|
539 |
|
|
540 |
$akey = $key; |
|
541 |
$akey =~ s/AR_//; |
|
542 |
|
|
543 |
if ($key eq "AR_tax" || $key eq "AP_tax") { |
|
544 |
$form->{"${key}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}"; |
|
545 |
$form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} = $form->round_amount($form->{acc_trans}{$key}->[$i - 1]->{amount} / $exchangerate, 2); |
|
546 |
|
|
547 |
if ($form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"} > 0) { |
|
548 |
$totaltax += $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"}; |
|
549 |
$taxrate += $form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"}; |
|
550 |
|
|
551 |
} else { |
|
552 |
$totalwithholding += $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"}; |
|
553 |
$withholdingrate += $form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"}; |
|
554 |
} |
|
555 |
|
|
556 |
$index = $form->{acc_trans}{$key}->[$i - 1]->{index}; |
|
557 |
$form->{"tax_$index"} = $form->{acc_trans}{$key}->[$i - 1]->{amount}; |
|
558 |
$totaltax += $form->{"tax_$index"}; |
|
559 |
|
|
560 |
} else { |
|
561 |
$k++; |
|
562 |
$form->{"${akey}_$k"} = $form->round_amount($form->{acc_trans}{$key}->[$i - 1]->{amount} / $exchangerate, 2); |
|
563 |
|
|
564 |
if ($akey eq 'amount') { |
|
565 |
$form->{rowcount}++; |
|
566 |
$totalamount += $form->{"${akey}_$i"}; |
|
567 |
|
|
568 |
$form->{"oldprojectnumber_$k"} = $form->{acc_trans}{$key}->[$i-1]->{projectnumber}; |
|
569 |
$form->{"projectnumber_$k"} = $form->{acc_trans}{$key}->[$i-1]->{projectnumber}; |
|
570 |
$form->{taxrate} = $form->{acc_trans}{$key}->[$i - 1]->{rate}; |
|
571 |
$form->{"project_id_$k"} = $form->{acc_trans}{$key}->[$i-1]->{project_id}; |
|
572 |
} |
|
573 |
|
|
574 |
$form->{"${key}_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}"; |
|
575 |
|
|
576 |
if ($akey eq "AR") { |
|
577 |
$form->{ARselected} = $form->{acc_trans}{$key}->[$i-1]->{accno}; |
|
578 |
|
|
579 |
} elsif ($akey eq "amount") { |
|
580 |
$form->{"${key}_$k"} = $form->{acc_trans}{$key}->[$i-1]->{accno} . "--" . $form->{acc_trans}{$key}->[$i-1]->{id}; |
|
581 |
$form->{"taxchart_$k"} = $form->{acc_trans}{$key}->[$i-1]->{id} . "--" . $form->{acc_trans}{$key}->[$i-1]->{rate}; |
|
582 |
} |
|
583 |
} |
|
584 |
} |
|
585 |
} |
|
586 |
} |
|
587 |
|
|
588 |
$form->{taxincluded} = $taxincluded if ($form->{id}); |
|
589 |
$form->{paidaccounts} = 1 if not defined $form->{paidaccounts}; |
|
590 |
|
|
591 |
if ($form->{taxincluded} && $form->{taxrate} && $totalamount) { |
|
592 |
|
|
593 |
# add tax to amounts and invtotal |
|
594 |
for $i (1 .. $form->{rowcount}) { |
|
595 |
$taxamount = ($totaltax + $totalwithholding) * $form->{"amount_$i"} / $totalamount; |
|
596 |
$tax = $form->round_amount($taxamount, 2); |
|
597 |
$diff += ($taxamount - $tax); |
|
598 |
$form->{"amount_$i"} += $form->{"tax_$i"}; |
|
599 |
} |
|
600 |
$form->{amount_1} += $form->round_amount($diff, 2); |
|
601 |
} |
|
602 |
|
|
603 |
$taxamount = $form->round_amount($taxamount, 2); |
|
604 |
$form->{tax} = $taxamount; |
|
605 |
|
|
606 |
$form->{invtotal} = $totalamount + $totaltax; |
|
607 |
} |
|
608 |
|
|
561 | 609 |
1; |
562 | 610 |
|
Auch abrufbar als: Unified diff
Die Funktion "Zahlung buchen" bei Debitorenrechnungen komplett umgeschrieben. Sie verlässt sich nun nicht mehr auf die aktuellen Daten in $form, um die alten Einträge in acc_trans zu löschen, sondern lädt den vorherigen Stand aus der Datenbank, entfernt darauf basierend die Einträge in acc_trans und lässt AR->post_transaction() selber die Zahlungen eintragen.