<?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,$exWarehouseOrderSn=null,$exWarehouseOrderInfo=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],"DESC");
            }else{
                $orderGoodsInfo = $this->entityManager->getRepository(AllWarehouseOrderGoods::class)->findWarehouseGoodsIdSearch(['goods_id' => $value,'warehouse_id'=>$warehouseId,'out_num'=>1],"DESC");
            }

            foreach ($orderGoodsInfo as $k=>$v){
                if ($goodsInfo && $goodsExNum>0) {
                    $exWarehouseGoods = new AllExWarehouseOrderGoods();
                    $exWarehouseGoods->setAllExWarehouseOrderGoodsId(null);
                    $exWarehouseGoods->setExWarehouseOrderId($orderId);
                    $exWarehouseGoods->setExWarehouseOrderSn($exWarehouseOrderSn);
                    $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)->updateOutNum(['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->setExWarehouseOrderInfo($exWarehouseOrderInfo);
                    $exWarehouseGoods->setAddTime(time());
                    $exWarehouseGoods->setType("ex");
                    $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,$exWarehouseOrderInfo=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],"DESC");
            }else{
                $orderGoodsInfo = $this->entityManager->getRepository(AllWarehouseOrderGoods::class)->findWarehouseGoodsIdSearch(['goods_id' => $value['goodsId'],'warehouse_id'=>$value['warehouseId'],'out_num'=>1],"DESC");
            }

            foreach ($orderGoodsInfo as $k=>$v){
                if ($goodsInfo && $goodsExNum>0) {
                    $exWarehouseGoods = new AllExWarehouseOrderGoods();
                    $exWarehouseGoods->setAllExWarehouseOrderGoodsId(null);
                    $exWarehouseGoods->setExWarehouseOrderId($sendOrder->getSendOrderId());
                    $exWarehouseGoods->setExWarehouseOrderSn($sendOrder->getSendOrderSn());
                    $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['goodsId']);
                    $exWarehouseGoods->setGoodsName($goodsInfo->getGoodsName());
                    $exWarehouseGoods->setGoodsNumber($goodsInfo->getGoodsNumber());
                    $exWarehouseGoods->setGoodsSpec($goodsInfo->getGoodsSpec());
                    $exWarehouseGoods->setExWarehouseOrderInfo($exWarehouseOrderInfo);
                    $exWarehouseGoods->setAddTime(time());
                    $exWarehouseGoods->setType("send");
                    $exWarehouseGoods->setAllWarehouseOrderId($v->getallWarehouseOrderGoodsId());
                    $exWarehouseGoods->setCostPrice($v->getwarehouseGoodsPrice());

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

        }

    }

}