#!/usr/bin/perl

#use utf8;
use strict;
use CGI;
use Opals::Template_ajax qw(
    tmpl_read
    tmpl_write
    tmpl_preference
);
use Opals::Context;
use Opals::Constant;
use Opals::Locale qw(
    loc_getMsgFile
    loc_write
);

use Opals::Ebook qw(
    eb_saveLicense
);

my $dbh = Opals::Context->dbh();
END { $dbh->disconnect(); }

my $cgi = CGI->new;
my $input = $cgi->Vars();
my ($permission, $cookie, $template) = tmpl_read(
        {
            dbh             => $dbh,
            cgi             => $cgi,
            tmplFile        => 'ajax/util/savePurchaseInfo.tmpl',
            reqPermission   => 'ebook_mgmt',
        }
);

my $uid       = $ENV{'curUserId'};
my $bid       = $input->{'bid'};
if ($permission && $permission->{'ebook_mgmt'}) {
   if($bid){
       my $licId       = $input->{'licId'};
       my $licenseType = ($input->{'licenseType'}) || 'paid';
    my $licInfo={
                bid             => $bid,
                licId           => $licId,
                licenseType     => $licenseType,
                noLicense       => $input->{'noLicense'},
                datePurchase    => $input->{'purchaseDate'},
                poNum           => $input->{'poNum'},
                vendor          => $input->{'vendor'},
                startDate       => $input->{'startDate'},
                endDate         => $input->{'endDate'},
                note            => $input->{'note'}
        };

        #my $rs      = wsc_updateEbookLicense($licInfo);                         
        $licId =eb_saveLicense($dbh,$uid,$licInfo);
        $template->param(
                licId           =>$licId,
                bid             => $bid,
                licenseType     => $licenseType,
                noLicense       => $input->{'noLicense'},
                datePurchase    => $input->{'purchaseDate'},
                poNum           => $input->{'poNum'},
                vendor          => $input->{'vendor'},
                startDate       => $input->{'startDate'},
                endDate         => $input->{'endDate'},
                note            => $input->{'note'}
   
                );
    }

}

#-------------------------------------------------------------
#Thu, Jan 07, 2010 @ 13:50:35 EST
my $msgValMap ={};
my $fileMsgMap            =loc_getMsgFile('user/userInfo.msg',$msgValMap);

loc_write($template,$fileMsgMap);
        
tmpl_write($dbh, $cgi, $cookie, $template);


