<?php
/**
 * Author:echo
 * Email:sqlgun@qq.com
 * Date: 2018-7-17
 * 有偿二次开发，邮件联系
 */
class Docput extends MY_Controller{
    
    public function __construct(){
        parent::__construct();
        $this->load->model('in/docput_model');
    }
    public function index(){
        $_data ['button']=$this->navButton('In');
        $_data ['url'] = $this->url;
        $_data['status']=$this->get_goods_status();
        $this->load->view('in/docput',$_data); 
    }
    
    public function add_view(){
        $_data ['url'] = $this->url;
        $this->load->view('in/docputa',$_data);
    }    
    
    public function edit_view($_id){
        $_data ['url'] = $this->url;
        $_data ['id']=$_id;
        $_data ['docputhead'] =$this->docput_model->get_one_docput($_id);
        $_data['area']=$this->get_area();
        $_data['position']=$this->get_position();
        $this->load->view('in/docpute',$_data);
    }
    
    public function look_view($_id){
        $_data ['url'] = $this->url;
        $_data ['id']=$_id;
        $_data['area']=$this->get_area();
        $_data['position']=$this->get_position();
        $_data ['docputhead'] =$this->docput_model->get_one_docput($_id);
        $this->load->view('in/docputl',$_data);
    }    
    
    public function get_docputlist($_id){
        $_rows=$this->docput_model->get_docputlist($_id);
        $_status=$this->get_goods_status();
        foreach($_rows as $_key=>$_value){
            foreach($_status as $_k=>$_v){
                if($_v->id==$_value->goods_status){
                    $_rows[$_key]->goods_status=$_v->text;break;
                }
            }
        }        
        echo json_encode($_rows);
//         print_r($_rows);
    }
    
    public function get_all_goods(){
        $_row=$this->docput_model->get_all_goods();
        echo json_encode($_row);        
    }    
    public function get_all_status(){
        $_row=$this->docput_model->get_all_status();
        echo json_encode($_row);
    }    
 
    public function get_all_area(){
        $_row=$this->docput_model->get_all_area();
        echo json_encode($_row);
    }    
    
    public function get_area(){
        $_row=$this->docput_model->get_all_area();
        return $_row;
    }    
    
    public function get_all_position(){
        $_row=$this->docput_model->get_all_position();
        echo json_encode($_row);
    }
    
    public function get_position(){
        $_row=$this->docput_model->get_all_position();
        return $_row;
    }    
    
    public function get_goods_status(){
        $_row = $this->docput_model->get_goods_status();
        foreach ($_row as $_key=>$_value){
            $_rows[$_key]->id=$_value->status_id;
            $_rows[$_key]->text=$_value->status_name;
        }
        $_row=array_values($_rows);
         return $_row;
    }    
    
    
     public function add_docput(){              
//          print_r($this->input->post() );exit;
             $_docputlist_date=$this->input->post('docputlist_date');
             $_docputlist_date_nums=count($_docputlist_date);
             if(empty($_docputlist_date) || $_docputlist_date_nums==0){
                 echo'{"success":false,"message":"非法访问"}';exit;
             }
             
            $_docput_number=$this->input->post('docput_number');
            $_docput_send_number=$this->input->post('docput_send_number');            
            if(empty($_docput_number)){
                $_data ["docput_number"]=time();
            }else{
                $_data ["docput_number"]=$_docput_number;
            }
            if(!empty($_docput_send_number)){
                $_data ["docput_send_number"]=$_docput_send_number;
            }
            $_data ["docput_datetime"]=time();
            $_id=$this->docput_model->add_docput($_data);
//             $_id=1;
            if($_id){
                $_status=$this->get_goods_status();
                $_arr=array();
                foreach($_docputlist_date as $_key=>$_value){
                    $_arr[$_key]['docput_id']=$_id;
                    $_arr[$_key]['goods_id']=$_value['goods_id'];
                    $_arr[$_key]['goods_unit']=$_value['goods_unit'];
//                     $_arr['goods_status']=$_value['goods_status'];
                    foreach($_status as $_k=>$_v){
                        if($_v->text==$_value['goods_status']){
                            $_arr[$_key]['goods_status']=$_v->id;break;
                        }
                    }
                    $_arr[$_key]['goods_nums']=$_value['goods_nums'];
                    $_arr[$_key]['goods_postil']=$_value['goods_postil'];
                }
                 if($this->docput_model->add_docputlist($_arr)){
                     echo'{"success":true,"message":"添加成功共'.$_docputlist_date_nums.'条明细！"}';
                 }else{
                     echo'{"success":false,"message":"添加失败"}';
                 }               
            }else{
                echo'{"success":false,"message":"单头生成异常！"}';exit;
            }
            

     }     
    
