<?php

class Order extends mfBaseModel {
  private $owner;
  private $billingaddress;
  private $products;
  private $terminations;
  private $journals;
  private $files;
  private $creator;
  private $editor;
  
  private $shippingdate;
  
  public function getNewPos() {
    if(!$this->id) {
      return 0;
    }
    
    $products = $this->getProperty("products");
    if(!is_array($products) || !count($products)) {
      return 1;
    }
    
    $p = end($products);
    return ++$p->pos;
  }
  
  public function getTerminations() {
    if(!$this->id) {
      return false;
    }
    
    $products = $this->getProperty("products");
    
    if(!is_array($products) || !count($products)) {
      return false;
    }

    $terminations = [];
    
    foreach($products as $product) {
      if($product->termination_id) {
        $terminations[] = $product->termination;
      }
    }
    //var_dump($this->terminations);exit;
    return $terminations;
  }
  
  public function getShippingdate() {
    if(!$this->id) {
      return false;
    }
    
    $products = $this->getProperty("products");
    
    if(!is_array($products) || !count($products) ) {
      return false;
    }
    
    foreach($this->getProperty("products") as $product) {
      if($product->cpeprovisioning) {
        return $product->cpeprovisioning->shipping_date;
      }
    }
    
    return false;
  }
  
  public function deletePositions() {
    if(!is_array($this->getProperty("products")) || !count($this->getProperty("products"))) {
      return true;
    }
    foreach($this->getProperty("products") as $position) {
      $position->delete();
    }
  }
  
  public function deleteFiles() {
    if(!is_array($this->getProperty("files")) || !count($this->getProperty("files"))) {
      return true;
    }
    //var_dump($this->files);exit;
    
    foreach($this->getProperty("files") as $file) {
      $file->file->delete();
      $file->delete();
    }
  }
  
  public function sendIntBillinfoEmail() {
    // TODO template rendern auslagern nach Emailtempate klasse
    $tpl = new Layout();
    $tpl->setTemplate("Emailtemplates/order/int_billing_info");
    $tpl->set("order", $this);
    $body = $tpl->render();
    
    $values = $tpl->getReturnedValue();
    
    //var_dump($values); echo "<pre class='text-monospace'>".$body."</pre>"; exit;
    
    $subject = $values['subject'];
    $from = $values['from_email'];
    $from_name = $values['from_email_name'];
    $to = TT_INTERNAL_BILLING_EMAIL_TO;
    
    if(!$subject || !$from || !$from_name || !$to) {
      $this->log->warn("Internl Billing Email not sent. (subject: '$subject', from: '$from', from_email: '$from_name', to: '$to')");
      
      return false;
    } else {
      $email = new Emailnotification();
      $email->setSubject($subject);
      $email->setBody($body);
      $email->setFrom($from, $from_name);
      $email->setTo($to);
      $email->setHeader("X-xinon-oid", $this->id);
      $email->send();
      
      return true;
    }
  }
  
  public function sendExtBillinfoEmail() {
    // TODO template rendern auslagern nach Emailtempate klasse
    $tpl = new Layout();
    $tpl->setTemplate("Emailtemplates/order/billing_external_energie");
    $tpl->set("order", $this);
    $body = $tpl->render();
    
    $values = $tpl->getReturnedValue();
    /*
    $this->finish_date = null;
    $this->save();
    */
    //var_dump($values); echo "<pre class='text-monospace'>".$body."</pre>"; exit;
    
    $subject = $values['subject'];
    $from = $values['from_email'];
    $from_name = $values['from_email_name'];
    $to = TT_EXTERNAL_BILLING_EMAIL_TO;
    
    if(!$subject || !$from || !$from_name || !$to) {
      $this->log->warn("Internl Billing Email not sent. (subject: '$subject', from: '$from', from_email: '$from_name', to: '$to')");
      
      return false;
    } else {
      $email = new Emailnotification();
      $email->setSubject($subject);
      $email->setBody($body);
      $email->setFrom($from, $from_name);
      $email->setTo($to);
      $email->setHeader("X-Xinon-Oid", $this->id);
      $email->send();
      
      return true;
    }
  }
  
  public function getProperty($name) {
    if($this->$name == null) {
      
      if(!$this->id) {
        return null;
      }
      
      if($name == "owner") {
        $this->owner = new Address($this->owner_id);
        return $this->owner;
      }
      
      if($name == "contact") {
        $this->contact = new Address($this->contact_id);
        return $this->contact;
      }
      
      if($name == "billingaddress") {
        $this->billingaddress = new Address($this->billingaddress_id);
        return $this->billingaddress;
      }
      
      if($name == "products") {
        $this->products = OrderProductModel::search(["order_id" => $this->id]);
        return $this->products;
      }
      
      if($name == "terminations") {
        $this->terminations = $this->getTerminations();
        return $this->terminations;
      }
      
      if($name == "journals") {
        $this->journals = OrderJournalModel::search(["order_id" => $this->id]);
        return $this->journals;
      }
      
      if($name == "files") {
        $this->files = OrderFileModel::search(['order_id' => $this->id]);
        return $this->files;
      }
      
      if($name == "shippingdate") {
        $this->shippingdate = $this->getShippingdate();
        return $this->shippingdate;
      }
      
      if($name == "creator") {
        $this->creator = new User($this->create_by);
        return $this->creator;
      }
      
      if($name == "editor") {
        $this->editor = new User($this->edit_by);
        return $this->editor;
      }
      
      $classname = ucfirst($name);
      $idfield = $name."_id";
      $this->$name = new $classname($this->$idfield);
      
      if($this->$name->id) {
        return $this->$name;
      } else {
        return null;
      }
    }
    
    return $this->$name;
  }
  
}