<?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 Sales\Entity\SalesOrderGoods;
use Sales\Entity\SalesSendOrder;
use Stock\Entity\ExWarehouseOrderGoods;
use Store\Entity\AllExWarehouseOrderGoods;
use Store\Entity\AllWarehouseOrderGoods;
use Store\Entity\Goods;

class AllExWarehouseOrderGoodsManager
{
    private $entityManager;

    private $allWarehouseOrderGoods;

    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 addExWarehouseOrderGoods(array $data, $warehouseId, $orderId,$warehouseOrderSn=null)
    {
        foreach ($data['goodsId'] as $key => $value) {
            $goodsInfo = $this->entityManager->getRepository(Goods::class)->findOneBy(['goodsId' => $value]);
            $goodsExNum=$data['goodsExNum'][$key];
            if($warehouseOrderSn){
                $orderGoodsInfo = $this->entityManager->getRepository(AllWarehouseOrderGoods::class)->findWarehouseGoodsIdSearch(['goods_id' => $value,'warehouse_order_sn'=>$warehouseOrderSn,'warehouse_id'=>$warehouseId,'out_num'=>1]);
            }else{
                $orderGoodsInfo = $this->entityManager->getRepository(AllWarehouseOrderGoods::class)->findWarehouseGoodsIdSearch(['goods_id' => $value,'warehouse_id'=>$warehouseId,'out_num'=>1]);
            }

            foreach ($orderGoodsInfo as $k=>$v){
                if ($goodsInfo && $goodsExNum>0) {
                    $exWarehouseGoods = new AllExWarehouseOrderGoods();
                    $exWarehouseGoods->setAllExWarehouseOrderGoodsId(null);
                    $exWarehouseGoods->setExWarehouseOrderId($orderId);
                    $exWarehouseGoods->setExWarehouseOrderSn($v->getwarehouseOrderSn());
                    $exWarehouseGoods->setWarehouseId($warehouseId);
                    if($goodsExNum >= $v->getoutNum()){
                        $exWarehouseGoods->setWarehouseGoodsExNum($v->getoutNum());
                        $exWarehouseGoods->setWarehouseGoodsTax(0);
                        $exWarehouseGoods->setWarehouseGoodsAmount($v->getoutNum() * $data['goodsPrice'][$key] + $data['goodsTax'][$key]);
                        $this->entityManager->getRepository(AllWarehouseOrderGoods::class)->updateWarehouseGoodsStock(['goodsId' => $value,'allWarehouseOrderGoodsId'=>$v->getallWarehouseOrderGoodsId(),'sendNum'=>$v->getoutNum()]);
                        $goodsExNum = $goodsExNum - $v->getoutNum();
                    }else{
                        $exWarehouseGoods->setWarehouseGoodsExNum($goodsExNum);
                        $exWarehouseGoods->setWarehouseGoodsTax($data['goodsTax'][$key]);
                        $exWarehouseGoods->setWarehouseGoodsAmount($goodsExNum * $data['goodsPrice'][$key] + $data['goodsTax'][$key]);
                        $this->entityManager->getRepository(AllWarehouseOrderGoods::class)->updateOutNum(['goodsId' => $value,'allWarehouseOrderGoodsId'=>$v->getallWarehouseOrderGoodsId(),'sendNum'=>$goodsExNum]);
                        $goodsExNum=0;
                    }

                    $exWarehouseGoods->setWarehouseGoodsPrice($data['goodsPrice'][$key]);
                    $exWarehouseGoods->setGoodsId($value);
                    $exWarehouseGoods->setGoodsName($goodsInfo->getGoodsName());
                    $exWarehouseGoods->setGoodsNumber($goodsInfo->getGoodsNumber());
                    $exWarehouseGoods->setGoodsSpec($goodsInfo->getGoodsSpec());
                    $exWarehouseGoods->setGoodsUnit($goodsInfo->getOneUnit()->getUnitName());
                    $exWarehouseGoods->setAddTime(time());
                    $exWarehouseGoods->setAllWarehouseOrderId($v->getallWarehouseOrderGoodsId());
                    $exWarehouseGoods->setCostPrice($v->getwarehouseGoodsPrice());

                    $this->entityManager->persist($exWarehouseGoods);
                    $this->entityManager->flush();
                    $this->entityManager->clear(AllExWarehouseOrderGoods::class);
                }
            }
        }
    }

