/**
  * This action create a proforma invoice and link with respective budgets and requisition entries
  *
  * @author Erick Guevara Martinez
  */
 public function createAction()
 {
     $proformaParams["proforma_invoice_number"] = $this->getRequest()->getParam("proforma_invoice_number", "");
     $proformaParams["date"] = $this->getRequest()->getParam("required_date", "");
     $proformaParams["delivery_estimate_date"] = $this->getRequest()->getParam("required_date", "");
     $proformaParams["id_forwarder"] = $this->getRequest()->getParam("id_forwarder", "");
     $proformaParams["id_supplier"] = $this->getRequest()->getParam("id_supplier", "");
     $proformaParams["id_port_custom"] = $this->getRequest()->getParam("id_port_custom", "");
     $proformaParams["id_courtyard"] = $this->getRequest()->getParam("id_courtyard", "");
     $proformaParams["idCurrencyPI"] = $this->getRequest()->getParam("idCurrencyPI", "");
     $supplier = SapSupplierQuery::create()->findByPKOrThrow($proformaParams["id_supplier"], $this->i18n->_("The Supplier with id {$proformaParams["id_supplier"]} does not exist"));
     $date = \Zend_Date::now();
     $accessRole = $this->getUser()->getAccessRole();
     $proformaInvoice = ProformaInvoiceFactory::createFromArray(array("id_proforma_invoice" => 0, "folio" => $proformaParams["proforma_invoice_number"], "groupnum" => $supplier->getGroupnum(), "id_courtyard" => $proformaParams["id_courtyard"], "id_currency" => $proformaParams["idCurrencyPI"], "id_supplier" => $proformaParams["id_supplier"], "date" => $proformaParams["date"], "delivery_estimate_date" => $proformaParams["delivery_estimate_date"], "creation_date" => $date->get("yyyy-MM-dd"), "status" => ProformaInvoice::$Status["New"]));
     $proformaInvoiceEntries = new ProformaInvoiceEntryCollection();
     $proformaEntriesParams["parentKeys"] = $this->getRequest()->getParam("parentKeys", "");
     $proformaEntriesParams["warehouse_store"] = $this->getRequest()->getParam("warehouse_store", WarehouseStore::$defaultWarehouseStore);
     $arrayRequisitionEntries = array();
     $lineCount = 0;
     foreach ($proformaEntriesParams["parentKeys"] as $parentKey) {
         foreach ($parentKey["products"] as $key => $product) {
             $proformaInvoiceEntry = ProformaInvoiceEntryFactory::createFromArray(array("id_proforma_invoice_entry" => 0, "line" => $lineCount, "vis_order" => $lineCount, "id_product" => $product["product"], "id_warehouse_store" => $proformaEntriesParams["warehouse_store"], "lead_time" => $parentKey["lead_time"], "quantity" => $product["Quantity"], "status" => ProformaInvoiceEntry::$Status["New"], "price" => $parentKey["price"], "currency" => $proformaParams["idCurrencyPI"], "tax_code" => "W0", "requisition_ids" => implode('-', $product['entries'])));
             $proformaInvoiceEntries->append($proformaInvoiceEntry);
             $lineCount++;
             $arrayRequisitionEntries = array_merge($arrayRequisitionEntries, $product["entries"]);
         }
     }
     $sapConnectionClient = new SAPConnectorClient();
     $result = $sapConnectionClient->purchaseQuotation($proformaInvoice, $proformaInvoiceEntries);
     if ($result->purchaseQuotationResult->code == 200) {
         try {
             $this->getFileCatalog()->beginTransaction();
             $proformaInvoice->setIdProformaInvoice($result->purchaseQuotationResult->id);
             while ($proformaInvoiceEntry = $proformaInvoiceEntries->read()) {
                 $proformaInvoiceEntry->setIdProformaInvoice($result->purchaseQuotationResult->id);
                 $this->newEntryLogForCreate($proformaInvoiceEntry);
             }
             $fileUploader = new FileUploader("proforma_invoice_file");
             $filePath = "files/proformas/" . $result->purchaseQuotationResult->id;
             $uploadPath = 'public/' . $filePath;
             $fileUploader->saveFile($uploadPath, false);
             // Create the file record in database
             $file = new File();
             $file->setType(File::$typeFile["typeFile"])->setContent($filePath . $fileUploader->getFileName());
             $this->getFileCatalog()->create($file);
             $proformaInvoiceFile = ProformaInvoiceFileFactory::createFromArray(array(ProformaInvoiceFile::ID_FILE => $file->getIdFile(), ProformaInvoiceFile::ID_PROFORMA_INVOICE => $result->purchaseQuotationResult->id, ProformaInvoiceFile::TYPE => ProformaInvoiceFile::$Types["PINew"]));
             $this->getProformaInvoiceFileCatalog()->create($proformaInvoiceFile);
             // Create Payment terms by Proforma invoice Line
             // Se consultan las entries debido a que hasta el momento no tenian id
             $proformaInvoiceEntries = ProformaInvoiceEntryQuery::create()->whereAdd(ProformaInvoiceEntry::ID_PROFORMA_INVOICE, $result->purchaseQuotationResult->id)->find();
             //die("<pre>".print_r($proformaInvoiceEntries, true));
             $events = EventQuery::create()->addColumns(array("Event2PaymentTerm.*", "Event.*"))->innerJoinPaymentTerm()->whereAdd("Event2PaymentTerm." . PaymentTerm::ID_PAYMENT_TERM, $supplier->getIdPaymentTerm(), BaseQuery::EQUAL)->fetchAll();
             foreach ($events as $event) {
                 $proformaInvoiceEntries->rewind();
                 while ($proformaInvoiceEntries->valid()) {
                     $proformaInvoiceEntry = $proformaInvoiceEntries->read();
                     $proformaInvoicePaymentTerm = ProformaInvoicePaymentTermFactory::createFromArray(array(ProformaInvoicePaymentTerm::ID_PROFORMA_INVOICE => $proformaInvoiceEntry->getIdProformaInvoice(), ProformaInvoicePaymentTerm::ID_PROFORMA_INVOICE_ENTRY => $proformaInvoiceEntry->getIdProformaInvoiceEntry(), ProformaInvoicePaymentTerm::ID_EVENT => $event["id_event"], ProformaInvoicePaymentTerm::LINE => $proformaInvoiceEntry->getVisOrder(), ProformaInvoicePaymentTerm::PIECES => $proformaInvoiceEntry->getQuantity(), ProformaInvoicePaymentTerm::PAYMENT => $proformaInvoiceEntry->getTotalImport() * $event["percent"] / 100, ProformaInvoicePaymentTerm::CURRENCY => $proformaInvoiceEntry->getCurrency(), ProformaInvoicePaymentTerm::STATUS => ProformaInvoicePaymentTerm::$Status["Active"]));
                     $this->getProformaInvoicePaymentTermCatalog()->create($proformaInvoicePaymentTerm);
                 }
             }
             // Close requisition entries and requisition
             $requisitionEntries = RequisitionEntryQuery::create()->whereAdd(RequisitionEntry::ID_REQUISITION_ENTRY, $arrayRequisitionEntries, RequisitionEntryQuery::IN)->find();
             $requisitions = RequisitionQuery::create()->whereAdd(Requisition::ID_REQUISITION, $requisitionEntries->getDistinctRequisitions(), RequisitionQuery::IN)->find();
             while ($requisitionEntries->valid()) {
                 $requisitionEntry = $requisitionEntries->read();
                 $requisitionEntry->setStatus(RequisitionEntry::$Status["Closed"]);
                 $this->getRequisitionEntryCatalog()->update($requisitionEntry);
                 $requisition = $requisitions->getByPK($requisitionEntry->getIdRequisition());
                 $requisitionEntriesByRequisition = RequisitionEntryQuery::create()->whereAdd(RequisitionEntry::ID_REQUISITION, $requisition->getIdRequisition(), RequisitionEntryQuery::EQUAL)->whereAdd(RequisitionEntry::ID_REQUISITION_ENTRY, $requisitionEntry->getIdRequisitionEntry(), RequisitionEntryQuery::NOT_EQUAL)->find();
                 if ($requisitionEntriesByRequisition->checkClosed()) {
                     $requisition->setStatus(Requisition::$Status["Closed"]);
                     $this->getRequisitionCatalog()->update($requisition);
                 }
             }
             $keyMotherSupplierBudgets = KeyMotherSupplierBudgetQuery::create()->innerJoinRequisitionEntry()->whereAdd("RequisitionEntry." . RequisitionEntry::ID_REQUISITION_ENTRY, $requisitionEntries->getPrimaryKeys(), KeyMotherSupplierBudgetQuery::IN)->find();
             while ($keyMotherSupplierBudgets->valid()) {
                 $budget = $keyMotherSupplierBudgets->read();
                 $budget->setStatus(KeyMotherSupplierBudget::$Status["Inactive"]);
                 $this->getKeyMotherSupplierBudgetCatalog()->update($budget);
             }
             $this->newLogForCreate($proformaInvoice, $proformaInvoiceEntries);
             $this->getFileCatalog()->commit();
             $this->setFlash('ok', $this->i18n->_("The Proforma Invoice was created with id {$result->purchaseQuotationResult->id}."));
         } catch (Exception $e) {
             $this->getFileCatalog()->rollBack();
             $this->setFlash('error', $this->i18n->_($e->getMessage()));
         }
     } else {
         $this->setFlash('error', $result->purchaseQuotationResult->code . " - " . $result->purchaseQuotationResult->message);
     }
     $this->_redirect('proforma-invoice/list');
 }
 /**
  *
  * makeBean
  * @param array $resultset
  * @return \Application\Model\Bean\ProformaInvoiceEntry
  */
 protected function makeBean($resultset)
 {
     return ProformaInvoiceEntryFactory::createFromArray($resultset);
 }