Package.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Model;
  4. class Package extends Model
  5. {
  6. protected $fillable=[
  7. 'owner_id','logistic_number','delivery_number','batch_number','batch_rule','recipient','recipient_mobile','logistic_id',
  8. 'measuring_machine_id','weight','length','width','height','bulk','paper_box_id','status','weighed_at'
  9. ];
  10. protected $appends=[
  11. 'owner_name',
  12. 'logistic_name',
  13. 'measuringMachine_name',
  14. 'measuring_machine_name',
  15. 'measuringMachine_status',
  16. 'paperBox_name',
  17. 'WMSReflectPackage_name'
  18. ];
  19. public function owner(){
  20. return $this->belongsTo('App\Owner','owner_id','id');
  21. }
  22. public function logistic(){
  23. return $this->belongsTo('App\Logistic','logistic_id','id');
  24. }
  25. public function measuringMachine(){
  26. return $this->belongsTo('App\MeasuringMachine','measuring_machine_id','id');
  27. }
  28. public function paperBox(){
  29. return $this->belongsTo('App\PaperBox','paper_box_id','id');
  30. }
  31. public function WMSReflectPackage(){
  32. return $this->hasOne('App\WMSReflectPackage','SOReference5','logistic_number');
  33. }
  34. public function getOwnerNameAttribute()
  35. {
  36. return $this['owner']? $this['owner']['name']:null;
  37. }
  38. public function getLogisticNameAttribute(){
  39. return $this['logistic']? $this['logistic']['name']:null;
  40. }
  41. public function getMeasuringMachineNameAttribute(){
  42. return $this['measuringMachine']? $this['measuringMachine']['name']:null;
  43. }
  44. public function getMeasuringMachineStatusAttribute(){
  45. return $this['measuringMachine']? $this['measuringMachine']['status']:null;
  46. }
  47. public function getPaperBoxNameAttribute(){
  48. return $this['paperBox']? $this['paperBox']['model']:null;
  49. }
  50. public function getWMSReflectPackageNameAttribute(){
  51. return $this['WMSReflectPackage']? $this['WMSReflectPackage']['TASKID']:null;
  52. }
  53. //寻找相近纸箱ID
  54. public function checkPaperBox($max,$centre,$min,$owner_id){
  55. $sumDiffer=0;
  56. $maxDiffer=0;
  57. $paperBox_id=null;
  58. $boxes=Owner::select('id')->with('paperBoxes')->find($owner_id);
  59. foreach ($boxes->paperBoxes as $i=>$paperBox){
  60. if ($paperBox->length==$max&&$paperBox->width==$centre&&$paperBox->height==$min){
  61. $paperBox_id=$paperBox->id;
  62. break;
  63. }
  64. $lengthDiffer=abs($paperBox->length-$max);
  65. $widthDiffer=abs($paperBox->width-$centre);
  66. $heightDiffer=abs($paperBox->height-$min);
  67. $thisMaxDiffer=($lengthDiffer>=($widthDiffer>=$heightDiffer?$widthDiffer:$heightDiffer)?$lengthDiffer:($widthDiffer>=$heightDiffer?$widthDiffer:$heightDiffer));
  68. if($i==0){
  69. $maxDiffer=$thisMaxDiffer;
  70. $sumDiffer=$lengthDiffer+$widthDiffer+$heightDiffer;
  71. $paperBox_id=$paperBox->id;
  72. }
  73. if ($thisMaxDiffer==$maxDiffer){
  74. if($sumDiffer>($lengthDiffer+$widthDiffer+$heightDiffer)){
  75. $sumDiffer=$lengthDiffer+$widthDiffer+$heightDiffer;
  76. $paperBox_id=$paperBox->id;
  77. }
  78. }
  79. if ($thisMaxDiffer<$maxDiffer){
  80. $sumDiffer=$lengthDiffer+$widthDiffer+$heightDiffer;
  81. $maxDiffer=$thisMaxDiffer;
  82. $paperBox_id=$paperBox->id;
  83. }
  84. }
  85. return $paperBox_id;
  86. }
  87. }