From 16916209865de738c6efcf9bdb2bb4178f69a41a Mon Sep 17 00:00:00 2001 From: Matthieu Lamalle Date: Mon, 12 Oct 2020 14:49:58 +0200 Subject: [PATCH 1/6] 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
-- 2.17.1 From e5863fdd0a8e9b04d55a5dbe5bfa25ee465ad5f7 Mon Sep 17 00:00:00 2001 From: Matthieu Lamalle Date: Mon, 12 Oct 2020 14:50:30 +0200 Subject: [PATCH 2/6] set template --- .../templates/Offer/getorders.html.twig | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/schedule-2.0/templates/Offer/getorders.html.twig diff --git a/src/schedule-2.0/templates/Offer/getorders.html.twig b/src/schedule-2.0/templates/Offer/getorders.html.twig new file mode 100644 index 0000000..9a9529d --- /dev/null +++ b/src/schedule-2.0/templates/Offer/getorders.html.twig @@ -0,0 +1,74 @@ +{% extends "base.html.twig" %} + +{% block localstyle %} + td { + padding:5px !important; + } + {% if fgprint is defined and fgprint %} + table { font-size:10px;} + th,td { + border: 1px solid #37474F; + } + thead { + display: table-header-group; + } + tr { page-break-inside: avoid; } + {%endif%} +{% endblock %} + +{% block body %} +

+ Nouvelles Commandes Dolibarr +

+ +

+
+
+ + + + + + + + + + + + + + + {% for order in orders %} + + + + + {% if order.type =="commande" %}{% endif %} + {% if order.type =="propal" %}{% endif %} + + + + + + + + {% endfor %} + +
Actionclientrefref_clientstatuttotal_httotal_tvatotal_ttc
+ + {{order.customer_name}}{{order.ref}}{{order.ref}}{{order.ref_client}}{{order.statut}}{{order.total_ht|number_format(2, '.', ' ')}}{{order.total_tva|number_format(2, '.', ' ')}}{{order.total_ttc|number_format(2, '.', ' ')}}
+
+
+ +{% endblock %} +{% block localjavascript %} + $(document).ready(function() { + $('#dataTables').DataTable({ + columnDefs: [ { "targets": "no-sort", "orderable": false }, { "targets": "no-string", "type" : "num" } ], + responsive: true, + iDisplayLength: 100, + order: [[ 2, "asc" ]] + }); + }); + +{% endblock %} -- 2.17.1 From bb7e66f0c9148d7c2ddf987914d3f7b23eacc3d0 Mon Sep 17 00:00:00 2001 From: Matthieu Lamalle Date: Tue, 13 Oct 2020 10:42:48 +0200 Subject: [PATCH 3/6] Add link on dolibarr on convertion --- .../src/Controller/OfferController.php | 6 ++- src/schedule-2.0/src/Entity/Offer.php | 42 +++++++++++++------ 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/schedule-2.0/src/Controller/OfferController.php b/src/schedule-2.0/src/Controller/OfferController.php index 55330da..9560240 100755 --- a/src/schedule-2.0/src/Controller/OfferController.php +++ b/src/schedule-2.0/src/Controller/OfferController.php @@ -29,7 +29,7 @@ class OfferController extends AbstractController { $em = $this->getDoctrine()->getManager(); $services=$em->getRepository("App:Service")->findAllOfferActive($this->get('session')->get('activeproject'),$this->get('session')->get('activeoffer'),$this->get('session')->get('idservice')); - + if($request->query->get('fgprint')) { $render = $this->renderView($this->render.'list.html.twig',[ "services" => $services, @@ -111,7 +111,10 @@ class OfferController extends AbstractController // Sur validation if ($form->get('submit')->isClicked() && $form->isValid()) { + $data = $form->getData(); + $data->setIddolibarr($order["id"]); + $data->setTypedolibarr($type); $em->persist($data); $em->flush(); @@ -215,7 +218,6 @@ class OfferController extends AbstractController $this->get('session')->set('activeoffer',!$this->get('session')->get('activeoffer')); return $this->redirectToRoute($this->route); } - public function getorders(){ $em = $this->getDoctrine()->getManager(); $orders = $this->dolibarrapi->getOrders(); diff --git a/src/schedule-2.0/src/Entity/Offer.php b/src/schedule-2.0/src/Entity/Offer.php index 46b9c72..c8996a4 100644 --- a/src/schedule-2.0/src/Entity/Offer.php +++ b/src/schedule-2.0/src/Entity/Offer.php @@ -60,10 +60,15 @@ class Offer private $active; /** - * @ORM\Column(name="iddolibarr", type="decimal", scale=2) + * @ORM\Column(name="iddolibarr", type="decimal", scale=5, nullable=true) * */ private $iddolibarr; + /** + * @ORM\Column(name="typedolibarr", type="string", nullable=true) + * + */ + private $typedolibarr; /** * @ORM\ManyToOne(targetEntity="Project", inversedBy="offers") @@ -151,17 +156,6 @@ 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; @@ -174,5 +168,29 @@ class Offer return $this; } + public function getIddolibarr(): ?string + { + return $this->iddolibarr; + } + + public function setIddolibarr(?string $iddolibarr): self + { + $this->iddolibarr = $iddolibarr; + + return $this; + } + + public function getTypedolibarr(): ?string + { + return $this->typedolibarr; + } + + public function setTypedolibarr(?string $typedolibarr): self + { + $this->typedolibarr = $typedolibarr; + + return $this; + } + } -- 2.17.1 From 5ea06c9aece8d51ea49bb727ad5a486697400398 Mon Sep 17 00:00:00 2001 From: Matthieu Lamalle Date: Tue, 13 Oct 2020 10:43:09 +0200 Subject: [PATCH 4/6] set template for link orders --- src/schedule-2.0/src/Service/dolibarrApi.php | 4 ++-- src/schedule-2.0/templates/Offer/getorders.html.twig | 3 ++- src/schedule-2.0/templates/Offer/list.html.twig | 4 ++++ 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/schedule-2.0/src/Service/dolibarrApi.php b/src/schedule-2.0/src/Service/dolibarrApi.php index 1353568..1bf2e1b 100644 --- a/src/schedule-2.0/src/Service/dolibarrApi.php +++ b/src/schedule-2.0/src/Service/dolibarrApi.php @@ -39,7 +39,7 @@ class dolibarrApi $qty = $qty + intval($line['qty']); } $order['total_qty'] = $qty; - array_push( $tbcontent, $order); + array_push($tbcontent, $order); } return $tbcontent; } @@ -65,11 +65,11 @@ class dolibarrApi $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")."')"); diff --git a/src/schedule-2.0/templates/Offer/getorders.html.twig b/src/schedule-2.0/templates/Offer/getorders.html.twig index 9a9529d..55fe391 100644 --- a/src/schedule-2.0/templates/Offer/getorders.html.twig +++ b/src/schedule-2.0/templates/Offer/getorders.html.twig @@ -21,7 +21,8 @@ Nouvelles Commandes Dolibarr -

+

Liste des dernières commandes/propal récupérées depuis Dolibarr.
+ Les commandes doivent avoir un statut "Validée", et le Propal un statut "Signée" pour apparaître dans la liste.

diff --git a/src/schedule-2.0/templates/Offer/list.html.twig b/src/schedule-2.0/templates/Offer/list.html.twig index 5c0a16e..e299072 100644 --- a/src/schedule-2.0/templates/Offer/list.html.twig +++ b/src/schedule-2.0/templates/Offer/list.html.twig @@ -85,6 +85,10 @@ + {%if offer.iddolibarr %} + {% if offer.typedolibarr == "commande" %}{% endif %} + {% if offer.typedolibarr == "propal" %}{% endif %} + {% endif %} -- 2.17.1 From 62e7339057208edef0a53be828389e221b333860 Mon Sep 17 00:00:00 2001 From: Matthieu Lamalle Date: Wed, 14 Oct 2020 17:02:41 +0200 Subject: [PATCH 5/6] ajout variable pour activer dolibarr --- src/schedule-2.0/.env | 1 + src/schedule-2.0/config/services.yaml | 1 + src/schedule-2.0/src/Controller/OfferController.php | 3 ++- src/schedule-2.0/templates/Offer/list.html.twig | 2 ++ 4 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/schedule-2.0/.env b/src/schedule-2.0/.env index c5152d3..75b159f 100644 --- a/src/schedule-2.0/.env +++ b/src/schedule-2.0/.env @@ -66,5 +66,6 @@ MAILER_URL= ## Sentry DSN SENTRY_DSN= +DOLIBARR_ACTIVE=false DOLIBARR_API_KEY= DOLIBARR_URI= diff --git a/src/schedule-2.0/config/services.yaml b/src/schedule-2.0/config/services.yaml index 17883fb..cb5a388 100644 --- a/src/schedule-2.0/config/services.yaml +++ b/src/schedule-2.0/config/services.yaml @@ -21,6 +21,7 @@ parameters: casFirstname: '%env(resolve:CAS_FIRSTNAME)%' officeHourStart: '%env(resolve:OFFICE_HOUR_START)%' officeHourEnd: '%env(resolve:OFFICE_HOUR_END)%' + doliActive: '%env(resolve:DOLIBARR_ACTIVE)%' doliApiKey: '%env(resolve:DOLIBARR_API_KEY)%' doliUri: '%env(resolve:DOLIBARR_URI)%' diff --git a/src/schedule-2.0/src/Controller/OfferController.php b/src/schedule-2.0/src/Controller/OfferController.php index 9560240..1b4512b 100755 --- a/src/schedule-2.0/src/Controller/OfferController.php +++ b/src/schedule-2.0/src/Controller/OfferController.php @@ -35,7 +35,7 @@ class OfferController extends AbstractController "services" => $services, "useheader" => true, "usesidebar" => true, - "fgprint" => true, + "fgprint" => true, ]); return new PdfResponse( @@ -48,6 +48,7 @@ class OfferController extends AbstractController "services" => $services, "useheader" => true, "usesidebar" => true, + "doliactive" => $this->getParameter('doliActive'), ]); } } diff --git a/src/schedule-2.0/templates/Offer/list.html.twig b/src/schedule-2.0/templates/Offer/list.html.twig index e299072..ab15892 100644 --- a/src/schedule-2.0/templates/Offer/list.html.twig +++ b/src/schedule-2.0/templates/Offer/list.html.twig @@ -22,7 +22,9 @@ Ajouter + {% if doliactive %} Récupérer les commandes de Dolibarr + {% endif %}
-- 2.17.1 From 0e159604e6e7483c958b4acc6ad8ffe1e21b8e58 Mon Sep 17 00:00:00 2001 From: Matthieu Lamalle Date: Wed, 14 Oct 2020 17:09:26 +0200 Subject: [PATCH 6/6] maj condition affichage dolibarr --- src/schedule-2.0/templates/Offer/list.html.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/schedule-2.0/templates/Offer/list.html.twig b/src/schedule-2.0/templates/Offer/list.html.twig index ab15892..3cfa755 100644 --- a/src/schedule-2.0/templates/Offer/list.html.twig +++ b/src/schedule-2.0/templates/Offer/list.html.twig @@ -22,7 +22,7 @@ Ajouter - {% if doliactive %} + {% if doliactive == "true" %} Récupérer les commandes de Dolibarr {% endif %} -- 2.17.1
{{offer.project.customer.name}} {{offer.project.name}} {{offer.name}}{{offer.ref}}{{offer.ref}}{{offer.ref}} {{offer.quantity|number_format(2, '.', ' ')}} {{offer.pu|number_format(2, '.', ' ')}}