Example #1
0
 public function importAttendance($options, $targetPath)
 {
     include_once LIBRARY_PATH . "/Base/Excel/PHPExcel.php";
     $objPHPExcel = new PHPExcel();
     $objPHPExcel = PHPExcel_IOFactory::load($targetPath);
     foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
         if ($worksheet->getTitle() == "Sheet1") {
             $rowctr = 0;
             foreach ($worksheet->getRowIterator() as $row) {
                 if ($row->getRowIndex() == 1) {
                     $cellIterator = $row->getCellIterator();
                     $cellIterator->setIterateOnlyExistingCells(false);
                     // Loop all cells, even if it is not set
                     foreach ($cellIterator as $cell) {
                         if (!is_null($cell)) {
                             if ($cell->getCoordinate() == "A1") {
                                 $columns[] = $cell->getCalculatedValue();
                             } else {
                                 $columns[] = $options['year'] . "-" . $options['month'] . "-" . $cell->getCalculatedValue();
                             }
                         }
                     }
                 } else {
                     $cellIterator = $row->getCellIterator();
                     $cellIterator->setIterateOnlyExistingCells(false);
                     // Loop all cells, even if it is not set
                     $cellctr = 0;
                     foreach ($cellIterator as $cell) {
                         if (!is_null($cell)) {
                             if ($cellctr == 0) {
                                 $empCode = $cell->getCalculatedValue();
                             } else {
                                 $user = new Application_Model_User();
                                 $user = $user->fetchRow("employee_code='{$empCode}'");
                                 if (false !== $user) {
                                     $attendance = new Application_Model_Attendance();
                                     $attendance = $attendance->fetchRow("user_id='" . $user->getId() . "' and attendance_date='" . $columns[$cellctr] . "'");
                                     if (false === $attendance) {
                                         //do insert
                                         if (trim($cell->getCalculatedValue()) != "") {
                                             $attendance = new Application_Model_Attendance();
                                             $attendance->setEmpCode($empCode);
                                             $attendance->setAttendance($cell->getCalculatedValue());
                                             $attendance->setAttendanceDate($columns[$cellctr]);
                                             $attendance->setUserId($user->getId());
                                             $attendance->save();
                                         }
                                     } else {
                                         //do update
                                         if (trim($cell->getCalculatedValue()) != "") {
                                             $attendance->setEmpCode($empCode);
                                             $attendance->setAttendance($cell->getCalculatedValue());
                                             $attendance->setAttendanceDate($columns[$cellctr]);
                                             $attendance->setUserId($user->getId());
                                             $attendance->save();
                                         }
                                     }
                                 }
                             }
                         }
                         $cellctr++;
                     }
                     // end of cellIterator foreach
                     $rowctr++;
                 }
             }
             //end of row iterator foreach
         }
         // If worksheet = Sheet1
     }
     // end of worksheet iterator foreach
     return $rowctr;
 }