<?php
/*
ALTER TABLE `tblVoucher` DROP FOREIGN KEY `tblVoucher.strCourseType`;
ALTER TABLE `tblVoucher` ADD CONSTRAINT `tblVoucher.strCourseType` FOREIGN KEY ( `refCourseTypeID` ) REFERENCES `tblCourseType` ( `CourseTypeID` ) ON DELETE CASCADE ON UPDATE CASCADE ;

*
* BR: no save for existing items!
*/
include_once("_nemo.list.cls.php");

class Voucher extends NemoList
{
   private $ID = 0;

   public function __construct($DataKey)
   {
      //filters
      $this->Filters[frSearch]->tag = "input";
      $this->Filters[frSearch]->html->value = "";
      $this->Filters[frSearch]->html->type = "text";
      $this->Filters[frSearch]->html->class = "controlText";

      //parent
      parent::__construct($DataKey);

   }

   public function getList()
   {
      global $SystemSettings;

      //fix for memory limit error with huge list
      ini_set('memory_limit','256M');

      if($this->Filters[frSearch]->html->value != "")
      {
         $like = "LIKE(". $this->db->qs("%".$this->Filters[frSearch]->html->value."%") .")";
         $Where .= " AND (strVoucher $like OR strDescription $like )";
      }

      $this->ListSQL("
         SELECT tblVoucher.VoucherID, tblVoucher.strVoucher AS Voucher, strDescription as Description, tblVoucher.percDiscount * 100 AS `% Discount`, tblVoucher.strStatus AS Status, tblLearner.strLearner as `Redeemed By`, tblVoucher.strLastUser AS 'Last User', tblVoucher.dtLastEdit AS 'Last Edit'
         FROM tblVoucher LEFT JOIN tblLicence ON tblVoucher.strVoucher = tblLicence.LicenceID LEFT JOIN tblLearner ON tblLicence.refLearnerID = tblLearner.LearnerID
         WHERE 1=1 $Where
         ORDER BY strVoucher");
      return $this->renderTable("Voucher List");
   }

   public static function Save(&$VoucherID)
   {

      //print_rr($_POST);

      if($_POST[intQuantity] >> 0)
      {
         $UniquePrefix = substr($_POST[strVoucher],0,6);

         for($i = 0; $i < $_POST[intQuantity]; $i++)
         {
            //ini
            $db = new NemoDatabase("tblVoucher", 0, null, 0);

            //load
            $db->SetValues($_POST);

            $db->Fields[percDiscount] = $_POST[percDiscount] / 100;
            $db->Fields[intQuantity] = 1;
            $db->Fields[strLastUser] = $_SESSION['USER']->USERNAME;
            $result = $db->Save(0,0);
            //post
            if($VoucherID == 0) $VoucherID = $db->ID[VoucherID];

            $_POST[strVoucher] = $UniquePrefix . Obfuscate();
         }
      }else
      {
         $db = new NemoDatabase("tblVoucher", $VoucherID, null, 0);

         //load
         $db->SetValues($_POST);

         $db->Fields[strDescription] = $_POST[strDescription];
         $db->Fields[strStatus] = $_POST[strStatus];
         $db->Fields[percDiscount] = $_POST[percDiscount] / 100;
         //$db->Fields[intQuantity] = 1;
         $db->Fields[strLastUser] = $_SESSION['USER']->USERNAME;
         $result = $db->Save(0,0);

      }



      //print_rr($db->Fields);
      if($result->Error == 1)
         return $result->Message;
      else
         return "Details Saved. ";
   }

   public static function GenerateVoucher($CourseTypeID, $UniquePrefix, $strDescription)
   {
      $vdb = new NemoDatabase("tblVoucher", 0, null, 0);
      $UID = $UniquePrefix . "VOU" . Obfuscate(); 
      $vdb->Fields[strVoucher] = $UID;
      $vdb->Fields[refCourseTypeID] = $CourseTypeID;
      $vdb->Fields[strStatus] = "Dormant";
      $vdb->Fields[strDescription] = $strDescription;
      $vdb->Fields[percDiscount] = 1;
      $vdb->Fields[intQuantity] = 1;
      $vdb->Fields[strLastUser] = "GenerateVoucher()";
      $result = $vdb->Save(0,0);

      return $UID;
   }

   public static function Delete($chkSelect)
   {
      global $xdb;
      //print_rr($chkSelect);
      if(count($chkSelect) > 0){
         foreach($chkSelect as $key => $value)
         {
            $xdb->doQuery("DELETE FROM tblVoucher WHERE VoucherID = ". $xdb->qs($key));
         }
         return "Records Deleted.";
      }
   }
}
?>
