<?php
/**
 * 门店进销存
 * @Copyright (C) 2017 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author
 */
 
namespace Shop\Model;
use Think\Model;
class InvoicingOrderModel extends Model{

    /**
     * 查询门店订货、进货、货流通知列表
     */
    public function order_list($param,$is_huoliu=0){
        $return=array('status'=>0,'msg'=>'');
        if(empty($param['shop_id'])){$return['msg']='参数错误'; return $return;}
        $map = array();
        if($param['status']) $map['status']=$param['status'];//默认查询所有的状态
        if($is_huoliu==0){
            $map['type'] = empty($param['type'])?0:$param['type'];//如果不传默认0查询门店订货列表 1进货
            //if($param['type']!=1)$map['mtype'] = empty($param['mtype'])?0:$param['mtype'];//如果不传默认0后台操作 1是门店操作
        }
        if($is_huoliu==1) unset($map['status']);
        $in_out=intval($param['in_out']);
        if($in_out==0){//需要只查进货门店
            $map['shop_id_in'] = $param['shop_id'];//查询的门店id
        }elseif($in_out==1){//需要只查出货门店
            $map['shop_id_out'] = $param['shop_id'];//查询的门店id
        }elseif($in_out==2){//需要进货出货门店都查
            $map['shop_id_in|shop_id_out'] = $param['shop_id'];//查询的门店id
        }elseif($in_out==3){
            $map['_string']='(type=4 and (status=2 and shop_id_in='.$param['shop_id'].') or (status=1 and shop_id_out='.$param['shop_id'].')) or (status=2 and (shop_id_in='.$param['shop_id'].' or shop_id_out='.$param['shop_id'].') and (type=0 or type=1))';
        }else {
            $map['shop_id_in'] = $param['shop_id'];//查询的门店id
        }
        $order='id desc';
        $order_list=page_list('InvoicingOrder',$map,$order);
        if(empty($order_list['list'])){$return['msg']='暂无数据'; return $return;}
        $return['status']=1;
        $return['data']=$order_list;
        return $return;
    }
    public function order_info($postdata){
        $return=array('status'=>0,'msg'=>'');
        if(empty($postdata['order_id'])){$return['msg']='参数错误'; return $return;}
        $map=array();
        $map['id']=$postdata['order_id'];
        $order_info=M('InvoicingOrder')->where($map)->field(true)->find();
        if($order_info){
            $map=array();
            $map['order_id']=$postdata['order_id'];
            $order_info['order_goods']=M('InvoicingGoods')->where($map)->field(true)->select();
            foreach($order_info['order_goods'] as $k => $v){
                $map=array();
                $map['id']=$v['goods_id'];
                $unit=M('Goods')->where($map)->getField('unit_id');
                if($unit){
                    $order_info['order_goods'][$k]['unit']=idtoname($unit,'GoodsUnit','name');
                }else{
                    $order_info['order_goods'][$k]['unit']='无';
                }
            }
            $return['status']=1;
            $return['data']=$order_info;
            return $return;
        }else{
            $return['msg']='订单信息错误';
            return $return;
        }
    }

