Refactor project structure

This commit is contained in:
Thilina Hasantha
2018-04-29 17:46:42 +02:00
parent 889baf124c
commit e3a7e18d9c
5513 changed files with 32 additions and 27 deletions

View File

@@ -0,0 +1,283 @@
<?php
namespace Data\Admin\Api;
use Classes\BaseService;
use Classes\IceResponse;
use Data\Common\Model\DataImport;
use FieldNames\Common\Model\CustomField;
use Utils\LogManager;
abstract class AbstractDataImporter implements DataImporter
{
protected $dataImport = null;
protected $headerMapping = array();
protected $primaryKeyColumn = 0;
protected $rowObjects = array();
protected $attachedObjects = array();
protected $objectKeys;
protected $customFields;
protected $columnsCompeted = array();
protected $relatedColumns = array();
public function getResult()
{
return $this->rowObjects;
}
public function getHeaderMapping()
{
return $this->headerMapping;
}
public function processHeader($data)
{
$columns = $this->dataImport->columns;
$headers = json_decode($columns);
$counter = 0;
foreach ($headers as $column) {
$this->headerMapping[] = $column;
if ($column->idField == "Yes") {
$this->primaryKeyColumn = $counter;
}
//Update related columns
if (($column->type == "Reference" || $column->type == "Attached") && $column->isKeyField == "Yes") {
$this->relatedColumns[$counter] = array();
for ($i = 0; $i< count($headers); $i++) {
$columnNew = $headers[$i];
if ($columnNew->id != $column->id &&
$columnNew->dependOn == $column->dependOn && $column->dependOn != "NULL") {
$this->relatedColumns[$counter][$i] = $columnNew;
}
}
}
$counter++;
}
$modelObject = $this->getModelObject();
$obj = new $modelObject();
$this->objectKeys = $obj->getObjectKeys();
$this->updateCustomFields();
}
public function setDataImportId($dataImportId)
{
$this->dataImport = new DataImport();
$this->dataImport->Load("id =?", array($dataImportId));
}
public function updateCustomFields()
{
$customField = new CustomField();
$customFields = $customField->Find("type = ?", array($this->getModelObject()));
$this->customFields = array();
foreach ($customFields as $cf) {
$this->customFields[$cf->name] = $cf;
}
}
public function addCustomField($column)
{
$customField = new CustomField();
$customField->type = $this->getModelObject();
$customField->name = $column->name;
$customField->display = "Form";
$customField->field_type = "text";
$customField->field_label = $column->title;
$customField->field_validation = "none";
$customField->display_order = 0;
$customField->Save();
}
public function markCellCompleted($row, $col)
{
if (!isset($this->columnsCompeted[$row])) {
$this->columnsCompeted[$row] = array();
}
$this->columnsCompeted[$row][$col] = true;
}
public function isCellCompleted($row, $col)
{
if (!isset($this->columnsCompeted[$row])) {
return false;
} else {
if (!isset($this->columnsCompeted[$row][$col])) {
return false;
}
return true;
}
}
public function processData($row, $column, $data, $allData)
{
LogManager::getInstance()->debug("processData:".json_encode($data));
if ($this->isCellCompleted($row, $column)) {
LogManager::getInstance()->debug("Already Competed");
return new IceResponse(IceResponse::SUCCESS, "Already Competed");
}
$headerColumn = $this->headerMapping[$column];
//Load row object if its empty
if (!isset($this->rowObjects[$row])) {
$modelObj = $this->getModelObject();
$this->rowObjects[$row] = new $modelObj();
if ($headerColumn->idField == "Yes") {
LogManager::getInstance()->debug("[processData] Load : $headerColumn->name = $data");
$this->rowObjects[$row]->Load("$headerColumn->name = ?", array($data));
}
}
//Check for non existing column names
if (!isset($this->objectKeys[$headerColumn->name])) {
if (!isset($this->customFields[$headerColumn->name])) {
$this->addCustomField($headerColumn);
$this->updateCustomFields();
}
}
if ($headerColumn->type == "Normal") {
$this->rowObjects[$row]->{$headerColumn->name} = $data;
} elseif ($headerColumn->type == "Reference" || $headerColumn->type == "Attached") {
if ($headerColumn->isKeyField == "Yes") {
$hcClass = BaseService::getInstance()->getFullQualifiedModelClassName($headerColumn->dependOn);
$hcField = $headerColumn->dependOnField;
/* @var \Model\BaseModel $hcObject */
$hcObject = new $hcClass();
if ($headerColumn->type == "Attached" && !empty($this->rowObjects[$row]->id)) {
$hcObject->Load("$hcField = ? and employee = ?", array($data,$this->rowObjects[$row]->id));
} else {
$hcObject->Load("$hcField = ?", array($data));
}
$hcObject->{$hcField} = $data;
foreach ($this->relatedColumns[$column] as $key => $val) {
$tempValName = $val->name;
if (strstr($val->name, "/")) {
$tempValName = explode("/", $val->name)[1];
}
$hcObject->{$tempValName} = $allData[$key];
$this->markCellCompleted($row, $key);
}
if ($headerColumn->type == "Reference") {
$hcObject->Save();
} else {
if (!isset($this->attachedObjects[$row])) {
$this->attachedObjects[$row] = array();
}
$this->attachedObjects[$row][] = $hcObject;
}
$this->rowObjects[$row]->{$headerColumn->name} = $hcObject->id;
}
}
$this->markCellCompleted($row, $column);
return new IceResponse(IceResponse::SUCCESS, $this->rowObjects[$row]);
}
public function processDataRow($row, $data)
{
$result = array();
LogManager::getInstance()->debug("processDataRow:".json_encode($data));
$counter = 0;
foreach ($data as $cell) {
$this->processData($row, $counter, $cell, $data);
$counter++;
}
LogManager::getInstance()->debug("Row Object :" . print_r($this->rowObjects[$row], true));
$this->rowObjects[$row] = $this->fixBeforeSave($this->rowObjects[$row], $data);
if (!$this->isDuplicate($this->rowObjects[$row])) {
$ok = $this->rowObjects[$row]->Save();
if (!$ok) {
LogManager::getInstance()->error(
"Row Object Error Saving Employee :" . $this->rowObjects[$row]->ErrorMsg()
);
$result['Error'] = "Row Object Error Saving Employee :" . $this->rowObjects[$row]->ErrorMsg();
} else {
$result['MainId'] = $this->rowObjects[$row]->id;
$class = $this->getModelObject();
$ele = new $class();
$result['CustomFields'] = array();
$customFields = $ele->getCustomFields($this->rowObjects[$row]);
foreach ($this->rowObjects[$row] as $k => $v) {
if (isset($customFields[$k])) {
BaseService::getInstance()->customFieldManager
->addCustomField($class, $this->rowObjects[$row]->id, $k, $v);
$result['CustomFields'][] = array($class, $this->rowObjects[$row]->id, $k, $v);
}
}
if (!empty($this->attachedObjects[$row])) {
/* @var \Model\BaseModel $aObj */
foreach ($this->attachedObjects[$row] as $aObj) {
$aObj->employee = $this->rowObjects[$row]->id;
$aObj->Save();
$result['attachedObjects'][] = $aObj;
}
}
}
} else {
$result['Error'] = "Duplicate Object Found";
}
return $result;
}
abstract public function getModelObject();
public function isDuplicate($obj)
{
return false;
}
abstract public function fixBeforeSave($object, $data);
public function process($data, $dataImportId)
{
$data = str_replace("\r", "\n", $data);
$data = str_replace("\n\n", "\n", $data);
$lines = str_getcsv($data, "\n");
$headerProcessed = false;
$counter = 0;
LogManager::getInstance()->info("Line Count:".count($lines));
$res = array();
foreach ($lines as $line) {
$cells = str_getcsv($line, ",");
if ($headerProcessed === false) {
$this->setDataImportId($dataImportId);
$this->processHeader($cells);
$headerProcessed = true;
} else {
$result = $this->processDataRow($counter, $cells);
$res[] = array($cells,$result);
}
$counter++;
}
return $res;
}
public function getLastStatus()
{
return IceResponse::SUCCESS;
}
}

