|
- <?php
- /**
- * Plugin Name: Acare Advanced Office
- * Plugin URI: http://biukop.com.au/acaresydney/timesheets
- * Description: Advanced Office system, timesheet, Payroll for AcareSydney
- * Version: 2.1
- * Author: Biukop Intelligence
- * Author URI: http://biukop.com.au/
- */
-
- namespace Biukop;
- use XeroPHP\Models\Accounting\Address;
-
- require_once(dirname(__FILE__) . '/autoload.php');
- require_once (ABSPATH . 'wp-includes/pluggable.php');
-
-
- class AcareOffice{
- private $nonce; //for ajax verification
- private $pages = array('time-sheets', 'user-list');
- private $acaresydney_userid = 0;
- private $xero ;
- private $db;
- private $table_name;
- private $addr_table;
-
- public function __construct() {
- add_option( "acare_ts_db_version", "1.0" );
- register_activation_hook( __FILE__, array($this, 'db_install') );
-
- add_action('init', array($this, 'class_loader'));
- add_action('wp', array($this, 'check_auth'));
- add_action('wp_enqueue_scripts', array($this, 'register_js_css'), 99);
-
- add_filter('show_admin_bar', '__return_false');
-
- //ts-xx for sync single user
- add_shortcode( 'ts-sync-users', array($this, 'sync_users'));
- //bts-xx for webpage
- add_shortcode( 'bts_staff_item', array($this, 'bts_staff_item'));
- add_shortcode( 'bts_client_item', array($this, 'bts_client_item'));
- add_shortcode( 'bts_job_item', array($this, 'bts_job_item'));
- add_shortcode( 'bts_rate_options', array($this, 'bts_rate_options'));
- add_shortcode( 'bts_select_staff', array($this, 'bts_select_staff'));
- add_shortcode( 'bts_select_client', array($this, 'bts_select_client'));
- add_shortcode( 'bts_type_of_service', array($this, 'bts_type_of_service'));
- //user profile page
- add_shortcode( 'bts_user_name', array($this,'bts_user_name'));
-
-
- add_action('wp_ajax_list_staff', array($this,'list_staff' ));
- add_action('wp_ajax_list_client', array($this,'list_client' ));
- add_action('wp_ajax_save_job', array($this,'save_job' ));
- add_action('wp_ajax_list_job', array($this,'list_job' ));
- add_action('wp_ajax_delete_job', array($this,'delete_job' ));
-
- add_action('wp_ajax_earnings_rate', array($this,'get_payitem_earnings_rate' ));
- add_action('wp_ajax_nopriv_earnings_rate', array($this,'get_payitem_earnings_rate' ));
-
- // hook add_rewrite_rules function into rewrite_rules_array
- add_filter('rewrite_rules_array', array($this,'my_add_rewrite_rules'));
- // hook add_query_vars function into query_vars
- add_filter('query_vars', array($this,'add_query_vars'));
-
-
- global $wpdb;
- $this->db = $wpdb;
- $this->table_name = $wpdb->prefix . 'acare_ts';
- $this->addr_table = $wpdb->prefix . 'acare_addr_distance';
- $this->ndis_table = $wpdb->prefix . 'acare_ndis_price';
-
- }
-
- /**
- * Autoload the custom theme classes
- */
- public function class_loader()
- {
- // Create a new instance of the autoloader
- $loader = new \Psr4AutoloaderClass();
-
- // Register this instance
- $loader->register();
-
- // Add our namespace and the folder it maps to
- $loader->addNamespace('\XeroPHP', dirname(__FILE__) . '/xero-php-master/src/XeroPHP');
- $loader->addNamespace('\Biukop', dirname(__FILE__) . '/' );
-
- $this->xero = new Xero();
- $this->xero->init_wp();
-
- //$abc = new AddrMap("01515b52-6936-46b2-a000-9ad4cd7a5b50", "0768db6d-e5f4-4b45-89a2-29f7e8d2953c");
- $abc = new AddrMap("122eb1d0-d8c4-4fc3-8bf8-b7825bee1a01", "0768db6d-e5f4-4b45-89a2-29f7e8d2953c");
-
- }
-
- //init database
- public function db_install () {
- global $wpdb;
- $charset_collate = $wpdb->get_charset_collate();
-
- //table name: timesheets jobs
- $table_name = $this->table_name;
- $sql = "CREATE TABLE $table_name (
- id INT NOT NULL AUTO_INCREMENT,
- tos VARCHAR(45) NULL,
- start DATETIME NULL,
- finish DATETIME NULL,
- rate VARCHAR(45) NULL,
- staff VARCHAR(45) NULL,
- client VARCHAR(45) NULL,
- ack TINYINT(4) NULL,
- rating INT(4) NULL DEFAULT 0,
- PRIMARY KEY (id)
- ) $charset_collate;";
-
- //addr distance
- $addr_table = $this->addr_table;
- $sql_addr = "CREATE TABLE $addr_table (
- id INT NOT NULL AUTO_INCREMENT,
- origin VARCHAR(1024) NULL,
- destination VARCHAR(1024) NULL,
- response VARCHAR(40960) NULL,
- distance INT NULL,
- PRIMARY KEY (id)
- ) $charset_collate;";
-
- $ndis_table = $this->ndis_table;
- $sql_ndis_price = "
- CREATE TABLE $ndis_table (
- code VARCHAR(45) NOT NULL,
- name VARCHAR(45) NULL,
- level INT NULL,
- unit VARCHAR(45) NULL,
- price FLOAT NULL,
- year INT NOT NULL,
- PRIMARY KEY (code, year)
- )$charset_collate;";
- //create database
- require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
- dbDelta( $sql );
- dbDelta( $sql_addr);
- dbDelta( $sql_ndis_price);
- }
-
- //
- //query var
- public function add_query_vars($aVars) {
- $aVars[] = "bts_user_id"; // represents the name of the product category as shown in the URL
- return $aVars;
- }
-
- //for customer profile and broker trans
- public function my_add_rewrite_rules($aRules) {
- $aNewRules = array('user/([^/]+)/?$' => 'index.php?pagename=user&bts_user_id=$matches[1]');
- $aRules = $aNewRules + $aRules;
-
- return $aRules;
- }
-
-
-
- //
- //
- ///check auth
- public function check_auth(){
- global $pagename;
- //echo $pagename;
- }
-
- ///
- // enqueue / register css /js
- //
- public function register_js_css() {
- $this->nonce = wp_create_nonce('acaresydney');
- $this->acaresydney_userid = get_query_var( 'acaresydney_userid' ) ;
- $this->register_bts_js();
- $this->register_timesheet_js_css();
- }
- private function register_bts_js()
- {
- wp_enqueue_style( 'bts', plugins_url('css/ts.css', __FILE__));
- wp_enqueue_script('bts', plugins_url('js/ts.js', __FILE__), array('jquery', 'jquery-ui-core'));
- wp_localize_script( 'bts', 'bts1', array(
- 'ajax_url' => admin_url( 'admin-ajax.php' ),
- 'nonce' => $this->nonce, // It is common practice to comma after
- 'display_name' => wp_get_current_user()->display_name,
- 'anonymous' => !is_user_logged_in(),
- 'me'=> get_current_user_id(),
- 'userid'=> $this->acaresydney_userid,
- 'load_user_img'=> plugins_url('img/loading_user.gif', __FILE__),
- 'load_job_img'=> plugins_url('img/loading_job.gif', __FILE__),
- 'earnings_rate'=> get_option('bts_payitem_earnings_rate'),
- 'high_pay_keywords' => ['sat ', 'sun ', 'high ', 'public holiday'], //space is important
- ) );
- }
-
- private function register_timesheet_js_css(){
- global $pagename;
- if ($pagename != 'time-sheets'){
- return;
- }
- wp_enqueue_style( 'bts_ts', plugins_url('css/bts_timesheet.css', __FILE__));
- wp_enqueue_script( 'bts_ts', plugins_url('js/bts_timesheet.js', __FILE__), array( 'jquery' , 'bts' ));
- wp_enqueue_script('mustache', plugins_url('js/mustache.min.js', __FILE__), array('jquery'));
- }
-
- public function sync_users()
- {
- //dummy sync
- return;
- }
-
- // Usage: `wp sync_users --mininterval=123
- public function sync_user_cli($args = array(), $assoc_args = array()){
- $arguments = wp_parse_args( $assoc_args, array(
- 'mininterval' => 600,
- ) );
- $this->xero->sync_users($arguments['mininterval']);
- return;
- }
-
-
- public function bts_staff_item($attr){
- return $this->template('staff_item', 'staff.html');
- }
-
- public function bts_client_item($attr){
- return $this->template('client_item', 'client.html');
- }
-
- public function bts_job_item($attr){
- $html =$this->template('job_item', 'job.html');
- //$html = str_replace('[bts-tos-options]', $this->bts_tos_options([]), $html);
- $html = do_shortcode($html);
- return $html;
- }
-
- public function bts_rate_options($attr){
- $result = "<select> \n";
- $options = get_option('bts_payitem_earnings_rate');
- foreach($options as $o){
- $result.=sprintf("<option value='%s'> $%3.2f-%s</option>",
- $o['EarningsRateID'], $o['RatePerUnit'], $o['Name']);
- }
- $result .="</select>";
- return $result;
- }
-
- public function bts_select_staff($attr){
- $result = "<select> \n";
- $staff = $this->get_people_by_role('staff');
- foreach ($staff as $u){
- $result .= sprintf("<option value=%s> %s</option>", $u->user_login, $u->first_name . " " . $u->last_name);
- }
- $result .="</select>";
- return $result;
- }
-
- public function bts_select_client($attr){
- $result = "<select> \n";
- $staff = $this->get_people_by_role('client');
- foreach ($staff as $u){
- $result .= sprintf("<option value=%s> %s</option>", $u->user_login, $u->first_name . " " . $u->last_name);
- }
- $result .="</select>";
- return $result;
- }
-
- public function bts_type_of_service($attr){
- $n = new NdisPrice(2019);
- return $n->get_html();
- }
-
- public function bts_user_name($attr)
- {
- //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>;';
- $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]';
- echo do_shortcode($content);
- }
-
- //generate template based on html file
- private function template($id, $file)
- {
- $text = '<script id="' . $id .'" type="text/x-biukop-template">';
- $text .= file_get_contents(plugin_dir_path(__FILE__) . "/html/$file");
- $text .= '</script>';
- return $text;
- }
-
-
- function list_staff(){
- check_ajax_referer('acaresydney');
- // Handle the ajax request
- $response = array(
- 'status' =>'error',
- 'users' => [],
- );
- //search all users that are staff
- $staffq = new \WP_User_Query(array('role'=>'staff','meta_key'=>'first_name', 'orderby'=>'meta_value', 'order'=>'ASC'));
- $staff = $staffq->get_results();
- if (! empty($staff)){
- $response['status'] = 'success';
- foreach( $staff as $s){
- $response['users'][] = array(
- 'login' => $s->user_login,
- 'firstname'=> $s->first_name,
- 'lastname'=> $s->last_name,
- 'mobile'=> get_user_meta($s->ID, 'mobile', true),
- 'email'=> $s->user_email,
- 'wages'=> 0,
- 'hour' => 0 ,
- 'OT' => 0 ,
- 'petrol'=> 0 ,
- 'rating'=> 0,
- 'unconfirmedjob'=> 0,
- );
- }
- }
- wp_send_json($response);
- wp_die();
- }
- function list_client(){
- check_ajax_referer('acaresydney');
- // Handle the ajax request
- $response = array(
- 'status' =>'error',
- 'users' => [],
- );
- //search all users that are staff
- $clientq = new \WP_User_Query(array('role'=>'client', 'meta_key'=>'first_name', 'orderby'=>'meta_value', 'order'=>'ASC'));
- $client = $clientq->get_results();
- if (! empty($client)){
- $response['status'] = 'success';
- foreach( $client as $s){
- $response['users'][] = array(
- 'login' => $s->user_login,
- 'firstname'=> $s->first_name,
- 'lastname'=> $s->last_name,
- 'mobile'=> get_user_meta($s->ID, 'mobile', true),
- 'email'=> $s->user_email,
- 'account'=> get_user_meta($s->ID, 'account', true),
- 'address' => get_user_meta($s->ID, 'address', true),
- 'rating'=> 0,
- 'unconfirmedjob'=> 0,
- );
- }
- }
- wp_send_json($response);
- wp_die();
- }
-
-
- private function get_people_by_role($role){
- //search all users that are staff
- $staffq = new \WP_User_Query(array('role'=>$role, 'meta_key'=>'first_name', 'orderby'=>'meta_value', 'order'=>'ASC'));
- $staff = $staffq->get_results();
- return $staff;
- }
-
- //ajax get earnings rates
- function get_payitem_earnings_rate()
- {
- $response= array(
- 'status' => 'success',
- 'options'=> get_option('bts_payitem_earnings_rate'),
- );
- wp_send_json($response);
- }
-
- //ajax job CRUD
- function save_job()
- {
- check_ajax_referer('acaresydney');
- $r = $_POST['record'];
- $response = array();
- $d = array(
- 'tos' => $r['tos'],
- 'start' => $r['start'],
- 'finish' => $r['finish'],
- 'rate' => $r['rate'],
- 'staff' => $r['staff'],
- 'client' => $r['client'],
- 'ack' => $r['ack']=='true'?1:0,
- 'rating'=>$r['rating'],
- );
-
- //this is an update
- if ( isset($r['id']) && trim($r['id']) !='' && is_numeric($r['id'])){
- $response['isNew'] = false; //add or update?
- $result = $this->db->update($this->table_name, $d, array('id' =>$r['id']));
- if ($result !== false && $this->db->last_error == ''){
- $d['id'] = $r['id'];
- $response['status'] = 'success';
- //do data type conversion, string to int
- $response['newdata'] = $this->get_ts_record($r['id']);
- $response['errors'] = array(); //empty array
- }else{
- $response['status'] = 'error';
- $repsonse['errors'] = array(
- 'db' => "network database error" . $this->db->last_error,
- );
- }
- }else{
- $response['isNew'] = true;
- $result = $this->db->insert($this->table_name, $d);
- $lastid = $this->db->insert_id;
- if ($result != false && $this->db->last_error == ''){
- $response['status'] = 'success';
- $response['newdata'] = $this->get_ts_record($lastid);
- }else{
- $response['status'] = 'error';
- $response['errors'] = array(
- 'db' => 'network database error ' . $this->db->last_error,
- );
- }
- }
- wp_send_json($response);
- wp_die();
- }
-
- private function get_ts_record($id){
- $sql = "SELECT * FROM $this->table_name WHERE id=%d";
- $row = $this->db->get_row($this->db->prepare ($sql, array($id)));
- $response = [];
- if ($row != null){
- $response = array(
- 'id' => (int)$row->id,
- 'tos' => $row->tos,
- 'start' => $row->start,
- 'finish' => $row->finish,
- 'rate' => $row->rate,
- 'staff' => $row->staff,
- 'client' => $row->client,
- 'ack' => (int)$row->ack,
- 'rating' =>(int) $row->rating,
- );
- }
- return $response;
- }
-
-
- //ajax delete job
- function delete_job(){
- check_ajax_referer('acaresydney');
- $id = $_POST['jobid'];
- $result = $this->db->delete($this->table_name, array('id'=> $id));
- $response=array(
- 'status' => 'success',
- 'id' => $id,
- 'action'=> 'delete',
- 'error' => '',
- );
- if ($result == 1){
- wp_send_json($response);
- }else{
- $response['status'] = 'error';
- $response['error'] = $this->db->last_error;
- wp_send_json($response);
- }
- wp_die();
- }
-
- //ajax browse job with different filters
- function list_job(){
- check_ajax_referer('acaresydney');
- $start = $_POST['start'];
- $finish = $_POST['finish'];
- $response = array(
- 'status'=>'success',
- 'jobs' => [],
- );
-
- $sql = "SELECT * FROM $this->table_name WHERE start>='%s' and start <='%s' order by start ASC ,staff ASC";
- $jobs = $this->db->get_results($this->db->prepare ($sql, array($start, $finish)));
-
- if (! empty($jobs)){
- $response['status'] = 'success';
- foreach( $jobs as $s){
- $response['jobs'][] = array(
- 'id' => $s->id,
- 'tos' => $s->tos,
- 'start'=> $s->start,
- 'finish'=> $s->finish,
- 'rate'=> $s->rate,
- 'staff'=> $s->staff,
- 'client'=> $s->client,
- 'ack' => $s->ack,
- 'rating' =>$s->rating,
- );
- }
- }
- wp_send_json($response);
- wp_die();
- }
- }
-
- $bb = new AcareOffice();
-
- if ( defined( 'WP_CLI' ) && WP_CLI ) {
- \WP_CLI::add_command( 'sync_users', array($bb, 'sync_user_cli'));
- }
-
|