    /**
     * 根据输入条件查询商品
     */
    public function search_goods($param){
        $return=array('status'=>0,'msg'=>'');
        if($param['is_add'] && empty($param['keyword'])){$return['msg']='请输入商品关键词进行搜索'; return $return;}
        $chu_type=$param['chu_type']; //出库类型 2供货商 3仓库 现在统一走供货商
        if(empty($chu_type)){$return['msg']='参数错误'; return $return;}
        if($param['keyword']) {//订货和入库的时候按照关键词查询商品
            $keyword = $param['keyword'];
            $map = array();
            if (is_numeric($keyword)) {
                $map['id|goods_sn|goods_name'] = array(intval($keyword), array('like', '%' . $keyword . '%'), '_multi' => true);
            } else {
                $map['goods_sn|goods_name'] = array('like', '%' . (string)$keyword . '%');
            }
        }
        if($chu_type==2){//已改
            $map['status'] = 1;
            $map['shop_id']=0;
            $map['is_delete']=0;
            $map['publish_wait']=1;
            $map['goods_type']=5;//订货商品
            $map['sale_type']=0;
            $count=0;
            $count=M('Goods')->where($map)->count();//查看是否有商品，如果有在商品表中查找，没有就到货品表查询
            if($count>0){
                $goods_info = M('Goods')->field(true)->where($map)->select();
                if(empty($goods_info)){$return['msg']='没有符合条件的商品'; return $return;}
                //有属性的商品
                $ids = M('Goods')->where($map)->getField('id',true);
                $map = array();
                $map['goods_id']=array('in',$ids);
                $count=0;
                $count=M('GoodsAttrStock')->field('goods_id,attr_key')->where($map)->count();
                $goods_attrs = array();//当前供应商的属性组合的商品
                if ($count > 0) {
                    $attrs=M('GoodsAttrStock')->field('goods_id,attr_key,stock_cfg')->where($map)->select();
                    foreach ($attrs as $v) {
                        $goods_attrs[$v['goods_id']][] = $v;
                    }
                }
                $goods_list=array();
                foreach($goods_info as $v){
                    if(!empty($goods_attrs[$v['id']])){//如果存在货品
                        foreach($goods_attrs[$v['id']] as $vo){
                            $attr_name=attr_key2name($vo['attr_key'],$v['id']);
                            $attr_key = $v['id'].'-'.$vo['attr_key'];
                            $goods_list[]=array('goods_id'=>$v['id'],'attr_key' => $attr_key,'goods_name' => $v['goods_name'], 'goods_sn' => $v['goods_sn'],'attr_name' => $attr_name,'supply_price' => $v['supply_price'],'cat_id' => $v['cat_id'],'shop_price'=>$v['shop_price'],'suppliers_id'=>$v['suppliers_id']);
                        }
                    }else{
                        $attr_key = $v['id'].'-';
                        $goods_list[]=array('goods_id'=>$v['id'],'attr_key' => $attr_key,'goods_name' => $v['goods_name'], 'goods_sn' => $v['goods_sn'],'supply_price' => $v['supply_price'],'cat_id' => $v['cat_id'],'shop_price'=>$v['shop_price'],'suppliers_id'=>$v['suppliers_id']);
                    }
                }
                $return['status']=1;
                $return['data']=$goods_list;
            }else{
                $return['msg']='没有符合条件的商品'; return $return;
            }
        }elseif($chu_type==3){
            $map['status'] = 1;
            $map['shop_id']=0;
            $map['is_delete']=0;
            $map['publish_wait']=1;
            $map['goods_type']=5;//订货商品
            $map['sale_type']=0;
            $goods_info = M('Goods')->field(true)->where($map)->select();
            if(empty($goods_info)){$return['msg']='没有符合条件的商品'; return $return;}
            //有属性的商品
            $ids = M('Goods')->where($map)->getField('id',true);
            $map = array();
            $map['goods_id']=array('in',$ids);
            $count=0;
            $count=M('StorageStock')->field('goods_id,attr_key')->where($map)->count();
            $goods_attrs = array();//当前供应商的属性组合的商品
            if ($count > 0) {
                $attrs=M('StorageStock')->field('goods_id,attr_key,stock_cfg')->where($map)->select();
                foreach ($attrs as $v) {
                    $goods_attrs[$v['goods_id']][] = $v;
                }
            }
            $goods_list=array();
            foreach($goods_info as $v){
                if(!empty($goods_attrs[$v['id']])){//如果存在货品
                    foreach($goods_attrs[$v['id']] as $vo){
                        $attr_name=attr_key2name($vo['attr_key'],$v['id']);
                        $attr_key = $v['id'].'-'.$vo['attr_key'];
                        $goods_list[]=array('goods_id'=>$v['id'],'attr_key' => $attr_key,'goods_name' => $v['goods_name'], 'goods_sn' => $v['goods_sn'],'attr_name' => $attr_name,'supply_price' => $v['supply_price'],'cat_id' => $v['cat_id'],'shop_price'=>$v['shop_price'],'suppliers_id'=>$v['suppliers_id']);
                    }
                }else{
                    $attr_key = $v['id'].'-';
                    $goods_list[]=array('goods_id'=>$v['id'],'attr_key' => $attr_key,'goods_name' => $v['goods_name'], 'goods_sn' => $v['goods_sn'],'supply_price' => $v['supply_price'],'cat_id' => $v['cat_id'],'shop_price'=>$v['shop_price'],'suppliers_id'=>$v['suppliers_id']);
                }
            }
            $return['status']=1;
            $return['data']=$goods_list;
        }
        return $return;
    }
}
?>