Przeglądaj źródła

job class created and data is being attached

master
patrick 6 lat temu
rodzic
commit
c996eeae9f
4 zmienionych plików z 275 dodań i 14 usunięć
  1. +2
    -2
      css/bts_timesheet.css
  2. +3
    -2
      html/job.html
  3. +109
    -4
      js/bts_timesheet.js
  4. +161
    -6
      ts.php

+ 2
- 2
css/bts_timesheet.css Wyświetl plik

@@ -648,7 +648,7 @@ div.sheettableheader {
div.btos,
div.bstart,
div.bfinish,
div.bhours,
div.brate,
div.bstaff,
div.bclient,
div.bconfirmed,
@@ -661,7 +661,7 @@ div.btos {
width: 20%;
}

div.bhours{
div.brate{
width: 20%;
max-width: 20px;
}

+ 3
- 2
html/job.html Wyświetl plik

@@ -1,6 +1,7 @@
<div class="divTable blueTable">
<div class="divTableBody">
<div class="divTableRow">
<input type='hidden' name='id' value=''>
<div class="divTableCell btos">
<select>
<option value="personalcare">Personal Care (stanard)</option>
@@ -15,10 +16,10 @@
</div>
<div class="divTableCell bstart"><input class="datepicker" placeholder="2019:03-02-11:30am"></input></div>
<div class="divTableCell bfinish"><input class="datepicker" placeholder="2019:03-02-11:30am"></input></div>
<div class="divTableCell bhours">[bts_hours_options]</div>
<div class="divTableCell brate">[bts_rate_options]</div>
<div class="divTableCell bstaff">[bts_select_staff]</div>
<div class="divTableCell bclient">[bts_select_client]</div>
<div class="divTableCell bconfirmed"><input type=checkbox checked></div>
<div class="divTableCell bconfirmed"><input type=checkbox checked> <span class="ticon ticon-save"></span></div>
<div class="divTableCell bdelete">
<span class="ticon ticon-trash"></span>
</div>

+ 109
- 4
js/bts_timesheet.js Wyświetl plik

@@ -217,7 +217,8 @@
_ajax_nonce: bts().nonce, // nonce
action: "earnings_rate", // action
}, function(response, status, xhr){
console.log("%o", response);
bts().earnings_rate = response;
console.log("%o", bts().earnings_rate);
});
}
@@ -225,8 +226,10 @@
$(document).on('click', 'div.divTableHead.bdelete', function(){
for (var i=1; i<10; i++){
var html = jQuery("#job_item").html();
jQuery('div.workspace').append(html);
// var html = jQuery("#job_item").html();
// var o = jQuery('div.workspace').append(html);
var o = new Job({i:i});
o.debug();
}
reset_date_time_picker();
});
@@ -237,7 +240,8 @@
}
$(document).on('click', 'div.divTableCell.bdelete', function(){
$(this).closest('div.divTable').remove();
if (confirm('delete this job?'))
$(this).closest('div.divTable').remove();
});
$(document).on('mouseenter', 'div.divTableCell', function(){
@@ -247,7 +251,108 @@
$(this).closest('div.divTable').removeClass('highlight');
});
$(document).on('click', 'span.ticon.ticon-save', function(){
var table = $(this).closest('div.divTable')
console.log('clicked table data = %o', $(table).data());
var r = get_record_from_ui(table);
do_save_record(r);
});
function do_save_record(r){
$.post(bts().ajax_url, { // POST request
_ajax_nonce: bts().nonce, // nonce
action: "save_job", // action
record: r,
}, function(response, status, xhr){
console.log("response for save %o", response);
});
}
function get_record_from_ui(table){
var record = {};
record.id = get_job_id(table);
record.tos = get_tos(table);
record.start = get_start(table);
record.finish = get_finish(table);
record.rate = get_rate(table);
record.staff = get_staff(table);
record.client = get_client(table);
record.ack = get_ack(table);
return record;
}
class Job{
constructor(data){
var html = jQuery("#job_item").html();
this.el = $(html);
this.data = data;
this.el.data({obj:this, data:data});
jQuery('div.workspace').append(this.el);
}
debug(){
console.log("data %o", this.el.data());
}
load_data(data)
{
$(selector).data(data);//save data to object
//update gui;
set_job_id(data.id);
}
get_job_id(){
return $(this.selector).find('input[name="id"]').attr('value');
}
set_job_id(val){
return $(this.selector).find('input[name="id"]').attr('value', val);
}
get_tos()
{
return $(this.selector).find('div.btos select').children("option:selected").val();
}
set_tos(val)
{
return $(this.selector).find('div.btos select').children("option:selected").val();
}
get_start(){
}
}
function get_job_id(table)
{
$(table).find('input[name="id"]').attr('value');
}
function get_tos(table)
{
return $(table).find('div.btos select').children("option:selected").val();
}
function get_start(table){
return $(table).find('div.bstart input').attr('value');
}
function get_finish(table)
{
return $(table).find('div.bfinish input').attr('value');
}
function get_rate(table)
{
return $(table).find('div.brate select').children("option:selected").val();
}
function get_staff(table)
{
return $(table).find('div.bstaff select').children("option:selected").val();
}
function get_client(table)
{
return $(table).find('div.bclient select').children("option:selected").val();
}
function get_ack(table)
{
return $(table).find('div.bconfirmed input:checked').length > 0;
}
/*________________________________________________________________________*/
});
})(jQuery);

