0009768: Use ModelConfig for Timetracker models
[tine20] / tine20 / Timetracker / Model / Timesheet.php
1 <?php
2 /**
3  * class to hold Timesheet data
4  * 
5  * @package     Timetracker
6  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
7  * @author      Philipp Schuele <p.schuele@metaways.de>
8  * @copyright   Copyright (c) 2007-2016 Metaways Infosystems GmbH (http://www.metaways.de)
9  * 
10  */
11
12 /**
13  * class to hold Timesheet data
14  * 
15  * @package     Timetracker
16  */
17 class Timetracker_Model_Timesheet extends Tinebase_Record_Abstract implements Sales_Model_Billable_Interface
18 {
19     /**
20      * holds the configuration object (must be declared in the concrete class)
21      *
22      * @var Tinebase_ModelConfiguration
23      */
24     protected static $_configurationObject = NULL;
25
26     /**
27      * Holds the model configuration (must be assigned in the concrete class)
28      *
29      * @var array
30      */
31     protected static $_modelConfiguration = array(
32         'recordName'        => 'Timesheet',
33         'recordsName'       => 'Timesheets', // ngettext('Timesheet', 'Timesheets', n)
34         'hasRelations'      => TRUE,
35         'hasCustomFields'   => TRUE,
36         'hasNotes'          => TRUE,
37         'hasTags'           => TRUE,
38         'modlogActive'      => TRUE,
39         'hasAttachments'    => TRUE,
40         'createModule'      => TRUE,
41         'containerProperty' => NULL,
42
43         'titleProperty'     => 'title',
44         'appName'           => 'Timetracker',
45         'modelName'         => 'Timesheet',
46         'multipleEdit'      => TRUE,
47         'splitButton'       => TRUE,
48
49         'defaultFilter' => 'start_date',
50
51         'fields'            => array(
52             'account_id'            => array(
53                 'label'                 => 'Account', //_('Account')
54                 'duplicateCheckGroup'   => 'account',
55                 'type'                  => 'user',
56                 'validators'            => array(Zend_Filter_Input::ALLOW_EMPTY => false, 'presence'=>'required'),
57                 // TODO add filter: 'inputFilters'          => array('Zend_Filter_Empty' => CURRENT USER ACCOUNT ID),
58             ),
59             'timeaccount_id'        => array(
60                 'label'                 => 'Time Account (Number - Title)', //_('Time Account (Number - Title)')
61                 'type'                  => 'record',
62                 'validators'            => array(Zend_Filter_Input::ALLOW_EMPTY => false, 'presence'=>'required'),
63                 'config'                => array(
64                     'appName'               => 'Timetracker',
65                     'modelName'             => 'Timeaccount',
66                     'idProperty'            => 'id'
67                 ),
68                 // TODO ?????
69                 //'default'               => array('account_grants' => array('bookOwnGrant' => true)),
70                 'filterDefinition'      => array(
71                     'filter'                => 'Tinebase_Model_Filter_ForeignId',
72                     'options'               => array(
73                         'filtergroup'           => 'Timetracker_Model_TimeaccountFilter',
74                         'controller'            => 'Timetracker_Controller_Timeaccount',
75                         'useTimesheetAcl'       => TRUE,
76                         'showClosed'            => TRUE,
77                         'appName'               => 'Timetracker',
78                         'modelName'             => 'Timeaccount',
79                     ),
80                     'jsConfig'              => array('filtertype' => 'timetracker.timeaccount')
81                 ),
82             ),
83             'is_billable'           => array(
84                 'label'                 => NULL,
85                 'validators'            => array(Zend_Filter_Input::ALLOW_EMPTY => true, Zend_Filter_Input::DEFAULT_VALUE => 1),
86                 'type'                  => 'boolean',
87                 'default'               => 1,
88                 'shy'                   => TRUE
89             ),
90             'is_billable_combined'  => array(
91                 'label'                 => 'Billable', // _('Billable')
92                 'validators'            => array(Zend_Filter_Input::ALLOW_EMPTY => true),
93                 'type'                  => 'boolean',
94                 'filterDefinition'      => array(
95                     'filter'                => 'Tinebase_Model_Filter_Bool',
96                     'title'                 => 'Billable', // _('Billable')
97                     'options'               => array(
98                         'leftOperand'           => '(timetracker_timesheet.is_billable*timetracker_timeaccount.is_billable)',
99                         'requiredCols'          => array('is_billable_combined')
100                     ),
101                 ),
102             ),
103             'billed_in'             => array(
104                 'label'                 => 'Cleared in', // _('Cleared in')
105                 'validators'            => array(Zend_Filter_Input::ALLOW_EMPTY => true),
106                 'copyOmit'              => TRUE,
107                 'shy'                   => TRUE
108             ),
109             'invoice_id'            => array(
110                 'validators'            => array(Zend_Filter_Input::ALLOW_EMPTY => true),
111                 'label'                 => 'Invoice', // _('Invoice')
112                 'type'                  => 'record',
113                 'inputFilters'          => array('Zend_Filter_Empty' => NULL),
114                 'config'                => array(
115                     'appName'               => 'Sales',
116                     'modelName'             => 'Invoice',
117                     'idProperty'            => 'id'
118                 ),
119             ),
120             'is_cleared'            => array(
121                 'label'                 => NULL,
122                 'validators'            => array(Zend_Filter_Input::ALLOW_EMPTY => true, Zend_Filter_Input::DEFAULT_VALUE => 0),
123                 'type'                  => 'boolean',
124                 'default'               => 0,
125                 'copyOmit'              => TRUE,
126                 'shy'                   => TRUE
127             ),
128             'is_cleared_combined'   => array(
129                 'label'                 => 'Cleared', // _('Cleared')
130                 'validators'            => array(Zend_Filter_Input::ALLOW_EMPTY => true),
131                 'type'                  => 'boolean',
132                 'filterDefinition'      => array(
133                     'filter'                => 'Tinebase_Model_Filter_Bool',
134                     'options'               => array(
135                         'leftOperand'           => "( (CASE WHEN timetracker_timesheet.is_cleared = '1' THEN 1 ELSE 0 END) | (CASE WHEN timetracker_timeaccount.status = 'billed' THEN 1 ELSE 0 END) )",
136                         'requiredCols'          => array('is_cleared_combined'),
137                     ),
138                 ),
139             ),
140             'start_date'            => array(
141                 'label'                 => 'Date', // _('Date')
142                 'validators'            => array(Zend_Filter_Input::ALLOW_EMPTY => false, 'presence'=>'required'),
143                 'type'                  => 'date',
144                 'default'               => 'today',
145                 // strip time information from datetime string
146                 'inputFilters'          => array('Zend_Filter_PregReplace' => array('/(\d{4}-\d{2}-\d{2}).*/', '$1'))
147             ),
148             'start_time'            => array(
149                 'label'                 => 'Start time', // _('Start time')
150                 'validators'            => array(Zend_Filter_Input::ALLOW_EMPTY => true),
151                 'inputFilters'          => array('Zend_Filter_Empty' => NULL),
152                 'type'                  => 'time',
153                 'shy'                   => TRUE
154             ),
155             'duration'              => array(
156                 'label'                 => 'Duration', // _('Duration')
157                 'validators'            => array(Zend_Filter_Input::ALLOW_EMPTY => false, 'presence'=>'required'),
158                 'type'                  => 'integer',
159                 'specialType'           => 'minutes',
160                 'default'               => '30'
161             ),
162             'description'           => array(
163                 'label'                 => 'Description', // _('Description')
164                 'type'                  => 'text',
165                 'validators'            => array(Zend_Filter_Input::ALLOW_EMPTY => false, 'presence'=>'required'),
166                 'queryFilter'           => TRUE
167             ),
168             // TODO ?????
169             /*
170             'timeaccount_closed' => array(
171                 'label' => 'Time Account closed', // _('Time Account closed')
172                 'validators' => array(Zend_Filter_Input::ALLOW_EMPTY => true),
173                 'type' => 'boolean',
174
175             ),*/
176         )
177     );
178     
179     /**
180      * returns the interval of this billable
181      *
182      * @return array
183      */
184     public function getInterval()
185     {
186         $startDate = clone new Tinebase_DateTime($this->start_date);
187         $startDate->setTimezone(Tinebase_Core::getUserTimezone());
188         $startDate->setDate($startDate->format('Y'), $startDate->format('n'), 1);
189         $startDate->setTime(0,0,0);
190         
191         $endDate = clone $startDate;
192         $endDate->addMonth(1)->subSecond(1);
193         
194         return array($startDate, $endDate);
195     }
196     
197     /**
198      * returns the quantity of this billable
199      *
200      * @return float
201      */
202     public function getQuantity()
203     {
204         return $this->duration / 60;
205     }
206     
207     /**
208      * returns the unit of this billable
209      *
210      * @return string
211      */
212     public function getUnit()
213     {
214         return 'hour'; // _('hour')
215     }
216 }