<?php
/**
 * CodeIgniter library
 *
 * @category CodeIgniter
 * @license /_licenses/untermStrich/agb_normal.pdf
 * @author Christian Koller
 * @copyright 2009 untermStrich
 * @package us_libraries
 * @version $Id: transmittal_form_values.php 5240 2013-01-07 18:23:09Z christian $
*/

if (!defined('BASEPATH')) { exit('No direct script access allowed'); }

require_once APPPATH.'us_libraries/Value_object.php';

/**
 * Used to save a calculate according to expenditure item
 *
 * @property    My_Controller           $CI                     The main CI object
 */
class Transmittal_form_values extends Value_object
{
    protected $project_id;
    protected $transmittal_form_name;
    protected $amount;
    protected $date;

    protected $purpose;
    protected $comment;
    protected $remarks;

    protected $recipients;
    protected $drawings;

    /**
     * Init the values from $_POST
     *
     * @return  object  An Calcexp_values object with all values out of the post array
     */
    public static function init_from_post()
    {
        $obj = new Transmittal_form_values();
        $obj->CI = CI_Controller::get_instance();
        $obj->add_or_id = $obj->CI->input->post('drawings_delivery_note_edit');
        $obj->project_id = intval($obj->CI->input->post('drawings_delivery_note_project'));
        $obj->transmittal_form_name = $obj->CI->input->post('drawings_delivery_note_transmittal_form_name');
        $obj->amount = intval($obj->CI->input->post('drawings_delivery_note_amount'));
        $obj->date = new UsDate($obj->CI->input->post('drawings_delivery_note_date'), null, true, null);
        
        $obj->purpose = $obj->CI->input->post('drawings_delivery_note_purpose');
        $obj->comment = $obj->CI->input->post('drawings_delivery_note_comment');
        $obj->remarks = $obj->CI->input->post('drawings_delivery_note_remarks');
        
        $obj->recipients = (array)$obj->CI->input->post('drawings_delivery_note_recipients');
        $obj->drawings = (array)$obj->CI->input->post('drawings_delivery_note_drawings');
        
        return $obj;
    }
    
    /**
     * Validate and ensure all values are appropriate
     *
     * @return  mixed   false on success, else the error message
     *
     * @todo    implement!!
     */
    public function validate()
    {
        if (!is_numeric($this->add_or_id) && $this->add_or_id!=='add') {
            return 'Item ID has to be an ID';
        }
        
        if (count($this->recipients)<1) {
            return 'Need one recipient';
        }
        
        if (count($this->drawings)<1) {
            return 'Need one drawing';
        }
        
        if ($this->date===null) {
            $this->date = new UsDate('now');
        }
        
        return false;
    }
    
    /**
     * Get drawing to save
     *
     * @return  array
     */
    public function get_delivery_note_to_save()
    {
        $data = array(
            'project_id'            => (integer)$this->project_id,
            'transmittal_form_name' => (!$this->is_really_empty($this->transmittal_form_name)) ? $this->transmittal_form_name : '',
            'date'                  => $this->date->mysql(),
            'purpose'               => (!$this->is_really_empty($this->purpose)) ? $this->purpose : '',
            'comment'               => (!$this->is_really_empty($this->comment)) ? $this->comment : '',
            'remarks'               => (!$this->is_really_empty($this->remarks)) ? $this->remarks : '',
            'address_id'            => (integer)$this->recipients[0],
        );
        
        return $data;
    }
    
    /**
     * Get amount
     * 
     * @return  integer
     */
    public function get_amount()
    {
        return (integer)$this->amount;
    }
    
    /**
     * Get date
     * 
     * @return  object
     */
    public function get_date()
    {
        return $this->date;
    }
    
    /**
     * Get purpose
     * 
     * @return  string
     */
    public function get_purpose()
    {
        return $this->purpose;
    }
    
    /**
     * Get recipients
     * 
     * @return array
     */
    public function get_recipients()
    {
        return $this->recipients;
    }

    /**
     * Get drawings
     * 
     * @return array
     */
    public function get_drawings()
    {
        return $this->drawings;
    }
    
} // END class Drawings_values