+ 161
- 6
ts.php Wyświetl plik

@@ -18,10 +18,14 @@ class AcareOffice{
private $nonce; //for ajax verification
private $pages = array('time-sheets', 'user-list');
private $acaresydney_userid = 0;
private $xero ;
private $xero ;
private $db;
private $table_name;
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);
@@ -34,17 +38,24 @@ class AcareOffice{
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_hours_options', array($this, 'bts_hours_options'));
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_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' ));
global $wpdb;
$this->db = $wpdb;
$this->table_name = $wpdb->prefix . 'acare_ts';
}
/**
@@ -66,6 +77,51 @@ class AcareOffice{
$this->xero->init_wp();
}
//init database
public function db_install () {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
//table name: broker transactions
$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 NULL DEFAULT 0,
PRIMARY KEY (id)
) $charset_collate;";
// $sql = "CREATE TABLE $table_name (
// id int(64) NOT NULL AUTO_INCREMENT,
// paid tinyint(4) DEFAULT NULL,
// referal varchar(45) COLLATE utf8mb4_unicode_520_ci DEFAULT NULL,
// clientid int(64) NOT NULL,
// loanamount float DEFAULT NULL,
// settledate date DEFAULT NULL,
// loanterm mediumint(10) DEFAULT NULL,
// commissionrate float DEFAULT NULL,
// commission float DEFAULT NULL,
// trailrate float DEFAULT NULL,
// trail float DEFAULT NULL,
// paydate date DEFAULT NULL,
// brokerid int(64) NOT NULL,
// status tinyint(4) NOT NULL,
// notes varchar(255) COLLATE utf8mb4_unicode_520_ci DEFAULT NULL,
// PRIMARY KEY (id)
// ) $charset_collate;";
//create database
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}

//
//
@@ -140,7 +196,7 @@ class AcareOffice{
return $html;
}
public function bts_hours_options($attr){
public function bts_rate_options($attr){
$result = "<select> \n";
$options = get_option('bts_payitem_earnings_rate');
foreach($options as $o){
@@ -242,7 +298,6 @@ class AcareOffice{
wp_die();
}

private function get_people_by_role($role){
//search all users that are staff
@@ -261,7 +316,107 @@ class AcareOffice{
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,
);
//this is an update
if ( isset($r['id']) && trim($r['id']) !='' && is_numeric($r['id'])){
$response['isNew'] = false; //add or update?
$result = $this->update($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['recordid'];
$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');
$r = $_POST['record'];
$response = array();
wp_send_json($response);
wp_die();
}
}

$bb = new AcareOffice();

Ładowanie…
Anuluj
Zapisz