<commit_before><?php
/**
 * Litus is a project by a group of students from the KU Leuven. The goal is to create
 * various applications to support the IT needs of student unions.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 *
 * @license http://litus.cc/LICENSE
 */

namespace BrBundle\Controller\Admin;

use BrBundle\Entity\Collaborator,
    BrBundle\Entity\Contract,
    BrBundle\Entity\Contract\ContractEntry,
    BrBundle\Entity\Contract\ContractHistory,
    BrBundle\Entity\Product\Order,
    BrBundle\Entity\Product\OrderEntry,
    Zend\View\Model\ViewModel;

/**
 * OrderController
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class OrderController extends \CommonBundle\Component\Controller\ActionController\AdminController
{
    public function manageAction()
    {
        $paginator = $this->paginator()->createFromQuery(
            $this->getEntityManager()
                ->getRepository('BrBundle\Entity\Product\Order')
                ->findAllNotOldUnsignedQuery(),
            $this->getParam('page')
        );

        foreach ($paginator as $order) {
            $order->setEntityManager($this->getEntityManager());
        }

        return new ViewModel(
            array(
                'paginator' => $paginator,
                'paginationControl' => $this->paginator()->createControl(true),
            )
        );
    }

    public function signedAction()
    {
        $paginator = $this->paginator()->createFromQuery(
            $this->getEntityManager()
                ->getRepository('BrBundle\Entity\Product\Order')
                ->findAllNotOldSignedQuery(),
            $this->getParam('page')
        );

        foreach ($paginator as $order) {
            $order->setEntityManager($this->getEntityManager());
        }

        return new ViewModel(
            array(
                'paginator' => $paginator,
                'paginationControl' => $this->paginator()->createControl(true),
            )
        );
    }

    public function addAction()
    {
        if (!($collaborator = $this->getCollaboratorEntity())) {
            return new ViewModel();
        }

        $form = $this->getForm('br_order_add', array('current_year' => $this->getCurrentAcademicYear()));

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            $form->setData($formData);

            if ($form->isValid()) {
                $order = $form->hydrateObject(
                    new Order($collaborator)
                );

                $this->getEntityManager()->persist($order);
                $this->getEntityManager()->flush();

                $this->redirect()->toRoute(
                    'br_admin_order',
                    array(
                        'action' => 'product',
                        'id' => $order->getId(),
                    )
                );

                return new ViewModel();
            }
        }

        return new ViewModel(
            array(
                'form' => $form,
            )
        );
    }

    public function productAction()
    {
        if (!($order = $this->getOrderEntity(false))) {
            return new ViewModel();
        }

        if ($order->hasContract() && $order->getContract()->isSigned()) {
            return new ViewModel();
        }

        if (!($collaborator = $this->getCollaboratorEntity())) {
            return new ViewModel();
        }

        $entries = $order->getEntries();

        $currentProducts = array();
        foreach ($entries as $entry) {
            $currentProducts[] = $entry->getProduct();
        }

        $form = $this->getForm('br_order_add-product', array('order' => $order, 'current_products' => $currentProducts, 'current_year' => $this->getCurrentAcademicYear()));

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            $form->setData($formData);

            if ($form->isValid()) {
                if ($order->hasContract()) {
                    $this->redirect()->toRoute(
                        'br_admin_order',
                        array(
                            'action' => 'manage',
                        )
                    );
                } else {
                    $this->getEntityManager()->flush();
                    $this->redirect()->toRoute(
                        'br_admin_order',
                        array(
                            'action' => 'product',
                            'id' => $order->getId(),
                        )
                    );
                }
            }
        }

        return new ViewModel(
            array(
                'order' => $order,
                'entries' => $entries,
                'addProductForm' => $form,
            )
        );
    }

    public function editAction()
    {
        if (!($order = $this->getOrderEntity(false))) {
            return new ViewModel();
        }

        if ($order->hasContract() && $order->getContract()->isSigned()) {
            return new ViewModel();
        }

        if (!($collaborator = $this->getCollaboratorEntity())) {
            return new ViewModel();
        }

        $form = $this->getForm('br_order_edit', array('order' => $order, 'current_year' => $this->getCurrentAcademicYear()));

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            $form->setData($formData);

            if ($form->isValid()) {
                $this->getEntityManager()->flush();

                $this->flashMessenger()->success(
                    'Success',
                    'The order was succesfully updated!'
                );

                $this->redirect()->toRoute(
                    'br_admin_order',
                    array(
                        'action' => 'manage',
                    )
                );

                return new ViewModel();
            }
        }

        return new ViewModel(
            array(
                'form' => $form,
            )
        );
    }

    public function editProductAction()
    {
        if (!($order = $this->getOrderEntity(false))) {
            return new ViewModel();
        }

        if ($order->hasContract() && $order->getContract()->isSigned()) {
            return new ViewModel();
        }

        if (!($collaborator = $this->getCollaboratorEntity())) {
            return new ViewModel();
        }

        $entry = $this->getEntityById('BrBundle\Entity\Product\OrderEntry', 'entry');

        $currentProducts = array();

        $form = $this->getForm('br_order_edit-product', array('order' => $order, 'entry' => $entry, 'current_products' => $currentProducts, 'current_year' => $this->getCurrentAcademicYear()));

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            $form->setData($formData);

            if ($form->isValid()) {
                $this->getEntityManager()->flush();

                if ($order->hasContract()) {
                    $this->redirect()->toRoute(
                        'br_admin_order',
                        array(
                            'action' => 'manage',
                        )
                    );
                } else {
                    $this->redirect()->toRoute(
                        'br_admin_order',
                        array(
                            'action' => 'product',
                            'id' => $order->getId(),
                        )
                    );
                }
            }
        }

        return new ViewModel(
            array(
                'order' => $order,
                'entry' => $entry,
                'editProductForm' => $form,
            )
        );
    }

    public function deleteAction()
    {
        $this->initAjax();

        if (!($order = $this->getOrderEntity())) {
            return new ViewModel();
        }

        $order->setOld();

        $this->getEntityManager()->flush();

        return new ViewModel(
            array(
                'result' => (object) array('status' => 'success'),
            )
        );
    }

    public function deleteProductAction()
    {
        $this->initAjax();

        if (!($entry = $this->getEntryEntity(false))) {
            return new ViewModel();
        }

        if ($entry->getOrder()->hasContract()) {
            $contract_entries = $this->getEntityManager()
                ->getRepository('BrBundle\Entity\Contract\ContractEntry')
                ->findAllContractEntriesByOrderEntry($entry);

            foreach ($contract_entries as $c_entry) {
                $this->getEntityManager()->remove($c_entry);
            }
        }

        $this->getEntityManager()->remove($entry);
        $this->getEntityManager()->flush();

        return new ViewModel(
            array(
                'result' => (object) array('status' => 'success'),
            )
        );
    }

    public function oldAction()
    {
        $paginator = $this->paginator()->createFromEntity(
            'BrBundle\Entity\Product\Order',
            $this->getParam('page'),
            array(
                'old' => true,
            )
        );

        return new ViewModel(
            array(
                'paginator' => $paginator,
                'paginationControl' => $this->paginator()->createControl(true),
            )<commit_msg>Fixed auto discount not updating<commit_after><?php
/**
 * Litus is a project by a group of students from the KU Leuven. The goal is to create
 * various applications to support the IT needs of student unions.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 *
 * @license http://litus.cc/LICENSE
 */

