From 36a1e9c1661fb552d368f2f675dcb0793d733748 Mon Sep 17 00:00:00 2001 From: Ivan Kohler Date: Sun, 23 Sep 2012 18:07:47 -0700 Subject: [PATCH] export host selection per service, RT#17914 --- FS/FS/Mason.pm | 1 + FS/FS/Schema.pm | 3 +- FS/FS/svc_Common.pm | 54 +++++++++++++++++++--- FS/FS/svc_export_machine.pm | 19 ++++++-- httemplate/edit/process/svc_acct.cgi | 27 ++++++++--- httemplate/edit/svc_acct.cgi | 6 +++ httemplate/elements/tr-svc_export_machine.html | 37 +++++++++++++++ .../view/elements/tr-svc_export_machine.html | 27 +++++++++++ httemplate/view/svc_acct/basics.html | 5 ++ 9 files changed, 161 insertions(+), 18 deletions(-) create mode 100644 httemplate/elements/tr-svc_export_machine.html create mode 100644 httemplate/view/elements/tr-svc_export_machine.html diff --git a/FS/FS/Mason.pm b/FS/FS/Mason.pm index 39c7dfdc6..11af25efa 100644 --- a/FS/FS/Mason.pm +++ b/FS/FS/Mason.pm @@ -325,6 +325,7 @@ if ( -e $addl_handler_use_file ) { use FS::cust_tax_exempt_pkg_void; use FS::cust_bill_pkg_discount_void; use FS::agent_pkg_class; + use FS::svc_export_machine; # Sammath Naur if ( $FS::Mason::addl_handler_use ) { diff --git a/FS/FS/Schema.pm b/FS/FS/Schema.pm index 6e3956a83..7be8c664f 100644 --- a/FS/FS/Schema.pm +++ b/FS/FS/Schema.pm @@ -1894,10 +1894,11 @@ sub tables_hashref { 'columns' => [ 'svcexportmachinenum', 'serial', '', '', '', '', 'svcnum', 'int', '', '', '', '', + 'exportnum', 'int', '', '', '', '', 'machinenum', 'int', '', '', '', '', ], 'primary_key' => 'svcexportmachinenum', - 'unique' => [], + 'unique' => [ ['svcnum', 'exportnum'] ], 'index' => [], }, diff --git a/FS/FS/svc_Common.pm b/FS/FS/svc_Common.pm index a6daf44c8..7aede54a6 100644 --- a/FS/FS/svc_Common.pm +++ b/FS/FS/svc_Common.pm @@ -200,12 +200,13 @@ I. If I is set to an array reference, the jobnums of any export jobs will be added to the referenced array. -If I is set to an array reference of FS::tablename objects (for -example, FS::acct_snarf objects), they will have their svcnum field set and -will be inserted after this record, but before any exports are run. Each -element of the array can also optionally be a two-element array reference -containing the child object and the name of an alternate field to be filled in -with the newly-inserted svcnum, for example C<[ $svc_forward, 'srcsvc' ]> +If I is set to an array reference of FS::tablename objects +(for example, FS::svc_export_machine or FS::acct_snarf objects), they +will have their svcnum field set and will be inserted after this record, +but before any exports are run. Each element of the array can also +optionally be a two-element array reference containing the child object +and the name of an alternate field to be filled in with the newly-inserted +svcnum, for example C<[ $svc_forward, 'srcsvc' ]> If I is set (to a scalar jobnum or an array reference of jobnums), all provisioning jobs will have a dependancy on the supplied @@ -439,7 +440,16 @@ sub expire { Replaces OLD_RECORD with this one. If there is an error, returns the error, otherwise returns false. -Currently available options are: I and I. +Currently available options are: I, I and +I. + +If I is set to an array reference of FS::tablename objects +(for example, FS::svc_export_machine or FS::acct_snarf objects), they +will have their svcnum field set and will be inserted or replaced after +this record, but before any exports are run. Each element of the array +can also optionally be a two-element array reference containing the +child object and the name of an alternate field to be filled in with +the newly-inserted svcnum, for example C<[ $svc_forward, 'srcsvc' ]> If I is set (to a scalar jobnum or an array reference of jobnums), all provisioning jobs will have a dependancy on the supplied @@ -462,6 +472,8 @@ sub replace { ? shift : { @_ }; + my $objects = $options->{'child_objects'} || []; + my @jobnums = (); local $FS::queue::jobnums = \@jobnums; warn "[$me] replace: set \$FS::queue::jobnums to $FS::queue::jobnums\n" @@ -511,6 +523,34 @@ sub replace { return $error; } + foreach my $object ( @$objects ) { + my($field, $obj); + if ( ref($object) eq 'ARRAY' ) { + ($obj, $field) = @$object; + } else { + $obj = $object; + $field = 'svcnum'; + } + $obj->$field($new->svcnum); + + my $oldobj = qsearchs( $obj->table, { + $field => $new->svcnum, + map { $_ => $obj->$_ } $obj->_svc_child_partfields, + }); + + if ( $oldobj ) { + my $pkey = $oldobj->primary_key; + $obj->$pkey($oldobj->$pkey); + $obj->replace($oldobj); + } else { + $error = $obj->insert; + } + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return $error; + } + } + #new-style exports! unless ( $noexport_hack ) { diff --git a/FS/FS/svc_export_machine.pm b/FS/FS/svc_export_machine.pm index 39629d8af..10f7b6821 100644 --- a/FS/FS/svc_export_machine.pm +++ b/FS/FS/svc_export_machine.pm @@ -2,10 +2,13 @@ package FS::svc_export_machine; use strict; use base qw( FS::Record ); -use FS::Record; # qw( qsearch qsearchs ); +use FS::Record qw( qsearchs ); #qsearch ); use FS::cust_svc; +use FS::part_export; use FS::part_export_machine; +sub _svc_child_partfields { ('exportnum') }; + =head1 NAME FS::svc_export_machine - Object methods for svc_export_machine records @@ -89,14 +92,24 @@ sub check { my $error = $self->ut_numbern('svcexportmachinenum') - || $self->ut_foreign_key('svcnum', 'cust_svc', 'svcnum') - || $self->ut_foreign_key('machinenum', 'part_export_machine', 'machinenum' ) + || $self->ut_foreign_key('svcnum', 'cust_svc', 'svcnum' ) + || $self->ut_foreign_key('exportnum', 'part_export', 'exportnum' ) + || $self->ut_foreign_key('machinenum', 'part_export_machine', 'machinenum') ; return $error if $error; $self->SUPER::check; } +=item part_export_machine + +=cut + +sub part_export_machine { + my $self = shift; + qsearchs('part_export_machine', { 'machinenum' => $self->machinenum } ); +} + =back =head1 BUGS diff --git a/httemplate/edit/process/svc_acct.cgi b/httemplate/edit/process/svc_acct.cgi index a7d5136fb..41aca65ee 100755 --- a/httemplate/edit/process/svc_acct.cgi +++ b/httemplate/edit/process/svc_acct.cgi @@ -56,13 +56,14 @@ my $new = new FS::svc_acct ( \%hash ); my $error = ''; +my $part_svc = $svcnum ? + $old->part_svc : + qsearchs( 'part_svc', + { 'svcpart' => $cgi->param('svcpart') } + ); + # google captcha auth if ( $cgi->param('captcha_response') ) { - my $part_svc = $svcnum ? - $old->part_svc : - qsearchs( 'part_svc', - { 'svcpart' => $cgi->param('svcpart') } - ); my ($export) = $part_svc->part_export('acct_google'); if ( $export and ! $export->captcha_auth($cgi->param('captcha_response')) ) { @@ -79,6 +80,18 @@ if ( $cgi->param('clear_password') eq '*HIDDEN*' } if ( ! $error ) { + + my $export_info = FS::part_export::export_info(); + + my @svc_export_machine = + map FS::svc_export_machine->new({ + 'svcnum' => $svcnum, + 'exportnum' => $_->exportnum, + 'machinenum' => scalar($cgi->param('exportnum'.$_->exportnum.'machinenum')), + }), + grep { $_->machine eq '_SVC_MACHINE' } + $part_svc->part_export; + if ( $svcnum ) { foreach ( grep { $old->$_ != $new->$_ } qw( seconds upbytes downbytes totalbytes ) @@ -92,9 +105,9 @@ if ( ! $error ) { $error ||= $new->set_usage(\%hash); #unoverlimit and trigger radius changes last; #once is enough } - $error ||= $new->replace($old); + $error ||= $new->replace($old, 'child_objects'=>\@svc_export_machine); } else { - $error ||= $new->insert; + $error ||= $new->insert('child_objects'=>\@svc_export_machine); $svcnum = $new->svcnum; } } diff --git a/httemplate/edit/svc_acct.cgi b/httemplate/edit/svc_acct.cgi index 38567ef67..142c11150 100755 --- a/httemplate/edit/svc_acct.cgi +++ b/httemplate/edit/svc_acct.cgi @@ -173,6 +173,12 @@ function randomPass() { %} +<& /elements/tr-svc_export_machine.html, + 'svc' => $svc_acct, + 'part_svc' => $part_svc, + 'cgi' => $cgi, +&> + % #uid/gid % foreach my $xid (qw( uid gid )) { % diff --git a/httemplate/elements/tr-svc_export_machine.html b/httemplate/elements/tr-svc_export_machine.html new file mode 100644 index 000000000..92b6ac1d7 --- /dev/null +++ b/httemplate/elements/tr-svc_export_machine.html @@ -0,0 +1,37 @@ +% foreach my $part_export (@part_export) { +% my $label = ( $part_export->exportname +% ? $part_export->exportname +% : $part_export->label +% ). +% ' hostname'; +% +% my $element = 'exportnum'. $part_export->exportnum. 'machinenum'; +% my $machinenum = $opt{cgi}->param($element); +% if ( ! $machinenum && $opt{svc}->svcnum ) { +% my $svc_export_machine = qsearchs('svc_export_machine', { +% 'svcnum' => $opt{svc}->svcnum, +% 'exportnum' => $part_export->exportnum, +% }); +% $machinenum = $svc_export_machine->machinenum if $svc_export_machine; +% } + + <& /elements/tr-select-table.html, + 'label' => $label, + 'element_name' => 'exportnum'. $part_export->exportnum. 'machinenum', + 'table' => 'part_export_machine', + 'name_col' => 'machine', + 'hashref' => { 'exportnum' => $part_export->exportnum, + 'disabled' => '', + }, + 'curr_value' => $machinenum, + 'empty_label' => 'Select export hostname', + &> +% } +<%init> + +my %opt = @_; + +my @part_export = grep { $_->machine eq '_SVC_MACHINE' } + $opt{part_svc}->part_export; + + diff --git a/httemplate/view/elements/tr-svc_export_machine.html b/httemplate/view/elements/tr-svc_export_machine.html new file mode 100644 index 000000000..1ba8d74a1 --- /dev/null +++ b/httemplate/view/elements/tr-svc_export_machine.html @@ -0,0 +1,27 @@ +% foreach my $part_export (@part_export) { +% my $label = ( $part_export->exportname +% ? $part_export->exportname +% : $part_export->label +% ). +% ' hostname'; +% +% my $svc_export_machine = qsearchs('svc_export_machine', { +% 'svcnum' => $opt{svc}->svcnum, +% 'exportnum' => $part_export->exportnum, +% }); + + <& tr.html, + 'label' => $label, + 'value' => $svc_export_machine + ? $svc_export_machine->part_export_machine->machine + : '', + &> +% } +<%init> + +my %opt = @_; + +my @part_export = grep { $_->machine eq '_SVC_MACHINE' } + $opt{part_svc}->part_export; + + diff --git a/httemplate/view/svc_acct/basics.html b/httemplate/view/svc_acct/basics.html index bcd84696e..1cdf77615 100644 --- a/httemplate/view/svc_acct/basics.html +++ b/httemplate/view/svc_acct/basics.html @@ -56,6 +56,11 @@ &> % } +<& /view/elements/tr-svc_export_machine.html, + 'svc' => $svc_acct, + 'part_svc' => $part_svc, +&> + % if ($svc_acct->uid ne '') { <& /view/elements/tr.html, label=>mt('UID'), value=>$svc_acct->uid &> % } -- 2.11.0