    /**
     * 添加销售商品
     * @param array $data
     * @param $warehouseId
     * @param $orderId
     * @return void
     * @throws \Doctrine\ORM\ORMException
     * @throws \Doctrine\ORM\OptimisticLockException
     * @throws \Doctrine\Persistence\Mapping\MappingException
     */
    public function addSalesWarehouseOrderGoods(array $data, SalesSendOrder $sendOrder,$warehouseOrderSn=null)
    {
        foreach ($data as $value) {
            $goodsInfo = $this->entityManager->getRepository(Goods::class)->findOneBy(['goodsId' => $value['goodsId']]);
            $salesOrder = $this->entityManager->getRepository(SalesOrderGoods::class)->findOneBy(['goodsId' => $value['goodsId'],'salesOrderId'=>$sendOrder->getSalesOrderId()]);

            $goodsExNum=$value['sendNum'];
            if($warehouseOrderSn){
                $orderGoodsInfo = $this->entityManager->getRepository(AllWarehouseOrderGoods::class)->findWarehouseGoodsIdSearch(['goods_id' => $value['goodsId'],'warehouse_order_sn'=>$warehouseOrderSn,'warehouse_id'=>$value['warehouseId'],'out_num'=>1]);
            }else{
                $orderGoodsInfo = $this->entityManager->getRepository(AllWarehouseOrderGoods::class)->findWarehouseGoodsIdSearch(['goods_id' => $value['goodsId'],'warehouse_id'=>$value['warehouseId'],'out_num'=>1]);
            }

            foreach ($orderGoodsInfo as $k=>$v){
                if ($goodsInfo && $goodsExNum>0) {
                    $exWarehouseGoods = new AllExWarehouseOrderGoods();
                    $exWarehouseGoods->setAllExWarehouseOrderGoodsId(null);
                    $exWarehouseGoods->setSendOrderId($sendOrder->getSendOrderId());
                    $exWarehouseGoods->setExWarehouseOrderSn($v->getwarehouseOrderSn());
                    $exWarehouseGoods->setWarehouseId($value['warehouseId']);
                    if($goodsExNum >= $v->getoutNum()){
                        $exWarehouseGoods->setWarehouseGoodsExNum($v->getoutNum());
                        $exWarehouseGoods->setWarehouseGoodsTax(0);
                        $exWarehouseGoods->setWarehouseGoodsAmount($v->getoutNum() * $salesOrder->getsalesGoodsPrice() + $salesOrder->getsalesGoodsTax());

                        $this->entityManager->getRepository(AllWarehouseOrderGoods::class)->updateOutNum(['goodsId' => $value,'allWarehouseOrderGoodsId'=>$v->getallWarehouseOrderGoodsId(),'sendNum'=>$v->getoutNum()]);
                        $goodsExNum = $goodsExNum - $v->getoutNum();
                    }else{
                        $exWarehouseGoods->setWarehouseGoodsExNum($goodsExNum);
                        $exWarehouseGoods->setWarehouseGoodsTax($salesOrder->getsalesGoodsTax());
                        $exWarehouseGoods->setWarehouseGoodsAmount($goodsExNum * $salesOrder->getsalesGoodsPrice() + $salesOrder->getsalesGoodsTax());
                        $this->entityManager->getRepository(AllWarehouseOrderGoods::class)->updateOutNum(['goodsId' => $value,'allWarehouseOrderGoodsId'=>$v->getallWarehouseOrderGoodsId(),'sendNum'=>$goodsExNum]);
                        $goodsExNum=0;
                    }

                    $exWarehouseGoods->setWarehouseGoodsPrice($salesOrder->getsalesGoodsPrice());
                    $exWarehouseGoods->setGoodsId($value);
                    $exWarehouseGoods->setGoodsName($goodsInfo->getGoodsName());
                    $exWarehouseGoods->setGoodsNumber($goodsInfo->getGoodsNumber());
                    $exWarehouseGoods->setGoodsSpec($goodsInfo->getGoodsSpec());
                    $exWarehouseGoods->setGoodsUnit($goodsInfo->getOneUnit()->getUnitName());
                    $exWarehouseGoods->setAddTime(time());
                    $exWarehouseGoods->setAllWarehouseOrderId($v->getallWarehouseOrderGoodsId());
                    $exWarehouseGoods->setCostPrice($v->getwarehouseGoodsPrice());

                    $this->entityManager->persist($exWarehouseGoods);
                    $this->entityManager->flush();
                    $this->entityManager->clear(AllExWarehouseOrderGoods::class);
                }
            }

        }

    }

}