'_doForm', 'check' => '_doCheck', 'import' => '_doImport', 'send' => '_doSend', 'cancel' => 'doBack', ); protected $_serverIp = '127.0.0.1'; protected $_serverPortGenerate = '8766'; protected $_serverPortSend = '8767'; public function InvoiceImportGrid($Doc, $DBObj) { parent::DB_Grid(&$Doc, &$DBObj); } protected function _getForm() { $form = $this->_getBaseForm(); $form->addElement('select', 'source_type', 'Source Type', array('exchange' => 'Exchange Invoices', 'mncar' => 'MNCAR Invoices')); $form->addElement('file_db', 'file', 'CSV File'); $form->addElement('text', 'payment_terms', 'Payment Terms'); $form->addElement('text', 'invoice_title', 'Invoice Title'); $form->addElement('calendar', 'due_date', 'Due Date'); $form->addElement('hidden', 'action', 'check') ; return $form; } protected function _doForm() { if (!isset($_REQUEST['send']) || $_REQUEST['send'] != 'start') { $this->Doc->addContent(array( 'tpl' => 'Invoice/ready.tpl', 'count' => $this->DBObj->getReadyInvoicesCount(), 'invoices' => $this->DBObj->getSendQueue() )); } if ($this->DBObj->isGenerateOrderOpen()) { $this->Doc->addContent(array( 'tpl' => 'Invoice/generationnotfinished.tpl', 'count' => $this->DBObj->getReadyInvoicesCount(), 'invoices' => $this->DBObj->getGenerateQueue() )); $sql = ""; } else { $form = $this->_getForm(); $form->setDefaults(array('payment_terms' => 'Due Upon Receipt', 'due_date' => date('Y-m-d', mktime()))); $form->exec(); } } protected function _doCheck() { if ($this->DBObj->isGenerateOrderOpen()) { $this->Doc->addContent(array( 'tpl' => 'Invoice/generationnotfinished.tpl', 'count' => $this->DBObj->getReadyInvoicesCount() )); } else { $form = $this->_getForm(); if (is_uploaded_file($_FILES['file']['tmp_name']['file'])) { copy($_FILES['file']['tmp_name']['file'], BASE_PATH . '/tmp/' . basename($_FILES['file']['tmp_name']['file']) . '.csv'); } chmod(BASE_PATH . '/tmp/' . basename($_FILES['file']['tmp_name']['file']) . '.csv', 0666); $this->DBObj->loadData(BASE_PATH . '/tmp/' . basename($_FILES['file']['tmp_name']['file']) . '.csv'); $checkResult = $this->DBObj->check($form->exportValue('source_type')); $this->Doc->addContent(array( 'tpl' => 'Invoice/Import/check.tpl', 'result' => $checkResult )); Session::setData('invoiceimport', 'file', BASE_PATH . '/tmp/' . basename($_FILES['file']['tmp_name']['file']) . '.csv'); Session::setData('invoiceimport', 'source', $form->exportValue('source_type')); Session::setData('invoiceimport', 'invoice_title', $form->exportValue('invoice_title')); Session::setData('invoiceimport', 'payment_terms', $form->exportValue('payment_terms')); Session::setData('invoiceimport', 'due_date', $form->exportValue('due_date')); } } protected function _doImport() { if (!$this->DBObj->isGenerateOrderOpen()) { $this->DBObj->loadData(Session::getData('invoiceimport', 'file')); unlink(Session::getData('invoiceimport', 'file')); $this->DBObj->import(Session::getData('invoiceimport', 'source')); $errno = ''; $errstr = ''; $fp = fsockopen('tcp://' . $this->_serverIp, $this->_serverPortGenerate, $errno, $errstr, 2); if (!$fp) { Session::setData($this->_getPage4SaveMessage(), 'error', 'Connection failed: tcp://' . $this->_serverIp . ':' . $this->_serverPortGenerate); } else { //Session::setData($this->_getPage4SaveMessage(), 'msg', 'Invoice generating process started'); } $dueDate = Session::getData('invoiceimport', 'due_date'); /*vdie(BASE_URL . '/_invoice-generate?payment_terms=' . Session::getData('invoiceimport', 'payment_terms') . '&invoice_title=' . Session::getData('invoiceimport', 'invoice_title') . '&due_date_year=' . $dueDate['Y'] . '&due_date_month=' . $dueDate['M'] . '&due_date_day=' . $dueDate['d']);*/ $ret = fwrite($fp, 'START ' . BASE_URL . '/_invoice-generate?payment_terms=' . urlencode(Session::getData('invoiceimport', 'payment_terms')) . '&invoice_title=' . urlencode(Session::getData('invoiceimport', 'invoice_title')) . '&due_date_year=' . (int)$dueDate['Y'] . '&due_date_month=' . (int)$dueDate['M'] . '&due_date_day=' . (int)$dueDate['d']); fclose($fp); } $this->doBack(); } protected function _doSend() { $errno = ''; $errstr = ''; $fp = fsockopen('tcp://' . $this->_serverIp, $this->_serverPortSend, $errno, $errstr, 2); if (!$fp) { Session::setData($this->_getPage4SaveMessage(), 'error', 'Connection failed: tcp://' . $this->_serverIp . ':' . $this->_serverPortSend); } else { $this->setBackUrl(BASE_URL . '/' . CURR_PAGE . '?send=start'); Session::setData($this->_getPage4SaveMessage(), 'msg', 'Invoice sending process started'); } $ret = fwrite($fp, 'START ' . BASE_URL . '/_invoice-send'); fclose($fp); $this->doBack(); } } ?>