Commodity.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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',"type","pack"];
  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 getNameAttribute($value){
  16. return str_replace(array("\r\n","\n","\r","\"","&quot;"),' ',$value);
  17. }
  18. public function barcodes()
  19. {
  20. return $this->hasMany('\App\CommodityBarcode');
  21. }
  22. public function owner(){
  23. return $this->belongsTo('App\Owner','owner_id','id');
  24. }
  25. public function getBarcodeAttribute(){
  26. return $this->barcodes[0]['code']??'';
  27. }
  28. public function getOwnerNameAttribute(){
  29. return $this->owner['name']??'';
  30. }
  31. public function getOwnerCodeAttribute(){
  32. return $this->owner['code']??'';
  33. }
  34. static function newCommodityBy_BarcodeOwnerIdNameSku($barcode,$ownerId,$name,$sku){
  35. $barcodes=rtrim($barcode,',');
  36. $barcodes=explode(',',$barcodes);
  37. foreach ($barcodes as $k=>$barcode){
  38. if(!trim($barcode)) unset($barcodes[$k]);
  39. }
  40. $commodities=[];
  41. foreach ($barcodes as $barcode){
  42. if(!trim($barcode))continue;
  43. $commodity=Commodity::whereHas('barcodes', function (Builder $query)use($barcode){
  44. $query->where('code',$barcode);
  45. })->where('name',$name)->where('owner_id',$ownerId)->first();
  46. if($commodity)$commodities[]=$commodity;
  47. }
  48. if(count($barcodes)==count($commodities)&&count($commodities)>0){//筛选掉仅有一个条码相等的。
  49. $commodity=$commodities[0];
  50. }else{
  51. $commodity=null;
  52. }
  53. if(!$commodity){
  54. $commodity=new Commodity(['name'=>$name,'sku'=>$sku,'owner_id'=>$ownerId]);
  55. $commodity->save();
  56. }else{
  57. if(!isset($commodity['name'])||(!isset($commodity['sku'])||$sku)){
  58. $commodity['name']=$name;
  59. $commodity['sku']=$sku;
  60. $commodity->save();
  61. }
  62. }
  63. foreach ($barcodes as $barcode){
  64. if(!trim($barcode))continue;
  65. $commodityBarcode=CommodityBarcode::where('code',$barcode)->where('commodity_id',$commodity['id'])->first();
  66. if(!$commodityBarcode){
  67. $commodityBarcode=new CommodityBarcode(['code'=>$barcode,'commodity_id'=>$commodity['id']]);
  68. $commodityBarcode->save();
  69. }
  70. }
  71. return $commodity;
  72. }
  73. public function newBarcode($barcode){
  74. $barcodeModel = $this->barcodes()->where('code', $barcode)->first();
  75. if(!$barcodeModel){
  76. return CommodityBarcode::query()->create(['commodity_id'=>$this['id'],'code'=>$barcode]);
  77. }
  78. return $barcodeModel;
  79. }
  80. }