Package.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. namespace App;
  3. use App\Http\Controllers\Controller;
  4. use Carbon\Carbon;
  5. use Illuminate\Database\Eloquent\Model;
  6. use App\Traits\ModelTimeFormat;
  7. use Illuminate\Support\Arr;
  8. class Package extends Model
  9. {
  10. use ModelTimeFormat;
  11. protected $fillable=[
  12. 'owner_id','logistic_number','delivery_number','batch_number','batch_rule','recipient','recipient_mobile','logistic_id',
  13. 'measuring_machine_id','weight','length','width','height','bulk','paper_box_id','status','weighed_at','order_code'
  14. ];
  15. protected $appends=[
  16. 'owner_name',
  17. 'logistic_name',
  18. 'measuringMachine_name',
  19. 'measuring_machine_name',
  20. 'measuringMachine_status',
  21. 'paperBox_name',
  22. 'WMSReflectPackage_name'
  23. ];
  24. static protected $oracleOrderHeaderFields = [
  25. 'doc_order_header.userdefine1',
  26. 'doc_order_header.soreference5',
  27. 'doc_order_header.waveno',
  28. 'doc_order_header.orderno',
  29. 'doc_order_header.customerid',
  30. 'doc_order_header.consigneename',
  31. 'doc_order_header.carrierid',
  32. 'doc_order_header.c_tel1',
  33. 'doc_order_header.c_tel2',
  34. 'doc_wave_header.descr',
  35. ];
  36. protected $tempFields=[
  37. 'temOracleInfo','temOwner','temLogistic',
  38. ];
  39. public function owner(){
  40. return $this->belongsTo('App\Owner','owner_id','id');
  41. }
  42. public function logistic(){
  43. return $this->belongsTo('App\Logistic','logistic_id','id');
  44. }
  45. public function measuringMachine(){
  46. return $this->belongsTo('App\MeasuringMachine','measuring_machine_id','id');
  47. }
  48. public function paperBox(){
  49. return $this->belongsTo('App\PaperBox','paper_box_id','id');
  50. }
  51. public function WMSReflectPackage(){
  52. return $this->hasOne('App\WMSReflectPackage','SOReference5','logistic_number');
  53. }
  54. public function setLengthAttribute($value){
  55. if(empty((int)($value)))return;
  56. $this->attributes['length'] = $value;
  57. }
  58. public function setWidthAttribute($value){
  59. if(empty((int)($value)))return;
  60. $this->attributes['width'] = $value;
  61. }
  62. public function setHeightAttribute($value){
  63. if(empty((int)($value)))return;
  64. $this->attributes['height'] = $value;
  65. }
  66. public function setBulkAttribute($value){
  67. if(empty((int)($value)))return;
  68. $this->attributes['bulk'] = $value;
  69. }
  70. public function isActivityBatch(){
  71. return ($this['batch_rule'] && strstr($this['batch_rule'],'组合') && $this['batch_number'] );
  72. }
  73. static public function createPackagesFromBatchCode($batchCode,$weight){
  74. $resultOracleObjs=OracleDOCOrderHeader::select(self::$oracleOrderHeaderFields);
  75. $resultOracleObjs->where('doc_order_header.waveno',$batchCode);
  76. $resultOracleObjs->leftJoin('act_allocation_details','act_allocation_details.orderno','doc_order_header.orderno');
  77. foreach($resultOracleObjs as $resultOracleObj){
  78. $now = Carbon::now();
  79. Package::create([
  80. 'logistic_number'=>$resultOracleObj['SOReference5']??'',
  81. 'delivery_number'=>$resultOracleObj['SOReference5']??'',
  82. 'weight'=>$weight,
  83. 'weighed_at'=> $now,
  84. 'status'=>"已上传",
  85. ]);
  86. }
  87. }
  88. public function unifyThisMeasureUnderSameBatch(){
  89. $this->fetchPaperBox();
  90. $params=[];
  91. !empty($this['weight'])?$params['weight']= $this['weight']:null;
  92. !empty($this['length'])?$params['length']= $this['length']:null;
  93. !empty($this['width'])?$params['width']= $this['width']:null;
  94. !empty($this['height'])?$params['height']= $this['height']:null;
  95. !empty($this['bulk'])?$params['bulk']= $this['bulk']:null;
  96. !empty($this['measuring_machine_id'])?$params['measuring_machine_id']= $this['measuring_machine_id']:null;
  97. !empty($this['weighed_at'])?$params['weighed_at']= $this['weighed_at']:null;
  98. !empty($this['paper_box_id'])?$params['paper_box_id']= $this['paper_box_id']:null;
  99. if(empty($params)||empty($this['batch_number']))return;
  100. Package::where(['batch_number'=>$this['batch_number']])->update([$params]);
  101. }
  102. public function fetchLogisticFromOracle(){
  103. if(empty($this->oracleInfo))return null;
  104. if(Arr::exists($this->tempFields,'temLogistic'))return $this->tempFields['temLogistic'];
  105. Controller::logs(__METHOD__, __FUNCTION__, "tempPackage:{$this->oracleInfo['carrierid']}||SOR:{$this->oracleInfo['SOReference5']}||sor:{$this->oracleInfo['soreference5']}||orderno:{$this['orderno']}" , null);
  106. if(!$this->oracleInfo['carrierid'])return null;
  107. $logistic= Logistic::where('code',$this->oracleInfo['carrierid'])->first();
  108. if(!$logistic){
  109. $logistic=Logistic::create(['code'=>$this->oracleInfo['carrierid'],'name'=>$this->oracleInfo['carrierid']]);
  110. Controller::logs(__METHOD__, __FUNCTION__, "富勒下发找不到快递公司,添加{$this->oracleInfo['carrierid']}" , null);
  111. }
  112. Controller::logs(__METHOD__, __FUNCTION__, "tempPackage2:{$logistic->id}" , null);
  113. if(!$logistic)return null;
  114. $this->tempFields['temLogistic']=$logistic;
  115. $this['logistic_id'] = $logistic['id'];
  116. return $logistic;
  117. }
  118. public function fetchOwnerFromOracle(){
  119. if(empty($this->oracleInfo))return null;
  120. if(Arr::exists($this->tempFields,'temOwner'))return $this->tempFields['temOwner'];
  121. $owner= Owner::where('code',$this->oracleInfo['customerid'])->first();
  122. if(!$owner){
  123. $owner=Owner::create(['code'=>$this->oracleInfo['customerid'],'name'=>$this->oracleInfo['customerid']]);
  124. Controller::logs(__METHOD__, __FUNCTION__, "富勒下发找不到货主,添加{$this->oracleInfo['customerid']}" , null);
  125. }
  126. if(!$owner)return null;
  127. $this->tempFields['temOwner']=$owner;
  128. $this['owner_id'] = $owner['id'];
  129. return $owner;
  130. }
  131. public function fetchAllFromOracle(){
  132. if(empty($this->oracleInfo))return null;
  133. $this->fetchOwnerFromOracle();
  134. $this->fetchLogisticFromOracle();
  135. $this['recipient'] = $this->oracleInfo['consigneename'];
  136. $this['order_code'] = $this->oracleInfo['orderno'];
  137. $this['batch_rule'] = $this->oracleInfo['descr'];
  138. $this['recipient_mobile'] = $this->oracleInfo['c_tel2']??$this->oracleInfo['c_tel1'];
  139. if(!$this['logistic_number']&&$this->oracleInfo['soreference5'])
  140. $this['logistic_number'] = $this->oracleInfo['soreference5'];
  141. $this['batch_number'] = $this->oracleInfo['waveno']??null;
  142. }
  143. public function getOracleInfoAttribute()
  144. {
  145. if(isset($this->tempFields['temOracleInfo']))return $this->tempFields['temOracleInfo'];
  146. if(empty($this['logistic_number'])&&empty($this['order_code']))return '';
  147. if($this['order_code']){
  148. $resultOracleObjs=OracleDOCOrderHeader::select(self::$oracleOrderHeaderFields)->where('orderno',$this['order_code']);
  149. $resultOracleObjs->leftJoin('doc_wave_header','doc_wave_header.waveno','doc_order_header.waveno');
  150. }else{
  151. $resultOracleObjs=OracleActAllocationDetails::select(self::$oracleOrderHeaderFields);
  152. $resultOracleObjs->where('picktotraceid',$this['logistic_number']);
  153. $resultOracleObjs->leftJoin('DOC_Order_Header','act_allocation_details.orderno','doc_order_header.orderno');
  154. $resultOracleObjs->leftJoin('doc_wave_header','doc_wave_header.waveno','doc_order_header.waveno');
  155. }
  156. $this->tempFields['temOracleInfo']=$resultOracleObjs->first();
  157. if(empty($this->tempFields['temOracleInfo'])) {
  158. $resultOracleObjs=OracleDOCOrderHeader::select(self::$oracleOrderHeaderFields)->where('soreference5',$this['logistic_number']);
  159. $resultOracleObjs->leftJoin('doc_wave_header','doc_wave_header.waveno','doc_order_header.waveno');
  160. }
  161. $this->tempFields['temOracleInfo']=$resultOracleObjs->first();
  162. return $this->tempFields['temOracleInfo'];
  163. }
  164. public function getOwnerNameAttribute()
  165. {
  166. return $this['owner']? $this['owner']['name']:null;
  167. }
  168. public function getLogisticNameAttribute(){
  169. return $this['logistic']? $this['logistic']['name']:null;
  170. }
  171. public function getMeasuringMachineNameAttribute(){
  172. return $this['measuringMachine']? $this['measuringMachine']['name']:null;
  173. }
  174. public function getMeasuringMachineStatusAttribute(){
  175. return $this['measuringMachine']? $this['measuringMachine']['status']:null;
  176. }
  177. public function getPaperBoxNameAttribute(){
  178. return $this['paperBox']? $this['paperBox']['model']:null;
  179. }
  180. public function getWMSReflectPackageNameAttribute(){
  181. return $this['WMSReflectPackage']? $this['WMSReflectPackage']['TASKID']:null;
  182. }
  183. //寻找相近纸箱ID
  184. public function fetchPaperBox($max=null, $centre=null, $min=null, $owner_id=null){
  185. if($this['paper_box_id'])return $this['paper_box_id'];
  186. $sumDiffer=0;
  187. $maxDiffer=0;
  188. $paperBox_id=null;
  189. if(!$max)$max=$this['length'];
  190. if(!$centre)$centre=$this['width'];
  191. if(!$min)$min=$this['height'];
  192. if(!$owner_id) $owner_id = $this['owner_id'];
  193. if(!$owner_id) {
  194. $owner = $this->fetchOwnerFromOracle();
  195. $owner_id = $owner['id'];
  196. if(!$owner_id)return null;
  197. }
  198. $boxes=Owner::select('id')->with('paperBoxes')->find($owner_id);
  199. $targetPaperBox=null;
  200. foreach ($boxes->paperBoxes as $i=>$paperBox){
  201. if ($paperBox->length==$max&&$paperBox->width==$centre&&$paperBox->height==$min){
  202. $targetPaperBox=$paperBox;
  203. break;
  204. }
  205. $lengthDiffer=abs($paperBox->length-$max);
  206. $widthDiffer=abs($paperBox->width-$centre);
  207. $heightDiffer=abs($paperBox->height-$min);
  208. $thisMaxDiffer=($lengthDiffer>=($widthDiffer>=$heightDiffer?$widthDiffer:$heightDiffer)?$lengthDiffer:($widthDiffer>=$heightDiffer?$widthDiffer:$heightDiffer));
  209. if($i==0){
  210. $maxDiffer=$thisMaxDiffer;
  211. $sumDiffer=$lengthDiffer+$widthDiffer+$heightDiffer;
  212. $targetPaperBox=$paperBox;
  213. }
  214. if ($thisMaxDiffer==$maxDiffer){
  215. if($sumDiffer>($lengthDiffer+$widthDiffer+$heightDiffer)){
  216. $sumDiffer=$lengthDiffer+$widthDiffer+$heightDiffer;
  217. $targetPaperBox=$paperBox;
  218. }
  219. }
  220. if ($thisMaxDiffer<$maxDiffer){
  221. $sumDiffer=$lengthDiffer+$widthDiffer+$heightDiffer;
  222. $maxDiffer=$thisMaxDiffer;
  223. $targetPaperBox=$paperBox;
  224. }
  225. }
  226. if($targetPaperBox)$this['paper_box_id']=$targetPaperBox['id'];
  227. return $targetPaperBox['id'];
  228. }
  229. }