/**
  * @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íne<
  * @param PurchaseOrderEntryCollection $entries
  * @return array
  */
 public function getCondensedPayments(PurchaseOrder $purchaseOrder)
 {
     $purchaseOrderPaymentTerms = PurchaseOrderPaymentTermQuery::create()->whereAdd(PurchaseOrderPaymentTerm::ID_PURCHASE_ORDER, $purchaseOrder->getIdPurchaseOrder())->find();
     $purchaseOrderProgrammedPayments = PurchaseOrderProgrammedPaymentQuery::create()->whereAdd(PurchaseOrderProgrammedPayment::ID_PURCHASE_ORDER_PAYMENT_TERM, $purchaseOrderPaymentTerms->getPrimaryKeys())->find();
     $events = EventQuery::create()->whereAdd(Event::ID_EVENT, $purchaseOrderPaymentTerms->getEventIds())->find();
     $payments = array();
     while ($purchaseOrderPaymentTerms->valid()) {
         $purchaseOrderPaymentTerm = $purchaseOrderPaymentTerms->read();
         $event = $events->getByPK($purchaseOrderPaymentTerm->getIdEvent());
         $purchaseOrderProgrammedPayments->filterByPurchaseOrderPaymentTerm($purchaseOrderPaymentTerm);
         $payments[$event->getIdEvent()]["event"] = $event->getName();
         $payments[$event->getIdEvent()]["idEvent"] = $event->getIdEvent();
         $payments[$event->getIdEvent()]["payment"] += $purchaseOrderPaymentTerm->getAmount();
         $payments[$event->getIdEvent()]["currency"] = $purchaseOrderPaymentTerm->getCurrency();
         if (!$purchaseOrderProgrammedPayments->isEmpty()) {
             $payments[$event->getIdEvent()]["noEdit"] = true;
         }
     }
     return $payments;
 }
 /**
  *
  *
  * @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;
 }
 /**
  * 
  * 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);
     }
 }
 /**
  * 
  * @param int $idPurchaseOrder
  * @param int $idEvent
  */
 private function getPurchaseInformation($idPurchaseOrder, $idEvent)
 {
     $purchaseOrder = PurchaseOrderQuery::create()->findByPKOrThrow($idPurchaseOrder, $this->i18n->_("PurchaseOrder with id {$idPurchaseOrder} not found."));
     $supplier = SapSupplierQuery::create()->findByPKOrThrow($purchaseOrder->getIdSupplier(), $this->i18n->_("SapSupplier with id {$purchaseOrder->getIdSupplier()} not found."));
     $event = EventQuery::create()->findByPKOrThrow($idEvent, $this->i18n->_("Event with id {$idEvent} not found."));
     $purchaseOrderEntries = PurchaseOrderEntryQuery::create()->whereAdd(PurchaseOrderEntry::ID_PURCHASE_ORDER, $purchaseOrder->getIdPurchaseOrder())->find();
     $proformaInvoiceEntries = ProformaInvoiceEntryQuery::create()->whereAdd(ProformaInvoiceEntry::ID_PROFORMA_INVOICE, $purchaseOrderEntries->getDisctinctProformaInvoiceIds())->find();
     $purchasePayments = PurchaseOrderProgrammedPaymentQuery::create()->innerJoinProformaInvoicePaymentTerm()->whereAdd("PurchaseOrderProgrammedPayment." . PurchaseOrderProgrammedPayment::ID_PURCHASE_ORDER, $purchaseOrder->getIdPurchaseOrder())->whereAdd("ProformaInvoicePaymentTerm." . ProformaInvoicePaymentTerm::ID_EVENT, $event->getIdEvent())->find();
     $actualPayments = ActualPaymentQuery::create()->whereAdd(ActualPayment::ID_PROGRAMMED_PAYMENT, $purchasePayments->getProgrammedPaymentIds())->find();
     $response = array("docNum" => $purchaseOrder->getSapDocumentNumber(), "docEntry" => $purchaseOrder->getIdPurchaseOrder(), "createDate" => $purchaseOrder->getDeliveryEstimateDate(), "supplier" => $supplier->getCardCode(), "supplierName" => $supplier->getCardname(), "event" => $event->getName());
     while ($purchaseOrderEntries->valid()) {
         $purchaseOrderEntry = $purchaseOrderEntries->read();
         $proformaInvoiceEntry = $proformaInvoiceEntries->getByIdProformaInvoiceAndLine($purchaseOrderEntry->getBaseEntry(), $purchaseOrderEntry->getBaseLine());
         $programmedPayment = $purchasePayments->filterByPurchaseOrderEntry($purchaseOrderEntry)->getOne();
         $actualPayment = $actualPayments->filterByProgrammedPayment($programmedPayment);
         $response["entries"][] = array("line" => $purchaseOrderEntry->getLine(), "product" => $purchaseOrderEntry->getIdProduct(), "quantity" => $purchaseOrderEntry->getQuantity(), "price" => $purchaseOrderEntry->getPrice(), "currency" => $purchaseOrderEntry->getCurrency(), "requisitions" => implode($proformaInvoiceEntry->getRequisitionIdsToArray()), "PI_Line" => $purchaseOrderEntry->getBaseEntry() . "/" . $purchaseOrderEntry->getBaseLine(), "ammount" => $programmedPayment->getAmmount(), "credits" => $actualPayment->getTotalAmmount(), "pendingBalance" => $programmedPayment->getAmmount() - $actualPayment->getTotalAmmount());
     }
     return $response;
 }
