Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 1118dab9

Von Moritz Bunkus vor mehr als 17 Jahren hinzugefügt

  • ID 1118dab96d2c42e872c9782b4374709d52b20653
  • Vorgänger b61818c3
  • Nachfolger 820f3066

Die Funktion "Zahlung buchen" bei Kreditorenrechnungen 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 AP->post_transaction() selber die Zahlungen eintragen.
Zusätzlich den Button "Storno" in der Buttonleiste weiter nach rechts verschoben, sodass wie überall der "Erneuern"-Button der Standardbutton ist.

Unterschiede anzeigen:

SL/AP.pm
35 35
package AP;
36 36

  
37 37
use SL::DBUtils;
38
use SL::MoreCommon;
38 39

  
39 40
sub post_transaction {
40 41
  $main::lxdebug->enter_sub();
41 42

  
42
  my ($self, $myconfig, $form) = @_;
43
  my ($self, $myconfig, $form, $provided_dbh, $payments_only) = @_;
43 44

  
44 45
  # connect to database
45
  my $dbh = $form->dbconnect_noauto($myconfig);
46
  my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect_noauto($myconfig);
46 47

  
47 48
  my ($null, $taxrate, $amount);
48 49
  my $exchangerate = 0;
......
157 158
  # amount for total AP
158 159
  $form->{payables} = $form->{invtotal};
159 160

  
160
  my ($query, $sth);
161

  
162
  # if we have an id delete old records
163
  if ($form->{id}) {
164

  
165
    # delete detail records
166
    $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
167
    do_query($form, $dbh, $query, $form->{id});
168

  
169
  } else {
170
    my $uid = rand() . time;
171

  
172
    $uid .= $form->{login};
173

  
174
    $uid = substr($uid, 2, 75);
175

  
176
    $query =
177
      qq|INSERT INTO ap (invnumber, employee_id) | .
178
      qq|VALUES (?, (SELECT e.id FROM employee e WHERE e.login = ?))|;
179
    do_query($form, $dbh, $query, $uid, $form->{login});
180

  
181
    $query = qq|SELECT a.id FROM ap a
182
                WHERE a.invnumber = ?|;
183
    ($form->{id}) = selectrow_query($form, $dbh, $query, $uid);
184
  }
185

  
186
  $form->{invnumber} = $form->{id} unless $form->{invnumber};
187

  
188 161
  $form->{datepaid} = $form->{transdate} unless ($form->{datepaid});
189 162
  my $datepaid = ($form->{invpaid} != 0) ? $form->{datepaid} : undef;
190 163

  
191
  $query = qq|UPDATE ap SET
192
              invnumber = ?, transdate = ?, ordnumber = ?, vendor_id = ?, taxincluded = ?,
193
              amount = ?, duedate = ?, paid = ?, datepaid = ?, netamount = ?,
194
              curr = ?, notes = ?, department_id = ?, storno = ?, storno_id = ?
195
             WHERE id = ?|;
196
  my @values = ($form->{invnumber}, conv_date($form->{transdate}),
197
                $form->{ordnumber}, conv_i($form->{vendor_id}),
198
                $form->{taxincluded} ? 't' : 'f', $form->{invtotal},
199
                conv_date($form->{duedate}), $form->{invpaid},
200
                conv_date($datepaid), $form->{netamount},
201
                $form->{currency}, $form->{notes},
202
                conv_i($form->{department_id}), $form->{storno},
203
                $form->{storno_id}, $form->{id});
204
  do_query($form, $dbh, $query, @values);
205

  
206 164
  # update exchangerate
207 165
  if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
208 166
    $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, 0,
209 167
                               $form->{exchangerate});
210 168
  }
211 169

  
212
  # add individual transactions
