<?php
/**
 * Created by PhpStorm.
 * User: boyer
 * Date: 2020/8/1
 * Time: 20:36
 */
class Process_BillBook{
    private $billBookInfo;
    private $referenceNo='';
    protected $note='';
    /**
     * Note:设置参考单号
     * User: Yao
     * Date: 2020/8/3
     * Time: 14:10
     * @param $referenceNo
     * @return $this
     */
    public function setReferenceNo($referenceNo){
        $this->referenceNo = $referenceNo;
        return $this;
    }

    /**
     * Note:设置备注
     * User: Yao
     * Date: 2020/8/3
     * Time: 14:10
     * @param $note
     * @return $this
     */
    public function setNote($note){
        $this->note = $note;
        return $this;
    }

    /**
     * Note:一定要加事务，枷锁
     * User: Yao
     * Date: 2020/8/1
     * Time: 21:39
     * @param $billRecodrNo
     * @throws Exception
     */
    public function setBillBookFromNo($billRecodrNo,$accountNo=''){
        if(empty($billRecodrNo)){
            throw new Exception('账册序号不能为空');
        }
        if(empty($accountNo)){
            throw new Exception('账册号不能为空');
        }
        $condition = [
            'recorde_no'=>$billRecodrNo,
            'warehous_account_no'=>$accountNo,
        ];
        $billBook = Service_BillBook::getByCondition($condition);
        if(empty($billBook)){
            throw new Exception('账册号【'.$accountNo.'】备案序号【'.$billRecodrNo.'】账册信息不存在');
        }
        $billBookLock = Service_BillBook::getByIdForUpdate($billBook[0]['id']);
        $this->billBookInfo = $billBookLock;
        return $this;
    }
    public function setBillBookFromId($id){
        $billBookLock = Service_BillBook::getByIdForUpdate($id);
        if($billBookLock['state']!=0){
            throw new Exception('账册号【'.$billBookLock['warehous_account_no'].'】备案序号【'.$billBookLock['recorde_no'].'】账册信息不是可用状态');
        }
        $this->billBookInfo = $billBookLock;
        return $this;
    }
    /**
     * Note:冻结库存
     * User: Yao
     * Date: 2020/8/1
     * Time: 20:37
     * @param $billId
     * @param $quantity
     */
    public function forozenQuantity($quantity){
        $frozenQuantity = $this->billBookInfo['frozen_quantity'] + $quantity;
        $canUseQuantity = $this->billBookInfo['can_use_quantity']-$quantity;
        if($canUseQuantity<0){
            throw new Exception('账册可用库存数量不足->序号'.$this->billBookInfo['recorde_no']."料号:".$this->billBookInfo['goods_code']."可用库存:{$this->billBookInfo['can_use_quantity']},出货数量:{$quantity}");
        }
        if($this->billBookInfo['state']!=0){
            throw new Exception('账册号【'.$this->billBookInfo['warehous_account_no'].'】备案序号【'.$this->billBookInfo['recorde_no'].'】账册信息不是可用状态');
        }
        Service_BillBook::update([
            'frozen_quantity'=>$frozenQuantity,
            'can_use_quantity'=>$canUseQuantity,
        ],$this->billBookInfo['id']);
        Service_BillStockLog::add([
            'warehous_account_no'=>$this->billBookInfo['warehous_account_no'],
            'recode_no'=>$this->billBookInfo['recorde_no'],
            'incre_fronzen_quantity'=>$quantity,
            'reduce_quantity'=>$quantity,
            'reference_no'=>$this->referenceNo,
            'add_time'=>date("Y-m-d H:i:s"),
            'note'=>$this->note,
        ]);
        return $this;
    }
    /**
     * Note:解冻库存，要加事务
     * User: Yao
     * Date: 2020/8/1
     * Time: 21:41
     * @param $quantity
     * @throws Exception
     */
    public function restoreQuantiy($quantity){
        $frozenQuantity = $this->billBookInfo['frozen_quantity'] - $quantity;
        $canUseQuantity = $this->billBookInfo['can_use_quantity']+$quantity;
        if($frozenQuantity<0){
            throw new Exception('备案序号【'.$this->billBookInfo['recorde_no'].'】账册冻结库存异常');
        }
        Service_BillBook::update([
            'frozen_quantity'=>$frozenQuantity,
            'can_use_quantity'=>$canUseQuantity,
        ],$this->billBookInfo['id']);
        Service_BillStockLog::add([
            'warehous_account_no'=>$this->billBookInfo['warehous_account_no'],
            'recode_no'=>$this->billBookInfo['recorde_no'],
            'reduce_fronzen_quantity'=>$quantity,
            'incre_quantity'=>$quantity,
            'reference_no'=>$this->referenceNo,
            'add_time'=>date("Y-m-d H:i:s"),
            'note'=>$this->note,
        ]);
        return $this;
    }
    /**
     * Note:增加可用库存，要加事务
     * User: Yao
     * Date: 2020/8/4
     * Time: 13:55
     * @param $quantity
     * @return $this
     * @throws Exception
     */
    public function increQuanity($quantity){
        $canUseQuantity = $this->billBookInfo['can_use_quantity']+$quantity;
        $stockQuantity = $this->billBookInfo['stock_quantity']+$quantity;
        if($canUseQuantity<0){
            throw new Exception('备案序号【'.$this->billBookInfo['recorde_no'].'】账册冻结库存异常');
        }
        Service_BillBook::update([
            'can_use_quantity'=>$canUseQuantity,
            'stock_quantity'=>$stockQuantity,
        ],$this->billBookInfo['id']);
        Service_BillStockLog::add([
            'warehous_account_no'=>$this->billBookInfo['warehous_account_no'],
            'recode_no'=>$this->billBookInfo['recorde_no'],
            'incre_quantity'=>$quantity,
            'reference_no'=>$this->referenceNo,
            'add_time'=>date("Y-m-d H:i:s"),
            'note'=>$this->note,
        ]);
        return $this;
    }
    /**
     * Note:核减可用库存，要加事务
     * User: Yao
     * Date: 2020/8/5
     * Time: 9:35
     * @param $quantity
     * @return $this
     * @throws Exception
     */
    public function reduceQuanity($quantity){
        $canUseQuantity = $this->billBookInfo['can_use_quantity']-$quantity;
        $stockQuantity = $this->billBookInfo['stock_quantity']-$quantity;
        if($canUseQuantity<0){
            throw new Exception('备案序号【'.$this->billBookInfo['recorde_no'].'】账册可用库存异常');
        }
        if($this->billBookInfo['state']!=0){
            throw new Exception('账册号【'.$this->billBookInfo['warehous_account_no'].'】备案序号【'.$this->billBookInfo['recorde_no'].'】账册信息不是可用状态');
        }
        Service_BillBook::update([
            'can_use_quantity'=>$canUseQuantity,
            'stock_quantity'=>$stockQuantity,
        ],$this->billBookInfo['id']);
        Service_BillStockLog::add([
            'warehous_account_no'=>$this->billBookInfo['warehous_account_no'],
            'recode_no'=>$this->billBookInfo['recorde_no'],
            'reduce_quantity'=>$quantity,
            'reference_no'=>$this->referenceNo,
            'add_time'=>date("Y-m-d H:i:s"),
            'note'=>$this->note,
        ]);
        return $this;
    }
    /**
     * Note:锁定库存，要加事务
     * User: Yao
     * Date: 2020/8/8
     * Time: 10:10
     * @param $quantity
     * @return $this
     * @throws Exception
     */
    public function lockQuantity($quantity){
        $canUseQuantity = $this->billBookInfo['can_use_quantity']-$quantity;
        $lockQuantity = $this->billBookInfo['lock_quantity']+$quantity;
        if($canUseQuantity<0){
            throw new Exception('账册可用库存异常-->账册序号:'.$this->billBookInfo['recorde_no']."料号->{$this->billBookInfo['goods_code']}");
        }
        if($this->billBookInfo['state']!=0){
            throw new Exception('账册号【'.$this->billBookInfo['warehous_account_no'].'】备案序号【'.$this->billBookInfo['recorde_no'].'】账册信息不是可用状态');
        }
        Service_BillBook::update([
            'can_use_quantity'=>$canUseQuantity,
            'lock_quantity'=>$lockQuantity,
        ],$this->billBookInfo['id']);
        Service_BillStockLog::add([
            'warehous_account_no'=>$this->billBookInfo['warehous_account_no'],
            'recode_no'=>$this->billBookInfo['recorde_no'],
            'incre_lock_quantity'=>$quantity,
            'reduce_quantity'=>$quantity,
            'reference_no'=>$this->referenceNo,
            'add_time'=>date("Y-m-d H:i:s"),
            'note'=>$this->note,
        ]);
        return $this;
    }
    /**
     * Note:解除锁定库存，要加事务
     * User: Yao
     * Date: 2020/8/8
     * Time: 10:11
     * @param $quantity
     * @return $this
     * @throws Exception
     */
    public function unlockQuantity($quantity){
        $canUseQuantity = $this->billBookInfo['can_use_quantity']+$quantity;
        $lockQuantity = $this->billBookInfo['lock_quantity']-$quantity;
        if($lockQuantity<0){
            throw new Exception('账册锁定库存异常');
        }
        Service_BillBook::update([
            'can_use_quantity'=>$canUseQuantity,
            'lock_quantity'=>$lockQuantity,
        ],$this->billBookInfo['id']);
        Service_BillStockLog::add([
            'warehous_account_no'=>$this->billBookInfo['warehous_account_no'],
            'recode_no'=>$this->billBookInfo['recorde_no'],
            'reduce_lock_quantity'=>$quantity,
            'incre_quantity'=>$quantity,
            'reference_no'=>$this->referenceNo,
            'add_time'=>date("Y-m-d H:i:s"),
            'note'=>$this->note,
        ]);
        return $this;
    }
    /**
     * Note:核销出库
     * User: Yao
     * Date: 2020/8/8
     * Time: 10:47
     * @param $quantity
     * @return $this
     * @throws Exception
     */
    public function destory($quantity){
        $lockQuantity= $this->billBookInfo['lock_quantity']-$quantity;
        $outQuantity = $this->billBookInfo['out_quantity']+$quantity;
        if($lockQuantity<0){
            throw new Exception('账册锁定库存异常');
        }
        if($this->billBookInfo['state']!=0){
            throw new Exception('账册号【'.$this->billBookInfo['warehous_account_no'].'】备案序号【'.$this->billBookInfo['recorde_no'].'】账册信息不是可用状态');
        }
        Service_BillBook::update([
            'lock_quantity'=>$lockQuantity,
            'out_quantity'=>$outQuantity,
        ],$this->billBookInfo['id']);
        Service_BillStockLog::add([
            'warehous_account_no'=>$this->billBookInfo['warehous_account_no'],
            'recode_no'=>$this->billBookInfo['recorde_no'],
            'incre_desotry_quantity'=>$quantity,
            'reduce_lock_quantity'=>$quantity,
            'reference_no'=>$this->referenceNo,
            'add_time'=>date("Y-m-d H:i:s"),
            'note'=>$this->note,
        ]);
        return $this;
    }
    /**
     * Note:bbc核注，出区
     * User: Yao
     * Date: 2020/8/13
     * Time: 10:51
     * @param $quantity
     * @return $this
     * @throws Exception
     */
    public function bbcDestory($quantity){
        $frozenQuantity= $this->billBookInfo['frozen_quantity']-$quantity;
        $outQuantity = $this->billBookInfo['out_quantity']+$quantity;
        if($frozenQuantity<0){
            throw new Exception('账册冻结库存异常');
        }
        if($this->billBookInfo['state']!=0){
            throw new Exception('账册号【'.$this->billBookInfo['warehous_account_no'].'】备案序号【'.$this->billBookInfo['recorde_no'].'】账册信息不是可用状态');
        }
        Service_BillBook::update([
            'frozen_quantity'=>$frozenQuantity,
            'out_quantity'=>$outQuantity,
        ],$this->billBookInfo['id']);
        Service_BillStockLog::add([
            'warehous_account_no'=>$this->billBookInfo['warehous_account_no'],
            'recode_no'=>$this->billBookInfo['recorde_no'],
            'reduce_fronzen_quantity'=>$quantity,
            'incre_desotry_quantity'=>$quantity,
            'reference_no'=>$this->referenceNo,
            'add_time'=>date("Y-m-d H:i:s"),
            'note'=>$this->note,
        ]);
        return $this;
    }
    /**
     * Note:直接出库
     * User: Yao
     * Date: 2020/8/8
     * Time: 10:47
     * @param $quantity
     * @return $this
     * @throws Exception
     */
    public function districtOut($quantity){
        $canUseQuantity= $this->billBookInfo['can_use_quantity']-$quantity;
        $outQuantity = $this->billBookInfo['out_quantity']+$quantity;
        if($canUseQuantity<0){
            throw new Exception('账册可用库存异常【序号'.$this->billBookInfo['recorde_no'].'】账册号:'.$this->billBookInfo['warehous_account_no'].'可用库存:'.$this->billBookInfo['can_use_quantity']."出库数量:".$quantity);
        }
        if($this->billBookInfo['state']!=0){
            throw new Exception('账册号【'.$this->billBookInfo['warehous_account_no'].'】备案序号【'.$this->billBookInfo['recorde_no'].'】账册信息不是可用状态');
        }
        Service_BillBook::update([
            'can_use_quantity'=>$canUseQuantity,
            'out_quantity'=>$outQuantity,
        ],$this->billBookInfo['id']);
        Service_BillStockLog::add([
            'warehous_account_no'=>$this->billBookInfo['warehous_account_no'],
            'recode_no'=>$this->billBookInfo['recorde_no'],
            'reduce_quantity'=>$quantity,
            'incre_desotry_quantity'=>$quantity,
            'reference_no'=>$this->referenceNo,
            'add_time'=>date("Y-m-d H:i:s"),
            'note'=>$this->note,
        ]);
        return $this;
    }
}