c7bdd3ea2cb1e11a755ceadc4763dd2d339105e9
[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 <% $cgi->redirect(popurl(2). $opt{error_redirect}. '?'. $cgi->query_string ) %>
25 %} else {
26 <% header("$src_thing application$to sucessful") %>
27   <SCRIPT TYPE="text/javascript">
28     window.top.location.reload();
29   </SCRIPT>
30   </BODY>
31   </HTML>
32 % } 
33 <%init>
34
35 my %opt = @_;
36
37 my $error = '';
38
39 my $src_thing = ucfirst($opt{'src_thing'});
40 my $src_table = $opt{'src_table'};
41 my $src_pkey = dbdef->table($src_table)->primary_key;
42
43 my $to = $opt{'link_table'} =~  /refund/ ? ' to Refund' : '';
44
45 $cgi->param($src_pkey) =~ /^(\d+)$/ or die "Illegal $src_pkey!";
46 my $src_pkeyvalue = $1;
47
48 my $src = qsearchs($src_table, { $src_pkey => $src_pkeyvalue } )
49   or die "No such $src_pkey: $src_pkeyvalue";
50
51 my $cust_main = qsearchs('cust_main', { 'custnum' => $src->custnum } )
52   or die "Bogus $src_thing: not attached to customer";
53
54 my $custnum = $cust_main->custnum;
55
56 my @subnames = grep { /.+/ } map { /^subnum(\d+)$/ ? $1 : '' } $cgi->param;
57 my @subitems = map { [ $cgi->param("subnum$_"), $cgi->param("subamount$_"), $cgi->param("taxXlocationnum$_") ] }
58                @subnames;
59 { local $^W = 0; @subitems = grep { $_->[1] + 0 } @subitems; }
60
61 my %options = ();
62 $options{subitems} = \@subitems if scalar(@subitems);
63
64 my $oldAutoCommit = $FS::UID::AutoCommit;
65 local $FS::UID::AutoCommit = 0;
66 my $dbh = dbh;
67  
68 my $new;
69 #  $new = new FS::cust_refund ( {
70 #    'reason'  => 'Refunding payment', #enter reason in UI
71 #    'refund'  => $cgi->param('amount'),
72 #    'payby'   => 'BILL',
73 #    #'_date'   => $cgi->param('_date'),
74 #    'payinfo' => 'Cash', #enter payinfo in UI
75 #    'paynum' => $paynum,
76 #  } );
77 #} else {
78
79   if ($src->amount != $cgi->param('src_amount')) {
80     $src->amount($cgi->param('src_amount'));
81     $error = $src->replace;
82   }
83
84   my $class = 'FS::'. $opt{link_table};
85
86   $new = $class->new( {
87     map {
88       $_ => scalar($cgi->param($_));
89     } fields($opt{link_table})
90   } );
91
92 #}
93
94
95 $options{manual} = 1;
96 $error ||= $new->insert( %options );
97
98 if ($error) {
99   $dbh->rollback if $oldAutoCommit;
100 } else {
101   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
102 }
103 </%init>