247 lines
8.7 KiB
PHP
247 lines
8.7 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Controllers\API;
|
||
|
|
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
use App\Mail\SendFlyer;
|
||
|
|
use App\Mail\SendFlyerFailed;
|
||
|
|
use App\Models\AppUser;
|
||
|
|
use App\Models\Contact;
|
||
|
|
use App\Models\Directory;
|
||
|
|
use App\Models\Document;
|
||
|
|
use App\Models\Setting;
|
||
|
|
use Google\Cloud\Vision\V1\ImageAnnotatorClient;
|
||
|
|
use GuzzleHttp\Exception\ClientException;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use Illuminate\Support\Facades\Mail;
|
||
|
|
use Illuminate\Support\Facades\Storage;
|
||
|
|
use Illuminate\Support\Facades\URL;
|
||
|
|
|
||
|
|
class SyncController extends Controller
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* @param \Illuminate\Http\Request $request
|
||
|
|
* @return \App\Http\Resources\AppUserCollection
|
||
|
|
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
|
||
|
|
*/
|
||
|
|
public function getUsers(Request $request)
|
||
|
|
{
|
||
|
|
return new \App\Http\Resources\AppUserCollection(AppUser::where('status', 1)->get());
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param \Illuminate\Http\Request $request
|
||
|
|
* @return \App\Http\Resources\ContactsCollection
|
||
|
|
*/
|
||
|
|
public function getContacts(Request $request)
|
||
|
|
{
|
||
|
|
$userId = $request->userId;
|
||
|
|
$this->syncContacts($request->contacts, $userId);
|
||
|
|
return new \App\Http\Resources\ContactsCollection(Contact::where('app_user_id', $userId)->get());
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param \Illuminate\Http\Request $request
|
||
|
|
* @return \App\Http\Resources\DirectoriesCollection
|
||
|
|
*/
|
||
|
|
public function getDirectories(Request $request)
|
||
|
|
{
|
||
|
|
return new \App\Http\Resources\DirectoriesCollection(Directory::all());
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param \Illuminate\Http\Request $request
|
||
|
|
* @return \App\Http\Resources\DocumentsCollection
|
||
|
|
*/
|
||
|
|
public function getDocuments(Request $request)
|
||
|
|
{
|
||
|
|
return new \App\Http\Resources\DocumentsCollection(Document::where('status', 1)->get());
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param \Illuminate\Http\Request $request
|
||
|
|
* @return array
|
||
|
|
*/
|
||
|
|
public function getVersion(Request $request)
|
||
|
|
{
|
||
|
|
$app_uuid = $request->app_uuid;
|
||
|
|
$app_version = $request->app_version;
|
||
|
|
$version = Setting::select('value AS version')->where('key', 'version')->first();
|
||
|
|
$ShowIsActive = Setting::select('value AS ShowIsActive')->where('key', 'ShowIsActive')->first();
|
||
|
|
|
||
|
|
try {
|
||
|
|
$client = new \GuzzleHttp\Client();
|
||
|
|
$response = $client->request(
|
||
|
|
'GET',
|
||
|
|
'https://store.ldc.fr/api/get-version?token=@P$oi6or!bL2-r%3C&app_uuid=' . $app_uuid
|
||
|
|
);
|
||
|
|
$body = $response->getBody();
|
||
|
|
$storeVersion = json_decode($body)->version;
|
||
|
|
} catch (ClientException $e) {
|
||
|
|
$storeVersion = $app_version;
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($storeVersion > $app_version) {
|
||
|
|
$newApp = true;
|
||
|
|
} else {
|
||
|
|
$newApp = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
return ['version' => $version->version, 'ShowIsActive' => $ShowIsActive->ShowIsActive, 'newApp' => $newApp];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param \Illuminate\Http\Request $request
|
||
|
|
* @return string
|
||
|
|
*/
|
||
|
|
public function sendFlyer(Request $request)
|
||
|
|
{
|
||
|
|
$email = $request->emails['email'];
|
||
|
|
$subject = $request->emails['subject'];
|
||
|
|
$message = $request->emails['message'];
|
||
|
|
$app_user_id = $request->emails['app_user_id'];
|
||
|
|
$docs = $request->emails['documents'];
|
||
|
|
|
||
|
|
$user = AppUser::find($app_user_id);
|
||
|
|
|
||
|
|
$attach = [];
|
||
|
|
$filesName = [];
|
||
|
|
$noExists = [];
|
||
|
|
foreach ($docs as $doc) {
|
||
|
|
$exists = Storage::disk('public')->exists('docs/' . $doc['uuid'] . '.' . $doc['type']);
|
||
|
|
if ($exists) {
|
||
|
|
array_push($attach, URL::temporarySignedRoute(
|
||
|
|
'bo.docs.download',
|
||
|
|
now()->addDays(30),
|
||
|
|
['uuid' => $doc['uuid']]
|
||
|
|
));
|
||
|
|
array_push($filesName, $doc['title']);
|
||
|
|
} else {
|
||
|
|
array_push($noExists, $doc['title']);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (count($noExists) === 0) {
|
||
|
|
$message = new SendFlyer([
|
||
|
|
'body' => $message,
|
||
|
|
'from' => $user->email,
|
||
|
|
'fromName' => $user->firstname . ' ' . $user->lastname,
|
||
|
|
'subject' => '[' . config('app.BRAND') . '] - ' . $subject,
|
||
|
|
'attach' => $attach,
|
||
|
|
'filesName' => $filesName,
|
||
|
|
]);
|
||
|
|
|
||
|
|
Mail::to($email)
|
||
|
|
->cc($user->email)
|
||
|
|
->queue($message);
|
||
|
|
} else {
|
||
|
|
$message = new SendFlyerFailed([
|
||
|
|
'email' => $email,
|
||
|
|
'message' => $message,
|
||
|
|
'subject' => $subject,
|
||
|
|
'filesName' => $filesName,
|
||
|
|
'noExists' => $noExists
|
||
|
|
]);
|
||
|
|
|
||
|
|
Mail::to($user->email)
|
||
|
|
->queue($message);
|
||
|
|
} //end if
|
||
|
|
return 'OK';
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param mixed $contacts
|
||
|
|
* @param mixed $userId
|
||
|
|
* @return void
|
||
|
|
*/
|
||
|
|
private function syncContacts(mixed $contacts, mixed $userId)
|
||
|
|
{
|
||
|
|
$contacts = json_decode(json_encode($contacts), false);
|
||
|
|
foreach ($contacts as $contact) {
|
||
|
|
if ($contact->to_delete === true) {
|
||
|
|
$tmp = Contact::where([
|
||
|
|
['id', '=', $contact->uniq_id],
|
||
|
|
['app_user_id', '=', $contact->app_user_id]
|
||
|
|
])->first();
|
||
|
|
if ($tmp) {
|
||
|
|
$tmp->delete();
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
if ($contact->app_user_id != $userId) {
|
||
|
|
$tmp = Contact::where([
|
||
|
|
['id', '=', $contact->uniq_id],
|
||
|
|
['app_user_id', '=', $userId]
|
||
|
|
])->first();
|
||
|
|
if ($tmp) {
|
||
|
|
$tmp->delete();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
$tmp = Contact::where([
|
||
|
|
['id', '=', $contact->uniq_id],
|
||
|
|
['app_user_id', '=', $contact->app_user_id]
|
||
|
|
])->first();
|
||
|
|
if ($tmp) {
|
||
|
|
$tmp->email = $contact->email;
|
||
|
|
$tmp->type = $contact->type;
|
||
|
|
$tmp->other = $contact->other;
|
||
|
|
$tmp->select1 = $contact->select1;
|
||
|
|
$tmp->select2 = $contact->select2;
|
||
|
|
$tmp->lastname = $contact->lastname;
|
||
|
|
$tmp->firstname = $contact->firstname;
|
||
|
|
$tmp->company = $contact->company;
|
||
|
|
$tmp->address = $contact->address;
|
||
|
|
$tmp->phone = empty($contact->phone) ? "" : $contact->phone;
|
||
|
|
$tmp->products = $contact->products;
|
||
|
|
$tmp->notes = $contact->notes;
|
||
|
|
$tmp->visits = $contact->visits;
|
||
|
|
$tmp->app_user_id = $contact->app_user_id;
|
||
|
|
$tmp->save();
|
||
|
|
} else {
|
||
|
|
$tmp = new Contact();
|
||
|
|
$tmp->id = $contact->uniq_id;
|
||
|
|
$tmp->email = $contact->email;
|
||
|
|
$tmp->type = $contact->type;
|
||
|
|
$tmp->other = $contact->other;
|
||
|
|
$tmp->select1 = $contact->select1;
|
||
|
|
$tmp->select2 = $contact->select2;
|
||
|
|
$tmp->lastname = $contact->lastname;
|
||
|
|
$tmp->firstname = $contact->firstname;
|
||
|
|
$tmp->company = $contact->company;
|
||
|
|
$tmp->address = $contact->address;
|
||
|
|
$tmp->phone = empty($contact->phone) ? "" : $contact->phone;
|
||
|
|
$tmp->products = json_encode($contact->products);
|
||
|
|
$tmp->notes = $contact->notes;
|
||
|
|
$tmp->visits = $contact->visits;
|
||
|
|
$tmp->app_user_id = $contact->app_user_id;
|
||
|
|
$tmp->save();
|
||
|
|
} //end if
|
||
|
|
} //end if
|
||
|
|
} //end foreach
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param \Illuminate\Http\Request $request
|
||
|
|
* @return void
|
||
|
|
*/
|
||
|
|
public function processOcr(Request $request)
|
||
|
|
{
|
||
|
|
// Instantiates a client.
|
||
|
|
$imageAnnotator = new ImageAnnotatorClient([
|
||
|
|
'credentials' => public_path() . '/credentials.json'
|
||
|
|
]);
|
||
|
|
|
||
|
|
// The name of the image file to annotate.
|
||
|
|
$fileName = 'test.jpg';
|
||
|
|
|
||
|
|
// Prepare the image to be annotated.
|
||
|
|
$image = file_get_contents(public_path() . "/" . $fileName);
|
||
|
|
|
||
|
|
// Performs label detection on the image file.
|
||
|
|
$response = $imageAnnotator->textDetection($image);
|
||
|
|
$labels = $response->getTextAnnotations();
|
||
|
|
|
||
|
|
foreach ($labels as $label) {
|
||
|
|
echo $label->getDescription() . "<br>";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|