<?php include '../plugin/sms_send.php';

//Customer Registration
if(isset($_POST['postReg'])){ $ref = $_SERVER['HTTP_REFERER'];
$fixpass = hash('sha256','12345');
if($linkr->queryCount('customers',array('frm_phone'=>collect('eqPhone')))>0){
print "<script>swal({text:'Customer Previously Captured',type:'warning',title:'Duplicate Attempt'},function(){ window.location = '$ref';});</script>"; 
}else{ $letters = random(); $country =collect('pcountry'); $state =collect('pstate');
 $insert_into_reg =QueryDB("INSERT INTO customers VALUES ('','".collect('eqName')."','".collect('eqPhone')."','".collect('eqEmail')."','".date('m')."','".date('Y')."','".date('Y-m-d')."','".time()."','".$_SESSION['supr']."','".$_POST['eqSex']."','$letters','$country','$state','','".collect('eqAddress')."','".collect('prog_type')."','$my_branch_code','$fixpass') ");
// 	$receiver = trim($_POST['eqPhone`']);
// $postdata = http_build_query(array('username' => SMSPROVIDER_EMAIL,'password' => SMSPROVIDER_PASSWORD,'message'=>'Hi '.$name.' Form Payment of &#8358;'.$amt_pay.' received for '.get_course_name($_POST['eqCourse']).' Acknowleged. Thanks.','mobiles' => $receiver,'sender' => $sender,));
// $result = sms_provider(SMSPROVIDER_ENDPOINT , $postdata);
// $decode = json_decode($result);

	print "<script>swal({text:'Customer captured',type:'success',title:'Approved'},function(){ window.location = '$ref';});</script>"; 
}

}

//Edit 
if(isset($_POST['EditReg'])){ $ref = $_SERVER['HTTP_REFERER'];
QueryDB("UPDATE customers SET frm_name='".collect('eqName')."',frm_phone='".collect('eqPhone')."',frm_email='".collect('eqEmail')."',cus_type='".collect('prog_type')."',gender='".collect('eqSex')."',address='".collect('eqAddress')."' where id='".collect('where')."'  ");
	print "<script>swal({text:'Customer Details Updated',type:'success',title:'Approved'},function(){ window.location = '$ref';});</script>";  
}

$ref =@$_SERVER['HTTP_REFERER'];
//Add Size
if(isset($_POST['addSize'])){
if(in_array(strtolower(collect('Newsize')),$sized_by)){print "<script>swal({text:'Size Previously Exist',type:'warning',title:'Duplicate'},function(){ window.location = '$ref';});</script>";  	
}else{ $sized_by[] =strtoupper(collect('Newsize')); QueryDB("UPDATE specs SET sized_by ='".implode(',',$sized_by)."' ");print "<script>swal({text:'New Size Added',type:'success',title:'Approved'},function(){ window.location = '$ref';});</script>"; 
}	}

//Add Colour
if(isset($_POST['addColour'])){
if(in_array(strtoupper(collect('NewColour')),$colored_by)){print "<script>swal({text:'Colour Already Exist',type:'warning',title:'Duplicate'},function(){ window.location = '$ref';});</script>";  	
}else{ $colored_by[] =strtoupper(collect('NewColour')); QueryDB("UPDATE specs SET colored_by ='".implode(',',$colored_by)."' ");print "<script>swal({text:'New Colour Added',type:'success',title:'Approved'},function(){ window.location = '$ref';});</script>"; 
}	}

if(isset($_POST['addBrand'])){
if(in_array(strtoupper(collect('brandName')),$brand)){print "<script>swal({text:'Brand Already Exist',type:'warning',title:'Duplicate'},function(){ window.location = '$ref';});</script>";  	
}else{ $brand[] =strtoupper(collect('brandName')); QueryDB("UPDATE specs SET brand ='".implode(',',$brand)."' ");print "<script>swal({text:'New Brand Added Added',type:'success',title:'Approved'},function(){ window.location = '$ref';});</script>"; 
}	}


