public function issueStatByProd()
 {
     $issues = Issue::find_all();
     $results = array(array());
     $machineIssues = array();
     $machineIssuesCount = array();
     $finalData = array();
     foreach ($issues as $issue) {
         $logCount = Issuelog::count_by_issue($issue->id);
         $machineIssues[] = $issue->issue_name;
         $machineIssuesCount[] = $logCount;
         $finalData[] = "'" . $issue->issue_name . "'" . "," . $logCount . "_";
     }
     $results = $finalData;
     // =  $machineIssues
     return $results;
 }
 public function create()
 {
     //if( !empty($_POST["issue"]) ){
     $newticket = new Ticket();
     $error = array();
     if (isset($_POST["cname"])) {
         if ($_POST['cname'] != "") {
             $newticket->contact_name = $_POST['cname'];
         } else {
             array_push($error, "Name");
         }
     } else {
         array_push($error, "Name");
     }
     if (isset($_POST["terminal_id"])) {
         if ($_POST['terminal_id'] != "") {
             $newticket->terminal_id = $_POST['terminal_id'];
         } else {
             array_push($error, "Terminal ID");
         }
     } else {
         array_push($error, "Terminal ID");
     }
     if (isset($_POST["atm_type"])) {
         if ($_POST['atm_type'] != "") {
             $newticket->atm_type = $_POST['atm_type'];
         } else {
             array_push($error, "ATM TYPE");
         }
     } else {
         array_push($error, "ATM TYPE");
     }
     if (isset($_POST["branch"])) {
         if ($_POST['branch'] != "") {
             $newticket->branch = $_POST['branch'];
         } else {
             array_push($error, "Branch");
         }
     } else {
         array_push($error, "Branch");
     }
     if (isset($_POST["email"])) {
         if ($_POST['email'] != "" && preg_match("/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})\$/i", $_POST["email"])) {
             $newticket->contact_email = $_POST['email'];
         } else {
             array_push($error, "Email");
         }
     } else {
         array_push($error, "Email");
     }
     if (isset($_POST["phone"])) {
         if ($_POST['phone'] != "" && ctype_digit($_POST['phone'])) {
             $newticket->contact_phone = $_POST['phone'];
         } else {
             array_push($error, "Phone Number");
         }
     } else {
         array_push($error, "Phone Number");
     }
     if (isset($_POST["location"])) {
         $newticket->location = $_POST["location"];
     } else {
         array_push($error, "Location");
     }
     if (isset($_POST["plevel"])) {
         $newticket->priority = $_POST["plevel"];
     } else {
         array_push($error, "Priority");
     }
     if (isset($_POST["issue"])) {
         $newticket->issue = htmlspecialchars($_POST['issue']);
     } else {
         array_push($error, "Issue");
     }
     if (isset($_POST["subject"])) {
         $newticket->subject = htmlspecialchars($_POST['subject']);
     } else {
         $newticket->subject = htmlspecialchars($_POST['issue']);
     }
     $newticket->department = $_POST["dept"];
     $newticket->datecreated = $newticket->datemodified = date("Y-m-d H:i:s");
     $newticket->status = "Open";
     $newticket->client_id = $_SESSION["client_ident"];
     if (!empty($_FILES['fupload']['name']) && $_FILES['fupload']['error'] == 0) {
         //if file upload is set
         move_uploaded_file($_FILES['fupload']['tmp_name'], "public/uploads/" . basename($_FILES['fupload']['name']));
         $ext = pathinfo($_FILES['fupload']['name'], PATHINFO_EXTENSION);
         $new_name = uniqid() . "_" . time() . $ext;
         //new name for the image
         rename("public/uploads/" . basename($_FILES['fupload']['name']), "public/uploads/" . $new_name);
         $photo = $new_name;
         $newticket->file = $photo;
     }
     if (empty($error)) {
         if (Cproduct::find_by_terminal($_POST['terminal_id'])) {
             $cproduct = Cproduct::find_by_terminal($_POST['terminal_id']);
             $cproduct->client_id = $_SESSION["client_ident"];
             //$cproduct->client_name				=	$_POST["clientname"];
             $cproduct->prod_id = $_POST['terminal_id'];
             $cproduct->terminal_id = $_POST['terminal_id'];
             $cproduct->prod_name = $_POST['product_name'];
             $cproduct->branch = $_POST['branch'];
             $cproduct->atm_type = $_POST['atm_type'];
             $cproduct->install_status = 1;
             $cproduct->status = 1;
             $cproduct->client_id = $_SESSION['client_ident'];
             $cproduct->install_city = $_POST['city'];
             $cproduct->install_address = $_POST['location'];
             $cproduct->install_state = $_POST['state'];
             $cproduct->update();
             $newticket->prod_id = $cproduct->id;
         } else {
             $cproduct = new Cproduct();
             $cproduct->client_id = $_SESSION["client_ident"];
             //$cproduct->client_name				=	$_POST["clientname"];
             $cproduct->prod_id = $_POST['terminal_id'];
             $cproduct->terminal_id = $_POST['terminal_id'];
             $cproduct->prod_name = $_POST['product_name'];
             $cproduct->branch = $_POST['branch'];
             $cproduct->atm_type = $_POST['atm_type'];
             $cproduct->install_status = 1;
             $cproduct->status = 1;
             $cproduct->datecreated = date("Y-m-d H:i:s");
             $cproduct->install_city = $_POST['city'];
             $cproduct->install_address = $_POST['location'];
             $cproduct->install_state = $_POST['state'];
             $cproduct->create();
             $newticket->prod_id = $cproduct->id;
             $newticket->prod_id = $cproduct->id;
         }
         // }
         $newticket->terminal_id = $cproduct->terminal_id;
         if ($newticket->create()) {
             $ddticket = Ticket::find_by_id($newticket->id);
             $issue = array();
             $objIssue = Issue::find_all();
             $ddticket->subject = "";
             foreach ($objIssue as $objI) {
                 if (isset($_POST[$objI->issue_accronym])) {
                     $issueLog = new Issuelog();
                     $issueLog->ticket_id = $newticket->id;
                     $issueLog->issue_id = $objI->id;
                     $issueLog->issue_accronym = $objI->issue_accronym;
                     $issueLog->issue_name = $objI->issue_name;
                     $issueLog->prod_id = $newticket->prod_id;
                     $issueLog->datecreated = date("Y-m-d H:i:s");
                     $ddticket->subject .= $_POST[$objI->issue_accronym] . ", ";
                     $issueLog->create();
                 }
             }
             $ddticket->issue = $ddticket->subject . "\r\n" . $_POST['issue'];
             $ddticket->update();
             //					=		Ticket::find_by_id($newticket->id);
             $newReply = new Ticketreply();
             $newReply->sender_id = $newticket->client_id;
             $newReply->ticket_id = $newticket->id;
             $theCustomer = Client::find_by_id($_SESSION["client_ident"]);
             $newReply->sender_name = $theCustomer->name;
             $newReply->sender_type = "Client";
             $newReply->message = $ddticket->issue;
             $newReply->datecreated = date("Y-m-d H:i:s");
             $newReply->create();
             if (!empty($_POST['ccemail'])) {
                 $emails = explode(",", $_POST['ccemail']);
                 $emaillenght = count($emails);
                 for ($j = 0; $j < $emaillenght; $j++) {
                     if (preg_match("/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})\$/i", trim($emails[$j]))) {
                         $ccemail = new Ccemail();
                         $ccemail->email = trim($emails[$j]);
                         $ccemail->ticket_id = $newticket->id;
                         $ccemail->create();
                     }
                 }
             }
             $this->sendMail($newReply->id);
             return 1;
             //returns 1 on success
         } else {
             return 2;
             // returns 2 on insert error
         }
     } else {
         $message = "Please check the following errors: ";
         $lenght = count($error);
         for ($i = 0; $i < $lenght; $i++) {
             $message = $message . $error[$i] . ", ";
         }
         echo "<div data-alert class='alert-box error'><a href='#' class='close'>&times;</a>{$message}</div>";
     }
 }
 public function createSchedule_Detail($id = "")
 {
     header('Content-Type: application/json');
     $data = json_decode(stripslashes($_POST['data']));
     foreach ($data as $d => $value) {
         $cntdata[] = $d;
     }
     if (!empty($data->taskdate) && !empty($data->cid) && !empty($data->empid)) {
         $issuefield = get_object_vars($data);
         // get the properties of an object into an array
         $thisclientproduct = Cproduct::find_by_id((int) preg_replace('#[^0-9]#i', '', $data->cid));
         $thisemployee = Employee::find_by_id((int) preg_replace('#[^0-9]#i', '', $data->empid));
         $thisSchedule = new Schedule();
         $thisSchedule->emp_id = $issuefield['empid'];
         $thisSchedule->emp_name = $thisemployee->emp_fname . " " . $thisemployee->emp_lname;
         $thisSchedule->client_id = $thisclientproduct->client_id;
         $thisClient = Client::find_by_id($thisclientproduct->client_id);
         //print_r($thisclientproduct);
         //print_r($thisClient);
         //print_r($thisemployee);
         $thisSchedule->prod_id = $issuefield['cid'];
         $thisSchedule->prod_name = $thisclientproduct->prod_name;
         /**
          * this is to check if ticket
          * is in existence
          */
         //print_r($theUser);
         global $session;
         //$partTicket                             =   ($id!="") ? Ticket::find_by_id($id) : ""; // get the ticket to get details needed for sending mail
         $theUser = Employee::find_by_id($_SESSION["emp_ident"]);
         // get cse detail to retrieve email
         $thisSchedule->s_date = $data->taskdate;
         /**
          * section to configure and setup sms and email message 
          */
         $issue = array();
         $objIssue = Issue::find_all();
         // creating the issue log object
         $message_issue = "";
         //$thisemployee->emp_phone
         foreach ($objIssue as $objI) {
             foreach ($issuefield as $key => $value) {
                 if ($key == $objI->issue_accronym) {
                     $message_issue .= $objI->issue_name . ", ";
                 }
             }
         }
         $cemail = array();
         $cemail = isset($_POST['cemail']) && !empty($_POST['cemail']) ? explode(",", $_POST['cemail']) : array('*****@*****.**');
         array_push($cemail, $thisemployee->emp_email, is_array($partTicket) ? $partTicket->contact_email : "", $theUser->emp_email, $thisClient->email);
         //print_r($cemail);
         $thisSchedule->status = "Open";
         $thisSchedule->issue = $message_issue;
         $thisSchedule->datecreated = date("Y-m-d H:i:s");
         $thisSchedule->maint_type = $_POST['mtype'];
         $subject = "Maintenance Alert";
         $smsmsg = "Maintenance alert for {$thisclientproduct->prod_name} at";
         $smsmsg .= $thisclientproduct->install_location . "," . $thisclientproduct->branch;
         $smsmsg .= "\r\n Issue:" . $message_issue . "\r\n";
         $smsmsg .= "\r\n Comment:" . $issuefield["tissue"] . "\r\n";
         $msg = "<h3>Maintenance Detail</h3> <hr />";
         $msg .= "<p><strong>Machine: </strong>{$thisclientproduct->prod_name} </p>";
         $msg .= "<p><strong>Client: </strong> {$thisClient->name}</p>";
         $msg .= "<p><strong>Location: </strong>{$thisclientproduct->install_location} {$thisclientproduct->branch} {$thisclientproduct->install_city} </p>";
         $msg .= "<p><strong>Issues: </strong>" . $message_issue . "</p>";
         $msg .= "<p><strong>Comment: </strong>" . $issuefield["tissue"] . "</p>";
         $msg .= "<br /><br /> <h4>Technician Details</h4> <hr />";
         $msg .= "<p><strong>Name: </strong>" . $thisemployee->emp_fname . " " . $thisemployee->emp_lname . "</p>";
         $msg .= "<p><strong>Email: </strong>" . $thisemployee->emp_email . "</p>";
         $msg .= "<p><strong>Telephone: </strong>" . $thisemployee->emp_phone . "</p>";
         $msg .= "<br /><br /><h4>Scheduled Date</h4> <hr />";
         $msg .= "<p><strong>{$thisSchedule->s_date}</strong></p><br /><br /><br /><br />";
         /**
          * the transaction log is created
          * to ensure that the product already 
          * has history and cannot be deleted
          */
         $Tlog2 = new Transaction();
         $Tlog2->com_id = $thisclientproduct->main_id;
         $Tlog2->trans_type = "TASK ASSIGNMENT";
         $Tlog2->trans_description = $issuefield["mtype"] . " task assigned to " . $thisemployee->emp_fname . " " . $thisemployee->emp_lname . "";
         $Tlog2->datecreated = date("Y-m-d H:i:s");
         $Tlog2->user_id = $_SESSION['emp_ident'];
         // print_r($msg);
         if ($thisSchedule->create()) {
             $Tlog2->create();
             /**
              * this section is needed
              * to post issues to database table
              * issuelog for statistical analysis on
              * issues
              */
             //$thisemployee->emp_phone
             foreach ($objIssue as $objI) {
                 foreach ($issuefield as $key => $value) {
                     if ($key == $objI->issue_accronym) {
                         $issueLog = new Issuelog();
                         $issueLog->ticket_id = 0;
                         $issueLog->issue_id = $objI->id;
                         $issueLog->issue_accronym = $objI->issue_accronym;
                         $issueLog->issue_name = $objI->issue_name;
                         $issueLog->prod_id = $issuefield['cid'];
                         $issueLog->datecreated = date("Y-m-d H:i:s");
                         //$ddticket->subject				.=	$_POST[$objI->issue_accronym]. ", ";
                         $issueLog->create();
                     }
                 }
             }
             sendSms($thisemployee->emp_phone, $smsmsg);
             //method in function.php in the library folder helps to send sms
             $this->sendMail($thisSchedule->emp_name, $subject, $msg, $cemail);
             // sends email to employee
             return 1;
         } else {
             return 2;
         }
     }
 }