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.

450 satır
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. 'load_job_img'=> plugins_url('img/loading_job.gif', __FILE__),
  114. ) );
  115. }
  116. private function register_timesheet_js_css(){
  117. global $pagename;
  118. if ($pagename != 'time-sheets'){
  119. return;
  120. }
  121. wp_enqueue_style( 'bts_ts', plugins_url('css/bts_timesheet.css', __FILE__));
  122. wp_enqueue_script( 'bts_ts', plugins_url('js/bts_timesheet.js', __FILE__), array( 'jquery' , 'bts' ));
  123. wp_enqueue_script('mustache', plugins_url('js/mustache.min.js', __FILE__), array('jquery'));
  124. }
  125. public function sync_users()
  126. {
  127. //dummy sync
  128. return;
  129. }
  130. // Usage: `wp sync_users --mininterval=123
  131. public function sync_user_cli($args = array(), $assoc_args = array()){
  132. $arguments = wp_parse_args( $assoc_args, array(
  133. 'mininterval' => 600,
  134. ) );
  135. $this->xero->sync_users($arguments['mininterval']);
  136. return;
  137. }
  138. public function bts_staff_item($attr){
  139. return $this->template('staff_item', 'staff.html');
  140. }
  141. public function bts_client_item($attr){
  142. return $this->template('client_item', 'client.html');
  143. }
  144. public function bts_job_item($attr){
  145. $html =$this->template('job_item', 'job.html');
  146. //$html = str_replace('[bts-tos-options]', $this->bts_tos_options([]), $html);
  147. $html = do_shortcode($html);
  148. return $html;
  149. }
  150. public function bts_rate_options($attr){
  151. $result = "<select> \n";
  152. $options = get_option('bts_payitem_earnings_rate');
  153. foreach($options as $o){
  154. $result.=sprintf("<option value='%s'> $%3.2f-%s</option>",
  155. $o['EarningsRateID'], $o['RatePerUnit'], $o['Name']);
  156. }
  157. $result .="</select>";
  158. return $result;
  159. }
  160. public function bts_select_staff($attr){
  161. $result = "<select> \n";
  162. $staff = $this->get_people_by_role('staff');
  163. foreach ($staff as $u){
  164. $result .= sprintf("<option value=%s> %s</option>", $u->user_login, $u->first_name . " " . $u->last_name);
  165. }
  166. $result .="</select>";
  167. return $result;
  168. }
  169. public function bts_select_client($attr){
  170. $result = "<select> \n";
  171. $staff = $this->get_people_by_role('client');
  172. foreach ($staff as $u){
  173. $result .= sprintf("<option value=%s> %s</option>", $u->user_login, $u->first_name . " " . $u->last_name);
  174. }
  175. $result .="</select>";
  176. return $result;
  177. }
  178. public function bts_type_of_service($attr){
  179. $result = ' <select>
  180. <option value="personalcare">Personal Care (stanard)</option>
  181. <option value="personalcare_h">Personal Care(Complex)</option>
  182. <option value="lunch">Lunch (stanard)</option>
  183. <option value="lunch_h">Lunch(Complex)</option>
  184. <option value="community">Community(stanard)</option>
  185. <option value="community_h">Community(Complex)</option>
  186. <option value="turn">Turn(stanard)</option>
  187. <option value="turn_h">Turn(Complex)</option>
  188. </select>
  189. ';
  190. return $result;
  191. }
  192. //generate template based on html file
  193. private function template($id, $file)
  194. {
  195. $text = '<script id="' . $id .'" type="text/x-biukop-template">';
  196. $text .= file_get_contents(plugin_dir_path(__FILE__) . "/html/$file");
  197. $text .= '</script>';
  198. return $text;
  199. }
  200. function list_staff(){
  201. check_ajax_referer('acaresydney');
  202. // Handle the ajax request
  203. $response = array(
  204. 'status' =>'error',
  205. 'users' => [],
  206. );
  207. //search all users that are staff
  208. $staffq = new \WP_User_Query(array('role'=>'staff','meta_key'=>'first_name', 'orderby'=>'meta_value', order=>'ASC'));
  209. $staff = $staffq->get_results();
  210. if (! empty($staff)){
  211. $response['status'] = 'success';
  212. foreach( $staff as $s){
  213. $response['users'][] = array(
  214. 'login' => $s->user_login,
  215. 'firstname'=> $s->first_name,
  216. 'lastname'=> $s->last_name,
  217. 'mobile'=> get_user_meta($s->ID, 'mobile', true),
  218. 'email'=> $s->user_email,
  219. 'wages'=> 0,
  220. 'hour' => 0 ,
  221. 'OT' => 0 ,
  222. 'petrol'=> 0 ,
  223. 'rating'=> 0,
  224. 'unconfirmedjob'=> 0,
  225. );
  226. }
  227. }
  228. wp_send_json($response);
  229. wp_die();
  230. }
  231. function list_client(){
  232. check_ajax_referer('acaresydney');
  233. // Handle the ajax request
  234. $response = array(
  235. 'status' =>'error',
  236. 'users' => [],
  237. );
  238. //search all users that are staff
  239. $clientq = new \WP_User_Query(array('role'=>'client', 'meta_key'=>'first_name', 'orderby'=>'meta_value', order=>'ASC'));
  240. $client = $clientq->get_results();
  241. if (! empty($client)){
  242. $response['status'] = 'success';
  243. foreach( $client as $s){
  244. $response['users'][] = array(
  245. 'login' => $s->user_login,
  246. 'firstname'=> $s->first_name,
  247. 'lastname'=> $s->last_name,
  248. 'mobile'=> get_user_meta($s->ID, 'mobile', true),
  249. 'email'=> $s->user_email,
  250. 'account'=> get_user_meta($s->ID, 'account', true),
  251. 'address' => get_user_meta($s->ID, 'address', true),
  252. 'rating'=> 0,
  253. 'unconfirmedjob'=> 0,
  254. );
  255. }
  256. }
  257. wp_send_json($response);
  258. wp_die();
  259. }
  260. private function get_people_by_role($role){
  261. //search all users that are staff
  262. $staffq = new \WP_User_Query(array('role'=>$role, 'meta_key'=>'first_name', 'orderby'=>'meta_value', order=>'ASC'));
  263. $staff = $staffq->get_results();
  264. return $staff;
  265. }
  266. //ajax get earnings rates
  267. function get_payitem_earnings_rate()
  268. {
  269. $response= array(
  270. 'status' => 'success',
  271. 'options'=> get_option('bts_payitem_earnings_rate'),
  272. );
  273. wp_send_json($response);
  274. }
  275. //ajax job CRUD
  276. function save_job()
  277. {
  278. check_ajax_referer('acaresydney');
  279. $r = $_POST['record'];
  280. $response = array();
  281. $d = array(
  282. 'tos' => $r['tos'],
  283. 'start' => $r['start'],
  284. 'finish' => $r['finish'],
  285. 'rate' => $r['rate'],
  286. 'staff' => $r['staff'],
  287. 'client' => $r['client'],
  288. 'ack' => $r['ack']=='true'?1:0,
  289. 'rating'=>$r['rating'],
  290. );
  291. //this is an update
  292. if ( isset($r['id']) && trim($r['id']) !='' && is_numeric($r['id'])){
  293. $response['isNew'] = false; //add or update?
  294. $result = $this->db->update($this->table_name, $d, array('id' =>$r['id']));
  295. if ($result !== false && $this->db->last_error == ''){
  296. $d['id'] = $r['id'];
  297. $response['status'] = 'success';
  298. //do data type conversion, string to int
  299. $response['newdata'] = $this->get_ts_record($r['id']);
  300. $response['errors'] = array(); //empty array
  301. }else{
  302. $response['status'] = 'error';
  303. $repsonse['errors'] = array(
  304. 'db' => "network database error" . $this->db->last_error,
  305. );
  306. }
  307. }else{
  308. $response['isNew'] = true;
  309. $result = $this->db->insert($this->table_name, $d);
  310. $lastid = $this->db->insert_id;
  311. if ($result != false && $this->db->last_error == ''){
  312. $response['status'] = 'success';
  313. $response['newdata'] = $this->get_ts_record($lastid);
  314. }else{
  315. $response['status'] = 'error';
  316. $response['errors'] = array(
  317. 'db' => 'network database error ' . $this->db->last_error,
  318. );
  319. }
  320. }
  321. wp_send_json($response);
  322. wp_die();
  323. }
  324. private function get_ts_record($id){
  325. $sql = "SELECT * FROM $this->table_name WHERE id=%d";
  326. $row = $this->db->get_row($this->db->prepare ($sql, array($id)));
  327. $response = [];
  328. if ($row != null){
  329. $response = array(
  330. 'id' => (int)$row->id,
  331. 'tos' => $row->tos,
  332. 'start' => $row->start,
  333. 'finish' => $row->finish,
  334. 'rate' => $row->rate,
  335. 'staff' => $row->staff,
  336. 'client' => $row->client,
  337. 'ack' => (int)$row->ack,
  338. 'rating' =>(int) $row->rating,
  339. );
  340. }
  341. return $response;
  342. }
  343. //ajax delete job
  344. function delete_job(){
  345. check_ajax_referer('acaresydney');
  346. $id = $_POST['jobid'];
  347. $result = $this->db->delete($this->table_name, array('id'=> $id));
  348. $response=array(
  349. 'status' => 'success',
  350. 'id' => $id,
  351. 'action'=> 'delete',
  352. 'error' => '',
  353. );
  354. if ($result == 1){
  355. wp_send_json($response);
  356. }else{
  357. $response['status'] = 'error';
  358. $response['error'] = $this->db->last_error;
  359. wp_send_json($response);
  360. }
  361. wp_die();
  362. }
  363. //ajax browse job with different filters
  364. function list_job(){
  365. check_ajax_referer('acaresydney');
  366. $start = $_POST['start'];
  367. $finish = $_POST['finish'];
  368. $response = array(
  369. 'status'=>'success',
  370. 'jobs' => [],
  371. );
  372. $sql = "SELECT * FROM $this->table_name WHERE start>='%s' and start <='%s' order by start ASC ,staff ASC";
  373. $jobs = $this->db->get_results($this->db->prepare ($sql, array($start, $finish)));
  374. if (! empty($jobs)){
  375. $response['status'] = 'success';
  376. foreach( $jobs as $s){
  377. $response['jobs'][] = array(
  378. 'id' => $s->id,
  379. 'tos' => $s->tos,
  380. 'start'=> $s->start,
  381. 'finish'=> $s->finish,
  382. 'rate'=> $s->rate,
  383. 'staff'=> $s->staff,
  384. 'client'=> $s->client,
  385. 'ack' => $s->ack,
  386. 'rating' =>$s->rating,
  387. );
  388. }
  389. }
  390. wp_send_json($response);
  391. wp_die();
  392. }
  393. }
  394. $bb = new AcareOffice();
  395. if ( defined( 'WP_CLI' ) && WP_CLI ) {
  396. \WP_CLI::add_command( 'sync_users', array($bb, 'sync_user_cli'));
  397. }