/**
  * Save employee contract
  */
 public function save()
 {
     $empNumber = $this->getValue('EmpID');
     $attachId = $this->getValue('seqNO');
     $empAttachment = false;
     if (empty($attachId)) {
         $q = Doctrine_Query::create()->select('MAX(a.attach_id)')->from('EmployeeAttachment a')->where('a.emp_number = ?', $empNumber);
         $result = $q->execute(array(), Doctrine::HYDRATE_ARRAY);
         if (count($result) != 1) {
             throw new PIMServiceException('MAX(a.attach_id) failed.');
         }
         $attachId = is_null($result[0]['MAX']) ? 1 : $result[0]['MAX'] + 1;
     } else {
         $q = Doctrine_Query::create()->select('a.emp_number, a.attach_id')->from('EmployeeAttachment a')->where('a.emp_number = ?', $empNumber)->andWhere('a.attach_id = ?', $attachId);
         $result = $q->execute();
         if ($result->count() == 1) {
             $empAttachment = $result[0];
         } else {
             throw new PIMServiceException('Invalid attachment');
         }
     }
     //
     // New file upload
     //
     $newFile = false;
     if ($empAttachment === false) {
         $empAttachment = new EmployeeAttachment();
         $empAttachment->emp_number = $empNumber;
         $empAttachment->attach_id = $attachId;
         $newFile = true;
     }
     $commentOnly = $this->getValue('commentOnly');
     if ($newFile || $commentOnly == '0') {
         $file = $this->getValue('ufile');
         $tempName = $file->getTempName();
         $empAttachment->size = $file->getSize();
         $empAttachment->filename = $file->getOriginalName();
         $empAttachment->attachment = file_get_contents($tempName);
         $empAttachment->file_type = $file->getType();
         $empAttachment->screen = $this->getValue('screen');
         $empAttachment->attached_by = $this->getOption('loggedInUser');
         $empAttachment->attached_by_name = $this->getOption('loggedInUserName');
         // emp_id and name
     }
     $empAttachment->description = $this->getValue('txtAttDesc');
     $empAttachment->save();
 }
 /**
  * Testing getAttachment
  */
 public function testGetAttachment()
 {
     $empNumber = 121;
     $attachments = array();
     foreach ($this->testCase['EmployeeAttachment'] as $values) {
         $attachment = new EmployeeAttachment();
         $attachment->fromArray($values);
         $attachments[] = $attachment;
     }
     $attachment = $attachments[0];
     $attachmentId = $attachment->getAttachId();
     $mockDao = $this->getMock('EmployeeDao');
     $mockDao->expects($this->once())->method('getAttachment')->with($empNumber, $attachmentId)->will($this->returnValue($attachment));
     $this->employeeService->setEmployeeDao($mockDao);
     $result = $this->employeeService->getAttachment($empNumber, $attachmentId);
     $this->assertEquals($attachment, $result);
     $mockDao = $this->getMock('EmployeeDao');
     $mockDao->expects($this->once())->method('getAttachment')->with($empNumber, $attachmentId)->will($this->throwException(new DaoException()));
     $this->employeeService->setEmployeeDao($mockDao);
     try {
         $result = $this->employeeService->getAttachment($empNumber, $attachmentId);
         $this->fail("Exception expected");
     } catch (Exception $e) {
     }
 }
 /**
  * Save employee contract
  */
 public function updateAttachment()
 {
     $empNumber = $this->getValue('emp_number');
     //$attachId = $this->getValue('seqNO');
     $update = $this->getValue('contract_update');
     $empAttachment = false;
     $file = $this->getValue('contract_file');
     if ($update == self::CONTRACT_DELETE) {
         $q = Doctrine_Query::create()->delete('EmployeeAttachment a')->where('emp_number = ?', $empNumber)->andWhere('screen = ?', "contract");
         $result = $q->execute();
     } else {
         if ($update == self::CONTRACT_UPLOAD || !empty($file)) {
             // find existing
             $q = Doctrine_Query::create()->select('a.emp_number, a.attach_id')->from('EmployeeAttachment a')->where('a.emp_number = ?', $empNumber)->andWhere('screen = ?', "contract");
             $result = $q->execute();
             if ($result->count() == 1) {
                 $empAttachment = $result[0];
             }
             //
             // New file upload
             //
             $newFile = false;
             if ($empAttachment === false) {
                 $empAttachment = new EmployeeAttachment();
                 $empAttachment->emp_number = $empNumber;
                 $q = Doctrine_Query::create()->select('MAX(a.attach_id)')->from('EmployeeAttachment a')->where('a.emp_number = ?', $empNumber);
                 $result = $q->execute(array(), Doctrine::HYDRATE_ARRAY);
                 if (count($result) != 1) {
                     throw new PIMServiceException('MAX(a.attach_id) failed.');
                 }
                 $attachId = is_null($result[0]['MAX']) ? 1 : $result[0]['MAX'] + 1;
                 $empAttachment->attach_id = $attachId;
                 $newFile = true;
             }
             $tempName = $file->getTempName();
             $empAttachment->size = $file->getSize();
             $empAttachment->filename = $file->getOriginalName();
             $empAttachment->attachment = file_get_contents($tempName);
             $empAttachment->file_type = $file->getType();
             $empAttachment->screen = EmployeeAttachment::SCREEN_JOB_CONTRACT;
             $empAttachment->attached_by = $this->getOption('loggedInUser');
             $empAttachment->attached_by_name = $this->getOption('loggedInUserName');
             $empAttachment->save();
         }
     }
 }