Package.php 11 KB

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