Welcome Guest, Not a member yet? Register   Sign In
Code mess - I need help with the logic
#1

[eluser]originalmmd[/eluser]
*EDIT: Please note, I am not a programmer. Therefore what is obvious for most of you might not be fore me. I really just need a helpin' hand / point in the right direction. Thank you in advance to anyone who can help.*

Hey all. This is one of those rewrite my program for me posts but I'm out of options here. We got this code from a programmer we hired and it's not working. Tried fiddling with it myself, tried oDesk (programmer didn't understand the problem) now I'm out of time on the project and over budget. Going to try and fix it myself, but i need some help with the logic.

The program does time and attendance per project for a small factor. Here's what it's supposed to do:
On page / controller index.php/employeeselect
The user their project and name and clocks in. The model checks if the user is already logged in, if not, it writes a punch to table punch. If user is logged in, the model selects the last logged in record, adds the times together and writes to table time, then writes a punch to table punch.

If an employee punches on the same job they are currently punched into, the system calculates time spent on the job and writes to the table time. Then the user is automatically punched into "waiting time".

All hours that are not clocked on a project, should be clocked to waiting time to make 8 hours for the day between 7am and 4:30pm.

index.php/reports
generates a report of the employee for all projects between start date and end date

index.php/reports2
generates a report of all employees working on a specified project between start date and end date

There are 4 cron jobs:
one that runs at 7:00 to clock everyone into waiting time.
one that runs at 12:30 to clock everyone into after hours.
on that runs at 13:00 to clock everyone back to waiting time.
and one that runs at 16:30 to clock everyone to after hours.

The problem that we have at the moment is that the logic is not working and the employees are being logged out and in of jobs and waiting time in the wrong order.

Here's the code for employeeselect:
Controller

Code:
<?php
class employeeselect extends CI_Controller {
// function index() {
//  $this -> load -> model('employeeselect_model');

//  $project = $this -> employeeselect_model -> get_project();
//  $data['project'] = $project;
//  $data['select_jobs'] = $this -> employeeselect_model -> get_project();

//  $person = $this -> employeeselect_model -> get_person();
//  $data['person'] = $person;
//  $data['page_title'] = "Log your time!";
// $this -> load -> view('employeeselect_view', $data);
// }

function index() {
  $data = array();
  $this -> load -> model('employeeselect_model');
  $data['select_options'] = $this -> employeeselect_model -> get_jobs();
  $data['select_people'] = $this -> employeeselect_model -> get_people();
  
  $this -> load -> view('employeeselect_view', $data);
}

function punch() {
  $this -> load -> model('employeeselect_model');
  $this -> employeeselect_model -> newtimespan($_POST);
  $this -> employeeselect_model -> newpunch_in();

  redirect('employeeselect', $data);
}

function punchout() {
  $this -> load -> model('employeeselect_model');
  //$this -> employeeselect_model -> timespans($_POST);
  $this -> employeeselect_model -> logoff();

  redirect('employeeselect', $data);
}

}
?>

#2

[eluser]originalmmd[/eluser]
Model

Code:
<?php
class employeeselect_model extends CI_Model {

function __construct() {
  // Call the Model constructor
  parent::__construct();
  $this -> load -> database();
}

public function get_jobs() {
  $this -> db -> select('*');

  //change this to the two main values you want to use
  $this -> db -> from('jobs');
  $this -> db -> order_by("description", "asc");
  //$this -> db -> where('category_online', 1);
  $query = $this -> db -> get();
  foreach ($query->result_array() as $row) {
   $data[] = $row;
  }
  return $data;
}

public function get_people() {
  $this -> db -> select('*');
  //change this to the two main values you want to use
  $this -> db -> from('person');
  $this -> db -> order_by("department", "asc");
  $this -> db -> order_by("userid", "asc");

  //$this -> db -> where('category_online', 1);
  $query = $this -> db -> get();
  foreach ($query->result_array() as $row) {
   $data[] = $row;

  }
  return $data;
}

function get_person() {
  $query = $this -> db -> get('person');
  return $query -> result();
}

function get_phase() {
  $this -> load -> database();
  $data = $_POST;
  $data['phaseid'] = 1;

}


function newtimespan() {

  $this -> load -> database();
  $this -> load -> helper('date');
  $datapst = $_POST;

  $this -> db -> from('punch');
  $this -> db -> where('firstname', $datapst['firstname']);
  $this -> db -> order_by("id", "desc");
  $this -> db -> limit(1);
  $query = $this -> db -> get();
  foreach ($query->result_array() as $row) {
   $data = $row;
  }
  if ($data['projectid'] != '302') {
   $data['workperiod'] = 'nothing';
  //$current_time = getdate();

  //MAKE TIME
  $now = time();
  $data['time'] = elapsed_time($data['timest'], $now);
  $data['entrydate'] = timestamp_to_mysqldatetime($now);
  $data['activitydate'] = timestamp_to_mysqldatetime($data['timest']);
  unset($data['id']);
  $this -> db -> insert('time', $data);
  
  }
  elseif ($data['projectid'] == '302' && $data['status'] = '1') {
   $data['workperiod'] = 'nothing';
  //$current_time = getdate();

  //MAKE TIME
  $now = time();
  $data['time'] = elapsed_time($data['timest'], $now);
  $data['entrydate'] = timestamp_to_mysqldatetime($now);
  $data['activitydate'] = timestamp_to_mysqldatetime($data['timest']);
  unset($data['id']);
  $this -> db -> insert('time', $data);
  }
  

}

function newpunch_in() {
  
  $this -> load -> database();
  $data = $_POST;

  //get status
  $sql = "SELECT status, projectid FROM punch WHERE firstname=? ORDER BY id DESC LIMIT 1";
  $query = $this -> db -> query($sql, array($data['firstname']));
  foreach ($query->result() as $row) {
   $data['status'] = $row -> status;
   $datanew['projectid'] = $row -> projectid;
  }

  //get phase

  $data['phaseid'] = 1;
  $data['activityid'] = '1';
  $data['todate'] = '1';
  $data['remark'] = '1';
  $data['timest'] = now();
  $data['workperiod'] = '1';

  foreach ($datanew as $rows) {
  
  
  //set status
  if ($datanew['projectid'] != '302' && $datanew['projectid'] == $data['projectid']) {
   $data['projectid'] = '302';
   $data['status'] = '1';
  }
  else {
   $data['status'] = '0';
   $datanew['projectid'] = $data['projectid'];
  }
  }
  unset($data['id']);  
  $this -> db -> insert('punch', $data);
}
#3

[eluser]originalmmd[/eluser]
View

Code:
<?php
// load header template
$this -> load -> view('inc/html/header');
$this -> load -> helper('form');
$this -> load -> helper('date');
$this -> load -> helper('url');
?>

<?php
echo form_open('employeeselect/punch/');

//echo form_dropdown('categories', $select_options,'',$js);

echo "<div class=\"fifty\">";
echo "<h3>Project</h3>";
foreach ($select_options as $row) :
$jcolor = array('onclick' => 'Jsomewhere();' );
$lblid = 'id= "' . $row['description'] . '" ';
echo form_radio('projectid', $row['id'], FALSE, $lblid);
echo form_label($row['description'], $row['description'], $jcolor);
//echo "</br>";
endforeach;
echo "</div>";
echo "<div class=\"space\">";
echo "</div>";
echo "<div class=\"fifty2\">";
echo "<h3 id=\"nameheader\">Name</h3>";
foreach ($select_people as $rows) :
$color = array('style' => 'color:#000; background-color:'.$rows['department'], 'id' => $rows['firstname'], 'class' => 'person');
$lblid = array('name' => 'firstname','value' => $rows['firstname'],'checked' => 'FALSE' , 'id' => $rows['firstname'], 'class' => 'prsn' );
echo form_radio($lblid);
echo form_label($rows['userid'] . ' ' . $rows['firstname'], $rows['firstname'], $color);
//echo "</br>";
endforeach;

echo "</div>";
echo form_hidden('status', 'x');
echo "<div>/div>";
//echo form_dropdown('categories', $select_people);
//echo "<div class=\"float\">";
echo form_submit('', 'clock in');
$red = "class=\"red\"";
echo form_submit('', 'clock out', $red);
echo form_submit('', 'After Hours', $red);
//echo "</div>";
echo form_close();
echo form_open('');
echo form_submit('', 'Cancel');
echo form_close();
echo form_open('/reports/');
echo form_submit('', 'Admin');
echo form_close();
?&gt;

&lt;?php
// load footer template
include 'inc/html/footer.php'
?&gt;
#4

[eluser]solid9[/eluser]
CODE SMELL

#5

[eluser]originalmmd[/eluser]
Thank you solid9, very helpful identification of an obvious problem.




Theme © iAndrew 2016 - Forum software by © MyBB