CustomerController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Owner;
  4. use App\Services\OwnerReportService;
  5. use App\Services\OwnerService;
  6. use Exception;
  7. use Illuminate\Database\Eloquent\Builder;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Http\Response;
  10. use Illuminate\Support\Facades\Gate;
  11. use Illuminate\Support\Facades\Http;
  12. use Illuminate\Support\Facades\Validator;
  13. class CustomerController extends Controller
  14. {
  15. /**
  16. * Display a listing of the resource.
  17. * @param Request $request
  18. * @return Response
  19. */
  20. public function projectReport(Request $request)
  21. {
  22. if(!Gate::allows('客户管理-项目-报表')){ return view('customer.index'); }
  23. $withs = ["ownerBillReport","owner"=>function($query){
  24. /** @var Builder $query */
  25. $query->select("id","name","deleted_at","created_at","customer_id","user_owner_group_id")
  26. ->with(["customer","userOwnerGroup"]);
  27. }];
  28. $ownerGroups = app('UserOwnerGroupService')->getSelection();
  29. $customers = app('CustomerService')->getSelection();
  30. $owners = app('OwnerService')->getSelection();
  31. $reports = app("OwnerReportService")->paginate($request->input(),$withs);
  32. return response()->view('customer.project.report',compact("reports","ownerGroups","customers","owners"));
  33. }
  34. public function projectReportExport(Request $request)
  35. {
  36. if(!Gate::allows('客户管理-项目-报表')){ return redirect('denied'); }
  37. /** @var OwnerReportService $service */
  38. $service = app('OwnerReportService');
  39. $withs = ["ownerBillReport","owner"=>function($query){
  40. /** @var Builder $query */
  41. $query->select("id","name","deleted_at","created_at","customer_id","user_owner_group_id")
  42. ->with(["customer","userOwnerGroup"]);
  43. }];
  44. if ($request->checkAllSign ?? false){
  45. $params = $request->input();
  46. unset($params['checkAllSign']);
  47. $reports = $service->get($params,$withs);
  48. }else $reports = $service->get(["id"=>$request->data ?? ''],$withs);
  49. $column = ["项目小组","客户","子项目","状态","创建日期","在库时长","结算月","日均单量","结算月上月盘点面积","结算月盘点面积","初始账单金额","确认账单金额","确认日期"];
  50. $list = [];
  51. foreach ($reports as $report){
  52. $list[] = [
  53. $report->owner ? ($report->owner->userOwnerGroup ? $report->owner->userOwnerGroup->name : '') : '',
  54. $report->owner ? ($report->owner->customer ? $report->owner->customer->name : '') : '',
  55. $report->owner ? $report->owner->name : '',
  56. $report->owner ? ($report->owner->deleted_at ? "冻结" : "激活") : '',
  57. $report->owner ? (string)$report->owner->created_at : '',
  58. $report->owner ? ($report->owner->created_at ? ((new \DateTime())->diff(new \DateTime($report->owner->created_at))->days)." 天" : '') : '',
  59. $report->counting_month,
  60. $report->daily_average_order_amount,
  61. $report->last_month_counting_area,
  62. $report->current_month_counting_area,
  63. $report->ownerBillReport ? $report->ownerBillReport->initial_fee : '',
  64. $report->ownerBillReport ? $report->ownerBillReport->confirm_fee : '',
  65. $report->ownerBillReport ? (string)$report->ownerBillReport->updated_at : '',
  66. ];
  67. }
  68. $post = Http::post(config('go.export.url'),['type'=>'base','data'=>json_encode(["row"=>$column,"list"=>$list],JSON_UNESCAPED_UNICODE)]);
  69. if ($post->status() == 500){
  70. throw new Exception($post->header("Msg"));
  71. }
  72. return response($post,200, [
  73. "Content-type"=>"application/octet-stream",
  74. "Content-Disposition"=>"attachment; filename=客户项目报表-".date('ymdHis').'.xlsx',
  75. ]);
  76. }
  77. public function projectIndex()
  78. {
  79. if(!Gate::allows('客户管理-项目-查询')){ return redirect('denied'); }
  80. /** @var OwnerService $service */
  81. $service = app('OwnerService');
  82. $owners = $service->paginate(['customer_id'=>true],['customer',"userOwnerGroup","ownerStoragePriceModels","ownerAreaReport"=>function($query){
  83. $month = date('Y-m');
  84. /** @var Builder $query */
  85. $query->where("counting_month","like",$month."%");
  86. }]);
  87. return response()->view('customer.project.index',compact("owners"));
  88. }
  89. public function projectIndexExport(Request $request)
  90. {
  91. if(!Gate::allows('客户管理-项目-查询')){ return redirect('denied'); }
  92. /** @var OwnerService $service */
  93. $service = app('OwnerService');
  94. $withs = ['customer',"userOwnerGroup","ownerStoragePriceModels","ownerAreaReport"=>function($query){
  95. $month = date('Y-m');
  96. /** @var Builder $query */
  97. $query->where("counting_month","like",$month."%");
  98. }];
  99. $params = $request->input();
  100. $params['customer_id']=true;
  101. if ($request->checkAllSign ?? false) unset($params['checkAllSign']);
  102. else $params = ["id"=>$request->data ?? ''];
  103. $owners = $service->get($params,$withs);
  104. $column = ["客户","税率","项目","货主代码","合同号","创建日期","销售名称","公司全称","联系人","联系电话","项目小组","用仓类型","当月结算面积","月单量预警","是否激活","项目描述"];
  105. $list = [];
  106. foreach ($owners as $owner){
  107. $list[] = [
  108. $owner->customer ? $owner->customer->name : '',
  109. $owner->tax_rate,
  110. $owner->name,
  111. $owner->code,
  112. $owner->contract_number,
  113. $owner->created_at,
  114. $owner->salesman,
  115. $owner->customer ? $owner->customer->company_name : '',
  116. $owner->linkman,
  117. $owner->phone_number,
  118. $owner->userOwnerGroup ? $owner->userOwnerGroup->name : '',
  119. implode(",",array_unique(array_column(($owner->ownerStoragePriceModels)->toArray(),"using_type"))),
  120. $owner->ownerAreaReport ? $owner->ownerAreaReport->accounting_area : '',
  121. $owner->waring_line_on,
  122. $owner->deleted_at ? '否' : '是',
  123. $owner->description
  124. ];
  125. }
  126. $post = Http::post(config('go.export.url'),['type'=>'base','data'=>json_encode(["row"=>$column,"list"=>$list],JSON_UNESCAPED_UNICODE)]);
  127. if ($post->status() == 500){
  128. throw new Exception($post->header("Msg"));
  129. }
  130. return response($post,200, [
  131. "Content-type"=>"application/octet-stream",
  132. "Content-Disposition"=>"attachment; filename=客户报表-".date('ymdHis').'.xlsx',
  133. ]);
  134. }
  135. public function projectCreate()
  136. {
  137. if(!Gate::allows('客户管理-项目-录入')){ return redirect('denied'); }
  138. $customers = app('CustomerService')->getSelection();
  139. $ownerGroups = app('UserOwnerGroupService')->getSelection();
  140. $storagePriceModels = app('OwnerStoragePriceModelService')->getSelection(["id","counting_type","using_type","minimum_area","price","unit_id"],["unit"=>function($query){$query->select("id","name");}]);
  141. $owner = null;
  142. return response()->view('customer.project.create',compact("customers","ownerGroups","storagePriceModels","owner"));
  143. }
  144. public function seekOwner(Request $request)
  145. {
  146. $params = $request->input();
  147. $params["customer_id"] = true;
  148. $owner = app('OwnerService')->first($params,["customer_id"=>"null"]);
  149. if ($owner)return ["success"=>true,"data"=>$owner];
  150. else return ["success"=>false];
  151. }
  152. public function projectStore(Request $request)
  153. {
  154. if(!Gate::allows('客户管理-项目-录入')){ return redirect('denied'); }
  155. $this->validator($request->input())->validate();
  156. $params = $request->input();
  157. if ($params["id"]){
  158. /** @var Owner $owner */
  159. $owner = app('OwnerService')->find($params["id"]);
  160. app('OwnerService')->update($owner,[
  161. "customer_id" => $params["customer_id"],
  162. "tax_rate" => $params["tax_rate"],
  163. "contract_number" => $params["contract_number"],
  164. "salesman" => $params["salesman"],
  165. "linkman" => $params["linkman"],
  166. "phone_number" => $params["phone_number"],
  167. "user_owner_group_id" => $params["owner_group_id"],
  168. "waring_line_on" => $params["waring_line_on"],
  169. "description" => $params["description"],
  170. ],[
  171. "ownerStoragePriceModels" => explode(',',$params["owner_storage_price_model_id"])
  172. ]);
  173. $msg = "成功更新“".$owner->name."”的信息!";
  174. }else{
  175. $owner = app('OwnerService')->create([
  176. "name" => $params["name"],
  177. "code" => $params["code"],
  178. "customer_id" => $params["customer_id"],
  179. "tax_rate" => $params["tax_rate"],
  180. "contract_number" => $params["contract_number"],
  181. "salesman" => $params["salesman"],
  182. "linkman" => $params["linkman"],
  183. "phone_number" => $params["phone_number"],
  184. "user_owner_group_id" => $params["owner_group_id"],
  185. "waring_line_on" => $params["waring_line_on"],
  186. "description" => $params["description"],
  187. ],[
  188. "ownerStoragePriceModels" => explode(',',$params["owner_storage_price_model_id"])
  189. ]);
  190. $msg = "成功创建“".$owner->name."”项目!";
  191. }
  192. return response()->redirectTo('customer/project/index')->with('successTip',$msg);
  193. }
  194. //获取货主下所有相关计费模型
  195. public function getOwnerPriceModel(Request $request)
  196. {
  197. $owner = new Owner();
  198. $owner->id = $request->id;
  199. $owner->load(["ownerPriceOperations","ownerPriceExpresses","ownerPriceLogistics","ownerPriceDirectLogistics"]);
  200. return ["success"=>true,"data"=>["ownerPriceOperations"=>$owner->ownerPriceOperations,
  201. "ownerPriceExpresses"=>$owner->ownerPriceExpresses,
  202. "ownerPriceLogistics"=>$owner->ownerPriceLogistics,
  203. "ownerPriceDirectLogistics"=>$owner->ownerPriceDirectLogistics]];
  204. }
  205. public function projectEdit($id)
  206. {
  207. if(!Gate::allows('客户管理-项目-编辑')){ return redirect('denied'); }
  208. /** @var Owner $owner */
  209. $owner = app('OwnerService')->find($id);
  210. $owner->owner_storage_price_model_id = $owner->getOwnerStoragePriceModelIds();
  211. $customers = app('CustomerService')->getSelection();
  212. $ownerGroups = app('UserOwnerGroupService')->getSelection();
  213. $storagePriceModels = app('OwnerStoragePriceModelService')->getSelection(["id","counting_type","using_type","minimum_area","price","unit_id"],["unit"=>function($query){$query->select("id","name");}]);
  214. return response()->view('customer.project.create',compact("customers","ownerGroups","storagePriceModels",'owner'));
  215. }
  216. public function projectArea()
  217. {
  218. if(!Gate::allows('客户管理-项目-面积')){ return redirect('denied'); }
  219. return response()->view('customer.project.area');
  220. }
  221. public function financeInstantBill()
  222. {
  223. if(!Gate::allows('客户管理-财务-即时账单')){ return redirect('denied'); }
  224. return response()->view('customer.finance.instantBill');
  225. }
  226. public function financeBillConfirmation()
  227. {
  228. if(!Gate::allows('客户管理-财务-账单确认')){ return redirect('denied'); }
  229. return response()->view('customer.finance.billConfirmation');
  230. }
  231. private function validator(array $params){
  232. $id = $params['id'] ?? null;
  233. $validator=Validator::make($params,[
  234. 'id' => ['required_without_all:code,name'],
  235. 'code'=>['required','max:50',$id ? "unique:owners,code,$id":'unique:owners,code'],
  236. 'name'=>['required','max:50'],
  237. 'customer_id'=>['required'],
  238. 'owner_group_id'=>['required'],
  239. 'tax_rate' => ['required','numeric'],
  240. ],[
  241. 'required'=>':attribute 为必填项',
  242. 'unique'=>':attribute 已存在',
  243. ],[
  244. 'code'=>'项目代码',
  245. 'name'=>'项目名称',
  246. 'customer_id'=>'客户',
  247. 'owner_group_id'=>'工作组',
  248. 'tax_rate' => '税率'
  249. ]);
  250. return $validator;
  251. }
  252. }