WaybillController.php 47 KB

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