123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?php
- declare (strict_types = 1);
- namespace app\admin\model;
- use think\Exception;
- use think\Model;
- /**
- * @mixin \think\Model
- */
- class GoodStockInfo extends Model
- {
- /**
- * @param int $stockid 仓库库存id
- * @param string $bn bn编号
- * @param int $num bn数量
- * @param float $origin_price 采购成本价
- * @return bool 入库bn库存数新建 或退回bn库存数
- */
- static function AddBn(int $stockid,string $bn,int $num,float $origin_price=0){
- $data=self::where(["stockid"=>$stockid,"bnCode"=>$bn])->findOrEmpty()->toArray();
- if(empty($data)){
- $data=[
- "stockid"=>$stockid,
- "bnCode"=>$bn,
- "total_num"=>$num,
- "used_num"=>0,
- "balance_num"=>$num,
- "origin_price"=>$origin_price,
- "addtime"=>date("Y-m-d H:i:s"),
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- }else{
- $total_num = $data["balance_num"]+$num;
- if($total_num> $data["total_num"]) throw new Exception("批次入库数量超过总数",1006);
- $data["balance_num"]=$num;
- $data["updatetime"]=date("Y-m-d H:i:s");
- }
- return (new self())->save($data);
- }
- /**
- * @param int $stockid
- * @param int $num
- * @return array 库存品下单消耗bn库存数
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- static function SaleBn(int $stockid,int $num){
- $arr = self::where([["stockid","=",$stockid],["balance_num",">",0]])->select()->toArray();
- if(empty($arr)) throw new Exception("库存批次数量不足","1006");
- $list=[];
- foreach ($arr as $item) {
- if($num<=0) break;
- $temp=[];
- if($item['balance_num']<=$num){
- $item['balance_num'] =0;
- $item['used_num'] +=$item['balance_num'];
- $num-=$item['balance_num'];
- $item['updatetime'] =date("Y-m-d H:i:s");
- $temp=["bnCode"=>$item['bnCode'],"num"=>$item['balance_num'],"origin_price"=>$item["origin_price"]];
- }else{
- $item['balance_num']-=$num;
- $item['used_num'] +=$num;
- $num=0;
- $item['updatetime'] =date("Y-m-d H:i:s");
- $temp=["bnCode"=>$item['bnCode'],"num"=>$num,"origin_price"=>$item["origin_price"]];
- }
- (new self())->save($item);
- $list[]=$temp;
- }
- return $list;
- }
- /**
- * @param int $stockid
- * @param string $bn
- * @param int $num
- * @return bool 盘点修改bn仓库库存数
- * @throws \think\Exception
- */
- static function CheckBn(int $stockid,string $bn,int $num){
- $data=self::where(["stockid"=>$stockid,"bnCode"=>$bn])->findOrEmpty()->toArray();
- if(empty($data)) throw new Exception("未找到Bn库存数据",1006);
- $data["balance_num"]=$num;
- $data["total_num"]=$data["used_num"]+$num;
- $data["updatetime"]=date("Y-m-d H:i:s");
- return (new self())->save($data);
- }
- /**
- * @param string $orderCode
- * @param int $stockid
- * @param int $num
- * @return int
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- static function OrderBn(string $orderCode,int $stockid,int $num){
- $bnArr=self::SaleBn($stockid,$num);
- if(empty($bnArr)) throw new Exception("未找到Bn库存数据",1006);
- $ordBn=[];
- foreach ($bnArr as $value){
- $saleBn=[
- "orderCode"=>$orderCode,
- "bnCode"=>$value['bnCode'],
- "stockid"=>$stockid,
- "num"=>$value['num'],
- "origin_price"=>$value['origin_price'],
- "addtime"=>date("Y-m-d H:i:s"),
- "updatetime"=>date("Y-m-d H:i:s"),
- ];
- $ordBn[]=$saleBn;
- }
- return self::name("sale_info")->insertAll($ordBn);
- }
- /**
- * @param string $returnCode
- * @param int $salebnid
- * @param int $num
- * @param int $type
- * @return bool
- * @throws \think\Exception
- */
- static function ReturnBn(string $returnCode,int $salebnid,int $num,int $type=1){
- $sabn=self::name("sale_info")->where(['id'=>$salebnid])->findOrEmpty()->toArray();
- if(empty($sabn))throw new Exception("未找到Bn订单数据",1006);
- $return=[
- "returnCode"=>$returnCode,
- "type"=>$type,
- "orderCode"=>$sabn['orderCode'],
- "bnCode"=>$sabn['bnCode'],
- "origin_price"=>$sabn['origin_price'],
- "num"=>$num,
- "addtime"=>date("Y-m-d H:i:s")
- ];
- return self::name("return_info")->save($return);
- }
- /** 调拨专用 需要注意bn总数变化
- * @param int $stockid 库存id
- * @param string $bnCode bn编号
- * @param int $num 库存数量
- * @param int $flag 操作类型 0 添加库存 1 减少库存
- * @param float|int $origin_price 成本价
- * @return bool
- * @throws \think\Exception
- */
- static function bnStock(int $stockid,string $bnCode,int $num,int $flag=0,float $origin_price=0){
- $stock =self::where(["stockid"=>$stockid,"bnCode"=>$bnCode])->lock(true)->findOrEmpty()->toArray();
- if($flag==1) {
- if(empty($stock)|| $stock['balance_num']< $num)
- throw new Exception("bn库存数不足",1006);
- else{
- $stock['balance_num']-=$num;
- $stock['total_num']-=$num;
- $stock['updatetime']=date("Y-m-d H:i:s");
- }
- }else{
- if (empty($stock)){
- $stock=[
- "bnCode"=>$bnCode,
- "stockid"=>$stockid,
- "total_num"=>$num,
- "used_num"=>0,
- "balance_num"=>$num,
- "origin_price"=>$origin_price,
- "addtime"=>date("Y-m-d H:i:s"),
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- }else{
- $stock['balance_num']+=$num;
- $stock['total_num']+=$num;
- $stock['updatetime']=date("Y-m-d H:i:s");
- }
- }
- return (new self())->save($stock);
- }
- }
|