Order.php 21.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
<?php

namespace Home;

use LibModels\Web\Home\OrderData;
use Configs\ChannelConfig;
use WebPlugin\Helpers;
use WebPlugin\Images;

/**
 * 个人中心-我的订单逻辑处理
 */
class OrderModel
{

    /**
     * 获取我的订单列表数据-分页
     * @param type $uid
     * @param type $page
     * @param type $limit
     * @param type $type 获取订单类型 type=1全部,type=2待付款,type=3待发货,type=4待收货,type=5待评论(已成功) 7取消
     */
    public static function getOrders($uid, $page, $limit, $type, $isPage = FALSE)
    {
        $orders = array();
        $orderInfo = OrderData::getUserOrders($uid, $page, $limit, $type);
        if (isset($orderInfo['data']['order_list']) && $orderInfo['data']['order_list']) {
            $url = Helpers::url('/product/pro', '', 'item');
            foreach ($orderInfo['data']['order_list'] as $orderK => $orderV) {
                $orders[$orderK]['orderNum'] = $orderV['order_code']; //订单标识
                $orders[$orderK]['orderTime'] = date("Y-m-d h:i:s", $orderV['create_time']); //下单时间
                $statusInfo = self::getOrderStatus($orderV['is_cancel'], $orderV['status'], $orderV['payment_type'], $orderV['payment_status']);
                //订单状态
                if ($statusInfo['cancel']) {
                    $orders[$orderK]['cancel'] = $statusInfo['cancel'];
                }
                else {
                    if (!empty($statusInfo['keyName'])) {
                        $orders[$orderK][$statusInfo['keyName']] = TRUE;
                        //已发货,物流信息
                        if ($statusInfo['keyName'] == 'shipped') {
                            $expressInfo = self::getExpressInfo($orderV['order_code'], $uid, $orderV['payment_type'], $orderV['create_time']);
                            $orders[$orderK]['logistics'] = $expressInfo['logistics'];
                        }
                    }
                }
                //订单商品相关信息
                if (isset($orderV['order_goods']) && $orderV['order_goods']) {
                    $goods = array();
                    foreach ($orderV['order_goods'] as $gokey => $goval) {
                        $goods[$gokey]['href'] = $url . '_' . $goval['product_id'] . '_' . $goval['goods_id'] . '/' . $goval['cn_alphabet'] . '.html';
                        $goods[$gokey]['thumb'] = Images::getImageUrl($goval['goods_image'], 100, 100);
                        $goods[$gokey]['name'] = $goval['product_name'];
                        $goods[$gokey]['color'] = $goval['color_name'];
                        $goods[$gokey]['size'] = $goval['size_name'];
                        $goods[$gokey]['price'] = $goval['goods_price'];
                        $goods[$gokey]['count'] = $goval['buy_number'];
//                        $goods[$gokey]['refundStatus'] = (intval($goval['refund_num']) > 0) ? true : false;
                        $goodsTagName = self::getGoodsTag($orderV['attribute'], $goval['goods_type']);
                        if ($goodsTagName) {
                            $goods[$gokey][$goodsTagName] = TRUE;
                        }
                        $orders[$orderK]['goods'] = $goods;
                    }
                    $orders[$orderK]['pay'] = $orderV['amount']; //付款数
                    $orders[$orderK]['fregit'] = $orderV['shipping_cost']; //邮费
                }
                //操作
                $orders[$orderK]['operation'] = self::getOperateInfo($orderV['attribute'], $orderV['is_cancel'], $orderV['status'], $orderV['payment_status'], $orderV['update_time'], $orderV['order_type'], $orderV['refund_status'], $orderV['payment_type'], $orderV['order_code']);
            }
            if ($isPage) {
                $orders['pager']['total'] = $orderInfo['data']['total'];
                $orders['pager']['pageTotal'] = $orderInfo['data']['page_total'];
                $orders['pager']['page'] = $orderInfo['data']['page'];
            }
        }
        else {
            $orders = array('empty' => '您没有成功订单!');
        }
        return $orders;
    }

