SortingController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace App\Http\Controllers\api\thirdPart\flux;
  3. use App\Batch;
  4. use App\Http\Controllers\Controller;
  5. use App\Order;
  6. use App\OrderCommodity;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Http\Response;
  9. use Illuminate\Support\Facades\Log;
  10. use Illuminate\Support\Facades\Validator;
  11. use Zttp\Zttp;
  12. class SortingController extends Controller
  13. {
  14. /**
  15. * 新增被通知的波次列表(一个以上的波次),并且保存在本地数据库,供get波次使用
  16. * 接收:request[(下边newBatch的字段)]
  17. * 返回:Response{return{returnFlag(1/0),returnCode(0000/0001),returnDesc,resultInfo}}
  18. *
  19. * @param Request $request
  20. * @return \Illuminate\Http\JsonResponse|Response|\Symfony\Component\HttpFoundation\JsonResponse
  21. *
  22. * 2021-01-20 zzd 因同步订单,波次等已上线,弃用该通知接口
  23. */
  24. public function newBatch(Request $request)
  25. {
  26. // $requestArr=$request->all();
  27. app('LogService')->log(__METHOD__, 'issued_' . __FUNCTION__, json_encode($request->all()));
  28. // !$requestArr?$requestArr=json_decode($request->getContent(),true):false;
  29. // $errors=$this->newBatchValidator($requestArr)->errors();
  30. // if(count($errors)>0){
  31. // app('LogService')->log(__METHOD__, 'error' . __FUNCTION__, 'fields wrong, see Errors report please.'.'|'.json_encode($request->all()).'|'.json_encode($errors));
  32. // return response()->json(['Response'=>['return'=>['returnFlag'=>'0','returnCode'=>'0001',
  33. // 'returnDesc'=>':Failure','resultInfo'=>'','errors'=>$errors]]])->setEncodingOptions(JSON_UNESCAPED_UNICODE);
  34. // }
  35. // $requestBatches = $requestArr['request']?? '';
  36. //
  37. // foreach ($requestBatches as $requestBatch){
  38. // $requestBatch['edittime']&&strpos(trim($requestBatch['edittime']),' ')?$editTimeFormat='Y-m-d H:i:s':$editTimeFormat='YmdHis';
  39. // $batch=Batch::query()->firstOrCreate(['code' => $requestBatch['waveno']]);
  40. //
  41. // if(!$batch)$batch=new Batch();
  42. // $batch->fill([
  43. // 'code' => $requestBatch['waveno'],
  44. // 'wms_type' => $requestBatch['batch_type']??'',
  45. // 'wms_status' => $requestBatch['docstatus']??'',
  46. // 'status' => '未处理',
  47. // 'wms_created_at' => $requestBatch['edittime']?Carbon::createFromFormat($editTimeFormat,$requestBatch['edittime']):'',
  48. // ]);
  49. // $batch->save();
  50. // $oracleAlloactions=OracleActAllocationDetails::query()->where('waveno',$requestBatch['waveno'])->get();
  51. // foreach($requestBatch['order_list'] as $requestOrder){
  52. // $owner=Owner::query()->where('code',$requestOrder['customerid'])->first();
  53. // $order=app("OrderService")->first(['code'=>$requestOrder['docno']]);
  54. // if(!$order){
  55. // $order=app("OrderService")->createOrder([
  56. // 'batch_id' => $batch['id'],
  57. // 'code' => $requestOrder['docno'],
  58. // 'owner_id' => $owner['id'],
  59. // 'wms_status' => $requestOrder['docstatus']??'波次下发',
  60. // 'status' => '未处理',
  61. // ]);
  62. // app('LogService')->log(__METHOD__,__FUNCTION__,'创建 Order'.json_encode($order));
  63. // }else{
  64. // $order['batch_id']= $batch['id'] ;
  65. // $order['owner_id']=$order['owner_id']??$owner['owner_id'];
  66. // $order['wms_status']=$order['wms_status']??$requestOrder['docstatus']??'波次下发';
  67. // $order['status']=$order['status']??'未处理';
  68. // }
  69. // $order->save();
  70. // OrderBin::query()->firstOrCreate([
  71. // 'order_id' => $order['id'],
  72. // 'number' => $requestOrder['reservedfield01'],
  73. // ]);
  74. // foreach($requestOrder['barcode_list'] as $requestBarcode){
  75. // $orderCommodity=OrderCommodity::query()
  76. // ->where('order_id',$order['id'])->where('wms_ptltaskid',$requestBarcode['ptltaskid'])->first();
  77. // if(!$orderCommodity){
  78. // /** @var CommodityService $commodityService */
  79. // $commodityService=app('CommodityService');
  80. // $commodity=$commodityService->syncBarcodes($requestBarcode['alternate_sku1'],$owner['id'],$requestBarcode['sku']);
  81. // $orderCommodity=OrderCommodity::query()->firstOrCreate(['order_id'=>$order['id'],'commodity_id'=>$commodity['id']]);
  82. // if(!$orderCommodity){
  83. // $orderCommodity = new OrderCommodity([
  84. // 'order_id' => $order['id'],
  85. // 'commodity_id' => $commodity['id'],
  86. // 'amount' => $requestBarcode['fmqty_each']??0,
  87. // 'wms_ptltaskid' => $requestBarcode['ptltaskid'],
  88. // ]);
  89. // }else{
  90. // $orderCommodity['order_id']=$order['id'];
  91. // $orderCommodity['commodity_id']=$commodity['id'];
  92. // $orderCommodity['amount']=$requestBarcode['fmqty_each']??0;
  93. // $orderCommodity['wms_ptltaskid']=$requestBarcode['ptltaskid']??0;
  94. // }
  95. // $allocation=$oracleAlloactions->where('orderno',$requestOrder['docno'])->where('sku',$commodity['sku'])->where('qty',$requestBarcode['fmqty_each'])->first();
  96. // if($allocation)
  97. // $orderCommodity['location'] = $allocation['location'];
  98. // $orderCommodity->save();
  99. // }
  100. // }
  101. // }
  102. // }
  103. return response()->json(['Response'=>['return'=>['returnFlag'=>'1','returnCode'=>'0000',
  104. 'returnDesc'=>'消息处理成功:Success','resultInfo'=>'']]])->setEncodingOptions(JSON_UNESCAPED_UNICODE);
  105. }
  106. protected function newBatchValidator(array $data)
  107. {
  108. return Validator::make($data, [
  109. 'request' => ['required', 'array', 'min:1'],
  110. 'request.*.waveno' => ['required', 'string', 'max:191'],
  111. 'request.*.taskprocess' => ['nullable', 'string', 'max:191'],
  112. 'request.*.edittime' => ['nullable', 'string', 'max:191'],
  113. 'request.*.batch_type' => ['nullable', 'string', 'max:191'],
  114. 'request.*.docstatus' => ['nullable', 'string', 'max:191'],
  115. 'request.*.batch_created_at' => ['nullable', 'string', 'max:191'],
  116. 'request.*.order_list' => ['required', 'array', 'min:1'],
  117. 'request.*.order_list.*.docno' => ['required', 'string', 'max:191'],
  118. 'request.*.order_list.*.customerid' => ['required', 'string', 'max:191','exists:owners,code'],
  119. 'request.*.order_list.*.docstatus' => ['nullable', 'string', 'max:191'],
  120. 'request.*.order_list.*.reservedfield01' => ['required', 'max:191'],
  121. 'request.*.order_list.*.barcode_list' => ['required_unless:request.*.order_list.*.docstatus,90', 'array'],
  122. 'request.*.order_list.*.barcode_list.*.alternate_sku1' => ['required', 'string', 'max:191'],
  123. 'request.*.order_list.*.barcode_list.*.descr_c' => ['required', 'string', 'max:191'],
  124. 'request.*.order_list.*.barcode_list.*.fmqty_each' => ['required', 'numeric'],
  125. 'request.*.order_list.*.barcode_list.*.ptltaskid' => ['required', 'string', 'max:191'],
  126. ]);
  127. }
  128. /**
  129. * 新增被通知的取消订单
  130. * 接收:docno(订单号),docstatus(状态,唯一:canceled)
  131. * 返回:Response{return{returnFlag(1/0),returnCode(0000/0001),returnDesc,resultInfo}} 1和0000成功,0和0001失败
  132. */
  133. public function newCanceledOrder(Request $request)
  134. {
  135. $requestArr=$request->all();
  136. !$requestArr?$requestArr=json_decode($request->getContent(),true):false;
  137. Controller::logS(__METHOD__,__FUNCTION__,"接收到WMS下发取消单:".$request->getContent());
  138. $errors=$this->newCanceledOrderValidator($requestArr)->errors();
  139. if(count($errors)>0){
  140. app('LogService')->log(__METHOD__, 'error' . __FUNCTION__, 'fields wrong, see Errors report please.'.'|'.json_encode($request->all()).'|'.json_encode($errors));
  141. return response()->json(['Response'=>['return'=>['returnFlag'=>'0','returnCode'=>'0001',
  142. 'returnDesc'=>':Failure','resultInfo'=>'','errors'=>$errors]]])->setEncodingOptions(JSON_UNESCAPED_UNICODE);
  143. }
  144. $order = app("OrderService")->first(['code'=>$requestArr['docno']]);
  145. $order->cancel();
  146. return response()->json(['Response'=>['return'=>['returnFlag'=>'1','returnCode'=>'0000',
  147. 'returnDesc'=>'消息处理成功:Success','resultInfo'=>'']]])->setEncodingOptions(JSON_UNESCAPED_UNICODE);
  148. }
  149. protected function newCanceledOrderValidator(array $data)
  150. {
  151. return Validator::make($data, [
  152. 'docno' => ['required', 'string', 'max:191', 'exists:orders,code'],
  153. 'docstatus' => ['required', 'string', 'max:191'],
  154. ]);
  155. }
  156. public function informBinAssignment(Batch $batch)
  157. {
  158. $apiUrl=config('api.flux.inform.binAssignment');
  159. if(config('api.faking')){$apiUrl=url(config('api.fake_flux_informBinAssignment'));}
  160. $sendingData=['request'=>[]];
  161. $batch->orders()->each(function(Order $order)use($batch,&$sendingData){
  162. $sendingData['request'][]=[
  163. 'batch_id'=>$batch['code'],
  164. 'status'=>'00',//原来是beforeSorting,改成了00
  165. 'order_id'=>$order['code'],
  166. 'bin'=>$order->bin()->first()['number']
  167. ];
  168. });
  169. try {
  170. $response=Zttp::post($apiUrl,$sendingData);
  171. }catch (\Exception $e){
  172. app('LogService')->log(__METHOD__,'error_'.__FUNCTION__,json_encode($sendingData).'|catch:'.$e->getMessage());
  173. return false;
  174. }
  175. $reJson=$response->json();
  176. if(!$reJson || (!isset($reJson['Response'])||!isset($reJson['Response']['return'])) || $reJson['Response']['return']['returnFlag']!='1'){
  177. app('LogService')->log(__METHOD__,'error_'.__FUNCTION__,'$sending:'.json_encode($sendingData).'$response:'.$response->body());
  178. return false;
  179. }
  180. return true;
  181. }
  182. public function informBatchFinished(Batch $batch){
  183. $sendingData=['request'=>[]];
  184. $batch->orders()->each(function (Order $order)use($batch,&$sendingData){
  185. $order->orderCommodities()->each(function (OrderCommodity $orderCommodity)use($batch,$order,&$sendingData){
  186. $bin=$order->bin()->first();
  187. $sendingData['request'][]=[
  188. 'ptltaskid'=>$orderCommodity['wms_ptltaskid'],
  189. 'batch_id'=>$batch['code'],
  190. 'status'=>'80', //原来是isSorted,改成了80
  191. 'order_id'=>$order['code'],
  192. 'bin'=>$bin?$bin['number']:'',
  193. 'docstatus'=>'success',
  194. 'sku'=>$orderCommodity->commodity()->first()?$orderCommodity->commodity()->first()['sku']:'',
  195. 'amount'=>'0',
  196. ];
  197. });
  198. });
  199. $informApiUrl = config('api.flux.inform.batchFinished');
  200. try{
  201. $response=Zttp::post($informApiUrl,$sendingData);
  202. $result=$response->json();
  203. if(!$result||!$result['Response']['return']['returnFlag']||$result['Response']['return']['returnCode']!='0000'){
  204. app('LogService')->log(__METHOD__,'error_'.__FUNCTION__,'$sending:'.json_encode($sendingData).'|$response:'.$response->body());
  205. return false;
  206. }
  207. }
  208. catch(\Exception $e){
  209. app('LogService')->log(__METHOD__,'error_'.__FUNCTION__,'catch:'.$e->getMessage());
  210. return false;
  211. }
  212. app('LogService')->log(__METHOD__,'temp_'.__FUNCTION__,'$sending:'.json_encode($sendingData).'|$response:'.$response->body());
  213. return true;
  214. }
  215. }