3 * Table Definition for period
5 require_once 'DB/DataObject.php';
7 class Pman_Xtuple_DataObjects_Period extends DB_DataObject
10 /* the code below is auto generated do not remove the above tag */
12 public $__table = 'period'; // table name
13 public $period_id; // int4(4) not_null default_nextval%28period_period_id_seq%29 primary_key
14 public $period_start; // date(4)
15 public $period_end; // date(4)
16 public $period_closed; // bool(1)
17 public $period_freeze; // bool(1)
18 public $period_initial; // bool(1) default_false
19 public $period_name; // text(-1)
20 public $period_yearperiod_id; // int4(4)
21 public $period_quarter; // int4(4)
22 public $period_number; // int4(4) not_null
25 /* the code above is auto generated do not remove the tag below */
31 $d = DB_DataObject::Factory('yearperiod');
32 $d->get($this->period_yearperiod_id);
36 function applyFilters($q, $au, $roo)
38 if (!empty($q['_initPeriods'])) {
39 $this->initPeriods($roo);
42 if (!empty($q['_renamePeriods'])) {
43 $this->renamePeriods($roo);
46 if(!empty($q['_with_transactions'])){
54 (gltrans_date BETWEEN period_start AND period_end)
62 if(!empty($q['_status'])){
63 switch ($q['_status']){
65 $this->period_closed = FALSE;
68 $this->period_closed = TRUE;
78 function initDatabase($roo)
83 echo "SKIP - already have periods\n";
86 $p->initPeriods($roo);
91 function initPeriods($roo)
94 // should be configurable somewhere...
95 $db = substr($this->database(),-2);
96 $start = date('Y') -1;//
101 //SG: 1 Oct - 30 Sept
112 //SG: 1 Oct - 30 Sept
118 $roo->jerr("not set up yet");
123 $yp = DB_DataObject::factory('yearperiod');
125 //DB_DataObject::debugLevel(1);
127 // adds to years from today...
128 for ($i = $start; $i < date('Y')+10 ;$i++ ) {
131 // we should now have the period + id..
133 for ($m=0; $m < 12; $m++) {
134 $ms = !$m ? ($i . $sdate) : date('Y-m-01', strtotime( $i . $sdate . ' + ' . $m . ' MONTHS' ));
135 $me = date('Y-m-d',strtotime( $ms .' + 1 MONTHS - 1 DAYS'));
137 $p = DB_DataObject::factory('period');
138 $p->get('period_start', $ms);
140 $yp = $this->createYearPeriod($i, $sdate, $edate);
142 $p->get('period_start', $ms);
144 if ($p->period_closed || $p->period_freeze) {
147 $end = date('Y-m-d',strtotime( "$ms + 1 MONTH - 1 DAY"));
150 'period_closed' => false,
151 'period_freeze' => false,
152 'period_end' => $end,
153 //'period_initial | t
154 'period_name' => date('Y-m', strtotime($ms)) .' '. date('M', strtotime($ms)),
155 'period_yearperiod_id' => $yp->yearperiod_id,
156 'period_quarter' => floor($m/3) + 1,
157 'period_number' => $m + 1
159 $p->period_id ? $p->update() : $p->insert();
169 function createYearPeriod($i, $sdate, $edate)
171 $yp = DB_DataObject::factory('yearperiod');
172 if ( $yp->get('yearperiod_start', $i.$sdate)) {
175 $yp = DB_DataObject::factory('yearperiod');
177 $yp->whereAdd("yearperiod_start >= '{$i}-01-01' AND yearperiod_start < '{$ni}-01-01'");
182 'yearperiod_start' => $i . $sdate,
183 'yearperiod_end' => ($i+1) . $edate,
184 'yearperiod_closed' => false,
186 if (!empty($yp->yearperiod_id)) {
194 function renamePeriods()
196 $p = DB_DataObject::factory($this->tableName());
197 //$p->whereAdd("period_name not like '____-__ %'");
198 $all = $p->fetchAll();
199 foreach($all as $p) {
202 $yp = $p->yearperiod();
203 $fy = date('Y', strtotime($yp->yearperiod_start));
204 $fe = date('Y', strtotime($yp->yearperiod_end));
205 $fy_name = "(FY {$fy} - {$fe})";
207 $ms = $p->period_start;
209 $p->period_name = date('Y-m', strtotime($ms)) .' '. date('M', strtotime($ms)) . ' ' . $fy_name ;
215 function isClosed($date)
217 $date = date('Y-m-d',strtotime($date));
218 $p = DB_DataObject::Factory('period');
219 $p->whereAdd("'{$date}' BETWEEN period_start AND period_end");
221 return ($p->period_closed || $p->period_freeze ) ? true : false;
227 $p = DB_DataObject::Factory('period');
228 $p->orderBy('period_start ASC');
230 $p->period_closed = 0;
231 $p->period_freeze = 0;
236 function createPeriod($y)
240 $db = substr($this->database(),-2);
255 HTML_FlexyFramework::get()->page->jerr("not set up yet");
258 $yp = $this->createYearPeriod($y, $sdate, $edate);
260 for ($m=0; $m < 12; $m++) {
261 $ms = !$m ? ($y . $sdate) : date('Y-m-01', strtotime( $y . $sdate . ' + ' . $m . ' MONTHS' )); // period_start
262 $me = date('Y-m-d',strtotime( $ms .' + 1 MONTHS - 1 DAYS')); // period_end
264 $p = DB_DataObject::factory('period');
265 $p->get('period_start', $ms);
267 if ($p->period_closed || $p->period_freeze) {
272 'period_closed' => false,
273 'period_freeze' => false,
274 'period_start' => $ms,
276 'period_name' => date('Y', strtotime($ms)) .' '. date('M', strtotime($ms)),
277 'period_yearperiod_id' => $yp->yearperiod_id,
278 'period_quarter' => floor($m/3) + 1,
279 'period_number' => $m + 1
281 $p->period_id ? $p->update() : $p->insert();
288 function beforeInsert($q,$roo)
290 if(isset($q['_group'])){
291 $roo->jerr("not supported");
293 $list = json_decode($q['_group'], true);
294 foreach ($list as $l){
295 $period = DB_DataObject::factory('period');
296 if(!$period->get($l['period_id'])){
297 $roo->jerr("Can not found the period id : {$l['period_id']}");
299 $period->close($roo);
304 if(isset($q['_addExtraYear'])){
305 $yearperiod = DB_DataObject::factory('yearperiod');
306 $yearperiod->selectAdd();
307 $yearperiod->selectAdd("
308 (EXTRACT(year FROM MAX(yearperiod_end))) AS next_year
310 if ($yearperiod->count()) {
311 $yearperiod->find(true);
312 $this->createPeriod($yearperiod->next_year);
314 $this->createPeriod(date('Y')-1);
322 function beforeUpdate($old, $q,$roo)
325 $valid_actions = array('open','close','freeze','thaw');
327 if(!empty($q['_action'])) {
328 if (!in_array($q['_action'], $valid_actions)) {
329 $roo->jerr("invalid action");
332 $method = $q['_action'];
335 $this->$method($roo);
337 if(!empty($q['_second_action']) && in_array($q['_second_action'], $valid_actions)) {
338 $method = $q['_second_action'];
339 $this->$method($roo);
353 $period = DB_DataObject::factory('period');
354 $period->query("SELECT openAccountingPeriod({$this->period_id}) AS result");
356 if($period->result < 0){
357 switch ($period->result){
359 $roo->jerr("This period is already open! name : {$this->period_name} result : {$period->result}");
362 $roo->jerr("This period is frozen! name : {$this->period_name} result : {$period->result}");
365 $roo->jerr("Some period after this period have been closed! name : {$this->period_name} result : {$period->result}");
368 $roo->jerr("This yearperiod of this period has been closed! name : {$this->period_name} result : {$period->result}");
370 $roo->jerr("Error occur on open accounting period! name : {$this->period_name} result : {$period->result}");
383 $period = DB_DataObject::factory('period');
384 $period->query("SELECT closeAccountingPeriod({$this->period_id}) AS result");
386 if($period->result < 0){
387 switch ($period->result){
389 $roo->jerr("This period is already close! name : {$this->period_name} result : {$period->result}");
392 $roo->jerr("The day before this period is not belongs to another period! name : {$this->period_name} result : {$period->result}");
395 $roo->jerr("Pervious period is not closed! name : {$this->period_name} result : {$period->result}");
398 $roo->jerr("Next period is not defined! name : {$this->period_name} result : {$period->result}");
401 $roo->jerr("Prematurely close this period! name : {$this->period_name} result : {$period->result}");
404 $roo->jerr("Can not found the yearperiod of the next period! name : {$this->period_name} result : {$period->result}");
407 $roo->jerr("Error occur on close accounting period! name : {$this->period_name} result : {$period->result}");
418 function freeze($roo)
420 $period = DB_DataObject::factory('period');
421 $period->query("SELECT freezeAccountingPeriod({$this->period_id}) AS result");
423 if($period->result < 0){
424 switch ($period->result){
426 $roo->jerr("This period is still open! name : {$this->period_name} result : {$period->result}");
429 $roo->jerr("This period is already frozen! name : {$this->period_name} result : {$period->result}");
432 $roo->jerr("Error occur on close accounting period! name : {$this->period_name} result : {$period->result}");
443 $period = DB_DataObject::factory('period');
444 $period->query("SELECT thawAccountingPeriod({$this->period_id}) AS result");
446 if($period->result < 0){
447 switch ($period->result){
449 $roo->jerr("This period is not frozen! name : {$this->period_name} result : {$period->result}");
452 $roo->jerr("Error occur on close accounting period! name : {$this->period_name} result : {$period->result}");