    /**
     * 我的订单-tabs
     * @param type $type
     * @return boolean
     */
    public static function getOrderTabs($type)
    {
        $tabs = array(
            array(
                'url' => Helpers::url('/home/orders', array('page' => 1, 'type' => 1)),
                'name' => '现有订单'
            ),
            array(
                'url' => Helpers::url('/home/orders', array('page' => 1, 'type' => 5)),
                'name' => '成功订单'
            ),
            array(
                'url' => Helpers::url('/home/orders', array('page' => 1, 'type' => 7)),
                'name' => '已取消订单'
            ),
            array(
                'url' => Helpers::url('/home/orders', array('page' => 1, 'type' => 8)),
                'name' => '历史订单'
            )
        );
        switch ($type) {
            case 1:
                $tabs[0]['active'] = true;
                break;
            case 5:
                $tabs[1]['active'] = true;
                break;
            case 7:
                $tabs[2]['active'] = true;
                break;
            case 8:
                $tabs[3]['active'] = true;
                break;
            default:
                $tabs[0]['active'] = true;
                break;
        }
        return $tabs;
    }

    /**
     * 详情页
     * @param type $orderCode
     */
    public static function getOrderDetail($uid, $orderCode)
    {
        $detail = array();
        $orderInfo = OrderData::getOrderDetail($uid, $orderCode);
        $orderDetail = $orderInfo['data'];
//        var_dump($orderDetail);
//        die();
        if ($orderDetail) {
            $detail['orderNum'] = $orderDetail['order_code'];
            $detail['statusStr'] = $orderDetail['status_str'];
            //订单是否已完成
            $detail['complete'] = false;
            if ($orderDetail['status'] == 6) {
                $detail['complete'] = true;
            }
            //未取消订单,进度
            if ($orderDetail['attribute'] == 3) {
                $detail['progress'] = self::getVirtualPro($orderDetail['is_cancel'], $orderDetail['status'], $orderDetail['create_time']);
            }
            else {
                $detail['progress'] = self::getNormalPro($orderDetail['is_cancel'], $orderDetail['status'], $orderDetail['create_time']);
            }
            //物流信息
            $detail['traceOrder']['orderDate'] = date('Y.m.d H:m:s', $orderDetail['create_time']);
            $expressInfo = self::getExpressInfo($orderCode, $uid, $orderDetail['payment_type'], $orderDetail['create_time']);
            $detail['traceOrder']['logistics'] = $expressInfo['logistics']; //物流信息
            $detail['traceOrder']['logisticsCompany'] = $expressInfo['logisticsCompany'];
            $detail['traceOrder']['courierNumbe'] = $expressInfo['courierNumbe'];
            //虚拟商品
            if ($orderDetail['attribute'] == 3) {
                $detail['virtualGoods'] = TRUE;
                $detail['virtualPayMode']['payMode'] = ChannelConfig::$payType[$orderDetail['payment_type']];
                $detail['virtualPayMode']['phone'] = $orderDetail['mobile'];
            }
            else {
                $detail['virtualGoods'] = FALSE;
                $detail['noramlPayMode']['payMode'] = ChannelConfig::$payType[$orderDetail['payment_type']];
                $detail['noramlPayMode']['payWay'] = $orderDetail['payment_name'];
                $detail['noramlPayMode']['deliverTime'] = ChannelConfig::$deliverTimeSpan[$orderDetail['delivery_time']];
                //配送信息
                $detail['orderInfo']['receiver'] = $orderDetail['user_name'];
                $detail['orderInfo']['address'] = $orderDetail['area'] . $orderDetail['address'];
                $detail['orderInfo']['phone'] = $orderDetail['mobile'] . (!empty($orderDetail['phone']) ? ',' . $orderDetail['phone'] : '');
            }
            //商品信息
            if ($orderDetail['order_goods']) {
                $url = Helpers::url('/product/pro', '', 'item');
                foreach ($orderDetail['order_goods'] as $gok => $gov) {
                    $detail['goods'][$gok]['url'] = $url . '_' . $gov['product_id'] . '_' . $gov['goods_id'] . '/' . $gov['cn_alphabet'] . '.html';
                    $detail['goods'][$gok]['img'] = Images::getImageUrl($gov['goods_image'], 60, 60);
                    $detail['goods'][$gok]['name'] = $gov['product_name'];
                    $detail['goods'][$gok]['color'] = $gov['color_name'];
                    $detail['goods'][$gok]['size'] = $gov['size_name'];
                    $detail['goods'][$gok]['price'] = $gov['goods_price'];
                    $detail['goods'][$gok]['coin'] = $gov['yoho_give_coin'];
                    $detail['goods'][$gok]['num'] = $gov['buy_number'];
                    $detail['goods'][$gok]['sum'] = $gov['goods_amount'];
                    $detail['goods'][$gok]['sku'] = $gov['product_sku'];
                    $goodsTagName = self::getGoodsTag($orderDetail['attribute'], $gov['goods_type']);
                    if ($goodsTagName) {
                        $detail['goods'][$gok][$goodsTagName] = TRUE;
                    }
                }
            }
            $detail['sumPrice'] = $orderDetail['goods_total_amount'];
            if (!strstr($orderDetail['shipping_cost'], '0.00')) {
                $detail['freight'] = $orderDetail['shipping_cost'];
            }
            if ($orderDetail['attribute'] == 3) {
                $detail['yohoCoin'] = 'YOHO币使用:' . floatval(substr($orderDetail['yoho_coin_num'], 3)) * 100;
                $detail['yohoCoin'].='个';
            }
            else {
                $detail['yohoCoin'] = 'YOHO币抵扣:' . $orderDetail['yoho_coin_num'] . '元';
            }
            $detail['yohoCoinUrl'] = Helpers::url('/help', array('category_id' => 87)); //什么是yoho币介绍
            if (!strstr($orderDetail['coupons_amount'], '0.00')) {
                $detail['coupons'] = $orderDetail['coupons_amount'];
            }
            $detail['payment'] = $orderDetail['amount'];
            $detail['remark'] = ''; //$orderDetail['remark']
            $detail['operation'] = self::getOrderDetailOp($orderDetail['order_code'], $orderDetail['payment'], $orderDetail['status'], $orderDetail['is_cancel'], $orderDetail['payment_status'], $orderDetail['payment_type'], 1, $orderDetail['attribute'], 'N'); //,$orderDetail['order_type_value']
        }
        return $detail;
    }

