<?php
/**
 * Author:echo
 * Email:sqlgun@qq.com
 * Date: 2018-7-17
 * 有偿二次开发，邮件联系
 */
class Docout extends MY_Controller{
    
    public function __construct(){
        parent::__construct();
        $this->load->model('out/docout_model');
    }
    public function index(){
        $_data ['button']=$this->navButton('In');
        $_data ['url'] = $this->url;
        $_data['status']=$this->get_goods_status();

        $this->load->view('out/docout',$_data); 
    }
    
    public function add_view(){
        $_data ['url'] = $this->url;
        $this->load->view('out/docouta',$_data);
    }    
    
    public function edit_view($_id){
        $_data ['url'] = $this->url;
        $_data ['id']=$_id;
        $_data ['docouthead'] =$this->docout_model->get_one_docout($_id);
        $this->load->view('out/docoute',$_data);
    }
    
    public function look_view($_id){
        $_data ['url'] = $this->url;
        $_data ['id']=$_id;
        $_data ['docouthead'] =$this->docout_model->get_one_docout($_id);
        $this->load->view('out/docoutl',$_data);
    }    
    
    public function get_docoutlist($_id){
        $_rows=$this->docout_model->get_docoutlist($_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->docout_model->get_all_goods();
        echo json_encode($_row);        
    }    
    public function get_all_status(){
        $_row=$this->docout_model->get_all_status();
        echo json_encode($_row);
    }    
    
    public function get_goods_status(){
        $_row = $this->docout_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_docout(){         
         
             $_docoutlist_date=$this->input->post('docoutlist_date');
             $_docoutlist_date_nums=count($_docoutlist_date);
             if(empty($_docoutlist_date) || $_docoutlist_date_nums==0){
                 echo'{"success":false,"message":"非法访问"}';exit;
             } 
             

            $_str=$this->expect($_docoutlist_date);
             
             if($_str){
                 echo'{"success":false,"message":"'.$_str.'"}';exit;
             }


            $_docout_number=$this->input->post('docout_number');
            $_docout_get_number=$this->input->post('docout_get_number');            
            if(empty($_docout_number)){
                $_data ["docout_number"]=time();
            }else{
                $_data ["docout_number"]=$_docout_number;
            }
            if(!empty($_docout_get_number)){
                $_data ["docout_get_number"]=$_docout_get_number;
            }
            
            $_data ["send_name"]=$this->input->post('send_name');
            $_data ["send_phone"]=$this->input->post('send_phone');
            $_data ["send_address"]=$this->input->post('send_address');            
            $_data ["docout_datetime"]=time();
            $_id=$this->docout_model->add_docout($_data);
            if($_id){
                $_arr=array();
                foreach($_docoutlist_date as $_key=>$_value){
                    $_arr[$_key]['docout_id']=$_id;
                    $_arr[$_key]['goods_id']=$_value['goods_id'];
                    $_arr[$_key]['goods_unit']=$_value['goods_unit'];
                    $_arr[$_key]['goods_status']=$_value['goods_status'];

                    $_arr[$_key]['goods_nums']=$_value['goods_nums'];
                    $_arr[$_key]['goods_postil']=$_value['goods_postil'];
                    $_arr[$_key]['inventory_id']=$_value['inventory_id'];
                }
                 if($this->docout_model->add_docoutlist($_arr)){
                     $_new_nums=count($_docoutlist_date);
                     echo'{"success":true,"message":"添加成功共'.$_docoutlist_date_nums.'条明细合成'.$_new_nums.'条明细"}';                     
                 }else{
                     echo'{"success":false,"message":"添加失败"}';exit;
                 }               
            }else{
                echo'{"success":false,"message":"单头生成异常！"}';exit;
            }
            

     }     
     
     protected  function postil($_str1,$_str2){
         $_string1=explode(",",$_str1);
         $_string2=explode(",",$_str2);
         $_postilarr1=array_diff($_string1,$_string2);
         $_postilarr2=array_diff($_string2,$_string1);
         if(empty($_postilarr1) and empty($_postilarr2)){
             return true;
         }else{
             return false;
         }
     }
     
     protected  function expect(&$_docoutlist_date){
         
         $_arr=$_docoutlist_date;
         foreach($_docoutlist_date as $_key=>$_value){
             foreach($_arr as $_k=>$_v){
                 if($_key!=$_k){
                      
                     if($_value['goods_id']==$_v['goods_id'] and $_value['goods_status']==$_v['goods_status'] and $_value['goods_unit']==$_v['goods_unit'] and empty($_value['goods_postil']) and empty($_v['goods_postil'])){
                         $_docoutlist_date[$_key]['goods_nums']+=$_v['goods_nums'];
                         unset($_docoutlist_date[$_k]);
                     }
                     if(!empty($_value['goods_postil'])){
                         $_postilequ=$this->postil($_value['goods_postil'],$_v['goods_postil']);
                         if($_value['goods_id']==$_v['goods_id'] and $_value['goods_status']==$_v['goods_status'] and $_value['goods_unit']==$_v['goods_unit'] and $_postilequ){
                             $_docoutlist_date[$_key]['goods_nums']+=$_v['goods_nums'];
                             unset($_docoutlist_date[$_k]);
                         }
                     }    
                 }
             }
         }
//          print_r($_docoutlist_date); exit;
         
         $_status=$this->get_goods_status();
         foreach($_docoutlist_date as $_key=>$_value){
             foreach($_status as $_k=>$_v){
                 if($_v->text==$_value['goods_status']){
                     $_docoutlist_date[$_key]['goods_status']=$_v->id;
                 }
             }
             $_try=array();
             $_try['goods_id']=$_value['goods_id'];
             $_try['goods_status']=$_docoutlist_date[$_key]['goods_status'];
             $_try['goods_unit']=$_value['goods_unit'];
              
             $_docoutlist_date[$_key]['inventory_id']="";
             $_nums=0;
             if(empty($_value['goods_postil'])){
                 $_try['goods_postil']='';
                 $_rows=$this->docout_model->get_inventory($_try);
                 if($_rows){
                     foreach ($_rows as $_kk=>$_vv){
                         $_nums+=$_vv->goods_nums;
                     }
                     if($_nums>=$_value['goods_nums']){
                         $_total=0;
                         foreach ($_rows as $_kk=>$_vv){
                             $_total+=$_vv->goods_nums;
                             if($_total>=$_value['goods_nums']){
                                 $_docoutlist_date[$_key]['inventory_id'].=$_vv->inventory_id;break;
                             }else{
                                 $_docoutlist_date[$_key]['inventory_id'].=$_vv->inventory_id.',';
                             }
                         }                          
                     }else{
                         $_str.=$_value['goods_name']."：数量".$_value['goods_nums'].$_value['goods_unit']."->库存不够！"."<br />";
                     }
                      
                 }else{
                     $_str.=$_value['goods_name']."：数量".$_value['goods_nums'].$_value['goods_unit']."->没有库存！"."<br />";
                 }
             }else{
                 $_goods_postil=explode(",",$_value['goods_postil']);
                 $_rows=$this->docout_model->get_inventory($_try);
                 if($_rows){
                     $_postil=array();
                     foreach ($_rows as $_kk=>$_vv){
                         $_postilsql=explode(",",$_vv->goods_postil);
                         $_postilarr1=array_diff($_goods_postil,$_postilsql);
                         $_postilarr2=array_diff($_postilsql,$_goods_postil);
                         if(empty($_postilarr1) and empty($_postilarr2)){
                             $_postil[]=$_vv;
                         }
                     }
                     if(!empty($_postil)){
                         foreach ($_postil as $_kk=>$_vv){
                             $_num+=$_vv->goods_nums;
                         }
                         if($_num>=$_value['goods_nums']){
                             $_totals=0;
                             foreach ($_postil as $_kk=>$_vv){
                                 $_totals+=$_vv->goods_nums;
                                 if($_totals>=$_value['goods_nums']){
                                     $_docoutlist_date[$_key]['inventory_id'].=$_vv->inventory_id;break;
                                 }else{
                                     $_docoutlist_date[$_key]['inventory_id'].=$_vv->inventory_id.',';
                                 }
                             }
         
                         }else{
                             $_str.=$_value['goods_name']."：数量".$_value['goods_nums'].$_value['goods_unit'].$_value['goods_postil']."->库存不够！"."<br />";
                         }
                     }else{
                         $_str.=$_value['goods_name']."：数量".$_value['goods_nums'].$_value['goods_unit'].$_value['goods_postil']."->没有库存！"."<br />";
                     }
                 }else{
                     $_str.=$_value['goods_name']."：数量".$_value['goods_nums'].$_value['goods_unit'].$_value['goods_postil']."->没有库存！"."<br />";
                 }
                  
             }
              
         }         
         return $_str;
     }
     
    
     public function update_docout($_id){
//          print_r($this->input->post());exit;
         $_docout_id=$_id;
         $_docoutlist_date=$this->input->post('docoutlist_date');
         $_docoutlist_date_nums=count($_docoutlist_date);
         if(empty($_docoutlist_date) || $_docoutlist_date_nums==0){
             echo'{"success":false,"message":"非法访问"}';exit;
         }
         
         $_str=$this->expect($_docoutlist_date);
          
         if($_str){
             echo'{"success":false,"message":"'.$_str.'"}';exit;
         }

         $_docout_number=$this->input->post('docout_number');
         $_docout_get_number=$this->input->post('docout_get_number');
         if(empty($_docout_number)){
             $_data ["docout_number"]=time();
         }else{
             $_data ["docout_number"]=$_docout_number;
         }

         $_data ["docout_get_number"]=$_docout_get_number;
         $_data ["send_name"]=$this->input->post('send_name');
         $_data ["send_phone"]=$this->input->post('send_phone');
         $_data ["send_address"]=$this->input->post('send_address');
       
         $_data ["docout_update_datetime"]=time();
         $_state=$this->docout_model->update_docout($_docout_id,$_data);
         if($_state){
             $_arr=array();
             foreach($_docoutlist_date as $_key=>$_value){
                 $_arr[$_key]['docout_id']=$_id;
                 $_arr[$_key]['goods_id']=$_value['goods_id'];
                 $_arr[$_key]['goods_unit']=$_value['goods_unit'];
                 $_arr[$_key]['goods_status']=$_value['goods_status'];

                 $_arr[$_key]['goods_nums']=$_value['goods_nums'];
                 $_arr[$_key]['goods_postil']=$_value['goods_postil'];
                 $_arr[$_key]['inventory_id']=$_value['inventory_id'];
             }
             if($this->docout_model->del_docoutlist($_docout_id)){
                 if($this->docout_model->add_docoutlist($_arr)){
                     $_new_nums=count($_docoutlist_date);
                     echo'{"success":true,"message":"修改成功,提交'.$_docoutlist_date_nums.'条明细合成'.$_new_nums.'条明细"}';
                 }else{
                     echo'{"success":false,"message":"明细修改失败"}';exit;
                 }
             }else{
                 echo'{"success":false,"message":"明细清理失败"}';exit;
             }
         }else{
             echo'{"success":false,"message":"单头修改异常！"}';exit;
         }         
         
     }
   
    
    public function get_docout(){
//         print_r($this->input->post());
       $_page=$this->input->post('page');
       $_num=$this->input->post('rows');
       $_offset = ($_page-1)*$_num;
       
       $_docout_number=$this->input->post('docout_number');
       $_docout_get_number=$this->input->post('docout_get_number');
       
       $_docout_date_from=$this->input->post('docout_date_from');
       if($_docout_date_from) $_docout_date_from=strtotime($_docout_date_from);
       $_docout_date_to=$this->input->post('docout_date_to');
       if($_docout_date_to) $_docout_date_to=strtotime($_docout_date_to);
      
       $_result["total"] =count($this->docout_model->get_docout($_docout_number,$_docout_get_number,$_docout_date_from,$_docout_date_to));
       $_row = $this->docout_model->get_docout($_docout_number,$_docout_get_number,$_docout_date_from,$_docout_date_to,$_offset,$_num);
       $_row=array_values($_row);
        foreach ($_row as $_key=>$_value){
            if($_value->docout_status==0) $_row[$_key]->docout_status='编辑';
            if($_value->docout_status==1) $_row[$_key]->docout_status='审核';
            if($_value->docout_status==2) $_row[$_key]->docout_status='关闭';
            $_row[$_key]->docout_datetime=date('Y-m-d H:i:s',$_value->docout_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 docout_ok(){       

        $_docout_id=$this->input->post('docout_id');
        if(!empty($_docout_id)){
            
            $_docout =$this->docout_model->get_one_docout($_docout_id);
           if($_docout['docout_status']==0){
               
                        $_arr['docout_status']=1;
                        if($this->docout_model->update_docout($_docout_id,$_arr)){
                           $_rows=$this->docout_model->get_docoutlist($_docout_id);
                           if($_rows){
                               foreach($_rows as $_key=>$_value){
                                   if(empty($_value->inventory_id)){
                                       $_error=true;
                                       break;
                                   }
                               }
                               if(!$_error){
                                   
                                   $_rollback=array();
                                   foreach ($_rows as $_key=>$_value){//foreach 开始
                                       $_inventory_id=explode(",",$_value->inventory_id);
                                       
                                       if(count($_inventory_id)==1){
                                           foreach ($_inventory_id as $_val){
                                               $_result=$this->docout_model->get_inventory_nums($_val);
                                               if($_value->goods_nums - $_result['goods_nums']==0 ){
                                                   $_inven['goods_nums']=0;
                                                   $_inven['inventory_used_datetime']=time();
                                                  if($this->docout_model->update_inventory($_val,$_inven)){
                                                      $_str.=$_value->goods_name."：数量".$_value->goods_nums.$_value->goods_unit.$_value->goods_postil."->库存扣除！"."<br />";
                                                      $_rollback[]=array($_val=>$_result['goods_nums']);
                                                      break;
                                                  }else{
                                                      $_arr['docout_status']=0;
                                                      $this->docout_model->update_docout($_docout_id,$_arr);
                                                      if(!empty($_rollback)){
                                                          if($this->rollback($_rollback)){
                                                              echo'{"success":false,"message":"审核成功但明细扣库存【等量】信息异常，撤销审核！"}';exit;
                                                          }else{
                                                              echo'{"success":false,"message":"审核回滚异常，只撤销了审核状态，停止使用！"}';exit;
                                                          }
                                                      }else{
                                                              echo'{"success":false,"message":"库存上来就不够了！"}';exit;
                                                      }    
                                                  }
                                               }
                                               if($_value->goods_nums - $_result['goods_nums'] < 0 ){
                                                   $_inven['goods_nums']=$_result['goods_nums'] - $_value->goods_nums;
                                                   $_inven['inventory_used_datetime']=time();
                                                   if($this->docout_model->update_inventory($_val,$_inven)){
                                                       $_str.=$_value->goods_name."：数量".$_value->goods_nums.$_value->goods_unit.$_value->goods_postil."->库存扣除！"."<br />";
                                                       $_rollback[]=array($_val=>$_value->goods_nums);
                                                       break;
                                                   }else{
                                                       $_arr['docout_status']=0;
                                                       $this->docout_model->update_docout($_docout_id,$_arr);
                                                       if(!empty($_rollback)){
                                                           if($this->rollback($_rollback)){
                                                               echo'{"success":false,"message":"审核成功但明细扣库存【超量】信息异常，撤销审核！"}';exit;
                                                           }else{
                                                               echo'{"success":false,"message":"审核回滚异常，只撤销了审核状态，停止使用！"}';exit;
                                                           }
                                                       }else{
                                                               echo'{"success":false,"message":"库存上来就不够了！"}';exit;
                                                       }    
                                                   }
                                               }else{
                                                   $_arr['docout_status']=0;
                                                   $this->docout_model->update_docout($_docout_id,$_arr);
                                                   if(!empty($_rollback)){
                                                       if($this->rollback($_rollback)){
                                                           echo'{"success":false,"message":"审核成功但明细出库量大于库存量信息异常，撤销审核！"}';exit;
                                                       }else{
                                                           echo'{"success":false,"message":"审核回滚异常，只撤销了审核状态，停止使用！"}';exit;
                                                       }
                                                   }else{
                                                           echo'{"success":false,"message":"库存上来就不够了！"}';exit;                                                 
                                                   }
                                               }                                               
                                           }    
                                       }
//                                        print_r($_inventory_id);
                                       if(count($_inventory_id)>1){
                                            
                                           $_total=0;
                                           foreach ($_inventory_id as $_val){
                                               $_result=$this->docout_model->get_inventory_nums($_val);
                                               $_total+=$_result['goods_nums'];
                                           }
                                           if($_total<$_value->goods_nums){
                                               $_arr['docout_status']=0;
                                               $this->docout_model->update_docout($_docout_id,$_arr);    
                                               if(!empty($_rollback)){
                                                   if($this->rollback($_rollback)){
                                                       echo'{"success":false,"message":"审核成功但明细出库量大于库存量信息异常，撤销审核！"}';exit;     
                                                   }else{
                                                       echo'{"success":false,"message":"审核回滚异常1，只撤销了审核状态，停止使用！"}';exit;
                                                   }
                                               }else{
                                                   echo'{"success":false,"message":"库存上来就不够了！"}';exit;                                                   
                                               }
                                           }
                                           
                                               $_add=0;$_array=array();$_back=array();
                                               $_nums=count($_inventory_id);
                                               for ($i=0; $i<=$_nums-1; $i++){
                                                   $_result=$this->docout_model->get_inventory_nums($_inventory_id[$i]);
                                                    if($i==$_nums-1){
                                                        $_inven['goods_nums']=$_result['goods_nums'] - ($_value->goods_nums - $_add);
                                                        $_back[]=array($_inventory_id[$i] => $_value->goods_nums - $_add );
                                                    }else{
                                                        $_add+=$_result['goods_nums'];
                                                        $_inven['goods_nums']=0;
                                                        $_back[]=array($_inventory_id[$i] => $_result['goods_nums'] );
                                                    }
                                                    $_array[]=array('inventory_id'=>$_inventory_id[$i],'goods_nums'=>$_inven['goods_nums'],'inventory_used_datetime'=>time());
                                                }
//                                                 print_r($_array);
                                                $_batchnums=$this->docout_model->update_inventory_batch('inventory', $_array, 'inventory_id');
//                                                 print_r($_batchnums);
                                                if($_batchnums){
                                                    $_str.=$_value->goods_name."：数量".$_value->goods_nums.$_value->goods_unit.$_value->goods_postil."->库存扣除！".$_batchnums." 条变更！"."<br />";
                                                    foreach($_back as $_vvv){
                                                        $_rollback[]=$_vvv;
                                                    }
                                                }else{
                                                   $_arr['docout_status']=0;
                                                   $this->docout_model->update_docout($_docout_id,$_arr);
                                                   if(!empty($_rollback)){
                                                       if($this->rollback($_rollback)){
                                                           echo'{"success":false,"message":"审核成功但批量修改明细信息异常，撤销审核！"}';exit;
                                                       }else{
                                                           echo'{"success":false,"message":"审核回滚异常，只撤销了审核状态，停止使用！"}';exit;
                                                       }
                                                   }else{
                                                       echo'{"success":false,"message":"库存上来就不够了！"}';exit;                                                       
                                                   }
                                               }  
                                       }
                                   }//foreach 结束  
//                               print_r($_rollback);exit;
                                   if($_str){
                                       $_log=$this->docout_model->get_docoutlog($_docout_id);
                                       $_data['subtract_log']=serialize($_rollback);
                                       if($_log){
                                           if ($this->docout_model->update_docoutlog($_docout_id,$_data)){
                                               $_str.="出库单：".$_docout['docout_number']."->所有库存已扣除，库存分配有修改完成！"."<br />";                                                
                                               echo'{"success":true,"message":"'.$_str.'"}';
                                           }else{
                                               $_str.="出库单：".$_docout['docout_number']."->所有库存已扣除，库存分配完成！"."<br />";
                                               echo'{"success":true,"message":"'.$_str.'"}';                                               
                                           }
                                       }else{
                                           $_data['docout_id']=$_docout_id;
                                           if ($this->docout_model->add_docoutlog($_data)){
                                               $_str.="出库单：".$_docout['docout_number']."->所有库存已扣除，库存分配新增完成！"."<br />";
                                               echo'{"success":true,"message":"'.$_str.'"}';                                                
                                           }else{
                                               $_str.="出库单：".$_docout['docout_number']."->所有库存已扣除，库存分配新增失败！"."<br />";
                                               echo'{"success":false,"message":"'.$_str.'"}';
                                           }                                           
                                       }
                                   }else{
                                       echo'{"success":false,"message":"这个报错不应该让你看到，异常中的异常！"}';exit;                                                                    
                                   }
                               }else{
                                   $_arr['docout_status']=0;
                                   $this->docout_model->update_docout($_docout_id,$_arr);
                                   echo'{"success":false,"message":"审核成功但明细所对应库存信息异常，撤销审核！"}';exit;
                               }
                           }else{
                               $_arr['docout_status']=0;
                               $this->docout_model->update_docout($_docout_id,$_arr);
                               echo'{"success":false,"message":"审核成功但逻辑上存在的单据消失，异常暂停后，撤销审核！"}';exit;
                           }           
                        }else{
                            echo'{"success":false,"message":"审核失败！"}';exit;
                        }                    

           }else{
               echo'{"success":false,"message":"这必须是非法访问！"}';exit;
           }
        }else{
            echo'{"success":false,"message":"这肯定非法访问！"}';exit;
        }       
        
    }
    
    protected  function rollback($_arr){
        foreach($_arr as $_value){
            foreach($_value as $_k=>$_v){
                $_ids[]=$_k;
            }
        }
        
        $_result=$this->docout_model->rollback_get_id($_ids);
        
        foreach($_arr as $_key=>$_value){
            foreach($_value as $_k=>$_v){
                foreach($_result as $_kk=>$_vv){
                    if($_k==$_vv->inventory_id){
                        $_arr[$_key][$_k]=$_v+$_vv->goods_nums;
                        break;
                    }
                }
            }
        }

        foreach($_arr as $_value){
            foreach($_value as $_k=>$_v){
                $_array[]=array('inventory_id'=>$_k,'goods_nums'=>$_v);                
            }
        }
        $_batchnums=$this->docout_model->update_inventory_batch('inventory', $_array, 'inventory_id');
        return $_batchnums;
    }
    
    
    
    public function docout_back(){

        $_docout_id=$this->input->post('docout_id');
        if(!empty($_docout_id)){
        
            $_docout =$this->docout_model->get_one_docout($_docout_id);
            if($_docout['docout_status']==1){
                 
                if(!$this->docout_model->get_docoutlog($_docout_id)){
                    echo'{"success":false,"message":"逻辑上存在的单据不存在，异常暂停！"}';exit;
                }else{
                    $_rows=$this->docout_model->get_docoutlog($_docout_id);
                    $_arr=unserialize($_rows['subtract_log']);
                    
                    $_arrs['docout_status']=0;
                    $this->docout_model->update_docout($_docout_id,$_arrs);
                    if($this->rollback($_arr)){
                        echo'{"success":true,"message":"操作成功"}';
                    }else{
                        $_arrs['docout_status']=1;
                        $this->docout_model->update_docout($_docout_id,$_arrs);
                        echo'{"success":false,"message":"操作回滚异常导致失败！"}';exit;
                    }
                }

            }else{
                echo'{"success":false,"message":"只有审核状态才能继续操作！"}';exit;
            }
        }else{
            echo'{"success":false,"message":"这肯定非法访问！"}';exit;
        }        
        
    }
    
    public function docout_no(){
        $_docout_id=$this->input->post('docout_id');
        $_status=$this->input->post('status');
        if(!empty($_docout_id)){
            if($_status!='关闭'){
                if($_status=='编辑'){
                    $_data ["docout_status"]=2;
                    $_state=$this->docout_model->update_docout($_docout_id,$_data);
                    if($_state){
                        echo'{"success":true,"message":"关闭成功"}';exit;
                    }else{
                        echo'{"success":false,"message":"关闭编辑单据失败！"}';exit;
                    }
                }else if($_status=='审核'){

//                     $_data ["docout_status"]=2;
//                     $_state=$this->docout_model->update_docout($_docout_id,$_data);
//                     if($_state){

                        if(!$this->docout_model->get_docoutlog($_docout_id)){
                            echo'{"success":false,"message":"逻辑上存在的单据不存在，异常暂停！"}';exit;
                        }else{
                            $_rows=$this->docout_model->get_docoutlog($_docout_id);
                            $_arr=unserialize($_rows['subtract_log']);
                        
                            $_arrs['docout_status']=2;
                            $this->docout_model->update_docout($_docout_id,$_arrs);
                            if($this->rollback($_arr)){
                                echo'{"success":true,"message":"操作成功"}';
                            }else{
                                $_arrs['docout_status']=1;
                                $this->docout_model->update_docout($_docout_id,$_arrs);
                                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 docout_print($_id){
        $_data ['url'] = $this->url;
        $_data ['docouthead'] =$this->docout_model->get_one_docout($_id);
        $_rows=$this->docout_model->get_docoutlist($_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;
                }
            }

            $_block=$this->docout_model->get_inventory_area_position($_value->inventory_id);
            $_str="";
            foreach($_block as $_kk=>$_vv){
                $_str.=$_vv->area_name."：".$_vv->position_name."<br />";
            }
            $_rows[$_key]->block_name=$_str;
        }
        $_data ['docoutlist']=$_rows;
        $this->load->view('out/docoutprint',$_data);
    }
}