<?php

class CpeprovisioningModel {
  public $termination_id;
  public $order_id;
  public $orderproduct_id;
  public $routerconfig_finished;
  public $order_journal_id;
  public $routertype;
  public $shipping;
  public $shipping_date;
  public $external_finished;
  public $external_finished_by;
  public $shipped;
  public $shipped_by;
  public $wifi_ssid;
  public $wifi_pass;
  public $mac;
  public $vlan_public;
  public $vlan_nat;
  public $vlan_ipv6;
  public $ship_weight;
  public $ship_length;
  public $ship_width;
  public $ship_height;
  public $note;
  
  public $create_by = null;
  public $edit_by = null;
  public $create = null;
  public $edit = null;

  public static function create(Array $data) {
    $model = new Cpeprovisioning();
    
    foreach($data as $field => $value) {
      if(property_exists(get_called_class(), $field)) {
        $model ->$field = $value;
      }
    }
    
    $me = new User();
    $me->loadMe();

    if($model->create_by === null) {
      $model->create_by = $me->id;
    }
    if($model->edit_by === null) {
      $model->edit_by = $me->id;
    }
    
    return $model;
  }
  
  public static function getOne($id) {
    if(!is_numeric($id) || !$id) {
      throw new Exception("Invalid number", 400);
    }
    $item = [];
    $db = FronkDB::singleton();
    
    $res = $db->select("Cpeprovisioning", "*", "id=$id LIMIT 1");
    if($db->num_rows($res)) {
      $data = $db->fetch_object($res);
      $item = new Cpeprovisioning($data);
    }
    return $item;
  }
  
  public static function getFirst($filter) {
    $db = FronkDB::singleton();
    
    $where = self::getSqlFilter($filter);
    $res = $db->select("Cpeprovisioning", "*", "$where ORDER BY order_id,orderproduct_id,termination_id");
    if($db->num_rows($res)) {
      $data = $db->fetch_object($res);
      $item = new Cpeprovisioning($data);
      if($item->id) {
        return $item;
      } else {
        return null;
      }
    }
    return null;
  }
  
  public static function getAll() {
    $items = [];
    
    $db = FronkDB::singleton();
    
    $res = $db->select("Cpeprovisioning", "*", "1=1 ORDER BY order_id,orderproduct_id,termination_id");
    if($db->num_rows($res)) {
      while($data = $db->fetch_object($res)) {
        $items[] = new Cpeprovisioning($data);
      }
    }
    return $items;
    
  }
  
  
  public static function count($filter) {
    $db = FronkDB::singleton();
    
    $where = self::getSqlFilter($filter);
    $sql = "SELECT COUNT(*) as cnt FROM Cpeprovisioning WHERE $where";
    
    $res = $db->query($sql);
    if($db->num_rows($res)) {
      $data = $db->fetch_object($res);
      return $data->cnt;
    }
    return 0;
  }
  
  public static function search($filter, $limit = false) {
    $items = [];
    $db = FronkDB::singleton();
    
    $where = self::getSqlFilter($filter);
    
    $sql = "SELECT * FROM Cpeprovisioning WHERE $where ORDER BY order_id,orderproduct_id,termination_id";
    
    if(is_array($limit) && count($limit)) {
      if(is_numeric($limit['start']) && is_numeric($limit['count'])) {
        $sql .= " LIMIT ".$limit['start'].", ".$limit['count'];
      } elseif(is_numeric($count)) {
        $sql .= " LIMIT ".$limit['count'];
      }
    }
    
    $res = $db->query($sql);
    if($db->num_rows($res)) {
      while($data = $db->fetch_object($res)) {
        $items[] = new Cpeprovisioning($data);
      }
    }
    return $items;
  }
  
  private function getSqlFilter($filter) {
    $where = "1=1 ";
    
    if(array_key_exists("termination_id", $filter)) {
      $termination_id = $filter['termination_id'];
      if(is_numeric($termination_id)) {
        $where .= " AND termination_id=$termination_id";
      }
    }
    
    if(array_key_exists("order_id", $filter)) {
      $order_id = $filter['order_id'];
      if(is_numeric($order_id)) {
        $where .= " AND order_id=$order_id";
      }
    }
    
    if(array_key_exists("orderproduct_id", $filter)) {
      $orderproduct_id = $filter['orderproduct_id'];
      if(is_numeric($orderproduct_id)) {
        $where .= " AND orderproduct_id=$orderproduct_id";
      }
    }
    
    if(array_key_exists("shipping", $filter)) {
      $shipping = $filter['shipping'];
      if($shipping) {
        $where .= " AND shipping=1";
      } else {
        $where .= " AND shipping=0";
      }
    }
    
    if(array_key_exists("shipped", $filter)) {
      $shipped = $filter['shipped'];
      if($shipped) {
        $where .= " AND shipped=1";
      } else {
        $where .= " AND shipped=0";
      }
    }
    
    //var_dump($filter, $where);exit;
    return $where;
  }
  
}
