CustomerController.php 25 KB

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