WaybillController.php 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  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\Database\Eloquent\Model;
  27. use Illuminate\Http\Request;
  28. use Illuminate\Support\Facades\Auth;
  29. use Illuminate\Support\Facades\DB;
  30. use Illuminate\Support\Facades\Gate;
  31. use Illuminate\Support\Facades\Http;
  32. use Illuminate\Support\Facades\Storage;
  33. use Illuminate\Support\Facades\Validator;
  34. use Intervention\Image\Facades\Image;
  35. use Ramsey\Uuid\Uuid;
  36. class WaybillController extends Controller
  37. {
  38. use AsyncResponse;
  39. public function __construct()
  40. {
  41. app()->singleton('waybillService',WaybillService::class);
  42. }
  43. /**
  44. * @param Request $request
  45. * @param OwnerService $ownerService
  46. * @param LogisticService $logisticService
  47. * @return void
  48. */
  49. public function index(Request $request,OwnerService $ownerService,LogisticService $logisticService)
  50. {
  51. if(!Gate::allows('运输管理-运单-查询')){ return view("transport.waybill.authorityMenu"); }
  52. $paginateParams = $request->input();
  53. $waybills=app('waybillService')->paginate($request->input());
  54. return view('transport.waybill.index', [
  55. 'waybills' => $waybills,
  56. 'logistics' => $logisticService->getSelection(["id","name"],"物流"),
  57. 'owners' => $ownerService->getIntersectPermitting(),
  58. 'paginateParams'=>$paginateParams,
  59. 'uriType'=>$request->uriType??'']);
  60. }
  61. public function create(Request $request,OwnerService $ownerService)
  62. {
  63. if(!Gate::allows('运输管理-运单-录入')){ return redirect(url('/')); }
  64. $type=$request->type ?? "";
  65. if ($type==='ZF')$type='直发车';
  66. if ($type==='ZX')$type='专线';
  67. return view('transport.waybill.create',['owners'=>$ownerService->getIntersectPermitting(),'type'=>$type]);
  68. }
  69. public function store(Request $request)
  70. {
  71. if(!Gate::allows('运输管理-运单-录入')){ return redirect(url('/')); }
  72. $this->validatorWaybill($request,false)->validate();
  73. /** @var WaybillService */
  74. $waybill=app('waybillService')->store($request);
  75. return redirect('transport/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('transport.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('transport/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('transport/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('transport.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. /** @var Model|\stdClass $waybill */
  359. $waybill=app('waybillService')->find($id);
  360. $waybillLog = WaybillAuditLog::query()->whereRaw('waybill_id = ? and audit_stage = ?',[$id,"运单阶段"])->delete();
  361. $waybill->status='待重审';
  362. return ['success'=>$waybill->save(),'status'=>$waybill->status,"log"=>$waybillLog];
  363. }
  364. public function waybillEndAudit(Request $request){
  365. if(!Gate::allows('运输管理-运单-调度审核')){ return redirect(url('/')); }
  366. $id=$request->input('id');
  367. $waybill=Waybill::query()->with(["owner","logistic","originationCity","destinationCity","carType",'priceModel',"amountUnit",
  368. "warehouseWeightUnit","carrierWeightUnit","warehouseWeightUnitOther","carrierWeightUnitOther"])->find($id);
  369. if (!$waybill->charge&&!$waybill->collect_fee)return ['exception'=>'收费或到付费用未填!'];
  370. if ($waybill->charge==0&&$waybill->collect_fee==0)return ['exception'=>'收费与到付费用都为0!'];
  371. if ($waybill->type=='专线'){
  372. if (!$waybill->carrier_weight_other||$waybill->carrier_weight_other==0)return ['exception'=>'承运商计重未填或为0!'];
  373. if (!$waybill->carrier_weight_unit_id_other)return ['exception'=>'承运商计重单位未选!'];
  374. }
  375. $isAudit=WaybillAuditLog::query()->whereRaw('waybill_id = ? and audit_stage = ?',[$id,"调度阶段"])->first();
  376. if (empty($isAudit)){
  377. $waybillAuditLog=new WaybillAuditLog([
  378. 'waybill_id'=>$id,
  379. 'audit_stage'=>'调度阶段',
  380. 'user_id'=>Auth::id(),
  381. ]);
  382. $waybillAuditLog->save();
  383. $waybillAuditLog['user']=Auth::user();
  384. if ($waybill->waybill_price_model_id||$waybill->type=='直发车'){
  385. $waybill->status='已完结';
  386. $result=$waybill->save();
  387. $waybillPayoff=WaybillPayoff::query()->where('waybill_id','=',$id)->first();
  388. $waybillPayoffJson=json_encode($this->createReportData($waybill,$waybillPayoff),JSON_UNESCAPED_UNICODE);
  389. WaybillFinancialSnapshot::query()->create([
  390. 'waybill_id'=>$id,
  391. 'json_content'=>$waybillPayoffJson,
  392. ]);
  393. }else{
  394. $waybill->status='无模型';
  395. $result=$waybill->save();
  396. $waybillPayoff=WaybillPayoff::query()->where('waybill_id','=',$id)->first();
  397. if ($waybillPayoff){
  398. $waybillPayoffJson=json_encode($this->createReportData($waybill,$waybillPayoff),JSON_UNESCAPED_UNICODE);
  399. WaybillFinancialExcepted::query()->create([
  400. 'waybill_id'=>$id,
  401. 'json_content'=>$waybillPayoffJson,
  402. ]);
  403. }
  404. }
  405. app("waybillService")->createInstantBill($waybill);
  406. app('LogService')->log(__METHOD__,__FUNCTION__,$waybillPayoffJson,Auth::id());
  407. return ['success'=>$result,'status'=>$waybill->status,'waybillAuditLog'=>$waybillAuditLog];
  408. }
  409. return ['exception'=>'请勿重复审核!'];
  410. }
  411. //生成报表数据
  412. private function createReportData($waybill,$waybillPayoff){
  413. /** @var Waybill $waybill */
  414. $waybill->loadMissing(["order.owner"]);
  415. return [
  416. "type"=>$waybill->type,
  417. "waybill_number"=>$waybill->waybill_number,
  418. "owner_name"=>$waybill->order->owner->name ?? ($waybill->owner->name ?? null),
  419. "wms_bill_number"=>$waybill->wms_bill_number,
  420. "source_bill"=>$waybill->source_bill,
  421. "origination"=>$waybill->origination,
  422. "destination"=>$waybill->order->address ?? $waybill->destination,
  423. "recipient"=>$waybill->order->consignee_name ?? $waybill->recipient,
  424. "recipient_mobile"=>$waybill->order->consignee_phone ?? $waybill->recipient_mobile,
  425. "charge"=>$waybill->charge,
  426. "collect_fee"=>$waybill->collect_fee,
  427. "ordering_remark"=>$waybill->ordering_remark,
  428. "carrier_name"=>$waybill->logistic->name ?? null,
  429. "carrier_bill"=>$waybill->carrier_bill,
  430. "origination_city_name"=>$waybill->originationCity ? $waybill->originationCity->name : null,
  431. "destination_city_name"=>$waybill->order->city ?? ($waybill->destinationCity->name ?? null),
  432. "warehouse_weight"=>$waybill->warehouse_weight.($waybill->warehouseWeightUnit ? $waybill->warehouseWeightUnit->name : ''),
  433. "carrier_weight"=>$waybill->carrier_weight.($waybill->carrierWeightUnit ? $waybill->carrierWeightUnit->name : ''),
  434. "warehouse_weight_other"=>$waybill->warehouse_weight_other.($waybill->warehouseWeightUnitOther ? $waybill->warehouseWeightUnitOther->name : ''),
  435. "carrier_weight_other"=>$waybill->carrier_weight_other.($waybill->carrierWeightUnitOther ? $waybill->carrierWeightUnitOther->name : ''),
  436. "car_type_name"=>$waybill->carType ? $waybill->carType->name : null,
  437. "fee"=>$waybill->fee,
  438. "pick_up_fee"=>$waybill->pick_up_fee,
  439. "other_fee"=>$waybill->other_fee,
  440. "dispatch_remark"=>$waybill->dispatch_remark,
  441. "price_model_range_min"=>$waybill->priceModel ? $waybill->priceModel->range_min : null,
  442. "price_model_range_max"=>$waybill->priceModel ? $waybill->priceModel->range_max : null,
  443. "price_model_unit_price"=>$waybill->priceModel ? $waybill->priceModel->unit_price : null,
  444. "price_model_base_fee"=>$waybill->priceModel ? $waybill->priceModel->base_fee : null,
  445. "price_model_initial_weight"=>$waybill->priceModel ? $waybill->priceModel->initial_weight : null,
  446. "car_owner_info"=>$waybill->car_owner_info,
  447. "status"=>$waybill->status,
  448. "mileage"=>$waybill->mileage,
  449. 'amount'=>$waybill->amount.($waybill->amountUnit ? $waybill->amountUnit->name : ''),
  450. "inquire_tel"=>$waybill->inquire_tel,
  451. "other_charge"=>$waybill->other_charge,
  452. "other_charge_remark"=>$waybill->other_charge_remark,
  453. "deliver_at"=>$waybill->deliver_at,
  454. "created_at"=>$waybill->created_at,
  455. "auditLog_user_name"=>Auth::user()['name'],
  456. "total_expense"=>$waybillPayoff->total_expense,
  457. "total_receivable"=>$waybillPayoff->total_receivable,
  458. "gross_margin"=>$waybillPayoff->gross_margin,
  459. "gross_profit_rate"=>$waybillPayoff->gross_profit_rate,
  460. ];
  461. }
  462. public function upload(Request $request){
  463. if(!Gate::allows('运输管理-运单-图片上传')){ return '没有权限'; }
  464. $file=$request->file('file');
  465. $waybill_number=$request->input('waybill_number');
  466. $waybill=Waybill::query()->where('waybill_number',$waybill_number)->first();
  467. if (!$waybill){
  468. return ['success'=>false,'error'=>"未找到该运单!"];
  469. }
  470. if ($waybill->upload_file_url){
  471. return ['success'=>false,'error'=>"该运单已存在照片!"];
  472. }
  473. if (!$file){
  474. return ['success'=>false,'error'=>"照片不得为空!"];
  475. }
  476. if (!$file->isValid()){
  477. return ['success'=>false,'error'=>"找不到照片!"];
  478. }
  479. $tmpFile = $file->getRealPath();
  480. if (! is_uploaded_file($tmpFile)) {
  481. return ['success'=>false,'error'=>"文件错误!"];
  482. }
  483. $fileExtension=$file->getClientOriginalExtension();
  484. // 5.存储, 生成一个随机文件名
  485. $fileName = date('ymd').'-'.Uuid::uuid1();//thumbnail common bulky
  486. $thumbnailName=storage_path('app/public/files/'.$fileName.'-thumbnail.'.$fileExtension);
  487. $commonName=storage_path('app/public/files/'.$fileName.'-common.'.$fileExtension);
  488. $bulkyName=storage_path('app/public/files/'.$fileName.'-bulky.'.$fileExtension);
  489. $result=move_uploaded_file ($tmpFile ,$bulkyName);
  490. if ($result){
  491. $img=Image::make($bulkyName);
  492. if ($img->height() > $img->width())
  493. $img->heighten(250)->save($commonName);
  494. else $img->widen(250)->save($commonName);
  495. $img->heighten(28)->save($thumbnailName);
  496. $uploadFile=new UploadFile([
  497. "table_name"=>"waybills",
  498. "table_id"=>$waybill->id,
  499. "url"=>'/files/'.$fileName,
  500. "type"=>$fileExtension,
  501. ]);
  502. if ($uploadFile->save())
  503. app('LogService')->log(__METHOD__,'图片上传',json_encode($request),Auth::user()['id']);
  504. $uploadFile->url=asset('/storage'.$uploadFile->url);
  505. return ['success'=>true,'data'=>$uploadFile];
  506. }
  507. return ['success'=>false,'error'=>"图片保存失败!"];
  508. }
  509. //批量上传图片
  510. public function batchUploadImages()
  511. {
  512. $this->gate("运输管理-运单-图片上传");
  513. ini_set('max_execution_time',1000);
  514. ini_set('memory_limit','100M');
  515. $images = request("images");
  516. $errors = [];
  517. $number = [];
  518. $mapping = [];
  519. $type = ["jpg","png","gif","jfif","pjpeg","jpeg","webp"];
  520. foreach ($images as $index => $image){
  521. $arr = explode(".",$image["name"]);
  522. $suffix = $arr[count($arr)-1];
  523. unset($arr[count($arr)-1]);
  524. $name = implode(".",$arr);
  525. if (array_search(strtolower($suffix),$type) === false){
  526. $errors[] = "“".$name."”格式错误";
  527. unset($images[$index]);
  528. continue;
  529. }
  530. $images[$index]["suffix"] = $suffix;
  531. $num = trim(rtrim($name,".".$suffix));
  532. $number[] = $num;
  533. $mapping[$num] = $index;
  534. }
  535. $waybills = Waybill::query()->select("id","source_bill")->doesntHave("uploadFile")->whereIn('source_bill',$number)->get();
  536. foreach (array_diff($number,array_column($waybills->toArray(),"source_bill")) as $diff){
  537. $errors[] = "“".$diff."”不存在运单或已存在照片";
  538. unset($images[$mapping[$diff]]);
  539. }
  540. $insert = [];
  541. foreach ($waybills as $waybill){
  542. $image = $images[$mapping[$waybill->source_bill]];
  543. $fileName = date('ymd').'-'.Uuid::uuid1();
  544. $suffix = $image["suffix"];
  545. $thumbnailName=storage_path('app/public/files/'.$fileName.'-thumbnail.'.$suffix);
  546. $commonName=storage_path('app/public/files/'.$fileName.'-common.'.$suffix);
  547. $bulkyName=storage_path('app/public/files/'.$fileName.'-bulky.'.$suffix);
  548. preg_match('/^(data:\s*image\/(\w+);base64,)/',$image["src"],$res);
  549. $base64_img=base64_decode(str_replace($res[1],'', $image["src"]));
  550. Storage::put('public/files/'.$fileName.'-bulky.'.$suffix,$base64_img);
  551. $img=Image::make($bulkyName);
  552. if ($img->height() > $img->width())
  553. $img->heighten(250)->save($commonName);
  554. else $img->widen(250)->save($commonName);
  555. $img->heighten(28)->save($thumbnailName);
  556. $insert[] = [
  557. "table_name"=>"waybills",
  558. "table_id"=>$waybill->id,
  559. "url"=>'/files/'.$fileName,
  560. "type"=>strtolower($suffix),
  561. ];
  562. }
  563. if ($insert)UploadFile::query()->insert($insert);
  564. $waybills->load("uploadFile");
  565. $this->success(["errors"=>$errors,"data"=>$waybills]);
  566. }
  567. //删除照片
  568. public function deleteImg(Request $request){
  569. if(!Gate::allows('运输管理-运单-图片删除')){ return '没有权限'; }
  570. $ids=$request->input('ids');
  571. $uploadFiles=UploadFile::where('table_name','waybills')->whereIn('table_id',$ids)->get();
  572. foreach ($uploadFiles as $uploadFile){
  573. $bulky=storage_path('app/public/'.$uploadFile->url.'-bulky.'.$uploadFile->type);
  574. $common=storage_path('app/public/'.$uploadFile->url.'-common.'.$uploadFile->type);
  575. $thumbnail=storage_path('app/public/'.$uploadFile->url.'-thumbnail.'.$uploadFile->type);
  576. if (file_exists($bulky) && file_exists($common) && file_exists($thumbnail)){
  577. unlink($bulky);unlink($common);unlink($thumbnail);
  578. }
  579. }
  580. UploadFile::where('table_name','waybills')->whereIn('table_id',$ids)->delete();
  581. app('LogService')->log(__METHOD__,'图片删除',json_encode($request),Auth::user()['id']);
  582. return ['success'=>true];
  583. }
  584. public function export(){
  585. $this->gate('运输管理-运单-查询');
  586. if (request("checkAllSign")){
  587. request()->offsetUnset("checkAllSign");
  588. $waybills = app('waybillService')->get(request()->input());
  589. }else $waybills = app('waybillService')->get(["id"=>request("id")]);
  590. /** @var Collection $waybills */
  591. $row = [
  592. "运单类型", "货主", "上游单号", "wms订单号", "运单号", "运输收费",
  593. "其他收费", "其他收费备注", "始发地", "目的地", "承运商", "承运商单号",
  594. "仓库计抛", "承运商计抛", "仓库计重", "承运商计重", "车型", "车辆信息",
  595. "计件", "里程数", "运费(元)", "提货费(元)", "其他费用(元)", "发货时间",
  596. "调度备注", "创建时间"
  597. ];
  598. $list = [];
  599. $waybills->each(function ($waybill)use(&$list){
  600. $list[] = [
  601. $waybill->type,
  602. $waybill->order->owner->name ?? ($waybill->owner->name ?? ""),
  603. $waybill->source_bill,
  604. $waybill->wms_bill_number,
  605. $waybill->waybill_number,
  606. $waybill->charge,
  607. $waybill->other_charge,
  608. $waybill->other_charge_remark,
  609. $waybill->origination,
  610. $waybill->order->address ?? $waybill->destination,
  611. $waybill->logistic->name ?? "",
  612. $waybill->carrier_bill,
  613. $waybill->warehouse_weight,
  614. $waybill->carrier_weight,
  615. $waybill->warehouse_weight_other,
  616. $waybill->carrier_weight_other,
  617. $waybill->car_type_name,
  618. $waybill->car_owner_info,
  619. $waybill->amount,
  620. $waybill->mileage,
  621. $waybill->fee,
  622. $waybill->pick_up_fee,
  623. $waybill->other_fee,
  624. $waybill->deliver_at,
  625. $waybill->dispatch_remark,
  626. $waybill->created_at,
  627. ];
  628. });
  629. return app(ExportService::class)->json($row,$list,"运输记录单");
  630. }
  631. public function deliveringExport(Request $request){
  632. if ($request->checkAllSign){
  633. $param = $request->input();
  634. unset($param['checkAllSign']);
  635. $sql = app('waybillService')->getDeliveringSql($param);
  636. }else{
  637. $sql = app('waybillService')->getDeliveringSql(['id'=>$request->data]);
  638. }
  639. $post = Http::post(config('go.export.url'),['type'=>'waybillDelivering','sql'=>$sql]);
  640. if ($post->status() == 500){
  641. throw new Exception($post->header("Msg"));
  642. }
  643. return response($post,200, [
  644. "Content-type"=>"application/octet-stream",
  645. "Content-Disposition"=>"attachment; filename=发运列表-".date('ymdHis').'.xlsx',
  646. ]);
  647. }
  648. //发运
  649. public function delivering(Request $request){
  650. if (!Auth::user())return view('exception.login');
  651. $waybills= app('waybillService')->paginate($request->input());
  652. if (!Auth::user()->isSuperAdmin()){
  653. $carriersUsers=DB::table('carrier_user')->where('user_id',Auth::id())->get();
  654. $carrierIds=array_column($carriersUsers->toArray(),'logistic_id');
  655. $waybills=$waybills->whereIn("logistic_id",$carrierIds);
  656. }
  657. return view('transport.waybill.delivering',compact('waybills'));
  658. }
  659. //承运商提交
  660. public function storeCarrierBill(Request $request){
  661. $errors=Validator::make($request->input(),[
  662. 'id'=>'required|integer',
  663. 'carrier_bill'=>'required',
  664. 'inquire_tel'=>'nullable',
  665. 'amount'=>'nullable|integer',
  666. 'carrier_weight'=>'required_without:carrier_weight_other|nullable|numeric',
  667. 'carrier_weight_other'=>'required_without:carrier_weight|nullable|numeric',
  668. ],[
  669. 'required'=>':attribute 为必填项',
  670. 'integer'=>':attribute 应为整数',
  671. 'numeric'=>':attribute 应为数字',
  672. 'required_with'=>':attribute 重量与体积至少存在一项',
  673. ],[
  674. 'carrier_bill'=>'专线运单号',
  675. 'inquire_tel'=>'查件电话',
  676. 'amount'=>'件数',
  677. 'carrier_weight'=>'体积',
  678. 'carrier_weight_other'=>'重量',
  679. ])->errors();
  680. if (count($errors)>0)return ["errors"=>$errors];
  681. $waybill=Waybill::query()->find($request->input('id'));
  682. if (!$waybill)return ["error"=>"未找到该运单!"];
  683. $waybill->fill($request->input());
  684. $waybill->update();
  685. return $waybill;
  686. }
  687. protected function validatorWaybill(Request $request,$id){
  688. if ($id){$wms_bill_number=$id;};
  689. $validator=Validator::make($request->input(),[
  690. 'owner_id'=>'required_without:order_id',
  691. 'wms_bill_number'=>['nullable','max:50',isset($wms_bill_number)?"unique:waybills,wms_bill_number,$wms_bill_number":'unique:waybills,wms_bill_number'],
  692. 'origination'=>'required|max:255',
  693. 'destination'=>'required_without:order_id|max:255',
  694. 'recipient'=>'required_without:order_id|max:50',
  695. 'recipient_mobile'=>['required_without:order_id','regex:/^(\d{7,11})|(1[3|4|5|7|8][0-9]\d{4,8})$/'],
  696. 'charge'=>'nullable|min:0|max:999999|numeric',
  697. 'collect_fee'=>'nullable|min:0|numeric',
  698. ],[
  699. 'required'=>':attribute 为必填项',
  700. 'required_without'=>':attribute 为必填项',
  701. 'alpha_num'=>':attribute 应为字母或数字',
  702. 'max'=>':attribute 字符过多或输入值过大',
  703. 'regex'=>':attribute 输入有误',
  704. 'integer'=>':attribute 应为整数',
  705. 'min'=>':attribute 不得为负',
  706. 'numeric'=>':attribute 应为数字',
  707. 'unique'=>':attribute 已存在',
  708. ],[
  709. 'owner_id'=>'货主',
  710. 'wms_bill_number'=>'WMS单号',
  711. 'origination'=>'始发地',
  712. 'destination'=>'目的地',
  713. 'recipient'=>'收件人',
  714. 'recipient_mobile'=>'收件人电话',
  715. 'charge'=>'收费',
  716. 'collect_fee'=>'到付金额',
  717. ]);
  718. return $validator;
  719. }
  720. protected function validatorWaybillDispatch(Request $request,$id){
  721. $rule=[
  722. 'logistic_id'=>'required_without:order_id|integer',
  723. 'carrier_bill'=>"sometimes|nullable|max:50|unique:waybills,carrier_bill,$id",
  724. 'fee'=>'sometimes|nullable|min:0|numeric|max:999999',
  725. 'other_fee'=>'sometimes|nullable|min:0|numeric|max:999999',
  726. 'charge'=>'sometimes|nullable|min:0|numeric|max:999999',
  727. 'mileage'=>'nullable|numeric|min:0',
  728. 'amount'=>'nullable|numeric|min:0',
  729. 'amount_unit_id'=>'required',
  730. 'origination_city_id'=>'sometimes|required|integer',
  731. 'destination_city_id'=>'sometimes|required_without:order_id|integer',
  732. 'warehouse_weight_other'=>'sometimes|nullable|min:0|numeric|max:999999',
  733. 'warehouse_weight_unit_id_other'=>'sometimes|required_with:warehouse_weight_other|nullable|integer',
  734. 'pick_up_fee'=>'sometimes|nullable|min:0|numeric|max:999999',
  735. 'warehouse_weight'=>'sometimes|nullable|min:0|numeric|max:999999',
  736. 'warehouse_weight_unit_id'=>'sometimes|required_with:warehouse_weight|nullable|integer',
  737. 'carrier_weight'=>'sometimes|nullable|min:0|numeric|max:999999',
  738. 'carrier_weight_unit_id'=>'sometimes|required_with:carrier_weight',
  739. 'carrier_weight_other'=>'sometimes|nullable|min:0|numeric|max:999999',
  740. 'carrier_weight_unit_id_other'=>'sometimes|required_with:carrier_weight_other',
  741. ];
  742. if ($request->type == '专线'){
  743. $rule['origination_city_id']='required|integer';
  744. $rule['destination_city_id']='required_without:order_id|integer';
  745. }
  746. $validator=Validator::make($request->input(),$rule,[
  747. 'required'=>':attribute 为必填项',
  748. 'required_without'=>':attribute 为必填项',
  749. 'alpha_num'=>':attribute 应为字母或数字',
  750. 'max'=>':attribute 字符过多或输入值过大',
  751. 'min'=>':attribute 不得为负',
  752. 'numeric'=>':attribute 应为数字',
  753. 'unique'=>':attribute 已存在',
  754. 'required_with'=>':attribute 未填',
  755. 'integer'=>':attribute 必须为数字',
  756. ],[
  757. 'logistic_id'=>'承运商',
  758. 'carrier_bill'=>'承运商单号',
  759. 'fee'=>'运费',
  760. 'other_fee'=>'其他费用',
  761. 'charge'=>'收费',
  762. 'mileage'=>'里程数',
  763. 'amount'=>'计数',
  764. 'amount_unit_id'=>'计数单位',
  765. 'warehouse_weight'=>'仓库计数(抛)',
  766. 'carrier_weight'=>'承运商计数(抛)',
  767. 'pick_up_fee'=>'提货费',
  768. 'destination_city_id'=>'目的市',
  769. 'carrier_weight_unit_id'=>'承运商计数单位',
  770. 'warehouse_weight_unit_id'=>'仓库计数单位',
  771. 'warehouse_weight_other'=>'仓库计数二',
  772. 'carrier_weight_other'=>'承运商计数二',
  773. 'warehouse_weight_unit_id_other'=>'仓库技数单位二',
  774. 'carrier_weight_unit_id_other'=>'承运商计数单位二',
  775. ]);
  776. return $validator;
  777. }
  778. public function addCounty(){
  779. $name = app("RegionService")->formatName(request("name"),2);
  780. if (!$name)$this->error("非法参数");
  781. $region = Region::query()->firstOrCreate(["name"=>$name,"type"=>2,"parent_id"=>request("province")]);
  782. $this->success($region);
  783. }
  784. // 运单删除 软删除
  785. public function destroy(int $id){
  786. if(!GAte::allows('运输管理-运单-删除')){return['success'=>0,'status'=>'没有权限'];}
  787. if(is_null($id)){return ['success'=>'0','status'=>'传入id为空'];}
  788. $result = Waybill::where('id',$id)->delete();
  789. return ['success'=>$result,'status'=>$result];
  790. }
  791. // 回收站
  792. public function recycle(Request $request){
  793. if(!Gate::allows('运输管理-运单-删除')){return redirect('/');}
  794. $paginate = $request->input('paginate')??50;
  795. /** @var Collection $waybills */
  796. $waybills = Waybill::query()->with(['owner','order.owner','logistic','amountUnit','warehouseWeightUnit','carrierWeightUnit',
  797. 'warehouseWeightUnitOther','carrierWeightUnitOther','carType','waybillAuditLogs' => function ($query) {
  798. /** @var Builder $query */
  799. $query->with('user');
  800. }])->orderBy('deleted_at', 'DESC')->withTrashed()->whereNotNull('deleted_at')->paginate(50);
  801. $total = $waybills->count();
  802. $paginateParams = [];
  803. $paginateParams['paginate'] = $paginate;
  804. return view('transport.waybill.recycle',compact('waybills','total','paginateParams'));
  805. }
  806. // 软删除恢复
  807. public function apiRestoreSelected(Request $request){
  808. if(!Gate::allows('运输管理-运单-删除')){return ['success'=>'false','fail_info'=>'没有权限'];}
  809. $ids = $request->input('ids')??'';
  810. if($ids == ''){return ['success'=>'false','fail_info'=>'没有可恢复对象'];}
  811. $waybills = Waybill::withTrashed()->whereIn('id',$ids)->get();
  812. $waybills->each(function (Waybill $waybill){
  813. $waybill->restore();
  814. });
  815. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  816. return ['success'=>'true','waybills'=>$waybills];
  817. }
  818. // 修改运费
  819. public function changeFee(Request $request){
  820. if(!Gate::allows('运输管理-运单-运费')){return ['success'=>'false','fail_info'=>'没有权限'];}
  821. $wayBillId = $request->input('id');
  822. $waybillFee = $request->input('fee');
  823. if(is_null($wayBillId) or is_null($waybillFee)){
  824. return ['success'=>'false','fail_info'=>'参数异常'];
  825. }
  826. $result = Waybill::where('id',$wayBillId)->update(['fee'=>$waybillFee]);
  827. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  828. return ['success'=>$result,'status'=>$result];
  829. }
  830. // 修改运输收费
  831. public function changeCharge(Request $request){
  832. if(!Gate::allows('运输管理-运单-运单编辑')){return ['success'=>'false','fail_info'=>'没有权限'];}
  833. $wayBillId = $request->id;
  834. $waybillCharge = $request->input('charge');
  835. if(is_null($wayBillId) or is_null($waybillCharge)){
  836. return ['success'=>'false','fail_info'=>'参数异常'];
  837. }
  838. $result = Waybill::where('id',$wayBillId)->update(['charge'=>$waybillCharge]);
  839. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  840. return ['success'=>$result,'status'=>$result];
  841. }
  842. // 置顶
  843. public function waybillOnTop(Request $request){
  844. $id = $request->input('id');
  845. $detail = $request->input('detail');
  846. if(!Gate::allows('运输管理-运单-置顶')){return ['success'=>'false','fail_info'=>'没有权限'];}
  847. if(is_null($id)){
  848. return ['success'=>'false','fail_info'=>'传参错误'];
  849. }
  850. $wayontop = WaybillOnTop::withTrashed()->where('waybill_id',$id);
  851. if(count($wayontop->get()) == 0){
  852. $wayontop = WaybillOnTop::create(['waybill_id'=>$id,'remark'=>$detail]);
  853. $result = $wayontop->save();
  854. }else{
  855. $result = WaybillOnTop::withTrashed()->where('waybill_id',$id)->restore();
  856. }
  857. return ['success'=>$result,'status'=>$result];
  858. }
  859. // 取消置顶
  860. public function cancelOnTop(Request $request){
  861. $id = $request->input('id');
  862. if(!Gate::allows('运输管理-运单-置顶')){return ['success'=>'false','fail_info'=>'没有权限'];}
  863. if(is_null($id)){
  864. return ['success'=>'false','fail_info'=>'传参错误'];
  865. }
  866. $result = WaybillOnTop::where('waybill_id',$id)->forceDelete();
  867. return ['success'=>$result,'status'=>$result];
  868. }
  869. //同步刷新仓库计重
  870. public function refreshWaveHouseWeight(Request $request){
  871. $wms_bill_number=$request->input('wms_bill_number');
  872. if(is_null($wms_bill_number)) return ['success'=>false,'fail_info'=>'传参错误'];
  873. $waybills=DB::connection('oracle')->table('DOC_ORDER_DETAILS')->where('orderno',$wms_bill_number)->get();
  874. if($waybills->isEmpty()) return ['success'=>false,'fail_info'=>'传参错误'];
  875. $warehouseWeight=0;
  876. foreach ($waybills as $waybill){
  877. if ($waybill->grossweight) $warehouseWeight += $waybill->grossweight;
  878. if (!$waybill->grossweight&& $waybill->netweight) $warehouseWeight +=$waybill->netweight;
  879. }
  880. $warehouseWeight=round($warehouseWeight,2);
  881. $waybill=Waybill::where('wms_bill_number',$wms_bill_number)->first();
  882. if ($warehouseWeight!=0){
  883. if ($waybill['warehouse_weight_other']!=$warehouseWeight){
  884. $waybill['warehouse_weight_other']=$warehouseWeight;
  885. $waybill->update();
  886. app('LogService')->log(__METHOD__,'刷新仓库计重'.__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  887. }
  888. }else{
  889. $warehouseWeight=$waybill['warehouse_weight_other'];
  890. }
  891. return ['success'=>true,'warehouseWeight'=>$warehouseWeight];
  892. }
  893. //寻找订单
  894. public function seekOrder()
  895. {
  896. $this->gate("运输管理");
  897. $code = request("code");
  898. if (!$code)$this->error("暂无绑定订单");
  899. $order = app("OrderService")->first(["code"=>$code]);
  900. if (!$order)$this->error("暂无绑定订单");
  901. $this->success($order);
  902. }
  903. }