Package.php 8.0 KB

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