OrderPackage.php 13 KB

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