<?php defined('SYSPATH') or die('No direct script access.');

class Model_Sale extends ORM_Searchable 
{
        /**
     * Define the fields to index
     */
    public function get_indexable_fields()
    {
        $fields = array();

        // Store the product id but don't index it
        $fields[] = new Search_Field('id', Searchable::UNINDEXED);

        // Index the product name
        $fields[] = new Search_Field('header', Searchable::TEXT);

        // Index but don't store the product description
        //$fields[] = new Search_Field('product_type', Searchable::UNSTORED, Searchable::DECODE_HTML);
        $fields[] = new Search_Field('product_type', Searchable::TEXT);
        $fields[] = new Search_Field('searchid',Searchable::TEXT);
        return $fields;
    }
    
    protected $_belongs_to = array( 'university'    => array('model' => 'university', 'foreign_key' => 'university_id'),
                                    'department'    => array('model' => 'department', 'foreign_key' => 'department_id'),
                                    'user'          => array('model' => 'user', 'foreign_key' => 'user_id'),
                                    'category'      => array('model' => 'category', 'foreign_key' => 'category_id'),
                                       );
    
    public function getMySale(){
        
        $me    = Auth::instance()->get_user()->id;
        $sales = ORM::factory('sale')   //->join('categories')->on('sales.category_id', '=', 'categories.id')
                                        ->where('user_id', '=', $me)
                                        ->find_all();
        
        return $sales;
    }
      
    public function setSold($saved_sales){
        
        $me    = Auth::instance()->get_user()->id;
        $sales = $this->setDefault();
        
        foreach($sales as $sale){
            foreach($saved_sales as $saved){
                if($sale->id == $saved){
                    $sale->sold = true;
                    $sale->save();
                    break;
                }
            }
        }
        
        $sales = ORM::factory('sale')   ->where('user_id', '=', $me)
                                        ->find_all();
        return $sales;
    }
      
    public function setDefault(){
        
        $me    = Auth::instance()->get_user()->id;
        $sales = ORM::factory('sale')   ->where('user_id', '=', $me)
                                        ->find_all();
        
        foreach($sales as $sale){
            $sale->sold = false;
            $sale->save();
        }
        
        $sales = ORM::factory('sale')   ->where('user_id', '=', $me)
                                        ->find_all();
        return $sales;
    }
    
    public function deleteProduct($sale_id){
        
        ORM::factory('sale')->where('id', '=', $sale_id)->delete_all();
    }
    
    public function getSale($sale_id){
        
        $sales = ORM::factory("sale")->where('id','=',$sale_id)->find();
        return $sales;
    }
    
    public function createSale($new_sale){
        
        $me	  = Auth::instance()->get_user();
        $sale = ORM::factory('sale');
        $sale->product_type = $new_sale["product_type"];
        $sale->header = $new_sale["header"];
        $sale->department_id = $me->department_id;
        $sale->university_id = $me->university_id;
        $sale->category_id = $new_sale["category"]; 
        $sale->user_id = $me->id; 
        $sale->time = gen::now();
        $sale->save();
        $sale = ORM::factory("sale")->where('user_id','=',$me->id)
                                    ->where('header','=',$new_sale["header"])
                                    ->where('product_type','=',$new_sale["product_type"])
                                    ->find();
        $sale->searchid = $sale->id;
        $sale->save();
    }
    
    public function editSale($sale_id, $new_sale){
        
        $me	  = Auth::instance()->get_user();
        $sale = ORM::factory("sale")->where('id','=',$sale_id)->find();
        $sale->product_type = $new_sale["product_type"];
        $sale->header = $new_sale["header"];
        $sale->department_id = $me->department_id;
        $sale->university_id = $me->university_id;
        $sale->category_id = $new_sale["category"]; 
        $sale->user_id = $me->id; 
        $sale->time = gen::now();
        $sale->save();
    }
}