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

namespace Store\Service;

use Doctrine\ORM\EntityManager;
use Purchase\Entity\WarehouseOrder;
use Stock\Entity\StockCheckGoods;
use Store\Entity\AllWarehouseOrderGoods;
use Store\Entity\Goods;
use Store\Entity\WarehouseGoods;

class AllWarehouseOrderGoodsManager
{
    private $entityManager;

    public function __construct(
        EntityManager $entityManager
    )
    {
        $this->entityManager = $entityManager;
    }

    /**
     * 添加其他入库商品
     * @param array $data
     * @param $warehouseId
     * @param $orderId
     * @return void
     * @throws \Doctrine\ORM\ORMException
     * @throws \Doctrine\ORM\OptimisticLockException
     * @throws \Doctrine\Persistence\Mapping\MappingException
     */
    public function addAllWarehouseOrderGoods(array $data, $warehouseId, $otherWarehouseOrderId,$warehouseOrderSn)
    {
        foreach ($data['goodsId'] as $key => $value) {
            $goodsInfo = $this->entityManager->getRepository(Goods::class)->findOneBy(['goodsId' => $value]);
            if($goodsInfo) {
                $allWarehouseGoods = new AllWarehouseOrderGoods();
                $allWarehouseGoods->setAllWarehouseOrderGoodsId(null);
                $allWarehouseGoods->setOtherWarehouseOrderId($otherWarehouseOrderId);
                $allWarehouseGoods->setWarehouseOrderSn($warehouseOrderSn);
                $allWarehouseGoods->setWarehouseId($warehouseId);
                $allWarehouseGoods->setWarehouseGoodsBuyNum($data['goodsBuyNum'][$key]);
                $allWarehouseGoods->setWarehouseGoodsPrice($data['goodsPrice'][$key]);
                $allWarehouseGoods->setWarehouseGoodsTax($data['goodsTax'][$key]);
                $allWarehouseGoods->setWarehouseGoodsAmount($data['goodsAmount'][$key]);
                $allWarehouseGoods->setGoodsId($value);
                $allWarehouseGoods->setGoodsName($goodsInfo->getGoodsName());
                $allWarehouseGoods->setGoodsNumber($goodsInfo->getGoodsNumber());
                $allWarehouseGoods->setGoodsSpec($goodsInfo->getGoodsSpec());
                $allWarehouseGoods->setGoodsUnit($goodsInfo->getOneUnit()->getUnitName());
                $allWarehouseGoods->setAddTime(time());
                $allWarehouseGoods->setOutNum($data['goodsBuyNum'][$key]);

                $this->entityManager->persist($allWarehouseGoods);
                $this->entityManager->flush();
                $this->entityManager->clear(AllWarehouseOrderGoods::class);
            }
        }
    }

    /**
     * 添加采购入库商品
     * @param $orderGoods
     * @param WarehouseOrder $warehouseOrder
     * @return bool
     */
    public function addAllPurchaseWarehouseOrderGoods($orderGoods, WarehouseOrder $warehouseOrder)
    {
        //$warehouseGoods = [];
        foreach ($orderGoods as $goodsValue) {
            $allWarehouseGoods = new AllWarehouseOrderGoods();
            $allWarehouseGoods->setAllWarehouseOrderGoodsId(null);
            $allWarehouseGoods->setPurchaseWarehouseOrderId($warehouseOrder->getWarehouseOrderId());
            $allWarehouseGoods->setWarehouseOrderSn($warehouseOrder->getWarehouseOrderSn());
            $allWarehouseGoods->setWarehouseId($warehouseOrder->getWarehouseId());
            $allWarehouseGoods->setWarehouseGoodsBuyNum($goodsValue->getPGoodsBuyNum());
            $allWarehouseGoods->setWarehouseGoodsPrice($goodsValue->getPGoodsPrice());
            $allWarehouseGoods->setWarehouseGoodsTax($goodsValue->getPGoodsTax());
            $allWarehouseGoods->setWarehouseGoodsAmount($goodsValue->getPGoodsAmount());
            $allWarehouseGoods->setGoodsId($goodsValue->getGoodsId());
            $allWarehouseGoods->setGoodsName($goodsValue->getGoodsName());
            $allWarehouseGoods->setGoodsNumber($goodsValue->getGoodsNumber());
            $allWarehouseGoods->setGoodsSpec($goodsValue->getGoodsSpec());
            $allWarehouseGoods->setGoodsUnit($goodsValue->getGoodsUnit());
            $allWarehouseGoods->setAddTime(time());
            $allWarehouseGoods->setOutNum($goodsValue->getPGoodsBuyNum());


            $this->entityManager->persist($allWarehouseGoods);
            $this->entityManager->flush();
            //$this->entityManager->clear(AllWarehouseOrderGoods::class);
        }
    }

    /**
     * 添加盘点入库商品
     * @param $orderGoods
     * @param WarehouseOrder $warehouseOrder
     * @return bool
     */
    public function addAllCheckWarehouseOrderGoods($warehouseId,  $stockCheckId,$warehouseOrderSn)
    {
        $stockCheckGoods = $this->entityManager->getRepository(StockCheckGoods::class)->findBy(['stockCheckId' => $stockCheckId]);
        foreach ($stockCheckGoods as $key => $value) {
            $goodsInfo = $this->entityManager->getRepository(Goods::class)->findOneBy(['goodsId' => $value->getgoodsId()]);
            $GoodsBuyNum=$value->getstockCheckAftGoodsNum() - $value->getstockCheckPreGoodsNum();
            if($goodsInfo && $GoodsBuyNum >0) {
                $allWarehouseGoods = new AllWarehouseOrderGoods();
                $allWarehouseGoods->setAllWarehouseOrderGoodsId(null);
                $allWarehouseGoods->setStockCheckId($stockCheckId);
                $allWarehouseGoods->setWarehouseId($warehouseId);
                $allWarehouseGoods->setWarehouseOrderSn($warehouseOrderSn);
                $allWarehouseGoods->setWarehouseGoodsBuyNum($GoodsBuyNum);
                $allWarehouseGoods->setWarehouseGoodsPrice($goodsInfo->getgoodsPrice());
                $allWarehouseGoods->setWarehouseGoodsTax(0);
                $allWarehouseGoods->setWarehouseGoodsAmount($goodsInfo->getgoodsPrice() * $GoodsBuyNum);
                $allWarehouseGoods->setGoodsId($value->getgoodsId());
                $allWarehouseGoods->setGoodsName($goodsInfo->getGoodsName());
                $allWarehouseGoods->setGoodsNumber($goodsInfo->getGoodsNumber());
                $allWarehouseGoods->setGoodsSpec($goodsInfo->getGoodsSpec());
                $allWarehouseGoods->setGoodsUnit($goodsInfo->getOneUnit()->getUnitName());
                $allWarehouseGoods->setAddTime(time());
                $allWarehouseGoods->setOutNum($GoodsBuyNum);
                $this->entityManager->persist($allWarehouseGoods);
                $this->entityManager->flush();

            }
        }
    }
}