213
  for $i (1 .. $form->{rowcount}) {
214
    if ($form->{"amount_$i"} != 0) {
215
      my $project_id;
216
      $project_id = conv_i($form->{"project_id_$i"});
217
      $taxkey = $form->{AP_amounts}{"amount_$i"}{taxkey};
170
  my ($query, $sth);
171

  
172
  if (!$payments_only) {
173
    # if we have an id delete old records
174
    if ($form->{id}) {
175

  
176
      # delete detail records
177
      $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
178
      do_query($form, $dbh, $query, $form->{id});
179

  
180
    } else {
181
      my $uid = rand() . time;
182

  
183
      $uid .= $form->{login};
184

  
185
      $uid = substr($uid, 2, 75);
218 186

  
219
      # insert detail records in acc_trans
220 187
      $query =
221
        qq|INSERT INTO acc_trans | .
222
        qq|  (trans_id, chart_id, amount, transdate, project_id, taxkey)| .
223
        qq|VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), | .
224
        qq|  ?, ?, ?, ?)|;
225
      @values = ($form->{id}, $form->{AP_amounts}{"amount_$i"},
226
                 $form->{"amount_$i"}, conv_date($form->{transdate}),
227
                 $project_id, $taxkey);
228
      do_query($form, $dbh, $query, @values);
188
        qq|INSERT INTO ap (invnumber, employee_id) | .
189
        qq|VALUES (?, (SELECT e.id FROM employee e WHERE e.login = ?))|;
190
      do_query($form, $dbh, $query, $uid, $form->{login});
191

  
192
      $query = qq|SELECT a.id FROM ap a
193
                  WHERE a.invnumber = ?|;
194
      ($form->{id}) = selectrow_query($form, $dbh, $query, $uid);
195
    }
196

  
197
    $form->{invnumber} = $form->{id} unless $form->{invnumber};
198

  
199
    $query = qq|UPDATE ap SET
200
                invnumber = ?, transdate = ?, ordnumber = ?, vendor_id = ?, taxincluded = ?,
201
                amount = ?, duedate = ?, paid = ?, datepaid = ?, netamount = ?,
202
                curr = ?, notes = ?, department_id = ?, storno = ?, storno_id = ?
203
               WHERE id = ?|;
204
    my @values = ($form->{invnumber}, conv_date($form->{transdate}),
205
                  $form->{ordnumber}, conv_i($form->{vendor_id}),
206
                  $form->{taxincluded} ? 't' : 'f', $form->{invtotal},
207
                  conv_date($form->{duedate}), $form->{invpaid},
208
                  conv_date($datepaid), $form->{netamount},
209
                  $form->{currency}, $form->{notes},
210
                  conv_i($form->{department_id}), $form->{storno},
211
                  $form->{storno_id}, $form->{id});
212
    do_query($form, $dbh, $query, @values);
213

  
214
    # add individual transactions
215
    for $i (1 .. $form->{rowcount}) {
216
      if ($form->{"amount_$i"} != 0) {
217
        my $project_id;
218
        $project_id = conv_i($form->{"project_id_$i"});
219
        $taxkey = $form->{AP_amounts}{"amount_$i"}{taxkey};
229 220

  
230
      if ($form->{"tax_$i"} != 0) {
231 221
        # insert detail records in acc_trans
232 222
        $query =
233
          qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, | .
234
          qq|  project_id, taxkey) | .
223
          qq|INSERT INTO acc_trans | .
224
          qq|  (trans_id, chart_id, amount, transdate, project_id, taxkey)| .
235 225
          qq|VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), | .
236 226
          qq|  ?, ?, ?, ?)|;
237
        @values = ($form->{id}, $form->{AP_amounts}{"tax_$i"},
238
                   $form->{"tax_$i"}, conv_date($form->{transdate}),
227
        @values = ($form->{id}, $form->{AP_amounts}{"amount_$i"},
228
                   $form->{"amount_$i"}, conv_date($form->{transdate}),
239 229
                   $project_id, $taxkey);
240 230
        do_query($form, $dbh, $query, @values);
241
      }
242 231

  
232
        if ($form->{"tax_$i"} != 0) {
233
          # insert detail records in acc_trans
234
          $query =
235
            qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, | .
236
            qq|  project_id, taxkey) | .
237
            qq|VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), | .
238
            qq|  ?, ?, ?, ?)|;
239
          @values = ($form->{id}, $form->{AP_amounts}{"tax_$i"},
240
                     $form->{"tax_$i"}, conv_date($form->{transdate}),
241
                     $project_id, $taxkey);
242
          do_query($form, $dbh, $query, @values);
243
        }
244

  
245
      }
243 246
    }
244
  }
245 247

  
246
  # add payables
247
  $query =
248
    qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey) | .
249
    qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, | .
250
    qq|        (SELECT taxkey_id FROM chart WHERE accno = ?))|;
251
  @values = ($form->{id}, $form->{AP_amounts}{payables}, $form->{payables},
252
             conv_date($form->{transdate}), $form->{AP_amounts}{payables});
