-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
173 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php $this->view('partials/head')?> | ||
|
||
<div class="container"> | ||
<div class="row"> | ||
<div class="col-lg-6"> | ||
<h3>Update warranty status</h3> | ||
<p>Update warranty status of the clients</p> | ||
<div class="form-group"> | ||
<button id="update-status" class="btn btn-default">Update</button> | ||
</div> | ||
|
||
<div class="alert alert-success hidden" role="alert" id="update-result"></div> | ||
</div> | ||
|
||
<div class="col-lg-6"> | ||
<h3>Update warranty information</h3> | ||
<?php if($result):?> | ||
|
||
<div class="alert alert-success alert-dismissible" role="alert">Updated entries: <?=$result['updated']?></div> | ||
<div class="alert alert-info alert-dismissible" role="alert">CSV entries: <?=$result['csv_entries']?></div> | ||
<div class="alert alert-warning alert-dismissible" role="alert">Invalid entries: <?=$result['invalid']?></div> | ||
|
||
<?php endif?> | ||
|
||
<p class="help-block">CSV file format (you also need the header): | ||
<pre> | ||
"serial_number","purchase_date","end_date" | ||
"3X6RHPJ3P7QM","2016-06-09","2020-06-09" | ||
</pre> | ||
</p> | ||
|
||
<form action="" method="post" enctype="multipart/form-data"> | ||
<input type="hidden" name="_token" value="<?php echo getCSRF();?>"> | ||
<div class="form-group"> | ||
<label for="csv">Select csv file</label> | ||
<input type="file" name="csv" id="csv" accept=".csv"> | ||
</div> | ||
<button type="submit" class="btn btn-default">Upload</button> | ||
</form> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<script> | ||
$('#update-status').click(function (e) { | ||
$(this).attr('disabled', true); | ||
var $btn = $(this); | ||
|
||
function done () { | ||
$btn.attr('disabled', false); | ||
$('#update-result').removeClass('hidden'); | ||
} | ||
|
||
$.getJSON(appUrl + '/module/warranty/update_status', function (data) { | ||
done(); | ||
$('#update-result').text('Updated: ' + data['updated'] + ' clients'); | ||
}); | ||
}); | ||
</script> | ||
|
||
<?php $this->view('partials/foot'); ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
class Warranty_upload | ||
{ | ||
public function __construct() | ||
{ | ||
# code... | ||
} | ||
|
||
public function handleUpload() | ||
{ | ||
if( ! isset( $_FILES['csv']['tmp_name'])){ | ||
return; | ||
} | ||
|
||
$csv = $this->_convertFileToCsv($_FILES['csv']['tmp_name']); | ||
|
||
$result = [ | ||
'csv_entries' => count($csv), | ||
'updated' => 0, | ||
'invalid' => 0, | ||
]; | ||
|
||
foreach ($csv as $entry) { | ||
if( ! $this->_validateEntry($entry)){ | ||
$result['invalid'] += 1; | ||
} | ||
else{ | ||
$result['updated'] += $this->_updateEntry($entry); | ||
} | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
private function _validateEntry($entry) | ||
{ | ||
if( ! isset($entry['serial_number'])){ | ||
return false; | ||
} | ||
if( ! isset($entry['purchase_date'])){ | ||
return false; | ||
} | ||
if( ! isset($entry['end_date'])){ | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
private function _updateEntry($entry) | ||
{ | ||
return Warranty_model::where('warranty.serial_number', $entry['serial_number']) | ||
->where('purchase_date', '!=', $entry['purchase_date']) | ||
->where('end_date', '!=', $entry['end_date']) | ||
->update([ | ||
'purchase_date' => $entry['purchase_date'], | ||
'end_date' => $entry['end_date'], | ||
]); | ||
} | ||
|
||
private function _convertFileToCsv($file) | ||
{ | ||
$csv = array_map('str_getcsv', file($file)); | ||
array_walk($csv, function(&$a) use ($csv) { | ||
$a = array_combine($csv[0], $a); | ||
}); | ||
array_shift($csv); # remove column header | ||
return $csv; | ||
} | ||
} |