Skip to content
This repository has been archived by the owner on Dec 6, 2019. It is now read-only.

Commit

Permalink
pilot pay per schedule, moved into status change code
Browse files Browse the repository at this point in the history
  • Loading branch information
Nabeel Shahzad committed Feb 20, 2011
1 parent a5e6189 commit b64e3ab
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 150 deletions.
105 changes: 30 additions & 75 deletions admin/modules/PIREPAdmin/PIREPAdmin.php
Expand Up @@ -17,10 +17,8 @@
* @license http://creativecommons.org/licenses/by-nc-sa/3.0/
*/

class PIREPAdmin extends CodonModule
{
public function HTMLHead()
{
class PIREPAdmin extends CodonModule {
public function HTMLHead() {
switch ($this->controller->function) {
case 'viewpending':
case 'viewrecent':
Expand All @@ -30,13 +28,11 @@ public function HTMLHead()
}
}

public function index()
{
public function index() {
$this->viewpending();
}

protected function post_action()
{
protected function post_action() {
if (isset($this->post->action)) {
switch ($this->post->action) {
case 'addcomment':
Expand All @@ -63,8 +59,7 @@ protected function post_action()
}
}

public function viewpending()
{
public function viewpending() {
$this->post_action();

$this->set('title', 'Pending Reports');
Expand All @@ -82,27 +77,23 @@ public function viewpending()
}


public function pilotpireps()
{
public function pilotpireps() {
$this->post_action();

$this->set('pending', false);
$this->set('load', 'pilotpireps');

$this->set('pireps', PIREPData::findPIREPS(array('p.pilotid' => $this->get->
pilotid)));
$this->set('pireps', PIREPData::findPIREPS(array('p.pilotid' => $this->get->pilotid)));
$this->render('pireps_list.tpl');
}


public function rejectpirep()
{
public function rejectpirep() {
$this->set('pirepid', $this->get->pirepid);
$this->render('pirep_reject.tpl');
}

public function viewrecent()
{
public function viewrecent() {
$this->set('title', Lang::gs('pireps.view.recent'));
$this->set('pireps', PIREPData::GetRecentReports());
$this->set('descrip', 'These pilot reports are from the past 48 hours');
Expand All @@ -113,21 +104,22 @@ public function viewrecent()
$this->render('pireps_list.tpl');
}

public function approveall()
{
public function approveall() {
echo '<h3>Approve All</h3>';

$allpireps = PIREPData::findPIREPS(array('p.accepted' => PIREP_PENDING));

$total = count($allpireps);
$count = 0;
foreach ($allpireps as $pirep_details) {

if ($pirep_details->aircraft == '') {
continue;
}

# Update pilot stats
SchedulesData::IncrementFlownCount($pirep_details->code, $pirep_details->
flightnum);
SchedulesData::IncrementFlownCount($pirep_details->code, $pirep_details->flightnum);
PIREPData::ChangePIREPStatus($pirep_details->pirepid, PIREP_ACCEPTED); // 1 is accepted
//PilotData::UpdateFlightData($pirep_details->pilotid, $pirep_details->flighttime, 1);
PilotData::UpdatePilotStats($pirep_details->pilotid);
Expand All @@ -145,8 +137,7 @@ public function approveall()
echo "$count of $total were approved ({$skipped} has errors)";
}

public function viewall()
{
public function viewall() {
$this->post_action();

if (!isset($this->get->start) || $this->get->start == '')
Expand Down Expand Up @@ -192,8 +183,7 @@ public function viewall()
$this->render('pireps_list.tpl');
}

public function editpirep()
{
public function editpirep() {
$this->set('pirep', PIREPData::GetReportDetails($this->get->pirepid));
$this->set('allairlines', OperationsData::GetAllAirlines());
$this->set('allairports', OperationsData::GetAllAirports());
Expand All @@ -205,14 +195,12 @@ public function editpirep()
$this->render('pirep_edit.tpl');
}

public function viewcomments()
{
public function viewcomments() {
$this->set('comments', PIREPData::GetComments($this->get->pirepid));
$this->render('pireps_comments.tpl');
}

public function deletecomment()
{
public function deletecomment() {
if (!isset($this->post)) {
return;
}
Expand All @@ -225,14 +213,12 @@ public function deletecomment()
$this->render('core_success.tpl');
}

public function viewlog()
{
public function viewlog() {
$this->set('report', PIREPData::GetReportDetails($this->get->pirepid));
$this->render('pirep_log.tpl');
}

public function addcomment()
{
public function addcomment() {
if (isset($this->post->submit)) {
$this->add_comment_post();

Expand All @@ -248,8 +234,7 @@ public function addcomment()

/* Utility functions */

protected function add_comment_post()
{
protected function add_comment_post() {
$comment = $this->post->comment;
$commenter = Auth::$userinfo->pilotid;
$pirepid = $this->post->pirepid;
Expand All @@ -273,29 +258,21 @@ protected function add_comment_post()
* Approve the PIREP, and then update
* the pilot's data
*/
protected function approve_pirep_post()
{
protected function approve_pirep_post() {
$pirepid = $this->post->id;

if ($pirepid == '')
return;

$pirep_details = PIREPData::GetReportDetails($pirepid);
$pirep_details = PIREPData::getReportDetails($pirepid);

# See if it's already been accepted
if (intval($pirep_details->accepted) == PIREP_ACCEPTED)
return;

# Update pilot stats
SchedulesData::IncrementFlownCount($pirep_details->code, $pirep_details->flightnum);

PIREPData::ChangePIREPStatus($pirepid, PIREP_ACCEPTED); // 1 is accepted
PilotData::UpdateFlightData($pirep_details->pilotid, $pirep_details->flighttime, 1);
PilotData::UpdatePilotPay($pirep_details->pilotid, $pirep_details->flighttime);

RanksData::CalculateUpdatePilotRank($pirep_details->pilotid);
PilotData::GenerateSignature($pirep_details->pilotid);
StatsData::UpdateTotalHours();

LogData::addLog(Auth::$userinfo->pilotid, 'Approved PIREP #' . $pirepid);

# Call the event
Expand All @@ -306,8 +283,7 @@ protected function approve_pirep_post()
* Delete a PIREP
*/

protected function delete_pirep_post()
{
protected function delete_pirep_post() {
$pirepid = $this->post->id;
if ($pirepid == '')
return;
Expand All @@ -323,8 +299,7 @@ protected function delete_pirep_post()
* Reject the report, and then send them the comment
* that was entered into the report
*/
protected function reject_pirep_post()
{
protected function reject_pirep_post() {
$pirepid = $this->post->pirepid;
$comment = $this->post->comment;

Expand All @@ -334,16 +309,6 @@ protected function reject_pirep_post()
PIREPData::changePIREPStatus($pirepid, PIREP_REJECTED); // 2 is rejected
$pirep_details = PIREPData::getReportDetails($pirepid);

// If it was previously accepted, subtract the flight data
if (intval($pirep_details->accepted) == PIREP_ACCEPTED) {
PilotData::updateFlightData($pirep_details->pilotid, -1 * floatval($pirep->flighttime), -1);
}

//PilotData::UpdatePilotStats($pirep_details->pilotid);
RanksData::CalculateUpdatePilotRank($pirep_details->pilotid);
PilotData::resetPilotPay($pirep_details->pilotid);
StatsData::UpdateTotalHours();

// Send comment for rejection
if ($comment != '') {
$commenter = Auth::$userinfo->pilotid; // The person logged in commented
Expand All @@ -364,11 +329,8 @@ protected function reject_pirep_post()
CodonEvent::Dispatch('pirep_rejected', 'PIREPAdmin', $pirep_details);
}

protected function edit_pirep_post()
{
if ($this->post->code == '' || $this->post->flightnum == '' || $this->post->
depicao == '' || $this->post->arricao == '' || $this->post->aircraft == '' || $this->
post->flighttime == '') {
protected function edit_pirep_post() {
if ($this->post->code == '' || $this->post->flightnum == '' || $this->post->depicao == '' || $this->post->arricao == '' || $this->post->aircraft == '' || $this->post->flighttime == '') {
$this->set('message', 'You must fill out all of the required fields!');
$this->render('core_error.tpl');
return false;
Expand All @@ -386,13 +348,7 @@ protected function edit_pirep_post()
$fuelcost = $this->post->fuelused * $this->post->fuelunitcost;

# form the fields to submit
$data = array('pirepid' => $this->post->pirepid, 'code' => $this->post->code,
'flightnum' => $this->post->flightnum, 'depicao' => $this->post->depicao,
'arricao' => $this->post->arricao, 'aircraft' => $this->post->aircraft,
'flighttime' => $this->post->flighttime, 'load' => $this->post->load, 'price' =>
$this->post->price, 'pilotpay' => $this->post->pilotpay, 'fuelused' => $this->
post->fuelused, 'fuelunitcost' => $this->post->fuelunitcost, 'fuelprice' => $fuelcost,
'expenses' => $this->post->expenses);
$data = array('pirepid' => $this->post->pirepid, 'code' => $this->post->code, 'flightnum' => $this->post->flightnum, 'depicao' => $this->post->depicao, 'arricao' => $this->post->arricao, 'aircraft' => $this->post->aircraft, 'flighttime' => $this->post->flighttime, 'load' => $this->post->load, 'price' => $this->post->price, 'pilotpay' => $this->post->pilotpay, 'fuelused' => $this->post->fuelused, 'fuelunitcost' => $this->post->fuelunitcost, 'fuelprice' => $fuelcost, 'expenses' => $this->post->expenses);

if (!PIREPData::updateFlightReport($this->post->pirepid, $data)) {
$this->set('message', 'There was an error editing your PIREP');
Expand All @@ -408,8 +364,7 @@ protected function edit_pirep_post()

// Add a comment
if (trim($this->post->comment) != '' && $submit != 'reject pirep') {
PIREPData::AddComment($this->post->pirepid, Auth::$userinfo->pilotid, $this->
post->comment);
PIREPData::AddComment($this->post->pirepid, Auth::$userinfo->pilotid, $this->post->comment);
}

if ($submit == 'accept pirep') {
Expand Down
65 changes: 59 additions & 6 deletions core/common/PIREPData.class.php
Expand Up @@ -293,8 +293,7 @@ public static function getReportDetails($pirepid) {
LEFT JOIN ' . TABLE_PREFIX . 'airports AS dep ON dep.icao = p.depicao
LEFT JOIN ' . TABLE_PREFIX . 'airports AS arr ON arr.icao = p.arricao
LEFT JOIN ' . TABLE_PREFIX . 'aircraft a ON a.id = p.aircraft
LEFT JOIN ' . TABLE_PREFIX .
'schedules s ON s.code = p.code AND s.flightnum = p.flightnum
LEFT JOIN ' . TABLE_PREFIX . 'schedules s ON s.code = p.code AND s.flightnum = p.flightnum
WHERE p.pilotid=u.pilotid AND p.pirepid=' . $pirepid;

$row = DB::get_row($sql);
Expand Down Expand Up @@ -1064,14 +1063,68 @@ public static function getAllReportsForPilot($pilotid) {
}

/**
* Change the status of a PIREP. For the status, use the
* constants:
* Change the status of a PIREP. For the status, use the constants:
* PIREP_PENDING, PIREP_ACCEPTED, PIREP_REJECTED,PIREP_INPROGRESS
*
* Also handle paying the pilot, and handle PIREP rejection, etc
*
* @deprecated Use editPIREPFields instead
*/
public static function ChangePIREPStatus($pirepid, $status) {
return self::editPIREPFields($pirepid, array('accepted' => $status));
public static function changePIREPStatus($pirepid, $status) {

# Look up the status of the PIREP of previous
$pirep_details = PIREPData::getReportDetails($pirepid);

if(!$pirep_details)) {
return false;
}

if($pirep_details->accepted == $status) {
return true;
}

$ret = self::editPIREPFields($pirepid, array('accepted' => $status));

# Do something if the PIREP was previously marked as pending
if($pirep_details->accepted == PIREP_PENDING) {

if($status == PIREP_ACCEPTED) {

PilotData::updateFlightData($pirep_details->pilotid, $pirep_details->flighttime, 1);

# Handle pilot pay
if(!(empty($pirep_details->payforflight))) {

# Pay by schedule
$sql = 'UPDATE ' . TABLE_PREFIX . "pilots
SET totalpay=totalpay+{$pirep_details->payforflight}
WHERE pilotid={$pirep_details->pilotid}";

DB::query($sql);

} else {
# Pay by hour
PilotData::updatePilotPay($pirep_details->pilotid, $pirep_details->flighttime);
}

SchedulesData::incrementFlownCount($pirep_details->code, $pirep_details->flightnum);

} elseif($status == PIREP_REJECTED) {
// Do nothing, since nothing in the PIREP was actually counted
}

} elseif($pirep_details->accepted == PIREP_ACCEPTED) { # If already accepted

if($status == PIREP_REJECTED) {
PilotData::updateFlightData($pirep_details->pilotid, -1 * floatval($pirep->flighttime), -1);
}
}

RanksData::calculateUpdatePilotRank($pirep_details->pilotid);
PilotData::generateSignature($pirep_details->pilotid);
StatsData::updateTotalHours();

return $ret;
}

/**
Expand Down
1 change: 1 addition & 0 deletions core/common/PilotData.class.php
Expand Up @@ -629,6 +629,7 @@ public static function resetPilotPay($pilotid) {
*
*/
public static function updatePilotPay($pilotid, $flighthours) {

$sql = 'SELECT payrate
FROM ' . TABLE_PREFIX . 'ranks r, ' . TABLE_PREFIX . 'pilots p
WHERE p.rank=r.rank
Expand Down

0 comments on commit b64e3ab

Please sign in to comment.