%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /home/emtnaeewxm/www/src/EEM/MagasinBundle/Controller/
Upload File :
Create Path :
Current File : /home/emtnaeewxm/www/src/EEM/MagasinBundle/Controller/AchatMagasinController.php

<?php

namespace EEM\MagasinBundle\Controller;

use EEM\FonctionnaliteBundle\Service\HistoriqueService;
use EEM\FonctionnaliteBundle\Service\TresorieService;
use EEM\MagasinBundle\Entity\AchatMagasin;
use EEM\MagasinBundle\Entity\LigneAchatMagasin;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\Response;

/**
 * Achatmagasin controller.
 *
 * @Route("/admin/magasin-externe")
 */
class AchatMagasinController extends Controller
{
    /**
     * Lists all achatMagasin entities.
     *
     * @Route("/", name="achatmagasin_index")
     * @Method("GET")
     * @Security("has_role('ROLE_ADMIN')")
     */
    public function indexAction(Request $request)
    {
        $em = $this->getDoctrine()->getManager();
        $paginator = $this->get('knp_paginator');


        $achatMagasins = $paginator->paginate(
            $em->getRepository('EEMMagasinBundle:AchatMagasin')->findBy([], ['dateAchat' => 'DESC']), /* query NOT result */
            $request->query->getInt('page', 1)/* page number */, 10/* limit per page */
        );

        if (isset($request->query->all()['type']) && !empty($request->query->all()['type'])) {
            $achatMagasins = $em->getRepository('EEMMagasinBundle:AchatMagasin')->findBy(['type' => $request->query->all()['type']], ['dateAchat' => 'DESC']);
        }

        return $this->render('@EEMMagasin/achatmagasin/index.html.twig', array(
            'achatMagasins' => $achatMagasins,
        ));

    }

    /**
     * Lists all article entities.
     *
     * @Route("/stock", name="stock_magasin_externe")
     * @Method("GET")
     * @Security("has_role('ROLE_ADMIN')")
     */
    public function stockMagasinExterneAction(Request $request)
    {
        $em = $this->getDoctrine()->getManager();
        $marques = $em->getRepository('EEMArticleBundle:Marque')->findAll();
        if (isset($request->query->all()['famille']) && !empty($request->query->all()['famille'])) {
            $marques = $em->getRepository('EEMArticleBundle:Marque')->findMarques($request->query->all()['famille']);
        }
        $modeles = $em->getRepository('EEMArticleBundle:Modele')->findModele(null);
        if (isset($request->query->all()['marque']) && !empty($request->query->all()['marque'])) {
            $modeles = $em->getRepository('EEMArticleBundle:Modele')->findModele($request->query->all()['marque']);
        }
        if (isset($request->request->all()['btn_search'])) {
            $url = $this->buildSearchUrl($request->request->all());

            if (!empty($url)) {
                return $this->redirectToRoute('stock_magasin_externe', $url);
            }
        }
        $array_article_magasin = [];
        $achat_magasins = $em->getRepository('EEMMagasinBundle:AchatMagasin')->findBy(['type' => 'achat']);
        foreach ($achat_magasins as $achat_magasin) {
            foreach ($achat_magasin->getLigneAchatMagasins() as $ligneAchatMagasin) {
                $array_article_magasin[] = $ligneAchatMagasin->getArticle()->getId();
            }
        }
        $array_article_magasin = array_unique($array_article_magasin);
        $paginator = $this->get('knp_paginator');

        $articles = $paginator->paginate(
            $em->getRepository('EEMArticleBundle:Article')->chiffreArticleMagasin($request->query->all(), $array_article_magasin), /* query NOT result */
            $request->query->getInt('page', 1)/* page number */, 20/* limit per page */
        );

        $valeur_article = 0;
        $article_stocks = $em->getRepository('EEMArticleBundle:Article')->chiffreArticleMagasin($request->query->all(), $array_article_magasin);
        foreach ($article_stocks as $article_stock) {
            $valeur_article += $article_stock->getQteMagasin() * $article_stock->getPrixAchatMagasin();
        }

        return $this->render('@EEMMagasin/achatmagasin/stock.html.twig', array(
            'articles' => $articles,
            'modeles' => $modeles,
            'valeur_article' => $valeur_article,
            'familles' => $em->getRepository('EEMArticleBundle:Famille')->findAll(),
            'marques' => $marques,
        ));
    }

