CustomerController.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Components\AsyncResponse;
  4. use App\Owner;
  5. use App\OwnerAreaReport;
  6. use App\OwnerReport;
  7. use App\Services\LogService;
  8. use App\Services\OwnerAreaReportService;
  9. use App\Services\OwnerBillReportService;
  10. use App\Services\OwnerReportService;
  11. use App\Services\OwnerService;
  12. use Exception;
  13. use Illuminate\Database\Eloquent\Builder;
  14. use Illuminate\Http\Request;
  15. use Illuminate\Http\Response;
  16. use Illuminate\Support\Facades\DB;
  17. use Illuminate\Support\Facades\Gate;
  18. use Illuminate\Support\Facades\Http;
  19. use Illuminate\Support\Facades\Validator;
  20. class CustomerController extends Controller
  21. {
  22. use AsyncResponse;
  23. /**
  24. * Display a listing of the resource.
  25. * @param Request $request
  26. * @return Response
  27. */
  28. public function projectReport(Request $request)
  29. {
  30. if(!Gate::allows('客户管理-项目-报表')){ return view('customer.index'); }
  31. $withs = ["ownerBillReport","owner"=>function($query){
  32. /** @var Builder $query */
  33. $query->select("id","name","deleted_at","created_at","customer_id","user_owner_group_id")
  34. ->with(["customer","userOwnerGroup"]);
  35. }];
  36. $ownerGroups = app('UserOwnerGroupService')->getSelection();
  37. $customers = app('CustomerService')->getSelection();
  38. $owners = app('OwnerService')->getIntersectPermitting();
  39. $reports = app("OwnerReportService")->paginate($request->input(),$withs);
  40. $params = $request->input();
  41. return response()->view('customer.project.report',compact("reports","ownerGroups","customers","owners","params"));
  42. }
  43. public function projectReportExport(Request $request)
  44. {
  45. if(!Gate::allows('客户管理-项目-报表')){ return redirect('denied'); }
  46. /** @var OwnerReportService $service */
  47. $service = app('OwnerReportService');
  48. $withs = ["ownerBillReport","owner"=>function($query){
  49. /** @var Builder $query */
  50. $query->select("id","name","deleted_at","created_at","customer_id","user_owner_group_id")
  51. ->with(["customer","userOwnerGroup"]);
  52. }];
  53. if ($request->checkAllSign ?? false){
  54. $params = $request->input();
  55. unset($params['checkAllSign']);
  56. $reports = $service->get($params,$withs);
  57. }else $reports = $service->get(["id"=>$request->data ?? ''],$withs);
  58. $column = ["项目小组","客户","子项目","状态","创建日期","在库时长","结算月","日均单量","结算月上月盘点面积","结算月盘点面积","初始账单金额","确认账单金额","确认日期"];
  59. $list = [];
  60. foreach ($reports as $report){
  61. $list[] = [
  62. $report->owner ? ($report->owner->userOwnerGroup ? $report->owner->userOwnerGroup->name : '') : '',
  63. $report->owner ? ($report->owner->customer ? $report->owner->customer->name : '') : '',
  64. $report->owner ? $report->owner->name : '',
  65. $report->owner ? ($report->owner->deleted_at ? "冻结" : "激活") : '',
  66. $report->owner ? (string)$report->owner->created_at : '',
  67. $report->owner ? ($report->owner->created_at ? ((new \DateTime())->diff(new \DateTime($report->owner->created_at))->days)." 天" : '') : '',
  68. $report->counting_month,
  69. $report->daily_average_order_amount,
  70. $report->last_month_counting_area,
  71. $report->current_month_counting_area,
  72. $report->ownerBillReport ? $report->ownerBillReport->initial_fee : '',
  73. $report->ownerBillReport ? $report->ownerBillReport->confirm_fee : '',
  74. $report->ownerBillReport ? (string)$report->ownerBillReport->updated_at : '',
  75. ];
  76. }
  77. $post = Http::post(config('go.export.url'),['type'=>'base','data'=>json_encode(["row"=>$column,"list"=>$list],JSON_UNESCAPED_UNICODE)]);
  78. if ($post->status() == 500){
  79. throw new Exception($post->header("Msg"));
  80. }
  81. return response($post,200, [
  82. "Content-type"=>"application/octet-stream",
  83. "Content-Disposition"=>"attachment; filename=客户项目报表-".date('ymdHis').'.xlsx',
  84. ]);
  85. }
  86. public function projectIndex()
  87. {
  88. if(!Gate::allows('客户管理-项目-查询')){ return redirect('denied'); }
  89. /** @var OwnerService $service */
  90. $service = app('OwnerService');
  91. $owners = $service->paginate(request()->input(),['customer',"userOwnerGroup","userWorkGroup",
  92. "ownerStoragePriceModels",/*"ownerAreaReport"=>function($query){
  93. $month = date('Y-m');
  94. /** @var Builder $query */
  95. /*$query->where("counting_month","like",$month."%");
  96. }*/]);
  97. $models = app('OwnerService')->getIntersectPermitting();
  98. $customers = app('CustomerService')->getSelection();
  99. $ownerGroups = app('UserOwnerGroupService')->getSelection();
  100. $params = request()->input();
  101. return response()->view('customer.project.index',compact("owners","models","customers","ownerGroups","params"));
  102. }
  103. public function projectIndexExport(Request $request)
  104. {
  105. if(!Gate::allows('客户管理-项目-查询')){ return redirect('denied'); }
  106. /** @var OwnerService $service */
  107. $service = app('OwnerService');
  108. $withs = ['customer',"userOwnerGroup","contracts","ownerStoragePriceModels","ownerAreaReport"=>function($query){
  109. $month = date('Y-m');
  110. /** @var Builder $query */
  111. $query->where("counting_month","like",$month."%");
  112. }];
  113. $params = $request->input();
  114. $params['customer_id']=true;
  115. if ($request->checkAllSign ?? false) unset($params['checkAllSign']);
  116. else $params = ["id"=>$request->data ?? ''];
  117. $owners = $service->get($params,$withs);
  118. $column = ["客户","税率","项目","货主代码","创建日期","合同号","销售名称","公司全称","联系人","联系电话","项目小组","用仓类型","当月结算面积","月单量预警","是否激活","项目描述"];
  119. $list = [];
  120. foreach ($owners as $owner){
  121. $list[] = [
  122. $owner->customer ? $owner->customer->name : '',
  123. $owner->tax_rate,
  124. $owner->name,
  125. $owner->code,
  126. $owner->created_at,
  127. implode("\r\n",array_column($owner->contracts,"contract_number")),
  128. implode("\r\n",array_column($owner->contracts,"salesman")),
  129. $owner->customer ? $owner->customer->company_name : '',
  130. $owner->linkman,
  131. $owner->phone_number,
  132. $owner->userOwnerGroup ? $owner->userOwnerGroup->name : '',
  133. implode(",",array_unique(array_column(($owner->ownerStoragePriceModels)->toArray(),"using_type"))),
  134. $owner->ownerAreaReport ? $owner->ownerAreaReport->accounting_area : '',
  135. $owner->waring_line_on,
  136. $owner->deleted_at ? '否' : '是',
  137. $owner->description
  138. ];
  139. }
  140. $post = Http::post(config('go.export.url'),['type'=>'base','data'=>json_encode(["row"=>$column,"list"=>$list],JSON_UNESCAPED_UNICODE)]);
  141. if ($post->status() == 500){
  142. throw new Exception($post->header("Msg"));
  143. }
  144. return response($post,200, [
  145. "Content-type"=>"application/octet-stream",
  146. "Content-Disposition"=>"attachment; filename=客户报表-".date('ymdHis').'.xlsx',
  147. ]);
  148. }
  149. public function projectCreate()
  150. {
  151. if(!Gate::allows('客户管理-项目-录入')){ return redirect('denied'); }
  152. $customers = app('CustomerService')->getSelection();
  153. $ownerGroups = app('UserOwnerGroupService')->getSelection();
  154. $userGroups = app('UserWorkgroupService')->getSelection(["id","name","warehouse_id"]);
  155. $warehouses = app('WarehouseService')->getSelection();
  156. $owner = null;
  157. return response()->view('customer.project.create',compact("customers","ownerGroups","owner","warehouses","userGroups"));
  158. }
  159. public function projectUpdate()
  160. {
  161. $this->gate("客户管理-项目-录入");
  162. if (!request("id"))$this->error("项目不存在,无法补充详细信息");
  163. $errors = $this->validator(request()->input())->errors();
  164. if (count($errors)>0)$this->success(["errors"=>$errors]);
  165. /** @var Owner $owner */
  166. $owner = app('OwnerService')->find(request("id"));
  167. if (!$owner)$this->error("项目已被删除,无法操作");
  168. $owner = app('OwnerService')->update($owner,[
  169. "customer_id" => request("customer_id"),
  170. "warehouse_id" => request("warehouse_id"),
  171. "tax_rate" => request("tax_rate"),
  172. "linkman" => request("linkman"),
  173. "phone_number" => request("phone_number"),
  174. "user_owner_group_id" => request("owner_group_id"),
  175. "user_workgroup_id" => request("user_workgroup_id"),
  176. "waring_line_on" => request("waring_line_on"),
  177. "description" => request("description"),
  178. ]);
  179. $this->success($owner);
  180. }
  181. //获取货主下所有相关计费模型
  182. public function getOwnerPriceModel(Request $request)
  183. {
  184. $owner = new Owner();
  185. $owner->id = $request->id;
  186. $owner->load(["ownerPriceOperations","ownerPriceExpresses","ownerPriceLogistics","ownerPriceDirectLogistics"]);
  187. return ["success"=>true,"data"=>["ownerPriceOperations"=>$owner->ownerPriceOperations,
  188. "ownerPriceExpresses"=>$owner->ownerPriceExpresses,
  189. "ownerPriceLogistics"=>$owner->ownerPriceLogistics,
  190. "ownerPriceDirectLogistics"=>$owner->ownerPriceDirectLogistics]];
  191. }
  192. public function projectEdit($id)
  193. {
  194. if(!Gate::allows('客户管理-项目-编辑')){ return redirect('denied'); }
  195. /** @var Owner $owner */
  196. $owner = app('OwnerService')->find($id);
  197. $owner->loadCount(["ownerStoragePriceModels","ownerPriceOperations","ownerPriceExpresses","ownerPriceLogistics","ownerPriceDirectLogistics"]);
  198. $isExist = false;
  199. /** @var \stdClass $owner */
  200. if($owner->owner_storage_price_models_count ||
  201. $owner->owner_price_operations_count ||
  202. $owner->owner_price_expresses_count ||
  203. $owner->owner_price_logistics_count ||
  204. $owner->owner_price_direct_logistics_count) $isExist = true;
  205. $customers = app('CustomerService')->getSelection();
  206. $ownerGroups = app('UserOwnerGroupService')->getSelection();
  207. $userGroups = app('UserWorkgroupService')->getSelection(["id","name","warehouse_id"]);
  208. $warehouses = app('WarehouseService')->getSelection();
  209. $type = request("type");
  210. return response()->view('customer.project.create',compact("customers","ownerGroups","warehouses",'owner',"isExist", "type","userGroups"));
  211. }
  212. public function projectArea(Request $request)
  213. {
  214. if(!Gate::allows('客户管理-项目-面积')){ return redirect('denied'); }
  215. $areas = app('OwnerAreaReportService')->paginate($request->input(),["owner"=>function($query){$query->with(["customer","ownerStoragePriceModels.unit"]);}]);
  216. $ownerGroups = app('UserOwnerGroupService')->getSelection();
  217. $customers = app('CustomerService')->getSelection();
  218. $owners = app('OwnerService')->getIntersectPermitting();
  219. $params = $request->input();
  220. return response()->view('customer.project.area',compact("areas","ownerGroups","customers","owners","params"));
  221. }
  222. public function updateArea(Request $request)
  223. {
  224. if(!Gate::allows('客户管理-项目-面积-编辑')){ return ["success"=>false,'data'=>"无权操作!"]; }
  225. if (!($request->id ?? false) || !($request->area ?? false)) return ["success"=>false,'data'=>"传递错误!"];
  226. if (!request("accounting_area")){
  227. $values = $request->area ?? null;
  228. if (!$values)return ["success"=>true,"data"=>$values];
  229. foreach ($values as $column=>$value){
  230. if ($value && (!is_numeric($value) || $value<0))return ["success"=>false,'data'=>$column."非数字或小于0!"];
  231. }
  232. $accounting_area = ((int)$values["area_on_tray"]*2.5) + ((int)$values["area_on_half_tray"]*1.8) + ((int)$values["area_on_flat"]*1.3);
  233. $values["accounting_area"] = $accounting_area;
  234. }else $values = ["accounting_area"=>request("accounting_area")];
  235. $row = app('OwnerAreaReportService')->update(["id"=>$request->id],$values);
  236. if ($row==1){
  237. LogService::log(__METHOD__,"客户管理-修改面积",json_encode($request->input()));
  238. return ["success"=>true,"data"=>$values];
  239. }
  240. return ["success"=>false,"data"=>"影响了".$row."条数据!"];
  241. }
  242. public function projectAreaExport(Request $request)
  243. {
  244. if(!Gate::allows('客户管理-项目-面积')){ return redirect('denied'); }
  245. $params = $request->input();
  246. if ($request->checkAllSign)unset($params['checkAllSign']);
  247. else $params = ["id"=>$request->data];
  248. /** @var OwnerAreaReportService $serves */
  249. $serves = app('OwnerAreaReportService');
  250. $areas = $serves->get($params,["owner"=>function($query){$query->with(["customer","ownerStoragePriceModels","userOwnerGroup"]);}]);
  251. $column = ["状态","项目组","客户","子项目","结算月","录入时间","用仓类型","货物整托","货物半托","平面区面积","结算面积"];
  252. $list = [];
  253. foreach ($areas as $area){
  254. $list[] = [
  255. $area->status,
  256. $area->owner ? ($area->owner->userOwnerGroup ? $area->owner->userOwnerGroup->name : '') : '',
  257. $area->owner ? ($area->owner->customer ? $area->owner->customer->name : '') : '',
  258. $area->owner ? $area->owner->name : '',
  259. $area->counting_month,
  260. $area->updated_at,
  261. $area->owner ? implode(",",array_unique(array_column(($area->owner->ownerStoragePriceModels)->toArray(),"using_type"))) : '',
  262. $area->area_on_tray,
  263. $area->area_on_half_tray,
  264. $area->area_on_flat,
  265. $area->accounting_area,
  266. ];
  267. }
  268. $post = Http::post(config('go.export.url'),['type'=>'base','data'=>json_encode(["row"=>$column,"list"=>$list],JSON_UNESCAPED_UNICODE)]);
  269. if ($post->status() == 500){
  270. throw new Exception($post->header("Msg"));
  271. }
  272. return response($post,200, [
  273. "Content-type"=>"application/octet-stream",
  274. "Content-Disposition"=>"attachment; filename=项目面积报表-".date('ymdHis').'.xlsx',
  275. ]);
  276. }
  277. public function financeInstantBill(Request $request)
  278. {
  279. if(!Gate::allows('客户管理-财务-即时账单')){ return redirect('denied'); }
  280. $params = $request->input();
  281. $shops = app('ShopService')->getSelection();
  282. $customers = app('CustomerService')->getSelection();
  283. $owners = app('OwnerService')->getIntersectPermitting();
  284. $details = app('OwnerFeeDetailService')->paginate($params,["owner.customer","shop","processMethod","logistic","items"]);
  285. return response()->view('customer.finance.instantBill',compact("details","params","shops","customers","owners"));
  286. }
  287. public function financeInstantBillExport(Request $request)
  288. {
  289. if(!Gate::allows('客户管理-财务-即时账单')){ return redirect('denied'); }
  290. $params = $request->input();
  291. if ($request->checkAllSign)unset($params['checkAllSign']);
  292. else $params = ["id"=>$request->data];
  293. $sql = app('OwnerFeeDetailService')->getSql($params);
  294. $row = ["客户", "项目", "作业时间", "类型","店铺", "单号(发/收/退/提)", "收件人", "收件人电话", "商品数量",
  295. "物流/快递单号", "体积", "重量", "承运商", "操作费", "物流费", "合计"];
  296. $column = ["customer_name", "owner_name", "worked_at", "type","shop_name", "operation_bill", "consignee_name", "consignee_phone", "commodity_amount",
  297. "logistic_bill", "volume", "weight", "logistic_name", "work_fee", "logistic_fee", "total"];
  298. $rule = ["work_fee"=>"mysqlDate"];
  299. $post = Http::post(config('go.export.url'),['type'=>'unify','sql'=>$sql, 'connection'=>'mysql',
  300. 'row'=>json_encode($row,JSON_UNESCAPED_UNICODE), 'column'=>json_encode($column), 'rule'=>json_encode($rule)]);
  301. if ($post->status() == 500){
  302. throw new Exception($post->header("Msg"));
  303. }
  304. return response($post,200, [
  305. "Content-type"=>"application/octet-stream",
  306. "Content-Disposition"=>"attachment; filename=即时账单记录-".date('ymdHis').'.xlsx',
  307. ]);
  308. }
  309. public function financeBillConfirmation(Request $request)
  310. {
  311. if(!Gate::allows('客户管理-财务-账单确认')){ return redirect('denied'); }
  312. $params = $request->input();
  313. $ownerGroups = app('UserOwnerGroupService')->getSelection();
  314. $customers = app('CustomerService')->getSelection();
  315. $owners = app('OwnerService')->getIntersectPermitting();
  316. $bills = app('OwnerBillReportService')->paginate($params,["owner"=>function($query){
  317. /** @var Builder $query */
  318. $query->with(["customer","userOwnerGroup"]);
  319. }]);
  320. return response()->view('customer.finance.billConfirmation',compact("params","owners","ownerGroups","customers","bills"));
  321. }
  322. public function financeBillConfirmationExport(Request $request)
  323. {
  324. if(!Gate::allows('客户管理-财务-账单确认')){ return redirect('denied'); }
  325. $params = $request->input();
  326. if ($request->checkAllSign)unset($params['checkAllSign']);
  327. else $params = ["id"=>$request->data];
  328. /** @var OwnerBillReportService $serves */
  329. $serves = app('OwnerBillReportService');
  330. $bills = $serves->get($params,["owner"=>function($query){
  331. /** @var Builder $query */
  332. $query->with(["customer","userOwnerGroup"]);
  333. }]);
  334. $column = ["项目小组","客户","子项目","结算月","录入日期","原始账单金额","确认账单金额","差额","状态"];
  335. $list = [];
  336. foreach ($bills as $bill){
  337. $list[] = [
  338. $bill->owner ? ($bill->owner->userOwnerGroup ? $bill->owner->userOwnerGroup->name : '') : '',
  339. $bill->owner ? ($bill->owner->customer ? $bill->owner->customer->name : '') : '',
  340. $bill->owner ? $bill->owner->name : '',
  341. $bill->counting_month,
  342. $bill->updated_at,
  343. $bill->initial_fee,
  344. $bill->confirm_fee,
  345. $bill->difference,
  346. $bill->confirmed == '是' ? '已确认' : '未确认',
  347. ];
  348. }
  349. $post = Http::post(config('go.export.url'),['type'=>'base','data'=>json_encode(["row"=>$column,"list"=>$list],JSON_UNESCAPED_UNICODE)]);
  350. if ($post->status() == 500){
  351. throw new Exception($post->header("Msg"));
  352. }
  353. return response($post,200, [
  354. "Content-type"=>"application/octet-stream",
  355. "Content-Disposition"=>"attachment; filename=客户账单报表-".date('ymdHis').'.xlsx',
  356. ]);
  357. }
  358. public function updateBillReport(Request $request)
  359. {
  360. if(!Gate::allows('客户管理-财务-账单确认-编辑')){ return ["success"=>false,'data'=>"无权操作!"]; }
  361. if (!$request->confirm_fee || !is_numeric($request->confirm_fee) || $request->confirm_fee<0)return ["success"=>false,"data"=>"非法金额参数"];
  362. $date = date('Y-m-d H:i:s');
  363. app('OwnerBillReportService')->update(["id"=>$request->id],["confirm_fee"=>$request->confirm_fee,"difference"=>DB::raw($request->confirm_fee.'- initial_fee'),"updated_at"=>$date]);
  364. LogService::log(__METHOD__,"客户管理-修改账单报表",json_encode($request->input()));
  365. return ["success"=>true,"data"=>$date];
  366. }
  367. public function billConfirm(Request $request)
  368. {
  369. if(!Gate::allows('客户管理-财务-账单确认-完结')){ return ["success"=>false,'data'=>"无权操作!"]; }
  370. if (!($request->id ?? false))return["success"=>false,"data"=>"非法参数"];
  371. app('OwnerBillReportService')->update(["id"=>$request->id],["confirmed"=>"是"]);
  372. LogService::log(__METHOD__,"客户管理-确认账单",json_encode($request->input()));
  373. $bill = app('OwnerBillReportService')->first(["id"=>$request->id,"confirmed"=>"是"]);
  374. app('OwnerAreaReportService')->lockArea(null, $bill->owner_id, $bill->counting_month);
  375. LogService::log(__METHOD__,"客户管理-锁定账单的所有面积",json_encode($bill,JSON_UNESCAPED_UNICODE));
  376. return ["success"=>true];
  377. }
  378. private function validator(array $params){
  379. $validator=Validator::make($params,[
  380. 'id' => ['required'],
  381. 'customer_id'=>['required'],
  382. 'owner_group_id'=>['required'],
  383. 'warehouse_id'=>['required'],
  384. 'tax_rate' => ["nullable",'numeric'],
  385. 'waring_line_on' => ["nullable",'integer'],
  386. ],[
  387. 'required'=>':attribute 为必填项',
  388. 'integer'=>':attribute 必须为整数',
  389. 'numeric'=>':attribute 必须为数字',
  390. ],[
  391. 'code'=>'项目代码',
  392. 'name'=>'项目名称',
  393. 'warehouse_id'=>'仓库',
  394. 'customer_id'=>'客户',
  395. 'owner_group_id'=>'工作组',
  396. 'tax_rate' => '税率',
  397. 'waring_line_on' => '月单量预警'
  398. ]);
  399. return $validator;
  400. }
  401. public function verifyProject(Request $request)
  402. {
  403. $this->success($this->validator($request->input())->errors());
  404. }
  405. public function createReport()
  406. {
  407. $ids = \request("val");
  408. if (!$ids)$this->error("未选择任何项目");
  409. $reports = OwnerReport::query()->with("owner")
  410. ->where("counting_month",">=",date("Y-m")."-01")
  411. ->whereIn("owner_id",$ids)->get(["id","owner_id"]);
  412. $errors = [];
  413. $exist = [];
  414. foreach ($reports as $report){
  415. $errors[] = "“".($report->owner ? $report->owner->name : $report->owner_id)."”已存在本月报表";
  416. $exist[] = $report->owner_id;
  417. }
  418. $ids = array_diff($ids,$exist);
  419. $insert = [];
  420. $date = date("Y-m-d H:i:s");
  421. foreach ($ids as $id){
  422. $insert[] = [
  423. "owner_id" => $id,
  424. "counting_month" => date("Y-m-d"),
  425. "created_at" => $date
  426. ];
  427. }
  428. if ($insert){
  429. OwnerReport::query()->insert($insert);
  430. LogService::log(__METHOD__,"手动生成报表",json_encode($insert));
  431. }
  432. $reports = OwnerReport::query()->with(["owner.userOwnerGroup","owner.customer"])
  433. ->where("counting_month",">=",date("Y-m")."-01")
  434. ->whereIn("owner_id",$ids)->get();
  435. $result = [];
  436. foreach ($reports as $report){
  437. $result[] = [
  438. "id" => $report->id,
  439. "ownerGroupName" => $report->owner ? ($report->owner->userOwnerGroup ? $report->owner->userOwnerGroup->name : '') : '',
  440. "customerName" => $report->owner ? ($report->owner->customer ? $report->owner->customer->name : '') : '',
  441. "ownerName" => $report->owner ? $report->owner->name : '',
  442. "ownerStatus" => $report->owner ? ($report->owner->deleted_at ? "冻结" : "激活") : '',
  443. "ownerStorageDuration" => $report->owner ? ($report->owner->created_at ? ((new \DateTime())->diff(new \DateTime($report->owner->created_at))->days) : '') : '',
  444. "ownerCreatedAt" => $report->owner ? $report->owner->created_at : '',
  445. "countingMonth" => $report->counting_month,
  446. ];
  447. }
  448. $this->success(["errors"=>$errors,"data"=>$result]);
  449. }
  450. public function createAreaReport()
  451. {
  452. $ids = \request("val");
  453. if (!$ids)$this->error("未选择任何项目");
  454. /** @var OwnerService $service */
  455. $service = app("OwnerService");
  456. $owners = $service->get(["id"=>$ids],["ownerStoragePriceModels"],false,true);
  457. app("OwnerAreaReportService")->notExistToInsert($owners);
  458. $reports = OwnerAreaReport::query()->with(["owner","ownerStoragePriceModel.unit"])
  459. ->where("counting_month",">=",date("Y-m")."-01")
  460. ->whereIn("owner_id",array_column($owners->toArray(),"id"))->get();
  461. $result = [];
  462. foreach ($reports as $report){
  463. $result[] = [
  464. "id" => $report->id,
  465. "ownerGroupId" => $report->user_owner_group_id,
  466. "ownerName" => $report->owner ? $report->owner->name : '',
  467. "customerName" => $report->owner ? ($report->owner->customer ? $report->owner->customer->name : '') : '',
  468. "countingMonth" => $report->counting_month,
  469. "areaOnTray" => $report->area_on_tray,
  470. "areaOnHalfTray" => $report->area_on_half_tray,
  471. "areaOnFlat" => $report->area_on_flat,
  472. "accountingArea" => $report->accounting_area,
  473. "status" => $report->status,
  474. "updatedAt" => $report->updated_at,
  475. "unitName" => $report->ownerStoragePriceModel ? ($report->ownerStoragePriceModel->unit ? $report->ownerStoragePriceModel->unit->name : '') : '',
  476. "ownerStoragePriceModel"=> $report->ownerStoragePriceModel ? $report->ownerStoragePriceModel->using_type : '' ,
  477. ];
  478. }
  479. $this->success($result);
  480. }
  481. }