From eb518737fe286cb180f4b5d4c0fc54831381e2cf Mon Sep 17 00:00:00 2001 From: Bernd Blessmann Date: Sat, 7 Apr 2012 17:45:56 +0200 Subject: [PATCH] =?utf8?q?Waren=20und=20Lieferanten=20als=20benutzerdefini?= =?utf8?q?erte=20Variablen=20hinzugef=C3=BCgt.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Mit einfachen vendor_ und part_selectoren im L-Plugin, die das select_tag verwenden. vc_limit wird nicht berücksichtigt und das ganze ist nur rudimentär getestet. --- SL/CVar.pm | 24 ++++++++++++--- SL/Template/Plugin/L.pm | 30 +++++++++++++++++++ bin/mozilla/amcvar.pl | 4 ++- templates/webpages/amcvar/render_inputs.html | 6 ++++ .../webpages/amcvar/render_inputs_block.html | 6 ++++ 5 files changed, 65 insertions(+), 5 deletions(-) diff --git a/SL/CVar.pm b/SL/CVar.pm index 714744a1a..c0b3f43f0 100644 --- a/SL/CVar.pm +++ b/SL/CVar.pm @@ -7,7 +7,7 @@ use Scalar::Util qw(blessed); use Data::Dumper; use SL::DBUtils; -use SL::MoreCommon qw(listify); +use SL::MoreCommon qw(any listify); sub get_configs { $main::lxdebug->enter_sub(); @@ -243,6 +243,8 @@ sub get_custom_variables { : $cvar->{type} eq 'timestamp' ? $act_var->{timestamp_value} : $cvar->{type} eq 'number' ? $act_var->{number_value} : $cvar->{type} eq 'customer' ? $act_var->{number_value} + : $cvar->{type} eq 'vendor' ? $act_var->{number_value} + : $cvar->{type} eq 'part' ? $act_var->{number_value} : $cvar->{type} eq 'bool' ? $act_var->{bool_value} : $act_var->{text_value}; $cvar->{valid} = $valid; @@ -279,6 +281,12 @@ sub get_custom_variables { } elsif ($cvar->{type} eq 'customer') { require SL::DB::Customer; $cvar->{value} = SL::DB::Manager::Customer->find_by(id => $cvar->{value} * 1); + } elsif ($cvar->{type} eq 'vendor') { + require SL::DB::Vendor; + $cvar->{value} = SL::DB::Manager::Vendor->find_by(id => $cvar->{value} * 1); + } elsif ($cvar->{type} eq 'part') { + require SL::DB::Part; + $cvar->{value} = SL::DB::Manager::Part->find_by(id => $cvar->{value} * 1); } } @@ -340,7 +348,7 @@ sub save_custom_variables { } elsif ($config->{type} eq 'bool') { push @values, $value ? 't' : 'f', undef, undef, undef; - } elsif ($config->{type} eq 'customer') { + } elsif (any { $config->{type} eq $_ } qw(customer vendor part)) { push @values, undef, undef, undef, $value * 1; } @@ -498,10 +506,16 @@ sub build_filter_query { $not = 'NOT' if ($params{filter}->{$name} eq 'no'); push @sub_where, qq|COALESCE(cvar.bool_value, false) = TRUE|; - } elsif ($config->{type} eq 'customer') { + } elsif (any { $config->{type} eq $_ } qw(customer vendor part)) { next unless $params{filter}->{$name}; - push @sub_where, qq|cvar.number_value * 1 IN (SELECT id FROM customer WHERE name ILIKE ?)|; + my $table = $config->{type}; + push @sub_where, qq|cvar.number_value * 1 IN (SELECT id FROM $table WHERE name ILIKE ?)|; + push @sub_values, "%$params{filter}->{$name}%"; + } elsif ($config->{type} eq 'part') { + next unless $params{filter}->{$name}; + + push @sub_where, qq|cvar.number_value * 1 IN (SELECT id FROM parts WHERE partnumber ILIKE ?)|; push @sub_values, "%$params{filter}->{$name}%"; } @@ -577,6 +591,8 @@ sub add_custom_variables_to_report { : $cfg->{type} eq 'timestamp' ? $ref->{timestamp_value} : $cfg->{type} eq 'number' ? $form->format_amount($myconfig, $ref->{number_value} * 1, $cfg->{precision}) : $cfg->{type} eq 'customer' ? (SL::DB::Manager::Customer->find_by(id => 1*$ref->{number_value}) || SL::DB::Customer->new)->name + : $cfg->{type} eq 'vendor' ? (SL::DB::Manager::Vendor->find_by(id => 1*$ref->{number_value}) || SL::DB::Vendor->new)->name + : $cfg->{type} eq 'part' ? (SL::DB::Manager::Part->find_by(id => 1*$ref->{number_value}) || SL::DB::Part->new)->partnumber : $cfg->{type} eq 'bool' ? ($ref->{bool_value} ? $locale->text('Yes') : $locale->text('No')) : $ref->{text_value}; } diff --git a/SL/Template/Plugin/L.pm b/SL/Template/Plugin/L.pm index f7ffc11b2..0b4967d34 100644 --- a/SL/Template/Plugin/L.pm +++ b/SL/Template/Plugin/L.pm @@ -342,6 +342,36 @@ autocomplete_customer('#$name_e\_name'); JS } +# simple version with select_tag +sub vendor_selector { + my ($self, $name, $value, %params) = @_; + + my $actual_vendor_id = (defined $::form->{"$name"})? ((ref $::form->{"$name"}) ? $::form->{"$name"}->id : $::form->{"$name"}) : + (ref $value && $value->can('id')) ? $value->id : ''; + my $options_str = $self->options_for_select(SL::DB::Manager::Vendor->get_all(), + default => $actual_vendor_id, + title_sub => sub { $_[0]->vendornumber . " : " . $_[0]->name }, + 'with_empty' => 1); + + return $self->select_tag($name, $options_str, %params); +} + + +# simple version with select_tag +sub part_selector { + my ($self, $name, $value, %params) = @_; + + my $actual_part_id = (defined $::form->{"$name"})? ((ref $::form->{"$name"})? $::form->{"$name"}->id : $::form->{"$name"}) : + (ref $value && $value->can('id')) ? $value->id : ''; + my $options_str = $self->options_for_select(SL::DB::Manager::Part->get_all(), + default => $actual_part_id, + title_sub => sub { $_[0]->partnumber . " : " . $_[0]->description }, + 'with_empty' => 1); + + return $self->select_tag($name, $options_str, %params); +} + + sub javascript_tag { my $self = shift; my $code = ''; diff --git a/bin/mozilla/amcvar.pl b/bin/mozilla/amcvar.pl index 25e3ec738..343350a24 100644 --- a/bin/mozilla/amcvar.pl +++ b/bin/mozilla/amcvar.pl @@ -55,9 +55,11 @@ our %translations = ('text' => $locale->text('Free-form text'), 'bool' => $locale->text('Yes/No (Checkbox)'), 'select' => $locale->text('Selection'), 'customer' => $locale->text('Customer'), + 'vendor' => $locale->text('Vendor'), + 'part' => $locale->text('Part'), ); -our @types = qw(text textfield number date bool select customer); # timestamp +our @types = qw(text textfield number date bool select customer vendor part); # timestamp our @modules = ({ module => 'CT', description => $locale->text('Customers and vendors') }, { module => 'Contacts', description => $locale->text('Contact persons') }, diff --git a/templates/webpages/amcvar/render_inputs.html b/templates/webpages/amcvar/render_inputs.html index a69101bf0..dc9c09fe8 100644 --- a/templates/webpages/amcvar/render_inputs.html +++ b/templates/webpages/amcvar/render_inputs.html @@ -27,6 +27,12 @@ [%- ELSIF var.type == 'customer' %] [% L.customer_picker(var_name, var.value) %] +[%- ELSIF var.type == 'vendor' %] +[% L.vendor_selector(var_name, var.value) %] + +[%- ELSIF var.type == 'part' %] +[% L.part_selector(var_name, var.value) %] + [%- ELSIF var.type == 'select' %]