WaybillController.php 50 KB

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