WaybillController.php 47 KB

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