<?php

/*

* date last modified 28-11-2017

* Edited by PI-Digi Logical Solutions

* HTML Files - add_user.html.php

* Mysql Tables - tbl_designation, tbl_user

*/

include_once('PHPMailer/class.phpmailer.php');

require_once('./classes/class.noseo_pagination.php'); # pagination class

include('includes/functions.php'); # general functions

require_once('includes/push_functions.php');



$debug = true;



if(isset($_REQUEST['p']) && $_REQUEST['p']!=''){

	$p = trim($_REQUEST['p']);

} else {

	$p = 1;

}



$pageVars = '';

$buttonHours = 0;



$categoryRs = mysqli_query($con, "SELECT * FROM tbl_service_categories WHERE parent_id = '0'");









if(isset($_REQUEST['id']) && $_REQUEST['id']!=''){		

	$editId = mysqli_real_escape_string($con, trim($_REQUEST['id']));	

	

	mysqli_query($con, "UPDATE tbl_job SET viewed = 'yes' WHERE id = '".$editId."'");

	

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

		if(isset($_POST['note']) && $_POST['note']!=''){

			$note = trim($_POST['note']);

			$curDate = date('Y-m-d H:i:s');

			$adminId = $_SESSION['adminId'];

			$insertNote = mysqli_query($con, "INSERT INTO tbl_job_notes (task_id, note, date, added_by) VALUES ('".$editId."', '".$note."', '".$curDate."', '".$adminId."')");

			if($insertNote){

				$success = 'Notes added for the task';

				header('Location:index.php?page=view_task&id='.$editId.'&p='.$p.'&success='.$success.'#add_note');

				exit();

			}

		}

	}	

	

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

		

		if(isset($_POST['discount_from']) && $_POST['discount_from']!=''){

			$discount_from = trim($_POST['discount_from']);					

		} else {

			$adjustError = 'Select discount from (Admin/Technician)';

		}

		

		if(isset($_POST['discount']) && $_POST['discount']!=''){

			$discount = trim($_POST['discount']);

			if(is_numeric($discount) && $discount>0){

				//ok				

			} else {

				$adjustError = 'Enter a valid amount as discount';

			}		

		} else {

			$adjustError = 'Enter discount';

		}

		

		$sendMailToUser = FALSE;

		

		if(empty($adjustError)){

			if($discount_from == 'admin'){

				$update = mysqli_query($con, "UPDATE tbl_job SET admin_discount = '".$discount."', tech_discount = '0' WHERE id = '".$editId."'");

				if($update){

					

					//if completed task, get total task amount. we should remove the tech discount and make the amounts back to original					

					//change amount in tbl_task_completed. if extra techs that too					

					$taskCompletedRs = mysqli_query($con, "SELECT id, tech_id, task_amount FROM tbl_task_completed WHERE task_id = '".$editId."'");

					if(mysqli_num_rows($taskCompletedRs)>0){

						$totTaskRec = mysqli_fetch_array(mysqli_query($con, "SELECT SUM(task_amount) AS amount FROM tbl_task_completed WHERE task_id = '".$editId."'"));

						$totalTaskAmount = $totTaskRec['amount'];

						$newTotalTaskAmount = $totalTaskAmount-$discount;

						$adminCommissionRec = mysqli_fetch_array(mysqli_query($con, "SELECT admin_commission_percent FROM tbl_job WHERE id = '".$editId."'"));

						$adminCommission = $adminCommissionRec['admin_commission_percent'];

						while($taskCompletedRec = mysqli_fetch_array($taskCompletedRs)){

							$completeId = $taskCompletedRec['id'];

							$extaTechId = $taskCompletedRec['tech_id'];

							$completeTaskAmount = $taskCompletedRec['task_amount'];

							//update completed rec. As this is admin discount no amount will be deducted in tech income or admin commission	

							//may be a tech discount applied before. so set the admin commission and tech income as that of the original amount

							$taskPercent = ($completeTaskAmount*100)/$totalTaskAmount;

							$newTechDeduction = 0;

							if($taskPercent == 100){

								$newTaskAmount = $totalTaskAmount;																

							} else {

								$taskPercent = number_format((float)$taskPercent, 2, '.', '');

								$newTaskAmount = ($totalTaskAmount*$taskPercent)/100;								

							}						

													

							if($adminCommission>0){

								$newAdminCommission = ($completeTaskAmount*$adminCommission)/100;								

							} else {

								$newAdminCommission = 0;

							}

							$newTechIncome = $completeTaskAmount-$newAdminCommission;

																				

							mysqli_query($con, "UPDATE tbl_task_completed SET tech_discount_amount = '0', admin_commission = '".$newAdminCommission."', tech_income = '".$newTechIncome."' WHERE id = '".$completeId."'");

							//update tbl_job_extra_tech also

							mysqli_query($con, "UPDATE tbl_job_extra_tech SET commission = '".$newTaskAmount."' WHERE job_id = '".$editId."' AND technician_id = '".$extaTechId."'");		

							//exit;						

						}

					}



					$adjustSuccess = 'Admin discount of Rs.'.$discount.' has been applied';

				} else {

					$adjustError = 'Something went wrong!';

				}

			} else {

				$update = mysqli_query($con, "UPDATE tbl_job SET admin_discount = '0', tech_discount = '".$discount."', admin_extra = '0' WHERE id = '".$editId."'");

				if($update){

					//if completed task, get total task amount					

					//change amount in tbl_task_completed. if extra techs that too

					$taskCompletedRs = mysqli_query($con, "SELECT id, tech_id, task_amount FROM tbl_task_completed WHERE task_id = '".$editId."'");

					if(mysqli_num_rows($taskCompletedRs)>0){

						$totTaskRec = mysqli_fetch_array(mysqli_query($con, "SELECT SUM(task_amount) AS amount FROM tbl_task_completed WHERE task_id = '".$editId."'"));

						$totalTaskAmount = $totTaskRec['amount'];

						$newTotalTaskAmount = $totalTaskAmount-$discount;

						$adminCommissionRec = mysqli_fetch_array(mysqli_query($con, "SELECT user_id, admin_commission_percent FROM tbl_job WHERE id = '".$editId."'"));

						$adminCommission = $adminCommissionRec['admin_commission_percent'];

						$userId = $adminCommissionRec['user_id'];

						$sendMailToUser = TRUE;

						while($taskCompletedRec = mysqli_fetch_array($taskCompletedRs)){

							$completeId = $taskCompletedRec['id'];

							$extaTechId = $taskCompletedRec['tech_id'];

							$completeTaskAmount = $taskCompletedRec['task_amount'];

							//find new task amount after deducting tech discount for this tech

							$taskPercent = ($completeTaskAmount*100)/$totalTaskAmount;

							if($taskPercent == 100){

								$newTaskAmount = $newTotalTaskAmount;

								$newTechDeduction = $discount;

							} else {

								$taskPercent = number_format((float)$taskPercent, 2, '.', '');

								$newTaskAmount = ($newTotalTaskAmount*$taskPercent)/100;

								$newTechDeduction = $completeTaskAmount-$newTaskAmount;

							}							

							//find new admin commission

							if($adminCommission>0){

								$newAdminCommission = ($newTaskAmount*$adminCommission)/100;								

							} else {

								$newAdminCommission = 0;

							}

							$newTechIncome = $newTaskAmount-$newAdminCommission;

							//update

							mysqli_query($con, "UPDATE tbl_task_completed SET tech_discount_amount = '".$newTaskAmount."', admin_commission = '".$newAdminCommission."', tech_income = '".$newTechIncome."' WHERE id = '".$completeId."'");

							/*if($_SERVER['REMOTE_ADDR']=='202.88.237.77'){

								echo 'New tot task amount - '.$newTotalTaskAmount.', deduction-'.$newTechDeduction.'<br>';	

								echo "UPDATE tbl_task_completed SET tech_discount_amount = '".$newTaskAmount."', admin_commission = '".$newAdminCommission."', tech_income = '".$newTechIncome."' WHERE id = '".$completeId."'";

							}*/

							//update tbl_job_extra_tech also

							mysqli_query($con, "UPDATE tbl_job_extra_tech SET commission = '".$newTaskAmount."' WHERE job_id = '".$editId."' AND technician_id = '".$extaTechId."'");

							/*if($_SERVER['REMOTE_ADDR']=='202.88.237.77'){

								echo "UPDATE tbl_job_extra_tech SET commission = '".$newTaskAmount."' WHERE job_id = '".$editId."' AND technician_id = '".$extaTechId."'";

							}*/	

						}

					}			



					if($sendMailToUser){

						//send mail to user

						//get from email address

						$settingsRec = mysqli_fetch_array(mysqli_query($con, "SELECT from_email FROM tbl_settings WHERE id = '1'"));

						$fromEmail = $settingsRec['from_email'];

						//get user details

						$userRec = mysqli_fetch_array(mysqli_query($con, "SELECT name, email FROM tbl_user WHERE id = '".$userId."'"));

						$userName = $userRec['name'];

						$userEmail = $userRec['email'];

														

						$mail = new PHPMailer();

						$mail->From = $fromEmail;

						$mail->FromName = SITE_NAME;

						$mail->IsHTML(true);	

						$mail->Subject = SITE_NAME.' Technician Discount';	

						$mail->AddAddress($userEmail, $userName);

														

						$email_content_user = '<table width="600" border="0" align="center" cellpadding="0" cellspacing="0">

															  <tr>

																<td height="68" align="left" valign="top"><a href="'.BASE_URL.'" target="_blank"><img src="'.BASE_URL.'images/logo.png" /></a></td>

																<td align="right" valign="bottom" style="color:#ff8d07;">&nbsp;</td>

															  </tr>

															   <tr>

																<td height="5" colspan="2" align="left" valign="top"></td>

															  </tr>

															  <tr>

																<td height="49" colspan="2" align="left" valign="top" bgcolor="#970198" style="padding:10px 0px 0px 20px; color:#FFFFFF; font-size:24px; font-family:Times New Roman, Times,   serif;">Discount from technician!</td>

															  </tr>

															  <tr>

																<td colspan="2" align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0" style="border:#970198 solid 1px;">

																  <tr>

																	<td height="4" colspan="4" align="left" valign="top" bgcolor="#970198"></td>

																  </tr>

																  <tr>

																	<td height="8" colspan="4" align="left" valign="top"></td>

																  </tr>

																	<tr>

																	<td width="258" align="left" valign="top" style="color:#4e4e4e; font-size:13px; line-height:16px; padding:0px 0px 0px 9px;">Hello  '. $userName .',</td>

																  </tr>

																	  <tr>

																		<td height="8" align="left" valign="top" style="color:#4e4e4e; font-size:11px;  padding:0px 0px 0px 9px;"></td>

																	</tr>

																  <tr>

																	<td height="10" colspan="4" align="left" valign="top"></td>

																  </tr>

																  <tr>

																	<td colspan="4" align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">

																	  <tr>

																		<td width="8" align="left" valign="top">&nbsp;</td>

																		<td width="581" height="140" align="left" valign="top" bgcolor="#f0f0f0" style="border:#c9ccc5 solid 1px; line-height:18px; padding:10px 0px 0px 8px; font-size:11px; font-weight:normal; line-height:27px;">

																			<p></p>

																			<p><b>You got an offer from your technician</b></p>									

																			<p>Task ID : '.$editId.'</p>

																			<p>Discount: <b>Rs.'.$discount.'</b></p>

																			<p></p>

																		  </td>

																		<td width="7" align="left" valign="top">&nbsp;</td>

																	  </tr>

																	</table></td>

																  </tr><tr>

																	<td colspan="4" align="left" valign="top">&nbsp;</td>

																  </tr>

																</table></td>

															  </tr>

															</table>';

							$mail->Body = $email_content_user;

							$mail->Send();

					}

					



		

					$adjustSuccess = 'Technician discount of Rs.'.$discount.' has been applied';

				} else {

					$adjustError = 'Something went wrong!';

				}

			}				

		}		

	}

	

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

		if(isset($_POST['admin_extra']) && $_POST['admin_extra']!=''){

			$adminExtra = trim($_POST['admin_extra']);

			$sendMailToUser = FALSE;

			if(is_numeric($adminExtra) && $adminExtra>0){

				$update = mysqli_query($con, "UPDATE tbl_job SET admin_discount = '0', tech_discount = '0', admin_extra = '".$adminExtra."' WHERE id = '".$editId."'");

				if($update){

					//if completed task, get total task amount					

					//change amount in tbl_task_completed. if extra techs that too

					$taskCompletedRs = mysqli_query($con, "SELECT id, tech_id, task_amount FROM tbl_task_completed WHERE task_id = '".$editId."'");

					if(mysqli_num_rows($taskCompletedRs)>0){

						$totTaskRec = mysqli_fetch_array(mysqli_query($con, "SELECT SUM(task_amount) AS amount FROM tbl_task_completed WHERE task_id = '".$editId."'"));

						$totalTaskAmount = $totTaskRec['amount'];

						$newTotalTaskAmount = $totalTaskAmount+$adminExtra;

						$adminCommissionRec = mysqli_fetch_array(mysqli_query($con, "SELECT user_id, admin_commission_percent FROM tbl_job WHERE id = '".$editId."'"));

						$adminCommission = $adminCommissionRec['admin_commission_percent'];

						$userId = $adminCommissionRec['user_id'];

						$sendMailToUser = TRUE;

						while($taskCompletedRec = mysqli_fetch_array($taskCompletedRs)){

							$completeId = $taskCompletedRec['id'];

							$extaTechId = $taskCompletedRec['tech_id'];

							$completeTaskAmount = $taskCompletedRec['task_amount'];

							//find new task amount after deducting tech discount for this tech

							$taskPercent = ($completeTaskAmount*100)/$totalTaskAmount;

							if($taskPercent == 100){

								$newTaskAmount = $newTotalTaskAmount;

								$newTechExtra = $adminExtra;

							} else {

								$taskPercent = number_format((float)$taskPercent, 2, '.', '');

								$newTaskAmount = ($newTotalTaskAmount*$taskPercent)/100;

								$newTechExtra = $newTaskAmount-$completeTaskAmount;

							}							

							//find new admin commission

							if($adminCommission>0){

								$newAdminCommission = ($newTaskAmount*$adminCommission)/100;								

							} else {

								$newAdminCommission = 0;

							}

							$newTechIncome = $newTaskAmount-$newAdminCommission;

							//update

							mysqli_query($con, "UPDATE tbl_task_completed SET tech_discount_amount = '".$newTaskAmount."', admin_commission = '".$newAdminCommission."', tech_income = '".$newTechIncome."' WHERE id = '".$completeId."'");

							/*if($_SERVER['REMOTE_ADDR']=='202.88.237.77'){

								echo 'New tot task amount - '.$newTotalTaskAmount.', deduction-'.$newTechDeduction.'<br>';	

								echo "UPDATE tbl_task_completed SET tech_discount_amount = '".$newTaskAmount."', admin_commission = '".$newAdminCommission."', tech_income = '".$newTechIncome."' WHERE id = '".$completeId."'";

							}*/

							//update tbl_job_extra_tech also

							mysqli_query($con, "UPDATE tbl_job_extra_tech SET commission = '".$newTaskAmount."' WHERE job_id = '".$editId."' AND technician_id = '".$extaTechId."'");

							/*if($_SERVER['REMOTE_ADDR']=='202.88.237.77'){

								echo "UPDATE tbl_job_extra_tech SET commission = '".$newTaskAmount."' WHERE job_id = '".$editId."' AND technician_id = '".$extaTechId."'";

							}*/	

						}

					}			



					if($sendMailToUser){

						//send mail to user

						//get from email address

						$settingsRec = mysqli_fetch_array(mysqli_query($con, "SELECT from_email FROM tbl_settings WHERE id = '1'"));

						$fromEmail = $settingsRec['from_email'];

						//get user details

						$userRec = mysqli_fetch_array(mysqli_query($con, "SELECT name, email FROM tbl_user WHERE id = '".$userId."'"));

						$userName = $userRec['name'];

						$userEmail = $userRec['email'];

														

						$mail = new PHPMailer();

						$mail->From = $fromEmail;

						$mail->FromName = SITE_NAME;

						$mail->IsHTML(true);	

						$mail->Subject = SITE_NAME.' Extra Charge';	

						$mail->AddAddress($userEmail, $userName);

														

						$email_content_user = '<table width="600" border="0" align="center" cellpadding="0" cellspacing="0">

															  <tr>

																<td height="68" align="left" valign="top"><a href="'.BASE_URL.'" target="_blank"><img src="'.BASE_URL.'images/logo.png" /></a></td>

																<td align="right" valign="bottom" style="color:#ff8d07;">&nbsp;</td>

															  </tr>

															   <tr>

																<td height="5" colspan="2" align="left" valign="top"></td>

															  </tr>

															  <tr>

																<td height="49" colspan="2" align="left" valign="top" bgcolor="#970198" style="padding:10px 0px 0px 20px; color:#FFFFFF; font-size:24px; font-family:Times New Roman, Times,   serif;">Extra Charge from '.SITE_NAME.'</td>

															  </tr>

															  <tr>

																<td colspan="2" align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0" style="border:#970198 solid 1px;">

																  <tr>

																	<td height="4" colspan="4" align="left" valign="top" bgcolor="#970198"></td>

																  </tr>

																  <tr>

																	<td height="8" colspan="4" align="left" valign="top"></td>

																  </tr>

																	<tr>

																	<td width="258" align="left" valign="top" style="color:#4e4e4e; font-size:13px; line-height:16px; padding:0px 0px 0px 9px;">Hello  '. $userName .',</td>

																  </tr>

																	  <tr>

																		<td height="8" align="left" valign="top" style="color:#4e4e4e; font-size:11px;  padding:0px 0px 0px 9px;"></td>

																	</tr>

																  <tr>

																	<td height="10" colspan="4" align="left" valign="top"></td>

																  </tr>

																  <tr>

																	<td colspan="4" align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">

																	  <tr>

																		<td width="8" align="left" valign="top">&nbsp;</td>

																		<td width="581" height="140" align="left" valign="top" bgcolor="#f0f0f0" style="border:#c9ccc5 solid 1px; line-height:18px; padding:10px 0px 0px 8px; font-size:11px; font-weight:normal; line-height:27px;">

																			<p></p>

																			<p><b>'.SITE_NAME.' added extra charge for your task</b></p>									

																			<p>Task ID : '.$editId.'</p>

																			<p>Extra Charge: <b>Rs.'.$adminExtra.'</b></p>

																			<p></p>

																		  </td>

																		<td width="7" align="left" valign="top">&nbsp;</td>

																	  </tr>

																	</table></td>

																  </tr><tr>

																	<td colspan="4" align="left" valign="top">&nbsp;</td>

																  </tr>

																</table></td>

															  </tr>

															</table>';

						$mail->Body = $email_content_user;

						$mail->Send();

					}

					

					$extraSuccess = 'The task amount has been added by '.$adminExtra;

				} else {

					$extraError = 'Something went wrong!';

				}

					

								

			} else {

				$extraError = 'Enter a valid amount as discount';

			}

			

		}

	}

	

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

			

		if(isset($_POST['sms_msg']) && $_POST['sms_msg']!=''){

			$smsMsg = $_POST['sms_msg'];

		} else {

			$extraError = 'Enter sms message';

		}



		if(isset($_POST['user_id']) && $_POST['user_id']!=''){

			$userId = $_POST['user_id'];

		} else {

			$extraError = 'Something went wrong!';

		}



		if(empty($extraError)){

			//get user details			

			$userPhoneRec = mysqli_fetch_array(mysqli_query($con, "SELECT name, phone FROM tbl_user WHERE id = '".$userId."'"));

			$userPhone = $userPhoneRec['phone'];

			$userName = $userPhoneRec['name'];		

			

			$smsMessage = urlencode($smsMsg);				

			if(sendSMSP($userPhone, $smsMessage)){							

				$extraSuccess = 'Message sent successfully';

			} else {

				mail($contact_email, 'SMS API Error','SMS API failed to send in task_details(web), task-completed:user phone:'.$userPhone);			

			}

		}



	}	

	

	//cancel assigned technician

	if(isset($_REQUEST['action']) && $_REQUEST['action']=='cancel_tech'){//store the details for later purposes

	

			//get user and tech details

				$taskRec = mysqli_fetch_array(mysqli_query($con, "SELECT user_id, technician_id, status FROM tbl_job WHERE id = '".$editId."'"));

				$userName = getUserName($taskRec['user_id']);

				$techId = $taskRec['technician_id'];

				$taskUserId = $taskRec['user_id'];

				$taskStatus = $taskRec['status'];

				$curDate = date('Y-m-d H:i:s');

				mysqli_query($con, "INSERT INTO tbl_task_cancelled (task_id, technician_id, user_id, date, status) VALUES ('".$editId."', '".$techId."', '".$taskUserId."', '".$curDate."', '".$taskStatus."')");

				

				//send mail to technician and user

				$techRec = mysqli_fetch_array(mysqli_query($con, "SELECT name, email FROM tbl_technician WHERE id = '".$techId."'"));

				$techName = $techRec['name'];

				$techEmail = $techRec['email'];

				

				$userRec = mysqli_fetch_array(mysqli_query($con, "SELECT email FROM tbl_user WHERE id = '".$taskUserId."'"));				

				$userEmail = $userRec['email'];

				

			$update = mysqli_query($con, "UPDATE tbl_job SET technician_id = '0', status = 'New', admin_slot = '', admin_slot_to = '' WHERE id = '".$editId."'");

			if($update){

				//delete all recs from status table

				mysqli_query($con, "DELETE FROM tbl_job_status WHERE task_id = '".$editId."'");	

				

				//send mail to technician

				//get from email address

				$settingsRec = mysqli_fetch_array(mysqli_query($con, "SELECT from_email FROM tbl_settings WHERE id = '1'"));

				$fromEmail = $settingsRec['from_email'];

				

				$mail = new PHPMailer();

				$mail->From = $fromEmail;

				$mail->FromName = SITE_NAME;

				$mail->IsHTML(true);	

				$mail->Subject = SITE_NAME.' Task Unassigned';	

		

				

				$mail->AddAddress($techEmail, $techName);					

				//$to_name = $name;			

		

						$email_content = '<table width="600" border="0" align="center" cellpadding="0" cellspacing="0">

					  <tr>

						<td height="68" align="left" valign="top"><a href="'.BASE_URL.'" target="_blank"><img src="'.BASE_URL.'images/logo.png" /></a></td>

						<td align="right" valign="bottom" style="color:#ff8d07;">&nbsp;</td>

					  </tr>

					   <tr>

						<td height="5" colspan="2" align="left" valign="top"></td>

					  </tr>

					  <tr>

						<td height="49" colspan="2" align="left" valign="top" bgcolor="#970198" style="padding:10px 0px 0px 20px; color:#FFFFFF; font-size:24px; font-family:Times New Roman, Times,   serif;">'.SITE_NAME.' Unassigned you!</td>

					  </tr>

					  <tr>

						<td colspan="2" align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0" style="border:#970198 solid 1px;">

						  <tr>

							<td height="4" colspan="4" align="left" valign="top" bgcolor="#970198"></td>

						  </tr>

						  <tr>

							<td height="8" colspan="4" align="left" valign="top"></td>

						  </tr>

							<tr>

							<td width="258" align="left" valign="top" style="color:#4e4e4e; font-size:13px; line-height:16px; padding:0px 0px 0px 9px;">Hello  '. $techName .',</td>

						  </tr>

							  <tr>

								<td height="8" align="left" valign="top" style="color:#4e4e4e; font-size:11px;  padding:0px 0px 0px 9px;"></td>

							</tr>

						  <tr>

							<td height="10" colspan="4" align="left" valign="top"></td>

						  </tr>

						  <tr>

							<td colspan="4" align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">

							  <tr>

								<td width="8" align="left" valign="top">&nbsp;</td>

								<td width="581" height="140" align="left" valign="top" bgcolor="#f0f0f0" style="border:#c9ccc5 solid 1px; line-height:18px; padding:10px 0px 0px 8px; font-size:11px; font-weight:normal; line-height:27px;">

									<p></p>

									<p><b>You have been unassigned from a task of the user '.$userName.'<b></p>

									<p>Task ID: #'.$editId.'</p>

									<p></p>

									<p></p>

									<p></p>

								  </td>

								<td width="7" align="left" valign="top">&nbsp;</td>

							  </tr>

							</table></td>

						  </tr><tr>

							<td colspan="4" align="left" valign="top">&nbsp;</td>

						  </tr>

						</table></td>

					  </tr>

					</table>';

				$mail->Body = $email_content;

				$mail->Send();			

				

				//send mail to user

				$mail = new PHPMailer();

				$mail->From = $fromEmail;

				$mail->FromName = SITE_NAME;

				$mail->IsHTML(true);	

				$mail->Subject = SITE_NAME.' Cancelled Technician';				

				$mail->AddAddress($userEmail, $userName);	

				

				$email_content_user = '<table width="600" border="0" align="center" cellpadding="0" cellspacing="0">

					  <tr>

						<td height="68" align="left" valign="top"><a href="'.BASE_URL.'" target="_blank"><img src="'.BASE_URL.'images/logo.png" /></a></td>

						<td align="right" valign="bottom" style="color:#ff8d07;">&nbsp;</td>

					  </tr>

					   <tr>

						<td height="5" colspan="2" align="left" valign="top"></td>

					  </tr>

					  <tr>

						<td height="49" colspan="2" align="left" valign="top" bgcolor="#970198" style="padding:10px 0px 0px 20px; color:#FFFFFF; font-size:24px; font-family:Times New Roman, Times,   serif;">Technician Unassigned</td>

					  </tr>

					  <tr>

						<td colspan="2" align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0" style="border:#970198 solid 1px;">

						  <tr>

							<td height="4" colspan="4" align="left" valign="top" bgcolor="#970198"></td>

						  </tr>

						  <tr>

							<td height="8" colspan="4" align="left" valign="top"></td>

						  </tr>

							<tr>

							<td width="258" align="left" valign="top" style="color:#4e4e4e; font-size:13px; line-height:16px; padding:0px 0px 0px 9px;">Hello  '. $techName .',</td>

						  </tr>

							  <tr>

								<td height="8" align="left" valign="top" style="color:#4e4e4e; font-size:11px;  padding:0px 0px 0px 9px;"></td>

							</tr>

						  <tr>

							<td height="10" colspan="4" align="left" valign="top"></td>

						  </tr>

						  <tr>

							<td colspan="4" align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">

							  <tr>

								<td width="8" align="left" valign="top">&nbsp;</td>

								<td width="581" height="140" align="left" valign="top" bgcolor="#f0f0f0" style="border:#c9ccc5 solid 1px; line-height:18px; padding:10px 0px 0px 8px; font-size:11px; font-weight:normal; line-height:27px;">

									<p></p>

									<p><b>'.SITE_NAME.' unassigned technician from your task<b></p>

									<p>Task ID: #'.$editId.'</p>

									<p></p>

									<p></p>

									<p></p>

								  </td>

								<td width="7" align="left" valign="top">&nbsp;</td>

							  </tr>

							</table></td>

						  </tr><tr>

							<td colspan="4" align="left" valign="top">&nbsp;</td>

						  </tr>

						</table></td>

					  </tr>

					</table>';

				$mail->Body = $email_content_user;

				$mail->Send();	

				

				//send push				

				$exec_token = mysqli_query($con, 'SELECT fcm_token, device FROM tbl_fcm_tokens WHERE tech_id='.$techId);

				if(isset($exec_token) && mysqli_num_rows($exec_token)>0) {

					while($res_token = mysqli_fetch_array($exec_token)) {

						if($res_token['fcm_token']!='') {

							$title = 'You have been unassigned from a task of the user '.$userName;

							$body = 'Task ID: #'.$editId;

							if($res_token['device']=='android') {								

								sendGCM($title, $body, $res_token['fcm_token'], "tech", $editId, $taskRec['user_id'], $userName, "task");

							} elseif($res_token['device']=='ios') {

								//sendIosTechPush($title, $body, $res_token['fcm_token'], "tech");

								sendIosTechAssignPush($title, $body, $res_token['fcm_token'], "tech", $editId, "task", $taskRec['user_id'], $userName, $techId, $techName);

							}

						}

					}

				}



				//sendGCM_user($title, $body, $token, $task_id, $from, $user_id, $user_name, $tech_id, $tech_name) {



				//send push to user

				$exec_token = mysqli_query($con, 'SELECT fcm_token, device FROM tbl_fcm_tokens WHERE user_id='.$taskRec['user_id']);

				if(isset($exec_token) && mysqli_num_rows($exec_token)>0) {

					while($res_token = mysqli_fetch_array($exec_token)) {

						if($res_token['fcm_token']!='') {

							$title = 'Technician has been unassigned from your task';

							$body = 'Task ID: #'.$editId;

							if($res_token['device']=='android') {																

								sendGCM($title, $body, $res_token['fcm_token'], "user", $editId, $taskRec['user_id'], $userName, "task");

							} elseif($res_token['device']=='ios') {

								sendIosTechPush($title, $body, $res_token['fcm_token'], "tech");

							}

						}

					}

				}

				//send push to tech

				$exec_token = mysqli_query($con, 'SELECT fcm_token, device FROM tbl_fcm_tokens WHERE user_id='.$taskUserId);

				if(isset($exec_token) && mysqli_num_rows($exec_token)>0) {

					while($res_token = mysqli_fetch_array($exec_token)) {

						if($res_token['fcm_token']!='') {

							$title = 'You have been unassigned from a task of the user '.$userName;

							$body = 'Task ID: #'.$editId;

							if($res_token['device']=='android') {								

								sendGCM($title, $body, $res_token['fcm_token'], "tech", $editId, $taskRec['user_id'], $userName, "task");

							} elseif($res_token['device']=='ios') {

								sendIosTechPush($title, $body, $res_token['fcm_token'], "tech");

							}

						}

					}

				}

			

			$success = 'Removed the technician from this task';

		}

	}



	//cancel assigned EXTRA technician

	if((isset($_REQUEST['action']) && $_REQUEST['action']=='cancel_extra_tech') && (isset($_REQUEST['extra_id']) && $_REQUEST['extra_id']!='')){		

		$extraTechId = trim($_REQUEST['extra_id']);

		

			//get user and tech details

				$taskRec = mysqli_fetch_array(mysqli_query($con, "SELECT user_id, technician_id FROM tbl_job WHERE id = '".$editId."'"));

				$userName = getUserName($taskRec['user_id']);

				$techId = $taskRec['technician_id'];

				

				$techRec = mysqli_fetch_array(mysqli_query($con, "SELECT name, email FROM tbl_technician WHERE id = '".$extraTechId."'"));

				$techName = $techRec['name'];

				$techEmail = $techRec['email'];				

				

				//send mail to technician

				//get from email address

				$settingsRec = mysqli_fetch_array(mysqli_query($con, "SELECT from_email FROM tbl_settings WHERE id = '1'"));

				$fromEmail = $settingsRec['from_email'];

				$mail = new PHPMailer();

				$mail->From = $fromEmail;

				$mail->FromName = SITE_NAME;

				$mail->IsHTML(true);	

				$mail->Subject = SITE_NAME.' Task Unassigned';	

		

				

				$mail->AddAddress($techEmail, $techName);					

				//$to_name = $name;			

		

						$email_content = '<table width="600" border="0" align="center" cellpadding="0" cellspacing="0">

					  <tr>

						<td height="68" align="left" valign="top"><a href="'.BASE_URL.'" target="_blank"><img src="'.BASE_URL.'images/logo.png" /></a></td>

						<td align="right" valign="bottom" style="color:#ff8d07;">&nbsp;</td>

					  </tr>

					   <tr>

						<td height="5" colspan="2" align="left" valign="top"></td>

					  </tr>

					  <tr>

						<td height="49" colspan="2" align="left" valign="top" bgcolor="#970198" style="padding:10px 0px 0px 20px; color:#FFFFFF; font-size:24px; font-family:Times New Roman, Times,   serif;">New task from user</td>

					  </tr>

					  <tr>

						<td colspan="2" align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0" style="border:#970198 solid 1px;">

						  <tr>

							<td height="4" colspan="4" align="left" valign="top" bgcolor="#970198"></td>

						  </tr>

						  <tr>

							<td height="8" colspan="4" align="left" valign="top"></td>

						  </tr>

							<tr>

							<td width="258" align="left" valign="top" style="color:#4e4e4e; font-size:13px; line-height:16px; padding:0px 0px 0px 9px;">Hello  '. $techName .',</td>

						  </tr>

							  <tr>

								<td height="8" align="left" valign="top" style="color:#4e4e4e; font-size:11px;  padding:0px 0px 0px 9px;"></td>

							</tr>

						  <tr>

							<td height="10" colspan="4" align="left" valign="top"></td>

						  </tr>

						  <tr>

							<td colspan="4" align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">

							  <tr>

								<td width="8" align="left" valign="top">&nbsp;</td>

								<td width="581" height="140" align="left" valign="top" bgcolor="#f0f0f0" style="border:#c9ccc5 solid 1px; line-height:18px; padding:10px 0px 0px 8px; font-size:11px; font-weight:normal; line-height:27px;">

									<p></p>

									<p><b>You have been unassigned from a task of the user '.$userName.'<b></p>

									<p>Task ID: #'.$editId.'</p>

									<p></p>

									<p></p>

									<p></p>

								  </td>

								<td width="7" align="left" valign="top">&nbsp;</td>

							  </tr>

							</table></td>

						  </tr><tr>

							<td colspan="4" align="left" valign="top">&nbsp;</td>

						  </tr>

						</table></td>

					  </tr>

					</table>';

				$mail->Body = $email_content;

				$mail->Send();			

				

				//send push

				$exec_token = mysqli_query($con, 'SELECT fcm_token, device FROM tbl_fcm_tokens WHERE tech_id='.$extraTechId);

				if(isset($exec_token) && mysqli_num_rows($exec_token)>0) {

					while($res_token = mysqli_fetch_array($exec_token)) {

						if($res_token['fcm_token']!='') {

							$title = 'You have been unassigned from a task of the user '.$userName;

							$body = 'Task ID: #'.$editId;

							if($res_token['device']=='android') {								

								sendGCM($title, $body, $res_token['fcm_token'], "tech", $editId, $taskRec['user_id'], $userName, "task");

							} elseif($res_token['device']=='ios') {

								sendIosTechPush($title, $body, $res_token['fcm_token'], "tech");

							}

						}

					}

				}

		

						

			$delete = mysqli_query($con, "UPDATE tbl_job_extra_tech SET technician_id = '0', commission_percent = '0', status = '' WHERE job_id = '".$editId."' AND technician_id = '".$extraTechId."'");

			if($delete){				

				//delete all recs from status table

				mysqli_query($con, "DELETE FROM tbl_job_extra_tech_status WHERE task_id = '".$editId."' AND tech_id = '".$extraTechId."'");	

				$success = 'Removed the technician from this task';

			}

	}

	

	

	//if assign action

	if(isset($_REQUEST['technician_id']) && $_REQUEST['technician_id']!=''){

		$techId = mysqli_real_escape_string($con, trim($_REQUEST['technician_id']));

		if(isset($_REQUEST['action']) && $_REQUEST['action']=='assign'){

			//assign the technician to this task

			$update = mysqli_query($con, "UPDATE tbl_job SET technician_id = '".$techId."', status = 'assigned' WHERE id = '".$editId."'");

			if($update){

				//get user and tech details

				$taskRec = mysqli_fetch_array(mysqli_query($con, "SELECT user_id FROM tbl_job WHERE id = '".$editId."'"));

				$userName = getUserName($taskRec['user_id']);

				

				$techRec = mysqli_fetch_array(mysqli_query($con, "SELECT name, email FROM tbl_technician WHERE id = '".$techId."'"));

				$techName = $techRec['name'];

				$techEmail = $techRec['email'];

				

				//send mail to technician

				//get from email address

				$settingsRec = mysqli_fetch_array(mysqli_query($con, "SELECT from_email FROM tbl_settings WHERE id = '1'"));

				$fromEmail = $settingsRec['from_email'];

				$mail = new PHPMailer();

				$mail->From = $fromEmail;

				$mail->FromName = SITE_NAME;

				$mail->IsHTML(true);	

				$mail->Subject = SITE_NAME.' New task';	

		

				

				$mail->AddAddress($techEmail, $techName);					

				//$to_name = $name;			

		

						$email_content = '<table width="600" border="0" align="center" cellpadding="0" cellspacing="0">

					  <tr>

						<td height="68" align="left" valign="top"><a href="'.BASE_URL.'" target="_blank"><img src="'.BASE_URL.'images/logo.png" /></a></td>

						<td align="right" valign="bottom" style="color:#ff8d07;">&nbsp;</td>

					  </tr>

					   <tr>

						<td height="5" colspan="2" align="left" valign="top"></td>

					  </tr>

					  <tr>

						<td height="49" colspan="2" align="left" valign="top" bgcolor="#970198" style="padding:10px 0px 0px 20px; color:#FFFFFF; font-size:24px; font-family:Times New Roman, Times,   serif;">New task from user</td>

					  </tr>

					  <tr>

						<td colspan="2" align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0" style="border:#970198 solid 1px;">

						  <tr>

							<td height="4" colspan="4" align="left" valign="top" bgcolor="#970198"></td>

						  </tr>

						  <tr>

							<td height="8" colspan="4" align="left" valign="top"></td>

						  </tr>

							<tr>

							<td width="258" align="left" valign="top" style="color:#4e4e4e; font-size:13px; line-height:16px; padding:0px 0px 0px 9px;">Hello  '. $techName .',</td>

						  </tr>

							  <tr>

								<td height="8" align="left" valign="top" style="color:#4e4e4e; font-size:11px;  padding:0px 0px 0px 9px;"></td>

							</tr>

						  <tr>

							<td height="10" colspan="4" align="left" valign="top"></td>

						  </tr>

						  <tr>

							<td colspan="4" align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">

							  <tr>

								<td width="8" align="left" valign="top">&nbsp;</td>

								<td width="581" height="140" align="left" valign="top" bgcolor="#f0f0f0" style="border:#c9ccc5 solid 1px; line-height:18px; padding:10px 0px 0px 8px; font-size:11px; font-weight:normal; line-height:27px;">

									<p></p>

									<p><b>'.SITE_NAME.' assigned you in a task from the user '.$userName.'<b></p>

									<p>Task ID: #'.$editId.'</p>

									<p></p>

									<p></p>

									<p></p>

								  </td>

								<td width="7" align="left" valign="top">&nbsp;</td>

							  </tr>

							</table></td>

						  </tr><tr>

							<td colspan="4" align="left" valign="top">&nbsp;</td>

						  </tr>

						</table></td>

					  </tr>

					</table>';

				$mail->Body = $email_content;

				$mail->Send();	

				

				$success = 'Task sent to technician';

								

				$exec_token = mysqli_query($con, 'SELECT fcm_token, device FROM tbl_fcm_tokens WHERE tech_id='.$techId);

				if(isset($exec_token) && mysqli_num_rows($exec_token)>0) {

					while($res_token = mysqli_fetch_array($exec_token)) {

						if($res_token['fcm_token']!='') {

							$title = SITE_NAME.' assigned you in a task from the user '.$userName;

							$body = 'Task ID: #'.$editId;

							if($res_token['device']=='android') {

								

								sendGCM($title, $body, $res_token['fcm_token'], "tech", $editId, $taskRec['user_id'], $userName, "task");

							} elseif($res_token['device']=='ios') {

								sendIosTechPush($title, $body, $res_token['fcm_token'], "tech");

							}

						}

					}

				}	

				

			} else {

				$error = 'Something went wrong';

			}

		} else if(isset($_REQUEST['action']) && $_REQUEST['action']=='assign_extra'){ 

			//additional tech assign

			//check if already added

			$chkExtraRs = mysqli_query($con, "SELECT id FROM tbl_job_extra_tech WHERE job_id = '".$editId."' AND technician_id = '".$techId."'");

			if(mysqli_num_rows($chkExtraRs)>0){

				$error = 'The technician already added';

			} else {

				$taskRec = mysqli_fetch_array(mysqli_query($con, "SELECT technician_id FROM tbl_job WHERE id = '".$editId."'"));	

				$techMainId = $taskRec['technician_id'];			

				$update = mysqli_query($con, "UPDATE tbl_job_extra_tech SET job_type = 'hourly', technician_id = '".$techId."', date = '".date('Y-m-d H:i:s')."', status = 'assigned' WHERE job_id = '".$editId."' AND technician_id = '0'");

				if($update){

					mysqli_query($con, "UPDATE tbl_job SET tech_request = '' WHERE id = '".$editId."'");

					//get user and tech details

					$taskRec = mysqli_fetch_array(mysqli_query($con, "SELECT user_id FROM tbl_job WHERE id = '".$editId."'"));

					$userName = getUserName($taskRec['user_id']);

					

					$techRec = mysqli_fetch_array(mysqli_query($con, "SELECT name, email FROM tbl_technician WHERE id = '".$techId."'"));

					$techName = $techRec['name'];

					$techEmail = $techRec['email'];

					

					//send mail to technician

					//get from email address

					$settingsRec = mysqli_fetch_array(mysqli_query($con, "SELECT from_email FROM tbl_settings WHERE id = '1'"));

					$fromEmail = $settingsRec['from_email'];

					$mail = new PHPMailer();

					$mail->From = $fromEmail;

					$mail->FromName = SITE_NAME;

					$mail->IsHTML(true);	

					$mail->Subject = SITE_NAME.' New sub task';	

			

					

					$mail->AddAddress($techEmail, $techName);					

					//$to_name = $name;			

			

							$email_content = '<table width="600" border="0" align="center" cellpadding="0" cellspacing="0">

						  <tr>

							<td height="68" align="left" valign="top"><a href="'.BASE_URL.'" target="_blank"><img src="'.BASE_URL.'images/logo.png" /></a></td>

							<td align="right" valign="bottom" style="color:#ff8d07;">&nbsp;</td>

						  </tr>

						   <tr>

							<td height="5" colspan="2" align="left" valign="top"></td>

						  </tr>

						  <tr>

							<td height="49" colspan="2" align="left" valign="top" bgcolor="#970198" style="padding:10px 0px 0px 20px; color:#FFFFFF; font-size:24px; font-family:Times New Roman, Times,   serif;">New task from admin</td>

						  </tr>

						  <tr>

							<td colspan="2" align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0" style="border:#970198 solid 1px;">

							  <tr>

								<td height="4" colspan="4" align="left" valign="top" bgcolor="#970198"></td>

							  </tr>

							  <tr>

								<td height="8" colspan="4" align="left" valign="top"></td>

							  </tr>

								<tr>

								<td width="258" align="left" valign="top" style="color:#4e4e4e; font-size:13px; line-height:16px; padding:0px 0px 0px 9px;">Hello  '. $techName .',</td>

							  </tr>

								  <tr>

									<td height="8" align="left" valign="top" style="color:#4e4e4e; font-size:11px;  padding:0px 0px 0px 9px;"></td>

								</tr>

							  <tr>

								<td height="10" colspan="4" align="left" valign="top"></td>

							  </tr>

							  <tr>

								<td colspan="4" align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">

								  <tr>

									<td width="8" align="left" valign="top">&nbsp;</td>

									<td width="581" height="140" align="left" valign="top" bgcolor="#f0f0f0" style="border:#c9ccc5 solid 1px; line-height:18px; padding:10px 0px 0px 8px; font-size:11px; font-weight:normal; line-height:27px;">

										<p></p>

										<p><b>'.SITE_NAME.' assigned you as an extra technician in a task from the user '.$userName.'<b></p>

										<p>Task ID: #'.$editId.'</p>

										<p></p>

										<p></p>

										<p></p>

									  </td>

									<td width="7" align="left" valign="top">&nbsp;</td>

								  </tr>

								</table></td>

							  </tr><tr>

								<td colspan="4" align="left" valign="top">&nbsp;</td>

							  </tr>

							</table></td>

						  </tr>

						</table>';

					$mail->Body = $email_content;

					$mail->Send();	

					

					$success = 'Task sent to technician';

					

					//clear the request field

					mysqli_query($con, "UPDATE `tbl_job` SET `tech_request` = '' WHERE id = '".$editId."'");

					

									

					$exec_token = mysqli_query($con, 'SELECT fcm_token, device FROM tbl_fcm_tokens WHERE tech_id='.$techId);

					if(isset($exec_token) && mysqli_num_rows($exec_token)>0) {

						while($res_token = mysqli_fetch_array($exec_token)) {	

							if($res_token['fcm_token']!='') {							

								$title = SITE_NAME.' assigned you as an extra technician in a task from the user '.$userName;

								$body = 'Task ID: #'.$editId;

								if($res_token['device']=='android') {

									sendGCM($title, $body, $res_token['fcm_token'], "tech", $editId, $taskRec['user_id'], $userName, "task");

								} else {

									sendIosTechPush($title, $body, $res_token['fcm_token'], "tech");

								}

							}

						}

					}

					

					// push for main technician

					if(isset($taskRec['technician_id']) && $taskRec['technician_id']>0) {

						$exec_main_tech_token = mysqli_query($con, 'SELECT fcm_token, device FROM tbl_fcm_tokens WHERE tech_id='.$taskRec['technician_id']);						

						if(isset($exec_main_tech_token) && mysqli_num_rows($exec_main_tech_token)>0) {

							while($res_main_tech_token = mysqli_fetch_array($exec_main_tech_token)) {	

								if($res_main_tech_token['fcm_token']!='') {							

									$title = 'Extra tech you requested is assigned';

									$body = 'Task ID: #'.$editId;

									$mainTechName = getTechnicianName($taskRec['technician_id']);

									if($res_main_tech_token['device']=='android') {

										sendGCM($title, $body, $res_main_tech_token['fcm_token'], "tech", $editId, $taskRec['technician_id'], $mainTechName, "task");

									} else {

										sendIosTechPush($title, $body, $res_main_tech_token['fcm_token'], "tech");

									}

								}

							}

						}

					}

					

					

				} else {

					$error = 'Something went wrong';

				}

								

			}

			

			

		}



					

	}		



	if(isset($success) && $success!=''){

		header('Location:index.php?page=view_task&id='.$editId.'&p='.$p.'&success='.$success);

		exit();

	}



	



	$editRs = mysqli_query($con, "SELECT * FROM tbl_job WHERE id = '".$editId."'");

	if(mysqli_num_rows($editRs)>0){

		$resEdit = mysqli_fetch_array($editRs);			

		$taskId = $editId;

		//check if extra technician added		

		$extraTechRs = mysqli_query($con, "SELECT * FROM tbl_job_extra_tech WHERE job_id = '".$editId."'");

				

		$dispStat = 'hide';

		//dispute_status	

		$chkRs = mysqli_query($con, "SELECT id FROM tbl_job_disputes WHERE task_id = '".$taskId."'");

		if(mysqli_num_rows($chkRs)>0){

					

		} else {		

			$dispStat = 'show';

			

		}

		

		//get disputes if any

		$disputeRs = mysqli_query($con, "SELECT * FROM tbl_job_disputes WHERE task_id = '".$taskId."' ORDER BY id");

		

		//rate details

		$fixedRate = $resEdit['fixed_rate'];

		$totChargePerHour = $totHours = $netTotal = $adminDiscount = 0;

		if($fixedRate>0){

			//echo $resEdit['status']; 

			$totCharge = $fixedRate;

			$techId = $resEdit['technician_id'];

			$mainTechComplteRec = mysqli_fetch_array(mysqli_query($con, "SELECT * FROM tbl_task_completed WHERE tech_id = '".$techId."' AND task_id = '".$taskId."'"));

				$mainTechTaskAmount = $mainTechComplteRec['task_amount'];

				$mainTechAdminCommission = $mainTechComplteRec['admin_commission'];

				$mainTechTaskIncome = $mainTechComplteRec['tech_income'];

				$mainTechTaskTime = $mainTechComplteRec['total_hours'];

		} else {

			$taskCatId = $resEdit['category_id'];		

			//get user image

			$techId = $resEdit['technician_id'];

			$techRec = mysqli_fetch_array(mysqli_query($con, "SELECT type, image1, city FROM tbl_technician WHERE id = '".$techId."'"));

			$ratePerHour = getTechHourlyRate($resEdit['technician_id'], $taskCatId);

			$visitCharge = getTechVisitCharge($resEdit['technician_id'], $taskCatId);

			$techDispatchType = getTechDispatchType($techId);

			$minCharge = getTechMinimumCharge($resEdit['technician_id'], $taskCatId);

			

			//get total extra tech charges if any

			$totalExtraTechAmount = 0;

						

			if($resEdit['status']=='completed' || $resEdit['status']=='user_paid' || $resEdit['status']=='paid'){

				$completedRec = mysqli_fetch_array(mysqli_query($con, "SELECT SUM(task_amount) AS amount FROM tbl_task_completed WHERE task_id = '".$taskId."'"));

				$totCharge = $completedRec['amount'];

				$extraCompleteRs = mysqli_query($con, "SELECT SUM(task_amount) AS amount FROM tbl_task_completed WHERE task_id = '".$taskId."' AND tech_id != '".$techId."'");

				if(mysqli_num_rows($extraCompleteRs)>0){

					$extraCompleteRec = mysqli_fetch_array($extraCompleteRs);

					$totalExtraTechAmount = $extraCompleteRec['amount'];

				}

				

				$mainTechComplteRec = mysqli_fetch_array(mysqli_query($con, "SELECT * FROM tbl_task_completed WHERE tech_id = '".$techId."' AND task_id = '".$taskId."'"));

				$mainTechTaskAmount = $mainTechComplteRec['task_amount'];

				$mainTechAdminCommission = $mainTechComplteRec['admin_commission'];

				$mainTechTaskIncome = $mainTechComplteRec['tech_income'];

				$mainTechTaskTime = $mainTechComplteRec['total_hours'];		

				

			}		

			

		}			



		if($resEdit['status']=='completed' || $resEdit['status']=='user_paid' || $resEdit['status']=='paid'){

			//get completed time. we have to show the rate change button for 1 day after task completion

			$curDateTime = date('Y-m-d H:i:s');

			$taskStatRec = mysqli_fetch_array(mysqli_query($con, "SELECT date FROM tbl_job_status WHERE task_id = '".$taskId."' AND status = 'completed'"));

			$completedTime = $taskStatRec['date'];

									

			$timeDiffRec = mysqli_fetch_array(mysqli_query($con, "SELECT TIMESTAMPDIFF(SECOND, '".$completedTime."', '".$curDateTime."') AS seconds"));

			$totSeconds = $timeDiffRec['seconds'];

			$buttonHours = $totSeconds/3600;

		}

	

		$adminDiscount = $resEdit['admin_discount'];

		$adminExtra = $resEdit['admin_extra'];		

		$techDiscount = $resEdit['tech_discount'];

		//get task notes if any

		$taskNotesRs = mysqli_query($con, "SELECT * FROM tbl_job_notes WHERE task_id = '".$taskId."'");

		

		

		

		//create sms text		

		$taskTechId = $resEdit['technician_id'];

		$adminDiscount = $resEdit['admin_discount'];

		$adminExtra = $resEdit['admin_extra'];

		$techDiscount = $resEdit['tech_discount'];

			

		$techPhoneRec = mysqli_fetch_array(mysqli_query($con, "SELECT name, phone FROM tbl_technician WHERE id = '".$taskTechId."'"));

		$techPhone = $techPhoneRec['phone'];

		$techName = $techPhoneRec['name'];

			

		//get total task amount

		$taskCompletedRec = mysqli_fetch_array(mysqli_query($con, "SELECT SUM(task_amount) AS amount FROM tbl_task_completed WHERE task_id = '".$taskId."'")); 

		$totalTaskAmountSms = $taskCompletedRec['amount'];

		$totalTaskAmountSms = $totalTaskAmountSms+$adminExtra-$adminDiscount-$techDiscount;

		$smsMessage = urlencode('Task: #'.$taskId.' completed successfully. Thanks for choosing '.SITE_NAME.'. Pay Mr.'.$techName.' Total Amount: Rs.'.$totalTaskAmountSms.' or Else please pay online (Internet handling charges applicable extra). Please call 83 38 83 83 83, select option 2 for any clarification.');

		

	} else {

		?>

	        <script language="javascript" type="text/javascript">	

				alert("The task does not exist!");		

				window.location='index.php?page=manage_user_tasks';

			</script>

<?php	}

	

}   







?>