timesheet source code
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

960 líneas
35KB

  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 $bts_user_id = 0;
  18. private $bts_week_id = 1; //week 1, we will try to calculate current week;
  19. private $xero ;
  20. private $db;
  21. private $table_name;
  22. private $addr_table;
  23. public function __construct() {
  24. add_option( "acare_ts_db_version", "1.0" );
  25. register_activation_hook( __FILE__, array($this, 'db_install') );
  26. add_action('init', array($this, 'class_loader'));
  27. add_action('wp', array($this, 'check_auth'));
  28. add_action('wp_enqueue_scripts', array($this, 'register_js_css'), 99);
  29. add_filter('show_admin_bar', '__return_false');
  30. //ts-xx for sync single user
  31. add_shortcode( 'ts-sync-users', array($this, 'sync_users'));
  32. //bts-xx for webpage
  33. add_shortcode( 'bts_staff_item', array($this, 'bts_staff_item'));
  34. add_shortcode( 'bts_client_item', array($this, 'bts_client_item'));
  35. add_shortcode( 'bts_job_item', array($this, 'bts_job_item'));
  36. add_shortcode( 'bts_rate_options', array($this, 'bts_rate_options'));
  37. add_shortcode( 'bts_select_staff', array($this, 'bts_select_staff'));
  38. add_shortcode( 'bts_select_client', array($this, 'bts_select_client'));
  39. add_shortcode( 'bts_type_of_service', array($this, 'bts_type_of_service'));
  40. add_shortcode( 'bts_staff_job_summary', array($this, 'bts_staff_job_summary'));
  41. add_shortcode( 'bts_feedback_card', array($this, 'bts_feedback_card'));
  42. //user profile page
  43. add_shortcode( 'bts_user_name', array($this,'bts_user_name'));
  44. add_action('wp_ajax_list_staff', array($this,'list_staff' ));
  45. add_action('wp_ajax_list_client', array($this,'list_client' ));
  46. add_action('wp_ajax_save_job', array($this,'save_job' ));
  47. add_action('wp_ajax_list_job', array($this,'list_job' ));
  48. add_action('wp_ajax_delete_job', array($this,'delete_job' ));
  49. add_action('wp_ajax_email_job', array($this,'email_job' ));
  50. add_action('wp_ajax_earnings_rate', array($this,'get_payitem_earnings_rate' ));
  51. add_action('wp_ajax_nopriv_earnings_rate', array($this,'get_payitem_earnings_rate' ));
  52. add_action('wp_ajax_list_job_by_staff', array($this,'list_job_by_staff' ));
  53. add_action('wp_ajax_nopriv_list_job_by_staff', array($this,'list_job_by_staff' ));
  54. add_action('wp_ajax_staff_ack_job', array($this,'staff_ack_job' ));
  55. add_action('wp_ajax_nopriv_staff_ack_job', array($this,'staff_ack_job' ));
  56. add_action('wp_ajax_list_job_by_client', array($this,'list_job_by_client' ));
  57. add_action('wp_ajax_nopriv_list_job_by_client', array($this,'list_job_by_client' ));
  58. add_action('wp_ajax_client_ack_job', array($this,'client_ack_job' ));
  59. add_action('wp_ajax_nopriv_client_ack_job', array($this,'client_ack_job' ));
  60. // hook add_rewrite_rules function into rewrite_rules_array
  61. add_filter('rewrite_rules_array', array($this,'my_add_rewrite_rules'));
  62. // hook add_query_vars function into query_vars
  63. add_filter('query_vars', array($this,'add_query_vars'));
  64. global $wpdb;
  65. $this->db = $wpdb;
  66. $this->table_name = $wpdb->prefix . 'acare_ts';
  67. $this->addr_table = $wpdb->prefix . 'acare_addr_distance';
  68. $this->ndis_table = $wpdb->prefix . 'acare_ndis_price';
  69. }
  70. /**
  71. * Autoload the custom theme classes
  72. */
  73. public function class_loader()
  74. {
  75. // Create a new instance of the autoloader
  76. $loader = new \Psr4AutoloaderClass();
  77. // Register this instance
  78. $loader->register();
  79. // Add our namespace and the folder it maps to
  80. $loader->addNamespace('\XeroPHP', dirname(__FILE__) . '/xero-php-master/src/XeroPHP');
  81. $loader->addNamespace('\Biukop', dirname(__FILE__) . '/' );
  82. $this->xero = new Xero();
  83. $this->xero->init_wp();
  84. //$abc = new AddrMap("01515b52-6936-46b2-a000-9ad4cd7a5b50", "0768db6d-e5f4-4b45-89a2-29f7e8d2953c");
  85. $abc = new AddrMap("122eb1d0-d8c4-4fc3-8bf8-b7825bee1a01", "0768db6d-e5f4-4b45-89a2-29f7e8d2953c");
  86. }
  87. //init database
  88. public function db_install () {
  89. global $wpdb;
  90. $charset_collate = $wpdb->get_charset_collate();
  91. //table name: timesheets jobs
  92. $table_name = $this->table_name;
  93. $sql = "CREATE TABLE $table_name (
  94. id INT NOT NULL AUTO_INCREMENT,
  95. tos VARCHAR(45) NULL,
  96. start DATETIME NULL,
  97. finish DATETIME NULL,
  98. rate VARCHAR(45) NULL,
  99. staff VARCHAR(45) NULL,
  100. client VARCHAR(45) NULL,
  101. ack TINYINT(4) NULL,
  102. rating INT(4) NULL DEFAULT 0,
  103. PRIMARY KEY (id)
  104. ) $charset_collate;";
  105. //addr distance
  106. $addr_table = $this->addr_table;
  107. $sql_addr = "CREATE TABLE $addr_table (
  108. id INT NOT NULL AUTO_INCREMENT,
  109. origin VARCHAR(1024) NULL,
  110. destination VARCHAR(1024) NULL,
  111. response VARCHAR(40960) NULL,
  112. distance INT NULL,
  113. PRIMARY KEY (id)
  114. ) $charset_collate;";
  115. $ndis_table = $this->ndis_table;
  116. $sql_ndis_price = "
  117. CREATE TABLE $ndis_table (
  118. code VARCHAR(45) NOT NULL,
  119. name VARCHAR(45) NULL,
  120. level INT NULL,
  121. unit VARCHAR(45) NULL,
  122. price FLOAT NULL,
  123. year INT NOT NULL,
  124. PRIMARY KEY (code, year)
  125. )$charset_collate;";
  126. //create database
  127. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  128. dbDelta( $sql );
  129. dbDelta( $sql_addr);
  130. dbDelta( $sql_ndis_price);
  131. }
  132. //
  133. //query var
  134. public function add_query_vars($aVars) {
  135. $aVars[] = "bts_user_id"; // represents the name of the product category as shown in the URL
  136. $aVars[] = "bts_week_id"; // represents the name of the product category as shown in the URL
  137. $aVars[] = "bts_job_start"; // represents the name of the product category as shown in the URL
  138. $aVars[] = "bts_job_finish"; // represents the name of the product category as shown in the URL
  139. return $aVars;
  140. }
  141. //for customer profile and broker trans
  142. public function my_add_rewrite_rules($aRules) {
  143. $aNewRules = array(
  144. 'user/([^/]+)/?$' => 'index.php?pagename=user&bts_user_id=$matches[1]',
  145. 'task/week-([^/]+)/?$' => 'index.php?pagename=task&bts_week_id=$matches[1]',
  146. 'task/start-([^/]+)/finish-([^/]+)/?$' => 'index.php?pagename=task&bts_job_start=$matches[1]&bts_job_finish=$matches[2]',
  147. 'task/([^/]+)/?$' => 'index.php?pagename=task&bts_user_id=$matches[1]',
  148. 'task/([^/]+)/week-([^/]+)/?$' => 'index.php?pagename=task&bts_user_id=$matches[1]&bts_week_id=$matches[2]',
  149. 'task/([^/]+)/start-([^/]+)/finish-([^/]+)/?$' => 'index.php?pagename=task&bts_user_id=$matches[1]&bts_job_start=$matches[2]&bts_job_finish=$matches[3]',
  150. 'feedback_card/week-([^/]+)/?$' => 'index.php?pagename=feedback_card&bts_week_id=$matches[1]',
  151. 'feedback_card/start-([^/]+)/finish-([^/]+)/?$' => 'index.php?pagename=feedback_card&bts_job_start=$matches[1]&bts_job_finish=$matches[2]',
  152. 'feedback_card/([^/]+)/?$' => 'index.php?pagename=feedback_card&bts_user_id=$matches[1]',
  153. 'feedback_card/([^/]+)/week-([^/]+)/?$' => 'index.php?pagename=feedback_card&bts_user_id=$matches[1]&bts_week_id=$matches[2]',
  154. 'feedback_card/([^/]+)/start-([^/]+)/finish-([^/]+)/?$' => 'index.php?pagename=feedback_card&bts_user_id=$matches[1]&bts_job_start=$matches[2]&bts_job_finish=$matches[3]',
  155. );
  156. $aRules = $aNewRules + $aRules;
  157. return $aRules;
  158. }
  159. //
  160. //
  161. ///check auth
  162. public function check_auth(){
  163. global $pagename;
  164. switch($pagename){
  165. case 'task':
  166. $this->cauth_task();
  167. break;
  168. case 'time-sheets':
  169. $this->cauth_time_sheet();
  170. break;
  171. case 'feedback_card':
  172. $this->cauth_feedback_card();
  173. break;
  174. }
  175. }
  176. private function cauth_task(){
  177. $login = get_query_var( 'bts_user_id' );
  178. $this->bts_job_start = get_query_var( 'bts_job_start' );
  179. $this->bts_job_finish = get_query_var( 'bts_job_finish' );
  180. $this->bts_week_id = get_query_var('bts_week_id');
  181. $redirect_url = $this->get_redirect_url_for_task();
  182. // wp_send_json(array(
  183. // 'week'=> $week,
  184. // 'userid'=>$login,
  185. // 'job_start' => $this->bts_job_start,
  186. // 'job_finish' => $this->bts_job_finish,
  187. // 'redirect' => $redirect_url,
  188. // ));
  189. if ($login != "")//perform autologin, and redirect
  190. {
  191. $staff = get_user_by('login', $login);
  192. if ($this->is_staff($staff)){//is valid staff;
  193. $current = wp_get_current_user();
  194. if($current->ID != $staff->ID){
  195. wp_logout();
  196. wp_set_current_user($staff->ID, $staff->display_name); //this is a must
  197. wp_set_auth_cookie($staff->ID, true);//only with this, wordpress calls login + redirect and lost week-%d
  198. }
  199. }
  200. wp_redirect($redirect_url);
  201. return;
  202. }
  203. //no auto login is required if reach here.
  204. $current = wp_get_current_user();
  205. if ($this->is_admin($current)){
  206. wp_redirect("/time-sheets/");
  207. return;
  208. }
  209. if (!$this->is_staff($current) && ! $this->is_admin($current))
  210. {
  211. wp_logout();
  212. wp_redirect("/login/");
  213. return;
  214. }
  215. }
  216. private function cauth_feedback_card(){
  217. $login = get_query_var( 'bts_user_id' );
  218. $this->bts_job_start = get_query_var( 'bts_job_start' );
  219. $this->bts_job_finish = get_query_var( 'bts_job_finish' );
  220. $this->bts_week_id = get_query_var('bts_week_id');
  221. $redirect_url = $this->get_redirect_url_for_feedback_card();
  222. if ($login != "")//perform autologin, and redirect
  223. {
  224. $client = get_user_by('login', $login);
  225. if ($this->is_client($client)){//is valid client;
  226. $current = wp_get_current_user();
  227. if($current->ID != $client->ID){
  228. wp_logout();
  229. wp_set_current_user($client->ID, $client->display_name); //this is a must
  230. wp_set_auth_cookie($client->ID, true);//only with this, wordpress calls login + redirect and lost week-%d
  231. }
  232. }
  233. wp_redirect($redirect_url);
  234. return;
  235. }
  236. //no auto login is required if reach here.
  237. $current = wp_get_current_user();
  238. if ($this->is_admin($current)){
  239. wp_redirect("/time-sheets/");
  240. return;
  241. }
  242. if (!$this->is_client($current) && ! $this->is_admin($current))
  243. {
  244. wp_logout();
  245. wp_redirect("/login/");
  246. return;
  247. }
  248. }
  249. private function get_week_id()
  250. {
  251. $week = get_query_var( 'bts_week_id' );
  252. $week_id = intval($week);
  253. if ($week_id == 0 || $week_id >53 ||$week_id < 1)
  254. return $this->get_current_week_id();
  255. else
  256. return $week;
  257. }
  258. private function get_current_week_id()
  259. {
  260. $now = new \DateTime();
  261. $week = $now->format("W");
  262. return $week;
  263. }
  264. private function get_redirect_url_for_task()
  265. {
  266. if ($this->bts_week_id != "")
  267. return "/task/week-" . $this->bts_week_id . "/";
  268. if ($this->bts_job_start!="" && $this->bts_job_finish !="")
  269. return "/task/start-" . $this->bts_job_start . "/finish-" .$this->bts_job_finish . "/";
  270. return '/task/';
  271. }
  272. private function get_redirect_url_for_feedback_card()
  273. {
  274. if ($this->bts_week_id != "")
  275. return "/feedback_card/week-" . $this->bts_week_id . "/";
  276. if ($this->bts_job_start!="" && $this->bts_job_finish !="")
  277. return "/feedback_card/start-" . $this->bts_job_start . "/finish-" .$this->bts_job_finish . "/";
  278. return '/feedback_card/';
  279. }
  280. private function cauth_time_sheet()
  281. {
  282. $current = wp_get_current_user();
  283. if ($current->ID == 0 ) { //visitor not logged in
  284. wp_redirect("/wp-login.php?");
  285. return;
  286. }
  287. if ($this->is_staff($current)){
  288. wp_redirect("/task");
  289. return;
  290. }
  291. if ($this->is_admin($current)){
  292. //proceed
  293. return;
  294. }
  295. if ($this->is_client($current)){
  296. wp_redirect("/service");
  297. return;
  298. }
  299. //everything else
  300. wp_redirect("/?invalid-access");
  301. }
  302. ///
  303. // enqueue / register css /js
  304. //
  305. public function register_js_css() {
  306. $this->nonce = wp_create_nonce('acaresydney');
  307. $this->bts_user_id = get_query_var( 'bts_user_id' ) ;
  308. $this->register_bts_js();
  309. $this->register_timesheet_js_css();
  310. $this->register_task_js_css();
  311. $this->register_feedback_card_js_css();
  312. }
  313. private function register_bts_js()
  314. {
  315. wp_enqueue_style( 'bts', plugins_url('css/ts.css', __FILE__));
  316. wp_enqueue_script('bts', plugins_url('js/ts.js', __FILE__), array('jquery', 'jquery-ui-core'));
  317. wp_localize_script( 'bts', 'bts1', array(
  318. 'ajax_url' => admin_url( 'admin-ajax.php' ),
  319. 'nonce' => $this->nonce, // It is common practice to comma after
  320. 'display_name' => wp_get_current_user()->display_name,
  321. 'anonymous' => !is_user_logged_in(),
  322. 'me'=> get_current_user_id(),
  323. 'userid'=> $this->bts_user_id,
  324. 'load_user_img'=> plugins_url('img/loading_user.gif', __FILE__),
  325. 'load_job_img'=> plugins_url('img/loading_job.gif', __FILE__),
  326. 'earnings_rate'=> get_option('bts_payitem_earnings_rate'),
  327. 'high_pay_keywords' => ['sat ', 'sun ', 'high ', 'public holiday'], //space is important
  328. ) );
  329. }
  330. private function register_timesheet_js_css(){
  331. global $pagename;
  332. if ($pagename != 'time-sheets'){
  333. return;
  334. }
  335. wp_enqueue_style( 'bts_ts', plugins_url('css/bts_timesheet.css', __FILE__));
  336. wp_enqueue_script( 'bts_ts', plugins_url('js/bts_timesheet.js', __FILE__), array( 'jquery' , 'bts' ));
  337. wp_enqueue_script('mustache', plugins_url('js/mustache.min.js', __FILE__), array('jquery'));
  338. }
  339. private function register_task_js_css(){
  340. global $pagename;
  341. if ($pagename != 'task'){
  342. return;
  343. }
  344. $this->bts_job_start = get_query_var( 'bts_job_start' );
  345. $this->bts_job_finish = get_query_var( 'bts_job_finish' );
  346. $this->bts_week_id = get_query_var('bts_week_id');
  347. wp_enqueue_style( 'bts_task', plugins_url('css/bts_task.css', __FILE__));
  348. wp_enqueue_script( 'bts_task', plugins_url('js/bts_task.js', __FILE__), array( 'jquery' , 'bts' ));
  349. wp_enqueue_script('mustache', plugins_url('js/mustache.min.js', __FILE__), array('jquery'));
  350. wp_localize_script('bts_task','bts_task1',array(
  351. 'ajax_url' => admin_url( 'admin-ajax.php' ),
  352. 'nonce' => wp_create_nonce('bts_task'),
  353. 'week_id' => $this->bts_week_id,
  354. 'bts_job_start' => $this->bts_job_start,
  355. 'bts_job_finish' => $this->bts_job_finish,
  356. ) );
  357. }
  358. private function register_feedback_card_js_css()
  359. {
  360. global $pagename;
  361. if ($pagename != 'feedback_card'){
  362. return;
  363. }
  364. $this->bts_job_start = get_query_var( 'bts_job_start' );
  365. $this->bts_job_finish = get_query_var( 'bts_job_finish' );
  366. $this->bts_week_id = get_query_var('bts_week_id');
  367. wp_enqueue_style( 'feedback_card', plugins_url('css/feedback_card.css', __FILE__));
  368. wp_enqueue_script( 'feedback_card', plugins_url('js/feedback_card.js', __FILE__), array( 'jquery' , 'bts' ));
  369. wp_enqueue_script('mustache', plugins_url('js/mustache.min.js', __FILE__), array('jquery'));
  370. wp_localize_script('feedback_card','feedback_card',array(
  371. 'ajax_url' => admin_url( 'admin-ajax.php' ),
  372. 'nonce' => wp_create_nonce('feedback_card'),
  373. 'week_id' => $this->bts_week_id,
  374. 'bts_job_start' => $this->bts_job_start,
  375. 'bts_job_finish' => $this->bts_job_finish,
  376. ) );
  377. }
  378. public function sync_users()
  379. {
  380. //dummy sync
  381. return;
  382. }
  383. // Usage: `wp sync_users --mininterval=123
  384. public function sync_user_cli($args = array(), $assoc_args = array()){
  385. $arguments = wp_parse_args( $assoc_args, array(
  386. 'mininterval' => 600,
  387. 'employeeonly' => false,
  388. 'clientsonly' => false,
  389. ) );
  390. $this->xero->sync_users($arguments['mininterval'], $arguments['employeeonly'], $arguments['clientsonly']);
  391. return;
  392. }
  393. public function email_jobs($args = array(), $assoc_args = array()){
  394. $users = get_users(array('role' => 'staff'));
  395. foreach ($users as $u){
  396. $n = new UserJob($u->user_login);
  397. $resp = $n->list_jobs_by_staff("2019-07-22 00:00:00", "2019-07-28 23:59:59");
  398. if ($resp['status']=='success' && $resp['job_count'] >0 ){
  399. if( $u->user_login != "9aa3308e-cc19-4c21-a110-f2c6abec4337" )
  400. continue;
  401. $msg = sprintf("Staff = %s, Login=%s, email=%s Job=%d\n", $u->display_name, $u->user_login, $u->user_email, $resp['job_count']);
  402. echo $msg;
  403. $this->send_email_with_job_link($u, "2019-07-22", "2019-07-28");
  404. }
  405. }
  406. return;
  407. }
  408. private function send_email_with_job_link($staff, $start, $finish)
  409. {
  410. $message = file_get_contents(plugin_dir_path(__FILE__) . "/html/email_job.html");
  411. $message = str_ireplace("{{display_name}}", $staff->display_name, $message);
  412. $message = str_ireplace("{{user_login}}", $staff->user_login, $message);
  413. $message = str_ireplace("{{job_start}}", $start, $message);
  414. $message = str_ireplace("{{job_finish}}", $finish, $message);
  415. $headers = ['Bcc: patrick@biukop.com.au, timesheet@acaresydney.com.au'];
  416. $subject = $staff->display_name . " Job arrangement $start ~ $finish";
  417. //wp_mail("sp@lawipac.com", $subject, $message, $headers);
  418. //wp_mail("timesheet@acaresydney.com.au", $subject, $message, $headers);
  419. wp_mail($staff->user_email, $subject, $message, $headers);
  420. }
  421. public function bts_staff_item($attr){
  422. return $this->template('staff_item', 'staff.html');
  423. }
  424. public function bts_client_item($attr){
  425. return $this->template('client_item', 'client.html');
  426. }
  427. public function bts_job_item($attr){
  428. $html =$this->template('job_item', 'job.html');
  429. //$html = str_replace('[bts-tos-options]', $this->bts_tos_options([]), $html);
  430. $html = do_shortcode($html);
  431. return $html;
  432. }
  433. public function bts_rate_options($attr){
  434. $result = "<select> \n";
  435. $options = get_option('bts_payitem_earnings_rate');
  436. foreach($options as $o){
  437. $result.=sprintf("<option value='%s'> $%3.2f-%s</option>",
  438. $o['EarningsRateID'], $o['RatePerUnit'], $o['Name']);
  439. }
  440. $result .="</select>";
  441. return $result;
  442. }
  443. public function bts_select_staff($attr){
  444. $result = "<select> \n";
  445. $staff = $this->get_people_by_role('staff');
  446. foreach ($staff as $u){
  447. $result .= sprintf("<option value=%s> %s</option>", $u->user_login, $u->first_name . " " . $u->last_name);
  448. }
  449. $result .="</select>";
  450. return $result;
  451. }
  452. public function bts_select_client($attr){
  453. $result = "<select> \n";
  454. $staff = $this->get_people_by_role('client');
  455. foreach ($staff as $u){
  456. $result .= sprintf("<option value=%s> %s</option>", $u->user_login, $u->display_name);
  457. }
  458. $result .="</select>";
  459. return $result;
  460. }
  461. public function bts_type_of_service($attr){
  462. $n = new NdisPrice(2019);
  463. return $n->get_html();
  464. }
  465. public function bts_user_name($attr)
  466. {
  467. $user = wp_get_current_user();
  468. return $user->display_name;
  469. }
  470. public function bts_staff_job_summary($attr)
  471. {
  472. $result ="<span>
  473. If there is more than one job, please click on 'confirm' to make sure it is included in your payment.
  474. </span>";
  475. return $result;
  476. }
  477. public function bts_feedback_card($attr)
  478. {
  479. return $this->template('bts_feedback_card', 'feedback_card.html');
  480. }
  481. //generate template based on html file
  482. private function template($id, $file)
  483. {
  484. $text = '<script id="' . $id .'" type="text/x-biukop-template">';
  485. $text .= file_get_contents(plugin_dir_path(__FILE__) . "/html/$file");
  486. $text .= '</script>';
  487. return $text;
  488. }
  489. function list_staff(){
  490. check_ajax_referer('acaresydney');
  491. // Handle the ajax request
  492. $response = array(
  493. 'status' =>'error',
  494. 'users' => [],
  495. );
  496. //search all users that are staff
  497. $staffq = new \WP_User_Query(array('role'=>'staff','meta_key'=>'first_name', 'orderby'=>'meta_value', 'order'=>'ASC'));
  498. $staff = $staffq->get_results();
  499. if (! empty($staff)){
  500. $response['status'] = 'success';
  501. foreach( $staff as $s){
  502. $response['users'][] = array(
  503. 'login' => $s->user_login,
  504. 'firstname'=> $s->first_name,
  505. 'lastname'=> $s->last_name,
  506. 'mobile'=> get_user_meta($s->ID, 'mobile', true),
  507. 'email'=> $s->user_email,
  508. 'wages'=> 0,
  509. 'hour' => 0 ,
  510. 'OT' => 0 ,
  511. 'petrol'=> 0 ,
  512. 'rating'=> 0,
  513. 'unconfirmedjob'=> 0,
  514. );
  515. }
  516. }
  517. wp_send_json($response);
  518. wp_die();
  519. }
  520. function list_client(){
  521. check_ajax_referer('acaresydney');
  522. // Handle the ajax request
  523. $response = array(
  524. 'status' =>'error',
  525. 'users' => [],
  526. );
  527. //search all users that are staff
  528. $clientq = new \WP_User_Query(array('role'=>'client', 'meta_key'=>'first_name', 'orderby'=>'meta_value', 'order'=>'ASC'));
  529. $client = $clientq->get_results();
  530. if (! empty($client)){
  531. $response['status'] = 'success';
  532. foreach( $client as $s){
  533. $response['users'][] = array(
  534. 'login' => $s->user_login,
  535. 'firstname'=> $s->first_name,
  536. 'lastname'=> $s->last_name,
  537. 'display_name'=> $s->display_name,
  538. 'mobile'=> get_user_meta($s->ID, 'mobile', true),
  539. 'email'=> $s->user_email,
  540. 'account'=> get_user_meta($s->ID, 'account', true),
  541. 'address' => get_user_meta($s->ID, 'address', true),
  542. 'rating'=> 0,
  543. 'unconfirmedjob'=> 0,
  544. );
  545. }
  546. }
  547. wp_send_json($response);
  548. wp_die();
  549. }
  550. private function get_people_by_role($role){
  551. //search all users that are staff
  552. $staffq = new \WP_User_Query(array('role'=>$role, 'meta_key'=>'first_name', 'orderby'=>'meta_value', 'order'=>'ASC'));
  553. $staff = $staffq->get_results();
  554. return $staff;
  555. }
  556. //ajax get earnings rates
  557. function get_payitem_earnings_rate()
  558. {
  559. $response= array(
  560. 'status' => 'success',
  561. 'options'=> get_option('bts_payitem_earnings_rate'),
  562. );
  563. wp_send_json($response);
  564. }
  565. //ajax job CRUD
  566. function save_job()
  567. {
  568. check_ajax_referer('acaresydney');
  569. $r = $_POST['record'];
  570. $response = array();
  571. $d = array(
  572. 'tos' => $r['tos'],
  573. 'start' => $r['start'],
  574. 'finish' => $r['finish'],
  575. 'rate' => $r['rate'],
  576. 'staff' => $r['staff'],
  577. 'client' => $r['client'],
  578. 'ack' => $r['ack']=='true'?1:0,
  579. 'rating'=>$r['rating'],
  580. );
  581. //this is an update
  582. if ( isset($r['id']) && trim($r['id']) !='' && is_numeric($r['id'])){
  583. $response['isNew'] = false; //add or update?
  584. $result = $this->db->update($this->table_name, $d, array('id' =>$r['id']));
  585. if ($result !== false && $this->db->last_error == ''){
  586. $d['id'] = $r['id'];
  587. $response['status'] = 'success';
  588. //do data type conversion, string to int
  589. $response['newdata'] = $this->get_ts_record($r['id']);
  590. $response['errors'] = array(); //empty array
  591. }else{
  592. $response['status'] = 'error';
  593. $repsonse['errors'] = array(
  594. 'db' => "network database error" . $this->db->last_error,
  595. );
  596. }
  597. }else{
  598. $response['isNew'] = true;
  599. $result = $this->db->insert($this->table_name, $d);
  600. $lastid = $this->db->insert_id;
  601. if ($result != false && $this->db->last_error == ''){
  602. $response['status'] = 'success';
  603. $response['newdata'] = $this->get_ts_record($lastid);
  604. }else{
  605. $response['status'] = 'error';
  606. $response['errors'] = array(
  607. 'db' => 'network database error ' . $this->db->last_error,
  608. );
  609. }
  610. }
  611. wp_send_json($response);
  612. wp_die();
  613. }
  614. private function get_ts_record($id){
  615. $sql = "SELECT * FROM $this->table_name WHERE id=%d";
  616. $row = $this->db->get_row($this->db->prepare ($sql, array($id)));
  617. $response = [];
  618. if ($row != null){
  619. $response = array(
  620. 'id' => (int)$row->id,
  621. 'tos' => $row->tos,
  622. 'start' => $row->start,
  623. 'finish' => $row->finish,
  624. 'rate' => $row->rate,
  625. 'staff' => $row->staff,
  626. 'client' => $row->client,
  627. 'ack' => (int)$row->ack,
  628. 'rating' =>(int) $row->rating,
  629. );
  630. }
  631. return $response;
  632. }
  633. //ajax delete job
  634. function delete_job(){
  635. check_ajax_referer('acaresydney');
  636. $id = $_POST['jobid'];
  637. $result = $this->db->delete($this->table_name, array('id'=> $id));
  638. $response=array(
  639. 'status' => 'success',
  640. 'id' => $id,
  641. 'action'=> 'delete',
  642. 'error' => '',
  643. );
  644. if ($result == 1){
  645. wp_send_json($response);
  646. }else{
  647. $response['status'] = 'error';
  648. $response['error'] = $this->db->last_error;
  649. wp_send_json($response);
  650. }
  651. wp_die();
  652. }
  653. //ajax email staff their job arrangement
  654. function email_job()
  655. {
  656. check_ajax_referer('acaresydney');
  657. $staff = $_POST['staff'];
  658. $start = $_POST['start'];
  659. $finish = $_POST['finish'];
  660. $response=array(
  661. 'status' => 'success',
  662. 'staff' => $staff,
  663. 'start' => $start,
  664. 'finish' => $finish,
  665. 'error' => '',
  666. 'sent' => false,
  667. 'emailstatus'=>"Bypass (no job)",
  668. );
  669. $u = get_user_by('login', $staff);
  670. if ($this->is_staff($u)){
  671. $n = new UserJob($staff);
  672. $resp = $n->list_jobs("$start 00:00:00", "$finish 23:59:59");
  673. if ($resp['status']=='success' && $resp['job_count'] >0 ){
  674. $msg = sprintf("Email to <strong>%s</strong> (with job=%d) \n", $u->user_email, $resp['job_count']);
  675. $this->send_email_with_job_link($u, $start, $finish);
  676. $response['sent'] = true;
  677. $response['emailstatus'] = $msg;
  678. }
  679. }
  680. wp_send_json($response);
  681. }
  682. //ajax browse job with different filters
  683. function list_job(){
  684. check_ajax_referer('acaresydney');
  685. $start = $_POST['start'] . " 00:00:00";
  686. $finish = $_POST['finish']. " 23:59:59";
  687. $response = array(
  688. 'status'=>'success',
  689. 'jobs' => [],
  690. );
  691. $sql = "SELECT * FROM $this->table_name WHERE start>='%s' and start <='%s' order by start ASC ,staff ASC";
  692. $query = $this->db->prepare ($sql, array($start, $finish));
  693. $response['sql'] = $query;
  694. $jobs = $this->db->get_results($query);
  695. if (! empty($jobs)){
  696. $response['status'] = 'success';
  697. foreach( $jobs as $s){
  698. $response['jobs'][] = array(
  699. 'id' => $s->id,
  700. 'tos' => $s->tos,
  701. 'start'=> $s->start,
  702. 'finish'=> $s->finish,
  703. 'rate'=> $s->rate,
  704. 'staff'=> $s->staff,
  705. 'client'=> $s->client,
  706. 'ack' => $s->ack,
  707. 'rating' =>$s->rating,
  708. );
  709. }
  710. }
  711. wp_send_json($response);
  712. wp_die();
  713. }
  714. public function list_job_by_staff()
  715. {
  716. check_ajax_referer('acaresydney');
  717. $start = $_POST['start'];
  718. $finish = $_POST['finish'];
  719. //$start="2019-07-01 00:00:00";
  720. //$finish="2019-07-14 23:59:59";
  721. $user = wp_get_current_user();// should be staff;
  722. if ( $this->is_staff($user) || $this->is_admin($user) ){
  723. $n = new UserJob($user->user_login);
  724. $response = $n->list_jobs_by_staff($start, $finish);
  725. wp_send_json($response);
  726. }else{
  727. $response = array(
  728. 'status' => 'error',
  729. 'errmsg' => 'invalid access',
  730. 'user' => $user,
  731. );
  732. wp_send_json($response);
  733. }
  734. wp_die();
  735. }
  736. public function list_job_by_client()
  737. {
  738. check_ajax_referer('acaresydney');
  739. $start = $_POST['start'];
  740. $finish = $_POST['finish'];
  741. //$start="2019-07-01 00:00:00";
  742. //$finish="2019-07-14 23:59:59";
  743. $user = wp_get_current_user();// should be staff;
  744. if ( $this->is_client($user) || $this->is_admin($user) ){
  745. $n = new UserJob($user->user_login);
  746. $response = $n->list_jobs_by_client($start, $finish);
  747. wp_send_json($response);
  748. }else{
  749. $response = array(
  750. 'status' => 'error',
  751. 'errmsg' => 'invalid access',
  752. 'user' => $user,
  753. );
  754. wp_send_json($response);
  755. }
  756. wp_die();
  757. }
  758. private function is_staff($user)
  759. {
  760. return ($user->ID !=0 && in_array('staff', $user->roles));
  761. }
  762. private function is_client($user)
  763. {
  764. return ($user->ID !=0 && in_array('client', $user->roles));
  765. }
  766. private function is_admin($user)
  767. {
  768. $allowed_roles = array('administrator', 'acare_owner');
  769. if( array_intersect($allowed_roles, $user->roles ) ) {
  770. return true;
  771. }
  772. }
  773. public function staff_ack_job()
  774. {
  775. check_ajax_referer('acaresydney');
  776. $jobs = $_POST['jobs'];
  777. $response = array(
  778. 'status'=>'success',
  779. 'jobs'=>$jobs,
  780. );
  781. $yes=[];
  782. $no=[];
  783. foreach($jobs as $job){
  784. if ( $job['ack'] == "true")
  785. $yes[] =(int) $job['id'];
  786. else
  787. $no[] = (int) $job['id'];
  788. }
  789. $err = $this->ack_multiple_job($yes, $no);
  790. if ($this->db->last_error !='')
  791. {
  792. $response['status']= 'error';
  793. $response['err_msg']= $err;
  794. }
  795. $response['yes'] = $yes;
  796. $response['no'] = $no;
  797. wp_send_json($response);
  798. wp_die();
  799. }
  800. public function ack_multiple_job($yes, $no)
  801. {
  802. $str_yes_ids = implode(",", $yes);
  803. $str_no_ids = implode(",", $no);
  804. $err = "";
  805. if (count($yes) >0 ){
  806. $sql = "UPDATE $this->table_name SET ack=1 WHERE id IN ( $str_yes_ids) ; ";
  807. $r = $this->db->get_results($sql);
  808. $err = $this->db->last_error;
  809. }
  810. if (count($no) >0 ){
  811. $sql = "UPDATE $this->table_name SET ack=0 WHERE id IN ( $str_no_ids) ; ";
  812. $r = $this->db->get_results($sql);
  813. $err .= $this->db->last_error;
  814. }
  815. return $err;
  816. }
  817. public function client_ack_job()
  818. {
  819. check_ajax_referer('acaresydney');
  820. $job_id = $_POST['job_id'];
  821. $rating = $_POST['rating'];
  822. $response = array(
  823. 'status'=>'success',
  824. );
  825. $sql= "UPDATE $this->table_name SET rating=%d WHERE id = %d ; ";
  826. $sql= $this->db->prepare($sql, array($rating, $job_id));
  827. $result = $this->db->get_results($sql);
  828. $response['rating'] = (int) $rating;
  829. if ($this->db->last_error !='')
  830. {
  831. $response['status']= 'error';
  832. $response['err_msg']= $this->db->last_error;
  833. $response['rating'] = 0;
  834. }
  835. wp_send_json($response);
  836. }
  837. public function feedback_url()
  838. {
  839. $users = get_users(array('role'=>'client'));
  840. foreach($users as $u){
  841. echo sprintf("%s:\t https://acaresydney.com.au/feedback_card/%s/\n", $u->display_name, $u->user_login);
  842. }
  843. }
  844. }
  845. $bb = new AcareOffice();
  846. if ( defined( 'WP_CLI' ) && WP_CLI ) {
  847. \WP_CLI::add_command( 'sync_users', array($bb, 'sync_user_cli'));
  848. \WP_CLI::add_command( 'email_jobs', array($bb, 'email_jobs'));
  849. \WP_CLI::add_command( 'feedback_url', array($bb, 'feedback_url'));
  850. }
  851. //$bb->class_loader();
  852. //$bb->list_job_by_staff();