/**
  * Edit kpi into db
  *
  * @author Hrayr Shahbazyan
  * @param integer $id
  * @param array $data
  * @return int
  */
 public function editKpiAndValue($id, $data)
 {
     $datasheet = $this->getDataSheetByKpi($id);
     $em = $this->getEntityManager();
     $kpi = $this->getKpiById($id);
     $kpi->setName($data['kpiname'])->setCritical($data['iscritical'])->setPercent($data['criticalvariance'])->setUpdatedAt(new \DateTime());
     $em->merge($kpi);
     $em->flush();
     if (!empty($datasheet[0])) {
         $qb = $this->getEntityManager()->createQueryBuilder();
         $qb->update('KPI\\Entity\\Value', 'v')->set('v.initialValue', '?1')->set('v.finalTargetValue', '?2')->set('v.dataType', '?3')->set('v.name', '?4')->setParameter(1, $data['initValue'])->setParameter(2, $data['targetValue'])->setParameter(3, $data['data_type'])->setParameter(4, $data['unit'])->where("v.dataSheet = '" . $datasheet[0]['id'] . "'");
         $q = $qb->getQuery()->execute();
         $qb = $this->getEntityManager()->createQueryBuilder();
         $qb->update('KPI\\Entity\\DataSheet', 'd')->set('d.name', '?1')->setParameter(1, $data['desc'])->where("d.id = '" . $datasheet[0]['id'] . "'");
         $q = $qb->getQuery()->execute();
     } else {
         // Add kpi to datssheet
         $em = $this->getEntityManager();
         $datasheet = new \KPI\Entity\DataSheet();
         $datasheet->setName($data['desc']);
         $datasheet->setMeasurementUnit($data['data_type']);
         $datasheet->setKpi($this->getEntityManager()->getRepository('KPI\\Entity\\Kpi')->find($id));
         $em->persist($datasheet);
         $em->flush();
         $datasheet_id = $datasheet->getId();
         // Add kpi to datssheet value
         $em = $this->getEntityManager();
         $value = new \KPI\Entity\Value();
         $value->setName($data['unit']);
         $value->setDataType($data['data_type']);
         $value->setInitialValue($data['initValue']);
         $value->setFinalTargetValue($data['targetValue']);
         $value->setDataSheet($this->getEntityManager()->getRepository('KPI\\Entity\\DataSheet')->find($datasheet_id));
         $em->persist($value);
         $em->flush();
     }
     if (isset($data['resp_id'])) {
         // First delete any existing Resp in KPI then add new on next line
         $this->getResponsibilityService()->deleteResponsibilityByResourceId(array('resource_id' => $id, 'resource_type' => 'kpi', 'vision_id' => $data['vision_id']));
         $result = $this->getResponsibilityService()->addResponsibility(array('resource_id' => $id, 'resource_type' => 'kpi', 'user_id' => $data['resp_id'], 'vision_id' => $data['vision_id']));
     }
     $result = array();
     $result['status_code'] = 200;
     return $result;
 }
 /**
  * Add value into db
  *
  * @author Hrayr Shahbazyan
  * @param array $data
  * @return array
  */
 public function addValue(array $data)
 {
     $em = $this->getEntityManager();
     $value = new \KPI\Entity\Value();
     $value->setName($data['name']);
     $value->setDataType($data['data_type']);
     $value->setInitialValue($data['initial_value']);
     $value->setFinalTargetValue($data['final_target_value']);
     $value->setDataSheet($this->getEntityManager()->getRepository('KPI\\Entity\\DataSheet')->find($data['datasheet_id']));
     $em->persist($value);
     $em->flush();
     $result = array();
     $result['status_code'] = 201;
     $result['value_id'] = $value->getId();
     return $result;
 }