WaybillController.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Services\CarrierService;
  4. use App\Services\CarTypeService;
  5. use App\Services\CityService;
  6. use App\Services\OwnerService;
  7. use App\Services\UnitService;
  8. use App\Services\WaybillPayoffService;
  9. use App\Services\WaybillPriceModelService;
  10. use App\Services\WaybillService;
  11. use App\UploadFile;
  12. use App\WaybillAuditLog;
  13. use App\WaybillOnTop;
  14. use App\WaybillPriceModel;
  15. use App\City;
  16. use App\Owner;
  17. use App\Unit;
  18. use App\Waybill;
  19. use App\WaybillPayoff;
  20. use App\WaybillFinancialExcepted;
  21. use App\WaybillFinancialSnapshot;
  22. use Carbon\Carbon;
  23. use Illuminate\Http\Request;
  24. use Illuminate\Support\Facades\Auth;
  25. use Illuminate\Support\Facades\DB;
  26. use Illuminate\Support\Facades\Gate;
  27. use Illuminate\Support\Facades\Http;
  28. use Illuminate\Support\Facades\Validator;
  29. use Intervention\Image\Facades\Image;
  30. use Ramsey\Uuid\Uuid;
  31. class WaybillController extends Controller
  32. {
  33. public function __construct()
  34. {
  35. app()->singleton('waybillService',WaybillService::class);
  36. }
  37. /**
  38. * @param Request $request
  39. * @param OwnerService $ownerService
  40. * @param CarrierService $carrierService
  41. * @return void
  42. */
  43. public function index(Request $request,OwnerService $ownerService,CarrierService $carrierService)
  44. {
  45. if(!Gate::allows('运输管理-查询')){ return redirect(url('/')); }
  46. $paginateParams = $request->input();
  47. $waybills=app('waybillService')->paginate($request->input());
  48. return view('waybill.index', [
  49. 'waybills' => $waybills,
  50. 'carriers' => $carrierService->getSelection(),
  51. 'owners' => $ownerService->getSelection(),
  52. 'paginateParams'=>$paginateParams,
  53. 'uriType'=>$request->uriType??'']);
  54. }
  55. public function create(Request $request,OwnerService $ownerService)
  56. {
  57. if(!Gate::allows('运输管理-录入')){ return redirect(url('/')); }
  58. $type="";
  59. if ($request->type==='ZF')$type='直发车';
  60. if ($request->type==='ZX')$type='专线';
  61. return view('waybill.create',['owners'=>$ownerService->getSelection(),'type'=>$type]);
  62. }
  63. public function store(Request $request)
  64. {
  65. if(!Gate::allows('运输管理-录入')){ return redirect(url('/')); }
  66. $id=false;
  67. $this->validatorWaybill($request,$id)->validate();
  68. $waybill=app('waybillService')->store($request);
  69. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  70. return redirect('waybill/index')->with('successTip','新运单“'.$waybill->waybill_number.'”录入成功');
  71. }
  72. public function edit($id,CarrierService $carrierService,CarTypeService $carTypeService,
  73. CityService $cityService,UnitService $unitService)
  74. {
  75. if(!Gate::allows('运输管理-编辑')){ return redirect(url('/')); }
  76. $waybill = app('waybillService')->find($id);
  77. if ($waybill->deliver_at){
  78. $waybill->deliver_at_date=Carbon::parse($waybill->deliver_at)->format('Y-m-d');
  79. $waybill->deliver_at_time=Carbon::parse($waybill->deliver_at)->format('H:i:s');
  80. }
  81. $cities=$cityService->getSelection();
  82. $units=$unitService->getSelection();
  83. $carTypes=$carTypeService->getSelection();
  84. return view('waybill/edit',['waybill'=>$waybill,'carriers'=>$carrierService->getSelection(),'cities'=>$cities,'units'=>$units,'carTypes'=>$carTypes]);
  85. }
  86. public function update(Request $request, $id,WaybillPriceModelService $waybillPriceModelService,
  87. CarrierService $carrierService,WaybillPayoffService $waybillPayoffService)
  88. {
  89. if(!Gate::allows('运输管理-调度')){ return redirect(url('/')); }
  90. if (!$request->warehouse_weight && $request->warehouse_weight_unit_id){
  91. $request->offsetUnset('warehouse_weight_unit_id');
  92. }
  93. if (!$request->warehouse_weight_other && $request->warehouse_weight_unit_id_other){
  94. $request->offsetUnset('warehouse_weight_unit_id_other');
  95. }
  96. if (!$request->carrier_weight && $request->carrier_weight_unit_id){
  97. $request->offsetUnset('carrier_weight_unit_id');
  98. }
  99. if (!$request->carrier_weight_other && $request->carrier_weight_unit_id_other){
  100. $request->offsetUnset('carrier_weight_unit_id_other');
  101. }
  102. $this->validatorWaybillDispatch($request,$id)->validate();
  103. $waybillPayoffParam = [];
  104. $waybillPayoffParam['total_receivable']=0;
  105. $waybill = app('waybillService')->update($request,$id);
  106. if ($waybill->type=="直发车"){
  107. if ($waybill->charge)$waybillPayoffParam['total_receivable'] = ($waybill->charge);
  108. elseif ($waybill->collect_fee)$waybillPayoffParam['total_receivable'] = ($waybill->collect_fee);
  109. $waybillPayoffParam['total_expense'] = ($waybill->fee)+($waybill->other_fee)-($waybill->collect_fee);
  110. }else {
  111. $waybillPriceModel_id=$request->input('waybillPriceModel');
  112. if ($waybillPriceModel_id){
  113. $carrier_weight=$request->input('carrier_weight');
  114. $waybillPriceModel=$waybillPriceModelService->find($waybillPriceModel_id);
  115. $carrier=$carrierService->find($waybill->carrier_id);
  116. if ($carrier_weight<$waybillPriceModel->initial_weight){
  117. $fee=(($waybillPriceModel->unit_price)*($waybillPriceModel->initial_weight))+$carrier->delivery_fee;
  118. }else{
  119. $fee=(($waybillPriceModel->unit_price)*$carrier_weight)+$carrier->delivery_fee;
  120. }
  121. if ($waybillPriceModel->base_fee&&$fee<$waybillPriceModel->base_fee){
  122. $fee=$waybillPriceModel->base_fee;
  123. }
  124. $waybill->fee=$fee;
  125. $waybill->waybill_price_model_id=$waybillPriceModel_id;
  126. }
  127. $waybill->save();
  128. if ($waybill->charge)$waybillPayoffParam['total_receivable'] = ($waybill->charge);
  129. elseif($waybill->collect_fee) {
  130. $waybillPayoffParam['total_receivable'] = $waybill->collect_fee;
  131. }
  132. $waybillPayoffParam['total_expense'] = ($waybill->pick_up_fee)+($waybill->other_fee)+($waybill->fee);
  133. }
  134. if ($waybillPayoffParam['total_receivable'] > 0){
  135. $waybillPayoffParam['waybill_id'] = $id;
  136. $waybillPayoffParam['gross_margin'] = $waybillPayoffParam['total_receivable'] - $waybillPayoffParam['total_expense'];
  137. $waybillPayoffParam['gross_profit_rate'] = $waybillPayoffParam['gross_margin']/$waybillPayoffParam['total_receivable'];
  138. $waybillPayoffService->updateOrCreate($waybillPayoffParam);
  139. }
  140. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  141. return redirect('waybill/index')->with('successTip','运单“'.$waybill->waybill_number.'”调度成功');
  142. }
  143. public function checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id){
  144. //确保承运商计数与计数单位为一个数组且长度2
  145. if(!$carrier_id)return false;
  146. if(!$destination_city_id)return false;
  147. if(!$carrier_weight)return false;
  148. if(!$carrier_weight_unit_id)return false;
  149. //多个计数标准,计算价格,取最贵
  150. if ($carrier_weight[0]&&$carrier_weight[1]&&$carrier_weight_unit_id[0]&&$carrier_weight_unit_id[1]){
  151. //城市价格区间不为空
  152. $waybillPriceModelOne=WaybillPriceModel::where('carrier_id',$carrier_id)->where('city_id',$destination_city_id)
  153. ->where('range_min','<',$carrier_weight[0])->where('range_max','>=',$carrier_weight[0])
  154. ->where('unit_id',$carrier_weight_unit_id[0])->first();
  155. $waybillPriceModelTwo=WaybillPriceModel::where('carrier_id',$carrier_id)->where('city_id',$destination_city_id)
  156. ->where('range_min','<',$carrier_weight[1])->where('range_max','>=',$carrier_weight[1])
  157. ->where('unit_id',$carrier_weight_unit_id[1])->first();
  158. if ($waybillPriceModelOne&&$waybillPriceModelTwo){
  159. if ($waybillPriceModelOne->unit_price*$carrier_weight[0]>=$waybillPriceModelTwo->unit_price*$carrier_weight[1]){
  160. return $waybillPriceModelOne->id;
  161. }else{
  162. return $waybillPriceModelTwo->id;
  163. }
  164. }
  165. if ($waybillPriceModelOne)return $waybillPriceModelOne->id;
  166. if ($waybillPriceModelTwo)return $waybillPriceModelTwo->id;
  167. //价格区间为空
  168. $waybillPriceModelRangeOne=WaybillPriceModel::whereRaw('carrier_id = ? AND city_id = ? AND unit_id = ? AND range_max IS NULL',[$carrier_id,$destination_city_id,$carrier_weight_unit_id[0]])->first();
  169. $waybillPriceModelRangeTwo=WaybillPriceModel::whereRaw('carrier_id = ? AND city_id = ? AND unit_id = ? AND range_max IS NULL',[$carrier_id,$destination_city_id,$carrier_weight_unit_id[1]])->first();
  170. if ($waybillPriceModelRangeOne&&$waybillPriceModelRangeTwo){
  171. if ($waybillPriceModelRangeOne->unit_price*$carrier_weight[0]>=$waybillPriceModelRangeTwo->unit_price*$carrier_weight[1]){
  172. return $waybillPriceModelRangeOne->id;
  173. }else{
  174. return $waybillPriceModelRangeTwo->id;
  175. }
  176. }
  177. if ($waybillPriceModelRangeOne)return $waybillPriceModelRangeOne->id;
  178. if ($waybillPriceModelRangeTwo)return $waybillPriceModelRangeTwo->id;
  179. //城市为空
  180. $city=City::where('id',$destination_city_id)->select('province_id')->first();
  181. $waybillPriceModelProvinceOne=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max >= ? AND range_min < ? AND city_id IS NULL',
  182. [$carrier_id,$city->province_id,$carrier_weight_unit_id[0],$carrier_weight[0],$carrier_weight[0]])->first();
  183. $waybillPriceModelProvinceTwo=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max >= ? AND range_min < ? AND city_id IS NULL',
  184. [$carrier_id,$city->province_id,$carrier_weight_unit_id[1],$carrier_weight[1],$carrier_weight[1]])->first();
  185. if ($waybillPriceModelProvinceOne&&$waybillPriceModelProvinceTwo){
  186. if ($waybillPriceModelProvinceOne->unit_price*$carrier_weight[0]>=$waybillPriceModelProvinceTwo->unit_price*$carrier_weight[1]){
  187. return $waybillPriceModelProvinceOne->id;
  188. }else{
  189. return $waybillPriceModelProvinceTwo->id;
  190. }
  191. }
  192. if ($waybillPriceModelProvinceOne)return $waybillPriceModelProvinceOne->id;
  193. if ($waybillPriceModelProvinceTwo)return $waybillPriceModelProvinceTwo->id;
  194. //城市价格区间都为空
  195. $waybillPriceModelProvinceRangeOne=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max IS NULL AND city_id IS NULL',
  196. [$carrier_id,$city->province_id,$carrier_weight_unit_id[0]])->first();
  197. $waybillPriceModelProvinceRangeTwo=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max IS NULL AND city_id IS NULL',
  198. [$carrier_id,$city->province_id,$carrier_weight_unit_id[1]])->first();
  199. if ($waybillPriceModelProvinceRangeOne&&$waybillPriceModelProvinceRangeTwo){
  200. if ($waybillPriceModelProvinceRangeOne->unit_price*$carrier_weight[0]>=$waybillPriceModelProvinceRangeTwo->unit_price*$carrier_weight[1]){
  201. return $waybillPriceModelProvinceRangeOne->id;
  202. }else{
  203. return $waybillPriceModelProvinceRangeOne->id;
  204. }
  205. }
  206. if ($waybillPriceModelProvinceRangeOne)return $waybillPriceModelProvinceRangeOne->id;
  207. if ($waybillPriceModelProvinceRangeTwo)return $waybillPriceModelProvinceRangeTwo->id;
  208. };
  209. for ($i=0;$i<count($carrier_weight);$i++){
  210. if ($carrier_weight[$i]&&$carrier_weight_unit_id[$i]){
  211. //城市价格区间不为空
  212. $waybillPriceModel=WaybillPriceModel::where('carrier_id',$carrier_id)->where('city_id',$destination_city_id)
  213. ->where('range_min','<',$carrier_weight[$i])->where('range_max','>=',$carrier_weight[$i])
  214. ->where('unit_id',$carrier_weight_unit_id[$i])->first();
  215. if($waybillPriceModel)return $waybillPriceModel->id;
  216. //价格区间为空
  217. $waybillPriceModelRange=WaybillPriceModel::whereRaw('carrier_id = ? AND city_id = ? AND unit_id = ? AND range_max IS NULL',[$carrier_id,$destination_city_id,$carrier_weight_unit_id[$i]])->first();
  218. if ($waybillPriceModelRange){ return $waybillPriceModelRange->id;}
  219. //城市为空
  220. $city=City::where('id',$destination_city_id)->select('province_id')->first();
  221. $waybillPriceModelProvince=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max >= ? AND range_min < ? AND city_id IS NULL',
  222. [$carrier_id,$city->province_id,$carrier_weight_unit_id[$i],$carrier_weight[$i],$carrier_weight[$i]])->first();
  223. if ($waybillPriceModelProvince){return $waybillPriceModelProvince->id;}
  224. //城市价格区间都为空
  225. $waybillPriceModelProvinceRange=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND unit_id = ? AND range_max IS NULL AND city_id IS NULL',
  226. [$carrier_id,$city->province_id,$carrier_weight_unit_id[$i]])->first();
  227. if ($waybillPriceModelProvinceRange){return $waybillPriceModelProvinceRange->id;}
  228. }
  229. }
  230. return false;
  231. }
  232. /*三层条件:无优先级,找到第一个直接返回
  233. * 无论是否为KG||T,计数单位一为KG,计数单位一为T,计数单位二为KG,计数单位二为T
  234. * 计数一与计数二同时存在取最贵价格:
  235. * 计数一存在,二不存在:
  236. * 计数二存在,一不存在:
  237. * 城市价格区间不为空,城市价格区间都为空,城市为空,价格区间为空
  238. * */
  239. public function isWaybillPriceModel(Request $request){
  240. $carrier_id=$request->input('carrier_id');
  241. $destination_city_id=$request->input('destination_city_id');
  242. $carrier_weight=$request->input('carrier_weight');
  243. $carrier_weight_unit_id=$request->input('carrier_weight_unit_id');
  244. $validatorData=["carrier_id"=>$carrier_id,"destination_city_id"=>$destination_city_id,
  245. 'carrier_weight'=>$carrier_weight[0],"carrier_weight_unit_id"=>$carrier_weight_unit_id[0],
  246. "carrier_weight_other"=>$carrier_weight[1],"carrier_weight_unit_id_other"=>$carrier_weight_unit_id[1]];
  247. $errors=Validator::make($validatorData,[
  248. 'carrier_id'=>'required|integer',
  249. 'destination_city_id'=>'required|integer',
  250. 'carrier_weight'=>'nullable|min:0|numeric|max:999999',
  251. 'carrier_weight_unit_id'=>'required_with:carrier_weight',
  252. 'carrier_weight_other'=>'nullable|min:0|numeric|max:999999',
  253. 'carrier_weight_unit_id_other'=>'required_with:carrier_weight_other',
  254. ],[
  255. 'required'=>':attribute 为必填项',
  256. 'max'=>':attribute 字符过多或输入值过大',
  257. 'min'=>':attribute 不得为负',
  258. 'numeric'=>':attribute 应为数字',
  259. 'unique'=>':attribute 已存在',
  260. 'required_with'=>':attribute 未填',
  261. 'integer'=>':attribute 必须为数字',
  262. ],[
  263. 'carrier_weight'=>'承运商计数(抛)',
  264. 'carrier_id'=>'承运商',
  265. 'destination_city_id'=>'目的市',
  266. 'carrier_weight_unit_id'=>'承运商计数单位',
  267. 'carrier_weight_other'=>'承运商计数二',
  268. 'carrier_weight_unit_id_other'=>'承运商计数单位二',
  269. ])->errors();
  270. if (count($errors)>0)return ['error'=>$errors];
  271. $result=$this->checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
  272. if (!$result){
  273. //单位为kg,T时
  274. $unitKG=Unit::where('name','kg')->first();
  275. $unitT=Unit::where('name','T')->first();
  276. if ($carrier_weight_unit_id[0]==$unitKG->id){
  277. $carrier_weight_unit_id[0]=$unitT->id;
  278. $carrier_weight[0]=$carrier_weight[0]/1000;
  279. $result=$this->checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
  280. if ($result)return ['success'=>$result];
  281. }
  282. if ($carrier_weight_unit_id[1]==$unitKG->id){
  283. $carrier_weight_unit_id[1]=$unitT->id;
  284. $carrier_weight[1]=$carrier_weight[1]/1000;
  285. $result=$this->checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
  286. if ($result)return ['success'=>$result];
  287. }
  288. if ($carrier_weight_unit_id[0]==$unitT->id){
  289. $carrier_weight_unit_id[0]=$unitKG->id;
  290. $carrier_weight[0]=$carrier_weight[0]*1000;
  291. $result=$this->checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
  292. if ($result)return ['success'=>$result];
  293. }
  294. if ($carrier_weight_unit_id[1]==$unitT->id){
  295. $carrier_weight_unit_id[1]=$unitKG->id;
  296. $carrier_weight[1]=$carrier_weight[1]*1000;
  297. $result=$this->checkWaybillPriceModel($carrier_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
  298. if ($result)return ['success'=>$result];
  299. }
  300. }
  301. return ['success'=>$result];
  302. }
  303. public function waybillUpdate(Request $request, $id){
  304. if(!Gate::allows('运输管理-编辑')){ return redirect(url('/')); }
  305. $this->validatorWaybill($request,$id)->validate();
  306. $data=$request->input();
  307. $waybill=Waybill::find($id);
  308. $waybill->fill($data);
  309. if ($waybill->save()){
  310. $this->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
  311. return redirect('waybill/index')->with('successTip','运单“'.$waybill->waybill_number.'”修改成功');
  312. }
  313. }
  314. public function waybillAudit(Request $request){
  315. if(!Gate::allows('运输管理-运单审核')){ return redirect(url('/')); }
  316. $id=$request->input('id');
  317. $waybill=Waybill::find($id);
  318. $isAudit=WaybillAuditLog::whereRaw('waybill_id = ? and audit_stage = ?',[$id,"运单阶段"])->first();
  319. if (empty($isAudit)){
  320. $waybillAuditLog=new WaybillAuditLog([
  321. 'waybill_id'=>$id,
  322. 'audit_stage'=>'运单阶段',
  323. 'user_id'=>Auth::id(),
  324. ]);
  325. $waybillAuditLog->save();
  326. $waybillAuditLog['user']=Auth::user();
  327. $waybill->status='已审核';
  328. $result=$waybill->save();
  329. $this->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
  330. return ['success'=>$result,'status'=>$waybill->status,'waybillAuditLog'=>$waybillAuditLog];
  331. }
  332. return ['exception'=>'请勿重复审核!'];
  333. }
  334. public function waybillEdit($id){
  335. if(!Gate::allows('运输管理-编辑')){ return redirect(url('/')); }
  336. $waybill=Waybill::find($id);
  337. $owners=Owner::get();
  338. return view('waybill.waybillEdit',['waybill'=>$waybill,'owners'=>$owners]);
  339. }
  340. public function waybillRetreatAudit(Request $request){
  341. if(!Gate::allows('运输管理-调度')){ return redirect(url('/')); }
  342. $id=$request->input('id');
  343. $waybill=Waybill::find($id);
  344. WaybillAuditLog::whereRaw('waybill_id = ? and audit_stage = ?',[$id,"运单阶段"])->delete();
  345. $waybill->status='待重审';
  346. $result=$waybill->save();
  347. $this->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
  348. return ['success'=>$result,'status'=>$waybill->status];
  349. }
  350. public function waybillEndAudit(Request $request){
  351. if(!Gate::allows('运输管理-调度审核')){ return redirect(url('/')); }
  352. $id=$request->input('id');
  353. $waybill=Waybill::query()->with(["owner","carrier","origination_city","destination_city","carType",'priceModel',"amountUnit",
  354. "warehouse_weight_unit","carrier_weight_unit","warehouse_weight_unit_other","carrier_weight_unit_other"])->find($id);
  355. if (!$waybill->charge&&!$waybill->collect_fee)return ['exception'=>'收费或到付费用未填!'];
  356. if ($waybill->charge==0&&$waybill->collect_fee==0)return ['exception'=>'收费与到付费用都为0!'];
  357. if ($waybill->type=='专线'){
  358. if (!$waybill->carrier_weight_other||$waybill->carrier_weight_other==0)return ['exception'=>'承运商计重未填或为0!'];
  359. if (!$waybill->carrier_weight_unit_id_other)return ['exception'=>'承运商计重单位未选!'];
  360. }
  361. $isAudit=WaybillAuditLog::whereRaw('waybill_id = ? and audit_stage = ?',[$id,"调度阶段"])->first();
  362. if (empty($isAudit)){
  363. $waybillAuditLog=new WaybillAuditLog([
  364. 'waybill_id'=>$id,
  365. 'audit_stage'=>'调度阶段',
  366. 'user_id'=>Auth::id(),
  367. ]);
  368. $waybillAuditLog->save();
  369. $waybillAuditLog['user']=Auth::user();
  370. if ($waybill->waybill_price_model_id||$waybill->type=='直发车'){
  371. $waybill->status='已完结';
  372. $result=$waybill->save();
  373. $waybillPayoff=WaybillPayoff::query()->where('waybill_id','=',$id)->first();
  374. $waybillPayoffJson=json_encode($this->createReportData($waybill,$waybillPayoff),JSON_UNESCAPED_UNICODE);
  375. WaybillFinancialSnapshot::query()->create([
  376. 'waybill_id'=>$id,
  377. 'json_content'=>$waybillPayoffJson,
  378. ]);
  379. }else{
  380. $waybill->status='无模型';
  381. $result=$waybill->save();
  382. $waybillPayoff=WaybillPayoff::query()->where('waybill_id','=',$id)->first();
  383. if ($waybillPayoff){
  384. $waybillPayoffJson=json_encode($this->createReportData($waybill,$waybillPayoff),JSON_UNESCAPED_UNICODE);
  385. WaybillFinancialExcepted::query()->create([
  386. 'waybill_id'=>$id,
  387. 'json_content'=>$waybillPayoffJson,
  388. ]);
  389. }
  390. }
  391. $this->log(__METHOD__,__FUNCTION__,$waybillPayoffJson,Auth::id());
  392. return ['success'=>$result,'status'=>$waybill->status,'waybillAuditLog'=>$waybillAuditLog];
  393. }
  394. return ['exception'=>'请勿重复审核!'];
  395. }
  396. //生成报表数据
  397. private function createReportData($waybill,$waybillPayoff){
  398. return [
  399. "type"=>$waybill->type,
  400. "waybill_number"=>$waybill->waybill_number,
  401. "owner_name"=>$waybill->owner ? $waybill->owner->name : null,
  402. "wms_bill_number"=>$waybill->wms_bill_number,
  403. "source_bill"=>$waybill->source_bill,
  404. "origination"=>$waybill->origination,
  405. "destination"=>$waybill->destination,
  406. "recipient"=>$waybill->recipient,
  407. "recipient_mobile"=>$waybill->recipient_mobile,
  408. "charge"=>$waybill->charge,
  409. "collect_fee"=>$waybill->collect_fee,
  410. "ordering_remark"=>$waybill->ordering_remark,
  411. "carrier_name"=>$waybill->carrier ? $waybill->carrier->name : null,
  412. "carrier_bill"=>$waybill->carrier_bill,
  413. "origination_city_name"=>$waybill->origination_city ? $waybill->origination_city->name : null,
  414. "destination_city_name"=>$waybill->destination_city ? $waybill->destination_city->name : null,
  415. "warehouse_weight"=>$waybill->warehouse_weight.($waybill->warehouse_weight_unit ? $waybill->warehouse_weight_unit->name : ''),
  416. "carrier_weight"=>$waybill->carrier_weight.($waybill->carrier_weight_unit ? $waybill->carrier_weight_unit->name : ''),
  417. "warehouse_weight_other"=>$waybill->warehouse_weight_other.($waybill->warehouse_weight_unit_other ? $waybill->warehouse_weight_unit_other->name : ''),
  418. "carrier_weight_other"=>$waybill->carrier_weight_other.($waybill->carrier_weight_unit_other ? $waybill->carrier_weight_unit_other->name : ''),
  419. "car_type_name"=>$waybill->carType ? $waybill->carType->name : null,
  420. "fee"=>$waybill->fee,
  421. "pick_up_fee"=>$waybill->pick_up_fee,
  422. "other_fee"=>$waybill->other_fee,
  423. "dispatch_remark"=>$waybill->dispatch_remark,
  424. "price_model_range_min"=>$waybill->priceModel ? $waybill->priceModel->range_min : null,
  425. "price_model_range_max"=>$waybill->priceModel ? $waybill->priceModel->range_max : null,
  426. "price_model_unit_price"=>$waybill->priceModel ? $waybill->priceModel->unit_price : null,
  427. "price_model_base_fee"=>$waybill->priceModel ? $waybill->priceModel->base_fee : null,
  428. "price_model_initial_weight"=>$waybill->priceModel ? $waybill->priceModel->initial_weight : null,
  429. "car_owner_info"=>$waybill->car_owner_info,
  430. "status"=>$waybill->status,
  431. "mileage"=>$waybill->mileage,
  432. 'amount'=>$waybill->amount.($waybill->amountUnit ? $waybill->amountUnit->name : ''),
  433. "inquire_tel"=>$waybill->inquire_tel,
  434. "other_charge"=>$waybill->other_charge,
  435. "other_charge_remark"=>$waybill->other_charge_remark,
  436. "deliver_at"=>$waybill->deliver_at,
  437. "created_at"=>$waybill->created_at,
  438. "auditLog_user_name"=>Auth::user()['name'],
  439. "total_expense"=>$waybillPayoff->total_expense,
  440. "total_receivable"=>$waybillPayoff->total_receivable,
  441. "gross_margin"=>$waybillPayoff->gross_margin,
  442. "gross_profit_rate"=>$waybillPayoff->gross_profit_rate,
  443. ];
  444. }
  445. public function upload(Request $request){
  446. if(!Gate::allows('运输管理-图片上传')){ return '没有权限'; }
  447. $file=$request->file('file');
  448. $waybill_number=$request->input('waybill_number');
  449. $waybill=Waybill::where('waybill_number',$waybill_number)->first();
  450. if (!$waybill){
  451. return ['success'=>false,'error'=>"未找到该运单!"];
  452. }
  453. if ($waybill->upload_file_url){
  454. return ['success'=>false,'error'=>"该运单已存在照片!"];
  455. }
  456. if (!$file){
  457. return ['success'=>false,'error'=>"照片不得为空!"];
  458. }
  459. if (!$file->isValid()){
  460. return ['success'=>false,'error'=>"找不到照片!"];
  461. }
  462. $tmpFile = $file->getRealPath();
  463. if (! is_uploaded_file($tmpFile)) {
  464. return ['success'=>false,'error'=>"文件错误!"];
  465. }
  466. $fileExtension=$file->getClientOriginalExtension();
  467. // 5.存储, 生成一个随机文件名
  468. $fileName = date('ymd').'-'.Uuid::uuid1();//thumbnail common bulky
  469. $thumbnailName=storage_path('app/public/files/'.$fileName.'-thumbnail.'.$fileExtension);
  470. $commonName=storage_path('app/public/files/'.$fileName.'-common.'.$fileExtension);
  471. $bulkyName=storage_path('app/public/files/'.$fileName.'-bulky.'.$fileExtension);
  472. $result=move_uploaded_file ($tmpFile ,$bulkyName);
  473. if ($result){
  474. $img=Image::make($bulkyName);
  475. if ($img->height() > $img->width())
  476. $img->heighten(250)->save($commonName);
  477. else $img->widen(250)->save($commonName);
  478. $img->heighten(28)->save($thumbnailName);
  479. $uploadFile=new UploadFile([
  480. "table_name"=>"waybills",
  481. "table_id"=>$waybill->id,
  482. "url"=>'/files/'.$fileName,
  483. "type"=>$fileExtension,
  484. ]);
  485. if ($uploadFile->save())
  486. $this->log(__METHOD__,'图片上传',json_encode($request),Auth::user()['id']);
  487. $uploadFile->url=asset('/storage'.$uploadFile->url);
  488. return ['success'=>true,'data'=>$uploadFile];
  489. }
  490. return ['success'=>false,'error'=>"图片保存失败!"];
  491. }
  492. //删除照片
  493. public function deleteImg(Request $request){
  494. if(!Gate::allows('运输管理-图片删除')){ return '没有权限'; }
  495. $ids=$request->input('ids');
  496. $uploadFiles=UploadFile::where('table_name','waybills')->whereIn('table_id',$ids)->get();
  497. foreach ($uploadFiles as $uploadFile){
  498. $bulky=storage_path('app/public/'.$uploadFile->url.'-bulky.'.$uploadFile->type);
  499. $common=storage_path('app/public/'.$uploadFile->url.'-common.'.$uploadFile->type);
  500. $thumbnail=storage_path('app/public/'.$uploadFile->url.'-thumbnail.'.$uploadFile->type);
  501. if (file_exists($bulky) && file_exists($common) && file_exists($thumbnail)){
  502. unlink($bulky);unlink($common);unlink($thumbnail);
  503. }
  504. }
  505. UploadFile::where('table_name','waybills')->whereIn('table_id',$ids)->delete();
  506. $this->log(__METHOD__,'图片删除',json_encode($request),Auth::user()['id']);
  507. return ['success'=>true];
  508. }
  509. public function export(Request $request){
  510. if(!Gate::allows('运输管理-查询')){ return '没有权限'; }
  511. if ($request->checkAllSign){
  512. $param = $request->input();
  513. unset($param['checkAllSign']);
  514. $sql = app('waybillService')->getSql($param);
  515. }else $sql = app('waybillService')->getSql(['id'=>$request->data]);
  516. return response(Http::post(config('go.export.url'),['type'=>'waybill','sql'=>$sql]),200, [
  517. "Content-type"=>"application/octet-stream",
  518. "Content-Disposition"=>"attachment; filename=运单列表-".date('ymdHis').'.xlsx',
  519. ]);
  520. }
  521. public function deliveringExport(Request $request){
  522. if ($request->checkAllSign){
  523. $param = $request->input();
  524. unset($param['checkAllSign']);
  525. $sql = app('waybillService')->getDeliveringSql($param);
  526. }else{
  527. $sql = app('waybillService')->getDeliveringSql(['id'=>$request->data]);
  528. }
  529. return response(Http::post(config('go.export.url'),['type'=>'waybillDelivering','sql'=>$sql]),200, [
  530. "Content-type"=>"application/octet-stream",
  531. "Content-Disposition"=>"attachment; filename=发运列表-".date('ymdHis').'.xlsx',
  532. ]);
  533. }
  534. //发运
  535. public function delivering(Request $request){
  536. if (!Auth::user())return view('exception.login');
  537. $waybills= app('waybillService')->paginate($request->input());
  538. if (!Auth::user()->isSuperAdmin()){
  539. $carriersUsers=DB::table('carrier_user')->where('user_id',Auth::id())->get();
  540. $carrierIds=array_column($carriersUsers->toArray(),'carrier_id');
  541. $waybills=$waybills->whereIn("carrier_id",$carrierIds);
  542. }
  543. return view('waybill.delivering',compact('waybills'));
  544. }
  545. //承运商提交
  546. public function storeCarrierBill(Request $request){
  547. $errors=Validator::make($request->input(),[
  548. 'id'=>'required|integer',
  549. 'carrier_bill'=>'required',
  550. 'inquire_tel'=>'nullable',
  551. 'amount'=>'nullable|integer',
  552. 'carrier_weight'=>'required_without:carrier_weight_other|nullable|numeric',
  553. 'carrier_weight_other'=>'required_without:carrier_weight|nullable|numeric',
  554. ],[
  555. 'required'=>':attribute 为必填项',
  556. 'integer'=>':attribute 应为整数',
  557. 'numeric'=>':attribute 应为数字',
  558. 'required_with'=>':attribute 重量与体积至少存在一项',
  559. ],[
  560. 'carrier_bill'=>'专线运单号',
  561. 'inquire_tel'=>'查件电话',
  562. 'amount'=>'件数',
  563. 'carrier_weight'=>'体积',
  564. 'carrier_weight_other'=>'重量',
  565. ])->errors();
  566. if (count($errors)>0)return ["errors"=>$errors];
  567. $waybill=Waybill::find($request->input('id'));
  568. if (!$waybill)return ["error"=>"未找到该运单!"];
  569. $waybill->fill($request->input());
  570. $waybill->update();
  571. return $waybill;
  572. }
  573. protected function validatorWaybill(Request $request,$id){
  574. if ($id){$wms_bill_number=$id;};
  575. $validator=Validator::make($request->input(),[
  576. 'owner_id'=>'required',
  577. 'wms_bill_number'=>['nullable','max:50',isset($wms_bill_number)?"unique:waybills,wms_bill_number,$wms_bill_number":'unique:waybills,wms_bill_number'],
  578. 'origination'=>'required|max:255',
  579. 'destination'=>'required|max:255',
  580. 'recipient'=>'required|max:50',
  581. 'recipient_mobile'=>['required','regex:/^(\d{7,11})|(1[3|4|5|7|8][0-9]\d{4,8})$/'],
  582. 'charge'=>'nullable|min:0|max:999999|numeric',
  583. 'collect_fee'=>'nullable|min:0|numeric',
  584. ],[
  585. 'required'=>':attribute 为必填项',
  586. 'alpha_num'=>':attribute 应为字母或数字',
  587. 'max'=>':attribute 字符过多或输入值过大',
  588. 'regex'=>':attribute 输入有误',
  589. 'integer'=>':attribute 应为整数',
  590. 'min'=>':attribute 不得为负',
  591. 'numeric'=>':attribute 应为数字',
  592. 'unique'=>':attribute 已存在',
  593. ],[
  594. 'owner_id'=>'货主',
  595. 'wms_bill_number'=>'WMS单号',
  596. 'origination'=>'始发地',
  597. 'destination'=>'目的地',
  598. 'recipient'=>'收件人',
  599. 'recipient_mobile'=>'收件人电话',
  600. 'charge'=>'收费',
  601. 'collect_fee'=>'到付金额',
  602. ]);
  603. return $validator;
  604. }
  605. protected function validatorWaybillDispatch(Request $request,$id){
  606. $rule=[
  607. 'carrier_id'=>'required|integer',
  608. 'carrier_bill'=>"sometimes|nullable|max:50|unique:waybills,carrier_bill,$id",
  609. 'fee'=>'sometimes|nullable|min:0|numeric|max:999999',
  610. 'other_fee'=>'sometimes|nullable|min:0|numeric|max:999999',
  611. 'charge'=>'sometimes|nullable|min:0|numeric|max:999999',
  612. 'mileage'=>'nullable|numeric|min:0',
  613. 'amount'=>'nullable|numeric|min:0',
  614. 'amount_unit_id'=>'required',
  615. 'origination_city_id'=>'sometimes|required|integer',
  616. 'destination_city_id'=>'sometimes|required|integer',
  617. 'warehouse_weight_other'=>'sometimes|nullable|min:0|numeric|max:999999',
  618. 'warehouse_weight_unit_id_other'=>'sometimes|required_with:warehouse_weight_other|nullable|integer',
  619. 'pick_up_fee'=>'sometimes|nullable|min:0|numeric|max:999999',
  620. 'warehouse_weight'=>'sometimes|nullable|min:0|numeric|max:999999',
  621. 'warehouse_weight_unit_id'=>'sometimes|required_with:warehouse_weight|nullable|integer',
  622. 'carrier_weight'=>'sometimes|nullable|min:0|numeric|max:999999',
  623. 'carrier_weight_unit_id'=>'sometimes|required_with:carrier_weight',
  624. 'carrier_weight_other'=>'sometimes|nullable|min:0|numeric|max:999999',
  625. 'carrier_weight_unit_id_other'=>'sometimes|required_with:carrier_weight_other',
  626. ];
  627. if ($request->type == '专线'){
  628. $rule['origination_city_id']='required|integer';
  629. $rule['destination_city_id']='required|integer';
  630. }
  631. $validator=Validator::make($request->input(),$rule,[
  632. 'required'=>':attribute 为必填项',
  633. 'alpha_num'=>':attribute 应为字母或数字',
  634. 'max'=>':attribute 字符过多或输入值过大',
  635. 'min'=>':attribute 不得为负',
  636. 'numeric'=>':attribute 应为数字',
  637. 'unique'=>':attribute 已存在',
  638. 'required_with'=>':attribute 未填',
  639. 'integer'=>':attribute 必须为数字',
  640. ],[
  641. 'carrier_id'=>'承运商',
  642. 'carrier_bill'=>'承运商单号',
  643. 'fee'=>'运费',
  644. 'other_fee'=>'其他费用',
  645. 'charge'=>'收费',
  646. 'mileage'=>'里程数',
  647. 'amount'=>'计数',
  648. 'amount_unit_id'=>'计数单位',
  649. 'warehouse_weight'=>'仓库计数(抛)',
  650. 'carrier_weight'=>'承运商计数(抛)',
  651. 'pick_up_fee'=>'提货费',
  652. 'destination_city_id'=>'目的市',
  653. 'carrier_weight_unit_id'=>'承运商计数单位',
  654. 'warehouse_weight_unit_id'=>'仓库计数单位',
  655. 'warehouse_weight_other'=>'仓库计数二',
  656. 'carrier_weight_other'=>'承运商计数二',
  657. 'warehouse_weight_unit_id_other'=>'仓库技数单位二',
  658. 'carrier_weight_unit_id_other'=>'承运商计数单位二',
  659. ]);
  660. return $validator;
  661. }
  662. public function addCounty(Request $request){
  663. if ($request->destination_city =='undefined')$request->offsetUnset('destination_city');
  664. $rule =[
  665. 'destination_city' => ['required', 'string','unique:cities,name'],
  666. ];
  667. $messages=[
  668. 'destination_city.required'=>'市/县不能为空',
  669. 'destination_city.string'=>'市/县必须是字符串',
  670. 'destination_city.unique'=>'市/县已存在',
  671. ];
  672. $validator = Validator::make($request->all(),$rule,$messages);
  673. if ($validator->fails()) {
  674. return $validator->errors();
  675. }
  676. $city=new City([
  677. 'name'=>$request->input('destination_city'),
  678. 'type'=>3,
  679. ]);
  680. $city->save();
  681. return $city;
  682. }
  683. // 运单删除 软删除
  684. public function destroy(int $id){
  685. if(!GAte::allows('运输管理-删除')){return['success'=>0,'status'=>'没有权限'];}
  686. if(is_null($id)){return ['success'=>'0','status'=>'传入id为空'];}
  687. $result = Waybill::where('id',$id)->delete();
  688. return ['success'=>$result,'status'=>$result];
  689. }
  690. // 回收站
  691. public function recycle(Request $request){
  692. if(!Gate::allows('运输管理-删除')){return redirect('/');}
  693. $user = Auth::user();
  694. $paginate = $request->input('paginate')??50;
  695. $waybills = Waybill::with(['owner','waybillAuditLogs' => function ($query) {
  696. return $query->with('user');
  697. }])->orderBy('deleted_at', 'DESC')->withTrashed()->whereNotNull('deleted_at')->paginate(50);
  698. $total = $waybills->count();
  699. $paginateParams = [];
  700. $paginateParams['paginate'] = $paginate;
  701. return view('waybill.recycle',compact('waybills','total','paginateParams'));
  702. }
  703. // 软删除恢复
  704. public function apiRestoreSelected(Request $request){
  705. if(!Gate::allows('运输管理-删除')){return ['success'=>'false','fail_info'=>'没有权限'];}
  706. $ids = $request->input('ids')??'';
  707. if($ids == ''){return ['success'=>'false','fail_info'=>'没有可恢复对象'];}
  708. $waybills = Waybill::withTrashed()->whereIn('id',$ids)->get();
  709. $result = '';
  710. $waybills->each(function (Waybill $waybill){
  711. $waybill->restore();
  712. });
  713. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  714. return ['success'=>'true','waybills'=>$waybills];
  715. }
  716. // 修改运费
  717. public function changeFee(Request $request){
  718. if(!Gate::allows('运输管理-运费')){return ['success'=>'false','fail_info'=>'没有权限'];}
  719. $wayBillId = $request->input('id');
  720. $waybillFee = $request->input('fee');
  721. if(is_null($wayBillId) or is_null($waybillFee)){
  722. return ['success'=>'false','fail_info'=>'参数异常'];
  723. }
  724. $result = Waybill::where('id',$wayBillId)->update(['fee'=>$waybillFee]);
  725. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  726. return ['success'=>$result,'status'=>$result];
  727. }
  728. // 修改运输收费
  729. public function changeCharge(Request $request){
  730. if(!Gate::allows('运输管理-运单编辑')){return ['success'=>'false','fail_info'=>'没有权限'];}
  731. $wayBillId = $request->id;
  732. $waybillCharge = $request->input('charge');
  733. if(is_null($wayBillId) or is_null($waybillCharge)){
  734. return ['success'=>'false','fail_info'=>'参数异常'];
  735. }
  736. $result = Waybill::where('id',$wayBillId)->update(['charge'=>$waybillCharge]);
  737. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  738. return ['success'=>$result,'status'=>$result];
  739. }
  740. // 置顶
  741. public function waybillOnTop(Request $request){
  742. $id = $request->input('id');
  743. $detail = $request->input('detail');
  744. if(!Gate::allows('运输管理-置顶')){return ['success'=>'false','fail_info'=>'没有权限'];}
  745. if(is_null($id)){
  746. return ['success'=>'false','fail_info'=>'传参错误'];
  747. }
  748. $wayontop = WaybillOnTop::withTrashed()->where('waybill_id',$id);
  749. if(count($wayontop->get()) == 0){
  750. $wayontop = WaybillOnTop::create(['waybill_id'=>$id,'remark'=>$detail]);
  751. $result = $wayontop->save();
  752. }else{
  753. $result = WaybillOnTop::withTrashed()->where('waybill_id',$id)->restore();
  754. }
  755. return ['success'=>$result,'status'=>$result];
  756. }
  757. // 取消置顶
  758. public function cancelOnTop(Request $request){
  759. $id = $request->input('id');
  760. if(!Gate::allows('运输管理-置顶')){return ['success'=>'false','fail_info'=>'没有权限'];}
  761. if(is_null($id)){
  762. return ['success'=>'false','fail_info'=>'传参错误'];
  763. }
  764. $result = WaybillOnTop::where('waybill_id',$id)->forceDelete();
  765. return ['success'=>$result,'status'=>$result];
  766. }
  767. //同步刷新仓库计重
  768. public function refreshWaveHouseWeight(Request $request){
  769. $wms_bill_number=$request->input('wms_bill_number');
  770. if(is_null($wms_bill_number)) return ['success'=>false,'fail_info'=>'传参错误'];
  771. $waybills=DB::connection('oracle')->table('DOC_ORDER_DETAILS')->where('orderno',$wms_bill_number)->get();
  772. if($waybills->isEmpty()) return ['success'=>false,'fail_info'=>'传参错误'];
  773. $warehouseWeight=0;
  774. foreach ($waybills as $waybill){
  775. if ($waybill->grossweight) $warehouseWeight += $waybill->grossweight;
  776. if (!$waybill->grossweight&& $waybill->netweight) $warehouseWeight +=$waybill->netweight;
  777. }
  778. $warehouseWeight=round($warehouseWeight,2);
  779. $waybill=Waybill::where('wms_bill_number',$wms_bill_number)->first();
  780. if ($warehouseWeight!=0){
  781. if ($waybill['warehouse_weight_other']!=$warehouseWeight){
  782. $waybill['warehouse_weight_other']=$warehouseWeight;
  783. $waybill->update();
  784. $this->log(__METHOD__,'刷新仓库计重'.__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  785. }
  786. }else{
  787. $warehouseWeight=$waybill['warehouse_weight_other'];
  788. }
  789. return ['success'=>true,'warehouseWeight'=>$warehouseWeight];
  790. }
  791. }