#!/usr/bin/perl

#use utf8;
use strict;
use JSON;
use CGI;
use Digest::SHA qw(
    sha1_base64
    sha1_hex
);

use Opals::Context;
use Opals::Template qw(
    tmpl_read
    tmpl_write
    tmpl_preference
);

use Opals::Transaction qw(
    trans_adjustCharge
    trans_getCharge
);


my $dbh = Opals::Context->dbh();
END { $dbh->disconnect(); }
my $input=getFine();
my $cgi    = CGI->new;

my ($errCode, $ck, $resp) = Opals::User::user_currentUser($dbh, $cgi);
my  $respUid =$resp->{'uid'};

my $charge =$input->{'charge'};
my $note   =$input->{'note'};
my $newCharge=trans_getCharge($dbh,$charge->{'cid'});
foreach my $type(qw(waive fee rate unit chargeAmnt)){
    $newCharge->{$type}= $charge->{$type};
}
my $newCharge = trans_adjustCharge($dbh,$respUid,$newCharge,$note);
my $status=(defined $newCharge)?1:0;
print "Content-type: text/plain\n\n";

my $rs={status=>$status,charge=>$newCharge};
print   to_json($rs);


#-------------------------------------------------------------------------------
sub getFine{
  my $fineList=[];
  my $in={};
  if ($ENV{'REQUEST_METHOD'} eq "POST") {
        my $json ="";
        while (<STDIN>) {
            $json .= $_;
        }
        $in = decode_json($json);
   }
   return $in;
}
#------------------------------------------------------------------------------
