Package.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. 'measuringMachine_status',
  15. 'paperBox_name',
  16. 'WMSReflectPackage_name'
  17. ];
  18. public function owner(){
  19. return $this->belongsTo('App\Owner','owner_id','id');
  20. }
  21. public function logistic(){
  22. return $this->belongsTo('App\Logistic','logistic_id','id');
  23. }
  24. public function measuringMachine(){
  25. return $this->belongsTo('App\MeasuringMachine','measuring_machine_id','id');
  26. }
  27. public function paperBox(){
  28. return $this->belongsTo('App\PaperBox','paper_box_id','id');
  29. }
  30. public function WMSReflectPackage(){
  31. return $this->hasOne('App\WMSReflectPackage','SOReference5','logistic_number');
  32. }
  33. public function getOwnerNameAttribute()
  34. {
  35. return $this['owner']? $this['owner']['name']:null;
  36. }
  37. public function getLogisticNameAttribute(){
  38. return $this['logistic']? $this['logistic']['name']:null;
  39. }
  40. public function getMeasuringMachineNameAttribute(){
  41. return $this['measuringMachine']? $this['measuringMachine']['name']:null;
  42. }
  43. public function getMeasuringMachineStatusAttribute(){
  44. return $this['measuringMachine']? $this['measuringMachine']['status']:null;
  45. }
  46. public function getPaperBoxNameAttribute(){
  47. return $this['paperBox']? $this['paperBox']['model']:null;
  48. }
  49. public function getWMSReflectPackageNameAttribute(){
  50. return $this['WMSReflectPackage']? $this['WMSReflectPackage']['TASKID']:null;
  51. }
  52. //寻找相近纸箱ID
  53. public function checkPaperBox($max,$centre,$min,$owner_id){
  54. $sumDiffer=0;
  55. $maxDiffer=0;
  56. $paperBox_id=null;
  57. $boxes=Owner::select('id')->with('paperBoxes')->find($owner_id);
  58. foreach ($boxes->paperBoxes as $i=>$paperBox){
  59. if ($paperBox->length==$max&&$paperBox->width==$centre&&$paperBox->height==$min){
  60. $paperBox_id=$paperBox->id;
  61. break;
  62. }
  63. $lengthDiffer=abs($paperBox->length-$max);
  64. $widthDiffer=abs($paperBox->width-$centre);
  65. $heightDiffer=abs($paperBox->height-$min);
  66. $thisMaxDiffer=($lengthDiffer>=($widthDiffer>=$heightDiffer?$widthDiffer:$heightDiffer)?$lengthDiffer:($widthDiffer>=$heightDiffer?$widthDiffer:$heightDiffer));
  67. if($i==0){
  68. $maxDiffer=$thisMaxDiffer;
  69. $sumDiffer=$lengthDiffer+$widthDiffer+$heightDiffer;
  70. $paperBox_id=$paperBox->id;
  71. }
  72. if ($thisMaxDiffer==$maxDiffer){
  73. if($sumDiffer>($lengthDiffer+$widthDiffer+$heightDiffer)){
  74. $sumDiffer=$lengthDiffer+$widthDiffer+$heightDiffer;
  75. $paperBox_id=$paperBox->id;
  76. }
  77. }
  78. if ($thisMaxDiffer<$maxDiffer){
  79. $sumDiffer=$lengthDiffer+$widthDiffer+$heightDiffer;
  80. $maxDiffer=$thisMaxDiffer;
  81. $paperBox_id=$paperBox->id;
  82. }
  83. }
  84. return $paperBox_id;
  85. }
  86. }