   /**
    * 获取虚拟商品二维码图片
    * @param type $uid
    * @param type $sku
    */
    public static function getTicketImgs($uid,$sku)
    {
        $data = OrderData::getYohoodTicket($uid, $sku);
//        var_dump($data);
//        die();
    }

    /**
     * 订单详情页物流
     * 
     */
    protected static function getExpressInfo($orderCode, $uid, $paymetType, $createTime)
    {
        $result = array();
        if ($paymetType == 1) {
            $result['logisticsCompany'] = '';
            $result['courierNumbe'] = '';
            $result['logistics'][] = date('Y-m-d H:i:s', $createTime) . ' ' . '您的订单已提交,等待付款';
        }
        else if ($paymetType == 2) {
            $result['logisticsCompany'] = '';
            $result['courierNumbe'] = '';
            $result['logistics'][] = date('Y-m-d H:i:s', $createTime) . ' ' . '您的订单已提交,等待审核';
        }
        else {
            if (isset($orderCode) && is_numeric($uid)) {
                $logistics = OrderData::getLogisticsData($orderCode, $uid);
                if (!empty($logistics['data'])) {
                    $result['logisticsUrl'] = $logistics['data']['url'];
                    $result['logisticsImg'] = $logistics['data']['logo'];
                    $result['logisticsCompany'] = $logistics['data']['caption'];
                    $result['courierNumbe'] = $logistics['data']['express_number'];
                    $expressDetail = $logistics['data']['express_detail'];
                    if (isset($expressDetail) && $expressDetail) {
                        foreach ($expressDetail as $value) {
                            $result['logistics'][] = $value['acceptTime'] . ' ' . $value['accept_address'];
                        }
                    }
                }
            }
        }
        return $result;
    }

