<?php

/**
 * inoERP
 *
 * @copyright   2014 Nishit R. Das
 * @license     https://www.mozilla.org/MPL/2.0/
 * @link        http://inoideas.org
 * @source code https://github.com/inoerp/inoERP
 */

/**
 * sys_program
 *
 */
class sys_program extends dbObject {

 public static $table_name = "sys_program";
 public static $primary_column = "sys_program_id";
 public static $key_column = 'program_name';
 public static $module = "sys"; //same as gl_journal_source
 public static $system_info = [
  'name' => 'Program',
  'number' => '9109',
  'description' => 'View & Update Program',
  'version' => '0.1.1',
  'db_version' => '1001',
  'mod_version' => '1.1.1',
  'primary_entity_cb' => '',
  'module_name' => 'sys',
  'weight' => 9
 ];
 public static $program_source_a = [
  'MANUAL' => 'Manual',
  'SCHEDULER' => 'Scheduler',
 ];
 public $field_a = [
  'sys_program_id',
  'program_name',
  'description',
  'message',
  'module_name',
  'class',
  'status',
  'program_source',
  'parameters',
  'request_type',
  'report_query',
  'output_path',
  'op_email_address',
  'op_email_format',
  "created_by",
  "creation_date",
  "last_update_by",
  "last_update_date"
 ];
 public $initial_search = [
  'sys_program_id',
  'program_name',
  'program_source',
  'message',
  'module_name',
  'class',
  'status',
  "created_by",
  "creation_date",
 ];
 public $column = [
  'sys_program_id',
  'program_name',
  'program_source',
  'module_name',
  'class',
  'status',
 ];
 public $requiredField = [
  "program_name"
 ];
 public $base_encoded_array = [
  "report_query"
 ];
 public $search = [
  '_show_view_path' => 1,
  '_show_update_path' => 1
 ];
 public $pageTitle = " Programs | inoERP "; //page Title
 public $option_lists = [
//		 'program_types' => 'ORG_TYPE'
 ];
 public $sys_program_id;
 public $program_name;
 public $description;
 public $message;
 public $module_name;
 public $class;
 public $request_type;
 public $report_query;
 public $status;
 public $op_email_address;
 public $op_email_format;
 public $program_source;
 public $parameters;
 public $output_path;
 public $created_by;
 public $creation_date;
 public $last_update_by;
 public $last_update_date;

 Public static function program_types() {
  $option_header = option_header::find_by_name('ORG_TYPE');
  $lines = option_line::find_by_option_id($option_header->option_header_id);
  return $lines;
 }

 public function findBy_status($status = 'Initiated') {
  $sql = " SELECT * FROM ";
  $sql .= self::$table_name;
  $sql .= " WHERE status = :status ";
  $orderby_sql = " ORDER BY sys_program_id ASC  ";
  $sql = ino_perPageOrderBySql_i($sql, $orderby_sql, 1);
  $value_a = ['status' => $status];
  $result = $this->findBySql($sql, $value_a);
  return !(empty($result)) ? array_pop($result) : false;
 }

 public function findBy_schdule_details() {
  $sql = " SELECT * FROM ";
  $sql .= self::$table_name;
  $sql .= " WHERE program_source = 'SCHEDULER' ";
  $sql .= " AND class = '{$this->class}' ";
  $sql .= " AND program_name = '{$this->program_name}' ";
  $sql .= " AND creation_date >= '{$this->creation_date}' ";
  $result = $this->findBySql($sql);
  return !(empty($result)) ? $result : false;
 }

 public function _before_showing() {
  
 }

 public function _before_save($postArray) {
  if (!empty($postArray) && !empty($postArray['frequency_uom'][0]) && !empty($postArray['frequency_value'][0])) {
   $ps = new sys_program_schedule();
   $ps->program_class_name = $postArray['class_name'][0];
   $ps->program_name = $postArray['program_name'][0];
   $ps->frequency_uom = $postArray['frequency_uom'][0];
   $ps->frequency_value = $postArray['frequency_value'][0];
   $ps->start_date_time = $postArray['start_date_time'][0];
   $ps->status = 'ACTIVE';
   $ps->request_type = 'PROGRAM';
   
   $ps->parameter = serialize($postArray);
   try {
    $ps->save();
    echo '<div class="alert alert-success alert-dismissible" role="alert">
   <button type="button" class="close" data-dismiss="alert" aria-label="' . gettext('Close') . '"><span aria-hidden="true">&times;</span></button>';
    echo "<div class='message'>The program is Successfully schduled; sys_program_schedule_id Id is " . $ps->sys_program_schedule_id . '</div>'
    . '</div>';
   } catch (Exception $e) {
    echo "<br>Unable to schedule the program. Error @ class_sys_program @@ line " . __LINE__ . '<br>' . $e->getMessage();
   }
  }
 }

}

$program = new sys_program();
$p = &$program;


//end of path class
?>
