GoodBasic.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\report\model;
  4. use think\facade\Db;use think\Model;
  5. /**
  6. * @mixin \think\Model
  7. */
  8. class GoodBasic extends Model
  9. {
  10. protected $append=["spec_info","cat_info","usable_stock","total_stock","wait_in_stock","wait_out_stock","child"];
  11. public function unit(){
  12. return $this->belongsTo(Unit::class,"good_unit")->bind(["unit_name"=>"unit"]);
  13. }
  14. public function brand(){
  15. return $this->belongsTo(Brand::class,'brand_id')->bind(['brand_name']);
  16. }
  17. public function getSpecInfoAttr($v,$row){
  18. return (new GoodSpec())->with(["spec","specinfo"])->where(["spuCode"=>$row['spuCode']])->field("spec_id,spec_value_id")->select();
  19. }
  20. public function getCatInfoAttr($v,$row){
  21. return made($row['cat_id']);
  22. }
  23. public function getTotalStockAttr($v,$row){
  24. return (new GoodStock())->withJoin(['WareInfo'],"left")->where(['spuCode'=>$row['spuCode'],"wsm_type"=>[5,4]])
  25. ->sum(Db::raw("usable_stock+wait_out_stock"));
  26. }
  27. public function getUsableStockAttr($v,$row){
  28. return (new GoodStock())->withJoin(['WareInfo'],'left')->where(['spuCode'=>$row['spuCode'],'wsm_type'=>[5,4]])
  29. ->sum("usable_stock");
  30. }
  31. public function getWaitInStockAttr($v,$row){
  32. return (new GoodStock())->withJoin(['WareInfo'],'left')->where(['spuCode'=>$row['spuCode'],'wsm_type'=>[5,4]])
  33. ->sum('wait_in_stock');
  34. }
  35. public function getWaitOutStockAttr($v,$row){
  36. return (new GoodStock())->withJoin([' WareInfo'],'left')->where(['spuCode'=>$row['spuCode'],'wsm_type'=>[5,4]])
  37. ->sum('wait_out_stock');
  38. }
  39. }