WaybillController.php 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  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\LogisticService;
  7. use App\Services\OwnerService;
  8. use App\Services\UnitService;
  9. use App\Services\WaybillPayoffService;
  10. use App\Services\WaybillPriceModelService;
  11. use App\Services\WaybillService;
  12. use App\UploadFile;
  13. use App\WaybillAuditLog;
  14. use App\WaybillOnTop;
  15. use App\WaybillPriceModel;
  16. use App\City;
  17. use App\Unit;
  18. use App\Waybill;
  19. use App\WaybillPayoff;
  20. use App\WaybillFinancialExcepted;
  21. use App\WaybillFinancialSnapshot;
  22. use Carbon\Carbon;
  23. use Exception;
  24. use Illuminate\Database\Eloquent\Builder;
  25. use Illuminate\Database\Eloquent\Collection;
  26. use Illuminate\Http\Request;
  27. use Illuminate\Support\Facades\Auth;
  28. use Illuminate\Support\Facades\DB;
  29. use Illuminate\Support\Facades\Gate;
  30. use Illuminate\Support\Facades\Http;
  31. use Illuminate\Support\Facades\Storage;
  32. use Illuminate\Support\Facades\Validator;
  33. use Intervention\Image\Facades\Image;
  34. use Ramsey\Uuid\Uuid;
  35. class WaybillController extends Controller
  36. {
  37. use AsyncResponse;
  38. public function __construct()
  39. {
  40. app()->singleton('waybillService',WaybillService::class);
  41. }
  42. /**
  43. * @param Request $request
  44. * @param OwnerService $ownerService
  45. * @param LogisticService $logisticService
  46. * @return void
  47. */
  48. public function index(Request $request,OwnerService $ownerService,LogisticService $logisticService)
  49. {
  50. if(!Gate::allows('运输管理-查询')){ return view("waybill.authorityMenu"); }
  51. $paginateParams = $request->input();
  52. $waybills=app('waybillService')->paginate($request->input());
  53. return view('waybill.index', [
  54. 'waybills' => $waybills,
  55. 'logistics' => $logisticService->getSelection(["id","name"],"物流"),
  56. 'owners' => $ownerService->getIntersectPermitting(),
  57. 'paginateParams'=>$paginateParams,
  58. 'uriType'=>$request->uriType??'']);
  59. }
  60. public function create(Request $request,OwnerService $ownerService)
  61. {
  62. if(!Gate::allows('运输管理-录入')){ return redirect(url('/')); }
  63. $type=$request->type ?? "";
  64. if ($type==='ZF')$type='直发车';
  65. if ($type==='ZX')$type='专线';
  66. return view('waybill.create',['owners'=>$ownerService->getIntersectPermitting(),'type'=>$type]);
  67. }
  68. public function store(Request $request)
  69. {
  70. if(!Gate::allows('运输管理-录入')){ return redirect(url('/')); }
  71. $this->validatorWaybill($request,false)->validate();
  72. /** @var WaybillService */
  73. $waybill=app('waybillService')->store($request);
  74. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  75. return redirect('waybill/index')->with('successTip','新运单“'.$waybill->waybill_number.'”录入成功');
  76. }
  77. public function edit($id,LogisticService $logisticService,CarTypeService $carTypeService,UnitService $unitService)
  78. {
  79. if(!Gate::allows('运输管理-编辑')){ return redirect(url('/')); }
  80. $waybill = app('waybillService')->find($id);
  81. if ($waybill->order_id){
  82. /** @var Waybill $waybill */
  83. $waybill->load("order.owner");
  84. $waybill->destination_city_id = app("RegionService")->getCity($waybill->order->city ?? '',$waybill->order->province ?? '');
  85. }else{
  86. /** @var Waybill $waybill */
  87. $waybill->load("owner");
  88. }
  89. /** @var \stdClass $waybill */
  90. if (!$waybill)return view("exception.default",["code"=>"500","message"=>"数据已被删除或丢失"]);
  91. if ($waybill->deliver_at){
  92. $waybill->deliver_at_date=Carbon::parse($waybill->deliver_at)->format('Y-m-d');
  93. $waybill->deliver_at_time=Carbon::parse($waybill->deliver_at)->format('H:i:s');
  94. }
  95. $cities=app("RegionService")->getSelection(2);
  96. $units=$unitService->getSelection();
  97. $carTypes=$carTypeService->getSelection();
  98. return view('waybill/edit',['waybill'=>$waybill,'logistics'=>$logisticService->getSelection(["id","name"],"物流"),'cities'=>$cities,'units'=>$units,'carTypes'=>$carTypes]);
  99. }
  100. public function update(Request $request, $id,WaybillPriceModelService $waybillPriceModelService,
  101. LogisticService $logisticService,WaybillPayoffService $waybillPayoffService)
  102. {
  103. if(!Gate::allows('运输管理-调度')){ return redirect(url('/')); }
  104. if (!$request->warehouse_weight && $request->warehouse_weight_unit_id){
  105. $request->offsetUnset('warehouse_weight_unit_id');
  106. }
  107. if (!$request->warehouse_weight_other && $request->warehouse_weight_unit_id_other){
  108. $request->offsetUnset('warehouse_weight_unit_id_other');
  109. }
  110. if (!$request->carrier_weight && $request->carrier_weight_unit_id){
  111. $request->offsetUnset('carrier_weight_unit_id');
  112. }
  113. if (!$request->carrier_weight_other && $request->carrier_weight_unit_id_other){
  114. $request->offsetUnset('carrier_weight_unit_id_other');
  115. }
  116. $this->validatorWaybillDispatch($request,$id)->validate();
  117. $waybillPayoffParam = [];
  118. $waybillPayoffParam['total_receivable']=0;
  119. /** @var WaybillService */
  120. $waybill = app('waybillService')->update($request,$id);
  121. if ($waybill->type=="直发车"){
  122. if ($waybill->charge)$waybillPayoffParam['total_receivable'] = ($waybill->charge);
  123. elseif ($waybill->collect_fee)$waybillPayoffParam['total_receivable'] = ($waybill->collect_fee);
  124. $waybillPayoffParam['total_expense'] = ($waybill->fee)+($waybill->other_fee)-($waybill->collect_fee);
  125. }else {
  126. $waybillPriceModel_id=$request->input('waybillPriceModel');
  127. if ($waybillPriceModel_id){
  128. $carrier_weight=$request->input('carrier_weight');
  129. $waybillPriceModel=$waybillPriceModelService->find($waybillPriceModel_id);
  130. $logistic=$logisticService->find($waybill->order->logistic_id ?? $waybill->logistic_id);
  131. if ($carrier_weight<$waybillPriceModel->initial_weight){
  132. $fee=(($waybillPriceModel->unit_price)*($waybillPriceModel->initial_weight))+$logistic->delivery_fee;
  133. }else{
  134. $fee=(($waybillPriceModel->unit_price)*$carrier_weight)+$logistic->delivery_fee;
  135. }
  136. if ($waybillPriceModel->base_fee&&$fee<$waybillPriceModel->base_fee){
  137. $fee=$waybillPriceModel->base_fee;
  138. }
  139. $waybill->fee=$fee;
  140. $waybill->waybill_price_model_id=$waybillPriceModel_id;
  141. }
  142. $waybill->save();
  143. if ($waybill->charge)$waybillPayoffParam['total_receivable'] = ($waybill->charge);
  144. elseif($waybill->collect_fee) {
  145. $waybillPayoffParam['total_receivable'] = $waybill->collect_fee;
  146. }
  147. $waybillPayoffParam['total_expense'] = ($waybill->pick_up_fee)+($waybill->other_fee)+($waybill->fee);
  148. }
  149. if ($waybillPayoffParam['total_receivable'] > 0){
  150. $waybillPayoffParam['waybill_id'] = $id;
  151. $waybillPayoffParam['gross_margin'] = $waybillPayoffParam['total_receivable'] - $waybillPayoffParam['total_expense'];
  152. $waybillPayoffParam['gross_profit_rate'] = $waybillPayoffParam['gross_margin']/$waybillPayoffParam['total_receivable'];
  153. $waybillPayoffService->updateOrCreate($waybillPayoffParam);
  154. }
  155. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  156. return redirect('waybill/index')->with('successTip','运单“'.$waybill->waybill_number.'”调度成功');
  157. }
  158. public function checkWaybillPriceModel($logistic_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id){
  159. //确保承运商计数与计数单位为一个数组且长度2
  160. if(!$logistic_id)return false;
  161. if(!$destination_city_id)return false;
  162. if(!$carrier_weight)return false;
  163. if(!$carrier_weight_unit_id)return false;
  164. //多个计数标准,计算价格,取最贵
  165. if ($carrier_weight[0]&&$carrier_weight[1]&&$carrier_weight_unit_id[0]&&$carrier_weight_unit_id[1]){
  166. //城市价格区间不为空
  167. $waybillPriceModelOne=WaybillPriceModel::query()->where('logistic_id',$logistic_id)->where('city_id',$destination_city_id)
  168. ->where('range_min','<',$carrier_weight[0])->where('range_max','>=',$carrier_weight[0])
  169. ->where('unit_id',$carrier_weight_unit_id[0])->first();
  170. $waybillPriceModelTwo=WaybillPriceModel::query()->where('logistic_id',$logistic_id)->where('city_id',$destination_city_id)
  171. ->where('range_min','<',$carrier_weight[1])->where('range_max','>=',$carrier_weight[1])
  172. ->where('unit_id',$carrier_weight_unit_id[1])->first();
  173. if ($waybillPriceModelOne&&$waybillPriceModelTwo){
  174. if ($waybillPriceModelOne->unit_price*$carrier_weight[0]>=$waybillPriceModelTwo->unit_price*$carrier_weight[1]){
  175. return $waybillPriceModelOne->id;
  176. }else{
  177. return $waybillPriceModelTwo->id;
  178. }
  179. }
  180. if ($waybillPriceModelOne)return $waybillPriceModelOne->id;
  181. if ($waybillPriceModelTwo)return $waybillPriceModelTwo->id;
  182. //价格区间为空
  183. $waybillPriceModelRangeOne=WaybillPriceModel::query()->whereRaw('logistic_id = ? AND city_id = ? AND unit_id = ? AND range_max IS NULL',[$logistic_id,$destination_city_id,$carrier_weight_unit_id[0]])->first();
  184. $waybillPriceModelRangeTwo=WaybillPriceModel::query()->whereRaw('logistic_id = ? AND city_id = ? AND unit_id = ? AND range_max IS NULL',[$logistic_id,$destination_city_id,$carrier_weight_unit_id[1]])->first();
  185. if ($waybillPriceModelRangeOne&&$waybillPriceModelRangeTwo){
  186. if ($waybillPriceModelRangeOne->unit_price*$carrier_weight[0]>=$waybillPriceModelRangeTwo->unit_price*$carrier_weight[1]){
  187. return $waybillPriceModelRangeOne->id;
  188. }else{
  189. return $waybillPriceModelRangeTwo->id;
  190. }
  191. }
  192. if ($waybillPriceModelRangeOne)return $waybillPriceModelRangeOne->id;
  193. if ($waybillPriceModelRangeTwo)return $waybillPriceModelRangeTwo->id;
  194. //城市为空
  195. $city=Region::query()->where('id',$destination_city_id)->select('parent_id')->first();
  196. $waybillPriceModelProvinceOne=WaybillPriceModel::query()->whereRaw('logistic_id = ? AND province_id = ? AND unit_id = ? AND range_max >= ? AND range_min < ? AND city_id IS NULL',
  197. [$logistic_id,$city->parent_id ?? 0,$carrier_weight_unit_id[0],$carrier_weight[0],$carrier_weight[0]])->first();
  198. $waybillPriceModelProvinceTwo=WaybillPriceModel::query()->whereRaw('logistic_id = ? AND province_id = ? AND unit_id = ? AND range_max >= ? AND range_min < ? AND city_id IS NULL',
  199. [$logistic_id,$city->parent_id ?? 0,$carrier_weight_unit_id[1],$carrier_weight[1],$carrier_weight[1]])->first();
  200. if ($waybillPriceModelProvinceOne&&$waybillPriceModelProvinceTwo){
  201. if ($waybillPriceModelProvinceOne->unit_price*$carrier_weight[0]>=$waybillPriceModelProvinceTwo->unit_price*$carrier_weight[1]){
  202. return $waybillPriceModelProvinceOne->id;
  203. }else{
  204. return $waybillPriceModelProvinceTwo->id;
  205. }
  206. }
  207. if ($waybillPriceModelProvinceOne)return $waybillPriceModelProvinceOne->id;
  208. if ($waybillPriceModelProvinceTwo)return $waybillPriceModelProvinceTwo->id;
  209. //城市价格区间都为空
  210. $waybillPriceModelProvinceRangeOne=WaybillPriceModel::query()->whereRaw('logistic_id = ? AND province_id = ? AND unit_id = ? AND range_max IS NULL AND city_id IS NULL',
  211. [$logistic_id,$city->parent_id ?? 0,$carrier_weight_unit_id[0]])->first();
  212. $waybillPriceModelProvinceRangeTwo=WaybillPriceModel::query()->whereRaw('logistic_id = ? AND province_id = ? AND unit_id = ? AND range_max IS NULL AND city_id IS NULL',
  213. [$logistic_id,$city->parent_id ?? 0,$carrier_weight_unit_id[1]])->first();
  214. if ($waybillPriceModelProvinceRangeOne&&$waybillPriceModelProvinceRangeTwo){
  215. if ($waybillPriceModelProvinceRangeOne->unit_price*$carrier_weight[0]>=$waybillPriceModelProvinceRangeTwo->unit_price*$carrier_weight[1]){
  216. return $waybillPriceModelProvinceRangeOne->id;
  217. }else{
  218. return $waybillPriceModelProvinceRangeOne->id;
  219. }
  220. }
  221. if ($waybillPriceModelProvinceRangeOne)return $waybillPriceModelProvinceRangeOne->id;
  222. if ($waybillPriceModelProvinceRangeTwo)return $waybillPriceModelProvinceRangeTwo->id;
  223. };
  224. for ($i=0;$i<count($carrier_weight);$i++){
  225. if ($carrier_weight[$i]&&$carrier_weight_unit_id[$i]){
  226. //城市价格区间不为空
  227. $waybillPriceModel=WaybillPriceModel::query()->where('logistic_id',$logistic_id)->where('city_id',$destination_city_id)
  228. ->where('range_min','<',$carrier_weight[$i])->where('range_max','>=',$carrier_weight[$i])
  229. ->where('unit_id',$carrier_weight_unit_id[$i])->first();
  230. if($waybillPriceModel)return $waybillPriceModel->id;
  231. //价格区间为空
  232. $waybillPriceModelRange=WaybillPriceModel::query()->whereRaw('logistic_id = ? AND city_id = ? AND unit_id = ? AND range_max IS NULL',[$logistic_id,$destination_city_id,$carrier_weight_unit_id[$i]])->first();
  233. if ($waybillPriceModelRange){ return $waybillPriceModelRange->id;}
  234. //城市为空
  235. $city=Region::query()->where('id',$destination_city_id)->select('parent_id')->first();
  236. $waybillPriceModelProvince=WaybillPriceModel::query()->whereRaw('logistic_id = ? AND province_id = ? AND unit_id = ? AND range_max >= ? AND range_min < ? AND city_id IS NULL',
  237. [$logistic_id,$city->parent_id ?? 0,$carrier_weight_unit_id[$i],$carrier_weight[$i],$carrier_weight[$i]])->first();
  238. if ($waybillPriceModelProvince){return $waybillPriceModelProvince->id;}
  239. //城市价格区间都为空
  240. $waybillPriceModelProvinceRange=WaybillPriceModel::query()->whereRaw('logistic_id = ? AND province_id = ? AND unit_id = ? AND range_max IS NULL AND city_id IS NULL',
  241. [$logistic_id,$city->parent_id ?? 0,$carrier_weight_unit_id[$i]])->first();
  242. if ($waybillPriceModelProvinceRange){return $waybillPriceModelProvinceRange->id;}
  243. }
  244. }
  245. return false;
  246. }
  247. /*三层条件:无优先级,找到第一个直接返回
  248. * 无论是否为KG||T,计数单位一为KG,计数单位一为T,计数单位二为KG,计数单位二为T
  249. * 计数一与计数二同时存在取最贵价格:
  250. * 计数一存在,二不存在:
  251. * 计数二存在,一不存在:
  252. * 城市价格区间不为空,城市价格区间都为空,城市为空,价格区间为空
  253. * */
  254. public function isWaybillPriceModel(Request $request){
  255. $logistic_id=$request->input('logistic_id');
  256. $destination_city_id=$request->input('destination_city_id');
  257. $carrier_weight=$request->input('carrier_weight');
  258. $carrier_weight_unit_id=$request->input('carrier_weight_unit_id');
  259. $validatorData=["logistic_id"=>$logistic_id,"destination_city_id"=>$destination_city_id,
  260. 'carrier_weight'=>$carrier_weight[0],"carrier_weight_unit_id"=>$carrier_weight_unit_id[0],
  261. "carrier_weight_other"=>$carrier_weight[1],"carrier_weight_unit_id_other"=>$carrier_weight_unit_id[1]];
  262. $errors=Validator::make($validatorData,[
  263. 'logistic_id'=>'required|integer',
  264. 'destination_city_id'=>'required|integer',
  265. 'carrier_weight'=>'nullable|min:0|numeric|max:999999',
  266. 'carrier_weight_unit_id'=>'required_with:carrier_weight',
  267. 'carrier_weight_other'=>'nullable|min:0|numeric|max:999999',
  268. 'carrier_weight_unit_id_other'=>'required_with:carrier_weight_other',
  269. ],[
  270. 'required'=>':attribute 为必填项',
  271. 'max'=>':attribute 字符过多或输入值过大',
  272. 'min'=>':attribute 不得为负',
  273. 'numeric'=>':attribute 应为数字',
  274. 'unique'=>':attribute 已存在',
  275. 'required_with'=>':attribute 未填',
  276. 'integer'=>':attribute 必须为数字',
  277. ],[
  278. 'carrier_weight'=>'承运商计数(抛)',
  279. 'logistic_id'=>'承运商',
  280. 'destination_city_id'=>'目的市',
  281. 'carrier_weight_unit_id'=>'承运商计数单位',
  282. 'carrier_weight_other'=>'承运商计数二',
  283. 'carrier_weight_unit_id_other'=>'承运商计数单位二',
  284. ])->errors();
  285. if (count($errors)>0)return ['error'=>$errors];
  286. $result=$this->checkWaybillPriceModel($logistic_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
  287. if (!$result){
  288. //单位为kg,T时
  289. $unitKG=Unit::query()->where('name','kg')->first();
  290. $unitT=Unit::query()->where('name','T')->first();
  291. if ($carrier_weight_unit_id[0]==$unitKG->id){
  292. $carrier_weight_unit_id[0]=$unitT->id;
  293. $carrier_weight[0]=$carrier_weight[0]/1000;
  294. $result=$this->checkWaybillPriceModel($logistic_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
  295. if ($result)return ['success'=>$result];
  296. }
  297. if ($carrier_weight_unit_id[1]==$unitKG->id){
  298. $carrier_weight_unit_id[1]=$unitT->id;
  299. $carrier_weight[1]=$carrier_weight[1]/1000;
  300. $result=$this->checkWaybillPriceModel($logistic_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
  301. if ($result)return ['success'=>$result];
  302. }
  303. if ($carrier_weight_unit_id[0]==$unitT->id){
  304. $carrier_weight_unit_id[0]=$unitKG->id;
  305. $carrier_weight[0]=$carrier_weight[0]*1000;
  306. $result=$this->checkWaybillPriceModel($logistic_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
  307. if ($result)return ['success'=>$result];
  308. }
  309. if ($carrier_weight_unit_id[1]==$unitT->id){
  310. $carrier_weight_unit_id[1]=$unitKG->id;
  311. $carrier_weight[1]=$carrier_weight[1]*1000;
  312. $result=$this->checkWaybillPriceModel($logistic_id,$destination_city_id,$carrier_weight,$carrier_weight_unit_id);
  313. if ($result)return ['success'=>$result];
  314. }
  315. }
  316. return ['success'=>$result];
  317. }
  318. public function waybillUpdate(Request $request, $id){
  319. if(!Gate::allows('运输管理-编辑')){ return redirect(url('/')); }
  320. $this->validatorWaybill($request,$id)->validate();
  321. $data=$request->input();
  322. $waybill=app('waybillService')->find($id);
  323. $waybill->fill($data);
  324. if ($waybill->save()){
  325. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
  326. return redirect('waybill/index')->with('successTip','运单“'.$waybill->waybill_number.'”修改成功');
  327. }
  328. }
  329. public function waybillAudit(Request $request){
  330. if(!Gate::allows('运输管理-运单审核')){ return redirect(url('/')); }
  331. $id=$request->input('id');
  332. $waybill=app('waybillService')->find($id);
  333. $isAudit=WaybillAuditLog::whereRaw('waybill_id = ? and audit_stage = ?',[$id,"运单阶段"])->first();
  334. if (empty($isAudit)){
  335. $waybillAuditLog=new WaybillAuditLog([
  336. 'waybill_id'=>$id,
  337. 'audit_stage'=>'运单阶段',
  338. 'user_id'=>Auth::id(),
  339. ]);
  340. $waybillAuditLog->save();
  341. $waybillAuditLog['user']=Auth::user();
  342. $waybill->status='已审核';
  343. $result=$waybill->save();
  344. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
  345. return ['success'=>$result,'status'=>$waybill->status,'waybillAuditLog'=>$waybillAuditLog];
  346. }
  347. return ['exception'=>'请勿重复审核!'];
  348. }
  349. public function waybillEdit($id){
  350. if(!Gate::allows('运输管理-编辑')){ return redirect(url('/')); }
  351. $waybill=app('waybillService')->find($id);
  352. $owners=app("OwnerService")->getIntersectPermitting();
  353. return view('waybill.waybillEdit',['waybill'=>$waybill,'owners'=>$owners]);
  354. }
  355. public function waybillRetreatAudit(Request $request){
  356. if(!Gate::allows('运输管理-调度')){ return redirect(url('/')); }
  357. $id=$request->input('id');
  358. $waybill=app('waybillService')->find($id);
  359. WaybillAuditLog::query()->whereRaw('waybill_id = ? and audit_stage = ?',[$id,"运单阶段"])->delete();
  360. $waybill->status='待重审';
  361. $result=$waybill->save();
  362. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($waybill),Auth::user()['id']);
  363. return ['success'=>$result,'status'=>$waybill->status];
  364. }
  365. public function waybillEndAudit(Request $request){
  366. if(!Gate::allows('运输管理-调度审核')){ return redirect(url('/')); }
  367. $id=$request->input('id');
  368. $waybill=Waybill::query()->with(["owner","logistic","originationCity","destinationCity","carType",'priceModel',"amountUnit",
  369. "warehouseWeightUnit","carrierWeightUnit","warehouseWeightUnitOther","carrierWeightUnitOther"])->find($id);
  370. if (!$waybill->charge&&!$waybill->collect_fee)return ['exception'=>'收费或到付费用未填!'];
  371. if ($waybill->charge==0&&$waybill->collect_fee==0)return ['exception'=>'收费与到付费用都为0!'];
  372. if ($waybill->type=='专线'){
  373. if (!$waybill->carrier_weight_other||$waybill->carrier_weight_other==0)return ['exception'=>'承运商计重未填或为0!'];
  374. if (!$waybill->carrier_weight_unit_id_other)return ['exception'=>'承运商计重单位未选!'];
  375. }
  376. $isAudit=WaybillAuditLog::query()->whereRaw('waybill_id = ? and audit_stage = ?',[$id,"调度阶段"])->first();
  377. if (empty($isAudit)){
  378. $waybillAuditLog=new WaybillAuditLog([
  379. 'waybill_id'=>$id,
  380. 'audit_stage'=>'调度阶段',
  381. 'user_id'=>Auth::id(),
  382. ]);
  383. $waybillAuditLog->save();
  384. $waybillAuditLog['user']=Auth::user();
  385. if ($waybill->waybill_price_model_id||$waybill->type=='直发车'){
  386. $waybill->status='已完结';
  387. $result=$waybill->save();
  388. $waybillPayoff=WaybillPayoff::query()->where('waybill_id','=',$id)->first();
  389. $waybillPayoffJson=json_encode($this->createReportData($waybill,$waybillPayoff),JSON_UNESCAPED_UNICODE);
  390. WaybillFinancialSnapshot::query()->create([
  391. 'waybill_id'=>$id,
  392. 'json_content'=>$waybillPayoffJson,
  393. ]);
  394. }else{
  395. $waybill->status='无模型';
  396. $result=$waybill->save();
  397. $waybillPayoff=WaybillPayoff::query()->where('waybill_id','=',$id)->first();
  398. if ($waybillPayoff){
  399. $waybillPayoffJson=json_encode($this->createReportData($waybill,$waybillPayoff),JSON_UNESCAPED_UNICODE);
  400. WaybillFinancialExcepted::query()->create([
  401. 'waybill_id'=>$id,
  402. 'json_content'=>$waybillPayoffJson,
  403. ]);
  404. }
  405. }
  406. app('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",'order.logistic']);
  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->order->logistic->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(Request $request){
  585. if(!Gate::allows('运输管理-查询')){ return '没有权限'; }
  586. if ($request->checkAllSign){
  587. $param = $request->input();
  588. unset($param['checkAllSign']);
  589. $sql = app('waybillService')->getSql($param);
  590. }else $sql = app('waybillService')->getSql(['id'=>$request->data]);
  591. $post = Http::post(config('go.export.url'),['type'=>'waybill','sql'=>$sql]);
  592. if ($post->status() == 500){
  593. throw new Exception($post->header("Msg"));
  594. }
  595. return response($post,200, [
  596. "Content-type"=>"application/octet-stream",
  597. "Content-Disposition"=>"attachment; filename=运单列表-".date('ymdHis').'.xlsx',
  598. ]);
  599. }
  600. public function deliveringExport(Request $request){
  601. if ($request->checkAllSign){
  602. $param = $request->input();
  603. unset($param['checkAllSign']);
  604. $sql = app('waybillService')->getDeliveringSql($param);
  605. }else{
  606. $sql = app('waybillService')->getDeliveringSql(['id'=>$request->data]);
  607. }
  608. $post = Http::post(config('go.export.url'),['type'=>'waybillDelivering','sql'=>$sql]);
  609. if ($post->status() == 500){
  610. throw new Exception($post->header("Msg"));
  611. }
  612. return response($post,200, [
  613. "Content-type"=>"application/octet-stream",
  614. "Content-Disposition"=>"attachment; filename=发运列表-".date('ymdHis').'.xlsx',
  615. ]);
  616. }
  617. //发运
  618. public function delivering(Request $request){
  619. if (!Auth::user())return view('exception.login');
  620. $waybills= app('waybillService')->paginate($request->input());
  621. if (!Auth::user()->isSuperAdmin()){
  622. $carriersUsers=DB::table('carrier_user')->where('user_id',Auth::id())->get();
  623. $carrierIds=array_column($carriersUsers->toArray(),'logistic_id');
  624. $waybills=$waybills->whereIn("logistic_id",$carrierIds);
  625. }
  626. return view('waybill.delivering',compact('waybills'));
  627. }
  628. //承运商提交
  629. public function storeCarrierBill(Request $request){
  630. $errors=Validator::make($request->input(),[
  631. 'id'=>'required|integer',
  632. 'carrier_bill'=>'required',
  633. 'inquire_tel'=>'nullable',
  634. 'amount'=>'nullable|integer',
  635. 'carrier_weight'=>'required_without:carrier_weight_other|nullable|numeric',
  636. 'carrier_weight_other'=>'required_without:carrier_weight|nullable|numeric',
  637. ],[
  638. 'required'=>':attribute 为必填项',
  639. 'integer'=>':attribute 应为整数',
  640. 'numeric'=>':attribute 应为数字',
  641. 'required_with'=>':attribute 重量与体积至少存在一项',
  642. ],[
  643. 'carrier_bill'=>'专线运单号',
  644. 'inquire_tel'=>'查件电话',
  645. 'amount'=>'件数',
  646. 'carrier_weight'=>'体积',
  647. 'carrier_weight_other'=>'重量',
  648. ])->errors();
  649. if (count($errors)>0)return ["errors"=>$errors];
  650. $waybill=Waybill::query()->find($request->input('id'));
  651. if (!$waybill)return ["error"=>"未找到该运单!"];
  652. $waybill->fill($request->input());
  653. $waybill->update();
  654. return $waybill;
  655. }
  656. protected function validatorWaybill(Request $request,$id){
  657. if ($id){$wms_bill_number=$id;};
  658. $validator=Validator::make($request->input(),[
  659. 'owner_id'=>'required_without:order_id',
  660. 'wms_bill_number'=>['nullable','max:50',isset($wms_bill_number)?"unique:waybills,wms_bill_number,$wms_bill_number":'unique:waybills,wms_bill_number'],
  661. 'origination'=>'required|max:255',
  662. 'destination'=>'required_without:order_id|max:255',
  663. 'recipient'=>'required_without:order_id|max:50',
  664. 'recipient_mobile'=>['required_without:order_id','regex:/^(\d{7,11})|(1[3|4|5|7|8][0-9]\d{4,8})$/'],
  665. 'charge'=>'nullable|min:0|max:999999|numeric',
  666. 'collect_fee'=>'nullable|min:0|numeric',
  667. ],[
  668. 'required'=>':attribute 为必填项',
  669. 'required_without'=>':attribute 为必填项',
  670. 'alpha_num'=>':attribute 应为字母或数字',
  671. 'max'=>':attribute 字符过多或输入值过大',
  672. 'regex'=>':attribute 输入有误',
  673. 'integer'=>':attribute 应为整数',
  674. 'min'=>':attribute 不得为负',
  675. 'numeric'=>':attribute 应为数字',
  676. 'unique'=>':attribute 已存在',
  677. ],[
  678. 'owner_id'=>'货主',
  679. 'wms_bill_number'=>'WMS单号',
  680. 'origination'=>'始发地',
  681. 'destination'=>'目的地',
  682. 'recipient'=>'收件人',
  683. 'recipient_mobile'=>'收件人电话',
  684. 'charge'=>'收费',
  685. 'collect_fee'=>'到付金额',
  686. ]);
  687. return $validator;
  688. }
  689. protected function validatorWaybillDispatch(Request $request,$id){
  690. $rule=[
  691. 'logistic_id'=>'required_without:order_id|integer',
  692. 'carrier_bill'=>"sometimes|nullable|max:50|unique:waybills,carrier_bill,$id",
  693. 'fee'=>'sometimes|nullable|min:0|numeric|max:999999',
  694. 'other_fee'=>'sometimes|nullable|min:0|numeric|max:999999',
  695. 'charge'=>'sometimes|nullable|min:0|numeric|max:999999',
  696. 'mileage'=>'nullable|numeric|min:0',
  697. 'amount'=>'nullable|numeric|min:0',
  698. 'amount_unit_id'=>'required',
  699. 'origination_city_id'=>'sometimes|required|integer',
  700. 'destination_city_id'=>'sometimes|required_without:order_id|integer',
  701. 'warehouse_weight_other'=>'sometimes|nullable|min:0|numeric|max:999999',
  702. 'warehouse_weight_unit_id_other'=>'sometimes|required_with:warehouse_weight_other|nullable|integer',
  703. 'pick_up_fee'=>'sometimes|nullable|min:0|numeric|max:999999',
  704. 'warehouse_weight'=>'sometimes|nullable|min:0|numeric|max:999999',
  705. 'warehouse_weight_unit_id'=>'sometimes|required_with:warehouse_weight|nullable|integer',
  706. 'carrier_weight'=>'sometimes|nullable|min:0|numeric|max:999999',
  707. 'carrier_weight_unit_id'=>'sometimes|required_with:carrier_weight',
  708. 'carrier_weight_other'=>'sometimes|nullable|min:0|numeric|max:999999',
  709. 'carrier_weight_unit_id_other'=>'sometimes|required_with:carrier_weight_other',
  710. ];
  711. if ($request->type == '专线'){
  712. $rule['origination_city_id']='required|integer';
  713. $rule['destination_city_id']='required_without:order_id|integer';
  714. }
  715. $validator=Validator::make($request->input(),$rule,[
  716. 'required'=>':attribute 为必填项',
  717. 'required_without'=>':attribute 为必填项',
  718. 'alpha_num'=>':attribute 应为字母或数字',
  719. 'max'=>':attribute 字符过多或输入值过大',
  720. 'min'=>':attribute 不得为负',
  721. 'numeric'=>':attribute 应为数字',
  722. 'unique'=>':attribute 已存在',
  723. 'required_with'=>':attribute 未填',
  724. 'integer'=>':attribute 必须为数字',
  725. ],[
  726. 'logistic_id'=>'承运商',
  727. 'carrier_bill'=>'承运商单号',
  728. 'fee'=>'运费',
  729. 'other_fee'=>'其他费用',
  730. 'charge'=>'收费',
  731. 'mileage'=>'里程数',
  732. 'amount'=>'计数',
  733. 'amount_unit_id'=>'计数单位',
  734. 'warehouse_weight'=>'仓库计数(抛)',
  735. 'carrier_weight'=>'承运商计数(抛)',
  736. 'pick_up_fee'=>'提货费',
  737. 'destination_city_id'=>'目的市',
  738. 'carrier_weight_unit_id'=>'承运商计数单位',
  739. 'warehouse_weight_unit_id'=>'仓库计数单位',
  740. 'warehouse_weight_other'=>'仓库计数二',
  741. 'carrier_weight_other'=>'承运商计数二',
  742. 'warehouse_weight_unit_id_other'=>'仓库技数单位二',
  743. 'carrier_weight_unit_id_other'=>'承运商计数单位二',
  744. ]);
  745. return $validator;
  746. }
  747. public function addCounty(){
  748. $name = app("RegionService")->formatName(request("name"),2);
  749. if (!$name)$this->error("非法参数");
  750. $region = Region::query()->firstOrCreate(["name"=>$name,"type"=>2]);
  751. $this->success($region);
  752. }
  753. // 运单删除 软删除
  754. public function destroy(int $id){
  755. if(!GAte::allows('运输管理-删除')){return['success'=>0,'status'=>'没有权限'];}
  756. if(is_null($id)){return ['success'=>'0','status'=>'传入id为空'];}
  757. $result = Waybill::where('id',$id)->delete();
  758. return ['success'=>$result,'status'=>$result];
  759. }
  760. // 回收站
  761. public function recycle(Request $request){
  762. if(!Gate::allows('运输管理-删除')){return redirect('/');}
  763. $paginate = $request->input('paginate')??50;
  764. /** @var Collection $waybills */
  765. $waybills = Waybill::query()->with(['owner','order.owner','order.logistic','logistic','amountUnit','warehouseWeightUnit','carrierWeightUnit',
  766. 'warehouseWeightUnitOther','carrierWeightUnitOther','carType','waybillAuditLogs' => function ($query) {
  767. /** @var Builder $query */
  768. $query->with('user');
  769. }])->orderBy('deleted_at', 'DESC')->withTrashed()->whereNotNull('deleted_at')->paginate(50);
  770. $total = $waybills->count();
  771. $paginateParams = [];
  772. $paginateParams['paginate'] = $paginate;
  773. return view('waybill.recycle',compact('waybills','total','paginateParams'));
  774. }
  775. // 软删除恢复
  776. public function apiRestoreSelected(Request $request){
  777. if(!Gate::allows('运输管理-删除')){return ['success'=>'false','fail_info'=>'没有权限'];}
  778. $ids = $request->input('ids')??'';
  779. if($ids == ''){return ['success'=>'false','fail_info'=>'没有可恢复对象'];}
  780. $waybills = Waybill::withTrashed()->whereIn('id',$ids)->get();
  781. $waybills->each(function (Waybill $waybill){
  782. $waybill->restore();
  783. });
  784. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  785. return ['success'=>'true','waybills'=>$waybills];
  786. }
  787. // 修改运费
  788. public function changeFee(Request $request){
  789. if(!Gate::allows('运输管理-运费')){return ['success'=>'false','fail_info'=>'没有权限'];}
  790. $wayBillId = $request->input('id');
  791. $waybillFee = $request->input('fee');
  792. if(is_null($wayBillId) or is_null($waybillFee)){
  793. return ['success'=>'false','fail_info'=>'参数异常'];
  794. }
  795. $result = Waybill::where('id',$wayBillId)->update(['fee'=>$waybillFee]);
  796. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  797. return ['success'=>$result,'status'=>$result];
  798. }
  799. // 修改运输收费
  800. public function changeCharge(Request $request){
  801. if(!Gate::allows('运输管理-运单编辑')){return ['success'=>'false','fail_info'=>'没有权限'];}
  802. $wayBillId = $request->id;
  803. $waybillCharge = $request->input('charge');
  804. if(is_null($wayBillId) or is_null($waybillCharge)){
  805. return ['success'=>'false','fail_info'=>'参数异常'];
  806. }
  807. $result = Waybill::where('id',$wayBillId)->update(['charge'=>$waybillCharge]);
  808. app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  809. return ['success'=>$result,'status'=>$result];
  810. }
  811. // 置顶
  812. public function waybillOnTop(Request $request){
  813. $id = $request->input('id');
  814. $detail = $request->input('detail');
  815. if(!Gate::allows('运输管理-置顶')){return ['success'=>'false','fail_info'=>'没有权限'];}
  816. if(is_null($id)){
  817. return ['success'=>'false','fail_info'=>'传参错误'];
  818. }
  819. $wayontop = WaybillOnTop::withTrashed()->where('waybill_id',$id);
  820. if(count($wayontop->get()) == 0){
  821. $wayontop = WaybillOnTop::create(['waybill_id'=>$id,'remark'=>$detail]);
  822. $result = $wayontop->save();
  823. }else{
  824. $result = WaybillOnTop::withTrashed()->where('waybill_id',$id)->restore();
  825. }
  826. return ['success'=>$result,'status'=>$result];
  827. }
  828. // 取消置顶
  829. public function cancelOnTop(Request $request){
  830. $id = $request->input('id');
  831. if(!Gate::allows('运输管理-置顶')){return ['success'=>'false','fail_info'=>'没有权限'];}
  832. if(is_null($id)){
  833. return ['success'=>'false','fail_info'=>'传参错误'];
  834. }
  835. $result = WaybillOnTop::where('waybill_id',$id)->forceDelete();
  836. return ['success'=>$result,'status'=>$result];
  837. }
  838. //同步刷新仓库计重
  839. public function refreshWaveHouseWeight(Request $request){
  840. $wms_bill_number=$request->input('wms_bill_number');
  841. if(is_null($wms_bill_number)) return ['success'=>false,'fail_info'=>'传参错误'];
  842. $waybills=DB::connection('oracle')->table('DOC_ORDER_DETAILS')->where('orderno',$wms_bill_number)->get();
  843. if($waybills->isEmpty()) return ['success'=>false,'fail_info'=>'传参错误'];
  844. $warehouseWeight=0;
  845. foreach ($waybills as $waybill){
  846. if ($waybill->grossweight) $warehouseWeight += $waybill->grossweight;
  847. if (!$waybill->grossweight&& $waybill->netweight) $warehouseWeight +=$waybill->netweight;
  848. }
  849. $warehouseWeight=round($warehouseWeight,2);
  850. $waybill=Waybill::where('wms_bill_number',$wms_bill_number)->first();
  851. if ($warehouseWeight!=0){
  852. if ($waybill['warehouse_weight_other']!=$warehouseWeight){
  853. $waybill['warehouse_weight_other']=$warehouseWeight;
  854. $waybill->update();
  855. app('LogService')->log(__METHOD__,'刷新仓库计重'.__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  856. }
  857. }else{
  858. $warehouseWeight=$waybill['warehouse_weight_other'];
  859. }
  860. return ['success'=>true,'warehouseWeight'=>$warehouseWeight];
  861. }
  862. //寻找订单
  863. public function seekOrder()
  864. {
  865. $this->gate("运输管理");
  866. $code = request("code");
  867. if (!$code)$this->error("暂无绑定订单");
  868. $order = app("OrderService")->first(["code"=>$code]);
  869. if (!$order)$this->error("暂无绑定订单");
  870. $this->success($order);
  871. }
  872. }