    /**
     * Creates a new achatMagasin entity.
     *
     * @Route("/{type}/{date}", name="achatmagasin_new")
     * @Method({"GET", "POST"})
     * @Security("has_role('ROLE_ADMIN')")
     */
    public function newAction(Request $request)
    {
        $data_attributes = $request->attributes->all();

        $date_operation = $data_attributes['date'];
        if (empty($data_attributes['type']) || !isset($data_attributes['type']) || !in_array($data_attributes['type'], ['achat', 'vente'])) {
            return $this->redirectToRoute('accueil');
        }
        $em = $this->getDoctrine()->getManager();
        $achatMagasins = $em->getRepository('EEMMagasinBundle:AchatMagasin')->rechercheDate($date_operation, $data_attributes['type']);
        if (count($achatMagasins) == 1) {
            $achatMagasin = $achatMagasins[0];
        } else {
            $achatMagasin = new AchatMagasin();
            $achatMagasin->setDateAchat(new \DateTime($date_operation));
            $achatMagasin->setType($data_attributes['type']);
            $em->persist($achatMagasin);
            $em->flush();
        }

        if ($data_attributes['type'] == 'achat') {
            return $this->redirectToRoute('achat_magasin_show', ['id' => $achatMagasin->getId(), 'date' => $date_operation]);
        }
        if ($data_attributes['type'] == 'vente') {
            return $this->redirectToRoute('vente_magasin_show', ['id' => $achatMagasin->getId(), 'date' => $date_operation]);
        }
    }

