WaybillController.php 10 KB

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