12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532 |
- <?php
- # 销售支付
- namespace Shop\Lib;
- use Dever;
- class Sell
- {
- # 1是用户,2是门店
- public $type = 1;
- # 1是列表,2是详情
- public $view = 1;
- # 获取配置
- public $config = array();
- # table
- public $table = 'shop/sell_order';
- public function __construct()
- {
- $this->config = Dever::db($this->table)->config;
- }
- # 设置订单的类型
- public function set($type, $view)
- {
- $this->type = $type;
- $this->view = $view;
- return $this;
- }
- # 获取公共的where
- public function where($id)
- {
- $where = array();
- if ($this->type == 1) {
- $where['uid'] = $id;
- } elseif ($this->type == 2) {
- $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;
- }
- $method = Dever::input('method');
- if ($method) {
- $where['method'] = $method;
- }
- $pay_method = Dever::input('pay_method');
- if ($pay_method) {
- $where['pay_method'] = $pay_method;
- }
- $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' => '待处理'),
- );
- if ($method == 1) {
- $result['search']['status'][] = array('value' => 3, 'name' => '待自提');
- } elseif ($method == 2) {
- $result['search']['status'][] = array('value' => 3, 'name' => '配送中');
- } else {
- $result['search']['status'][] = array('value' => 3, 'name' => '待自提');
- $result['search']['status'][] = array('value' => 3, 'name' => '配送中');
- }
- $result['search']['status'][] = array('value' => 8, 'name' => '退款');
- $result['search']['status'][] = array('value' => '5,6', 'name' => '已完成');
- $result['search']['status'][] = array('value' => '7,11', 'name' => '已取消');
- $result['search']['method'] = array
- (
- array('value' => 1, 'name' => '自提'),
- array('value' => 2, 'name' => '配送')
- );
- $result['search']['pay_method'] = array
- (
- array('value' => 1, 'name' => '平台结算'),
- array('value' => 2, '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);
- }
- return $result;
- }
- # 获取订单详细信息
- public function getInfo($info)
- {
- $info['time'] = 0;
- if ($info['status'] == 1) {
- # 15分钟内支付,900秒
- $m = 900;
- # 支付倒计时
- $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['method_name'] = $this->config['method'][$info['method']];
- $info['pay_method_name'] = $this->config['pay_method'][$info['pay_method']];
- $info['pay_type_name'] = $this->config['pay_type'][$info['pay_type']];
- if (!$info['coupon_cash']) {
- $info['coupon_cash'] = 0;
- }
- if (!$info['refund_cash']) {
- $info['refund_cash'] = 0;
- }
- if (!$info['ps_cash']) {
- $info['ps_cash'] = 0;
- }
- if (!$info['ps_cash']) {
- $info['ps_cash'] = 0;
- }
- if (!$info['wallet_cash']) {
- $info['wallet_cash'] = 0;
- } else {
- $info['wallet_cash'] = round($info['wallet_cash'], 2);
- }
- if (!$info['card_code_cash']) {
- $info['card_code_cash'] = 0;
- } else {
- $info['card_code_cash'] = round($info['card_code_cash'], 2);
- }
- $info['card_name'] = '';
- if ($info['card_code_id'] > 0) {
- $config = Dever::db('main/sell_config')->find();
- $info['card_name'] = $config['card_name'];
- $card_code = Dever::db('card/code')->find($info['card_code_id']);
- if ($card_code) {
- $card_info = Dever::db('card/info')->find($card_code['card_id']);
- if ($card_info) {
- $card_type = Dever::db('card/type')->find($card_info['type_id']);
- $card = substr($card_code['card'], -4);
- $info['card_name'] .= '('.$card_type['name'].$card.')';
- }
- }
- }
-
- if ($this->type == 1) {
- if ($info['status'] == 2) {
- $info['status_name'] = '支付成功';
- }
- if ($info['status'] == 3) {
- if ($info['method'] == 1) {
- $info['status_name'] = '待自提';
- } else {
- $info['status_name'] = '配送中';
- }
- }
- } elseif ($this->type == 2) {
- if ($info['status'] == 2) {
- $info['status_name'] = '待处理';
- }
- if ($info['status'] == 3) {
- if ($info['method'] == 1) {
- $info['status_name'] = '待自提';
- } else {
- $info['status_name'] = '配送中';
- }
- }
- }
- if ($info['method'] == 2) {
- $info['ps_info'] = Dever::db('shop/sell_order_ps')->find(array('order_id' => $info['id']));
- if ($info['ps_info']) {
- $info['ps_info']['service_name'] = '商家自送';
- $info['ps_info']['cdate'] = date('Y-m-d H:i', $info['ps_info']['cdate']);
- if ($info['ps_info']['qu_date']) {
- $info['ps_info']['qu_date'] = date('Y-m-d H:i', $info['ps_info']['qu_date']);
- }
- if ($info['ps_info']['qs_date']) {
- $info['ps_info']['qs_date'] = date('Y-m-d H:i', $info['ps_info']['qs_date']);
- }
- $status = Dever::db('shop/sell_order_ps')->config['status'];
- $info['ps_info']['status_name'] = $status[$info['ps_info']['status']];
-
- if ($info['ps_info']['service_id'] > 0) {
- $service = Dever::db('shop/service')->find($info['ps_info']['service_id']);
- $info['ps_info']['service_name'] = $service['name'];
- }
- }
- }
- $info['goods'] = Dever::db('shop/sell_order_goods')->select(array('order_id' => $info['id']));
- if ($info['uid'] && $info['uid'] > 0) {
- $info['user'] = Dever::load('passport/api')->info($info['uid']);
- }
-
- $info['shop'] = Dever::db('shop/info')->getOne($info['shop_id']);
- $info['cdate'] = date('Y-m-d H:i', $info['cdate']);
- if ($info['operdate']) {
- $info['operdate'] = date('Y-m-d H:i', $info['operdate']);
- } else {
- $info['operdate'] = '';
- }
- if ($info['fdate']) {
- $info['fdate'] = date('Y-m-d H:i', $info['fdate']);
- } else {
- $info['fdate'] = '';
- }
- $goods_status = Dever::db('shop/sell_order_goods')->config['status'];
- if ($info['address_id']) {
- $info['address'] = Dever::db('passport/address')->find($info['address_id']);
- }
- if ($this->view == 2) {
- 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']];
- }
- if ($info['invoice_id']) {
- $info['invoice'] = Dever::db('passport/invoice')->find($info['invoice_id']);
- }
- if ($info['user_coupon_id']) {
- $coupon = Dever::db('shop/user_coupon')->find($info['user_coupon_id']);
- if ($coupon) {
- $info['coupon'] = Dever::db('goods/coupon')->find($coupon['coupon_id']);
- }
- }
- } 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']];
- }
- }
- return $info;
- }
- # 取消订单
- public function cancel($id, $order_id)
- {
- $data = $this->getView($id, $order_id, false);
- if ($data['status'] == 1) {
- $state = Dever::db('shop/sell_order')->update(array('where_id' => $data['id'], 'status' => 7, 'operdate' => time()));
- if ($state) {
- if ($data['user_coupon_id']) {
- # 还原优惠券
- Dever::db('shop/user_coupon')->update(array('where_id' => $data['user_coupon_id'], 'status' => 1));
- }
- if (isset($data['card_code_id']) && $data['card_code_id'] && $data['card_code_cash'] > 0) {
- # 还原礼品卡
- Dever::db('card/code')->decUseCash(array('where_id' => $data['card_code_id'], 'set_use_cash' => $data['card_code_cash']));
- } elseif (isset($data['wallet_cash']) && $data['wallet_cash'] && $data['wallet_cash'] > 0) {
- # 还原钱包
-
- }
- $this->updateSell($data, 2);
- }
- return 'ok';
- } else {
- Dever::alert('当前订单状态不允许取消');
- }
- }
- # 获取优惠券
- public function coupon(&$data, $type = 1)
- {
- $user_coupon_id = Dever::input('user_coupon_id');
- $shop_coupon_id = Dever::input('shop_coupon_id');
- $data['user_coupon_id'] = 0;
- $data['coupon_id'] = 0;
- $data['coupon_cash'] = 0;
- if ($data['pay_method'] == 3) {
- return $data;
- }
- if ($type == 1) {
- if (isset($data['uid']) && $data['uid'] > 0) {
- # 默认选择优惠券
- # 查找符合要求的优惠券
- $coupon = Dever::db('shop/user_coupon')->getAll(array('uid' => $data['uid'], 'city' => $data['shop']['city'], 'status' => 1, 'edate' => time()));
- if ($coupon) {
- foreach ($coupon as $k => $v) {
- $coupon_info = Dever::db('goods/coupon')->find($v['coupon_id']);
- if ($coupon_info) {
- if ($v['shop_id'] != $data['shop']['id'] && $coupon_info['method'] == 3) {
- # 门店专属券 不能给别的门店用
- continue;
- }
- $kou = false;
- if ($coupon_info['type'] == 1) {
- # 满减券
- if ($data['price'] >= $coupon_info['total_cash']) {
- $kou = true;
- }
- } else {
- $kou = true;
- }
- if ($kou) {
- $coupon_info['user_coupon_id'] = $v['id'];
- $coupon_info['uid'] = $v['uid'];
- $coupon_info['edate'] = date('Y-m-d', $v['edate']);
- $coupon_info['shop_id'] = $v['shop_id'];
- $coupon_info['coupon_id'] = $v['coupon_id'];
- $coupon_info['city'] = $v['city'];
- $coupon_info = Dever::load('shop/lib/coupon')->getShop($coupon_info, $data['shop'], false);
- $data['coupon'][] = $coupon_info;
- if (!$user_coupon_id && $data['coupon_cash'] <= $coupon_info['cash']) {
- $data['user_coupon_id'] = $v['id'];
- $data['coupon_id'] = $coupon_info['id'];
- $data['coupon_cash'] = $coupon_info['cash'];
- }
- }
- }
- }
- }
- } else {
- $coupon = Dever::db('shop/coupon')->getAll(array('shop_id' => $data['shop']['id']));
- if ($coupon) {
- foreach ($coupon as $k => $v) {
- $coupon_info = Dever::db('goods/coupon')->find($v['coupon_id']);
- $kou = false;
- if ($coupon_info['type'] == 1) {
- # 满减券
- if ($data['price'] >= $coupon_info['total_cash']) {
- $kou = true;
- }
- } else {
- $kou = true;
- }
- if ($kou) {
- $coupon_info['shop_id'] = $v['shop_id'];
- $coupon_info['shop_coupon_id'] = $v['id'];
- $coupon_info['coupon_id'] = $v['coupon_id'];
- $coupon_info['city'] = $v['city'];
- $coupon_info = Dever::load('shop/lib/coupon')->getShop($coupon_info, $data['shop'], false);
- $data['coupon'][] = $coupon_info;
- }
- }
- }
- }
- }
- if ($user_coupon_id && isset($data['uid']) && $data['uid'] > 0) {
- # 用户自己使用优惠券
- if ($data['price'] <= 0) {
- Dever::alert('付款价格错误');
- }
- $coupon = Dever::db('shop/user_coupon')->find(array('uid' => $data['uid'], 'id' => $user_coupon_id, 'status' => 1));
- if (!$coupon) {
- Dever::alert('优惠券不可用');
- }
- if (time() > $coupon['edate']) {
- Dever::db('shop/user_coupon')->update(array('where_id' => $user_coupon_id, 'status' => 3));
- Dever::alert('优惠券已过期');
- }
- $goods_coupon = Dever::db('goods/coupon')->find($coupon['coupon_id']);
- if (!$goods_coupon) {
- Dever::alert('优惠券不可用');
- }
- if ($coupon['shop_id'] != $data['shop']['id']) {
- if ($goods_coupon['method'] == 3) {
- Dever::alert('优惠券不可用');
- } else {
- $coupon_info = Dever::db('shop/coupon')->find(array('shop_id' => $coupon['shop_id'], 'coupon_id' => $coupon['coupon_id'], 'city' => $coupon['city']));
- if (!$coupon_info) {
- Dever::alert('优惠券不可用');
- }
- }
- }
- if ($goods_coupon['type'] == 2 && $data['price'] < $goods_coupon['total_cash']) {
- Dever::alert('优惠券不可用');
- }
- $data['user_coupon_id'] = $user_coupon_id;
- $data['coupon_id'] = $goods_coupon['id'];
- $data['coupon_cash'] = $goods_coupon['cash'];
- } elseif ($shop_coupon_id) {
- # 店里直接给用优惠券
- if ($data['price'] <= 0) {
- Dever::alert('付款价格错误');
- }
- $coupon_info = Dever::db('shop/coupon')->find(array('shop_id' => $data['shop']['id'], 'id' => $shop_coupon_id));
- if (!$coupon_info) {
- Dever::alert('优惠券不可用');
- }
- $goods_coupon = Dever::db('goods/coupon')->find($coupon_info['coupon_id']);
- if (!$goods_coupon) {
- Dever::alert('优惠券不可用');
- }
- if ($goods_coupon['type'] == 2 && $data['price'] < $goods_coupon['total_cash']) {
- Dever::alert('优惠券不可用');
- }
- $data['user_coupon_id'] = -1;
- $data['coupon_id'] = $goods_coupon['id'];
- $data['coupon_cash'] = $goods_coupon['cash'];
- }
- if (isset($data['coupon_cash']) && $data['coupon_cash'] > 0) {
- # 这里还要计算每个商品的优惠金额
- if (isset($data['list']) && $data['list']) {
- foreach ($data['list'] as $k => $v) {
- $data['list'][$k]['coupon_id'] = $data['coupon_id'];
- $data['list'][$k]['user_coupon_id'] = $data['user_coupon_id'];
- $data['list'][$k]['coupon_cash'] = round(($v['price'] * $v['buy_num'])/$data['price'], 2) * $data['coupon_cash'];
- }
- }
- $data['price'] -= $data['coupon_cash'];
- }
- # 计算礼品卡
- $card = Dever::input('card');
- $wallet = Dever::input('wallet');
- if ($data['price'] > 0 && isset($data['uid']) && $data['uid'] > 0) {
- if ($card) {
- # 验证这个礼品卡属于自己
- $check = Dever::db('card/code')->find($card);
- if ($check['dh_uid'] == $data['uid']) {
- if (!$check['total_cash']) {
- $card_info = Dever::db('card/info')->find($check['card_id']);
- $check['total_cash'] = $card_info['value'];
- }
- if (!$check['use_cash']) {
- $check['use_cash'] = 0;
- }
- $card_cash = $check['total_cash'] - $check['use_cash'];
- if ($card_cash > 0) {
- $data['card'] = array();
- $data['card']['id'] = $check['id'];
- $data['card']['card'] = $check['card'];
- if ($card_cash >= $data['price']) {
- $data['card']['cash'] = $data['price'];
- $data['price'] = 0;
- } else {
- $data['card']['cash'] = $card_cash;
- $data['price'] -= $data['card']['cash'];
- }
- $data['card']['cash'] = round($data['card']['cash'], 2);
- }
- }
- } elseif ($wallet && $wallet > 0) {
- $data['score'] = Dever::load('score/lib/core')->getUserScore($data['uid']);
- }
- }
- if ($data['price'] < 0) {
- $data['price'] = 0;
- }
- $data['price'] = round($data['price'], 2);
- return $data;
- }
- # 获取礼品卡 - 更改商品卡 - 废弃
- public function card($uid, $shop)
- {
- $card = Dever::input('card');
- $pwd = Dever::input('pwd');
- if (!$card) {
- Dever::alert('卡号/密码错误');
- }
- if (!$pwd) {
- Dever::alert('卡号/密码错误');
- }
- $data = Dever::db('goods/card_code')->find(array('card' => $card, 'pwd' => $pwd));
- if (!$data) {
- Dever::alert('卡号/密码错误');
- }
- $info = Dever::db('goods/card')->find($data['card_id']);
- if (!$info || ($info && $info['status'] != 1)) {
- Dever::alert('卡号/密码错误');
- }
- if ($data['uid'] && $data['uid'] > 0) {
- if ($data['uid'] != $uid) {
- Dever::alert('礼品卡已被领取');
- }
- if (time() > $data['edate']) {
- # 更新礼品卡状态
- $card_update['where_id'] = $data['id'];
- $card_update['status'] = 5;
- Dever::db('goods/card_code')->update($card_update);
- Dever::alert('礼品卡已失效');
- }
- } else {
- if (time() > $data['edate']) {
- # 更新礼品卡状态
- $card_update['where_id'] = $data['id'];
- $card_update['uid'] = $uid;
- $card_update['status'] = 5;
- if ($shop) {
- $card_update['shop_id'] = $shop['id'];
- }
- Dever::db('goods/card_code')->update($card_update);
- Dever::alert('礼品卡已失效');
- } else {
- # 更新礼品卡状态
- $card_update['where_id'] = $data['id'];
- $card_update['status'] = 2;
- $card_update['uid'] = $uid;
- $card_update['ddate'] = time();
- if ($shop) {
- $card_update['shop_id'] = $shop['id'];
- }
- Dever::db('goods/card_code')->update($card_update);
- }
- }
- $data = Dever::db('goods/card_code')->find($data['id']);
- $data['goods'] = $info['goods'];
- return $data;
- }
- # 得到商品和总价
- public function goods(&$data)
- {
- # 1自提,2配送
- $data['method'] = Dever::input('method', 1);
- $data['pay_method'] = Dever::input('pay_method');
- if ($data['pay_method'] == 3) {
- $data['card'] = $this->card($data['uid'], $data['shop']);
- $goods = Dever::array_decode($data['card']['goods']);
- $goods_id = array();
- $num = array();
- $sku_id = array();
- foreach ($goods as $k => $v) {
- $goods_id[] = $v['goods_id'];
- $num[] = $v['num'];
- $sku_id[] = -1;
- }
- } else {
- $goods_id = Dever::input('goods_id');
- if (!$goods_id) {
- Dever::alert('请传入商品');
- }
- $goods_id = explode(',', $goods_id);
- $sku_id = Dever::input('price_id');
- if ($sku_id) {
- $sku_id = explode(',', $sku_id);
- }
- $single_id = Dever::input('single_id');
- if ($single_id) {
- $single_id = explode(',', $single_id);
- }
- $input_id = Dever::input('input_id');
- if ($input_id) {
- $input_id = explode(',', $input_id);
- }
- $input_value = Dever::input('input_value');
- if ($input_value) {
- $input_value = explode(',', $input_value);
- }
-
- $num = Dever::input('num');
- if (!$num) {
- Dever::alert('请传入商品数量');
- }
- $num = explode(',', $num);
- }
- # 钱包金额
- $data['wallet_cash'] = 0;
- # 配送金额
- $data['ps_cash'] = 0;
- # 原价
- $data['oprice'] = 0;
- # 现价
- $data['price'] = 0;
- $data['num'] = 0;
- $data['name'] = array();
- $count = count($goods_id);
- # 计算总价格
- foreach ($goods_id as $k => $v) {
- $s = isset($sku_id[$k]) ? $sku_id[$k] : -1;
- $n = isset($num[$k]) ? $num[$k] : 1;
- $data['list'][$k] = Dever::load('goods/lib/info')->getPayInfo($v, $s);
- $w = array();
- $w['goods_id'] = $v;
- $w['sku_id'] = $s;
- $w['shop_id'] = $data['shop']['id'];
- $goods_sku = Dever::db('shop/goods_sku')->find($w);
- if ($goods_sku && isset($goods_sku['min'])) {
- $data['list'][$k]['min'] = $goods_sku['min'];
- }
- $data['list'][$k]['num'] = $n;
- # 2是库存不足
- $data['list'][$k]['ku_state'] = 1;
- # 验证是否有货
- $total = Dever::load('shop/lib/info')->checkTotal($n, $v, $data['shop']['id'], $s);
- if (isset($data['list'][$k]['min']) && $data['list'][$k]['min'] > 0) {
- if ($total < $data['list'][$k]['min']) {
- $total = 0;
- }
- }
- if ($total <= 0) {
- $data['list'][$k]['ku_state'] = 2;
- $data['list'][$k]['buy_num'] = 0;
- }
- if ($data['list'][$k]['ku_state'] == 1) {
- $data['list'][$k]['buy_num'] = $n;
- $data['num'] += $n;
- $data['price'] += $data['list'][$k]['price'] * $n;
- $data['name'][] = $data['list'][$k]['name'];
- }
- }
- $data['name'] = implode(',', $data['name']);
- if ($data['method'] == 2) {
- $data['ps_cash'] = $data['shop']['ps_cash'];
- if ($data['shop']['free_ps_cash'] && $data['shop']['free_ps_cash'] > 0 && $data['price'] >= $data['shop']['free_ps_cash']) {
- $data['ps_cash'] = 0;
- }
- }
- $data['oprice'] = $data['price'];
- return $data;
- }
- # 发起支付
- public function pay($method, $pay_method, $pay_type, $user, $shop, $name, $num, $goods, $oprice, $price, $cash, $address_id, $invoice_id, $info, $card, $wallet_cash, $coupon_id, $user_coupon_id, $coupon_cash, $cart = false, $system_source = 5, $refer = '')
- {
- if (!$user) {
- Dever::alert('错误的用户信息');
- }
- $uid = $user['id'];
- if ($uid > 0) {
- $wechat = Dever::db('passport/wechat')->one(array('uid' => $uid, 'type' => 1, 'system_id' => 1));
- if (!$wechat) {
- Dever::alert('错误的用户信息');
- }
- }
- if ($method == 2) {
- $order_data['ps_cash'] = $shop['ps_cash'];
- $price += $order_data['ps_cash'];
- }
-
- $order_data['shop_id'] = $shop['id'];
- if ($shop['type'] == 10) {
- $order_data['shop_type'] = 2;
- } else {
- $order_data['shop_type'] = 1;
- }
-
- $order_data['area'] = $shop['area'] . ',' . $shop['id'];
- $order_data['uid'] = $uid;
- $order_data['mobile'] = $user['mobile'];
- $order_data['address_id'] = $address_id;
- $order_data['invoice_id'] = $invoice_id;
- $order_data['name'] = $name;
- $order_data['num'] = $num;
- $order_data['info'] = $info;
- $order_data['oprice'] = $oprice;
- $order_data['price'] = $price;
- $order_data['kou_cash'] = $cash;
- $order_data['method'] = $method;
- $order_data['pay_method'] = $pay_method;
- $order_data['pay_type'] = $pay_type;
- $order_data['refund_cash'] = 0;
- if ($card) {
- $order_data['card_code_id'] = $card['id'];
- $order_data['card_code_card'] = $card['card'];
- $order_data['card_code_cash'] = $card['cash'];
- } elseif ($wallet_cash) {
- $order_data['wallet_cash'] = $wallet_cash;
- }
- if ($user_coupon_id) {
- $order_data['user_coupon_id'] = $user_coupon_id;
- }
- if ($coupon_id) {
- $order_data['coupon_id'] = $coupon_id;
- }
- if ($coupon_cash) {
- $order_data['coupon_cash'] = $coupon_cash;
- }
- $order_data['order_num'] = $this->getOrderId();
- $id = Dever::db('shop/sell_order')->insert($order_data);
- if (!$id) {
- Dever::alert('支付失败');
- }
- foreach($goods as $k => $v) {
- if ($v['ku_state'] == 1) {
- $data['uid'] = $uid;
- $data['order_id'] = $id;
- $data['shop_id'] = $shop['id'];
- $data['goods_id'] = $v['id'];
- $data['sku_id'] = $v['sku_id'];
- $data['price'] = $v['price'];
- $data['num'] = $v['buy_num'];
- if (isset($v['user_coupon_id']) && $v['user_coupon_id']) {
- $data['user_coupon_id'] = $v['user_coupon_id'];
- }
- if (isset($v['coupon_id']) && $v['coupon_id']) {
- $data['coupon_id'] = $v['coupon_id'];
- }
- if (isset($v['coupon_cash']) && $v['coupon_cash']) {
- $data['coupon_cash'] = $v['coupon_cash'];
- } else {
- $data['coupon_cash'] = 0;
- }
- Dever::db('shop/sell_order_goods')->insert($data);
- }
- }
- # 减少库存 增加销量
- $order_data['id'] = $id;
- $this->updateSell($order_data);
- if ($price <= 0 || $pay_type == 2) {
- $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);
- if ($cart) {
- # 来自购物车,要清空一下
- $this->dropCart($cart, $uid, $goods, $shop['id']);
- }
- # 使用优惠券
- $this->useCoupon($coupon_id, $user_coupon_id, $card, $wallet_cash);
- /*
- if ($card) {
- # 更新礼品卡状态
- $card_update['where_id'] = $card['id'];
- $card_update['status'] = 3;
- $card_update['order_id'] = $id;
- $card_update['shop_id'] = $shop['id'];
- Dever::db('goods/card_code')->update($card_update);
- }*/
- return array
- (
- 'order' => '',
- 'order_num' => $order_data['order_num'],
- 'order_id' => $id,
- 'type' => 'pay',
- );
- }
- $sell_config = Dever::db('main/sell_config')->one();
- # 分账
- $other = false;
- if (isset($shop['mid']) && $shop['mid']) {
- $other = array
- (
- 'mid' => $shop['mid'],
- 'order_id' => $order_data['order_num'] . '_' . $shop['id'],
- 'per' => $sell_config['per'],
- 'oprice' => $oprice,
- );
- }
- $param = array
- (
- 'project_id' => 1,
- 'channel_id' => 1,
- 'system_source' => $system_source,
- 'uid' => $uid,
- 'name' => $order_data['name'],
- 'cash' => $price,
- 'product_id' => $id,
- 'order_id' => $order_data['order_num'],
- 'other' => $other,
- 'refer' => $refer,
- );
- if ($uid > 0 && isset($wechat) && $wechat) {
- $param['openid'] = $wechat['openid'];
- }
- $receipt = Dever::input('receipt');
- if ($receipt) {
- $param['receipt'] = $receipt;
- }
- $result = Dever::load('pay/api.pay', $param);
- //$result['type'] = 'test';
- //$result['order'] = '';
- if ($cart) {
- # 来自购物车,要清空一下
- $this->dropCart($cart, $uid, $goods, $shop['id']);
- }
- # 使用优惠券
- $this->useCoupon($coupon_id, $user_coupon_id, $card, $wallet_cash);
- $result['order_num'] = $order_data['order_num'];
- $result['order_id'] = $id;
- return $result;
- }
- public function dropCart($cart, $uid, $goods, $shop_id)
- {
- $where['uid'] = $uid;
- $where['shop_id'] = $shop_id;
- foreach ($goods as $k => $v) {
- $where['goods_id'] = $v['id'];
- if ($cart == 1) {
- Dever::db('shop/cart')->delete($where);
- } else {
- unset($where['uid']);
- Dever::db('shop/shop_cart')->delete($where);
- }
- }
- }
- public function useCoupon($coupon_id, $user_coupon_id, $card, $wallet_cash)
- {
- if ($user_coupon_id) {
- Dever::db('shop/user_coupon')->update(array('where_id' => $user_coupon_id, 'status' => 2));
- }
- if ($card) {
- $state = Dever::db('card/code')->incUseCash(array('where_id' => $card['id'], 'set_use_cash' => $card['cash']));
- } elseif ($wallet_cash) {
- }
- # 这里可以记录一下使用日志
- }
- # 再次发起支付
- public function rpay($id, $system_source = 5, $refer = '')
- {
- $order = Dever::db('shop/sell_order')->find($id);
- if (!$order) {
- Dever::alert('订单信息错误');
- }
- if ($order['status'] > 1) {
- Dever::alert('订单已支付或已取消');
- }
- $param = array
- (
- 'project_id' => 1,
- 'channel_id' => 1,
- 'system_source' => $system_source,
- 'uid' => $order['uid'],
- '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;
- }
- $order = $this->getInfo($order);
- $result = Dever::load('pay/api.pay', $param);
- $result['order_time'] = $order['time'];
- $result['order_price'] = $order['price'];
- $result['order_num'] = $order['order_num'];
- $result['order_id'] = $id;
- return $result;
- }
- # 查询是否支付
- public function cpay($id, $system_source = 5, $refer = '')
- {
- $order = Dever::db('shop/sell_order')->find($id);
- if (!$order) {
- Dever::alert('订单信息错误');
- }
- $result['order_num'] = $order['order_num'];
- $result['order_id'] = $id;
- if ($order['status'] > 1 && $order['status'] <= 6) {
- # 已支付
- $result['status'] = 1;
- } else {
- # 待支付
- $result['status'] = 2;
- }
- return $result;
- }
- # 支付成功回调 安全加密 设置token
- 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_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/sell_order')->one(array('id' => $product_id, 'time' => time()));
- if ($order && $order['status'] == 1) {
- $update = array();
- if ($status == 2) {
- # 生成自提码或者取件码
- $update['code'] = $this->getCode();
- if ($order['method'] == 1) {
- $msg = '您有一件自提商品已订购成功,取件码:' . $update['code'];
- } else {
- $msg = '您有一件外送商品已订购成功,核销码:' . $update['code'];
- # 分配配送信息
- //$this->updatePs($order);
- }
- # 发消息
- if (Dever::project('message') && $order['uid']) {
- $shop = Dever::db('shop/info')->one($order['shop_id']);
- $msg_param['type'] = 1;//消息类型1是订单消息
- $msg_param['id'] = $order['id'];
- $msg_param['name'] = $shop['name'];
- $msg_param = Dever::json_encode($msg_param);
- Dever::load('message/lib/data')->push(-1, $order['uid'], '订购成功通知', $msg, 2, 1, false, $msg_param);
- }
-
- if ($order['pay_method'] == 2) {
- if ($order['refund_cash'] > 0) {
- $status = 6;
- } else {
- $status = 5;
- }
- $update['fdate'] = time();
- }
- $update['status'] = $status;
- $update['where_id'] = $order['id'];
- $update['paydate'] = time();
- Dever::db('shop/sell_order')->update($update);
- }
- }
- return 'ok';
- }
- # 添加配送信息
- public function updatePs($order, $status = 1, $service_id = -1, $order_num = false)
- {
- $where['order_id'] = $order['id'];
- $data = Dever::db('shop/sell_order_ps')->find($where);
- if (!$data) {
- $insert = $where;
- $insert['service_id'] = $service_id;
- $insert['price'] = 0;
- $insert['status'] = $status;
- $insert['num'] = 1;
- if ($status == 2) {
- $insert['qu_date'] = time();
- }
- if ($order_num) {
- $insert['order_num'] = $order_num;
- }
-
- Dever::db('shop/sell_order_ps')->insert($insert);
- } else {
- $update = $where;
- $update['where_id'] = $data['id'];
- $update['status'] = $status;
- if ($status == 2) {
- $update['qu_date'] = time();
- }
- if ($status == 3) {
- $update['qs_date'] = time();
- }
- if ($order_num) {
- $update['order_num'] = $order_num;
- }
- if ($service_id) {
- $update['service_id'] = $service_id;
- }
- Dever::db('shop/sell_order_ps')->update($update);
- }
- }
- # 减少库存,增加销量
- public function updateSell($order, $type = 1)
- {
- $where['order_id'] = $order['id'];
- $where['status'] = 1;
- $data = Dever::db('shop/sell_order_goods')->select($where);
- if ($data) {
- Dever::load('shop/lib/goods')->oper($order, $type, 2, $data);
- }
- }
- # 生成自提码
- public function getCode()
- {
- $where['code'] = Dever::rand(6, 0);
- $state = Dever::db('shop/sell_order')->one($where);
- if (!$state) {
- return $where['code'];
- } else {
- return $this->getCode();
- }
- }
- # 生成订单号
- public function getOrderId()
- {
- $where['order_num'] = Dever::order('G');
- $state = Dever::db('shop/sell_order')->one($where);
- if (!$state) {
- return $where['order_num'];
- } else {
- return $this->getOrderId();
- }
- }
- # 展示订单详情
- public function show()
- {
- $id = Dever::input('order_id');
- $config = Dever::db('shop/sell_order')->config;
- $info = Dever::db('shop/sell_order')->one($id);
- $tk_status = Dever::db('shop/sell_order_refund')->config['status'];
- $tk_process = Dever::db('shop/sell_order_refund')->config['process'];
- $status = $config['status_manage'][$info['status']];
- $tk = Dever::db('shop/sell_order_refund')->find(array('order_id' => $info['id'], 'type' => 1));
- $shop = Dever::db('shop/info')->find($info['shop_id']);
- $user = Dever::db('passport/user')->find($info['uid']);
- $cdate = date('Y-m-d H:i', $info['cdate']);
- if ($info['operdate']) {
- $opertime = date('Y-m-d H:i', $info['operdate']);
- } else {
- $opertime = '';
- }
- if ($info['fdate']) {
- $fdate = date('Y-m-d H:i', $info['fdate']);
- } else {
- $fdate = '';
- }
- if (!$user) {
- $user['username'] = '无';
- }
- if (!isset($user['truename'])) {
- $user['truename'] = $user['username'];
- }
- if (!$info['coupon_cash']) {
- $info['coupon_cash'] = 0;
- }
- if (!$info['kou_cash']) {
- $info['kou_cash'] = 0;
- }
- if (!$info['ps_cash']) {
- $info['ps_cash'] = 0;
- }
- if (!$info['wallet_cash']) {
- $info['wallet_cash'] = 0;
- }
- if (!$info['card_code_cash']) {
- $info['card_code_cash'] = 0;
- }
- if ($info['card_code_card']) {
- $info['card_code_cash'] .= '('.$info['card_code_card'].')';
- }
- if ($tk && $tk['process'] == 1) {
- $status = '申请' . $tk_status[$tk['status']];
- $process = $tk_process[$tk['process']];
- $process = '';
- }
- $time = array();
- $time[] = array('下单时间', $cdate);
- if ($opertime) {
- if ($info['status'] == 2) {
- $time[] = array('通知时间', $opertime);
- }
- if ($info['status'] == 7) {
- $time[] = array('取消时间:', $opertime);
- }
- }
- if ($fdate) {
- $time[] = array('完成时间:', $fdate);
- }
- $result = array();
- $result[$info['order_num']] = array
- (
- 'type' => 'info',
- 'content' => array
- (
- array
- (
- array('订单状态', $status),
- array('支付方式', $config['pay_method'][$info['pay_method']]),
- array('配送方式', $config['method'][$info['method']]),
- ),
-
- array
- (
- array('门店名称', $shop['name']),
- array('联系人', $shop['truename']),
- array('联系电话', $shop['mobile']),
- ),
- array
- (
- array('下单用户', $user['username']),
- array('联系电话', $info['mobile']),
- array('订单备注', $info['info']),
- ),
- array
- (
- array('支付金额', '¥ ' . round($info['price'], 2)),
- array('优惠券满减', '¥ ' . $info['coupon_cash']),
- array('配送费', '¥ ' . $info['ps_cash']),
- ),
- array
- (
- array('礼品卡抵扣', '¥ ' . round($info['card_code_cash'], 2)),
- array('钱包抵扣', '¥ ' . round($info['wallet_cash'], 2)),
- array('抹零', '¥ ' . round($info['kou_cash'], 2)),
- ),
- $time,
- )
- );
- if ($info['address_id'] > 0) {
- $address = Dever::load('passport/address')->getOne($info['uid'], $info['address_id']);
- if ($address) {
- $result['收货地址'] = array
- (
- 'type' => 'info',
- 'content' => array
- (
- array
- (
- array('联系人', $address['contact']),
- array('联系电话', $address['mobile']),
- ),
- array
- (
- array('收货地址', $address['address']),
- array('门牌号', $address['house_number']),
- ),
- ),
- );
- }
- }
-
- if ($info['invoice_id'] > 0) {
- $invoice = Dever::load('passport/invoice')->getOne($info['uid'], $info['invoice_id']);
- if ($invoice) {
- $result['发票信息'] = array
- (
- 'type' => 'info',
- 'content' => array
- (
- array
- (
- array('发票类型', $invoice['title_type_name']),
- array('抬头', $invoice['title']),
- array('税号', $invoice['number']),
- ),
- array
- (
- array('手机号', $invoice['mobile']),
- array('邮箱', $invoice['email']),
- ),
- ),
- );
- }
- }
- if (!$info['refund_cash']) {
- $info['refund_cash'] = 0;
- }
- if ($info['refund_cash'] > 0 && $tk) {
- $process = $tk_process[$tk['process']];
- $tk['cdate'] = date('Y-m-d H:i', $tk['cdate']);
- $result['退款信息'] = array
- (
- 'type' => 'info',
- 'content' => array
- (
- array
- (
- array('退款金额', $info['refund_cash']),
- array('数量', $info['num']),
- array('退款进度', $process),
- ),
- array
- (
- array('申请时间', $tk['cdate']),
- array('退款原因', $tk['desc']),
- ),
- ),
- );
- }
- $body = $refund_body = array();
- $body_total = array();
- $body_total['price'] = 0;
- $body_total['num'] = 0;
- $refund_body_total = array();
- $refund_body_total['price'] = 0;
- $refund_body_total['num'] = 0;
- $goods = Dever::db('shop/sell_order_goods')->select(array('order_id' => $info['id']));
- $goods_status = Dever::db('shop/sell_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/sell_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']];
- //$status .= ' [' . $tk_process[$tk['process']] . ']';
- }
- if (isset($goods_info['sku'])) {
- $sku = $goods_info['sku']['string'];
- } else {
- $sku = '无';
- }
- $d = array
- (
- 'pic' => $goods_info['cover'],
- 'name' => $goods_info['name'],
- 'sku' => $sku,
- 'price' => $v['price'],
- 'num' => $v['num'],
- 'status' => $status,
- );
- if ($tk && $v['status'] == 3) {
- $d['status'] = $tk_status[$tk['status']];
- $refund_body[] = $d;
- $price = $v['price']*$v['num'];
- $refund_body_total['price'] += $price;
- $refund_body_total['num'] += $v['num'];
- } else {
- $body[] = $d;
- $price = $v['price']*$v['num'];
- $body_total['price'] += $price;
- $body_total['num'] += $v['num'];
- }
- }
- if ($body) {
- if ($refund_body_total['price']) {
- $body_total['set_price'] = $body_total['price'] - $refund_body_total['price'];
- }
- if ($refund_body_total['num']) {
- $body_total['set_num'] = $body_total['num'] - $refund_body_total['num'];
- }
- $result['商品清单'] = array
- (
- 'type' => 'list',
- 'content' => $body,
- 'total' => $body_total,
- );
- }
- if ($refund_body) {
- $result['退款商品'] = array
- (
- 'type' => 'list',
- 'content' => $refund_body,
- 'total' => $refund_body_total,
- );
- }
-
- $head = array
- (
- 'name' => '基本信息',
- 'btn' => array(),
- );
- $html = Dever::show($head, $result);
- return $html;
- }
- # 第三方确认
- public function tixian()
- {
- $param = array();
- ksort($param);
- $string = array();
- foreach ($param as $k => $v) {
- if (strstr($v, '&') || strstr($v, '@')) {
- $v = urlencode($v);
- }
- $string[] = $k . '=' . $v;
- }
- }
-
- # 发货
- public function notice($info)
- {
- $config = Dever::db('shop/sell_order')->config;
- $state = Dever::db('shop/sell_order')->update(array('where_id' => $info['id'], 'status' => 3, 'operdate' => time()));
- # 给用户发消息
- if ($info['uid'] && $info['uid'] > 0) {
- $shop = Dever::db('shop/info')->one($info['shop_id']);
- $msg_param['type'] = 1;//消息类型1是订单消息
- $msg_param['id'] = $info['id'];
- $msg_param['name'] = $shop['name'];
- $msg_param = Dever::json_encode($msg_param);
- if ($info['method'] == 1) {
- $msg = '您有一件自提商品已完成配货,请尽快到指定门店使用取件码取货,取件码:' . $info['code'];
- $name = '取货通知';
- $wechat_msg = '您的商品已完成配货';
- } else {
- $msg = '您有一件外送商品已开始配送,收货时请将取件码提供给配送员核实货品信息,取件码:' . $info['code'];
- $name = '配送通知';
- $wechat_msg = '您的商品已开始配送';
- }
-
- Dever::load('message/lib/data')->push(-1, $info['uid'], $name, $msg, 1, 1, false, $msg_param);
- if (Dever::load('wechat_applet')) {
- $user = Dever::db('passport/wechat')->one(array('uid' => $info['uid'], 'system_id' => 1, 'system_source' => 5));
- if ($user && $user['openid']) {
- $info['status_name'] = $config['status'][$info['status']];
- $send = array
- (
- 'character_string6' => array
- (
- 'value' => $info['order_num'],
- ),
-
- 'phrase7' => array
- (
- 'value' => $info['status_name'],
- ),
- 'thing8' => array
- (
- 'value' => $info['code'],
- ),
- 'amount1' => array
- (
- 'value' => $info['price'] . '元',
- ),
- 'thing5' => array
- (
- 'value' => $wechat_msg,
- ),
- );
- $send = Dever::json_encode($send);
- Dever::load('wechat_applet/subscribe')->sendOne('order_process', 1, $user['openid'], 'pages/app/order/order?id=' . $info['id'], $send, Dever::config('base')->wechat_applet);
- }
- }
- }
- }
- }
|