Revision a05eead3
Von Sven Schöling vor etwa 17 Jahren hinzugefügt
SL/IR.pm | ||
---|---|---|
38 | 38 |
use SL::Common; |
39 | 39 |
use SL::DBUtils; |
40 | 40 |
use SL::MoreCommon; |
41 |
use List::Util qw(min); |
|
41 | 42 |
|
42 | 43 |
sub post_invoice { |
43 | 44 |
$main::lxdebug->enter_sub(); |
... | ... | |
59 | 60 |
if (!$payments_only) { |
60 | 61 |
if ($form->{id}) { |
61 | 62 |
&reverse_invoice($dbh, $form); |
62 |
|
|
63 | 63 |
} else { |
64 | 64 |
($form->{id}) = selectrow_query($form, $dbh, qq|SELECT nextval('glid')|); |
65 |
|
|
66 | 65 |
do_query($form, $dbh, qq|INSERT INTO ap (id, invnumber) VALUES (?, '')|, $form->{id}); |
67 | 66 |
} |
68 | 67 |
} |
... | ... | |
73 | 72 |
if ($form->{currency} eq $defaultcurrency) { |
74 | 73 |
$form->{exchangerate} = 1; |
75 | 74 |
} else { |
76 |
$exchangerate = |
|
77 |
$form->check_exchangerate($myconfig, $form->{currency}, |
|
78 |
$form->{transdate}, 'sell'); |
|
75 |
$exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{transdate}, 'sell'); |
|
79 | 76 |
} |
80 | 77 |
|
81 |
$form->{exchangerate} = |
|
82 |
($exchangerate) |
|
83 |
? $exchangerate |
|
84 |
: $form->parse_amount($myconfig, $form->{exchangerate}); |
|
85 |
|
|
78 |
$form->{exchangerate} = $exchangerate || $form->parse_amount($myconfig, $form->{exchangerate}); |
|
86 | 79 |
$form->{exchangerate} = 1 unless ($form->{exchangerate} * 1); |
87 | 80 |
|
88 | 81 |
my %item_units; |
... | ... | |
92 | 85 |
for my $i (1 .. $form->{rowcount}) { |
93 | 86 |
next unless $form->{"id_$i"}; |
94 | 87 |
|
95 |
$form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"}); |
|
88 |
$form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"}); |
|
89 |
$form->{"qty_$i"} *= -1 if $form->{storno}; |
|
96 | 90 |
|
97 |
if ($form->{storno}) { |
|
98 |
$form->{"qty_$i"} *= -1; |
|
99 |
} |
|
100 |
|
|
101 |
if ($main::eur) { |
|
102 |
$form->{"inventory_accno_$i"} = $form->{"expense_accno_$i"}; |
|
103 |
} |
|
91 |
$form->{"inventory_accno_$i"} = $form->{"expense_accno_$i"} if $main::eur; |
|
104 | 92 |
|
105 | 93 |
# get item baseunit |
106 | 94 |
if (!$item_units{$form->{"id_$i"}}) { |
... | ... | |
111 | 99 |
my $item_unit = $item_units{$form->{"id_$i"}}; |
112 | 100 |
|
113 | 101 |
if (defined($all_units->{$item_unit}->{factor}) |
114 |
&& ($all_units->{$item_unit}->{factor} ne '') |
|
115 |
&& ($all_units->{$item_unit}->{factor} * 1 != 0)) { |
|
102 |
&& ($all_units->{$item_unit}->{factor} ne '')
|
|
103 |
&& ($all_units->{$item_unit}->{factor} * 1 != 0)) {
|
|
116 | 104 |
$basefactor = $all_units->{$form->{"unit_$i"}}->{factor} / $all_units->{$item_unit}->{factor}; |
117 | 105 |
} else { |
118 | 106 |
$basefactor = 1; |
... | ... | |
125 | 113 |
$taxrate = 0; |
126 | 114 |
|
127 | 115 |
$form->{"sellprice_$i"} = $form->parse_amount($myconfig, $form->{"sellprice_$i"}); |
128 |
my $fxsellprice = $form->{"sellprice_$i"}; |
|
129 |
|
|
130 |
my ($dec) = ($fxsellprice =~ /\.(\d+)/); |
|
131 |
$dec = length $dec; |
|
116 |
(my $fxsellprice = $form->{"sellprice_$i"}) =~ /\.(\d+)/; |
|
117 |
my $dec = length $1; |
|
132 | 118 |
my $decimalplaces = ($dec > 2) ? $dec : 2; |
133 | 119 |
|
134 | 120 |
map { $taxrate += $form->{"${_}_rate"} } @taxaccounts; |
... | ... | |
175 | 161 |
$form->{amount}{ $form->{id} }{ $form->{"inventory_accno_$i"} } -= $linetotal; |
176 | 162 |
|
177 | 163 |
# adjust and round sellprice |
178 |
$form->{"sellprice_$i"} = |
|
179 |
$form->round_amount($form->{"sellprice_$i"} * $form->{exchangerate}, $decimalplaces); |
|
164 |
$form->{"sellprice_$i"} = $form->round_amount($form->{"sellprice_$i"} * $form->{exchangerate}, $decimalplaces); |
|
180 | 165 |
|
181 | 166 |
$lastinventoryaccno = $form->{"inventory_accno_$i"}; |
182 | 167 |
|
... | ... | |
187 | 172 |
@values = ($form->{"sellprice_$i"}, conv_i($form->{"id_$i"})); |
188 | 173 |
do_query($form, $dbh, $query, @values); |
189 | 174 |
|
190 |
if (!$form->{shipped}) { |
|
191 |
$form->update_balance($dbh, "parts", "onhand", qq|id = ?|, $baseqty, $form->{"id_$i"}) |
|
192 |
} |
|
175 |
$form->update_balance($dbh, "parts", "onhand", qq|id = ?|, $baseqty, $form->{"id_$i"}) if !$form->{shipped}; |
|
193 | 176 |
|
194 | 177 |
# check if we sold the item already and |
195 | 178 |
# make an entry for the expense and inventory |
... | ... | |
207 | 190 |
my $totalqty = $base_qty; |
208 | 191 |
|
209 | 192 |
while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { |
210 |
|
|
211 |
my $qty = $ref->{base_qty} + $ref->{allocated}; |
|
212 |
|
|
213 |
if (($qty - $totalqty) > 0) { |
|
214 |
$qty = $totalqty; |
|
215 |
} |
|
216 |
|
|
193 |
my $qty = min $totalqty, ($ref->{base_qty} + $ref->{allocated}); |
|
217 | 194 |
$linetotal = $form->round_amount(($form->{"sellprice_$i"} * $qty) / $basefactor, 2); |
218 | 195 |
|
219 | 196 |
if ($ref->{allocated} < 0) { |
... | ... | |
233 | 210 |
|
234 | 211 |
} elsif ($linetotal != 0) { |
235 | 212 |
# add entry for inventory, this one is for the sold item |
236 |
$query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey) |
|
237 |
VALUES (?, ?, ?, ?, (SELECT taxkey_id FROM chart WHERE id = ?))|; |
|
238 |
@values = ($ref->{trans_id}, $ref->{inventory_accno_id}, $linetotal, |
|
239 |
$ref->{transdate}, $ref->{inventory_accno_id}); |
|
213 |
$query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey) VALUES (?, ?, ?, ?, (SELECT taxkey_id FROM chart WHERE id = ?))|; |
|
214 |
@values = ($ref->{trans_id}, $ref->{inventory_accno_id}, $linetotal, $ref->{transdate}, $ref->{inventory_accno_id}); |
|
240 | 215 |
do_query($form, $dbh, $query, @values); |
241 | 216 |
|
242 | 217 |
# add expense |
243 |
$query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey) |
|
244 |
VALUES (?, ?, ?, ?, (SELECT taxkey from tax WHERE chart_id = ?))|; |
|
245 |
@values = ($ref->{trans_id}, $ref->{expense_accno_id}, ($linetotal * -1), |
|
246 |
$ref->{transdate}, $ref->{expense_accno_id}); |
|
218 |
$query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey) VALUES (?, ?, ?, ?, (SELECT taxkey from tax WHERE chart_id = ?))|; |
|
219 |
@values = ($ref->{trans_id}, $ref->{expense_accno_id}, ($linetotal * -1), $ref->{transdate}, $ref->{expense_accno_id}); |
|
247 | 220 |
do_query($form, $dbh, $query, @values); |
248 | 221 |
} |
249 | 222 |
|
... | ... | |
252 | 225 |
|
253 | 226 |
$allocated += $qty; |
254 | 227 |
|
255 |
last if (($totalqty -= $qty) <= 0);
|
|
228 |
last if ($totalqty -= $qty) <= 0;
|
|
256 | 229 |
} |
257 | 230 |
|
258 | 231 |
$sth->finish(); |
... | ... | |
274 | 247 |
if ($form->round_amount($taxrate, 7) == 0) { |
275 | 248 |
if ($form->{taxincluded}) { |
276 | 249 |
foreach $item (@taxaccounts) { |
277 |
$taxamount = |
|
278 |
$linetotal * $form->{"${item}_rate"} |
|
279 |
/ (1 + abs($form->{"${item}_rate"})); |
|
250 |
$taxamount = $linetotal * $form->{"${item}_rate"} / (1 + abs($form->{"${item}_rate"})); |
|
280 | 251 |
$totaltax += $taxamount; |
281 | 252 |
$form->{amount}{ $form->{id} }{$item} -= $taxamount; |
282 | 253 |
} |
283 |
|
|
284 | 254 |
} else { |
285 | 255 |
map { $form->{amount}{ $form->{id} }{$_} -= $linetotal * $form->{"${_}_rate"} } @taxaccounts; |
286 | 256 |
} |
287 |
|
|
288 | 257 |
} else { |
289 | 258 |
map { $form->{amount}{ $form->{id} }{$_} -= $taxamount * $form->{"${_}_rate"} / $taxrate } @taxaccounts; |
290 | 259 |
} |
... | ... | |
341 | 310 |
for my $i (1 .. $form->{paidaccounts}) { |
342 | 311 |
$form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"}); |
343 | 312 |
$form->{paid} += $form->{"paid_$i"}; |
344 |
$form->{datepaid} = $form->{"datepaid_$i"} if ($form->{"datepaid_$i"});
|
|
313 |
$form->{datepaid} = $form->{"datepaid_$i"} if $form->{"datepaid_$i"};
|
|
345 | 314 |
} |
346 | 315 |
|
347 | 316 |
my ($tax, $paiddiff) = (0, 0); |
... | ... | |
368 | 337 |
$expensediff += $paiddiff; |
369 | 338 |
|
370 | 339 |
######## this only applies to tax included |
371 |
if ($lastinventoryaccno) { |
|
372 |
$form->{amount}{ $form->{id} }{$lastinventoryaccno} -= $invoicediff; |
|
373 |
} |
|
374 |
if ($lastexpenseaccno) { |
|
375 |
$form->{amount}{ $form->{id} }{$lastexpenseaccno} -= $expensediff; |
|
376 |
} |
|
340 |
|
|
341 |
$form->{amount}{ $form->{id} }{$lastinventoryaccno} -= $invoicediff if $lastinventoryaccno; |
|
342 |
$form->{amount}{ $form->{id} }{$lastexpenseaccno} -= $expensediff if $lastexpenseaccno; |
|
377 | 343 |
|
378 | 344 |
} else { |
379 | 345 |
$amount = $form->round_amount($netamount * $form->{exchangerate}, 2); |
... | ... | |
392 | 358 |
|
393 | 359 |
$form->{amount}{ $form->{id} }{ $form->{AP} } = $netamount + $tax; |
394 | 360 |
|
395 |
if ($form->{paid} != 0) { |
|
396 |
$form->{paid} = $form->round_amount($form->{paid} * $form->{exchangerate} + $paiddiff, 2); |
|
397 |
} |
|
361 |
|
|
362 |
$form->{paid} = $form->round_amount($form->{paid} * $form->{exchangerate} + $paiddiff, 2) if $form->{paid} != 0; |
|
398 | 363 |
|
399 | 364 |
# update exchangerate |
400 |
if (($form->{currency} ne $defaultcurrency) && !$exchangerate) { |
|
401 |
$form->update_exchangerate($dbh, $form->{currency}, $form->{invdate}, 0, $form->{exchangerate});
|
|
402 |
}
|
|
365 |
|
|
366 |
$form->update_exchangerate($dbh, $form->{currency}, $form->{invdate}, 0, $form->{exchangerate})
|
|
367 |
if ($form->{currency} ne $defaultcurrency) && !$exchangerate;
|
|
403 | 368 |
|
404 | 369 |
# record acc_trans transactions |
405 | 370 |
foreach my $trans_id (keys %{ $form->{amount} }) { |
406 | 371 |
foreach my $accno (keys %{ $form->{amount}{$trans_id} }) { |
407 | 372 |
$form->{amount}{$trans_id}{$accno} = $form->round_amount($form->{amount}{$trans_id}{$accno}, 2); |
408 | 373 |
|
409 |
next if ($payments_only || !$form->{amount}{$trans_id}{$accno});
|
|
374 |
next if $payments_only || !$form->{amount}{$trans_id}{$accno};
|
|
410 | 375 |
|
411 | 376 |
$query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, project_id) |
412 | 377 |
VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, |
... | ... | |
426 | 391 |
} |
427 | 392 |
|
428 | 393 |
# force AP entry if 0 |
429 |
if ($form->{amount}{ $form->{id} }{ $form->{AP} } == 0) { |
|
430 |
$form->{amount}{ $form->{id} }{ $form->{AP} } = $form->{paid}; |
|
431 |
} |
|
394 |
|
|
395 |
$form->{amount}{ $form->{id} }{ $form->{AP} } = $form->{paid} if $form->{amount}{$form->{id}}{$form->{AP}} == 0; |
|
432 | 396 |
|
433 | 397 |
# record payments and offsetting AP |
434 | 398 |
for my $i (1 .. $form->{paidaccounts}) { |
435 |
next if ($form->{"paid_$i"} == 0);
|
|
399 |
next if $form->{"paid_$i"} == 0;
|
|
436 | 400 |
|
437 | 401 |
my ($accno) = split /--/, $form->{"AP_paid_$i"}; |
438 | 402 |
$form->{"datepaid_$i"} = $form->{invdate} unless ($form->{"datepaid_$i"}); |
... | ... | |
463 | 427 |
if ($form->{currency} eq $defaultcurrency) { |
464 | 428 |
$form->{"exchangerate_$i"} = 1; |
465 | 429 |
} else { |
466 |
$exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell'); |
|
467 |
|
|
468 |
$form->{"exchangerate_$i"} = |
|
469 |
($exchangerate) |
|
470 |
? $exchangerate |
|
471 |
: $form->parse_amount($myconfig, $form->{"exchangerate_$i"}); |
|
430 |
$exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell'); |
|
431 |
$form->{"exchangerate_$i"} = $exchangerate || $form->parse_amount($myconfig, $form->{"exchangerate_$i"}); |
|
472 | 432 |
} |
473 | 433 |
|
474 | 434 |
# exchangerate difference |
475 |
$form->{fx}{$accno}{ $form->{"datepaid_$i"} } += |
|
476 |
$form->{"paid_$i"} * ($form->{"exchangerate_$i"} - 1) + $paiddiff; |
|
435 |
$form->{fx}{$accno}{ $form->{"datepaid_$i"} } += $form->{"paid_$i"} * ($form->{"exchangerate_$i"} - 1) + $paiddiff; |
|
477 | 436 |
|
478 | 437 |
# gain/loss |
479 | 438 |
$amount = |
... | ... | |
481 | 440 |
($form->{"paid_$i"} * $form->{"exchangerate_$i"}); |
482 | 441 |
if ($amount > 0) { |
483 | 442 |
$form->{fx}{ $form->{fxgain_accno} }{ $form->{"datepaid_$i"} } += $amount; |
484 |
|
|
485 | 443 |
} else { |
486 | 444 |
$form->{fx}{ $form->{fxloss_accno} }{ $form->{"datepaid_$i"} } += $amount; |
487 | 445 |
} |
... | ... | |
489 | 447 |
$paiddiff = 0; |
490 | 448 |
|
491 | 449 |
# update exchange rate |
492 |
if (($form->{currency} ne $defaultcurrency) && !$exchangerate) { |
|
493 |
$form->update_exchangerate($dbh, $form->{currency}, |
|
494 |
$form->{"datepaid_$i"}, |
|
495 |
0, $form->{"exchangerate_$i"}); |
|
496 |
} |
|
450 |
$form->update_exchangerate($dbh, $form->{currency}, $form->{"datepaid_$i"}, 0, $form->{"exchangerate_$i"}) |
|
451 |
if ($form->{currency} ne $defaultcurrency) && !$exchangerate; |
|
497 | 452 |
} |
498 | 453 |
|
499 | 454 |
# record exchange rate differences and gains/losses |
... | ... | |
529 | 484 |
$form->{department_id} = (split /--/, $form->{department})[1]; |
530 | 485 |
$form->{invnumber} = $form->{id} unless $form->{invnumber}; |
531 | 486 |
|
532 |
$taxzone_id = 0 if ((3 < $taxzone_id) || (0 > $taxzone_id));
|
|
487 |
$taxzone_id = 0 if (3 < $taxzone_id) || (0 > $taxzone_id);
|
|
533 | 488 |
|
534 | 489 |
# save AP record |
535 | 490 |
$query = qq|UPDATE ap SET |
536 |
invnumber = ?, |
|
537 |
ordnumber = ?, |
|
538 |
quonumber = ?, |
|
539 |
transdate = ?, |
|
540 |
orddate = ?, |
|
541 |
quodate = ?, |
|
542 |
vendor_id = ?, |
|
543 |
amount = ?, |
|
544 |
netamount = ?, |
|
545 |
paid = ?, |
|
546 |
datepaid = ?, |
|
547 |
duedate = ?, |
|
548 |
invoice = '1', |
|
549 |
taxzone_id = ?, |
|
550 |
taxincluded = ?, |
|
551 |
notes = ?, |
|
552 |
intnotes = ?, |
|
553 |
curr = ?, |
|
554 |
department_id = ?, |
|
555 |
storno = ?, |
|
556 |
storno_id = ?, |
|
557 |
globalproject_id = ?, |
|
558 |
cp_id = ?, |
|
559 |
employee_id = ? |
|
491 |
invnumber = ?, ordnumber = ?, quonumber = ?, transdate = ?, |
|
492 |
orddate = ?, quodate = ?, vendor_id = ?, amount = ?, |
|
493 |
netamount = ?, paid = ?, duedate = ?, datepaid = ?, |
|
494 |
invoice = ?, taxzone_id = ?, notes = ?, taxincluded = ?, |
|
495 |
intnotes = ?, curr = ?, storno_id = ?, storno = ?, |
|
496 |
cp_id = ?, employee_id = ?, department_id = ?, |
|
497 |
globalproject_id = ? |
|
560 | 498 |
WHERE id = ?|; |
561 |
@values = ($form->{invnumber}, $form->{ordnumber}, $form->{quonumber}, |
|
562 |
conv_date($form->{invdate}), conv_date($form->{orddate}), conv_date($form->{quodate}), |
|
563 |
conv_i($form->{vendor_id}), $amount, $netamount, $form->{paid}, |
|
564 |
$form->{paid} ? conv_date($form->{datepaid}) : undef, |
|
565 |
conv_date($form->{duedate}), $taxzone_id, |
|
566 |
$form->{taxincluded} ? 't' : 'f', |
|
567 |
$form->{notes}, $form->{intnotes}, $form->{currency}, conv_i($form->{department_id}), |
|
568 |
$form->{storno} ? 't' : 'f', conv_i($form->{storno_id}), |
|
569 |
conv_i($form->{globalproject_id}), conv_i($form->{cp_id}), |
|
570 |
conv_i($form->{employee_id}), |
|
571 |
conv_i($form->{id})); |
|
499 |
@values = ( |
|
500 |
$form->{invnumber}, $form->{ordnumber}, $form->{quonumber}, conv_date($form->{invdate}), |
|
501 |
conv_date($form->{orddate}), conv_date($form->{quodate}), conv_i($form->{vendor_id}), $amount, |
|
502 |
$netamount, $form->{paid}, conv_date($form->{duedate}), $form->{paid} ? conv_date($form->{datepaid}) : undef, |
|
503 |
'1', $taxzone_id, $form->{notes}, $form->{taxincluded} ? 't' : 'f', |
|
504 |
$form->{intnotes}, $form->{currency}, conv_i($form->{storno_id}), $form->{storno} ? 't' : 'f', |
|
505 |
conv_i($form->{cp_id}), conv_i($form->{employee_id}), conv_i($form->{department_id}), |
|
506 |
conv_i($form->{globalproject_id}), |
|
507 |
conv_i($form->{id}) |
|
508 |
); |
|
572 | 509 |
do_query($form, $dbh, $query, @values); |
573 | 510 |
|
574 | 511 |
if ($form->{storno}) { |
Auch abrufbar als: Unified diff
Kosmetik, merge aus -r5105,5106,5118,5120,5124