View File

@@ -0,0 +1,60 @@
<?php
/**
* Created by PhpStorm.
* User: Thilina
* Date: 8/19/17
* Time: 8:02 AM
*/
namespace Data\Admin\Api;
use Classes\FileService;
use Classes\IceResponse;
use Classes\SubActionManager;
use Data\Common\Model\DataImport;
use Data\Common\Model\DataImportFile;
use Utils\LogManager;
class DataActionManager extends SubActionManager
{
public function processDataFile($req)
{
$id = $req->id;
$dataFile = new DataImportFile();
$dataFile->Load("id = ?", array($id));
if (empty($dataFile->id)) {
return new IceResponse(IceResponse::ERROR, null);
}
$url = FileService::getInstance()->getFileUrl($dataFile->file);
if (strstr($url, CLIENT_BASE_URL) !== false) {
$url = str_replace(CLIENT_BASE_URL, CLIENT_BASE_PATH, $url);
}
LogManager::getInstance()->info("File Path:".$url);
$data = file_get_contents($url);
$dataImport = new DataImport();
$dataImport->Load("id =?", array($dataFile->data_import_definition));
if (empty($dataImport->id)) {
return new IceResponse(IceResponse::ERROR, null);
}
$processClass = '\\Data\Admin\Import\\'.$dataImport->dataType;
$processObj = new $processClass();
$res = $processObj->process($data, $dataImport->id);
if ($processObj->getLastStatus() === IceResponse::SUCCESS) {
$dataFile->status = "Processed";
}
$dataFile->details = json_encode($res, JSON_PRETTY_PRINT);
$dataFile->Save();
return new IceResponse($processObj->getLastStatus(), $processObj->getResult());
}
private function processHeader($dataImportId, $data)
{
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace Data\Admin\Api;
use Classes\AbstractModuleManager;
class DataAdminManager extends AbstractModuleManager
{
public function initializeUserClasses()
{
}
public function initializeFieldMappings()
{
$this->addFileFieldMapping('DataImportFile', 'file', 'name');
}
public function initializeDatabaseErrorMappings()
{
}
public function setupModuleClassDefinitions()
{
$this->addModelClass('DataImport');
$this->addModelClass('DataImportFile');
}
}

View File

@@ -0,0 +1,15 @@
<?php
/**
* Created by PhpStorm.
* User: Thilina
* Date: 10/3/17
* Time: 5:54 PM
*/
namespace Data\Admin\Api;
interface DataImporter
{
public function getResult();
public function process($data, $dataImporterId);
}

View File

@@ -0,0 +1,36 @@
<?php
namespace Data\Admin\Import;
use Attendance\Common\Model\Attendance;
use Data\Admin\Api\AbstractDataImporter;
class AttendanceDataImporter extends AbstractDataImporter
{
protected $processed = array();
public function getModelObject()
{
return "\\Attendance\\Common\\Model\\Attendance";
}
public function fixBeforeSave($object, $data)
{
return $object;
}
public function isDuplicate($obj)
{
$old = new Attendance();
$data = $old->Find(
"employee = ? and in_time = ? and out_time = ?",
array($obj->employee, $obj->in_time, $obj->out_time)
);
if (count($data)>0) {
return true;
}
return false;
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace Data\Admin\Import;
use Data\Admin\Api\AbstractDataImporter;
class EmployeeDataImporter extends AbstractDataImporter
{
protected $processed = array();
public function getModelObject()
{
return "\\Employees\\Common\\Model\\Employee";
}
public function fixBeforeSave($object, $data)
{
if (empty($object->status)) {
$object->status = "Active";
}
return $object;
}
}

View File

@@ -0,0 +1,318 @@
<?php
/**
* Created by PhpStorm.
* User: Thilina
* Date: 10/3/17
* Time: 5:39 PM
*/
namespace Data\Admin\Import;
use Classes\IceResponse;
use Company\Common\Model\CompanyStructure;
use Data\Admin\Api\DataImporter;
use Payroll\Common\Model\Deduction;
use Payroll\Common\Model\DeductionGroup;
use Payroll\Common\Model\Payroll;
use Payroll\Common\Model\PayrollColumn;
use Payroll\Common\Model\PayslipTemplate;
use Salary\Common\Model\SalaryComponent;
use Salary\Common\Model\SalaryComponentType;
use Utils\LogManager;
class PayrollDataImporter implements DataImporter
{
protected $model;
protected $lastStatus = IceResponse::ERROR;
public function getResult()
{
return $this->model;
}
public function process($data, $dataImporterId)
{
$compStructure = new CompanyStructure();
$compStructure->Load("1 = 1 limit 1", array());
if (empty($compStructure->id)) {
$this->lastStatus = IceResponse::ERROR;
return "No company structures exists";
}
$this->model = json_decode($data);
$deductionGroup = $this->createDeductionGroup($this->model);
if (!$deductionGroup) {
$this->lastStatus = IceResponse::ERROR;
return "Deduction group already exists";
}
$salaryComponentTypeIdMap = $this->addSalaryComponentTypes($this->model);
$salaryComponentIdMap = $this->addSalaryComponents($this->model, $salaryComponentTypeIdMap);
$payrollColumnIdMap = $this->addPayrollColumns($this->model, $deductionGroup->id, $salaryComponentIdMap);
$deductionIdMap = $this->addDeductions(
$this->model,
$deductionGroup->id,
$salaryComponentTypeIdMap,
$salaryComponentIdMap,
$payrollColumnIdMap
);
$this->refinePayrollColumns($payrollColumnIdMap, $deductionIdMap);
$payslipTemplate = $this->addPayslipTemplate($this->model, $payrollColumnIdMap);
$this->addPayroll(
$this->model,
$payrollColumnIdMap,
$deductionGroup->id,
$payslipTemplate->id,
$compStructure->id
);
$this->lastStatus = IceResponse::SUCCESS;
return $this->model;
}
protected function addPayroll($model, $payrollColumnIdMap, $deductionGroupId, $payslipId, $departmentId)
{
$samplePr = $model->samplePayroll;
$payroll = new Payroll();
$payroll->name = $samplePr->name;
$payroll->pay_period = $samplePr->pay_period;
$payroll->date_start = $samplePr->date_start;
$payroll->date_end = $samplePr->date_end;
$payroll->columns = $this->replaceJsonIds(
$samplePr->columns,
$payrollColumnIdMap
);
$payroll->status = $samplePr->status;
$payroll->deduction_group = $deductionGroupId;
$payroll->payslipTemplate = $payslipId;
$payroll->department = $departmentId;
$ok = $payroll->Save();
if (!$ok) {
LogManager::getInstance()->error($payroll->ErrorMsg());
}
}
protected function addPayslipTemplate($model, $payrollColumnIdMap)
{
$pst = $model->payslipTemplate;
$payslipTemplate = new PayslipTemplate();
$payslipTemplate->name = $pst->name;
$data = json_decode($pst->data, true);
$newData = [];
foreach ($data as $row) {
if ($row['type'] === 'Payroll Column') {
$row['payrollColumn'] = $payrollColumnIdMap[$row['payrollColumn']];
}
$newData[] = $row;
}
$payslipTemplate->data = json_encode($newData);
$payslipTemplate->status = $pst->status;
$payslipTemplate->created = $pst->created;
$payslipTemplate->updated = $pst->updated;
$payslipTemplate->Save();
return $payslipTemplate;
}
protected function refinePayrollColumns($payrollColumnIdMap, $deductionIdMap)
{
$payrollColumn = new PayrollColumn();
$columns = $payrollColumn->Find('id in ('.implode(',', array_values($payrollColumnIdMap)).')', array());
foreach ($columns as $column) {
$column->deductions = $this->replaceJsonIds(
$column->deductions,
$deductionIdMap
);
$column->add_columns = $this->replaceJsonIds(
$column->add_columns,
$payrollColumnIdMap
);
$column->sub_columns = $this->replaceJsonIds(
$column->sub_columns,
$payrollColumnIdMap
);
$column->sub_columns = $this->replaceJsonIdsForCalculations(
$column->calculation_columns,
$payrollColumnIdMap
);
$column->Save();
}
}
protected function addPayrollColumns($model, $deductionGroupId, $salaryComponentIdMap)
{
$payrollColumnIdMap = [];
$payrollColumns = $model->columns;
foreach ($payrollColumns as $payrollColumn) {
$column = new PayrollColumn();
$column->name = $payrollColumn->name;
$column->calculation_hook = $payrollColumn->calculation_hook;
$column->salary_components = $this->replaceJsonIds(
$payrollColumn->salary_components,
$salaryComponentIdMap
);
$column->deductions = $payrollColumn->deductions; // need to map
$column->add_columns = $payrollColumn->add_columns; // need to map
$column->sub_columns = $payrollColumn->sub_columns; // need to map
$column->colorder = $payrollColumn->colorder;
$column->editable = $payrollColumn->editable;
$column->enabled = $payrollColumn->enabled;
$column->default_value = $payrollColumn->default_value;
$column->calculation_columns = $payrollColumn->calculation_columns; // need to map
$column->calculation_function = $payrollColumn->calculation_function;
$column->deduction_group = $deductionGroupId;
$column->Save();
$payrollColumnIdMap[$payrollColumn->id] = $column->id;
}
return $payrollColumnIdMap;
}
protected function addDeductions(
$model,
$deductionGroupId,
$salaryComponentTypeIdMap,
$salaryComponentIdMap,
$payrollColumnIdMap
) {
$deductionIdMap = [];
$deductions = $model->deductions;
foreach ($deductions as $deduction) {
$dbDeduction = new Deduction();
$dbDeduction->name = $deduction->name;
$dbDeduction->componentType = $this->replaceJsonIds(
$deduction->componentType,
$salaryComponentTypeIdMap
);
$dbDeduction->component = $this->replaceJsonIds(
$deduction->component,
$salaryComponentIdMap
);
$dbDeduction->payrollColumn = $payrollColumnIdMap[$deduction->payrollColumn];
$dbDeduction->rangeAmounts = $deduction->rangeAmounts;
$dbDeduction->deduction_group = $deductionGroupId;
$dbDeduction->Save();
$deductionIdMap[$deduction->id] = $dbDeduction->id;
}
return $deductionIdMap;
}
protected function createDeductionGroup($model)
{
$deductionGroup = new DeductionGroup();
$deductionGroup->Load("name = ?", array($model->name));
if (!empty($deductionGroup->id)) {
return false;
}
$deductionGroup->name = $model->name;
$deductionGroup->description = $model->description;
$ok = $deductionGroup->Save();
if (!$ok) {
return false;
}
return $deductionGroup;
}
/**
* @param $salaryComponentTypeIdMap
*/
protected function addSalaryComponentTypes($model)
{
$salaryComponentTypeIdMap = [];
$salaryComponentTypes = $model->salaryComponentTypes;
foreach ($salaryComponentTypes as $salaryComponentType) {
$tempSct = new SalaryComponentType();
$tempSct->Load(
"code = ? and name = ?",
array($salaryComponentType->code, $salaryComponentType->name)
);
if (!empty($tempSct->id)) {
$salaryComponentTypeIdMap[$salaryComponentType->id] = $tempSct->id;
continue;
}
$tempSct = new SalaryComponentType();
$tempSct->code = $salaryComponentType->code;
$tempSct->name = $salaryComponentType->name;
$tempSct->Save();
$salaryComponentTypeIdMap[$salaryComponentType->id] = $tempSct->id;
}
return $salaryComponentTypeIdMap;
}
protected function addSalaryComponents($model, $salaryComponentTypeIdMap)
{
$salaryComponentIdMap = [];
$salaryComponents = $model->salaryComponents;
foreach ($salaryComponents as $salaryComponent) {
$tempSct = new SalaryComponent();
$tempSct->Load(
"componentType = ? and name = ?",
array(
$salaryComponentTypeIdMap[$salaryComponent->componentType],
$salaryComponent->name
)
);
if (!empty($tempSct->id)) {
$salaryComponentIdMap[$salaryComponent->id] = $tempSct->id;
continue;
}
$tempSct = new SalaryComponent();
$tempSct->name = $salaryComponent->name;
$tempSct->componentType = $salaryComponentTypeIdMap[$salaryComponent->componentType];
$tempSct->details = $salaryComponent->details;
$tempSct->Save();
$salaryComponentIdMap[$salaryComponent->id] = $tempSct->id;
}
return $salaryComponentIdMap;
}
private function replaceJsonIds($ids, $idMap)
{
$newIds = [];
$data = json_decode($ids, true);
foreach ($data as $id) {
$newIds[] = $idMap[$id];
}
return json_encode($newIds);
}
private function replaceJsonIdsForCalculations($calculations, $idMap)
{
$newCalculations = [];
$data = json_decode($calculations, true);
foreach ($data as $cal) {
$cal['column'] = $idMap[$cal['column']];
$newCalculations[] = $cal;
}
return json_encode($newCalculations);
}
public function getLastStatus()
{
return $this->lastStatus;
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace Data\Common\Model;
use Model\BaseModel;
class DataImport extends BaseModel
{
public $table = 'DataImport';
public function getAdminAccess()
{
return array("get","element","save","delete");
}
public function getUserAccess()
{
return array("get","element");
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace Data\Common\Model;
use Classes\IceResponse;
use Model\BaseModel;
class DataImportFile extends BaseModel
{
public $table = 'DataImportFiles';
public function getAdminAccess()
{
return array("get","element","save","delete");
}
public function getUserAccess()
{
return array("get","element");
}
public function executePreSaveActions($obj)
{
if (empty($obj->status)) {
$obj->status = "Not Processed";
}
return new IceResponse(IceResponse::SUCCESS, $obj);
}
}