86 lines
3.0 KiB
XML
86 lines
3.0 KiB
XML
<?xml version="1.0" encoding="UTF-8"?>
|
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
|
<mapper namespace="com.zioinfo.hrm.payroll.PayrollMapper">
|
|
|
|
<select id="findPayrolls" resultType="map">
|
|
SELECT p.id, p.year, p.month, p.status, p.total_amount,
|
|
p.processed_by, p.processed_at,
|
|
(SELECT COUNT(*) FROM hrm_payslips ps WHERE ps.payroll_id=p.id) AS payslip_count
|
|
FROM hrm_payroll p
|
|
<where>
|
|
<if test="year > 0">AND p.year = #{year}</if>
|
|
<if test="month > 0">AND p.month = #{month}</if>
|
|
<if test="deptId != null">AND p.dept_id = #{deptId}</if>
|
|
</where>
|
|
ORDER BY p.year DESC, p.month DESC
|
|
LIMIT #{limit} OFFSET #{offset}
|
|
</select>
|
|
|
|
<select id="countPayrolls" resultType="long">
|
|
SELECT COUNT(*) FROM hrm_payroll p
|
|
<where>
|
|
<if test="year > 0">AND p.year = #{year}</if>
|
|
<if test="month > 0">AND p.month = #{month}</if>
|
|
<if test="deptId != null">AND p.dept_id = #{deptId}</if>
|
|
</where>
|
|
</select>
|
|
|
|
<select id="findPayslip" resultType="map">
|
|
SELECT ps.*, e.name AS emp_name, e.emp_no, d.dept_name
|
|
FROM hrm_payslips ps
|
|
JOIN hrm_employees e ON ps.emp_id = e.id
|
|
LEFT JOIN hrm_departments d ON e.department_id = d.id
|
|
WHERE ps.emp_id=#{empId} AND ps.year=#{year} AND ps.month=#{month}
|
|
</select>
|
|
|
|
<select id="findPayslipsByEmp" resultType="map">
|
|
SELECT ps.id, ps.year, ps.month, ps.base_salary, ps.total_deduction, ps.net_salary
|
|
FROM hrm_payslips ps
|
|
WHERE ps.emp_id=#{empId} AND ps.year=#{year}
|
|
ORDER BY ps.month DESC
|
|
</select>
|
|
|
|
<insert id="insertPayroll">
|
|
INSERT INTO hrm_payroll (year, month, status, processed_by, processed_at)
|
|
VALUES (#{year}, #{month}, #{status}, #{processedBy}, #{processedAt})
|
|
ON CONFLICT (year, month) DO UPDATE SET status=#{status}, processed_at=NOW()
|
|
</insert>
|
|
|
|
<update id="updatePayrollStatus">
|
|
UPDATE hrm_payroll SET status=#{status} WHERE id=#{id}
|
|
</update>
|
|
|
|
<select id="findSalaryItems" resultType="map">
|
|
SELECT id, item_code, item_name, item_type, is_taxable, sort_order
|
|
FROM hrm_salary_items WHERE active=true ORDER BY sort_order
|
|
</select>
|
|
|
|
<select id="findSalaryByEmpId" resultType="map">
|
|
SELECT s.*, e.name AS emp_name, e.emp_no
|
|
FROM hrm_emp_salaries s
|
|
JOIN hrm_employees e ON s.emp_id = e.id
|
|
WHERE s.emp_id=#{empId}
|
|
ORDER BY s.effective_date DESC LIMIT 1
|
|
</select>
|
|
|
|
<insert id="upsertSalary">
|
|
INSERT INTO hrm_emp_salaries (emp_id, base_salary, allowances, bonus, effective_date)
|
|
VALUES (#{empId}, #{baseSalary}, #{allowances}, #{bonus}, #{effectiveDate})
|
|
ON CONFLICT (emp_id) DO UPDATE SET
|
|
base_salary=#{baseSalary}, allowances=#{allowances},
|
|
bonus=#{bonus}, effective_date=#{effectiveDate}
|
|
</insert>
|
|
|
|
<select id="getYearlySummary" resultType="map">
|
|
SELECT
|
|
SUM(net_salary) AS total_net,
|
|
SUM(base_salary) AS total_base,
|
|
SUM(total_deduction) AS total_deduction,
|
|
COUNT(*) AS month_count
|
|
FROM hrm_payslips
|
|
WHERE emp_id=#{empId} AND year=#{year}
|
|
</select>
|
|
|
|
</mapper>
|