Bankauszug verbuchen: Umstellung auf Belegvorlagen & Filter-Fixes
authorMoritz Bunkus <m.bunkus@linet-services.de>
Fri, 10 Feb 2017 10:21:45 +0000 (11:21 +0100)
committerMoritz Bunkus <m.bunkus@linet-services.de>
Fri, 10 Feb 2017 10:21:45 +0000 (11:21 +0100)
SL/Controller/BankTransaction.pm
js/kivi.BankTransaction.js
templates/webpages/bank_transactions/_template_list.html [new file with mode: 0644]
templates/webpages/bank_transactions/create_invoice.html
templates/webpages/bank_transactions/filter_drafts.html [deleted file]
templates/webpages/bank_transactions/list.html

index 13b65b7..8715727 100644 (file)
@@ -21,8 +21,8 @@ use SL::JSON;
 use SL::DB::Chart;
 use SL::DB::AccTransaction;
 use SL::DB::Tax;
-use SL::DB::Draft;
 use SL::DB::BankAccount;
+use SL::DB::RecordTemplate;
 use SL::DB::SepaExportItem;
 use SL::DBUtils qw(like);
 use SL::Presenter;
@@ -32,6 +32,7 @@ use List::Util qw(max);
 
 use Rose::Object::MakeMethods::Generic
 (
+  scalar                  => [ qw(callback transaction) ],
   'scalar --get_set_init' => [ qw(models problems) ],
 );
 
@@ -284,41 +285,33 @@ sub action_create_invoice {
   my ($self) = @_;
   my %myconfig = %main::myconfig;
 
-  $self->{transaction} = SL::DB::Manager::BankTransaction->find_by(id => $::form->{bt_id});
-  my $vendor_of_transaction = SL::DB::Manager::Vendor->find_by(account_number => $self->{transaction}->{remote_account_number});
-
-  my $use_vendor_filter = $self->{transaction}->{remote_account_number} && $vendor_of_transaction;
+  $self->transaction(SL::DB::Manager::BankTransaction->find_by(id => $::form->{bt_id}));
 
-  my $drafts = SL::DB::Manager::Draft->get_all(where => [ module => 'ap'] , with_objects => 'employee');
+  my $vendor_of_transaction = SL::DB::Manager::Vendor->find_by(account_number => $self->transaction->{remote_account_number});
+  my $use_vendor_filter     = $self->transaction->{remote_account_number} && $vendor_of_transaction;
 
-  my @filtered_drafts;
-
-  foreach my $draft ( @{ $drafts } ) {
-    my $draft_as_object = YAML::Load($draft->form);
-    my $vendor = SL::DB::Manager::Vendor->find_by(id => $draft_as_object->{vendor_id});
-    $draft->{vendor} = $vendor->name;
-    $draft->{vendor_id} = $vendor->id;
-    push @filtered_drafts, $draft;
-  }
+  my $templates             = SL::DB::Manager::RecordTemplate->get_all(
+    where        => [ template_type => 'ap_transaction' ],
+    with_objects => [ qw(employee vendor) ],
+  );
 
-  #Filter drafts
-  @filtered_drafts = grep { $_->{vendor_id} == $vendor_of_transaction->id } @filtered_drafts if $use_vendor_filter;
+  #Filter templates
+  $templates = [ grep { $_->vendor_id == $vendor_of_transaction->id } @{ $templates } ] if $use_vendor_filter;
 
-  my $all_vendors = SL::DB::Manager::Vendor->get_all();
-  my $callback    = $self->url_for(action                => 'list',
-                                   'filter.bank_account' => $::form->{filter}->{bank_account},
-                                   'filter.todate'       => $::form->{filter}->{todate},
-                                   'filter.fromdate'     => $::form->{filter}->{fromdate});
+  $self->callback($self->url_for(
+    action                => 'list',
+    'filter.bank_account' => $::form->{filter}->{bank_account},
+    'filter.todate'       => $::form->{filter}->{todate},
+    'filter.fromdate'     => $::form->{filter}->{fromdate},
+  ));
 
   $self->render(
     'bank_transactions/create_invoice',
     { layout => 0 },
     title       => t8('Create invoice'),
-    DRAFTS      => \@filtered_drafts,
+    TEMPLATES   => $templates,
     vendor_id   => $use_vendor_filter ? $vendor_of_transaction->id   : undef,
     vendor_name => $use_vendor_filter ? $vendor_of_transaction->name : undef,
-    ALL_VENDORS => $all_vendors,
-    callback    => $callback,
   );
 }
 
