scalar cgi param
[freeside.git] / httemplate / edit / process / elements / ApplicationCommon.html
1 <%doc>
2
3 Examples:
4
5   #cust_bill_pay
6   include('elements/ApplicationCommon.html',
7     'error_redirect' => 'cust_bill_pay.cgi',
8     'src_table'      => 'cust_pay',
9     'src_thing'      => 'payment',
10     'link_table'     => 'cust_bill_pay',
11   )
12
13   #cust_credit_bill
14   include('elements/ApplicationCommon.html',
15     'error_redirect' => 'cust_credit_bill.cgi',
16     'src_table'      => 'cust_credit',
17     'src_thing'      => 'credit',
18     'link_table'     => 'cust_credit_bill',
19   )
20
21 </%doc>
22 %if ( $error ) {
23 %  $cgi->param('error', $error);
24 %  my $query = $m->scomp('/elements/create_uri_query');
25 <% $cgi->redirect(popurl(2). $opt{error_redirect}. "?$query") %>
26 %} else {
27 <& /elements/header-popup.html, "$src_thing application$to sucessful" &>
28   <SCRIPT TYPE="text/javascript">
29     topreload();
30   </SCRIPT>
31   </BODY>
32   </HTML>
33 % } 
34 <%init>
35
36 my %opt = @_;
37
38 my $error = '';
39
40 my $src_thing = ucfirst($opt{'src_thing'});
41 my $src_table = $opt{'src_table'};
42 my $src_pkey = dbdef->table($src_table)->primary_key;
43
44 my $to = $opt{'link_table'} =~  /refund/ ? ' to Refund' : '';
45
46 $cgi->param($src_pkey) =~ /^(\d+)$/ or die "Illegal $src_pkey!";
47 my $src_pkeyvalue = $1;
48
49 my $src = qsearchs($src_table, { $src_pkey => $src_pkeyvalue } )
50   or die "No such $src_pkey: $src_pkeyvalue";
51
52 my $cust_main = qsearchs('cust_main', { 'custnum' => $src->custnum } )
53   or die "Bogus $src_thing: not attached to customer";
54
55 my $custnum = $cust_main->custnum;
56
57 my @subnames = grep { /.+/ } map { /^subnum(\d+)$/ ? $1 : '' } $cgi->param;
58 my @subitems = map { [ $cgi->param("subnum$_"), $cgi->param("subamount$_"), $cgi->param("taxXlocationnum$_") ] }
59                @subnames;
60 { local $^W = 0; @subitems = grep { $_->[1] + 0 } @subitems; }
61
62 my %options = ();
63 $options{subitems} = \@subitems if scalar(@subitems);
64
65 my $oldAutoCommit = $FS::UID::AutoCommit;
66 local $FS::UID::AutoCommit = 0;
67 my $dbh = dbh;
68  
69 my $new;
70 #  $new = new FS::cust_refund ( {
71 #    'reason'  => 'Refunding payment', #enter reason in UI
72 #    'refund'  => scalar($cgi->param('amount')),
73 #    'payby'   => 'BILL',
74 #    #'_date'   => scalar($cgi->param('_date')),
75 #    'payinfo' => 'Cash', #enter payinfo in UI
76 #    'paynum' => $paynum,
77 #  } );
78 #} else {
79
80   if ($src->amount != $cgi->param('src_amount')) {
81     $src->amount($cgi->param('src_amount'));
82     $error = $src->replace;
83   }
84
85   my $class = 'FS::'. $opt{link_table};
86
87   $new = $class->new( {
88     map {
89       $_ => scalar($cgi->param($_));
90     } fields($opt{link_table})
91   } );
92
93 #}
94
95
96 $options{manual} = 1;
97 $error ||= $new->insert( %options );
98
99 if ($error) {
100   $dbh->rollback if $oldAutoCommit;
101 } else {
102   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
103 }
104 </%init>