Esempio n. 1
0
 public function saveAction()
 {
     $this->_helper->getHelper('layout')->disableLayout();
     $this->_helper->viewRenderer->setRender('pdf');
     $id = $this->_getParam('id', 0);
     $locale = Zend_Registry::get('Zend_Locale');
     $creditnoteDb = new Sales_Model_DbTable_Creditnote();
     $creditnote = $creditnoteDb->getCreditnote($id);
     if ($creditnote['templateid']) {
         $templateDb = new Application_Model_DbTable_Template();
         $template = $templateDb->getTemplate($creditnote['templateid']);
         if ($template['filename']) {
             $this->_helper->viewRenderer->setRender($template['filename']);
         }
         $this->view->template = $template;
     }
     $positions = $this->getPositions($id);
     if (!$creditnote['creditnoteid']) {
         //Get latest creditnote Id
         $latestCreditnote = $creditnoteDb->fetchRow($creditnoteDb->select()->where('clientid = ?', $this->_user['clientid'])->order('creditnoteid DESC')->limit(1));
         //Set new creditnote Id
         $newCreditnoteId = $latestCreditnote['creditnoteid'] + 1;
         $creditnoteDb->saveCreditnote($id, $newCreditnoteId, $this->_date, 105, $this->_date, $this->_user['id']);
         $creditnote = $creditnoteDb->getCreditnote($id);
         //Update item data
         if (count($positions)) {
             $itemsDb = new Items_Model_DbTable_Item();
             foreach ($positions as $position) {
                 $item = $itemsDb->fetchRow($itemsDb->select()->where('sku = ?', $position['sku']));
                 if ($item) {
                     $quantity = $item->quantity + $position->quantity;
                     $itemsDb->quantityItem($item->id, $quantity, $this->_date, $this->_user['id']);
                 }
             }
         }
     }
     if (count($positions)) {
         foreach ($positions as $position) {
             $precision = floor($position->quantity) == $position->quantity ? 0 : 2;
             $position->total = $this->_currency->toCurrency($position->price * $position->quantity);
             $position->price = $this->_currency->toCurrency($position->price);
             $position->quantity = Zend_Locale_Format::toNumber($position->quantity, array('precision' => $precision, 'locale' => Zend_Registry::get('Zend_Locale')));
         }
         $creditnote['taxes'] = $this->_currency->toCurrency($creditnote['taxes']);
         $creditnote['subtotal'] = $this->_currency->toCurrency($creditnote['subtotal']);
         $creditnote['total'] = $this->_currency->toCurrency($creditnote['total']);
         if ($creditnote['taxfree']) {
             $creditnote['taxrate'] = Zend_Locale_Format::toNumber(0, array('precision' => 2, 'locale' => $locale));
         } else {
             $creditnote['taxrate'] = Zend_Locale_Format::toNumber($positions[0]->taxrate, array('precision' => 2, 'locale' => $locale));
         }
     }
     $this->view->creditnote = $creditnote;
     $this->view->positions = $positions;
     $this->view->footers = $this->_helper->Footer->getFooters($creditnote['templateid'], $this->_user['clientid']);
 }