Data.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. <?php
  2. namespace Mshop\Src;
  3. use Dever;
  4. use Mshop\Lib\Core;
  5. class Data extends Core
  6. {
  7. public function home()
  8. {
  9. $day = Dever::input('day');
  10. if (!$day) {
  11. $day = date('Y-m-d');
  12. }
  13. $where['start'] = Dever::maketime($day . ' 00:00:00');
  14. $where['end'] = Dever::maketime($day . ' 23:59:59');
  15. $where['shop_id'] = $this->shop_id;
  16. $where['status'] = '1,2,3,4';
  17. $this->data['num_order'] = round(Dever::db('shop/sell_order')->getOrderNum($where), 2);
  18. $cash = Dever::db('shop/sell_order')->getCashNum($where);
  19. if ($cash) {
  20. $this->data['num_cash'] = $cash['total'];
  21. }
  22. if (!$this->data['num_cash']) {
  23. $this->data['num_cash'] = 0;
  24. }
  25. $goods = Dever::db('shop/sell_order')->getGoodsNum($where);
  26. if ($goods) {
  27. $this->data['num_goods'] = $goods['total'];
  28. }
  29. if (!$this->data['num_goods']) {
  30. $this->data['num_goods'] = 0;
  31. }
  32. $this->data['num_cash'] = round($this->data['num_cash'], 2);
  33. $this->data['num_goods'] = round($this->data['num_goods'], 2);
  34. return $this->data;
  35. }
  36. # 销售订单列表
  37. public function sell_order()
  38. {
  39. return Dever::load('shop/lib/sell')->set(2, 1)->getList($this->shop_id);
  40. }
  41. # 查看订单详情
  42. public function order_view()
  43. {
  44. $order_id = Dever::input('order_id');
  45. return Dever::load('shop/lib/sell')->set(2, 2)->getView($this->shop_id, $order_id);
  46. }
  47. # 通知用户取件或者配送
  48. public function notice()
  49. {
  50. $order_id = Dever::input('order_id');
  51. $info = Dever::db('shop/sell_order')->find(array('id' => $order_id));
  52. if ($info && $info['shop_id'] == $this->shop_id && $info['status'] < 3) {
  53. $state = Dever::db('shop/sell_order')->update(array('where_id' => $info['id'], 'status' => 3, 'operdate' => time()));
  54. # 给用户发消息
  55. if ($info['uid'] && $info['uid'] > 0) {
  56. $shop = Dever::db('shop/info')->one($info['shop_id']);
  57. $msg_param['type'] = 1;//消息类型1是订单消息
  58. $msg_param['id'] = $info['id'];
  59. $msg_param['name'] = $shop['name'];
  60. $msg_param = Dever::json_encode($msg_param);
  61. if ($info['method'] == 1) {
  62. $msg = '您有一件自提商品已完成配货,请尽快到指定门店使用取件码取货,取件码:' . $info['code'];
  63. $name = '取货通知';
  64. } else {
  65. $msg = '您有一件外送商品已开始配送,收货时请将取件码提供给配送员核实货品信息,取件码::' . $info['code'];
  66. $name = '配送通知';
  67. }
  68. Dever::load('message/lib/data')->push(-1, $info['uid'], $name, $msg, 1, 1, false, $msg_param);
  69. }
  70. if ($info['method'] == 2) {
  71. Dever::load('shop/lib/sell')->updatePs($info, 2);
  72. }
  73. return 'ok';
  74. } else {
  75. Dever::alert('您没有权限');
  76. }
  77. }
  78. # 核销取件码,完成
  79. public function finish_commit()
  80. {
  81. $code = Dever::input('code');
  82. if (!$code) {
  83. Dever::alert('请输入自提码');
  84. }
  85. $order_id = Dever::input('order_id');
  86. $info = Dever::db('shop/sell_order')->find(array('code' => $code, 'id' => $order_id));
  87. if ($info && $info['shop_id'] == $this->shop_id && $info['status'] <= 4) {
  88. if ($info['refund_cash'] > 0) {
  89. $status = 6;
  90. } else {
  91. $status = 5;
  92. }
  93. $state = Dever::db('shop/sell_order')->update(array('where_id' => $info['id'], 'status' => $status, 'fdate' => time()));
  94. # 给用户发消息
  95. if ($info['uid'] && $info['uid'] > 0) {
  96. $shop = Dever::db('shop/info')->one($info['shop_id']);
  97. $msg_param['type'] = 1;//消息类型1是订单消息
  98. $msg_param['id'] = $info['id'];
  99. $msg_param['name'] = $shop['name'];
  100. $msg_param = Dever::json_encode($msg_param);
  101. $msg = '您有一笔订单已签收,祝您用餐愉快~';
  102. Dever::load('message/lib/data')->push(-1, $info['uid'], '订单完成通知', $msg, 1, 1, false, $msg_param);
  103. }
  104. if ($info['method'] == 2) {
  105. Dever::load('shop/lib/sell')->updatePs($info, 3);
  106. }
  107. return 'ok';
  108. } else {
  109. Dever::alert('您没有权限核销');
  110. }
  111. }
  112. # 获取订单退款记录
  113. public function order_tui_log()
  114. {
  115. $order_id = Dever::input('order_id');
  116. return Dever::load('shop/lib/refund')->set('sell')->getList(2, $this->shop_id, $order_id);
  117. }
  118. # 门店全部退款
  119. public function order_tui_commit()
  120. {
  121. $order_id = Dever::input('order_id');
  122. $status = Dever::input('status');
  123. $desc = Dever::input('desc');
  124. return Dever::load('shop/lib/refund')->set('sell')->apply(2, $this->shop_id, $order_id, false, $status, 0, $desc);
  125. }
  126. # 门店部分退款信息
  127. public function order_tui_one_info()
  128. {
  129. $order_id = Dever::input('order_id');
  130. $order_goods_id = Dever::input('order_goods_id');
  131. return Dever::load('shop/lib/refund')->set('sell')->getInfo(2, $this->shop_id, $order_id, $order_goods_id);
  132. }
  133. # 门店部分退款
  134. public function order_tui_one_commit()
  135. {
  136. $order_id = Dever::input('order_id');
  137. $order_goods_id = Dever::input('order_goods_id');
  138. $num = Dever::input('num');
  139. $status = Dever::input('status');
  140. $desc = Dever::input('desc');
  141. return Dever::load('shop/lib/refund')->set('sell')->apply(2, $this->shop_id, $order_id, $order_goods_id, $status, $num, $desc);
  142. }
  143. # 根据店铺获取商品列表
  144. public function getGoods()
  145. {
  146. return Dever::load('shop/lib/goods')->getList();
  147. }
  148. # 获取店铺的优惠券
  149. public function getCoupon()
  150. {
  151. $this->data = Dever::db('shop/coupon')->getAllPage(array('shop_id' => $this->shop_id));
  152. if ($this->data) {
  153. $time = time();
  154. foreach ($this->data as $k => $v) {
  155. $this->data[$k]['info'] = Dever::db('goods/coupon')->find($v['coupon_id']);
  156. $this->data[$k]['shop'] = array();
  157. $shop_num = Dever::db('shop/coupon')->total(array('coupon_id' => $v['coupon_id'], 'city' => $v['city']));
  158. $shop = Dever::db('shop/info')->getOne($v['shop_id']);
  159. if ($this->data[$k]['info']['method'] == 1) {
  160. $this->data[$k]['shop_name'] = '平台通用';
  161. } elseif ($this->data[$k]['info']['method'] == 2) {
  162. $this->data[$k]['shop_name'] = $shop['name'];
  163. if ($shop_num > 1) {
  164. $this->data[$k]['shop_name'] .= '多店通用';
  165. }
  166. } elseif ($this->data[$k]['info']['method'] == 3) {
  167. $this->data[$k]['shop_name'] = $shop['name'];
  168. }
  169. }
  170. }
  171. return $this->data;
  172. }
  173. # 赠送优惠券
  174. public function giveCoupon()
  175. {
  176. $coupon_id = Dever::input('coupon_id');
  177. $coupon = explode(',', $coupon_id);
  178. $num = Dever::input('num');
  179. $coupon_num = explode(',', $num);
  180. $order_id = Dever::input('order_id');
  181. $info = Dever::db('shop/sell_order')->find(array('shop_id' => $this->shop_id, 'id' => $order_id));
  182. if ($info['uid'] && $info['uid'] > 0) {
  183. foreach ($coupon as $k => $v) {
  184. $num = isset($coupon_num[$k]) ? $coupon_num[$k] : 1;
  185. Dever::load('shop/lib/coupon')->getOne($info['uid'], $this->shop, $v, 1, $num, false);
  186. }
  187. }
  188. return 'ok';
  189. }
  190. # 获取购物车的数据
  191. public function getCart()
  192. {
  193. $where['shop_id'] = $this->shop_id;
  194. $this->data['cart'] = Dever::db('shop/shop_cart')->select($where);
  195. if ($this->data['cart']) {
  196. foreach ($this->data['cart'] as $k => $v) {
  197. $this->data['cart'][$k]['goods'] = Dever::load('goods/lib/info')->getPayInfo($v['goods_id'], $v['sku_id']);
  198. if (isset($this->data['cart'][$k]['goods']['sku_id'])) {
  199. $this->data['cart'][$k]['price_id'] = $this->data['cart'][$k]['goods']['sku_id'];
  200. }
  201. }
  202. }
  203. return $this->data['cart'];
  204. }
  205. # 加入到购物车
  206. public function addCart()
  207. {
  208. $where['shop_id'] = $this->shop_id;
  209. $where['goods_id'] = Dever::input('goods_id');
  210. $where['sku_id'] = Dever::input('price_id');
  211. if (!$where['goods_id']) {
  212. Dever::alert('错误的商品');
  213. }
  214. $info = Dever::db('shop/shop_cart')->find($where);
  215. $where['num'] = Dever::input('num');
  216. if (!$info) {
  217. # 验证库存
  218. Dever::load('shop/lib/info')->checkTotal($where['num'], $where['goods_id'], $this->shop_id, $where['sku_id'], 2);
  219. $state = Dever::db('shop/shop_cart')->insert($where);
  220. } else {
  221. $where['num'] += $info['num'];
  222. # 验证库存
  223. Dever::load('shop/lib/info')->checkTotal($where['num'], $where['goods_id'], $this->shop_id, $where['sku_id'], 2);
  224. $where['where_id'] = $info['id'];
  225. $state = Dever::db('shop/shop_cart')->update($where);
  226. }
  227. return $this->getCart();
  228. }
  229. # 清空购物车
  230. public function dropCart()
  231. {
  232. $where['shop_id'] = $this->shop_id;
  233. $state = Dever::db('shop/shop_cart')->delete($where);
  234. return $this->alert($state);
  235. }
  236. # 更新购物车数量
  237. public function upCart()
  238. {
  239. $where['shop_id'] = $this->shop_id;
  240. $where['id'] = Dever::input('cart_id');
  241. $status = Dever::input('status', 1);
  242. $type = Dever::input('type', 1);
  243. $num = Dever::input('num');
  244. $info = Dever::db('shop/shop_cart')->find($where);
  245. $state = false;
  246. if ($info) {
  247. if ($type == 1) {
  248. $num = $info['num'] + $num;
  249. } else {
  250. $num = $info['num'] - $num;
  251. }
  252. $where = array();
  253. $where['where_id'] = $info['id'];
  254. if ($num <= 0) {
  255. $state = Dever::db('shop/shop_cart')->delete($info['id']);
  256. } else {
  257. $where['status'] = $status;
  258. $where['num'] = $num;
  259. # 验证库存
  260. $total = Dever::load('shop/lib/info')->checkTotal($num, $info['goods_id'], $this->shop_id, $info['sku_id']);
  261. if ($where['num'] > $total) {
  262. # 库存不足
  263. $where['num'] = $total;
  264. $state = Dever::db('shop/shop_cart')->update($where);
  265. return array('cart' => 2, 'data' => $total);
  266. }
  267. $state = Dever::db('shop/shop_cart')->update($where);
  268. }
  269. }
  270. return array('cart' => 1, 'data' => $this->getCart());
  271. }
  272. # 确认订单页面
  273. public function confirm()
  274. {
  275. $this->data['shop'] = $this->shop;
  276. Dever::load('shop/lib/sell')->goods($this->data);
  277. Dever::load('shop/lib/sell')->coupon($this->data);
  278. return $this->data;
  279. }
  280. # 开始下单
  281. public function pay()
  282. {
  283. $refer = Dever::input('refer');
  284. $cart = Dever::input('cart', 2);
  285. $address_id = Dever::input('address_id');
  286. $invoice_id = Dever::input('invoice_id');
  287. $pay_type = Dever::input('pay_type', 1);
  288. $mobile = Dever::input('mobile');
  289. $cash = Dever::input('cash', 0);
  290. Dever::setInput('pay_method', 2);
  291. if (!$mobile) {
  292. Dever::alert('手机号不能为空');
  293. }
  294. $info = Dever::input('info');
  295. $this->data['shop'] = $this->shop;
  296. Dever::load('shop/lib/sell')->goods($this->data);
  297. Dever::load('shop/lib/sell')->coupon($this->data, 3);
  298. if ($this->data['pay_method'] > 1) {
  299. $cart = false;
  300. }
  301. $cart = 2;
  302. if (!$this->data['coupon_id'] && $this->data['price'] <= 0) {
  303. Dever::alert('已售空');
  304. }
  305. if ($this->data['method'] == 2 && !$address_id) {
  306. Dever::alert('收货地址不能为空');
  307. }
  308. if (!isset($this->data['card'])) {
  309. $this->data['card'] = false;
  310. }
  311. $this->user = array
  312. (
  313. 'id' => -1,
  314. 'mobile' => $mobile,
  315. );
  316. $this->data['price'] -= $cash;
  317. $pay = Dever::load('shop/lib/sell')->pay($this->data['method'], $this->data['pay_method'], $pay_type, $this->user, $this->shop, $this->data['name'], $this->data['num'], $this->data['list'], $this->data['price'], $cash, $address_id, $invoice_id, $info, $this->data['card'], $this->data['coupon_id'], $this->data['user_coupon_id'], $this->data['coupon_cash'], $cart, 4, $refer);
  318. return $pay;
  319. }
  320. # 再次付款
  321. public function r_pay()
  322. {
  323. $refer = Dever::input('refer');
  324. $order_id = Dever::input('order_id');
  325. $pay = Dever::load('shop/lib/sell')->rpay($order_id, 4, $refer);
  326. return $pay;
  327. }
  328. # 销量统计
  329. public function stat()
  330. {
  331. $where['shop_id'] = $this->shop_id;
  332. $where['end'] = time();
  333. $where['start'] = $where['end'] - 86400*30;
  334. $this->data['total'] = array();
  335. $this->data['total']['cash'] = 0;
  336. $this->data['total']['order'] = 0;
  337. $this->data['total']['goods'] = 0;
  338. $this->data['data'] = Dever::db('shop/sell_stat')->getAll($where);
  339. $this->data['day'] = $this->data['cash'] = $this->data['order'] = $this->data['goods'] = array();
  340. if ($this->data['data']) {
  341. foreach ($this->data['data'] as $k => $v) {
  342. if (!$v['cash']) {
  343. $this->data['data'][$k]['cash'] = 0;
  344. }
  345. if (!$v['order']) {
  346. $this->data['data'][$k]['order'] = 0;
  347. }
  348. if (!$v['goods']) {
  349. $this->data['data'][$k]['goods'] = 0;
  350. }
  351. $this->data['data'][$k]['cash'] = floatval($this->data['data'][$k]['cash']);
  352. $this->data['data'][$k]['order'] = floatval($this->data['data'][$k]['order']);
  353. $this->data['data'][$k]['goods'] = floatval($this->data['data'][$k]['goods']);
  354. $this->data['data'][$k]['day'] = date('m-d', $v['day']);
  355. $this->data['total']['cash'] += $this->data['data'][$k]['cash'];
  356. $this->data['total']['order'] += $this->data['data'][$k]['order'];
  357. $this->data['total']['goods'] += $this->data['data'][$k]['goods'];
  358. $this->data['day'][] = $this->data['data'][$k]['day'];
  359. $this->data['cash'][] = $this->data['data'][$k]['cash'];
  360. $this->data['order'][] = $this->data['data'][$k]['order'];
  361. $this->data['goods'][] = $this->data['data'][$k]['goods'];
  362. }
  363. }
  364. return $this->data;
  365. }
  366. }