    /**
     * 订单详情页操作步骤
     */
    protected static function getOrderDetailOp($orderCode, $payment, $status, $isCancel, $paymentStatus, $paymentType, $orderType, $attribute, $refundStatus)
    {
        $operation = array();
        //立刻付款
        if ($paymentType == 1 && $paymentStatus == 'N' && $isCancel == 'N' && isset($payment) && $payment != 19) {
            $operation +=array('goPay' => Helpers::url('/shopping/pay', array('ordercode' => $orderCode)));
        }
        //取消订单
        if ($status < 3 && $isCancel == 'N' && $paymentStatus == 'N' && $orderType != 5) {
            $operation +=array('cancelOrder' => TRUE);
        }
        //订单已支付
        if ($paymentType == 1 && $paymentStatus == 'Y' && $status < 6) {
            $operation +=array('paid' => TRUE);
        }
        //确认收货
        if ($status >= 4 && $status < 6 && $refundStatus == 0 && $attribute != 3 && $isCancel == 'N') {
            $operation[] += array('shipped' => TRUE);
        }
        //订单已取消
        if ($isCancel == 'Y') {
            $operation +=array('cancel' => TRUE);
        }
        //虚拟查看二维码
        if ($attribute == 3) {
            $operation+=array('checkQrCode' => Helpers::url('/home/orders/ticket', array('orderCode' => $orderCode)));
        }
        return $operation;
    }

    /**
     * 详情页进度条-普通商品
     * @param type $isCancel
     * @param type $status
     * @param type $createTime
     * @return boolean
     */
    protected static function getNormalPro($isCancel, $status, $createTime)
    {
        $process['middleStatus'] = array(
            array(
                'name' => '1. 提交订单',
                'date' => date('Y.m.d H:m:s', $createTime),
            ),
            array(
                'name' => '2. 商品出库'
            ),
            array(
                'name' => '3. 等待收获'
            ),
            array(
                'name' => '4. 交易完成'
            )
        );
        //订单已取消
        if ($isCancel == 'N') {
            if ($status == 0) {
                $process['percent'] = '25%';
                $process['middleStatus'][0]['cur'] = TRUE;
            }
            else if ($status > 0 && $status < 4) {
                $process['percent'] = '50%';
                $process['middleStatus'][1]['cur'] = TRUE;
            }
            else if ($status >= 4 && $status < 6) {
                $process['percent'] = '75%';
                $process['middleStatus'][2]['cur'] = TRUE;
            }
            elseif ($status == 6) {
                $process['percent'] = '100%';
                $process['middleStatus'][3]['cur'] = TRUE;
            }
        }
        return $process;
    }

    /**
     * 详情页进度条-虚拟商品
     * @param type $isCancel
     * @param type $status
     * @param type $createTime
     * @return boolean
     */
    protected static function getVirtualPro($isCancel, $status, $createTime)
    {
        $process['middleStatus'] = array(
            array(
                'name' => '1. 提交订单',
                'date' => date('Y.m.d H:m:s', $createTime),
            ),
            array(
                'name' => '2. 已发货'
            ),
            array(
                'name' => '3. 交易完成'
            )
        );
        //订单已取消
        if ($isCancel == 'N') {
            if ($status == 0) {
                $process['percent'] = '30%';
                $process['middleStatus'][0]['cur'] = TRUE;
            }
            else if ($status > 0 && $status < 6) {
                $process['percent'] = '80%';
                $process['middleStatus'][1]['cur'] = TRUE;
            }
            elseif ($status == 6) {
                $process['percent'] = '100%';
                $process['middleStatus'][3]['cur'] = TRUE;
            }
        }
        return $process;
    }

    /**
     * 订单状态
     * @param type $isCancel
     * @param type $status
     * @param type $payType
     * @param type $payStatus
     * @return string
     */
    protected static function getOrderStatus($isCancel, $status, $payType, $payStatus)
    {
        //初始化:未取消,待付款
        $ret = array('cancel' => FALSE, 'keyName' => 'noPay');
        //订单已取消
        if ($isCancel == 'Y') {
            $ret['cancel'] = TRUE;
        }
        else {
            switch ($status) {
                case 0:
                    //'订单已成功,等待付款'
                    if ($payType != 2 && $payStatus == 'N') {
                        $ret['keyName'] = 'noPay';
                    }
                    //'订单已付款,等待备货中'
                    elseif ($payType != 2 && $payStatus == 'Y') {
                        $ret['keyName'] = 'paid';
                    }
                    //'订单已成功,等待备货中'
                    elseif ($payType == 2 && $payStatus == 'N') {
                        $ret['keyName'] = 'complete';
                    }
                    break;
                case 1:
                    //'订单已付款,等待备货中'
                    $ret['keyName'] = 'paid';
                    break;
                case 2:
                    //'订单备货中'
                    $ret['keyName'] = '';
                    break;
                case 3:
                    //'订单配货中'
                    $ret['keyName'] = '';
                    break;
                case 4:
                    //'订单已发货'
                    $ret['keyName'] = 'shipped';
                    break;
                case 5:
                    //'运输中'
                    $ret['keyName'] = '';
                    break;
                case 6:
                    //'交易完成';
                    $ret['keyName'] = 'reback';
                    break;
            }
        }
        return $ret;
    }

