use SL::DB::Draft;
use SL::DB::Order;
use SL::DB::PurchaseInvoice;
+use SL::DB::EmailJournal;
+use SL::DB::ValidityToken;
use SL::Util qw(trim);
use SL::DB;
use Data::Dumper;
use List::Util qw(sum0);
use strict;
+use URI::Escape;
sub post_transaction {
my ($self, $myconfig, $form, $provided_dbh, %params) = @_;
sub _post_transaction {
my ($self, $myconfig, $form, $provided_dbh, %params) = @_;
+ my $validity_token;
+ if (!$form->{id}) {
+ $validity_token = SL::DB::Manager::ValidityToken->fetch_valid_token(
+ scope => SL::DB::ValidityToken::SCOPE_PURCHASE_INVOICE_POST(),
+ token => $form->{form_validity_token},
+ );
+
+ die $::locale->text('The form is not valid anymore.') if !$validity_token;
+ }
+
my $payments_only = $params{payments_only};
my $dbh = $provided_dbh || SL::DB->client->dbh;
$form->{defaultcurrency} = $form->get_default_currency($myconfig);
$form->{taxincluded} = 0 unless $form->{taxincluded};
+ $form->{script} = 'ap.pl' unless $form->{script};
+
+ # make sure to have a id
+ my ($query, $sth, @values);
+ if (!$payments_only) {
+ # if we have an id delete old records
+ if ($form->{id}) {
+ # delete detail records
+ $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
+ do_query($form, $dbh, $query, $form->{id});
+
+ } else {
+
+ ($form->{id}) = selectrow_query($form, $dbh, qq|SELECT nextval('glid')|);
+
+ $query =
+ qq|INSERT INTO ap (id, invnumber, employee_id,currency_id, taxzone_id) | .
+ qq|VALUES (?, ?, (SELECT e.id FROM employee e WHERE e.login = ?),
+ (SELECT id FROM currencies WHERE name = ?), (SELECT taxzone_id FROM vendor WHERE id = ?) )|;
+ do_query($form, $dbh, $query, $form->{id}, $form->{invnumber}, $::myconfig{login}, $form->{currency}, $form->{vendor_id});
+
+ }
+ }
+ # check default or record exchangerate
if ($form->{currency} eq $form->{defaultcurrency}) {
$form->{exchangerate} = 1;
} else {
$exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{transdate}, 'sell');
- $form->{exchangerate} = $exchangerate || $form->parse_amount($myconfig, $form->{exchangerate});
+ $form->{exchangerate} = $form->parse_amount($myconfig, $form->{exchangerate}, 5);
+
+ # if default exchangerate is not defined, define one
+ unless ($exchangerate) {
+ $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, 0, $form->{exchangerate});
+ # delete records exchangerate -> if user sets new invdate for record
+ $query = qq|UPDATE ap set exchangerate = NULL where id = ?|;
+ do_query($form, $dbh, $query, $form->{"id"});
+ }
+ # update record exchangerate, if the default is set and differs from current
+ if ($exchangerate && ($form->{exchangerate} != $exchangerate)) {
+ $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate},
+ 0, $form->{exchangerate}, $form->{id}, 'ap');
+ }
}
-
# get the charts selected
$form->{AP_amounts}{"amount_$_"} = $form->{"AP_amount_chart_id_$_"} for (1 .. $form->{rowcount});
# amount for total AP
$form->{payables} = $form->{invtotal};
- # update exchangerate
- if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
- $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, 0,
- $form->{exchangerate});
- }
-
- my ($query, $sth, @values);
-
if (!$payments_only) {
- # if we have an id delete old records
- if ($form->{id}) {
-
- # delete detail records
- $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
- do_query($form, $dbh, $query, $form->{id});
-
- } else {
-
- ($form->{id}) = selectrow_query($form, $dbh, qq|SELECT nextval('glid')|);
-
- $query =
- qq|INSERT INTO ap (id, invnumber, employee_id,currency_id, taxzone_id) | .
- qq|VALUES (?, ?, (SELECT e.id FROM employee e WHERE e.login = ?),
- (SELECT id FROM currencies WHERE name = ?), (SELECT taxzone_id FROM vendor WHERE id = ?) )|;
- do_query($form, $dbh, $query, $form->{id}, $form->{invnumber}, $::myconfig{login}, $form->{currency}, $form->{vendor_id});
-
- }
-
$query = qq|UPDATE ap SET invnumber = ?,
transdate = ?, ordnumber = ?, vendor_id = ?, taxincluded = ?,
amount = ?, duedate = ?, deliverydate = ?, tax_point = ?, paid = ?, netamount = ?,
currency_id = (SELECT id FROM currencies WHERE name = ?), notes = ?, department_id = ?, storno = ?, storno_id = ?,
- globalproject_id = ?, direct_debit = ?, payment_id = ?, transaction_description = ?
+ globalproject_id = ?, direct_debit = ?, payment_id = ?, transaction_description = ?, intnotes = ?,
+ qrbill_data = ?
WHERE id = ?|;
@values = ($form->{invnumber}, conv_date($form->{transdate}),
$form->{ordnumber}, conv_i($form->{vendor_id}),
$form->{storno_id}, conv_i($form->{globalproject_id}),
$form->{direct_debit} ? 't' : 'f',
conv_i($form->{payment_id}), $form->{transaction_description},
+ $form->{intnotes},
+ $form->{qrbill_data_encoded} ? uri_unescape($form->{qrbill_data_encoded}) : undef,
$form->{id});
do_query($form, $dbh, $query, @values);
# Link this record to the record it was created from.
my $convert_from_oe_id = delete $form->{convert_from_oe_id};
- if (!$form->{postasnew} && $convert_from_oe_id) {
+ if ($convert_from_oe_id) {
RecordLinks->create_links('dbh' => $dbh,
'mode' => 'ids',
'from_table' => 'oe',
$form->{"AP_amount_chart_id_$i"});
do_query($form, $dbh, $query, @values);
- if ($form->{"tax_$i"} != 0) {
+ if ($form->{"tax_$i"} != 0 && !$form->{"reverse_charge_$i"}) {
# insert detail records in acc_trans
$query =
qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, | .
}
# hook for taxkey 94
- $self->_reverse_charge($myconfig, $form);
+ $self->_reverse_charge($myconfig, $form) unless $payments_only;
# safety check datev export
if ($::instance_conf->get_datev_check_on_ap_transaction) {
my $datev = SL::DATEV->new(
}
}
+ $validity_token->delete if $validity_token;
+ delete $form->{form_validity_token};
+
return 1;
}
my $query =
qq|SELECT a.id, a.invnumber, a.transdate, a.duedate, a.amount, a.paid, | .
qq| a.ordnumber, v.name, a.invoice, a.netamount, a.datepaid, a.notes, | .
- qq| a.globalproject_id, a.storno, a.storno_id, a.direct_debit, | .
+ qq| a.intnotes, a.globalproject_id, a.storno, a.storno_id, a.direct_debit, | .
qq| a.transaction_description, a.itime::DATE AS insertdate, | .
qq| pr.projectnumber AS globalprojectnumber, | .
qq| e.name AS employee, | .
$where .= " AND a.taxzone_id = ?";
push(@values, $form->{taxzone_id});
}
+ if ($form->{payment_id}) {
+ $where .= " AND a.payment_id = ?";
+ push(@values, $form->{payment_id});
+ }
if ($form->{transaction_description}) {
$where .= " AND a.transaction_description ILIKE ?";
push(@values, like($form->{transaction_description}));
$where .= " AND a.notes ILIKE ?";
push(@values, like($form->{notes}));
}
+ if ($form->{intnotes}) {
+ $where .= " AND a.intnotes ILIKE ?";
+ push(@values, like($form->{intnotes}));
+ }
if ($form->{project_id}) {
$where .=
qq| AND ((a.globalproject_id = ?) OR EXISTS | .
$where .= " AND a.duedate <= ?";
push(@values, trim($form->{duedateto}));
}
+ if ($form->{datepaidfrom}) {
+ $where .= " AND a.datepaid >= ?";
+ push(@values, trim($form->{datepaidfrom}));
+ }
+ if ($form->{datepaidto}) {
+ $where .= " AND a.datepaid <= ?";
+ push(@values, trim($form->{datepaidto}));
+ }
+ if ($form->{insertdatefrom}) {
+ $where .= " AND a.itime >= ?";
+ push(@values, trim($form->{insertdatefrom}));
+ }
+ if ($form->{insertdateto}) {
+ $where .= " AND a.itime <= ?";
+ push(@values, trim($form->{insertdateto}));
+ }
if ($form->{open} || $form->{closed}) {
unless ($form->{open} && $form->{closed}) {
$where .= " AND a.amount <> a.paid" if ($form->{open});
}
}
+ $form->{fulltext} = trim($form->{fulltext});
+ if ($form->{fulltext}) {
+ my @fulltext_fields = qw(a.notes
+ a.intnotes
+ a.shipvia
+ a.transaction_description
+ a.quonumber
+ a.ordnumber
+ a.invnumber);
+ $where .= ' AND (';
+ $where .= join ' OR ', map {"$_ ILIKE ?"} @fulltext_fields;
+
+ $where .= <<SQL;
+ OR EXISTS (
+ SELECT files.id FROM files LEFT JOIN file_full_texts ON (file_full_texts.file_id = files.id)
+ WHERE files.object_id = a.id AND files.object_type = 'purchase_invoice'
+ AND file_full_texts.full_text ILIKE ?)
+SQL
+ $where .= ')'; # end AND
+
+ push(@values, like($form->{fulltext})) for 1 .. (scalar @fulltext_fields) + 1;
+ }
+
if ($form->{parts_partnumber}) {
$where .= <<SQL;
AND EXISTS (
my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
my $sortorder = join(', ', map { "$_ $sortdir" } @a);
- if (grep({ $_ eq $form->{sort} } qw(transdate id invnumber ordnumber name netamount tax amount paid datepaid due duedate notes employee transaction_description direct_debit department taxzone))) {
+ if (grep({ $_ eq $form->{sort} } qw(transdate id invnumber ordnumber name netamount tax amount paid datepaid due duedate notes employee transaction_description direct_debit department taxzone insertdate))) {
$sortorder = $form->{sort} . " $sortdir";
}
$form->{AP} = [ @result ];
+ if ($form->{l_items} && scalar @{ $form->{AP} }) {
+ my ($items_query, $items_sth);
+ if ($form->{l_items}) {
+ $items_query =
+ qq|SELECT id
+ FROM invoice
+ WHERE trans_id = ?
+ ORDER BY position|;
+
+ $items_sth = prepare_query($form, $dbh, $items_query);
+ }
+
+ foreach my $ap (@{ $form->{AP} }) {
+ do_statement($form, $items_sth, $items_query, $ap->{id});
+ $ap->{item_ids} = $dbh->selectcol_arrayref($items_sth);
+ $ap->{item_ids} = undef if !@{$ap->{item_ids}};
+ }
+ $items_sth->finish();
+ }
+
$main::lxdebug->leave_sub();
}
$form->{"forex_$j"} = $form->{"exchangerate_$i"};
$form->{"AP_paid_$j"} = $form->{acc_trans}{$key}->[$i-1]->{accno};
$form->{"paid_project_id_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{project_id};
+ $form->{"defaultcurrency_paid_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{defaultcurrency_paid};
+ $form->{"fx_transaction_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{fx_transaction};
$form->{paidaccounts}++;
} else {
map { IO->set_datepaid(table => 'ap', id => $_, dbh => $dbh) } ($id, $new_id);
+ if ($form->{workflow_email_journal_id}) {
+ my $ap_transaction_storno = SL::DB::PurchaseInvoice->new(id => $new_id)->load;
+ my $email_journal = SL::DB::EmailJournal->new(
+ id => delete $form->{workflow_email_journal_id}
+ )->load;
+ $email_journal->link_to_record_with_attachment(
+ $ap_transaction_storno,
+ delete $form->{workflow_email_attachment_id}
+ );
+ $form->{callback} = delete $form->{workflow_email_callback};
+ }
+
+ $form->{storno_id} = $id;
return 1;
}