Exemple #6
0
 public function createGenericPayment(PurchaseOrderEvent $event, $type)
 {
     $purchaseOrder = $event->getPurchaseOrder();
     //TODO: agregar arreglo de eventos a PurchaseOrderPaymentTerm
     $paymentTerms = PurchaseOrderPaymentTermQuery::create()->whereAdd(PurchaseOrderPaymentTerm::ID_EVENT, $type)->whereAdd(PurchaseOrderPaymentTerm::ID_PURCHASE_ORDER, $purchaseOrder->getIdPurchaseOrder())->find();
     try {
         $this->getPurchaseOrderProgrammedPaymentCatalog()->beginTransaction();
         while ($paymentTerms->valid()) {
             $paymentTerm = $paymentTerms->read();
             if (PurchaseOrderProgrammedPaymentQuery::create()->whereAdd(PurchaseOrderProgrammedPayment::ID_PURCHASE_ORDER_PAYMENT_TERM, $paymentTerm->getIdPurchaseOrderPaymentTerm())->find()->count() > 0) {
                 continue;
             }
             $payment = new PurchaseOrderProgrammedPayment();
             $payment->setIdPurchaseOrderPaymentTerm($paymentTerm->getIdPurchaseOrderPaymentTerm());
             $payment->setAmmount($paymentTerm->getAmount());
             $payment->setDueDate(\Zend_Date::now()->addDay(15)->get("YYYY-MM-dd"));
             //TODO: dias del cat�logo
             $payment->setIdCompany(SupplierQuery::create()->findByPK($purchaseOrder->getIdSupplier())->getIdCompany());
             $payment->setIdCurrency($purchaseOrder->getIdCurrency());
             $payment->setIdPurchaseOrder($purchaseOrder->getIdPurchaseOrder());
             $payment->setStatus(PurchaseOrderProgrammedPayment::$TypeStatus['Pending']);
             $payment->setType(PurchaseOrderProgrammedPayment::$TypePayment['PurchaseOrder']);
             $this->getPurchaseOrderProgrammedPaymentCatalog()->create($payment);
         }
         $this->getPurchaseOrderProgrammedPaymentCatalog()->commit();
     } catch (Exception $e) {
         $this->getPurchaseOrderProgrammedPaymentCatalog()->rollBack();
         throw $e;
     }
 }
 public function fixPaymentTermsAction()
 {
     $POPaymentTerms = PurchaseOrderPaymentTermQuery::create()->find();
     $this->getPurchaseOrderPaymentTermsCatalog()->beginTransaction();
     try {
         while ($POPaymentTerms->valid()) {
             $POPaymentTerm = $POPaymentTerms->read();
             $this->getPurchaseOrderPaymentTermsCatalog()->deleteById($POPaymentTerm->getIdPurchaseOrderPaymentTerm());
         }
         $purchaseOrderEntries = PurchaseOrderEntryQuery::create()->whereAdd(PurchaseOrderEntry::BASE_ENTRY, "", PurchaseOrderEntryQuery::IS_NOT_NULL)->whereAdd(PurchaseOrderEntry::BASE_LINE, "", PurchaseOrderEntryQuery::IS_NOT_NULL)->find();
         $purchaseOrderPaymentTerm = new PurchaseOrderPaymentsManager();
         $POPaymentTerms = $purchaseOrderPaymentTerm->createPaymentTerms($purchaseOrderEntries);
         $programmedPayments = PurchaseOrderProgrammedPaymentQuery::create()->find();
         $proformaInvoicePaymentTerms = ProformaInvoicePaymentTermQuery::create()->whereAdd(ProformaInvoicePaymentTerm::ID_PROFORMA_INVOICE_PAYMENT, $programmedPayments->getProformaInvoicePaymentTermIds())->find();
         while ($programmedPayments->valid()) {
             $programmedPayment = $programmedPayments->read();
             $proformaInvoicePaymentTerm = $proformaInvoicePaymentTerms->getByPK($programmedPayment->getIdProformaInvoicePaymentTerm());
             $POPaymentTerm = null;
             $POPaymentTerms->each(function (PurchaseOrderPaymentTerm $purchaseOrderPaymentTerm) use($programmedPayment, $proformaInvoicePaymentTerm, &$POPaymentTerm) {
                 if ($purchaseOrderPaymentTerm->getIdEvent() == $proformaInvoicePaymentTerm->getIdEvent() && $purchaseOrderPaymentTerm->getLine() == $programmedPayment->getLine() && $purchaseOrderPaymentTerm->getIdPurchaseOrder() == $programmedPayment->getIdPurchaseOrder()) {
                     $POPaymentTerm = $purchaseOrderPaymentTerm;
                 }
             });
             if ($POPaymentTerm instanceof PurchaseOrderPaymentTerm) {
                 $programmedPayment->setIdPurchaseOrderPaymentTerm($POPaymentTerm->getIdPurchaseOrderPaymentTerm());
                 $this->getPurchaseOrderProgrammedPaymentCatalog()->update($programmedPayment);
             }
         }
         $this->getPurchaseOrderPaymentTermsCatalog()->commit();
         die("fin");
     } catch (Exception $e) {
         $this->getPurchaseOrderPaymentTermsCatalog()->rollBack();
         die($e->getMessage());
     }
 }