namespace backend\controllers; | namespace backend\controllers; | ||||
use yii\filters\AccessControl; | use yii\filters\AccessControl; | ||||
use yii\filters\VerbFilter; | |||||
class ProducerInvoiceController extends BackendController | class ProducerInvoiceController extends BackendController | ||||
{ | { | ||||
'allow' => true, | 'allow' => true, | ||||
'roles' => ['@'], | 'roles' => ['@'], | ||||
'matchCallback' => function ($rule, $action) { | 'matchCallback' => function ($rule, $action) { | ||||
return $this->getUserModule() | |||||
return | |||||
$this->getParameterBag()->get('dolibarrApiKey') | |||||
&& $this->getUserModule() | |||||
->getAuthorizationChecker() | ->getAuthorizationChecker() | ||||
->isGrantedAsProducer($this->getUserCurrent()); | ->isGrantedAsProducer($this->getUserCurrent()); | ||||
} | } | ||||
public function actionIndex() | public function actionIndex() | ||||
{ | { | ||||
$producerCurrent = $this->getProducerCurrent(); | |||||
$invoicesArray = []; | |||||
if($producerCurrent->dolibarr_socid) { | |||||
$invoicesArray = \Yii::$app->dolibarrApi->getInvoices($producerCurrent->dolibarr_socid); | |||||
} | |||||
return $this->render('index', [ | return $this->render('index', [ | ||||
'invoicesArray' => $invoicesArray | |||||
'invoicesArray' => $this->getProducerModule() | |||||
->getDolibarrUtils() | |||||
->getDolibarrProducerInvoices($this->getProducerCurrent()) | |||||
]); | ]); | ||||
} | } | ||||
public function actionDownload(string $filename) | |||||
public function actionDownload(int $idDolibarrInvoice) | |||||
{ | { | ||||
$documentDownload = \Yii::$app->dolibarrApi->downloadInvoice($filename); | |||||
return \Yii::$app->response->sendContentAsFile(base64_decode($documentDownload['content']), $documentDownload['filename'], [ | |||||
'mimeType' => $documentDownload['content-type'] | |||||
]); | |||||
$documentDownload = \Yii::$app->dolibarrApi->downloadInvoice($idDolibarrInvoice); | |||||
if($documentDownload) { | |||||
return \Yii::$app->response->sendContentAsFile(base64_decode($documentDownload['content']), $documentDownload['filename'], [ | |||||
'mimeType' => $documentDownload['content-type'] | |||||
]); | |||||
} | |||||
else { | |||||
$this->addFlash('error', 'Facture introuvable'); | |||||
return $this->redirectReferer(); | |||||
} | |||||
} | } | ||||
} | } |
'label' => 'Mes factures', | 'label' => 'Mes factures', | ||||
'icon' => 'clone', | 'icon' => 'clone', | ||||
'url' => ['/producer-invoice/index'], | 'url' => ['/producer-invoice/index'], | ||||
'visible' => $isUserCurrentGrantedAsProducer, | |||||
'visible' => $isUserCurrentGrantedAsProducer && Yii::$app->parameterBag->get('dolibarrApiKey'), | |||||
'active' => Yii::$app->controller->id == 'producer-invoice', | 'active' => Yii::$app->controller->id == 'producer-invoice', | ||||
], | ], | ||||
[ | [ |
$this->addBreadcrumb($this->getTitle()) ; | $this->addBreadcrumb($this->getTitle()) ; | ||||
?> | ?> | ||||
<div class="callout callout-info"> | |||||
<span class="glyphicon glyphicon-info-sign"></span> Les factures et les réglements sont saisis en début de mois. | |||||
</div> | |||||
<?php if($invoicesArray && count($invoicesArray)): ?> | <?php if($invoicesArray && count($invoicesArray)): ?> | ||||
<table class="table table-striped table-bordered"> | <table class="table table-striped table-bordered"> | ||||
<thead> | <thead> | ||||
<td><?= Price::format($invoice['total_ttc']); ?></td> | <td><?= Price::format($invoice['total_ttc']); ?></td> | ||||
<td><?= ($invoice['remaintopay'] > 0) ? '<span class="label label-warning">Impayée</span>' : '<span class="label label-success">Payée</span>'; ?></td> | <td><?= ($invoice['remaintopay'] > 0) ? '<span class="label label-warning">Impayée</span>' : '<span class="label label-success">Payée</span>'; ?></td> | ||||
<td> | <td> | ||||
<a class="btn btn-default" href="<?= $this->getUrlManagerBackend()->createUrl(['producer-invoice/download', 'filename' => str_replace('facture/', '', $invoice['last_main_doc'])]); ?>"> | |||||
<a class="btn btn-default" href="<?= $this->getUrlManagerBackend()->createUrl(['producer-invoice/download', 'idDolibarrInvoice' => $invoice['id']]); ?>"> | |||||
<span class="glyphicon glyphicon-download-alt"></span> Télécharger | <span class="glyphicon glyphicon-download-alt"></span> Télécharger | ||||
</a> | </a> | ||||
</td> | </td> |
namespace common\components; | namespace common\components; | ||||
use Psr\Http\Message\ResponseInterface; | |||||
class DolibarrApi extends AbstractApi | class DolibarrApi extends AbstractApi | ||||
{ | { | ||||
const RESOURCE_INVOICES = 'invoices'; | const RESOURCE_INVOICES = 'invoices'; | ||||
const RESOURCE_PRODUCTS = 'products'; | const RESOURCE_PRODUCTS = 'products'; | ||||
const RESOURCE_DOCUMENTS = 'documents'; | const RESOURCE_DOCUMENTS = 'documents'; | ||||
public function getInvoices(int $idThirdParty) | |||||
public function getInvoicesByThirParty(int $idThirdParty) | |||||
{ | { | ||||
return $this->get(self::RESOURCE_INVOICES, [ | return $this->get(self::RESOURCE_INVOICES, [ | ||||
'sortfield' => 't.rowid', | 'sortfield' => 't.rowid', | ||||
]); | ]); | ||||
} | } | ||||
public function downloadInvoice(string $originalFile) | |||||
public function getInvoice(int $idInvoice) | |||||
{ | { | ||||
return $this->get(self::RESOURCE_DOCUMENTS.'/download', [ | |||||
'modulepart' => 'facture', | |||||
'original_file' => $originalFile | |||||
return $this->get(self::RESOURCE_INVOICES.'/'.$idInvoice, [ | |||||
'contact_list' => 1 | |||||
]); | ]); | ||||
} | } | ||||
public function downloadInvoice(string $idInvoice) | |||||
{ | |||||
$invoice = $this->getInvoice($idInvoice); | |||||
if($invoice && isset($invoice['last_main_doc'])) { | |||||
$originalFilename = str_replace('facture/', '', $invoice['last_main_doc']); | |||||
return $this->get(self::RESOURCE_DOCUMENTS.'/download', [ | |||||
'modulepart' => 'facture', | |||||
'original_file' => $originalFilename | |||||
]); | |||||
} | |||||
return null; | |||||
} | |||||
public function createInvoice(int $idUser) | public function createInvoice(int $idUser) | ||||
{ | { | ||||
return $this->post(self::RESOURCE_INVOICES, [ | return $this->post(self::RESOURCE_INVOICES, [ |
use common\components\BusinessLogic; | use common\components\BusinessLogic; | ||||
use common\components\BusinessLogicTrait; | use common\components\BusinessLogicTrait; | ||||
use common\components\ParameterBag; | |||||
use common\logic\User\User\Model\User; | use common\logic\User\User\Model\User; | ||||
use yii; | use yii; | ||||
use yii\web\Response; | use yii\web\Response; | ||||
{ | { | ||||
return $this->redirect(Yii::$app->request->referrer ?: Yii::$app->homeUrl); | return $this->redirect(Yii::$app->request->referrer ?: Yii::$app->homeUrl); | ||||
} | } | ||||
public function getParameterBag(): ParameterBag | |||||
{ | |||||
return Yii::$app->parameterBag; | |||||
} | |||||
} | } | ||||
?> | ?> |
$this->producerRepository = $this->loadService(ProducerRepository::class); | $this->producerRepository = $this->loadService(ProducerRepository::class); | ||||
} | } | ||||
public function getDolibarrProducerInvoices(Producer $producer): array | |||||
{ | |||||
$invoicesArray = []; | |||||
if($producer->dolibarr_socid) { | |||||
$invoicesArray = $this->dolibarrApi->getInvoicesByThirParty($producer->dolibarr_socid); | |||||
} | |||||
return $invoicesArray; | |||||
} | |||||
public function generateDolibarrProducerInvoice(Producer $producer) | public function generateDolibarrProducerInvoice(Producer $producer) | ||||
{ | { | ||||
$idProduct = $this->getDolibarrProductId($producer); | $idProduct = $this->getDolibarrProductId($producer); |