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

448 lines
15KB

  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. private $db;
  19. private $table_name;
  20. public function __construct() {
  21. add_option( "acare_ts_db_version", "1.0" );
  22. register_activation_hook( __FILE__, array($this, 'db_install') );
  23. add_action('init', array($this, 'class_loader'));
  24. add_action('wp', array($this, 'check_auth'));
  25. add_action('wp_enqueue_scripts', array($this, 'register_js_css'), 99);
  26. add_filter('show_admin_bar', '__return_false');
  27. //ts-xx for sync single user
  28. add_shortcode( 'ts-sync-users', array($this, 'sync_users'));
  29. //bts-xx for webpage
  30. add_shortcode( 'bts_staff_item', array($this, 'bts_staff_item'));
  31. add_shortcode( 'bts_client_item', array($this, 'bts_client_item'));
  32. add_shortcode( 'bts_job_item', array($this, 'bts_job_item'));
  33. add_shortcode( 'bts_rate_options', array($this, 'bts_rate_options'));
  34. add_shortcode( 'bts_select_staff', array($this, 'bts_select_staff'));
  35. add_shortcode( 'bts_select_client', array($this, 'bts_select_client'));
  36. add_shortcode( 'bts_type_of_service', array($this, 'bts_type_of_service'));
  37. add_action('wp_ajax_list_staff', array($this,'list_staff' ));
  38. add_action('wp_ajax_list_client', array($this,'list_client' ));
  39. add_action('wp_ajax_save_job', array($this,'save_job' ));
  40. add_action('wp_ajax_list_job', array($this,'list_job' ));
  41. add_action('wp_ajax_delete_job', array($this,'delete_job' ));
  42. add_action('wp_ajax_earnings_rate', array($this,'get_payitem_earnings_rate' ));
  43. add_action('wp_ajax_nopriv_earnings_rate', array($this,'get_payitem_earnings_rate' ));
  44. global $wpdb;
  45. $this->db = $wpdb;
  46. $this->table_name = $wpdb->prefix . 'acare_ts';
  47. }
  48. /**
  49. * Autoload the custom theme classes
  50. */
  51. public function class_loader()
  52. {
  53. // Create a new instance of the autoloader
  54. $loader = new \Psr4AutoloaderClass();
  55. // Register this instance
  56. $loader->register();
  57. // Add our namespace and the folder it maps to
  58. $loader->addNamespace('\XeroPHP', dirname(__FILE__) . '/xero-php-master/src/XeroPHP');
  59. $loader->addNamespace('\Biukop', dirname(__FILE__) . '/' );
  60. $this->xero = new Xero();
  61. $this->xero->init_wp();
  62. }
  63. //init database
  64. public function db_install () {
  65. global $wpdb;
  66. $charset_collate = $wpdb->get_charset_collate();
  67. //table name: broker transactions
  68. $table_name = $this->table_name;
  69. $sql = "CREATE TABLE $table_name (
  70. id INT NOT NULL AUTO_INCREMENT,
  71. tos VARCHAR(45) NULL,
  72. start DATETIME NULL,
  73. finish DATETIME NULL,
  74. rate VARCHAR(45) NULL,
  75. staff VARCHAR(45) NULL,
  76. client VARCHAR(45) NULL,
  77. ack TINYINT(4) NULL,
  78. rating INT(4) NULL DEFAULT 0,
  79. PRIMARY KEY (id)
  80. ) $charset_collate;";
  81. //create database
  82. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  83. dbDelta( $sql );
  84. }
  85. //
  86. //
  87. ///check auth
  88. public function check_auth(){
  89. global $pagename;
  90. //echo $pagename;
  91. }
  92. ///
  93. // enqueue / register css /js
  94. //
  95. public function register_js_css() {
  96. $this->nonce = wp_create_nonce('acaresydney');
  97. $this->acaresydney_userid = get_query_var( 'acaresydney_userid' ) ;
  98. $this->register_bts_js();
  99. $this->register_timesheet_js_css();
  100. }
  101. private function register_bts_js()
  102. {
  103. wp_enqueue_style( 'bts', plugins_url('css/ts.css', __FILE__));
  104. wp_enqueue_script('bts', plugins_url('js/ts.js', __FILE__), array('jquery', 'jquery-ui-core'));
  105. wp_localize_script( 'bts', 'bts1', array(
  106. 'ajax_url' => admin_url( 'admin-ajax.php' ),
  107. 'nonce' => $this->nonce, // It is common practice to comma after
  108. 'display_name' => wp_get_current_user()->display_name,
  109. 'anonymous' => !is_user_logged_in(),
  110. 'me'=> get_current_user_id(),
  111. 'userid'=> $this->acaresydney_userid,
  112. 'load_user_img'=> plugins_url('img/loading_user.gif', __FILE__),
  113. ) );
  114. }
  115. private function register_timesheet_js_css(){
  116. global $pagename;
  117. if ($pagename != 'time-sheets'){
  118. return;
  119. }
  120. wp_enqueue_style( 'bts_ts', plugins_url('css/bts_timesheet.css', __FILE__));
  121. wp_enqueue_script( 'bts_ts', plugins_url('js/bts_timesheet.js', __FILE__), array( 'jquery' , 'bts' ));
  122. wp_enqueue_script('mustache', plugins_url('js/mustache.min.js', __FILE__), array('jquery'));
  123. }
  124. public function sync_users()
  125. {
  126. //dummy sync
  127. return;
  128. }
  129. // Usage: `wp sync_users --mininterval=123
  130. public function sync_user_cli($args = array(), $assoc_args = array()){
  131. $arguments = wp_parse_args( $assoc_args, array(
  132. 'mininterval' => 600,
  133. ) );
  134. $this->xero->sync_users($arguments['mininterval']);
  135. return;
  136. }
  137. public function bts_staff_item($attr){
  138. return $this->template('staff_item', 'staff.html');
  139. }
  140. public function bts_client_item($attr){
  141. return $this->template('client_item', 'client.html');
  142. }
  143. public function bts_job_item($attr){
  144. $html =$this->template('job_item', 'job.html');
  145. //$html = str_replace('[bts-tos-options]', $this->bts_tos_options([]), $html);
  146. $html = do_shortcode($html);
  147. return $html;
  148. }
  149. public function bts_rate_options($attr){
  150. $result = "<select> \n";
  151. $options = get_option('bts_payitem_earnings_rate');
  152. foreach($options as $o){
  153. $result.=sprintf("<option value='%s'> $%3.2f-%s</option>",
  154. $o['EarningsRateID'], $o['RatePerUnit'], $o['Name']);
  155. }
  156. $result .="</select>";
  157. return $result;
  158. }
  159. public function bts_select_staff($attr){
  160. $result = "<select> \n";
  161. $staff = $this->get_people_by_role('staff');
  162. foreach ($staff as $u){
  163. $result .= sprintf("<option value=%s> %s</option>", $u->user_login, $u->first_name . " " . $u->last_name);
  164. }
  165. $result .="</select>";
  166. return $result;
  167. }
  168. public function bts_select_client($attr){
  169. $result = "<select> \n";
  170. $staff = $this->get_people_by_role('client');
  171. foreach ($staff as $u){
  172. $result .= sprintf("<option value=%s> %s</option>", $u->user_login, $u->first_name . " " . $u->last_name);
  173. }
  174. $result .="</select>";
  175. return $result;
  176. }
  177. public function bts_type_of_service($attr){
  178. $result = ' <select>
  179. <option value="personalcare">Personal Care (stanard)</option>
  180. <option value="personalcare_h">Personal Care(Complex)</option>
  181. <option value="lunch">Lunch (stanard)</option>
  182. <option value="lunch_h">Lunch(Complex)</option>
  183. <option value="community">Community(stanard)</option>
  184. <option value="community_h">Community(Complex)</option>
  185. <option value="turn">Turn(stanard)</option>
  186. <option value="turn_h">Turn(Complex)</option>
  187. </select>
  188. ';
  189. return $result;
  190. }
  191. //generate template based on html file
  192. private function template($id, $file)
  193. {
  194. $text = '<script id="' . $id .'" type="text/x-biukop-template">';
  195. $text .= file_get_contents(plugin_dir_path(__FILE__) . "/html/$file");
  196. $text .= '</script>';
  197. return $text;
  198. }
  199. function list_staff(){
  200. check_ajax_referer('acaresydney');
  201. // Handle the ajax request
  202. $response = array(
  203. 'status' =>'error',
  204. 'users' => [],
  205. );
  206. //search all users that are staff
  207. $staffq = new \WP_User_Query(array('role'=>'staff','meta_key'=>'first_name', 'orderby'=>'meta_value', order=>'ASC'));
  208. $staff = $staffq->get_results();
  209. if (! empty($staff)){
  210. $response['status'] = 'success';
  211. foreach( $staff as $s){
  212. $response['users'][] = array(
  213. 'login' => $s->user_login,
  214. 'firstname'=> $s->first_name,
  215. 'lastname'=> $s->last_name,
  216. 'mobile'=> get_user_meta($s->ID, 'mobile', true),
  217. 'email'=> $s->user_email,
  218. 'wages'=> 0,
  219. 'hour' => 0 ,
  220. 'OT' => 0 ,
  221. 'petrol'=> 0 ,
  222. 'rating'=> 0,
  223. 'unconfirmedjob'=> 0,
  224. );
  225. }
  226. }
  227. wp_send_json($response);
  228. wp_die();
  229. }
  230. function list_client(){
  231. check_ajax_referer('acaresydney');
  232. // Handle the ajax request
  233. $response = array(
  234. 'status' =>'error',
  235. 'users' => [],
  236. );
  237. //search all users that are staff
  238. $clientq = new \WP_User_Query(array('role'=>'client', 'meta_key'=>'first_name', 'orderby'=>'meta_value', order=>'ASC'));
  239. $client = $clientq->get_results();
  240. if (! empty($client)){
  241. $response['status'] = 'success';
  242. foreach( $client as $s){
  243. $response['users'][] = array(
  244. 'login' => $s->user_login,
  245. 'firstname'=> $s->first_name,
  246. 'lastname'=> $s->last_name,
  247. 'mobile'=> get_user_meta($s->ID, 'mobile', true),
  248. 'email'=> $s->user_email,
  249. 'account'=> get_user_meta($s->ID, 'account', true),
  250. 'address' => get_user_meta($s->ID, 'address', true),
  251. 'rating'=> 0,
  252. 'unconfirmedjob'=> 0,
  253. );
  254. }
  255. }
  256. wp_send_json($response);
  257. wp_die();
  258. }
  259. private function get_people_by_role($role){
  260. //search all users that are staff
  261. $staffq = new \WP_User_Query(array('role'=>$role, 'meta_key'=>'first_name', 'orderby'=>'meta_value', order=>'ASC'));
  262. $staff = $staffq->get_results();
  263. return $staff;
  264. }
  265. //ajax get earnings rates
  266. function get_payitem_earnings_rate()
  267. {
  268. $response= array(
  269. 'status' => 'success',
  270. 'options'=> get_option('bts_payitem_earnings_rate'),
  271. );
  272. wp_send_json($response);
  273. }
  274. //ajax job CRUD
  275. function save_job()
  276. {
  277. check_ajax_referer('acaresydney');
  278. $r = $_POST['record'];
  279. $response = array();
  280. $d = array(
  281. 'tos' => $r['tos'],
  282. 'start' => $r['start'],
  283. 'finish' => $r['finish'],
  284. 'rate' => $r['rate'],
  285. 'staff' => $r['staff'],
  286. 'client' => $r['client'],
  287. 'ack' => $r['ack']=='true'?1:0,
  288. );
  289. //this is an update
  290. if ( isset($r['id']) && trim($r['id']) !='' && is_numeric($r['id'])){
  291. $response['isNew'] = false; //add or update?
  292. $result = $this->db->update($this->table_name, $d, array('id' =>$r['id']));
  293. if ($result !== false && $this->db->last_error == ''){
  294. $d['id'] = $r['id'];
  295. $response['status'] = 'success';
  296. //do data type conversion, string to int
  297. $response['newdata'] = $this->get_ts_record($r['id']);
  298. $response['errors'] = array(); //empty array
  299. }else{
  300. $response['status'] = 'error';
  301. $repsonse['errors'] = array(
  302. 'db' => "network database error" . $this->db->last_error,
  303. );
  304. }
  305. }else{
  306. $response['isNew'] = true;
  307. $result = $this->db->insert($this->table_name, $d);
  308. $lastid = $this->db->insert_id;
  309. if ($result != false && $this->db->last_error == ''){
  310. $response['status'] = 'success';
  311. $response['newdata'] = $this->get_ts_record($lastid);
  312. }else{
  313. $response['status'] = 'error';
  314. $response['errors'] = array(
  315. 'db' => 'network database error ' . $this->db->last_error,
  316. );
  317. }
  318. }
  319. wp_send_json($response);
  320. wp_die();
  321. }
  322. private function get_ts_record($id){
  323. $sql = "SELECT * FROM $this->table_name WHERE id=%d";
  324. $row = $this->db->get_row($this->db->prepare ($sql, array($id)));
  325. $response = [];
  326. if ($row != null){
  327. $response = array(
  328. 'id' => (int)$row->id,
  329. 'tos' => $row->tos,
  330. 'start' => $row->start,
  331. 'finish' => $row->finish,
  332. 'rate' => $row->rate,
  333. 'staff' => $row->staff,
  334. 'client' => $row->client,
  335. 'ack' => (int)$row->ack,
  336. 'rating' =>(int) $row->rating,
  337. );
  338. }
  339. return $response;
  340. }
  341. //ajax delete job
  342. function delete_job(){
  343. check_ajax_referer('acaresydney');
  344. $id = $_POST['recordid'];
  345. $result = $this->db->delete($this->table_name, array('id'=> $id));
  346. $response=array(
  347. 'status' => 'success',
  348. 'id' => $id,
  349. 'action'=> 'delete',
  350. 'error' => '',
  351. );
  352. if ($result == 1){
  353. wp_send_json($response);
  354. }else{
  355. $response['status'] = 'error';
  356. $response['error'] = $this->db->last_error;
  357. wp_send_json($response);
  358. }
  359. wp_die();
  360. }
  361. //ajax browse job with different filters
  362. function list_job(){
  363. check_ajax_referer('acaresydney');
  364. $start = $_POST['start'];
  365. $finish = $_POST['finish'];
  366. $response = array(
  367. 'status'=>'success',
  368. 'jobs' => [],
  369. );
  370. $sql = "SELECT * FROM $this->table_name WHERE start>='%s' and start <='%s'";
  371. $jobs = $this->db->get_results($this->db->prepare ($sql, array($start, $finish)));
  372. if (! empty($jobs)){
  373. $response['status'] = 'success';
  374. foreach( $jobs as $s){
  375. $response['jobs'][] = array(
  376. 'id' => $s->id,
  377. 'tos' => $s->tos,
  378. 'start'=> $s->start,
  379. 'finish'=> $s->finish,
  380. 'rate'=> $s->rate,
  381. 'staff'=> $s->staff,
  382. 'client'=> $s->client,
  383. 'ack' => $s->ack,
  384. 'rating' =>$s->rating,
  385. );
  386. }
  387. }
  388. wp_send_json($response);
  389. wp_die();
  390. }
  391. }
  392. $bb = new AcareOffice();
  393. if ( defined( 'WP_CLI' ) && WP_CLI ) {
  394. \WP_CLI::add_command( 'sync_users', array($bb, 'sync_user_cli'));
  395. }