<?php
namespace Smi\Model;

use Think\Model;

header("Content-type:text/html;charset=utf-8");
class SalesModel extends Model
{
	//图片上传操作
	
	protected  $tableName = 'purchase_goods';
    public function upload(){    	
    	$id=I('id');  	
    	if($FILES['img']['error']!=4){
    		if($id){
    			//是否已有图片
    			$old_goods=$this->where("`id`={$id}")->getField('pic_goods');  
    		}   		  	
            $upload = new \Think\Upload();
	    	$upload->maxSize = 3145728;
	    	$upload->exts = array('jpg','gif','png','jpeg');
	    	$upload->rootPath = './Uploads/'; // 上传附件根目录
	    	$upload->savePath = 'goods_img/'; //设置附件上传子目录
	    	//上传文件
	    	$info = $upload->upload();
	    	if(empty($old_goods)){
	    		if(!$info){
	    		$result=$upload->getError();
	    		alertMsg($result);
		    	} else {
		    		  $pic_goods = $info['img']["savepath"] . $info['img']["savename"];
		    		  return $pic_goods;
		    	}
	    	}else{
	    		return $old_goods;
	    	}
	    	
    	} 
    	
    }
    //模糊查询匹配
    public function searching($search){
    	$types = M('goods_categories')->field('id,name')->select();
    	foreach ($types as $key => $value) {
    			if($value['name']==$search){
    				$catid=$value['id'];
    			}
    	}
    		return $catid;
    }
}