<?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,$warehouseOrderInfo)
    {
        foreach ($data['goodsId'] as $key => $value) {
            $goodsInfo = $this->entityManager->getRepository(Goods::class)->findOneBy(['goodsId' => $value]);
            if($goodsInfo) {
                $allWarehouseGoods = new AllWarehouseOrderGoods();
                $allWarehouseGoods->setAllWarehouseOrderGoodsId(null);
                $allWarehouseGoods->setWarehouseOrderId($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->setWarehouseOrderInfo($warehouseOrderInfo);
                $allWarehouseGoods->setAddTime(time());
                $allWarehouseGoods->setOutNum($data['goodsBuyNum'][$key]);
                $allWarehouseGoods->setType("other");

                $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->setWarehouseOrderId($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->setWarehouseOrderInfo($warehouseOrder->getWarehouseOrderInfo());
            $allWarehouseGoods->setAddTime(time());
            $allWarehouseGoods->setOutNum($goodsValue->getPGoodsBuyNum());
            $allWarehouseGoods->setType("purchase");

            $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,$warehouseOrderInfo)
    {
        $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->setWarehouseOrderId($stockCheckId);
                $allWarehouseGoods->setWarehouseId($warehouseId);
                $allWarehouseGoods->setWarehouseOrderSn($warehouseOrderSn);
                $allWarehouseGoods->setWarehouseGoodsBuyNum($GoodsBuyNum);
                $allWarehouseGoods->setWarehouseGoodsPrice(empty($goodsInfo->getgoodsPrice())?0:$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->setWarehouseOrderInfo($warehouseOrderInfo);
                $allWarehouseGoods->setAddTime(time());
                $allWarehouseGoods->setOutNum($GoodsBuyNum);
                $allWarehouseGoods->setType("check");

                $this->entityManager->persist($allWarehouseGoods);
                $this->entityManager->flush();

            }
        }
    }
}