<?php

namespace Factory\Lib;

use Dever;

class Order
{
    # 得到商品和总价
    private function goods()
    {
    	$goods = Dever::input('goods');
    	if (!$goods) {
            Dever::alert('请传入商品');
        }
        $goods = Dever::json_decode($goods);

        $this->data['price'] = 0;
        $this->data['p_price'] = 0;
        $this->data['num'] = 0;
        $this->data['name'] = array();
        $factory_id = Dever::input('factory_id');
        # 计算总价格
        foreach ($goods as $k => $v) {
        	if (strstr($v['id'], '-')) {
        		$t = explode('-', $v['id']);
        		$v['id'] = $t[0];
        		$s = $t[1];
        	} else {
        		$s = -1;
        	}
            $n = $v['num'];
            $goods_sku = Dever::db('factory/goods_sku')->find(array('factory_id' => $factory_id, 'goods_id' => $v['id'], 'sku_id' => $s));

            $this->data['list'][$k] = Dever::load('goods/lib/info')->getPayInfo($v['id'], $s);
            if (isset($this->data['list'][$k]['min']) && $this->data['list'][$k]['min'] > 0) {
                if ($n < $this->data['list'][$k]['min']) {
                    //$n = $this->data['list'][$k]['min'];
                }
            }
            $this->data['list'][$k]['num'] = $n;

            $this->data['list'][$k]['buy_num'] = $n;
            $this->data['num'] += $n;
            if ($goods_sku) {
                $p_price = $goods_sku['p_price'];
            } else {
                $p_price = 0;
            }
            $this->data['list'][$k]['p_price'] = $p_price;

            $price = $this->data['list'][$k]['price'];
            $this->data['price'] += $price * $n;
            $this->data['p_price'] += $p_price * $n;
            $this->data['name'][] = $this->data['list'][$k]['name'];
        }

        $this->data['name'] = implode(',', $this->data['name']);
    }

    # 下单
    public function action_commit_api()
    {
        $info = Dever::input('info');

        $this->goods();
        
        $order_data['type'] = 2;
        $order_data['type_id'] = Dever::input('store_id');
        $order_data['source_id'] = Dever::input('factory_id');
        $order_data['source_type'] = 3;
        $order_data['name'] = $this->data['name'];
        $order_data['num'] = $this->data['num'];
        $order_data['info'] = $info;
        $order_data['price'] = $this->data['price'];
        $order_data['p_price'] = $this->data['p_price'];
        $order_data['operdate'] = time();
        $order_data['status'] = 3;

        $order_data['order_num'] = $this->getOrderId();
        $order_data['refund_cash'] = 0;
        $order_data['refund_p_cash'] = 0;
        $id = Dever::db('shop/buy_order')->insert($order_data);

        if (!$id) {
            Dever::alert('下单失败');
        }

        $order_data['id'] = $id;

        foreach($this->data['list'] as $k => $v) {
            $data['order_id'] = $id;
            $data['goods_id'] = $v['id'];
            $data['sku_id'] = $v['sku_id'];
            $data['price'] = $v['price'];
            $data['p_price'] = $v['p_price'];
            $data['num'] = $v['buy_num'];

            $state = Dever::db('shop/buy_order_goods')->insert($data);
        }

        Dever::load('cash/lib/order')->up($order_data, 1, 1);

        return Dever::url('project/database/list&project=shop&table=buy_order&menu=shop&menu_id=77&search_option_state=1&search_option_type=2&search_option_source_type=3', 'manage');
    }

    # 生成订单号
    public function getOrderId()
    {
        $where['order_num'] = Dever::order('F');
        $state = Dever::db('shop/buy_order')->one($where);
        if (!$state) {
            return $where['order_num'];
        } else {
            return $this->getOrderId();
        }
    }
}