财务权责明细表.sql 4.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. select mm.*, dm.themonth, dm.money
  2. from (
  3. select m.*, n.kind
  4. from (
  5. select
  6. crc.tenant_id,
  7. hhb.dept_id '房源所属门店ID', house_sd.name '房源所属门店名称', hhr.address '房源地址', hhr.house_area '房源面积',
  8. crc_sd.id '合同所属门店部门ID', crc_sd.name '合同所属门店部门',
  9. crc.id '合同ID', crc.contract_no '合同编号', crc.maintainer_id '合同维护人ID', mt_emp.name '合同维护人',
  10. cri.name '租客姓名', crc.begin_time '合同开始日期', crc.end_time '合同结束日期', DATEDIFF(crc.end_time, crc.begin_time)+1 '签约天数',
  11. crc.`type` '合同类型', crc.sign_type '成交方式',
  12. TRIM(TRAILING ',' FROM CONCAT(
  13. IF(crc.contract_status = 1 AND crc.contract_sub_status = 1, '待处理,', ''),
  14. IF(crc.contract_status = 1 AND crc.contract_sub_status = 2 AND crc.sign_status = 1, '待租客签字,', ''),
  15. IF(crc.contract_status = 1 AND crc.contract_sub_status = 2 AND crc.sign_status = 0, '带租客实名,', ''),
  16. IF(crc.contract_status = 2 AND crc.transition_type != 1 AND LENGTH(trim(crc.`down_payment_info`)) > 1 AND JSON_EXTRACT(crc.`down_payment_info`, '$.status') = 0, '待付款,', ''),
  17. IF(crc.contract_status = 2 AND crc.transition_type != 1 AND crc.is_sync_tenant = 1, '待租客确认,', ''),
  18. IF(crc.contract_status = 2 AND crc.transition_type != 1 AND DATE(NOW()) < crc.begin_time, '即将搬入,', ''),
  19. IF(crc.contract_status = 2 AND crc.transition_type != 1 AND LENGTH(trim(crc.`down_payment_info`)) > 1 AND (JSON_EXTRACT(crc.`down_payment_info`, '$.status') = 1 AND DATE(NOW()) >= crc.begin_time AND DATE(NOW()) < DATE_ADD(DATE(crc.end_time), INTERVAL 30 DAY) ), '租约中,', ''),
  20. IF(crc.contract_status = 2 AND crc.transition_type != 1 AND crc.`begin_time` <= DATE(NOW()) AND DATE(NOW()) >= DATE_ADD(DATE(crc.end_time), INTERVAL 30 DAY) AND DATE(NOW())< DATE(crc.end_time), '即将到期,', ''),
  21. IF(crc.contract_status = 2 AND crc.transition_type != 1 AND DATE(NOW()) >= DATE(crc.end_time), '已到期,', ''),
  22. IF(crc.contract_status = 2 AND crc.transition_type = 1, '已续约,', ''),
  23. IF(crc.contract_status = 3 AND crc.terminate_type = 1, '正常退,', ''),
  24. IF(crc.contract_status = 3 AND crc.terminate_type = 2, '违约退,', ''),
  25. IF(crc.contract_status = 4 AND crc.invalid_type = 1, '已作废,', ''),
  26. IF(crc.contract_status = 4 AND crc.invalid_type = 2, '已拒绝,', '')
  27. )) '合同状态',
  28. crc.approval_status '审批状态',
  29. 0 'splitter',
  30. _total.subject_name '费用类型',
  31. _total.fee_subject_id
  32. from yuxin_contract.cont_renter_contract crc
  33. left join yuxin_house.hse_house_room hhr on hhr.is_delete=0 and hhr.id=crc.house_id
  34. left join yuxin_house.hse_house_base hhb on hhb.is_delete=0 and hhb.id=crc.house_id
  35. left join yuxin_setting.setting_department house_sd on house_sd.id=hhb.dept_id and house_sd.is_delete=0
  36. left join yuxin_house.hse_community hc on hc.id=hhb.community_id and hc.is_delete=0
  37. left join yuxin_setting.setting_employee_dept crc_ed on crc_ed.emp_id=crc.maintainer_id and crc_ed.is_delete=0
  38. left join yuxin_setting.setting_department crc_sd on crc_sd.id=crc_ed.dept_id and crc_sd.is_delete=0
  39. left join yuxin_contract.cont_renter_info cri on cri.is_delete=0 and cri.customer_type=1 and cri.contract_id=crc.id
  40. left join yuxin_setting.setting_employee_info sign_emp on sign_emp.id=crc.sign_emp_id and sign_emp.is_delete=0
  41. left join yuxin_setting.setting_employee_info mt_emp on mt_emp.id=crc.maintainer_id and mt_emp.is_delete=0
  42. left join (
  43. select bd.biz_id, bd.fee_subject_id, sd.name 'subject_name'
  44. from yuxin_finance.fin_finance_bill_detail bd
  45. left join yuxin_setting.setting_dictionary sd on sd.id=bd.fee_subject_id
  46. where bd.is_valid=1 and bd.is_delete=0 and bd.biz_type=2
  47. group by bd.biz_id, bd.fee_subject_id, sd.name
  48. ) _total on _total.biz_id= crc.id
  49. where _total.fee_subject_id is not null
  50. ) m
  51. cross join (
  52. select '应付' as 'kind' union all
  53. select '应收' as 'kind' union all
  54. select '实收' as 'kind' union all
  55. select '实付' as 'kind' union all
  56. select '待收' as 'kind' union all
  57. select '待付' as 'kind') n) mm
  58. left join yuxin_bi.bi_bill_detail_month dm
  59. on dm.fee_subject_id=mm.fee_subject_id and
  60. dm.kind=mm.kind and
  61. dm.contract_id=mm.合同ID;