SortingController.php 12 KB

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