From 8c39c41a00e15164ea306bfd73b2ae187dade9a5 Mon Sep 17 00:00:00 2001 From: Ivan Kohler Date: Mon, 9 Dec 2013 23:37:20 -0800 Subject: [PATCH] on-the-fly alarm vendor / type / CS adding, RT#23694 --- FS/FS/svc_Common.pm | 14 ++-- FS/FS/svc_alarm.pm | 71 ++++++++++++++++--- httemplate/elements/select-alarm_station.html | 7 +- httemplate/elements/select-alarm_system.html | 15 ++-- httemplate/elements/select-alarm_type.html | 7 +- httemplate/elements/tr-select-alarm_station.html | 21 +++--- httemplate/elements/tr-select-alarm_system.html | 21 +++--- httemplate/elements/tr-select-alarm_type.html | 21 +++--- httemplate/elements/tr-select-table.html | 87 +++++++++++++++++++++++- 9 files changed, 196 insertions(+), 68 deletions(-) diff --git a/FS/FS/svc_Common.pm b/FS/FS/svc_Common.pm index 3993d3d64..56567e8ce 100644 --- a/FS/FS/svc_Common.pm +++ b/FS/FS/svc_Common.pm @@ -335,6 +335,7 @@ sub preinsert_hook_first { ''; } sub _check_duplcate { ''; } sub preinsert_hook { ''; } sub table_dupcheck_fields { (); } +sub prereplace_hook { ''; } sub predelete_hook { ''; } sub predelete_hook_first { ''; } @@ -472,15 +473,10 @@ sub replace { local $FS::UID::AutoCommit = 0; my $dbh = dbh; - my $error = $new->set_auto_inventory($old); - if ( $error ) { - $dbh->rollback if $oldAutoCommit; - return $error; - } - - #redundant, but so any duplicate fields are maniuplated as appropriate - # (svc_phone.phonenum) - $error = $new->check; + my $error = $new->prereplace_hook_first($old) + || $new->set_auto_inventory($old) + || $new->check; #redundant, but so any duplicate fields are + #maniuplated as appropriate (svc_phone.phonenum) if ( $error ) { $dbh->rollback if $oldAutoCommit; return $error; diff --git a/FS/FS/svc_alarm.pm b/FS/FS/svc_alarm.pm index 3e910f0bf..4a355c7c1 100644 --- a/FS/FS/svc_alarm.pm +++ b/FS/FS/svc_alarm.pm @@ -99,12 +99,15 @@ sub table_info { ; { - 'name' => 'Alarm service', - 'sorts' => 'acctnum', - 'display_weight' => 80, - 'cancel_weight' => 85, - 'fields' => \%fields, - + 'name' => 'Alarm service', + 'sorts' => 'acctnum', + 'display_weight' => 80, + 'cancel_weight' => 85, + 'fields' => \%fields, + 'addl_process_fields' => [qw( alarmsystemnum_systemname + alarmtypenum_inputs alarmtypenum_outputs + alarmstationnum_stationname + )], }; } @@ -134,6 +137,50 @@ Delete this record from the database. Replaces the OLD_RECORD with this one in the database. If there is an error, returns the error, otherwise returns false. +=cut + +sub preinsert_hook_first { shift->_inline_add(@_); } +sub prereplace_hook_first { shift->_inline_add(@_); } + +sub _inline_add { + my $self = shift; + + my $agentnum = $self->cust_svc->cust_pkg->cust_main->agentnum; + + if ( $self->alarmsystemnum == -1 ) { + my $alarm_system = new FS::alarm_system { + 'agentnum' => $agentnum, + 'systemname' => $self->alarmsystemnum_systemname, + }; + my $error = $alarm_system->insert; + return $error if $error; + $self->alarmsystemnum($alarm_system->alarmsystemnum); + } + + if ( $self->alarmtypenum == -1 ) { + my $alarm_type = new FS::alarm_type { + 'agentnum' => $agentnum, + 'inputs' => $self->alarmtypenum_inputs, + 'outputs' => $self->alarmtypenum_outputs, + }; + my $error = $alarm_type->insert; + return $error if $error; + $self->alarmtypenum($alarm_type->alarmtypenum); + } + + if ( $self->alarmstationnum == -1 ) { + my $alarm_station = new FS::alarm_station { + 'agentnum' => $agentnum, + 'stationname' => $self->alarmstationnum_stationname, + }; + my $error = $alarm_station->insert; + return $error if $error; + $self->alarmstationnum($alarm_station->alarmstationnum) + } + + ''; +} + =item check Checks all fields to make sure this is a valid service. If there is @@ -162,9 +209,15 @@ sub check { $self->SUPER::check; } -sub alarm_system { qsearchs('alarm_system', {systemnum =>shift->systemnum } ) } -sub alarm_type { qsearchs('alarm_type', {typenum =>shift->systemnum } ) } -sub alarm_station { qsearchs('alarm_station',{stationnum=>shift->stationnum} ) } +sub alarm_system { + qsearchs('alarm_system', { alarmsystemnum => shift->alarmsystemnum } ); +} +sub alarm_type { + qsearchs('alarm_type', { alarmtypenum => shift->alarmtypenum } ); +} +sub alarm_station { + qsearchs('alarm_station', { alarmstationnum => shift->alarmstationnum } ); +} =back diff --git a/httemplate/elements/select-alarm_station.html b/httemplate/elements/select-alarm_station.html index 04d1383b0..2bae7b15b 100644 --- a/httemplate/elements/select-alarm_station.html +++ b/httemplate/elements/select-alarm_station.html @@ -1,7 +1,8 @@ -<% include( '/elements/select-table.html', +<& /elements/select-table.html, 'table' => 'alarm_station', 'name_col' => 'stationname', 'empty_label' => 'Select central station', + 'agent_virt' => 1, + 'agent_null' => 1, @_, - ) -%> +&> diff --git a/httemplate/elements/select-alarm_system.html b/httemplate/elements/select-alarm_system.html index 7f22fcefa..6bde51874 100644 --- a/httemplate/elements/select-alarm_system.html +++ b/httemplate/elements/select-alarm_system.html @@ -1,7 +1,8 @@ -<% include( '/elements/select-table.html', - 'table' => 'alarm_system', - 'name_col' => 'systemname', - 'empty_label' => 'Select vendor', - @_, - ) -%> +<& /elements/select-table.html, + table => 'alarm_system', + name_col => 'systemname', + empty_label => 'Select vendor', + 'agent_virt' => 1, + 'agent_null' => 1, + @_, +&> diff --git a/httemplate/elements/select-alarm_type.html b/httemplate/elements/select-alarm_type.html index 5f15988b0..fc1f98696 100644 --- a/httemplate/elements/select-alarm_type.html +++ b/httemplate/elements/select-alarm_type.html @@ -1,8 +1,9 @@ -<% include( '/elements/select-table.html', +<& /elements/select-table.html, 'table' => 'alarm_type', 'name_col' => 'typename', 'order_by' => 'ORDER BY inputs, outputs', 'empty_label' => 'Select type', + 'agent_virt' => 1, + 'agent_null' => 1, @_, - ) -%> +&> diff --git a/httemplate/elements/tr-select-alarm_station.html b/httemplate/elements/tr-select-alarm_station.html index 71ed0db0c..919282264 100644 --- a/httemplate/elements/tr-select-alarm_station.html +++ b/httemplate/elements/tr-select-alarm_station.html @@ -1,12 +1,9 @@ -% #if ( scalar(@domains) < 2 ) { -% #} else { - - <% $opt{'label'} || 'Central Station' %> - - <% include( '/elements/select-alarm_station.html', %opt) %> - - -% #} -<%init> - my %opt = @_; - +<& /elements/tr-select-table.html, + label => 'Central Station', + table => 'alarm_station', + #name_col => 'stationname', + add_inline => 1, + add_fields => [ 'stationname' ], + add_right => [ 'Alarm configuration', 'Alarm global configuration' ], + @_, +&> diff --git a/httemplate/elements/tr-select-alarm_system.html b/httemplate/elements/tr-select-alarm_system.html index a46a015f2..e1b2ae329 100644 --- a/httemplate/elements/tr-select-alarm_system.html +++ b/httemplate/elements/tr-select-alarm_system.html @@ -1,12 +1,9 @@ -% #if ( scalar(@domains) < 2 ) { -% #} else { - - <% $opt{'label'} || 'Vendor' %> - - <% include( '/elements/select-alarm_system.html', %opt) %> - - -% #} -<%init> - my %opt = @_; - +<& /elements/tr-select-table.html, + label => 'Vendor', + table => 'alarm_system', + #name_col => 'systemname', + add_inline => 1, + add_fields => [ 'systemname' ], + add_right => [ 'Alarm configuration', 'Alarm global configuration' ], + @_, +&> diff --git a/httemplate/elements/tr-select-alarm_type.html b/httemplate/elements/tr-select-alarm_type.html index b929fb8e6..1fc93555e 100644 --- a/httemplate/elements/tr-select-alarm_type.html +++ b/httemplate/elements/tr-select-alarm_type.html @@ -1,12 +1,9 @@ -% #if ( scalar(@domains) < 2 ) { -% #} else { - - <% $opt{'label'} || 'Type' %> - - <% include( '/elements/select-alarm_type.html', %opt) %> - - -% #} -<%init> - my %opt = @_; - +<& /elements/tr-select-table.html, + label => 'Type', + table => 'alarm_type', + #name_col => + add_inline => 1, + add_fields => [ 'inputs', 'outputs' ], + add_right => [ 'Alarm configuration', 'Alarm global configuration' ], + @_, +&> diff --git a/httemplate/elements/tr-select-table.html b/httemplate/elements/tr-select-table.html index 6ac748782..2e62d8717 100644 --- a/httemplate/elements/tr-select-table.html +++ b/httemplate/elements/tr-select-table.html @@ -1,3 +1,16 @@ +<%doc> + +Example: + + <& /elements/tr-select-table, + + 'add_inline' => 0, #enable "on the fly" adding + + #options passed to tr-td-label.html and select-$table.html (or + #select-table.html if that does not exist) + &> + + % unless ( $opt{'js_only'} ) { <% include('tr-td-label.html', @_ ) %> @@ -5,16 +18,88 @@ > % } - <% include( '/elements/select-table.html', %opt ) %> +% my $element = '/elements/select-'.$opt{table}.'.html'; +% if ( $m->interp->comp_exists($element) ) { + <& $element, %opt &> +% } else { + <& /elements/select-table.html, %opt &> +% } % unless ( $opt{'js_only'} ) { % } + +% if ( $opt{'add_inline'} ) { + +% foreach my $add_field ( @{ $opt{'add_fields'} } ) { +% my $label = scalar(@{ $opt{'add_fields'} }) > 1 +% ? $add_field : $opt{'label'}; + + <& /elements/tr-input-text.html, + 'label' => "New $label", + 'field' => $field."_$add_field", + 'id' => $field."_$add_field", + 'colspan' => $opt{'colspan'}, + &> + +% } + + +% unless ( $opt{'html_only'} ) { + + + +% } + +% } + <%init> my( %opt ) = @_; my $style = $opt{'cell_style'} ? 'STYLE="'. $opt{'cell_style'}. '"' : ''; +my $curuser = $FS::CurrentUser::CurrentUser; + +my $field = ''; +if ( $opt{'add_inline'} && $curuser->access_right($opt{'add_right'}) ) { + + push @{ $opt{'post_options'} }, '-1' => 'Add new '. lc($opt{'label'}); + + $field = $opt{'field'} || dbdef->table($opt{'table'})->primary_key; + + my $onchange = ( $opt{'onchange'} ? delete($opt{'onchange'}).';' : '' ). + $field.'_changed(this);'; + $opt{'onchange'} = $onchange; + +} + -- 2.11.0