itmes_sorted liefert immer eine Array-Referenz ...
[kivitendo-erp.git] / SL / Presenter / Record.pm
index 64cb21b..a00f400 100644 (file)
@@ -7,6 +7,8 @@ use parent qw(Exporter);
 use Exporter qw(import);
 our @EXPORT = qw(grouped_record_list empty_record_list record_list);
 
+use SL::Util;
+
 use Carp;
 use List::Util qw(first);
 
@@ -20,9 +22,9 @@ sub _arrayify {
 sub grouped_record_list {
   my ($self, $list, %params) = @_;
 
-  %params    = map { exists $params{$_} ? ($_ => $params{$_}) : () } qw(selectable with_columns);
+  %params    = map { exists $params{$_} ? ($_ => $params{$_}) : () } qw(edit_record_links with_columns object_id object_model);
 
-  my %groups = _group_records($list);
+  my %groups = _sort_grouped_lists(_group_records($list));
   my $output = '';
 
   $output .= _sales_quotation_list(        $self, $groups{sales_quotations},         %params) if $groups{sales_quotations};
@@ -37,12 +39,17 @@ sub grouped_record_list {
   $output .= _purchase_invoice_list(       $self, $groups{purchase_invoices},        %params) if $groups{purchase_invoices};
   $output .= _ar_transaction_list(         $self, $groups{ar_transactions},          %params) if $groups{ar_transactions};
 
-  return $output || $self->empty_record_list;
+  $output .= _sepa_collection_list(        $self, $groups{sepa_collections},         %params) if $groups{sepa_collections};
+  $output .= _sepa_transfer_list(          $self, $groups{sepa_transfers},           %params) if $groups{sepa_transfers};
+
+  $output  = $self->render('presenter/record/grouped_record_list', %params, output => $output);
+
+  return $output;
 }
 
 sub empty_record_list {
-  my ($self) = @_;
-  return $self->render('presenter/record/empty_record_list');
+  my ($self, %params) = @_;
+  return $self->grouped_record_list([], %params);
 }
 
 sub record_list {
@@ -90,8 +97,9 @@ sub record_list {
       my $meta         =  $column_meta{ $spec->{data} };
       my $type         =  ref $meta;
       my $relationship =  $relationships{ $spec->{data} };
-      my $rel_type     =  !$relationship ? '' : lc $relationship->class;
-      $rel_type        =~ s/^sl::db:://;
+      my $rel_type     =  !$relationship ? '' : $relationship->class;
+      $rel_type        =~ s/^SL::DB:://;
+      $rel_type        =  SL::Util::snakify($rel_type);
 
       if (ref($spec->{data}) eq 'CODE') {
         $cell{value} = $spec->{data}->($obj);
@@ -110,12 +118,12 @@ sub record_list {
       push @row, \%cell;
     }
 
-    push @data, \@row;
+    push @data, { columns => \@row, record_link => $obj->{_record_link} };
   }
 
   my @header =
     map +{ value     => $columns[$_]->{title},
-           alignment => $data[0]->[$_]->{alignment},
+           alignment => $data[0]->{columns}->[$_]->{alignment},
          }, (0..scalar(@columns) - 1);
 
   return $self->render(
@@ -144,6 +152,8 @@ sub _group_records {
     purchase_delivery_orders => sub { (ref($_[0]) eq 'SL::DB::DeliveryOrder')   && !$_[0]->is_sales                     },
     purchase_invoices        => sub { (ref($_[0]) eq 'SL::DB::PurchaseInvoice') &&  $_[0]->invoice                      },
     ap_transactions          => sub { (ref($_[0]) eq 'SL::DB::PurchaseInvoice') && !$_[0]->invoice                      },
+    sepa_collections         => sub { (ref($_[0]) eq 'SL::DB::SepaExportItem')  &&  $_[0]->ar_id                        },
+    sepa_transfers           => sub { (ref($_[0]) eq 'SL::DB::SepaExportItem')  &&  $_[0]->ap_id                        },
   );
 
   my %groups;
@@ -157,12 +167,28 @@ sub _group_records {
   return %groups;
 }
 
+sub _sort_grouped_lists {
+  my (%groups) = @_;
+
+  foreach my $group (keys %groups) {
+    next unless @{ $groups{$group} };
+    if ($groups{$group}->[0]->can('compare_to')) {
+      $groups{$group} = [ sort { $a->compare_to($b)    } @{ $groups{$group} } ];
+    } else {
+      $groups{$group} = [ sort { $a->date <=> $b->date } @{ $groups{$group} } ];
+    }
+  }
+
+  return %groups;
+}
+
 sub _sales_quotation_list {
   my ($self, $list, %params) = @_;
 
   return $self->record_list(
     $list,
     title   => $::locale->text('Sales Quotations'),
+    type    => 'sales_quotation',
     columns => [
       [ $::locale->text('Quotation Date'),          'transdate'                                                                ],
       [ $::locale->text('Quotation Number'),        sub { $self->sales_quotation($_[0], display => 'table-cell') }   ],
@@ -182,9 +208,10 @@ sub _request_quotation_list {
   return $self->record_list(
     $list,
     title   => $::locale->text('Request Quotations'),
+    type    => 'request_quotation',
     columns => [
       [ $::locale->text('Quotation Date'),          'transdate'                                                                ],
-      [ $::locale->text('Quotation Number'),        sub { $self->sales_quotation($_[0], display => 'table-cell') }   ],
+      [ $::locale->text('Quotation Number'),        sub { $self->request_quotation($_[0], display => 'table-cell') }   ],
       [ $::locale->text('Vendor'),                  'vendor'                                                                   ],
       [ $::locale->text('Net amount'),              'netamount'                                                                ],
       [ $::locale->text('Transaction description'), 'transaction_description'                                                  ],
@@ -201,6 +228,7 @@ sub _sales_order_list {
   return $self->record_list(
     $list,
     title   => $::locale->text('Sales Orders'),
+    type    => 'sales_order',
     columns => [
       [ $::locale->text('Order Date'),              'transdate'                                                                ],
       [ $::locale->text('Order Number'),            sub { $self->sales_order($_[0], display => 'table-cell') }   ],
@@ -221,9 +249,10 @@ sub _purchase_order_list {
   return $self->record_list(
     $list,
     title   => $::locale->text('Purchase Orders'),
+    type    => 'purchase_order',
     columns => [
       [ $::locale->text('Order Date'),              'transdate'                                                                ],
-      [ $::locale->text('Order Number'),            sub { $self->sales_order($_[0], display => 'table-cell') }   ],
+      [ $::locale->text('Order Number'),            sub { $self->purchase_order($_[0], display => 'table-cell') }   ],
       [ $::locale->text('Request for Quotation'),   'quonumber' ],
       [ $::locale->text('Vendor'),                  'vendor'                                                                 ],
       [ $::locale->text('Net amount'),              'netamount'                                                                ],
@@ -241,6 +270,7 @@ sub _sales_delivery_order_list {
   return $self->record_list(
     $list,
     title   => $::locale->text('Sales Delivery Orders'),
+    type    => 'sales_delivery_order',
     columns => [
       [ $::locale->text('Delivery Order Date'),     'transdate'                                                                ],
       [ $::locale->text('Delivery Order Number'),   sub { $self->sales_delivery_order($_[0], display => 'table-cell') } ],
@@ -261,9 +291,10 @@ sub _purchase_delivery_order_list {
   return $self->record_list(
     $list,
     title   => $::locale->text('Purchase Delivery Orders'),
+    type    => 'purchase_delivery_order',
     columns => [
       [ $::locale->text('Delivery Order Date'),     'transdate'                                                                ],
-      [ $::locale->text('Delivery Order Number'),   sub { $self->sales_delivery_order($_[0], display => 'table-cell') } ],
+      [ $::locale->text('Delivery Order Number'),   sub { $self->purchase_delivery_order($_[0], display => 'table-cell') } ],
       [ $::locale->text('Order Number'),            'ordnumber' ],
       [ $::locale->text('Vendor'),                  'vendor'                                                                 ],
       [ $::locale->text('Transaction description'), 'transaction_description'                                                  ],
@@ -281,6 +312,7 @@ sub _sales_invoice_list {
   return $self->record_list(
     $list,
     title   => $::locale->text('Sales Invoices'),
+    type    => 'sales_invoice',
     columns => [
       [ $::locale->text('Invoice Date'),            'transdate'               ],
       [ $::locale->text('Invoice Number'),          sub { $self->sales_invoice($_[0], display => 'table-cell') } ],
@@ -301,9 +333,10 @@ sub _purchase_invoice_list {
   return $self->record_list(
     $list,
     title   => $::locale->text('Purchase Invoices'),
+    type    => 'purchase_invoice',
     columns => [
       [ $::locale->text('Invoice Date'),                 'transdate'               ],
-      [ $::locale->text('Invoice Number'),               sub { $self->sales_invoice($_[0], display => 'table-cell') } ],
+      [ $::locale->text('Invoice Number'),               sub { $self->purchase_invoice($_[0], display => 'table-cell') } ],
       [ $::locale->text('Request for Quotation Number'), 'quonumber' ],
       [ $::locale->text('Order Number'),                 'ordnumber' ],
       [ $::locale->text('Vendor'),                       'vendor'                 ],
@@ -321,6 +354,7 @@ sub _ar_transaction_list {
   return $self->record_list(
     $list,
     title   => $::locale->text('AR Transactions'),
+    type    => 'ar_transaction',
     columns => [
       [ $::locale->text('Invoice Date'),            'transdate'               ],
       [ $::locale->text('Invoice Number'),          sub { $self->ar_transaction($_[0], display => 'table-cell') } ],
@@ -339,9 +373,10 @@ sub _ap_transaction_list {
   return $self->record_list(
     $list,
     title   => $::locale->text('AP Transactions'),
+    type    => 'ap_transaction',
     columns => [
       [ $::locale->text('Invoice Date'),            'transdate'                      ],
-      [ $::locale->text('Invoice Number'),          sub { $self->ar_transaction($_[0 ], display => 'table-cell') } ],
+      [ $::locale->text('Invoice Number'),          sub { $self->ap_transaction($_[0 ], display => 'table-cell') } ],
       [ $::locale->text('Vendor'),                  'vendor'                         ],
       [ $::locale->text('Net amount'),              'netamount'                      ],
       [ $::locale->text('Paid'),                    'paid'                           ],
@@ -351,6 +386,41 @@ sub _ap_transaction_list {
   );
 }
 
+sub _sepa_export_list {
+  my ($self, $list, %params) = @_;
+
+  my ($source, $destination) = $params{type} eq 'sepa_transfer' ? qw(our vc)                                 : qw(vc our);
+  $params{title}             = $params{type} eq 'sepa_transfer' ? $::locale->text('Bank transfers via SEPA') : $::locale->text('Bank collections via SEPA');
+  $params{with_columns}      = [ grep { $_ ne 'record_link_direction' } @{ $params{with_columns} || [] } ];
+
+  delete $params{edit_record_links};
+
+  return $self->record_list(
+    $list,
+    columns => [
+      [ $::locale->text('Export Number'),    'sepa_export',                                  ],
+      [ $::locale->text('Execution date'),   'execution_date'                                ],
+      [ $::locale->text('Export date'),      sub { $_[0]->sepa_export->itime->to_kivitendo } ],
+      [ $::locale->text('Source BIC'),       "${source}_bic"                                 ],
+      [ $::locale->text('Source IBAN'),      "${source}_iban"                                ],
+      [ $::locale->text('Destination BIC'),  "${destination}_bic"                            ],
+      [ $::locale->text('Destination IBAN'), "${destination}_iban"                           ],
+      [ $::locale->text('Amount'),           'amount'                                        ],
+    ],
+    %params,
+  );
+}
+
+sub _sepa_transfer_list {
+  my ($self, $list, %params) = @_;
+  _sepa_export_list($self, $list, %params, type => 'sepa_transfer');
+}
+
+sub _sepa_collection_list {
+  my ($self, $list, %params) = @_;
+  _sepa_export_list($self, $list, %params, type => 'sepa_collection');
+}
+
 1;
 
 __END__
@@ -421,10 +491,17 @@ The order in which the records are grouped is:
 
 =item * AP transactions
 
+=item * SEPA collections
+
+=item * SEPA transfers
+
 =back
 
 Objects of unknown types are skipped.
 
+Parameters are passed to C<record_list> include C<with_objects> and
+C<edit_record_links>.
+
 =item C<record_list $list, %params>
 
 Returns a rendered version (actually an instance of
@@ -465,6 +542,26 @@ If the column spec is a hash reference then the same arguments are
 expected. The corresponding hash keys are C<title>, C<data> and
 C<link>.
 
+=item C<with_columns>
+
+Can be set by the caller to indicate additional columns to
+list. Currently supported:
+
+=over 2
+
+=item C<record_link_destination>
+
+The record link destination. Requires that the records to list have
+been retrieved via the L<SL::DB::Helper::LinkedRecords> helper.
+
+=back
+
+=item C<edit_record_links>
+
+If trueish additional controls will be rendered that allow the user to
+remove and add record links. Requires that the records to list have
+been retrieved via the L<SL::DB::Helper::LinkedRecords> helper.
+
 =back
 
 =back