Example #1
0
 public function editAction()
 {
     $request = $this->getRequest();
     $id = $this->_getParam('id', 0);
     $activeTab = $request->getCookie('tab', null);
     $creditnoteDb = new Sales_Model_DbTable_Creditnote();
     $creditnote = $creditnoteDb->getCreditnote($id);
     if ($creditnote['creditnoteid']) {
         $this->_helper->redirector->gotoSimple('view', 'creditnote', null, array('id' => $id));
     } elseif ($this->isLocked($creditnote['locked'], $creditnote['lockedtime'])) {
         if ($request->isPost()) {
             header('Content-type: application/json');
             $this->_helper->viewRenderer->setNoRender();
             $this->_helper->getHelper('layout')->disableLayout();
             echo Zend_Json::encode(array('message' => $this->view->translate('MESSAGES_LOCKED')));
         } else {
             $this->_flashMessenger->addMessage('MESSAGES_LOCKED');
             $this->_helper->redirector('index');
         }
     } else {
         $creditnoteDb->lock($id, $this->_user['id'], $this->_date);
         $form = new Sales_Form_Creditnote();
         $options = $this->_helper->Options->getOptions($form, $this->_user['clientid']);
         //Get contact
         if ($creditnote['contactid']) {
             $contactDb = new Contacts_Model_DbTable_Contact();
             $contact = $contactDb->getContact($creditnote['contactid']);
             //Phone
             $phoneDb = new Contacts_Model_DbTable_Phone();
             $contact['phone'] = $phoneDb->getPhone($creditnote['contactid']);
             //Email
             $emailDb = new Contacts_Model_DbTable_Email();
             $contact['email'] = $emailDb->getEmail($creditnote['contactid']);
             //Internet
             $internetDb = new Contacts_Model_DbTable_Internet();
             $contact['internet'] = $internetDb->getInternet($creditnote['contactid']);
             $this->view->contact = $contact;
         }
         if ($request->isPost()) {
             header('Content-type: application/json');
             $this->_helper->viewRenderer->setNoRender();
             $this->_helper->getHelper('layout')->disableLayout();
             $data = $request->getPost();
             $element = key($data);
             if (isset($form->{$element}) && $form->isValidPartial($data)) {
                 $data['contactperson'] = $this->_user['name'];
                 $data['modified'] = $this->_date;
                 $data['modifiedby'] = $this->_user['id'];
                 if (isset($data['taxfree'])) {
                     $calculations = $this->_helper->Calculate($id, $this->_currency, $this->_date, $this->_user['id'], $data['taxfree']);
                     $data['subtotal'] = $calculations['subtotal'];
                     $data['taxes'] = $calculations['taxes'];
                     $data['total'] = $calculations['total'];
                 }
                 $creditnoteDb->updateCreditnote($id, $data);
                 echo Zend_Json::encode($creditnoteDb->getCreditnote($id));
             } else {
                 echo Zend_Json::encode(array('message' => $this->view->translate('MESSAGES_FORM_IS_INVALID')));
             }
         } else {
             if ($id > 0) {
                 $data = $creditnote;
                 if ($creditnote['contactid']) {
                     $data['contactinfo'] = $contact['info'];
                     $form->contactinfo->setAttrib('data-id', $contact['id']);
                     $form->contactinfo->setAttrib('data-controller', 'contact');
                     $form->contactinfo->setAttrib('data-module', 'contacts');
                     $form->contactinfo->setAttrib('readonly', null);
                 }
                 $form->populate($data);
                 //Toolbar
                 $toolbar = new Sales_Form_Toolbar();
                 $toolbar->state->setValue($data['state']);
                 $toolbarPositions = new Sales_Form_ToolbarPositions();
                 $this->view->form = $form;
                 $this->view->activeTab = $activeTab;
                 $this->view->toolbar = $toolbar;
                 $this->view->toolbarPositions = $toolbarPositions;
             }
         }
     }
     $this->view->messages = $this->_flashMessenger->getMessages();
 }