WaybillController.php 47 KB

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