<?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 Store\Entity\AllWarehouseOrderGoods;
use Store\Entity\Goods;

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)
    {
        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->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->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);
        }
    }
}