From 4df5b41b8088a026b80a62324ad1329e208688c0 Mon Sep 17 00:00:00 2001 From: Pererinha Date: Thu, 2 Apr 2015 08:49:31 -0300 Subject: [PATCH 1/2] partial #5212 --- .../controllers/default/cockpit2/api/test/sandbox.php | 6 +++--- include/library/Crunchbutton/Settlement.php | 11 +++++++---- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/include/controllers/default/cockpit2/api/test/sandbox.php b/include/controllers/default/cockpit2/api/test/sandbox.php index ff4304108..8b5dc6efd 100644 --- a/include/controllers/default/cockpit2/api/test/sandbox.php +++ b/include/controllers/default/cockpit2/api/test/sandbox.php @@ -3,8 +3,6 @@ class Controller_Api_Test_Sandbox extends Cana_Controller { public function init(){ - die('not today!'); - $out = [ 'ok' => [], 'no' => [] ]; $admins = Admin::q( 'SELECT * FROM admin WHERE active = 0 OR active IS NULL ORDER BY name ASC' ); @@ -15,10 +13,12 @@ class Controller_Api_Test_Sandbox extends Cana_Controller { if( $change_set->id_admin_change_set ){ $date = new DateTime( $change_set->date, new DateTimeZone( c::config()->timezone ) ); $admin->date_terminated = $date->format( 'Y-m-d' ); - // $admin->save(); + $admin->save(); $out[ 'ok' ][ $admin->id_admin ] = $admin->name; } else { + $admin->date_terminated = '2015-01-01'; $out[ 'no' ][ $admin->id_admin ] = $admin->name; + $admin->save(); } } diff --git a/include/library/Crunchbutton/Settlement.php b/include/library/Crunchbutton/Settlement.php index da2375cc4..860cdbe6d 100644 --- a/include/library/Crunchbutton/Settlement.php +++ b/include/library/Crunchbutton/Settlement.php @@ -168,11 +168,14 @@ class Crunchbutton_Settlement extends Cana_Model { public function workedShiftsByPeriod( $id_admin, $filters ){ $admin = Admin::o( $id_admin ); - if( $admin->date_terminated ){ - $where = 'AND DATE_FORMAT( cs.date_start, "%Y-%m-%d" ) <= "' . $admin->date_terminated . '"'; - } else { - $where = ''; + if( !$admin->active ){ + if( $admin->date_terminated ){ + $where = 'AND DATE_FORMAT( cs.date_start, "%Y-%m-%d" ) <= "' . $admin->date_terminated . '"'; + } else { + $where = 'AND 1 = 0'; + } } + $start = ( new DateTime( $filters[ 'start' ] ) )->format( 'Y-m-d' ); $end = ( new DateTime( $filters[ 'end' ] ) )->format( 'Y-m-d' ); $query = 'SELECT cs.*, asa.id_admin_shift_assign FROM community_shift cs From 104ffe0d165e6f703c2aaebed27deb01fead706b Mon Sep 17 00:00:00 2001 From: Pererinha Date: Thu, 2 Apr 2015 11:47:58 -0300 Subject: [PATCH 2/2] partial #5006 --- .../Crunchbutton/Cron/Job/CSTicketsDigest.php | 14 +-- .../library/Crunchbutton/Email/CSDigest.php | 2 +- include/library/Crunchbutton/Support.php | 54 +++++++++++- .../views/mail/crunchbutton/cs/digest.phtml | 85 ++++++++++++++----- 4 files changed, 122 insertions(+), 33 deletions(-) diff --git a/include/library/Crunchbutton/Cron/Job/CSTicketsDigest.php b/include/library/Crunchbutton/Cron/Job/CSTicketsDigest.php index 310d864ca..53823e689 100644 --- a/include/library/Crunchbutton/Cron/Job/CSTicketsDigest.php +++ b/include/library/Crunchbutton/Cron/Job/CSTicketsDigest.php @@ -2,17 +2,11 @@ class Crunchbutton_Cron_Job_CSTicketsDigest extends Crunchbutton_Cron_Log { public function run($params){ - $supports = Crunchbutton_Support::q('SELECT - support.* - FROM support - WHERE - support.type != "WARNING" - AND support.datetime > date_sub(now(), interval 1 day) - ORDER BY support.id_support ASC - limit 250' ); - $params = array( 'to' => 'digests@_DOMAIN_', 'messages'=> $supports ); - $email = new Crunchbutton_Email_CSDigest($params); + $tickets = Crunchbutton_Support::dailyDigest( 1 ); + $params = array( 'to' => 'digests@_DOMAIN_', 'tickets' => $tickets ); + $email = new Crunchbutton_Email_CSDigest( $params ); $email->send(); + // echo $email->message(); // it always must call finished method at the end $this->finished(); } diff --git a/include/library/Crunchbutton/Email/CSDigest.php b/include/library/Crunchbutton/Email/CSDigest.php index 76f3d7cae..87b761a42 100644 --- a/include/library/Crunchbutton/Email/CSDigest.php +++ b/include/library/Crunchbutton/Email/CSDigest.php @@ -11,7 +11,7 @@ class Crunchbutton_Email_CSDigest extends Email{ $this->buildView($params); - $params['messageHtml'] = $this->view()->render('cs/digest',['display' => true, 'set' => ['messages' => $params['messages'], ]]); + $params['messageHtml'] = $this->view()->render('cs/digest',['display' => true, 'set' => ['tickets' => $params['tickets'], ]]); parent::__construct($params); } diff --git a/include/library/Crunchbutton/Support.php b/include/library/Crunchbutton/Support.php index 523f24687..3d5b4701b 100644 --- a/include/library/Crunchbutton/Support.php +++ b/include/library/Crunchbutton/Support.php @@ -806,7 +806,59 @@ class Crunchbutton_Support extends Cana_Table_Trackchange { } public function comments(){ - return Crunchbutton_Support_Message::q( 'SELECT * FROM support_message sm WHERE sm.id_support = "' . $this->id_support . '" AND sm.type = "' . Crunchbutton_Support_Message::TYPE_NOTE . '" AND `from` != "' . Crunchbutton_Support_Message::TYPE_FROM_SYSTEM . '" ORDER BY sm.id_support_message DESC' ); + return Crunchbutton_Support_Message::q( 'SELECT * FROM support_message sm WHERE sm.id_support = "' . $this->id_support . '" AND sm.type = "' . Crunchbutton_Support_Message::TYPE_NOTE . '" AND `from` != "' . Crunchbutton_Support_Message::TYPE_FROM_SYSTEM . '" ORDER BY sm.date DESC' ); } + public function dailyDigest( $days = 1 ){ + + $query = 'SELECT DISTINCT( sm.id_support ) AS id, s.* FROM support_message sm + INNER JOIN support s ON s.id_support = sm.id_support + WHERE sm.date > DATE_SUB( NOW(), interval ' . $days . ' day ) AND s.type != "' . Crunchbutton_Support::TYPE_WARNING . '" + ORDER BY sm.date ASC'; + + $tickets = Crunchbutton_Support::q( $query ); + $out = [ 'open' => [], 'closed' => [] ]; + foreach( $tickets as $ticket ){ + $data = []; + $data[ 'id_support' ] = $ticket->id_support; + $data[ 'phone' ] = Crunchbutton_Util::format_phone( $ticket->firstMessage()->phone ); + $data[ 'name' ] = $ticket->firstMessage()->name; + $messages = Crunchbutton_Support_Message::q( 'SELECT * FROM support_message sm WHERE id_support = "' . $ticket->id_support . '" AND sm.date > DATE_SUB( NOW(), interval ' . $days . ' day )' ); + $count = 0; + $prev_type = null; + $prev_from = null; + $prev_body = null; + foreach( $messages as $message ){ + if( $message->from == Crunchbutton_Support_Message::TYPE_FROM_SYSTEM || + $message->body == '(Ticket created at cockpit)' ){ + continue; + } + if( !$data[ 'date' ] ){ + $data[ 'date' ] = $message->date()->format( 'M jS g:i a' ); + } + if( $prev_from == $message->from && $prev_type == $message->type ){ + $join = ( ctype_upper( substr( $message->body, 0 ) ) ) ? '' : '
'; + $data[ 'messages' ][ $count ][ 'body' ] .= $join . $message->body; + } else { + $count++; + $data[ 'messages' ][ $count ] = [ 'type' => $message->from , 'body' => $message->body ]; + } + $prev_type = $message->type; + $prev_from = $message->from; + $prev_body = $message->body; + } + if( count( $data[ 'messages' ] ) ){ + if( $ticket->status == Crunchbutton_Support::STATUS_OPEN ){ + $data[ 'status' ] = 'Opened'; + $out[ 'open' ][] = $data; + } else { + $data[ 'status' ] = 'Closed'; + $out[ 'closed' ][] = $data; + } + } + } + return $out; + } + + } diff --git a/include/views/mail/crunchbutton/cs/digest.phtml b/include/views/mail/crunchbutton/cs/digest.phtml index 699777770..22560efe5 100644 --- a/include/views/mail/crunchbutton/cs/digest.phtml +++ b/include/views/mail/crunchbutton/cs/digest.phtml @@ -1,23 +1,66 @@ - - -

Customer Service Morning Digest

- - - - - - - - - - - - - - - - - -
NameTimeMessagePhone
firstMessage()->name ?>firstMessage()->date), 'g:i A')?>firstMessage()->body ?>firstMessage()->phone ?>
+ +

+ + Opened: + +
+ + Closed: + +
+ Total tickets: +

+ + $client_row_style = "font-size: 12px; font-family: 'Helvetica', 'Arial' !important; padding: 5px 3px 5px 5px; border: 0;background: #e6e6e6; bgcolor=#e6e6e6"; + $crunchbutton_row_style = "font-size: 12px; font-family: 'Helvetica', 'Arial' !important; padding: 5px 3px 5px 5px; border: 0;background: #e6e6e6; bgcolor=#F5F5F5"; + $thead_th_row = "font-family: 'Helvetica', 'Arial' !important; padding-top: 25px; text-align: left; font-size: 13px; padding-right: 3px;"; + $body_td_row = "font-size: 12px; font-family: 'Helvetica', 'Arial' !important; padding: 5px 3px 5px 5px; border: 0;"; +?> + $tickets ) { ?> + +

Opened tickets

+ +

Closed tickets

+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + Client + + Crunchbutton + + + +
+