Package.php 11 KB

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