<?php

class CpeshippingController extends mfBaseController {
  
  protected function init() {
    $this->needlogin=true;
    $me = new User();
    $me->loadMe();
    $this->me = $me;
    $this->layout()->set("me",$me);
    
    if(!$me->is(["Admin"])) {
      $this->redirect("Dashboard");
    }
  }
  
  protected function indexAction() {
    $this->layout()->setTemplate("Cpeshipping/Index");
    
    // pagination defaults
    $pagination = [];
    $pagination['start'] = 0;
    $pagination['count'] = 25;
    $pagination['maxItems'] = 0;
    
    if(is_numeric($this->request->s)) {
      $pagination['start'] = intval($this->request->s);
    }
    
    $provisions_search = ["shipping" => 1, "shipped" => 0];
    
    $pagination['maxItems'] = CpeprovisioningModel::count($provisions_search);
    $provisions = CpeprovisioningModel::search($provisions_search, $pagination);
    //$o = $provisions[0]->orderproduct->product->attributes;
    //var_dump($o);exit;
    $this->layout()->set("cpeprovisionings", $provisions);
    $this->layout()->set("pagination", $pagination);
    
  }
  
  protected function historyAction() {
    $this->layout()->setTemplate("Cpeshipping/History");
    
    // pagination defaults
    $pagination = [];
    $pagination['start'] = 0;
    $pagination['count'] = 25;
    $pagination['maxItems'] = 0;
    
    if(is_numeric($this->request->s)) {
      $pagination['start'] = intval($this->request->s);
    }
    
    $ship_search = ["SqlOptions" => ["orderBy" => "id DESC"]];
    
    $pagination['maxItems'] = CpeshippingFileModel::count($ship_search);
    $shippings = CpeshippingFileModel::search($ship_search, $pagination);
    
    $this->layout()->set("shippings", $shippings);
    $this->layout()->set("pagination", $pagination);
  }

  protected function saveAction() {
    $r = $this->request;
    if($r->type == "ship") {
      return $this->shipAction();
    }
    //var_dump($r);exit;
    if(is_array($r->external_finish) && count($r->external_finish)) {
      $ext_finish = $r->external_finish;
    }
    
    if(is_array($r->prov) && count($r->prov)) {
      $provs = $r->prov;
    }
    
    if(!count($provs)) {
      $this->layout()->setFlash("Nichts zu speichern", "warning");
      $this->redirect("Cpeshipping");
    }
    
    $shippings = [];
    foreach($provs as $prov_id) {
      if(!is_numeric($prov_id) || $prov_id <= 0) {
        continue;
      }
      
      $prov = new Cpeprovisioning($prov_id);
      
      if(!$prov->id) {
        continue;
      }
      if(!$prov->shipping) {
        // is not supposed to be selectable in UI
        continue;
      }
      
      if(array_key_exists($prov_id, $ext_finish) && $ext_finish[$prov_id] == 1) {
        $prov->external_finished = date('U');
        $prov->external_finished_by = $this->me->id;
      } else {
        $prov->external_finished = null;
        $prov->external_finished_by = $this->me->id;
      }
      $prov->save();
    }
    
    $this->layout()->setFlash("Erfolgreich gespeichert.", "success");
    $this->redirect("Cpeshipping");
  }
  
  protected function shipAction() {
    $r = $this->request;
    //var_dump($r);exit;
    
    $ships = [];
    if(is_array($r->ships) && count($r->ships)) {
      $ships = $r->ships;
    }
    $ext_finish = [];
    if(is_array($r->external_finish) && count($r->external_finish)) {
      $ext_finish = $r->external_finish;
    }
    
    if(!count($ships)) {
      $this->layout()->setFlash("Keine CPEs ausgewählt", "warning");
      $this->redirect("Cpeshipping");
    }
    
    $shippings = [];
    foreach($ships as $prov_id) {
      if(!is_numeric($prov_id) || $prov_id <= 0) {
        continue;
      }
      
      $prov = new Cpeprovisioning($prov_id);
      
      if(!$prov->id) {
        continue;
      }
      if(!$prov->shipping) {
        // is not supposed to be selectable in UI
        continue;
      }
      
      if(array_key_exists($prov_id, $ext_finish) && $ext_finish[$prov_id] == 1) {
        $prov->external_finished = date('U');
        $prov->external_finished_by = $this->me->id;
      } else {
        $prov->external_finished = null;
        $prov->external_finished_by = $this->me->id;
      }
      $prov->save();
      $shippings[] = $prov;
    }
    
    $tpl = new Layout();
    $tpl->setTemplate("Cpeshipping/csv/dhl.csv");
    $tpl->set("shippings", $shippings);
    $tpl->set("me", $this->me);
    $body = $tpl->render();
    
    /*
    header("Content-type: text/csv");
    header("Content-disposition: attachment; filename=\"dhl-shipment-".date("Y-m-d-H-i-s").".csv\"");
    echo $body;exit;
    */
    
    $folder = MFUPLOAD_FILE_SAVE_PATH."/csv";
    $filename = "dhl-shipment-".date("Y-m-d-H-i-s").".csv";
    $path = "$folder/$filename";
    
    if(!file_put_contents($path, $body)) {
      $this->layout()->setFlash("Fehler beim Erstellen der CSV-Datei", "error");
      $this->redirect("Cpeshipping");
    }
    
    $file = new File();
    $file->name = "DHL Shipment CSV ".date("d.m.Y H:i:s");
    $file->filename = $filename;
    $file->store_filename = $filename;
    $file->subfolder = "csv";
    $file->create_by = $this->me->id;
    $file->edit_by = $this->me->id;
    $file_id = $file->save();
    if(!$file_id) {
      $this->layout()->setFlash("Fehler beim Speichern der CSV-Datei (File)", "error");
      $this->redirect("Cpeshipping");
    }
    
    $shippingfile = CpeshippingFileModel::create(['file_id' => $file_id]);
    $shippingfile_id = $shippingfile->save();
    
    if(!$shippingfile_id) {
      $this->layout()->setFlash("Fehler beim Speichern der CSV-Datei (ShippingFile)", "error");
      $this->redirect("Cpeshipping");
    }
    
    foreach($shippings as $prov) {
      $prov->shipped = 1;
      $prov->shipping_date = date('U');
      $prov->shipped_by = $this->me->id;
      $prov->cpeshippingfile_id = $shippingfile_id;
      $prov->save();
    }
    
    $this->layout()->setFlash("Versand-CSV erfolgreich erstellt.", "success");
    $this->redirect("Cpeshipping", "History", null, "download=".$file_id);
    
  }
}