+ # record payments and offsetting AR
+ if (!$form->{storno}) {
+ for my $i (1 .. $form->{paidaccounts}) {
+
+ if ($form->{"paid_$i"} != 0) {
+ my ($accno) = split /--/, $form->{"AR_paid_$i"};
+ $form->{"datepaid_$i"} = $form->{invdate}
+ unless ($form->{"datepaid_$i"});
+ $form->{datepaid} = $form->{"datepaid_$i"};
+
+ $exchangerate = 0;
+
+ if ($form->{currency} eq $form->{defaultcurrency}) {
+ $form->{"exchangerate_$i"} = 1;
+ } else {
+ $exchangerate =
+ $form->check_exchangerate($myconfig, $form->{currency},
+ $form->{"datepaid_$i"}, 'buy');
+
+ $form->{"exchangerate_$i"} =
+ ($exchangerate)
+ ? $exchangerate
+ : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
+ }
+
+ # record AR
+ $amount =
+ $form->round_amount($form->{"paid_$i"} * $form->{exchangerate} + $diff,
+ 2);
+
+ if ($form->{amount}{ $form->{id} }{ $form->{AR} } != 0) {
+ $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
+ transdate, project_id)
+ VALUES ($form->{id}, (SELECT c.id FROM chart c
+ WHERE c.accno = '$form->{AR}'),
+ $amount, '$form->{"datepaid_$i"}', ?)|;
+ do_query($form, $dbh, $query, $project_id);
+ }
+
+ # record payment
+ $form->{"paid_$i"} *= -1;
+
+ $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
+ source, memo, ?)
+ VALUES ($form->{id}, (SELECT c.id FROM chart c
+ WHERE c.accno = '$accno', project_id),
+ $form->{"paid_$i"}, '$form->{"datepaid_$i"}',
+ '$form->{"source_$i"}', '$form->{"memo_$i"}')|;
+ do_query($form, $dbh, $query, $project_id);
+
+ # exchangerate difference
+ $form->{fx}{$accno}{ $form->{"datepaid_$i"} } +=
+ $form->{"paid_$i"} * ($form->{"exchangerate_$i"} - 1) + $diff;
+
+ # gain/loss
+ $amount =
+ $form->{"paid_$i"} * $form->{exchangerate} - $form->{"paid_$i"} *
+ $form->{"exchangerate_$i"};
+ if ($amount > 0) {
+ $form->{fx}{ $form->{fxgain_accno} }{ $form->{"datepaid_$i"} } +=
+ $amount;
+ } else {
+ $form->{fx}{ $form->{fxloss_accno} }{ $form->{"datepaid_$i"} } +=
+ $amount;
+ }
+
+ $diff = 0;
+
+ # update exchange rate
+ if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
+ $form->update_exchangerate($dbh, $form->{currency},
+ $form->{"datepaid_$i"},
+ $form->{"exchangerate_$i"}, 0);
+ }
+ }
+ }
+ }
+
+ # record exchange rate differences and gains/losses
+ foreach my $accno (keys %{ $form->{fx} }) {
+ foreach my $transdate (keys %{ $form->{fx}{$accno} }) {
+ if (
+ ($form->{fx}{$accno}{$transdate} =
+ $form->round_amount($form->{fx}{$accno}{$transdate}, 2)
+ ) != 0
+ ) {
+
+ $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
+ transdate, cleared, fx_transaction, project_id)
+ VALUES ($form->{id},
+ (SELECT c.id FROM chart c
+ WHERE c.accno = '$accno'),
+ $form->{fx}{$accno}{$transdate}, '$transdate', '0', '1', ?)|;
+ do_query($form, $dbh, $query, $project_id);
+ }
+ }
+ }
+
+ $amount = $netamount + $tax;
+
+ # set values which could be empty to 0
+ $form->{terms} *= 1;
+ $form->{taxincluded} *= 1;
+ my $datepaid = ($form->{paid}) ? qq|'$form->{datepaid}'| : "NULL";
+ my $duedate = ($form->{duedate}) ? qq|'$form->{duedate}'| : "NULL";
+ my $deliverydate =
+ ($form->{deliverydate}) ? qq|'$form->{deliverydate}'| : "NULL";
+
+ # fill in subject if there is none
+ $form->{subject} = qq|$form->{label} $form->{invnumber}|
+ unless $form->{subject};
+
+ # if there is a message stuff it into the intnotes
+ my $cc = "Cc: $form->{cc}\\r\n" if $form->{cc};
+ my $bcc = "Bcc: $form->{bcc}\\r\n" if $form->{bcc};
+ my $now = scalar localtime;
+ $form->{intnotes} .= qq|\r
+\r| if $form->{intnotes};
+
+ $form->{intnotes} .= qq|[email]\r
+Date: $now
+To: $form->{email}\r
+$cc${bcc}Subject: $form->{subject}\r
+\r
+Message: $form->{message}\r| if $form->{message};
+
+ # save AR record
+ $query = qq|UPDATE ar set
+ invnumber = '$form->{invnumber}',
+ ordnumber = '$form->{ordnumber}',
+ quonumber = '$form->{quonumber}',
+ cusordnumber = '$form->{cusordnumber}',
+ transdate = '$form->{invdate}',
+ orddate = | . conv_dateq($form->{orddate}) . qq|,
+ quodate = | . conv_dateq($form->{quodate}) . qq|,
+ customer_id = $form->{customer_id},
+ amount = $amount,
+ netamount = $netamount,
+ paid = $form->{paid},
+ datepaid = $datepaid,
+ duedate = $duedate,
+ deliverydate = $deliverydate,
+ invoice = '1',
+ shippingpoint = '$form->{shippingpoint}',
+ shipvia = '$form->{shipvia}',
+ terms = $form->{terms},
+ notes = '$form->{notes}',
+ intnotes = '$form->{intnotes}',
+ taxincluded = '$form->{taxincluded}',
+ curr = '$form->{currency}',
+ department_id = $form->{department_id},
+ payment_id = $form->{payment_id},
+ type = '$form->{type}',
+ language_id = $form->{language_id},
+ taxzone_id = $form->{taxzone_id},
+ shipto_id = $form->{shipto_id},
+ delivery_customer_id = $form->{delivery_customer_id},
+ delivery_vendor_id = $form->{delivery_vendor_id},
+ employee_id = $form->{employee_id},
+ storno = '$form->{storno}',
+ globalproject_id = | . conv_i($form->{"globalproject_id"}, 'NULL') . qq|,
+ cp_id = | . conv_i($form->{"cp_id"}, 'NULL') . qq|
+ WHERE id = $form->{id}
+ |;
+ $dbh->do($query) || $form->dberror($query);
+
+ if ($form->{storno}) {
+ $query = qq| update ar set paid=paid+amount where id=$form->{storno_id}|;
+ $dbh->do($query) || $form->dberror($query);
+ $query = qq| update ar set storno='$form->{storno}' where id=$form->{storno_id}|;
+ $dbh->do($query) || $form->dberror($query);
+ $query = qq§ update ar set intnotes='Rechnung storniert am $form->{invdate} ' || intnotes where id=$form->{storno_id}§;
+ $dbh->do($query) || $form->dberror($query);
+
+ $query = qq| update ar set paid=amount where id=$form->{id}|;
+ $dbh->do($query) || $form->dberror($query);
+ }
+
+ $form->{pago_total} = $amount;
+
+ # add shipto
+ $form->{name} = $form->{customer};
+ $form->{name} =~ s/--$form->{customer_id}//;
+
+ if (!$form->{shipto_id}) {
+ $form->add_shipto($dbh, $form->{id}, "AR");
+ }
+
+ # save printed, emailed and queued
+ $form->save_status($dbh);
+
+ Common::webdav_folder($form) if ($main::webdav);
+
+ my $rc = $dbh->commit;
+ $dbh->disconnect;
+
+ $main::lxdebug->leave_sub();
+
+ return $rc;
+}
+
+sub post_payment {
+ $main::lxdebug->enter_sub();
+
+ my ($self, $myconfig, $form, $locale) = @_;
+
+ # connect to database, turn off autocommit
+ my $dbh = $form->dbconnect_noauto($myconfig);
+
+ $form->{datepaid} = $form->{invdate};
+
+ # total payments, don't move we need it here
+ for my $i (1 .. $form->{paidaccounts}) {
+ if ($form->{type} eq "credit_note") {
+ $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"}) * -1;
+ } else {
+ $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"});
+ }
+ $form->{paid} += $form->{"paid_$i"};
+ $form->{datepaid} = $form->{"datepaid_$i"} if ($form->{"datepaid_$i"});
+ }
+
+ $form->{exchangerate} =
+ $form->get_exchangerate($dbh, $form->{currency}, $form->{invdate},
+ "buy");
+
+ my $project_id = conv_i($form->{"globalproject_id"});
+