<?php

/***************************************************************************
 *   copyright            : (C) 2017 PCRepairTracker.com
 *   email                : info@pcrepairtracker.com
 *   This program is a copyrighted work.
 *   Please see the license.txt file for details
 *
 ***************************************************************************/


function loadwonotes($woid, $notetype) {

require("deps.php");
require_once("common.php");

$rs_findnotes = "SELECT * FROM wonotes WHERE woid = '$woid' AND notetype = '$notetype' ORDER BY notetime ASC";
$rs_result_n = mysqli_query($rs_connect, $rs_findnotes);
$bubblego = "first";
$usercomm = array();
while($rs_result_qn = mysqli_fetch_object($rs_result_n)) {
$thenote = parseforlinks(nl2br("$rs_result_qn->thenote"));
$noteuser = "$rs_result_qn->noteuser";
$notetype = "$rs_result_qn->notetype";
$noteid = "$rs_result_qn->noteid";
$notetime2 = "$rs_result_qn->notetime";

if ($notetype == 1) {
$switchnotetype = 0;
$thenotearea = "technotearea";
} else {
$switchnotetype = 1;
$thenotearea = "custnotearea";
}


$notetime = pcrtdate("$pcrt_time", "$notetime2")." ".pcrtdate("$pcrt_shortdate", "$notetime2");

if(array_key_exists("$noteuser",$usercomm)) {
$bubblego = $usercomm[$noteuser];
} else {
if($bubblego == "first") {
$bubblego = "left";
} elseif ($prevuser == "noteuser") {
$bubblego = "$thedir";
} else {
$bubblego = "$thediropp";
}
}

if ("$bubblego" == "left") {
echo "<table style=\"width:100%\"><tr><td style=\"width:125px;text-align:center;vertical-align:top;\">";
echo "<span class=\"boldme\">$noteuser</span>";
echo "<br><span class=\"sizemesmaller\">$notetime</span>";
echo "<br><table><tr><td><button onClick=\"parent.location='pc.php?func=editnote&woid=$woid&noteid=$noteid'\" class=button style=\"padding:0px;font-size:10px;\"><i class=\"fa fa-edit fa-lg\"></i>".pcrtlang("edit")."</button></td>";
echo "<td><form action=\"pc.php?func=deletenote&woid=$woid&noteid=$noteid&notetype=$notetype&ajaxcall=yes\" id=\"custnotedelete$noteid\" method=post><button type=submit class=ibutton style=\"padding:0px;font-size:10px;\"><i class=\"fa fa-times fa-lg\"></i>".pcrtlang("delete")."</button></form></td>";
echo "<td><button onClick=\"parent.location='pc.php?func=convertnote&woid=$woid&noteid=$noteid&notetype=$switchnotetype&touch=no'\" class=button style=\"padding:0px;font-size:10px;\"><i class=\"fa fa-exchange fa-lg\"></i>".pcrtlang("switch")."</button></td></tr></table>";
echo "</td><td>";
echo "<div class=\"wonote left\">$thenote</div>";
echo "</td></tr></table>";


$prevuser = "$noteuser";
$thedir = "left";
$thediropp = "right";
} else {
echo "<table style=\"width:100%\"><tr><td>";
echo "<div class=\"wonote right\">$thenote</div></td>";
echo "<td style=\"width:125px;text-align:center;align:top\">";
echo "<span class=\"boldme\">$noteuser</span>";
echo "<br><span class=\"sizemesmaller\">$notetime</span>";
echo "<br><table><tr><td><button onClick=\"parent.location='pc.php?func=editnote&woid=$woid&noteid=$noteid'\" class=button style=\"padding:0px;font-size:10px;\"><i class=\"fa fa-edit fa-lg\"></i>".pcrtlang("edit")."</a></td>";
echo "<td><form action=\"pc.php?func=deletenote&woid=$woid&noteid=$noteid&notetype=$notetype&ajaxcall=yes\" id=\"custnotedelete$noteid\" method=post><button type=submit class=ibutton style=\"padding:0px;font-size:10px;\"><i class=\"fa fa-times\"></i>".pcrtlang("delete")."</button></form></td>";
echo "<td><button onClick=\"parent.location='pc.php?func=convertnote&woid=$woid&noteid=$noteid&notetype=$switchnotetype&touch=no'\" class=button style=\"padding:0px;font-size:10px;\"><i class=\"fa fa-exchange fa-lg\"></i>".pcrtlang("switch")."</button></td></tr></table>";
echo "</td></tr></table>";
$prevuser = "$noteuser";
$thedir = "right";
$thediropp = "left";
}

$usercomm[$noteuser] = "$thedir";

?>
<script type='text/javascript'>
$(document).ready(function(){
$('#custnotedelete<?php echo "$noteid"; ?>').submit(function(e) { // catch the form's submit event
        e.preventDefault();
        	$.ajax({ // create an AJAX call...
		beforeSend: function(request) {
			if (confirm('<?php echo pcrtlang("Are you sure you wish to delete this..."); ?>')) {
			} else {
			return false;
			}		
		},
        	data: $(this).serialize(), // get the form data
        	type: $(this).attr('method'), // GET or POST
        	url: $(this).attr('action'), // the file to call
        	success: function(response) { // on success..
            	$('#<?php echo "$thenotearea"; ?>').html(response); // update the DIV
	}
    });
});
});
</script>
<?php




}

}

