Пример #1
0
 public function deleteAction()
 {
     $wallet_id = $this->params()->fromRoute('id');
     if (!$wallet_id) {
         return $this->redirect()->toRoute('home/wallets');
     }
     $em = $this->EntityPlugin()->getEntityManager();
     $wallet_sum = $em->getRepository('HouseholdBudget\\Entity\\Wallet')->getOneWalletSummary($wallet_id);
     if ($wallet_sum->getTotal() == 0) {
         $wallet = $em->find('HouseholdBudget\\Entity\\Wallet', $wallet_id);
         $wallet->setDeleted(1);
         $em->persist($wallet);
         $em->flush();
     } else {
         $auth = $this->getServiceLocator()->get('Zend\\Authentication\\AuthenticationService');
         $user = $auth->getIdentity();
         $wallet = $em->find('HouseholdBudget\\Entity\\Wallet', $wallet_id);
         $operationType = $em->find('HouseholdBudget\\Entity\\OperationType', 3);
         $operation = new Operation();
         $operation->setAmount(-$wallet_sum->getTotal());
         $operation->setDescription('Removal of the wallet.');
         $operation->setOperationType($operationType);
         $operation->setUser($user);
         $operation->setWallet($wallet);
         $em->persist($operation);
         $em->flush();
         $wallet->setDeleted(1);
         $em->persist($wallet);
         $em->flush();
     }
     $this->redirect()->toRoute('home/wallets');
 }
Пример #2
0
 public function addAction()
 {
     $em = $this->EntityPlugin()->getEntityManager();
     $auth = $this->getServiceLocator()->get('Zend\\Authentication\\AuthenticationService');
     $user = $auth->getIdentity();
     $wallets = $em->getRepository('HouseholdBudget\\Entity\\Wallet')->getWalletsByUser($user->getId());
     if (!$wallets) {
         $this->flashMessenger()->addMessage('First, add wallet.');
         return $this->redirect()->toRoute('home/operations');
     }
     $form = new OperationForm($em);
     $options = array();
     foreach ($wallets as $wallet) {
         $options[$wallet['id']] = $wallet['name'];
     }
     $form->get('wallet')->setValueOptions($options);
     $operationType = $this->params()->fromRoute('type');
     if ($operationType == 'transfer') {
         $form->get('wallet2')->setValueOptions($options);
         if ($this->getRequest()->isPost()) {
             $form->setInputFilter(new OperationInputFilter());
             $form->setData($this->getRequest()->getPost());
             if ($form->isValid()) {
                 $operation1 = new Operation();
                 $operation1->setAmount(-(int) $this->getRequest()->getPost('amount'));
                 $operation1->setDescription($this->getRequest()->getPost('description'));
                 $wallet = $em->find('HouseholdBudget\\Entity\\Wallet', $this->getRequest()->getPost('wallet'));
                 $operationType = $em->getRepository('HouseholdBudget\\Entity\\OperationType')->findOneBy(array('name' => $operationType));
                 $operation1->setUser($user);
                 $operation1->setWallet($wallet);
                 $operation1->setOperationType($operationType);
                 $operation2 = new Operation();
                 $operation2->setAmount($this->getRequest()->getPost('amount'));
                 $operation2->setDescription($this->getRequest()->getPost('description'));
                 $wallet = $em->find('HouseholdBudget\\Entity\\Wallet', $this->getRequest()->getPost('wallet2'));
                 $operation2->setUser($user);
                 $operation2->setWallet($wallet);
                 $operation2->setOperationType($operationType);
                 $em->persist($operation2);
                 $em->flush();
                 $em->persist($operation1);
                 $em->flush();
                 return $this->redirect()->toRoute('home/operations');
             }
         }
     } else {
         $form->remove('wallet2');
         if ($this->getRequest()->isPost()) {
             $form->setInputFilter(new OperationInputFilter());
             $form->setData($this->getRequest()->getPost());
             if ($form->isValid()) {
                 $operation = new Operation();
                 $amount = $operationType == 'expense' ? -(int) $this->getRequest()->getPost('amount') : $this->getRequest()->getPost('amount');
                 $operation->setAmount($amount);
                 $operation->setDescription($this->getRequest()->getPost('description'));
                 $wallet = $em->find('HouseholdBudget\\Entity\\Wallet', $this->getRequest()->getPost('wallet'));
                 $operationType = $em->getRepository('HouseholdBudget\\Entity\\OperationType')->findOneBy(array('name' => $operationType));
                 $operation->setUser($user);
                 $operation->setWallet($wallet);
                 $operation->setOperationType($operationType);
                 $em->persist($operation);
                 $em->flush();
                 return $this->redirect()->toRoute('home/operations');
             }
         }
     }
     return new ViewModel(array('form' => $form, 'operationType' => $operationType));
 }