<?php
class Cron{

    private $table_history = "T_Cron_History";
    private $table_shipment = "T_Cron_Shipment";

    public function insert_history($crud, $type){
        $result = 0;
        date_default_timezone_set("Asia/Jakarta");
        $now = date("Y-m-d H:i:s");

        $text = "INSERT INTO $this->table_history (ch_type, ch_create_date) VALUES('$type', '$now')";
        $result = $crud->execute_query($text);
        return $result;
    }

    public function delete_cron_history($crud, $first_id, $last_id){
        $result = 0;

        $text = "DELETE FROM T_Cron_History WHERE ch_id BETWEEN $first_id AND $last_id";
        $result = $crud->execute_query($text);
        return $result;
    }

    public function delete_cron_shipment($crud, $first_id, $last_id){
        $result = 0;

        $text = "DELETE FROM T_Cron_Shipment WHERE tcs_id BETWEEN $first_id AND $last_id";
        $result = $crud->execute_query($text);
        return $result;
    }

    public function insert_history_shipment($crud, $note, $total){
        $result = 0;
        date_default_timezone_set("Asia/Jakarta");
        $now = date("Y-m-d H:i:s");

        $text = "INSERT INTO $this->table_shipment (tcs_note, tcs_total, tcs_create_date) VALUES('$note', '$total', '$now')";
        $result = $crud->execute_query($text);
        return $result;
    }
    public function get_first_id($crud, $tables){
        $result = 0;

        if($tables == 'stock'){
            $text = "SELECT * FROM T_Cron_Shipment ORDER BY tcs_create_date ASC LIMIT 1";
        } else {
            $text = "SELECT * FROM T_Cron_History ORDER BY ch_id ASC LIMIT 1";
        }
        $query = $crud->conn()->query($text);
        if($query->num_rows >= 1){
            $result = array();
            while($row = $query->fetch_assoc()){
                $result[] = $row;
            }
        }
        return $result;
    }

    public function get_last_cron($crud){
        $result = 0;

        $text = "SELECT * FROM $this->table_shipment ORDER BY tcs_create_date DESC LIMIT 1";
        $query = $crud->conn()->query($text);
        if($query->num_rows >= 1){
            $result = array();
            while($row = $query->fetch_assoc()){
                $result[] = $row;
            }
        }
        return $result;
    }

}
?>