WaybillController.php 10 KB

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