Sell.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. <?php
  2. namespace 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('order/sell')->find($id);
  11. $data['order'] = array();
  12. $data['order']['订单号'] = $info['order_num'];
  13. $seller = Dever::db('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('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(&$data)
  51. {
  52. # 1自提,2配送
  53. $data['method'] = Dever::input('method', 1);
  54. $goods = Dever::input('goods');
  55. if (!$goods) {
  56. Dever::alert('请传入商品');
  57. }
  58. $goods = Dever::json_decode($goods);
  59. if (!$goods) {
  60. Dever::alert('商品信息不存在');
  61. }
  62. # 总价
  63. $data['cash'] = 0;
  64. # 支付价格
  65. $data['pay_cash'] = 0;
  66. # 钱包金额
  67. $data['wallet_cash'] = 0;
  68. # 配送金额
  69. $data['ps_cash'] = 0;
  70. $data['num'] = 0;
  71. # 获取角色
  72. $role = $this->getRole($data);
  73. $data['discount_goods'] = array();
  74. $data['discount_cash'] = 0;
  75. $data['discount_id'] = 0;
  76. # 计算总价格
  77. foreach ($goods as $k => $v) {
  78. if (!isset($v['goods_id']) || !isset($v['sku_id'])) {
  79. continue;
  80. }
  81. $v['num'] = isset($v['num']) ? $v['num'] : 1;
  82. $data['list'][$k] = Dever::load('product/lib/info')->getInfo($v['goods_id'], $v['sku_id']);
  83. $w = array();
  84. $w['goods_id'] = $v;
  85. $w['sku_id'] = $s;
  86. $w['shop_id'] = $data['shop']['id'];
  87. $goods_sku = Dever::db('shop/goods_sku')->find($w);
  88. if ($goods_sku) {
  89. if (isset($goods_sku['min'])) {
  90. $data['list'][$k]['min'] = $goods_sku['min'];
  91. }
  92. if (isset($goods_sku['price_id']) && $goods_sku['price_id'] > 0) {
  93. $price_template = Dever::load('price/lib/data')->get($v, $s, $goods_sku['price_id']);
  94. if ($price_template) {
  95. /*
  96. if ($goods_sku['min'] <= 0 && $price_template['price_num'] > 0) {
  97. $data['list'][$k]['min'] = $price_template['price_num'];
  98. }
  99. */
  100. if ($price_template['price_sell'] && $price_template['price_sell'] > 0) {
  101. $data['list'][$k]['price'] = $price_template['price_sell'];
  102. }
  103. }
  104. }
  105. }
  106. $data['list'][$k]['cash'] = $data['list'][$k]['price'];
  107. $data['list'][$k]['num'] = $n;
  108. # 2是库存不足
  109. $data['list'][$k]['ku_state'] = 1;
  110. if ($data['list'][$k]['status'] == 2) {
  111. $total = 0;
  112. } else {
  113. # 验证是否有货
  114. $total = Dever::load('shop/lib/info')->checkTotal($n, $v, $data['shop']['id'], $s);
  115. if (isset($data['list'][$k]['min']) && $data['list'][$k]['min'] > 0) {
  116. if ($total < $data['list'][$k]['min']) {
  117. $total = 0;
  118. }
  119. }
  120. }
  121. if ($total <= 0) {
  122. $data['list'][$k]['ku_state'] = 2;
  123. $data['list'][$k]['buy_num'] = 0;
  124. }
  125. if ($data['list'][$k]['ku_state'] == 1) {
  126. $data['list'][$k]['buy_num'] = $n;
  127. $data['num'] += $n;
  128. # 根据角色设置折扣
  129. if ($role) {
  130. $role_set = false;
  131. foreach ($role as $rk => $rv) {
  132. $discount_goods = Dever::db('act/discount_goods')->getData(array('discount_id' => $rv['id']));
  133. if ($discount_goods) {
  134. $discount_goods = array_keys($discount_goods);
  135. if (in_array($data['list'][$k]['id'], $discount_goods)) {
  136. $role_set = $rv;
  137. break;
  138. }
  139. } elseif ($rv['category'] && in_array($data['list'][$k]['top_category_id'], $rv['category'])) {
  140. $role_set = $rv;
  141. break;
  142. }
  143. }
  144. if ($role_set) {
  145. $discount_cash = round($data['list'][$k]['price'] * ($role_set['num']/10), 2);
  146. $data['list'][$k]['discount_id'] = $role_set['id'];
  147. $data['discount_goods'][] = $data['list'][$k];
  148. $data['discount_id'] = $role_set['id'];
  149. $data['list'][$k]['discount_cash'] = round(($data['list'][$k]['price'] - $discount_cash) * $n, 2);
  150. $data['discount_cash'] += $data['list'][$k]['discount_cash'];
  151. $data['price'] += $discount_cash * $n;
  152. } else {
  153. $data['price'] += $data['list'][$k]['price'] * $n;
  154. }
  155. } else {
  156. $data['price'] += $data['list'][$k]['price'] * $n;
  157. }
  158. $data['oprice'] += $data['list'][$k]['cash'] * $n;
  159. $data['name'][] = $data['list'][$k]['name'];
  160. }
  161. }
  162. $data['name'] = implode(',', $data['name']);
  163. if ($data['method'] == 2) {
  164. $data['ps_cash'] = $data['shop']['ps_cash'];
  165. if ($data['shop']['free_ps_cash'] && $data['shop']['free_ps_cash'] > 0 && $data['price'] >= $data['shop']['free_ps_cash']) {
  166. $data['ps_cash'] = 0;
  167. }
  168. if (!$data['ps_cash']) {
  169. $data['ps_cash'] = 0;
  170. }
  171. }
  172. return $data;
  173. }
  174. public function getRole($data)
  175. {
  176. if(isset($data['uid']) && $data['uid'] > 0) {
  177. return Dever::load('shop/lib/info')->getRole($data['uid']);
  178. }
  179. return false;
  180. }
  181. # 发起支付
  182. 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 = '')
  183. {
  184. if (!$user) {
  185. Dever::alert('错误的用户信息');
  186. }
  187. $uid = $user['id'];
  188. if ($uid > 0) {
  189. $wechat = Dever::db('passport/wechat')->one(array('uid' => $uid, 'type' => 1, 'system_id' => 1));
  190. if (!$wechat) {
  191. //Dever::alert('错误的用户信息');
  192. }
  193. }
  194. if ($method == 2) {
  195. $ps_cash = $shop['ps_cash'];
  196. if ($shop['free_ps_cash'] && $shop['free_ps_cash'] > 0 && $price >= $shop['free_ps_cash']) {
  197. $ps_cash = 0;
  198. }
  199. if (!$ps_cash) {
  200. $ps_cash = 0;
  201. }
  202. $order_data['ps_cash'] = $ps_cash;
  203. $price += $order_data['ps_cash'];
  204. }
  205. $order_data['shop_id'] = $shop['id'];
  206. if ($shop['type'] == 10) {
  207. $order_data['shop_type'] = 2;
  208. } else {
  209. $order_data['shop_type'] = 1;
  210. }
  211. $order_data['area'] = $shop['area'] . ',' . $shop['id'];
  212. $order_data['uid'] = $uid;
  213. $order_data['mobile'] = $user['mobile'];
  214. $order_data['address_id'] = $address_id;
  215. $order_data['invoice_id'] = $invoice_id;
  216. $order_data['name'] = $name;
  217. $order_data['num'] = $num;
  218. $order_data['info'] = $info;
  219. $order_data['oprice'] = $oprice;
  220. $order_data['price'] = $price;
  221. $order_data['kou_cash'] = $cash;
  222. $order_data['method'] = $method;
  223. $order_data['pay_method'] = $pay_method;
  224. $order_data['pay_type'] = $pay_type;
  225. $order_data['refund_cash'] = 0;
  226. if ($card) {
  227. // $order_data['card_id'] = $card['info']['id'];
  228. $order_data['card_code_id'] = $card['id'];
  229. $order_data['card_code_card'] = $card['card'];
  230. $order_data['card_code_cash'] = $card['cash'];
  231. } elseif ($wallet_cash && $wallet_cash > 0) {
  232. $order_data['wallet_cash'] = $wallet_cash;
  233. }
  234. if ($user_coupon_id) {
  235. $order_data['user_coupon_id'] = $user_coupon_id;
  236. }
  237. if ($coupon_id) {
  238. $order_data['coupon_id'] = $coupon_id;
  239. }
  240. if ($coupon_cash) {
  241. $order_data['coupon_cash'] = $coupon_cash;
  242. }
  243. if ($discount_id) {
  244. $order_data['discount_id'] = $discount_id;
  245. }
  246. if ($discount_cash) {
  247. $order_data['discount_cash'] = $discount_cash;
  248. }
  249. $time = time();
  250. $order_data_where = $order_data;
  251. $order_data_where['start'] = $time - 60;
  252. $order_data_where['end'] = $time;
  253. $order_data_where['option']['start'] = array('yes-cdate', '>=');
  254. $order_data_where['option']['end'] = array('yes-cdate', '<=');
  255. $info = Dever::db('shop/sell_order')->find($order_data_where);
  256. $order_data['order_num'] = $this->getOrderId();
  257. if (!$info) {
  258. $id = Dever::db('shop/sell_order')->insert($order_data);
  259. } else {
  260. return array
  261. (
  262. 'order' => '',
  263. 'order_num' => $info['order_num'],
  264. 'order_id' => $info['id'],
  265. 'type' => 'test',
  266. );
  267. }
  268. if (!$id) {
  269. Dever::alert('支付失败');
  270. }
  271. foreach($goods as $k => $v) {
  272. if ($v['ku_state'] == 1) {
  273. $data['uid'] = $uid;
  274. $data['order_id'] = $id;
  275. $data['shop_id'] = $shop['id'];
  276. $data['goods_id'] = $v['id'];
  277. $data['sku_id'] = $v['sku_id'];
  278. $data['price'] = $v['price'];
  279. $data['num'] = $v['buy_num'];
  280. if (isset($v['user_coupon_id']) && $v['user_coupon_id']) {
  281. $data['user_coupon_id'] = $v['user_coupon_id'];
  282. }
  283. if (isset($v['coupon_id']) && $v['coupon_id']) {
  284. $data['coupon_id'] = $v['coupon_id'];
  285. }
  286. if (isset($v['coupon_cash']) && $v['coupon_cash']) {
  287. $data['coupon_cash'] = $v['coupon_cash'];
  288. } else {
  289. $data['coupon_cash'] = 0;
  290. }
  291. if (isset($v['discount_id']) && $v['discount_id']) {
  292. $data['discount_id'] = $v['discount_id'];
  293. }
  294. if (isset($v['discount_cash']) && $v['discount_cash']) {
  295. $data['discount_cash'] = $v['discount_cash'];
  296. }
  297. Dever::db('shop/sell_order_goods')->insert($data);
  298. }
  299. }
  300. # 减少库存 增加销量
  301. $order_data['id'] = $id;
  302. $this->updateSell($order_data);
  303. if ($price <= 0 || $pay_type == 2) {
  304. $param['pay_product_id'] = $id;
  305. $param['pay_order_id'] = $order_data['order_num'];
  306. $param['pay_status'] = 2;
  307. $param['pay_cash'] = $price;
  308. $param['pay_msg'] = '';
  309. $this->success($param);
  310. if ($cart) {
  311. # 来自购物车,要清空一下
  312. $this->dropCart($cart, $uid, $goods, $shop['id']);
  313. }
  314. # 使用优惠券
  315. $this->useCoupon($order_data, $coupon_id, $user_coupon_id, $card, $wallet_cash);
  316. /*
  317. if ($card) {
  318. # 更新礼品卡状态
  319. $card_update['where_id'] = $card['id'];
  320. $card_update['status'] = 3;
  321. $card_update['order_id'] = $id;
  322. $card_update['shop_id'] = $shop['id'];
  323. Dever::db('goods/card_code')->update($card_update);
  324. }*/
  325. return array
  326. (
  327. 'order' => '',
  328. 'order_num' => $order_data['order_num'],
  329. 'order_id' => $id,
  330. 'type' => 'test',
  331. );
  332. }
  333. $sell_config = Dever::db('main/sell_config')->one();
  334. # 分账
  335. $other = false;
  336. if (isset($shop['mid']) && $shop['mid']) {
  337. $other = array
  338. (
  339. 'mid' => $shop['mid'],
  340. 'order_id' => $order_data['order_num'] . '_' . $shop['id'],
  341. 'per' => $sell_config['per'],
  342. 'oprice' => $oprice,
  343. );
  344. }
  345. $param = array
  346. (
  347. 'project_id' => 1,
  348. 'channel_id' => 1,
  349. 'system_source' => $system_source,
  350. 'uid' => $uid,
  351. 'name' => $order_data['name'],
  352. 'cash' => $price,
  353. 'product_id' => $id,
  354. 'order_id' => $order_data['order_num'],
  355. 'other' => $other,
  356. 'refer' => $refer,
  357. );
  358. if ($uid > 0 && isset($wechat) && $wechat) {
  359. $param['openid'] = $wechat['openid'];
  360. }
  361. $receipt = Dever::input('receipt');
  362. if ($receipt) {
  363. $param['receipt'] = $receipt;
  364. }
  365. $result = Dever::load('pay/api.pay', $param);
  366. //$result['type'] = 'test';
  367. //$result['order'] = '';
  368. if ($cart) {
  369. # 来自购物车,要清空一下
  370. $this->dropCart($cart, $uid, $goods, $shop['id']);
  371. }
  372. # 使用优惠券
  373. $this->useCoupon($order_data, $coupon_id, $user_coupon_id, $card, $wallet_cash);
  374. $result['order_num'] = $order_data['order_num'];
  375. $result['order_id'] = $id;
  376. return $result;
  377. }
  378. }