【问题标题】:How to create a payroll report out of an attendance records using PHP如何使用 PHP 从考勤记录中创建工资单报告
【发布时间】:2016-11-23 23:35:25
【问题描述】:

我有一个名为:timesheets 的数据库表,其中包含以下列和数据:

employee_id  employee_name  year  month  day  timein  timeout  department
1            dave           2016  09     15   8       4        finance
1            dave           2016  09     16   8       4        finance
1            dave           2016  09     17   8       4        finance
2            frank          2016  09     15   8       4        purchase
2            frank          2016  09     16   8       4        purchase

如上所示,它记录了员工的每日出勤情况。我想要的是使用 PHP 从这些出勤记录创建一个工资单报告,该报告在单独的 html 表中显示每个部门的每个员工的总工作时间,如下所示:

财务部门工资单html表格

employe_id | employee_name  | total_working_hours
-----------+----------------+---------------------
1          | dave           | 24 (8 hrs * 3 days)

采购部门工资单html表格

employe_id | employee_name  | total_working_hours
-----------+----------------+---------------------
1          | frank          | 16

请注意,我不知道所有员工的 ID,所以代码应该只列出使用 PHP / MYSQL 按部门分组的每个人

【问题讨论】:

  • 你为什么不使用任何数据库?
  • 我的意思是,您是否使用数据库系统来存储和获取数据?
  • 那么到目前为止你尝试过什么?
  • 想不出什么

标签: php


【解决方案1】:

有几个视图可以完成这项工作:一个用于工作时间,一个用于员工,然后您可以加入它们并对您的过滤器进行编程,如下所示:

-- Query the working hours
CREATE VIEW
 vw_working_hours
AS
SELECT
    employee_id,
    year,
    month, 
    SUM((timeout+12) - timein ) AS total_working_hours 
FROM
    timesheets
GROUP BY
    employee_id,
    year,
    month;

-- Query the employees
CREATE VIEW
    vw_employees
AS

SELECT 
    DISTINCT
    employee_id,
    employee_name,
    department
FROM
    timesheets;

-- This query is the actual report
-- just had to filter by department
-- in your script
SELECT
    wh.employee_id,
    emp.employee_name,
    wh.total_working_hours
FROM
    vw_working_hours AS wh
JOIN
    vw_employees AS emp
ON
    wh.employee_id = emp.employee_id
WHERE
    wh.year = 2016
    AND
        wh.month = '09'
    AND
        emp.department = 'finance';

或者,在单个查询中(无视图):

SELECT
    wh.employee_id,
    emp.employee_name,
    wh.total_working_hours
FROM
    (
    SELECT
        employee_id,
        year,
        month, 
        SUM((timeout+12) - timein ) AS total_working_hours 
    FROM
        timesheets
    GROUP BY
        employee_id,
        year,
        month

    ) AS wh
JOIN
    (
    SELECT 
        DISTINCT
        employee_id,
        employee_name,
        department
    FROM
        timesheets

    ) AS emp
ON
    wh.employee_id = emp.employee_id
WHERE
    wh.year = 2016
    AND
        wh.month = '09'
    AND
        emp.department = 'finance';

在 php 中,您应该使用循环和累积变量。首先,您应该使用这样的查询进行预过滤:

SELECT
  *
FROM
  timesheets
WHERE
  department = 'finance'
  AND
    year = 2016
  AND
    month = '09'
  ORDER BY
    employee_id;

那么如果结果在一个名为 $rows 的多维数组中,在 php 中是这样的:

$employee_id = $rows[0]['employee_id'];
    $employee_name = $rows[0]['employee_name'];
    $accumulated = 0;

    foreach($rows as $row) {
      $total_working_hours = ($row['timeout'] + 12) - $row['timein'];
      if ( $row['employee_id'] == $employee_id ) {
        //  Same employee, acumulate
        $accumulated += $total_working_hours;
      } else {
        //  Another employee, pass the acumulation to result
        $rowTmp['employee_id'] = $employee_id;
        $rowTmp['employee_name'] = $employee_name;
        $rowTmp['total_working_hours'] = $accumulated;
        $result[] = $rowTmp;
        //  Updated the accumulation variables
        //  new employee
        $employee_id = $row['employee_id'];
        $employee_name = $row['employee_name'];
        //  reset accumulated
        $accumulated = $total_working_hours;
      }
    }

    // at the end, updates the las result:
    $rowTmp['employee_id'] = $employee_id;
    $rowTmp['employee_name'] = $employee_name;
    $rowTmp['total_working_hours'] = $accumulated;
    $result[] = $rowTmp;

    // Should pass result to HTML table
    print_r( $result ); 

【讨论】:

  • 我会试试这个,然后我会报告回来。非常感谢。
  • 难道没有办法在 php 中做到这一点吗?对我来说它看起来很复杂,因为我对 SQL 知之甚少
  • 视图只创建一次,一旦创建,您就不需要在查询中使用它们。如果已经创建,请将它们从您的脚本中删除,然后再次运行它。只保留最后一部分,从“这个查询是实际的......”到底部。
  • 我已经编辑了答案以便在没有视图的情况下工作,您可以查看它。
  • 我已经再次编辑,例如在 php 中,选择你的风格。
猜你喜欢
  • 2019-01-04
  • 2014-08-13
  • 2016-07-20
  • 2012-02-27
  • 1970-01-01
  • 1970-01-01
  • 2020-10-15
  • 1970-01-01
  • 2019-04-25
相关资源
最近更新 更多