From 16916209865de738c6efcf9bdb2bb4178f69a41a Mon Sep 17 00:00:00 2001 From: Matthieu Lamalle Date: Mon, 12 Oct 2020 14:49:58 +0200 Subject: [PATCH] first implementation of dolibarr api --- src/schedule-2.0/.env | 5 +- .../config/packages/http_client.yaml | 6 + src/schedule-2.0/config/routes.yaml | 8 ++ src/schedule-2.0/config/services.yaml | 2 + .../src/Controller/OfferController.php | 110 +++++++++++++++++- src/schedule-2.0/src/Entity/Offer.php | 16 +++ src/schedule-2.0/src/Service/dolibarrApi.php | 98 ++++++++++++++++ .../templates/Offer/list.html.twig | 1 + 8 files changed, 242 insertions(+), 4 deletions(-) create mode 100644 src/schedule-2.0/config/packages/http_client.yaml create mode 100644 src/schedule-2.0/src/Service/dolibarrApi.php diff --git a/src/schedule-2.0/.env b/src/schedule-2.0/.env index 9b1367b..c5152d3 100644 --- a/src/schedule-2.0/.env +++ b/src/schedule-2.0/.env @@ -64,4 +64,7 @@ CAS_FIRSTNAME=firstname MAILER_URL= ## Sentry DSN -SENTRY_DSN= \ No newline at end of file +SENTRY_DSN= + +DOLIBARR_API_KEY= +DOLIBARR_URI= diff --git a/src/schedule-2.0/config/packages/http_client.yaml b/src/schedule-2.0/config/packages/http_client.yaml new file mode 100644 index 0000000..6dfae85 --- /dev/null +++ b/src/schedule-2.0/config/packages/http_client.yaml @@ -0,0 +1,6 @@ +# config/packages/framework.yaml +framework: + http_client: + default_options: + headers: + DOLAPIKEY: '%env(DOLIBARR_API_KEY)%' \ No newline at end of file diff --git a/src/schedule-2.0/config/routes.yaml b/src/schedule-2.0/config/routes.yaml index 60334f7..870f7b1 100644 --- a/src/schedule-2.0/config/routes.yaml +++ b/src/schedule-2.0/config/routes.yaml @@ -233,6 +233,14 @@ app_offer_activeoffer: path: /master/offer/activeoffer defaults: { _controller: App\Controller\OfferController:activeoffer } +app_offer_getorders: + path: /master/offer/getorders + defaults: { _controller: App\Controller\OfferController:getorders } + +app_offer_convert: + path: /master/offer/convert/{type}/{id} + defaults: { _controller: App\Controller\OfferController:convert } + #== Task ==================================================================================================== app_task: path: /master/task diff --git a/src/schedule-2.0/config/services.yaml b/src/schedule-2.0/config/services.yaml index c9ebaa1..17883fb 100644 --- a/src/schedule-2.0/config/services.yaml +++ b/src/schedule-2.0/config/services.yaml @@ -21,6 +21,8 @@ parameters: casFirstname: '%env(resolve:CAS_FIRSTNAME)%' officeHourStart: '%env(resolve:OFFICE_HOUR_START)%' officeHourEnd: '%env(resolve:OFFICE_HOUR_END)%' + doliApiKey: '%env(resolve:DOLIBARR_API_KEY)%' + doliUri: '%env(resolve:DOLIBARR_URI)%' services: # default configuration for services in *this* file diff --git a/src/schedule-2.0/src/Controller/OfferController.php b/src/schedule-2.0/src/Controller/OfferController.php index 7ffc0b9..55330da 100755 --- a/src/schedule-2.0/src/Controller/OfferController.php +++ b/src/schedule-2.0/src/Controller/OfferController.php @@ -17,9 +17,13 @@ class OfferController extends AbstractController private $route = "app_offer"; private $render = "Offer/"; private $entity = "App:Offer"; + private $dolibarrapi; private $knpSnappy; - public function __construct(\Knp\Snappy\Pdf $knpSnappy) { $this->knpSnappy = $knpSnappy; } + public function __construct(\Knp\Snappy\Pdf $knpSnappy, \App\Service\dolibarrApi $dolibarrapi) { + $this->knpSnappy = $knpSnappy; + $this->dolibarrapi = $dolibarrapi; + } public function list(Request $request) { @@ -53,7 +57,6 @@ class OfferController extends AbstractController // Initialisation de l'enregistrement $em = $this->getDoctrine()->getManager(); $data = new Entity(); - // Création du formulaire $form = $this->createForm(Form::class,$data,array("mode"=>"submit")); @@ -82,7 +85,49 @@ class OfferController extends AbstractController 'form' => $form->createView() ]); } - + public function convert($type, $id, Request $request) + { + // Initialisation de l'enregistrement + $em = $this->getDoctrine()->getManager(); + $data = new Entity(); + $order = $this->dolibarrapi->getOrder($id, $type); + if (isset($order["ref_customer"]) && $order["ref_customer"]!= ""){ + $data->setRef($order["ref_customer"]); + } + if (isset($order["ref"]) && $order["ref"]!= ""){ + $data->setRef($order["ref"]); + } + $data->setName($order["customer_name"]); + $data->setQuantity($order["total_qty"]); + + // Création du formulaire + $form = $this->createForm(Form::class,$data,array("mode"=>"submit")); + + // Récupération des data du formulaire + $form->handleRequest($request); + + // Sur erreur + $this->getErrorForm(null,$form,$request,$data,"submit"); + + // Sur validation + if ($form->get('submit')->isClicked() && $form->isValid()) { + $data = $form->getData(); + $em->persist($data); + $em->flush(); + + // Retour à la liste + return $this->redirectToRoute($this->route); + } + + // Affichage du formulaire + return $this->render($this->render.'edit.html.twig', [ + 'useheader' => true, + 'usesidebar' => true, + $this->data => $data, + 'mode' => 'submit', + 'form' => $form->createView() + ]); + } public function update($id,Request $request) { // Initialisation de l'enregistrement @@ -171,7 +216,66 @@ class OfferController extends AbstractController return $this->redirectToRoute($this->route); } + public function getorders(){ + $em = $this->getDoctrine()->getManager(); + $orders = $this->dolibarrapi->getOrders(); + $propals = $this->dolibarrapi->getPropals(); + $tborders = []; + foreach($orders as $order){ + if (intval($order["statut"]> 0)){ + + $exist1 = $em->getRepository($this->entity)->findOneBy(['ref' => $order["ref"]]); + $exist2 = $em->getRepository($this->entity)->findOneBy(['ref' => $order["ref_client"]]); + + if (!$exist1 && !$exist2) { + $torder["id"]=$order["id"]; + $torder["customer_name"]=$order["customer_name"]; + $torder["ref"]=$order["ref"]; + $torder["ref_client"]=$order["ref_client"]; + $torder["statut"]=$order["statut"]; + $torder["total_ht"]=$order["total_ht"]; + $torder["total_tva"]=$order["total_tva"]; + $torder["total_ttc"]=$order["total_ttc"]; + $torder["type"] = "commande"; + + array_push($tborders, $torder); + } + } + } + foreach($propals as $propal){ + if (intval($propal["statut"]> 1)){ + $exist1 = false; + $exist2 = false; + if ($propal["ref"] != ""){ + $exist1 = $em->getRepository($this->entity)->findOneBy(['ref' => $propal["ref"]]); + } + if ($propal["ref_client"] != ""){ + $exist2 = $em->getRepository($this->entity)->findOneBy(['ref' => $propal["ref_client"]]); + } + + if (!$exist1 && !$exist2) { + $tpropal["id"]=$propal["id"]; + $tpropal["customer_name"]=$propal["customer_name"]; + $tpropal["ref"]=$propal["ref"]; + $tpropal["ref_client"]=$propal["ref_client"]; + $tpropal["statut"]=$propal["statut"]; + $tpropal["total_ht"]=$propal["total_ht"]; + $tpropal["total_tva"]=$propal["total_tva"]; + $tpropal["total_ttc"]=$propal["total_ttc"]; + $tpropal["type"] = "propal"; + + array_push($tborders, $tpropal); + } + } + } + + return $this->render($this->render.'getorders.html.twig', [ + 'useheader' => true, + 'usesidebar' => true, + 'orders' => $tborders + ]); + } protected function getErrorForm($id,$form,$request,$data,$mode) { if ($form->get('submit')->isClicked()&&$mode=="delete") { } diff --git a/src/schedule-2.0/src/Entity/Offer.php b/src/schedule-2.0/src/Entity/Offer.php index fd39472..46b9c72 100644 --- a/src/schedule-2.0/src/Entity/Offer.php +++ b/src/schedule-2.0/src/Entity/Offer.php @@ -59,6 +59,12 @@ class Offer */ private $active; + /** + * @ORM\Column(name="iddolibarr", type="decimal", scale=2) + * + */ + private $iddolibarr; + /** * @ORM\ManyToOne(targetEntity="Project", inversedBy="offers") */ @@ -145,7 +151,17 @@ class Offer return $this; } + public function getIdDolibarr(): ?string + { + return $this->iddolibarr; + } + public function setIdDolibarr(string $iddolibarr): self + { + $this->iddolibarr = $iddolibarr; + + return $this; + } public function getProject(): ?Project { return $this->project; diff --git a/src/schedule-2.0/src/Service/dolibarrApi.php b/src/schedule-2.0/src/Service/dolibarrApi.php new file mode 100644 index 0000000..1353568 --- /dev/null +++ b/src/schedule-2.0/src/Service/dolibarrApi.php @@ -0,0 +1,98 @@ +em = $em; + $this->container = $container; + $this->client = $client; + $this->doliuri = $this->container->getParameter('doliUri'); + + } + public function getOrders() { + + $start=new \Datetime(); + $start->sub(new \DateInterval('P6M')); + $response = $this->client->request('GET',$this->doliuri."/api/index.php/orders?sortfield=t.rowid&sortorder=DESC&limit=100&sqlfilters=(t.date_creation%3A%3E%3A'".$start->format("Ymd")."')"); + + $content = $response->toArray(); + $tbcontent = []; + foreach($content as $order){ + $order['customer_name'] = $this->getCustomer($order['socid']); + $qty = 0; + foreach($order['lines'] as $line){ + $qty = $qty + intval($line['qty']); + } + $order['total_qty'] = $qty; + array_push( $tbcontent, $order); + } + return $tbcontent; + } + public function getOrder($id, $type) { + + $start=new \Datetime(); + $start->sub(new \DateInterval('P6M')); + $order=[]; + if ($type =="commande") { + $response = $this->client->request('GET',$this->doliuri."/api/index.php/orders/".$id); + $order=$response->toArray(); + } + if ($type =="propal") { + $response = $this->client->request('GET',$this->doliuri."/api/index.php/proposals/".$id); + $order=$response->toArray(); + } + + $order['customer_name'] = $this->getCustomer($order['socid']); + $qty = 0; + foreach($order['lines'] as $line){ + $qty = $qty + intval($line['qty']); + } + $order['total_qty'] = $qty; + return $order; + } + public function getPropals() { + + $start=new \Datetime(); + $start->sub(new \DateInterval('P6M')); + + + $response = $this->client->request('GET',$this->doliuri."/api/index.php/proposals?sortfield=t.rowid&sortorder=DESC&limit=100&sqlfilters=(t.datec%3A%3E%3A'".$start->format("Ymd")."')"); + + $content = $response->toArray(); + $tbcontent = []; + foreach($content as $propal){ + $propal['customer_name'] = $this->getCustomer($propal['socid']); + $qty = 0; + foreach($propal['lines'] as $line){ + $qty = $qty + intval($line['qty']); + } + $propal['total_qty'] = $qty; + //$propal['customer_name'] = $this->getCustomer($propal['socid']); + array_push( $tbcontent, $propal); + } + return $tbcontent; + } + public function getCustomer($customerid) { + + $response = $this->client->request('GET',$this->doliuri."/api/index.php/thirdparties/".$customerid); + + $content = $response->toArray(); + + return $content['name']; + } +} \ No newline at end of file diff --git a/src/schedule-2.0/templates/Offer/list.html.twig b/src/schedule-2.0/templates/Offer/list.html.twig index 99f9f02..5c0a16e 100644 --- a/src/schedule-2.0/templates/Offer/list.html.twig +++ b/src/schedule-2.0/templates/Offer/list.html.twig @@ -22,6 +22,7 @@ Ajouter + Récupérer les commandes de Dolibarr