/**
  * @author joseluis
  * @param int $idEvent
  * @return bool
  */
 public function getEditableByEvent($idEvent)
 {
     $proformaInvoicePaymentTerms = $this->filter(function (ProformaInvoicePaymentTerm $proformaInvoicePaymentTerm) use($idEvent) {
         if ($proformaInvoicePaymentTerm->getIdEvent() == $idEvent) {
             return $proformaInvoicePaymentTerm;
         }
     });
     while ($proformaInvoicePaymentTerm = $proformaInvoicePaymentTerms->read()) {
         if (ProformaInvoiceProgrammedPaymentQuery::create()->whereAdd(ProformaInvoiceProgrammedPayment::ID_PROFORMA_INVOICE_PAYMENT_TERM, $proformaInvoicePaymentTerm->getIdProformaInvoicePayment())->count()) {
             return false;
         }
         if (PurchaseOrderProgrammedPaymentQuery::create()->whereAdd(PurchaseOrderProgrammedPayment::ID_PROFORMA_INVOICE_PAYMENT_TERM, $proformaInvoicePaymentTerm->getIdProformaInvoicePayment())->count()) {
             return false;
         }
     }
     return true;
 }
 /**
  *
  *
  * @author Erick Guevara Martínez
  * @param ProgrammedPaymentCollection $payments
  * @return \Application\Model\Collection\ProformaInvoicePaymentTermCollection
  */
 private function getAvailablePaymentTerms(ProgrammedPaymentCollection $payments)
 {
     $availableProformaEvents = ProformaInvoiceProgrammedPaymentQuery::create()->innerJoinProformaInvoicePaymentTerm()->distinct()->addColumn("ProformaInvoicePaymentTerm." . ProformaInvoicePaymentTerm::ID_EVENT)->addColumn("ProformaInvoicePaymentTerm." . ProformaInvoicePaymentTerm::ID_PROFORMA_INVOICE)->whereAdd("ProformaInvoiceProgrammedPayment." . ProformaInvoiceProgrammedPayment::ID_PROGRAMMED_PAYMENT, $payments->getPrimaryKeys(), ProformaInvoiceProgrammedPaymentQuery::IN)->fetchAll();
     $availablePurchaseEvents = PurchaseOrderProgrammedPaymentQuery::create()->innerJoinProformaInvoicePaymentTerm()->distinct()->addColumn("ProformaInvoicePaymentTerm." . ProformaInvoicePaymentTerm::ID_EVENT)->addColumn("ProformaInvoicePaymentTerm." . ProformaInvoicePaymentTerm::ID_PROFORMA_INVOICE)->whereAdd("PurchaseOrderProgrammedPayment." . ProformaInvoiceProgrammedPayment::ID_PROGRAMMED_PAYMENT, $payments->getPrimaryKeys(), ProformaInvoiceProgrammedPaymentQuery::IN)->fetchAll();
     $availableEvents = array_merge($availableProformaEvents, $availablePurchaseEvents);
     if (!empty($availableEvents)) {
         $proformaInvoicePaymentTermQuery = ProformaInvoicePaymentTermQuery::create();
         $proformaInvoicePaymentTermQuery->where()->setOR();
         foreach ($availableEvents as $availableEvent) {
             $where = "(" . ProformaInvoicePaymentTerm::ID_PROFORMA_INVOICE . " = " . $availableEvent["id_proforma_invoice"] . " AND " . ProformaInvoicePaymentTerm::ID_EVENT . " = " . $availableEvent["id_event"] . ")";
             $proformaInvoicePaymentTermQuery->whereAdd("", null, null, $where, BaseQuery::AS_EXPRESSION);
         }
         $proformaPaymentTerms = $proformaInvoicePaymentTermQuery->find();
     } else {
         $proformaPaymentTerms = new ProformaInvoicePaymentTermCollection();
     }
     return $proformaPaymentTerms;
 }
 public function updatePaymentEntriesIdsAction()
 {
     $paymentTerms = ProformaInvoicePaymentTermQuery::create()->whereAdd(ProformaInvoicePaymentTerm::ID_PROFORMA_INVOICE_ENTRY, "", ProformaInvoicePaymentTermQuery::IS_NULL)->find();
     $proformaInvoiceEntries = ProformaInvoiceEntryQuery::create()->find();
     $proformaInvoiceProgrammedPayments = ProformaInvoiceProgrammedPaymentQuery::create()->whereAdd(ProformaInvoiceProgrammedPayment::ID_PROFORMA_INVOICE_ENTRY, "", ProformaInvoiceProgrammedPaymentQuery::IS_NULL)->find();
     $this->getProformaInvoicePaymentTermCatalog()->beginTransaction();
     try {
         while ($paymentTerms->valid()) {
             $paymentTerm = $paymentTerms->read();
             $proformaInvoiceEntry = $proformaInvoiceEntries->getByIdProformaInvoiceAndLine($paymentTerm->getIdProformaInvoice(), $paymentTerm->getLine());
             if ($proformaInvoiceEntry->getIdProformaInvoiceEntry() > 0) {
                 $paymentTerm->setIdProformaInvoiceEntry($proformaInvoiceEntry->getIdProformaInvoiceEntry());
                 $this->getProformaInvoicePaymentTermCatalog()->update($paymentTerm);
                 $programmedPayments = $proformaInvoiceProgrammedPayments->filterByPaymentTerm($paymentTerm);
                 while ($programmedPayments->valid()) {
                     $programmedPayment = $programmedPayments->read();
                     $programmedPayment->setIdProformaInvoiceEntry($proformaInvoiceEntry->getIdProformaInvoiceEntry());
                     $this->getProformaInvoiceProgrammedPaymentCatalog()->update($programmedPayment);
                 }
             }
         }
         $this->getProformaInvoicePaymentTermCatalog()->commit();
         die("FIN");
     } catch (Exception $e) {
         die("<pre>" . print_r($e, true));
     }
 }
 /**
  * 
  * @param int $idProformaInvoice
  * @param int $idEvent
  */
 private function getProformaInformation($idProformaInvoice, $idEvent)
 {
     $event = EventQuery::create()->findByPKOrThrow($idEvent, $this->i18n->_("Event with id {$idEvent} not found."));
     $proformaInvoice = ProformaInvoiceQuery::create()->findByPKOrThrow($idProformaInvoice, $this->i18n->_("ProformaInvoice with id {$idProformaInvoice} not found."));
     $supplier = SapSupplierQuery::create()->findByPKOrThrow($proformaInvoice->getIdSupplier(), $this->i18n->_("SapSupplier with id {$proformaInvoice->getIdSupplier()} not found."));
     $proformaInvoiceEntries = ProformaInvoiceEntryQuery::create()->whereAdd(ProformaInvoiceEntry::ID_PROFORMA_INVOICE, $proformaInvoice->getIdProformaInvoice())->find();
     if (count($proformaInvoiceEntries->getDistinctRequisitionEntryIdsToArray()) > 0) {
         $requisitionEntries = RequisitionEntryQuery::create()->whereAdd(RequisitionEntry::ID_REQUISITION_ENTRY, $proformaInvoiceEntries->getDistinctRequisitionEntryIdsToArray())->find();
     } else {
         $requisitionEntries = new RequisitionEntryCollection();
     }
     $paymentTerms = ProformaInvoicePaymentTermQuery::create()->whereAdd(ProformaInvoicePaymentTerm::ID_PROFORMA_INVOICE, $proformaInvoice->getIdProformaInvoice())->whereAdd(ProformaInvoicePaymentTerm::ID_EVENT, $event->getIdEvent())->find();
     $programmedPayments = ProformaInvoiceProgrammedPaymentQuery::create()->whereAdd("ProformaInvoiceProgrammedPayment." . ProformaInvoiceProgrammedPayment::ID_PROFORMA_INVOICE_PAYMENT_TERM, $paymentTerms->getPrimaryKeys())->find();
     $actualPayments = ActualPaymentQuery::create()->whereAdd(ActualPayment::ID_PROGRAMMED_PAYMENT, $programmedPayments->getProgrammedPaymentIds())->find();
     $response = array("docNum" => $proformaInvoice->getSapDocumentNumber(), "docEntry" => $proformaInvoice->getIdProformaInvoice(), "createDate" => $proformaInvoice->getDocDate(), "supplier" => $supplier->getCardCode(), "supplierName" => $supplier->getCardname(), "event" => $event->getName());
     while ($proformaInvoiceEntries->valid()) {
         $proformaInvoiceEntry = $proformaInvoiceEntries->read();
         $paymentTerm = $paymentTerms->filterByProformaInvoiceEntry($proformaInvoiceEntry)->getOne();
         $programmedPayment = $programmedPayments->filterByPaymentTerm($paymentTerm)->getOne();
         $actualPayment = $actualPayments->filterByProgrammedPayment($programmedPayment);
         $entryIds = $proformaInvoiceEntry->getRequisitionIdsToArray();
         if (count($entryIds) > 0) {
             $entries = $requisitionEntries->filter(function (RequisitionEntry $entry) use($entryIds) {
                 return in_array($entry->getIdRequisitionEntry(), $entryIds);
             });
         } else {
             $entries = new RequisitionEntryCollection();
         }
         $response["entries"][] = array("line" => $proformaInvoiceEntry->getLine(), "product" => $proformaInvoiceEntry->getIdProduct(), "quantity" => $proformaInvoiceEntry->getQuantity(), "price" => $proformaInvoiceEntry->getPrice(), "currency" => $proformaInvoiceEntry->getCurrency(), "requisitions" => $entries->isEmpty() ? "-" : implode($entries->getDistinctRequisitions()), "ammount" => $programmedPayment->getAmmount(), "credits" => $actualPayment->getTotalAmmount(), "pendingBalance" => $programmedPayment->getAmmount() - $actualPayment->getTotalAmmount());
     }
     return $response;
 }