Package.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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::query()->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. $packages = [];
  78. foreach($resultOracleObjs as $resultOracleObj){
  79. $now = Carbon::now();
  80. array_push($packages,[
  81. 'batch_number'=>$batchCode??'',
  82. 'logistic_number'=>$resultOracleObj['SOReference5']??'',
  83. 'delivery_number'=>$resultOracleObj['SOReference5']??'',
  84. 'weight'=>$weight,
  85. 'weighed_at'=> $now,
  86. 'status'=>"已上传",
  87. "created_at"=>$now,
  88. ]);
  89. }
  90. Package::query()->insert($packages);
  91. }
  92. public function unifyThisMeasureUnderSameBatch(){
  93. $this->fetchPaperBox();
  94. $params=[];
  95. !empty($this['weight'])?$params['weight']= $this['weight']:null;
  96. !empty($this['length'])?$params['length']= $this['length']:null;
  97. !empty($this['width'])?$params['width']= $this['width']:null;
  98. !empty($this['height'])?$params['height']= $this['height']:null;
  99. !empty($this['bulk'])?$params['bulk']= $this['bulk']:null;
  100. !empty($this['measuring_machine_id'])?$params['measuring_machine_id']= $this['measuring_machine_id']:null;
  101. !empty($this['weighed_at'])?$params['weighed_at']= $this['weighed_at']:null;
  102. !empty($this['paper_box_id'])?$params['paper_box_id']= $this['paper_box_id']:null;
  103. if(empty($params)||empty($this['batch_number']))return;
  104. Package::query()->where(['batch_number'=>$this['batch_number']])->update([$params]);
  105. }
  106. public function fetchLogisticFromOracle(){
  107. if(empty($this->oracleInfo))return null;
  108. if(Arr::exists($this->tempFields,'temLogistic'))return $this->tempFields['temLogistic'];
  109. Controller::logs(__METHOD__, __FUNCTION__, "tempPackage:{$this->oracleInfo['carrierid']}||SOR:{$this->oracleInfo['SOReference5']}||sor:{$this->oracleInfo['soreference5']}||orderno:{$this['orderno']}" , null);
  110. if(!$this->oracleInfo['carrierid'])return null;
  111. $logistic= Logistic::query()->where('code',$this->oracleInfo['carrierid'])->first();
  112. if(!$logistic){
  113. $logistic=Logistic::query()->create(['code'=>$this->oracleInfo['carrierid'],'name'=>$this->oracleInfo['carrierid']]);
  114. Controller::logs(__METHOD__, __FUNCTION__, "富勒下发找不到快递公司,添加{$this->oracleInfo['carrierid']}" , null);
  115. }
  116. Controller::logs(__METHOD__, __FUNCTION__, "tempPackage2:{$logistic->id}" , null);
  117. if(!$logistic)return null;
  118. $this->tempFields['temLogistic']=$logistic;
  119. $this['logistic_id'] = $logistic['id'];
  120. return $logistic;
  121. }
  122. public function fetchOwnerFromOracle(){
  123. if(empty($this->oracleInfo))return null;
  124. if(Arr::exists($this->tempFields,'temOwner'))return $this->tempFields['temOwner'];
  125. $owner= Owner::query()->where('code',$this->oracleInfo['customerid'])->first();
  126. if(!$owner){
  127. $owner=Owner::query()->create(['code'=>$this->oracleInfo['customerid'],'name'=>$this->oracleInfo['customerid']]);
  128. Controller::logs(__METHOD__, __FUNCTION__, "富勒下发找不到货主,添加{$this->oracleInfo['customerid']}" , null);
  129. }
  130. if(!$owner)return null;
  131. $this->tempFields['temOwner']=$owner;
  132. $this['owner_id'] = $owner['id'];
  133. return $owner;
  134. }
  135. public function fetchAllFromOracle(){
  136. if(empty($this->oracleInfo))return null;
  137. $this->fetchOwnerFromOracle();
  138. $this->fetchLogisticFromOracle();
  139. $this['recipient'] = $this->oracleInfo['consigneename'];
  140. $this['order_code'] = $this->oracleInfo['orderno'];
  141. $this['batch_rule'] = $this->oracleInfo['descr'];
  142. $this['recipient_mobile'] = $this->oracleInfo['c_tel2']??$this->oracleInfo['c_tel1'];
  143. if(!$this['logistic_number']&&$this->oracleInfo['soreference5'])
  144. $this['logistic_number'] = $this->oracleInfo['soreference5'];
  145. $this['batch_number'] = $this->oracleInfo['waveno']??null;
  146. }
  147. public function getOracleInfoAttribute()
  148. {
  149. if(isset($this->tempFields['temOracleInfo']))return $this->tempFields['temOracleInfo'];
  150. if(empty($this['logistic_number'])&&empty($this['order_code']))return '';
  151. if($this['order_code']){
  152. $resultOracleObjs=OracleDOCOrderHeader::query()->select(self::$oracleOrderHeaderFields)->where('orderno',$this['order_code']);
  153. $resultOracleObjs->leftJoin('doc_wave_header','doc_wave_header.waveno','doc_order_header.waveno');
  154. }else{
  155. $resultOracleObjs=OracleActAllocationDetails::query()->select(self::$oracleOrderHeaderFields);
  156. $resultOracleObjs->where('picktotraceid',$this['logistic_number']);
  157. $resultOracleObjs->leftJoin('DOC_Order_Header','act_allocation_details.orderno','doc_order_header.orderno');
  158. $resultOracleObjs->leftJoin('doc_wave_header','doc_wave_header.waveno','doc_order_header.waveno');
  159. }
  160. $this->tempFields['temOracleInfo']=$resultOracleObjs->first();
  161. if(empty($this->tempFields['temOracleInfo'])) {
  162. $resultOracleObjs=OracleDOCOrderHeader::query()->select(self::$oracleOrderHeaderFields)->where('soreference5',$this['logistic_number']);
  163. $resultOracleObjs->leftJoin('doc_wave_header','doc_wave_header.waveno','doc_order_header.waveno');
  164. }
  165. $this->tempFields['temOracleInfo']=$resultOracleObjs->first();
  166. return $this->tempFields['temOracleInfo'];
  167. }
  168. public function getOwnerNameAttribute()
  169. {
  170. return $this['owner']? $this['owner']['name']:null;
  171. }
  172. public function getLogisticNameAttribute(){
  173. return $this['logistic']? $this['logistic']['name']:null;
  174. }
  175. public function getMeasuringMachineNameAttribute(){
  176. return $this['measuringMachine']? $this['measuringMachine']['name']:null;
  177. }
  178. public function getMeasuringMachineStatusAttribute(){
  179. return $this['measuringMachine']? $this['measuringMachine']['status']:null;
  180. }
  181. public function getPaperBoxNameAttribute(){
  182. return $this['paperBox']? $this['paperBox']['model']:null;
  183. }
  184. public function getWMSReflectPackageNameAttribute(){
  185. return $this['WMSReflectPackage']? $this['WMSReflectPackage']['TASKID']:null;
  186. }
  187. //寻找相近纸箱ID
  188. public function fetchPaperBox($max=null, $centre=null, $min=null, $owner_id=null){
  189. if($this['paper_box_id'])return $this['paper_box_id'];
  190. $sumDiffer=0;
  191. $maxDiffer=0;
  192. $paperBox_id=null;
  193. if(!$max)$max=$this['length'];
  194. if(!$centre)$centre=$this['width'];
  195. if(!$min)$min=$this['height'];
  196. if(!$owner_id) $owner_id = $this['owner_id'];
  197. if(!$owner_id) {
  198. $owner = $this->fetchOwnerFromOracle();
  199. $owner_id = $owner['id'];
  200. if(!$owner_id)return null;
  201. }
  202. $boxes=Owner::select('id')->with('paperBoxes')->find($owner_id);
  203. $targetPaperBox=null;
  204. foreach ($boxes->paperBoxes as $i=>$paperBox){
  205. if ($paperBox->length==$max&&$paperBox->width==$centre&&$paperBox->height==$min){
  206. $targetPaperBox=$paperBox;
  207. break;
  208. }
  209. $lengthDiffer=abs($paperBox->length-$max);
  210. $widthDiffer=abs($paperBox->width-$centre);
  211. $heightDiffer=abs($paperBox->height-$min);
  212. $thisMaxDiffer=($lengthDiffer>=($widthDiffer>=$heightDiffer?$widthDiffer:$heightDiffer)?$lengthDiffer:($widthDiffer>=$heightDiffer?$widthDiffer:$heightDiffer));
  213. if($i==0){
  214. $maxDiffer=$thisMaxDiffer;
  215. $sumDiffer=$lengthDiffer+$widthDiffer+$heightDiffer;
  216. $targetPaperBox=$paperBox;
  217. }
  218. if ($thisMaxDiffer==$maxDiffer){
  219. if($sumDiffer>($lengthDiffer+$widthDiffer+$heightDiffer)){
  220. $sumDiffer=$lengthDiffer+$widthDiffer+$heightDiffer;
  221. $targetPaperBox=$paperBox;
  222. }
  223. }
  224. if ($thisMaxDiffer<$maxDiffer){
  225. $sumDiffer=$lengthDiffer+$widthDiffer+$heightDiffer;
  226. $maxDiffer=$thisMaxDiffer;
  227. $targetPaperBox=$paperBox;
  228. }
  229. }
  230. if($targetPaperBox)$this['paper_box_id']=$targetPaperBox['id'];
  231. return $targetPaperBox['id'];
  232. }
  233. }