vr_order.model.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <?php
  2. /**
  3. * 虚拟订单模型
  4. *
  5. *
  6. *
  7. *
  8. */
  9. defined('InShopNC') or exit('Access Invalid!');
  10. class vr_orderModel extends Model
  11. {
  12. /**
  13. * 取单条订单信息
  14. *
  15. * @param array $condition
  16. * @return unknown
  17. */
  18. public function getOrderInfo($condition = array(), $fields = '*', $master = false)
  19. {
  20. $order_info = $this->table('vr_order')->field($fields)->where($condition)->master($master)->find();
  21. if (empty($order_info)) {
  22. return array();
  23. }
  24. if (isset($order_info['order_state'])) {
  25. $order_info['state_desc'] = $this->_orderState($order_info['order_state']);
  26. $order_info['state_desc'] = $order_info['state_desc'][0];
  27. }
  28. if (isset($order_info['payment_code'])) {
  29. $order_info['payment_name'] = orderPaymentName($order_info['payment_code']);
  30. }
  31. return $order_info;
  32. }
  33. /**
  34. * 新增订单
  35. * @param array $data
  36. * @return int 返回 insert_id
  37. */
  38. public function addOrder($data)
  39. {
  40. $insert = $this->table('vr_order')->insert($data);
  41. return $insert;
  42. }
  43. /**
  44. * 生成兑换码
  45. * @param array $order_info
  46. * @return int 返回 insert_id
  47. */
  48. public function addOrderCode($order_info)
  49. {
  50. // 好商 城提供二次开发
  51. $vrc_num = Model()->table('vr_order_code')->where(array('order_id' => $order_info['order_id']))->count();
  52. if (!empty($vrc_num) && intval($vrc_num) >= intval($order_info['goods_num']))
  53. return false;
  54. if (empty($order_info)) return false;
  55. //均摊后每个兑换码支付金额
  56. $each_pay_price = ncPriceFormat($order_info['order_amount'] / $order_info['goods_num'], 2);
  57. //取得店铺兑换码前缀
  58. $store_info = Model('store')->getStoreInfoByID($order_info['store_id']);
  59. $virtual_code_perfix = $store_info['store_vrcode_prefix'] ? $store_info['store_vrcode_prefix'] : rand(100, 999);
  60. //生成兑换码
  61. $code_list = $this->_makeVrCode($virtual_code_perfix, $order_info['store_id'], $order_info['buyer_id'], $order_info['goods_num']);
  62. for ($i = 0; $i < $order_info['goods_num']; $i++) {
  63. $order_code[$i]['order_id'] = $order_info['order_id'];
  64. $order_code[$i]['store_id'] = $order_info['store_id'];
  65. $order_code[$i]['buyer_id'] = $order_info['buyer_id'];
  66. $order_code[$i]['vr_code'] = $code_list[$i];
  67. $order_code[$i]['pay_price'] = $each_pay_price;
  68. $order_code[$i]['vr_indate'] = $order_info['vr_indate'];
  69. $order_code[$i]['vr_invalid_refund'] = $order_info['vr_invalid_refund'];
  70. }
  71. //将因舍出小数部分出现的差值补到最后一个商品的实际成交价中
  72. // $diff_amount = $order_info['order_amount'] - $each_pay_price * $order_info['goods_num'];
  73. // $order_code[$i-1]['pay_price'] += $diff_amount;
  74. return $this->table('vr_order_code')->insertAll($order_code);
  75. }
  76. /**
  77. * 更改订单信息
  78. *
  79. * @param array $data
  80. * @param array $condition
  81. */
  82. public function editOrder($data, $condition, $limit = '')
  83. {
  84. return $this->table('vr_order')->where($condition)->limit($limit)->update($data);
  85. }
  86. /**
  87. * 更新兑换码
  88. * @param unknown $data
  89. * @param unknown $condition
  90. */
  91. public function editOrderCode($data, $condition)
  92. {
  93. return $this->table('vr_order_code')->where($condition)->update($data);
  94. }
  95. /**
  96. * 兑换码列表
  97. * @param unknown $condition
  98. * @param string $fields
  99. */
  100. public function getCodeList($condition = array(), $fields = '*', $pagesize = '', $order = 'rec_id desc', $limit = '', $group = '')
  101. {
  102. return $this->table('vr_order_code')->field($fields)->where($condition)->page($pagesize)->order($order)->limit($limit)->group($group)->select();
  103. }
  104. /**
  105. * 兑换码列表
  106. * @param unknown $condition
  107. * @param string $fields
  108. */
  109. public function getCodeUnusedList($condition = array(), $fields = '*', $pagesize = '', $order = 'rec_id desc', $limit = '')
  110. {
  111. $condition['vr_state'] = 0;
  112. $condition['refund_lock'] = 0;
  113. return $this->getCodeList($condition, $fields, $pagesize, $order, $limit, 'order_id');
  114. }
  115. /**
  116. * 根据虚拟订单取没有使用的兑换码列表
  117. *
  118. * @param
  119. * @return array
  120. */
  121. public function getCodeRefundList($order_list = array())
  122. {
  123. if (!empty($order_list) && is_array($order_list)) {
  124. $order_ids = array();//订单编号数组
  125. foreach ($order_list as $key => $value) {
  126. $order_id = $value['order_id'];
  127. $order_ids[$order_id] = $key;
  128. }
  129. $condition = array();
  130. $condition['order_id'] = array('in', array_keys($order_ids));
  131. $condition['refund_lock'] = '0';//退款锁定状态:0为正常(能退款),1为锁定(待审核),2为同意
  132. $code_list = $this->getCodeList($condition);
  133. if (!empty($code_list) && is_array($code_list)) {
  134. foreach ($code_list as $key => $value) {
  135. $order_id = $value['order_id'];//虚拟订单编号
  136. $rec_id = $value['rec_id'];//兑换码表编号
  137. if ($value['vr_state'] != '1') {//使用状态 0:未使用1:已使用2:已过期
  138. $order_key = $order_ids[$order_id];
  139. $order_list[$order_key]['code_list'][$rec_id] = $value;
  140. }
  141. }
  142. }
  143. }
  144. return $order_list;
  145. }
  146. /**
  147. * 取得兑换码列表
  148. * @param unknown $condition
  149. * @param string $fields
  150. */
  151. public function getOrderCodeList($condition = array(), $fields = '*')
  152. {
  153. $code_list = $this->getCodeList($condition);
  154. //进一步处理
  155. if (!empty($code_list)) {
  156. $i = 0;
  157. foreach ($code_list as $k => $v) {
  158. if ($v['vr_state'] == '1') {
  159. $content = '已使用,使用时间 ' . date('Y-m-d', $v['vr_usetime']);
  160. } else if ($v['vr_state'] == '0') {
  161. if ($v['vr_indate'] < time()) {
  162. $content = '已过期,过期时间 ' . date('Y-m-d', $v['vr_indate']);
  163. } else {
  164. $content = '未使用,有效期至 ' . date('Y-m-d', $v['vr_indate']);
  165. }
  166. }
  167. if ($v['refund_lock'] == '1') {
  168. $content = '退款审核中';
  169. } else if ($v['refund_lock'] == '2') {
  170. $content = '退款已完成';
  171. }
  172. $code_list[$k]['vr_code_desc'] = $content;
  173. if ($v['vr_state'] == '0') $i++;
  174. }
  175. $code_list[0]['vr_code_valid_count'] = $i;
  176. }
  177. return $code_list;
  178. }
  179. /**
  180. * 取得兑换码信息
  181. * @param unknown $condition
  182. * @param string $fields
  183. */
  184. public function getOrderCodeInfo($condition = array(), $fields = '*', $master = false)
  185. {
  186. return $this->table('vr_order_code')->field($fields)->where($condition)->master($master)->find();
  187. }
  188. /**
  189. * 取得兑换码数量
  190. * @param unknown $condition
  191. */
  192. public function getOrderCodeCount($condition)
  193. {
  194. return $this->table('vr_order_code')->where($condition)->count();
  195. }
  196. /**
  197. * 生成兑换码
  198. * 长度 =3位 + 4位 + 2位 + 3位 + 1位 + 5位随机 = 18位
  199. * @param string $perfix 前缀
  200. * @param int $store_id
  201. * @param int $member_id
  202. * @param unknown $num
  203. * @return multitype:string
  204. */
  205. private function _makeVrCode($perfix, $store_id, $member_id, $num)
  206. {
  207. $perfix .= sprintf('%04d', (int)$store_id * $member_id % 10000)
  208. . sprintf('%02d', (int)$member_id % 100)
  209. . sprintf('%03d', (float)microtime() * 1000);
  210. $code_list = array();
  211. for ($i = 0; $i < $num; $i++) {
  212. $code_list[$i] = $perfix . sprintf('%01d', (int)$i % 10) . random(5, 1);
  213. }
  214. return $code_list;
  215. }
  216. /**
  217. * 取得订单列表(所有)
  218. * @param unknown $condition
  219. * @param string $pagesize
  220. * @param string $field
  221. * @param string $order
  222. * @return array
  223. */
  224. public function getOrderList($condition, $pagesize = '', $field = '*', $order = 'order_id desc', $limit = '')
  225. {
  226. $list = $this->table('vr_order')->field($field)->where($condition)->page($pagesize)->order($order)->limit($limit)->select();
  227. if (empty($list)) return array();
  228. foreach ($list as $key => $order) {
  229. if (isset($order['order_state'])) {
  230. list($list[$key]['state_desc'], $list[$key]['order_state_text']) = $this->_orderState($order['order_state']);
  231. }
  232. if (isset($order['payment_code'])) {
  233. $list[$key]['payment_name'] = orderPaymentName($order['payment_code']);
  234. }
  235. }
  236. return $list;
  237. }
  238. /**
  239. * 取得订单状态文字输出形式
  240. *
  241. * @param array $order_info 订单数组
  242. * @return array
  243. */
  244. private function _orderState($order_state)
  245. {
  246. switch ($order_state) {
  247. case ORDER_STATE_CANCEL:
  248. $order_state = '<span style="color:#999">已取消</span>';
  249. $order_state_text = '已取消';
  250. break;
  251. case ORDER_STATE_NEW:
  252. $order_state = '<span style="color:#36C">待付款</span>';
  253. $order_state_text = '待付款';
  254. break;
  255. case ORDER_STATE_PAY:
  256. $order_state = '<span style="color:#999">已支付</span>';
  257. $order_state_text = '已支付';
  258. break;
  259. case ORDER_STATE_SUCCESS:
  260. $order_state = '<span style="color:#999">已完成</span>';
  261. $order_state_text = '已完成';
  262. break;
  263. }
  264. return array($order_state, $order_state_text);
  265. }
  266. /**
  267. * 返回是否允许某些操作
  268. * @param string $operate
  269. * @param array $order_info
  270. */
  271. public function getOrderOperateState($operate, $order_info)
  272. {
  273. if (!is_array($order_info) || empty($order_info)) return false;
  274. switch ($operate) {
  275. //买家取消订单
  276. case 'buyer_cancel':
  277. $state = $order_info['order_state'] == ORDER_STATE_NEW;
  278. break;
  279. //商家
  280. case 'store_send':
  281. $state = $order_info['order_state'] == ORDER_STATE_PAY;
  282. break;
  283. //商家
  284. case 'store_receive':
  285. $state = $order_info['order_state'] == ORDER_STATE_SEND;
  286. break;
  287. //商家取消订单
  288. case 'store_cancel':
  289. $state = $order_info['order_state'] == ORDER_STATE_NEW;
  290. break;
  291. //平台取消订单
  292. case 'system_cancel':
  293. $state = $order_info['order_state'] == ORDER_STATE_NEW;
  294. break;
  295. //平台收款
  296. case 'system_receive_pay':
  297. $state = $order_info['order_state'] == ORDER_STATE_NEW;
  298. break;
  299. //支付
  300. case 'payment':
  301. $state = $order_info['order_state'] == ORDER_STATE_NEW;
  302. break;
  303. //评价
  304. case 'evaluation':
  305. $state = !$order_info['lock_state'] && $order_info['evaluation_state'] == '0' && $order_info['use_state'];
  306. break;
  307. //买家退款
  308. case 'refund':
  309. $state = false;
  310. $code_list = $order_info['code_list'];//没有使用的兑换码列表
  311. if (!empty($code_list) && is_array($code_list)) {
  312. if ($order_info['vr_indate'] > time()) {//有效期内的能退款
  313. $state = true;
  314. }
  315. if ($order_info['vr_invalid_refund'] == 1 && ($order_info['vr_indate'] + 60 * 60 * 24 * CODE_INVALID_REFUND) > time()) {//兑换码过期后可退款
  316. $state = true;
  317. }
  318. }
  319. break;
  320. //分享
  321. case 'share':
  322. $state = true;
  323. break;
  324. }
  325. return $state;
  326. }
  327. /**
  328. * 订单详情页显示进行步骤
  329. * @param array $order_info
  330. */
  331. public function getOrderStep($order_info)
  332. {
  333. if (!is_array($order_info) || empty($order_info)) return array();
  334. $step_list = array();
  335. // 第一步 下单完成
  336. $step_list['step1'] = true;
  337. //第二步 付款完成
  338. $step_list['step2'] = !empty($order_info['payment_time']);
  339. //第三步 兑换码使用中
  340. $step_list['step3'] = !empty($order_info['payment_time']);
  341. //第四步 使用完成或到期结束
  342. $step_list['step4'] = $order_info['order_state'] == ORDER_STATE_SUCCESS;
  343. return $step_list;
  344. }
  345. /**
  346. * 取得订单数量
  347. * @param unknown $condition
  348. */
  349. public function getOrderCount($condition)
  350. {
  351. return $this->table('vr_order')->where($condition)->count();
  352. }
  353. /**
  354. * 订单销售记录 订单状态为20、30、40时
  355. * @param unknown $condition
  356. * @param string $field
  357. * @param number $page
  358. * @param string $order
  359. */
  360. public function getOrderAndOrderGoodsSalesRecordList($condition, $field = "*", $page = 0, $order = 'order_id desc')
  361. {
  362. $condition['order_state'] = array('in', array(ORDER_STATE_PAY, ORDER_STATE_SUCCESS));
  363. return $this->getOrderList($condition, $field, $page, $order);
  364. }
  365. }