Commit 81aded79 authored by Mat's avatar Mat

récupère les données de légume, incrémente correctement la nouvelle référence

parent a54bcc00
......@@ -164,6 +164,35 @@ class LegumeAPIController extends MasterAPIController
}
/**
* @param Legume $legume
* @param $id_offre
* @return string
* @throws \Doctrine\ORM\NonUniqueResultException
*/
private function newReference($legume, $id_offre)
{
$reference = ''
. ($legume->getFruit() === true ? 'F' : 'L')
. ($legume->getRevente() === true ? 'R' : 'D')
. sprintf("%03d", $legume->getId());
$em = $this->getDoctrine()->getManager();
$last = $em->getRepository('PotageBundle:OffreLegumes')->findOneLastReferenceByOffre($id_offre);
if ($last !== null)
{
$compteur = intval(substr($last->getReference(), -3));
$compteur++;
$reference .= sprintf("%03d", $compteur);
}
else {
$reference .= '001';
}
return $reference;
}
/**
* @param Request $request
* @param $id
......@@ -173,29 +202,34 @@ class LegumeAPIController extends MasterAPIController
public function insertAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$legume = $em->getRepository('PotageBundle:Legume')
->findOneForAPIRead($id); // TODO temporaire, réutilise un repository déjà existant
$legume = $em->getRepository('PotageBundle:Legume')->findOneForAPIRead($id);
// TODO temporaire, réutilise un repository déjà existant
if ($legume === null ) {
return $this->api($legume, Response::HTTP_NOT_FOUND);
}
$formInsert = $request->request->get('offre_legumes_insert');
$id_offre = $formInsert['offre'];
$reference = $this->newReference($legume, $id_offre);
$offrelegumes = new OffreLegumes();
$offrelegumes
->setNom($legume)
->setDescription($legume)
->setOrigine($legume)
->setUnite($legume)
->setQuantiteMin($legume)
->setQuantiteMax($legume)
->setQuantiteStep($legume)
->setPrixUnitaire($legume)
->setFruit($legume)
->setRevente($legume)
->setImage($legume);
->setReference($reference)
->setNom($legume->getNom())
->setDescription($legume->getDescription())
->setUnite($legume->getUnite())
->setPrixUnitaire($legume->getPrixUnitaire())
->setImage($legume->getImage())
->setOrigine($legume->getOrigine())
->setQuantiteMin($legume->getQuantiteMin())
->setQuantiteMax($legume->getQuantiteMax())
->setQuantiteStep($legume->getQuantiteStep())
->setFruit($legume->getFruit())
->setRevente($legume->getRevente())
;
$form = $this->createForm(OffreLegumesInsertAPIType::class, $offrelegumes);
$form->handleRequest($request);
......
......@@ -130,7 +130,16 @@ class Offre
{
return $this->reference;
}
/**
* @return string
*/
public function getReferenceMore()
{
return '(#' . sprintf("%03d", $this->id) . ') référence ' . $this->reference;
}
/**
* Set createdAt
*
......
......@@ -37,7 +37,7 @@ class OffreLegumes
/**
* @var string
*
* @ORM\Column(name="reference", type="string", length=16, unique=true)
* @ORM\Column(name="reference", type="string", length=16, unique=false)
*/
private $reference;
......@@ -51,7 +51,7 @@ class OffreLegumes
/**
* @var \DateTime
*
* @ORM\Column(name="updatedAt", type="datetime")
* @ORM\Column(name="updatedAt", type="datetime", nullable=true)
*/
private $updatedAt;
......@@ -163,6 +163,8 @@ class OffreLegumes
public function __construct()
{
$this->isPromo = false;
$this->fruit = false;
$this->revente = false;
}
/**
......@@ -172,6 +174,7 @@ class OffreLegumes
public function prePersist()
{
$this->createdAt = new \DateTime();
}
/**
*
......@@ -268,9 +271,8 @@ class OffreLegumes
/**
* Set reference
*
* @param string $reference
*
* @return OffreLegumes
* @param $reference
* @return $this
*/
public function setReference($reference)
{
......@@ -288,7 +290,15 @@ class OffreLegumes
{
return $this->reference;
}
/**
* @return string
*/
public function getReferenceMore()
{
return '(#' . sprintf("%03d", $this->id) . ') ' . $this->reference;
}
/**
* Set createdAt
*
......
......@@ -33,7 +33,7 @@ class OffreLegumesInsertType extends AbstractType
->setParameter(':status', 'draft')
->orderBy('o.reference', 'DESC');
},
'choice_label' => 'getReference',
'choice_label' => 'getReferenceMore',
'label' => 'Choisissez une offre',
'placeholder' => 'Offres en préparation :',
'attr' => array('class' => 'form-control form-control-sm'),
......
......@@ -10,4 +10,21 @@ namespace PotageBundle\Repository;
*/
class OffreLegumesRepository extends \Doctrine\ORM\EntityRepository
{
/**
* @param $id_offre
* @return mixed
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function findOneLastReferenceByOffre($id_offre)
{
$qb = $this->createQueryBuilder('ol')
->where('ol.offre = :id_offre')
->setParameter(':id_offre', $id_offre)
->orderBy('ol.id', 'DESC')
->setMaxResults(1);
return $qb->getQuery()->getOneOrNullResult();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment