first implementation of dolibarr api

This commit is contained in:
2020-10-12 14:49:58 +02:00
parent bceee0582e
commit 1691620986
8 changed files with 242 additions and 4 deletions

View File

@@ -64,4 +64,7 @@ CAS_FIRSTNAME=firstname
MAILER_URL=
## Sentry DSN
SENTRY_DSN=
SENTRY_DSN=
DOLIBARR_API_KEY=
DOLIBARR_URI=

View File

@@ -0,0 +1,6 @@
# config/packages/framework.yaml
framework:
http_client:
default_options:
headers:
DOLAPIKEY: '%env(DOLIBARR_API_KEY)%'

View File

@@ -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

View File

@@ -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

View File

@@ -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") {
}

View File

@@ -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;

View File

@@ -0,0 +1,98 @@
<?php
namespace App\Service;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class dolibarrApi
{
private $container;
private $em;
private $session;
private $client;
private $doliuri;
public function __construct(EntityManagerInterface $em, ContainerInterface $container, HttpClientInterface $client )
{
$this->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'];
}
}

View File

@@ -22,6 +22,7 @@
</h1>
<a class="btn btn-success" href={{ path('app_offer_submit') }}>Ajouter</a>
<a class="btn btn-success" href={{ path('app_offer_getorders') }}>Récupérer les commandes de Dolibarr</a>
<div class="custom-control custom-switch float-right">
<input type="checkbox" class="custom-control-input" id="switchactiveproject" {% if app.session.get('activeproject') %} checked {% endif %}>