Register Child and Waitlist working

This commit is contained in:
Deon George
2014-10-22 22:22:47 +11:00
parent df7776931d
commit 48debfbd6c
21 changed files with 645 additions and 160 deletions

View File

@@ -0,0 +1,24 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This Model manages both the accounts that users use to login to the system, as well as the account where services are owned.
*
* @package Membership Database
* @category Models
* @author Deon George
* @copyright (c) 2014 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Model_Account extends lnAuth_Model_Account {
// Relationships
protected $_has_many = array(
'child'=>array('far_key'=>'id'),
'email_log'=>array('far_key'=>'id'),
'group'=>array('through'=>'account_group'),
);
public function list_children() {
return $this->child->find_all();
}
}
?>

View File

@@ -1,7 +1,7 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class supports Children belonging to a family
* This class supports Children
*
* @package Membership Database
* @category Models
@@ -10,5 +10,90 @@
* @license http://dev.leenooks.net/license.html
*/
class Model_Child extends ORM {
protected $_start = '42D'; // Minimum age to be accepted (42D = 6 Weeks)
protected $_max = '4Y'; // Maximum age
protected $_max_date = '04-30'; // Date Maximum age must be met, eg: 5Y on April 30
protected $_max_return = '12-31'; // Date to leave the center when max date reached, eg: Jan 1
protected $_has_many = array(
'room'=>array('model'=>'Room_Children','foreign_key'=>'child_id','far_key'=>'id'),
);
public function filters() {
return Arr::merge(parent::filters(),array(
'dob'=>array(array('strtotime', array(':value'))),
));
}
protected $_display_filters = array(
'dob'=>array(
array('Site::Date',array(':value')),
),
);
protected $_sub_items_load = array(
'room'=>'date_start,date_stop',
);
private function _dob() {
$x = new DateTime();
return $x->setTimestamp($this->dob);
}
public function age($asat=NULL,$format='%Yy%Mm') {
if (is_null($asat))
$asat = time();
$dob = $this->_dob();
$today = new DateTime();
$today->setTimestamp($asat);
return $format == '%w' ? sprintf('%02dw',$dob->diff($today)->days/7) : $dob->diff($today)->format($format);
}
public function date_enrol_min() {
$dob = $this->_dob();
$dob->add(new DateInterval('P'.$this->_start));
return $format ? $result->format(Kohana::$config->load('config')->date_format) : $result->format('U');
}
public function date_enrol_max($format=FALSE) {
$dob = $this->_dob();
$dob->add(new DateInterval('P'.$this->_max));
$last = new DateTime(sprintf('%s-%s',$dob->format('Y'),$this->_max_date));
$x = $dob->diff($last);
$result = new DateTime(sprintf('%s-%s',$dob->format('Y')+($x->invert ? 1 : 0),$this->_max_return));
return $format ? $result->format(Company::instance()->date_format()) : $result->format('U');
}
public function save(Validation $validation=NULL) {
$changed = $this->changed();
parent::save($validation);
// Insert into waitlist
$rco = ORM::factory('Room_Children',array('child_id'=>$this,'code'=>$_POST['room']['code']));
$rco->values($_POST['room']);
$rco->child_id = (string)$this;
foreach ($_POST['room']['R'] as $k => $v) {
if (! $v OR isset($_POST['room']['d_'.$k]))
continue;
$rco->{'d_'.$k} = NULL;
}
if ($rco->changed() AND (! $rco->save()))
$rco->reload()->values($_POST['room']);
return $this->reload();
}
}
?>

View File

