OrderPackage.php 13 KB

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