WaybillController.php 46 KB

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