if(isset($_POST['addCompany'])){
if(in_array(strtoupper(collect('compName')),$company)){print "<script>swal({text:'Company Already Exist',type:'warning',title:'Duplicate'},function(){ window.location = '$ref';});</script>";  	
}else{ $company[] =strtoupper(collect('compName')); QueryDB("UPDATE specs SET company ='".implode(',',$company)."' ");print "<script>swal({text:'New Company Added Added',type:'success',title:'Approved'},function(){ window.location = '$ref';});</script>"; 
}	}


//Add a Product
if(isset($_POST['addProduct'])){
if($linkr->queryCount('products',array('size'=>collect('size'),'color'=>collect('color'),'brand'=>collect('brand'),'company'=>collect('company'),'item_cost'=>collect('cost'),'item_name'=>collect('prodname')))>0){
print "<script>swal({text:'Product Previouly Registered',type:'warning',title:'Error'},function(){ window.location = '$ref';});</script>"; 
}else{
	QueryDB("INSERT INTO products VALUES ('','".collect('prodname')."','".collect('cost')."','".collect('cost')."','','','".collect('color')."','".collect('size')."','".collect('brand')."','".collect('company')."','','".time()."','1','1','1','".$results['username']."','".date('Y-m-d')."') ");
print "<script>swal({text:'Product Added Successfully',type:'success',title:'Approved'},function(){ window.location = '$ref';});</script>"; 
}	}

//Edit Product
if(isset($_POST['EditProduct'])){
QueryDB("UPDATE products SET item_name ='".collect('prodname')."',company='".collect('company')."',brand='".collect('brand')."',size='".collect('size')."',color='".collect('color')."',tm_status='".collect('tm_status')."' where id='".collect('where')."' ");
print "<script>swal({text:'Product Updated',type:'success',title:'Approved'},function(){ window.location = '$ref';});</script>"; 
}





//Stock Up
if(isset($_POST['stockUp'])){
	QueryDB("INSERT INTO  storekeeper VALUES('','".collect('itemName')."','".collect('qty')."','".time()."','".date('Y-m-d')."','".$results['username']."','".date('m')."','".date('Y')."') ");
		print "<script>swal({text:'Product Loaded Successfully',type:'success',title:'Approved'},function(){ window.location = '$ref';});</script>"; 
}





