diff --git a/controllers/jobs_controller.php b/controllers/jobs_controller.php index 9eae3f3d..979c6857 100755 --- a/controllers/jobs_controller.php +++ b/controllers/jobs_controller.php @@ -108,6 +108,11 @@ class JobsController extends AppController { $monthList = $this->getYearMonths($jobs); $this->set('monthList', $monthList); + + + $Fyears = $this->getFinancialYears($jobs); + $this->set('Fyears',$Fyears); + /* $this->set('jobs', $jobs); $this->set('customers', $this->Job->Enquiry->Customer->find('list')); $this->set('currencies', $this->Job->Currency->find('list'));*/ @@ -116,11 +121,9 @@ class JobsController extends AppController { - function viewReport($year, $month) { - $this->layout = 'ajax'; + function viewReport($year, $month = null) { + //$this->layout = 'ajax'; - $this->set('year', $year); - $this->set('month', $month); if(isset($month) && isset($year)) { //After a Specific Month for a Year @@ -140,6 +143,11 @@ class JobsController extends AppController { $jobs = $this->Job->find('all', array('conditions'=> array('Job.date_order_received BETWEEN ? AND ?'=>array($startDate, $endDate)))); + + $this->set('year', $year); + $this->set('month', $month); + + $this->set('jobs', $jobs); $this->set('principleList',$this->Job->Enquiry->Principle->find('list')); @@ -150,8 +158,48 @@ class JobsController extends AppController { } - elseif(isset($year) && !isset($month)) { //After a whole Years Stats. + elseif(isset($year) && !isset($month)) { //Requested a whole Financial Year Jobs + $this->set('year', $year); + + + $lastDayUnix = $this->getLastDayFY($year); + $firstDayUnix = $this->getFirstDayFY($year,true); + + + $lastDayDate = date('Y-m-d', $lastDayUnix); + $firstDayDate = date('Y-m-d', $firstDayUnix); + + $dateStringFormat = 'j F Y'; + + $dateString['first'] = date($dateStringFormat, $firstDayUnix); + $dateString['last'] = date($dateStringFormat, $lastDayUnix); + + + $jobs = $this->Job->find('all', array('conditions'=> + array('Job.date_order_received BETWEEN ? AND ?'=>array($firstDayDate, $lastDayDate)) + ) + ); + $this->set('jobs',$jobs); + + + $lastYear = $year - 1; + + $jobRangeTitle = $lastYear.' - '.$year.' ('.$dateString['first']." to ".$dateString['last'].')'; + + $this->set('jobRangeTitle', $jobRangeTitle); + + + $this->set('principleList',$this->Job->Enquiry->Principle->find('list')); + $this->set('sale_category_options', $this->sale_category_options()); + $this->set('sale_category_array', $this->sale_category_array()); + $this->set('job_type_options', $this->job_type_options()); + $this->set('shipment_category_options', $this->shipment_category_options()); + $this->set('freight_paid_by_options',$this->freight_paid_options()); + $this->set('gst_options', $this->gst_options()); + + $this->set('totals', $this->getTotals($jobs)); + //print_r($jobs); } else { @@ -167,7 +215,7 @@ class JobsController extends AppController { /** - * Generate aggregates for the financial data in $jobs. + * Generate aggregates for the financial data in $jobs. * * Procedure: * 1. Loop over the $jobs. @@ -176,19 +224,113 @@ class JobsController extends AppController { * * @param $jobs */ - function getTotals($jobs, $fields) { + function getTotals($jobs) { + + $fields = array( + 'company_gross_sales_aud' => 'total', + 'net_sales_aud' =>'total', + 'gross_profit_aud' => 'total', + 'net_export_sales_aud' => 'total', + 'gross_profit_exports_aud' => 'total', + 'gp_percent' => 'average', + 'gross_australian_sales_foreign_currency' => 'total', + 'net_australian_sales_foreign_currency' => 'total', + 'gross_profit_value_australian_sales_foreign_currency' => 'total', + 'gross_export_sales_foreign_currency' => 'total', + 'net_export_sales_foreign_currency' => 'total', + 'gross_profit_value_after_discount_exports_foreign_currency' => 'total', + 'gross_commissions' => 'total', + 'net_commissions' => 'total' + ); + + $totals = array(); + + foreach($fields as $field => $action) { + $totals[$field] = 0; + } + + $jobCount = 0; + foreach($jobs as $job) { + if($job['Job']['job_status'] == 'JOB CANCELLED' || + $job['Job']['job_status'] == 'JOB ON HOLD' ) { + continue; //Don't total cancelled or on hold jobs. + } + else { //Total all other jobs. + + foreach(array_keys($job['Job']) as $key) { + if(isset($fields[$key])) { + $totals[$key] += $job['Job'][$key]; + } + } + + $jobCount++; + } + } + + foreach($totals as $key => $value) { + if($fields[$key] == 'average') { + $avg = $value / $jobCount; + $totals[$key] = $avg; + } + } + + return $totals; } - function getFinancialYears() { - + function getFinancialYears($jobs) { + + $Fyears = array(); + + foreach($jobs as $job) { + + $time = strtotime($job['Job']['date_order_received']); + $year = date('Y', $time); //ISO-8601 year number + + + $startFYunix = $this->getFirstDayFY($year); //1 July of $year. + + $month = date('n', $time); + if($time < $startFYunix) { //Date is before 1 July $year. + $lastYear = $year - 1; + + if(!isset($Fyears[$year])) { + $Fyears[$year] = 1; + } + else { + $Fyears[$year]++; + } + } + else { //Date is After 1 July $year. + + $nextYear = $year + 1; + if(!isset($Fyears[$nextYear])) { + $Fyears[$nextYear] = 1; + } + else { + $Fyears[$nextYear]++; + } + } + + } + krsort($Fyears); //Sort the array in reverse order, most recent to oldest. + + return $Fyears; } + function getFirstDayFY($year,$prevYear = false) { + if($prevYear == false) { + return mktime(0,0,0,7,1,$year); + } + else { + return mktime(0,0,0,7,1,$year-1); + } + } - function getFirstFYday($year) { - + function getLastDayFY($year) { + return mktime(23,59,59,6,30,$year); } @@ -422,7 +564,7 @@ class JobsController extends AppController { } - function autocomplete() { + function autocomplete() { $this->layout = 'ajax'; $query = strtolower($_GET["term"]); diff --git a/views/jobs/reports.ctp b/views/jobs/reports.ctp index f63bc7bb..a6610b84 100644 --- a/views/jobs/reports.ctp +++ b/views/jobs/reports.ctp @@ -3,27 +3,29 @@

Book 1 - Reports

-
- $monthArr): - ?> -