finish create order when choosing thanato subcontractor on agenda
This commit is contained in:
parent
727df31516
commit
bfc30fbb35
8
gestion/lib/Constants/OrderStatusConstant.php
Normal file
8
gestion/lib/Constants/OrderStatusConstant.php
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace OCA\Gestion\Constants;
|
||||||
|
abstract class OrderStatusConstant
|
||||||
|
{
|
||||||
|
const ORDERED_KEY = "ORDERED";
|
||||||
|
}
|
||||||
10
gestion/lib/Constants/ThanatoTypeConstant.php
Normal file
10
gestion/lib/Constants/ThanatoTypeConstant.php
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace OCA\Gestion\Constants;
|
||||||
|
abstract class ThanatoTypeConstant
|
||||||
|
{
|
||||||
|
const DEFAULT_CALENDAR_UUID_FOR_DEVIS = "not-related";
|
||||||
|
const THANATO_TYPE_EMPLOYEE = "EMPLOYEE";
|
||||||
|
const THANATO_TYPE_SUBCONTRACTOR = "SUBCONTRACTOR";
|
||||||
|
}
|
||||||
@ -1736,6 +1736,17 @@ class Bdd {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getThanatoByUserUuid($userUuid){
|
||||||
|
$sql = "SELECT *
|
||||||
|
FROM ".$this->tableprefix."thanato as thanato
|
||||||
|
WHERE thanato.fk_user_uuid = ?";
|
||||||
|
$res = $this->execSQLNoJsonReturn($sql,array($userUuid));
|
||||||
|
if($res){
|
||||||
|
return $res[0];
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public function getThanatoIdByUserUuid($userUuid){
|
public function getThanatoIdByUserUuid($userUuid){
|
||||||
$sql = "SELECT min(id) as FIRST_INSERT_ID
|
$sql = "SELECT min(id) as FIRST_INSERT_ID
|
||||||
FROM ".$this->tableprefix."thanato
|
FROM ".$this->tableprefix."thanato
|
||||||
|
|||||||
122
gestion/lib/Db/OrderBdd.php
Normal file
122
gestion/lib/Db/OrderBdd.php
Normal file
@ -0,0 +1,122 @@
|
|||||||
|
<?php
|
||||||
|
namespace OCA\Gestion\Db;
|
||||||
|
|
||||||
|
use OCA\Gestion\Constants\OrderStatusConstant;
|
||||||
|
use OCA\Gestion\Constants\ThanatoTypeConstant;
|
||||||
|
use OCA\Gestion\Helpers\OrderHelpers;
|
||||||
|
use OCP\IDBConnection;
|
||||||
|
use \Datetime;
|
||||||
|
use OCA\Gestion\Constants\BddConstant;
|
||||||
|
use Ramsey\Uuid\Uuid;
|
||||||
|
|
||||||
|
class OrderBdd {
|
||||||
|
private $orderTablePrefix;
|
||||||
|
|
||||||
|
private $defaultTablePrefix;
|
||||||
|
private IDbConnection $pdo;
|
||||||
|
|
||||||
|
public function __construct(IDbConnection $db) {
|
||||||
|
$this->orderTablePrefix = BddConstant::DEFAULT_TABLE_PREFIX ."gestion_";
|
||||||
|
$this->defaultTablePrefix = BddConstant::DEFAULT_TABLE_PREFIX;
|
||||||
|
$this->pdo = $db;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function execSQL($sql, $conditions){
|
||||||
|
$stmt = $this->pdo->prepare($sql);
|
||||||
|
$stmt->execute($conditions);
|
||||||
|
$data = $stmt->fetchAll(\PDO::FETCH_ASSOC);
|
||||||
|
$stmt->closeCursor();
|
||||||
|
return json_encode($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function execSQLNoData($sql, $conditions){
|
||||||
|
$stmt = $this->pdo->prepare($sql);
|
||||||
|
$stmt->execute($conditions);
|
||||||
|
$stmt->closeCursor();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function execSQLNoJsonReturn($sql, $conditions){
|
||||||
|
$stmt = $this->pdo->prepare($sql);
|
||||||
|
$stmt->execute($conditions);
|
||||||
|
$data = $stmt->fetchAll(\PDO::FETCH_ASSOC);
|
||||||
|
$stmt->closeCursor();
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getLastOrderNumber(){
|
||||||
|
$sql = "SELECT max(orders.order_number) as order_last_number FROM "
|
||||||
|
.$this->orderTablePrefix."orders as orders";
|
||||||
|
|
||||||
|
$result = $this->execSQLNoJsonReturn($sql,[]);
|
||||||
|
$lastNumber = 0;
|
||||||
|
if(!empty($result)){
|
||||||
|
$lastNumber = $result[0]["order_last_number"];
|
||||||
|
}
|
||||||
|
return $lastNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function insertOrderProduct(int $orderId,int $productId){
|
||||||
|
$sql = "INSERT INTO ".$this->orderTablePrefix."order_product (fk_order_id, fk_product_id, quantity) VALUES (?,?,1)";
|
||||||
|
$this->execSQLNoData($sql, array($orderId, $productId));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getOrderByCalendarUuid($calendarUuid){
|
||||||
|
$sql = "SELECT * FROM ".$this->orderTablePrefix."orders as orders
|
||||||
|
WHERE orders.fk_calendar_uuid = ? LIMIT 1";
|
||||||
|
|
||||||
|
$result = $this->execSQLNoJsonReturn($sql,[$calendarUuid]);
|
||||||
|
if(!empty($result)){
|
||||||
|
return $result[0];
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function insertOrderProductsByProductIds(int $orderId,array $productIds){
|
||||||
|
if (!empty($productIds)) {
|
||||||
|
foreach ($productIds as $productId) {
|
||||||
|
$this->insertOrderProduct(
|
||||||
|
$orderId,
|
||||||
|
$productId
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function insertOrderFromVCalendarPropertyAndReturnId($thanatoId,$clientId,$locationId,$defuntId,$calendarUuid,$date,$idNextCloud = BddConstant::DEFAULT_ADMIN_ID_NEXTCLOUD){
|
||||||
|
$dateTime = new DateTime($date);
|
||||||
|
|
||||||
|
$lastOrderNumber = $this->getLastOrderNumber();
|
||||||
|
$thisOrderNumber = $lastOrderNumber + 1;
|
||||||
|
$thisOrderFullNumber = OrderHelpers::GetFullOrderNumberByDateAndOrderNumber($dateTime,$thisOrderNumber);
|
||||||
|
$sql = "INSERT INTO `".$this->orderTablePrefix."orders` (
|
||||||
|
`order_date`,
|
||||||
|
`order_number`,
|
||||||
|
`order_full_number`,
|
||||||
|
`fk_defunt_id`,
|
||||||
|
`fk_lieu_id`,
|
||||||
|
`fk_client_id`,
|
||||||
|
`fk_thanato_id`,
|
||||||
|
`fk_order_status_key`,
|
||||||
|
`fk_calendar_uuid`,
|
||||||
|
`id_nextcloud`
|
||||||
|
)
|
||||||
|
VALUES (?,?,?,?,?,?,?,?,?,?);";
|
||||||
|
$this->execSQLNoData($sql, array(
|
||||||
|
$date,
|
||||||
|
$thisOrderNumber,
|
||||||
|
$thisOrderFullNumber,
|
||||||
|
$defuntId,
|
||||||
|
$locationId,
|
||||||
|
$clientId,
|
||||||
|
$thanatoId,
|
||||||
|
OrderStatusConstant::ORDERED_KEY,
|
||||||
|
$calendarUuid,
|
||||||
|
$idNextCloud
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$order = $this->getOrderByCalendarUuid($calendarUuid);
|
||||||
|
return $order['id'];
|
||||||
|
}
|
||||||
|
}
|
||||||
14
gestion/lib/Helpers/OrderHelpers.php
Normal file
14
gestion/lib/Helpers/OrderHelpers.php
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace OCA\Gestion\Helpers;
|
||||||
|
|
||||||
|
class OrderHelpers
|
||||||
|
{
|
||||||
|
public static function GetFullOrderNumberByDateAndOrderNumber($datetime,$orderNumber,$prefix = "BDC",$companyKey = "H2F"){
|
||||||
|
$year = $datetime->format('y');
|
||||||
|
$month = $datetime->format('m');
|
||||||
|
$orderFullNumber = str_pad($orderNumber, 3, '0', STR_PAD_LEFT);
|
||||||
|
|
||||||
|
return "{$prefix} {$companyKey}/{$year}/{$month}/{$orderFullNumber}";
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -26,7 +26,9 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace OCA\Gestion\Service;
|
namespace OCA\Gestion\Service;
|
||||||
|
|
||||||
|
use OCA\Gestion\Constants\ThanatoTypeConstant;
|
||||||
use OCA\Gestion\Db\Bdd;
|
use OCA\Gestion\Db\Bdd;
|
||||||
|
use OCA\Gestion\Db\OrderBdd;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use OCA\Gestion\Helpers\VCalendarHelpers;
|
use OCA\Gestion\Helpers\VCalendarHelpers;
|
||||||
|
|
||||||
@ -37,9 +39,14 @@ class GestionService {
|
|||||||
/** @var LoggerInterface */
|
/** @var LoggerInterface */
|
||||||
private $logger;
|
private $logger;
|
||||||
|
|
||||||
|
/** @var \OCA\Gestion\Db\OrderBdd */
|
||||||
|
private $orderBdd;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
Bdd $gestionBdd,
|
Bdd $gestionBdd,
|
||||||
|
OrderBdd $orderBdd,
|
||||||
LoggerInterface $logger) {
|
LoggerInterface $logger) {
|
||||||
|
$this->orderBdd = $orderBdd;
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
$this->gestionBdd = $gestionBdd;
|
$this->gestionBdd = $gestionBdd;
|
||||||
}
|
}
|
||||||
@ -54,6 +61,29 @@ class GestionService {
|
|||||||
return $summaryValue;
|
return $summaryValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function GetThanatoFromVCalendarString(string $vCalendarString){
|
||||||
|
$thanato = null;
|
||||||
|
$thanatoNames = $this->GetAttendeesNameFromVCalendarString($vCalendarString);
|
||||||
|
if(count($thanatoNames) > 0){
|
||||||
|
$thanatoName = $thanatoNames[0];
|
||||||
|
$thanatoFromDb = $this->gestionBdd->getThanatoByUserUuid($thanatoName);
|
||||||
|
if($thanatoFromDb != null){
|
||||||
|
$thanato = $thanatoFromDb;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
//get from calendar object
|
||||||
|
$organizerName = $this->getPrincipalUsernameFromVCalendarString($vCalendarString);
|
||||||
|
if($organizerName != null){
|
||||||
|
$thanatoFromDb = $this->gestionBdd->getThanatoByUserUuid($organizerName);
|
||||||
|
if($thanatoFromDb != null){
|
||||||
|
$thanato = $thanatoFromDb;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $thanato;
|
||||||
|
}
|
||||||
|
|
||||||
private function GetThanatoIdFromVCalendarString(string $vCalendarString)
|
private function GetThanatoIdFromVCalendarString(string $vCalendarString)
|
||||||
{
|
{
|
||||||
$thanatoId = 0;
|
$thanatoId = 0;
|
||||||
@ -114,11 +144,55 @@ class GestionService {
|
|||||||
return $calendarStartDate;
|
return $calendarStartDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function HandleCreatedCalendarObject(string $vCalendarString){
|
private function IsOrderAlreadyCreated($calendarUuid){
|
||||||
|
$order = $this->orderBdd->getOrderByCalendarUuid($calendarUuid);
|
||||||
|
return $order != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function CreateOrderFromVCalendarString($vCalendarString,$thanatoId){
|
||||||
|
$calendarUuid = $this->GetCalendarUuidFromVCalendarString($vCalendarString);
|
||||||
|
$orderAlreadyCreated = $this->IsOrderAlreadyCreated($calendarUuid);
|
||||||
|
if($orderAlreadyCreated){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$nextcloudUser = $this->GetThanatoNameFromVCalendarString($vCalendarString);
|
||||||
|
$calendarSummary = $this->GetCalendarSummaryFromVCalendarString($vCalendarString);
|
||||||
|
$clientId = $this->GetClientIdFromVCalendarString($vCalendarString);
|
||||||
|
$locationId = $this->GetLocationIdFromVCalendarString($vCalendarString);
|
||||||
|
$calendarStartDate = $this->GetCalendarDateFromVCalendarString($vCalendarString);
|
||||||
|
$defuntId = $this->gestionBdd->insertDefuntByNameAndReturnId($calendarSummary);
|
||||||
|
$orderId = $this->orderBdd->insertOrderFromVCalendarPropertyAndReturnId(
|
||||||
|
$thanatoId,
|
||||||
|
$clientId,
|
||||||
|
$locationId,
|
||||||
|
$defuntId,
|
||||||
|
$calendarUuid,
|
||||||
|
$calendarStartDate,
|
||||||
|
$nextcloudUser);
|
||||||
|
$productsValue = $this->GetArticlesNameFromVCalendarString($vCalendarString);
|
||||||
|
if(!empty($productsValue)){
|
||||||
|
$productIds = $this->gestionBdd->getArticleIdsByArticleReferences($productsValue);
|
||||||
|
$this->orderBdd->insertOrderProductsByProductIds($orderId, $productIds);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function HandleCreatedCalendarObject(string $vCalendarString){
|
||||||
|
$thanato = $this->GetThanatoFromVCalendarString($vCalendarString);
|
||||||
|
if($thanato != null){
|
||||||
|
$thanatoId = $thanato["id"];
|
||||||
|
$thanatoIsSubcontractor = $thanato["fk_thanato_type_key"] === ThanatoTypeConstant::THANATO_TYPE_SUBCONTRACTOR;
|
||||||
|
if($thanatoIsSubcontractor){
|
||||||
|
$this->CreateOrderFromVCalendarString($vCalendarString,$thanatoId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
$thanatoId = 0;
|
||||||
|
}
|
||||||
$calendarSummary = $this->GetCalendarSummaryFromVCalendarString($vCalendarString);
|
$calendarSummary = $this->GetCalendarSummaryFromVCalendarString($vCalendarString);
|
||||||
$clientId = $this->GetClientIdFromVCalendarString($vCalendarString);
|
$clientId = $this->GetClientIdFromVCalendarString($vCalendarString);
|
||||||
$locationId = $this->GetLocationIdFromVCalendarString($vCalendarString);
|
$locationId = $this->GetLocationIdFromVCalendarString($vCalendarString);
|
||||||
$thanatoId = $this->GetThanatoIdFromVCalendarString($vCalendarString);
|
|
||||||
$calendarUuid = $this->GetCalendarUuidFromVCalendarString($vCalendarString);
|
$calendarUuid = $this->GetCalendarUuidFromVCalendarString($vCalendarString);
|
||||||
$userName = $this->GetThanatoNameFromVCalendarString($vCalendarString);
|
$userName = $this->GetThanatoNameFromVCalendarString($vCalendarString);
|
||||||
$devisAlreadyCreated = $this->IsDevisAlreadyCreated($clientId,$locationId,$thanatoId,$calendarSummary,$calendarUuid);
|
$devisAlreadyCreated = $this->IsDevisAlreadyCreated($clientId,$locationId,$thanatoId,$calendarSummary,$calendarUuid);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user