OrderPackage.php 11 KB

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