Commodity.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Builder;
  4. use Illuminate\Database\Eloquent\Model;
  5. use App\Traits\ModelTimeFormat;
  6. class Commodity extends Model
  7. {
  8. use ModelTimeFormat;
  9. protected $fillable=['name','sku','owner_id','created_at','length','width','height','volumn'];
  10. protected $appends=['barcode'];
  11. // protected $appends=['barcode','owner_name','owner_code'];
  12. public function setNameAttribute($value){
  13. $this->attributes['name']=str_replace(PHP_EOL,'',$value);
  14. }
  15. public function barcodes()
  16. {
  17. return $this->hasMany('\App\CommodityBarcode');
  18. }
  19. public function owner(){
  20. return $this->belongsTo('App\Owner','owner_id','id');
  21. }
  22. public function getBarcodeAttribute(){
  23. return $this->barcodes[0]['code']??'';
  24. }
  25. public function getOwnerNameAttribute(){
  26. return $this->owner['name']??'';
  27. }
  28. public function getOwnerCodeAttribute(){
  29. return $this->owner['code']??'';
  30. }
  31. static function newCommodityBy_BarcodeOwnerIdNameSku($barcode,$ownerId,$name,$sku){
  32. $barcodes=rtrim($barcode,',');
  33. $barcodes=explode(',',$barcodes);
  34. foreach ($barcodes as $k=>$barcode){
  35. if(!trim($barcode)) unset($barcodes[$k]);
  36. }
  37. $commodities=[];
  38. foreach ($barcodes as $barcode){
  39. if(!trim($barcode))continue;
  40. $commodity=Commodity::whereHas('barcodes', function (Builder $query)use($barcode){
  41. $query->where('code',$barcode);
  42. })->where('name',$name)->where('owner_id',$ownerId)->first();
  43. if($commodity)$commodities[]=$commodity;
  44. }
  45. if(count($barcodes)==count($commodities)&&count($commodities)>0){//筛选掉仅有一个条码相等的。
  46. $commodity=$commodities[0];
  47. }else{
  48. $commodity=null;
  49. }
  50. if(!$commodity){
  51. $commodity=new Commodity(['name'=>$name,'sku'=>$sku,'owner_id'=>$ownerId]);
  52. $commodity->save();
  53. }else{
  54. if(!isset($commodity['name'])||(!isset($commodity['sku'])||$sku)){
  55. $commodity['name']=$name;
  56. $commodity['sku']=$sku;
  57. $commodity->save();
  58. }
  59. }
  60. foreach ($barcodes as $barcode){
  61. if(!trim($barcode))continue;
  62. $commodityBarcode=CommodityBarcode::where('code',$barcode)->where('commodity_id',$commodity['id'])->first();
  63. if(!$commodityBarcode){
  64. $commodityBarcode=new CommodityBarcode(['code'=>$barcode,'commodity_id'=>$commodity['id']]);
  65. $commodityBarcode->save();
  66. }
  67. }
  68. return $commodity;
  69. }
  70. public function newBarcode($barcode){
  71. $barcodeModel = $this->barcodes()->where('code', $barcode)->first();
  72. if(!$barcodeModel){
  73. return CommodityBarcode::query()->create(['commodity_id'=>$this['id'],'code'=>$barcode]);
  74. }
  75. return $barcodeModel;
  76. }
  77. }