timesheet source code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

500 lines
18KB

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