WaybillController.php 8.7 KB

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