WaybillController.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. 'is_to_pay'=>strstr($receiveInputting['CarrierID']??'','DF')===false ? 0 : 1,
  82. 'destination_city_id'=>app(RegionService::class)->getCity($receiveInputting['C_City']),
  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. $order = app("OrderService")->first(["code"=>$waybill->wms_bill_number]);
  98. if ($order)$waybill->order_id = $order->id;
  99. }
  100. $waybill->save();
  101. WaybillAuditLog::query()->create([
  102. 'waybill_id'=>$waybill->id,
  103. 'audit_stage'=>'创建',
  104. 'user_id'=>Auth::id() ?? 0,
  105. ]);
  106. if ($waybill->type=='直发车'){
  107. $waybill_number='BSZF'.date ("ymd").str_pad($waybill->id>99999?$waybill->id%99999:$waybill->id,4,"0",STR_PAD_LEFT);
  108. $waybill->waybill_number=$waybill_number;
  109. $waybill->update();
  110. }else{
  111. $waybill_number='BSZX'.date ("ymd").str_pad($waybill->id>99999?$waybill->id%99999:$waybill->id,4,"0",STR_PAD_LEFT);
  112. $waybill->waybill_number=$waybill_number;
  113. $waybill->update();
  114. }
  115. //回传FLUX
  116. $this->accomplishToWMS($waybill);
  117. }
  118. return response()->json(['response'=>['return'=>['returnFlag'=>'1','returnCode'=>'0000','returnDesc'=>'正确接收','resultInfo'=>'']]])
  119. ->setEncodingOptions(JSON_UNESCAPED_UNICODE);
  120. }
  121. protected function validatorForNew(array $data)
  122. {
  123. return Validator::make($data, [
  124. 'request' => ['required', 'array', 'min:1'],
  125. 'request.*.OrderNo' => ['required', 'string', 'max:191','unique:w_m_s_waybills,OrderNo'],
  126. 'request.*.CustomerID' => ['required', 'string', 'max:191'],
  127. 'request.*.CarrierID' => ['required', 'string', 'max:191'],
  128. 'request.*.ConsigneeName' => ['nullable', 'string', 'max:191'],
  129. 'request.*.C_Tel1' => ['nullable', 'string', 'max:191'],
  130. 'request.*.C_Address1' => ['nullable', 'string', 'max:191'],
  131. 'request.*.C_Province' => ['nullable', 'string', 'max:191'],
  132. 'request.*.C_City' => ['nullable', 'string', 'max:191'],
  133. 'request.*.C_District' => ['nullable', 'string', 'max:191'],
  134. 'request.*.order_list' => ['nullable', 'array', 'min:1'],
  135. 'request.*.order_list.*.SKU_Descr_c' => ['nullable', 'string', 'max:191'],
  136. 'request.*.order_list.*.SKU' => ['nullable', 'string', 'max:191'],
  137. 'request.*.order_list.*.Alternate_sku1' => ['nullable', 'string', 'max:191'],
  138. 'request.*.order_list.*.Qtyordered_each' => ['nullable', 'string', 'max:191'],
  139. 'request.*.order_list.*.Cubic' => ['nullable', 'string', 'max:191'],
  140. 'request.*.order_list.*.GrossWeight' => ['nullable', 'string', 'max:191'],
  141. 'request.*.order_list.*.NetWeight' => ['nullable', 'string', 'max:191'],
  142. ]);
  143. }
  144. public function accomplishToWMS(Waybill $waybill)
  145. {
  146. $json="
  147. {
  148. \"request\": [
  149. {
  150. \"OrderNo\": \"{$waybill['wms_bill_number']}\",
  151. \"DELIVERYNO\": \"{$waybill['waybill_number']}\"
  152. }
  153. ]
  154. }
  155. ";
  156. $sendingJson = json_decode($json,true);
  157. $url=url(config('api.flux.waybill.new'));
  158. try{
  159. $response = Zttp::post($url, $sendingJson);
  160. }catch (\Exception $exception){
  161. app('LogService')->log(__METHOD__,'Exception_'.__FUNCTION__,'CURL请求异常:'.$exception->getMessage(),null);
  162. return false;
  163. }
  164. $responseJson = $response->json();
  165. if(!$responseJson||!isset($responseJson['Response'])||!$responseJson['Response']['return']['returnFlag']=='1'){
  166. Controller::logS(__METHOD__,'error_'.__FUNCTION__,"向WMS提交运单失败!!SO单号:{$waybill['wms_bill_number']}。提交:{$json},返回:{$response->body()}, URL:{$url}",Auth::user()['id']);
  167. return false;
  168. }
  169. Controller::logS(__METHOD__,''.__FUNCTION__,"向WMS提交运单成功,SO单号:{$waybill['wms_bill_number']}。返回:{$response->body()}",Auth::user()['id']);
  170. return true;
  171. }
  172. }