2021-02-19 16:32:08 +01:00
< ? php
namespace App\Http\Controllers\Api\v1 ;
use App\Http\Controllers\Controller ;
use App\Http\Resources\StatusResource ;
use App\Mail\CourrierArcadiaError ;
use App\Models\Customer ;
use DOMDocument ;
use Illuminate\Http\Request ;
use Illuminate\Support\Facades\Log ;
use Illuminate\Support\Facades\Mail ;
use Illuminate\Support\Facades\Storage ;
use Illuminate\Support\Facades\Validator ;
class ArcadiaController extends Controller
{
public function outgoingMail ( Request $request )
{
$validator = Validator :: make ( $request -> all (), [
'crm_id' => 'required' ,
'fields' => 'required|json' ,
'type' => 'required' ,
2021-05-17 17:39:56 +02:00
'shipping' => 'required_if:type,default|json'
2021-02-19 16:32:08 +01:00
]);
if ( $validator -> fails ())
return new StatusResource ([ 'status' => 'KO' , 'code' => 201 , 'message' => 'Error on required fields.' , 'errors' => $validator -> errors ()]);
$brandId = $request -> brand_id ;
$crmId = $request -> crm_id ;
$fields = json_decode ( $request -> fields );
$type = $request -> type ;
2021-05-17 17:39:56 +02:00
$shipping = $type === 'default' ? json_decode ( $request -> shipping ) : false ;
2021-02-19 16:32:08 +01:00
$customer = Customer :: where ( 'id' , $crmId ) -> first ();
if ( ! $customer )
return new StatusResource ([ 'status' => 'KO' , 'code' => 202 , 'message' => 'User not found.' ]);
$customerInfos = $customer -> infos () -> where ( 'brand_id' , $brandId ) -> first ();
if ( ! $customerInfos )
return new StatusResource ([ 'status' => 'KO' , 'code' => 202 , 'message' => 'User not found.' ]);
$check = $this -> checkRequiredFields ( $fields , $type );
if ( $check [ 'status' ] !== 'OK' )
return new StatusResource ( $check );
2021-05-17 17:39:56 +02:00
if ( $type === 'default' ) {
$check = $this -> checkShippingRequiredFields ( $shipping );
if ( $check [ 'status' ] !== 'OK' )
return new StatusResource ( $check );
}
$xml = $this -> makeXML ( $fields , $type , $customer , $customerInfos , $shipping );
2021-02-19 16:32:08 +01:00
try {
2021-05-12 18:39:53 +02:00
Storage :: disk ( 'arcadia' ) -> put ( date ( " Ymd_His " ) . microtime ( true ) . " .xml " , $xml );
2021-02-19 16:32:08 +01:00
return new StatusResource ([ 'status' => 'OK' ]);
} catch ( \Exception $e ){
Log :: critical ( " ARCADIA XML CREATION FAILED : " . $e -> getMessage ());
Mail :: to ( config ( 'mail.MAIL_ADMIN' )) -> send ( new CourrierArcadiaError ( " ARCADIA XML CREATION FAILED " , " Une erreur est survenue lors de la création du fichier XML : " . $e -> getMessage (), $xml ));
return new StatusResource ([ 'status' => 'KO' , 'code' => 204 , 'message' => 'Arcadia XML Creation failed.' ]);
}
}
2021-05-17 17:39:56 +02:00
private function checkShippingRequiredFields ( $fields )
{
$test = [
'firstname' ,
'lastname' ,
'address' ,
'city' ,
'postcode'
];
foreach ( $test as $t ) {
if ( ! array_key_exists ( $t , $fields )){
$errors [] = [ 'field' => $t , 'error' => 'required' ];
}
}
if ( ! empty ( $errors ))
return [ 'status' => 'KO' , 'code' => 203 , 'message' => $errors ];
return [ 'status' => 'OK' ];
}
2021-02-19 16:32:08 +01:00
private function checkRequiredFields ( $fields , $type )
{
$errors = [];
$key = array_search ( 'operation' , array_column ( $fields -> root , 'fieldName' ));
if ( $key === false )
$errors [] = [ 'field' => 'operation' , 'error' => 'required' ];
2021-02-23 15:45:35 +01:00
$key = array_search ( 'support_code' , array_column ( $fields -> orderLine , 'fieldName' ));
2021-02-19 16:32:08 +01:00
if ( $key === false )
$errors [] = [ 'field' => 'support_code' , 'error' => 'required' ];
2021-02-23 15:45:35 +01:00
$key = array_search ( 'label_type_support' , array_column ( $fields -> orderLine , 'fieldName' ));
2021-02-19 16:32:08 +01:00
if ( $key === false )
$errors [] = [ 'field' => 'label_type_support' , 'error' => 'required' ];
$key = array_search ( 'order_date' , array_column ( $fields -> root , 'fieldName' ));
if ( $key === false )
$errors [] = [ 'field' => 'order_date' , 'error' => 'required' ];
if ( ! preg_match ( " /^(((0[1-9]|[12] \ d|3[01]) \ /(0[13578]|1[02]) \ /((19|[2-9] \ d) \ d { 2}))|((0[1-9]|[12] \ d|30) \ /(0[13456789]|1[012]) \ /((19|[2-9] \ d) \ d { 2}))|((0[1-9]|1 \ d|2[0-8]) \ /02 \ /((19|[2-9] \ d) \ d { 2}))|(29 \ /02 \ /((1[6-9]|[2-9] \ d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))) $ / " , $fields -> root [ $key ] -> fieldValue )) {
$errors [] = [ 'field' => 'order_date' , 'error' => 'format must be DD/MM/YYYY' ];
}
if ( $type === 'default' ){
$key = array_search ( 'order_num' , array_column ( $fields -> root , 'fieldName' ));
if ( $key === false )
$errors [] = [ 'field' => 'order_num' , 'error' => 'required for default type' ];
2021-02-26 11:08:15 +01:00
if ( ! is_numeric ( $fields -> root [ $key ] -> fieldValue ) || strlen ( $fields -> root [ $key ] -> fieldValue ) !== 9 )
$errors [] = [ 'field' => 'order_num' , 'error' => 'format must be 9 numeric characters' ];
2021-02-19 16:32:08 +01:00
$key = array_search ( 'state_order_cb' , array_column ( $fields -> root , 'fieldName' ));
if ( $key === false )
$errors [] = [ 'field' => 'state_order_cb' , 'error' => 'required for default type' ];
if ( $fields -> root [ $key ] -> fieldValue !== 1 && $fields -> root [ $key ] -> fieldValue !== 'Null' )
$errors [] = [ 'field' => 'state_order_cb' , 'error' => 'value must be 1 or Null (Yes Null not NULL)' ];
$orderCB = $fields -> root [ $key ] -> fieldValue ;
$key = array_search ( 'state_order_check' , array_column ( $fields -> root , 'fieldName' ));
if ( $key === false )
$errors [] = [ 'field' => 'state_order_check' , 'error' => 'required for default type' ];
if ( $fields -> root [ $key ] -> fieldValue !== 1 && $fields -> root [ $key ] -> fieldValue !== 'Null' )
$errors [] = [ 'field' => 'state_order_check' , 'error' => 'value must be 1 or Null (Yes Null not NULL)' ];
$orderCheck = $fields -> root [ $key ] -> fieldValue ;
if ( $orderCB === $orderCheck )
$errors [] = [ 'field' => 'state_order' , 'error' => 'state_order_cb and state_order_check values must be different' ];
$key = array_search ( 'package_unit_weight' , array_column ( $fields -> orderLine , 'fieldName' ));
if ( $key === false )
$errors [] = [ 'field' => 'package_unit_weight' , 'error' => 'required for default type' ];
} elseif ( $type === 'game' ){
$key = array_search ( 'code_unique' , array_column ( $fields -> orderLine , 'fieldName' ));
if ( $key === false )
$errors [] = [ 'field' => 'code_unique' , 'error' => 'required for game type' ];
}
if ( ! empty ( $errors ))
2021-05-12 18:39:53 +02:00
return [ 'status' => 'KO' , 'code' => 203 , 'message' => $errors ];
2021-02-19 16:32:08 +01:00
return [ 'status' => 'OK' ];
}
2021-05-17 17:39:56 +02:00
private function makeXML ( $fields , $type , $customer , $customerInfos , $shipping )
2021-02-19 16:32:08 +01:00
{
$pivot = [
2021-02-26 11:08:15 +01:00
'order_num' => 'Num_Commande' , // required if type = default - format 9 numeric characters
2021-02-19 16:32:08 +01:00
'order_date' => 'Date_Commande' , // required - format : DD/MM/YYYY
'operation' => 'Operation' , // required
'support_code' => 'Code_Support' , // required
'label_type_support' => 'Libelle_Type_Support' , // required
'package_unit_weight' => 'Poids_Unitaire_Colis' , // required if type = default
'qty' => 'Quantite' ,
'code_on_pack' => 'Code_on_Pack' ,
'code_unique' => 'Code_Unique' , // required if type = game
'state_order_cb' => 'Etat_Commande_CB' , // required if type = default - format 1 ou Null (oui Null et pas NULL)
'state_order_check' => 'Etat_Commande_Cheque' , // required if type = default - format 1 ou Null (oui Null et pas NULL)
];
$dom = new DOMDocument ( '1.0' , 'UTF-8' );
$dom -> formatOutput = true ;
$root = $dom -> createElement ( 'export_arcadia' );
$dom -> appendChild ( $root );
$order = $root -> appendChild ( $dom -> createElement ( 'Commande' ));
$key = array_search ( 'order_num' , array_column ( $fields -> root , 'fieldName' ));
if ( $key === false )
$order -> appendChild ( $dom -> createElement ( 'Num_Commande' ));
2021-02-23 15:45:35 +01:00
else
$order -> appendChild ( $dom -> createElement ( 'Num_Commande' , $fields -> root [ $key ] -> fieldValue ));
2021-02-19 16:32:08 +01:00
foreach ( $fields -> root as $value ){
if ( isset ( $pivot [ $value -> fieldName ]) && $value -> fieldName !== 'state_order_cb' && $value -> fieldName !== 'state_order_check' && $value -> fieldName !== 'order_num' )
$order -> appendChild ( $dom -> createElement ( $pivot [ $value -> fieldName ], $value -> fieldValue ));
}
if ( $type === 'default' ) {
$key = array_search ( 'state_order_cb' , array_column ( $fields -> root , 'fieldName' ));
if ( $key )
$order -> appendChild ( $dom -> createElement ( 'Etat_Commande_CB' , $fields -> root [ $key ] -> fieldValue ));
$key = array_search ( 'state_order_check' , array_column ( $fields -> root , 'fieldName' ));
if ( $key )
$order -> appendChild ( $dom -> createElement ( 'Etat_Commande_Cheque' , $fields -> root [ $key ] -> fieldValue ));
}
$order -> appendChild ( $dom -> createElement ( 'Email_conso' )) -> appendChild ( $dom -> createCDATASection ( " $customer->email " ));
$order -> appendChild ( $dom -> createElement ( 'Civilite' ));
2021-05-17 17:39:56 +02:00
if ( $type === 'default' ) {
$order -> appendChild ( $dom -> createElement ( 'Nom' )) -> appendChild ( $dom -> createCDATASection ( " $shipping->lastname " ));
$order -> appendChild ( $dom -> createElement ( 'Prenom' )) -> appendChild ( $dom -> createCDATASection ( " $shipping->firstname " ));
$order -> appendChild ( $dom -> createElement ( 'Adresse' )) -> appendChild ( $dom -> createCDATASection ( " $shipping->address " ));
$order -> appendChild ( $dom -> createElement ( 'Adresse_Complementaire' ));
$order -> appendChild ( $dom -> createElement ( 'CP' )) -> appendChild ( $dom -> createCDATASection ( " $shipping->postcode " ));
$order -> appendChild ( $dom -> createElement ( 'Ville' )) -> appendChild ( $dom -> createCDATASection ( " $shipping->city " ));
if ( isset ( $shipping -> phone ) && ! empty ( $shipping -> phone )) {
$order -> appendChild ( $dom -> createElement ( 'Telephone' )) -> appendChild ( $dom -> createCDATASection ( " $shipping->phone " ));
} else {
$order -> appendChild ( $dom -> createElement ( 'Telephone' ));
}
} else {
$order -> appendChild ( $dom -> createElement ( 'Nom' )) -> appendChild ( $dom -> createCDATASection ( " $customerInfos->lastname " ));
$order -> appendChild ( $dom -> createElement ( 'Prenom' )) -> appendChild ( $dom -> createCDATASection ( " $customerInfos->firstname " ));
$order -> appendChild ( $dom -> createElement ( 'Adresse' )) -> appendChild ( $dom -> createCDATASection ( " $customerInfos->address " ));
$order -> appendChild ( $dom -> createElement ( 'Adresse_Complementaire' )) -> appendChild ( $dom -> createCDATASection ( " $customerInfos->address2 " ));
$order -> appendChild ( $dom -> createElement ( 'CP' )) -> appendChild ( $dom -> createCDATASection ( " $customerInfos->postcode " ));
$order -> appendChild ( $dom -> createElement ( 'Ville' )) -> appendChild ( $dom -> createCDATASection ( " $customerInfos->city " ));
$order -> appendChild ( $dom -> createElement ( 'Telephone' )) -> appendChild ( $dom -> createCDATASection ( " $customerInfos->phone " ));
}
2021-02-19 16:32:08 +01:00
$orderLine = $order -> appendChild ( $dom -> createElement ( 'Ligne_de_Commande' ));
$key = array_search ( 'qty' , array_column ( $fields -> orderLine , 'fieldName' ));
if ( $key === false )
$orderLine -> appendChild ( $dom -> createElement ( 'Quantite' , '1' ));
$key = array_search ( 'package_unit_weight' , array_column ( $fields -> orderLine , 'fieldName' ));
if ( $key === false )
$orderLine -> appendChild ( $dom -> createElement ( 'Poids_Unitaire_Colis' ));
foreach ( $fields -> orderLine as $value ){
if ( isset ( $pivot [ $value -> fieldName ]) && $value -> fieldName !== 'code_on_pack' && $value -> fieldName !== 'code_unique' )
$orderLine -> appendChild ( $dom -> createElement ( $pivot [ $value -> fieldName ], $value -> fieldValue ));
}
if ( $type === 'game' ) {
$key = array_search ( 'code_on_pack' , array_column ( $fields -> orderLine , 'fieldName' ));
if ( $key )
$orderLine -> appendChild ( $dom -> createElement ( 'Code_on_Pack' )) -> appendChild ( $dom -> createCDATASection ( $fields -> orderLine [ $key ] -> fieldValue ));
$key = array_search ( 'code_unique' , array_column ( $fields -> orderLine , 'fieldName' ));
if ( $key )
$orderLine -> appendChild ( $dom -> createElement ( 'Code_Unique' )) -> appendChild ( $dom -> createCDATASection ( $fields -> orderLine [ $key ] -> fieldValue ));
}
return $dom -> saveXML ();
}
}