WaybillController.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace App\Http\Controllers\api\thirdPart\flux;
  3. use App\City;
  4. use App\Http\Controllers\Controller;
  5. use App\Order;
  6. use App\Owner;
  7. use App\Services\RegionService;
  8. use App\Unit;
  9. use App\Waybill;
  10. use App\OracleBasCustomer;
  11. use App\WaybillAuditLog;
  12. use App\WMSWaybill;
  13. use App\WMSWaybillOrder;
  14. use Illuminate\Http\Request;
  15. use Illuminate\Support\Facades\Auth;
  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. 'destination_city_id'=>app(RegionService::class)->getCity($receiveInputting['C_City']),
  82. ]);
  83. $dflist=config('merchantsInfo.waybill.DFList');
  84. if (isset($dflist[$receiveInputting['CarrierID']]) && $dflist[$receiveInputting['CarrierID']])$waybill->collect_fee=0;
  85. if (isset($warehouseVolume)&&$warehouseVolume){
  86. $waybill->warehouse_weight=$warehouseVolume;
  87. $unit=Unit::query()->where('name','m³')->first();
  88. if ($unit)$waybill->warehouse_weight_unit_id=$unit->id;
  89. }
  90. if (isset($warehouseWeight)){
  91. $waybill->warehouse_weight_other=$warehouseWeight;
  92. $unit=Unit::query()->where('name','kg')->first();
  93. if ($unit)$waybill->warehouse_weight_unit_id_other=$unit->id;
  94. }
  95. if ($waybill->wms_bill_number){
  96. $order = app("OrderService")->first(["code"=>$waybill->wms_bill_number]);
  97. if ($order)$waybill->order_id = $order->id;
  98. }
  99. $waybill->save();
  100. WaybillAuditLog::query()->create([
  101. 'waybill_id'=>$waybill->id,
  102. 'audit_stage'=>'创建',
  103. 'user_id'=>Auth::id() ?? 0,
  104. ]);
  105. if ($waybill->type=='直发车'){
  106. $waybill_number='BSZF'.date ("ymd").str_pad($waybill->id>99999?$waybill->id%99999:$waybill->id,4,"0",STR_PAD_LEFT);
  107. $waybill->waybill_number=$waybill_number;
  108. $waybill->update();
  109. }else{
  110. $waybill_number='BSZX'.date ("ymd").str_pad($waybill->id>99999?$waybill->id%99999:$waybill->id,4,"0",STR_PAD_LEFT);
  111. $waybill->waybill_number=$waybill_number;
  112. $waybill->update();
  113. }
  114. //回传FLUX
  115. $this->accomplishToWMS($waybill);
  116. }
  117. return response()->json(['response'=>['return'=>['returnFlag'=>'1','returnCode'=>'0000','returnDesc'=>'正确接收','resultInfo'=>'']]])
  118. ->setEncodingOptions(JSON_UNESCAPED_UNICODE);
  119. }
  120. protected function validatorForNew(array $data)
  121. {
  122. return Validator::make($data, [
  123. 'request' => ['required', 'array', 'min:1'],
  124. 'request.*.OrderNo' => ['required', 'string', 'max:191','unique:w_m_s_waybills,OrderNo'],
  125. 'request.*.CustomerID' => ['required', 'string', 'max:191'],
  126. 'request.*.CarrierID' => ['required', 'string', 'max:191'],
  127. 'request.*.ConsigneeName' => ['nullable', 'string', 'max:191'],
  128. 'request.*.C_Tel1' => ['nullable', 'string', 'max:191'],
  129. 'request.*.C_Address1' => ['nullable', 'string', 'max:191'],
  130. 'request.*.C_Province' => ['nullable', 'string', 'max:191'],
  131. 'request.*.C_City' => ['nullable', 'string', 'max:191'],
  132. 'request.*.C_District' => ['nullable', 'string', 'max:191'],
  133. 'request.*.order_list' => ['nullable', 'array', 'min:1'],
  134. 'request.*.order_list.*.SKU_Descr_c' => ['nullable', 'string', 'max:191'],
  135. 'request.*.order_list.*.SKU' => ['nullable', 'string', 'max:191'],
  136. 'request.*.order_list.*.Alternate_sku1' => ['nullable', 'string', 'max:191'],
  137. 'request.*.order_list.*.Qtyordered_each' => ['nullable', 'string', 'max:191'],
  138. 'request.*.order_list.*.Cubic' => ['nullable', 'string', 'max:191'],
  139. 'request.*.order_list.*.GrossWeight' => ['nullable', 'string', 'max:191'],
  140. 'request.*.order_list.*.NetWeight' => ['nullable', 'string', 'max:191'],
  141. ]);
  142. }
  143. public function accomplishToWMS(Waybill $waybill)
  144. {
  145. $json="
  146. {
  147. \"request\": [
  148. {
  149. \"OrderNo\": \"{$waybill['wms_bill_number']}\",
  150. \"DELIVERYNO\": \"{$waybill['waybill_number']}\"
  151. }
  152. ]
  153. }
  154. ";
  155. $sendingJson = json_decode($json,true);
  156. $url=url(config('api.flux.waybill.new'));
  157. try{
  158. $response = Zttp::post($url, $sendingJson);
  159. }catch (\Exception $exception){
  160. app('LogService')->log(__METHOD__,'Exception_'.__FUNCTION__,'CURL请求异常:'.$exception->getMessage(),null);
  161. return false;
  162. }
  163. $responseJson = $response->json();
  164. if(!$responseJson||!isset($responseJson['Response'])||!$responseJson['Response']['return']['returnFlag']=='1'){
  165. Controller::logS(__METHOD__,'error_'.__FUNCTION__,"向WMS提交运单失败!!SO单号:{$waybill['wms_bill_number']}。提交:{$json},返回:{$response->body()}, URL:{$url}",Auth::user()['id']);
  166. return false;
  167. }
  168. Controller::logS(__METHOD__,''.__FUNCTION__,"向WMS提交运单成功,SO单号:{$waybill['wms_bill_number']}。返回:{$response->body()}",Auth::user()['id']);
  169. return true;
  170. }
  171. }