<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Workorder_controller extends CI_Controller {

    public function __construct() {
        parent::__construct();
        // Your own constructor code
        $this->load->model('workorder_model');
        $this->load->model('supplier_model');
        
        if($this->session->userdata('role')==''){
            redirect('logout');
        }
    }

    
    public function workorderlist() { 

        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            checkroles(1,0,1);

            $data['nav'] = 'invoice';
            $data['subnav'] = 'daily_sheet';
            
            if(null != $this->input->post('range')){
                $range = explode(' - ', $this->input->post('range'));
                
                $start = date_format(date_create($range[0]), 'Y-m-d');
                $end = date_format(date_create($range[1]), 'Y-m-d');

            }

            if(!isset($start) || empty($start)){
                $start = date('Y-m-d');
                $end = date('Y-m-d');
            }

            $data['workorders'] = $this->workorder_model->getworkorders($start, $end); 
            //print_r($data['workorders']); exit();


            $data['suppliers'] = $this->supplier_model->get_suppliers();

            $this->load->view('includes/header', $data);
            $this->load->view('accounting/work_orders/workorder_list');
            $this->load->view('includes/footer');

        }
    }


    public function add_workorder(){

        $data = array(
            'workorder_date' => date('Y-m-d H:i:s'),
            'workorder_supplier_id' => $this->input->post('suppliers'),
            'created_by' => $this->session->userdata('username')
        );


        $this->db->insert('workorders', $data);

        redirect('workorderlist');

    }


    public function open_workorder_items($id){


        $data['workorder_items'] = $this->workorder_model->getworkorderitems($id);
        $data['idworkorder'] = $id;

        $this->load->model('product_model');
        $data['measure_units']= $this->product_model->get_measurement_unit();
        

        $this->load->view('includes/header', $data);
        $this->load->view('accounting/work_orders/workorder_items');
        $this->load->view('includes/footer');


    }

    public function add_workorder_item( ){

        $this->db->select('max(item_no) +1 as item_no');
        $this->db->where('workorder_id', $this->input->post('workorder_id'));
        $query = $this->db->get('workorder_items');
        $item = $query->row();
        $item_no = $item->item_no != null ? $item->item_no : 1;

        $final_price = (floatval($this->input->post('item_price')) * floatval($this->input->post('item_qty'))) ;
        $final_price =  $final_price - floatval($this->input->post('item_discount')!=null?$this->input->post('item_discount'):0);
        $data = array(
            'workorder_id' => $this->input->post('workorder_id'),
            'item_no' => $item_no,
            'item_desc' => $this->input->post('item_desc'),
            'item_price' => $this->input->post('item_price'),
            'item_discount' => $this->input->post('item_discount'),
            'item_qty' => $this->input->post('item_qty'),
            'item_status' => 'Active',
            'item_final_price' => $final_price,
            'item_measure_unit' => $this->input->post('item_measure_unit'),
            
        );


        $this->db->insert('workorder_items', $data);

        redirect('workorder_items/'.$this->input->post('workorder_id'));


    }

}