OrderPackage.php 16 KB

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