Sell.php 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386
  1. <?php
  2. # 销售支付
  3. namespace Shop\Lib;
  4. use Dever;
  5. class Sell
  6. {
  7. # 1是用户,2是门店
  8. public $type = 1;
  9. # 1是列表,2是详情
  10. public $view = 1;
  11. # 获取配置
  12. public $config = array();
  13. # table
  14. public $table = 'shop/sell_order';
  15. public function __construct()
  16. {
  17. $this->config = Dever::db($this->table)->config;
  18. }
  19. # 设置订单的类型
  20. public function set($type, $view)
  21. {
  22. $this->type = $type;
  23. $this->view = $view;
  24. return $this;
  25. }
  26. # 获取公共的where
  27. public function where($id)
  28. {
  29. $where = array();
  30. if ($this->type == 1) {
  31. $where['uid'] = $id;
  32. } elseif ($this->type == 2) {
  33. $where['shop_id'] = $id;
  34. }
  35. if (!$where) {
  36. Dever::alert('参数错误');
  37. }
  38. return $where;
  39. }
  40. # 获取订单列表
  41. public function getList($id)
  42. {
  43. $result = array();
  44. $where = $this->where($id);
  45. $mobile = Dever::input('mobile');
  46. if ($mobile) {
  47. $where['mobile'] = $mobile;
  48. }
  49. $status = Dever::input('status');
  50. if ($status) {
  51. $where['status'] = $status;
  52. }
  53. $method = Dever::input('method');
  54. if ($method) {
  55. $where['method'] = $method;
  56. }
  57. $pay_method = Dever::input('pay_method');
  58. if ($pay_method) {
  59. $where['pay_method'] = $pay_method;
  60. }
  61. $result['search_value'] = $where;
  62. $result['search_value']['day'] = $day = Dever::input('day');
  63. if ($day) {
  64. $where['start'] = Dever::maketime($day . ' 00:00:00');
  65. $where['end'] = Dever::maketime($day . ' 23:59:59');
  66. }
  67. $result['order'] = Dever::db($this->table)->getAll($where);
  68. if ($result['order']) {
  69. foreach ($result['order'] as $k => $v) {
  70. $result['order'][$k] = $this->getInfo($v);
  71. }
  72. }
  73. $result['search'] = array();
  74. $result['search']['status'] = array
  75. (
  76. array('value' => 1, 'name' => '待支付'),
  77. array('value' => 2, 'name' => '待处理'),
  78. );
  79. if ($method == 1) {
  80. $result['search']['status'][] = array('value' => 3, 'name' => '待自提');
  81. } elseif ($method == 2) {
  82. $result['search']['status'][] = array('value' => 3, 'name' => '配送中');
  83. } else {
  84. $result['search']['status'][] = array('value' => 3, 'name' => '待自提');
  85. $result['search']['status'][] = array('value' => 3, 'name' => '配送中');
  86. }
  87. $result['search']['status'][] = array('value' => 8, 'name' => '退款');
  88. $result['search']['status'][] = array('value' => '5,6', 'name' => '已完成');
  89. $result['search']['status'][] = array('value' => '7,11', 'name' => '已取消');
  90. $result['search']['method'] = array
  91. (
  92. array('value' => 1, 'name' => '自提'),
  93. array('value' => 2, 'name' => '配送')
  94. );
  95. $result['search']['pay_method'] = array
  96. (
  97. array('value' => 1, 'name' => '平台结算'),
  98. array('value' => 2, 'name' => '门店代下单')
  99. );
  100. return $result;
  101. }
  102. # 查看详情
  103. public function getView($id, $order_id, $show = true)
  104. {
  105. $where = $this->where($id);
  106. $where['id'] = $order_id;
  107. $result = Dever::db($this->table)->find($where);
  108. if (!$result) {
  109. Dever::alert('订单不存在');
  110. }
  111. if ($show) {
  112. $result = $this->getInfo($result);
  113. }
  114. return $result;
  115. }
  116. # 获取订单详细信息
  117. public function getInfo($info)
  118. {
  119. $info['time'] = 0;
  120. if ($info['status'] == 1) {
  121. # 15分钟内支付,900秒
  122. $m = 900;
  123. # 支付倒计时
  124. $info['time'] = time() - $info['cdate'];
  125. if ($info['time'] >= $m) {
  126. # 已过期,自动取消
  127. $info['time'] = -1;
  128. //Dever::db($this->table)->update(array('where_id' => $info['id'], 'status' => 11));
  129. # 还原库存
  130. if ($this->type == 1) {
  131. $this->cancel($info['uid'], $info['id'], 11);
  132. } else {
  133. $this->type = 2;
  134. $this->cancel($info['shop_id'], $info['id'], 11);
  135. }
  136. $info['status'] = 11;
  137. } else {
  138. $info['time'] = $m - $info['time'];
  139. }
  140. }
  141. $info['status_name'] = $this->config['status'][$info['status']];
  142. $info['method_name'] = $this->config['method'][$info['method']];
  143. $info['pay_method_name'] = $this->config['pay_method'][$info['pay_method']];
  144. $info['pay_type_name'] = $this->config['pay_type'][$info['pay_type']];
  145. if (!$info['coupon_cash']) {
  146. $info['coupon_cash'] = 0;
  147. }
  148. if (!$info['discount_cash']) {
  149. $info['discount_cash'] = 0;
  150. }
  151. if (!$info['refund_cash']) {
  152. $info['refund_cash'] = 0;
  153. }
  154. if (!$info['ps_cash']) {
  155. $info['ps_cash'] = 0;
  156. }
  157. if (!$info['ps_cash']) {
  158. $info['ps_cash'] = 0;
  159. }
  160. if (!$info['wallet_cash']) {
  161. $info['wallet_cash'] = 0;
  162. } else {
  163. $info['wallet_cash'] = round($info['wallet_cash'], 2);
  164. }
  165. if (!$info['card_code_cash']) {
  166. $info['card_code_cash'] = 0;
  167. } else {
  168. $info['card_code_cash'] = round($info['card_code_cash'], 2);
  169. }
  170. $tk = Dever::db('shop/sell_order_refund')->find(array('order_id' => $info['id'], 'process' => 2));
  171. if ($tk) {
  172. $info['tk_status'] = 1;
  173. } else {
  174. $info['tk_status'] = 2;
  175. }
  176. $info['card'] = array();
  177. if ($info['card_code_id'] > 0) {
  178. $config = Dever::db('main/sell_config')->find();
  179. $info['card']['name'] = $config['card_name'];
  180. $card_code = Dever::db('card/code')->find($info['card_code_id']);
  181. if ($card_code) {
  182. $card_info = Dever::db('card/info')->find($card_code['card_id']);
  183. if ($card_info) {
  184. $card_type = Dever::db('card/type')->find($card_info['type_id']);
  185. $card = substr($card_code['card'], -4);
  186. $info['card']['name'] .= '('.$card_type['name'].$card.')';
  187. $info['card']['info'] = $card_info;
  188. $info['card']['id'] = $card_code['id'];
  189. $info['card']['card'] = $card_code['card'];
  190. $info['card']['cash'] = $info['card_code_cash'];
  191. }
  192. }
  193. }
  194. if ($this->type == 1) {
  195. if ($info['status'] == 2) {
  196. $info['status_name'] = '支付成功';
  197. }
  198. if ($info['status'] == 3) {
  199. if ($info['method'] == 1) {
  200. $info['status_name'] = '待自提';
  201. } else {
  202. $info['status_name'] = '配送中';
  203. }
  204. }
  205. } elseif ($this->type == 2) {
  206. if ($info['status'] == 2) {
  207. $info['status_name'] = '待处理';
  208. }
  209. if ($info['status'] == 3) {
  210. if ($info['method'] == 1) {
  211. $info['status_name'] = '待自提';
  212. } else {
  213. $info['status_name'] = '配送中';
  214. }
  215. }
  216. }
  217. if ($info['method'] == 2) {
  218. $info['ps_info'] = Dever::db('shop/sell_order_ps')->find(array('order_id' => $info['id']));
  219. if ($info['ps_info']) {
  220. $info['ps_info']['service'] = Dever::array_decode($info['ps_info']['service']);
  221. $info['ps_info']['cdate'] = date('Y-m-d H:i', $info['ps_info']['cdate']);
  222. if ($info['ps_info']['qu_date']) {
  223. $info['ps_info']['qu_date'] = date('Y-m-d H:i', $info['ps_info']['qu_date']);
  224. }
  225. if ($info['ps_info']['qs_date']) {
  226. $info['ps_info']['qs_date'] = date('Y-m-d H:i', $info['ps_info']['qs_date']);
  227. }
  228. $status = Dever::db('shop/sell_order_ps')->config['status'];
  229. $info['ps_info']['status_name'] = $status[$info['ps_info']['status']];
  230. if ($info['ps_info']['service']) {
  231. if (isset($info['ps_info']['service']['serivce'])) {
  232. $info['ps_info']['service'] = $info['ps_info']['service']['service'];
  233. }
  234. foreach ($info['ps_info']['service'] as $k => $v) {
  235. if (isset($v['order_num']) && $v['order_num']) {
  236. $info['ps_info']['service'][$k]['service_name'] = '商家自送';
  237. if ($v['service_id'] > 0) {
  238. $service = Dever::db('shop/service')->find($v['service_id']);
  239. if ($service) {
  240. $info['ps_info']['service'][$k]['service_name'] = $service['name'];
  241. }
  242. }
  243. } else {
  244. unset($info['ps_info']['service'][$k]);
  245. }
  246. }
  247. }
  248. }
  249. }
  250. $info['goods'] = Dever::db('shop/sell_order_goods')->select(array('order_id' => $info['id']));
  251. if ($info['uid'] && $info['uid'] > 0) {
  252. $info['user'] = Dever::load('passport/api')->info($info['uid']);
  253. }
  254. $info['shop'] = Dever::db('shop/info')->getOne($info['shop_id']);
  255. $info['cdate'] = date('Y-m-d H:i', $info['cdate']);
  256. if ($info['operdate']) {
  257. $info['operdate'] = date('Y-m-d H:i', $info['operdate']);
  258. } else {
  259. $info['operdate'] = '';
  260. }
  261. if ($info['fdate']) {
  262. $info['fdate'] = date('Y-m-d H:i', $info['fdate']);
  263. } else {
  264. $info['fdate'] = '';
  265. }
  266. $goods_status = Dever::db('shop/sell_order_goods')->config['status'];
  267. if ($info['address_id']) {
  268. $info['address'] = Dever::db('passport/address')->find($info['address_id']);
  269. }
  270. if ($this->view == 2) {
  271. $check = false;
  272. if (isset($card_info) && $card_info) {
  273. $info['card']['goods'] = array();
  274. $check = false;
  275. $shop = Dever::db('shop/info')->find($info['shop_id']);
  276. if ($shop['type'] == 1 && $card_info['shop_set'] == 1) {
  277. $check = false;
  278. } else {
  279. $goods = Dever::db('card/info_goods')->getData(array('card_id' => $card_info['id']));
  280. if ($goods || $card_info['category']) {
  281. $check = true;
  282. $card_info['category'] = explode(',', $card_info['category']);
  283. if ($goods) {
  284. $goods = array_keys($goods);
  285. }
  286. }
  287. }
  288. }
  289. $info['discount_goods'] = array();
  290. foreach ($info['goods'] as $k => $v) {
  291. $info['goods'][$k]['info'] = Dever::load('goods/lib/info')->getPayInfo($v['goods_id'], $v['sku_id']);
  292. $info['goods'][$k]['status_name'] = $goods_status[$v['status']];
  293. if ($check) {
  294. if ($goods) {
  295. if (in_array($v['goods_id'], $goods)) {
  296. $info['card']['goods'][] = $info['goods'][$k]['info'];
  297. }
  298. } elseif ($card_info['category']) {
  299. if (in_array($info['goods'][$k]['info']['top_category_id'], $card_info['category'])) {
  300. $info['card']['goods'][] = $info['goods'][$k]['info'];
  301. }
  302. }
  303. }
  304. if ($v['discount_id'] && $v['discount_id'] > 0) {
  305. $info['discount_goods'][] = $info['goods'][$k];
  306. }
  307. }
  308. if ($info['invoice_id']) {
  309. $info['invoice'] = Dever::db('passport/invoice')->find($info['invoice_id']);
  310. }
  311. if ($info['user_coupon_id']) {
  312. $coupon = Dever::db('shop/user_coupon')->find($info['user_coupon_id']);
  313. if ($coupon) {
  314. $info['coupon'] = Dever::db('goods/coupon')->find($coupon['coupon_id']);
  315. }
  316. }
  317. } else {
  318. foreach ($info['goods'] as $k => $v) {
  319. $goods = Dever::db('goods/info')->one($v['goods_id']);
  320. $info['goods'][$k]['name'] = $goods['name'];
  321. $info['goods'][$k]['cover'] = $goods['cover'];
  322. $info['goods'][$k]['status_name'] = $goods_status[$v['status']];
  323. }
  324. }
  325. $info = $this->refund_button($info);
  326. return $info;
  327. }
  328. # 是否可以退款
  329. public function refund_button($info)
  330. {
  331. $info['refund_button'] = 1;
  332. # 是否显示退款按钮
  333. $manage_config = Dever::db('main/manage_config')->find();
  334. if (isset($info['shop_id']) && $info['shop_id'] == 3) {
  335. # 平台商城
  336. $info['code'] = '';
  337. $refund_time = $manage_config['shop_refund_day']*86400;
  338. } else {
  339. # 体验店
  340. $refund_time = $manage_config['sell_refund_day']*86400;
  341. }
  342. if ($refund_time > 0 && ($info['status'] == 5 || $info['status'] == 6)) {
  343. $cur = time();
  344. if (strstr($info['fdate'], '-')) {
  345. $info['fdate'] = Dever::maketime($info['fdate']);
  346. }
  347. if ($cur - $info['fdate'] < $refund_time) {
  348. # 可以退款
  349. $info['refund_button'] = 1;
  350. } else {
  351. # 不可以退款
  352. $info['refund_button'] = 2;
  353. }
  354. }
  355. return $info;
  356. }
  357. # 取消订单
  358. public function cancel($id, $order_id, $status = 7)
  359. {
  360. $data = $this->getView($id, $order_id, false);
  361. if ($data['status'] == 1) {
  362. $state = Dever::db('shop/sell_order')->update(array('where_id' => $data['id'], 'status' => $status, 'operdate' => time()));
  363. if ($state) {
  364. if ($data['user_coupon_id']) {
  365. # 还原优惠券
  366. Dever::db('shop/user_coupon')->update(array('where_id' => $data['user_coupon_id'], 'status' => 1));
  367. }
  368. if (isset($data['card_code_id']) && $data['card_code_id'] && $data['card_code_cash'] > 0) {
  369. # 还原礼品卡
  370. Dever::db('card/code')->decUseCash(array('where_id' => $data['card_code_id'], 'set_use_cash' => $data['card_code_cash']));
  371. Dever::load('cash/lib/order')->up($data, 3, 3, false, 2);
  372. } elseif (isset($data['wallet_cash']) && $data['wallet_cash'] && $data['wallet_cash'] > 0) {
  373. # 还原钱包
  374. Dever::load('shop/lib/money')->up($data['uid'], 4, $data['wallet_cash'], $data['id'], '商品退款', 2);
  375. Dever::load('cash/lib/order')->up($data, 3, 3, false, 3);
  376. }
  377. $this->updateSell($data, 2);
  378. }
  379. return 'ok';
  380. } else {
  381. Dever::alert('当前订单状态不允许取消');
  382. }
  383. }
  384. # 获取优惠券
  385. public function coupon(&$data, $type = 1)
  386. {
  387. $user_coupon_id = Dever::input('user_coupon_id');
  388. $shop_coupon_id = Dever::input('shop_coupon_id');
  389. $data['user_coupon_id'] = 0;
  390. $data['coupon_id'] = 0;
  391. $data['coupon_cash'] = 0;
  392. if ($data['pay_method'] == 3) {
  393. return $data;
  394. }
  395. if ($type == 1) {
  396. if (isset($data['uid']) && $data['uid'] > 0) {
  397. # 默认选择优惠券
  398. # 查找符合要求的优惠券
  399. $coupon = Dever::db('shop/user_coupon')->getAll(array('uid' => $data['uid'], 'city' => $data['shop']['city'], 'status' => 1, 'edate' => time()));
  400. if ($coupon) {
  401. foreach ($coupon as $k => $v) {
  402. $coupon_info = Dever::db('goods/coupon')->find($v['coupon_id']);
  403. if ($coupon_info) {
  404. if ($v['shop_id'] != $data['shop']['id'] && $coupon_info['method'] == 3) {
  405. # 门店专属券 不能给别的门店用
  406. continue;
  407. }
  408. $kou = false;
  409. if ($coupon_info['type'] == 1) {
  410. # 满减券
  411. if ($data['price'] >= $coupon_info['total_cash']) {
  412. $kou = true;
  413. }
  414. } else {
  415. $kou = true;
  416. }
  417. if ($kou) {
  418. $coupon_info['user_coupon_id'] = $v['id'];
  419. $coupon_info['uid'] = $v['uid'];
  420. $coupon_info['edate'] = date('Y-m-d', $v['edate']);
  421. $coupon_info['shop_id'] = $v['shop_id'];
  422. $coupon_info['coupon_id'] = $v['coupon_id'];
  423. $coupon_info['city'] = $v['city'];
  424. $coupon_info = Dever::load('shop/lib/coupon')->getShop($coupon_info, $data['shop'], false);
  425. $data['coupon'][] = $coupon_info;
  426. if (!$user_coupon_id && $data['coupon_cash'] <= $coupon_info['cash']) {
  427. $data['user_coupon_id'] = $v['id'];
  428. $data['coupon_id'] = $coupon_info['id'];
  429. $data['coupon_cash'] = $coupon_info['cash'];
  430. }
  431. }
  432. }
  433. }
  434. }
  435. } else {
  436. $coupon = Dever::db('shop/coupon')->getAll(array('shop_id' => $data['shop']['id']));
  437. if ($coupon) {
  438. foreach ($coupon as $k => $v) {
  439. $coupon_info = Dever::db('goods/coupon')->find($v['coupon_id']);
  440. $kou = false;
  441. if ($coupon_info['type'] == 1) {
  442. # 满减券
  443. if ($data['price'] >= $coupon_info['total_cash']) {
  444. $kou = true;
  445. }
  446. } else {
  447. $kou = true;
  448. }
  449. if ($kou) {
  450. $coupon_info['shop_id'] = $v['shop_id'];
  451. $coupon_info['shop_coupon_id'] = $v['id'];
  452. $coupon_info['coupon_id'] = $v['coupon_id'];
  453. $coupon_info['city'] = $v['city'];
  454. $coupon_info = Dever::load('shop/lib/coupon')->getShop($coupon_info, $data['shop'], false);
  455. $data['coupon'][] = $coupon_info;
  456. }
  457. }
  458. }
  459. }
  460. }
  461. if ($user_coupon_id && isset($data['uid']) && $data['uid'] > 0) {
  462. # 用户自己使用优惠券
  463. if ($data['price'] <= 0) {
  464. Dever::alert('付款价格错误');
  465. }
  466. $coupon = Dever::db('shop/user_coupon')->find(array('uid' => $data['uid'], 'id' => $user_coupon_id, 'status' => 1));
  467. if (!$coupon) {
  468. Dever::alert('优惠券不可用');
  469. }
  470. if (time() > $coupon['edate']) {
  471. Dever::db('shop/user_coupon')->update(array('where_id' => $user_coupon_id, 'status' => 3));
  472. Dever::alert('优惠券已过期');
  473. }
  474. $goods_coupon = Dever::db('goods/coupon')->find($coupon['coupon_id']);
  475. if (!$goods_coupon) {
  476. Dever::alert('优惠券不可用');
  477. }
  478. if ($coupon['shop_id'] != $data['shop']['id']) {
  479. if ($goods_coupon['method'] == 3) {
  480. Dever::alert('优惠券不可用');
  481. } else {
  482. $coupon_info = Dever::db('shop/coupon')->find(array('shop_id' => $coupon['shop_id'], 'coupon_id' => $coupon['coupon_id'], 'city' => $coupon['city']));
  483. if (!$coupon_info) {
  484. Dever::alert('优惠券不可用');
  485. }
  486. }
  487. }
  488. if ($goods_coupon['type'] == 2 && $data['price'] < $goods_coupon['total_cash']) {
  489. Dever::alert('优惠券不可用');
  490. }
  491. $data['user_coupon_id'] = $user_coupon_id;
  492. $data['coupon_id'] = $goods_coupon['id'];
  493. $data['coupon_cash'] = $goods_coupon['cash'];
  494. } elseif ($shop_coupon_id) {
  495. # 店里直接给用优惠券
  496. if ($data['price'] <= 0) {
  497. Dever::alert('付款价格错误');
  498. }
  499. $coupon_info = Dever::db('shop/coupon')->find(array('shop_id' => $data['shop']['id'], 'id' => $shop_coupon_id));
  500. if (!$coupon_info) {
  501. Dever::alert('优惠券不可用');
  502. }
  503. $goods_coupon = Dever::db('goods/coupon')->find($coupon_info['coupon_id']);
  504. if (!$goods_coupon) {
  505. Dever::alert('优惠券不可用');
  506. }
  507. if ($goods_coupon['type'] == 2 && $data['price'] < $goods_coupon['total_cash']) {
  508. Dever::alert('优惠券不可用');
  509. }
  510. $data['user_coupon_id'] = -1;
  511. $data['coupon_id'] = $goods_coupon['id'];
  512. $data['coupon_cash'] = $goods_coupon['cash'];
  513. }
  514. if (isset($data['coupon_cash']) && $data['coupon_cash'] > 0) {
  515. # 这里还要计算每个商品的优惠金额
  516. if (isset($data['list']) && $data['list']) {
  517. foreach ($data['list'] as $k => $v) {
  518. $data['list'][$k]['coupon_id'] = $data['coupon_id'];
  519. $data['list'][$k]['user_coupon_id'] = $data['user_coupon_id'];
  520. $data['list'][$k]['coupon_cash'] = round(($v['price'] * $v['buy_num'])/$data['price'], 2) * $data['coupon_cash'];
  521. $data['list'][$k]['cash'] = $v['price'] - $data['list'][$k]['coupon_cash'];
  522. }
  523. }
  524. $data['price'] -= $data['coupon_cash'];
  525. }
  526. # 计算礼品卡
  527. $card = Dever::input('card');
  528. $wallet = Dever::input('wallet');
  529. if ($data['price'] > 0 && isset($data['uid']) && $data['uid'] > 0 && $data['discount_id'] <= 0) {
  530. if ($card) {
  531. # 验证这个礼品卡属于自己
  532. $card_code = Dever::db('card/code')->find($card);
  533. if ($card_code['dh_uid'] == $data['uid']) {
  534. $card_state = Dever::load('card/lib/code')->check($card_code);
  535. if ($card_state == 1) {
  536. if (!$card_code['total_cash']) {
  537. $card_code['total_cash'] = $card_code['info']['value'];
  538. }
  539. if (!$card_code['use_cash']) {
  540. $card_code['use_cash'] = 0;
  541. }
  542. $card_cash = $card_code['total_cash'] - $card_code['use_cash'];
  543. if ($card_cash > 0) {
  544. $data['card'] = array();
  545. $card_goods = array();
  546. $check = false;
  547. $xian = false;
  548. if ($data['shop']['type'] == 1 && $card_code['info']['shop_set'] == 1) {
  549. $check = false;
  550. $xian = false;
  551. } else {
  552. $xian = true;
  553. $goods = Dever::db('card/info_goods')->getData(array('card_id' => $card_code['info']['id']));
  554. if ($goods || $card_code['info']['category']) {
  555. $check = true;
  556. }
  557. }
  558. if ($check) {
  559. $price = 0;
  560. $card_code['info']['category'] = explode(',', $card_code['info']['category']);
  561. if ($goods) {
  562. $goods = array_keys($goods);
  563. }
  564. # 检测当前商品是否符合该卡的限制
  565. foreach ($data['list'] as $k => $v) {
  566. if (!isset($v['cash'])) {
  567. $v['cash'] = $v['price'];
  568. }
  569. if ($goods) {
  570. if (in_array($v['id'], $goods)) {
  571. $price += ($v['cash'] * $v['num']);
  572. $card_goods[] = $v;
  573. }
  574. } elseif ($card_code['info']['category']) {
  575. if (in_array($v['top_category_id'], $card_code['info']['category'])) {
  576. $price += ($v['cash'] * $v['num']);
  577. $card_goods[] = $v;
  578. }
  579. }
  580. }
  581. } else {
  582. $price = $data['price'];
  583. }
  584. if ($card_cash > $price) {
  585. $data['card']['cash'] = $price;
  586. } else {
  587. $data['card']['cash'] = $card_cash;
  588. }
  589. $data['price'] -= $data['card']['cash'];
  590. if ($xian && ($price <= 0 || ($card_code['info']['min'] > 0 && $price < $card_code['info']['min']))) {
  591. # 不满足限额
  592. unset($data['card']);
  593. } else {
  594. $data['card']['info'] = $card_code['info'];
  595. $data['card']['goods'] = $card_goods;
  596. $data['card']['id'] = $card_code['id'];
  597. $data['card']['card'] = $card_code['card'];
  598. $data['card']['cash'] = round($data['card']['cash'], 2);
  599. $config = Dever::db('main/sell_config')->find();
  600. $card_type = Dever::db('card/type')->find($card_code['info']['type_id']);
  601. $data['card']['name'] = $config['card_name'];
  602. $card = substr($card_code['card'], -4);
  603. $data['card']['name'] .= '('.$card_type['name'].$card.')';
  604. }
  605. }
  606. }
  607. }
  608. } elseif ($wallet && $wallet > 0) {
  609. $user = Dever::db('passport/user')->find($data['uid']);
  610. if ($user) {
  611. if ($user['cash'] >= $data['price']) {
  612. $data['wallet_cash'] = $data['price'];
  613. $data['price'] = 0;
  614. } else {
  615. $data['wallet_cash'] = $user['cash'];
  616. $data['price'] = $data['price'] - $user['cash'];
  617. }
  618. }
  619. }
  620. }
  621. if ($data['price'] < 0) {
  622. $data['price'] = 0;
  623. }
  624. $data['price'] = round($data['price'], 2);
  625. return $data;
  626. }
  627. # 获取礼品卡 - 更改商品卡 - 废弃
  628. public function card($uid, $shop)
  629. {
  630. $card = Dever::input('card');
  631. $pwd = Dever::input('pwd');
  632. if (!$card) {
  633. Dever::alert('卡号/密码错误');
  634. }
  635. if (!$pwd) {
  636. Dever::alert('卡号/密码错误');
  637. }
  638. $data = Dever::db('goods/card_code')->find(array('card' => $card, 'pwd' => $pwd));
  639. if (!$data) {
  640. Dever::alert('卡号/密码错误');
  641. }
  642. $info = Dever::db('goods/card')->find($data['card_id']);
  643. if (!$info || ($info && $info['status'] != 1)) {
  644. Dever::alert('卡号/密码错误');
  645. }
  646. if ($data['uid'] && $data['uid'] > 0) {
  647. if ($data['uid'] != $uid) {
  648. Dever::alert('礼品卡已被领取');
  649. }
  650. if (time() > $data['edate']) {
  651. # 更新礼品卡状态
  652. $card_update['where_id'] = $data['id'];
  653. $card_update['status'] = 5;
  654. Dever::db('goods/card_code')->update($card_update);
  655. Dever::alert('礼品卡已失效');
  656. }
  657. } else {
  658. if (time() > $data['edate']) {
  659. # 更新礼品卡状态
  660. $card_update['where_id'] = $data['id'];
  661. $card_update['uid'] = $uid;
  662. $card_update['status'] = 5;
  663. if ($shop) {
  664. $card_update['shop_id'] = $shop['id'];
  665. }
  666. Dever::db('goods/card_code')->update($card_update);
  667. Dever::alert('礼品卡已失效');
  668. } else {
  669. # 更新礼品卡状态
  670. $card_update['where_id'] = $data['id'];
  671. $card_update['status'] = 2;
  672. $card_update['uid'] = $uid;
  673. $card_update['ddate'] = time();
  674. if ($shop) {
  675. $card_update['shop_id'] = $shop['id'];
  676. }
  677. Dever::db('goods/card_code')->update($card_update);
  678. }
  679. }
  680. $data = Dever::db('goods/card_code')->find($data['id']);
  681. $data['goods'] = $info['goods'];
  682. return $data;
  683. }
  684. # 得到商品和总价
  685. public function goods(&$data)
  686. {
  687. # 1自提,2配送
  688. $data['method'] = Dever::input('method', 1);
  689. $data['pay_method'] = Dever::input('pay_method');
  690. if ($data['pay_method'] == 3) {
  691. $data['card'] = $this->card($data['uid'], $data['shop']);
  692. $goods = Dever::array_decode($data['card']['goods']);
  693. $goods_id = array();
  694. $num = array();
  695. $sku_id = array();
  696. foreach ($goods as $k => $v) {
  697. $goods_id[] = $v['goods_id'];
  698. $num[] = $v['num'];
  699. $sku_id[] = -1;
  700. }
  701. } else {
  702. $goods_id = Dever::input('goods_id');
  703. if (!$goods_id) {
  704. Dever::alert('请传入商品');
  705. }
  706. $goods_id = explode(',', $goods_id);
  707. $sku_id = Dever::input('price_id');
  708. if ($sku_id) {
  709. $sku_id = explode(',', $sku_id);
  710. }
  711. $single_id = Dever::input('single_id');
  712. if ($single_id) {
  713. $single_id = explode(',', $single_id);
  714. }
  715. $input_id = Dever::input('input_id');
  716. if ($input_id) {
  717. $input_id = explode(',', $input_id);
  718. }
  719. $input_value = Dever::input('input_value');
  720. if ($input_value) {
  721. $input_value = explode(',', $input_value);
  722. }
  723. $num = Dever::input('num');
  724. if (!$num) {
  725. Dever::alert('请传入商品数量');
  726. }
  727. $num = explode(',', $num);
  728. }
  729. # 钱包金额
  730. $data['wallet_cash'] = 0;
  731. # 配送金额
  732. $data['ps_cash'] = 0;
  733. # 原价
  734. $data['oprice'] = 0;
  735. # 现价
  736. $data['price'] = 0;
  737. $data['num'] = 0;
  738. $data['name'] = array();
  739. $count = count($goods_id);
  740. # 获取角色
  741. $role = $this->getRole($data);
  742. $data['discount_goods'] = array();
  743. $data['discount_cash'] = 0;
  744. $data['discount_id'] = 0;
  745. # 计算总价格
  746. foreach ($goods_id as $k => $v) {
  747. $s = isset($sku_id[$k]) ? $sku_id[$k] : -1;
  748. $n = isset($num[$k]) ? $num[$k] : 1;
  749. $data['list'][$k] = Dever::load('goods/lib/info')->getPayInfo($v, $s);
  750. $w = array();
  751. $w['goods_id'] = $v;
  752. $w['sku_id'] = $s;
  753. $w['shop_id'] = $data['shop']['id'];
  754. $goods_sku = Dever::db('shop/goods_sku')->find($w);
  755. if ($goods_sku) {
  756. if (isset($goods_sku['min'])) {
  757. $data['list'][$k]['min'] = $goods_sku['min'];
  758. }
  759. if (isset($goods_sku['price_id']) && $goods_sku['price_id'] > 0) {
  760. $price_template = Dever::load('price/lib/data')->get($v, $s, $goods_sku['price_id']);
  761. if ($price_template) {
  762. /*
  763. if ($goods_sku['min'] <= 0 && $price_template['price_num'] > 0) {
  764. $data['list'][$k]['min'] = $price_template['price_num'];
  765. }
  766. */
  767. if ($price_template['price_sell'] && $price_template['price_sell'] > 0) {
  768. $data['list'][$k]['price'] = $price_template['price_sell'];
  769. }
  770. }
  771. }
  772. }
  773. $data['list'][$k]['cash'] = $data['list'][$k]['price'];
  774. $data['list'][$k]['num'] = $n;
  775. # 2是库存不足
  776. $data['list'][$k]['ku_state'] = 1;
  777. if ($data['list'][$k]['status'] == 2) {
  778. $total = 0;
  779. } else {
  780. # 验证是否有货
  781. $total = Dever::load('shop/lib/info')->checkTotal($n, $v, $data['shop']['id'], $s);
  782. if (isset($data['list'][$k]['min']) && $data['list'][$k]['min'] > 0) {
  783. if ($total < $data['list'][$k]['min']) {
  784. $total = 0;
  785. }
  786. }
  787. }
  788. if ($total <= 0) {
  789. $data['list'][$k]['ku_state'] = 2;
  790. $data['list'][$k]['buy_num'] = 0;
  791. }
  792. if ($data['list'][$k]['ku_state'] == 1) {
  793. $data['list'][$k]['buy_num'] = $n;
  794. $data['num'] += $n;
  795. # 根据角色设置折扣
  796. if ($role) {
  797. $role_set = false;
  798. foreach ($role as $rk => $rv) {
  799. $discount_goods = Dever::db('act/discount_goods')->getData(array('discount_id' => $rv['id']));
  800. if ($discount_goods) {
  801. $discount_goods = array_keys($discount_goods);
  802. if (in_array($data['list'][$k]['id'], $discount_goods)) {
  803. $role_set = $rv;
  804. break;
  805. }
  806. } elseif ($rv['category'] && in_array($data['list'][$k]['top_category_id'], $rv['category'])) {
  807. $role_set = $rv;
  808. break;
  809. }
  810. }
  811. if ($role_set) {
  812. $discount_cash = round($data['list'][$k]['price'] * ($role_set['num']/10), 2);
  813. $data['list'][$k]['discount_id'] = $role_set['id'];
  814. $data['discount_goods'][] = $data['list'][$k];
  815. $data['discount_id'] = $role_set['id'];
  816. $data['list'][$k]['discount_cash'] = round(($data['list'][$k]['price'] - $discount_cash) * $n, 2);
  817. $data['discount_cash'] += $data['list'][$k]['discount_cash'];
  818. $data['price'] += $discount_cash * $n;
  819. } else {
  820. $data['price'] += $data['list'][$k]['price'] * $n;
  821. }
  822. } else {
  823. $data['price'] += $data['list'][$k]['price'] * $n;
  824. }
  825. $data['oprice'] += $data['list'][$k]['cash'] * $n;
  826. $data['name'][] = $data['list'][$k]['name'];
  827. }
  828. }
  829. $data['name'] = implode(',', $data['name']);
  830. if ($data['method'] == 2) {
  831. $data['ps_cash'] = $data['shop']['ps_cash'];
  832. if ($data['shop']['free_ps_cash'] && $data['shop']['free_ps_cash'] > 0 && $data['price'] >= $data['shop']['free_ps_cash']) {
  833. $data['ps_cash'] = 0;
  834. }
  835. if (!$data['ps_cash']) {
  836. $data['ps_cash'] = 0;
  837. }
  838. }
  839. return $data;
  840. }
  841. public function getRole($data)
  842. {
  843. if(isset($data['uid']) && $data['uid'] > 0) {
  844. return Dever::load('shop/lib/info')->getRole($data['uid']);
  845. }
  846. return false;
  847. }
  848. # 发起支付
  849. 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 = '')
  850. {
  851. if (!$user) {
  852. Dever::alert('错误的用户信息');
  853. }
  854. $uid = $user['id'];
  855. if ($uid > 0) {
  856. $wechat = Dever::db('passport/wechat')->one(array('uid' => $uid, 'type' => 1, 'system_id' => 1));
  857. if (!$wechat) {
  858. //Dever::alert('错误的用户信息');
  859. }
  860. }
  861. if ($method == 2) {
  862. $ps_cash = $shop['ps_cash'];
  863. if ($shop['free_ps_cash'] && $shop['free_ps_cash'] > 0 && $price >= $shop['free_ps_cash']) {
  864. $ps_cash = 0;
  865. }
  866. if (!$ps_cash) {
  867. $ps_cash = 0;
  868. }
  869. $order_data['ps_cash'] = $ps_cash;
  870. $price += $order_data['ps_cash'];
  871. }
  872. $order_data['shop_id'] = $shop['id'];
  873. if ($shop['type'] == 10) {
  874. $order_data['shop_type'] = 2;
  875. } else {
  876. $order_data['shop_type'] = 1;
  877. }
  878. $order_data['area'] = $shop['area'] . ',' . $shop['id'];
  879. $order_data['uid'] = $uid;
  880. $order_data['mobile'] = $user['mobile'];
  881. $order_data['address_id'] = $address_id;
  882. $order_data['invoice_id'] = $invoice_id;
  883. $order_data['name'] = $name;
  884. $order_data['num'] = $num;
  885. $order_data['info'] = $info;
  886. $order_data['oprice'] = $oprice;
  887. $order_data['price'] = $price;
  888. $order_data['kou_cash'] = $cash;
  889. $order_data['method'] = $method;
  890. $order_data['pay_method'] = $pay_method;
  891. $order_data['pay_type'] = $pay_type;
  892. $order_data['refund_cash'] = 0;
  893. if ($card) {
  894. // $order_data['card_id'] = $card['info']['id'];
  895. $order_data['card_code_id'] = $card['id'];
  896. $order_data['card_code_card'] = $card['card'];
  897. $order_data['card_code_cash'] = $card['cash'];
  898. } elseif ($wallet_cash && $wallet_cash > 0) {
  899. $order_data['wallet_cash'] = $wallet_cash;
  900. }
  901. if ($user_coupon_id) {
  902. $order_data['user_coupon_id'] = $user_coupon_id;
  903. }
  904. if ($coupon_id) {
  905. $order_data['coupon_id'] = $coupon_id;
  906. }
  907. if ($coupon_cash) {
  908. $order_data['coupon_cash'] = $coupon_cash;
  909. }
  910. if ($discount_id) {
  911. $order_data['discount_id'] = $discount_id;
  912. }
  913. if ($discount_cash) {
  914. $order_data['discount_cash'] = $discount_cash;
  915. }
  916. $time = time();
  917. $order_data_where = $order_data;
  918. $order_data_where['start'] = $time - 60;
  919. $order_data_where['end'] = $time;
  920. $order_data_where['option']['start'] = array('yes-cdate', '>=');
  921. $order_data_where['option']['end'] = array('yes-cdate', '<=');
  922. $info = Dever::db('shop/sell_order')->find($order_data_where);
  923. $order_data['order_num'] = $this->getOrderId();
  924. if (!$info) {
  925. $id = Dever::db('shop/sell_order')->insert($order_data);
  926. } else {
  927. return array
  928. (
  929. 'order' => '',
  930. 'order_num' => $info['order_num'],
  931. 'order_id' => $info['id'],
  932. 'type' => 'test',
  933. );
  934. }
  935. if (!$id) {
  936. Dever::alert('支付失败');
  937. }
  938. foreach($goods as $k => $v) {
  939. if ($v['ku_state'] == 1) {
  940. $data['uid'] = $uid;
  941. $data['order_id'] = $id;
  942. $data['shop_id'] = $shop['id'];
  943. $data['goods_id'] = $v['id'];
  944. $data['sku_id'] = $v['sku_id'];
  945. $data['price'] = $v['price'];
  946. $data['num'] = $v['buy_num'];
  947. if (isset($v['user_coupon_id']) && $v['user_coupon_id']) {
  948. $data['user_coupon_id'] = $v['user_coupon_id'];
  949. }
  950. if (isset($v['coupon_id']) && $v['coupon_id']) {
  951. $data['coupon_id'] = $v['coupon_id'];
  952. }
  953. if (isset($v['coupon_cash']) && $v['coupon_cash']) {
  954. $data['coupon_cash'] = $v['coupon_cash'];
  955. } else {
  956. $data['coupon_cash'] = 0;
  957. }
  958. if (isset($v['discount_id']) && $v['discount_id']) {
  959. $data['discount_id'] = $v['discount_id'];
  960. }
  961. if (isset($v['discount_cash']) && $v['discount_cash']) {
  962. $data['discount_cash'] = $v['discount_cash'];
  963. }
  964. Dever::db('shop/sell_order_goods')->insert($data);
  965. }
  966. }
  967. # 减少库存 增加销量
  968. $order_data['id'] = $id;
  969. $this->updateSell($order_data);
  970. if ($price <= 0 || $pay_type == 2) {
  971. $param['pay_product_id'] = $id;
  972. $param['pay_order_id'] = $order_data['order_num'];
  973. $param['pay_status'] = 2;
  974. $param['pay_cash'] = $price;
  975. $param['pay_msg'] = '';
  976. $this->success($param);
  977. if ($cart) {
  978. # 来自购物车,要清空一下
  979. $this->dropCart($cart, $uid, $goods, $shop['id']);
  980. }
  981. # 使用优惠券
  982. $this->useCoupon($order_data, $coupon_id, $user_coupon_id, $card, $wallet_cash);
  983. /*
  984. if ($card) {
  985. # 更新礼品卡状态
  986. $card_update['where_id'] = $card['id'];
  987. $card_update['status'] = 3;
  988. $card_update['order_id'] = $id;
  989. $card_update['shop_id'] = $shop['id'];
  990. Dever::db('goods/card_code')->update($card_update);
  991. }*/
  992. return array
  993. (
  994. 'order' => '',
  995. 'order_num' => $order_data['order_num'],
  996. 'order_id' => $id,
  997. 'type' => 'test',
  998. );
  999. }
  1000. $sell_config = Dever::db('main/sell_config')->one();
  1001. # 分账
  1002. $other = false;
  1003. if (isset($shop['mid']) && $shop['mid']) {
  1004. $other = array
  1005. (
  1006. 'mid' => $shop['mid'],
  1007. 'order_id' => $order_data['order_num'] . '_' . $shop['id'],
  1008. 'per' => $sell_config['per'],
  1009. 'oprice' => $oprice,
  1010. );
  1011. }
  1012. $param = array
  1013. (
  1014. 'project_id' => 1,
  1015. 'channel_id' => 1,
  1016. 'system_source' => $system_source,
  1017. 'uid' => $uid,
  1018. 'name' => $order_data['name'],
  1019. 'cash' => $price,
  1020. 'product_id' => $id,
  1021. 'order_id' => $order_data['order_num'],
  1022. 'other' => $other,
  1023. 'refer' => $refer,
  1024. );
  1025. if ($uid > 0 && isset($wechat) && $wechat) {
  1026. $param['openid'] = $wechat['openid'];
  1027. }
  1028. $receipt = Dever::input('receipt');
  1029. if ($receipt) {
  1030. $param['receipt'] = $receipt;
  1031. }
  1032. $result = Dever::load('pay/api.pay', $param);
  1033. //$result['type'] = 'test';
  1034. //$result['order'] = '';
  1035. if ($cart) {
  1036. # 来自购物车,要清空一下
  1037. $this->dropCart($cart, $uid, $goods, $shop['id']);
  1038. }
  1039. # 使用优惠券
  1040. $this->useCoupon($order_data, $coupon_id, $user_coupon_id, $card, $wallet_cash);
  1041. $result['order_num'] = $order_data['order_num'];
  1042. $result['order_id'] = $id;
  1043. return $result;
  1044. }
  1045. public function dropCart($cart, $uid, $goods, $shop_id)
  1046. {
  1047. $where['uid'] = $uid;
  1048. $where['shop_id'] = $shop_id;
  1049. foreach ($goods as $k => $v) {
  1050. $where['goods_id'] = $v['id'];
  1051. if ($cart == 1) {
  1052. Dever::db('shop/cart')->delete($where);
  1053. } else {
  1054. unset($where['uid']);
  1055. Dever::db('shop/shop_cart')->delete($where);
  1056. }
  1057. }
  1058. }
  1059. public function useCoupon($order_data, $coupon_id, $user_coupon_id, $card, $wallet_cash)
  1060. {
  1061. if ($user_coupon_id) {
  1062. Dever::db('shop/user_coupon')->update(array('where_id' => $user_coupon_id, 'status' => 2));
  1063. }
  1064. # 使用礼品卡
  1065. if ($card) {
  1066. $state = Dever::db('card/code')->incUseCash(array('where_id' => $card['id'], 'set_use_cash' => $card['cash']));
  1067. # 生成结算单
  1068. $code = Dever::db('card/code')->find($card['id']);
  1069. if ($code) {
  1070. $card_info = Dever::db('card/info')->find($code['card_id']);
  1071. if ($card_info) {
  1072. if ($card_info['price'] == $card_info['value']) {
  1073. $price = $card['cash'];
  1074. } else {
  1075. $zhe = round($card_info['price']/$card_info['value'], 2);
  1076. $price = round($card['cash']*$zhe, 2);
  1077. }
  1078. $order_data['type'] = 1;
  1079. $order_data['type_id'] = $order_data['shop_id'];
  1080. $order_data['source_type'] = 4;
  1081. $order_data['source_id'] = -1;
  1082. $order_data['price'] = $price;
  1083. $order_data['p_price'] = $card['cash'];
  1084. Dever::load('cash/lib/order')->up($order_data, 3, 1, false, 2);
  1085. }
  1086. }
  1087. } elseif ($wallet_cash && $wallet_cash > 0) {
  1088. //$wallet_cash = -1*$wallet_cash;
  1089. Dever::load('shop/lib/money')->up($order_data['uid'], 12, $wallet_cash, $order_data['id'], '购买商品', 2);
  1090. $order_data['type'] = 1;
  1091. $order_data['type_id'] = $order_data['shop_id'];
  1092. $order_data['source_type'] = 4;
  1093. $order_data['source_id'] = -1;
  1094. $order_data['price'] = $wallet_cash;
  1095. $order_data['p_price'] = $wallet_cash;
  1096. Dever::load('cash/lib/order')->up($order_data, 3, 1, false, 3);
  1097. }
  1098. # 这里可以记录一下使用日志
  1099. }
  1100. # 再次发起支付
  1101. public function rpay($id, $system_source = 5, $refer = '')
  1102. {
  1103. $order = Dever::db('shop/sell_order')->find($id);
  1104. if (!$order) {
  1105. Dever::alert('订单信息错误');
  1106. }
  1107. if ($order['status'] > 1) {
  1108. Dever::alert('订单已支付或已取消');
  1109. }
  1110. $param = array
  1111. (
  1112. 'project_id' => 1,
  1113. 'channel_id' => 1,
  1114. 'system_source' => $system_source,
  1115. 'uid' => $order['uid'],
  1116. 'name' => $order['name'],
  1117. 'cash' => $order['price'],
  1118. 'product_id' => $id,
  1119. 'order_id' => $order['order_num'],
  1120. 'refer' => $refer,
  1121. );
  1122. $receipt = Dever::input('receipt');
  1123. if ($receipt) {
  1124. $param['receipt'] = $receipt;
  1125. }
  1126. $order = $this->getInfo($order);
  1127. $result = Dever::load('pay/api.pay', $param);
  1128. $result['order_time'] = $order['time'];
  1129. $result['order_price'] = $order['price'];
  1130. $result['order_num'] = $order['order_num'];
  1131. $result['order_id'] = $id;
  1132. return $result;
  1133. }
  1134. # 查询是否支付
  1135. public function cpay($id, $system_source = 5, $refer = '')
  1136. {
  1137. $order = Dever::db('shop/sell_order')->find($id);
  1138. if (!$order) {
  1139. Dever::alert('订单信息错误');
  1140. }
  1141. $result['order_num'] = $order['order_num'];
  1142. $result['order_id'] = $id;
  1143. if ($order['status'] > 1 && $order['status'] <= 6) {
  1144. # 已支付
  1145. $result['status'] = 1;
  1146. } else {
  1147. # 待支付
  1148. $result['status'] = 2;
  1149. }
  1150. return $result;
  1151. }
  1152. # 支付成功回调 安全加密 设置token
  1153. public function success_secure_api_token()
  1154. {
  1155. $project_id = Dever::input('pay_project_id');
  1156. $info = Dever::db('pay/project')->one($project_id);
  1157. if ($info) {
  1158. return $info['key'];
  1159. }
  1160. return 'shop_dever_2020';
  1161. }
  1162. # 支付成功回调 安全加密
  1163. public function success_secure_api($param = array())
  1164. {
  1165. $this->success($param);
  1166. }
  1167. # 支付成功回调
  1168. public function success($param = array())
  1169. {
  1170. $send = $param ? $param : Dever::preInput('pay_');
  1171. $product_id = $send['pay_product_id'];
  1172. $order_id = $send['pay_order_id'];
  1173. $status = $send['pay_status'];
  1174. $cash = $send['pay_cash'];
  1175. $msg = $send['pay_msg'];
  1176. $order = Dever::db('shop/sell_order')->one(array('id' => $product_id, 'time' => time()));
  1177. if ($order && $order['status'] == 1) {
  1178. $update = array();
  1179. if ($status == 2) {
  1180. # 生成自提码或者取件码
  1181. $update['code'] = $this->getCode();
  1182. if ($order['method'] == 1) {
  1183. $msg = '您有一件自提商品已订购成功,取件码:' . $update['code'];
  1184. } else {
  1185. $msg = '您有一件外送商品已订购成功,核销码:' . $update['code'];
  1186. # 分配配送信息
  1187. //$this->updatePs($order);
  1188. }
  1189. # 发消息
  1190. if (Dever::project('message') && $order['uid']) {
  1191. $shop = Dever::db('shop/info')->one($order['shop_id']);
  1192. $msg_param['type'] = 1;//消息类型1是订单消息
  1193. $msg_param['id'] = $order['id'];
  1194. $msg_param['name'] = $shop['name'];
  1195. $msg_param = Dever::json_encode($msg_param);
  1196. Dever::project('message') && Dever::load('message/lib/data')->push(-1, $order['uid'], '订购成功通知', $msg, 2, 1, false, $msg_param);
  1197. }
  1198. if ($order['pay_method'] == 2) {
  1199. if ($order['refund_cash'] > 0) {
  1200. $status = 6;
  1201. } else {
  1202. $status = 5;
  1203. }
  1204. $update['fdate'] = time();
  1205. }
  1206. $update['status'] = $status;
  1207. $update['where_id'] = $order['id'];
  1208. $update['paydate'] = time();
  1209. Dever::db('shop/sell_order')->update($update);
  1210. }
  1211. }
  1212. return 'ok';
  1213. }
  1214. # 添加配送信息
  1215. public function updatePs($order, $status = 1, $service_id = -1, $order_num = '', $cdate = false)
  1216. {
  1217. $where['order_id'] = $order['id'];
  1218. $where['clear'] = true;
  1219. $data = Dever::db('shop/sell_order_ps')->find($where);
  1220. $service = array();
  1221. if ($service_id) {
  1222. $service['service_id'] = $service_id;
  1223. }
  1224. if ($order_num) {
  1225. $service['order_num'] = $order_num;
  1226. }
  1227. if (!$data) {
  1228. $insert = $where;
  1229. if ($service) {
  1230. $insert['service'][] = $service;
  1231. $insert['service'] = Dever::array_encode($insert['service']);
  1232. }
  1233. $insert['status'] = $status;
  1234. if ($status == 2) {
  1235. $insert['qu_date'] = time();
  1236. }
  1237. if ($cdate) {
  1238. $insert['cdate'] = $cdate;
  1239. }
  1240. Dever::db('shop/sell_order_ps')->insert($insert);
  1241. } else {
  1242. $update = $where;
  1243. $update['where_id'] = $data['id'];
  1244. $update['status'] = $status;
  1245. if ($status == 2) {
  1246. $update['qu_date'] = time();
  1247. }
  1248. if ($status == 3) {
  1249. $update['qs_date'] = time();
  1250. }
  1251. if ($service) {
  1252. $data['service'] = Dever::array_decode($data['service']);
  1253. $update['service'] = $data['service'];
  1254. $update['service'][] = $service;
  1255. $insert['service'] = Dever::array_encode($update['service']);
  1256. }
  1257. if ($cdate) {
  1258. $update['cdate'] = $cdate;
  1259. }
  1260. Dever::db('shop/sell_order_ps')->update($update);
  1261. }
  1262. }
  1263. # 减少库存,增加销量
  1264. public function updateSell($order, $type = 1)
  1265. {
  1266. $where['order_id'] = $order['id'];
  1267. $where['status'] = 1;
  1268. $data = Dever::db('shop/sell_order_goods')->select($where);
  1269. if ($data) {
  1270. Dever::load('shop/lib/goods')->oper($order, $type, 2, $data);
  1271. }
  1272. }
  1273. # 完成
  1274. public function finish($info, $shop_id)
  1275. {
  1276. if ($info && $info['shop_id'] == $shop_id && $info['status'] <= 4) {
  1277. if ($info['refund_cash'] > 0) {
  1278. $status = 6;
  1279. } else {
  1280. $status = 5;
  1281. }
  1282. $state = Dever::db('shop/sell_order')->update(array('where_id' => $info['id'], 'status' => $status, 'fdate' => time()));
  1283. # 给用户发消息
  1284. if ($info['uid'] && $info['uid'] > 0) {
  1285. $shop = Dever::db('shop/info')->one($info['shop_id']);
  1286. $msg_param['type'] = 1;//消息类型1是订单消息
  1287. $msg_param['id'] = $info['id'];
  1288. $msg_param['name'] = $shop['name'];
  1289. $msg_param = Dever::json_encode($msg_param);
  1290. $msg = '您有一笔订单已签收,祝您用餐愉快~';
  1291. Dever::project('message') && Dever::project('message') && Dever::load('message/lib/data')->push(-1, $info['uid'], '订单完成通知', $msg, 2, 1, false, $msg_param);
  1292. }
  1293. if ($info['method'] == 2) {
  1294. Dever::load('shop/lib/sell')->updatePs($info, 3);
  1295. }
  1296. if ($info['card_code_id'] && $info['card_code_id'] > 0) {
  1297. # 更新礼品卡状态
  1298. $card_update['where_id'] = $info['card_code_id'];
  1299. $card_update['status'] = 4;
  1300. Dever::db('goods/card_code')->update($card_update);
  1301. Dever::load('cash/lib/order')->up($info, 3, 2, false, 2);
  1302. } elseif (isset($info['wallet_cash']) && $info['wallet_cash'] && $info['wallet_cash'] > 0) {
  1303. Dever::load('cash/lib/order')->up($info, 3, 2, false, 3);
  1304. }
  1305. if ($info['shop_type'] == 2) {
  1306. $w['parent_type'] = 2;
  1307. $w['parent_order_id'] = $info['id'];
  1308. $w['status'] = 4;
  1309. $buy_order = Dever::db('shop/buy_order')->select($w);
  1310. if ($buy_order) {
  1311. foreach ($buy_order as $k => $v) {
  1312. Dever::load('mshop/lib/buy')->set(1,1)->finish($shop_id, $v['id']);
  1313. }
  1314. }
  1315. }
  1316. return 'ok';
  1317. } else {
  1318. Dever::alert('您没有权限核销');
  1319. }
  1320. }
  1321. # 平台订单已收货
  1322. public function yes_api()
  1323. {
  1324. $shop_id = Dever::input('shop_id');
  1325. $order_id = Dever::input('order_id');
  1326. $info = Dever::db('shop/sell_order')->find(array('id' => $order_id));
  1327. if ($info && $info['type'] == 10) {
  1328. $this->finish($info, $shop_id);
  1329. } else {
  1330. Dever::alert('您没有权限核销');
  1331. }
  1332. return 'reload';
  1333. }
  1334. # 生成自提码
  1335. public function getCode()
  1336. {
  1337. $where['code'] = Dever::rand(6, 0);
  1338. $state = Dever::db('shop/sell_order')->one($where);
  1339. if (!$state) {
  1340. return $where['code'];
  1341. } else {
  1342. return $this->getCode();
  1343. }
  1344. }
  1345. # 生成订单号
  1346. public function getOrderId()
  1347. {
  1348. $where['order_num'] = Dever::order('G');
  1349. $state = Dever::db('shop/sell_order')->one($where);
  1350. if (!$state) {
  1351. return $where['order_num'];
  1352. } else {
  1353. return $this->getOrderId();
  1354. }
  1355. }
  1356. # 展示订单详情
  1357. public function show()
  1358. {
  1359. $id = Dever::input('order_id');
  1360. $config = Dever::db('shop/sell_order')->config;
  1361. $info = Dever::db('shop/sell_order')->one($id);
  1362. $tk_status = Dever::db('shop/sell_order_refund')->config['status'];
  1363. $tk_process = Dever::db('shop/sell_order_refund')->config['process'];
  1364. $status = $config['status_manage'][$info['status']];
  1365. $tk = Dever::db('shop/sell_order_refund')->find(array('order_id' => $info['id'], 'type' => 1));
  1366. $shop = Dever::db('shop/info')->find($info['shop_id']);
  1367. $user = Dever::db('passport/user')->find($info['uid']);
  1368. $cdate = date('Y-m-d H:i', $info['cdate']);
  1369. if ($info['operdate']) {
  1370. $opertime = date('Y-m-d H:i', $info['operdate']);
  1371. } else {
  1372. $opertime = '';
  1373. }
  1374. if ($info['fdate'] && $info['fdate'] > 0) {
  1375. $fdate = date('Y-m-d H:i', $info['fdate']);
  1376. } else {
  1377. $fdate = '';
  1378. }
  1379. if (!$user) {
  1380. $user['username'] = '无';
  1381. }
  1382. if (!isset($user['truename'])) {
  1383. $user['truename'] = $user['username'];
  1384. }
  1385. if (!$info['coupon_cash']) {
  1386. $info['coupon_cash'] = 0;
  1387. }
  1388. if (!$info['discount_cash']) {
  1389. $info['discount_cash'] = 0;
  1390. }
  1391. if (!$info['kou_cash']) {
  1392. $info['kou_cash'] = 0;
  1393. }
  1394. if (!$info['ps_cash']) {
  1395. $info['ps_cash'] = 0;
  1396. }
  1397. if (!$info['wallet_cash']) {
  1398. $info['wallet_cash'] = 0;
  1399. }
  1400. if (!$info['card_code_cash']) {
  1401. $info['card_code_cash'] = 0;
  1402. }
  1403. if ($info['card_code_card']) {
  1404. $info['card_code_cash'] .= '('.$info['card_code_card'].')';
  1405. }
  1406. if ($tk && $tk['process'] == 1) {
  1407. $status = '申请' . $tk_status[$tk['status']];
  1408. $process = $tk_process[$tk['process']];
  1409. $process = '';
  1410. }
  1411. $time = array();
  1412. $time[] = array('下单时间', $cdate);
  1413. if ($opertime) {
  1414. if ($info['status'] == 2) {
  1415. $time[] = array('通知时间', $opertime);
  1416. }
  1417. if ($info['status'] == 7) {
  1418. $time[] = array('取消时间', $opertime);
  1419. }
  1420. }
  1421. if ($fdate) {
  1422. $time[] = array('完成时间', $fdate);
  1423. }
  1424. $result = array();
  1425. $info['card'] = '';
  1426. if ($info['card_code_id']) {
  1427. $code = Dever::db('card/code')->find($info['card_code_id']);
  1428. if ($code) {
  1429. $card_info = Dever::db('card/info')->find($code['card_id']);
  1430. if ($card_info) {
  1431. if (!$code['total_cash']) {
  1432. $code['total_cash'] = $card_info['value'];
  1433. }
  1434. $info['card'] = '(' . $card_info['name'] .$code['card']. ',面值:'.$code['total_cash'].', 剩余:'.round($code['total_cash']-$code['use_cash'], 2).')';
  1435. }
  1436. }
  1437. }
  1438. $result[$info['order_num']] = array
  1439. (
  1440. 'type' => 'info',
  1441. 'content' => array
  1442. (
  1443. array
  1444. (
  1445. array('订单状态', $status),
  1446. array('支付方式', $config['pay_method'][$info['pay_method']]),
  1447. array('配送方式', $config['method'][$info['method']]),
  1448. ),
  1449. array
  1450. (
  1451. array('门店名称', $shop['name']),
  1452. array('联系人', $shop['truename']),
  1453. array('联系电话', $shop['mobile']),
  1454. ),
  1455. array
  1456. (
  1457. array('下单用户', $user['username']),
  1458. array('联系电话', $info['mobile']),
  1459. array('订单备注', $info['info']),
  1460. ),
  1461. array
  1462. (
  1463. array('支付金额', '¥ ' . round($info['price'], 2)),
  1464. array('优惠券满减', '¥ ' . $info['coupon_cash']),
  1465. array('配送费', '¥ ' . $info['ps_cash']),
  1466. ),
  1467. array
  1468. (
  1469. array('礼品卡抵扣', '¥ ' . round($info['card_code_cash'], 2) . $info['card']),
  1470. array('钱包抵扣', '¥ ' . round($info['wallet_cash'], 2)),
  1471. array('抹零', '¥ ' . round($info['kou_cash'], 2)),
  1472. array('代理优惠', '¥ ' . round($info['discount_cash'], 2)),
  1473. ),
  1474. $time,
  1475. )
  1476. );
  1477. if ($info['address_id'] > 0) {
  1478. $address = Dever::load('passport/address')->getOne($info['uid'], $info['address_id']);
  1479. if ($address) {
  1480. $result['收货地址'] = array
  1481. (
  1482. 'type' => 'info',
  1483. 'content' => array
  1484. (
  1485. array
  1486. (
  1487. array('联系人', $address['contact']),
  1488. array('联系电话', $address['mobile']),
  1489. ),
  1490. array
  1491. (
  1492. array('收货地址', $address['address_info']),
  1493. array('门牌号', $address['house_number']),
  1494. ),
  1495. ),
  1496. );
  1497. }
  1498. }
  1499. if ($info['invoice_id'] > 0) {
  1500. $invoice = Dever::load('passport/invoice')->getOne($info['uid'], $info['invoice_id']);
  1501. if ($invoice) {
  1502. $result['发票信息'] = array
  1503. (
  1504. 'type' => 'info',
  1505. 'content' => array
  1506. (
  1507. array
  1508. (
  1509. array('发票类型', $invoice['title_type_name']),
  1510. array('抬头', $invoice['title']),
  1511. array('税号', $invoice['number']),
  1512. ),
  1513. array
  1514. (
  1515. array('手机号', $invoice['mobile']),
  1516. array('邮箱', $invoice['email']),
  1517. ),
  1518. ),
  1519. );
  1520. }
  1521. }
  1522. $audit = Dever::input('audit');
  1523. if ($audit == 1) {
  1524. # 审核信息
  1525. $this->show_audit($info, $result);
  1526. } else {
  1527. # 配送信息
  1528. $this->show_ps($info, $result);
  1529. # 退款信息
  1530. $this->show_refund($info, $result, $tk, $tk_process);
  1531. # 商品信息
  1532. $this->show_goods($info, $result, $tk_status);
  1533. # 对账信息
  1534. $this->show_dz($info, $result);
  1535. }
  1536. $button = array();
  1537. if (Dever::load('manage/auth')->checkFunc('menu_61.menu_218', 'editaddress', '修改地址')) {
  1538. if ($info['address_id']>0){
  1539. $button[] = array
  1540. (
  1541. 'type' => 'edit',
  1542. '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'),
  1543. 'name' => '修改地址',
  1544. );
  1545. }
  1546. }
  1547. $head = array
  1548. (
  1549. 'name' => '基本信息',
  1550. 'btn' => $button,
  1551. );
  1552. $html = Dever::show($head, $result);
  1553. return $html;
  1554. }
  1555. private function show_goods($info, &$result, $tk_status)
  1556. {
  1557. $body = $refund_body = array();
  1558. $body_total = array();
  1559. $body_total['price'] = 0;
  1560. $body_total['num'] = 0;
  1561. $refund_body_total = array();
  1562. $refund_body_total['price'] = 0;
  1563. $refund_body_total['num'] = 0;
  1564. $goods = Dever::db('shop/sell_order_goods')->select(array('order_id' => $info['id']));
  1565. $goods_status = Dever::db('shop/sell_order_goods')->config['status'];
  1566. foreach ($goods as $k => $v) {
  1567. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  1568. $status = $goods_status[$v['status']];
  1569. $tk = Dever::db('shop/sell_order_refund')->find(array('order_id' => $info['id'], 'order_goods_id' => $v['id'], 'type' => 2));
  1570. if ($tk && $tk['process'] == 1) {
  1571. $status = '申请' . $tk_status[$tk['status']];
  1572. //$status .= '&nbsp;&nbsp;[' . $tk_process[$tk['process']] . ']';
  1573. }
  1574. if (isset($goods_info['sku'])) {
  1575. $sku = $goods_info['sku']['string'];
  1576. } else {
  1577. $sku = '无';
  1578. }
  1579. if ($info['shop_type'] == 2 && $info['status'] > 1 && $info['status'] < 7) {
  1580. $tui = Dever::url('buy_order_tui?shop_id='.$v['shop_id'].'&order_goods_id=' . $v['id'] . '&order_id=' . $info['id'] . '&cash=' . $v['price'], 'shop');
  1581. $info = $this->refund_button($info);
  1582. if ($info['refund_button'] == 1) {
  1583. $status = '<a href="javascript:;" onclick="fastEdit($(this), \''.$tui.'\', \'退款\', \'\', 1)" class="layui-btn">退款</a>';
  1584. }
  1585. }
  1586. $d = array
  1587. (
  1588. 'pic' => $goods_info['cover'],
  1589. 'name' => $goods_info['name'],
  1590. 'sku' => $sku,
  1591. 'price' => $v['price'],
  1592. 'num' => $v['num'] . $goods_info['unit'],
  1593. 'status' => $status,
  1594. );
  1595. if ($goods_info['price_type'] > 2) {
  1596. $d['goods'] = $goods_info['goods'];
  1597. }
  1598. if ($tk && $v['status'] == 3) {
  1599. $d['status'] = $tk_status[$tk['status']];
  1600. $refund_body[] = $d;
  1601. $price = $v['price']*$v['num'];
  1602. $refund_body_total['price'] += $price;
  1603. $refund_body_total['num'] += $v['num'];
  1604. } else {
  1605. $body[] = $d;
  1606. $price = $v['price']*$v['num'];
  1607. $body_total['price'] += $price;
  1608. $body_total['num'] += $v['num'];
  1609. }
  1610. }
  1611. if ($body) {
  1612. if ($refund_body_total['price']) {
  1613. $body_total['set_price'] = $body_total['price'] - $refund_body_total['price'];
  1614. }
  1615. if ($refund_body_total['num']) {
  1616. $body_total['set_num'] = $body_total['num'] - $refund_body_total['num'];
  1617. }
  1618. $result['商品清单'] = array
  1619. (
  1620. 'type' => 'list',
  1621. 'content' => $body,
  1622. 'total' => $body_total,
  1623. );
  1624. }
  1625. if ($refund_body) {
  1626. $result['退款商品'] = array
  1627. (
  1628. 'type' => 'list',
  1629. 'content' => $refund_body,
  1630. 'total' => $refund_body_total,
  1631. );
  1632. }
  1633. }
  1634. private function show_dz($info, &$result)
  1635. {
  1636. $search_option_shop_type = Dever::input('search_option_shop_type');
  1637. if ($search_option_shop_type == 2) {
  1638. # 平台门店采购单
  1639. $buy_order = Dever::db('shop/buy_order')->getDataToId(array('type' => 1, 'type_id' => $info['shop_id'], 'parent_type' => 2, 'parent_order_id' => $info['id']));
  1640. $source_order_id = array();
  1641. if ($buy_order) {
  1642. $source_order_id = array_keys($buy_order);
  1643. }
  1644. # 平台门店结算单
  1645. if ($source_order_id) {
  1646. $cash_order = Dever::db('cash/order')->getDataByOrderId(array('source_order_id' => $source_order_id));
  1647. $col = 'cash';
  1648. }
  1649. }
  1650. if (isset($cash_order) && $cash_order) {
  1651. $head = array('序号', '结算单号', '数量', '金额', '结算类型', '入账状态', '审核状态', '结算时间', '审核备注', '管理');
  1652. $body = array();
  1653. $config = Dever::db('cash/order')->config;
  1654. foreach ($cash_order as $k => $v) {
  1655. $cdate = date('Y-m-d H:i', $v['cdate']);
  1656. $fdate = $v['fdate'] ? date('Y-m-d H:i', $v['fdate']) : '';
  1657. $operdate = $v['operdate'] ? date('Y-m-d H:i', $v['operdate']) : '';
  1658. $cash = $v[$col];
  1659. $url = Dever::url('project/database/list?project=shop&table=buy_order_goods&order_id='.$v['source_order_id'].'&page_type=1', 'manage');
  1660. $v['source_order_num'] = '<a href="'.$url.'" style="color:blue">'.$v['source_order_num'].'</a>';
  1661. $m = '';
  1662. if ($v['audit'] == 1 && $v['audit_type'] == 2) {
  1663. $url = Dever::url('project/database/update?project=cash&table=order&where_id='.$v['id'].'&col=audit,desc', 'manage');
  1664. $m = '<a href="javascript:;" onclick="fastEdit($(this), \''.$url.'\', \'审核\', \'\', 1)" class="layui-btn">审核</a>';
  1665. }
  1666. $d = array
  1667. (
  1668. $k+1,
  1669. $v['order_num'],
  1670. $v['num'],
  1671. $cash,
  1672. $config['config_jstype'][$v['jstype']],
  1673. $config['config_status'][$v['status']],
  1674. $config['config_audit'][$v['audit']],
  1675. $operdate,
  1676. $v['desc'],
  1677. $m,
  1678. );
  1679. $body[] = $d;
  1680. }
  1681. $result['对账清单'] = array
  1682. (
  1683. 'type' => 'table',
  1684. 'content' => array
  1685. (
  1686. 'head' => $head,
  1687. 'body' => $body,
  1688. )
  1689. );
  1690. }
  1691. }
  1692. private function show_refund($info, &$result, $tk, $tk_process)
  1693. {
  1694. if (!$info['refund_cash']) {
  1695. $info['refund_cash'] = 0;
  1696. }
  1697. if ($info['refund_cash'] > 0 && $tk) {
  1698. $process = $tk_process[$tk['process']];
  1699. $tk['cdate'] = date('Y-m-d H:i', $tk['cdate']);
  1700. $result['退款信息'] = array
  1701. (
  1702. 'type' => 'info',
  1703. 'content' => array
  1704. (
  1705. array
  1706. (
  1707. array('退款金额', $info['refund_cash']),
  1708. array('数量', $info['num']),
  1709. array('退款进度', $process),
  1710. ),
  1711. array
  1712. (
  1713. array('申请时间', $tk['cdate']),
  1714. array('退款原因', $tk['desc']),
  1715. ),
  1716. ),
  1717. );
  1718. }
  1719. }
  1720. private function show_ps($info, &$result)
  1721. {
  1722. $info['ps_info'] = Dever::db('shop/sell_order_ps')->find(array('order_id' => $info['id']));
  1723. if ($info['ps_info']) {
  1724. $info['ps_info']['service'] = Dever::array_decode($info['ps_info']['service']);
  1725. $info['ps_info']['cdate'] = date('Y-m-d H:i', $info['ps_info']['cdate']);
  1726. if ($info['ps_info']['qs_date']) {
  1727. $info['ps_info']['qs_date'] = date('Y-m-d H:i', $info['ps_info']['qs_date']);
  1728. } else {
  1729. $info['ps_info']['qs_date'] = '无';
  1730. }
  1731. $status = Dever::db('shop/sell_order_ps')->config['status'];
  1732. $info['ps_info']['status_name'] = $status[$info['ps_info']['status']];
  1733. $service = array();
  1734. if ($info['ps_info']['service']) {
  1735. if (isset($info['ps_info']['service']['serivce'])) {
  1736. $info['ps_info']['service'] = $info['ps_info']['service']['service'];
  1737. }
  1738. foreach ($info['ps_info']['service'] as $k => $v) {
  1739. if (isset($v['order_num']) && $v['order_num']) {
  1740. $info['ps_info']['service'][$k]['service_name'] = '';
  1741. if ($v['service_id'] > 0) {
  1742. $service_info = Dever::db('shop/service')->find($v['service_id']);
  1743. if ($service_info) {
  1744. $info['ps_info']['service'][$k]['service_name'] = $service_info['name'];
  1745. }
  1746. }
  1747. $service[] = array
  1748. (
  1749. array('名称', $info['ps_info']['service'][$k]['service_name']),
  1750. array('单号', isset($v['order_num']) ? $v['order_num'] : ''),
  1751. );
  1752. }
  1753. }
  1754. }
  1755. $result['物流信息'] = array
  1756. (
  1757. 'type' => 'info',
  1758. 'content' => array
  1759. (
  1760. array
  1761. (
  1762. array('发货时间', $info['ps_info']['cdate']),
  1763. array('收货时间', $info['ps_info']['qs_date']),
  1764. array('查单', '<a href="https://www.kuaidi100.com/?from=openv" target="_blank">点此手动查单</a>'),
  1765. ),
  1766. ),
  1767. );
  1768. $result['物流信息']['content'] = array_merge($result['物流信息']['content'], $service);
  1769. }
  1770. }
  1771. private function show_audit($info, &$result)
  1772. {
  1773. $info['type'] = 1;
  1774. $goods = Dever::db('shop/sell_order_goods')->select(array('order_id' => $info['id'], 'status' => 1));
  1775. $shop = Dever::db('shop/info')->find($info['shop_id']);
  1776. list($data, $source) = Dever::load('mshop/lib/buy')->assign($goods, $info, $shop, true);
  1777. if ($info) {
  1778. $head = array('序号', '商品编号', '商品名称', '供货商', '订货总数量', '商品计量单位', '订货单总金额');
  1779. $body = array();
  1780. $i = 0;
  1781. if ($data) {
  1782. foreach ($data as $k => $v) {
  1783. foreach ($v as $v1) {
  1784. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v1['goods_id'], $v1['sku_id']);
  1785. if (isset($goods_info['sku'])) {
  1786. $sku = '[' . $goods_info['sku']['string'] . ']';
  1787. } else {
  1788. $sku = '';
  1789. }
  1790. $d = array
  1791. (
  1792. $i+1,
  1793. $goods_info['code'],
  1794. $goods_info['name'] . $sku,
  1795. Dever::load('mshop/lib/buy')->getSelect($source, $v1['source_id'], $v1['order_goods_id']),
  1796. $v1['num'],
  1797. $goods_info['unit'],
  1798. $v1['price'] * $v1['num'],
  1799. );
  1800. $body[] = $d;
  1801. $i++;
  1802. }
  1803. }
  1804. }
  1805. if ($goods) {
  1806. foreach ($goods as $k => $v) {
  1807. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  1808. if (isset($goods_info['sku'])) {
  1809. $sku = '[' . $goods_info['sku']['string'] . ']';
  1810. } else {
  1811. $sku = '';
  1812. }
  1813. $d = array
  1814. (
  1815. $i+1,
  1816. $goods_info['code'],
  1817. $goods_info['name'] . $sku,
  1818. Dever::load('mshop/lib/buy')->getSelect($source, false, $v['id']),
  1819. $v['num'],
  1820. $goods_info['unit'],
  1821. $v['price'] * $v['num'],
  1822. );
  1823. $body[] = $d;
  1824. $i++;
  1825. }
  1826. }
  1827. $btn = array();
  1828. $btn[] = array(
  1829. 'type' => 'click',
  1830. 'content' => 'setAudit()',
  1831. 'name' => '全部调整<script>function setAudit(){layer.confirm(\'确定进行此项操作吗?\', function(){var audit = $(\'.audit_set\').val();$(\'.audit\').each(function(){$(this).val(audit);layui.form.render();layer.closeAll()})})}</script>'
  1832. );
  1833. $result['商品拆单'] = array
  1834. (
  1835. 'type' => 'table',
  1836. 'head' => array('name' => '供货商:' . Dever::load('mshop/lib/buy')->getSelect($source, false, false), 'btn' => $btn),
  1837. 'content' => array
  1838. (
  1839. 'head' => $head,
  1840. 'body' => $body,
  1841. )
  1842. );
  1843. $url = Dever::url('lib/sell.audit_set_commit?json=1&order_id=' . $info['id'], 'shop');
  1844. $result['审核结果'] = array
  1845. (
  1846. 'type' => 'html',
  1847. '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"> 审核通过 &nbsp;&nbsp;<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"> 审核未通过 &nbsp;&nbsp;</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>',
  1848. );
  1849. }
  1850. }
  1851. # 第三方确认
  1852. public function tixian()
  1853. {
  1854. $param = array();
  1855. ksort($param);
  1856. $string = array();
  1857. foreach ($param as $k => $v) {
  1858. if (strstr($v, '&') || strstr($v, '@')) {
  1859. $v = urlencode($v);
  1860. }
  1861. $string[] = $k . '=' . $v;
  1862. }
  1863. }
  1864. # 发货
  1865. public function notice($info)
  1866. {
  1867. $config = Dever::db('shop/sell_order')->config;
  1868. $state = Dever::db('shop/sell_order')->update(array('where_id' => $info['id'], 'status' => 3, 'operdate' => time(),'fadate'=>time()));
  1869. # 给用户发消息
  1870. if ($info['uid'] && $info['uid'] > 0) {
  1871. $shop = Dever::db('shop/info')->one($info['shop_id']);
  1872. $msg_param['type'] = 1;//消息类型1是订单消息
  1873. $msg_param['id'] = $info['id'];
  1874. $msg_param['name'] = $shop['name'];
  1875. $msg_param = Dever::json_encode($msg_param);
  1876. if ($info['method'] == 1) {
  1877. $msg = '您有一件自提商品已完成配货,请尽快到指定门店使用取件码取货,取件码:' . $info['code'];
  1878. $name = '取货通知';
  1879. $wechat_msg = '您的商品已完成配货';
  1880. } else {
  1881. $msg = '您有一件外送商品已开始配送,收货时请将取件码提供给配送员核实货品信息,取件码:' . $info['code'];
  1882. $name = '配送通知';
  1883. $wechat_msg = '您的商品已开始配送';
  1884. }
  1885. Dever::project('message') && Dever::load('message/lib/data')->push(-1, $info['uid'], $name, $msg, 2, 1, false, $msg_param);
  1886. if (Dever::project('wechat_applet')) {
  1887. $user = Dever::db('passport/wechat')->one(array('uid' => $info['uid'], 'system_id' => 1, 'system_source' => 5));
  1888. if ($user && $user['openid']) {
  1889. $info['status_name'] = $config['status'][$info['status']];
  1890. $send = array
  1891. (
  1892. 'character_string6' => array
  1893. (
  1894. 'value' => $info['order_num'],
  1895. ),
  1896. 'phrase7' => array
  1897. (
  1898. 'value' => $info['status_name'],
  1899. ),
  1900. 'thing8' => array
  1901. (
  1902. 'value' => $info['code'],
  1903. ),
  1904. 'amount1' => array
  1905. (
  1906. 'value' => $info['price'] . '元',
  1907. ),
  1908. 'thing5' => array
  1909. (
  1910. 'value' => $wechat_msg,
  1911. ),
  1912. );
  1913. $send = Dever::json_encode($send);
  1914. Dever::load('wechat_applet/subscribe')->sendOne('order_process', 1, $user['openid'], 'pages/app/order/order?id=' . $info['id'], $send, Dever::config('base')->wechat_applet);
  1915. }
  1916. }
  1917. }
  1918. }
  1919. # 采购订单审核 手动拆单
  1920. public function audit_set_commit_api()
  1921. {
  1922. Dever::config('base')->hook = true;
  1923. //{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"}
  1924. $id = Dever::input('order_id');
  1925. $goods = Dever::input('goods');
  1926. $audit = Dever::input('audit');
  1927. $audit_text = Dever::input('audit_text');
  1928. $info = Dever::db('shop/sell_order')->one($id);
  1929. if ($audit > 1 && $info && $info['status'] == 2) {
  1930. $shop = Dever::db('shop/info')->find($info['shop_id']);
  1931. if ($audit == 2) {
  1932. Dever::db('shop/sell_order')->update(array('where_id' => $info['id'], 'audit' => $audit, 'audit_desc' => $audit_text));
  1933. # 成功
  1934. if (strstr($info['order_num'], '_')) {
  1935. $temp = explode('_', $info['order_num']);
  1936. $info['order_num'] = $temp[0];
  1937. $info['order_index'] = $temp[1];
  1938. } else {
  1939. $info['order_index'] = 0;
  1940. }
  1941. $goods = explode(',', $goods);
  1942. $data = $other = array();
  1943. foreach ($goods as $k => $v) {
  1944. list($source_type, $source_id, $name, $goods_id) = explode('_', $v);
  1945. $gv = Dever::db('shop/sell_order_goods')->find($goods_id);
  1946. if ($source_id > 0) {
  1947. $key = $source_type . '_' . $source_id;
  1948. $data[$key][] = array
  1949. (
  1950. 'area' => $shop['area'],
  1951. 'order_id' => $info['id'],
  1952. 'order_type' => 2,
  1953. 'order_num' => $info['order_num'],
  1954. 'order_index' => $info['order_index'],
  1955. 'type' => 1,
  1956. 'type_id' => $shop['id'],
  1957. 'source_type' => $source_type,
  1958. 'source_id' => $source_id,
  1959. 'order_goods_id' => $gv['id'],
  1960. 'goods_id' => $gv['goods_id'],
  1961. 'sku_id' => $gv['sku_id'],
  1962. 'price' => $gv['price'],
  1963. 'p_price' => $gv['price'],
  1964. 'num' => $gv['num'],
  1965. );
  1966. } else {
  1967. $other[] = $gv;
  1968. }
  1969. }
  1970. $this->audit_action($data, $other, $info, $shop, 1);
  1971. } else {
  1972. Dever::db('shop/sell_order')->update(array('where_id' => $info['id'], 'audit' => $audit, 'audit_desc' => $audit_text));
  1973. # 退款
  1974. $shop = Dever::db('shop/info')->find($info['shop_id']);
  1975. Dever::load('shop/lib/refund')->set('sell')->apply(2, $shop['id'], $id, false, 3, 0, '未通过审核');
  1976. }
  1977. }
  1978. return 'refer';
  1979. 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');
  1980. }
  1981. # 采购订单审核 拆单
  1982. public function audit_commit()
  1983. {
  1984. $order_id = Dever::input('order_id');
  1985. $this->auditOne($order_id);
  1986. return 'ok';
  1987. }
  1988. # 采购订单审核 拆单
  1989. public function auditOne($order_id)
  1990. {
  1991. $order = Dever::db('shop/sell_order')->find($order_id);
  1992. if (!$order) {
  1993. Dever::alert('订单不存在');
  1994. }
  1995. if ($order && $order['status'] != 2) {
  1996. Dever::alert($order['order_num'] . '当前订单状态无法拆单');
  1997. }
  1998. if ($order['shop_type'] != 2) {
  1999. Dever::alert('这不是平台店的订单');
  2000. }
  2001. $order['type'] = 2;
  2002. $goods = Dever::db('shop/sell_order_goods')->select(array('order_id' => $order_id));
  2003. $shop = Dever::db('shop/info')->find($order['shop_id']);
  2004. $data = Dever::load('mshop/lib/buy')->assign($goods, $order, $shop);
  2005. $this->audit_action($data, $goods, $order, $shop);
  2006. return 'ok';
  2007. }
  2008. private function audit_action($data, $goods, $order, $shop, $state = 2)
  2009. {
  2010. $send = array();
  2011. if (!$goods && $data) {
  2012. # 开始生成订单
  2013. $i = $order['order_index'];
  2014. foreach ($data as $k => $v) {
  2015. foreach ($v as $k1 => $v1) {
  2016. $v1['order_num'] .= '_' . $i;
  2017. //$state = Dever::load('mshop/lib/buy')->createOrder($v1, $order['cdate']);
  2018. $state = Dever::load('mshop/lib/buy')->createOrder($v1);
  2019. $send[$state] = $v1;
  2020. }
  2021. $i++;
  2022. }
  2023. Dever::db('shop/sell_order')->update(array('where_id' => $order['id'], 'status' => 3));
  2024. } else {
  2025. Dever::db('shop/sell_order')->update(array('where_id' => $order['id'], 'audit' => 1));
  2026. if ($goods) {
  2027. $string = array();
  2028. foreach ($goods as $k => $v) {
  2029. $info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  2030. if ($info['sku_name']) {
  2031. $info['name'] .= '('.$info['sku_name'].')';
  2032. }
  2033. $string[] = $info['name'];
  2034. }
  2035. $string = implode('、', $string);
  2036. if ($state == 1) {
  2037. Dever::alert('商品:'.$string.'未选择供货商');
  2038. } else {
  2039. Dever::alert('商品:'.$string.'库存不足或厂家未生产,请联系供货商备货,该订单进入重新审核状态');
  2040. }
  2041. Dever::alert('商品:'.$string.'库存不足或厂家未生产,请联系供货商备货,该订单进入重新审核状态');
  2042. } else {
  2043. if ($state == 1) {
  2044. Dever::alert('未选择供货商,该订单进入重新审核状态');
  2045. } else {
  2046. Dever::alert('供货商库存不足,请联系供货商备货,该订单进入重新审核状态');
  2047. }
  2048. }
  2049. }
  2050. if ($send) {
  2051. foreach ($send as $k => $v) {
  2052. Dever::load('mshop/lib/buy')->sendMsg($v['source_type'], $v['source_id'], $k);
  2053. $order_info = Dever::db('shop/buy_order')->find(array('id' => $k, 'clear' => true));
  2054. Dever::load('cash/lib/order')->up($order_info, 1, 1);
  2055. }
  2056. }
  2057. }
  2058. }