WaybillController.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. $warehouseVolume=0;
  39. $warehouseWeight=0;
  40. $sign=false;
  41. foreach ($receiveInputting['order_list'] as $orderInputting){
  42. if ($orderInputting['Cubic'])$warehouseVolume += $orderInputting['Cubic'];
  43. if ($orderInputting['Cubic']==0)$sign=true;
  44. if ($orderInputting['GrossWeight'])$warehouseWeight +=$orderInputting['GrossWeight'];
  45. if (!$orderInputting['GrossWeight'] && $orderInputting['NetWeight']) $warehouseWeight +=$orderInputting['NetWeight'];
  46. $orderInputting['OrderNo']=$receiveInputting['OrderNo'];
  47. if(!WMSWaybillOrder::create($orderInputting)){
  48. Controller::logS(__METHOD__,'error_'.__FUNCTION__,'运单订单行WMS写入错误'.'|'.json_encode($orderInputting));
  49. return response()->json(['response'=>['return'=>['returnFlag'=>'0','returnCode'=>'0001','returnDesc'=>'运单订单行WMS写入错误'.'|'
  50. .json_encode($orderInputting),'resultInfo'=>'']]])
  51. ->setEncodingOptions(JSON_UNESCAPED_UNICODE);
  52. }
  53. }
  54. if ($sign)$warehouseVolume=0;
  55. }
  56. $owner=Owner::where('code',$receiveInputting['CustomerID'])->first();
  57. if (!$owner){$owner=new Owner(['name'=>$receiveInputting['CustomerID'],'code'=>$receiveInputting['CustomerID']]);$owner->save();}
  58. $city=City::where('name',$receiveInputting['C_City'])->first();
  59. $zfList=config('merchantsInfo.waybill.ZFList');
  60. $bswasCustomer=OraccleBasCustomer::where("customerid",$receiveInputting['CustomerID'])->first();
  61. if ($bswasCustomer){
  62. $origination=$bswasCustomer->address1;
  63. $addresses=config("merchantsInfo.address");
  64. foreach ($addresses as $addressKey=>$addressValue){
  65. if (strpos($origination,$addressKey))$origination=$addressValue;
  66. }
  67. }
  68. $recipient_mobile=trim($receiveInputting['C_Tel1'],',');
  69. $waybill=new Waybill([
  70. 'type'=>isset($zfList[$receiveInputting['CarrierID']])&&$zfList[$receiveInputting['CarrierID']]?"直发车":"专线",
  71. 'waybill_number'=>Uuid::uuid1(),
  72. 'owner_id'=>$owner->id,
  73. 'wms_bill_number'=>$receiveInputting['OrderNo']??'',
  74. 'origination'=>isset($origination)?$origination:"",
  75. 'destination'=>$receiveInputting['C_Address1']??'',
  76. 'recipient'=>$receiveInputting['ConsigneeName']??'',
  77. 'recipient_mobile'=>$recipient_mobile??'',
  78. 'source_bill'=>$receiveInputting['ReservedField01']??'',
  79. 'destination_city_id'=>$city['id'],
  80. ]);
  81. $dflist=config('merchantsInfo.waybill.DFList');
  82. if (isset($dflist[$receiveInputting['CarrierID']]) && $dflist[$receiveInputting['CarrierID']])$waybill->collect_fee=0;
  83. if (isset($warehouseVolume)&&$warehouseVolume){
  84. $waybill->warehouse_weight=$warehouseVolume;
  85. $unit=Unit::where('name','m³')->first();
  86. if ($unit)$waybill->warehouse_weight_unit_id=$unit->id;
  87. }
  88. if (isset($warehouseWeight)){
  89. $waybill->warehouse_weight_other=$warehouseWeight;
  90. $unit=Unit::where('name','kg')->first();
  91. if ($unit)$waybill->warehouse_weight_unit_id_other=$unit->id;
  92. }
  93. $waybill->save();
  94. if ($waybill->type=='直发车'){
  95. $waybill_number='BSZF'.date ("ymd").str_pad($waybill->id>99999?$waybill->id%99999:$waybill->id,4,"0",STR_PAD_LEFT);
  96. $waybill->waybill_number=$waybill_number;
  97. $waybill->update();
  98. }else{
  99. $waybill_number='BSZX'.date ("ymd").str_pad($waybill->id>99999?$waybill->id%99999:$waybill->id,4,"0",STR_PAD_LEFT);
  100. $waybill->waybill_number=$waybill_number;
  101. $waybill->update();
  102. }
  103. //回传FLUX
  104. $this->accomplishToWMS($waybill);
  105. }
  106. return response()->json(['response'=>['return'=>['returnFlag'=>'1','returnCode'=>'0000','returnDesc'=>'正确接收','resultInfo'=>'']]])
  107. ->setEncodingOptions(JSON_UNESCAPED_UNICODE);
  108. }
  109. protected function validatorForNew(array $data)
  110. {
  111. return Validator::make($data, [
  112. 'request' => ['required', 'array', 'min:1'],
  113. 'request.*.OrderNo' => ['required', 'string', 'max:191','unique:w_m_s_waybills,OrderNo'],
  114. 'request.*.CustomerID' => ['required', 'string', 'max:191'],
  115. 'request.*.CarrierID' => ['required', 'string', 'max:191'],
  116. 'request.*.ConsigneeName' => ['nullable', 'string', 'max:191'],
  117. 'request.*.C_Tel1' => ['nullable', 'string', 'max:191'],
  118. 'request.*.C_Address1' => ['nullable', 'string', 'max:191'],
  119. 'request.*.C_Province' => ['nullable', 'string', 'max:191'],
  120. 'request.*.C_City' => ['nullable', 'string', 'max:191'],
  121. 'request.*.C_District' => ['nullable', 'string', 'max:191'],
  122. 'request.*.order_list' => ['nullable', 'array', 'min:1'],
  123. 'request.*.order_list.*.SKU_Descr_c' => ['nullable', 'string', 'max:191'],
  124. 'request.*.order_list.*.SKU' => ['nullable', 'string', 'max:191'],
  125. 'request.*.order_list.*.Alternate_sku1' => ['nullable', 'string', 'max:191'],
  126. 'request.*.order_list.*.Qtyordered_each' => ['nullable', 'string', 'max:191'],
  127. 'request.*.order_list.*.Cubic' => ['nullable', 'string', 'max:191'],
  128. 'request.*.order_list.*.GrossWeight' => ['nullable', 'string', 'max:191'],
  129. 'request.*.order_list.*.NetWeight' => ['nullable', 'string', 'max:191'],
  130. ]);
  131. }
  132. public function accomplishToWMS(Waybill $waybill)
  133. {
  134. $json="
  135. {
  136. \"request\": [
  137. {
  138. \"OrderNo\": \"{$waybill['wms_bill_number']}\",
  139. \"DELIVERYNO\": \"{$waybill['waybill_number']}\"
  140. }
  141. ]
  142. }
  143. ";
  144. $sendingJson = json_decode($json,true);
  145. $url=url(config('api.flux.waybill.new'));
  146. try{
  147. $response = Zttp::post($url, $sendingJson);
  148. }catch (\Exception $exception){
  149. $this->log(__METHOD__,'Exception_'.__FUNCTION__,'CURL请求异常:'.$exception->getMessage(),null);
  150. return false;
  151. }
  152. $responseJson = $response->json();
  153. if(!$responseJson||!isset($responseJson['Response'])||!$responseJson['Response']['return']['returnFlag']=='1'){
  154. Controller::logS(__METHOD__,'error_'.__FUNCTION__,"向WMS提交运单失败!!SO单号:{$waybill['wms_bill_number']}。提交:{$json},返回:{$response->body()}, URL:{$url}",Auth::user()['id']);
  155. return false;
  156. }
  157. Controller::logS(__METHOD__,''.__FUNCTION__,"向WMS提交运单成功,SO单号:{$waybill['wms_bill_number']}。返回:{$response->body()}",Auth::user()['id']);
  158. return true;
  159. }
  160. }