use strict;
use List::MoreUtils qw(any none uniq);
-use List::Util qw(first);
+use List::Util qw(sum first);
use POSIX qw(strftime);
-use SL::BankAccount;
+use Data::Dumper;
+use SL::DB::BankAccount;
use SL::Chart;
use SL::CT;
use SL::Form;
+use SL::GenericTranslations;
use SL::ReportGenerator;
use SL::SEPA;
use SL::SEPA::XML;
$form->{title} = $vc eq 'customer' ? $::locale->text('Prepare bank collection via SEPA XML') : $locale->text('Prepare bank transfer via SEPA XML');
- my $bank_accounts = SL::BankAccount->list();
+ my $bank_accounts = SL::DB::Manager::BankAccount->get_all_sorted( query => [ obsolete => 0 ] );
if (!scalar @{ $bank_accounts }) {
$form->error($locale->text('You have not added bank accounts yet.'));
return;
}
- my $bank_account_label_sub = sub { $locale->text('Account number #1, bank code #2, #3', $_[0]->{account_number}, $_[0]->{bank_code}, $_[0]->{bank}) };
+ # Only include those per default that require manual action from our
+ # side. For sales invoices these are the ones for which direct debit
+ # has been selected. For purchase invoices it's the other way
+ # around: if direct debit is active then the vendor will collect
+ # from us automatically and we don't have to send money manually.
+ $_->{checked} = ($vc eq 'customer' ? $_->{direct_debit} : !$_->{direct_debit}) for @{ $invoices };
+
+ my $translation_list = GenericTranslations->list(translation_type => 'sepa_remittance_info_pfx');
+ my %translations = map { ( ($_->{language_id} || 'default') => $_->{translation} ) } @{ $translation_list };
+
+ foreach my $invoice (@{ $invoices }) {
+ my $prefix = $translations{ $invoice->{language_id} } || $translations{default} || $::locale->text('Invoice');
+ $prefix .= ' ' unless $prefix =~ m/ $/;
+ $invoice->{reference_prefix} = $prefix;
+ }
$form->header();
print $form->parse_html_template('sepa/bank_transfer_add',
{ 'INVOICES' => $invoices,
'BANK_ACCOUNTS' => $bank_accounts,
- 'bank_account_label' => $bank_account_label_sub,
'vc' => $vc,
});
$form->{title} = $vc eq 'customer' ? $::locale->text('Create bank collection via SEPA XML') : $locale->text('Create bank transfer via SEPA XML');
- my $bank_accounts = SL::BankAccount->list();
-
+ my $bank_accounts = SL::DB::Manager::BankAccount->get_all_sorted( query => [ obsolete => 0 ] );
if (!scalar @{ $bank_accounts }) {
$form->error($locale->text('You have not added bank accounts yet.'));
}
- my $bank_account = first { $form->{bank_account}->{id} == $_->{id} } @{ $bank_accounts };
+ my $bank_account = SL::DB::Manager::BankAccount->find_by( id => $form->{bank_account} );
- if (!$bank_account) {
+ unless ( $bank_account ) {
$form->error($locale->text('The selected bank account does not exist anymore.'));
}
my $arap_id = $vc eq 'customer' ? 'ar_id' : 'ap_id';
my $invoices = SL::SEPA->retrieve_open_invoices(vc => $vc);
+ # load all open invoices (again), but grep out the ones that were selected with checkboxes beforehand ($_->selected). At this stage we again have all the invoice information, including dropdown with payment_type options
+ # all the information from retrieve_open_invoices is then ADDED to what was passed via @{ $form->{bank_transfers} }
+ # parse amount from the entry in the form, but take skonto_amount from PT again
+ # the map inserts the values of invoice_map directly into the array of hashes
my %invoices_map = map { $_->{id} => $_ } @{ $invoices };
my @bank_transfers =
map +{ %{ $invoices_map{ $_->{$arap_id} } }, %{ $_ } },
map { $_->{amount} = $form->parse_amount($myconfig, $_->{amount}); $_ }
@{ $form->{bank_transfers} || [] };
+ # override default payment_type selection and set it to the one chosen by the user
+ # in the previous step, so that we don't need the logic in the template
+ foreach my $bt (@bank_transfers) {
+ foreach my $type ( @{$bt->{payment_select_options}} ) {
+ if ( $type->{payment_type} eq $bt->{payment_type} ) {
+ $type->{selected} = 1;
+ } else {
+ $type->{selected} = 0;
+ };
+ };
+ };
+
if (!scalar @bank_transfers) {
$form->error($locale->text('You have selected none of the invoices.'));
}
+ my $total_trans = sum map { $_->{open_amount} } @bank_transfers;
+
my ($vc_bank_info);
my $error_message;
+ my @bank_columns = qw(iban bic);
+ push @bank_columns, qw(mandator_id mandate_date_of_signature) if $vc eq 'customer';
+
if ($form->{confirmation}) {
$vc_bank_info = { map { $_->{id} => $_ } @{ $form->{vc_bank_info} || [] } };
foreach my $info (values %{ $vc_bank_info }) {
- if (any { !$info->{$_} } qw(iban bic)) {
+ if (any { !$info->{$_} } @bank_columns) {
$error_message = $locale->text('The bank information must not be empty.');
last;
}
}
if ($error_message || !$form->{confirmation}) {
- my @vc_ids = uniq map { $_->{"${vc}_id"} } @bank_transfers;
+ my @vc_ids = uniq map { $_->{vc_id} } @bank_transfers;
$vc_bank_info ||= CT->get_bank_info('vc' => $vc,
'id' => \@vc_ids);
my @vc_bank_info = sort { lc $a->{name} cmp lc $b->{name} } values %{ $vc_bank_info };
- my $bank_account_label_sub = sub { $locale->text('Account number #1, bank code #2, #3', $_[0]->{account_number}, $_[0]->{bank_code}, $_[0]->{bank}) };
-
- $form->{jsscript} = 1;
-
$form->header();
print $form->parse_html_template('sepa/bank_transfer_create',
{ 'BANK_TRANSFERS' => \@bank_transfers,
'BANK_ACCOUNTS' => $bank_accounts,
'VC_BANK_INFO' => \@vc_bank_info,
'bank_account' => $bank_account,
- 'bank_account_label' => $bank_account_label_sub,
'error_message' => $error_message,
'vc' => $vc,
+ 'total_trans' => $total_trans,
});
} else {
foreach my $bank_transfer (@bank_transfers) {
- foreach (qw(iban bic)) {
- $bank_transfer->{"vc_${_}"} = $vc_bank_info->{ $bank_transfer->{"${vc}_id"} }->{$_};
+ foreach (@bank_columns) {
+ $bank_transfer->{"vc_${_}"} = $vc_bank_info->{ $bank_transfer->{vc_id} }->{$_};
$bank_transfer->{"our_${_}"} = $bank_account->{$_};
}
$bank_transfer->{chart_id} = $bank_account->{chart_id};
}
- my $id = SL::SEPA->create_export('employee' => $form->{login},
+ my $id = SL::SEPA->create_export('employee' => $::myconfig{login},
'bank_transfers' => \@bank_transfers,
'vc' => $vc);
my $vc = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
$form->{title} = $vc eq 'customer' ? $::locale->text('List of bank collections') : $locale->text('List of bank transfers');
- $form->{jsscript} = 1;
$form->header();
print $form->parse_html_template('sepa/bank_transfer_search', { vc => $vc });
my $form = $main::form;
my $locale = $main::locale;
- my $cgi = $main::cgi;
+ my $cgi = $::request->{cgi};
my $vc = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
$form->{title} = $vc eq 'customer' ? $::locale->text('List of bank collections') : $locale->text('List of bank transfers');
'attachment_basename' => $locale->text('banktransfers') . strftime('_%Y%m%d', localtime time),
);
$report->set_options_from_form();
+ $locale->set_numberformat_wo_thousands_separator(\%::myconfig) if lc($report->{options}->{output_format}) eq 'csv';
$report->set_columns(%column_defs);
$report->set_column_order(@columns);
$form->error($locale->text('That export does not exist.'));
}
- $form->{jsscript} = 1;
$form->{title} = $locale->text('View SEPA export');
$form->header();
print $form->parse_html_template('sepa/bank_transfer_edit',
$main::lxdebug->leave_sub();
}
-# TODO
sub bank_transfer_payment_list_as_pdf {
$main::lxdebug->enter_sub();
my $form = $main::form;
my %myconfig = %main::myconfig;
my $locale = $main::locale;
+ my $vc = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
my @ids = @{ $form->{items} || [] };
my @export_ids = uniq map { $_->{export_id} } @ids;
$form->show_generic_error($locale->text('Multi mode not supported.'), 'back_button' => 1) if 1 != scalar @export_ids;
- my $export = SL::SEPA->retrieve_export('id' => $export_ids[0], 'details' => 1);
+ my $export = SL::SEPA->retrieve_export('id' => $export_ids[0], 'details' => 1, vc => $vc);
my @items = ();
foreach my $id (@ids) {
my $report = SL::ReportGenerator->new(\%main::myconfig, $form);
my %column_defs = (
- 'invnumber' => { 'text' => $locale->text('Invoice'), },
- 'vendor_name' => { 'text' => $locale->text('Vendor'), },
- 'our_iban' => { 'text' => $locale->text('Source IBAN'), },
- 'our_bic' => { 'text' => $locale->text('Source BIC'), },
- 'vendor_iban' => { 'text' => $locale->text('Destination IBAN'), },
- 'vendor_bic' => { 'text' => $locale->text('Destination BIC'), },
- 'amount' => { 'text' => $locale->text('Amount'), },
- 'reference' => { 'text' => $locale->text('Reference'), },
- 'execution_date' => { 'text' => $locale->text('Execution date'), },
+ 'invnumber' => { 'text' => $locale->text('Invoice'), },
+ 'vc_name' => { 'text' => $vc eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
+ 'our_iban' => { 'text' => $vc eq 'customer' ? $locale->text('Destination IBAN') : $locale->text('Source IBAN'), },
+ 'our_bic' => { 'text' => $vc eq 'customer' ? $locale->text('Destination BIC') : $locale->text('Source BIC'), },
+ 'vc_iban' => { 'text' => $vc eq 'customer' ? $locale->text('Source IBAN') : $locale->text('Destination IBAN'), },
+ 'vc_bic' => { 'text' => $vc eq 'customer' ? $locale->text('Source BIC') : $locale->text('Destination BIC'), },
+ 'amount' => { 'text' => $locale->text('Amount'), },
+ 'reference' => { 'text' => $locale->text('Reference'), },
+ 'execution_date' => { 'text' => $locale->text('Execution date'), },
);
map { $column_defs{$_}->{align} = 'right' } qw(amount execution_date);
- my @columns = qw(invnumber vendor_name our_iban our_bic vendor_iban vendor_bic amount reference execution_date);
+ my @columns = qw(invnumber vc_name our_iban our_bic vc_iban vc_bic amount reference execution_date);
$report->set_options('std_column_visibility' => 1,
'output_format' => 'PDF',
- 'title' => $locale->text('Bank transfer payment list for export #1', $export->{id}),
- 'attachment_basename' => $locale->text('bank_transfer_payment_list_#1', $export->{id}) . strftime('_%Y%m%d', localtime time),
+ 'title' => $vc eq 'customer' ? $locale->text('Bank collection payment list for export #1', $export->{id}) : $locale->text('Bank transfer payment list for export #1', $export->{id}),
+ 'attachment_basename' => ($vc eq 'customer' ? $locale->text('bank_collection_payment_list_#1', $export->{id}) : $locale->text('bank_transfer_payment_list_#1', $export->{id})) . strftime('_%Y%m%d', localtime time),
);
$report->set_columns(%column_defs);
$main::lxdebug->leave_sub();
}
+# TODO
sub bank_transfer_download_sepa_xml {
$main::lxdebug->enter_sub();
my $form = $main::form;
my $myconfig = \%main::myconfig;
my $locale = $main::locale;
- my $cgi = $main::cgi;
+ my $cgi = $::request->{cgi};
+ my $vc = $form->{vc} eq 'customer' ? 'customer' : 'vendor';
+ my $defaults = SL::DB::Default->get;
+
+ if (!$defaults->company) {
+ $form->show_generic_error($locale->text('You have to enter a company name in the client configuration.'), 'back_button' => 1);
+ }
- if (!$myconfig->{company}) {
- $form->show_generic_error($locale->text('You have to enter a company name in your user preferences (see the "Program" menu, "Preferences").'), 'back_button' => 1);
+ if (($vc eq 'customer') && !$defaults->sepa_creditor_id) {
+ $form->show_generic_error($locale->text('You have to enter the SEPA creditor ID in the client configuration.'), 'back_button' => 1);
}
my @ids;
my @items = ();
foreach my $id (@ids) {
- my $export = SL::SEPA->retrieve_export('id' => $id, 'details' => 1);
+ my $export = SL::SEPA->retrieve_export('id' => $id, 'details' => 1, vc => $vc);
push @items, grep { !$_->{executed} } @{ $export->{items} } if ($export && !$export->{closed});
}
my $message_id = strftime('MSG%Y%m%d%H%M%S', localtime) . sprintf('%06d', $$);
- my $sepa_xml = SL::SEPA::XML->new('company' => $myconfig->{company},
- 'src_charset' => $main::dbcharset || 'ISO-8859-15',
+ my $sepa_xml = SL::SEPA::XML->new('company' => $defaults->company,
+ 'creditor_id' => $defaults->sepa_creditor_id,
+ 'src_charset' => 'UTF-8',
'message_id' => $message_id,
'grouped' => 1,
+ 'collection' => $vc eq 'customer',
);
foreach my $item (@items) {
my $requested_execution_date;
+ my $mandator_id;
if ($item->{requested_execution_date}) {
my ($yy, $mm, $dd) = $locale->parse_date($myconfig, $item->{requested_execution_date});
$requested_execution_date = sprintf '%04d-%02d-%02d', $yy, $mm, $dd;
}
+ if ($vc eq 'customer') {
+ my ($yy, $mm, $dd) = $locale->parse_date($myconfig, $item->{reference_date});
+ $item->{reference_date} = sprintf '%04d-%02d-%02d', $yy, $mm, $dd;
+ $mandator_id = $item->{mandator_id};
+ if ($item->{mandate_date_of_signature}) {
+ ($yy, $mm, $dd) = $locale->parse_date($myconfig, $item->{mandate_date_of_signature});
+ $item->{mandate_date_of_signature} = sprintf '%04d-%02d-%02d', $yy, $mm, $dd;
+ }
+ }
+
$sepa_xml->add_transaction({ 'src_iban' => $item->{our_iban},
'src_bic' => $item->{our_bic},
- 'dst_iban' => $item->{vendor_iban},
- 'dst_bic' => $item->{vendor_bic},
- 'recipient' => $item->{vendor_name},
+ 'dst_iban' => $item->{vc_iban},
+ 'dst_bic' => $item->{vc_bic},
+ 'company' => $item->{vc_name},
+ 'company_number' => $item->{vc_number},
'amount' => $item->{amount},
'reference' => $item->{reference},
+ 'mandator_id' => $mandator_id,
+ 'reference_date' => $item->{reference_date},
'execution_date' => $requested_execution_date,
- 'end_to_end_id' => $item->{end_to_end_id} });
+ 'end_to_end_id' => $item->{end_to_end_id},
+ 'date_of_signature' => $item->{mandate_date_of_signature}, });
}
my $xml = $sepa_xml->to_xml();
print $cgi->header('-type' => 'application/octet-stream',
- '-content-disposition' => 'attachment; filename="SEPA_' . $message_id . '.cct"',
+ '-content-disposition' => 'attachment; filename="SEPA_' . $message_id . ($vc eq 'customer' ? '.cdd' : '.cct') . '"',
'-content-length' => length $xml);
print $xml;
$main::lxdebug->leave_sub();
}
-# TODO
sub bank_transfer_mark_as_closed_step2 {
$main::lxdebug->enter_sub();