<?php

class salesman {

    /**
     * @var array Holds an array of data
     */
    protected $info = array();

    /**
     * @var obj Holds the database adapter object
     */
    private $db;
    private $year_id;
    private $year_id_actual;

    public function __construct() {
        $this->db = DBConnectorFactory::loadDriver();
        $this->year_id = Session::getCurrentYearID();
        $this->year_id_actual = Session::getCurrentYearIDActual();
    }

    public function setdata(array $data) {
        $this->info = $data;
        return $this;
    }
    
    public function assign_salesman_depot(){
        $data = [&$this->info['salesman_id'],&$this->info['depot_id'],&$this->info['depot_id']];
        $this->db->prepare_Stmt('INSERT INTO `tbl_salesman`( `salesman_id`, `depot_id`) 
            values(?,?) on duplicate key update depot_id=?',$data);
        $error = $this->db->error();
        return $error;
    }
    
    public function get_salesman_depot($salesman_id){
        $data = array();
        $query = $this->db->query('select s.depot_id,d.Depot_Name from tbl_salesman s 
            left join tbl_stock_depot_warehouse d on d.Depot_ID = s.depot_id 
            where s.salesman_id = "'.$salesman_id.'" and s.depot_id <> 0');
        $row = $this->db->assoc($query);
        if(!empty($row))
            $data = $row;
        return $data;
    }
}