mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-19 04:23:55 +00:00
upload-honey-trap
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\Modules\Imports\Services;
|
||||
|
||||
use Maatwebsite\Excel\Concerns\WithHeadingRow;
|
||||
use Illuminate\Support\Collection;
|
||||
use Maatwebsite\Excel\Concerns\ToCollection;
|
||||
|
||||
|
||||
class GenericImport implements ToCollection, WithHeadingRow
|
||||
{
|
||||
function headingRow(): int { return 1; }
|
||||
|
||||
public $rows;
|
||||
|
||||
/**
|
||||
* @param Collection $collection
|
||||
*/
|
||||
public function collection(Collection $collection)
|
||||
{
|
||||
$this->rows = $collection;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Imports;
|
||||
|
||||
use App\Classes\Modules\Documents\DataTransferObjects\DocumentObject;
|
||||
use App\Classes\Modules\Imports\Services\GenericImport;
|
||||
use App\Classes\Modules\Segments\DataTransferObjects\SeasonalSegmentObject;
|
||||
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
||||
use App\Models\Segment;
|
||||
use App\Models\User;
|
||||
use Carbon\Carbon;
|
||||
use DateTime;
|
||||
use Illuminate\Http\Request;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
use App\Classes\Modules\Segments\Services\CreatesSeasonalSegment;
|
||||
use App\Classes\Modules\Companies\Processors\AssignSegmentProcessor;
|
||||
|
||||
class ImportHoneyTrapController
|
||||
{
|
||||
/**
|
||||
* @param Request $request
|
||||
* @return array
|
||||
* @throws \App\Classes\Exceptions\MalformedRequestException
|
||||
*/
|
||||
public function import(Request $request)
|
||||
{
|
||||
$object = new DocumentObject('', $request->input('files'), '', ApprovalStatus::APPROVED, 'imports');
|
||||
$file = json_decode($object->getFiles()[0])->file_info->original->file;
|
||||
|
||||
$import = new GenericImport();
|
||||
Excel::import($import, $file);
|
||||
$excelRows = $import->rows;
|
||||
$excelRows = $excelRows->toArray();
|
||||
|
||||
$returnArray = [];
|
||||
|
||||
$segment_id = Segment::where('name', 'honey trap')->first()->id;
|
||||
|
||||
foreach ($excelRows as $row) {
|
||||
|
||||
if (is_null($row['email']) || empty($row['email'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (is_null($row['end_date']) || empty($row['end_date'])) {
|
||||
// use csv import must have an end_date
|
||||
$row['status'] = 'failed';
|
||||
$row['message'] = 'End Date is required';
|
||||
$returnArray[] = $row;
|
||||
continue;
|
||||
}
|
||||
|
||||
$row['end_date'] = $end_date = $this->changeExcelDate($row['end_date']);
|
||||
$start_date = $row['start_date'] ? $this->changeExcelDate($row['start_date']) : Carbon::now();
|
||||
$row['start_date'] = $start_date;
|
||||
|
||||
$end_date = Carbon::parse($end_date);
|
||||
// Check if the end_date is in the past
|
||||
if ($end_date->isPast()) {
|
||||
// end_date must be after today's date
|
||||
$row['status'] = 'failed';
|
||||
$row['message'] = "End Date must be after today's date";
|
||||
$returnArray[] = $row;
|
||||
continue;
|
||||
}
|
||||
|
||||
$user = User::where('email', $row['email'])->first();
|
||||
if (!$user) {
|
||||
// if user not found
|
||||
$row['status'] = 'failed';
|
||||
$row['message'] = 'Email not found';
|
||||
$returnArray[] = $row;
|
||||
continue;
|
||||
}
|
||||
|
||||
$company = $user->company->first();
|
||||
if (!$company) {
|
||||
// if company not found
|
||||
$row['status'] = 'failed';
|
||||
$row['message'] = 'Company not found';
|
||||
$returnArray[] = $row;
|
||||
continue;
|
||||
}
|
||||
|
||||
$seasonalSegmentObject = new SeasonalSegmentObject($company->id, $segment_id, $start_date, $end_date ?? null);
|
||||
|
||||
(App()->make(createsSeasonalSegment::class))->execute($seasonalSegmentObject);
|
||||
(App()->make(assignSegmentProcessor::class))->execute($company, $segment_id);
|
||||
|
||||
// success added honey trap seasonal segment
|
||||
$row['status'] = 'success';
|
||||
$row['message'] = '';
|
||||
$returnArray[] = $row;
|
||||
continue;
|
||||
}
|
||||
|
||||
return response()->json($returnArray);
|
||||
}
|
||||
|
||||
public function changeExcelDate($date)
|
||||
{
|
||||
$unixTime = (($date - 25569) * 86400);
|
||||
$date = new DateTime("@$unixTime");
|
||||
return $date->format('d/m/Y');
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@
|
||||
<div class="row" @keyup.enter="submitForm">
|
||||
<div class="col ml-md-3 ml-0 mt-md-0 mt-3 bg-white padding-15">
|
||||
<loading-component style="height: 200px; top: 0;" key="1" color="success" v-show="$store.getters.isLoading(section)"></loading-component>
|
||||
<div class="row" v-show="!$store.getters.isLoading(section)">
|
||||
<div class="row" v-show="!$store.getters.isLoading(section)" v-if="!returnData">
|
||||
<div class="col">
|
||||
<div class="row m-b-10">
|
||||
<div class="col">
|
||||
@@ -32,6 +32,19 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div v-if="returnData">
|
||||
<div class="font-heading fs-16 all-caps bold m-b-15">Uploaded Customer</div>
|
||||
<table class="w-100 table table-bordered">
|
||||
<tr class="thead-dark">
|
||||
<th class="text-capitalize" v-for="key in returnDataKeys"> {{ key }}</th>
|
||||
</tr>
|
||||
<tr v-for="row in returnData">
|
||||
<td v-for="col in row" :class="[{'text-danger': row['status'] == 'failed'}]">
|
||||
{{ col }}
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -45,7 +58,8 @@
|
||||
data(){
|
||||
return {
|
||||
files: [],
|
||||
parameters: {}
|
||||
parameters: {},
|
||||
returnData: null
|
||||
}
|
||||
},
|
||||
validations: {
|
||||
@@ -53,14 +67,25 @@
|
||||
required
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
returnDataKeys() {
|
||||
return Object.keys(Object.assign({}, this.returnData[0]))
|
||||
},
|
||||
},
|
||||
methods: {
|
||||
submitForm(){
|
||||
|
||||
this.parameters = {
|
||||
files: this.files
|
||||
};
|
||||
|
||||
this.submit(this.route('api.debtor.import'), 'post', this.section, true, false)
|
||||
this.submit(this.route('api.honey_trap.upload'), 'post', this.section, true, false);
|
||||
},
|
||||
errorHandler(error){
|
||||
console.log(error);
|
||||
},
|
||||
successHandler(response) {
|
||||
console.log(response);
|
||||
this.returnData = response;
|
||||
console.log(this.returnDataKeys);
|
||||
},
|
||||
},
|
||||
mixins: [ModalFromHandler]
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
@extends('layouts.base_portal')
|
||||
@section('inner_content')
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<div class="row m-b-20">
|
||||
<div class="col">
|
||||
<upload-honey-trap-csv-component section="uploadHoneyTrapCsvComponent"></upload-honey-trap-csv-component>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
@@ -321,11 +321,6 @@
|
||||
<seasonal-segment-section-component></seasonal-segment-section-component>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row m-b-20">
|
||||
<div class="col">
|
||||
<upload-honey-trap-csv-component section="uploadHoneyTrapCsvComponent"></upload-honey-trap-csv-component>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -29,6 +29,7 @@ Route::group(['middleware' => 'api', 'prefix' => 'v1', 'as' => 'api.'], function
|
||||
|
||||
Route::get('/storage/{fileName}/fetch', 'Documents\RenderDocumentController@fileStorageServe')->where(['fileName' => '.*'])->name('storage.document.file');
|
||||
Route::post('/import/update-debtor/f614e339d7058904a831aad742e24d55', 'Imports\ImportUpdateDebtorController@import')->name('debtor.import');
|
||||
Route::post('/import/upload-honey-trap', 'Imports\ImportHoneyTrapController@import')->name('honey_trap.upload');
|
||||
|
||||
require __DIR__ . '/company.php';
|
||||
|
||||
|
||||
@@ -598,3 +598,6 @@ Route::get('/po/outsource/check', function(){
|
||||
|
||||
});
|
||||
|
||||
Route::get('/upload-honey-trap', function () {
|
||||
return view('pages.honey_trap');
|
||||
})->name('upload_honey_trap');
|
||||
Reference in New Issue
Block a user