<?php
session_start();
include("../include/config.php");
include("../include/functions.php"); 
include("../include/burq.php");
validate_admin();

if($_REQUEST['submitForm']=='yes' && $_REQUEST['request_type']=='create'){
	
	$request_type=mysqli_real_escape_string($GLOBALS['conn'],$_REQUEST['request_type']);
  $order_id=mysqli_real_escape_string($GLOBALS['conn'],$_REQUEST['order_id']);
  $burq_store=$_POST['stid'];
	$burq_store=array_unique($burq_store);

	$burq_order=$obj->query("select ship_full_address,ship_fname,ship_lname,ship_mobile,delivery_note,ship_type from tbl_order where id='$order_id'");
$burq_order_rslt=$obj->fetchNextObject($burq_order);
if($burq_order_rslt->ship_type=='Home Delivery'){
	$orderprefix="QD";
}else{
	$orderprefix="QP";
}
if($burq_order_rslt->ship_full_address==''){
	$ship_address=$burq_order_rslt->ship_house_no.' '.$burq_order_rslt->ship_complex.' '.$burq_order_rslt->ship_street.', '.$burq_order_rslt->ship_city_id.', '.$burq_order_rslt->ship_state.', USA';
}else{
	if($burq_order_rslt->ship_complex){
    $ship_address=$burq_order_rslt->ship_complex.' '.$burq_order_rslt->ship_full_address;
}else{
$ship_address=$burq_order_rslt->ship_full_address;
}
}
for($x=0; $x<count($burq_store); $x++){
    $burq_itemarr=array();
    $storeItem_query=$obj->query("select product_name,qty from tbl_order_itmes 
        where 
        order_id='$order_id' and storeid='$burq_store[$x]'");

while($storeitem_result=mysqli_fetch_assoc($storeItem_query)){

$burq_itemarr['size']="medium";
$burq_itemarr['name']=$storeitem_result['product_name'];
$burq_itemarr['quantity']=$storeitem_result['qty'];

$itemarray[]=$burq_itemarr;
}

$burq_store_detailQry=$obj->query("select storename,storeaddress,phone from stores where storeid='$burq_store[$x]'");
$burq_store_detailQry_rslt=$obj->fetchNextObject($burq_store_detailQry);

$item_list=$itemarray;
$pickup=$burq_store_detailQry_rslt->storeaddress;
$dropoff=$ship_address;
$d_name=$burq_order_rslt->ship_fname." ".$burq_order_rslt->ship_lname;
$d_phone=$burq_order_rslt->ship_mobile;
$d_notes=$burq_order_rslt->delivery_note;
$p_name=$burq_store_detailQry_rslt->storename;
$p_phone=$burq_store_detailQry_rslt->phone;
$p_notes="Pick up with care";
$tip="0";
$order_desc="Food Items";
$order_id=$orderprefix.$order_id;
  
$burq_response=create_delivery($pickup,$dropoff,$item_list,$d_name,$d_phone,$d_notes,$p_name,$p_phone,$p_notes,$tip,$order_desc,$order_id);
$burq_response=json_decode($burq_response, true);
//print_r($burq_response);
$burq_identifier = $burq_response['id'];
$err=$burq_response['message'];
if($burq_identifier){
	$obj->query("update tbl_order_itmes set ups_tracking_no='$burq_identifier', courier='BURQUP' where storeid='$burq_store[$x]' and order_id='".$_REQUEST['order_id']."'");
//Assign to driver
$initiate_response=initiate_delivery($burq_identifier);
$initiate_response=json_decode($initiate_response, true);
$err=$initiate_response['message'];
$_SESSION['sess_msg']="Order assigned to BURQUP successfully.!";
}else{
  $burq_error="Create : ".$err;
  $obj->query("update tbl_order_itmes set ups_tracking_no='$burq_error', courier='BURQUP Failed' where storeid='$burq_store[$x]' and order_id='$order_id'");
  $_SESSION['sess_msg']="Order not assigned to BURQUP due to ".$err;
}
}

header("location:order-list.php");
	exit;
}
elseif($_REQUEST['submitForm']=='yes' && $_REQUEST['request_type']=='cancel')
{
	$request_type=mysqli_real_escape_string($GLOBALS['conn'],$_REQUEST['request_type']);
  $order_id=mysqli_real_escape_string($GLOBALS['conn'],$_REQUEST['order_id']);
  $burq_store=$_POST['cstid'];
	$burq_store=array_unique($burq_store);

for($x=0; $x<count($burq_store); $x++){
   $store_query=$obj->query("select ups_tracking_no from tbl_order_itmes where storeid='$burq_store[$x]' and ups_tracking_no!='' and courier='burqup' and order_id='$order_id'");
   $rslt_store=$obj->fetchNextObject($store_query);
   $identifier=$rslt_store->ups_tracking_no;

$burq_response=cancel_delivery($identifier);
$burq_response=json_decode($burq_response, true);
$err=$burq_response['message'];
if($err){
  }else{
  $obj->query("update tbl_order_itmes set courier='BURQUP Cancel' where storeid='$burq_store[$x]' and order_id='$order_id'");
}
}
if(!$err){
	$_SESSION['sess_msg']="Order canceled for BURQUP.!";
}else{
	$_SESSION['sess_msg']="Order not canceled to BURQUP due to ".$err;
}
header("location:order-list.php");
	exit;
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
	<title><?php echo SITE_TITLE; ?></title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<link href="css/admin.css" rel="stylesheet" type="text/css" />
	<script type="text/javascript">

function validate(obj){

if(obj.order_status.value=='')	{

alert("Please select status");	

obj.order_status.focus();

return false;	

}

if(obj.payment_status.value==1){

if(obj.pay_via.value!='' && obj.pay_via.value=='Credit/Debit Card'){

	if(obj.transaction_id.value=='')	{

	alert("Please enter transaction id");	

    obj.transaction_id.focus();

    return false;	

	}
} 
if(obj.pay_amount.value==''){

	alert("Please enter received amount");	

    obj.pay_amount.focus();

    return false;	

	}
}}

</script>
	<script type="text/javascript">

function showFields(val){

	if(val==1){

	document.getElementById('pay_details').style.display='';	

	}else{

	document.getElementById('pay_details').style.display='none';	

	}

	

	}



</script>
	<script type="text/javascript">

function showCardDetail(val){

	if(val==1){

	document.getElementById('card_details').style.display='';	

	}else{

	document.getElementById('card_details').style.display='none';	

	}

	

	}



</script>
	<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
	<link rel="stylesheet" href="calender/css/jquery-ui.css">
	<script src="calender/js/jquery-ui.js"></script>
	<script>
	$(document).ready(function () {
	$("#create").show();
	$("#cancel").hide();
});

function burqSelection(selectionValue){
if(selectionValue == 'create'){
	$("#create").show();
	$("#cancel").hide();
}
if(selectionValue == 'cancel'){
	$("#create").hide();
	$("#cancel").show();
} 
}
</script>
	</head>

	<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
    <td align="left" valign="middle" class="headingbg bodr text14"><em><img src="images/arrow2.gif" width="21" height="21" hspace="10" align="absmiddle" /></em>Admin: Assign to BURQUP
          
          On Order ID : <?php echo $_REQUEST['order_id']; ?></td>
  </tr>
      <tr>
    <td height="100" align="left" valign="top" bgcolor="#f7faf9" class="bodr">
    	<form name="frm" method="POST" enctype="multipart/form-data" action="" onSubmit="return validate(this)">
        <input type="hidden" name="submitForm" value="yes" />
        <input type="hidden" name="order_id" value="<?php echo $_REQUEST['order_id'];?>" />
        <table width="100%" cellpadding="0" cellspacing="0">
          <tr>
            <td align="center" colspan="2" style="color:#C00;"><?php echo $_SESSION['sess_msg']; $_SESSION['sess_msg']=''; ?></td>
          </tr>
          <tr><br>
            <td width="33%" align="right" class="paddBot11 paddRt14"><strong>Request Type</strong></td>
            <td width="67%" align="left" class="paddBot11"><select name="request_type" onchange="burqSelection(this.value)" style="width:240px;" >
                <option value="create" selected>Create</option>
                <option value="cancel">Cancel</option>
              </select></td>
          </tr>
          <tr>
          	<table id="create" width="100%">
          		<tr>
          <?php $sql_section=$obj->query("select section from tbl_order_itmes where order_id='".$_REQUEST['order_id']."' and courier!='burqup' and ups_tracking_no='' group by section");
          if($obj->numRows($sql_section)){
          while($rslt_section=$obj->fetchNextObject($sql_section)){ ?>
          <tr>
            <td width="33%" align="right" class="paddBot11 paddRt14"><br><strong><span style="font-size: 14px; color: #000"><?php echo ucwords($rslt_section->section); ?></strong></td>
            <td width="67%" align="left" class="paddBot11"></td>
          </tr>
          <?php $sql_store=$obj->query("select ot.storeid,storename from tbl_order_itmes ot join stores s on s.storeid=ot.storeid where order_id='".$_REQUEST['order_id']."' and courier!='burqup' and ups_tracking_no='' and section='$rslt_section->section' group by storeid");
          while($rslt_store=$obj->fetchNextObject($sql_store)){ ?>
          <tr>
            <td width="33%" align="right" class="paddBot11 paddRt14"><strong><?php echo ucwords($rslt_store->storename); ?></strong></td>
            <td width="67%" align="left" class="paddBot11"><input type="checkbox" checked name="stid[]" value="<?php echo $rslt_store->storeid; ?>"></td>
          </tr>
          <?php } } ?>
          <tr>
            <td align="right" class="paddRt14 paddBot11">&nbsp;</td>
            <td align="left" class="paddBot11">&nbsp;</td>
          </tr>
          <tr>
            <td width="33%" align="right" class="paddRt14 paddBot11">&nbsp;</td>
            <td width="67%" align="left" class="paddBot11"><input type="submit" name="submit" value="Send to Burqup"  class="submit" border="0" />
              &nbsp;&nbsp;&nbsp;&nbsp;</td>
          </tr>
        <?php }else{ echo "<td width='100%' align='center' style='color:red'><strong>Nothing to create</strong></td>"; }?>
        </tr>
      </table>
       <table id="cancel"  width="100%">
          		<tr>
          <?php $sql_section=$obj->query("select section from tbl_order_itmes where order_id='".$_REQUEST['order_id']."' and courier='burqup' and ups_tracking_no!='' group by section");
          if($obj->numRows($sql_section)){
          while($rslt_section=$obj->fetchNextObject($sql_section)){ ?>
          <tr>
            <td width="33%" align="right" class="paddBot11 paddRt14"><br><strong><span style="font-size: 14px; color: #000"><?php echo ucwords($rslt_section->section); ?></strong></td>
            <td width="67%" align="left" class="paddBot11"></td>
          </tr>
          <?php $sql_store=$obj->query("select ot.storeid,storename from tbl_order_itmes ot join stores s on s.storeid=ot.storeid where order_id='".$_REQUEST['order_id']."' and courier='burqup' and ups_tracking_no!='' and section='$rslt_section->section' group by storeid");
          while($rslt_store=$obj->fetchNextObject($sql_store)){ ?>
          <tr>
            <td width="33%" align="right" class="paddBot11 paddRt14"><strong><?php echo ucwords($rslt_store->storename); ?></strong></td>
            <td width="67%" align="left" class="paddBot11"><input type="checkbox" checked name="cstid[]" value="<?php echo $rslt_store->storeid; ?>"></td>
          </tr>
          <?php } } ?>
          <tr>
            <td align="right" class="paddRt14 paddBot11">&nbsp;</td>
            <td align="left" class="paddBot11">&nbsp;</td>
          </tr>
          <tr>
            <td width="33%" align="right" class="paddRt14 paddBot11">&nbsp;</td>
            <td width="67%" align="left" class="paddBot11"><input type="submit" name="submit" value="Cancel for Burqup"  class="submit" border="0" />
              &nbsp;&nbsp;&nbsp;&nbsp;</td>
          </tr>
        <?php }else{ echo "<td width='100%' align='center' style='color:red'><strong>Nothing to Cancel</strong></td>"; }?>
        </tr>
      </table>
        </table>
      </form></td>
  </tr>
      <tr>
    <td align="center"></td>
  </tr>
      <?php $commentArr=$obj->query("select * from $tbl_order_comments where order_id='".$_REQUEST['order_id']."'  order by id desc ");

	if($obj->numRows($commentArr)>0){?>
      <tr>
    <td><table width="100%" border="0" cellspacing="4" cellpadding="4" bgcolor="#f7faf9" class="bodr">
        <tr>
          <td width="21%"><strong>Date</strong></td>
          <td width="21%"><strong>Pay Via</strong></td>
          <td width="21%"><strong>Received Amount</strong></td>
          <td width="21%"><strong>Received Date</strong></td>
          <td width="58%"><strong>Comment</strong></td>
        </tr>
        <?php while($resultComment=$obj->fetchNextObject($commentArr)){?>
        <tr>
          <td><?php echo date('d M Y H:i',strtotime($resultComment->posted_date)); ?></td>
          <td><?php  if($resultComment->pay_via=='Credit/Debit Card'){

		if($resultComment->transaction_id!=''){

		echo 'Transaction ID:#'.$resultComment->transaction_id."<br/>";

		}

		if($resultComment->card_number!=''){

		echo 'Crad Last digits:'.$resultComment->card_number."<br/>";

		}	

		

		?>
            <?php } else{?>
            Cash
            <?php }?></td>
          <td><?php  if($resultComment->pay_amount!=0){echo 'Rs. '.stripslashes($resultComment->pay_amount);} ?></td>
          <td><?php if($resultComment->posted_date!='0000-00-00'){ echo stripslashes($resultComment->posted_date); }?></td>
          <td><?php echo stripslashes($resultComment->comments); ?></td>
        </tr>
        <?php } ?>
      </table></td>
  </tr>
      <?php } ?>
    </table>
</td>
</tr>
</table>
<?php
if($resultOrder->coupon_code!=''){
// insert refer a friend reward point
if($_REQUEST['order_status']==3 && $_REQUEST['payment_status']==1){
$referarr=$obj->query("select ref_user_id,hero from tbl_coupon where coupon_code='$resultOrder->coupon_code' and status=1");
$resultrefer=$obj->fetchNextObject($referarr);

if($resultrefer->ref_user_id!='' && $resultrefer->hero==''){
	$obj->query("insert into tbl_reward_history set user_id='$resultrefer->ref_user_id', order_id='$order_id', reward_point='500', type='Cr', added_date='now()', status='1'");
	} 
	
// insert be a hero reward point
if($resultrefer->ref_user_id!='' && $resultrefer->hero=='0'){
$start_date= date('Y-m-01',strtotime(date('Y-m-d')));
$last_date = date('Y-m-t',strtotime(date('Y-m-d')));
$cArr=$obj->query("select count(*) as ordered from $tbl_order where coupon_code='$resultOrder->coupon_code' and order_date>'$start_date' and order_date<'$last_date'");
$os=$obj->fetchNextObject($cArr);

if($os==5){ $rewardearned=5000; 
$obj->query("insert into tbl_reward_history set user_id='$resultrefer->ref_user_id', order_id='$order_id', reward_point='$rewardearned', type='Cr', added_date='now()', status='1'"); }

if($os==10){ $rewardearned=5000; 
$obj->query("insert into tbl_reward_history set user_id='$resultrefer->ref_user_id', order_id='$order_id', reward_point='$rewardearned', type='Cr', added_date='now()', status='1'"); }

if($os==15){ $rewardearned=5000; 
$obj->query("insert into tbl_reward_history set user_id='$resultrefer->ref_user_id', order_id='$order_id', reward_point='$rewardearned', type='Cr', added_date='now()', status='1'"); }
}
}
	}
	


?>
</body>
</html>
