X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=httemplate%2Fedit%2Fprocess%2Fquick-charge.cgi;h=b28d225308fa294542c5769057f9aef8e724d692;hp=38f06e1e92777fb0e9d687332d5e71bf1f72d48d;hb=25efd7207d9ea9be4bea1093917c0d8dc4b1e87a;hpb=b5dbd9e1bcbb701a20ed23e723b1e0105fd7c1a1 diff --git a/httemplate/edit/process/quick-charge.cgi b/httemplate/edit/process/quick-charge.cgi index 38f06e1e9..b28d22530 100644 --- a/httemplate/edit/process/quick-charge.cgi +++ b/httemplate/edit/process/quick-charge.cgi @@ -1,17 +1,9 @@ -% if ( $error ) { -% $cgi->param('error', $error ); -<% $cgi->redirect($p.'quick-charge.html?'. $cgi->query_string) %> -% } else { -<% header(emt("One-time charge added")) %> - - -% } +<% $cgi->redirect(@redirect) %> <%init> +my $curuser = $FS::CurrentUser::CurrentUser; die "access denied" - unless $FS::CurrentUser::CurrentUser->access_right('One-time charge'); + unless $curuser->access_right('One-time charge'); my $error = ''; my $conf = new FS::conf; @@ -23,53 +15,157 @@ for ( my $row = 0; exists($param->{"description$row"}); $row++ ) { if ($param->{"description$row"} =~ /\S/); } -$param->{"custnum"} =~ /^(\d+)$/ - or $error .= "Illegal customer number " . $param->{"custnum"} . " "; -my $custnum = $1; +my( $cust_main, $prospect_main, $quotation ) = ( '', '', '' ); +if ( $cgi->param('quotationnum') =~ /^(\d+)$/ ) { + $quotation = FS::quotation->by_key($1) or die "quotationnum $1 not found"; +} +if ( $param->{"custnum"} =~ /^(\d+)$/ ) { + $cust_main = FS::cust_main->by_key($1) or die "custnum $1 not found"; + exists($curuser->agentnums_href->{$cust_main->agentnum}) + or die "access denied"; +} +if ( $param->{"prospectnum"} =~ /^(\d+)$/ ) { + $prospect_main = FS::prospect_main->by_key($1) or die "prospectnum $1 not found"; + exists($curuser->agentnums_href->{$prospect_main->agentnum}) + or die "access denied"; +} -$param->{"amount"} =~ /^\s*(\d*(?:\.?\d{1,2}))\s*$/ - or $error .= "Illegal amount " . $param->{"amount"} . " "; -my $amount = $1; +my $message; -my $quantity = 1; -if ( $cgi->param('quantity') =~ /^\s*(\d+)\s*$/ ) { - $quantity = $1; -} +if ( $param->{'pkgnum'} =~ /^(\d+)$/ ) { #modifying an existing one-time charge + $message = "One-time charge changed"; + my $pkgnum = $1; + die "access denied" + unless $curuser->access_right('Modify one-time charge'); -$param->{'tax_override'} =~ /^\s*([,\d]*)\s*$/ - or $error .= "Illegal tax override " . $param->{"tax_override"} . " "; -my $override = $1; + my $cust_pkg = FS::cust_pkg->by_key($1) + or die "pkgnum $pkgnum not found"; -if ( $param->{'taxclass'} eq '(select)' ) { - $error .= "Must select a tax class. " - unless ($conf->exists('enable_taxproducts') && - ( $override || $param->{taxproductnum} ) - ); - $cgi->param('taxclass', ''); -} + my $part_pkg = $cust_pkg->part_pkg; + die "pkgnum $pkgnum is not a one-time charge" unless $part_pkg->freq eq '0'; + + my ($amount, $setup_cost, $quantity); + if ( $cgi->param('amount') =~ /^\s*(\d*(\.\d{1,2})*)\s*$/ ) { + $amount = sprintf('%.2f', $1); + } + if ( $cgi->param('setup_cost') =~ /^\s*(\d*(\.\d{1,2})*)\s*$/ ) { + $setup_cost = sprintf('%.2f', $1); + } + if ( $cgi->param('quantity') =~ /^\s*(\d*)\s*$/ ) { + $quantity = $1 || 1; + } + + my $start_date = $cgi->param('start_date') + ? parse_datetime($cgi->param('start_date')) + : time; + + $param->{'tax_override'} =~ /^\s*([,\d]*)\s*$/ + or $error .= "Illegal tax override " . $param->{"tax_override"} . " "; + my $override = $1; + + if ( $param->{'taxclass'} eq '(select)' ) { + $error .= "Must select a tax class. " + unless ($conf->config('tax_data_vendor') && + ( $override || $param->{taxproductnum} ) + ); + $cgi->param('taxclass', ''); + } + + $error = $cust_pkg->modify_charge( + 'pkg' => scalar($cgi->param('pkg')), + 'classnum' => scalar($cgi->param('classnum')), + 'additional' => \@description, + 'adjust_commission' => ($cgi->param('adjust_commission') ? 1 : 0), + 'amount' => $amount, + 'setup_cost' => $setup_cost, + 'setuptax' => scalar($cgi->param('setuptax')), + 'taxclass' => scalar($cgi->param('taxclass')), + 'taxproductnum' => scalar($cgi->param('taxproductnum')), + 'tax_override' => $override, + 'quantity' => $quantity, + 'start_date' => $start_date, + 'separate_bill' => scalar($cgi->param('separate_bill')), + ); + +} else { # the usual case: new one-time charge + + $message = "One-time charge added"; -unless ( $error ) { - my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } ) - or $error .= "Unknown customer number $custnum. "; + $param->{"amount"} =~ /^\s*(\d*(?:\.?\d{1,2}))\s*$/ + or $error .= "Illegal amount " . $param->{"amount"} . " "; + my $amount = $1; - $error ||= $cust_main->charge( { + my $setup_cost = ''; + if ( $param->{setup_cost} =~ /\S/ ) { + $param->{setup_cost} =~ /^\s*(\d*(?:\.?\d{1,2}))\s*$/ + or $error .= "Illegal setup_cost " . $param->{setup_cost} . " "; + $setup_cost = $1; + } + + my $quantity = 1; + if ( $cgi->param('quantity') =~ /^\s*(\d+)\s*$/ ) { + $quantity = $1; + } + + $param->{'tax_override'} =~ /^\s*([,\d]*)\s*$/ + or $error .= "Illegal tax override " . $param->{"tax_override"} . " "; + my $override = $1; + + if ( $param->{'taxclass'} eq '(select)' ) { + $error .= "Must select a tax class. " + unless ($conf->config('tax_data_vendor') && + ( $override || $param->{taxproductnum} ) + ); + $cgi->param('taxclass', ''); + } + + my %charge = ( 'amount' => $amount, + 'setup_cost' => $setup_cost, 'quantity' => $quantity, - 'bill_now' => scalar($cgi->param('bill_now')), - 'invoice_terms' => scalar($cgi->param('invoice_terms')), 'start_date' => ( scalar($cgi->param('start_date')) ? parse_datetime($cgi->param('start_date')) : '' ), - 'no_auto' => scalar($cgi->param('no_auto')), - 'pkg' => scalar($cgi->param('pkg')), - 'setuptax' => scalar($cgi->param('setuptax')), - 'taxclass' => scalar($cgi->param('taxclass')), - 'taxproductnum' => scalar($cgi->param('taxproductnum')), 'tax_override' => $override, - 'classnum' => scalar($cgi->param('classnum')), 'additional' => \@description, - } ); + + map { $_ => scalar($cgi->param($_)), } qw( + bill_now invoice_terms no_auto separate_bill pkg + setuptax taxclass taxproductnum classnum + setup_discountnum setup_discountnum_amount setup_discountnum_percent + ) + ); + + if ( $quotation ) { + $error ||= $quotation->charge( \%charge ); + } else { + $error ||= $cust_main->charge( \%charge ); + } + +} + +my @redirect = (); +if ( $error ) { + $cgi->param('error', $error ); + @redirect = ( $p.'quick-charge.html?'. $cgi->query_string ); +} elsif ( $quotation ) { + @redirect = ( + -uri => $fsurl.'view/quotation.html?' . $quotation->quotationnum, + -cookie => CGI::Cookie->new( -name => 'freeside_status', + -value => mt('One-time charge added to quotation'), + -expires => '+5m', + ), + ); +} else { + @redirect = ( + -uri => $fsurl.'view/cust_main.cgi?custnum='. $cust_main->custnum. + ';show=last', + -cookie => CGI::Cookie->new( -name => 'freeside_status', + -value => mt('One-time charge ordered'), + -expires => '+5m', + ), + ); }