     public function update_docput($_id){
         $_docput_id=$_id;
         $_docputlist_date=$this->input->post('docputlist_date');
         $_docputlist_date_nums=count($_docputlist_date);
         if(empty($_docputlist_date) || $_docputlist_date_nums==0){
             echo'{"success":false,"message":"非法访问"}';exit;
         }
          
         $_docput_number=$this->input->post('docput_number');
         if(!empty($_docput_number)){

             $_data ["docput_number"]=$_docput_number;
         }
       
         $_data ["docput_update_datetime"]=time();
         $_state=$this->docput_model->update_docput($_docput_id,$_data);
         if($_state){
             $_status=$this->get_goods_status();
             $_arr=array();
             foreach($_docputlist_date as $_key=>$_value){
                 $_arr[$_key]['docput_id']=$_id;
                 $_arr[$_key]['goods_id']=$_value['goods_id'];
                 $_arr[$_key]['goods_unit']=$_value['goods_unit'];
                 //                     $_arr['goods_status']=$_value['goods_status'];
                 foreach($_status as $_k=>$_v){
                     if($_v->text==$_value['goods_status']){
                         $_arr[$_key]['goods_status']=$_v->id;break;
                     }
                 }
                 $_arr[$_key]['goods_nums']=$_value['goods_nums'];
                 $_arr[$_key]['goods_postil']=$_value['goods_postil'];
                 $_arr[$_key]['area_id']=$_value['area_id'];
                 $_arr[$_key]['position_id']=$_value['position_id'];
             }

             if($this->docput_model->del_docputlist($_docput_id)){
                 if($this->docput_model->add_docputlist($_arr)){
                     echo'{"success":true,"message":"修改成功共更新'.$_docputlist_date_nums.'条明细！"}';
                 }else{
                     echo'{"success":false,"message":"明细修改失败"}';exit;
                 }
             }else{
                 echo'{"success":false,"message":"明细清理失败"}';exit;
             }
         }else{
             echo'{"success":false,"message":"单头修改异常！"}';exit;
         }         
         
     }
   
    
    public function get_docput(){
//         print_r($this->input->post());
       $_page=$this->input->post('page');
       $_num=$this->input->post('rows');
       $_offset = ($_page-1)*$_num;
       
       $_docput_number=$this->input->post('docput_number');
       $_docin_number=$this->input->post('docin_number');
       $_docin_send_number=$this->input->post('docin_send_number');
       
       $_docput_date_from=$this->input->post('docput_date_from');
       if($_docput_date_from) $_docput_date_from=strtotime($_docput_date_from);
       $_docput_date_to=$this->input->post('docput_date_to');
       if($_docput_date_to) $_docput_date_to=strtotime($_docput_date_to);
      
       $_result["total"] =count($this->docput_model->get_docput($_docput_number,$_docin_number,$_docin_send_number,$_docput_date_from,$_docput_date_to));
       $_row = $this->docput_model->get_docput($_docput_number,$_docin_number,$_docin_send_number,$_docput_date_from,$_docput_date_to,$_offset,$_num);
       $_row=array_values($_row);
        foreach ($_row as $_key=>$_value){
            if($_value->docput_status==0) $_row[$_key]->docput_status='编辑';
            if($_value->docput_status==1) $_row[$_key]->docput_status='审核';
            if($_value->docput_status==2) $_row[$_key]->docput_status='关闭';
            $_row[$_key]->docput_datetime=date('Y-m-d H:i:s',$_value->docput_datetime);
        }
       $_result["rows"]=$_row;
        echo json_encode($_result);
    }

    public function get_goods_type($_attr){
        $_row = $this->goods_model->get_goods_type();
        foreach ($_row as $_key=>$_value){
            $_rows[$_key]->id=$_value->type_id;
            $_rows[$_key]->text=$_value->type_name;
        }
        $_row=array_values($_rows);
        $_str='['.substr(json_encode($_row),1,-1).']';
        if($_attr=='array'){
            return  $_row;
        }else{
            echo $_str;
        }
    }    
    