@@ -348,43 +341,37 @@ sub action_ajax_payment_suggestion {
   $self->render(\ SL::JSON::to_json( { 'html' => "$html" } ), { layout => 0, type => 'json', process => 0 });
 };
 
-sub action_filter_drafts {
+sub action_filter_templates {
   my ($self) = @_;
 
   $self->{transaction}      = SL::DB::Manager::BankTransaction->find_by(id => $::form->{bt_id});
   my $vendor_of_transaction = SL::DB::Manager::Vendor->find_by(account_number => $self->{transaction}->{remote_account_number});
 
-  my $drafts                = SL::DB::Manager::Draft->get_all(with_objects => 'employee');
-
-  my @filtered_drafts;
-
-  foreach my $draft ( @{ $drafts } ) {
-    my $draft_as_object = YAML::Load($draft->form);
-    next unless $draft_as_object->{vendor_id};  # we cannot filter for vendor name, if this is a gl draft
-
-    my $vendor          = SL::DB::Manager::Vendor->find_by(id => $draft_as_object->{vendor_id});
-    $draft->{vendor}    = $vendor->name;
-    $draft->{vendor_id} = $vendor->id;
+  my @filter;
+  push @filter, ('vendor.id'   => $::form->{vendor_id})                       if $::form->{vendor_id};
+  push @filter, ('vendor.name' => { ilike => '%' . $::form->{vendor} . '%' }) if $::form->{vendor};
 
-    push @filtered_drafts, $draft;
-  }
+  my $templates = SL::DB::Manager::RecordTemplate->get_all(
+    where        => [ template_type => 'ap_transaction', (or => \@filter) x !!@filter ],
+    with_objects => [ qw(employee vendor) ],
+  );
 
-  my $vendor_name = $::form->{vendor};
-  my $vendor_id   = $::form->{vendor_id};
+  $::form->{filter} //= {};
 
-  #Filter drafts
-  @filtered_drafts = grep { $_->{vendor_id} == $vendor_id      } @filtered_drafts if $vendor_id;
-  @filtered_drafts = grep { $_->{vendor}    =~ /$vendor_name/i } @filtered_drafts if $vendor_name;
+  $self->callback($self->url_for(
+    action                => 'list',
+    'filter.bank_account' => $::form->{filter}->{bank_account},
+    'filter.todate'       => $::form->{filter}->{todate},
+    'filter.fromdate'     => $::form->{filter}->{fromdate},
+  ));
 
   my $output  = $self->render(
-    'bank_transactions/filter_drafts',
+    'bank_transactions/_template_list',
     { output => 0 },
-    DRAFTS => \@filtered_drafts,
+    TEMPLATES => $templates,
   );
 
-  my %result = ( count => 0, html => $output );
-
-  $self->render(\to_json(\%result), { type => 'json', process => 0 });
+  $self->render(\to_json({ html => $output }), { type => 'json', process => 0 });
 }
 
 sub action_ajax_add_list {
@@ -883,6 +870,24 @@ sub init_models {
   );
 }
 
+sub load_ap_record_template_url {
+  my ($self, $template) = @_;
+
+  return $self->url_for(
+    controller                 => 'ap.pl',
+    action                     => 'load_record_template',
+    id                         => $template->id,
+    'form_defaults.amount_1'   => $::form->format_amount(\%::myconfig, -1 * $self->transaction->amount, 2),
+    'form_defaults.transdate'  => $self->transaction->transdate_as_date,
+    'form_defaults.duedate'    => $self->transaction->transdate_as_date,
+    'form_defaults.datepaid_1' => $self->transaction->transdate_as_date,
+    'form_defaults.paid_1'     => $::form->format_amount(\%::myconfig, -1 * $self->transaction->amount, 2),
+    'form_defaults.currency'   => $self->transaction->currency->name,
+    'form_defaults.AP_paid_1'  => $self->transaction->local_bank_account->chart->accno,
+    'form_defaults.callback'   => $self->callback,
+  );
+}
+
 1;
 __END__
 
index 838b98a..0c13463 100644 (file)
@@ -44,7 +44,7 @@ namespace('kivi.BankTransaction', function(ns) {
   ns.create_invoice = function(bank_transaction_id) {
     kivi.popup_dialog({
       url:    'controller.pl?action=BankTransaction/create_invoice',
-      data:   '&bt_id=' + bank_transaction_id + "&filter.bank_account=" + $('#filter_bankaccount').val() + '&filter.fromdate=' + $('#filter_fromdate').val() + '&filter.todate=' + $('#filter_todate').val(),
+      data:   '&bt_id=' + bank_transaction_id + "&filter.bank_account=" + $('#filter_bank_account').val() + '&filter.fromdate=' + $('#filter_fromdate').val() + '&filter.todate=' + $('#filter_todate').val(),
       type:   'POST',
       id:     'create_invoice_window',
       dialog: { title: kivi.t8('Create invoice') }
@@ -130,4 +130,14 @@ namespace('kivi.BankTransaction', function(ns) {
 
     $dlg.dialog('close');
   };
+
+  ns.filter_templates = function() {
+    var url="controller.pl?action=BankTransaction/filter_templates&" + $("#create_invoice_window form").serialize();
+    $.ajax({
+      url: url,
+      success: function(new_data) {
+        $("#templates").html(new_data.error || new_data.html);
+      }
+    });
+  };
 });
diff --git a/templates/webpages/bank_transactions/_template_list.html b/templates/webpages/bank_transactions/_template_list.html
new file mode 100644 (file)
index 0000000..9bd54e3
--- /dev/null
@@ -0,0 +1,26 @@
+[%- USE HTML -%][%- USE LxERP -%][%- USE P -%][% IF TEMPLATES.size %]
+ [% LxERP.t8('Template suggestions') %]:
+ <table>
+  <thead>
+   <tr>
+    <th class="listheading">[% LxERP.t8('Description') %]</th>
+    <th class="listheading">[% LxERP.t8('Vendor') %]</th>
+    <th class="listheading">[% LxERP.t8('Employee') %]</th>
+    <th class="listheading">[% LxERP.t8('Template date') %]</th>
+   </tr>
+  </thead>
+
+  <tbody>
+   [% FOREACH template = TEMPLATES %]
+    <tr class="listrow">
+     <td>[% P.link(SELF.load_ap_record_template_url(template), template.template_name) %]</td>
+     <td>[% HTML.escape(template.vendor.name) %]</td>
+     <td>[% HTML.escape(template.employee.name || template.employee.login) %]</td>
+     <td>[% HTML.escape(template.itime_as_date) %]</td>
+    </tr>
+   [% END %]
+  </tbody>
+ </table>
+[% ELSE %]
+ <p class="message_hint">[% LxERP.t8('No template was found.') %]</p>
+[% END %]
index 818f01f..25b32b3 100644 (file)
@@ -1,4 +1,4 @@
-[%- USE HTML %][%- USE L %][%- USE LxERP %][%- USE T8 %][%- USE P -%]
+[%- USE HTML %][%- USE L %][%- USE LxERP %][%- USE P -%]
 
   <b>Transaction</b>
   <table>
 
 
 <br>
-[% 'Vendor filter for AP transaction drafts' | $T8 %]:
-
-<form method="post" action="javascript:filter_drafts();">
-[% L.hidden_tag('bt_id', SELF.transaction.id) %]
-  <table>
-   <tr>
-    <th align="right">[%- LxERP.t8("Vendor") %]</th>
-    <td>[% P.input_tag("vendor", vendor_name, class="initial_focus", style="width: 250px") %]</td>
-   </tr>
-  </table>
+[% LxERP.t8('Vendor filter for AP transaction templates') %]:
+
+<form method="post" action="javascript:kivi.BankTransaction.filter_templates()">
+ [% L.hidden_tag("bt_id",               SELF.transaction.id) %]
+ [% L.hidden_tag("filter.bank_account", FORM.filter.bank_account) %]
+ [% L.hidden_tag("filter.fromdate",     FORM.filter.fromdate) %]
+ [% L.hidden_tag("filter.todate",      FORM.filter.todate) %]
+ <table>
+  <tr>
+   <th align="right">[%- LxERP.t8("Vendor") %]</th>
+   <td>[% P.input_tag("vendor", vendor_name, class="initial_focus", style="width: 250px") %]</td>
+  </tr>
+ </table>
 </form>
 
   <p>
+   [% P.button_tag("kivi.BankTransaction.filter_templates()", LxERP.t8("Filter vendors")) %]
    <a href="#" onclick="$('#create_invoice_window').dialog('close');">[% LxERP.t8("Cancel") %]</a>
   </p>
 
   <hr>
-<div id="drafts">
-[% IF DRAFTS.size %]
-[% 'Draft suggestions' | $T8 %]:
-
-
-  <table>
-   <tr>
-    <th class="listheading">[% 'Description' | $T8 %]</th>
-    <th class="listheading">[% 'Vendor' | $T8 %]</th>
-    <th class="listheading">[% 'Employee' | $T8 %]</th>
-    <th class="listheading">[% 'Draft from:' | $T8 %]</th>
-   </tr>
-
-   [% FOREACH draft = DRAFTS %]
-    <tr class="listrow[% loop.count % 2 %]">
-     <td><a href="controller.pl?action=Draft/load&id=[% HTML.url(draft.id) %]&form.amount_1=[% LxERP.format_amount(-1 * SELF.transaction.amount, 2) %]&form.transdate=[% HTML.url(SELF.transaction.transdate_as_date) %]&form.duedate=[% HTML.url(SELF.transaction.transdate_as_date) %]&form.datepaid_1=[% HTML.url(SELF.transaction.transdate_as_date) %]&form.paid_1=[% LxERP.format_amount(-1 * SELF.transaction.amount, 2) %]&form.currency=[% HTML.url(SELF.transaction.currency.name) %]&form.AP_paid_1=[% HTML.url(SELF.transaction.local_bank_account.chart.accno) %]&form.callback=[% HTML.url(callback) %]">[% HTML.escape(draft.description) %]</a></td>
-     <td>[% HTML.escape(draft.vendor) %]</td>
-     <td>[% HTML.escape(draft.employee.name) %]</td>
-     <td>[% HTML.escape(draft.itime_as_date) %]</td>
-    </tr>
-   [% END %]
-  </table>
-[% ELSE %]
-  <p class="message_hint">[% 'No draft was found.' | $T8 %]</p>
-[% END %]
+<div id="templates">
+ [% PROCESS "bank_transactions/_template_list.html" %]
 </div>
-
-<script type="text/javascript">
-<!--
-
-function filter_drafts() {
-  var url="controller.pl?action=BankTransaction/filter_drafts&" + $("#create_invoice_window form").serialize();
-  $.ajax({
-    url: url,
-    success: function(new_data) {
-      $("#drafts").html(new_data['html']);
-    }
-  });
-}
-//-->
-</script>
diff --git a/templates/webpages/bank_transactions/filter_drafts.html b/templates/webpages/bank_transactions/filter_drafts.html
deleted file mode 100644 (file)
index 7f08d26..0000000
+++ /dev/null
@@ -1,23 +0,0 @@
-[%- USE T8 -%][%- USE HTML -%][%- USE LxERP -%][%- USE P -%][%- USE L -%]
-[%- IF !DRAFTS.size %]
-  <p class="message_hint">[% 'No draft was found.' | $T8 %]</p>
-[%- ELSE %]
-  <table>
-   <tr>
-    <th class="listheading">[% 'Date' | $T8 %]</th>
-    <th class="listheading">[% 'Description' | $T8 %]</th>
-    <th class="listheading">[% 'Employee' | $T8 %]</th>
-    <th class="listheading">[% 'Vendor' | $T8 %]</th>
-   </tr>
-
-   [% FOREACH draft = DRAFTS %]
-    <tr class="listrow[% loop.count % 2 %]">
-     <td>[% HTML.escape(draft.itime_as_date) %]</td>
-     <td><a href="controller.pl?action=Draft/load&id=[% HTML.url(draft.id) %]&form.amount_1=[% LxERP.format_amount(-1 * SELF.transaction.amount, 2) %]&form.datepaid_1=[% HTML.url(SELF.transaction.transdate_as_date) %]&form.paid_1=[% LxERP.format_amount(-1 * SELF.transaction.amount, 2) %]&form.callback=[% HTML.url(callback) %]">[% HTML.escape(draft.description) %]</a></td>
-     <td>[% HTML.escape(draft.employee.name) %]</td>
-     <td>[% HTML.escape(draft.vendor) %]</td>
-    </tr>
-   [% END %]
-  </table>
-[%- END %]
-
index 2727ba4..2828d54 100644 (file)
@@ -12,9 +12,9 @@
 <p>
 [% IF FORM.filter.fromdate %] [% 'From' | $T8 %] [% FORM.filter.fromdate %] [% END %]
 [% IF FORM.filter.todate %]   [% 'to (date)' | $T8 %] [% FORM.filter.todate %][% END %]
-[% L.hidden_tag("filter_bankaccount", FORM.filter.bankaccount) %]
-[% L.hidden_tag("filter_fromdate", FORM.filter.fromdate) %]
-[% L.hidden_tag("filter_todate", FORM.filter.todate) %]
+[% L.hidden_tag("filter.bank_account", FORM.filter.bank_account) %]
+[% L.hidden_tag("filter.fromdate",     FORM.filter.fromdate) %]
+[% L.hidden_tag("filter.todate",       FORM.filter.todate) %]
 </p>
 
 <div id="bt_tabs" class="tabwidget">