<?php
/**
 * DBERP 进销存系统
 *
 * ==========================================================================
 * @link      http://www.dberp.net/
 * @copyright 北京珑大钜商科技有限公司，并保留所有权利。
 * @license   http://www.dberp.net/license.html License
 * ==========================================================================
 *
 * @author    静静的风 <baron@loongdom.cn>
 *
 */

namespace Sales\Validator;

use Laminas\I18n\Translator\Translator;
use Store\Entity\AllWarehouseOrderGoods;
use Store\Entity\WarehouseGoods;
use Laminas\Validator\AbstractValidator;

class SendOrderWarehouseValidator extends AbstractValidator
{
    const NOT_SCALAR            = 'notScalar';
    const WAREHOUSE_NOT_STOCK   = 'warehouseNotStock';
    const GOODS_SN_NOT_WAREHOUSE   = 'goodsSnNotWarehouse';
    const GOODS_ORDER_SN_NOT_WAREHOUSE   = 'goodsOrderSnNotWarehouse';

    protected $messageTemplates = [];

    private $entityManager;
    private $sendOrderGoods;

    public function __construct($options)
    {
        $this->entityManager    = $options['entityManager'];
        $this->sendOrderGoods   = $options['sendOrderGoods'];

        $trans = new Translator();
        $this->messageTemplates = [
            self::NOT_SCALAR        => $trans->translate("这不是一个标准输入值"),
            self::WAREHOUSE_NOT_STOCK => $trans->translate("库存不足，无法发货"),
            self::GOODS_SN_NOT_WAREHOUSE        => $trans->translate("商品入库单号在仓库中不存在"),
            self::GOODS_ORDER_SN_NOT_WAREHOUSE  => $trans->translate("该商品入库单号在仓库中的数量小于出库量"),
        ];

        parent::__construct($options);
    }

    public function isValid($value,$context=null): bool
    {
        if (!is_array($value)) {
            $this->error(self::NOT_SCALAR);
            return false;
        }

        if(empty($this->sendOrderGoods)) return false;

        foreach ($this->sendOrderGoods as $goodsValue) {
            if(!isset($value[$goodsValue->getGoodsId()])) {
                $this->error(self::WAREHOUSE_NOT_STOCK);
                return false;
            }

            $stockNum = $this->entityManager->getRepository(WarehouseGoods::class)->findMoreWarehouseGoodsNum($value[$goodsValue->getGoodsId()], $goodsValue->getGoodsId());
            if($goodsValue->getSalesGoodsSellNum() > $stockNum) {
                $this->error(self::WAREHOUSE_NOT_STOCK);
                return false;
            }

            if($context['warehouseOrderSn']){
                $allWarehouseGoods=$this->entityManager->getRepository(AllWarehouseOrderGoods::class)->findWarehouseGoodsIdSearch(['goods_id' => $goodsValue->getGoodsId(),'warehouse_order_sn'=>$context['warehouseOrderSn'],'warehouseIds'=>$value[$goodsValue->getGoodsId()],'out_num'=>1]);
                if (!$allWarehouseGoods) {
                    $this->error(self::GOODS_SN_NOT_WAREHOUSE);
                    return false;
                }
                $WarehouseGoodsStock=0;
                foreach ($allWarehouseGoods as $k=>$v){
                    $WarehouseGoodsStock += $v->getoutNum();
                }
                if ($WarehouseGoodsStock < $goodsValue->getSalesGoodsSellNum()) {
                    $this->error(self::GOODS_ORDER_SN_NOT_WAREHOUSE);
                    return false;
                }
            }

        }

        return true;
    }
}