    public function docput_ok(){       
        $_docput_id=$this->input->post('docput_id');
        if(!empty($_docput_id)){
            
            $_docput =$this->docput_model->get_one_docput($_docput_id);
           if($_docput['docput_status']==0){
               
               if($this->docput_model->get_one_inventory($_docput_id)){
                    echo'{"success":false,"message":"逻辑上不该存在的单据已经存在，异常暂停！"}';exit;
                }            

                $_docput_number=$this->input->post('docput_number');

                $_docin_id=$this->input->post('docin_id');
                $_docin_number=$this->input->post('docin_number');
                $_docin_send_number=$this->input->post('docin_send_number');            
                if(!empty($_docin_send_number)){
                    $_data ["docin_send_number"]=$_docin_send_number;
                }

                $_data ["inventory_datetime"]=time();
              
                $_rows=$this->docput_model->get_docputlist($_docput_id);
                foreach($_rows as $_key=>$_value){
                    unset($_value->docputlist_id);
                    unset($_value->goods_name);
                    unset($_value->goods_code);
                    unset($_value->goods_type);
                    unset($_value->goods_weight);
                    $_value->docput_number=$_docput_number;
                    $_value->docin_id=$_docin_id;
                    $_value->docin_number=$_docin_number;
                    $_value->docin_send_number=$_docin_send_number;
                    $_value->inventory_datetime=time();
                }

                if($this->docput_model->add_inventory($_rows)){
                    $_arr['docput_status']=1;
                    if($this->docput_model->update_docput($_docput_id,$_arr)){
                        echo'{"success":true,"message":"审核成功！"}';                        
                    }else{
                        echo'{"success":false,"message":"审核成功但单据更新失败！"}';exit;
                    }                    
                }else{
                    echo'{"success":false,"message":"审核后续单明细生成失败！"}';exit;    
                }

           }else{
               echo'{"success":false,"message":"这必须是非法访问！"}';exit;
           }
        }else{
            echo'{"success":false,"message":"这肯定非法访问！"}';exit;
        }       
        
    }

    public function docput_back(){

        $_docput_id=$this->input->post('docput_id');
        if(!empty($_docput_id)){
        
            $_docput =$this->docput_model->get_one_docput($_docput_id);
            if($_docput['docput_status']==1){
                 
                if(!$this->docput_model->get_one_inventory($_docput_id)){
                    echo'{"success":false,"message":"逻辑上存在的单据不存在，异常暂停！"}';exit;
                }

                    if($this->docput_model->del_inventory($_docput_id)){
                        $_arr['docput_status']=0;
                        if($this->docput_model->update_docput($_docput_id,$_arr)){
                            echo'{"success":true,"message":"后退成功！"}';
                        }else{
                            echo'{"success":false,"message":"后退成功但单据更新失败！"}';exit;
                        }
                    }else{
                        echo'{"success":false,"message":"后退后续单明细删除失败！"}';exit;
                    }

            }else{
                echo'{"success":false,"message":"只有审核状态才能继续操作！"}';exit;
            }
        }else{
            echo'{"success":false,"message":"这肯定非法访问！"}';exit;
        }        
        
    }
     
    public function docput_print($_id){
        $_data ['url'] = $this->url;
        $_data ['docputhead'] =$this->docput_model->get_one_docput($_id);
        $_rows=$this->docput_model->get_docputlist($_id);
        $_status=$this->get_goods_status();
        $_area=$this->get_area();
        $_position=$this->get_position();
        foreach($_rows as $_key=>$_value){
            foreach($_status as $_k=>$_v){
                if($_v->id==$_value->goods_status){
                    $_rows[$_key]->goods_status=$_v->text;break;
                }
            }
            foreach($_area as $_k=>$_v){
                if($_v->area_id==$_value->area_id){
                    $_rows[$_key]->area_id=$_v->area_name;break;
                }
            }
            foreach($_position as $_k=>$_v){
                if($_v->position_id==$_value->position_id){
                    $_rows[$_key]->position_id=$_v->position_name;break;
                }
            }            
        }
        $_data ['docputlist']=$_rows;
        $this->load->view('in/docputprint',$_data);
    }
}