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

427 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_action('wp_ajax_list_staff', array($this,'list_staff' ));
  37. add_action('wp_ajax_list_client', array($this,'list_client' ));
  38. add_action('wp_ajax_save_job', array($this,'save_job' ));
  39. add_action('wp_ajax_list_job', array($this,'list_job' ));
  40. add_action('wp_ajax_delete_job', array($this,'delete_job' ));
  41. add_action('wp_ajax_earnings_rate', array($this,'get_payitem_earnings_rate' ));
  42. add_action('wp_ajax_nopriv_earnings_rate', array($this,'get_payitem_earnings_rate' ));
  43. global $wpdb;
  44. $this->db = $wpdb;
  45. $this->table_name = $wpdb->prefix . 'acare_ts';
  46. }
  47. /**
  48. * Autoload the custom theme classes
  49. */
  50. public function class_loader()
  51. {
  52. // Create a new instance of the autoloader
  53. $loader = new \Psr4AutoloaderClass();
  54. // Register this instance
  55. $loader->register();
  56. // Add our namespace and the folder it maps to
  57. $loader->addNamespace('\XeroPHP', dirname(__FILE__) . '/xero-php-master/src/XeroPHP');
  58. $loader->addNamespace('\Biukop', dirname(__FILE__) . '/' );
  59. $this->xero = new Xero();
  60. $this->xero->init_wp();
  61. }
  62. //init database
  63. public function db_install () {
  64. global $wpdb;
  65. $charset_collate = $wpdb->get_charset_collate();
  66. //table name: broker transactions
  67. $table_name = $this->table_name;
  68. $sql = "CREATE TABLE $table_name (
  69. id INT NOT NULL AUTO_INCREMENT,
  70. tos VARCHAR(45) NULL,
  71. start DATETIME NULL,
  72. finish DATETIME NULL,
  73. rate VARCHAR(45) NULL,
  74. staff VARCHAR(45) NULL,
  75. client VARCHAR(45) NULL,
  76. ack TINYINT(4) NULL,
  77. rating INT NULL DEFAULT 0,
  78. PRIMARY KEY (id)
  79. ) $charset_collate;";
  80. // $sql = "CREATE TABLE $table_name (
  81. // id int(64) NOT NULL AUTO_INCREMENT,
  82. // paid tinyint(4) DEFAULT NULL,
  83. // referal varchar(45) COLLATE utf8mb4_unicode_520_ci DEFAULT NULL,
  84. // clientid int(64) NOT NULL,
  85. // loanamount float DEFAULT NULL,
  86. // settledate date DEFAULT NULL,
  87. // loanterm mediumint(10) DEFAULT NULL,
  88. // commissionrate float DEFAULT NULL,
  89. // commission float DEFAULT NULL,
  90. // trailrate float DEFAULT NULL,
  91. // trail float DEFAULT NULL,
  92. // paydate date DEFAULT NULL,
  93. // brokerid int(64) NOT NULL,
  94. // status tinyint(4) NOT NULL,
  95. // notes varchar(255) COLLATE utf8mb4_unicode_520_ci DEFAULT NULL,
  96. // PRIMARY KEY (id)
  97. // ) $charset_collate;";
  98. //create database
  99. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  100. dbDelta( $sql );
  101. }
  102. //
  103. //
  104. ///check auth
  105. public function check_auth(){
  106. global $pagename;
  107. //echo $pagename;
  108. }
  109. ///
  110. // enqueue / register css /js
  111. //
  112. public function register_js_css() {
  113. $this->nonce = wp_create_nonce('acaresydney');
  114. $this->acaresydney_userid = get_query_var( 'acaresydney_userid' ) ;
  115. $this->register_bts_js();
  116. $this->register_timesheet_js_css();
  117. }
  118. private function register_bts_js()
  119. {
  120. wp_enqueue_style( 'bts', plugins_url('css/ts.css', __FILE__));
  121. wp_enqueue_script('bts', plugins_url('js/ts.js', __FILE__), array('jquery', 'jquery-ui-core'));
  122. wp_localize_script( 'bts', 'bts1', array(
  123. 'ajax_url' => admin_url( 'admin-ajax.php' ),
  124. 'nonce' => $this->nonce, // It is common practice to comma after
  125. 'display_name' => wp_get_current_user()->display_name,
  126. 'anonymous' => !is_user_logged_in(),
  127. 'me'=> get_current_user_id(),
  128. 'userid'=> $this->acaresydney_userid,
  129. 'load_user_img'=> plugins_url('img/loading_user.gif', __FILE__),
  130. ) );
  131. }
  132. private function register_timesheet_js_css(){
  133. global $pagename;
  134. if ($pagename != 'time-sheets'){
  135. return;
  136. }
  137. wp_enqueue_style( 'bts_ts', plugins_url('css/bts_timesheet.css', __FILE__));
  138. wp_enqueue_script( 'bts_ts', plugins_url('js/bts_timesheet.js', __FILE__), array( 'jquery' , 'bts' ));
  139. wp_enqueue_script('mustache', plugins_url('js/mustache.min.js', __FILE__), array('jquery'));
  140. }
  141. public function sync_users()
  142. {
  143. //dummy sync
  144. return;
  145. }
  146. // Usage: `wp sync_users --mininterval=123
  147. public function sync_user_cli($args = array(), $assoc_args = array()){
  148. $arguments = wp_parse_args( $assoc_args, array(
  149. 'mininterval' => 600,
  150. ) );
  151. $this->xero->sync_users($arguments['mininterval']);
  152. return;
  153. }
  154. public function bts_staff_item($attr){
  155. return $this->template('staff_item', 'staff.html');
  156. }
  157. public function bts_client_item($attr){
  158. return $this->template('client_item', 'client.html');
  159. }
  160. public function bts_job_item($attr){
  161. $html =$this->template('job_item', 'job.html');
  162. //$html = str_replace('[bts-tos-options]', $this->bts_tos_options([]), $html);
  163. $html = do_shortcode($html);
  164. return $html;
  165. }
  166. public function bts_rate_options($attr){
  167. $result = "<select> \n";
  168. $options = get_option('bts_payitem_earnings_rate');
  169. foreach($options as $o){
  170. $result.=sprintf("<option value='%s'> $%3.2f-%s</option>",
  171. $o['EarningsRateID'], $o['RatePerUnit'], $o['Name']);
  172. }
  173. $result .="</select>";
  174. return $result;
  175. }
  176. public function bts_select_staff($attr){
  177. $result = "<select> \n";
  178. $staff = $this->get_people_by_role('staff');
  179. foreach ($staff as $u){
  180. $result .= sprintf("<option value=%s> %s</option>", $u->user_login, $u->first_name . " " . $u->last_name);
  181. }
  182. $result .="</select>";
  183. return $result;
  184. }
  185. public function bts_select_client($attr){
  186. $result = "<select> \n";
  187. $staff = $this->get_people_by_role('client');
  188. foreach ($staff as $u){
  189. $result .= sprintf("<option value=%s> %s</option>", $u->user_login, $u->first_name . " " . $u->last_name);
  190. }
  191. $result .="</select>";
  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. );
  292. //this is an update
  293. if ( isset($r['id']) && trim($r['id']) !='' && is_numeric($r['id'])){
  294. $response['isNew'] = false; //add or update?
  295. $result = $this->db->update($this->table_name, $d, array('id' =>$r['id']));
  296. if ($result !== false && $this->db->last_error == ''){
  297. $d['id'] = $r['id'];
  298. $response['status'] = 'success';
  299. //do data type conversion, string to int
  300. $response['newdata'] = $this->get_ts_record($r['id']);
  301. $response['errors'] = array(); //empty array
  302. }else{
  303. $response['status'] = 'error';
  304. $repsonse['errors'] = array(
  305. 'db' => "network database error" . $this->db->last_error,
  306. );
  307. }
  308. }else{
  309. $response['isNew'] = true;
  310. $result = $this->db->insert($this->table_name, $d);
  311. $lastid = $this->db->insert_id;
  312. if ($result != false && $this->db->last_error == ''){
  313. $response['status'] = 'success';
  314. $response['newdata'] = $this->get_ts_record($lastid);
  315. }else{
  316. $response['status'] = 'error';
  317. $response['errors'] = array(
  318. 'db' => 'network database error ' . $this->db->last_error,
  319. );
  320. }
  321. }
  322. wp_send_json($response);
  323. wp_die();
  324. }
  325. private function get_ts_record($id){
  326. $sql = "SELECT * FROM $this->table_name WHERE id=%d";
  327. $row = $this->db->get_row($this->db->prepare ($sql, array($id)));
  328. $response = [];
  329. if ($row != null){
  330. $response = array(
  331. 'id' => (int)$row->id,
  332. 'tos' => $row->tos,
  333. 'start' => $row->start,
  334. 'finish' => $row->finish,
  335. 'rate' => $row->rate,
  336. 'staff' => $row->staff,
  337. 'client' => $row->client,
  338. 'ack' => (int)$row->ack,
  339. 'rating' =>(int) $row->rating,
  340. );
  341. }
  342. return $response;
  343. }
  344. //ajax delete job
  345. function delete_job(){
  346. check_ajax_referer('acaresydney');
  347. $id = $_POST['recordid'];
  348. $result = $this->db->delete($this->table_name, array('id'=> $id));
  349. $response=array(
  350. 'status' => 'success',
  351. 'id' => $id,
  352. 'action'=> 'delete',
  353. 'error' => '',
  354. );
  355. if ($result == 1){
  356. wp_send_json($response);
  357. }else{
  358. $response['status'] = 'error';
  359. $response['error'] = $this->db->last_error;
  360. wp_send_json($response);
  361. }
  362. wp_die();
  363. }
  364. //ajax browse job with different filters
  365. function list_job(){
  366. check_ajax_referer('acaresydney');
  367. $r = $_POST['record'];
  368. $response = array();
  369. wp_send_json($response);
  370. wp_die();
  371. }
  372. }
  373. $bb = new AcareOffice();
  374. if ( defined( 'WP_CLI' ) && WP_CLI ) {
  375. \WP_CLI::add_command( 'sync_users', array($bb, 'sync_user_cli'));
  376. }