123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- namespace Store\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();
- $source_id = Dever::input('source_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'];
- $where['store_id'] = $source_id;
- $where['goods_id'] = $v['id'];
- $where['sku_id'] = $s;
- $goods_sku = Dever::db('store/goods_sku')->getOne($where);
- $this->data['list'][$k] = Dever::load('goods/lib/info')->getPayInfo($v['id'], $s, 2);
- 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'];
- }
- }
- if ($goods_sku && $goods_sku['total'] < $n) {
- $n = $goods_sku['total'];
- }
- $this->data['list'][$k]['num'] = $n;
- $this->data['list'][$k]['buy_num'] = $n;
- $this->data['num'] += $n;
- $price = $this->data['list'][$k]['price'];
- $this->data['price'] += $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('source_id');
- if ($order_data['type_id'] == $order_data['source_id']) {
- Dever::alert('发货仓库和收货仓库不能相同');
- }
- $order_data['source_type'] = 2;
- $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'] = 0;
- $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;
- $buy_order_goods = array();
- 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'] = 0;
- $data['num'] = $v['buy_num'];
- $state = Dever::db('shop/buy_order_goods')->insert($data);
- if ($state) {
- $buy_order_goods[$k] = $data;
- $buy_order_goods[$k]['id'] = $state;
- }
- }
- Dever::load('cash/lib/order')->up($order_data, 1, 1);
- # 减少发货库存
- if ($buy_order_goods) {
- Dever::load('store/lib/goods')->oper($order_data['source_id'], 1, 2, $buy_order_goods);
- }
-
- 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=2', 'manage');
- }
- # 生成订单号
- public function getOrderId()
- {
- $where['order_num'] = Dever::order('C');
- $state = Dever::db('shop/buy_order')->one($where);
- if (!$state) {
- return $where['order_num'];
- } else {
- return $this->getOrderId();
- }
- }
- }
|