PageRenderTime 29ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/themes/chef2go/weekly-mail.php

https://gitlab.com/pankajmohale/chef2go
PHP | 283 lines | 191 code | 52 blank | 40 comment | 21 complexity | 6178dcf41d0a97df590737eb6753a18e MD5 | raw file
  1. <?php
  2. /**
  3. * Tell WordPress we are doing the CRON task.
  4. *
  5. * @var bool
  6. */
  7. define('DOING_CRON', true);
  8. if ( !defined('ABSPATH') ) {
  9. /** Set up WordPress environment */
  10. require_once( '/home/rgbastud/public_html/chefs2go/wp-load.php' );
  11. }
  12. require('/home/rgbastud/public_html/chefs2go/wp-content/themes/chef2go/mpdf60/mpdf.php');
  13. $timesheet_value = weekly_mail_timesheet_function();
  14. function weekly_mail_timesheet_function(){
  15. // $monday = date('Y-m-d',strtotime('-1 Monday'));
  16. // $sunday = date('Y-m-d',strtotime('-0 Sunday'));
  17. // $tovalue = date('d-m-Y');
  18. $fromvalue = date('Y-m-d',strtotime('-2 Monday'));
  19. $tovalue = date('Y-m-d',strtotime('last sunday'));
  20. // $fromvalue = '28-8-2017';
  21. // $tovalue = '1-9-2017';
  22. $newfromvalue = date("Ymd", strtotime($fromvalue));
  23. $newtovalue = date("Ymd", strtotime($tovalue));
  24. $timesheet_args = array(
  25. 'post_type' => 'employee_timesheet',
  26. 'posts_per_page' => -1,
  27. );
  28. // the query
  29. $timesheet_query = new WP_Query( $timesheet_args );
  30. if ( $timesheet_query->have_posts() ) :
  31. $emp_value = '<div id="allemployeelisting">';
  32. $emp_value = '<div style="text-align: center;margin-bottom:20px;"><img src="/home/rgbastud/public_html/chefs2go/logo.png" width="250" style="vertical-align: middle; text-align: center;" /></div>';
  33. $emp_value .= '<div class="timesheet-content">';
  34. while ( $timesheet_query->have_posts() ) : $timesheet_query->the_post();
  35. for ($timesheet_meta = 1; $timesheet_meta < 11; $timesheet_meta++) {
  36. $datekey = 'timesheet_date_'.$timesheet_meta;
  37. $timesheet_date = get_post_meta( get_the_ID(), $datekey, true );
  38. if ($timesheet_date >= $newfromvalue && $timesheet_date <= $newtovalue ) {
  39. $author_id = get_the_author_meta('ID');
  40. $author_id_array[] = $author_id;
  41. $author_unique_id_array = array_unique($author_id_array);
  42. }
  43. }
  44. endwhile;
  45. $emp_value .= '<table border="1" cellpadding="10" style="width: 100%;">';
  46. $emp_value .= '<tbody align="center">';
  47. if ($author_unique_id_array) {
  48. foreach ($author_unique_id_array as $key => $author_id) {
  49. $totaltime = '';
  50. $user_info = get_userdata($author_id);
  51. $emp_value .= '<tr>';
  52. $emp_value .= '<th colspan="5" style="text-align: center;font-size: 18px;padding: 10px 0;">';
  53. $emp_value .= $user_info->nickname.' ( '.$user_info->user_login.' )';
  54. $emp_value .= '</th>';
  55. $emp_value .= '</tr>';
  56. $emp_value .= '<tr>';
  57. $emp_value .= '<th style="text-align: center;">Location</th>';
  58. $emp_value .= '<th style="text-align: center;">date</th>';
  59. $emp_value .= '<th style="text-align: center;">Start</th>';
  60. $emp_value .= '<th style="text-align: center;">Finish</th>';
  61. $emp_value .= '<th style="text-align: center;">Daily Total HRS Worked</th>';
  62. $emp_value .= '</tr>';
  63. while ( $timesheet_query->have_posts() ) : $timesheet_query->the_post();
  64. $authorid = get_the_author_meta('ID');
  65. $post_id = get_the_ID();
  66. $clinedet = get_post_meta( get_the_ID(), false );
  67. for ($timesheet_meta = 1; $timesheet_meta < 11; $timesheet_meta++) {
  68. $datekey = 'timesheet_date_'.$timesheet_meta;
  69. $timesheet_date = get_post_meta( get_the_ID(), $datekey, true );
  70. if ($timesheet_date >= $newfromvalue && $timesheet_date <= $newtovalue ) {
  71. if ($authorid == $author_id) {
  72. $clientkey = 'clients_'.$timesheet_meta;
  73. $clientval = get_post_meta( get_the_ID(), $clientkey, true );
  74. $startkey = 'start_time_'.$timesheet_meta;
  75. $endkey = 'end_time_'.$timesheet_meta;
  76. $value = weekly_mail_timesheet($clientkey, $datekey, $startkey, $endkey );
  77. $emp_value .= $value;
  78. $totaltime += difference_calculator_function($startkey, $endkey );
  79. $difference_total = difference_total_function($totaltime);
  80. }
  81. }
  82. }
  83. endwhile;
  84. $emp_value .= $difference_total;
  85. }
  86. }
  87. $emp_value .= '</tbody>';
  88. $emp_value .= '</table>';
  89. $emp_value .= '</div>';
  90. /*$totalhourswork = $totaltime/60;
  91. $totaltime = explode(".",$totalhourswork);
  92. $minutes = $totaltime[1]*0.6;
  93. $numlength = strlen((string)$minutes);
  94. if ($numlength <= 1) {
  95. $min = $minutes.'0';
  96. } else {
  97. $min = $minutes;
  98. }
  99. $emp_value .= '<div class="timesheet-footer">';
  100. $emp_value .= '<div style="text-align: right; font-size: 18px; margin-bottom: 20px;">';
  101. $emp_value .= 'Total:';
  102. $emp_value .= $totaltime[0] . ' Hours ' . $min . ' Minutes' ;
  103. $emp_value .= '</div>';
  104. $emp_value .= '</div>';*/
  105. $emp_value .= '</div>';
  106. wp_reset_postdata();
  107. else :
  108. $emp_value .= '<h2>';
  109. $emp_value .= _e( 'Sorry, no timesheet matched your criteria.' );
  110. $emp_value .= '</h2>';
  111. endif;
  112. return $emp_value;
  113. }
  114. function weekly_mail_timesheet($clientkey, $timesheet_date_key, $start_time_key, $end_time_key){
  115. $client = get_post_meta( get_the_ID(), $clientkey, true );
  116. $date = get_post_meta( get_the_ID(), $timesheet_date_key, true );
  117. $start_time = get_post_meta( get_the_ID(), $start_time_key, true );
  118. $end_time = get_post_meta( get_the_ID(), $end_time_key, true );
  119. $starttime = get_post_meta( get_the_ID(), $start_time_key, true );
  120. $endtime = get_post_meta( get_the_ID(), $end_time_key, true );
  121. $start = array_filter(explode(' ',str_replace(':',' ',$starttime)));
  122. $end = array_filter(explode(' ',str_replace(':',' ',$endtime)));
  123. $newDate = date("m/d/Y", strtotime($date));
  124. // convert date and time arrays into datetime formats
  125. $startdate = date_create_from_format('m/d/Y@h:i a', $newDate . "@". $start[0].":".$start[1]." ".$start[2]);
  126. $enddate = date_create_from_format('m/d/Y@h:i a', $newDate . "@". $end[0].":".$end[1]." ".$end[2]);
  127. //convert datetimes into seconds to compare
  128. $starttime = strtotime($startdate->format('Y-m-d H:i:s'));
  129. $endtime = strtotime($enddate->format('Y-m-d H:i:s'));
  130. // check to see if the times span overnight
  131. if($starttime > $endtime)
  132. $endtime = strtotime($enddate->format('Y-m-d H:i:s') . " +1 day");
  133. // perform calculation
  134. $diff = floor(($endtime - $starttime)/60);
  135. $totaldiff = $diff/60;
  136. $totaldiff = explode(".",$totaldiff);
  137. $totaltime += $diff;
  138. if ($diff >= 60 ) {
  139. $time = $diff/60 . ' Hours';
  140. } else {
  141. $time = $diff . ' Minutes';
  142. }
  143. $weekly_mail_timesheet = '<tr>';
  144. $weekly_mail_timesheet .= '<td>';
  145. $term = get_term_by('term_id', $client, 'client_location');
  146. $weekly_mail_timesheet .= $term->name;
  147. $weekly_mail_timesheet .= '</td>';
  148. $weekly_mail_timesheet .= '<td>';
  149. $weekly_mail_timesheet .= date("d-m-Y", strtotime($date));
  150. $weekly_mail_timesheet .= '</td>';
  151. $weekly_mail_timesheet .= '<td>';
  152. $weekly_mail_timesheet .= $start_time;
  153. $weekly_mail_timesheet .= '</td>';
  154. $weekly_mail_timesheet .='<td>'. $end_time .'</td>';
  155. $weekly_mail_timesheet .='<td>'. $totaldiff[0] . ' Hours ' . $diff%60 . ' Minutes</td>';
  156. $weekly_mail_timesheet .='</tr>';
  157. return $weekly_mail_timesheet;
  158. }
  159. function difference_calculator_function($start_time_key, $end_time_key){
  160. $starttime = get_post_meta( get_the_ID(), $start_time_key, true );
  161. $endtime = get_post_meta( get_the_ID(), $end_time_key, true );
  162. $start = array_filter(explode(' ',str_replace(':',' ',$starttime)));
  163. $end = array_filter(explode(' ',str_replace(':',' ',$endtime)));
  164. $newDate = date("m/d/Y", strtotime($date));
  165. // convert date and time arrays into datetime formats
  166. $startdate = date_create_from_format('m/d/Y@h:i a', $newDate . "@". $start[0].":".$start[1]." ".$start[2]);
  167. $enddate = date_create_from_format('m/d/Y@h:i a', $newDate . "@". $end[0].":".$end[1]." ".$end[2]);
  168. //convert datetimes into seconds to compare
  169. $starttime = strtotime($startdate->format('Y-m-d H:i:s'));
  170. $endtime = strtotime($enddate->format('Y-m-d H:i:s'));
  171. // check to see if the times span overnight
  172. if($starttime > $endtime)
  173. $endtime = strtotime($enddate->format('Y-m-d H:i:s') . " +1 day");
  174. // perform calculation
  175. $diff = floor(($endtime - $starttime)/60);
  176. return $diff;
  177. }
  178. function difference_total_function($difference_total){
  179. $totalhourswork = $difference_total/60;
  180. $totaltime = explode(".",$totalhourswork);
  181. $minutes = $totaltime[1]*0.6;
  182. $numlength = strlen((string)$minutes);
  183. if ($numlength <= 1) {
  184. $min = $minutes.'0';
  185. } else {
  186. $min = $minutes;
  187. }
  188. $difference_calculator_timesheet .= '<tr class="timesheet-footer">';
  189. $difference_calculator_timesheet .= '<td style="text-align: right; font-size: 18px; margin-bottom: 20px;" colspan="5">';
  190. $difference_calculator_timesheet .= 'Total:';
  191. $difference_calculator_timesheet .= $totaltime[0] . ' Hours ' . $min . ' Minutes' ;
  192. $difference_calculator_timesheet .= '</td>';
  193. $difference_calculator_timesheet .= '</tr>';
  194. return $difference_calculator_timesheet;
  195. }
  196. ob_start();
  197. $mpdf = new mPDF('F');
  198. $mpdf->WriteHTML($timesheet_value);
  199. $content = $mpdf->Output('/home/rgbastud/public_html/chefs2go/pdf/lmn.pdf','F');
  200. $content = $mpdf->Output('', 'S');
  201. $content = chunk_split(base64_encode($content));
  202. $to = 'test.bizcatalyst@gmail.com, ' . $Email;
  203. $subject = 'Employee Timesheet Details';
  204. $repEmail = 'info@chefs2go.com';
  205. $fileName = 'weekly-timesheet.pdf';
  206. $eol = PHP_EOL;
  207. $separator = md5(time());
  208. $headers = 'From: chefs2go.com <'.$repEmail.'>'.$eol;
  209. $headers .= 'MIME-Version: 1.0' .$eol;
  210. $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
  211. $message = "--".$separator.$eol;
  212. $message .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
  213. $message .= "Please check attached pdf for all employee details.".$eol;
  214. $message .= "--".$separator.$eol;
  215. $message .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
  216. $message .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
  217. $message .= "--".$separator.$eol;
  218. $message .= "Content-Type: application/pdf; name=\"".$fileName."\"".$eol;
  219. $message .= "Content-Transfer-Encoding: base64".$eol;
  220. $message .= "Content-Disposition: attachment".$eol.$eol;
  221. $message .= $content.$eol;
  222. $message .= "--".$separator."--";
  223. mail($to, $subject, $message, $headers);
  224. /*if (mail($to, $subject, $message, $headers)){
  225. echo "Email sent to pankaj.mohale@bizcatalyst.co.in";
  226. } else {
  227. echo "Email failed";
  228. }*/
  229. ob_end_flush();
  230. exit;
  231. ?>