<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/16
 * Time: 20:46
 */

namespace app\mobile\controller;


class Sellerorder extends MobileSeller
{
    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
    }

    public function order_list()
    {
        $model_order = Model('order');
        $order_list = $model_order->getStoreOrderList($this->store_info['store_id'], $_POST['order_sn'], $_POST['buyer_name'], $_POST['state_type'], $_POST['query_start_date'], $_POST['query_end_date'], $_POST['skip_off'], '*', array('order_goods'));
        output_data(array('order_list' => $order_list), mobile_page($model_order->page_info));
    }

    /**
     * 取消订单
     */
    public function order_cancel()
    {
        $order_id = intval($_POST['order_id']);
        $reason = $_POST['reason'];
        $model_order = Model('order');
        $condition = array();
        $condition['order_id'] = $order_id;
        $condition['store_id'] = $this->store_info['store_id'];
        $order_info = $model_order->getOrderInfo($condition);

        $if_allow = $model_order->getOrderOperateState('store_cancel', $order_info);
        if (!$if_allow) {
            output_error('无权操作');
        }

        if (TIMESTAMP - 86400 < $order_info['api_pay_time']) {
            $_hour = ceil(($order_info['api_pay_time'] + 86400 - TIMESTAMP) / 3600);
            output_error('该订单曾尝试使用第三方支付平台支付，须在' . $_hour . '小时以后才可取消');
        }

        if ($order_info['order_type'] == 2) {
            //预定订单
            $result = Logic('order_book')->changeOrderStateCancel($order_info, 'seller', $this->seller_info['seller_name'], $reason);
        }
        else {
            $cancel_condition = array();
            if ($order_info['payment_code'] != 'offline') {
                $cancel_condition['order_state'] = ORDER_STATE_NEW;
            }
            $result = Logic('order')->changeOrderStateCancel($order_info, 'seller', $this->seller_info['seller_name'], $reason, true, $cancel_condition);
        }

        if (!$result['code']) {
            output_error($result['msg']);
        }
        output_data('1');
    }

    /**
     * 修改运费
     */
    public function order_ship_price()
    {
        $order_id = intval($_POST['order_id']);
        $shipping_fee = dsPriceFormat($_POST['shipping_fee']);
        $model_order = Model('order');
        $condition = array();
        $condition['order_id'] = $order_id;
        $condition['store_id'] = $this->store_info['store_id'];
        $order_info = $model_order->getOrderInfo($condition);

        $if_allow = $model_order->getOrderOperateState('modify_price', $order_info);
        if (!$if_allow) {
            output_error('无权操作');
        }
        $result = Logic('order')->changeOrderShipPrice($order_info, 'seller', $this->seller_info['seller_name'], $shipping_fee);

        if (!$result['code']) {
            output_error($result['msg']);
        }
        output_data('1');
    }

    /**
     * 发货
     */
    public function order_deliver_send()
    {
        $order_id = intval($_POST['order_id']);
        $model_order = Model('order');
        $condition = array();
        $condition['order_id'] = $order_id;
        $condition['store_id'] = $this->store_info['store_id'];
        $order_info = $model_order->getOrderInfo($condition, array('order_common', 'order_goods'));
        $if_allow_send = intval($order_info['lock_state']) || !in_array($order_info['order_state'], array(
                ORDER_STATE_PAY, ORDER_STATE_SEND
            ));
        if ($if_allow_send) {
            output_error('无权操作');
        }

        $result = Logic('order')->changeOrderSend($order_info, 'seller', $this->seller_info['seller_name'], $_POST);
        if (!$result['code']) {
            output_error($result['msg']);
        }
        output_data('1');
    }
}