GoodCombind.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\model;
  4. use think\Model;
  5. /**
  6. * @mixin \think\Model
  7. */
  8. class GoodCombind extends Model
  9. {
  10. protected $createTime="createtime";
  11. protected $updateTime="updatetime";
  12. protected $append=["usable_stock"];
  13. // 定义全局的查询范围
  14. protected $globalScope = ['is_del'];
  15. public function scopeIsDel($query)
  16. {
  17. $query->where('is_del',0);
  18. }
  19. public function GoodZx(){
  20. return $this->belongsTo(GoodZixun::class,"childCode","spuCode");
  21. }
  22. public function GoodBasic(){
  23. return $this->belongsTo(GoodBasic::class,'childCode','spuCode');
  24. }
  25. public function getUsableStockAttr($v,$row){
  26. return (new GoodStock())->withJoin(['wsminfo'] , 'left')
  27. ->where(['spuCode'=>$row['childCode'] , 'wsm_type'=>[2 , 5]])
  28. ->sum('usable_stock');
  29. }
  30. public function AddStock($spuCode='',$num="0"){
  31. $comblist = $this->where(['spuCode'=>$spuCode,'is_del'=>0])->select()->toArray();
  32. if(empty($comblist))throw new \Exception("未找到对应子商品信息");
  33. $GoodStock=new GoodStock();
  34. $returnBn=[];
  35. foreach ($comblist as $item){
  36. $row =$GoodStock->withJoin(["wsminfo"],"left")
  37. ->field('id,usable_stock')
  38. ->where(['spuCode' => $item['childCode'], 'warehouse_info.wsm_type'=>[2,5]])
  39. ->findOrEmpty();
  40. if($row->isEmpty()) throw new \Exception('未找到对应子商品信息');
  41. $stocknum =bcmul($num,$item["child_num"]);
  42. $up =GoodStockInfo::StockBnSub($row->id,intval($stocknum));
  43. if(empty($up)) throw new \Exception('未找到对应子商品BN库存信息');
  44. $returnBn=array_merge($returnBn,$up);
  45. }
  46. return $returnBn;
  47. }
  48. /**
  49. * @param string $spuCode
  50. * @param string $bnCode
  51. * @param string $num
  52. * @param string $stockCode 库存变化申请编号
  53. * @param int $flag 0 解除库存 1 占用库存驳回
  54. * @return bool
  55. * @throws \think\db\exception\DataNotFoundException
  56. * @throws \think\db\exception\DbException
  57. * @throws \think\db\exception\ModelNotFoundException
  58. */
  59. public function subStock($spuCode='',$bnCode='',$num='0',$stockCode=''){
  60. $comblist = $this->where(['spuCode'=>$spuCode,'is_del'=>0])->select()->toArray();
  61. if(empty($comblist))throw new \Exception('未找到对应子商品信息');
  62. $goodinfo = new Good();
  63. $combind = new CombindBninfo();
  64. foreach ($comblist as $item){
  65. $stocknum =bcmul($num.'',$item['child_num']);
  66. $good = $goodinfo->where(["spuCode"=>$item['childCode']])->findOrEmpty();
  67. if($good->isEmpty()==false){
  68. $good->usable_stock = bcadd($good->usable_stock,$stocknum);
  69. $gdup=$good->save();
  70. if($gdup==false)throw new \Exception('子商品库存更新失败');
  71. }
  72. $up=$combind->subBN($spuCode,$bnCode,$item['childCode'],$stocknum,$stockCode);
  73. if($up==false)throw new \Exception('子商品库存更新失败');
  74. }
  75. return true;
  76. }
  77. }