namespace BrBundle\Controller\Admin;

use BrBundle\Entity\Collaborator,
    BrBundle\Entity\Contract,
    BrBundle\Entity\Contract\ContractEntry,
    BrBundle\Entity\Contract\ContractHistory,
    BrBundle\Entity\Product\Order,
    BrBundle\Entity\Product\OrderEntry,
    Zend\View\Model\ViewModel;

/**
 * OrderController
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class OrderController extends \CommonBundle\Component\Controller\ActionController\AdminController
{
    public function manageAction()
    {
        $paginator = $this->paginator()->createFromQuery(
            $this->getEntityManager()
                ->getRepository('BrBundle\Entity\Product\Order')
                ->findAllNotOldUnsignedQuery(),
            $this->getParam('page')
        );

        foreach ($paginator as $order) {
            $order->setEntityManager($this->getEntityManager());
        }

        return new ViewModel(
            array(
                'paginator' => $paginator,
                'paginationControl' => $this->paginator()->createControl(true),
            )
        );
    }

    public function signedAction()
    {
        $paginator = $this->paginator()->createFromQuery(
            $this->getEntityManager()
                ->getRepository('BrBundle\Entity\Product\Order')
                ->findAllNotOldSignedQuery(),
            $this->getParam('page')
        );

        foreach ($paginator as $order) {
            $order->setEntityManager($this->getEntityManager());
        }

        return new ViewModel(
            array(
                'paginator' => $paginator,
                'paginationControl' => $this->paginator()->createControl(true),
            )
        );
    }

    public function addAction()
    {
        if (!($collaborator = $this->getCollaboratorEntity())) {
            return new ViewModel();
        }

        $form = $this->getForm('br_order_add', array('current_year' => $this->getCurrentAcademicYear()));

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            $form->setData($formData);

            if ($form->isValid()) {
                $order = $form->hydrateObject(
                    new Order($collaborator)
                );

                $this->getEntityManager()->persist($order);
                $this->getEntityManager()->flush();

                $this->redirect()->toRoute(
                    'br_admin_order',
                    array(
                        'action' => 'product',
                        'id' => $order->getId(),
                    )
                );

                return new ViewModel();
            }
        }

        return new ViewModel(
            array(
                'form' => $form,
            )
        );
    }

    public function productAction()
    {
        if (!($order = $this->getOrderEntity(false))) {
            return new ViewModel();
        }

        if ($order->hasContract() && $order->getContract()->isSigned()) {
            return new ViewModel();
        }

        if (!($collaborator = $this->getCollaboratorEntity())) {
            return new ViewModel();
        }

        $entries = $order->getEntries();

        $currentProducts = array();
        foreach ($entries as $entry) {
            $currentProducts[] = $entry->getProduct();
        }

        $form = $this->getForm('br_order_add-product', array('order' => $order, 'current_products' => $currentProducts, 'current_year' => $this->getCurrentAcademicYear()));

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            $form->setData($formData);

            if ($form->isValid()) {
                if ($order->hasContract()) {
                    $this->redirect()->toRoute(
                        'br_admin_order',
                        array(
                            'action' => 'manage',
                        )
                    );
                } else {
                    $this->getEntityManager()->flush();
                    $this->redirect()->toRoute(
                        'br_admin_order',
                        array(
                            'action' => 'product',
                            'id' => $order->getId(),
                        )
                    );
                }
            }
        }

        return new ViewModel(
            array(
                'order' => $order,
                'entries' => $entries,
                'addProductForm' => $form,
            )
        );
    }

    public function editAction()
    {
        if (!($order = $this->getOrderEntity(false))) {
            return new ViewModel();
        }

        if ($order->hasContract() && $order->getContract()->isSigned()) {
            return new ViewModel();
        }

        if (!($collaborator = $this->getCollaboratorEntity())) {
            return new ViewModel();
        }

        $form = $this->getForm('br_order_edit', array('order' => $order, 'current_year' => $this->getCurrentAcademicYear()));

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            $form->setData($formData);

            if ($form->isValid()) {
                $this->getEntityManager()->flush();

                $this->flashMessenger()->success(
                    'Success',
                    'The order was succesfully updated!'
                );

                $this->redirect()->toRoute(
                    'br_admin_order',
                    array(
                        'action' => 'manage',
                    )
                );

                return new ViewModel();
            }
        }

        return new ViewModel(
            array(
                'form' => $form,
            )
        );
    }

    public function editProductAction()
    {
        if (!($order = $this->getOrderEntity(false))) {
            return new ViewModel();
        }

        if ($order->hasContract() && $order->getContract()->isSigned()) {
            return new ViewModel();
        }

        if (!($collaborator = $this->getCollaboratorEntity())) {
            return new ViewModel();
        }

        $entry = $this->getEntityById('BrBundle\Entity\Product\OrderEntry', 'entry');

        $currentProducts = array();

        $form = $this->getForm('br_order_edit-product', array('order' => $order, 'entry' => $entry, 'current_products' => $currentProducts, 'current_year' => $this->getCurrentAcademicYear()));

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            $form->setData($formData);

            if ($form->isValid()) {
                $this->getEntityManager()->flush();

                if ($order->hasContract()) {
                    $this->redirect()->toRoute(
                        'br_admin_order',
                        array(
                            'action' => 'manage',
                        )
                    );
                } else {
                    $this->redirect()->toRoute(
                        'br_admin_order',
                        array(
                            'action' => 'product',
                            'id' => $order->getId(),
                        )
                    );
                }
            }
        }

        return new ViewModel(
            array(
                'order' => $order,
                'entry' => $entry,
                'editProductForm' => $form,
            )
        );
    }

    public function deleteAction()
    {
        $this->initAjax();

        if (!($order = $this->getOrderEntity())) {
            return new ViewModel();
        }

        $order->setOld();

        $this->getEntityManager()->flush();

        return new ViewModel(
            array(
                'result' => (object) array('status' => 'success'),
            )
        );
    }

    public function deleteProductAction()
    {
        $this->initAjax();

        if (!($entry = $this->getEntryEntity(false))) {
            return new ViewModel();
        }

        if ($entry->getOrder()->hasContract()) {
            $contract_entries = $this->getEntityManager()
                ->getRepository('BrBundle\Entity\Contract\ContractEntry')
                ->findAllContractEntriesByOrderEntry($entry);

            foreach ($contract_entries as $c_entry) {
                $this->getEntityManager()->remove($c_entry);
            }
        }

        if ($entry->getOrder()->getAutoDiscountPercentage() > 0) {
            $order = $entry->getOrder();
        }

        $this->getEntityManager()->remove($entry);
        $this->getEntityManager()->flush();

        if ($order) {
            $order->setEntityManager($this->getEntityManager())->setAutoDiscountPercentage();
        }
        $this->getEntityManager()->flush();

        return new ViewModel(
            array(
                'result' => (object) array('status' => 'success'),
            )
        );
    }

    public function oldAction()
    {
        $paginator = $this->paginator()->createFromEntity(
            'BrBundle\Entity\Product\Order',
            $this->getParam('page'),
            array(
                'old' => true,
            )
        );

        return new ViewModel(
            array(
                'paginator' => $paginator,
                'paginationControl' => $this->paginator()->createControl(true),
            )
{'fraction_non_alphanumeric': 0.14982595974373203, 'fraction_numerical': 5.044645109216567e-05, 'mean_word_length': 1.4786196549137285, 'pattern_counts': {'":': 0, '<': 83, '<?xml version=': 0, '>': 414, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1557981', 'n_tokens_mistral': 5252, 'n_tokens_neox': 4979, 'n_words': 887}