    /**
     * 操作栏
     * @param type $attribute
     * @param type $isCancel
     * @param type $status
     * @param type $payStatus
     * @param type $updateTime
     * @param type $orderType
     * @param type $refundStatus
     * @return array
     */
    protected static function getOperateInfo($attribute, $isCancel, $status, $payStatus, $updateTime, $orderType, $refundStatus, $paymentType, $orderCode)
    {
        //查看订单
        $orderDetailUrl = Helpers::url('/home/orders/detail', array('orderCode' => $orderCode));
        //查看二维码
        $ticketUrl = Helpers::url('/home/orders/tickets', array('orderCode' => $orderCode));
        //立即付款
        $payUrl = Helpers::url('/shopping/pay', array('ordercode' => $orderCode));
        //取消订单
        $cancelOrderUrl = 'javascript:void(0)';
        //确认订单
        $confirmOrderUrl = 'javascript:void(0)';
        //申请换货
        $exchangeUrl = Helpers::url('/home/returns/exchangeRequest', array('orderCode' => $orderCode));
        //申请退货
        $refundUrl = Helpers::url('/home/returns/refundrequest', array('orderCode' => $orderCode));
        //立即付款
        if ($payStatus == 'N' && $paymentType != 2 && $isCancel == 'N') {
            $operation[] = array('payNow' => true, 'href' => $payUrl);
        }

        $operation[] = array('href' => $orderDetailUrl, 'name' => '查看订单');
        //查看订单,虚拟订单查看二维码
        if ($attribute == 3) {
            if ($payStatus == 'Y') {
                $operation[] = array('href' => $ticketUrl, 'name' => '查看二维码');
            }
        }

        //取消订单
        if ($status < 3 && $isCancel == 'N' && $orderType != 5 && $payStatus == 'N') {
            $operation[] = array('href' => $cancelOrderUrl, 'name' => '取消订单', 'cancelOrder' => true);
        }
        //确认收货
        if ($status >= 4 && $status < 6 && $refundStatus == 0 && $attribute != 3 && $isCancel == 'N') {
            $operation[] = array('href' => $confirmOrderUrl, 'name' => '确认收货', 'confirmReceived' => true);
        }
        //换货
        $time = (time() - $updateTime);
        $orderExchangeLimitTime = ChannelConfig::$exchangeDay;
        if ($status >= 6 && $time < 86400 * $orderExchangeLimitTime && $attribute != 3 && $isCancel == 'N') {
            $operation[] = array('href' => $exchangeUrl, 'name' => '申请换货');
        }
        //退货
        $orderRefundLimitTime = ChannelConfig::$refundDay;
        if ($status >= 6 && $time < 86400 * $orderRefundLimitTime && $attribute != 3 && $isCancel == 'N') {
            $operation[] = array('href' => $refundUrl, 'name' => '申请退货');
        }
        return $operation;
    }

    /**
     * 商品属性标签
     * @param type $attribute
     * @param type $goodsType
     * @return string
     */
    protected static function getGoodsTag($attribute, $goodsType)
    {
        $goodsTagName = '';
        switch ($goodsType) {
            //赠品
            case 'gift':
            case 'price_gift':
            case 'free':
                $goodsTagName = 'freebie';
                break;
            //加价购
            case 'outlet':
                $goodsTagName = 'advanceBuy';
                break;
            //预售
            case 'advance':
                $goodsTagName = 'preSaleGood';
                break;
            default:
                break;
        }
        if ($attribute == 3) {
            $goodsTagName = 'virtualGood';
        }
        return $goodsTagName;
    }

}