package AP;
use SL::DBUtils;
+use SL::IO;
use SL::MoreCommon;
use strict;
# amount for total AP
$form->{payables} = $form->{invtotal};
- $form->{datepaid} = $form->{transdate} unless ($form->{datepaid});
- my $datepaid = ($form->{invpaid} != 0) ? $form->{datepaid} : undef;
-
# update exchangerate
if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
$form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, 0,
$query = qq|UPDATE ap SET
invnumber = ?, transdate = ?, ordnumber = ?, vendor_id = ?, taxincluded = ?,
- amount = ?, duedate = ?, paid = ?, datepaid = ?, netamount = ?,
+ amount = ?, duedate = ?, paid = ?, netamount = ?,
curr = ?, notes = ?, department_id = ?, storno = ?, storno_id = ?
WHERE id = ?|;
@values = ($form->{invnumber}, conv_date($form->{transdate}),
$form->{ordnumber}, conv_i($form->{vendor_id}),
$form->{taxincluded} ? 't' : 'f', $form->{invtotal},
conv_date($form->{duedate}), $form->{invpaid},
- conv_date($datepaid), $form->{netamount},
+ $form->{netamount},
$form->{currency}, $form->{notes},
conv_i($form->{department_id}), $form->{storno},
$form->{storno_id}, $form->{id});
do_query($form, $dbh, $query, $form->{invpaid}, $form->{invpaid} ? conv_date($form->{datepaid}) : undef, conv_i($form->{id}));
}
+ IO->set_datepaid(table => 'ap', id => $form->{id}, dbh => $dbh);
+
my $rc = 1;
if (!$provided_dbh) {
$dbh->commit();
do_query($form, $dbh, $query, (values %$row));
}
+ map { IO->set_datepaid(table => 'ap', id => $_, dbh => $dbh) } ($id, $new_id);
+
$dbh->commit;
$main::lxdebug->leave_sub();
use Data::Dumper;
use SL::DBUtils;
+use SL::IO;
use SL::MoreCommon;
use strict;
($null, $form->{department_id}) = split(/--/, $form->{department});
$form->{department_id} *= 1;
- # record last payment date in ar table
- $form->{datepaid} ||= $form->{transdate} ;
- my $datepaid = ($form->{paid} != 0) ? $form->{datepaid} : undef;
-
# amount for AR account
$form->{receivables} = $form->round_amount($form->{amount}, 2) * -1;
$query =
qq|UPDATE ar set
invnumber = ?, ordnumber = ?, transdate = ?, customer_id = ?,
- taxincluded = ?, amount = ?, duedate = ?, paid = ?, datepaid = ?,
+ taxincluded = ?, amount = ?, duedate = ?, paid = ?,
netamount = ?, curr = ?, notes = ?, department_id = ?,
employee_id = ?, storno = ?, storno_id = ?
WHERE id = ?|;
my @values = ($form->{invnumber}, $form->{ordnumber}, conv_date($form->{transdate}), conv_i($form->{customer_id}), $form->{taxincluded} ? 't' : 'f', $form->{amount},
- conv_date($form->{duedate}), $form->{paid}, conv_date($datepaid), $form->{netamount}, $form->{currency}, $form->{notes}, conv_i($form->{department_id}),
+ conv_date($form->{duedate}), $form->{paid}, $form->{netamount}, $form->{currency}, $form->{notes}, conv_i($form->{department_id}),
conv_i($form->{employee_id}), $form->{storno} ? 't' : 'f', $form->{storno_id}, conv_i($form->{id}));
do_query($form, $dbh, $query, @values);
}
}
+ IO->set_datepaid(table => 'ar', id => $form->{id}, dbh => $dbh);
+
my $rc = 1;
if (!$provided_dbh) {
$rc = $dbh->commit();
do_query($form, $dbh, $query, (values %$row));
}
+ map { IO->set_datepaid(table => 'ap', id => $_, dbh => $dbh) } ($id, $new_id);
+
$dbh->commit;
$main::lxdebug->leave_sub();
package IO;
+use List::Util qw(first);
+use List::MoreUtils qw(any);
+
use SL::DBUtils;
use strict;
}
+sub set_datepaid {
+ $main::lxdebug->enter_sub();
+
+ my $self = shift;
+ my %params = @_;
+
+ Common::check_params(\%params, qw(id table));
+
+ my $myconfig = \%main::myconfig;
+ my $form = $main::form;
+
+ my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
+ my $id = conv_i($params{id});
+ my $table = (any { $_ eq $params{table} } qw(ar ap gl)) ? $params{table} : 'ar';
+
+ my ($curr_datepaid, $curr_paid) = selectfirst_array_query($form, $dbh, qq|SELECT datepaid, paid FROM $table WHERE id = ?|, $id);
+
+ $query = <<SQL;
+ SELECT MAX(at.transdate)
+ FROM acc_trans at
+ LEFT JOIN chart c ON (at.chart_id = c.id)
+ WHERE (at.trans_id = ?)
+ AND (c.link LIKE '%paid%')
+SQL
+
+ my ($max_acc_trans_date) = selectfirst_array_query($form, $dbh, $query, $id);
+
+ if ($max_acc_trans_date && ($max_acc_trans_date ne $curr_datepaid)) {
+ # 1. Fall: Es gab mindestens eine Zahlung, und das Datum der Zahlung entspricht nicht
+ # dem vermerkten Zahlungsdatum.
+ do_query($form, $dbh, qq|UPDATE $table SET datepaid = ? WHERE id = ?|, $max_acc_trans_date, $id);
+
+ } elsif (!$max_acc_trans_date && ($curr_paid * 1)) {
+ # 2. Fall: Es gab keine Zahlung, aber paid ist nicht 0. Das ist z.B. der Fall, wenn
+ # die Funktion "als bezahlt buchen" verwendet oder wenn ein Beleg storniert wird.
+ # In diesem Fall das letzte Modifikationsdatum als Bezahldatum nehmen, oder aber das
+ # Erstelldatum, wenn keine Modifikation erfolgt ist (bei Stornos z.B.).
+ do_query($form, $dbh, qq|UPDATE $table SET datepaid = COALESCE(mtime::date, itime::date) WHERE id = ?|, $id);
+ }
+
+ $dbh->commit() unless $params{dbh};
+
+ $main::lxdebug->leave_sub();
+}
+
+
1;
use SL::DBUtils;
use SL::DO;
use SL::GenericTranslations;
+use SL::IO;
use SL::MoreCommon;
use List::Util qw(min);
}
}
+ IO->set_datepaid(table => 'ap', id => $form->{id}, dbh => $dbh);
+
if ($payments_only) {
- $query = qq|UPDATE ap SET paid = ?, datepaid = ? WHERE id = ?|;
- do_query($form, $dbh, $query, $form->{paid}, $form->{paid} ? conv_date($form->{datepaid}) : undef, conv_i($form->{id}));
+ $query = qq|UPDATE ap SET paid = ? WHERE id = ?|;
+ do_query($form, $dbh, $query, $form->{paid}, conv_i($form->{id}));
if (!$provided_dbh) {
$dbh->commit();
$query = qq|UPDATE ap SET
invnumber = ?, ordnumber = ?, quonumber = ?, transdate = ?,
orddate = ?, quodate = ?, vendor_id = ?, amount = ?,
- netamount = ?, paid = ?, duedate = ?, datepaid = ?,
+ netamount = ?, paid = ?, duedate = ?,
invoice = ?, taxzone_id = ?, notes = ?, taxincluded = ?,
intnotes = ?, curr = ?, storno_id = ?, storno = ?,
cp_id = ?, employee_id = ?, department_id = ?,
@values = (
$form->{invnumber}, $form->{ordnumber}, $form->{quonumber}, conv_date($form->{invdate}),
conv_date($form->{orddate}), conv_date($form->{quodate}), conv_i($form->{vendor_id}), $amount,
- $netamount, $form->{paid}, conv_date($form->{duedate}), $form->{paid} ? conv_date($form->{datepaid}) : undef,
+ $netamount, $form->{paid}, conv_date($form->{duedate}),
'1', $taxzone_id, $form->{notes}, $form->{taxincluded} ? 't' : 'f',
$form->{intnotes}, $form->{currency}, conv_i($form->{storno_id}), $form->{storno} ? 't' : 'f',
conv_i($form->{cp_id}), conv_i($form->{employee_id}), conv_i($form->{department_id}),
use SL::GenericTranslations;
use SL::MoreCommon;
use SL::IC;
+use SL::IO;
use Data::Dumper;
use strict;
$form->{marge_total} *= -1;
}
+ IO->set_datepaid(table => 'ar', id => $form->{id}, dbh => $dbh);
+
if ($payments_only) {
- $query = qq|UPDATE ar SET paid = ?, datepaid = ? WHERE id = ?|;
- do_query($form, $dbh, $query, $form->{paid}, $form->{paid} ? conv_date($form->{datepaid}) : undef, conv_i($form->{id}));
+ $query = qq|UPDATE ar SET paid = ? WHERE id = ?|;
+ do_query($form, $dbh, $query, $form->{paid}, conv_i($form->{id}));
if (!$provided_dbh) {
$dbh->commit();
$query = qq|UPDATE ar set
invnumber = ?, ordnumber = ?, quonumber = ?, cusordnumber = ?,
transdate = ?, orddate = ?, quodate = ?, customer_id = ?,
- amount = ?, netamount = ?, paid = ?, datepaid = ?,
+ amount = ?, netamount = ?, paid = ?,
duedate = ?, deliverydate = ?, invoice = ?, shippingpoint = ?,
shipvia = ?, terms = ?, notes = ?, intnotes = ?,
curr = ?, department_id = ?, payment_id = ?, taxincluded = ?,
if($form->{mark_as_paid}) {
my $dbh ||= $form->get_standard_dbh($myconfig);
- my $query = qq|UPDATE $db_name SET paid = amount WHERE id = ?|;
+ my $query = qq|UPDATE $db_name SET paid = amount, datepaid = current_date WHERE id = ?|;
do_query($form, $dbh, $query, $form->{id});
$dbh->commit();
$form->redirect($locale->text("Marked as paid"));
--- /dev/null
+-- @tag: fix_datepaid
+-- @description: Felder datepaid in ar und ap richtig setzen
+-- @depends: release_2_6_0
+
+UPDATE ap
+ SET datepaid = COALESCE((SELECT MAX(at.transdate)
+ FROM acc_trans at
+ LEFT JOIN chart c ON (at.chart_id = c.id)
+ WHERE (at.trans_id = ap.id)
+ AND (c.link LIKE '%paid%')),
+ COALESCE(ap.mtime::date, ap.itime::date))
+ WHERE paid <> 0;
+
+UPDATE ar
+ SET datepaid = COALESCE((SELECT MAX(at.transdate)
+ FROM acc_trans at
+ LEFT JOIN chart c ON (at.chart_id = c.id)
+ WHERE (at.trans_id = ar.id)
+ AND (c.link LIKE '%paid%')),
+ COALESCE(ar.mtime::date, ar.itime::date))
+ WHERE paid <> 0;