<?php
/**
 * Author:echo
 * Email:sqlgun@qq.com
 * Date: 2018-7-17
 * 有偿二次开发，邮件联系
 */
class Docin extends MY_Controller{
    
    public function __construct(){
        parent::__construct();
        $this->load->model('in/docin_model');
    }
    public function index(){
        $_data ['button']=$this->navButton('In');
        $_data ['url'] = $this->url;
        $_data['status']=$this->get_goods_status();
        $this->load->view('in/docin',$_data); 
    }
    
    public function add_view(){
        $_data ['url'] = $this->url;
        $this->load->view('in/docina',$_data);
    }    
    
    public function edit_view($_id){
        $_data ['url'] = $this->url;
        $_data ['id']=$_id;
        $_data ['docinhead'] =$this->docin_model->get_one_docin($_id);
        $this->load->view('in/docine',$_data);
    }
    
    public function look_view($_id){
        $_data ['url'] = $this->url;
        $_data ['id']=$_id;
        $_data ['docinhead'] =$this->docin_model->get_one_docin($_id);
        $this->load->view('in/docinl',$_data);
    }    
    
    public function get_docinlist($_id){
        $_rows=$this->docin_model->get_docinlist($_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->docin_model->get_all_goods();
        echo json_encode($_row);        
    }    
    public function get_all_status(){
        $_row=$this->docin_model->get_all_status();
        echo json_encode($_row);
    }    
    
    public function get_goods_status(){
        $_row = $this->docin_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_docin(){              
//          print_r($this->input->post() );exit;
             $_docinlist_date=$this->input->post('docinlist_date');
             $_docinlist_date_nums=count($_docinlist_date);
             if(empty($_docinlist_date) || $_docinlist_date_nums==0){
                 echo'{"success":false,"message":"非法访问"}';exit;
             }
             
            $_docin_number=$this->input->post('docin_number');
            $_docin_send_number=$this->input->post('docin_send_number');            
            if(empty($_docin_number)){
                $_data ["docin_number"]=time();
            }else{
                $_data ["docin_number"]=$_docin_number;
            }
            if(!empty($_docin_send_number)){
                $_data ["docin_send_number"]=$_docin_send_number;
            }
            $_data ["docin_datetime"]=time();
            $_id=$this->docin_model->add_docin($_data);
//             $_id=1;
            if($_id){
                $_status=$this->get_goods_status();
                $_arr=array();
                foreach($_docinlist_date as $_key=>$_value){
                    $_arr[$_key]['docin_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->docin_model->add_docinlist($_arr)){
                     echo'{"success":true,"message":"添加成功共'.$_docinlist_date_nums.'条明细！"}';
                 }else{
                     echo'{"success":false,"message":"添加失败"}';
                 }               
            }else{
                echo'{"success":false,"message":"单头生成异常！"}';exit;
            }
            

     }     
    
     public function update_docin($_id){
         $_docin_id=$_id;
         $_docinlist_date=$this->input->post('docinlist_date');
         $_docinlist_date_nums=count($_docinlist_date);
         if(empty($_docinlist_date) || $_docinlist_date_nums==0){
             echo'{"success":false,"message":"非法访问"}';exit;
         }
          
         $_docin_number=$this->input->post('docin_number');
         $_docin_send_number=$this->input->post('docin_send_number');
         if(empty($_docin_number)){
             $_data ["docin_number"]=time();
         }else{
             $_data ["docin_number"]=$_docin_number;
         }

         $_data ["docin_send_number"]=$_docin_send_number;
       
         $_data ["docin_update_datetime"]=time();
         $_state=$this->docin_model->update_docin($_docin_id,$_data);
         if($_state){
             $_status=$this->get_goods_status();
             $_arr=array();
             foreach($_docinlist_date as $_key=>$_value){
                 $_arr[$_key]['docin_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->docin_model->del_docinlist($_docin_id)){
                 if($this->docin_model->add_docinlist($_arr)){
                     echo'{"success":true,"message":"修改成功共更新'.$_docinlist_date_nums.'条明细！"}';
                 }else{
                     echo'{"success":false,"message":"明细修改失败"}';exit;
                 }
             }else{
                 echo'{"success":false,"message":"明细清理失败"}';exit;
             }
         }else{
             echo'{"success":false,"message":"单头修改异常！"}';exit;
         }         
         
     }
   
    
    public function get_docin(){
//         print_r($this->input->post());
       $_page=$this->input->post('page');
       $_num=$this->input->post('rows');
       $_offset = ($_page-1)*$_num;
       
       $_docin_number=$this->input->post('docin_number');
       $_docin_send_number=$this->input->post('docin_send_number');
       
       $_docin_date_from=$this->input->post('docin_date_from');
       if($_docin_date_from) $_docin_date_from=strtotime($_docin_date_from);
       $_docin_date_to=$this->input->post('docin_date_to');
       if($_docin_date_to) $_docin_date_to=strtotime($_docin_date_to);
      
       $_result["total"] =count($this->docin_model->get_docin($_docin_number,$_docin_send_number,$_docin_date_from,$_docin_date_to));
       $_row = $this->docin_model->get_docin($_docin_number,$_docin_send_number,$_docin_date_from,$_docin_date_to,$_offset,$_num);
       $_row=array_values($_row);
        foreach ($_row as $_key=>$_value){
            if($_value->docin_status==0) $_row[$_key]->docin_status='编辑';
            if($_value->docin_status==1) $_row[$_key]->docin_status='审核';
            if($_value->docin_status==2) $_row[$_key]->docin_status='关闭';
            $_row[$_key]->docin_datetime=date('Y-m-d H:i:s',$_value->docin_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 docin_ok(){       
        
        $_docin_id=$this->input->post('docin_id');
        if(!empty($_docin_id)){
            
            $_docin =$this->docin_model->get_one_docin($_docin_id);
           if($_docin['docin_status']==0){
               
               if($this->docin_model->get_one_docput($_docin_id)){
                    echo'{"success":false,"message":"逻辑上不该存在的单据已经存在，异常暂停！"}';exit;
                }            
                $_data ["docin_id"]=$_docin_id;
                $_data ["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 ["docput_number"]=time();
                $_data ["docput_datetime"]=time();
              
                $_id=$this->docin_model->add_docput($_data);
                if($_id){
                    $_rows=$this->docin_model->get_docinlist($_docin_id);
                    foreach($_rows as $_key=>$_value){
                        unset($_value->docinlist_id);
                        unset($_value->docin_id);
                        unset($_value->goods_name);
                        unset($_value->goods_code);
                        unset($_value->goods_type);
                        unset($_value->goods_weight);
                        $_value->docput_id=$_id;
                    }
                    if($this->docin_model->add_docputlist($_rows)){
                        $_arr['docin_status']=1;
                        if($this->docin_model->update_docin($_docin_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;
           }
        }else{
            echo'{"success":false,"message":"这肯定非法访问！"}';exit;
        }       
        
    }

    public function docin_back(){

        $_docin_id=$this->input->post('docin_id');
        if(!empty($_docin_id)){
        
            $_docin =$this->docin_model->get_one_docin($_docin_id);
            if($_docin['docin_status']==1){
                 
                if(!$this->docin_model->get_one_docput($_docin_id)){
                    echo'{"success":false,"message":"逻辑上存在的单据不存在，异常暂停！"}';exit;
                }else{
                    $_rows=$this->docin_model->get_one_docput($_docin_id);
                    $_docput_id=$_rows['docput_id'];
                    if($_rows['docput_status']!=0){
                        echo'{"success":false,"message":"后续单据已经审核，这里不能直接后退了！"}';exit;
                    }
                }
                if($this->docin_model->del_docput($_docin_id)){
                    if($this->docin_model->del_docputlist($_docput_id)){
                        $_arr['docin_status']=0;
                        if($this->docin_model->update_docin($_docin_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;
            }
        }else{
            echo'{"success":false,"message":"这肯定非法访问！"}';exit;
        }        
        
    }
    
    public function docin_no(){
        $_docin_id=$this->input->post('docin_id');
        $_status=$this->input->post('status');
        if(!empty($_docin_id)){
            if($_status!='关闭'){
                if($_status=='编辑'){
                    $_data ["docin_status"]=2;
                    $_state=$this->docin_model->update_docin($_docin_id,$_data);
                    if($_state){
                        echo'{"success":true,"message":"关闭成功"}';exit;
                    }else{
                        echo'{"success":false,"message":"关闭编辑单据失败！"}';exit;
                    }
                }else if($_status=='审核'){

                    $_data ["docin_status"]=2;
                    $_state=$this->docin_model->update_docin($_docin_id,$_data);
                    if($_state){
                        $_rows=$this->docin_model->get_one_docput($_docin_id);
                        if($_rows){
                            if($_rows['docput_status']==0){
                                $_data1 ["docput_status"]=2;
                                $_state=$this->docin_model->update_docput($_docin_id,$_data1);
                                if($_state){
                                    echo'{"success":true,"message":"该单据已生成上架单，全部关闭成功"}';exit;                                    
                                }else{
                                    echo'{"success":false,"message":"关闭成功，但是更新后续单据状态失败！"}';exit;
                                }                                  
                            }else if($_rows['docput_status']==1){
                                
                                $_data1 ["docput_status"]=2;
                                $_state=$this->docin_model->update_docput($_docin_id,$_data1);
                                if($_state){
                                    $_docput_id=$_rows['docput_id'];

                                    $_state=$this->docin_model->del_inventory($_docput_id);
                                    if($_state){
                                        echo'{"success":true,"message":"该单据已生成库存，全部关闭成功"}';exit;
                                    }else{
                                        echo'{"success":false,"message":"关闭成功，但是库存清除失败！"}';exit;                                        
                                    }         
                                }else{
                                    echo'{"success":false,"message":"关闭成功，但是更新后续单据状态失败，且库存未清！"}';exit;
                                }                                
                                
                            }else if($_rows['docput_status']==2){
                                echo'{"success":false,"message":"关闭成功，但是逻辑上后续单据已经关闭！"}';exit;
                            }else{
                                echo'{"success":false,"message":"关闭成功，但是逻辑上后续单据数据异常暂停！"}';exit;                                
                            }
                        }else{
                            echo'{"success":false,"message":"关闭成功，但是逻辑上后续单据不存在，异常暂停！"}';exit;                           
                        }
                    }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 docin_print($_id){
        $_data ['url'] = $this->url;
        $_data ['docinhead'] =$this->docin_model->get_one_docin($_id);
        $_rows=$this->docin_model->get_docinlist($_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;
                }
            }
        }
        $_data ['docinlist']=$_rows;
        $this->load->view('in/docinprint',$_data);
    }
}