// +----------------------------------------------------------------------+
 
 import('ttUserHelper');
+import('ttFileHelper');
 
 // Class ttTimesheetHelper is used to help with project related tasks.
 class ttTimesheetHelper {
     $group_id = $user->getGroup();
     $org_id = $user->org_id;
 
+    $includeFiles = $user->isPluginEnabled('at');
+    if ($includeFiles) {
+      $filePart = ', if(Sub1.entity_id is null, 0, 1) as has_files';
+      $fileJoin =  " left join (select distinct entity_id from tt_files".
+      " where entity_type = 'timesheet' and group_id = $group_id and org_id = $org_id and status = 1) Sub1".
+      " on (ts.id = Sub1.entity_id)";
+    }
+
     $result = array();
     $sql = "select ts.id, ts.name, ts.client_id, c.name as client_name,".
-      " ts.submit_status, ts.approve_status from tt_timesheets ts".
-      " left join tt_clients c on (c.id = ts.client_id)".
+      " ts.submit_status, ts.approve_status $filePart from tt_timesheets ts".
+      " left join tt_clients c on (c.id = ts.client_id) $fileJoin".
       " where ts.status = 1 and ts.group_id = $group_id and ts.org_id = $org_id and ts.user_id = $user_id".
       " order by ts.name";
     $res = $mdb2->query($sql);
     $group_id = $user->getGroup();
     $org_id = $user->org_id;
 
+    $includeFiles = $user->isPluginEnabled('at');
+    if ($includeFiles) {
+      $filePart = ', if(Sub1.entity_id is null, 0, 1) as has_files';
+      $fileJoin =  " left join (select distinct entity_id from tt_files".
+      " where entity_type = 'timesheet' and group_id = $group_id and org_id = $org_id and status = 1) Sub1".
+      " on (ts.id = Sub1.entity_id)";
+    }
+
     $result = array();
     $sql = "select ts.id, ts.name, ts.client_id, c.name as client_name,".
-      " ts.submit_status, ts.approve_status from tt_timesheets ts".
-      " left join tt_clients c on (c.id = ts.client_id)".
+      " ts.submit_status, ts.approve_status $filePart from tt_timesheets ts".
+      " left join tt_clients c on (c.id = ts.client_id) $fileJoin".
       " where ts.status = 0 and ts.group_id = $group_id and ts.org_id = $org_id and ts.user_id = $user_id".
       " order by ts.name";
     $res = $mdb2->query($sql);
     global $user;
     $mdb2 = getConnection();
 
+    // Delete associated files.
+    if ($user->isPluginEnabled('at')) {
+      import('ttFileHelper');
+      global $err;
+      $fileHelper = new ttFileHelper($err);
+      if (!$fileHelper->deleteEntityFiles($timesheet_id, 'timesheet'))
+        return false;
+    }
+
     $user_id = $user->getUser();
     $group_id = $user->getGroup();
     $org_id = $user->org_id;
 
 // 'title.send_report' => 'Sending Report',
 // 'title.timesheets' => 'Timesheets',
 // 'title.timesheet' => 'Timesheet',
+// 'title.timesheet_files' => 'Timesheet Files',
 'title.invoice' => 'Factura',
 // TODO: translate the following.
 // 'title.send_invoice' => 'Sending Invoice',
 
 // 'title.send_report' => 'Sending Report',
 // 'title.timesheets' => 'Timesheets',
 // 'title.timesheet' => 'Timesheet',
+// 'title.timesheet_files' => 'Timesheet Files',
 'title.invoice' => 'Faktura',
 // TODO: translate the following.
 // 'title.send_invoice' => 'Sending Invoice',
 
 // TODO: Translate the following.
 // 'title.timesheets' => 'Timesheets',
 // 'title.timesheet' => 'Timesheet',
+// 'title.timesheet_files' => 'Timesheet Files',
 'title.invoice' => 'Faktura',
 'title.send_invoice' => 'Sender Faktura',
 'title.charts' => 'Diagrammer',
 
 // TODO: Translate the following.
 // 'title.timesheets' => 'Timesheets',
 // 'title.timesheet' => 'Timesheet',
+// 'title.timesheet_files' => 'Timesheet Files',
 'title.invoice' => 'Rechnung',
 'title.send_invoice' => 'Rechnung senden',
 'title.charts' => 'Diagramme',
 
 'title.report' => 'Report',
 'title.send_report' => 'Sending Report',
 'title.timesheets' => 'Timesheets',
-'title.timesheet' => 'Timesheet',  
+'title.timesheet' => 'Timesheet',
+'title.timesheet_files' => 'Timesheet Files',
 'title.invoice' => 'Invoice',
 'title.send_invoice' => 'Sending Invoice',
 'title.charts' => 'Charts',
 
 // 'title.send_report' => 'Sending Report',
 // 'title.timesheets' => 'Timesheets',
 // 'title.timesheet' => 'Timesheet',
+// 'title.timesheet_files' => 'Timesheet Files',
 'title.invoice' => 'Factura',
 // TODO: translate the following.
 // 'title.send_invoice' => 'Sending Invoice',
 
 // TODO: Translate the following.
 // 'title.timesheets' => 'Timesheets',
 // 'title.timesheet' => 'Timesheet',
+// 'title.timesheet_files' => 'Timesheet Files',
 'title.invoice' => 'Arve',
 'title.send_invoice' => 'Saada arve',
 'title.charts' => 'Diagrammid',
 
 // TODO: Translate the following.
 // 'title.timesheets' => 'Timesheets',
 // 'title.timesheet' => 'Timesheet',
+// 'title.timesheet_files' => 'Timesheet Files',
 'title.invoice' => 'فاکتور',
 'title.send_invoice' => 'ارسال فاکتور',
 'title.charts' => 'نمودارها',
 
 // TODO: Translate the following.
 // 'title.timesheets' => 'Timesheets',
 // 'title.timesheet' => 'Timesheet',
+// 'title.timesheet_files' => 'Timesheet Files',
 'title.invoice' => 'Lasku',
 'title.send_invoice' => 'Laskun lähetys',
 'title.charts' => 'Kaaviot',
 
 // TODO: Translate the following.
 // 'title.timesheets' => 'Timesheets',
 // 'title.timesheet' => 'Timesheet',
+// 'title.timesheet_files' => 'Timesheet Files',
 'title.invoice' => 'Facture',
 'title.send_invoice' => 'Envoi de la facture',
 'title.charts' => 'Graphiques',
 
 // TODO: Translate the following.
 // 'title.timesheets' => 'Timesheets',
 // 'title.timesheet' => 'Timesheet',
+// 'title.timesheet_files' => 'Timesheet Files',
 'title.invoice' => 'Τιμολόγιο',
 'title.send_invoice' => 'Αποστολή τιμολόγιου',
 'title.charts' => 'Γραφήματα',
 
 // TODO: Translate the following.
 // 'title.timesheets' => 'Timesheets',
 // 'title.timesheet' => 'Timesheet',
+// 'title.timesheet_files' => 'Timesheet Files',
 'title.invoice' => 'חשבונית',
 'title.send_invoice' => 'שליחת חשבונית',
 'title.charts' => 'תרשימים',
 
 // 'title.send_report' => 'Sending Report',
 // 'title.timesheets' => 'Timesheets',
 // 'title.timesheet' => 'Timesheet',
+// 'title.timesheet_files' => 'Timesheet Files',
 'title.invoice' => 'Számla',
 // TODO: translate the following.
 // 'title.send_invoice' => 'Sending Invoice',
 
 // TODO: Translate the following.
 // 'title.timesheets' => 'Timesheets',
 // 'title.timesheet' => 'Timesheet',
+// 'title.timesheet_files' => 'Timesheet Files',
 'title.invoice' => 'Fattura',
 'title.send_invoice' => 'Invia fattura',
 'title.charts' => 'Grafici',
 
 // 'title.send_report' => 'Sending Report',
 // 'title.timesheets' => 'Timesheets',
 // 'title.timesheet' => 'Timesheet',
+// 'title.timesheet_files' => 'Timesheet Files',
 'title.invoice' => '送り状',
 // TODO: translate the following.
 // 'title.send_invoice' => 'Sending Invoice',
 
 // 'title.send_report' => 'Sending Report',
 // 'title.timesheets' => 'Timesheets',
 // 'title.timesheet' => 'Timesheet',
+// 'title.timesheet_files' => 'Timesheet Files',
 'title.invoice' => '송장',
 // TODO: translate the following.
 // 'title.send_invoice' => 'Sending Invoice',
 
 'title.send_report' => 'Rapport aan het versturen',
 'title.timesheets' => 'Tijdenoverzichten',
 'title.timesheet' => 'Tijdenoverzicht',
+// TODO: Translate the following.
+// 'title.timesheet_files' => 'Timesheet Files',
 'title.invoice' => 'Factuur',
 'title.send_invoice' => 'Factuur verzenden',
 'title.charts' => 'Grafieken',
 
 // 'title.send_report' => 'Sending Report',
 // 'title.timesheets' => 'Timesheets',
 // 'title.timesheet' => 'Timesheet',
+// 'title.timesheet_files' => 'Timesheet Files',
 'title.invoice' => 'Faktura',
 // TODO: translate the following.
 // 'title.send_invoice' => 'Sending Invoice',
 
 // TODO: Translate the following.
 // 'title.timesheets' => 'Timesheets',
 // 'title.timesheet' => 'Timesheet',
+// 'title.timesheet_files' => 'Timesheet Files',
 'title.invoice' => 'Faktura',
 'title.send_invoice' => 'Wysyłanie faktury',
 'title.charts' => 'Statystyki',
 
 // TODO: Translate the following.
 // 'title.timesheets' => 'Timesheets',
 // 'title.timesheet' => 'Timesheet',
+// 'title.timesheet_files' => 'Timesheet Files',
 'title.invoice' => 'Fatura',
 'title.send_invoice' => 'Enviando fatura',
 'title.charts' => 'Gráficos',
 
 // 'title.send_report' => 'Sending Report',
 // 'title.timesheets' => 'Timesheets',
 // 'title.timesheet' => 'Timesheet',
+// 'title.timesheet_files' => 'Timesheet Files',
 // 'title.invoice' => 'Invoice',
 // 'title.send_invoice' => 'Sending Invoice',
 // 'title.charts' => 'Charts',
 
 // 'title.send_report' => 'Sending Report',
 // 'title.timesheets' => 'Timesheets',
 // 'title.timesheet' => 'Timesheet',
+// 'title.timesheet_files' => 'Timesheet Files',
 'title.invoice' => 'Factura',
 // TODO: translate the following.
 // 'title.send_invoice' => 'Sending Invoice',
 
 'title.send_report' => 'Отсылка отчёта',
 'title.timesheets' => 'Табели учёта',
 'title.timesheet' => 'Табель учёта',
+'title.timesheet_files' => 'Файлы табеля учёта',
 'title.invoice' => 'Счёт',
 'title.send_invoice' => 'Отсылка счёта',
 'title.charts' => 'Диаграммы',
 
 // TODO: Translate the following.
 // 'title.timesheets' => 'Timesheets',
 // 'title.timesheet' => 'Timesheet',
+// 'title.timesheet_files' => 'Timesheet Files',
 'title.invoice' => 'Faktúra',
 'title.send_invoice' => 'Odosielanie faktúry',
 'title.charts' => 'Grafy',
 
 // 'title.send_report' => 'Sending Report',
 // 'title.timesheets' => 'Timesheets',
 // 'title.timesheet' => 'Timesheet',
+// 'title.timesheet_files' => 'Timesheet Files',
 // 'title.invoice' => 'Invoice',
 // 'title.send_invoice' => 'Sending Invoice',
 // 'title.charts' => 'Charts',
 
 // TODO: Translate the following.
 // 'title.timesheets' => 'Timesheets',
 // 'title.timesheet' => 'Timesheet',
+// 'title.timesheet_files' => 'Timesheet Files',
 'title.invoice' => 'Račun',
 'title.send_invoice' => 'Slanje računa',
 'title.charts' => 'Grafikoni',
 
 // TODO: Translate the following.
 // 'title.timesheets' => 'Timesheets',
 // 'title.timesheet' => 'Timesheet',
+// 'title.timesheet_files' => 'Timesheet Files',
 'title.invoice' => 'Faktura',
 'title.send_invoice' => 'Skicka faktura',
 'title.charts' => 'Diagram',
 
 // 'title.send_report' => 'Sending Report',
 // 'title.timesheets' => 'Timesheets',
 // 'title.timesheet' => 'Timesheet',
+// 'title.timesheet_files' => 'Timesheet Files',
 'title.invoice' => 'Fatura',
 // TODO: translate the following.
 // 'title.send_invoice' => 'Sending Invoice',
 
 // 'title.send_report' => 'Sending Report',
 // 'title.timesheets' => 'Timesheets',
 // 'title.timesheet' => 'Timesheet',
+// 'title.timesheet_files' => 'Timesheet Files',
 'title.invoice' => '发票',
 // TODO: translate the following.
 // 'title.send_invoice' => 'Sending Invoice',
 
 // 'title.send_report' => 'Sending Report',
 // 'title.timesheets' => 'Timesheets',
 // 'title.timesheet' => 'Timesheet',
+// 'title.timesheet_files' => 'Timesheet Files',
 'title.invoice' => '發票',
 // TODO: translate the following.
 // 'title.send_invoice' => 'Sending Invoice',
 
       <br>
       <table cellspacing="0" cellpadding="4" width="100%" border="0">
         <tr>
-          <td align="center"> Anuko Time Tracker 1.18.65.4951 | Copyright © <a href="https://www.anuko.com/lp/tt_3.htm" target="_blank">Anuko</a> |
+          <td align="center"> Anuko Time Tracker 1.18.65.4952 | Copyright © <a href="https://www.anuko.com/lp/tt_3.htm" target="_blank">Anuko</a> |
             <a href="https://www.anuko.com/lp/tt_4.htm" target="_blank">{$i18n.footer.credits}</a> |
             <a href="https://www.anuko.com/lp/tt_5.htm" target="_blank">{$i18n.footer.license}</a> |
             <a href="https://www.anuko.com/lp/tt_7.htm" target="_blank">{$i18n.footer.improve}</a>
 
 {/if}
           <td class="tableHeader">{$i18n.label.submitted}</td>
           <td class="tableHeader">{$i18n.label.approved}</td>
+{if $show_files}
+          <td></td>
+{/if}
           <td></td>
           <td></td>
         </tr>
           <td></td>
   {else}
           <td>{if $timesheet.approve_status}{$i18n.label.yes}{else}{$i18n.label.no}{/if}</td>
+  {/if}
+  {if $show_files}
+        {if $timesheet.has_files}
+          <td><a href="timesheet_files.php?id={$timesheet.id}"><img class="table_icon" alt="{$i18n.label.files}" src="images/icon_files.png"></a></td>
+        {else}
+          <td><a href="timesheet_files.php?id={$timesheet.id}"><img class="table_icon" alt="{$i18n.label.files}" src="images/icon_file.png"></a></td>
+        {/if}
   {/if}
           <td><a href="timesheet_edit.php?id={$timesheet.id}"><img class="table_icon" alt="{$i18n.label.edit}" src="images/icon_edit.png"></a></td>
           <td><a href="timesheet_delete.php?id={$timesheet.id}"><img class="table_icon" alt="{$i18n.label.delete}" src="images/icon_delete.png"></a></td>
   {/if}
           <td class="tableHeader">{$i18n.label.submitted}</td>
           <td class="tableHeader">{$i18n.label.approved}</td>
-          <td class="tableHeader">{$i18n.label.view}</td>
+  {if $show_files}
+          <td></td>
+  {/if}
           <td></td>
           <td></td>
         </tr>
   {foreach $inactive_timesheets as $timesheet}
         <tr valign="top" bgcolor="{cycle values="#f5f5f5,#ffffff"}">
-          <td>{$timesheet.name|escape}</td>
+            <td><a href="timesheet_view.php?id={$timesheet.id}">{$timesheet.name|escape}</a></td>
     {if $show_client}
           <td>{$timesheet.client_name|escape}</td>
     {/if}
     {else}
           <td>{if $timesheet.approve_status}{$i18n.label.yes}{else}{$i18n.label.no}{/if}</td>
     {/if}
-          <td><a href="timesheet_view.php?id={$timesheet.id}">{$i18n.label.view}</a></td>
+    {if $show_files}
+      {if $timesheet.has_files}
+          <td><a href="timesheet_files.php?id={$timesheet.id}"><img class="table_icon" alt="{$i18n.label.files}" src="images/icon_files.png"></a></td>
+      {else}
+          <td><a href="timesheet_files.php?id={$timesheet.id}"><img class="table_icon" alt="{$i18n.label.files}" src="images/icon_file.png"></a></td>
+      {/if}
+    {/if}
           <td><a href="timesheet_edit.php?id={$timesheet.id}"><img class="table_icon" alt="{$i18n.label.edit}" src="images/icon_edit.png"></a></td>
           <td><a href="timesheet_delete.php?id={$timesheet.id}"><img class="table_icon" alt="{$i18n.label.delete}" src="images/icon_delete.png"></a></td>
         </tr>
 
 
 $smarty->assign('active_projects', $active_projects);
 $smarty->assign('inactive_projects', $inactive_projects);
-$smarty->assign('show_files', $user->isPluginEnabled('at'));
+$smarty->assign('show_files', $showFiles);
 $smarty->assign('title', $i18n->get('title.projects'));
 $smarty->assign('content_page_name', 'projects.tpl');
 $smarty->display('index.tpl');
 
--- /dev/null
+<?php
+// +----------------------------------------------------------------------+
+// | Anuko Time Tracker
+// +----------------------------------------------------------------------+
+// | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
+// +----------------------------------------------------------------------+
+// | LIBERAL FREEWARE LICENSE: This source code document may be used
+// | by anyone for any purpose, and freely redistributed alone or in
+// | combination with other software, provided that the license is obeyed.
+// |
+// | There are only two ways to violate the license:
+// |
+// | 1. To redistribute this code in source form, with the copyright
+// |    notice or license removed or altered. (Distributing in compiled
+// |    forms without embedded copyright notices is permitted).
+// |
+// | 2. To redistribute modified versions of this code in *any* form
+// |    that bears insufficient indications that the modifications are
+// |    not the work of the original author(s).
+// |
+// | This license applies to this document only, not any other software
+// | that it may be combined with.
+// |
+// +----------------------------------------------------------------------+
+// | Contributors:
+// | https://www.anuko.com/time_tracker/credits.htm
+// +----------------------------------------------------------------------+
+
+require_once('initialize.php');
+import('form.Form');
+import('ttTimesheetHelper');
+import('ttFileHelper');
+
+// Access checks.
+if (!(ttAccessAllowed('track_own_time') || ttAccessAllowed('track_time'))) {
+  header('Location: access_denied.php');
+  exit();
+}
+if (!$user->isPluginEnabled('ts')) {
+  header('Location: feature_disabled.php');
+  exit();
+}
+$cl_timesheet_id = (int)$request->getParameter('id');
+$timesheet = ttTimesheetHelper::getTimesheet($cl_timesheet_id);
+if (!$timesheet) {
+  header('Location: access_denied.php');
+  exit();
+}
+// End of access checks.
+
+if ($request->isPost()) {
+  $cl_description = trim($request->getParameter('description'));
+}
+
+$fileHelper = new ttFileHelper($err);
+$files = $fileHelper::getEntityFiles($cl_timesheet_id, 'timesheet');
+
+$form = new Form('fileUploadForm');
+$form->addInput(array('type'=>'hidden','name'=>'id','value'=>$cl_timesheet_id));
+$form->addInput(array('type'=>'upload','name'=>'newfile','value'=>$i18n->get('button.submit')));
+$form->addInput(array('type'=>'textarea','name'=>'description','style'=>'width: 250px; height: 40px;','value'=>$cl_description));
+$form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->get('button.add')));
+
+if ($request->isPost()) {
+  // We are adding a new file.
+
+  // Validate user input.
+  if (!$_FILES['newfile']['name']) $err->add($i18n->get('error.upload'));
+  if (!ttValidString($cl_description, true)) $err->add($i18n->get('error.field'), $i18n->get('label.description'));
+  // Finished validating user input.
+
+  if ($err->no()) {
+    $fields = array('entity_type'=>'timesheet',
+      'entity_id' => $cl_timesheet_id,
+      'file_name' => $_FILES['newfile']['name'],
+      'description'=>$cl_description);
+    if ($fileHelper->putFile($fields)) {
+      header('Location: timesheet_files.php?id='.$cl_timesheet_id);
+      exit();
+    }
+  }
+} // isPost
+
+$smarty->assign('can_edit', true); // Relying on access checks above.
+$smarty->assign('forms', array($form->getName()=>$form->toArray()));
+$smarty->assign('files', $files);
+$smarty->assign('title', $i18n->get('title.timesheet_files').': '.$timesheet['name']);
+$smarty->assign('content_page_name', 'entity_files.tpl');
+$smarty->display('index.tpl');
 
 }
 // End of access checks.
 
-// Determine user for which we display this page.
+// Determine user for whom we display this page.
 if ($request->isPost() && $userChanged) {
   $user_id = $request->getParameter('user');
   $user->setOnBehalfUser($user_id);
 
 $group_id = $user->getGroup();
 
+$showFiles = $user->isPluginEnabled('at');
+
 // Elements of timesheetsForm.
 $form = new Form('timesheetsForm');
 
 $smarty->assign('active_timesheets', $active_timesheets);
 $smarty->assign('inactive_timesheets', $inactive_timesheets);
 $smarty->assign('show_client', $showClient);
+$smarty->assign('show_files', $showFiles);
 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
 $smarty->assign('title', $i18n->get('title.timesheets'));
 $smarty->assign('content_page_name', 'timesheets.tpl');