/**
  * This action convert a requisition to purchase order
  *
  * @author Erick Guevara Mart�nez
  * @return array
  * Created October 15th, 2012
  */
 public function createAction()
 {
     if ($this->getRequest()->isPost()) {
         //die("<pre>".print_r($this->getRequest()->getParams(), true));
         $folio = $this->getRequest()->getParam("folio");
         $comments = $this->getRequest()->getParam("comments");
         $lines = $this->getRequest()->getParam("line");
         foreach ($lines as $line) {
             if ($line["checked"]) {
                 $idProformaInvoice[] = $line["id_proforma_invoice"];
             }
         }
         $proformasInvoice = ProformaInvoiceQuery::create()->whereAdd(ProformaInvoice::ID_PROFORMA_INVOICE, $idProformaInvoice, ProformaInvoiceQuery::IN)->find();
         $proformaInvoiceEntries = ProformaInvoiceEntryQuery::create()->whereAdd(ProformaInvoiceEntry::ID_PROFORMA_INVOICE, $proformasInvoice->getPrimaryKeys(), ProformaInvoiceEntryQuery::IN)->whereAdd(ProformaInvoiceEntry::STATUS, ProformaInvoiceEntry::$Status["End Production"])->find();
         $supplier = SapSupplierQuery::create()->findByPKOrThrow($proformasInvoice->current()->getIdSupplier(), $this->i18n->_("The Supplier with id {$proformasInvoice->current()->getIdSupplier()} does not exist"));
         $date = \Zend_Date::now();
         $purchaseOrder = PurchaseOrderFactory::createFromArray(array('id_purchase_order' => "0", 'id_supplier' => $supplier->getCardCode(), 'purchase_order_date' => $date->get("yyyy-MM-dd"), 'delivery_estimate_date' => $date->get("yyyy-MM-dd"), 'invoice_number' => $folio, 'id_currency' => $proformasInvoice->current()->getIdCurrency(), 'comments' => $comments, 'groupnum' => $supplier->getGroupnum(), 'status' => PurchaseOrder::$Status["New"], 'id_courtyard' => $proformasInvoice->current()->getIdCourtyard(), 'id_folio_import' => -1));
         $purchaseOrderEntries = new PurchaseOrderEntryCollection();
         $linesCount = 0;
         foreach ($lines as $line) {
             if ($line["checked"]) {
                 $proformaInvoiceEntry = $proformaInvoiceEntries->getByPK($line["line"]);
                 $purchaseOrderEntry = PurchaseOrderEntryFactory::createFromArray(array('id_purchase_order_entry' => "0", 'line' => $linesCount, "vis_order" => $linesCount, "id_product" => $proformaInvoiceEntry->getIdProduct(), "quantity" => $line["quantity"], "price" => $proformaInvoiceEntry->getPrice(), "id_warehouse_store" => $proformaInvoiceEntry->getIdWarehouseStore(), "tax_code" => $proformaInvoiceEntry->getTaxCode(), 'lead_time' => $proformaInvoiceEntry->getLeadTime(), 'status' => PurchaseOrderEntry::$Status["New"], 'base_line' => $proformaInvoiceEntry->getLine(), 'base_entry' => $proformaInvoiceEntry->getIdProformaInvoice()));
                 $purchaseOrderEntries->append($purchaseOrderEntry);
                 $linesCount++;
             }
         }
         $SAPConnectionClient = new SAPConnectorClient();
         $result = $SAPConnectionClient->purchaseOrder($purchaseOrder, $purchaseOrderEntries);
         if ($result->purchaseOrderResult->code == 200) {
             try {
                 $this->getPurchaseOrderCommercialInvoiceCatalog()->beginTransaction();
                 $filePath = "files/purchase-order/" . $result->purchaseOrderResult->id;
                 $uploadPath = 'public/' . $filePath;
                 $fileUploader = new FileUploader("commercial_invoice_file");
                 $fileUploader->saveFile($uploadPath, false);
                 $commercialInvoiceFile = PurchaseOrderFileFactory::createFromArray(array(PurchaseOrderFile::CONTENT => $filePath . $fileUploader->getFileName(), PurchaseOrderFile::TYPE => File::$typeFile["typeFile"], PurchaseOrderFile::DOCUMENT_TYPE => PurchaseOrderFile::$DocumentType["CommercialInvoice"], PurchaseOrderFile::ID_PURCHASE_ORDER => $result->purchaseOrderResult->id));
                 $this->getPurchaseOrderFileCatalog()->create($commercialInvoiceFile);
                 if ($_FILES["packing_list_file"]["name"] != "") {
                     $fileUploader = new FileUploader("packing_list_file");
                     $fileUploader->saveFile($uploadPath, false);
                     $packingListFile = PurchaseOrderFileFactory::createFromArray(array(PurchaseOrderFile::CONTENT => $filePath . $fileUploader->getFileName(), PurchaseOrderFile::TYPE => File::$typeFile["typeFile"], PurchaseOrderFile::DOCUMENT_TYPE => PurchaseOrderFile::$DocumentType["PackingList"], PurchaseOrderFile::ID_PURCHASE_ORDER => $result->purchaseOrderResult->id));
                     $this->getPurchaseOrderFileCatalog()->create($packingListFile);
                 }
                 $purchaseOrderCommercialInvoice = PurchaseOrderCommercialInvoiceFactory::createFromArray(array(PurchaseOrderCommercialInvoice::ID_PURCHASE_ORDER => $result->purchaseOrderResult->id, PurchaseOrderCommercialInvoice::COMMERCIAL_INVOICE_NUMBER => $folio));
                 $this->getPurchaseOrderCommercialInvoiceCatalog()->create($purchaseOrderCommercialInvoice);
                 $purchaseOrder = PurchaseOrderQuery::create()->findByPKOrThrow($purchaseOrderCommercialInvoice->getIdPurchaseOrder(), $this->i18n->_("The purchase order with id{$purchaseOrderEntry->getIdPurchaseOrder()} does not exist"));
                 $purchaseOrderEntries = PurchaseOrderEntryQuery::create()->whereAdd(PurchaseOrderEntry::ID_PURCHASE_ORDER, $purchaseOrderCommercialInvoice->getIdPurchaseOrder())->find();
                 $purchaseOrderPaymentManager = new PurchaseOrderPaymentsManager();
                 $purchaseOrderPaymentManager->createPaymentTerms($purchaseOrderEntries);
                 $docNum = PurchaseOrderQuery::create()->findByPK($result->purchaseOrderResult->id)->getSapDocumentNumber();
                 $this->setFlash('ok', $this->i18n->_("The purchase Order with DocNum {$docNum} was saved correctly"));
                 $this->newLogForCreate($purchaseOrder);
                 $this->newLogForUploadCI($purchaseOrder);
                 $this->getEventDispatcherService()->dispatchPurchaseOrderEvent($purchaseOrder, PurchaseOrderEvent::PURCHASEORDER_INSPECTION_PASSED, $purchaseOrderEntries);
                 $this->getEventDispatcherService()->dispatchPurchaseOrderEvent($purchaseOrder, PurchaseOrderEvent::PURCHASEORDER_CREATED, $purchaseOrderEntries);
                 $this->getPurchaseOrderCommercialInvoiceCatalog()->commit();
             } catch (Exception $e) {
                 $this->getPurchaseOrderCommercialInvoiceCatalog()->rollBack();
                 $this->setFlash('error', $this->i18n->_($e->getMessage()));
             }
         } else {
             $this->setFlash('error', $result->purchaseOrderResult->code . " - " . $result->purchaseOrderResult->message);
         }
     }
     $this->_redirect('purchase-order/list');
 }
 public function saveProgrammedPaymentAction()
 {
     $id = $this->getRequest()->getParam('id');
     $paymentStatus = $this->getRequest()->getParam('payment_status');
     $folioImport = FolioImportQuery::create()->findByPK($id);
     $sapConnector = new SAPConnectorClient();
     $redir = $this->getBaseUrl() . '/' . $this->getRequest()->getParam('controller') . '/list';
     $programmedPayments = $this->getRequest()->getParam('payment-term');
     $date = \Zend_Date::now();
     $results = array();
     $this->getCatalog('ProgrammedPaymentCatalog')->beginTransaction();
     foreach ($programmedPayments as $programmedPayment) {
         if (empty($programmedPayment['id_folios_import_programmed_payment'])) {
             $supplier = SapSupplierQuery::create()->findByPK($programmedPayment['id_company']);
             $servicePurchaseOrder = PurchaseOrderFactory::createFromArray(array('id_purchase_order' => "0", 'id_supplier' => $supplier->getCardCode(), 'purchase_order_date' => $date->get("yyyy-MM-dd"), 'delivery_estimate_date' => $date->get("yyyy-MM-dd"), 'invoice_number' => 'Pending', 'id_currency' => $programmedPayment['id_currency'], 'comments' => "Folio Import Service Purchase Order", 'groupnum' => $supplier->getGroupnum(), 'status' => PurchaseOrder::$Status["NewService"], 'id_courtyard' => -1, 'id_folio_import' => $programmedPayment['id_folio_import']));
             $purchaseOrderEntries = new PurchaseOrderEntryCollection();
             $linesCount = 0;
             $purchaseOrderEntry = PurchaseOrderEntryFactory::createFromArray(array('line' => $linesCount, "vis_order" => $linesCount, "id_product" => $programmedPayment['concept'], "quantity" => 1, "price" => $programmedPayment['ammount'], "id_warehouse_store" => '9999', "tax_code" => 'W3', 'lead_time' => -1, 'status' => PurchaseOrderEntry::$Status["ServiceEntry"], 'base_line' => -1, 'base_entry' => -1));
             $purchaseOrderEntries->append($purchaseOrderEntry);
             $result = $sapConnector->servicePurchaseOrder($servicePurchaseOrder, $purchaseOrderEntries);
             $results[] = array('success' => $result->purchaseOrderResult->success, 'id_purchase_order' => $result->purchaseOrderResult->id, 'id_folio_import' => $id, 'message' => $result->purchaseOrderResult->message);
             if ($result->purchaseOrderResult->success) {
                 $programmedPayment['doc_entry'] = $result->purchaseOrderResult->id;
                 $idFolioImportProgrammedPayment = $programmedPayment['id_folios_import_programmed_payment'];
                 if ($idFolioImportProgrammedPayment) {
                     if ($programmedPayment['status']) {
                         unset($programmedPayment['status']);
                         $folioImportProgrammedPayment = FolioImportProgrammedPaymentFactory::createFromArray($programmedPayment);
                         $folioImportProgrammedPayment->setType(ProgrammedPayment::$TypePayment['Folio']);
                         $this->getCatalog('FolioImportProgrammedPaymentCatalog')->update($folioImportProgrammedPayment);
                     }
                 } else {
                     $folioImportProgrammedPayment = FolioImportProgrammedPaymentFactory::createFromArray($programmedPayment);
                     $folioImportProgrammedPayment->setType(ProgrammedPayment::$TypePayment['Folio']);
                     $folioImportProgrammedPayment->setStatus(ProgrammedPayment::$TypeStatus['Pending']);
                     $folioImportProgrammedPayment->setIdFolioImport($id);
                     $folioImportProgrammedPayment->setFolio($folioImport->getFolio());
                     $this->getCatalog('FolioImportProgrammedPaymentCatalog')->create($folioImportProgrammedPayment);
                 }
             }
         }
     }
     try {
         if ($this->setResponseMessage($results) && $paymentStatus == FolioImport::$PaymentStatus['Inactive']) {
             $this->setFlash('warning', 'The payments were closed');
             $purchaseOrderQuery = PurchaseOrderQuery::create();
             $purchaseOrders = $purchaseOrderQuery->whereAdd(PurchaseOrder::ID_FOLIO_IMPORT, $id)->whereAdd(PurchaseOrder::STATUS, PurchaseOrder::$Status['NewService'], PurchaseOrderQuery::NOT_EQUAL)->find();
             $result = $sapConnector->createLandedCost($purchaseOrders);
         } else {
             $this->setFlash('warning', 'The payments were NOT closed');
         }
     } catch (Exception $e) {
         $this->getCatalog("ProgrammedPaymentCatalog")->rollBack();
         $this->setFlash('error', $this->i18n->_($e->getMessage()));
     }
     $this->newLogForPaymentAdded($folioImport);
     $this->getCatalog('ProgrammedPaymentCatalog')->commit();
     $this->_redirect('folio-import/list');
 }
 /**
  *
  * makeBean
  * @param array $resultset
  * @return \Application\Model\Bean\PurchaseOrder
  */
 protected function makeBean($resultset)
 {
     return PurchaseOrderFactory::createFromArray($resultset);
 }