Sell.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. namespace Scm_order\Lib;
  3. use Dever;
  4. class Sell
  5. {
  6. # 获取订单信息
  7. public function info($id)
  8. {
  9. Dever::load('manage/auth.init');
  10. $info = Dever::db('scm_order/sell')->find($id);
  11. $data['order'] = array();
  12. $data['order']['订单号'] = $info['order_num'];
  13. $seller = Dever::db('scm_role/seller')->find($info['seller_id']);
  14. $data['order']['销售商'] = $seller['name'];
  15. if ($info['uid'] && $info['uid'] > 0) {
  16. $user = Dever::db('passport/api')->info($info['uid']);
  17. $data['order']['购买人'] = $user;
  18. }
  19. $data['order'] = Dever::table($data['order']);
  20. if ($info['address']) {
  21. $data['address'] = array();
  22. $info['address'] = Dever::json_encode($info['address']);
  23. if ($info['address']) {
  24. $data['address']['收件人'] = $info['address']['name'];
  25. $data['address']['手机号'] = $info['address']['mobile'];
  26. $data['address']['地址'] = $info['address']['address'];
  27. }
  28. $data['address'] = Dever::table($data['address']);
  29. }
  30. $data['cash'] = array();
  31. $data['cash']['总数量'] = $info['num'];
  32. $data['cash']['总金额'] = $info['cash'];
  33. $data['cash']['支付金额'] = $info['pay_cash'];
  34. $data['cash'] = Dever::table($data['cash']);
  35. return $data;
  36. }
  37. public function ps($id, $name, $data)
  38. {
  39. Dever::config('base')->hook = true;
  40. $order_id = Dever::param('order_id', $data);
  41. if ($order_id) {
  42. /*
  43. $info = Dever::db('scm_order/sell')->find(array('id' => $order_id));
  44. if ($info && $info['status'] < 3) {
  45. Dever::load('shop/lib/sell')->notice($info);
  46. }*/
  47. }
  48. }
  49. # 得到商品和总价
  50. public function goods($uid, $seller_id = 1)
  51. {
  52. $goods = Dever::input('goods');
  53. if (!$goods) {
  54. Dever::alert('请传入商品');
  55. }
  56. $goods = Dever::json_decode($goods);
  57. if (!$goods) {
  58. Dever::alert('商品信息不存在');
  59. }
  60. $data = array();
  61. # 总价
  62. $data['cash'] = 0;
  63. # 支付价格
  64. $data['pay_cash'] = 0;
  65. # 钱包金额
  66. $data['wallet_cash'] = 0;
  67. # 配送金额
  68. $data['ps_cash'] = 0;
  69. # 总数量
  70. $data['num'] = 0;
  71. # 计算总价格
  72. foreach ($goods as $k => $v) {
  73. if (!isset($v['goods_id']) || !isset($v['sku_id'])) {
  74. continue;
  75. }
  76. $v['num'] = isset($v['num']) ? $v['num'] : 1;
  77. $data['list'][$k] = Dever::load('scm_product/lib/info')->getPayInfo($v['goods_id'], $v['sku_id'], $seller_id);
  78. if (!$data['list'][$k]) {
  79. continue;
  80. }
  81. if ($data['list'][$k]['sell_type'] == 2) {
  82. continue;
  83. }
  84. $data['list'][$k]['pay_cash'] = $data['list'][$k]['cash'] = $data['list'][$k]['price'];
  85. unset($data['list'][$k]['price']);
  86. $data['list'][$k]['num'] = $v['num'];
  87. # 1可以购买 2不可以购买
  88. $data['list'][$k]['buy_state'] = 1;
  89. if ($data['list'][$k]['stock'] == 1) {
  90. if ($data['list'][$k]['status'] == 2) {
  91. $stock = 0;
  92. } else {
  93. # 获取库存
  94. $stock = $this->stock($data['list'][$k]['num'], $v['goods_id'], $v['sku_id'], $seller_id);
  95. if (isset($data['list'][$k]['min']) && $data['list'][$k]['min'] > 0) {
  96. if ($stock < $data['list'][$k]['min']) {
  97. $stock = 0;
  98. }
  99. }
  100. }
  101. if ($stock <= 0) {
  102. # 如果没有库存了
  103. $data['list'][$k]['buy_state'] = 2;
  104. $data['list'][$k]['buy_num'] = 0;
  105. }
  106. }
  107. if ($data['list'][$k]['buy_state'] == 1) {
  108. $data['list'][$k]['buy_num'] = $v['num'];
  109. $data['num'] += $v['num'];
  110. $data['cash'] += $data['list'][$k]['cash'] * $v['num'];
  111. $data['pay_cash'] += $data['list'][$k]['pay_cash'] * $v['num'];
  112. }
  113. }
  114. /*
  115. if ($data['method'] == 2) {
  116. # 获取配送商的价格
  117. $data['ps_cash'] = $data['shop']['ps_cash'];
  118. if ($data['shop']['free_ps_cash'] && $data['shop']['free_ps_cash'] > 0 && $data['cash'] >= $data['shop']['free_ps_cash']) {
  119. $data['ps_cash'] = 0;
  120. }
  121. if (!$data['ps_cash']) {
  122. $data['ps_cash'] = 0;
  123. }
  124. }*/
  125. return $data;
  126. }
  127. # 实时获取库存 之后改成从redis中获取
  128. public function stock($num, $goods_id, $sku_id, $seller_id)
  129. {
  130. $seller = Dever::db('scm_role/seller')->find($seller_id);
  131. if ($seller['stock'] == 2 && $seller['stock_seller'] > 0) {
  132. $seller_id = $seller['stock_seller'];
  133. }
  134. $where['seller_id'] = $seller_id;
  135. $where['goods_id'] = $goods_id;
  136. $where['sku_id'] = $sku_id;
  137. $info = Dever::db('scm_role/seller_goods_sku')->getOne($where);
  138. if (!$info) {
  139. return 0;
  140. }
  141. $total = $info['total'];
  142. return $total;
  143. }
  144. # 发起支付
  145. public function pay($data, $uid, $seller_id, $info = '', $cart = false, $refer = '')
  146. {
  147. if (!$uid) {
  148. Dever::alert('错误的用户信息');
  149. }
  150. if ($uid > 0) {
  151. $wechat = Dever::db('passport/wechat')->one(array('uid' => $uid, 'type' => 1, 'system_id' => 1));
  152. if (!$wechat) {
  153. //Dever::alert('错误的用户信息');
  154. }
  155. }
  156. $order_data['seller_id'] = $seller_id;
  157. $order_data['uid'] = $uid;
  158. //$order_data['address'] = $address;
  159. $order_data['num'] = $data['num'];
  160. $order_data['info'] = $info;
  161. $order_data['cash'] = $data['cash'];
  162. $order_data['pay_cash'] = $data['pay_cash'];
  163. $order_data['wallet_cash'] = $data['wallet_cash'];
  164. $order_data['ps_cash'] = $data['ps_cash'];
  165. $time = time();
  166. $order_data_where = $order_data;
  167. $order_data_where['start'] = $time - 60;
  168. $order_data_where['end'] = $time;
  169. $order_data_where['option']['start'] = array('yes-cdate', '>=');
  170. $order_data_where['option']['end'] = array('yes-cdate', '<=');
  171. $info = Dever::db('scm_order/sell')->find($order_data_where);
  172. $order_data['order_num'] = $this->getOrderId();
  173. if (!$info) {
  174. $id = Dever::db('scm_order/sell')->insert($order_data);
  175. } else {
  176. return array
  177. (
  178. 'order' => '',
  179. 'order_num' => $info['order_num'],
  180. 'order_id' => $info['id'],
  181. 'type' => 'test',
  182. );
  183. }
  184. if (!$id) {
  185. Dever::alert('支付失败');
  186. }
  187. foreach($data['list'] as $k => $v) {
  188. if ($v['buy_state'] == 1) {
  189. $data['uid'] = $uid;
  190. $data['order_id'] = $id;
  191. $data['seller_id'] = $seller_id;
  192. $data['goods_id'] = $v['id'];
  193. $data['sku_id'] = $v['sku_id'];
  194. $data['cash'] = $v['cash'];
  195. $data['pay_cash'] = $v['pay_cash'];
  196. $data['num'] = $v['buy_num'];
  197. Dever::db('scm_order/sell_goods')->insert($data);
  198. }
  199. }
  200. # 减少库存 增加销量
  201. $order_data['id'] = $id;
  202. $this->updateStock($order_data);
  203. if ($price <= 0) {
  204. $param['pay_product_id'] = $id;
  205. $param['pay_order_id'] = $order_data['order_num'];
  206. $param['pay_status'] = 2;
  207. $param['pay_cash'] = $price;
  208. $param['pay_msg'] = '';
  209. $this->success($param);
  210. if ($cart) {
  211. # 来自购物车,要清空一下
  212. $this->dropCart($cart, $uid, $seller_id, $data['list']);
  213. }
  214. # 使用优惠券
  215. //$this->useCoupon($order_data, $coupon_id, $user_coupon_id, $card, $wallet_cash);
  216. return array
  217. (
  218. 'order' => '',
  219. 'order_num' => $order_data['order_num'],
  220. 'order_id' => $id,
  221. 'type' => 'test',
  222. );
  223. }
  224. $sell_config = Dever::db('main/sell_config')->one();
  225. # 分账
  226. $other = false;
  227. if (isset($shop['mid']) && $shop['mid']) {
  228. $other = array
  229. (
  230. 'mid' => $shop['mid'],
  231. 'order_id' => $order_data['order_num'] . '_' . $shop['id'],
  232. 'per' => $sell_config['per'],
  233. 'oprice' => $oprice,
  234. );
  235. }
  236. $param = array
  237. (
  238. 'project_id' => 1,
  239. 'channel_id' => 1,
  240. 'system_source' => $system_source,
  241. 'uid' => $uid,
  242. 'name' => $order_data['name'],
  243. 'cash' => $price,
  244. 'product_id' => $id,
  245. 'order_id' => $order_data['order_num'],
  246. 'other' => $other,
  247. 'refer' => $refer,
  248. );
  249. if ($uid > 0 && isset($wechat) && $wechat) {
  250. $param['openid'] = $wechat['openid'];
  251. }
  252. $receipt = Dever::input('receipt');
  253. if ($receipt) {
  254. $param['receipt'] = $receipt;
  255. }
  256. $result = Dever::load('pay/api.pay', $param);
  257. //$result['type'] = 'test';
  258. //$result['order'] = '';
  259. if ($cart) {
  260. # 来自购物车,要清空一下
  261. $this->dropCart($cart, $uid, $goods, $shop['id']);
  262. }
  263. # 使用优惠券
  264. $this->useCoupon($order_data, $coupon_id, $user_coupon_id, $card, $wallet_cash);
  265. $result['order_num'] = $order_data['order_num'];
  266. $result['order_id'] = $id;
  267. return $result;
  268. }
  269. }