#####################################


function displaymessages($phonenumbers,$emails,$woid,$pickup,$dropoff) {

require("deps.php");
require_once("common.php");

$phnumbers = explode_list($phonenumbers);
$emailsexploded = explode_list_email($emails);

foreach ($phnumbers as $key=>&$value) {
    if (strlen($value) < 7) {
        unset($phnumbers[$key]);
    }
}

reset($phnumbers);

if (function_exists('date_default_timezone_set')) {
date_default_timezone_set("$pcrt_timezone");
}
$currentdatetime = date('Y-m-d H:i:s');


if($pickup == "0000-00-00 00:00:00") {
$pickup2 =  date('Y-m-d H:i:s', (strtotime($currentdatetime) + 86400));
} else {
$pickup2 =  date('Y-m-d H:i:s', (strtotime($pickup) + 604800));
}

$dropoff2 =  date('Y-m-d H:i:s', (strtotime($dropoff) - 604800));

$numberstosearch = "";

foreach($phnumbers as $key => $val) {
$numberstosearch .= " OR  TRIM(REPLACE(REPLACE(messagefrom,'-',''),' ',''))  LIKE '%".substr("$val", -7)."'";
$numberstosearch .= " OR messagefrom LIKE '%".substr("$val", -7)."'";
}

if($woid != 0) {
$numberstosearch .= " OR woid = '$woid'";
}

$rs_findowner = "SELECT * FROM pc_wo WHERE woid = '$woid'";
$rs_result2 = mysqli_query($rs_connect, $rs_findowner);
$rs_result_q2 = mysqli_fetch_object($rs_result2);
$pcid = "$rs_result_q2->pcid";

$rs_findgroup = "SELECT * FROM pc_owner WHERE pcid = '$pcid'";
$rs_resultg2 = mysqli_query($rs_connect, $rs_findgroup);
$rs_resultg_q2 = mysqli_fetch_object($rs_resultg2);
$pcgroupid = "$rs_resultg_q2->pcgroupid";

if($pcgroupid != 0) {
$numberstosearch .= " OR groupid = '$pcgroupid'";
}


foreach($emailsexploded as $key => $val) {
if (filter_var($val, FILTER_VALIDATE_EMAIL)) {
$numberstosearch .= " OR messagefrom = '$val'";
}
}

$rs_findmessages = "SELECT * FROM messages WHERE 1=2 $numberstosearch AND messagedatetime > '$dropoff2' AND messagedatetime < '$pickup2' 
ORDER BY messagedatetime DESC";


$rs_result_n = mysqli_query($rs_connect, $rs_findmessages);

echo "<table class=standard>";

while($rs_result_qn = mysqli_fetch_object($rs_result_n)) {
$messageid = "$rs_result_qn->messageid";
$messagebody = "$rs_result_qn->messagebody";
$messagefrom = "$rs_result_qn->messagefrom";
$messagevia = "$rs_result_qn->messagevia";
$messagedatetime2 = "$rs_result_qn->messagedatetime";
$mediaurls = serializedarraytest("$rs_result_qn->mediaurls");

$imageatt = "";
foreach($mediaurls as $key => $val) {
$imageatt .= "<a href=\"$val\" class=\"assetpreview\"><img src=$val height=50 class=\"assetimage\"></a> ";
}

if($messagevia == "call") {
$viaicon = "<i class=\"fa fa-phone fa-lg fa-fw\"></i>";
} elseif($messagevia == "sms") {
$viaicon = "<i class=\"fa fa-mobile fa-lg fa-fw\"></i>";
} elseif($messagevia == "im") {
$viaicon = "<i class=\"fa fa-comment fa-lg fa-fw\"></i>";
} elseif($messagevia == "email") {
$viaicon = "<i class=\"fa fa-envelope-o fa-lg\"></i>";
} else {
$viaicon = "";
}


$messagedatetime = pcrtdate("$pcrt_time", "$messagedatetime2")." ".pcrtdate("$pcrt_shortdate", "$messagedatetime2");

$messagedirection = "$rs_result_qn->messagedirection";
if($messagedirection == "in") {
echo "<tr><td><i class=\"fa fa-reply fa-lg colormeblue\"></i> $viaicon <span>$messagefrom</span> </td>";
echo "<td><i class=\"fa fa-clock-o fa-fw fa-lg\"></i> <span>$messagedatetime</span> </td>";
echo "<td style=\"width:60%\"><div class=showmoreless><i class=\"fa fa-comment fa-lg\"></i> $messagebody</div>$imageatt</td><td>";
if($messagevia == "call") {
echo "<a href=\"pc.php?func=deletemessage&woid=$woid&messageid=$messageid\" class=\"linkbuttonsmall linkbuttonred radiusall\" onClick=\"return confirm('".pcrtlang("Are you sure you wish to delete this message?")."');\"><i class=\"fa fa-trash fa-lg\"></i></a>";
}
echo "</td></tr>";
} elseif($messagedirection == "out") {
echo "<tr><td>$viaicon <i class=\"fa fa-share fa-fw fa-lg\"></i> <span>$messagefrom</span> </td>";
echo "<td><i class=\"fa fa-clock-o fa-lg\"></i> <span>$messagedatetime</span> </td>";
echo "<td style=\"width:60%\"><div class=showmoreless><i class=\"fa fa-comment fa-lg\"></i> $messagebody</div>$imageatt</td><td>";
if($messagevia == "call") {
echo "<a href=\"pc.php?func=deletemessage&woid=$woid&messageid=$messageid\" class=\"linkbuttonsmall linkbuttonred radiusall\" onClick=\"return confirm('".pcrtlang("Are you sure you wish to delete this message?")."');\" ><i class=\"fa fa-trash fa-lg\"></i></a>";
}
echo "</td></tr>";
} else {
echo "<tr><td><i class=\"fa fa-user-md fa-lg\"></i> <span>$messagefrom</span> </td>";
echo "<td><i class=\"fa fa-clock-o fa-fw fa-lg\"></i> <span>$messagedatetime</span> </td>";
echo "<td style=\"width:60%\"><div class=showmoreless><i class=\"fa fa-comment fa-lg\"></i> $messagebody</div> </td><td>";
if($messagevia == "call") {
echo "<a href=\"pc.php?func=deletemessage&woid=$woid&messageid=$messageid\" class=\"linkbuttonsmall linkbuttonred radiusall\" onClick=\"return confirm('".pcrtlang("Are you sure you wish to delete this message?")."');\" ><i class=\"fa fa-trash fa-lg\"></i></a>";
}
echo "</td></tr>";
}

}

echo "</table>";

?>
<script src="jq/readmore.js"></script>
<script src="jq/facebox.js" type="text/javascript"></script>
<script>
$('.showmoreless').readmore({
  speed: 250,
  collapsedHeight: 30,
  lessLink: '<a href="#" class=\"linkbuttontiny linkbuttongray radiusall\"><i class=\"fa fa-minus-circle fa-lg\"></i> <?php echo pcrtlang("Show Less"); ?></a>',
  moreLink: '<a href="#" class=\"linkbuttontiny linkbuttongray radiusall\"><i class=\"fa fa-plus-circle fa-lg\"></i>  <?php echo pcrtlang("Show More"); ?></a>'
});
</script>
<?php


}


