summaryrefslogtreecommitdiff
path: root/httemplate/edit/process/elements/ApplicationCommon.html
blob: c7bdd3ea2cb1e11a755ceadc4763dd2d339105e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<%doc>

Examples:

  #cust_bill_pay
  include('elements/ApplicationCommon.html',
    'error_redirect' => 'cust_bill_pay.cgi',
    'src_table'      => 'cust_pay',
    'src_thing'      => 'payment',
    'link_table'     => 'cust_bill_pay',
  )

  #cust_credit_bill
  include('elements/ApplicationCommon.html',
    'error_redirect' => 'cust_credit_bill.cgi',
    'src_table'      => 'cust_credit',
    'src_thing'      => 'credit',
    'link_table'     => 'cust_credit_bill',
  )

</%doc>
%if ( $error ) {
%  $cgi->param('error', $error);
<% $cgi->redirect(popurl(2). $opt{error_redirect}. '?'. $cgi->query_string ) %>
%} else {
<% header("$src_thing application$to sucessful") %>
  <SCRIPT TYPE="text/javascript">
    window.top.location.reload();
  </SCRIPT>
  </BODY>
  </HTML>
% } 
<%init>

my %opt = @_;

my $error = '';

my $src_thing = ucfirst($opt{'src_thing'});
my $src_table = $opt{'src_table'};
my $src_pkey = dbdef->table($src_table)->primary_key;

my $to = $opt{'link_table'} =~  /refund/ ? ' to Refund' : '';

$cgi->param($src_pkey) =~ /^(\d+)$/ or die "Illegal $src_pkey!";
my $src_pkeyvalue = $1;

my $src = qsearchs($src_table, { $src_pkey => $src_pkeyvalue } )
  or die "No such $src_pkey: $src_pkeyvalue";

my $cust_main = qsearchs('cust_main', { 'custnum' => $src->custnum } )
  or die "Bogus $src_thing: not attached to customer";

my $custnum = $cust_main->custnum;

my @subnames = grep { /.+/ } map { /^subnum(\d+)$/ ? $1 : '' } $cgi->param;
my @subitems = map { [ $cgi->param("subnum$_"), $cgi->param("subamount$_"), $cgi->param("taxXlocationnum$_") ] }
               @subnames;
{ local $^W = 0; @subitems = grep { $_->[1] + 0 } @subitems; }

my %options = ();
$options{subitems} = \@subitems if scalar(@subitems);

my $oldAutoCommit = $FS::UID::AutoCommit;
local $FS::UID::AutoCommit = 0;
my $dbh = dbh;
 
my $new;
#  $new = new FS::cust_refund ( {
#    'reason'  => 'Refunding payment', #enter reason in UI
#    'refund'  => $cgi->param('amount'),
#    'payby'   => 'BILL',
#    #'_date'   => $cgi->param('_date'),
#    'payinfo' => 'Cash', #enter payinfo in UI
#    'paynum' => $paynum,
#  } );
#} else {

  if ($src->amount != $cgi->param('src_amount')) {
    $src->amount($cgi->param('src_amount'));
    $error = $src->replace;
  }

  my $class = 'FS::'. $opt{link_table};

  $new = $class->new( {
    map {
      $_ => scalar($cgi->param($_));
    } fields($opt{link_table})
  } );

#}


$options{manual} = 1;
$error ||= $new->insert( %options );

if ($error) {
  $dbh->rollback if $oldAutoCommit;
} else {
  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
}
</%init>