if(isset($_POST['sendNew'])){ 

$paying = collect('paying'); $actual = collect('debt');
$mth = collect('pay_mth');$diff = $actual-$paying; 
$_SESSION['receipt_id'] =collect('whereRecpID');$id = collect('whereID');

$customer =collect('userCust');



if($paying>$actual){
print "<script>swal({text:'Payment cannot Exceed Debt',type:'warning',title:'Error'},function(){ window.location = '$ref';});</script>";
}else{

  if($diff>0){
    QueryDB("UPDATE payment SET amt_paid='$paying',pay_mth='$mth',out_standing='$diff',month='".date('m')."',year='".date('Y')."',date_paid='".date('Y-m-d')."',pay_status=1 where id='".$id."' ");




	
QueryDB("INSERT INTO `payment` (id,cus_id,amt,item,qty,s_price,bulk_price,amt_paid,amt_before,date_paid,pay_mth,out_standing,month,year,receipt_id,type,onboarded,pay_status,real_cost,discount,time_paid,view_status,day,branchcode)
 SELECT '',cus_id,amt,item,qty,s_price,bulk_price,'','$diff','','','$diff','','',receipt_id,type,onboarded,0,'$actual','','','0',day,branchcode FROM `payment` where id ='$id' ");






// $postdata = http_build_query(array('username' => SMSPROVIDER_EMAIL,'password' => SMSPROVIDER_PASSWORD,'message'=>'Hi '.$student.'('.$_SESSION['receipt_id'].') Payment of #'.$paying.' received for '.get_course_name($selected_item).' Balance:#'.$diff.' Thank you.','mobiles' => $receiver,'sender' => $sender,));
// $result = sms_provider(SMSPROVIDER_ENDPOINT , $postdata);
// $decode = json_decode($result);

    }else{
 QueryDB("UPDATE payment SET amt_paid='$paying',pay_mth='$mth',out_standing='$diff',month='".date('m')."',date_paid='".date('Y-m-d')."',year='".date('Y')."',pay_status=1 where id='$id' ");
//  $postdata = http_build_query(array('username' => SMSPROVIDER_EMAIL,'password' => SMSPROVIDER_PASSWORD,'message'=>'Hi '.$student.' ('.$_SESSION['receipt_id'].') Payment of #'.$paying.' received for '.get_course_name($selected_item).' Balance:#'.$diff.' Thank you.','mobiles' => $receiver,'sender' => $sender,));
// $result = sms_provider(SMSPROVIDER_ENDPOINT , $postdata);
// $decode = json_decode($result);
    }
print "<script>swal({text:'Payment was Successful',type:'success',title:'Success'},function(){ window.location = 'receeipts.php?no=".$_SESSION['receipt_id']."';});</script>";
}

}


//View Product Load History
if(isset($_GET['sel_product'])){
	$_SESSION['sel_product']=$_GET['sel_product'];
	header('location:?view_load_hstory');die();
}

if(isset($_GET['view_sales_history'])){
$_SESSION['sel_product']=$_GET['view_sales_history'];
	header('location:?salesHistory');die();	
}

//Add Staff
if(isset($_POST['adStf'])){
if($linkr->queryCount('login',array('username'=>collect('username')))>0){
print "<script>swal({text:'Username Previously Used',type:'warning',title:'Error'},function(){ window.location = '$ref';});</script>";
}else{
	QueryDB("INSERT INTO login VALUES('','".collect('username')."','".hash('sha256','12345')."','".collect('name')."','".collect('act_type')."','".time()."','".time()."','1','".collect('gender')."','')");
		print "<script>swal({text:'Account Created',type:'success',title:'Success'},function(){ window.location = '$ref';});</script>";
}
}

//edit Staff
if(isset($_POST['adStfedit'])){
QueryDB("UPDATE login set name='".collect('name')."',acct_status='".collect('status')."',sex='".collect('gender')."',position='".collect('act_type')."' where id ='".collect('where')."' ");
print "<script>swal({text:'Account Updated',type:'success',title:'Success'},function(){ window.location = '$ref';});</script>";
}

//view Customer Purchase
if(isset($_GET['view_selCus'])){
	$_SESSION['sel_cust']=$_GET['view_selCus'];
	header('location:stocks?view_cust_trans'); die();
}


//edit Loaded Quantity
if(isset($_POST['editLoaded'])){
	QueryDB("UPDATE storekeeper SET item_qty='".collect('Newsize')."' where id='".collect('where')."'  ");
	print "<script>swal({text:'Product Quantity Updated',type:'success',title:'Success'},function(){ window.location = '$ref';});</script>";
}

//Vhange



















if(isset($_POST['postEnq'])){ $ref = $_SERVER['HTTP_REFERER'];
	
$insert = QueryDB("INSERT INTO enquiry VALUES('','".$_POST['eqName']."','".$_POST['eqSex']."','".$_POST['eqCourse']."','".$_POST['eqNotes']."','".$_POST['eqPhone']."','".$_POST['eqEmail']."','".date('m')."','".date('Y')."','".date('Y-m-d')."',1,'','".$_SESSION['supr']."') ");
$receiver = trim($_POST['eqPhone']);
	$postdata = http_build_query(array('username' => SMSPROVIDER_EMAIL,'password' => SMSPROVIDER_PASSWORD,'message'=>'Hi  '.$_POST['eqName'].' thank you for your interest in learning Practical '.get_course_name($_POST['eqCourse']).'.We hope you start soon.Cheers to greater days ahead.','mobiles' => $receiver,'sender' => $sender,));
$result=sms_provider(SMSPROVIDER_ENDPOINT , $postdata);$decode = json_decode($result);
print "<script>swal({text:'Enquiry Submitted',type:'success',title:'Glory'},function(){ window.location = '$ref';});</script>"; 
}

//chnage passwod
if(isset($_POST['changePass'])){$ref = $_SERVER['HTTP_REFERER'];
	if(collect('newPass')==collect('newPass2')){
QueryDB("UPDATE login SET password='".hash('sha256', collect('newPass'))."' where username='{$_SESSION['supr']}' ");
print "<script>swal({text:'Password Changed Successfully',type:'success',title:'Done'},function(){ window.location = '$ref';});</script>"; 
}else{
print "<script>swal({text:'Password do not match',type:'warning',title:'Mismatch'},function(){ window.location = '$ref';});</script>"; 
}
}































































//Add New Course
if(isset($_POST['postReg_Add'])){ $ref = $_SERVER['HTTP_REFERER'];
$name = $_POST['eqName'];  
$mth =$_POST['pay_method'];
$crs =$_POST['eqCourse']; 
$amt_pay =$_POST['amt_paid'];

// $phone = $_POST['eqPhone']; $email = $_POST['eqEmail']; 
$type = $_POST['prog_type'];
$letters = $_POST['whereTo'];

$insert_into_payment = QueryDB("INSERT INTO payment VALUES('','$letters','$crs','$amt_pay','$amt_pay','$amt_pay','".date('Y-m-d')."','$mth','0','".date('m')."','".date('Y')."','$letters','reg','".$_SESSION['supr']."','1','$amt_pay','','','".time()."') ");
$receiver = trim($_POST['eqPhone`']);
$postdata = http_build_query(array('username' => SMSPROVIDER_EMAIL,'password' => SMSPROVIDER_PASSWORD,'message'=>'Hi '.$name.' Form Payment of &#8358;'.$amt_pay.' received for '.get_course_name($_POST['eqCourse']).' Acknowleged. Thanks.','mobiles' => $receiver,'sender' => $sender,));
$result = sms_provider(SMSPROVIDER_ENDPOINT , $postdata);
$decode = json_decode($result);
//send registration SMS and email where applicable to the persons
print "<script>swal({text:'New Form Purchase was Successful',type:'success',title:'$letters'},function(){ window.location = '$ref';});</script>"; 
}








//Fil Form
if(isset($_POST['sendForm'])){ //where
	$setup =QueryDB("UPDATE reg SET frm_name='".v($_POST['name'])."',frm_phone='".v($_POST['phone'])."',frm_email='".v($_POST['email'])."',gender='".v($_POST['sex'])."' where reg_id='".v($_POST['where'])."' ");

QueryDB("INSERT INTO fillup VALUES ('','".$_POST['where']."','".v($_POST['address'])."','".v($_POST['pro_session'])."','".v($_POST['m_status'])."','".($_POST['dob'])."','".v($_POST['hdegree'])."','".v($_POST['discipline'])."','".v($_POST['question1'])."','".v($_POST['reasons'])."','','','".v($_POST['mode'])."','".v($_POST['prog_exp'])."','','".$_POST['prog_type']."','".v($_POST['eqCourse'])."','".date('Y-m-d')."','".$_SESSION['supr']."','".date('m')."','".date('Y')."','') ");
print "<script>swal({text:'Student Record successfully Captured',type:'success',title:'Success'},function(){ window.location = 'manage_reg';});</script>"; 
}



if(isset($_POST['upDateForm'])){
//find Existence 
if($linkr->queryCount('fillup',array('ref_id'=>$_POST['where']))>0){
$setup =QueryDB("UPDATE reg SET frm_name='".v($_POST['name'])."',frm_phone='".v($_POST['phone'])."',frm_email='".v($_POST['email'])."',gender='".v($_POST['sex'])."' where reg_id='".v($_POST['where'])."' ");
QueryDB("UPDATE fillup SET address='".v($_POST['address'])."',prog_session='".v($_POST['pro_session'])."',mstatus='".v($_POST['m_status'])."',dob='".($_POST['dob'])."',highest_degree='".v($_POST['hdegree'])."',discipline='".v($_POST['discipline'])."',question1='".v($_POST['question1'])."',expressions='".v($_POST['reasons'])."',others='".v($_POST['mode'])."',prog_exp='".v($_POST['prog_exp'])."',doc='".v($_POST['doc'])."' where id='".$_SESSION['ref_id']."' ");

print "<script>swal({text:'Student Record Updated',type:'success',title:'Success'},function(){ window.location = 'manage_reg';});</script>"; 

}else{
print "<script>swal({text:'Record could not be Updated',type:'warning',title:'failed'},function(){ window.location = 'manage_reg';});</script>"; 
}
	

}





//Generate Report
if(isset($_GET['generate_report'])){
	$_SESSION['ref_id'] = $_GET['generate_report'];
	header('location:ecform'); die();
}

//Edit Form
// if(isset($_GET['editForm'])){
// 	$_SESSION['editForm'] = $_GET['editForm'];
// 	header('location:fillup'); die();
// }




//Process Payment

if(isset($_POST['send'])){ //$ref = $_SERVER['HTTP_REFERER'];
// include 'plugin/sms_send.php';
$paying = collect('paying');  
$realCost =collect('actual'); $diff = $realCost-$paying;

 $studentID = $_POST['where'];
 $formID = $_POST['reg_code'];

$selStd =get_std($studentID); 
$student=$selStd['frm_name'];
$receiver =$selStd['frm_phone'];

$enrollmentID = generateAdmnNo($_POST['rsn']);

if($diff>0){
QueryDB("INSERT INTO payment VALUES('','".$_POST['where']."','".$_POST['rsn']."','".$_POST['actual']."','".$_POST['paying']."','".$_POST['actual']."','".date('Y-m-d H:i:s')."','{$_POST['pay_mth']}','{$_POST['bal']}','".date('m')."','".date('Y')."','".$enrollmentID."','Fees','".$results['username']."','1','".$_POST['actualCost']."','".$_POST['discount']."','".$_POST['reg_code']."','".time()."') ");

 QueryDB("INSERT INTO payment VALUES('','".$_POST['where']."','".$_POST['rsn']."','".$_POST['actual']."','','".$_POST['bal']."','','','{$_POST['bal']}','','','".$enrollmentID."','Fees','".$results['username']."','0','".$_POST['actualCost']."','".$_POST['discount']."','".$_POST['reg_code']."','".time()."') ");

//generate form ID
QueryDB("UPDATE fillup SET enrollment_code='$enrollmentID' where id ='$formID' ");


  $_SESSION['receipt_id'] = $enrollmentID;
 $postdata = http_build_query(array('username' => SMSPROVIDER_EMAIL,'password' => SMSPROVIDER_PASSWORD,'message'=>'Hi '.$student.'('.$enrollmentID.') Payment of #'.$_POST['paying'].' received for '.get_course_name($_POST['rsn']).' Balance: #'.$diff.'  Thank you.','mobiles' => $receiver,'sender' => $sender,));
$result = sms_provider(SMSPROVIDER_ENDPOINT , $postdata);
$decode = json_decode($result);
// print_r($decode);

 print "<script>swal({text:'Payment was Successful',type:'success',title:'Success'},function(){ window.location = 'receeipts.php?no=".$_SESSION['receipt_id']."';});</script>"; 

}else if($diff==0){
QueryDB("INSERT INTO payment VALUES('','".$_POST['where']."','".$_POST['rsn']."','".$_POST['actual']."','".$_POST['paying']."','".$_POST['actual']."','".date('Y-m-d H:i:s')."','{$_POST['pay_mth']}','{$_POST['bal']}','".date('m')."','".date('Y')."','".$enrollmentID."','Fees','".$results['username']."','1','".$_POST['actualCost']."','".$_POST['discount']."','".$_POST['reg_code']."','".time()."') ");

QueryDB("UPDATE fillup SET enrollment_code='$enrollmentID' where id ='$formID' ");

  $_SESSION['receipt_id'] = $enrollmentID;
 $postdata = http_build_query(array('username' => SMSPROVIDER_EMAIL,'password' => SMSPROVIDER_PASSWORD,'message'=>'Hi '.$student.'('.$enrollmentID.') Payment of #'.$_POST['paying'].' received for '.get_course_name($_POST['rsn']).' Balance:#'.$diff.' Thank you.','mobiles' => $receiver,'sender' => $sender,));
$result = sms_provider(SMSPROVIDER_ENDPOINT , $postdata);
$decode = json_decode($result);

 print "<script>swal({text:'Payment was Successful',type:'success',title:'Success'},function(){ window.location = 'receeipts.php?no=".$_SESSION['receipt_id']."';});</script>"; 
}



}



//Debtors Payment
//make debtors Payment
if(isset($_POST['sendPay'])){ $paying = collect('paying'); $actual = collect('actual');
$mth = collect('pay_mth');
$formID = collect('formID');
$diff = $actual-$paying; $std =collect('std_id_number');
$selected_item = collect('my_item');
$acAmount = collect('acAmount'); $ref = $_SERVER['HTTP_REFERER'];


if($paying>$actual){
print "<script>swal({text:'Payment cannot Exceed Debt',type:'warning',title:'Error'},function(){ window.location = '$ref';});</script>";
}else{
    $selStd =get_std($_POST['std_id_number']); $student=$selStd['frm_name'];
$receiver =$selStd['frm_phone'];
$_SESSION['receipt_id'] =collect('receipt_id');
  if($diff>0){
    QueryDB("UPDATE payment SET amt_paid='$paying',pay_mth='$mth',out_standing='$diff',month='".date('m')."',year='".date('Y')."',pay_status=1 where id='".collect('where')."' ");

 QueryDB("INSERT INTO payment VALUES ('','$std','$selected_item','$acAmount','','$diff',
    '".date('Y-m-d H:i:s')."','".collect('pay_mth')."','$diff','".date('m')."','".date('Y')."','{$_SESSION['receipt_id']}','Fees','".$results['username']."','0','$acAmount','0','$formID','".time()."') ");

$postdata = http_build_query(array('username' => SMSPROVIDER_EMAIL,'password' => SMSPROVIDER_PASSWORD,'message'=>'Hi '.$student.'('.$_SESSION['receipt_id'].') Payment of #'.$paying.' received for '.get_course_name($selected_item).' Balance:#'.$diff.' Thank you.','mobiles' => $receiver,'sender' => $sender,));
$result = sms_provider(SMSPROVIDER_ENDPOINT , $postdata);
$decode = json_decode($result);

    }else{
 QueryDB("UPDATE payment SET amt_paid='$paying',pay_mth='$mth',out_standing='$diff',month='".date('m')."',date_paid='".date('Y-m-d H:i:s')."',year='".date('Y')."',pay_status=1 where id='".collect('where')."' ");
 $postdata = http_build_query(array('username' => SMSPROVIDER_EMAIL,'password' => SMSPROVIDER_PASSWORD,'message'=>'Hi '.$student.' ('.$_SESSION['receipt_id'].') Payment of #'.$paying.' received for '.get_course_name($selected_item).' Balance:#'.$diff.' Thank you.','mobiles' => $receiver,'sender' => $sender,));
$result = sms_provider(SMSPROVIDER_ENDPOINT , $postdata);
$decode = json_decode($result);
    }
print "<script>swal({text:'Payment was Successful',type:'success',title:'Success'},function(){ window.location = 'receeipts.php?no=".$_SESSION['receipt_id']."';});</script>";
}

}


//Purchase Item
if(isset($_POST['buyProduct'])){ 
$item = $_POST['item'];		$paying = collect('product_cost'); 
$realCost =collect('realCost'); $diff = $realCost-$paying; $code = code_generator(4);



if(is_numeric($_POST['userName'])){ 
	$selStd =get_std(get_std_reg_number($_POST['userName'])); 
	$name=get_std_reg_number($_POST['userName']);
	$student =$selStd['frm_name'];
}else{ $name = $_POST['userName'].'-'.$_POST['userPhone'];}

if(!empty($_POST['userPhone'])){ $receiver = $_POST['userPhone'];
}else{$receiver =$selStd['frm_phone'];}

$discount = 0;

$_SESSION['receipt_id'] = $code;
$ref = $_SERVER['HTTP_REFERER'];

if(is_numeric($_POST['userName'])){  //if Student
if($diff>0){
 QueryDB("INSERT INTO payment VALUES ('','$name','$item','$realCost','$paying','$realCost',
    '".date('Y-m-d H:i:s')."','".collect('pay_mth')."','$diff','".date('m')."','".date('Y')."','$code','Item','$staff_id','1','$realCost','$discount','','".time()."') ");

QueryDB("INSERT INTO payment VALUES ('','$name','$item','$realCost','','$diff',
    '','','$diff','','','$code','Item','".$_SESSION['supr']."','0','$realCost','$discount','','".time()."') ");

$postdata = http_build_query(array('username' => SMSPROVIDER_EMAIL,'password' => SMSPROVIDER_PASSWORD,'message'=>'Hi '.$student.' Payment of #'.$paying.' received for '.get_course_name($_POST['item']).' Balance:#'.$diff.' Thank you.','mobiles' => $receiver,'sender' => $sender,));
$result = sms_provider(SMSPROVIDER_ENDPOINT , $postdata);
$decode = json_decode($result);
}else{

QueryDB("INSERT INTO payment VALUES ('','$name','$item','$realCost','$paying','$realCost',
    '".date('Y-m-d H:i:s')."','".collect('pay_mth')."','$diff','".date('m')."','".date('Y')."','$code','Item','".$_SESSION['supr']."','1','$realCost','$discount','','".time()."') ");

$postdata = http_build_query(array('username' => SMSPROVIDER_EMAIL,'password' => SMSPROVIDER_PASSWORD,'message'=>'Hi '.$student.' Payment of #'.$paying.' received for '.get_course_name($_POST['item']).' Balance:#'.$diff.' Thank you.','mobiles' => $receiver,'sender' => $sender,));
$result = sms_provider(SMSPROVIDER_ENDPOINT , $postdata);
$decode = json_decode($result);
// print_r($decode);
print "<script>swal({text:'Payment was Successful',type:'success',title:'Success'},function(){ window.location = 'receeipts.php?no=".$_SESSION['receipt_id']."';});</script>";
}

}else{ //not a Student

if($linkr->queryCount('reg',array('frm_phone'=>$receiver))>0){ 
print "<script>swal({text:'Already a Student',type:'warning',title:'Error'},function(){ window.location = '$ref';});</script>";  
}else{
	QueryDB("INSERT INTO payment VALUES ('','$name','$item','$realCost','$paying','$realCost',
    '".date('Y-m-d H:i:s')."','".collect('pay_mth')."','$diff','".date('m')."','".date('Y')."','$code','Item','{$_SESSION['supr']}','1','$realCost','0','','".time()."') ");

	$postdata = http_build_query(array('username' => SMSPROVIDER_EMAIL,'password' => SMSPROVIDER_PASSWORD,'message'=>'Hi '.$name.' Payment of #'.$paying.' received for '.get_course_name($_POST['item']).' Balance:#'.$diff.' Thank you.','mobiles' => $receiver,'sender' => $sender,));
$result = sms_provider(SMSPROVIDER_ENDPOINT , $postdata);
$decode = json_decode($result);

	print "<script>swal({text:'Payment was Successful',type:'success',title:'Success'},function(){ window.location = 'receeipts.php?no=".$_SESSION['receipt_id']."';});</script>";
}

}


}





//courses
//Add Course
if(isset($_POST['add_crs'])){  $crsDesc = $_POST['crsDesc'];
    if(is_numeric($_POST['crsName'])){
if(empty($_POST['newCourseName'])){
QueryDB("UPDATE courses SET crs_price='".$_POST['crsCost']."',crs_duration='".$_POST['crsDuration']."',crs_desc='$crsDesc',reg_cost='".$_POST['regCost']."',sort_code='".$_POST['crsSort']."',certify='".collect('crsCert')."' where id='".$_POST['crsName']."' ");
}else{
QueryDB("UPDATE courses SET crs_title='".$_POST['newCourseName']."',crs_price='".$_POST['crsCost']."',crs_duration='".$_POST['crsDuration']."',crs_desc='$crsDesc',reg_cost='".$_POST['regCost']."',sort_code='".$_POST['crsSort']."',certify='".collect('crsCert')."' where id='".$_POST['crsName']."' ");
}
   print "<script>swal({text:'Course Details Updated',type:'success',title:'Success'},function(){ window.location = '';});</script>";  
    }else{
 QueryDB("INSERT INTO courses VALUES('','".$_POST['newCourseName']."','$crsDesc','".$_POST['crsCost']."','".$_POST['crsDuration']."','".$_POST['regCost']."','".$_POST['crsSort']."','1','0','".collect('crsCert')."') ");   
   print "<script>swal({text:'New Item Added',type:'success',title:'Success'},function(){ window.location = '';});</script>";      
    }

}

//Add Programme
if(isset($_POST['AddProg'])){
if($linkr->queryCount('category',array('type'=>collect('progName')))>0){
	print "<script>swal({text:'Programme Already Exist',type:'warning',title:'Duplicate Attempt'},function(){ window.location = '';});</script>";   
}else{
QueryDB("INSERT INTO category VALUES('','".collect('progName')."','".collect('progDesc')."','".date('Y-m-d')."','{$_SESSION['supr']}','".collect('discount')."',1) ");
	print "<script>swal({text:'Programme Added',type:'success',title:'Success'},function(){ window.location = '';});</script>";  
}
}

//Add Item
if(isset($_POST['AddItem'])){
if($linkr->queryCount('courses',array('crs_title'=>collect('item')))>0){
	print "<script>swal({text:'Programme previously Exist',type:'warning',title:'Duplicate Attempt'},function(){ window.location = '';});</script>";   
}else{
	QueryDB("INSERT INTO courses VALUES ('','".collect('item')."','".collect('progDesc')."','".collect('cost')."','','','','','1','') ");
print "<script>swal({text:'Item Added',type:'success',title:'success'},function(){ window.location = '';});</script>"; 

}

}

//Process
if(isset($_GET['change_stats'])){
	if($_GET['change_stats']==1){
		QueryDB("UPDATE courses SET status=2 where id ='".$_GET['setID']."' ");
	}else{
		QueryDB("UPDATE courses SET status=1 where id ='".$_GET['setID']."' ");
	}
	print "<script>swal({text:'Item Status Modified',type:'success',title:'success'},function(){ window.location = 'views?view_Items';});</script>"; 
}


//Process AFTECC REG
if(isset($_POST['postRegAftec'])){ $ref = $_SERVER['HTTP_REFERER'];
$name = $_POST['eqName'];  $mth =$_POST['pay_method'];
$crs =$_POST['eqCourse']; $amt_pay =$_POST['amt_paid'];
$phone = $_POST['eqPhone']; $email = $_POST['eqEmail']; $type = $_POST['prog_type'];
$state =collect('pstate'); $country = collect('pcountry');




$letters = random();

if($linkr->queryCount('reg',array('frm_phone'=>$phone))<1){
$insert_into_reg =QueryDB("INSERT INTO reg VALUES ('','$name','$phone','$email','".date('m')."','".date('Y')."','".date('Y-m-d')."','','".time()."','".$_SESSION['supr']."','".$_POST['eqSex']."','$letters','$country','$state','$type','') ");

$insert_into_payment = QueryDB("INSERT INTO payment VALUES('','$letters','$crs','$amt_pay','$amt_pay','$amt_pay','".date('Y-m-d')."','$mth','0','".date('m')."','".date('Y')."','$letters','reg','".$_SESSION['supr']."','1','$amt_pay','','','".time()."') ");
$receiver = trim($_POST['eqPhone`']);
$postdata = http_build_query(array('username' => SMSPROVIDER_EMAIL,'password' => SMSPROVIDER_PASSWORD,'message'=>'Hi '.$name.' Form Payment of &#8358;'.$amt_pay.' received for '.get_course_name($_POST['eqCourse']).' Acknowleged. Thanks.','mobiles' => $receiver,'sender' => $sender,));
$result = sms_provider(SMSPROVIDER_ENDPOINT , $postdata);
$decode = json_decode($result);
//send registration SMS and email where applicable to the persons
print "<script>swal({text:'AFTECC Form Purchase was Successful',type:'success',title:'$letters'},function(){ window.location = '$ref';});</script>"; 
}else{
	print "<script>swal({text:'Already a Student',type:'warning',title:'Duplicate Attempt'},function(){ window.location = '$ref';});</script>"; 
}


}




//
if(isset($_GET['admn_no_set'])){
	$_SESSION['admn_no_set'] =$_GET['admn_no_set'];
	$_SESSION['ref_id_set'] = $_GET['ref_id_set'];
	$_SESSION['selected_crs'] =$_GET['crs_link'];
	$link = courseLink($_GET['crs_link']);
	header('location:../cert/'.$link);die();
}



if(isset($_POST['Change_sel_state'])){ $ref = $_SERVER['HTTP_REFERER'];
QueryDB("UPDATE partners SET status='".$_POST['pchs_type']."',super_appr='".date('Y-m-d')."' where id='".$_POST['where']."' ");
	print "<script>swal({text:'Status Changed',type:'success',title:'Approved Attempt'},function(){ window.location = '$ref';});</script>"; 
}



?>