WaybillController.php 43 KB

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