OrderPackage.php 16 KB

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