timesheet source code
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

222 lines
7.1KB

  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_tos_options', array($this, 'bts_tos_options'));
  30. add_action('wp_ajax_list_staff', array($this,'list_staff' ));
  31. add_action('wp_ajax_list_client', array($this,'list_client' ));
  32. add_action('wp_ajax_earnings_rate', array($this,'get_payitem_earnings_rate' ));
  33. add_action('wp_ajax_nopriv_earnings_rate', array($this,'get_payitem_earnings_rate' ));
  34. }
  35. /**
  36. * Autoload the custom theme classes
  37. */
  38. public function class_loader()
  39. {
  40. // Create a new instance of the autoloader
  41. $loader = new \Psr4AutoloaderClass();
  42. // Register this instance
  43. $loader->register();
  44. // Add our namespace and the folder it maps to
  45. $loader->addNamespace('\XeroPHP', dirname(__FILE__) . '/xero-php-master/src/XeroPHP');
  46. $loader->addNamespace('\Biukop', dirname(__FILE__) . '/' );
  47. $this->xero = new Xero();
  48. $this->xero->init_wp();
  49. }
  50. //
  51. //
  52. ///check auth
  53. public function check_auth(){
  54. global $pagename;
  55. //echo $pagename;
  56. }
  57. ///
  58. // enqueue / register css /js
  59. //
  60. public function register_js_css() {
  61. $this->nonce = wp_create_nonce('acaresydney');
  62. $this->acaresydney_userid = get_query_var( 'acaresydney_userid' ) ;
  63. $this->register_bts_js();
  64. $this->register_timesheet_js_css();
  65. }
  66. private function register_bts_js()
  67. {
  68. wp_enqueue_style( 'bts', plugins_url('css/ts.css', __FILE__));
  69. wp_enqueue_script('bts', plugins_url('js/ts.js', __FILE__), array('jquery', 'jquery-ui-core'));
  70. wp_localize_script( 'bts', 'bts1', array(
  71. 'ajax_url' => admin_url( 'admin-ajax.php' ),
  72. 'nonce' => $this->nonce, // It is common practice to comma after
  73. 'display_name' => wp_get_current_user()->display_name,
  74. 'anonymous' => !is_user_logged_in(),
  75. 'me'=> get_current_user_id(),
  76. 'userid'=> $this->acaresydney_userid,
  77. 'load_user_img'=> plugins_url('img/loading_user.gif', __FILE__),
  78. ) );
  79. }
  80. private function register_timesheet_js_css(){
  81. global $pagename;
  82. if ($pagename != 'time-sheets'){
  83. return;
  84. }
  85. wp_enqueue_style( 'bts_ts', plugins_url('css/bts_timesheet.css', __FILE__));
  86. wp_enqueue_script( 'bts_ts', plugins_url('js/bts_timesheet.js', __FILE__), array( 'jquery' , 'bts' ));
  87. wp_enqueue_script('mustache', plugins_url('js/mustache.min.js', __FILE__), array('jquery'));
  88. }
  89. public function sync_users()
  90. {
  91. //dummy sync
  92. return;
  93. }
  94. // Usage: `wp sync_users --mininterval=123
  95. public function sync_user_cli($args = array(), $assoc_args = array()){
  96. $arguments = wp_parse_args( $assoc_args, array(
  97. 'mininterval' => 600,
  98. ) );
  99. $this->xero->sync_users($arguments['mininterval']);
  100. return;
  101. }
  102. public function bts_staff_item($attr){
  103. return $this->template('staff_item', 'staff.html');
  104. }
  105. public function bts_client_item($attr){
  106. return $this->template('client_item', 'client.html');
  107. }
  108. public function bts_job_item($attr){
  109. $html =$this->template('job_item', 'job.html');
  110. //$html = str_replace('[bts-tos-options]', $this->bts_tos_options([]), $html);
  111. $html = do_shortcode($html);
  112. return $html;
  113. }
  114. public function bts_tos_options($attr){
  115. $result = "<select> \n";
  116. $options = get_option('bts_payitem_earnings_rate');
  117. foreach($options as $o){
  118. $result.=sprintf("<option value='%s'> $%03.2f-%s</option>",
  119. $o['EarningsRateID'], $o['RatePerUnit'], $o['Name']);
  120. }
  121. $result .="</select>";
  122. return $result;
  123. }
  124. //generate template based on html file
  125. private function template($id, $file)
  126. {
  127. $text = '<script id="' . $id .'" type="text/x-biukop-template">';
  128. $text .= file_get_contents(plugin_dir_path(__FILE__) . "/html/$file");
  129. $text .= '</script>';
  130. return $text;
  131. }
  132. function list_staff(){
  133. check_ajax_referer('acaresydney');
  134. return $this->list_people_by_role('staff');
  135. }
  136. function list_client(){
  137. check_ajax_referer('acaresydney');
  138. return $this->list_people_by_role('client');
  139. }
  140. function list_people_by_role($role){
  141. check_ajax_referer('acaresydney');
  142. // Handle the ajax request
  143. $response = array(
  144. 'status' =>'error',
  145. 'users' => [],
  146. );
  147. //search all users that are staff
  148. $staffq = new \WP_User_Query(array('role'=>$role));
  149. $staff = $staffq->get_results();
  150. if (! empty($staff)){
  151. $response['status'] = 'success';
  152. foreach( $staff as $s){
  153. $response['users'][] = array(
  154. 'login' => $s->user_login,
  155. 'firstname'=> $s->first_name,
  156. 'lastname'=> $s->last_name,
  157. 'mobile'=> get_user_meta($s->ID, 'mobile', true),
  158. 'email'=> $s->user_email,
  159. 'wages'=> 0,
  160. 'hour' => 0 ,
  161. 'OT' => 0 ,
  162. 'petrol'=> 0 ,
  163. 'rating'=> 0,
  164. 'unconfirmedjob'=> 0,
  165. );
  166. }
  167. }
  168. wp_send_json($response);
  169. wp_die();
  170. }
  171. //ajax get earnings rates
  172. function get_payitem_earnings_rate()
  173. {
  174. $response= array(
  175. 'status' => 'success',
  176. 'options'=> get_option('bts_payitem_earnings_rate'),
  177. );
  178. wp_send_json($response);
  179. }
  180. }
  181. $bb = new AcareOffice();
  182. if ( defined( 'WP_CLI' ) && WP_CLI ) {
  183. \WP_CLI::add_command( 'sync_users', array($bb, 'sync_user_cli'));
  184. }