public function receiveTicketMachine($message, $phone) { $dbOps = new DBOperations(); $con = new mysqli($dbOps->servername, $dbOps->username, $dbOps->password, $dbOps->dbname); //create the db connection $query = ""; if ($con->connect_error) { die("connection failed: " . $con->connect_error); } else { //Get the suitable serial_number for phone_number from ticket_machine table $queryGetEditer = "SELECT * FROM ticket_machine WHERE phone_number = {$phone}"; $result = $con->query($queryGetEditer); $editer = ""; while ($row = mysqli_fetch_array($result)) { $editer = $row['serial_number']; } //set the time zone to Colombo time date_default_timezone_set('Asia/Colombo'); $dt = new DateTime(); //create DateTime object $prepare = explode(" ", $message); // split the ticket machine into array //Example Code: supCode approvedKGs units suppliedKGs if (count($prepare) == 4) { if (is_numeric($prepare[1]) && is_numeric($prepare[2]) && is_numeric($prepare[3])) { //check the approvedKGs units suppliedKGs are integers if (!supplierCodeExists($prepare[0])) { //Example Code: supCode approvedKGs units suppliedKGs if (DBOperations::isSupCodeValid($prepare[0])) { //Check the supplier code valid or invalid $query = "INSERT INTO today_supply(supplier_code, date, approved_kgs, units, supplied_kgs, editer) VALUES ('" . $prepare[0] . "', '" . $dt->format('y-m-d') . "', '" . $prepare[1] . "', '" . $prepare[2] . "', '" . $prepare[3] . "', '" . $editer . "')"; $con->query($query); } else { //If the supplier code invalid then send error message Communication::sendMessage("Supplier code is invalid !", $phone); } } else { //If the supplier code exists then send error message Communication::sendMessage("Supplier code is already exists the system !", $phone); } } else { //reply again to the ticket machine Communication::sendMessage("Sent message is invalid.", $phone); } //Example Code: supCode approvedKGs suppliedKGs //Example Code: supCode approvedKGs units } else { if (count($prepare) == 3) { if (is_numeric($prepare[1]) && is_numeric($prepare[2])) { //check the integers as above if (!supplierCodeExists($prepare[0])) { if ($prepare[1] <= $prepare[2]) { //Example Code: supCode approvedKGs suppliedKGs if (DBOperations::isSupCodeValid($prepare[0])) { //Check the supplier code valid or invalid $query = "INSERT INTO today_supply(supplier_code, date, approved_kgs, supplied_kgs, editer) VALUES ('" . $prepare[0] . "', '" . $dt->format('y-m-d') . "', '" . $prepare[1] . "', '" . $prepare[2] . "', '" . $editer . "')"; $con->query($query); } else { //If the supplier code invalid then send error message Communication::sendMessage("Supplier code is invalid !", $phone); } } else { //Example Code: supCode approvedKGs units if (DBOperations::isSupCodeValid($prepare[0])) { //Check the supplier code valid or invalid $query = "INSERT INTO today_supply(supplier_code, date, approved_kgs, units, editer) VALUES ('" . $prepare[0] . "', '" . $dt->format('y-m-d') . "', '" . $prepare[1] . "', '" . $prepare[2] . "', '" . $editer . "')"; $con->query($query); } else { //If the supplier code invalid then send error message Communication::sendMessage("Supplier code is invalid !", $phone); } } } else { //If the supplier code exists then send error message Communication::sendMessage("Supplier code is already exists the system !", $phone); } } else { //reply again to the ticket machine Communication::sendMessage("Sent message is invalid.", $phone); } //Example Code: supCode approvedKGs } else { if (count($prepare) == 2) { if (is_numeric($prepare[1])) { if (!DBOperations::supplierCodeExists($prepare[0])) { //Example Code: supCode approvedKGs if (DBOperations::isSupCodeValid($prepare[0])) { //Check the supplier code valid or invalid $query = "INSERT INTO today_supply(supplier_code, date, approved_kgs, editer) VALUES ('" . $prepare[0] . "', '" . $dt->format('y-m-d') . "', '" . $prepare[1] . "', '" . $editer . "')"; $con->query($query); } else { //If the supplier code invalid then send error message Communication::sendMessage("Supplier code is invalid !", $phone); } } else { //If the supplier code exists then send error message Communication::sendMessage("Supplier code is already exists the system !", $phone); } } else { //reply again to the ticket machine Communication::sendMessage("Sent message is invalid.", $phone); } //Example Code: without above codes } else { //reply again to the ticket machine Communication::sendMessage("Sent message is invalid.", $phone); } } } } }