Sell.php 89 KB

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