WaybillController.php 48 KB

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