timesheet source code
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

452 lines
16KB

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