    /**
     * Creates a new vente entity.
     *
     * @Route("/achat/{id}/{date}", name="achat_magasin_show")
     * @Route("/vente/{id}/{date}", name="vente_magasin_show")
     * @Method({"GET", "POST"})
     * @Security("has_role('ROLE_USER')")
     */
    public function achatDetailAction(AchatMagasin $achatMagasin, Request $request)
    {
        $em = $this->getDoctrine()->getManager();
        $form = $this->createForm('EEM\MagasinBundle\Form\AddLigneAchatMagasinType', null, ['type' => $achatMagasin->getType()]);
        $ligne_achats = $em->getRepository('EEMMagasinBundle:LigneAchatMagasin')->findBy(['achatMagasin' => $achatMagasin->getId()], ['createdAt' => 'desc']);
        $deleteForm = $this->createDeleteForm($achatMagasin);

        return $this->render('@EEMMagasin/achatmagasin/show.html.twig', array(
            'achatmagasin' => $achatMagasin,
            'ligne_achats' => $ligne_achats,
            'form' => $form->createView(),
            'date_jour' => date('d/m/Y'),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Creates a new vente entity.
     *
     * @Route("/edit_ligne_achat_magasin", name="edit_ligne_achat_magasin")
     * @Method({"GET", "POST"})
     */
    public function editLigneAchatMagasinAction(Request $request)
    {
        $id_ligne_achat = $request->request->get('id_ligne_achat');
        $em = $this->getDoctrine()->getManager();

        $ligne_Achat = $em->getRepository('EEMMagasinBundle:LigneAchatMagasin')->find($id_ligne_achat);
        $form = $this->createForm('EEM\MagasinBundle\Form\AddLigneAchatMagasinType', null, ['type' => $ligne_Achat->getAchatMagasin()->getType()]);
        return $this->render('@EEMMagasin/achatmagasin/edit_ajax.html.twig', array(
            'ligne_achat' => $ligne_Achat,
            'form' => $form->createView(),
        ));
    }

    /**
     * Creates a new vente entity.
     *
     * @Route("/save_edit_achat_magasin_ajax", name="save_edit_achat_magasin_ajax")
     * @Method({"GET", "POST"})
     */
    public function saveEditAchatAjaxAction(Request $request)
    {
        $em = $this->getDoctrine()->getManager();
        $data = $request->request->all()['add_ligne'];
        echo '<pre>'.print_r($data,1).'</pre>';
        $old_article = $request->request->all()['old_article'];
        $old_qte = $request->request->all()['old_qte'];
        $article = $em->getRepository('EEMArticleBundle:Article')->find($data['article']);

        $historiqueService = new HistoriqueService($em);
        $ligne_achat = $em->getRepository('EEMMagasinBundle:LigneAchatMagasin')->find($data['ligneAchat']);
        $achat = $ligne_achat->getAchatMagasin();

        if ($ligne_achat->getAchatMagasin()->getType() == 'achat') {
            if ($data['article'] == $old_article) {
                $article->setQteMagasin($article->getQteMagasin() - $old_qte);
                $article->setQte($article->getQte() + $old_qte);
            } elseif ($data['article'] != $old_article) {
                $old_article = $em->getRepository('EEMArticleBundle:Article')->find($old_article);
                $old_article->setQteMagasin($old_article->getQteMagasin() - $old_qte);
                $old_article->setQte($old_article->getQte() + $old_qte);

                $old_article->setPrixAchatMagasin($ligne_achat->getPrixAchatOld());
                $ligne_achat->setPrixAchatOld($article->getPrixAchat());
            }

            $ligne_achat->setArticle($article);
            $ligne_achat->setPrixAchat($data['prixAchat']);
            $ligne_achat->setQte($data['quantite']);

            $article->setQte($article->getQte() - $data['quantite']);
            $article->setQteMagasin($article->getQteMagasin() + $data['quantite']);
            $article->setPrixAchatMagasin($data['prixAchat']);
            $em->persist($ligne_achat);

            $em->flush();
            $historiqueService->HistoriqueAchat($article, $achat->getId(), $achat->getType(), 'Achat modifié', $data['quantite']);
        }
        if ($ligne_achat->getAchatMagasin()->getType() == 'vente') {
            if ($data['article'] == $old_article) {
                $article->setQteMagasin($article->getQteMagasin() + $old_qte);
            } elseif ($data['article'] != $old_article) {
                $old_article = $em->getRepository('EEMArticleBundle:Article')->find($old_article);
                $old_article->setQteMagasin($old_article->getQteMagasin() + $old_qte);
            }

            $tresorieService = new TresorieService($em);
            $tresorieService->controleExisteTresorie();
            $tresorieService->mettreAJourTresorie(-($old_article->getPrixAchatMagasin()*$old_qte));
            $tresorieService->mettreAJourTresorie($ligne_achat->getPrixAchat() * $data['quantite']);

            $ligne_achat->setArticle($article);
            $ligne_achat->setPrixAchat($data['prixAchat']);
            $ligne_achat->setQte($data['quantite']);

            $article->setQteMagasin($article->getQteMagasin() - $data['quantite']);
            $em->persist($ligne_achat);

            $em->flush();
            $historiqueService->HistoriqueAchat($article, $achat->getId(), $achat->getType(), 'Achat modifié', $data['quantite']);
        }

        $this->calculChiffreVente();

        $tab['id_ligne_achat'] = $ligne_achat->getId();
        $tab['code_barre'] = $article->getCodeABarre();
        $tab['article'] = $article->getArticleComplet();
        $tab['prixAchat'] = $data['prixAchat'];
        $tab['qte'] = $ligne_achat->getQte();

        return new Response(json_encode($tab));
    }

    /**
     * Creates a new vente entity.
     *
     * @Route("/delete_ligne_achat_magasin", name="delete_ligne_achat_magasin")
     * @Method({"GET", "POST"})
     */
    public function deleteLigneAchatAction(Request $request)
    {
        $id_ligne_achat = $request->request->get('id_ligne_achat');
        $em = $this->getDoctrine()->getManager();
        $ligne_achat = $em->getRepository('EEMMagasinBundle:LigneAchatMagasin')->find($id_ligne_achat);

        $historiqueService = new HistoriqueService($em);
        $article = $ligne_achat->getArticle();
        $achat = $ligne_achat->getAchatMagasin();

        if ($achat->getType() == 'achat') {
            $article->setPrixAchatMagasin($ligne_achat->getPrixAchatOld());
            $article->setQteMagasin($article->getQteMagasin() - $ligne_achat->getQte());
            $article->setQte($article->getQte() + $ligne_achat->getQte());
            //$historiqueService->HistoriqueAchat($article,$achat->getId(),$achat->getType(),'Achat annulé',$ligne_achat->getQte());
        }
        if ($achat->getType() == 'vente') {
            $article->setQteMagasin($article->getQteMagasin() + $ligne_achat->getQte());
            //$historiqueService->HistoriqueAchat($article,$achat->getId(),$achat->getType(),'Achat annulé',$ligne_achat->getQte());

            $tresorieService = new TresorieService($em);
            $tresorieService->controleExisteTresorie();
            $tresorieService->mettreAJourTresorie(-($article->getPrixAchatMagasin()*$ligne_achat->getQte()));
        }

        $em->remove($ligne_achat);
        $em->flush();

        $this->calculChiffreVente();
        return new Response('ok');
    }

    /**
     * Creates a new vente entity.
     *
     * @Route("/save_achat_magasin_ajax", name="save_achat_magasin_ajax")
     * @Method({"GET", "POST"})
     */
    public function saveAchatMagasinAjaxAction(Request $request)
    {
        $em = $this->getDoctrine()->getManager();
        //print_r($request->request->all());
        //exit();
        $data = $request->request->all()['add_ligne'];
        $article = $em->getRepository('EEMArticleBundle:Article')->find($data['article']);
        $achatMagasin = $em->getRepository('EEMMagasinBundle:AchatMagasin')->find($data['achat']);
        $ligne_achat_magasin = new LigneAchatMagasin();
        $ligne_achat_magasin->setArticle($article);
        $ligne_achat_magasin->setAchatMagasin($achatMagasin);

        if ($data['quantite'] != '') $quantite = $data['quantite']; else $quantite = 1;
        if ($data['prixAchat'] != '') $prix_achat = $data['prixAchat']; else $prix_achat = '0.000';
        $ligne_achat_magasin->setPrixAchat($prix_achat);

        $historiqueService = new HistoriqueService($em);
        if ($achatMagasin->getType() == 'achat') {
            $ligne_achat_magasin->setPrixAchatOld($article->getPrixAchat());
            $article->setQteGros($article->getQteGros() - $quantite);
            $article->setPrixAchatMagasin($ligne_achat_magasin->getPrixAchat());
            $article->setQteMagasin($article->getQteMagasin() + $quantite);
            $historiqueService->HistoriqueAchat($article, $achatMagasin->getId(), $achatMagasin->getType(), 'Nouveau achat magasin', $quantite);
        }
        if ($achatMagasin->getType() == 'vente') {
            $ligne_achat_magasin->setPrixAchatOld($article->getPrixAchat());
            $article->setPrixAchatMagasin($ligne_achat_magasin->getPrixAchat());
            $article->setQteMagasin($article->getQteMagasin() - $quantite);
            $historiqueService->HistoriqueAchat($article, $achatMagasin->getId(), $achatMagasin->getType(), 'Nouveau vente magasin', $quantite);

            $tresorieService = new TresorieService($em);
            $tresorieService->controleExisteTresorie();
            $tresorieService->mettreAJourTresorie($ligne_achat_magasin->getPrixAchat() * $quantite);
        }


        $ligne_achat_magasin->setQte($quantite);
        $em->persist($ligne_achat_magasin);

        $em->flush();

        $this->calculChiffreVente();
        $tab['id_ligne_achat'] = $ligne_achat_magasin->getId();
        $tab['code_barre'] = $article->getCodeABarre();
        $tab['article'] = $article->getArticleComplet();
        $tab['prixAchat'] = $prix_achat;
        $tab['qte'] = $ligne_achat_magasin->getQte();

        return new Response(json_encode($tab));
    }

    /**
     * Deletes a achatMagasin entity.
     *
     * @Route("/{id}", name="achatmagasin_delete")
     * @Method("DELETE")
     * @Security("has_role('ROLE_ADMIN')")
     */
    public function deleteAction(Request $request, AchatMagasin $achatMagasin)
    {
        $form = $this->createDeleteForm($achatMagasin);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->remove($achatMagasin);
            $em->flush();
        }

        return $this->redirectToRoute('achatmagasin_index');
    }

    /**
     * Creates a form to delete a achatMagasin entity.
     *
     * @param AchatMagasin $achatMagasin The achatMagasin entity
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createDeleteForm(AchatMagasin $achatMagasin)
    {
        return $this->createFormBuilder()
            ->setAction($this->generateUrl('achatmagasin_delete', array('id' => $achatMagasin->getId())))
            ->setMethod('DELETE')
            ->getForm();
    }

    private function buildSearchUrl($data)
    {
        foreach ($data as $k => $v) {
            if (isset($data['famille']) && !empty($data['famille'])) {
                $url['famille'] = $data['famille'];
            }
            if (isset($data['marque']) && !empty($data['marque'])) {
                $url['marque'] = $data['marque'];
            }
            if (isset($data['modele']) && !empty($data['modele'])) {
                $url['modele'] = $data['modele'];
            }
            if (isset($data['stock']) && !empty($data['stock'])) {
                $url['stock'] = $data['stock'];
            }
            if (isset($data['barcode']) && !empty($data['barcode'])) {
                $url['barcode'] = $data['barcode'];
            }
        }
        return $url;
    }

    public
    function calculChiffreVente()
    {
        $em = $this->getDoctrine()->getManager();
        $ventes = $em->getRepository('EEMVenteBundle:Vente')->rechercheDate(date('Y-m-d'), 'detail');

        if (count($ventes) == 1) {
            $vente = $ventes[0];

            $stock_detail = 0;
            $stock_gros = 0;
            $stock_magasin = 0;
            $article_stocks = $em->getRepository('EEMArticleBundle:Article')->findAll();
            foreach ($article_stocks as $article_stock) {
                $stock_detail += $article_stock->getQte() * $article_stock->getPrixAchat();
                $stock_gros += $article_stock->getQteGros() * $article_stock->getPrixAchatGros();
                $stock_magasin += $article_stock->getQteMagasin() * $article_stock->getPrixAchatMagasin();
            }
            $vente->setChiffreRestant($stock_detail);
            $vente->setChiffreRestantGros($stock_gros);
            $vente->setChiffreRestantMagasin($stock_magasin);
            $em->flush();
        }
    }
}

Zerion Mini Shell 1.0