<?php
if (!defined('BASEPATH')

    )exit('No direct script access allowed');

class Companyinfo extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('settings/companyinfo_model');
        $this->load->model('settings/warehouse_model');
    }

    function index() {
        $data['company'] = $this->companyinfo_model->getcompanyinfo('0');
        $data['locale'] = $this->companyinfo_model->getlocaleinfo();
        $data['warehouselist'] = $this->warehouse_model->getwarehouseautocomplete();
        $data["error"]  =   "";
        $displaylogo    =   "./assets/img/company/".$data['company']->DISPLAYLOGO;
//        $printlogo    =   site_url()."assets/img/company/".$data['company']->PRINTLOGO;
//        if(file_exists($displaylogo)){
//            $data['company']->DISPLAYLOGO   =   site_url()."assets/img/company/".$data['company']->DISPLAYLOGO;
//        }else{
//            $data['company']->DISPLAYLOGO   =   "./assets/img/icons/noimage.jpeg";
//        }
//        if(file_exists($printlogo)){
//            $data['company']->PRINTLOGO   =   $printlogo;
//        }else{
//            $data['company']->PRINTLOGO   =   site_url()."assets/img/icons/noimage.jpeg";
//        }
        $this->load->library('Localelist');
        $data['countrylist']    =   $this->localelist->listLocaleArray();
        
        
        $data['template'] = 'settings/companyinfo_view';
        $this->load->view('template',$data);
        
    }

    public function update() {
        //$this->session->keep_flashdata('errmsg');
        //$this->load->library('form_validation');
        /* $this->form_validation->set_rules('companyname', 'Company Name', 'trim|required');
        $this->form_validation->set_rules('postal', 'Postal', 'trim|numeric');
        $this->form_validation->set_rules('phone1', 'Phone1', 'trim|numeric');
        $this->form_validation->set_rules('phone2', 'Phone2', 'trim|numeric');
        $this->form_validation->set_rules('email', 'Email', 'trim|valid_email');
        $this->form_validation->set_rules('fax', 'Fax', 'trim|numeric');*/
        $this->companyinfo_model->updatecompanyinfo(
        array(
            'COMPANYNAME' => $this->input->post('companyname'),
            'ADDRESS1' => $this->input->post('address1'),
            'ADDRESS2' => $this->input->post('address2'),
            'POSTAL' => $this->input->post('postal'),
            'CITY' => $this->input->post('city'),
            'REGION' => $this->input->post('region'),
            'COUNTRY' => $this->input->post('country'),
            'PHONE1' => $this->input->post('phone1'),
            'PHONE2' => $this->input->post('phone2'),
            'FAX' => $this->input->post('fax'),
            'EMAIL' => $this->input->post('email'),
            'WEBSITE' => $this->input->post('web'),
            'TAXID' => $this->input->post('taxid')
        ), '0');
        $this->session->set_flashdata('success_msg', 'Company information updated successfully.');
        redirect(base_url().'settings/companyinfo/');
    }

    public function updatelogo() {
//        $config['upload_path']          = './assets/img/company/';
//        $config['allowed_types']        = 'gif|jpg|png';
//        $config['max_size']             = 100;
//        $config['max_width']            = 2500;
//        $config['max_height']           = 600;
//        $this->load->library('upload', $config);
//        if ( ! $this->upload->do_upload('displaylogo')){
//            $this->session->set_flashdata('success_msg', $this->upload->display_errors());
//            redirect(base_url().'settings/companyinfo/');
//        }else{
//            $data = array('upload_data' => $this->upload->data());
            $this->companyinfo_model->updatecompanyinfo(array('DISPLAYLOGO' => $_POST["displaylogohidden"]), '0');
            $this->session->set_flashdata('success_msg', 'Company logo updated successfully.');
            redirect(base_url().'settings/companyinfo/');
//        }
    }

    public function updatelocalesettings() {
        $FORMATINTEGER=$this->input->post('FORMATINTEGER');
        if($FORMATINTEGER=='')
            $FORMATINTEGER='#0';
        $FORMATDOUBLE=$this->input->post('FORMATDOUBLE');
        if($FORMATDOUBLE=='')
            $FORMATDOUBLE='#0.0';
        $FORMATCURRENCY=$this->input->post('FORMATCURRENCY');
        if($FORMATCURRENCY=='')
            $FORMATCURRENCY='#0.00';
        $FORMATPERCENT=$this->input->post('FORMATPERCENT');
        if($FORMATPERCENT=='')
            $FORMATPERCENT='#0%';
        $FORMATDATE=$this->input->post('FORMATDATE');
        if($FORMATDATE=='')
            $FORMATDATE='d-m-y';
        $FORMATTIME=$this->input->post('FORMATTIME');
        if($FORMATTIME=='')
            $FORMATTIME='h:i';
        $FORMATDATETIME=$this->input->post('FORMATDATETIME');
        if($FORMATDATETIME=='')
            $FORMATDATETIME='d-m-y hh:i';
        
        $this->companyinfo_model->updatelocaleinfo(
        array(
            'COUNTRY' => $this->input->post('COUNTRY'),
            'FORMATINTEGER' => $FORMATINTEGER,
            'FORMATDOUBLE' => $FORMATDOUBLE,
            'FORMATCURRENCY' => $FORMATCURRENCY,
            'FORMATPERCENT' => $FORMATPERCENT,
            'FORMATDATE' => $FORMATDATE,
            'FORMATTIME' => $FORMATTIME,
            'FORMATDATETIME' => $FORMATDATETIME,
            'TIMEZONE' => $this->input->post('TIMEZONE')
        ));
        $this->session->set_flashdata('success_msg', 'Locale information updated successfully.');
        redirect(base_url().'settings/companyinfo/');
    }
    
    public function updatetermsandconditions() {
        $this->companyinfo_model->updatelocaleinfo(
        array(
            'POTERMS' => $this->input->post('POTERMS'),
            'LOCATION' => $this->input->post('LOCATION'),
            'NEXTBARCODE' => $this->input->post('NEXTBARCODE')
        ));
        $this->session->set_flashdata('success_msg', 'Terms and Conditions updated successfully.');
        redirect(base_url().'settings/companyinfo/');
    }
    
    
}