select
b.employee_number as x_employee_number,
b.name as x_employee_name,
b_1.name as x_dep_manager,
b_2.name as x_parent,
b.hiredate as x_hiredate,
c.name as department_name,
d.name as job_name,
e.name as x_legal_entity,
case f.name when 'Labor Contract' then '劳动合同' when 'None Disclosure Agreement' then '保密协议' when 'Competitive Restriction' then '竞业协议' when 'Service Agreement' then '劳务协议' when '兼职协议' then '兼职协议' end as x_contract_category,
a.date_end as x_date_end,
to_number(to_char(a.date_end-now()+ interval '1 days','dd'), '999999999') as x_rest_days
from hr_contract a
left join hr_employee b on a.employee_id=b.id
left join hr_employee b_1 on a.dep_manager=b_1.id
left join hr_employee b_2 on a.parent_id=b_2.id
left join hr_department c on b.department_id=c.id
left join hr_job d on b.job_id=d.id
left join legal_entity e on b.legal_entity=e.id
left join hr_contract_agreement_category f on a.contract_agreement_id=f.id
where a.state='done' and a.date_end>=now() and a.active=True order by x_rest_days