1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386 |
- <?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));
- # 还原库存
- if ($this->type == 1) {
- $this->cancel($info['uid'], $info['id'], 11);
- } else {
- $this->type = 2;
- $this->cancel($info['shop_id'], $info['id'], 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['discount_cash']) {
- $info['discount_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);
- }
- $tk = Dever::db('shop/sell_order_refund')->find(array('order_id' => $info['id'], 'process' => 2));
- if ($tk) {
- $info['tk_status'] = 1;
- } else {
- $info['tk_status'] = 2;
- }
- $info['card'] = array();
- 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.')';
- $info['card']['info'] = $card_info;
- $info['card']['id'] = $card_code['id'];
- $info['card']['card'] = $card_code['card'];
- $info['card']['cash'] = $info['card_code_cash'];
- }
- }
- }
-
- 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'] = Dever::array_decode($info['ps_info']['service']);
- $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']) {
- if (isset($info['ps_info']['service']['serivce'])) {
- $info['ps_info']['service'] = $info['ps_info']['service']['service'];
- }
- foreach ($info['ps_info']['service'] as $k => $v) {
- if (isset($v['order_num']) && $v['order_num']) {
- $info['ps_info']['service'][$k]['service_name'] = '商家自送';
- if ($v['service_id'] > 0) {
- $service = Dever::db('shop/service')->find($v['service_id']);
- if ($service) {
- $info['ps_info']['service'][$k]['service_name'] = $service['name'];
- }
- }
- } else {
- unset($info['ps_info']['service'][$k]);
- }
- }
- }
- }
- }
- $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) {
- $check = false;
- if (isset($card_info) && $card_info) {
- $info['card']['goods'] = array();
- $check = false;
- $shop = Dever::db('shop/info')->find($info['shop_id']);
- if ($shop['type'] == 1 && $card_info['shop_set'] == 1) {
- $check = false;
- } else {
- $goods = Dever::db('card/info_goods')->getData(array('card_id' => $card_info['id']));
- if ($goods || $card_info['category']) {
- $check = true;
- $card_info['category'] = explode(',', $card_info['category']);
- if ($goods) {
- $goods = array_keys($goods);
- }
- }
- }
- }
- $info['discount_goods'] = array();
- 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 ($check) {
- if ($goods) {
- if (in_array($v['goods_id'], $goods)) {
- $info['card']['goods'][] = $info['goods'][$k]['info'];
- }
- } elseif ($card_info['category']) {
- if (in_array($info['goods'][$k]['info']['top_category_id'], $card_info['category'])) {
- $info['card']['goods'][] = $info['goods'][$k]['info'];
- }
- }
- }
- if ($v['discount_id'] && $v['discount_id'] > 0) {
- $info['discount_goods'][] = $info['goods'][$k];
- }
- }
- 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']];
- }
- }
- $info = $this->refund_button($info);
- return $info;
- }
- # 是否可以退款
- public function refund_button($info)
- {
- $info['refund_button'] = 1;
- # 是否显示退款按钮
- $manage_config = Dever::db('main/manage_config')->find();
- if (isset($info['shop_id']) && $info['shop_id'] == 3) {
- # 平台商城
- $info['code'] = '';
- $refund_time = $manage_config['shop_refund_day']*86400;
- } else {
- # 体验店
- $refund_time = $manage_config['sell_refund_day']*86400;
- }
- if ($refund_time > 0 && ($info['status'] == 5 || $info['status'] == 6)) {
- $cur = time();
- if (strstr($info['fdate'], '-')) {
- $info['fdate'] = Dever::maketime($info['fdate']);
- }
- if ($cur - $info['fdate'] < $refund_time) {
- # 可以退款
- $info['refund_button'] = 1;
- } else {
- # 不可以退款
- $info['refund_button'] = 2;
- }
- }
- return $info;
- }
- # 取消订单
- public function cancel($id, $order_id, $status = 7)
- {
- $data = $this->getView($id, $order_id, false);
- if ($data['status'] == 1) {
- $state = Dever::db('shop/sell_order')->update(array('where_id' => $data['id'], 'status' => $status, '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']));
- Dever::load('cash/lib/order')->up($data, 3, 3, false, 2);
- } elseif (isset($data['wallet_cash']) && $data['wallet_cash'] && $data['wallet_cash'] > 0) {
- # 还原钱包
- Dever::load('shop/lib/money')->up($data['uid'], 4, $data['wallet_cash'], $data['id'], '商品退款', 2);
- Dever::load('cash/lib/order')->up($data, 3, 3, false, 3);
- }
- $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['list'][$k]['cash'] = $v['price'] - $data['list'][$k]['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 && $data['discount_id'] <= 0) {
- if ($card) {
- # 验证这个礼品卡属于自己
- $card_code = Dever::db('card/code')->find($card);
- if ($card_code['dh_uid'] == $data['uid']) {
- $card_state = Dever::load('card/lib/code')->check($card_code);
- if ($card_state == 1) {
- if (!$card_code['total_cash']) {
- $card_code['total_cash'] = $card_code['info']['value'];
- }
- if (!$card_code['use_cash']) {
- $card_code['use_cash'] = 0;
- }
- $card_cash = $card_code['total_cash'] - $card_code['use_cash'];
- if ($card_cash > 0) {
- $data['card'] = array();
- $card_goods = array();
- $check = false;
- $xian = false;
- if ($data['shop']['type'] == 1 && $card_code['info']['shop_set'] == 1) {
- $check = false;
- $xian = false;
- } else {
- $xian = true;
- $goods = Dever::db('card/info_goods')->getData(array('card_id' => $card_code['info']['id']));
- if ($goods || $card_code['info']['category']) {
- $check = true;
- }
- }
- if ($check) {
- $price = 0;
- $card_code['info']['category'] = explode(',', $card_code['info']['category']);
- if ($goods) {
- $goods = array_keys($goods);
- }
- # 检测当前商品是否符合该卡的限制
- foreach ($data['list'] as $k => $v) {
- if (!isset($v['cash'])) {
- $v['cash'] = $v['price'];
- }
- if ($goods) {
- if (in_array($v['id'], $goods)) {
- $price += ($v['cash'] * $v['num']);
- $card_goods[] = $v;
- }
- } elseif ($card_code['info']['category']) {
- if (in_array($v['top_category_id'], $card_code['info']['category'])) {
- $price += ($v['cash'] * $v['num']);
- $card_goods[] = $v;
- }
- }
- }
- } else {
- $price = $data['price'];
- }
- if ($card_cash > $price) {
- $data['card']['cash'] = $price;
- } else {
- $data['card']['cash'] = $card_cash;
- }
- $data['price'] -= $data['card']['cash'];
- if ($xian && ($price <= 0 || ($card_code['info']['min'] > 0 && $price < $card_code['info']['min']))) {
- # 不满足限额
- unset($data['card']);
- } else {
- $data['card']['info'] = $card_code['info'];
- $data['card']['goods'] = $card_goods;
- $data['card']['id'] = $card_code['id'];
- $data['card']['card'] = $card_code['card'];
-
- $data['card']['cash'] = round($data['card']['cash'], 2);
- $config = Dever::db('main/sell_config')->find();
- $card_type = Dever::db('card/type')->find($card_code['info']['type_id']);
- $data['card']['name'] = $config['card_name'];
- $card = substr($card_code['card'], -4);
- $data['card']['name'] .= '('.$card_type['name'].$card.')';
- }
- }
- }
- }
- } elseif ($wallet && $wallet > 0) {
- $user = Dever::db('passport/user')->find($data['uid']);
- if ($user) {
- if ($user['cash'] >= $data['price']) {
- $data['wallet_cash'] = $data['price'];
- $data['price'] = 0;
- } else {
- $data['wallet_cash'] = $user['cash'];
- $data['price'] = $data['price'] - $user['cash'];
- }
- }
- }
- }
- 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);
- # 获取角色
- $role = $this->getRole($data);
- $data['discount_goods'] = array();
- $data['discount_cash'] = 0;
- $data['discount_id'] = 0;
- # 计算总价格
- 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) {
- if (isset($goods_sku['min'])) {
- $data['list'][$k]['min'] = $goods_sku['min'];
- }
- if (isset($goods_sku['price_id']) && $goods_sku['price_id'] > 0) {
- $price_template = Dever::load('price/lib/data')->get($v, $s, $goods_sku['price_id']);
- if ($price_template) {
- /*
- if ($goods_sku['min'] <= 0 && $price_template['price_num'] > 0) {
- $data['list'][$k]['min'] = $price_template['price_num'];
- }
- */
- if ($price_template['price_sell'] && $price_template['price_sell'] > 0) {
- $data['list'][$k]['price'] = $price_template['price_sell'];
- }
- }
- }
- }
-
- $data['list'][$k]['cash'] = $data['list'][$k]['price'];
- $data['list'][$k]['num'] = $n;
- # 2是库存不足
- $data['list'][$k]['ku_state'] = 1;
- if ($data['list'][$k]['status'] == 2) {
- $total = 0;
- } else {
- # 验证是否有货
- $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;
- # 根据角色设置折扣
- if ($role) {
- $role_set = false;
- foreach ($role as $rk => $rv) {
- $discount_goods = Dever::db('act/discount_goods')->getData(array('discount_id' => $rv['id']));
- if ($discount_goods) {
- $discount_goods = array_keys($discount_goods);
- if (in_array($data['list'][$k]['id'], $discount_goods)) {
- $role_set = $rv;
- break;
- }
- } elseif ($rv['category'] && in_array($data['list'][$k]['top_category_id'], $rv['category'])) {
- $role_set = $rv;
- break;
- }
- }
- if ($role_set) {
- $discount_cash = round($data['list'][$k]['price'] * ($role_set['num']/10), 2);
- $data['list'][$k]['discount_id'] = $role_set['id'];
- $data['discount_goods'][] = $data['list'][$k];
- $data['discount_id'] = $role_set['id'];
- $data['list'][$k]['discount_cash'] = round(($data['list'][$k]['price'] - $discount_cash) * $n, 2);
- $data['discount_cash'] += $data['list'][$k]['discount_cash'];
- $data['price'] += $discount_cash * $n;
- } else {
- $data['price'] += $data['list'][$k]['price'] * $n;
- }
- } else {
- $data['price'] += $data['list'][$k]['price'] * $n;
- }
-
- $data['oprice'] += $data['list'][$k]['cash'] * $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;
- }
- if (!$data['ps_cash']) {
- $data['ps_cash'] = 0;
- }
- }
- return $data;
- }
- public function getRole($data)
- {
- if(isset($data['uid']) && $data['uid'] > 0) {
- return Dever::load('shop/lib/info')->getRole($data['uid']);
- }
- return false;
- }
- # 发起支付
- 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, $discount_id, $discount_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) {
- $ps_cash = $shop['ps_cash'];
- if ($shop['free_ps_cash'] && $shop['free_ps_cash'] > 0 && $price >= $shop['free_ps_cash']) {
- $ps_cash = 0;
- }
- if (!$ps_cash) {
- $ps_cash = 0;
- }
- $order_data['ps_cash'] = $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_id'] = $card['info']['id'];
- $order_data['card_code_id'] = $card['id'];
- $order_data['card_code_card'] = $card['card'];
- $order_data['card_code_cash'] = $card['cash'];
- } elseif ($wallet_cash && $wallet_cash > 0) {
- $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;
- }
- if ($discount_id) {
- $order_data['discount_id'] = $discount_id;
- }
- if ($discount_cash) {
- $order_data['discount_cash'] = $discount_cash;
- }
- $time = time();
- $order_data_where = $order_data;
- $order_data_where['start'] = $time - 60;
- $order_data_where['end'] = $time;
- $order_data_where['option']['start'] = array('yes-cdate', '>=');
- $order_data_where['option']['end'] = array('yes-cdate', '<=');
- $info = Dever::db('shop/sell_order')->find($order_data_where);
- $order_data['order_num'] = $this->getOrderId();
-
- if (!$info) {
- $id = Dever::db('shop/sell_order')->insert($order_data);
- } else {
- return array
- (
- 'order' => '',
- 'order_num' => $info['order_num'],
- 'order_id' => $info['id'],
- 'type' => 'test',
- );
- }
- 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;
- }
- if (isset($v['discount_id']) && $v['discount_id']) {
- $data['discount_id'] = $v['discount_id'];
- }
- if (isset($v['discount_cash']) && $v['discount_cash']) {
- $data['discount_cash'] = $v['discount_cash'];
- }
- 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($order_data, $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' => 'test',
- );
- }
- $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($order_data, $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($order_data, $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']));
- # 生成结算单
- $code = Dever::db('card/code')->find($card['id']);
- if ($code) {
- $card_info = Dever::db('card/info')->find($code['card_id']);
- if ($card_info) {
- if ($card_info['price'] == $card_info['value']) {
- $price = $card['cash'];
- } else {
- $zhe = round($card_info['price']/$card_info['value'], 2);
- $price = round($card['cash']*$zhe, 2);
- }
- $order_data['type'] = 1;
- $order_data['type_id'] = $order_data['shop_id'];
- $order_data['source_type'] = 4;
- $order_data['source_id'] = -1;
- $order_data['price'] = $price;
- $order_data['p_price'] = $card['cash'];
- Dever::load('cash/lib/order')->up($order_data, 3, 1, false, 2);
- }
- }
-
- } elseif ($wallet_cash && $wallet_cash > 0) {
- //$wallet_cash = -1*$wallet_cash;
- Dever::load('shop/lib/money')->up($order_data['uid'], 12, $wallet_cash, $order_data['id'], '购买商品', 2);
- $order_data['type'] = 1;
- $order_data['type_id'] = $order_data['shop_id'];
- $order_data['source_type'] = 4;
- $order_data['source_id'] = -1;
- $order_data['price'] = $wallet_cash;
- $order_data['p_price'] = $wallet_cash;
- Dever::load('cash/lib/order')->up($order_data, 3, 1, false, 3);
- }
- # 这里可以记录一下使用日志
- }
- # 再次发起支付
- 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::project('message') && 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 = '', $cdate = false)
- {
- $where['order_id'] = $order['id'];
- $where['clear'] = true;
- $data = Dever::db('shop/sell_order_ps')->find($where);
- $service = array();
- if ($service_id) {
- $service['service_id'] = $service_id;
- }
- if ($order_num) {
- $service['order_num'] = $order_num;
- }
- if (!$data) {
- $insert = $where;
- if ($service) {
- $insert['service'][] = $service;
- $insert['service'] = Dever::array_encode($insert['service']);
- }
- $insert['status'] = $status;
- if ($status == 2) {
- $insert['qu_date'] = time();
- }
- if ($cdate) {
- $insert['cdate'] = $cdate;
- }
- 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 ($service) {
- $data['service'] = Dever::array_decode($data['service']);
- $update['service'] = $data['service'];
- $update['service'][] = $service;
- $insert['service'] = Dever::array_encode($update['service']);
- }
- if ($cdate) {
- $update['cdate'] = $cdate;
- }
- 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 finish($info, $shop_id)
- {
- if ($info && $info['shop_id'] == $shop_id && $info['status'] <= 4) {
- if ($info['refund_cash'] > 0) {
- $status = 6;
- } else {
- $status = 5;
- }
- $state = Dever::db('shop/sell_order')->update(array('where_id' => $info['id'], 'status' => $status, 'fdate' => 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);
- $msg = '您有一笔订单已签收,祝您用餐愉快~';
- Dever::project('message') && Dever::project('message') && Dever::load('message/lib/data')->push(-1, $info['uid'], '订单完成通知', $msg, 2, 1, false, $msg_param);
- }
- if ($info['method'] == 2) {
- Dever::load('shop/lib/sell')->updatePs($info, 3);
- }
- if ($info['card_code_id'] && $info['card_code_id'] > 0) {
- # 更新礼品卡状态
- $card_update['where_id'] = $info['card_code_id'];
- $card_update['status'] = 4;
- Dever::db('goods/card_code')->update($card_update);
- Dever::load('cash/lib/order')->up($info, 3, 2, false, 2);
- } elseif (isset($info['wallet_cash']) && $info['wallet_cash'] && $info['wallet_cash'] > 0) {
- Dever::load('cash/lib/order')->up($info, 3, 2, false, 3);
- }
- if ($info['shop_type'] == 2) {
- $w['parent_type'] = 2;
- $w['parent_order_id'] = $info['id'];
- $w['status'] = 4;
- $buy_order = Dever::db('shop/buy_order')->select($w);
- if ($buy_order) {
- foreach ($buy_order as $k => $v) {
- Dever::load('mshop/lib/buy')->set(1,1)->finish($shop_id, $v['id']);
- }
- }
- }
- return 'ok';
- } else {
- Dever::alert('您没有权限核销');
- }
- }
- # 平台订单已收货
- public function yes_api()
- {
- $shop_id = Dever::input('shop_id');
- $order_id = Dever::input('order_id');
- $info = Dever::db('shop/sell_order')->find(array('id' => $order_id));
- if ($info && $info['type'] == 10) {
- $this->finish($info, $shop_id);
- } else {
- Dever::alert('您没有权限核销');
- }
- return 'reload';
- }
- # 生成自提码
- 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'] && $info['fdate'] > 0) {
- $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['discount_cash']) {
- $info['discount_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();
- $info['card'] = '';
- if ($info['card_code_id']) {
- $code = Dever::db('card/code')->find($info['card_code_id']);
- if ($code) {
- $card_info = Dever::db('card/info')->find($code['card_id']);
- if ($card_info) {
- if (!$code['total_cash']) {
- $code['total_cash'] = $card_info['value'];
- }
- $info['card'] = '(' . $card_info['name'] .$code['card']. ',面值:'.$code['total_cash'].', 剩余:'.round($code['total_cash']-$code['use_cash'], 2).')';
- }
- }
- }
- $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) . $info['card']),
- array('钱包抵扣', '¥ ' . round($info['wallet_cash'], 2)),
- array('抹零', '¥ ' . round($info['kou_cash'], 2)),
- array('代理优惠', '¥ ' . round($info['discount_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_info']),
- 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']),
- ),
- ),
- );
- }
- }
- $audit = Dever::input('audit');
- if ($audit == 1) {
- # 审核信息
- $this->show_audit($info, $result);
- } else {
- # 配送信息
- $this->show_ps($info, $result);
- # 退款信息
- $this->show_refund($info, $result, $tk, $tk_process);
- # 商品信息
- $this->show_goods($info, $result, $tk_status);
- # 对账信息
- $this->show_dz($info, $result);
- }
- $button = array();
- if (Dever::load('manage/auth')->checkFunc('menu_61.menu_218', 'editaddress', '修改地址')) {
- if ($info['address_id']>0){
- $button[] = array
- (
- 'type' => 'edit',
- 'link' => Dever::url('project/database/update?project=passport&table=address&where_id='.$info['address_id'].'&status=1&col=contact,mobile,area,address,house_number', 'manage'),
- 'name' => '修改地址',
- );
- }
- }
- $head = array
- (
- 'name' => '基本信息',
- 'btn' => $button,
- );
- $html = Dever::show($head, $result);
- return $html;
- }
- private function show_goods($info, &$result, $tk_status)
- {
- $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 = '无';
- }
- if ($info['shop_type'] == 2 && $info['status'] > 1 && $info['status'] < 7) {
- $tui = Dever::url('buy_order_tui?shop_id='.$v['shop_id'].'&order_goods_id=' . $v['id'] . '&order_id=' . $info['id'] . '&cash=' . $v['price'], 'shop');
- $info = $this->refund_button($info);
- if ($info['refund_button'] == 1) {
- $status = '<a href="javascript:;" onclick="fastEdit($(this), \''.$tui.'\', \'退款\', \'\', 1)" class="layui-btn">退款</a>';
- }
- }
- $d = array
- (
- 'pic' => $goods_info['cover'],
- 'name' => $goods_info['name'],
- 'sku' => $sku,
- 'price' => $v['price'],
- 'num' => $v['num'] . $goods_info['unit'],
- 'status' => $status,
- );
- if ($goods_info['price_type'] > 2) {
- $d['goods'] = $goods_info['goods'];
- }
- 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,
- );
- }
- }
- private function show_dz($info, &$result)
- {
- $search_option_shop_type = Dever::input('search_option_shop_type');
- if ($search_option_shop_type == 2) {
- # 平台门店采购单
- $buy_order = Dever::db('shop/buy_order')->getDataToId(array('type' => 1, 'type_id' => $info['shop_id'], 'parent_type' => 2, 'parent_order_id' => $info['id']));
- $source_order_id = array();
- if ($buy_order) {
- $source_order_id = array_keys($buy_order);
- }
- # 平台门店结算单
- if ($source_order_id) {
- $cash_order = Dever::db('cash/order')->getDataByOrderId(array('source_order_id' => $source_order_id));
- $col = 'cash';
- }
- }
- if (isset($cash_order) && $cash_order) {
- $head = array('序号', '结算单号', '数量', '金额', '结算类型', '入账状态', '审核状态', '结算时间', '审核备注', '管理');
- $body = array();
- $config = Dever::db('cash/order')->config;
- foreach ($cash_order as $k => $v) {
- $cdate = date('Y-m-d H:i', $v['cdate']);
- $fdate = $v['fdate'] ? date('Y-m-d H:i', $v['fdate']) : '';
- $operdate = $v['operdate'] ? date('Y-m-d H:i', $v['operdate']) : '';
- $cash = $v[$col];
- $url = Dever::url('project/database/list?project=shop&table=buy_order_goods&order_id='.$v['source_order_id'].'&page_type=1', 'manage');
- $v['source_order_num'] = '<a href="'.$url.'" style="color:blue">'.$v['source_order_num'].'</a>';
- $m = '';
- if ($v['audit'] == 1 && $v['audit_type'] == 2) {
- $url = Dever::url('project/database/update?project=cash&table=order&where_id='.$v['id'].'&col=audit,desc', 'manage');
- $m = '<a href="javascript:;" onclick="fastEdit($(this), \''.$url.'\', \'审核\', \'\', 1)" class="layui-btn">审核</a>';
- }
- $d = array
- (
- $k+1,
- $v['order_num'],
- $v['num'],
- $cash,
- $config['config_jstype'][$v['jstype']],
- $config['config_status'][$v['status']],
- $config['config_audit'][$v['audit']],
- $operdate,
- $v['desc'],
- $m,
- );
- $body[] = $d;
- }
- $result['对账清单'] = array
- (
- 'type' => 'table',
- 'content' => array
- (
- 'head' => $head,
- 'body' => $body,
- )
- );
-
- }
- }
- private function show_refund($info, &$result, $tk, $tk_process)
- {
- 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']),
- ),
- ),
- );
- }
- }
- private function show_ps($info, &$result)
- {
- $info['ps_info'] = Dever::db('shop/sell_order_ps')->find(array('order_id' => $info['id']));
- if ($info['ps_info']) {
- $info['ps_info']['service'] = Dever::array_decode($info['ps_info']['service']);
- $info['ps_info']['cdate'] = date('Y-m-d H:i', $info['ps_info']['cdate']);
- if ($info['ps_info']['qs_date']) {
- $info['ps_info']['qs_date'] = date('Y-m-d H:i', $info['ps_info']['qs_date']);
- } else {
- $info['ps_info']['qs_date'] = '无';
- }
- $status = Dever::db('shop/sell_order_ps')->config['status'];
- $info['ps_info']['status_name'] = $status[$info['ps_info']['status']];
-
- $service = array();
- if ($info['ps_info']['service']) {
- if (isset($info['ps_info']['service']['serivce'])) {
- $info['ps_info']['service'] = $info['ps_info']['service']['service'];
- }
- foreach ($info['ps_info']['service'] as $k => $v) {
- if (isset($v['order_num']) && $v['order_num']) {
- $info['ps_info']['service'][$k]['service_name'] = '';
- if ($v['service_id'] > 0) {
- $service_info = Dever::db('shop/service')->find($v['service_id']);
- if ($service_info) {
- $info['ps_info']['service'][$k]['service_name'] = $service_info['name'];
- }
- }
- $service[] = array
- (
- array('名称', $info['ps_info']['service'][$k]['service_name']),
- array('单号', isset($v['order_num']) ? $v['order_num'] : ''),
- );
- }
- }
- }
- $result['物流信息'] = array
- (
- 'type' => 'info',
- 'content' => array
- (
- array
- (
- array('发货时间', $info['ps_info']['cdate']),
- array('收货时间', $info['ps_info']['qs_date']),
- array('查单', '<a href="https://www.kuaidi100.com/?from=openv" target="_blank">点此手动查单</a>'),
- ),
- ),
- );
- $result['物流信息']['content'] = array_merge($result['物流信息']['content'], $service);
- }
- }
- private function show_audit($info, &$result)
- {
- $info['type'] = 1;
- $goods = Dever::db('shop/sell_order_goods')->select(array('order_id' => $info['id'], 'status' => 1));
- $shop = Dever::db('shop/info')->find($info['shop_id']);
- list($data, $source) = Dever::load('mshop/lib/buy')->assign($goods, $info, $shop, true);
- if ($info) {
- $head = array('序号', '商品编号', '商品名称', '供货商', '订货总数量', '商品计量单位', '订货单总金额');
- $body = array();
- $i = 0;
- if ($data) {
- foreach ($data as $k => $v) {
- foreach ($v as $v1) {
- $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v1['goods_id'], $v1['sku_id']);
- if (isset($goods_info['sku'])) {
- $sku = '[' . $goods_info['sku']['string'] . ']';
- } else {
- $sku = '';
- }
- $d = array
- (
- $i+1,
- $goods_info['code'],
- $goods_info['name'] . $sku,
- Dever::load('mshop/lib/buy')->getSelect($source, $v1['source_id'], $v1['order_goods_id']),
- $v1['num'],
- $goods_info['unit'],
- $v1['price'] * $v1['num'],
- );
- $body[] = $d;
- $i++;
- }
- }
- }
-
- if ($goods) {
- foreach ($goods as $k => $v) {
- $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
- if (isset($goods_info['sku'])) {
- $sku = '[' . $goods_info['sku']['string'] . ']';
- } else {
- $sku = '';
- }
- $d = array
- (
- $i+1,
- $goods_info['code'],
- $goods_info['name'] . $sku,
- Dever::load('mshop/lib/buy')->getSelect($source, false, $v['id']),
- $v['num'],
- $goods_info['unit'],
- $v['price'] * $v['num'],
- );
- $body[] = $d;
- $i++;
- }
- }
- $btn = array();
- $btn[] = array(
- 'type' => 'click',
- 'content' => 'setAudit()',
- 'name' => '全部调整<script>function setAudit(){layer.confirm(\'确定进行此项操作吗?\', function(){var audit = $(\'.audit_set\').val();$(\'.audit\').each(function(){$(this).val(audit);layui.form.render();layer.closeAll()})})}</script>'
- );
- $result['商品拆单'] = array
- (
- 'type' => 'table',
- 'head' => array('name' => '供货商:' . Dever::load('mshop/lib/buy')->getSelect($source, false, false), 'btn' => $btn),
- 'content' => array
- (
- 'head' => $head,
- 'body' => $body,
- )
- );
- $url = Dever::url('lib/sell.audit_set_commit?json=1&order_id=' . $info['id'], 'shop');
- $result['审核结果'] = array
- (
- 'type' => 'html',
- 'content' => '<script>function saveAudit(){var goods = [];var audit = 1; var audit_text = $(".update_audit_text").val();$(".update_audit").each(function(){if ($(this).get(0).checked){audit = $(this).val()}});$(\'.audit\').each(function(){var v = $(this).val();var id = $(this).attr("id");goods.push(v + "_" + id);});layer.confirm(\'确定进行此项操作吗?\', function(){layui.form.render();var send = {};send.goods = goods.join(",");send.audit = audit;send.audit_text = audit_text;console.info(send);$.post("'.$url.'", send, function(t){t = JSON.parse(t);showYes(t)})})}</script><style>.layui-form-radio{padding-right: 0px;margin: 0px 0px 0 0;margin-left:40px}</style><div class="layui-form-item show_input"><div classs="layui-col-lg6"><div><label style="font-size:14px;font-weight:bold;" class="">审核状态</label><div style="display:inline" class="am-btn-group"><input update_type="checked" class="checkbox-checkall-0_update_audit update_audit update_value validate[required]" type="radio" name="update_audit[]" value="2" checked="" show_id="audit" autocomplete="off" id="form-validation-field-1"> 审核通过 <input update_type="checked" class="autoload checkbox-checkall-0_update_audit update_audit update_value validate[required]" type="radio" name="update_audit[]" value="3" autocomplete="off" id="form-validation-field-0"> 审核未通过 </div></div></div></div><div class="layui-form-item" style="display: block;"><div classs="layui-col-lg6"><div><label style="font-size:14px;font-weight:bold;" class="">审核备注</label></div><div><textarea style="height:100px;" update_type="value" class="update_value update_audit_text form-control layui-input validate[required]" name="update_desc"></textarea></div></div></div> <div style="text-align:left" class="layui-layer-btn layui-layer-btn-"><a class="layui-btn layui-btn-primary" onclick="saveAudit()">保存</a><a class="layui-layer-btn1" style="display:none">取消</a></div>',
- );
- }
- }
- # 第三方确认
- 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(),'fadate'=>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::project('message') && Dever::load('message/lib/data')->push(-1, $info['uid'], $name, $msg, 2, 1, false, $msg_param);
- if (Dever::project('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);
- }
- }
- }
- }
- # 采购订单审核 手动拆单
- public function audit_set_commit_api()
- {
- Dever::config('base')->hook = true;
- //{goods: "2_4_audit_615,2_4_audit_614,-1_-1_audit_617,-1_-1_audit_616,-1_-1_audit_613", audit: "1", audit_text: "1231"}
- $id = Dever::input('order_id');
- $goods = Dever::input('goods');
- $audit = Dever::input('audit');
- $audit_text = Dever::input('audit_text');
- $info = Dever::db('shop/sell_order')->one($id);
- if ($audit > 1 && $info && $info['status'] == 2) {
- $shop = Dever::db('shop/info')->find($info['shop_id']);
- if ($audit == 2) {
- Dever::db('shop/sell_order')->update(array('where_id' => $info['id'], 'audit' => $audit, 'audit_desc' => $audit_text));
- # 成功
- if (strstr($info['order_num'], '_')) {
- $temp = explode('_', $info['order_num']);
- $info['order_num'] = $temp[0];
- $info['order_index'] = $temp[1];
- } else {
- $info['order_index'] = 0;
- }
- $goods = explode(',', $goods);
- $data = $other = array();
- foreach ($goods as $k => $v) {
- list($source_type, $source_id, $name, $goods_id) = explode('_', $v);
- $gv = Dever::db('shop/sell_order_goods')->find($goods_id);
- if ($source_id > 0) {
- $key = $source_type . '_' . $source_id;
- $data[$key][] = array
- (
- 'area' => $shop['area'],
- 'order_id' => $info['id'],
- 'order_type' => 2,
- 'order_num' => $info['order_num'],
- 'order_index' => $info['order_index'],
- 'type' => 1,
- 'type_id' => $shop['id'],
- 'source_type' => $source_type,
- 'source_id' => $source_id,
- 'order_goods_id' => $gv['id'],
- 'goods_id' => $gv['goods_id'],
- 'sku_id' => $gv['sku_id'],
- 'price' => $gv['price'],
- 'p_price' => $gv['price'],
- 'num' => $gv['num'],
- );
- } else {
- $other[] = $gv;
- }
- }
- $this->audit_action($data, $other, $info, $shop, 1);
- } else {
- Dever::db('shop/sell_order')->update(array('where_id' => $info['id'], 'audit' => $audit, 'audit_desc' => $audit_text));
- # 退款
- $shop = Dever::db('shop/info')->find($info['shop_id']);
- Dever::load('shop/lib/refund')->set('sell')->apply(2, $shop['id'], $id, false, 3, 0, '未通过审核');
- }
- }
- return 'refer';
- return Dever::url('project/database/list&project=shop&table=sell_order&menu=shop&search_option_state=1&search_option_shop_type=2&menu_id=218', 'manage');
- }
- # 采购订单审核 拆单
- public function audit_commit()
- {
- $order_id = Dever::input('order_id');
- $this->auditOne($order_id);
- return 'ok';
- }
- # 采购订单审核 拆单
- public function auditOne($order_id)
- {
- $order = Dever::db('shop/sell_order')->find($order_id);
- if (!$order) {
- Dever::alert('订单不存在');
- }
- if ($order && $order['status'] != 2) {
- Dever::alert($order['order_num'] . '当前订单状态无法拆单');
- }
- if ($order['shop_type'] != 2) {
- Dever::alert('这不是平台店的订单');
- }
- $order['type'] = 2;
- $goods = Dever::db('shop/sell_order_goods')->select(array('order_id' => $order_id));
- $shop = Dever::db('shop/info')->find($order['shop_id']);
- $data = Dever::load('mshop/lib/buy')->assign($goods, $order, $shop);
- $this->audit_action($data, $goods, $order, $shop);
- return 'ok';
- }
- private function audit_action($data, $goods, $order, $shop, $state = 2)
- {
- $send = array();
- if (!$goods && $data) {
- # 开始生成订单
- $i = $order['order_index'];
- foreach ($data as $k => $v) {
- foreach ($v as $k1 => $v1) {
- $v1['order_num'] .= '_' . $i;
- //$state = Dever::load('mshop/lib/buy')->createOrder($v1, $order['cdate']);
- $state = Dever::load('mshop/lib/buy')->createOrder($v1);
- $send[$state] = $v1;
- }
- $i++;
- }
- Dever::db('shop/sell_order')->update(array('where_id' => $order['id'], 'status' => 3));
- } else {
- Dever::db('shop/sell_order')->update(array('where_id' => $order['id'], 'audit' => 1));
- if ($goods) {
- $string = array();
- foreach ($goods as $k => $v) {
- $info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
-
- if ($info['sku_name']) {
- $info['name'] .= '('.$info['sku_name'].')';
- }
- $string[] = $info['name'];
- }
- $string = implode('、', $string);
- if ($state == 1) {
- Dever::alert('商品:'.$string.'未选择供货商');
- } else {
- Dever::alert('商品:'.$string.'库存不足或厂家未生产,请联系供货商备货,该订单进入重新审核状态');
- }
- Dever::alert('商品:'.$string.'库存不足或厂家未生产,请联系供货商备货,该订单进入重新审核状态');
- } else {
- if ($state == 1) {
- Dever::alert('未选择供货商,该订单进入重新审核状态');
- } else {
- Dever::alert('供货商库存不足,请联系供货商备货,该订单进入重新审核状态');
- }
-
- }
- }
- if ($send) {
- foreach ($send as $k => $v) {
- Dever::load('mshop/lib/buy')->sendMsg($v['source_type'], $v['source_id'], $k);
- $order_info = Dever::db('shop/buy_order')->find(array('id' => $k, 'clear' => true));
- Dever::load('cash/lib/order')->up($order_info, 1, 1);
- }
- }
- }
- }
|