WaybillController.php 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Components\AsyncResponse;
  4. use App\Region;
  5. use App\Services\CarTypeService;
  6. use App\Services\common\ExportService;
  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\Unit;
  18. use App\Waybill;
  19. use App\WaybillPayoff;
  20. use App\WaybillFinancialExcepted;
  21. use App\WaybillFinancialSnapshot;
  22. use Carbon\Carbon;
  23. use Exception;
  24. use Illuminate\Database\Eloquent\Builder;
  25. use Illuminate\Database\Eloquent\Collection;
  26. use Illuminate\Http\Request;
  27. use Illuminate\Support\Facades\Auth;
  28. use Illuminate\Support\Facades\DB;
  29. use Illuminate\Support\Facades\Gate;
  30. use Illuminate\Support\Facades\Http;
  31. use Illuminate\Support\Facades\Storage;
  32. use Illuminate\Support\Facades\Validator;
  33. use Intervention\Image\Facades\Image;
  34. use Ramsey\Uuid\Uuid;
  35. class WaybillController extends Controller
  36. {
  37. use AsyncResponse;
  38. public function __construct()
  39. {
  40. app()->singleton('waybillService',WaybillService::class);
  41. }
  42. /**
  43. * @param Request $request
  44. * @param OwnerService $ownerService
  45. * @param LogisticService $logisticService
  46. * @return void
  47. */
  48. public function index(Request $request,OwnerService $ownerService,LogisticService $logisticService)
  49. {
  50. if(!Gate::allows('运输管理-查询')){ return view("waybill.authorityMenu"); }
  51. $paginateParams = $request->input();
  52. $waybills=app('waybillService')->paginate($request->input());
  53. return view('waybill.index', [
  54. 'waybills' => $waybills,
  55. 'logistics' => $logisticService->getSelection(["id","name"],"物流"),
  56. 'owners' => $ownerService->getIntersectPermitting(),
  57. 'paginateParams'=>$paginateParams,
  58. 'uriType'=>$request->uriType??'']);
  59. }
  60. public function create(Request $request,OwnerService $ownerService)
  61. {
  62. if(!Gate::allows('运输管理-录入')){ return redirect(url('/')); }
  63. $type=$request->type ?? "";
  64. if ($type==='ZF')$type='直发车';
  65. if ($type==='ZX')$type='专线';
  66. return view('waybill.create',['owners'=>$ownerService->getIntersectPermitting(),'type'=>$type]);
  67. }
  68. public function store(Request $request)
  69. {
  70. if(!Gate::allows('运输管理-录入')){ return redirect(url('/')); }
  71. $this->validatorWaybill($request,false)->validate();
  72. /** @var WaybillService */
  73. $waybill=app('waybillService')->store($request);
  74. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  75. return redirect('waybill/index')->with('successTip','新运单“'.$waybill->waybill_number.'”录入成功');
  76. }
  77. public function edit($id,LogisticService $logisticService,CarTypeService $carTypeService,UnitService $unitService)
  78. {
  79. if(!Gate::allows('运输管理-编辑')){ return redirect(url('/')); }
  80. $waybill = app('waybillService')->find($id);
  81. if ($waybill->order_id){
  82. /** @var Waybill $waybill */
  83. $waybill->load("order.owner");
  84. $waybill->destination_city_id = app("RegionService")->getCity($waybill->order->city ?? '',$waybill->order->province ?? '');
  85. }else{
  86. /** @var Waybill $waybill */
  87. $waybill->load("owner");
  88. }
  89. /** @var \stdClass $waybill */
  90. if (!$waybill)return view("exception.default",["code"=>"500","message"=>"数据已被删除或丢失"]);
  91. if ($waybill->deliver_at){
  92. $waybill->deliver_at_date=Carbon::parse($waybill->deliver_at)->format('Y-m-d');
  93. $waybill->deliver_at_time=Carbon::parse($waybill->deliver_at)->format('H:i:s');
  94. }
  95. $cities=app("RegionService")->getSelection(2);
  96. $units=$unitService->getSelection();
  97. $carTypes=$carTypeService->getSelection();
  98. return view('waybill/edit',['waybill'=>$waybill,'logistics'=>$logisticService->getSelection(["id","name"],"物流"),'cities'=>$cities,'units'=>$units,'carTypes'=>$carTypes]);
  99. }
  100. public function update(Request $request, $id,WaybillPriceModelService $waybillPriceModelService,
  101. LogisticService $logisticService,WaybillPayoffService $waybillPayoffService)
  102. {
  103. if(!Gate::allows('运输管理-调度')){ return redirect(url('/')); }
  104. if (!$request->warehouse_weight && $request->warehouse_weight_unit_id){
  105. $request->offsetUnset('warehouse_weight_unit_id');
  106. }
  107. if (!$request->warehouse_weight_other && $request->warehouse_weight_unit_id_other){
  108. $request->offsetUnset('warehouse_weight_unit_id_other');
  109. }
  110. if (!$request->carrier_weight && $request->carrier_weight_unit_id){
  111. $request->offsetUnset('carrier_weight_unit_id');
  112. }
  113. if (!$request->carrier_weight_other && $request->carrier_weight_unit_id_other){
  114. $request->offsetUnset('carrier_weight_unit_id_other');
  115. }
  116. $this->validatorWaybillDispatch($request,$id)->validate();
  117. $waybillPayoffParam = [];
  118. $waybillPayoffParam['total_receivable']=0;
  119. /** @var WaybillService */
  120. $waybill = app('waybillService')->update($request,$id);
  121. if ($waybill->type=="直发车"){
  122. if ($waybill->charge)$waybillPayoffParam['total_receivable'] = ($waybill->charge);
  123. elseif ($waybill->collect_fee)$waybillPayoffParam['total_receivable'] = ($waybill->collect_fee);
  124. $waybillPayoffParam['total_expense'] = ($waybill->fee)+($waybill->other_fee)-($waybill->collect_fee);
  125. }else {
  126. $waybillPriceModel_id=$request->input('waybillPriceModel');
  127. if ($waybillPriceModel_id){
  128. $carrier_weight=$request->input('carrier_weight');
  129. $waybillPriceModel=$waybillPriceModelService->find($waybillPriceModel_id);
  130. $logistic=$logisticService->find($waybill->logistic_id);
  131. if ($carrier_weight<$waybillPriceModel->initial_weight){
  132. $fee=(($waybillPriceModel->unit_price)*($waybillPriceModel->initial_weight))+$logistic->delivery_fee;
  133. }else{
  134. $fee=(($waybillPriceModel->unit_price)*$carrier_weight)+$logistic->delivery_fee;
  135. }
  136. if ($waybillPriceModel->base_fee&&$fee<$waybillPriceModel->base_fee){
  137. $fee=$waybillPriceModel->base_fee;
  138. }
  139. $waybill->fee=$fee;
  140. $waybill->waybill_price_model_id=$waybillPriceModel_id;
  141. }
  142. $waybill->save();
  143. if ($waybill->charge)$waybillPayoffParam['total_receivable'] = ($waybill->charge);
  144. elseif($waybill->collect_fee) {
  145. $waybillPayoffParam['total_receivable'] = $waybill->collect_fee;
  146. }
  147. $waybillPayoffParam['total_expense'] = ($waybill->pick_up_fee)+($waybill->other_fee)+($waybill->fee);
  148. }
  149. if ($waybillPayoffParam['total_receivable'] > 0){
  150. $waybillPayoffParam['waybill_id'] = $id;
  151. $waybillPayoffParam['gross_margin'] = $waybillPayoffParam['total_receivable'] - $waybillPayoffParam['total_expense'];
  152. $waybillPayoffParam['gross_profit_rate'] = $waybillPayoffParam['gross_margin']/$waybillPayoffParam['total_receivable'];
  153. $waybillPayoffService->updateOrCreate($waybillPayoffParam);
  154. }
  155. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  156. return redirect('waybill/index')->with('successTip','运单“'.$waybill->waybill_number.'”调度成功');
  157. }
  158. public function checkWaybillPriceModel($logistic_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id){
  159. //确保承运商计数与计数单位为一个数组且长度2
  160. if(!$logistic_id)return false;
  161. if(!$destination_city_id)return false;
  162. if(!$carrier_weight)return false;
  163. if(!$carrier_weight_unit_id)return false;
  164. //多个计数标准,计算价格,取最贵
  165. if ($carrier_weight[0]&&$carrier_weight[1]&&$carrier_weight_unit_id[0]&&$carrier_weight_unit_id[1]){
  166. //城市价格区间不为空
  167. $waybillPriceModelOne=WaybillPriceModel::query()->where('logistic_id',$logistic_id)->where('city_id',$destination_city_id)
  168. ->where('range_min','<',$carrier_weight[0])->where('range_max','>=',$carrier_weight[0])
  169. ->where('unit_id',$carrier_weight_unit_id[0])->first();
  170. $waybillPriceModelTwo=WaybillPriceModel::query()->where('logistic_id',$logistic_id)->where('city_id',$destination_city_id)
  171. ->where('range_min','<',$carrier_weight[1])->where('range_max','>=',$carrier_weight[1])
  172. ->where('unit_id',$carrier_weight_unit_id[1])->first();
  173. if ($waybillPriceModelOne&&$waybillPriceModelTwo){
  174. if ($waybillPriceModelOne->unit_price*$carrier_weight[0]>=$waybillPriceModelTwo->unit_price*$carrier_weight[1]){
  175. return $waybillPriceModelOne->id;
  176. }else{
  177. return $waybillPriceModelTwo->id;
  178. }
  179. }
  180. if ($waybillPriceModelOne)return $waybillPriceModelOne->id;
  181. if ($waybillPriceModelTwo)return $waybillPriceModelTwo->id;
  182. //价格区间为空
  183. $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();
  184. $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();
  185. if ($waybillPriceModelRangeOne&&$waybillPriceModelRangeTwo){
  186. if ($waybillPriceModelRangeOne->unit_price*$carrier_weight[0]>=$waybillPriceModelRangeTwo->unit_price*$carrier_weight[1]){
  187. return $waybillPriceModelRangeOne->id;
  188. }else{
  189. return $waybillPriceModelRangeTwo->id;
  190. }
  191. }
  192. if ($waybillPriceModelRangeOne)return $waybillPriceModelRangeOne->id;
  193. if ($waybillPriceModelRangeTwo)return $waybillPriceModelRangeTwo->id;
  194. //城市为空
  195. $city=Region::query()->where('id',$destination_city_id)->select('parent_id')->first();
  196. $waybillPriceModelProvinceOne=WaybillPriceModel::query()->whereRaw('logistic_id = ? AND province_id = ? AND unit_id = ? AND range_max >= ? AND range_min < ? AND city_id IS NULL',
  197. [$logistic_id,$city->parent_id ?? 0,$carrier_weight_unit_id[0],$carrier_weight[0],$carrier_weight[0]])->first();
  198. $waybillPriceModelProvinceTwo=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[1],$carrier_weight[1],$carrier_weight[1]])->first();
  200. if ($waybillPriceModelProvinceOne&&$waybillPriceModelProvinceTwo){
  201. if ($waybillPriceModelProvinceOne->unit_price*$carrier_weight[0]>=$waybillPriceModelProvinceTwo->unit_price*$carrier_weight[1]){
  202. return $waybillPriceModelProvinceOne->id;
  203. }else{
  204. return $waybillPriceModelProvinceTwo->id;
  205. }
  206. }
  207. if ($waybillPriceModelProvinceOne)return $waybillPriceModelProvinceOne->id;
  208. if ($waybillPriceModelProvinceTwo)return $waybillPriceModelProvinceTwo->id;
  209. //城市价格区间都为空
  210. $waybillPriceModelProvinceRangeOne=WaybillPriceModel::query()->whereRaw('logistic_id = ? AND province_id = ? AND unit_id = ? AND range_max IS NULL AND city_id IS NULL',
  211. [$logistic_id,$city->parent_id ?? 0,$carrier_weight_unit_id[0]])->first();
  212. $waybillPriceModelProvinceRangeTwo=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[1]])->first();
  214. if ($waybillPriceModelProvinceRangeOne&&$waybillPriceModelProvinceRangeTwo){
  215. if ($waybillPriceModelProvinceRangeOne->unit_price*$carrier_weight[0]>=$waybillPriceModelProvinceRangeTwo->unit_price*$carrier_weight[1]){
  216. return $waybillPriceModelProvinceRangeOne->id;
  217. }else{
  218. return $waybillPriceModelProvinceRangeOne->id;
  219. }
  220. }
  221. if ($waybillPriceModelProvinceRangeOne)return $waybillPriceModelProvinceRangeOne->id;
  222. if ($waybillPriceModelProvinceRangeTwo)return $waybillPriceModelProvinceRangeTwo->id;
  223. };
  224. for ($i=0;$i<count($carrier_weight);$i++){
  225. if ($carrier_weight[$i]&&$carrier_weight_unit_id[$i]){
  226. //城市价格区间不为空
  227. $waybillPriceModel=WaybillPriceModel::query()->where('logistic_id',$logistic_id)->where('city_id',$destination_city_id)
  228. ->where('range_min','<',$carrier_weight[$i])->where('range_max','>=',$carrier_weight[$i])
  229. ->where('unit_id',$carrier_weight_unit_id[$i])->first();
  230. if($waybillPriceModel)return $waybillPriceModel->id;
  231. //价格区间为空
  232. $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();
  233. if ($waybillPriceModelRange){ return $waybillPriceModelRange->id;}
  234. //城市为空
  235. $city=Region::query()->where('id',$destination_city_id)->select('parent_id')->first();
  236. $waybillPriceModelProvince=WaybillPriceModel::query()->whereRaw('logistic_id = ? AND province_id = ? AND unit_id = ? AND range_max >= ? AND range_min < ? AND city_id IS NULL',
  237. [$logistic_id,$city->parent_id ?? 0,$carrier_weight_unit_id[$i],$carrier_weight[$i],$carrier_weight[$i]])->first();
  238. if ($waybillPriceModelProvince){return $waybillPriceModelProvince->id;}
  239. //城市价格区间都为空
  240. $waybillPriceModelProvinceRange=WaybillPriceModel::query()->whereRaw('logistic_id = ? AND province_id = ? AND unit_id = ? AND range_max IS NULL AND city_id IS NULL',
  241. [$logistic_id,$city->parent_id ?? 0,$carrier_weight_unit_id[$i]])->first();
  242. if ($waybillPriceModelProvinceRange){return $waybillPriceModelProvinceRange->id;}
  243. }
  244. }
  245. return false;
  246. }
  247. /*三层条件:无优先级,找到第一个直接返回
  248. * 无论是否为KG||T,计数单位一为KG,计数单位一为T,计数单位二为KG,计数单位二为T
  249. * 计数一与计数二同时存在取最贵价格:
  250. * 计数一存在,二不存在:
  251. * 计数二存在,一不存在:
  252. * 城市价格区间不为空,城市价格区间都为空,城市为空,价格区间为空
  253. * */
  254. public function isWaybillPriceModel(Request $request){
  255. $logistic_id=$request->input('logistic_id');
  256. $destination_city_id=$request->input('destination_city_id');
  257. $carrier_weight=$request->input('carrier_weight');
  258. $carrier_weight_unit_id=$request->input('carrier_weight_unit_id');
  259. $validatorData=["logistic_id"=>$logistic_id,"destination_city_id"=>$destination_city_id,
  260. 'carrier_weight'=>$carrier_weight[0],"carrier_weight_unit_id"=>$carrier_weight_unit_id[0],
  261. "carrier_weight_other"=>$carrier_weight[1],"carrier_weight_unit_id_other"=>$carrier_weight_unit_id[1]];
  262. $errors=Validator::make($validatorData,[
  263. 'logistic_id'=>'required|integer',
  264. 'destination_city_id'=>'required|integer',
  265. 'carrier_weight'=>'nullable|min:0|numeric|max:999999',
  266. 'carrier_weight_unit_id'=>'required_with:carrier_weight',
  267. 'carrier_weight_other'=>'nullable|min:0|numeric|max:999999',
  268. 'carrier_weight_unit_id_other'=>'required_with:carrier_weight_other',
  269. ],[
  270. 'required'=>':attribute 为必填项',
  271. 'max'=>':attribute 字符过多或输入值过大',
  272. 'min'=>':attribute 不得为负',
  273. 'numeric'=>':attribute 应为数字',
  274. 'unique'=>':attribute 已存在',
  275. 'required_with'=>':attribute 未填',
  276. 'integer'=>':attribute 必须为数字',
  277. ],[
  278. 'carrier_weight'=>'承运商计数(抛)',
  279. 'logistic_id'=>'承运商',
  280. 'destination_city_id'=>'目的市',
  281. 'carrier_weight_unit_id'=>'承运商计数单位',
  282. 'carrier_weight_other'=>'承运商计数二',
  283. 'carrier_weight_unit_id_other'=>'承运商计数单位二',
  284. ])->errors();
  285. if (count($errors)>0)return ['error'=>$errors];
  286. $result=$this->checkWaybillPriceModel($logistic_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
  287. if (!$result){
  288. //单位为kg,T时
  289. $unitKG=Unit::query()->where('name','kg')->first();
  290. $unitT=Unit::query()->where('name','T')->first();
  291. if ($carrier_weight_unit_id[0]==$unitKG->id){
  292. $carrier_weight_unit_id[0]=$unitT->id;
  293. $carrier_weight[0]=$carrier_weight[0]/1000;
  294. $result=$this->checkWaybillPriceModel($logistic_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
  295. if ($result)return ['success'=>$result];
  296. }
  297. if ($carrier_weight_unit_id[1]==$unitKG->id){
  298. $carrier_weight_unit_id[1]=$unitT->id;
  299. $carrier_weight[1]=$carrier_weight[1]/1000;
  300. $result=$this->checkWaybillPriceModel($logistic_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
  301. if ($result)return ['success'=>$result];
  302. }
  303. if ($carrier_weight_unit_id[0]==$unitT->id){
  304. $carrier_weight_unit_id[0]=$unitKG->id;
  305. $carrier_weight[0]=$carrier_weight[0]*1000;
  306. $result=$this->checkWaybillPriceModel($logistic_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
  307. if ($result)return ['success'=>$result];
  308. }
  309. if ($carrier_weight_unit_id[1]==$unitT->id){
  310. $carrier_weight_unit_id[1]=$unitKG->id;
  311. $carrier_weight[1]=$carrier_weight[1]*1000;
  312. $result=$this->checkWaybillPriceModel($logistic_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
  313. if ($result)return ['success'=>$result];
  314. }
  315. }
  316. return ['success'=>$result];
  317. }
  318. public function waybillUpdate(Request $request, $id){
  319. if(!Gate::allows('运输管理-编辑')){ return redirect(url('/')); }
  320. $this->validatorWaybill($request,$id)->validate();
  321. $data=$request->input();
  322. $waybill=app('waybillService')->find($id);
  323. $waybill->fill($data);
  324. if ($waybill->save()){
  325. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
  326. return redirect('waybill/index')->with('successTip','运单“'.$waybill->waybill_number.'”修改成功');
  327. }
  328. }
  329. public function waybillAudit(Request $request){
  330. if(!Gate::allows('运输管理-运单审核')){ return redirect(url('/')); }
  331. $id=$request->input('id');
  332. $waybill=app('waybillService')->find($id);
  333. $isAudit=WaybillAuditLog::whereRaw('waybill_id = ? and audit_stage = ?',[$id,"运单阶段"])->first();
  334. if (empty($isAudit)){
  335. $waybillAuditLog=new WaybillAuditLog([
  336. 'waybill_id'=>$id,
  337. 'audit_stage'=>'运单阶段',
  338. 'user_id'=>Auth::id(),
  339. ]);
  340. $waybillAuditLog->save();
  341. $waybillAuditLog['user']=Auth::user();
  342. $waybill->status='已审核';
  343. $result=$waybill->save();
  344. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
  345. return ['success'=>$result,'status'=>$waybill->status,'waybillAuditLog'=>$waybillAuditLog];
  346. }
  347. return ['exception'=>'请勿重复审核!'];
  348. }
  349. public function waybillEdit($id){
  350. if(!Gate::allows('运输管理-编辑')){ return redirect(url('/')); }
  351. $waybill=app('waybillService')->find($id);
  352. $owners=app("OwnerService")->getIntersectPermitting();
  353. return view('waybill.waybillEdit',['waybill'=>$waybill,'owners'=>$owners]);
  354. }
  355. public function waybillRetreatAudit(Request $request){
  356. if(!Gate::allows('运输管理-调度')){ return redirect(url('/')); }
  357. $id=$request->input('id');
  358. $waybill=app('waybillService')->find($id);
  359. WaybillAuditLog::query()->whereRaw('waybill_id = ? and audit_stage = ?',[$id,"运单阶段"])->delete();
  360. $waybill->status='待重审';
  361. $result=$waybill->save();
  362. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
  363. return ['success'=>$result,'status'=>$waybill->status];
  364. }
  365. public function waybillEndAudit(Request $request){
  366. if(!Gate::allows('运输管理-调度审核')){ return redirect(url('/')); }
  367. $id=$request->input('id');
  368. $waybill=Waybill::query()->with(["owner","logistic","originationCity","destinationCity","carType",'priceModel',"amountUnit",
  369. "warehouseWeightUnit","carrierWeightUnit","warehouseWeightUnitOther","carrierWeightUnitOther"])->find($id);
  370. if (!$waybill->charge&&!$waybill->collect_fee)return ['exception'=>'收费或到付费用未填!'];
  371. if ($waybill->charge==0&&$waybill->collect_fee==0)return ['exception'=>'收费与到付费用都为0!'];
  372. if ($waybill->type=='专线'){
  373. if (!$waybill->carrier_weight_other||$waybill->carrier_weight_other==0)return ['exception'=>'承运商计重未填或为0!'];
  374. if (!$waybill->carrier_weight_unit_id_other)return ['exception'=>'承运商计重单位未选!'];
  375. }
  376. $isAudit=WaybillAuditLog::query()->whereRaw('waybill_id = ? and audit_stage = ?',[$id,"调度阶段"])->first();
  377. if (empty($isAudit)){
  378. $waybillAuditLog=new WaybillAuditLog([
  379. 'waybill_id'=>$id,
  380. 'audit_stage'=>'调度阶段',
  381. 'user_id'=>Auth::id(),
  382. ]);
  383. $waybillAuditLog->save();
  384. $waybillAuditLog['user']=Auth::user();
  385. if ($waybill->waybill_price_model_id||$waybill->type=='直发车'){
  386. $waybill->status='已完结';
  387. $result=$waybill->save();
  388. $waybillPayoff=WaybillPayoff::query()->where('waybill_id','=',$id)->first();
  389. $waybillPayoffJson=json_encode($this->createReportData($waybill,$waybillPayoff),JSON_UNESCAPED_UNICODE);
  390. WaybillFinancialSnapshot::query()->create([
  391. 'waybill_id'=>$id,
  392. 'json_content'=>$waybillPayoffJson,
  393. ]);
  394. }else{
  395. $waybill->status='无模型';
  396. $result=$waybill->save();
  397. $waybillPayoff=WaybillPayoff::query()->where('waybill_id','=',$id)->first();
  398. if ($waybillPayoff){
  399. $waybillPayoffJson=json_encode($this->createReportData($waybill,$waybillPayoff),JSON_UNESCAPED_UNICODE);
  400. WaybillFinancialExcepted::query()->create([
  401. 'waybill_id'=>$id,
  402. 'json_content'=>$waybillPayoffJson,
  403. ]);
  404. }
  405. }
  406. app("waybillService")->createInstantBill($waybill);
  407. app('LogService')->log(__METHOD__,__FUNCTION__,$waybillPayoffJson,Auth::id());
  408. return ['success'=>$result,'status'=>$waybill->status,'waybillAuditLog'=>$waybillAuditLog];
  409. }
  410. return ['exception'=>'请勿重复审核!'];
  411. }
  412. //生成报表数据
  413. private function createReportData($waybill,$waybillPayoff){
  414. /** @var Waybill $waybill */
  415. $waybill->loadMissing(["order.owner"]);
  416. return [
  417. "type"=>$waybill->type,
  418. "waybill_number"=>$waybill->waybill_number,
  419. "owner_name"=>$waybill->order->owner->name ?? ($waybill->owner->name ?? null),
  420. "wms_bill_number"=>$waybill->wms_bill_number,
  421. "source_bill"=>$waybill->source_bill,
  422. "origination"=>$waybill->origination,
  423. "destination"=>$waybill->order->address ?? $waybill->destination,
  424. "recipient"=>$waybill->order->consignee_name ?? $waybill->recipient,
  425. "recipient_mobile"=>$waybill->order->consignee_phone ?? $waybill->recipient_mobile,
  426. "charge"=>$waybill->charge,
  427. "collect_fee"=>$waybill->collect_fee,
  428. "ordering_remark"=>$waybill->ordering_remark,
  429. "carrier_name"=>$waybill->logistic->name ?? null,
  430. "carrier_bill"=>$waybill->carrier_bill,
  431. "origination_city_name"=>$waybill->originationCity ? $waybill->originationCity->name : null,
  432. "destination_city_name"=>$waybill->order->city ?? ($waybill->destinationCity->name ?? null),
  433. "warehouse_weight"=>$waybill->warehouse_weight.($waybill->warehouseWeightUnit ? $waybill->warehouseWeightUnit->name : ''),
  434. "carrier_weight"=>$waybill->carrier_weight.($waybill->carrierWeightUnit ? $waybill->carrierWeightUnit->name : ''),
  435. "warehouse_weight_other"=>$waybill->warehouse_weight_other.($waybill->warehouseWeightUnitOther ? $waybill->warehouseWeightUnitOther->name : ''),
  436. "carrier_weight_other"=>$waybill->carrier_weight_other.($waybill->carrierWeightUnitOther ? $waybill->carrierWeightUnitOther->name : ''),
  437. "car_type_name"=>$waybill->carType ? $waybill->carType->name : null,
  438. "fee"=>$waybill->fee,
  439. "pick_up_fee"=>$waybill->pick_up_fee,
  440. "other_fee"=>$waybill->other_fee,
  441. "dispatch_remark"=>$waybill->dispatch_remark,
  442. "price_model_range_min"=>$waybill->priceModel ? $waybill->priceModel->range_min : null,
  443. "price_model_range_max"=>$waybill->priceModel ? $waybill->priceModel->range_max : null,
  444. "price_model_unit_price"=>$waybill->priceModel ? $waybill->priceModel->unit_price : null,
  445. "price_model_base_fee"=>$waybill->priceModel ? $waybill->priceModel->base_fee : null,
  446. "price_model_initial_weight"=>$waybill->priceModel ? $waybill->priceModel->initial_weight : null,
  447. "car_owner_info"=>$waybill->car_owner_info,
  448. "status"=>$waybill->status,
  449. "mileage"=>$waybill->mileage,
  450. 'amount'=>$waybill->amount.($waybill->amountUnit ? $waybill->amountUnit->name : ''),
  451. "inquire_tel"=>$waybill->inquire_tel,
  452. "other_charge"=>$waybill->other_charge,
  453. "other_charge_remark"=>$waybill->other_charge_remark,
  454. "deliver_at"=>$waybill->deliver_at,
  455. "created_at"=>$waybill->created_at,
  456. "auditLog_user_name"=>Auth::user()['name'],
  457. "total_expense"=>$waybillPayoff->total_expense,
  458. "total_receivable"=>$waybillPayoff->total_receivable,
  459. "gross_margin"=>$waybillPayoff->gross_margin,
  460. "gross_profit_rate"=>$waybillPayoff->gross_profit_rate,
  461. ];
  462. }
  463. public function upload(Request $request){
  464. if(!Gate::allows('运输管理-图片上传')){ return '没有权限'; }
  465. $file=$request->file('file');
  466. $waybill_number=$request->input('waybill_number');
  467. $waybill=Waybill::query()->where('waybill_number',$waybill_number)->first();
  468. if (!$waybill){
  469. return ['success'=>false,'error'=>"未找到该运单!"];
  470. }
  471. if ($waybill->upload_file_url){
  472. return ['success'=>false,'error'=>"该运单已存在照片!"];
  473. }
  474. if (!$file){
  475. return ['success'=>false,'error'=>"照片不得为空!"];
  476. }
  477. if (!$file->isValid()){
  478. return ['success'=>false,'error'=>"找不到照片!"];
  479. }
  480. $tmpFile = $file->getRealPath();
  481. if (! is_uploaded_file($tmpFile)) {
  482. return ['success'=>false,'error'=>"文件错误!"];
  483. }
  484. $fileExtension=$file->getClientOriginalExtension();
  485. // 5.存储, 生成一个随机文件名
  486. $fileName = date('ymd').'-'.Uuid::uuid1();//thumbnail common bulky
  487. $thumbnailName=storage_path('app/public/files/'.$fileName.'-thumbnail.'.$fileExtension);
  488. $commonName=storage_path('app/public/files/'.$fileName.'-common.'.$fileExtension);
  489. $bulkyName=storage_path('app/public/files/'.$fileName.'-bulky.'.$fileExtension);
  490. $result=move_uploaded_file ($tmpFile ,$bulkyName);
  491. if ($result){
  492. $img=Image::make($bulkyName);
  493. if ($img->height() > $img->width())
  494. $img->heighten(250)->save($commonName);
  495. else $img->widen(250)->save($commonName);
  496. $img->heighten(28)->save($thumbnailName);
  497. $uploadFile=new UploadFile([
  498. "table_name"=>"waybills",
  499. "table_id"=>$waybill->id,
  500. "url"=>'/files/'.$fileName,
  501. "type"=>$fileExtension,
  502. ]);
  503. if ($uploadFile->save())
  504. app('LogService')->log(__METHOD__,'图片上传',json_encode($request),Auth::user()['id']);
  505. $uploadFile->url=asset('/storage'.$uploadFile->url);
  506. return ['success'=>true,'data'=>$uploadFile];
  507. }
  508. return ['success'=>false,'error'=>"图片保存失败!"];
  509. }
  510. //批量上传图片
  511. public function batchUploadImages()
  512. {
  513. $this->gate("运输管理-图片上传");
  514. ini_set('max_execution_time',1000);
  515. ini_set('memory_limit','100M');
  516. $images = request("images");
  517. $errors = [];
  518. $number = [];
  519. $mapping = [];
  520. $type = ["jpg","png","gif","jfif","pjpeg","jpeg","webp"];
  521. foreach ($images as $index => $image){
  522. $arr = explode(".",$image["name"]);
  523. $suffix = $arr[count($arr)-1];
  524. unset($arr[count($arr)-1]);
  525. $name = implode(".",$arr);
  526. if (array_search(strtolower($suffix),$type) === false){
  527. $errors[] = "“".$name."”格式错误";
  528. unset($images[$index]);
  529. continue;
  530. }
  531. $images[$index]["suffix"] = $suffix;
  532. $num = trim(rtrim($name,".".$suffix));
  533. $number[] = $num;
  534. $mapping[$num] = $index;
  535. }
  536. $waybills = Waybill::query()->select("id","source_bill")->doesntHave("uploadFile")->whereIn('source_bill',$number)->get();
  537. foreach (array_diff($number,array_column($waybills->toArray(),"source_bill")) as $diff){
  538. $errors[] = "“".$diff."”不存在运单或已存在照片";
  539. unset($images[$mapping[$diff]]);
  540. }
  541. $insert = [];
  542. foreach ($waybills as $waybill){
  543. $image = $images[$mapping[$waybill->source_bill]];
  544. $fileName = date('ymd').'-'.Uuid::uuid1();
  545. $suffix = $image["suffix"];
  546. $thumbnailName=storage_path('app/public/files/'.$fileName.'-thumbnail.'.$suffix);
  547. $commonName=storage_path('app/public/files/'.$fileName.'-common.'.$suffix);
  548. $bulkyName=storage_path('app/public/files/'.$fileName.'-bulky.'.$suffix);
  549. preg_match('/^(data:\s*image\/(\w+);base64,)/',$image["src"],$res);
  550. $base64_img=base64_decode(str_replace($res[1],'', $image["src"]));
  551. Storage::put('public/files/'.$fileName.'-bulky.'.$suffix,$base64_img);
  552. $img=Image::make($bulkyName);
  553. if ($img->height() > $img->width())
  554. $img->heighten(250)->save($commonName);
  555. else $img->widen(250)->save($commonName);
  556. $img->heighten(28)->save($thumbnailName);
  557. $insert[] = [
  558. "table_name"=>"waybills",
  559. "table_id"=>$waybill->id,
  560. "url"=>'/files/'.$fileName,
  561. "type"=>strtolower($suffix),
  562. ];
  563. }
  564. if ($insert)UploadFile::query()->insert($insert);
  565. $waybills->load("uploadFile");
  566. $this->success(["errors"=>$errors,"data"=>$waybills]);
  567. }
  568. //删除照片
  569. public function deleteImg(Request $request){
  570. if(!Gate::allows('运输管理-图片删除')){ return '没有权限'; }
  571. $ids=$request->input('ids');
  572. $uploadFiles=UploadFile::where('table_name','waybills')->whereIn('table_id',$ids)->get();
  573. foreach ($uploadFiles as $uploadFile){
  574. $bulky=storage_path('app/public/'.$uploadFile->url.'-bulky.'.$uploadFile->type);
  575. $common=storage_path('app/public/'.$uploadFile->url.'-common.'.$uploadFile->type);
  576. $thumbnail=storage_path('app/public/'.$uploadFile->url.'-thumbnail.'.$uploadFile->type);
  577. if (file_exists($bulky) && file_exists($common) && file_exists($thumbnail)){
  578. unlink($bulky);unlink($common);unlink($thumbnail);
  579. }
  580. }
  581. UploadFile::where('table_name','waybills')->whereIn('table_id',$ids)->delete();
  582. app('LogService')->log(__METHOD__,'图片删除',json_encode($request),Auth::user()['id']);
  583. return ['success'=>true];
  584. }
  585. public function export(){
  586. $this->gate('运输管理-查询');
  587. if (request("checkAllSign")){
  588. request()->offsetUnset("checkAllSign");
  589. $waybills = app('waybillService')->get(request()->input());
  590. }else $waybills = app('waybillService')->get(["id"=>request("id")]);
  591. /** @var Collection $waybills */
  592. $row = [
  593. "运单类型", "货主", "上游单号", "wms订单号", "运单号", "运输收费",
  594. "其他收费", "其他收费备注", "始发地", "目的地", "承运商", "承运商单号",
  595. "仓库计抛", "承运商计抛", "仓库计重", "承运商计重", "车型", "车辆信息",
  596. "计件", "里程数", "运费(元)", "提货费(元)", "其他费用(元)", "发货时间",
  597. "调度备注", "创建时间"
  598. ];
  599. $list = [];
  600. $waybills->each(function ($waybill)use(&$list){
  601. $list[] = [
  602. $waybill->type,
  603. $waybill->order->owner->name ?? ($waybill->owner->name ?? ""),
  604. $waybill->source_bill,
  605. $waybill->wms_bill_number,
  606. $waybill->waybill_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('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('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. }