<?php
/**
 * 入库管理Controller
 * Created by PhpStorm.
 * User: 小威
 * Date: 2019/11/11
 * Time: 18:30
 */

namespace JinDouYun\Controller\Stock;

use Exception;
use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\StatusCode;

use JinDouYun\Controller\BaseController;

use JinDouYun\Model\Stock\MInventoryIn;
use JinDouYun\Model\Stock\MInventory;


class InventoryIn extends BaseController
{
    private $objMInventoryIn;
    private $objMInventory;


    public function __construct($isCheckAcl = true, $isMustLogin = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin);
        $this->objMInventoryIn = new MInventoryIn($this->onlineEnterpriseId, $this->onlineUserId);
        $this->objMInventory = new MInventory($this->onlineEnterpriseId, $this->onlineUserId);


    }

    /**
     * 入库审核
     * @throws Exception
     */
    public function updateInventoryInStatus()
    {
        $paramsData = $this->request->getRawJson();
        if (empty($paramsData)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $params = [
            'id' => isset($paramsData['id']) ? $paramsData['id'] : '',//入库id
            'auditName' => isset($paramsData['auditName']) ? $paramsData['auditName'] : '',
        ];
        foreach ($params as $key => $value) {
            if (empty($value) && $value !== 0) {
                $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }
        $params['costAllocationType'] = isset($paramsData['costAllocationType']) ? $paramsData['costAllocationType'] : 0;
        $params['costAllocation'] = isset($paramsData['costAllocation']) ? $paramsData['costAllocation'] : 0;
        $params['remark'] = isset($paramsData['remark']) ? $paramsData['remark'] : '';
        $params['auditId'] = $this->onlineUserId;
        $params['inNumTotal'] = 0;
        if(isset($paramsData['details']) && !empty($paramsData['details'])){
            foreach($paramsData['details'] as $value){
                unset($value['isMaster']);
                unset($value['skuBarCode']);
                unset($value['categoryName']);
                unset($value['images']);
                unset($value['storage']);
                unset($value['isEq']);
                unset($value['_XID']);
                unset($value['unitId']);
                $value['productionData'] = isset($value['productionData']) ? $value['productionData'] : '';
                $value['inCostTotal'] = isset($value['inCostTotal']) ? bcmul($value['inNum'],$value['inCost'],2) : 0;
                $params['details'][] = $value;

                $params['inNumTotal'] += $value['inNum'];
            }
        }else{
            $this->sendOutput('details参数错误', ErrorCode::$paramError);
        }
        $result = $this->objMInventoryIn->updateInventoryInStatus($params);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 入库列表
     */
    public function getAllInventoryIn()
    {
        $params = $this->request->getRawJson();

        $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
        $selectParams['limit'] = $pageParams['limit'];
        $selectParams['offset'] = $pageParams['offset'];

        (isset($params['warehouseId']) && !empty($params['warehouseId'])) && $selectParams['warehouseId'] = $params['warehouseId'];
        (isset($params['auditStatus']) && !empty($params['auditStatus'])) && $selectParams['auditStatus'] = $params['auditStatus'];
        if(isset($params['type']) && !empty($params['type'])){
            $selectParams['type'] = $params['type'];
        }
        if(isset($params['start']) && !empty($params['start']) && isset($params['end']) && !empty($params['end'])){
            $selectParams['createTime'] = ['star' => $params['start'], 'end' => $params['end']];
        }

        if(isset($params['search']) && !empty($params['search'])){
            $selectParams['search']['no'] = $params['search'];
            $selectParams['search']['auditName'] = $params['search'];
        }

        !empty($this->shopId) && $selectParams['shopId'] = $this->shopId;
        $export = isset($params['export']) ? $params['export'] : 0;
        $result = $this->objMInventoryIn->getAllInventoryIn($selectParams,$export);

        if ($result->isSuccess()) {
            $returnData = $result->getData();
            $pageData = [
                'pageIndex' => $params['page'],
                'pageSize' => $params['pageSize'],
                'pageTotal' => $returnData['total'],
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        } else {
            parent::sendOutput($result->getData(), ErrorCode::$dberror);
        }
    }

    /**
     * 入库列表搜索
     */
    public function searchAllInventoryIn()
    {
        $params = $this->request->getRawJson();
        if( empty($params) ){
            $this->sendOutput('参数为空', ErrorCode::$paramError );
        }

        $selectParams = [
            'warehouseId' => isset($params['warehouseId']) ? $params['warehouseId'] : '',
            'type' => isset($params['type']) ? $params['type'] : '',
            'auditStatus' => isset($params['auditStatus']) ? $params['auditStatus'] : '',
            'start' => isset($params['start']) ? $params['start'] : '',
            'end' => isset($params['end']) ? $params['end'] : '',
            'search' => isset($params['search']) ? $params['search'] : '',

        ];
        $pageParams = pageToOffset($params['page'] ? : 1, $params['pageSize'] ? : 10);
        $selectParams['limit'] = $pageParams['limit'];
        $selectParams['offset'] = $pageParams['offset'];

        $selectParams['shopId'] = $this->shopId;
        $result = $this->objMInventoryIn->searchAllInventoryIn($selectParams);
        if($result->isSuccess()){
            $returnData = $result->getData();
            $pageData = [
                'pageIndex' => $params['page'],
                'pageSize' => $params['pageSize'],
                'pageTotal' => $returnData['total'],
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        }else{
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 入库统计
     * @throws Exception
     */
    public function statisticsAllInventoryIn()
    {
        $params = $this->request->getRawJson();
        if(empty($params)){
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $type = $params['type'];
        $modelResult = $this->objMInventoryIn->statisticsAllInventoryIn($type, $this->shopId);
        if(!$modelResult->isSuccess()){
            parent::sendOutput($modelResult->getData(),$modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());

    }

    /**
     * 入库成功列表(采购退货单用)
     * @throws Exception
     */
    public function getAllInventoryInByAudit()
    {
        $params = $this->request->getRawJson();
        $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
        $selectParams['limit'] = $pageParams['limit'];
        $selectParams['offset'] = $pageParams['offset'];
        $result = $this->objMInventoryIn->getAllInventoryInByAudit($selectParams);
        if ($result->isSuccess()) {
            $returnData = $result->getData();
            $pageData = [
                'pageIndex' => $params['page'],
                'pageSize' => $params['pageSize'],
                'pageTotal' => $returnData['total'],
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        } else {
            parent::sendOutput($result->getData(), ErrorCode::$dberror);
        }
    }

    /**
     * 入库单明细删除
     */
    public function deleteDetailByInventoryInId()
    {
        $params = $this->request->getRawJson();
        if(empty($params)){
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $params = [
            'inventoryInId' => getArrayItem($params, 'inventoryInId', 0),
            'inventoryInDetailIds' => getArrayItem($params, 'inventoryInDetailIds', 0),
        ];
        foreach ($params as $key => $value) {
            if (empty($value) && $value !== 0) {
                $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }
        $result = $this->objMInventoryIn->deleteDetailByInventoryInId($params);
        if(!$result->isSuccess()){
            parent::sendOutput($result->getData(),$result->getErrorCode());
        }
        parent::sendOutput($result->getData());
    }
    
    /**
     * 入库单驳回
     */
    public function rejectInventory()
    {
        $params = $this->request->getRawJson();
        if(empty($params)){
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $params = [
            'inventoryInId' => getArrayItem($params, 'inventoryInId', 0),//入库id
            'inventoryInDetailIds' => getArrayItem($params, 'inventoryInDetailIds', 0),//采购入库详情id
            
        ];
        foreach ($params as $key => $value) {
            if (empty($value) && $value !== 0) {
                $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }
        $result = $this->objMInventoryIn->rejectInventory($params);
        if(!$result->isSuccess()){
            parent::sendOutput($result->getData(),$result->getErrorCode());
        }
        parent::sendOutput($result->getData());
    }
    
    /**
     * 入库详情
     * @throws Exception
     */
    public function getInventoryInInfo()
    {
        $params['id'] = $this->request->param('request_id');

        $data = $this->request->getRawJson();
        $params['originId'] = isset($data['originId']) ? $data['originId'] : '';
        $params['source'] = isset($data['source']) ? $data['source'] : '';
        $where = [];
        if(isset($params['id']) && !empty($params['id'])){
            $where['id'] = $params['id'];
        }else{
            if(isset($params['originId']) && !empty($params['originId'])){
                $where['originId'] = $params['originId'];
                if(isset($params['source']) && !empty($params['source'])){
                    $where['source'] = $params['source'];
                }else{
                    $this->sendOutput('source参数为空', ErrorCode::$paramError);
                }
            }
        }
        if (empty($where)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }

        $result = $this->objMInventoryIn->getInventoryInInfo($where);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }


    /**
     * 格式化入库es
     */
    public function formatInventoryInEsData()
    {
        $params = $this->request->getRawJson();
        if (!isset($params['key']) || md5('123456123456') != $params['key']) {
            $this->sendOutput('参数校验失败', ErrorCode::$paramError);
        }

        $result = $this->objMInventoryIn->formatInventoryInEsData();
        if (!$result->isSuccess()) {
            parent::sendOutput($result->getData(),$result->getErrorCode());
        }
        parent::sendOutput($result->getData());
    }

    public function countPurchaseCost()
    {
        $params = $this->request->getRawJson();
        $data = [
            'money' => isset($params['money']) ? $params['money'] : 0,
            'type' => isset($params['type']) ? $params['type'] : '',
            'skuData' => isset($params['skuData']) ? $params['skuData'] : [],
        ];
        foreach($data as $key => $value){
            if(empty($value)){
                parent::sendOutput($key.'参数为空',ErrorCode::$paramError);
            }
        }

        $result = $this->objMInventoryIn->countPurchaseCost($data);
        if(!$result->isSuccess()){
            parent::sendOutput($result->getData(),$result->getErrorCode());
        }
        parent::sendOutput($result->getData());
    }
    
    
}