258 lines
9.0 KiB
PHP
258 lines
9.0 KiB
PHP
<?php namespace App\Controllers;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
|
|
use App\Models\Transaction_model;
|
|
use App\Models\Donation_data_model;
|
|
|
|
|
|
class PhpspreadsheetController extends BaseController
|
|
{
|
|
|
|
public $session;
|
|
public function __construct()
|
|
{
|
|
helper('filesystem');
|
|
$this->session = session();
|
|
$this->Donation_data_model = new Donation_data_model();
|
|
$this->Transaction_model = new Transaction_model();
|
|
|
|
}
|
|
|
|
public function import_donation_data()
|
|
{
|
|
|
|
|
|
if($this->request->getmethod() == 'post')
|
|
{
|
|
//validate
|
|
$rules=[
|
|
|
|
'upload_file' => [
|
|
'rules'=>'uploaded[upload_file]|max_size[upload_file,1024]|ext_in[upload_file,xlsx]',
|
|
'lable'=>'upload file'
|
|
|
|
]
|
|
|
|
];
|
|
|
|
if(! $this->validate($rules))
|
|
{
|
|
$data['validation'] = $this->validator;
|
|
}else{
|
|
|
|
$file= $this->request->getFile('upload_file');
|
|
//echo $file->getName();exit();
|
|
$ext=$file->getClientExtension();
|
|
if($ext == 'xls'){
|
|
$render = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
|
|
} else {
|
|
$render = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
|
|
}
|
|
$spreadsheet = $render->load($file);
|
|
$allDataInSheet = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
|
|
$arrayCount = count($allDataInSheet);
|
|
|
|
$flag = 0;
|
|
$createArray = array('DATE', 'CAUSE_NAME', 'AMOUNT', 'PAYMENT_MODE' , 'REF' , 'DONATION_REF');
|
|
|
|
$makeArray = array('DATE' => 'DATE', 'CAUSE_NAME' => 'CAUSE_NAME', 'AMOUNT' => 'AMOUNT', 'PAYMENT_MODE' => 'PAYMENT_MODE', 'REF' => 'REF', 'DONATION_REF' => 'DONATION_REF');
|
|
|
|
$SheetDataKey = array();
|
|
|
|
foreach ($allDataInSheet as $dataInSheet) {
|
|
foreach ($dataInSheet as $key => $value) {
|
|
|
|
if (in_array(trim($value), $createArray)) {
|
|
|
|
$value = preg_replace('/\s+/', '', $value);
|
|
$SheetDataKey[trim($value)] = $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
$dataDiff = array_diff_key($makeArray, $SheetDataKey);
|
|
|
|
if (empty($dataDiff)) {
|
|
$flag = 1;
|
|
}
|
|
// match excel sheet column
|
|
if ($flag == 1)
|
|
{
|
|
for ($i = 2; $i <= $arrayCount; $i++)
|
|
{
|
|
|
|
$date = $SheetDataKey['DATE'];
|
|
$cause_name = $SheetDataKey['CAUSE_NAME'];
|
|
$amount = $SheetDataKey['AMOUNT'];
|
|
$payment_mode = $SheetDataKey['PAYMENT_MODE'];
|
|
$ref = $SheetDataKey['REF'];
|
|
$donation_ref = $SheetDataKey['DONATION_REF'];
|
|
|
|
|
|
$date = filter_var(trim($allDataInSheet[$i][$date]), FILTER_SANITIZE_STRING);
|
|
$cause_name = filter_var(trim($allDataInSheet[$i][$cause_name]), FILTER_SANITIZE_STRING);
|
|
$amount = filter_var(trim($allDataInSheet[$i][$amount]), FILTER_SANITIZE_EMAIL);
|
|
$payment_mode = filter_var(trim($allDataInSheet[$i][$payment_mode]), FILTER_SANITIZE_STRING);
|
|
$ref = filter_var(trim($allDataInSheet[$i][$ref]), FILTER_SANITIZE_EMAIL);
|
|
$donation_ref = filter_var(trim($allDataInSheet[$i][$donation_ref]), FILTER_SANITIZE_STRING);
|
|
|
|
$fetchData= array('date' => $date, 'cause_name' => $cause_name, 'amount' => $amount, 'mode_of_payment' => $payment_mode, 'ref_1' => $ref, 'donation_ref' => $donation_ref);
|
|
|
|
|
|
$model = new Donation_data_model();
|
|
$model->save($fetchData);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
echo '<script>alert("Please import correct file, did not match excel sheet column")</script>';
|
|
//echo "Please import correct file, did not match excel sheet column";
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
echo view('templates/header');
|
|
echo view('donation_data');
|
|
echo view('templates/footer');
|
|
// $file_mimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
|
|
|
// if(isset($_FILES['upload_file']['name']) && in_array($_FILES['upload_file']['type'], $file_mimes))
|
|
// {
|
|
// $arr_file = explode('.', $_FILES['upload_file']['name']);
|
|
// $extension = end($arr_file);
|
|
// if('csv' == $extension){
|
|
// $reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
|
|
// } else {
|
|
// $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
|
|
// }
|
|
// $spreadsheet = $reader->load($_FILES['upload_file']['tmp_name']);
|
|
// $sheetData = $spreadsheet->getActiveSheet()->toArray();
|
|
// echo "<pre>";
|
|
// print_r($sheetData);
|
|
// }
|
|
}
|
|
|
|
public function import_transaction_data()
|
|
{
|
|
|
|
|
|
if($this->request->getmethod() == 'post')
|
|
{
|
|
//validate
|
|
$rules=[
|
|
|
|
'transacton_upload' => [
|
|
'rules'=>'uploaded[transacton_upload]|max_size[transacton_upload,1024]|ext_in[transacton_upload,xlsx]',
|
|
'lable'=>'upload file'
|
|
|
|
]
|
|
|
|
];
|
|
|
|
if(! $this->validate($rules))
|
|
{
|
|
$data['validation'] = $this->validator;
|
|
}else{
|
|
|
|
$file= $this->request->getFile('transacton_upload');
|
|
//echo $file->getName();exit();
|
|
$ext=$file->getClientExtension();
|
|
if($ext == 'xls'){
|
|
$render = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
|
|
} else {
|
|
$render = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
|
|
}
|
|
$spreadsheet = $render->load($file);
|
|
$allDataInSheet = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
|
|
$arrayCount = count($allDataInSheet);
|
|
|
|
$flag = 0;
|
|
$createArray = array('DATE', 'REF_1', 'REF_2', 'REMARKS' , 'AMOUNT' , 'DONATION_REF');
|
|
|
|
$makeArray = array('DATE' => 'DATE', 'REF_1' => 'REF_1', 'REF_2' => 'REF_2', 'REMARKS' => 'REMARKS', 'AMOUNT' => 'AMOUNT', 'DONATION_REF' => 'DONATION_REF');
|
|
|
|
$SheetDataKey = array();
|
|
|
|
foreach ($allDataInSheet as $dataInSheet) {
|
|
foreach ($dataInSheet as $key => $value) {
|
|
|
|
if (in_array(trim($value), $createArray)) {
|
|
|
|
$value = preg_replace('/\s+/', '', $value);
|
|
$SheetDataKey[trim($value)] = $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
$dataDiff = array_diff_key($makeArray, $SheetDataKey);
|
|
|
|
if (empty($dataDiff)) {
|
|
$flag = 1;
|
|
}
|
|
// match excel sheet column
|
|
if ($flag == 1)
|
|
{
|
|
for ($i = 2; $i <= $arrayCount; $i++)
|
|
{
|
|
|
|
$date = $SheetDataKey['DATE'];
|
|
$ref_1 = $SheetDataKey['REF_1'];
|
|
$ref_2 = $SheetDataKey['REF_2'];
|
|
$remarks = $SheetDataKey['REMARKS'];
|
|
$amount = $SheetDataKey['AMOUNT'];
|
|
$donation_ref = $SheetDataKey['DONATION_REF'];
|
|
|
|
|
|
$date = filter_var(trim($allDataInSheet[$i][$date]), FILTER_SANITIZE_STRING);
|
|
$ref_1 = filter_var(trim($allDataInSheet[$i][$ref_1]), FILTER_SANITIZE_STRING);
|
|
$ref_2 = filter_var(trim($allDataInSheet[$i][$ref_2]), FILTER_SANITIZE_EMAIL);
|
|
$remarks = filter_var(trim($allDataInSheet[$i][$remarks]), FILTER_SANITIZE_STRING);
|
|
$amount = filter_var(trim($allDataInSheet[$i][$amount]), FILTER_SANITIZE_EMAIL);
|
|
$donation_ref = filter_var(trim($allDataInSheet[$i][$donation_ref]), FILTER_SANITIZE_STRING);
|
|
|
|
$fetchData= array('date' => $date, 'ref_1' => $ref_1, 'ref_2' => $ref_2, 'remarks' => $remarks, 'amount' => $amount, 'donation_ref' => $donation_ref);
|
|
|
|
//print_r($fetchData); exit();
|
|
$model = new Transaction_model();
|
|
$model->save($fetchData);
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
echo '<script>alert("Please import correct file, did not match excel sheet column")</script>';
|
|
//echo "Please import correct file, did not match excel sheet column";
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
// echo view('templates/header');
|
|
// echo view('transaction_upload');
|
|
// echo view('templates/footer');
|
|
return view('transaction_upload');
|
|
|
|
}
|
|
public function transaction_file()
|
|
{
|
|
return $this->response->download('public/assets/download/transaction_upload_file.xlsx', NULL);
|
|
}
|
|
public function donation_data_file()
|
|
{
|
|
return $this->response->download('public/assets/download/donation_data_upload_file.xlsx', NULL);
|
|
}
|
|
|
|
} |