/**
  *
  * @return array
  */
 public function createAction()
 {
     if ($this->getRequest()->isPost()) {
         $params = $this->getRequest()->getParams();
         try {
             $this->getNpdSheetSupplierCatalog()->beginTransaction();
             foreach ($params["supplier"] as $index => $supplier) {
                 $idSupplier = $supplier['id_supplier'];
                 $idNpdSupplier = $supplier['id_npd_supplier'];
                 //     		    $supplierParams = Application\Query\SapSupplierQuery::create()->findByPKOrThrow($idSupplier,$this->i18n->_("It does not exist the supplier with id {$idSupplier}"));
                 if ($idNpdSupplier > 0) {
                     $npdSupplierParams = Application\Query\NpdSheetSupplierQuery::create()->findByPKOrElse($idNpdSupplier, new NpdSheetSupplier());
                     $npdSheetSupplier = NpdSheetSupplierFactory::populate($npdSupplierParams, $supplier);
                     $this->getNpdSheetSupplierCatalog()->update($npdSupplierParams);
                     foreach ($_FILES as $name => $value) {
                         if (strstr($name, "image_" . $index . "_") != false && $value["name"] != "") {
                             $fileUploader = new FileUploader($name);
                             if (strstr($name, "image") != false) {
                                 $uploadPath = 'public/files/npd/' . $supplier['id_npd_sheet'] . '/imagesSupplier';
                                 $fileUploader->saveFile($uploadPath, true);
                                 $type = File::$typeFile["typeImage"];
                             }
                             $sheetFile = new File();
                             $sheetFile->setType($type)->setContent('files/npd/' . $supplier['id_npd_sheet'] . '/imagesSupplier' . $fileUploader->getFileName());
                             $this->getCatalog("FileCatalog")->create($sheetFile);
                             $this->getNpdSheetSupplierCatalog()->linkToFile($idNpdSupplier, $sheetFile->getIdFile());
                         }
                     }
                     //Videos
                     //search if npd supplier have videos
                     $npdVideos = NpdSheetSupplierQuery::create()->addColumns(array("File.*"))->innerJoinFile()->whereAdd("NpdSheetSupplier." . NpdSheetSupplier::ID_NPD_SUPPLIER, $idNpdSupplier)->whereAdd("File." . File::TYPE, File::$typeFile["typeLink"])->fetchAll();
                     // if npd supplier have videos then delete it all
                     foreach ($npdVideos as $npdVideo) {
                         $this->getFileCatalog()->unlinkFromNpdSheetSupplier($npdVideo["id_file"], $idNpdSupplier);
                         $this->getFileCatalog()->deleteById($npdVideo["id_file"]);
                     }
                     //die("<pre>".print_r($supplier, true)."</pre>");
                     // insert new videos
                     $supplier["video"] = (array) $supplier["video"];
                     foreach ($supplier["video"] as $video) {
                         if ($video != "") {
                             //insert a new video with Type -> video
                             $sheetFile = new File();
                             $sheetFile->setType(File::$typeFile["typeLink"])->setContent($video);
                             //make relationship file - npdSupplier
                             $this->getFileCatalog()->create($sheetFile);
                             $this->getNpdSheetSupplierCatalog()->linkToFile($idNpdSupplier, $sheetFile->getIdFile());
                         }
                     }
                     //Moq and Price
                     //search if npd supplier have moq and price
                     $npdMoqPrices = SupplierBudgetQuery::create()->whereAdd(SupplierBudget::ID_NPD_SUPPLIER, $idNpdSupplier)->fetchAll();
                     //if npd supplier have moq and price then delete it all
                     foreach ($npdMoqPrices as $npdMoqPrice) {
                         $this->getSupplierBudgetCatalog()->deleteById($npdMoqPrice["id_supplier_budget"]);
                     }
                     //insert new moq and price
                     foreach ($supplier["moqPrice"] as $moqprice) {
                         $npdMoq = new SupplierBudget();
                         $npdMoq->setIdNpdSupplier($idNpdSupplier)->setMoq($moqprice["moq"])->setLeadTime($moqprice["leadtime"])->setPrice($moqprice["price"])->setIdCurrency($supplier["id_currency"]);
                         $this->getSupplierBudgetCatalog()->create($npdMoq);
                     }
                     $this->getNpdSheetSupplierCatalog()->unlinkAllColor($idNpdSupplier);
                 } else {
                     $newSuppliers = $supplier;
                     $npdSheetSupplier = NpdSheetSupplierFactory::createFromArray($newSuppliers);
                     $this->getNpdSheetSupplierCatalog()->create($npdSheetSupplier);
                     $newSupplier = $npdSheetSupplier->getIdNpdSupplier();
                     foreach ($_FILES as $name => $value) {
                         if (strstr($name, "image_" . $index . "_") != false && $value["name"] != "") {
                             $fileUploader = new FileUploader($name);
                             if (strstr($name, "image") != false) {
                                 $uploadPath = 'public/files/npd/' . $supplier['id_npd_sheet'] . '/imagesSupplier';
                                 $fileUploader->saveFile($uploadPath, true);
                                 $type = File::$typeFile["typeImage"];
                             }
                             $uploadPath = 'files/npd/' . $supplier['id_npd_sheet'] . '/imagesSupplier';
                             $sheetFile = new File();
                             $sheetFile->setType($type)->setContent($uploadPath . $fileUploader->getFileName());
                             $this->getCatalog("FileCatalog")->create($sheetFile);
                             $this->getNpdSheetSupplierCatalog()->linkToFile($newSupplier, $sheetFile->getIdFile());
                         }
                     }
                     foreach ($supplier["video"] as $video) {
                         if ($video != "") {
                             //insert a new video with Type -> video
                             $sheetFile = new File();
                             $sheetFile->setType(File::$typeFile["typeLink"])->setContent($video);
                             //make relationship file - npdSupplier
                             $this->getFileCatalog()->create($sheetFile);
                             $this->getNpdSheetSupplierCatalog()->linkToFile($newSupplier, $sheetFile->getIdFile());
                         }
                     }
                     //insert new moq and price
                     foreach ($supplier["moqPrice"] as $moqprice) {
                         $npdMoq = new SupplierBudget();
                         $npdMoq->setIdNpdSupplier($newSupplier)->setMoq($moqprice["moq"])->setLeadTime($moqprice["leadtime"])->setPrice($moqprice["price"])->setIdCurrency($supplier["id_currency"]);
                         $this->getSupplierBudgetCatalog()->create($npdMoq);
                     }
                 }
             }
             $this->getNpdSheetSupplierCatalog()->commit();
             $this->setFlash('ok', $this->i18n->_("The Supplier has been saved"));
         } catch (Exception $e) {
             // Roll back any change in database
             $this->getNpdSheetSupplierCatalog()->rollBack();
             $this->setFlash('error', $this->i18n->_($e->getMessage()));
             throw $e;
         }
     }
     $this->_redirect('npd-sheet/list');
 }
 /**
  *
  * makeBean
  * @param array $resultset
  * @return \Application\Model\Bean\NpdSheetSupplier
  */
 protected function makeBean($resultset)
 {
     return NpdSheetSupplierFactory::createFromArray($resultset);
 }