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;
use Rose::Object::MakeMethods::Generic
(
+ scalar => [ qw(callback transaction) ],
'scalar --get_set_init' => [ qw(models problems) ],
);
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,
);
}
$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 {
);
}
+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__
-[%- 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>