Package.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Model;
  4. use App\Traits\ModelTimeFormat;
  5. class Package extends Model
  6. {
  7. use ModelTimeFormat;
  8. protected $fillable=[
  9. 'owner_id','logistic_number','delivery_number','batch_number','batch_rule','recipient','recipient_mobile','logistic_id',
  10. 'measuring_machine_id','weight','length','width','height','bulk','paper_box_id','status','weighed_at','order_code'
  11. ];
  12. protected $appends=[
  13. 'owner_name',
  14. 'logistic_name',
  15. 'measuringMachine_name',
  16. 'measuring_machine_name',
  17. 'measuringMachine_status',
  18. 'paperBox_name',
  19. 'WMSReflectPackage_name'
  20. ];
  21. public function owner(){
  22. return $this->belongsTo('App\Owner','owner_id','id');
  23. }
  24. public function logistic(){
  25. return $this->belongsTo('App\Logistic','logistic_id','id');
  26. }
  27. public function measuringMachine(){
  28. return $this->belongsTo('App\MeasuringMachine','measuring_machine_id','id');
  29. }
  30. public function paperBox(){
  31. return $this->belongsTo('App\PaperBox','paper_box_id','id');
  32. }
  33. public function WMSReflectPackage(){
  34. return $this->hasOne('App\WMSReflectPackage','SOReference5','logistic_number');
  35. }
  36. public function save(array $options=[]){
  37. unset($this->_temOracleInfo);
  38. unset($this->temOwner);
  39. unset($this->temLogistic);
  40. parent::save($options);
  41. }
  42. public function isActivityBatch(){
  43. return ($this['batch_rule'] && strstr($this['batch_rule'],'组合') && $this['batch_number'] );
  44. }
  45. public function unifyThisMeasureUnderBatch(){
  46. $this->fetchPaperBox();
  47. Package::where(['batch_number'=>$this['batch_number']])->update(['weight'=>$this['weight']??'',
  48. 'length'=>$this['length']??'',
  49. 'width'=>$this['width']??'',
  50. 'height'=>$this['height']??'',
  51. 'paper_box_id'=>$this['paper_box_id']??'']);
  52. }
  53. public function fetchLogisticFromOracle(){
  54. if(!$this['oracleInfo'])return null;
  55. if($this['temLogistic'])return $this['temLogistic'];
  56. $logistic= Logistic::where('code',$this['oracleInfo']['SOReference5'])->first();
  57. if(!$logistic){
  58. Logistic::create(['code'=>$this['oracleInfo']['SOReference5'],'name'=>$this['oracleInfo']['SOReference5']]);
  59. $this->log(__METHOD__, __FUNCTION__, "富勒下发找不到快递公司,添加{$this['oracleInfo']['customerid']}" , null);
  60. }
  61. if(!$logistic)return null;
  62. $this['temLogistic']=$logistic;
  63. $this['logistic_id'] = $logistic['id'];
  64. return $logistic;
  65. }
  66. public function fetchOwnerFromOracle(){
  67. if(!$this['oracleInfo'])return null;
  68. if($this['temOwner'])return $this['temOwner'];
  69. $owner= Owner::where('code',$this['oracleInfo']['customerid'])->first();
  70. if(!$owner){
  71. Owner::create(['code'=>$this['oracleInfo']['customerid'],'name'=>$this['oracleInfo']['customerid']]);
  72. $this->log(__METHOD__, __FUNCTION__, "富勒下发找不到货主,添加{$this['oracleInfo']['customerid']}" , null);
  73. }
  74. if(!$owner)return null;
  75. $this['temOwner']=$owner;
  76. $this['owner_id'] = $owner['id'];
  77. return $owner;
  78. }
  79. public function fetchAllFromOracle(){
  80. if(!$this['oracleInfo'])return null;
  81. $this->fetchOwnerFromOracle();
  82. $this->fetchLogisticFromOracle();
  83. $this['recipient'] = $this['oracleInfo']['Consigneename'];
  84. if($this['oracleInfo']['SOReference5'])
  85. $this['logistic_number'] = $this['oracleInfo']['SOReference5'];
  86. $this['batch_rule'] = isset($this['oracleInfo']['userdefine2'])?$this['oracleInfo']['userdefine2']:'';
  87. }
  88. public function getOracleInfoAttribute()
  89. {
  90. if(!$this['logistic_number'])return '';
  91. if(isset($this->_temOracleInfo)&&$this->_temOracleInfo)return $this->_temOracleInfo;
  92. $allocationDetail=OracleActAllocationDetails::select([
  93. 'doc_order_header.userdefine1',
  94. 'doc_order_header.userdefine2',
  95. 'doc_order_header.SOReference5',
  96. 'doc_order_header.orderno',
  97. 'doc_order_header.customerid',
  98. 'doc_order_header.Consigneename'
  99. ])
  100. ->where('picktotraceid',$this['logistic_number'])
  101. ->leftJoin('DOC_Order_Header','act_allocation_details.orderno','doc_order_header.orderno');
  102. $this->_temOracleInfo=$allocationDetail->first();
  103. return $this->_temOracleInfo;
  104. }
  105. public function getOwnerNameAttribute()
  106. {
  107. return $this['owner']? $this['owner']['name']:null;
  108. }
  109. public function getLogisticNameAttribute(){
  110. return $this['logistic']? $this['logistic']['name']:null;
  111. }
  112. public function getMeasuringMachineNameAttribute(){
  113. return $this['measuringMachine']? $this['measuringMachine']['name']:null;
  114. }
  115. public function getMeasuringMachineStatusAttribute(){
  116. return $this['measuringMachine']? $this['measuringMachine']['status']:null;
  117. }
  118. public function getPaperBoxNameAttribute(){
  119. return $this['paperBox']? $this['paperBox']['model']:null;
  120. }
  121. public function getWMSReflectPackageNameAttribute(){
  122. return $this['WMSReflectPackage']? $this['WMSReflectPackage']['TASKID']:null;
  123. }
  124. //寻找相近纸箱ID
  125. public function fetchPaperBox($max, $centre, $min, $owner_id=null){
  126. if($this['paper_box_id'])return $this['paper_box_id'];
  127. $sumDiffer=0;
  128. $maxDiffer=0;
  129. $paperBox_id=null;
  130. if(!$owner_id) $owner_id = $this['owner_id'];
  131. if(!$owner_id) {
  132. $owner = $this->fetchOwnerFromOracle();
  133. $owner_id = $owner['id'];
  134. if(!$owner_id)return null;
  135. }
  136. $boxes=Owner::select('id')->with('paperBoxes')->find($owner_id);
  137. $targetPaperBox=null;
  138. foreach ($boxes->paperBoxes as $i=>$paperBox){
  139. if ($paperBox->length==$max&&$paperBox->width==$centre&&$paperBox->height==$min){
  140. $targetPaperBox=$paperBox;
  141. break;
  142. }
  143. $lengthDiffer=abs($paperBox->length-$max);
  144. $widthDiffer=abs($paperBox->width-$centre);
  145. $heightDiffer=abs($paperBox->height-$min);
  146. $thisMaxDiffer=($lengthDiffer>=($widthDiffer>=$heightDiffer?$widthDiffer:$heightDiffer)?$lengthDiffer:($widthDiffer>=$heightDiffer?$widthDiffer:$heightDiffer));
  147. if($i==0){
  148. $maxDiffer=$thisMaxDiffer;
  149. $sumDiffer=$lengthDiffer+$widthDiffer+$heightDiffer;
  150. $targetPaperBox=$paperBox;
  151. }
  152. if ($thisMaxDiffer==$maxDiffer){
  153. if($sumDiffer>($lengthDiffer+$widthDiffer+$heightDiffer)){
  154. $sumDiffer=$lengthDiffer+$widthDiffer+$heightDiffer;
  155. $targetPaperBox=$paperBox;
  156. }
  157. }
  158. if ($thisMaxDiffer<$maxDiffer){
  159. $sumDiffer=$lengthDiffer+$widthDiffer+$heightDiffer;
  160. $maxDiffer=$thisMaxDiffer;
  161. $targetPaperBox=$paperBox;
  162. }
  163. }
  164. if($targetPaperBox)$this['paper_box_id']=$targetPaperBox['id'];
  165. return $targetPaperBox['id'];
  166. }
  167. }