@@ -14,6 +14,30 @@ class Model_Room_Children extends ORM {
'child'=>array(),
);
public function filters() {
return Arr::merge(parent::filters(),array(
'date_start'=>array(array('strtotime', array(':value'))),
'date_stop'=>array(array('strtotime', array(':value'))),
));
}
public function rules() {
$x = parent::rules();
unset($x['id']);
return $x;
}
protected $_display_filters = array(
'date_start'=>array(
array('Site::Date',array(':value')),
),
'date_stop'=>array(
array('Site::Date',array(':value')),
),
);
// @todo: Code A (availble) start/end dates cannot overlap with existing records - put in validation that it cannot be saved.
public function day($day) {
return $this->{'d_'.$day};

View File

@@ -13,7 +13,7 @@ class Model_Room_Dates extends ORM {
// @todo: Code A (availble) start/end dates cannot overlap with existing records - put in validation that it cannot be saved.
public function avail($day) {
return $this->{'d_'.$day};
return $this->{'d_'.$day} ? $this->{'d_'.$day} : 0;
}
}
?>

View File

@@ -28,24 +28,19 @@ class Model_Rooms extends ORM {
$x = $date;
while ($x<$date_end) {
// If we havent made the start date yet, we need to advance
if (! $open_dates[$x]
OR ($o->date_start > $x AND (is_null($o->date_stop) OR $o->date_stop > $date_end))
OR ((is_null($o->date_start) OR $o->date_start > $x) AND $o->date_stop > $date_end)
OR (! is_null($o->date_start) AND ! is_null($o->date_stop))) {
if (($o->date_start <= $x AND (is_null($o->date_stop) OR $o->date_stop >= $x))
OR ($o->date_stop >= $x AND (is_null($o->date_start) OR $o->date_start <= $x))
OR (is_null($o->date_start) AND is_null($o->date_start))) {
if (! isset($result[$x]) OR ! $result[$x])
$result[$x] = 0;
$result[$x] = $o->avail(date('w',$x));
$x += 86400;
continue;
}
// Check that this record covers our current date
if ($o->date_stop < $x AND ! is_null($o->date_stop))
break;
if (! isset($result[$x]) OR ! $result[$x])
$result[$x] = 0;
$result[$x] = $o->avail(date('w',$x));
$x += 86400;
}
}
@@ -66,6 +61,10 @@ class Model_Rooms extends ORM {
$result = array();
$x = $date;
// We need to set this, so that unassigned room records are found.
if (! $this->loaded())
$this->site_id = Company::instance()->site();
$date_end = $date+$days*86400;
$open_dates = $this->site->open_dates($date,$days);
foreach ($this->children->where('code','=',$code)->where_startstop($date,$date_end)->find_all() as $o) {
@@ -75,8 +74,7 @@ class Model_Rooms extends ORM {
// If we havent made the start date yet, we need to advance
if (! $open_dates[$x]
OR ($o->date_start > $x AND (is_null($o->date_stop) OR $o->date_stop > $date_end))
OR ((is_null($o->date_start) OR $o->date_start > $x) AND $o->date_stop > $date_end)
OR (! is_null($o->date_start) AND ! is_null($o->date_stop))) {
OR ((is_null($o->date_start) OR $o->date_start > $x) AND $o->date_stop > $date_end)) {
if (! isset($result[$x]) OR ! $result[$x])
$result[$x] = array();

View File

@@ -97,11 +97,23 @@ class Model_Setup extends ORM {
return $result;
}
public function open_dates($date,$days=0) {
public function open_days() {
$result = array();
//@todo this needs to change to node have any dates, since the current date may be closed, or a public holiday
foreach ($this->open_dates(Site::DateStartOfWeek(time()),7) as $date => $open)
$result[date('w',$date)] = $open;
ksort($result);
return $result;
}
public function open_dates($date,$days=0,$code='O') {
$result = array();
$date_end = $date+$days*86400;
foreach ($this->dates->where('code','=','O')->where_startstop($date,$date_end)->find_all() as $o)
foreach ($this->dates->where('code','=',$code)->where_startstop($date,$date_end)->find_all() as $o)
while ($date<$date_end) {
// If we havent made the start date yet, we need to advance
if ($o->date_start > $date AND $o->date_stop > $date_end) {
@@ -146,5 +158,20 @@ class Model_Setup extends ORM {
return $this;
}
public function total_places($date_start,$days=0) {
$result = array();
foreach ($this->rooms->find_all() as $ro) {
foreach ($ro->availability_dates($date_start,$days) as $date => $total) {
if (! isset($result[$date]))
$result[$date] = 0;
$result[$date] += $total;
}
}
return $result;
}
}
?>