OrderPackage.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <?php
  2. namespace App;
  3. use App\Http\Controllers\Controller;
  4. use App\Services\OrderService;
  5. use App\Traits\ModelTimeFormat;
  6. use Carbon\Carbon;
  7. use Illuminate\Database\Eloquent\Model;
  8. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  9. use Illuminate\Database\Eloquent\Relations\HasMany;
  10. use Illuminate\Database\Eloquent\Relations\HasOne;
  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 ModelLogChanging;
  17. use ModelTimeFormat;
  18. protected $fillable = [
  19. 'order_id',
  20. 'logistic_number',
  21. 'batch_number',
  22. 'batch_rule',
  23. 'bulk',
  24. 'weight',
  25. 'length',
  26. 'width',
  27. 'height',
  28. 'paper_box_id',
  29. 'measuring_machine_id',
  30. 'weighed_at',
  31. 'status',
  32. 'sent_at',
  33. 'received_at',
  34. 'transfer_status',
  35. 'owner_id',
  36. 'uploaded_to_wms',
  37. 'sync_routes_flag',//同步物流信息标记
  38. 'is_manual_update',//是否手动更新状态
  39. 'exception_status',//异常状态
  40. 'is_delay_deliver',//是否延时发货
  41. 'collecting_status',//手动揽收状态
  42. 'route_length',//路由长度
  43. ];
  44. protected $casts = [
  45. 'transfer_status' => 'array',
  46. 'sync_routes_flag' => 'boolean',
  47. 'is_manual_update' => 'boolean',
  48. 'is_delay_deliver' => 'boolean',
  49. ];
  50. protected $dates = [
  51. 'sent_at',
  52. 'received_at',
  53. 'weighed_at',
  54. ];
  55. static public $enums = [
  56. 'status' => [
  57. '' => 0,
  58. '生成订单' => 1,
  59. '已复核' => 2,
  60. '已称重' => 3,
  61. '已揽件' => 4,
  62. '在途' => 5,
  63. '派送中' => 6,
  64. '已签收' => 7,
  65. '其他' => 8,
  66. '返回中' => 9,
  67. ],
  68. 'exception_status' => [
  69. '' => 0,
  70. '单号异常' => 1,
  71. '无法获取路由' => 2,
  72. '延迟发货' => 3,
  73. '疑似库内丢件' => 4,
  74. '在途异常' => 5,
  75. '揽件异常' => 6,
  76. '派送异常' => 7,
  77. ],
  78. ];
  79. function __construct(array $attributes = [])
  80. {
  81. foreach (self::$enums as &$enum) {
  82. $enum = $enum + array_flip($enum);
  83. }
  84. parent::__construct($attributes);
  85. }
  86. public function getStatusAttribute($value)
  87. {
  88. if (!$value || !isset(self::$enums['status'][$value])) return '';
  89. return self::$enums['status'][$value];
  90. }
  91. public function setStatusAttribute($value)
  92. {
  93. if (!$value) return 0;
  94. if (!(self::$enums['status'][$value] ?? false)) return 0;
  95. $this->attributes['status'] = self::$enums['status'][$value];
  96. }
  97. public static function switchStatus($value): int
  98. {
  99. if (!(self::$enums['status'][$value] ?? false)) return 0;
  100. return self::$enums['status'][$value];
  101. }
  102. public function getExceptionStatusAttribute($value)
  103. {
  104. if (!$value || !isset(self::$enums['exception_status'][$value])) return '';
  105. return self::$enums['exception_status'][$value];
  106. }
  107. public function setExceptionStatusAttribute($value)
  108. {
  109. if (!$value) return 0;
  110. if (!(self::$enums['exception_status'][$value] ?? false)) return 0;
  111. $this->attributes['exception_status'] = self::$enums['exception_status'][$value];
  112. }
  113. public static function switchExceptionStatus($value): int
  114. {
  115. if (!(self::$enums['exception_status'][$value] ?? false)) return 0;
  116. return self::$enums['exception_status'][$value];
  117. }
  118. public function order()
  119. {
  120. return $this->belongsTo('App\Order', 'order_id', 'id');
  121. }
  122. public function commodities(): HasMany
  123. {
  124. return $this->hasMany('App\OrderPackageCommodities', 'order_package_id', 'id');
  125. }
  126. public function paperBox(): HasOne
  127. {
  128. return $this->hasOne('App\PaperBox', 'id', 'paper_box_id');
  129. }
  130. public function measuringMachine(): HasOne
  131. {
  132. return $this->hasOne('App\MeasuringMachine', 'id', 'measuring_machine_id');
  133. }
  134. public function WMSReflectPackage(): HasOne
  135. {
  136. return $this->hasOne('App\WMSReflectPackage', 'SOReference5', 'logistic_number');
  137. }
  138. static protected $oracleOrderHeaderFields = [
  139. 'doc_order_header.userdefine1',
  140. 'doc_order_header.soreference5',
  141. 'doc_order_header.waveno',
  142. 'doc_order_header.orderno',
  143. 'doc_order_header.customerid',
  144. 'doc_order_header.consigneename',
  145. 'doc_order_header.carrierid',
  146. 'doc_order_header.c_tel1',
  147. 'doc_order_header.c_tel2',
  148. 'doc_wave_header.descr',
  149. ];
  150. protected $tempFields = [
  151. 'temOracleInfo', 'temOwner', 'temLogistic',
  152. ];
  153. public function setLengthAttribute($value)
  154. {
  155. if (empty((int)($value))) return;
  156. $this->attributes['length'] = $value;
  157. }
  158. public function setWidthAttribute($value)
  159. {
  160. if (empty((int)($value))) return;
  161. $this->attributes['width'] = $value;
  162. }
  163. public function setHeightAttribute($value)
  164. {
  165. if (empty((int)($value))) return;
  166. $this->attributes['height'] = $value;
  167. }
  168. public function setBulkAttribute($value)
  169. {
  170. if (empty((int)($value))) return;
  171. $this->attributes['bulk'] = $value;
  172. }
  173. public function delete()
  174. {
  175. $this->commodities()->delete();
  176. return parent::delete();
  177. }
  178. public function deleteSafe()
  179. {
  180. return parent::delete();
  181. }
  182. public function isActivityBatch()
  183. {
  184. return ($this['batch_rule'] && strstr($this['batch_rule'], '组合') && $this['batch_number']);
  185. }
  186. public function fetchAllFromOracle()
  187. {
  188. if (empty($this->oracleInfo)) return null;
  189. /* $this->fetchOwnerFromOracle();
  190. $this->fetchLogisticFromOracle();*/
  191. /* $this['recipient'] = $this->oracleInfo['consigneename'];
  192. $this['order_code'] = $this->oracleInfo['orderno'];*/
  193. $this['batch_rule'] = $this->oracleInfo['descr'];
  194. /* $this['recipient_mobile'] = $this->oracleInfo['c_tel2']??$this->oracleInfo['c_tel1'];
  195. if(!$this['logistic_number']&&$this->oracleInfo['soreference5'])
  196. $this['logistic_number'] = $this->oracleInfo['soreference5'];*/
  197. $this['batch_number'] = $this->oracleInfo['waveno'] ?? null;
  198. }
  199. static public function createPackagesFromBatchCode($batchCode, $weight)
  200. {
  201. $queryBuilder = OracleDOCOrderHeader::query()->select(self::$oracleOrderHeaderFields);
  202. $queryBuilder->where('doc_order_header.waveno', $batchCode);
  203. $queryBuilder->leftJoin('act_allocation_details', 'act_allocation_details.orderno', 'doc_order_header.orderno');
  204. $queryBuilder->leftJoin('doc_wave_header', 'doc_wave_header.waveno', 'doc_order_header.waveno');
  205. $resultOracleObjs = $queryBuilder->get();
  206. $resultOracleObjs_grouped = $resultOracleObjs->groupBy('soreference5');
  207. $packages = [];
  208. $now = Carbon::now();
  209. foreach ($resultOracleObjs_grouped as $resultOracleObj_grouped) {
  210. $resultOracleObj = $resultOracleObj_grouped[0];
  211. /** @var OrderService $orderService */
  212. $orderService = app('OrderService');
  213. $order = $orderService->logisticNumberFirstOrCreateOrder($resultOracleObj['soreference5']);
  214. if (!$order) {
  215. app('LogService')->log(__METHOD__, "此包裹在WMS未找到order", json_encode($resultOracleObj), Auth::user()['id']);
  216. continue;
  217. }
  218. array_push($packages, [
  219. 'batch_number' => $batchCode ?? '',
  220. 'order_id' => $order->id,
  221. 'logistic_number' => $resultOracleObj['soreference5'] ?? '',
  222. 'weight' => $weight,
  223. 'weighed_at' => $now,
  224. 'uploaded_to_wms' => "是",
  225. "created_at" => $now,
  226. ]);
  227. }
  228. $packagesLogisticNumbers = array_map(function ($orderPackage) {
  229. return $orderPackage['logistic_number'];
  230. }, $packages);
  231. $existingOrderPackages = OrderPackage::whereIn('logistic_number', $packagesLogisticNumbers)->get();
  232. $existingLogisticNumbers = $existingOrderPackages->map(function ($orderPackage) {
  233. return $orderPackage['logistic_number'];
  234. })->toArray();
  235. OrderPackage::whereIn('logistic_number', $existingLogisticNumbers)->update([
  236. 'batch_number' => $batchCode ?? '',
  237. 'weight' => $weight,
  238. 'weighed_at' => $now,
  239. 'uploaded_to_wms' => "是",]);
  240. $newPackages = $packages;
  241. if ($existingOrderPackages->isNotEmpty())
  242. $newPackages = array_filter($packages, function ($package) use ($existingLogisticNumbers) {
  243. return array_search($package['logistic_number'], $existingLogisticNumbers) === false;
  244. });
  245. DB::transaction(function () use ($newPackages) {
  246. OrderPackage::query()->insert($newPackages);
  247. });
  248. app('LogService')->log(__METHOD__, "批量录入包裹成功", json_encode($packages), Auth::user()['id']);
  249. }
  250. public function unifyThisMeasureUnderSameBatch()
  251. {
  252. $this->fetchPaperBox();
  253. $params = [];
  254. !empty($this['weight']) ? $params['weight'] = $this['weight'] : null;
  255. !empty($this['length']) ? $params['length'] = $this['length'] : null;
  256. !empty($this['width']) ? $params['width'] = $this['width'] : null;
  257. !empty($this['height']) ? $params['height'] = $this['height'] : null;
  258. !empty($this['bulk']) ? $params['bulk'] = $this['bulk'] : null;
  259. !empty($this['measuring_machine_id']) ? $params['measuring_machine_id'] = $this['measuring_machine_id'] : null;
  260. !empty($this['weighed_at']) ? $params['weighed_at'] = $this['weighed_at'] : null;
  261. !empty($this['paper_box_id']) ? $params['paper_box_id'] = $this['paper_box_id'] : null;
  262. if (empty($params) || empty($this['batch_number'])) return;
  263. OrderPackage::query()->where(['batch_number' => $this['batch_number']])->update($params);
  264. }
  265. public function fetchLogisticFromOracle()
  266. {
  267. if (empty($this->oracleInfo)) return null;
  268. if (Arr::exists($this->tempFields, 'temLogistic')) return $this->tempFields['temLogistic'];
  269. Controller::logs(__METHOD__, __FUNCTION__, "tempPackage:{$this->oracleInfo['carrierid']}||SOR:{$this->oracleInfo['SOReference5']}||sor:{$this->oracleInfo['soreference5']}||orderno:{$this['orderno']}", null);
  270. if (!$this->oracleInfo['carrierid']) return null;
  271. $logistic = Logistic::query()->where('code', $this->oracleInfo['carrierid'])->first();
  272. if (!$logistic) {
  273. $logistic = Logistic::query()->create(['code' => $this->oracleInfo['carrierid'], 'name' => $this->oracleInfo['carrierid']]);
  274. Controller::logs(__METHOD__, __FUNCTION__, "富勒下发找不到快递公司,添加{$this->oracleInfo['carrierid']}", null);
  275. }
  276. Controller::logs(__METHOD__, __FUNCTION__, "tempPackage2:{$logistic->id}", null);
  277. if (!$logistic) return null;
  278. $this->tempFields['temLogistic'] = $logistic;
  279. $this['logistic_id'] = $logistic['id'];
  280. return $logistic;
  281. }
  282. public function fetchOwnerFromOracle()
  283. {
  284. if (empty($this->oracleInfo)) return null;
  285. if (Arr::exists($this->tempFields, 'temOwner')) return $this->tempFields['temOwner'];
  286. $owner = Owner::query()->where('code', $this->oracleInfo['customerid'])->first();
  287. if (!$owner) {
  288. $owner = Owner::query()->create(['code' => $this->oracleInfo['customerid'], 'name' => $this->oracleInfo['customerid']]);
  289. Controller::logs(__METHOD__, __FUNCTION__, "富勒下发找不到货主,添加{$this->oracleInfo['customerid']}", null);
  290. }
  291. if (!$owner) return null;
  292. $this->tempFields['temOwner'] = $owner;
  293. $this['owner_id'] = $owner['id'];
  294. return $owner;
  295. }
  296. //寻找相近纸箱ID
  297. public function fetchPaperBox($max = null, $centre = null, $min = null, $owner_id = null)
  298. {
  299. if ($this['paper_box_id']) return $this['paper_box_id'];
  300. $sumDiffer = 0;
  301. $maxDiffer = 0;
  302. $paperBox_id = null;
  303. if (!$max) $max = $this['length'];
  304. if (!$centre) $centre = $this['width'];
  305. if (!$min) $min = $this['height'];
  306. if (!$owner_id) $owner_id = $this['order'] ? $this['order']['owner_id'] : null;
  307. if (!$owner_id) {
  308. $owner = $this->fetchOwnerFromOracle();
  309. $owner_id = $owner['id'];
  310. if (!$owner_id) return null;
  311. }
  312. $boxes = Owner::select('id')->with('paperBoxes')->find($owner_id);
  313. $targetPaperBox = null;
  314. foreach ($boxes->paperBoxes as $i => $paperBox) {
  315. if ($paperBox->length == $max && $paperBox->width == $centre && $paperBox->height == $min) {
  316. $targetPaperBox = $paperBox;
  317. break;
  318. }
  319. $lengthDiffer = abs($paperBox->length - $max);
  320. $widthDiffer = abs($paperBox->width - $centre);
  321. $heightDiffer = abs($paperBox->height - $min);
  322. $thisMaxDiffer = ($lengthDiffer >= ($widthDiffer >= $heightDiffer ? $widthDiffer : $heightDiffer) ? $lengthDiffer : ($widthDiffer >= $heightDiffer ? $widthDiffer : $heightDiffer));
  323. if ($i == 0) {
  324. $maxDiffer = $thisMaxDiffer;
  325. $sumDiffer = $lengthDiffer + $widthDiffer + $heightDiffer;
  326. $targetPaperBox = $paperBox;
  327. }
  328. if ($thisMaxDiffer == $maxDiffer) {
  329. if ($sumDiffer > ($lengthDiffer + $widthDiffer + $heightDiffer)) {
  330. $sumDiffer = $lengthDiffer + $widthDiffer + $heightDiffer;
  331. $targetPaperBox = $paperBox;
  332. }
  333. }
  334. if ($thisMaxDiffer < $maxDiffer) {
  335. $sumDiffer = $lengthDiffer + $widthDiffer + $heightDiffer;
  336. $maxDiffer = $thisMaxDiffer;
  337. $targetPaperBox = $paperBox;
  338. }
  339. }
  340. if ($targetPaperBox) $this['paper_box_id'] = $targetPaperBox['id'];
  341. return $targetPaperBox['id'];
  342. }
  343. public function getOracleInfoAttribute()
  344. {
  345. if (isset($this->tempFields['temOracleInfo'])) return $this->tempFields['temOracleInfo'];
  346. if (empty($this['logistic_number']) && empty($this['order_code'])) return '';
  347. if ($this['order_code']) {
  348. $resultOracleObjs = OracleDOCOrderHeader::query()->select(self::$oracleOrderHeaderFields)->where('orderno', $this['order_code']);
  349. $resultOracleObjs->leftJoin('doc_wave_header', 'doc_wave_header.waveno', 'doc_order_header.waveno');
  350. } else {
  351. $resultOracleObjs = OracleActAllocationDetails::query()->select(self::$oracleOrderHeaderFields);
  352. $resultOracleObjs->where('picktotraceid', $this['logistic_number']);
  353. $resultOracleObjs->leftJoin('DOC_Order_Header', 'act_allocation_details.orderno', 'doc_order_header.orderno');
  354. $resultOracleObjs->leftJoin('doc_wave_header', 'doc_wave_header.waveno', 'doc_order_header.waveno');
  355. }
  356. $this->tempFields['temOracleInfo'] = $resultOracleObjs->first();
  357. if (empty($this->tempFields['temOracleInfo'])) {
  358. $resultOracleObjs = OracleDOCOrderHeader::query()->select(self::$oracleOrderHeaderFields)->where('soreference5', $this['logistic_number']);
  359. $resultOracleObjs->leftJoin('doc_wave_header', 'doc_wave_header.waveno', 'doc_order_header.waveno');
  360. }
  361. $this->tempFields['temOracleInfo'] = $resultOracleObjs->first();
  362. return $this->tempFields['temOracleInfo'];
  363. }
  364. public function getLogisticNumberAttribute($val)
  365. {
  366. if (strpos($val, 'null') !== false) return '';
  367. return $val;
  368. }
  369. public function scopeFilter($query, $filters)
  370. {
  371. return $filters->apply($query);
  372. }
  373. public function owner(): BelongsTo
  374. {
  375. return $this->belongsTo(Owner::class);
  376. }
  377. public function orderPackageRemarks(): HasMany
  378. {
  379. return $this->hasMany(OrderPackageRemark::class);
  380. }
  381. public function rejectedBill(): BelongsTo
  382. {
  383. return $this->belongsTo(RejectedBill::class, 'logistic_number', 'logistic_number_return');
  384. }
  385. }