Merge branch 'api_dolibarr' of Cadoles/schedule into master

This commit is contained in:
2020-11-03 09:55:46 +01:00
committed by Gitea
9 changed files with 350 additions and 8 deletions

View File

@@ -64,4 +64,8 @@ CAS_FIRSTNAME=firstname
MAILER_URL=
## Sentry DSN
SENTRY_DSN=
SENTRY_DSN=
DOLIBARR_ACTIVE=false
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,9 @@ 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)%'
services:
# default configuration for services in *this* file

View File

@@ -17,21 +17,25 @@ 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)
{
$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,
"useheader" => true,
"usesidebar" => true,
"fgprint" => true,
"fgprint" => true,
]);
return new PdfResponse(
@@ -44,6 +48,7 @@ class OfferController extends AbstractController
"services" => $services,
"useheader" => true,
"usesidebar" => true,
"doliactive" => $this->getParameter('doliActive'),
]);
}
}
@@ -53,7 +58,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 +86,52 @@ 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();
$data->setIddolibarr($order["id"]);
$data->setTypedolibarr($type);
$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
@@ -170,8 +219,66 @@ 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();
$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,17 @@ class Offer
*/
private $active;
/**
* @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")
*/
@@ -145,7 +156,6 @@ class Offer
return $this;
}
public function getProject(): ?Project
{
return $this->project;
@@ -158,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;
}
}

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

@@ -0,0 +1,75 @@
{% 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 %}
<h1 class="page-header">
Nouvelles Commandes Dolibarr
</h1>
<p>Liste des dernières commandes/propal récupérées depuis Dolibarr.<br>
Les commandes doivent avoir un statut "Validée", et le Propal un statut "Signée" pour apparaître dans la liste.</p>
<div class="card">
<div class="dataTable_wrapper">
<table class="table table-striped table-bordered table-hover small" id="dataTables" style="width:100%">
<thead>
<tr>
<th width="70px" class="no-sort no-print">Action</th>
<th width="300px">client</th>
<th width="150px">ref</th>
<th width="150px">ref_client</th>
<th width="50">statut</th>
<th width="100px" class="text-center no-sort no-string">total_ht</th>
<th width="100px" class="text-center no-sort no-string">total_tva</th>
<th width="100px" class="text-center no-sort no-string">total_ttc</th>
</tr>
</thead>
<tbody>
{% for order in orders %}
<tr>
<td class="no-print">
<a href="{{path("app_offer_convert", {type:order.type, id:order.id})}}"><i class="fas fa-file-download"></i></a>
</td>
<td>{{order.customer_name}}</td>
{% if order.type =="commande" %}<td><a href="https://doli.cadoles.com/dolibarr/commande/card.php?id={{order.id}}" target="_blank">{{order.ref}}</td>{% endif %}
{% if order.type =="propal" %}<td><a href="https://doli.cadoles.com/dolibarr/comm/propal/card.php?id={{order.id}}" target="_blank">{{order.ref}}</td>{% endif %}
<td>{{order.ref_client}}</td>
<td>{{order.statut}}</td>
<td class="text-right">{{order.total_ht|number_format(2, '.', ' ')}}</td>
<td class="text-right">{{order.total_tva|number_format(2, '.', ' ')}}</td>
<td class="text-right">{{order.total_ttc|number_format(2, '.', ' ')}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% 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 %}

View File

@@ -22,6 +22,9 @@
</h1>
<a class="btn btn-success" href={{ path('app_offer_submit') }}>Ajouter</a>
{% if doliactive == "true" %}
<a class="btn btn-success" href={{ path('app_offer_getorders') }}>Récupérer les commandes de Dolibarr</a>
{% endif %}
<div class="custom-control custom-switch float-right">
<input type="checkbox" class="custom-control-input" id="switchactiveproject" {% if app.session.get('activeproject') %} checked {% endif %}>
@@ -84,6 +87,10 @@
<td>{{offer.project.customer.name}}</td>
<td>{{offer.project.name}}</td>
<td>{{offer.name}}</td>
{%if offer.iddolibarr %}
{% if offer.typedolibarr == "commande" %}<td><a href="https://doli.cadoles.com/dolibarr/commande/card.php?id={{offer.iddolibarr}}" target="_blank">{{offer.ref}}</td>{% endif %}
{% if offer.typedolibarr == "propal" %}<td><a href="https://doli.cadoles.com/dolibarr/comm/propal/card.php?id={{offer.iddolibarr}}" target="_blank">{{offer.ref}}</td>{% endif %}
{% endif %}
<td>{{offer.ref}}</td>
<td class="text-right">{{offer.quantity|number_format(2, '.', ' ')}}</td>
<td class="text-right">{{offer.pu|number_format(2, '.', ' ')}}</td>