public function doAction() { $id = $this->params('id'); $req = $this->getRequest(); if (!$req->isPost() && !$id) { return $this->redirect()->toRoute('venda'); } if ($req->isPost() && !$id) { $entity = new VendaEntity(); $venda = $this->getVendaMapper()->save($entity); if ($venda->valid()) { $id = $venda->getGeneratedValue(); return $this->redirect()->toRoute(null, array('action' => 'do', 'id' => $id)); } throw new \Exception('A entity não é válida!'); } $venda = $this->getVendaMapper()->fetchOne($id); if (!$venda) { return $this->redirect()->toRoute('venda'); } $entity = new VendaItemEntity(); $form = new VendaItemForm(); $form->bind($entity); $form->get('venda_id')->setValue($venda->getId()); $itens = $this->getVendaMapper()->fetchVendaItem($venda->getId()); $total = $this->getVendaMapper()->fetchVendaTotal($venda->getId())->toArray(); if (isset($total[0])) { $total = $total[0]; } else { $total['imposto'] = ''; $total['total'] = ''; } return array('venda' => $venda, 'form' => $form, 'id' => $id, 'itens' => $itens, 'total' => $total); }
public function indexAction() { if (!$this->getRequest()->isXmlHttpRequest()) { return $this->redirect()->toRoute('venda'); } $entity = new VendaItemEntity(); $form = new VendaItemForm(); $form->bind($entity); $post = $this->getRequest()->getPost(); $form->setData($post); if ($form->isValid()) { $service = $this->getCalculoService(); $venda_id = $form->get('venda_id')->getValue(); $item_id = $form->get('item_id')->getValue(); $quantidade = $form->get('quantidade')->getValue(); $result = $service->calcule($item_id, $quantidade); if ($result) { $data = $result->current(); $entity->setImposto($data['imposto']); $entity->setTotal($data['total']); $service = $this->getVendaItemMapper(); $result = $service->save($entity); if ($result) { $vendas = $this->getVendaMapper()->fetchVendaItem($venda_id)->toArray(); } } } return new JsonModel($vendas); }