/**
  *
  * @return array
  */
 public function updateAction()
 {
     $params = $this->getRequest()->getParams();
     if ($this->getRequest()->isPost()) {
         $idProformaInvoice = $this->getRequest()->getParam('id_proforma_invoice');
         $proformaInvoiceParams['date'] = $this->getRequest()->getParam('date', null);
         $proformaInvoiceParams['folio'] = $this->getRequest()->getParam('folio', null);
         $proformaInvoiceParams['id_courtyard'] = $this->getRequest()->getParam('id_courtyard', null);
         $proformaInvoiceParams['id_currency'] = $this->getRequest()->getParam('id_currency');
         $proformaInvoiceParams['comments'] = $this->getRequest()->getParam('comments', null);
         $proformaInvoiceEntriesParams['warehouseStore'] = $this->getRequest()->getParam('warehouse_store', '');
         $proformaInvoiceEntriesParams['entries'] = $this->getRequest()->getParam('entries', array());
         $proformaInvoicePaymentTermIds = ProformaInvoiceProgrammedPaymentQuery::create()->addColumn(ProformaInvoiceProgrammedPayment::ID_PROFORMA_INVOICE_PAYMENT_TERM)->whereAdd(ProformaInvoiceProgrammedPayment::ID_PROFORMA_INVOICE, $idProformaInvoice)->fetchCol();
         $proformaInvoiceNotErasableEvents = ProformaInvoicePaymentTermQuery::create()->addColumn(ProformaInvoicePaymentTerm::ID_EVENT)->whereAdd(ProformaInvoicePaymentTerm::ID_PROFORMA_INVOICE_PAYMENT, $proformaInvoicePaymentTermIds, ProformaInvoicePaymentTermQuery::IN)->distinct()->fetchCol();
         $erasableEvents = EventQuery::create()->addColumn(Event::ID_EVENT)->whereAdd(Event::ID_EVENT, $proformaInvoiceNotErasableEvents, EventQuery::NOT_IN)->distinct()->fetchCol();
         $proformaInvoicePaymentTermsParams["events"] = $this->getRequest()->getParam('events', array());
         $totalAmmount = $this->getRequest()->getParam('quantityMatch');
         $totalProformaInvoiceEntries = count($proformaInvoiceEntriesParams['entries']);
         try {
             $proformaInvoice = ProformaInvoiceQuery::create()->findByPKOrThrow($idProformaInvoice, $this->i18n->_("Proforma Invoice with id {$idProformaInvoice} not found"));
             $proformaInvoiceEntries = ProformaInvoiceEntryQuery::create()->whereAdd(ProformaInvoiceEntry::ID_PROFORMA_INVOICE, $proformaInvoice->getIdProformaInvoice())->find();
             foreach ($proformaInvoiceEntriesParams['entries'] as $index => $values) {
                 $proformaInvoiceEntry = $proformaInvoiceEntries->getByPK($index);
                 $values["id_warehouse_store"] = $proformaInvoiceEntriesParams['warehouseStore'];
                 ProformaInvoiceEntryFactory::populate($proformaInvoiceEntry, $values);
             }
             ProformaInvoiceFactory::populate($proformaInvoice, $proformaInvoiceParams);
             $sapConnectionClient = new SAPConnectorClient();
             $result = $sapConnectionClient->purchaseQuotation($proformaInvoice, $proformaInvoiceEntries);
             if ($result->purchaseQuotationResult->code == 200) {
                 $proformaInvoiceFiles = ProformaInvoiceFileQuery::create()->whereAdd(ProformaInvoiceFile::ID_PROFORMA_INVOICE, $proformaInvoice->getIdProformaInvoice())->find();
                 $uploadPath = 'public/' . ($filePath = "files/proformas/" . $result->purchaseQuotationResult->id);
                 if ($_FILES["proforma_invoice_file"]["name"] != "") {
                     $fileUploader = new FileUploader("proforma_invoice_file");
                     $fileUploader->saveFile($uploadPath, false);
                     $newFile = FileFactory::createFromArray(array(File::CONTENT => $filePath . $fileUploader->getFileName(), File::TYPE => file::$typeFile["typeFile"]));
                     $this->getFileCatalog()->create($newFile);
                     $newProformaInvoiceFile = ProformaInvoiceFileFactory::createFromArray(array(ProformaInvoiceFile::ID_PROFORMA_INVOICE => $proformaInvoice->getIdProformaInvoice(), ProformaInvoiceFile::ID_FILE => $newFile->getIdFile(), ProformaInvoiceFile::TYPE => ProformaInvoiceFile::$Types["PINew"]));
                     $this->getProformaInvoiceFileCatalog()->create($newProformaInvoiceFile);
                     $proformaInvoiceFile = $proformaInvoiceFiles->getByType(ProformaInvoiceFile::$Types["PINew"]);
                     if ($proformaInvoiceFile->getIdProformaInvoiceFile() > 0) {
                         $this->getProformaInvoiceFileCatalog()->deleteById($proformaInvoiceFile->getIdProformaInvoiceFile());
                         $this->getFileCatalog()->deleteById($proformaInvoiceFile->getIdFile());
                     }
                 }
                 if ($_FILES["proforma_invoice_file_authorized"]["name"] != "") {
                     $fileUploader = new FileUploader("proforma_invoice_file_authorized");
                     $fileUploader->saveFile($uploadPath, false);
                     $newFile = FileFactory::createFromArray(array(File::CONTENT => $filePath . $fileUploader->getFileName(), File::TYPE => file::$typeFile["typeFile"]));
                     $this->getFileCatalog()->create($newFile);
                     $newProformaInvoiceFile = ProformaInvoiceFileFactory::createFromArray(array(ProformaInvoiceFile::ID_PROFORMA_INVOICE => $proformaInvoice->getIdProformaInvoice(), ProformaInvoiceFile::ID_FILE => $newFile->getIdFile(), ProformaInvoiceFile::TYPE => ProformaInvoiceFile::$Types["PIAuthorized"]));
                     $this->getProformaInvoiceFileCatalog()->create($newProformaInvoiceFile);
                     $proformaInvoiceFile = $proformaInvoiceFiles->getByType(ProformaInvoiceFile::$Types["PIAuthorized"]);
                     if ($proformaInvoiceFile->getIdProformaInvoiceFile() > 0) {
                         $this->getProformaInvoiceFileCatalog()->deleteById($proformaInvoiceFile->getIdProformaInvoiceFile());
                         $this->getFileCatalog()->deleteById($proformaInvoiceFile->getIdFile());
                     }
                 }
                 $allPaymentTerms = ProformaInvoicePaymentTermQuery::create()->whereAdd(ProformaInvoicePaymentTerm::ID_PROFORMA_INVOICE, $proformaInvoice->getIdProformaInvoice())->find();
                 try {
                     $this->getProformaInvoicePaymentTermCatalog()->beginTransaction();
                     $paymentTerms = $allPaymentTerms->filterByEventId($erasableEvents);
                     while ($paymentTerms->valid()) {
                         $paymentTerm = $paymentTerms->read();
                         $this->getProformaInvoicePaymentTermCatalog()->deleteById($paymentTerm->getIdProformaInvoicePayment());
                     }
                     $notErasabledPaymentTerms = $allPaymentTerms->filterByEventId($proformaInvoiceNotErasableEvents, $line);
                     $programmedPayments = ProformaInvoiceProgrammedPaymentQuery::create()->whereAdd(ProformaInvoiceProgrammedPayment::ID_PROFORMA_INVOICE_PAYMENT_TERM, $notErasabledPaymentTerms->getPrimaryKeys())->find();
                     while ($proformaInvoiceEntries->valid()) {
                         $proformaInvoiceEntry = $proformaInvoiceEntries->read();
                         $totalPayment = 0;
                         foreach ($proformaInvoicePaymentTermsParams["events"] as $key => $payment) {
                             $amount = str_replace(",", "", $payment['payment']);
                             $proformaInvoicePaymentTermsParams["events"][$key]['percentage'] = $amount / $totalAmmount * 100;
                         }
                         foreach ($proformaInvoicePaymentTermsParams["events"] as $event) {
                             $newPaymentTerm = ProformaInvoicePaymentTermFactory::createFromArray(array(ProformaInvoicePaymentTerm::ID_PROFORMA_INVOICE => $proformaInvoice->getIdProformaInvoice(), ProformaInvoicePaymentTerm::ID_PROFORMA_INVOICE_ENTRY => $proformaInvoiceEntry->getIdProformaInvoiceEntry(), ProformaInvoicePaymentTerm::LINE => $proformaInvoiceEntry->getVisOrder(), ProformaInvoicePaymentTerm::ID_EVENT => $event["id"], ProformaInvoicePaymentTerm::PIECES => $proformaInvoiceEntry->getQuantity(), ProformaInvoicePaymentTerm::PAYMENT => $proformaInvoiceEntry->getTotalImport() * $event["percentage"] / 100, ProformaInvoicePaymentTerm::CURRENCY => $proformaInvoice->getIdCurrency()));
                             $this->getProformaInvoicePaymentTermCatalog()->create($newPaymentTerm);
                         }
                         $entryPaymentTerms = $notErasabledPaymentTerms->filterByProformaInvoiceEntry($proformaInvoiceEntry);
                         while ($entryPaymentTerms->valid()) {
                             $paymentTerm = $entryPaymentTerms->read();
                             $entryProgrammedPayments = $programmedPayments->filterByPaymentTerm($paymentTerm);
                             while ($entryProgrammedPayments->valid()) {
                                 $payment = $entryProgrammedPayments->read();
                                 $payment->setIdCurrency($proformaInvoice->getIdCurrency());
                                 $payment->setLine($proformaInvoiceEntry->getVisOrder());
                                 $this->getProformaInvoiceProgrammedPaymentCatalog()->update($payment);
                             }
                             $paymentTerm->setCurrency($proformaInvoice->getIdCurrency());
                             $paymentTerm->setIdProformaInvoiceEntry($proformaInvoiceEntry->getIdProformaInvoiceEntry());
                             $paymentTerm->setLine($proformaInvoiceEntry->getVisOrder());
                             $this->getProformaInvoicePaymentTermCatalog()->update($paymentTerm);
                         }
                     }
                     $this->newLogForUpdate($proformaInvoice);
                     $this->getProformaInvoicePaymentTermCatalog()->commit();
                     $this->setFlash('ok', $this->i18n->_("Proforma Invoice with id {$result->purchaseQuotationResult->id} was updated."));
                 } catch (Exception $e) {
                     $this->getProformaInvoicePaymentTermCatalog()->rollBack();
                     $this->setFlash('error', $this->i18n->_($e->getMessage()));
                 }
             } else {
                 $this->setFlash('error', $result->purchaseQuotationResult->code . " - " . $result->purchaseQuotationResult->message);
             }
         } catch (Exception $e) {
             $this->setFlash('error', $this->i18n->_($e->getMessage()));
         }
     }
     $this->_redirect('proforma-invoice/list');
 }
 /**
  *
  * makeBean
  * @param array $resultset
  * @return \Application\Model\Bean\ProformaInvoice
  */
 protected function makeBean($resultset)
 {
     return ProformaInvoiceFactory::createFromArray($resultset);
 }