WaybillController.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace App\Http\Controllers\api\thirdPart\flux;
  3. use App\City;
  4. use App\Http\Controllers\Controller;
  5. use App\Jobs\HandleExceptionWaybill;
  6. use App\Order;
  7. use App\Owner;
  8. use App\Services\RegionService;
  9. use App\Unit;
  10. use App\Waybill;
  11. use App\OracleBasCustomer;
  12. use App\WaybillAuditLog;
  13. use App\WMSWaybill;
  14. use App\WMSWaybillOrder;
  15. use Illuminate\Http\Request;
  16. use Illuminate\Support\Facades\Auth;
  17. use Illuminate\Support\Facades\Validator;
  18. use Ramsey\Uuid\Uuid;
  19. use Zttp\Zttp;
  20. class WaybillController extends Controller
  21. {
  22. public function new_(Request $request)
  23. {
  24. (new Controller())->log(__METHOD__,__FUNCTION__,'WMS requesting:.'.'|'.json_encode($request->all()));
  25. $errors=$this->validatorForNew($request->all())->errors();
  26. if(count($errors)>0){
  27. app('LogService')->log(__METHOD__, 'error_' . __FUNCTION__, json_encode($request->getContent()));
  28. (new Controller())->log(__METHOD__,'error_'.__FUNCTION__,'fields wrong, see Errors report please.'.'|'.json_encode($request->all()).'|'.json_encode($errors));
  29. return response()->json(['response'=>['return'=>['returnFlag'=>'0','returnCode'=>'0001','returnDesc'=>'消息处理失败:Failure','resultInfo'=>'',
  30. 'errors'=>$errors]]])
  31. ->setEncodingOptions(JSON_UNESCAPED_UNICODE);
  32. }
  33. $receivedInputtings=$request->input('request');
  34. foreach ($receivedInputtings as $receiveInputting){
  35. if(!WMSWaybill::query()->create($receiveInputting)){
  36. Controller::logS(__METHOD__,'error_'.__FUNCTION__,'运单WMS写入错误'.'|'.json_encode($receiveInputting));
  37. return response()->json(['response'=>['return'=>['returnFlag'=>'0','returnCode'=>'0001','returnDesc'=>'运单WMS写入错误'.'|'
  38. .json_encode($receiveInputting),'resultInfo'=>'']]])
  39. ->setEncodingOptions(JSON_UNESCAPED_UNICODE);
  40. }
  41. if(isset($receiveInputting['order_list'])&&$receiveInputting['order_list']){
  42. $warehouseVolume=0;
  43. $warehouseWeight=0;
  44. $sign=false;
  45. foreach ($receiveInputting['order_list'] as $orderInputting){
  46. if ($orderInputting['Cubic'])$warehouseVolume += $orderInputting['Cubic'];
  47. if ($orderInputting['Cubic']==0)$sign=true;
  48. if ($orderInputting['GrossWeight'])$warehouseWeight +=$orderInputting['GrossWeight'];
  49. if (!$orderInputting['GrossWeight'] && $orderInputting['NetWeight']) $warehouseWeight +=$orderInputting['NetWeight'];
  50. $orderInputting['OrderNo']=$receiveInputting['OrderNo'];
  51. if(!WMSWaybillOrder::query()->create($orderInputting)){
  52. Controller::logS(__METHOD__,'error_'.__FUNCTION__,'运单订单行WMS写入错误'.'|'.json_encode($orderInputting));
  53. return response()->json(['response'=>['return'=>['returnFlag'=>'0','returnCode'=>'0001','returnDesc'=>'运单订单行WMS写入错误'.'|'
  54. .json_encode($orderInputting),'resultInfo'=>'']]])
  55. ->setEncodingOptions(JSON_UNESCAPED_UNICODE);
  56. }
  57. }
  58. if ($sign)$warehouseVolume=0;
  59. }
  60. $owner=Owner::query()->where('code',$receiveInputting['CustomerID'])->first();
  61. if (!$owner){$owner=new Owner(['name'=>$receiveInputting['CustomerID'],'code'=>$receiveInputting['CustomerID']]);$owner->save();}
  62. $zfList=config('merchantsInfo.waybill.ZFList');
  63. $bswasCustomer=OracleBasCustomer::query()->where("customerid",$receiveInputting['CustomerID'])->first();
  64. if ($bswasCustomer){
  65. $origination=$bswasCustomer->address1;
  66. $addresses=config("merchantsInfo.address");
  67. foreach ($addresses as $addressKey=>$addressValue){
  68. if (strpos($origination,$addressKey))$origination=$addressValue;
  69. }
  70. }
  71. $recipient_mobile=trim($receiveInputting['C_Tel1'],',');
  72. $waybill=new Waybill([
  73. 'type'=>isset($zfList[$receiveInputting['CarrierID']])&&$zfList[$receiveInputting['CarrierID']]?"直发车":"专线",
  74. 'waybill_number'=>Uuid::uuid1(),
  75. 'owner_id'=>$owner->id,
  76. 'wms_bill_number'=>$receiveInputting['OrderNo']??'',
  77. 'origination'=>isset($origination)?$origination:"",
  78. 'destination'=>$receiveInputting['C_Address1']??'',
  79. 'recipient'=>$receiveInputting['ConsigneeName']??'',
  80. 'recipient_mobile'=>$recipient_mobile??'',
  81. 'source_bill'=>$receiveInputting['ReservedField01']??'',
  82. 'is_to_pay'=>strstr($receiveInputting['CarrierID']??'','DF')===false ? 0 : 1,
  83. 'destination_city_id'=>app(RegionService::class)->getCity($receiveInputting['C_City']),
  84. ]);
  85. $dflist=config('merchantsInfo.waybill.DFList');
  86. if (isset($dflist[$receiveInputting['CarrierID']]) && $dflist[$receiveInputting['CarrierID']])$waybill->collect_fee=0;
  87. if (isset($warehouseVolume)&&$warehouseVolume){
  88. $waybill->warehouse_weight=$warehouseVolume;
  89. $unit=Unit::query()->where('name','m³')->first();
  90. if ($unit)$waybill->warehouse_weight_unit_id=$unit->id;
  91. }
  92. if (isset($warehouseWeight)){
  93. $waybill->warehouse_weight_other=$warehouseWeight;
  94. $unit=Unit::query()->where('name','kg')->first();
  95. if ($unit)$waybill->warehouse_weight_unit_id_other=$unit->id;
  96. }
  97. if ($waybill->wms_bill_number){
  98. $order = app("OrderService")->first(["code"=>$waybill->wms_bill_number]);
  99. if ($order)$waybill->order_id = $order->id;
  100. }
  101. $waybill->save();
  102. WaybillAuditLog::query()->create([
  103. 'waybill_id'=>$waybill->id,
  104. 'audit_stage'=>'创建',
  105. 'user_id'=>Auth::id() ?? 0,
  106. ]);
  107. if ($waybill->type=='直发车'){
  108. $waybill_number='BSZF'.date ("ymd").str_pad($waybill->id>99999?$waybill->id%99999:$waybill->id,4,"0",STR_PAD_LEFT);
  109. $waybill->waybill_number=$waybill_number;
  110. $waybill->update();
  111. }else{
  112. $waybill_number='BSZX'.date ("ymd").str_pad($waybill->id>99999?$waybill->id%99999:$waybill->id,4,"0",STR_PAD_LEFT);
  113. $waybill->waybill_number=$waybill_number;
  114. $waybill->update();
  115. }
  116. if (!$waybill->order_id && $waybill->wms_bill_number)dispatch(new HandleExceptionWaybill($waybill))->delay(now()->addMinutes(15));
  117. //回传FLUX
  118. $this->accomplishToWMS($waybill);
  119. }
  120. return response()->json(['response'=>['return'=>['returnFlag'=>'1','returnCode'=>'0000','returnDesc'=>'正确接收','resultInfo'=>'']]])
  121. ->setEncodingOptions(JSON_UNESCAPED_UNICODE);
  122. }
  123. protected function validatorForNew(array $data)
  124. {
  125. return Validator::make($data, [
  126. 'request' => ['required', 'array', 'min:1'],
  127. 'request.*.OrderNo' => ['required', 'string', 'max:191','unique:w_m_s_waybills,OrderNo'],
  128. 'request.*.CustomerID' => ['required', 'string', 'max:191'],
  129. 'request.*.CarrierID' => ['required', 'string', 'max:191'],
  130. 'request.*.ConsigneeName' => ['nullable', 'string', 'max:191'],
  131. 'request.*.C_Tel1' => ['nullable', 'string', 'max:191'],
  132. 'request.*.C_Address1' => ['nullable', 'string', 'max:191'],
  133. 'request.*.C_Province' => ['nullable', 'string', 'max:191'],
  134. 'request.*.C_City' => ['nullable', 'string', 'max:191'],
  135. 'request.*.C_District' => ['nullable', 'string', 'max:191'],
  136. 'request.*.order_list' => ['nullable', 'array', 'min:1'],
  137. 'request.*.order_list.*.SKU_Descr_c' => ['nullable', 'string', 'max:191'],
  138. 'request.*.order_list.*.SKU' => ['nullable', 'string', 'max:191'],
  139. 'request.*.order_list.*.Alternate_sku1' => ['nullable', 'string', 'max:191'],
  140. 'request.*.order_list.*.Qtyordered_each' => ['nullable', 'string', 'max:191'],
  141. 'request.*.order_list.*.Cubic' => ['nullable', 'string', 'max:191'],
  142. 'request.*.order_list.*.GrossWeight' => ['nullable', 'string', 'max:191'],
  143. 'request.*.order_list.*.NetWeight' => ['nullable', 'string', 'max:191'],
  144. ]);
  145. }
  146. public function accomplishToWMS(Waybill $waybill)
  147. {
  148. $json="
  149. {
  150. \"request\": [
  151. {
  152. \"OrderNo\": \"{$waybill['wms_bill_number']}\",
  153. \"DELIVERYNO\": \"{$waybill['waybill_number']}\"
  154. }
  155. ]
  156. }
  157. ";
  158. $sendingJson = json_decode($json,true);
  159. $url=url(config('api.flux.waybill.new'));
  160. try{
  161. $response = Zttp::post($url, $sendingJson);
  162. }catch (\Exception $exception){
  163. app('LogService')->log(__METHOD__,'Exception_'.__FUNCTION__,'CURL请求异常:'.$exception->getMessage(),null);
  164. return false;
  165. }
  166. $responseJson = $response->json();
  167. if(!$responseJson||!isset($responseJson['Response'])||!$responseJson['Response']['return']['returnFlag']=='1'){
  168. Controller::logS(__METHOD__,'error_'.__FUNCTION__,"向WMS提交运单失败!!SO单号:{$waybill['wms_bill_number']}。提交:{$json},返回:{$response->body()}, URL:{$url}",Auth::user()['id']);
  169. return false;
  170. }
  171. Controller::logS(__METHOD__,''.__FUNCTION__,"向WMS提交运单成功,SO单号:{$waybill['wms_bill_number']}。返回:{$response->body()}",Auth::user()['id']);
  172. return true;
  173. }
  174. }