timesheet source code
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

272 lines
9.2KB

  1. <?php
  2. /**
  3. * Plugin Name: Acare Advanced Office
  4. * Plugin URI: http://biukop.com.au/acaresydney/timesheets
  5. * Description: Advanced Office system, timesheet, Payroll for AcareSydney
  6. * Version: 2.1
  7. * Author: Biukop Intelligence
  8. * Author URI: http://biukop.com.au/
  9. */
  10. namespace Biukop;
  11. require_once(dirname(__FILE__) . '/autoload.php');
  12. require_once (ABSPATH . 'wp-includes/pluggable.php');
  13. class AcareOffice{
  14. private $nonce; //for ajax verification
  15. private $pages = array('time-sheets', 'user-list');
  16. private $acaresydney_userid = 0;
  17. private $xero ;
  18. public function __construct() {
  19. add_action('init', array($this, 'class_loader'));
  20. add_action('wp', array($this, 'check_auth'));
  21. add_action('wp_enqueue_scripts', array($this, 'register_js_css'), 99);
  22. add_filter('show_admin_bar', '__return_false');
  23. //ts-xx for sync single user
  24. add_shortcode( 'ts-sync-users', array($this, 'sync_users'));
  25. //bts-xx for webpage
  26. add_shortcode( 'bts_staff_item', array($this, 'bts_staff_item'));
  27. add_shortcode( 'bts_client_item', array($this, 'bts_client_item'));
  28. add_shortcode( 'bts_job_item', array($this, 'bts_job_item'));
  29. add_shortcode( 'bts_hours_options', array($this, 'bts_hours_options'));
  30. add_shortcode( 'bts_select_staff', array($this, 'bts_select_staff'));
  31. add_shortcode( 'bts_select_client', array($this, 'bts_select_client'));
  32. add_action('wp_ajax_list_staff', array($this,'list_staff' ));
  33. add_action('wp_ajax_list_client', array($this,'list_client' ));
  34. add_action('wp_ajax_earnings_rate', array($this,'get_payitem_earnings_rate' ));
  35. add_action('wp_ajax_nopriv_earnings_rate', array($this,'get_payitem_earnings_rate' ));
  36. }
  37. /**
  38. * Autoload the custom theme classes
  39. */
  40. public function class_loader()
  41. {
  42. // Create a new instance of the autoloader
  43. $loader = new \Psr4AutoloaderClass();
  44. // Register this instance
  45. $loader->register();
  46. // Add our namespace and the folder it maps to
  47. $loader->addNamespace('\XeroPHP', dirname(__FILE__) . '/xero-php-master/src/XeroPHP');
  48. $loader->addNamespace('\Biukop', dirname(__FILE__) . '/' );
  49. $this->xero = new Xero();
  50. $this->xero->init_wp();
  51. }
  52. //
  53. //
  54. ///check auth
  55. public function check_auth(){
  56. global $pagename;
  57. //echo $pagename;
  58. }
  59. ///
  60. // enqueue / register css /js
  61. //
  62. public function register_js_css() {
  63. $this->nonce = wp_create_nonce('acaresydney');
  64. $this->acaresydney_userid = get_query_var( 'acaresydney_userid' ) ;
  65. $this->register_bts_js();
  66. $this->register_timesheet_js_css();
  67. }
  68. private function register_bts_js()
  69. {
  70. wp_enqueue_style( 'bts', plugins_url('css/ts.css', __FILE__));
  71. wp_enqueue_script('bts', plugins_url('js/ts.js', __FILE__), array('jquery', 'jquery-ui-core'));
  72. wp_localize_script( 'bts', 'bts1', array(
  73. 'ajax_url' => admin_url( 'admin-ajax.php' ),
  74. 'nonce' => $this->nonce, // It is common practice to comma after
  75. 'display_name' => wp_get_current_user()->display_name,
  76. 'anonymous' => !is_user_logged_in(),
  77. 'me'=> get_current_user_id(),
  78. 'userid'=> $this->acaresydney_userid,
  79. 'load_user_img'=> plugins_url('img/loading_user.gif', __FILE__),
  80. ) );
  81. }
  82. private function register_timesheet_js_css(){
  83. global $pagename;
  84. if ($pagename != 'time-sheets'){
  85. return;
  86. }
  87. wp_enqueue_style( 'bts_ts', plugins_url('css/bts_timesheet.css', __FILE__));
  88. wp_enqueue_script( 'bts_ts', plugins_url('js/bts_timesheet.js', __FILE__), array( 'jquery' , 'bts' ));
  89. wp_enqueue_script('mustache', plugins_url('js/mustache.min.js', __FILE__), array('jquery'));
  90. }
  91. public function sync_users()
  92. {
  93. //dummy sync
  94. return;
  95. }
  96. // Usage: `wp sync_users --mininterval=123
  97. public function sync_user_cli($args = array(), $assoc_args = array()){
  98. $arguments = wp_parse_args( $assoc_args, array(
  99. 'mininterval' => 600,
  100. ) );
  101. $this->xero->sync_users($arguments['mininterval']);
  102. return;
  103. }
  104. public function bts_staff_item($attr){
  105. return $this->template('staff_item', 'staff.html');
  106. }
  107. public function bts_client_item($attr){
  108. return $this->template('client_item', 'client.html');
  109. }
  110. public function bts_job_item($attr){
  111. $html =$this->template('job_item', 'job.html');
  112. //$html = str_replace('[bts-tos-options]', $this->bts_tos_options([]), $html);
  113. $html = do_shortcode($html);
  114. return $html;
  115. }
  116. public function bts_hours_options($attr){
  117. $result = "<select> \n";
  118. $options = get_option('bts_payitem_earnings_rate');
  119. foreach($options as $o){
  120. $result.=sprintf("<option value='%s'> $%3.2f-%s</option>",
  121. $o['EarningsRateID'], $o['RatePerUnit'], $o['Name']);
  122. }
  123. $result .="</select>";
  124. return $result;
  125. }
  126. public function bts_select_staff($attr){
  127. $result = "<select> \n";
  128. $staff = $this->get_people_by_role('staff');
  129. foreach ($staff as $u){
  130. $result .= sprintf("<option value=%s> %s</option>", $u->user_login, $u->first_name . " " . $u->last_name);
  131. }
  132. $result .="</select>";
  133. return $result;
  134. }
  135. public function bts_select_client($attr){
  136. $result = "<select> \n";
  137. $staff = $this->get_people_by_role('client');
  138. foreach ($staff as $u){
  139. $result .= sprintf("<option value=%s> %s</option>", $u->user_login, $u->first_name . " " . $u->last_name);
  140. }
  141. $result .="</select>";
  142. return $result;
  143. }
  144. //generate template based on html file
  145. private function template($id, $file)
  146. {
  147. $text = '<script id="' . $id .'" type="text/x-biukop-template">';
  148. $text .= file_get_contents(plugin_dir_path(__FILE__) . "/html/$file");
  149. $text .= '</script>';
  150. return $text;
  151. }
  152. function list_staff(){
  153. check_ajax_referer('acaresydney');
  154. // Handle the ajax request
  155. $response = array(
  156. 'status' =>'error',
  157. 'users' => [],
  158. );
  159. //search all users that are staff
  160. $staffq = new \WP_User_Query(array('role'=>'staff','meta_key'=>'first_name', 'orderby'=>'meta_value', order=>'ASC'));
  161. $staff = $staffq->get_results();
  162. if (! empty($staff)){
  163. $response['status'] = 'success';
  164. foreach( $staff as $s){
  165. $response['users'][] = array(
  166. 'login' => $s->user_login,
  167. 'firstname'=> $s->first_name,
  168. 'lastname'=> $s->last_name,
  169. 'mobile'=> get_user_meta($s->ID, 'mobile', true),
  170. 'email'=> $s->user_email,
  171. 'wages'=> 0,
  172. 'hour' => 0 ,
  173. 'OT' => 0 ,
  174. 'petrol'=> 0 ,
  175. 'rating'=> 0,
  176. 'unconfirmedjob'=> 0,
  177. );
  178. }
  179. }
  180. wp_send_json($response);
  181. wp_die();
  182. }
  183. function list_client(){
  184. check_ajax_referer('acaresydney');
  185. // Handle the ajax request
  186. $response = array(
  187. 'status' =>'error',
  188. 'users' => [],
  189. );
  190. //search all users that are staff
  191. $clientq = new \WP_User_Query(array('role'=>'client', 'meta_key'=>'first_name', 'orderby'=>'meta_value', order=>'ASC'));
  192. $client = $clientq->get_results();
  193. if (! empty($client)){
  194. $response['status'] = 'success';
  195. foreach( $client as $s){
  196. $response['users'][] = array(
  197. 'login' => $s->user_login,
  198. 'firstname'=> $s->first_name,
  199. 'lastname'=> $s->last_name,
  200. 'mobile'=> get_user_meta($s->ID, 'mobile', true),
  201. 'email'=> $s->user_email,
  202. 'account'=> get_user_meta($s->ID, 'account', true),
  203. 'address' => get_user_meta($s->ID, 'address', true),
  204. 'rating'=> 0,
  205. 'unconfirmedjob'=> 0,
  206. );
  207. }
  208. }
  209. wp_send_json($response);
  210. wp_die();
  211. }
  212. private function get_people_by_role($role){
  213. //search all users that are staff
  214. $staffq = new \WP_User_Query(array('role'=>$role, 'meta_key'=>'first_name', 'orderby'=>'meta_value', order=>'ASC'));
  215. $staff = $staffq->get_results();
  216. return $staff;
  217. }
  218. //ajax get earnings rates
  219. function get_payitem_earnings_rate()
  220. {
  221. $response= array(
  222. 'status' => 'success',
  223. 'options'=> get_option('bts_payitem_earnings_rate'),
  224. );
  225. wp_send_json($response);
  226. }
  227. }
  228. $bb = new AcareOffice();
  229. if ( defined( 'WP_CLI' ) && WP_CLI ) {
  230. \WP_CLI::add_command( 'sync_users', array($bb, 'sync_user_cli'));
  231. }