WaybillController.php 10 KB

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