vr_order.model.php 13 KB

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