253
  do_query($form, $dbh, $query, @values);
248
    # add payables
249
    $query =
250
      qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey) | .
251
      qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, | .
252
      qq|        (SELECT taxkey_id FROM chart WHERE accno = ?))|;
253
    @values = ($form->{id}, $form->{AP_amounts}{payables}, $form->{payables},
254
               conv_date($form->{transdate}), $form->{AP_amounts}{payables});
255
    do_query($form, $dbh, $query, @values);
256
  }
254 257

  
255 258
  # if there is no amount but a payment record a payable
256 259
  if ($form->{amount} == 0 && $form->{invtotal} == 0) {
......
356 359
    }
357 360
  }
358 361

  
359
  my $rc = $dbh->commit;
360
  $dbh->disconnect;
362
  if ($payments_only) {
363
    $query = qq|UPDATE ap SET paid = ? WHERE id = ?|;
364
    do_query($form, $dbh, $query, $form->{invpaid}, conv_i($form->{id}));
365
  }
366

  
367
  my $rc = 1;
368
  if (!$provided_dbh) {
369
    $dbh->commit();
370
    $dbh->disconnect();
371
  }
361 372

  
362 373
  $main::lxdebug->leave_sub();
363 374

  
......
406 417
    qq|LEFT JOIN employee e ON (a.employee_id = e.id) | .
407 418
    qq|LEFT JOIN project pr ON (a.globalproject_id = pr.id) |;
408 419

  
409
  my $where = qq| WHERE storno != true |;
420
  my $where = qq| WHERE COALESCE(storno, false) != true |;
410 421
  my @values;
