X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=bin%2Fmozilla%2Fsepa.pl;h=78b5eea6e9d72b520a174e17b81425f74adf2d14;hb=7ca30d76589b74e1b84c1a182550e33ebf618372;hp=40d2dff79f4b62864f26527783855cd5b4598fbb;hpb=b6d6ad18d758a2725f46a6706b709fbf92d8d0eb;p=kivitendo-erp.git diff --git a/bin/mozilla/sepa.pl b/bin/mozilla/sepa.pl index 40d2dff79..78b5eea6e 100755 --- a/bin/mozilla/sepa.pl +++ b/bin/mozilla/sepa.pl @@ -1,10 +1,12 @@ 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::DB::SepaExport; use SL::Chart; use SL::CT; use SL::Form; @@ -22,10 +24,11 @@ sub bank_transfer_add { my $form = $main::form; my $locale = $main::locale; my $vc = $form->{vc} eq 'customer' ? 'customer' : 'vendor'; + my $vc_no = $form->{vc} eq 'customer' ? $::locale->text('VN') : $::locale->text('CN'); $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.')); @@ -40,9 +43,12 @@ sub bank_transfer_add { return; } - $_->{checked} = $_->{direct_debit} for @{ $invoices }; - - 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 }; @@ -51,13 +57,19 @@ sub bank_transfer_add { my $prefix = $translations{ $invoice->{language_id} } || $translations{default} || $::locale->text('Invoice'); $prefix .= ' ' unless $prefix =~ m/ $/; $invoice->{reference_prefix} = $prefix; + + # add c_vendor_id or v_vendor_id as a prefix if a entry exists + next unless $invoice->{vc_vc_id}; + + my $prefix_vc_number = $translations{ $invoice->{language_id} } || $translations{default} || $vc_no; + $prefix_vc_number .= ' ' unless $prefix_vc_number =~ m/ $/; + $invoice->{reference_prefix_vc} = ' ' . $prefix_vc_number unless $prefix_vc_number =~ m/^ /; } $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, }); @@ -74,21 +86,24 @@ sub bank_transfer_create { $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} } }, %{ $_ } }, @@ -96,18 +111,35 @@ sub bank_transfer_create { 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; } @@ -120,24 +152,20 @@ sub bank_transfer_create { '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)) { + foreach (@bank_columns) { $bank_transfer->{"vc_${_}"} = $vc_bank_info->{ $bank_transfer->{vc_id} }->{$_}; $bank_transfer->{"our_${_}"} = $bank_account->{$_}; } @@ -145,7 +173,7 @@ sub bank_transfer_create { $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); @@ -164,7 +192,6 @@ sub bank_transfer_search { 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 }); @@ -190,7 +217,7 @@ sub bank_transfer_list { my %filter = map +( $_ => $form->{"f_${_}"} ), grep { $form->{"f_${_}"} } - (qw(vc invnumber), + (qw(vc invnumber message_id), map { ("${_}_date_from", "${_}_date_to") } qw(export requested_execution execution)); $filter{executed} = $form->{l_executed} ? 1 : 0 if ($form->{l_executed} != $form->{l_not_executed}); @@ -216,9 +243,13 @@ sub bank_transfer_list { 'employee' => { 'text' => $locale->text('Employee'), }, 'executed' => { 'text' => $locale->text('Executed'), }, 'closed' => { 'text' => $locale->text('Closed'), }, + num_invoices => { 'text' => $locale->text('Number of invoices'), }, + sum_amounts => { 'text' => $locale->text('Sum of all amounts'), }, + message_ids => { 'text' => $locale->text('SEPA message IDs'), }, ); - my @columns = qw(selected id export_date employee executed closed); + my @columns = qw(selected id export_date employee executed closed num_invoices sum_amounts message_ids); + my %column_alignment = map { ($_ => 'right') } qw(num_invoices sum_amounts); foreach my $name (qw(id export_date employee executed closed)) { my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir}; @@ -228,10 +259,12 @@ sub bank_transfer_list { $column_defs{selected}->{visible} = $open_available ? 'HTML' : 0; $column_defs{executed}->{visible} = $form->{l_executed} && $form->{l_not_executed} ? 1 : 0; $column_defs{closed}->{visible} = $form->{l_closed} && $form->{l_open} ? 1 : 0; + $column_defs{$_}->{align} = $column_alignment{$_} for keys %column_alignment; my @options = (); push @options, ($vc eq 'customer' ? $::locale->text('Customer') : $locale->text('Vendor')) . ' : ' . $form->{f_vc} if ($form->{f_vc}); push @options, $locale->text('Invoice number') . ' : ' . $form->{f_invnumber} if ($form->{f_invnumber}); + push @options, $locale->text('SEPA message ID') . ' : ' . $form->{f_message_id} if (length $form->{f_message_id}); push @options, $locale->text('Export date from') . ' : ' . $form->{f_export_date_from} if ($form->{f_export_date_from}); push @options, $locale->text('Export date to') . ' : ' . $form->{f_export_date_to} if ($form->{f_export_date_to}); push @options, $locale->text('Requested execution date from') . ' : ' . $form->{f_requested_execution_date_from} if ($form->{f_requested_execution_date_from}); @@ -260,12 +293,14 @@ sub bank_transfer_list { my $edit_url = build_std_url('action=bank_transfer_edit', 'callback'); foreach my $export (@{ $exports }) { - my $row = { map { $_ => { 'data' => $export->{$_} } } keys %{ $export } }; + my $row = { map { $_ => { 'data' => $export->{$_}, 'align' => $column_alignment{$_} } } keys %{ $export } }; map { $row->{$_}->{data} = $export->{$_} ? $locale->text('yes') : $locale->text('no') } qw(executed closed); $row->{id}->{link} = $edit_url . '&id=' . E($export->{id}) . '&vc=' . E($vc); + $row->{$_}->{data} = $::form->format_amount(\%::myconfig, $row->{$_}->{data}, 2) for qw(sum_amounts); + if (!$export->{closed}) { $row->{selected}->{raw_data} = $cgi->hidden(-name => "exports[+].id", -value => $export->{id}) @@ -294,7 +329,7 @@ sub bank_transfer_edit { @ids = map $_->{id}, grep { $_->{selected} } @{ $form->{exports} || [] }; if (!@ids) { - $form->show_generic_error($locale->text('You have not selected any export.'), 'back_button' => 1); + $form->show_generic_error($locale->text('You have not selected any export.')); } } @@ -318,14 +353,13 @@ sub bank_transfer_edit { $export->{items} = [ grep { !$_->{export_closed} && !$_->{executed} } @{ $export->{items} } ]; if (!@{ $export->{items} }) { - $form->show_generic_error($locale->text('All the selected exports have already been closed, or all of their items have already been executed.'), 'back_button' => 1); + $form->show_generic_error($locale->text('All the selected exports have already been closed, or all of their items have already been executed.')); } } elsif (!$export) { $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', @@ -348,7 +382,7 @@ sub bank_transfer_post_payments { my @items = grep { $_->{selected} } @{ $form->{items} || [] }; if (!@items) { - $form->show_generic_error($locale->text('You have not selected any item.'), 'back_button' => 1); + $form->show_generic_error($locale->text('You have not selected any item.')); } my @export_ids = uniq map { $_->{sepa_export_id} } @items; my %exports = map { $_ => SL::SEPA->retrieve_export('id' => $_, 'details' => 1, vc => $vc) } @export_ids; @@ -362,11 +396,11 @@ sub bank_transfer_post_payments { } if (!@items_to_post) { - $form->show_generic_error($locale->text('All the selected exports have already been closed, or all of their items have already been executed.'), 'back_button' => 1); + $form->show_generic_error($locale->text('All the selected exports have already been closed, or all of their items have already been executed.')); } if (any { !$_->{execution_date} } @items_to_post) { - $form->show_generic_error($locale->text('You have to specify an execution date for each antry.'), 'back_button' => 1); + $form->show_generic_error($locale->text('You have to specify an execution date for each antry.')); } SL::SEPA->post_payment('items' => \@items_to_post, vc => $vc); @@ -387,7 +421,7 @@ sub bank_transfer_payment_list_as_pdf { 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; + $form->show_generic_error($locale->text('Multi mode not supported.')) if 1 != scalar @export_ids; my $export = SL::SEPA->retrieve_export('id' => $export_ids[0], 'details' => 1, vc => $vc); my @items = (); @@ -397,7 +431,7 @@ sub bank_transfer_payment_list_as_pdf { push @items, $item if $item; } - $form->show_generic_error($locale->text('No transfers were executed in this export.'), 'back_button' => 1) if 1 > scalar @items; + $form->show_generic_error($locale->text('No transfers were executed in this export.')) if 1 > scalar @items; my $report = SL::ReportGenerator->new(\%main::myconfig, $form); @@ -447,13 +481,14 @@ sub bank_transfer_download_sepa_xml { my $locale = $main::locale; my $cgi = $::request->{cgi}; my $vc = $form->{vc} eq 'customer' ? 'customer' : 'vendor'; + my $defaults = SL::DB::Default->get; - 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 (!$defaults->company) { + $form->show_generic_error($locale->text('You have to enter a company name in the client configuration.')); } - if (($vc eq 'customer') && !$myconfig->{sepa_creditor_id}) { - $form->show_generic_error($locale->text('You have to enter the SEPA creditor ID 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.')); } my @ids; @@ -465,7 +500,7 @@ sub bank_transfer_download_sepa_xml { } if (!@ids) { - $form->show_generic_error($locale->text('You have not selected any export.'), 'back_button' => 1); + $form->show_generic_error($locale->text('You have not selected any export.')); } my @items = (); @@ -476,14 +511,14 @@ sub bank_transfer_download_sepa_xml { } if (!@items) { - $form->show_generic_error($locale->text('All the selected exports have already been closed, or all of their items have already been executed.'), 'back_button' => 1); + $form->show_generic_error($locale->text('All the selected exports have already been closed, or all of their items have already been executed.')); } my $message_id = strftime('MSG%Y%m%d%H%M%S', localtime) . sprintf('%06d', $$); - my $sepa_xml = SL::SEPA::XML->new('company' => $myconfig->{company}, - 'creditor_id' => $myconfig->{sepa_creditor_id}, - 'src_charset' => $::lx_office_conf{system}->{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', @@ -491,6 +526,7 @@ sub bank_transfer_download_sepa_xml { 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; @@ -499,6 +535,11 @@ sub bank_transfer_download_sepa_xml { 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}, @@ -506,11 +547,23 @@ sub bank_transfer_download_sepa_xml { '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}, }); + } + + # Store the message ID used in each of the entries in order to + # facilitate finding them by looking at bank statements. + foreach my $id (@ids) { + SL::DB::SepaExportMessageId->new( + sepa_export_id => $id, + message_id => $message_id, + )->save; } my $xml = $sepa_xml->to_xml(); @@ -533,7 +586,7 @@ sub bank_transfer_mark_as_closed_step1 { my @export_ids = map { $_->{id} } grep { $_->{selected} } @{ $form->{exports} || [] }; if (!@export_ids) { - $form->show_generic_error($locale->text('You have not selected any export.'), 'back_button' => 1); + $form->show_generic_error($locale->text('You have not selected any export.')); } my @open_export_ids = (); @@ -543,7 +596,7 @@ sub bank_transfer_mark_as_closed_step1 { } if (!@open_export_ids) { - $form->show_generic_error($locale->text('All of the exports you have selected were already closed.'), 'back_button' => 1); + $form->show_generic_error($locale->text('All of the exports you have selected were already closed.')); } $form->{title} = $locale->text('Close SEPA exports');