WaybillController.php 48 KB

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