411 422

  
412 423
  if ($form->{vendor_id}) {
......
508 519
  $main::lxdebug->leave_sub();
509 520
}
510 521

  
522
sub _delete_payments {
523
  $main::lxdebug->enter_sub();
524

  
525
  my ($self, $form, $dbh) = @_;
526

  
527
  my @delete_oids;
528

  
529
  # Delete old payment entries from acc_trans.
530
  my $query =
531
    qq|SELECT oid
532
       FROM acc_trans
533
       WHERE (trans_id = ?) AND fx_transaction
534

  
535
       UNION
536

  
537
       SELECT at.oid
538
       FROM acc_trans at
539
       LEFT JOIN chart c ON (at.chart_id = c.id)
540
       WHERE (trans_id = ?) AND (c.link LIKE '%AP_paid%')|;
541
  push @delete_oids, selectall_array_query($form, $dbh, $query, conv_i($form->{id}), conv_i($form->{id}));
542

  
543
  $query =
544
    qq|SELECT at.oid
545
       FROM acc_trans at
546
       LEFT JOIN chart c ON (at.chart_id = c.id)
547
       WHERE (trans_id = ?)
548
         AND ((c.link = 'AP') OR (c.link LIKE '%:AP') OR (c.link LIKE 'AP:%'))
549
       ORDER BY at.oid
550
       OFFSET 1|;
551
  push @delete_oids, selectall_array_query($form, $dbh, $query, conv_i($form->{id}));
552

  
553
  if (@delete_oids) {
554
    $query = qq|DELETE FROM acc_trans WHERE oid IN (| . join(", ", @delete_oids) . qq|)|;
555
    do_query($form, $dbh, $query);
556
  }
557

  
558
  $main::lxdebug->leave_sub();
559
}
511 560

  
512 561
sub post_payment {
513 562
  $main::lxdebug->enter_sub();
......
517 566
  # connect to database, turn off autocommit
518 567
  my $dbh = $form->dbconnect_noauto($myconfig);
519 568

  
520
  $form->{datepaid} = $form->{transdate};
569
  my (%payments, $old_form, $row, $item, $query, %keep_vars);
521 570

  
522
  # total payments, don't move we need it here
523
  for my $i (1 .. $form->{paidaccounts}) {
524
    $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"});
525
    $form->{paid} += $form->{"paid_$i"};
526
    $form->{datepaid} = $form->{"datepaid_$i"} if ($form->{"datepaid_$i"});
527
  }
571
  $old_form = save_form();
528 572

  
529
  $form->{exchangerate} =
530
      $form->get_exchangerate($dbh, $form->{currency}, $form->{transdate},
531
                              "buy");
573
  # Delete all entries in acc_trans from prior payments.
574
  $self->_delete_payments($form, $dbh);
532 575

  
533
  my (@values, $query);
576
  # Save the new payments the user made before cleaning up $form.
577
  my $payments_re = '^datepaid_\d+$|^memo_\d+$|^source_\d+$|^exchangerate_\d+$|^paid_\d+$|^paid_project_id_\d+$|^AP_paid_\d+$|^paidaccounts$';
578
  map { $payments{$_} = $form->{$_} } grep m/$payments_re/, keys %{ $form };
534 579

  
535
  my ($accno_ap) = split(/--/, $form->{APselected});
580
  # Clean up $form so that old content won't tamper the results.
581
  %keep_vars = map { $_, 1 } qw(login password id);
582
  map { delete $form->{$_} unless $keep_vars{$_} } keys %{ $form };
536 583

  
537
  # record payments and offsetting AP
538
  for my $i (1 .. $form->{paidaccounts}) {
584
  # Retrieve the invoice from the database.
585
  $form->create_links('AP', $myconfig, 'vendor', $dbh);
539 586

  
540
    if ($form->{"paid_$i"} != 0) {
541
      my ($accno) = split /--/, $form->{"AP_paid_$i"};
542
      $form->{"datepaid_$i"} = $form->{transdate}
543
        unless ($form->{"datepaid_$i"});
544
      $form->{datepaid} = $form->{"datepaid_$i"};
587
  # Restore the payment options from the user input.
588
  map { $form->{$_} = $payments{$_} } keys %payments;
545 589

  
546
      $exchangerate = 0;
547
      if (($form->{currency} eq $form->{defaultcurrency}) || ($form->{defaultcurrency} eq "")) {
548
        $form->{"exchangerate_$i"} = 1;
549
      } else {
550
        $exchangerate =
551
          $form->check_exchangerate($myconfig, $form->{currency},
552
                                    $form->{"datepaid_$i"}, 'buy');
590
  # Set up the content of $form in the way that AR::post_transaction() expects.
553 591

  
554
        $form->{"exchangerate_$i"} =
555
          ($exchangerate)
556
          ? $exchangerate
557
          : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
558
      }
592
  $self->setup_form($form);
559 593

  
560
      # record AP
561
      $amount =
562
        $form->round_amount($form->{"paid_$i"} * $form->{"exchangerate"},
563
                            2) * -1;
594
  ($form->{defaultcurrency}) = selectrow_query($form, $dbh, qq|SELECT curr FROM defaults|);
595
  $form->{defaultcurrency}   = (split m/:/, $form->{defaultcurrency})[0];
564 596

  
565
      $query =
566
        qq|DELETE FROM acc_trans | .
567
        qq|WHERE trans_id = ? | .
568
        qq|  AND chart_id = (SELECT c.id FROM chart c WHERE c.accno = ?) | .
569
        qq|  AND amount = ? AND transdate = ?|;
570
      @values = ($form->{id}, $accno_ap, $amount,
571
                 conv_date($form->{"datepaid_$i"}));
572
      do_query($form, $dbh, $query, @values);
597
  $form->{exchangerate}      = $form->format_amount($myconfig, $form->{exchangerate});
573 598

  
574
      $query =
575
        qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey) | .
576
        qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, | .
577
        qq|        (SELECT taxkey_id FROM chart WHERE accno = ?))|;
578
      @values = ($form->{id}, $accno_ap, $amount,
579
                 conv_date($form->{"datepaid_$i"}),
580
                 conv_i($form->{"paid_project_id_$i"}), $accno_ap);
581
      do_query($form, $dbh, $query, @values);
599
  # Get the AP accno.
600
  $query =
601
    qq|SELECT c.accno
602
       FROM acc_trans at
603
       LEFT JOIN chart c ON (at.chart_id = c.id)
604
       WHERE (trans_id = ?)
605
         AND ((c.link = 'AP') OR (c.link LIKE '%:AP') OR (c.link LIKE 'AP:%'))
606
       ORDER BY at.oid
607
       LIMIT 1|;
582 608

  
583
      $query =
584
        qq|DELETE FROM acc_trans | .
585
        qq|WHERE trans_id = ? | .
586
        qq|  AND chart_id = (SELECT c.id FROM chart c WHERE c.accno = ?) | .
587
        qq|  AND amount = ? AND transdate = ? AND source = ? AND memo = ?|;
588
      @values = ($form->{id}, $accno, $form->{"paid_$i"},
589
                 conv_date($form->{"datepaid_$i"}), $form->{"source_$i"},
590
                 $form->{"memo_$i"});
591
      do_query($form, $dbh, $query, @values);
609
  ($form->{APselected}) = selectfirst_array_query($form, $dbh, $query, conv_i($form->{id}));
592 610

  
593
      $query =
594
        qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, source, memo, project_id, taxkey) | .
