<?php
$PageSecurity = 11;
include('includes/SQL_CommonFunctions.inc');
include('includes/function_lib.php');
include('includes/session.inc');
$code=0;
error_reporting(0);
switch ($_REQUEST['form_type']) {
    case 'adjust_leave':
        if(!empty($_REQUEST['id']) && !empty($_REQUEST['adj_type']) && !empty($_REQUEST['adj_val']) && !empty($_REQUEST['location']) && !empty($_REQUEST['emp_id']) ){
            $data['location']=trim(mysqli_real_escape_string($db, $_REQUEST['location']));
            $data['emp_id']=trim(mysqli_real_escape_string($db, $_REQUEST['emp_id']));
            $data['emp_leave_id']=trim(mysqli_real_escape_string($db, $_REQUEST['id']));
            $data['adjustment_type']=trim(mysqli_real_escape_string($db, $_REQUEST['adj_type']));
            $data['adjustment']=trim(mysqli_real_escape_string($db, $_REQUEST['adj_val']));
            $data['leave_name']=trim(mysqli_real_escape_string($db, $_REQUEST['emp_leave_name']));
            $data['old_leave']=trim(mysqli_real_escape_string($db, $_REQUEST['emp_leave']));
            $data['created_by']=$_SESSION['UserID'];
            $data['created_date']=date('Y-m-d H:I:s');
            $ErrMsg='';
            $s->RecordInsert('emp_leave_adjustment', $data);
            $code=DB_Last_Insert_ID($db,'emp_leave_adjustment',$ErrMsg);
            $qry='UPDATE `emp_leave` SET leave_days=leave_days'.$data['adjustment_type'].$data['adjustment'].' WHERE id='.trim($_REQUEST['id']);
            DB_query($qry, $db);
            if(!empty($code)){
                $outputdata['code']=$code;
            } else {
                $code='0';
            }
        } else {
            $code='0';
        }
    break;
    case 'leave_mail':
        require_once('mailerfunction/phpmailer/class.phpmailer.php');
        include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
        $mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
        $mail->IsSMTP(); // telling the class to use SMTP
        
        try {
            $mail->SMTPDebug  = 0;                     // enables SMTP debug information (for testing)
            $mail->SMTPAuth   = true;                  // enable SMTP authentication
            $mail->Host       = "mail.marketing.i2k2.com"; // sets the SMTP server
            $mail->Port       = 25;
            $mail->Username   = "authsasl@marketing.i2k2.com"; // SMTP account username
            $mail->Password   = "MtaMyNetwork?1024?";        // SMTP account password
            $mail->Mailer       = "smtp";
            //$mail->AddCC('gautam@acgil.com', 'Dharmendra');
            $mail->AddReplyTo('noreply@aeh.com','AEH');
            $mail->AddAddress('manish8881@rediffmail.com ','Manish');
            $mail->SetFrom('noreply@aeh.com','AEH');
            $mail->Subject = 'Leave Adjustment '.date('d-m-Y');
            $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';   // optional - MsgHTML will create an alternate automatically
            //$mail->MsgHTML(file_get_contents('contents.html'));
            $mail->MsgHTML(get_mail_content($db));
            if($mail->Send()){
                $code='1';
                $qry='UPDATE `emp_leave_adjustment` SET status="1" WHERE status="0"';
                DB_query($qry, $db);                 
            } else {
                 $code='0';
            }
        } catch (phpmailerException $e) {
            $code='0';
        } catch (Exception $e) {
            $code='0';
        }
        //print_r($dataArray);
    break;
}

$outputdata['code']=$code;
echo json_encode($outputdata);

function get_mail_content($db){
    ob_start();
    $qry='SELECT loc.locationname,emp.emp_name,adj.leave_name,adj.old_leave,adj.adjustment_type,adj.adjustment,usr.realname,adj.created_date
FROM `emp_leave_adjustment` AS adj
INNER JOIN `locations` AS loc ON(loc.loccode=adj.location)
INNER JOIN `empmaster` AS emp ON(emp.id=adj.emp_id)
INNER JOIN `www_users` AS usr ON(usr.id=adj.created_by)
WHERE adj.status=0';
    $result=DB_query($qry, $db);
    ?>
    <h1 style="font-size: 20px; line-height: 34px; text-align:center;">Employees leave Adjustment Details</h1>
    <?php if(DB_num_rows($result)>0){ ?>
    <table width="100%" border="1" cellpadding="3" cellspacing="0">
        <tr>
            <td style="font-size:12px; background: #b7b7b7;"><strong>Location:</strong></td>
            <td style="font-size:12px; background: #b7b7b7;"><strong>Employee:</strong></td>
            <td style="font-size:12px; background: #b7b7b7;"><strong>Leave name:</strong></td>
            <td style="font-size:12px; background: #b7b7b7; text-align:center;"><strong>Old Leave:</strong></td>
            <td style="font-size:12px; background: #b7b7b7; text-align:center;"><strong>Adjustment:</strong></td>
            <td style="font-size:12px; background: #b7b7b7;"><strong>Adjustment By:</strong></td>
            <td style="font-size:12px; background: #b7b7b7; text-align:center;"><strong>Adjustment Date:</strong></td>
        </tr>
    <?php while($myrow=DB_fetch_array($result)){ ?>
        <tr>
            <td style="font-size:12px;"><?php echo $myrow['locationname']; ?></td>
            <td style="font-size:12px;"><?php echo $myrow['emp_name']; ?></td>
            <td style="font-size:12px;"><?php echo $myrow['leave_name']; ?></td>
            <td style="font-size:12px; text-align:right;"><?php echo $myrow['old_leave']; ?></td>
            <td style="font-size:12px; text-align:right;"><?php echo $myrow['adjustment_type'].$myrow['adjustment']; ?></td>
            <td style="font-size:12px;"><?php echo $myrow['realname']; ?></td>
            <td style="font-size:12px; text-align:center;"><?php echo $myrow['created_date']; ?></td>
        </tr>
     <?php } ?>
    </table>
    <p align="left" style="font-size: 10px;">You have received this mail from Ayodhya HMIS.</p>
    <?php         
    }
    $content =ob_get_contents();
    ob_end_clean();
    return $content;
}
die;