<?php
/** 店铺营销
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.
 * @Author zhaoh <zhaoh@qq.com>
 */

namespace Shop\Model;
use Think\Model;
class PurOrderModel extends Model{
	protected $tableName = 'Library_info';
	public function sub($postdata){
		$return = $goods_list = $add_data =  $add_goods = array();
		$return['status'] = 0;
		if(!$postdata){$return['msg'] = '参数获取错误';}
		$chu_id = $postdata['chu_id'];
		$chu_type = $postdata['chu_type'];
		$order_status = $postdata['order_status'];
		$ru_id =$postdata['ru_id'];
		$goods_allnum = 0;
		$$goods_allmoney = 0;
		if($postdata['infos'] && MID){
			foreach($postdata['infos']['goods_attr_sn'] as $k => $v){
				if($postdata['infos']['reality'][$k] > 0){
					$goods_list[$v]['goods_id'] = $postdata['infos']['goods_id'][$k];
					$goods_list[$v]['goods_name'] = $postdata['infos']['goods_name'][$k];
					$goods_list[$v]['goods_sn'] = $postdata['infos']['goods_sn'][$k];
					$goods_list[$v]['goods_attr_sn'] = $postdata['infos']['goods_attr_sn'][$k];
					$goods_list[$v]['order_num'] = $goods_list[$v]['num'] = $postdata['infos']['reality'][$k];
					$goods_list[$v]['attr_price'] = $postdata['infos']['shop_price'][$k];
					$goods_list[$v]['all_price'] = $postdata['infos']['reality'][$k]*$postdata['infos']['shop_price'][$k];
					$goods_allnum += $postdata['infos']['reality'][$k];
					$goods_allmoney += $postdata['infos']['reality'][$k]*$postdata['infos']['shop_price'][$k];
				}else{
					$return['msg'] = '订货商品数量必须大于0！';
					$this->ajaxReturn($return);
				}
			}
		}
		
		$add_data['chu_id'] = $chu_id;
		$add_data['chu_type'] = $chu_type;
		$add_data['ru_type'] = 4;
		$add_data['ru_id'] = $ru_id;
		$add_data['order_sn'] = base_order();//生成订货单编号
		$add_data['order_sum'] = $goods_allnum;
		$add_data['goods_allnum'] = $goods_allnum;
		$add_data['goods_allmoney'] = $goods_allmoney;
		$add_data['addtime'] = NOW_TIME;
		$add_data['updatetime'] = 0;
		$add_data['shipping_time'] =0;
		$add_data['order_status'] = $order_status;
		$add_data['status'] = 0;
		$add_data['is_delete'] = 0;
	
		$library_id = M('LibraryInfo')->add($add_data);
		
		if($library_id){
			foreach($goods_list as $k=>$v){
				$add_goods = $v;
				$add_goods['libinfo_id'] = $library_id;
				$add_goods['supplier_id'] = idtoname($v['goods_id'],'Goods','suppliers_id','id',false);
				$add_g_id = M('LibraryGoods')->add($add_goods);
			}
			$return['status'] = 1;
			if($order_status == 1){
				$return['msg'] = '订货单生成成功';
				if($postdata['is_print'] == 1){
					$return['url'] = U('Shop/PurOrder/print_template',array('id'=>$library_id,'type'=>'4'));
				}
				
			}elseif($order_status == 4){
				$return['msg'] = '调拨单生成成功';
				if($postdata['is_print'] == 1){
					$return['url'] = U('Shop/PurOrder/print_template',array('id'=>$library_id,'type'=>'3'));
					
				}
			}
			
		}
		return $return;
	}
}
?>