<?php
namespace App\Controller\Api;
use App\Entity\Material;
use App\Entity\MaterialUnitTypes;
use App\Entity\Suppliers;
use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* @Route("/api/material")
*/
class MaterialController extends AbstractController
{
/**
* @Route("/materials/{supplier_id}", name="app_material", defaults={"supplier_id"=null})
*/
public function materials(EntityManagerInterface $manager, SerializerInterface $serializer, ?int $supplier_id = null ): Response
{
if(!$supplier_id){
$materials = $manager->getRepository(Material::class)->findAll();
} else {
$supplier = $manager->getRepository(Suppliers::class)->find($supplier_id);
$materials = $supplier->getMaterials();
#dd($materials);
$materials = $manager->getRepository(Material::class)->findBy(["supplier" => $supplier_id]);
}
#dd($supplier_id);
$materials = $serializer->serialize($materials, 'json', [
'groups' => [
'material.base',
"material.unit.type.base"
]
]);
#dd($materials);
return $this->json([
"message" => "Successfully",
"severity" => "success",
"materials" => json_decode($materials, true)
], Response::HTTP_OK );
}
/**
* @Route("/fetch-manage-nested-data", name="app_material_fetch_manage_nested_data")
*/
public function fetchMaterialManageNestedData(EntityManagerInterface $manager, SerializerInterface $serializer ): Response
{
try{
$unitTypes = $manager->getRepository(MaterialUnitTypes::class)->findAll();
$unitTypes = json_decode($serializer->serialize($unitTypes, 'json', [
'groups' => [
"material.unit.type.base"
]
]));
$suppliers = $manager->getRepository(Suppliers::class)->findAll();
$suppliers = json_decode($serializer->serialize($suppliers, 'json', [
'groups' => [
"supplier.base"
]
]));
} catch (\Exception $exception){
return $this->json([
"message" => $exception->getMessage(),
"severity" => "error"
], Response::HTTP_NOT_FOUND );
}
return $this->json([
"message" => "Successfully",
"severity" => "successful",
"unit_types" => $unitTypes,
"suppliers" => $suppliers
], Response::HTTP_OK );
}
/**
* @Route("/save/{material_id}", name="app_material_save", defaults={"material_id": null })
* @ParamConverter("material", options={"mapping":{"material_id": "id"}})
*/
public function save(?Material $material, EntityManagerInterface $manager, Request $request, SerializerInterface $serializer, ?int $material_id = null ): Response
{
$params = $request->request;
#dd($params->all());
if( $material_id && !$material ){
return $this->json([
"message" => "Material not found!",
"severity" => "error"
], Response::HTTP_NOT_FOUND );
}
if(!$material){
$material = new Material();
}
if( $params->get('supplier') ){
$supplier = $manager->getRepository(Suppliers::class)->find($params->get('supplier'));
$material->setSupplier($supplier);
}
try{
$unitType = $manager->getRepository(MaterialUnitTypes::class)->find($params->get('unit_type'));
$material
->setAlias($params->get('alias'))
->setContent($params->get('content'))
->setUnitType($unitType)
->setPrice($params->get('price'))
->setArticleNr($params->get('article_nr'))
->setDescription($params->get('description') ?? null );
$manager->persist($material);
$manager->flush();
} catch (\Exception $exception){
return $this->json([
"message" => $exception->getMessage(),
"severity" => "error"
], Response::HTTP_NOT_FOUND );
}
$material = json_decode($serializer->serialize($material, 'json', [
'groups' => [
'material.base'
]
]), true );
return $this->json([
"message" => "Saved successfully",
"severity" => "success",
"material" => $material
], Response::HTTP_OK );
}
}