595
        qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?, ?, | .
596
        qq|        (SELECT taxkey_id FROM chart WHERE accno = ?))|;
597
      @values = ($form->{id}, $accno, $form->{"paid_$i"},
598
                 $form->{"datepaid_$i"},
599
                 $form->{"source_$i"}, $form->{"memo_$i"},
600
                 conv_i($form->{"paid_project_id_$i"}), $accno);
601
      do_query($form, $dbh, $query, @values);
611
  # Post the new payments.
612
  $self->post_transaction($myconfig, $form, $dbh, 1);
602 613

  
603
      # gain/loss
604
      $amount =
605
        $form->{"paid_$i"} * $form->{exchangerate} - $form->{"paid_$i"} *
606
        $form->{"exchangerate_$i"};
607
      if ($amount > 0) {
608
        $form->{fx}{ $form->{fxgain_accno} }{ $form->{"datepaid_$i"} } +=
609
          $amount;
614
  restore_form($old_form);
615

  
616
  my $rc = $dbh->commit();
617
  $dbh->disconnect();
618

  
619
  $main::lxdebug->leave_sub();
620

  
621
  return $rc;
622
}
623

  
624
sub setup_form {
625
  $main::lxdebug->enter_sub();
626

  
627
  my ($self, $form) = @_;
628

  
629
  my ($exchangerate, $i, $j, $k, $key, $akey, $ref, $index, $taxamount, $totalamount);
630

  
631
  # forex
632
  $form->{forex} = $form->{exchangerate};
633
  $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
634

  
635
  foreach $key (keys %{ $form->{AP_links} }) {
636
    foreach $ref (@{ $form->{AP_links}{$key} }) {
637
      if ($key eq "AP_paid") {
638
        $form->{"select$key"} .= "<option value=\"$ref->{accno}\">$ref->{accno}--$ref->{description}</option>\n";
610 639
      } else {
611
        $form->{fx}{ $form->{fxloss_accno} }{ $form->{"datepaid_$i"} } +=
612
          $amount;
640
        $form->{"select$key"} .= "<option value=\"$ref->{accno}--$ref->{tax_id}\">$ref->{accno}--$ref->{description}</option>\n";
613 641
      }
642
    }
614 643

  
615
      $diff = 0;
644
    $form->{$key} = $form->{"select$key"};
616 645

  
617
      # update exchange rate
618
      if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
619
        $form->update_exchangerate($dbh, $form->{currency},
620
                                   $form->{"datepaid_$i"},
621
                                   $form->{"exchangerate_$i"}, 0);
646
    # if there is a value we have an old entry
647
    $j = 0;
648
    $k = 0;
649

  
650
    for $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
651

  
652
      if ($key eq "AP_paid") {
653
        $j++;
654
        $form->{"AP_paid_$j"}         = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
655
        $form->{"paid_$j"}            = $form->{acc_trans}{$key}->[$i - 1]->{amount};
656
        $form->{"datepaid_$j"}        = $form->{acc_trans}{$key}->[$i - 1]->{transdate};
657
        $form->{"source_$j"}          = $form->{acc_trans}{$key}->[$i - 1]->{source};
658
        $form->{"memo_$j"}            = $form->{acc_trans}{$key}->[$i - 1]->{memo};
659

  
660
        $form->{"exchangerate_$i"}    = $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
661
        $form->{"forex_$j"}           = $form->{"exchangerate_$i"};
662
        $form->{"AP_paid_$j"}         = $form->{acc_trans}{$key}->[$i-1]->{accno};
663
        $form->{"paid_project_id_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{project_id};
664
        $form->{paidaccounts}++;
665

  
666
      } else {
667
        $akey = $key;
668
        $akey =~ s/AP_//;
669

  
670
        if (($key eq "AP_tax") || ($key eq "AR_tax")) {
671
          $form->{"${key}_$form->{acc_trans}{$key}->[$i-1]->{accno}"}  = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
672
          $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} = $form->round_amount($form->{acc_trans}{$key}->[$i - 1]->{amount} / $exchangerate, 2);
673

  
674
          if ($form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"} > 0) {
675
            $totaltax += $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"};
676
          } else {
677
            $totalwithholding += $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"};
678
            $withholdingrate  += $form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"};
679
          }
680

  
681
          $index                 = $form->{acc_trans}{$key}->[$i - 1]->{index};
682
          $form->{"tax_$index"}  = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1;
683
          $totaltax             += $form->{"tax_$index"};
684

  
685
        } else {
686
          $k++;
687
          $form->{"${akey}_$k"} = $form->round_amount($form->{acc_trans}{$key}->[$i - 1]->{amount} / $exchangerate, 2);
688

  
689
          if ($akey eq 'amount') {
690
            $form->{rowcount}++;
691
            $form->{"${akey}_$i"} *= -1;
692
            $totalamount          += $form->{"${akey}_$i"};
693
            $form->{taxrate}       = $form->{acc_trans}{$key}->[$i - 1]->{rate};
694

  
695
            $form->{"projectnumber_$k"}    = "$form->{acc_trans}{$key}->[$i-1]->{projectnumber}";
696
            $form->{"oldprojectnumber_$k"} = $form->{"projectnumber_$k"};
697
            $form->{"project_id_$k"}       = "$form->{acc_trans}{$key}->[$i-1]->{project_id}";
698
          }
699

  
700
          $form->{"${key}_$k"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
701

  
702
          my $q_description    = quotemeta($form->{acc_trans}{$key}->[$i-1]->{description});
703
          $form->{"select${key}"} =~
704
            m/<option value=\"
705
                ($form->{acc_trans}{$key}->[$i-1]->{accno}--[^\"]*)
706
              \">
707
              $form->{acc_trans}{$key}->[$i-1]->{accno}
708
              --
709
              ${q_description}
710
              <\/option>\n/x;
711
          $form->{"${key}_$k"} = $1;
712

  
713
          if ($akey eq "AP") {
714
            $form->{APselected} = $form->{acc_trans}{$key}->[$i-1]->{accno};
715

  
716
          } elsif ($akey eq 'amount') {
717
            $form->{"${key}_$k"}   = $form->{acc_trans}{$key}->[$i-1]->{accno} . "--" . $form->{acc_trans}{$key}->[$i-1]->{id};
718
            $form->{"taxchart_$k"} = $form->{acc_trans}{$key}->[$i-1]->{id}    . "--" . $form->{acc_trans}{$key}->[$i-1]->{rate};
719
          }
720
        }
622 721
      }
623 722
    }
624 723
  }
625 724

  
626
  # record exchange rate differences and gains/losses
627
  foreach my $accno (keys %{ $form->{fx} }) {
628
    foreach my $transdate (keys %{ $form->{fx}{$accno} }) {
629
      if (
630
          ($form->{fx}{$accno}{$transdate} =
631
           $form->round_amount($form->{fx}{$accno}{$transdate}, 2)
632
          ) != 0
633
        ) {
634
        $query =
635
          qq|DELETE FROM acc_trans | .
636
          qq|WHERE trans_id = ? AND chart_id = | .
637
          qq|  (SELECT c.id FROM chart c WHERE c.accno = ?) AND amount = ? | .
638
          qq|  AND transdate = ? AND cleared = 'f' AND fx_transaction = 't'|;
639
        @values = ($form->{id}, $accno, $form->{fx}{$accno}{$transdate},
640
                   conv_date($transdate),);
641
        do_query($form, $dbh, $query, @values);
725
  $form->{taxincluded}  = $taxincluded if ($form->{id});
726
  $form->{paidaccounts} = 1            if not defined $form->{paidaccounts};
642 727

  
643
        $query =
644
          qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, cleared, fx_transaction, taxkey) | .
645
          qq|VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), ?, ?, 'f', 't', | .
646
          qq|        (SELECT taxkey_id FROM chart WHERE accno = ?))|;
647
        @values = ($form->{id}, $accno, $form->{fx}{$accno}{$transdate},
648
                   conv_date($transdate), $accno);
649
        do_query($form, $dbh, $query, @values);
650
      }
728
  if ($form->{taxincluded} && $form->{taxrate} && $totalamount) {
729
    # add tax to amounts and invtotal
730
    for $i (1 .. $form->{rowcount}) {
731
      $taxamount            = ($totaltax + $totalwithholding) * $form->{"amount_$i"} / $totalamount;
732
      $tax                  = $form->round_amount($taxamount, 2);
733
      $diff                += ($taxamount - $tax);
734
      $form->{"amount_$i"} += $form->{"tax_$i"};
651 735
    }
736

  
737
    $form->{amount_1} += $form->round_amount($diff, 2);
652 738
  }
653 739

  
654
  # save AP record
655
  my $query = qq|UPDATE ap SET paid = ?, datepaid = ? WHERE id = ?|;
656
  @values = ($form->{paid}, $form->{paid} ? $form->{datepaid} : undef,
657
             $form->{id});
658
  do_query($form, $dbh, $query, @values);
659

  
660
  my $rc = $dbh->commit;
661
  $dbh->disconnect;
740
  $taxamount        = $form->round_amount($taxamount, 2);
741
  $form->{invtotal} = $totalamount + $totaltax;
662 742

  
663 743
  $main::lxdebug->leave_sub();
664

  
665
  return $rc;
666 744
}
667 745

  
668 746
1;
bin/mozilla/ap.pl
158 158

  
159 159
  $form->{employee} = "$form->{employee}--$form->{employee_id}";
160 160

  
161
  # forex
162
  $form->{forex} = $form->{exchangerate};
163
  $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
164

  
165
  foreach $key (keys %{ $form->{AP_links} }) {
166
    foreach $ref (@{ $form->{AP_links}{$key} }) {
167
      if ($key eq "AP_paid") {
168
        $form->{"select$key"} .=
169
          "<option value=\"$ref->{accno}\">$ref->{accno}--$ref->{description}</option>\n";
170
      } else {
171
        $form->{"select$key"} .=
172
          "<option value=\"$ref->{accno}--$ref->{tax_id}\">$ref->{accno}--$ref->{description}</option>\n";
173
      }
174
    }
175

  
176
    $form->{$key} = $form->{"select$key"};
177

  
178
    # if there is a value we have an old entry
179
    my $j = 0;
180
    my $k = 0;
181

  
182
    for $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
183

  
184
      if ($key eq "AP_paid") {
185
        $j++;
186
        $form->{"AP_paid_$j"} =
187
          "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
188
        $form->{"paid_$j"}     = $form->{acc_trans}{$key}->[$i - 1]->{amount};
189
        $form->{"datepaid_$j"} =
190
          $form->{acc_trans}{$key}->[$i - 1]->{transdate};
191
        $form->{"source_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{source};
192
        $form->{"memo_$j"}   = $form->{acc_trans}{$key}->[$i - 1]->{memo};
193

  
194
        $form->{"forex_$j"} = $form->{"exchangerate_$i"} =
195
          $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
196
        $form->{"AP_paid_$j"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}";
197
        $form->{"paid_project_id_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{project_id};
198
        $form->{paidaccounts}++;
199
      } else {
200

  
201
        $akey = $key;
202
        $akey =~ s/AP_//;
203

  
204
        if (($key eq "AP_tax") || ($key eq "AR_tax")) {
205
          $form->{"${key}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} =
206
            "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
207
          $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} =
208
            $form->round_amount(
209
                  $form->{acc_trans}{$key}->[$i - 1]->{amount} / $exchangerate,
210
                  2);
211

  
212
          if ($form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"} > 0) {
213
            $totaltax +=
214
              $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"};
215
          } else {
216
            $totalwithholding +=
217
              $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"};
218
            $withholdingrate +=
219
              $form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"};
220
          }
221
          $index = $form->{acc_trans}{$key}->[$i - 1]->{index};
222
          $form->{"tax_$index"} =
223
            $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1;
224
          $totaltax += $form->{"tax_$index"};
225

  
226
        } else {
227
          $k++;
228
          $form->{"${akey}_$k"} =
229
            $form->round_amount(
230
                  $form->{acc_trans}{$key}->[$i - 1]->{amount} / $exchangerate,
231
                  2);
232
          if ($akey eq 'amount') {
233
            $form->{rowcount}++;
234
            $form->{"${akey}_$i"} *= -1;
235
            $totalamount += $form->{"${akey}_$i"};
236
            $form->{taxrate} = $form->{acc_trans}{$key}->[$i - 1]->{rate};
237
            $form->{"oldprojectnumber_$k"} = $form->{"projectnumber_$k"} =
238
              "$form->{acc_trans}{$key}->[$i-1]->{projectnumber}";
239
            $form->{"project_id_$k"} =
240
              "$form->{acc_trans}{$key}->[$i-1]->{project_id}";
241
          }
242
          $form->{"${key}_$k"} =
243
            "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
244
          my $q_description = quotemeta($form->{acc_trans}{$key}->[$i-1]->{description});
245
          $form->{"select${key}"} =~
246
            /<option value=\"($form->{acc_trans}{$key}->[$i-1]->{accno}--[^\"]*)\">$form->{acc_trans}{$key}->[$i-1]->{accno}--${q_description}<\/option>\n/;
247
          $form->{"${key}_$k"} = $1;
248

  
249
          if ($akey eq "AP") {
250
            $form->{APselected} = $form->{acc_trans}{$key}->[$i-1]->{accno};
251

  
252
          } elsif ($akey eq 'amount') {
253
            $form->{"${key}_$k"} = $form->{acc_trans}{$key}->[$i-1]->{accno} .
254
              "--" . $form->{acc_trans}{$key}->[$i-1]->{id};
255
            $form->{"taxchart_$k"} = $form->{acc_trans}{$key}->[$i-1]->{id} .
256
              "--" . $form->{acc_trans}{$key}->[$i-1]->{rate};
257
          }
258
        }
259
      }
260
    }
261
  }
262

  
263
  $form->{taxincluded}  = $taxincluded if ($form->{id});
264
  $form->{paidaccounts} = 1            if not defined $form->{paidaccounts};
265

  
266
  if ($form->{taxincluded} && $form->{taxrate} && $totalamount) {
267

  
268
    # add tax to amounts and invtotal
269
    for $i (1 .. $form->{rowcount}) {
270
      $taxamount =
271
        ($totaltax + $totalwithholding) * $form->{"amount_$i"} / $totalamount;
272
      $tax = $form->round_amount($taxamount, 2);
273
      $diff                += ($taxamount - $tax);
274
      $form->{"amount_$i"} += $form->{"tax_$i"};
275
    }
276
    $form->{amount_1} += $form->round_amount($diff, 2);
277
  }
278

  
279
  $taxamount = $form->round_amount($taxamount, 2);
280

  
281
  $form->{invtotal} = $totalamount + $totaltax;
161
  AP->setup_form($form);
282 162

  
283 163
  $form->{locked} =
284 164
    ($form->datetonum($form->{transdate}, \%myconfig) <=
......
872 752
  $transdate = $form->datetonum($form->{transdate}, \%myconfig);
873 753
  $closedto  = $form->datetonum($form->{closedto},  \%myconfig);
874 754

  
875
  # ToDO: - insert a global check for stornos, so that a storno is only possible a limited time after saving it
876
  print qq|<input class=submit type=submit name=action value="| . $locale->text('Storno') . qq|">|
877
    if ($form->{id} && !IS->has_storno(\%myconfig, $form, 'ap') && !IS->is_storno(\%myconfig, $form, 'ap') && !$form->{paid_1});
878

  
879 755
  print qq|<input class="submit" type="submit" name="action" id="update_button" value="| . $locale->text('Update') . qq|">|;
880 756

  
881 757
  if ($form->{id}) {
......
885 761
|;
886 762
    }
887 763

  
888
    print qq| <input class=submit type=submit name=action value="| . $locale->text('Use As Template') . qq|">
889
              <input class=submit type=submit name=action value="| . $locale->text('Post Payment') . qq|">
764
    # ToDO: - insert a global check for stornos, so that a storno is only possible a limited time after saving it
765
    print qq|<input class=submit type=submit name=action value="| . $locale->text('Storno') . qq|"> |
766
      if ($form->{id} && !IS->has_storno(\%myconfig, $form, 'ap') && !IS->is_storno(\%myconfig, $form, 'ap') && !$form->{paid_1});
767

  
768
    print qq| <input class=submit type=submit name=action value="| . $locale->text('Post Payment') . qq|">
769
              <input class=submit type=submit name=action value="| . $locale->text('Use As Template') . qq|">
890 770
|;
891 771
  } elsif (($transdate > $closedto) && !$form->{id}) {
892 772
    print qq|

Auch abrufbar als: Unified diff