123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725 |
- <?php
- namespace Mshop\Lib;
- use Dever;
- class Buy
- {
-
- public $type = 1;
-
- public $view = 1;
-
- public $config = array();
-
- public $table = 'shop/buy_order';
- public function __construct()
- {
- $this->config = Dever::db($this->table)->config;
- }
-
- public function set($type, $view)
- {
- $this->type = $type;
- $this->view = $view;
- return $this;
- }
-
- public function where($id)
- {
- $where = array();
- $where['shop_id'] = $id;
- if (!$where) {
- Dever::alert('参数错误');
- }
- return $where;
- }
-
- public function getList($id)
- {
- $result = array();
- $where = $this->where($id);
- $mobile = Dever::input('mobile');
- if ($mobile) {
- $where['mobile'] = $mobile;
- }
- $status = Dever::input('status');
- if ($status) {
- $where['status'] = $status;
- }
- $result['search_value'] = $where;
- $result['search_value']['day'] = $day = Dever::input('day');
- if ($day) {
- $where['start'] = Dever::maketime($day . ' 00:00:00');
- $where['end'] = Dever::maketime($day . ' 23:59:59');
- }
- $result['order'] = Dever::db($this->table)->getAll($where);
- if ($result['order']) {
- foreach ($result['order'] as $k => $v) {
- $result['order'][$k] = $this->getInfo($v);
- }
- }
- $result['search'] = array();
- $result['search']['status'] = array
- (
- array('value' => 1, 'name' => '待支付'),
- array('value' => 2, 'name' => '待审核'),
- array('value' => 3, 'name' => '待发货'),
- array('value' => 4, 'name' => '待收货确认'),
- array('value' => 5, 'name' => '已完成'),
- array('value' => 6, 'name' => '已完成(有退款)'),
- array('value' => '7,8,11', 'name' => '已取消'),
- );
- return $result;
- }
-
- public function getView($id, $order_id, $show = true)
- {
- $where = $this->where($id);
- $where['id'] = $order_id;
- $result = Dever::db($this->table)->find($where);
- if (!$result) {
- Dever::alert('订单不存在');
- }
- if ($show) {
- $result = $this->getInfo($result, true);
- }
- return $result;
- }
-
- public function getRefund($id, $order_id)
- {
- $data = $this->getView($id, $order_id, false);
- $data['refund'] = Dever::db('shop/buy_order_refund')->select(array('order_id' => $info['id']));
- return $data;
- }
-
- public function getInfo($info, $view = false)
- {
- if ($info['status'] == 1) {
-
- $m = 9000;
-
- $info['time'] = time() - $info['cdate'];
- if ($info['time'] >= $m) {
-
- $info['time'] = -1;
- Dever::db($this->table)->update(array('where_id' => $info['id'], 'status' => 11));
- $info['status'] = 11;
- } else {
- $info['time'] = $m - $info['time'];
- }
- }
- $info['status_name'] = $this->config['status'][$info['status']];
- $info['goods'] = Dever::db('shop/buy_order_goods')->select(array('order_id' => $info['id']));
- $info['shop'] = Dever::db('shop/info')->getOne($info['shop_id']);
- $info['cdate'] = date('Y-m-d H:i', $info['cdate']);
- $goods_status = Dever::db('shop/buy_order_goods')->config['status'];
- $goods_process = Dever::db('shop/buy_order_refund')->config['process'];
- if ($view) {
- foreach ($info['goods'] as $k => $v) {
- $info['goods'][$k]['info'] = Dever::load('goods/lib/info')->getPayInfo($v['goods_id'], $v['sku_id']);
- $info['goods'][$k]['status_name'] = $goods_status[$v['status']];
- }
- } else {
- foreach ($info['goods'] as $k => $v) {
- $goods = Dever::db('goods/info')->one($v['goods_id']);
- $info['goods'][$k]['name'] = $goods['name'];
- $info['goods'][$k]['cover'] = $goods['cover'];
- $info['goods'][$k]['status_name'] = $goods_status[$v['status']];
- }
- }
- if (!$info['refund_cash']) {
- $info['refund_cash'] = 0;
- }
- if ($info['fdate']) {
- $info['fdate'] = date('Y-m-d H:i', $info['fdate']);
- }
- if ($info['qfdate']) {
- $info['qfdate'] = date('Y-m-d H:i', $info['qfdate']);
- }
- if ($info['operdate']) {
- $info['operdate'] = date('Y-m-d H:i', $info['operdate']);
- }
- if ($info['shdate']) {
- $info['shdate'] = date('Y-m-d H:i', $info['shdate']);
- }
- return $info;
- }
-
- public function action($shop, $name, $num, $goods, $price, $info, $refer = '')
- {
- if (!$shop) {
- Dever::alert('错误的门店信息');
- }
-
- $order_data['shop_id'] = $shop['id'];
- $order_data['mobile'] = $shop['mobile'];
- $order_data['name'] = $name;
- $order_data['num'] = $num;
- $order_data['info'] = $info;
- $order_data['price'] = $price;
- $order_data['area'] = $shop['area'];
- $order_data['province'] = $shop['province'];
- $order_data['city'] = $shop['city'];
- $order_data['county'] = $shop['county'];
- $order_data['town'] = $shop['town'];
- $order_data['order_num'] = $this->getOrderId();
- $id = Dever::db('shop/buy_order')->insert($order_data);
- if (!$id) {
- Dever::alert('支付失败');
- }
- foreach($goods as $k => $v) {
- $data['shop_id'] = $shop['id'];
- $data['order_id'] = $id;
- $data['goods_id'] = $v['id'];
- $data['sku_id'] = $v['sku_id'];
- $data['price'] = $v['price'] * $v['buy_num'];
- $data['num'] = $v['buy_num'];
- Dever::db('shop/buy_order_goods')->insert($data);
- }
- if ($price <= 0) {
- $param['pay_product_id'] = $id;
- $param['pay_order_id'] = $order_data['order_num'];
- $param['pay_status'] = 2;
- $param['pay_cash'] = $price;
- $param['pay_msg'] = '';
- $this->success($param);
- return array
- (
- 'order' => '',
- 'order_num' => $order_data['order_num'],
- 'order_id' => $id,
- 'type' => 'pay',
- );
- }
- $param = array
- (
- 'project_id' => 2,
- 'channel_id' => 1,
- 'system_source' => 5,
- 'uid' => -1,
- 'name' => $order_data['name'],
- 'cash' => $price,
- 'product_id' => $id,
- 'order_id' => $order_data['order_num'],
- 'refer' => $refer,
- );
- $receipt = Dever::input('receipt');
- if ($receipt) {
- $param['receipt'] = $receipt;
- }
-
- $result['type'] = 'test';
- $result['order'] = '';
- $result['order_num'] = $order_data['order_num'];
- $result['order_id'] = $id;
- return $result;
- }
-
- public function raction($id, $refer = '')
- {
- $order = Dever::db('shop/buy_order')->find($id);
- if (!$order) {
- Dever::alert('订单信息错误');
- }
- if ($order['status'] > 1) {
- Dever::alert('订单已支付或已取消');
- }
- $param = array
- (
- 'project_id' => 2,
- 'channel_id' => 1,
- 'system_source' => 5,
- 'uid' => -1,
- 'name' => $order['name'],
- 'cash' => $order['price'],
- 'product_id' => $id,
- 'order_id' => $order['order_num'],
- 'refer' => $refer,
- );
- $receipt = Dever::input('receipt');
- if ($receipt) {
- $param['receipt'] = $receipt;
- }
- $result = Dever::load('pay/api.pay', $param);
- $result['order_num'] = $order['order_num'];
- $result['order_id'] = $id;
- return $result;
- }
-
- public function success_secure_api_token()
- {
- $project_id = Dever::input('pay_project_id');
- $info = Dever::db('pay/project')->one($project_id);
- if ($info) {
- return $info['key'];
- }
- return 'shop_buy_dever_2020';
- }
-
- public function success_secure_api($param = array())
- {
- $this->success($param);
- }
-
- public function success($param = array())
- {
- $send = $param ? $param : Dever::preInput('pay_');
- $product_id = $send['pay_product_id'];
- $order_id = $send['pay_order_id'];
- $status = $send['pay_status'];
- $cash = $send['pay_cash'];
- $msg = $send['pay_msg'];
- $order = Dever::db('shop/buy_order')->one(array('id' => $product_id, 'time' => time()));
- if ($order && $order['status'] == 1) {
- if ($status == 2) {
-
- }
- $update['status'] = 2;
- $update['where_id'] = $order['id'];
- Dever::db('shop/buy_order')->update($update);
- }
- return 'ok';
- }
-
- 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();
- }
- }
-
- public function finish($id, $order_id)
- {
- $data = $this->getView($id, $order_id, false);
- if ($data['status'] <= 4) {
- if ($data['refund_cash'] > 0) {
- $status = 6;
- } else {
- $status = 5;
- }
- $state = Dever::db('shop/buy_order')->update(array('where_id' => $data['id'], 'status' => $status, 'qsdate' => time()));
- if ($state) {
-
- $where['order_id'] = $data['id'];
- $where['status'] = 1;
- $data = Dever::db('shop/buy_order_goods')->select($where);
- if ($data) {
- Dever::load('shop/lib/goods')->oper(1, 1, $data);
- }
- }
- return 'ok';
- } else {
- Dever::alert('您没有权限');
- }
- }
-
- public function cancel($id, $order_id)
- {
- $data = $this->getView($id, $order_id, false);
- if ($data['status'] == 1) {
- $state = Dever::db('shop/buy_order')->update(array('where_id' => $data['id'], 'status' => 7, 'operdate' => time()));
- return 'ok';
- } else {
- Dever::alert('当前订单状态不允许取消');
- }
- }
-
- public function send_api()
- {
- $id = Dever::input('id');
- $process = Dever::input('process', 3);
- $info = Dever::db('shop/buy_order_refund')->find($id);
- if ($info && $info['process'] == 1) {
- $update['process'] = $process;
- $update['where_id'] = $info['id'];
- $state = Dever::db('shop/buy_order_refund')->update($update);
- if ($state && $process == 2) {
- $data = $this->getView($info['shop_id'], $info['order_id'], false);
- if ($data) {
- if ($info['order_goods_id'] > 0) {
- $state = Dever::db('shop/buy_order_goods')->update(array('where_id' => $info['order_goods_id'], 'status' => 3));
- $this->pay_refund($state, $data, $info['cash']);
-
- $total = Dever::db('shop/buy_order_goods')->total(array('order_id' => $info['order_id'], 'shop_id' => $id, 'status' => 1));
- if ($total <= 0) {
- $state = Dever::db('shop/buy_order')->update(array('where_id' => $info['order_id'], 'status' => 8));
- }
- } else {
-
- $state = Dever::db('shop/buy_order')->update(array('where_id' => $info['order_id'], 'status' => 8));
- $this->pay_refund($state, $data, $info['cash']);
- }
- }
- }
- }
- return 'ok';
- }
-
- public function orderStatus($id)
- {
- $config = Dever::db('shop/buy_order')->config;
- $info = Dever::db('shop/buy_order')->one($id);
- $tk = Dever::db('shop/buy_order_refund')->find(array('order_id' => $info['id'], 'process' => 1));
- $status = $config['status'][$info['status']];
- if ($tk) {
- $status = '申请退款中';
- }
- return $status;
- }
-
- public function refund_api()
- {
- $id = Dever::input('id');
- $process = Dever::input('process');
- return Dever::load('shop/lib/refund')->set('buy')->action($id, $process, false);
- }
-
- public function show()
- {
- $id = Dever::input('order_id');
- $config = Dever::db('shop/buy_order')->config;
- $info = Dever::db('shop/buy_order')->one($id);
- $shop = Dever::db('shop/info')->find($info['shop_id']);
- $tk_status = Dever::db('shop/buy_order_refund')->config['status'];
- $tk_process = Dever::db('shop/buy_order_refund')->config['process'];
- $status = $config['status'][$info['status']];
- $tk = Dever::db('shop/buy_order_refund')->find(array('order_id' => $info['id'], 'type' => 1));
-
- $html = '[基本信息]:<table class="layui-table"><thead><tr><th style="width:20%">项目</th><th style="width:80%">详情</th></tr> </thead><tbody>';
- $html .= '<tr>
- <td>订单号</td>
- <td>'.$this->table(false, array(array($info['order_num']))).'</td>
- </tr>';
- $html .= '<tr>
- <td>门店信息</td>
- <td>'.$this->table(false, array(array('门店名称:' . $shop['name'], '联系人:' . $shop['truename'], '联系电话:' . $shop['mobile']))).'</td>
- </tr>';
- $url = Dever::url('lib/buy.refund', 'mshop');
- if ($tk && $tk['process'] == 1) {
- $status = '申请' . $tk_status[$tk['status']];
- $process = $tk_process[$tk['process']];
- $process = '';
- if ($tk['process'] == 1) {
- $process .= ' <a href="javascript:;" onclick="audit('.$tk['id'].', 2, \''.$url.'\')" class="layui-btn">通过</a><a href="javascript:;" onclick="audit('.$tk['id'].', 3, \''.$url.'\')" class="layui-btn layui-btn-danger">驳回</a>';
- }
- $html .= '<tr>
- <td>订单信息</td>
- <td>'.$this->table(array('金额', '退款金额', '数量', '订单状态', '审核状态'), array(array($info['price'], $info['refund_cash'], $info['num'], $status, $process))).'</td>
- </tr>';
- } else {
- $html .= '<tr>
- <td>订单信息</td>
- <td>'.$this->table(array('金额', '退款金额', '数量', '订单状态'), array(array($info['price'], $info['refund_cash'], $info['num'], $status))).'</td>
- </tr>';
- }
-
-
- $html .= '</tbody></table>';
- $html .= '[商品清单]:';
- $head = array('名称', '属性', '价格', '数量', '状态');
- $body = array();
- $goods = Dever::db('shop/buy_order_goods')->select(array('order_id' => $info['id']));
- $goods_status = Dever::db('shop/buy_order_goods')->config['status'];
- foreach ($goods as $k => $v) {
- $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
- $status = $goods_status[$v['status']];
- $tk = Dever::db('shop/buy_order_refund')->find(array('order_id' => $info['id'], 'order_goods_id' => $v['id'], 'type' => 2));
- if ($tk && $tk['process'] == 1) {
- $status = '申请' . $tk_status[$tk['status']];
-
- if ($tk['process'] == 1) {
- $status .= ' <a href="javascript:;" onclick="audit('.$tk['id'].', 2, \''.$url.'\')" class="layui-btn">通过</a><a href="javascript:;" onclick="audit('.$tk['id'].', 3, \''.$url.'\')" class="layui-btn layui-btn-danger">驳回</a>';
- }
- }
- $body[$k] = array
- (
- $goods_info['name'],
- $v['price'],
- $v['price'],
- $v['num'],
- $status,
- );
- }
- $html .= $this->table($head, $body);
- return $html;
- }
- private function table($head, $data)
- {
- $html = '';
- if ($head) {
- $html = '<table class="layui-table">';
- $html .= '<thead><tr>';
- foreach ($head as $k => $v) {
- $html .= '<th>'.$v.'</th>';
- }
- $html .= '</tr></thead>';
- $html .= '<tbody>';
- foreach ($data as $k => $v) {
- $html .= '<tr>';
- foreach ($v as $k1 => $v1) {
- $html .= '<td>'.$v1.'</td>';
- }
- $html .= '</tr>';
- }
- $html .= '</tbody>';
- $html .= '</table>';
- } else {
- foreach ($data as $k => $v) {
- $html .= '';
- foreach ($v as $k1 => $v1) {
- $html .= $v1 . ' ';
- }
- $html .= '';
- }
- }
- return $html;
- }
-
- public function audit()
- {
- $order_id = Dever::input('order_id');
- $info = Dever::db('shop/buy_order')->find($order_id);
- if (!$info || ($info && $info['status'] != 2)) {
- Dever::alert('当前订单状态无法拆单');
- }
- $goods = Dever::db('shop/buy_order_goods')->select(array('order_id' => $order_id));
- $shop = Dever::db('shop/info')->find($info['shop_id']);
-
- $shop_store = Dever::db('shop/store')->select(array('shop_id' => $shop['id']));
- $store = array();
- $distance = array();
-
- foreach ($shop_store as $k => $v) {
- $info = Dever::db('store/info')->find($v['store_id']);
- if ($info && $info['status'] == 1) {
- $distance[] = $info['distance'] = Dever::distance($shop['lng'], $shop['lat'], $info['lng'], $info['lat']);
- $store[] = $info;
- }
- }
- array_multisort($distance, SORT_ASC, SORT_NUMERIC, $store);
- $shop_factory = Dever::db('shop/factory')->select(array('shop_id' => $shop['id']));
- $factory = array();
- $distance = array();
-
- foreach ($shop_factory as $k => $v) {
- $info = Dever::db('factory/info')->find($v['factory_id']);
- if ($info && $info['status'] == 1) {
- $distance[] = $info['distance'] = Dever::distance($shop['lng'], $shop['lat'], $info['lng'], $info['lat']);
- $factory[] = $info;
- }
- }
- array_multisort($distance, SORT_ASC, SORT_NUMERIC, $factory);
- print_r($store);
- print_r($factory);die;
- $num = 0;
- foreach ($goods as $k => $v) {
-
- $find = $this->store($order_id, $shop, $store, $v);
- if (!$find[1]) {
-
- if ($factory) {
- $find = $this->factory($order_id, $shop, $factory, $v);
- }
-
- if (!$find[1]) {
-
- }
- }
- if ($find[1]) {
- $num++;
- Dever::db('shop/buy_order_goods')->update(array('where_id' => $v['id'], 'status' => 2, 'type' => $find[0], 'type_id' => implode(',', $find[1])));
- }
- }
- if ($num > 0) {
- Dever::db('shop/buy_order')->update(array('where_id' => $info['id'], 'status' => 3));
- } else {
- Dever::db('shop/buy_order')->update(array('where_id' => $info['id'], 'status' => 7));
- }
- }
- private function store($order_id, $shop, $store, $goods, $state = 2)
- {
- $find = array();
- $id = array();
- if (!$store) {
- return array(1, $id);
- }
- foreach ($store as $k => $v) {
- $where['store_id'] = $v['id'];
- $where['goods_id'] = $goods['goods_id'];
- $where['sku_id'] = ($goods['sku_id'] && $goods['sku_id'] > 0) ? $goods['sku_id'] : -1;
- $sku = Dever::db('store/goods_sku')->getOne($where);
- if ($sku) {
- if ($goods['num'] <= $sku['total']) {
-
- $state = Dever::load('store/lib/order')->pay($order_id, $shop['id'], $where['store_id'], $goods['price'], $goods['num'], $where['goods_id'], $where['sku_id']);
- if ($state) {
- $id[] = $where['store_id'];
- break;
- }
- } elseif ($state == 1 && $sku['total'] > 0) {
-
- $goods['num'] = $goods['num'] - $sku['total'];
-
- $state = Dever::load('store/lib/order')->pay($order_id, $shop['id'], $where['store_id'], $goods['price'], $sku['total'], $where['goods_id'], $where['sku_id']);
- if (!$state) {
- $goods['num'] = $goods['num'] + $sku['total'];
- } else {
- $id[] = $where['store_id'];
- }
- continue;
- } else {
-
- continue;
- }
- }
- }
- return array(1, $id);
- }
- private function factory($order_id, $shop, $factory, $goods)
- {
- $find = array();
- $id = array();
- if (!$factory) {
- return array(1, $id);
- }
- foreach ($factory as $k => $v) {
- $where['factory_id'] = $v['id'];
- $where['goods_id'] = $goods['goods_id'];
- $where['sku_id'] = ($goods['sku_id'] && $goods['sku_id'] > 0) ? $goods['sku_id'] : -1;
- $sku = Dever::db('factory/goods_sku')->getOne($where);
- if ($sku) {
- $state = Dever::load('factory/lib/order')->pay($order_id, $shop['id'], $where['factory_id'], $goods['price'], $goods['num'], $where['goods_id'], $where['sku_id']);
- if ($state) {
- $id[] = $where['factory_id'];
- break;
- }
- } else {
- continue;
- }
- }
- return array(2, $id);
- }
- }
|