################################################

function loadchecks($woid) {

require("deps.php");
require_once("common.php");

$pcwo = $woid;

$rs_findwo = "SELECT * FROM pc_wo WHERE woid = '$woid'";
$rs_resultw = mysqli_query($rs_connect, $rs_findwo);
$rs_resultw_q = mysqli_fetch_object($rs_resultw);
$wochecks = "$rs_resultw_q->wochecks";
$pcid = "$rs_resultw_q->pcid";

$rs_findpc = "SELECT * FROM pc_owner WHERE pcid = '$pcid'";
$rs_result = mysqli_query($rs_connect, $rs_findpc);
$rs_result_q = mysqli_fetch_object($rs_result);
$mainassettypeidindb = "$rs_result_q->mainassettypeid";


$rs_sq = "SELECT * FROM mainassettypes WHERE mainassettypeid = '$mainassettypeidindb'";
$rs_result1 = mysqli_query($rs_connect, $rs_sq);
$rs_result_q1 = mysqli_fetch_object($rs_result1);
$mainassetchecksindb = serializedarraytest("$rs_result_q1->mainassetchecks");

start_box();



$wochecks = serializedarraytest("$wochecks");


echo "<table class=standard>";

echo "<tr><th colspan=2>".pcrtlang("Pre Checks")."</th><th colspan=2>".pcrtlang("Post Checks")."</th></tr>";

foreach($mainassetchecksindb as $key => $val) {

$rs_checks = "SELECT * FROM checks WHERE checkid = '$val'";
$rs_checksq = mysqli_query($rs_connect, $rs_checks);
$rs_result_cq = mysqli_fetch_object($rs_checksq);
$checkid = "$rs_result_cq->checkid";
$checkname = "$rs_result_cq->checkname";


if (in_array($checkid, $mainassetchecksindb)) {

if(array_key_exists("$checkid", $wochecks)) {
if(isset($wochecks[$checkid][0])) {
$precheck = $wochecks[$checkid][0];
} else {
$precheck = 0;
}

if(isset($wochecks[$checkid][1])) {
$postcheck = $wochecks[$checkid][1];
} else {
$postcheck = 0;
}
} else {
$precheck = 0;
$postcheck = 0;
}

# not checked = 0, not applicable = 1, pass = 2, fail = 3

echo "<tr><td>$checkname</td><td>";
if($precheck == 0) {
echo "<a href=pc.php?func=setcheck&checktype=0&checkvalue=0&woid=$pcwo&checkid=$checkid class=\"linkbuttonsmall linkbuttonblack radiusleft nopointerevents\"
title=\"".pcrtlang("not checked")."\"><i class=\"fa fa-square-o  fa-lg\"></i></a>";
echo "<a href=pc.php?func=setcheck&checktype=0&checkvalue=1&woid=$pcwo&checkid=$checkid class=\"linkbuttonsmall linkbuttongray\"
title=\"".pcrtlang("does not apply")."\"><i class=\"fa fa-minus fa-lg\"></i></a>";
echo "<a href=pc.php?func=setcheck&checktype=0&checkvalue=2&woid=$pcwo&checkid=$checkid class=\"linkbuttonsmall linkbuttongray\"
title=\"".pcrtlang("pass")."\"><i class=\"fa fa-check fa-lg\"></i></a>";
echo "<a href=pc.php?func=setcheck&checktype=0&checkvalue=3&woid=$pcwo&checkid=$checkid class=\"linkbuttonsmall linkbuttongray radiusright\"
title=\"".pcrtlang("fail")."\"><i class=\"fa fa-warning fa-lg\"></i></a>";
} elseif($precheck == 1) {
echo "<a href=pc.php?func=setcheck&checktype=0&checkvalue=0&woid=$pcwo&checkid=$checkid class=\"linkbuttonsmall linkbuttongray radiusleft\"
title=\"".pcrtlang("not checked")."\"><i class=\"fa fa-square-o  fa-lg\"></i></a>";
echo "<a href=pc.php?func=setcheck&checktype=0&checkvalue=1&woid=$pcwo&checkid=$checkid class=\"linkbuttonsmall linkbuttonblack nopointerevents\"
title=\"".pcrtlang("does not apply")."\"><i class=\"fa fa-minus fa-lg\"></i></a>";
echo "<a href=pc.php?func=setcheck&checktype=0&checkvalue=2&woid=$pcwo&checkid=$checkid class=\"linkbuttonsmall linkbuttongray\"
title=\"".pcrtlang("pass")."\"><i class=\"fa fa-check fa-lg\"></i></a>";
echo "<a href=pc.php?func=setcheck&checktype=0&checkvalue=3&woid=$pcwo&checkid=$checkid class=\"linkbuttonsmall linkbuttongray radiusright\"
title=\"".pcrtlang("fail")."\"><i class=\"fa fa-warning fa-lg\"></i></a>";
} elseif($precheck == 2) {
echo "<a href=pc.php?func=setcheck&checktype=0&checkvalue=0&woid=$pcwo&checkid=$checkid class=\"linkbuttonsmall linkbuttongray radiusleft\"
title=\"".pcrtlang("not checked")."\"><i class=\"fa fa-square-o  fa-lg\"></i></a>";
echo "<a href=pc.php?func=setcheck&checktype=0&checkvalue=1&woid=$pcwo&checkid=$checkid class=\"linkbuttonsmall linkbuttongray\"
title=\"".pcrtlang("does not apply")."\"><i class=\"fa fa-minus fa-lg\"></i></a>";
echo "<span class=\"linkbuttonsmall linkbuttongreenlabel\"
title=\"".pcrtlang("pass")."\"><i class=\"fa fa-check fa-lg\"></i></span>";
echo "<a href=pc.php?func=setcheck&checktype=0&checkvalue=3&woid=$pcwo&checkid=$checkid class=\"linkbuttonsmall linkbuttongray radiusright\"
title=\"".pcrtlang("fail")."\"><i class=\"fa fa-warning fa-lg\"></i></a>";
} elseif($precheck == 3) {
echo "<a href=pc.php?func=setcheck&checktype=0&checkvalue=0&woid=$pcwo&checkid=$checkid class=\"linkbuttonsmall linkbuttongray radiusleft\"
title=\"".pcrtlang("not checked")."\"><i class=\"fa fa-square-o fa-lg\"></i></a>";
echo "<a href=pc.php?func=setcheck&checktype=0&checkvalue=1&woid=$pcwo&checkid=$checkid class=\"linkbuttonsmall linkbuttongray\"
title=\"".pcrtlang("does not apply")."\"><i class=\"fa fa-minus fa-lg\"></i></a>";
echo "<a href=pc.php?func=setcheck&checktype=0&checkvalue=2&woid=$pcwo&checkid=$checkid class=\"linkbuttonsmall linkbuttongray\"
title=\"".pcrtlang("pass")."\"><i class=\"fa fa-check fa-lg\"></i></a>";
echo "<span class=\"linkbuttonsmall linkbuttonredlabel radiusright\"
title=\"".pcrtlang("fail")."\"><i class=\"fa fa-warning fa-lg\"></i></span>";
} else {
}


echo "</td><td>$checkname</td><td>";

if($postcheck == 0) {
echo "<a href=pc.php?func=setcheck&checktype=1&checkvalue=0&woid=$pcwo&checkid=$checkid class=\"linkbuttonsmall linkbuttonblack radiusleft nopointerevents\"
title=\"".pcrtlang("not checked")."\"><i class=\"fa fa-square-o  fa-lg\"></i></a>";
echo "<a href=pc.php?func=setcheck&checktype=1&checkvalue=1&woid=$pcwo&checkid=$checkid class=\"linkbuttonsmall linkbuttongray\"
title=\"".pcrtlang("does not apply")."\"><i class=\"fa fa-minus fa-lg\"></i></a>";
echo "<a href=pc.php?func=setcheck&checktype=1&checkvalue=2&woid=$pcwo&checkid=$checkid class=\"linkbuttonsmall linkbuttongray\"
title=\"".pcrtlang("pass")."\"><i class=\"fa fa-check fa-lg\"></i></a>";
echo "<a href=pc.php?func=setcheck&checktype=1&checkvalue=3&woid=$pcwo&checkid=$checkid class=\"linkbuttonsmall linkbuttongray radiusright\"
title=\"".pcrtlang("fail")."\"><i class=\"fa fa-warning fa-lg\"></i></a>";
} elseif($postcheck == 1) {
echo "<a href=pc.php?func=setcheck&checktype=1&checkvalue=0&woid=$pcwo&checkid=$checkid class=\"linkbuttonsmall linkbuttongray radiusleft\"
title=\"".pcrtlang("not checked")."\"><i class=\"fa fa-square-o  fa-lg\"></i></a>";
echo "<a href=pc.php?func=setcheck&checktype=1&checkvalue=1&woid=$pcwo&checkid=$checkid class=\"linkbuttonsmall linkbuttonblack nopointerevents\"
title=\"".pcrtlang("does not apply")."\"><i class=\"fa fa-minus fa-lg\"></i></a>";
echo "<a href=pc.php?func=setcheck&checktype=1&checkvalue=2&woid=$pcwo&checkid=$checkid class=\"linkbuttonsmall linkbuttongray\"
title=\"".pcrtlang("pass")."\"><i class=\"fa fa-check fa-lg\"></i></a>";
echo "<a href=pc.php?func=setcheck&checktype=1&checkvalue=3&woid=$pcwo&checkid=$checkid class=\"linkbuttonsmall linkbuttongray radiusright\"
title=\"".pcrtlang("fail")."\"><i class=\"fa fa-warning fa-lg\"></i></a>";
} elseif($postcheck == 2) {
echo "<a href=pc.php?func=setcheck&checktype=1&checkvalue=0&woid=$pcwo&checkid=$checkid class=\"linkbuttonsmall linkbuttongray radiusleft\"
title=\"".pcrtlang("not checked")."\"><i class=\"fa fa-square-o  fa-lg\"></i></a>";
echo "<a href=pc.php?func=setcheck&checktype=1&checkvalue=1&woid=$pcwo&checkid=$checkid class=\"linkbuttonsmall linkbuttongray\"
title=\"".pcrtlang("does not apply")."\"><i class=\"fa fa-minus fa-lg\"></i></a>";
echo "<span class=\"linkbuttonsmall linkbuttongreenlabel\"
title=\"".pcrtlang("pass")."\"><i class=\"fa fa-check fa-lg\"></i></span>";
echo "<a href=pc.php?func=setcheck&checktype=1&checkvalue=3&woid=$pcwo&checkid=$checkid class=\"linkbuttonsmall linkbuttongray radiusright\"
title=\"".pcrtlang("fail")."\"><i class=\"fa fa-warning fa-lg\"></i></a>";
} elseif($postcheck == 3) {
echo "<a href=pc.php?func=setcheck&checktype=1&checkvalue=0&woid=$pcwo&checkid=$checkid class=\"linkbuttonsmall linkbuttongray radiusleft\"
title=\"".pcrtlang("not checked")."\"><i class=\"fa fa-square-o fa-lg\"></i></a>";
echo "<a href=pc.php?func=setcheck&checktype=1&checkvalue=1&woid=$pcwo&checkid=$checkid class=\"linkbuttonsmall linkbuttongray\"
title=\"".pcrtlang("does not apply")."\"><i class=\"fa fa-minus fa-lg\"></i></a>";
echo "<a href=pc.php?func=setcheck&checktype=1&checkvalue=2&woid=$pcwo&checkid=$checkid class=\"linkbuttonsmall linkbuttongray\"
title=\"".pcrtlang("pass")."\"><i class=\"fa fa-check fa-lg\"></i></a>";
echo "<span class=\"linkbuttonsmall linkbuttonredlabel radiusright\"
title=\"".pcrtlang("fail")."\"><i class=\"fa fa-warning fa-lg\"></i></span>";
} else {

}


echo "</td></tr>";
}

}


echo "</table>";

stop_box();




}




?>
