| 1 |
<?php |
| 2 |
namespace WeDevs\ERP\HRM; |
| 3 |
|
| 4 |
/** |
| 5 |
* The department class |
| 6 |
*/ |
| 7 |
class Department extends \WeDevs\ERP\Item { |
| 8 |
|
| 9 |
|
| 10 |
/** |
| 11 |
* Get a company by ID |
| 12 |
* |
| 13 |
* @param int company id |
| 14 |
* |
| 15 |
* @return object wpdb object |
| 16 |
*/ |
| 17 |
protected function get_by_id( $department_id ) { |
| 18 |
return \WeDevs\ERP\HRM\Models\Department::find( $department_id ); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Get number of employee belongs to this department |
| 23 |
* |
| 24 |
* @return int |
| 25 |
*/ |
| 26 |
public function num_of_employees() { |
| 27 |
return \WeDevs\ERP\HRM\Models\Employee::where( array( 'status' => 'active', 'department' => $this->id ) )->count(); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Get the name of department lead |
| 32 |
* |
| 33 |
* @return string |
| 34 |
*/ |
| 35 |
public function get_lead() { |
| 36 |
|
| 37 |
$employee = new Employee( intval( $this->lead ) ); |
| 38 |
|
| 39 |
if ( ! $employee->get_user_id() ) { |
| 40 |
return false; |
| 41 |
} |
| 42 |
|
| 43 |
return $employee; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Get the name of department lead |
| 48 |
* |
| 49 |
* @return string |
| 50 |
*/ |
| 51 |
public function get_depth( $department, $max_depth = 5 ) { |
| 52 |
$depth = 0; |
| 53 |
$parent_id = $department->parent; |
| 54 |
while( $parent_id > 0 ){ |
| 55 |
|
| 56 |
if ( $depth > $max_depth ) { |
| 57 |
continue; |
| 58 |
} |
| 59 |
$parent_id = $this->get_parent_id( $parent_id ); |
| 60 |
$depth++; |
| 61 |
} |
| 62 |
|
| 63 |
return $depth; |
| 64 |
} |
| 65 |
|
| 66 |
function get_parent_id( $parent_id ) { |
| 67 |
return \WeDevs\ERP\HRM\Models\Department::select( array( 'parent' ) )->find( $parent_id )->parent; |
| 68 |
} |
| 69 |
} |