/**
  * 
  * Cancela los pagos relacionados con la proforma invoice
  * @param int $idProformaInvoice
  */
 private function cancelPaymentsByIdProformaInvoice($idProformaInvoice)
 {
     $proformaInvoicePaymentTerms = ProformaInvoicePaymentTermQuery::create()->whereAdd(ProformaInvoicePaymentTerm::ID_PROFORMA_INVOICE, $idProformaInvoice)->find();
     $proformaInvoiceProgrammedPayments = ProformaInvoiceProgrammedPaymentQuery::create()->whereAdd(ProformaInvoiceProgrammedPayment::ID_PROFORMA_INVOICE, $idProformaInvoice)->find();
     $purchaseOrderPaymentTerms = PurchaseOrderPaymentTermQuery::create()->whereAdd(PurchaseOrderPaymentTerm::ID_PROFORMA_INVOICE_PAYMENT_TERM, $proformaInvoicePaymentTerms->getPrimaryKeys())->find();
     $purchaseOrderProgrammedPayments = PurchaseOrderProgrammedPaymentQuery::create()->whereAdd(PurchaseOrderProgrammedPayment::ID_PURCHASE_ORDER_PAYMENT_TERM, $purchaseOrderPaymentTerms->getPrimaryKeys())->find();
     $actualPayments = ActualPaymentQuery::create()->whereAdd(ActualPayment::ID_PROGRAMMED_PAYMENT, $proformaInvoiceProgrammedPayments->getProgrammedPaymentIds() + $purchaseOrderProgrammedPayments->getProgrammedPaymentIds())->find();
     while ($proformaInvoicePaymentTerm = $proformaInvoicePaymentTerms->read()) {
         $proformaInvoicePaymentTerm->setStatus(ProformaInvoicePaymentTerm::$Status['Canceled']);
         $this->getProformaInvoicePaymentTermCatalog()->update($proformaInvoicePaymentTerm);
     }
     while ($proformaInvoiceProgrammedPayment = $proformaInvoiceProgrammedPayments->read()) {
         $proformaInvoiceProgrammedPayment->setStatus(ProformaInvoiceProgrammedPayment::$TypeStatus['Canceled']);
         $this->getProformaInvoiceProgrammedPaymentCatalog()->update($proformaInvoiceProgrammedPayment);
     }
     while ($purchaseOrderPaymentTerm = $purchaseOrderPaymentTerms->read()) {
         $purchaseOrderPaymentTerm->setStatus(PurchaseOrderPaymentTerm::$Status['Canceled']);
         $this->getPurchaseOrderPaymentTermsCatalog()->update($purchaseOrderPaymentTerm);
     }
     while ($purchaseOrderProgrammedPayment = $purchaseOrderProgrammedPayments->read()) {
         $purchaseOrderProgrammedPayment->setStatus(PurchaseOrderProgrammedPayment::$TypeStatus['Canceled']);
         $this->getPurchaseOrderProgrammedPaymentCatalog()->update($purchaseOrderProgrammedPayment);
     }
     while ($actualPayment = $actualPayments->read()) {
         $actualPayment->setStatus(ActualPayment::$Status['Canceled']);
         $this->getActualPaymentCatalog()->update($actualPayment);
     }
 }
 /**
  *
  *
  * @author Erick Guevara Martínez
  * @param FolioImportProgrammedPaymentCollection $folioProgrammedPayments
  * @return CondensedProgrammedPaymentCollection
  */
 private function condenseFolioPayments(FolioImportProgrammedPaymentCollection $folioProgrammedPayments)
 {
     $condensedPayments = new CondensedProgrammedPaymentCollection();
     $actualPayments = ActualPaymentQuery::create()->whereAdd(ActualPayment::ID_PROGRAMMED_PAYMENT, $folioProgrammedPayments->getProgrammedPaymentIds())->find();
     while ($folioProgrammedPayments->valid()) {
         $programmedPayment = $folioProgrammedPayments->read();
         $credits = $actualPayments->filterByProgrammedPayment($programmedPayment);
         $condensedPayment = CondensedProgrammedPaymentFactory::createFromArray(array(CondensedProgrammedPayment::ID_DOCUMENT => $programmedPayment->getFolio(), CondensedProgrammedPayment::SAP_DOCNUM => $programmedPayment->getDocNum(), CondensedProgrammedPayment::ID_SUPPLIER => $programmedPayment->getIdCompany(), CondensedProgrammedPayment::TYPE => $programmedPayment->getType(), CondensedProgrammedPayment::AMMOUNT => $programmedPayment->getAmmount(), CondensedProgrammedPayment::CREDITS => $credits->getTotalAmmount(), CondensedProgrammedPayment::CURRENCY => $programmedPayment->getIdCurrency(), CondensedProgrammedPayment::DUE_DATE => $programmedPayment->getDueDate(), CondensedProgrammedPayment::PROGRAMMED_PAYMENT_IDS => array($programmedPayment->getIdProgrammedPayment()), CondensedProgrammedPayment::STATUS => $programmedPayment->getStatus()));
         $condensedPayments->append($condensedPayment);
     }
     return $condensedPayments;
 }
 /**
  * 
  * @throws Exception
  */
 public function downloadBankslipAction()
 {
     $programmedPayments = $this->getRequest()->getParam('programmedPayment');
     $actualPayments = ActualPaymentQuery::create()->innerJoinFile()->addColumn('File.*')->whereAdd(ActualPayment::ID_PROGRAMMED_PAYMENT, $programmedPayments)->fetchAll();
     $zip = new ZipArchive();
     $tempdir = sys_get_temp_dir();
     $filePath = tempnam($tempdir, md5(rand()));
     $fileName = "bankslip.zip";
     $result = $zip->open($filePath, ZipArchive::CREATE);
     if ($result === true) {
         foreach ($actualPayments as $actualPayment) {
             //     			    		print_r($actualPayment['content']);die;
             if (file_exists($actualPayment['content'])) {
                 $file = end(explode('/', $actualPayment['content']));
                 if (!$zip->addFile($actualPayment['content'], $file)) {
                     throw new Exception("A file cannot be added to the zip folder");
                 }
             }
         }
         $zip->close();
         if (false !== fopen($filePath, 'r')) {
             $fileBytes = readfile($filePath);
             if (!$fileBytes) {
                 throw new Exception('The file is completly empty');
             }
             header('Content-type: application/zip');
             header('Content-Disposition: attachment; filename="' . $fileName . '"');
             die;
         } else {
             throw new Exception('The ZIP file cannot be created');
         }
     } else {
         echo $zip->getStatusString();
         die;
         throw new Exception($result);
     }
 }