Skip to content

Commit

Permalink
interface gestão de códigos de serviços #9
Browse files Browse the repository at this point in the history
  • Loading branch information
andrekutianski committed Oct 18, 2021
1 parent 89d0038 commit 0c0e66a
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

### Rebrand do Módulo

#### Interfaces

* Nova interface de configuração
* Nova interface de edição de código de serviço

#### Migração

* Adicionada rotina que migra os dados da versão anterior a 2.0 na ativação do módulo. Desta forma não é preciso se preocupar na reconfiguração do módulo.
Expand Down
69 changes: 64 additions & 5 deletions modules/addons/NFEioServiceInvoices/lib/Admin/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@
require_once(dirname(dirname(__DIR__)) . DS . 'Loader.php');

use NFEioServiceInvoices\CustomFields;
use NFEioServiceInvoices\Helpers\Versions;
use NFEioServiceInvoices\Migrations\Migrations;
use NFEioServiceInvoices\Models\ModuleConfiguration\Repository;
use Smarty;
use WHMCS\Database\Capsule;
use Plasticbrain\FlashMessages\FlashMessages;
use WHMCSExpert\Template\Template;
use NFEioServiceInvoices\Addon;
use NFEioServiceInvoices\Configuration;
use \NFEioServiceInvoices\Addon;
use \NFEioServiceInvoices\Configuration;

/**
* Sample Admin Area Controller
Expand Down Expand Up @@ -178,6 +175,68 @@ public function configurationSave($vars)
}

}

public function servicesCode($vars)
{
try {


$msg = new FlashMessages;
$template = new Template(Addon::getModuleTemplatesDir());
$config = new \NFEioServiceInvoices\Configuration();
$servicesCodeRepo = new \NFEioServiceInvoices\Models\ProductCode\Repository();
// metodo para verificar se existe algum campo obrigatório não preenchido.
$config->verifyMandatoryFields($vars);
// URL absoluta dos assets
$assetsURL = Addon::I()->getAssetsURL();

$vars['assetsURL'] = $assetsURL;
$vars['dtData'] = $servicesCodeRepo->dataTable();
// parametro para o atributo action dos formulários da página
$vars['formAction'] = 'servicesCodeSave';

if ($msg->hasMessages()) {
$msg->display();
}

return $template->fetch('servicescode', $vars);

} catch (\Exception $e) {
echo $e->getMessage();
}
}

public function servicesCodeSave($vars)
{

$msg = new FlashMessages();
$post = $_POST;

if (!isset($post) && !is_array($post)) {
$msg->error("Erro na submissão: dados inválidos", "{$vars['modulelink']}&action=servicesCode");
}

$productCodeRepo = new \NFEioServiceInvoices\Models\ProductCode\Repository();

if ($post['btnSave'] === 'true') {
$response = $productCodeRepo->save($post);
if ($response) {
$msg->success("Código {$post['service_code']} para {$post['product_name']} atualizado.", "{$vars['modulelink']}&action=servicesCode");
} else {
$msg->info("Nenhuma alteração realizada.", "{$vars['modulelink']}&action=servicesCode");
}
}

if ($post['btnDelete'] === 'true') {
$productCodeRepo->delete($post);
$msg->warning("Código {$post['service_code']} para {$post['product_name']} removido.", "{$vars['modulelink']}&action=servicesCode");

}



}

/**
* Support action.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,74 @@
class Repository extends \WHMCSExpert\mtLibs\models\Repository
{

public $tableName = 'mod_nfeio_si_productcode';
public static $tableName = 'mod_nfeio_si_productcode';
public static $fieldDeclaration = array(
'id',
'product_id',
'code_service',
'create_at',
'update_at',
'ID_user',
);

function getModelClass()
{
return __NAMESPACE__ . '\ProductCode';
return __NAMESPACE__ . '\Repository';
}

public function fieldDeclaration()
{
return self::$fieldDeclaration;
}

public function tableName()
{
return self::$tableName;
}

/**
* verifica se table possui algum registro já inserido
* Realiza um join entre produtos e códigos personalizados de serviços
* e estrutura os dados para a dataTable
* @return array
*/
public function get()
public function dataTable()
{
return Capsule::table($this->tableName)->first();
return Capsule::table('tblproducts')
->leftJoin(self::$tableName, 'tblproducts.id', '=', self::$tableName.'.product_id')
->orderByDesc('tblproducts.id')
->select('tblproducts.id', 'tblproducts.name', self::$tableName.'.code_service')
->get()
->toArray();
}

public function save($data)
{
/*if (!in_array( 'product_id', $data) && !in_array('service_code', $data)) {
return false;
}*/

try {
return Capsule::table(self::$tableName)->updateOrInsert(
[ 'product_id' => $data['product_id'] ],
[
'code_service' => $data['service_code'],
'ID_user' => 1,
]
);
} catch (\Exception $exception) {
echo $exception->getMessage();
}
}

public function delete($data)
{
try {
return Capsule::table(self::$tableName)
->where('product_id', '=', $data['product_id'])
->delete();
} catch (\Exception $exception) {
echo $exception->getMessage();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{include file="includes/menu.tpl"}
<script>
let dtData = {$dtJsonData};
</script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/bs/dt-1.11.3/af-2.3.7/b-2.0.1/fh-3.2.0/datatables.min.css"/>
<script type="text/javascript" src="https://cdn.datatables.net/v/bs/dt-1.11.3/af-2.3.7/b-2.0.1/fh-3.2.0/datatables.min.js"></script>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title text-center">Código de Serviço</h3>
</div>
<div class="panel-body">
<p>Gerencie os códigos de serviço individualmente por produto existente no WHMCS.</p>
<div class="panel panel-default">
<div class="panel-body">
<table id="productCodeTable" class="table table-hover">
<thead>
<tr>
<th>ID</th>
<th>Nome</th>
<th>Código do Serviço</th>
<th>Ação</th>
</tr>
</thead>
<tbody>
{foreach from=$dtData item=produto }
<tr>
<form action="{$modulelink}&action={$formAction}" method="post">
<td>{$produto->id}</td>
<td>{$produto->name}</td>
<td><input class="form-control" type="text" name="service_code" value="{$produto->code_service}"></td>
<td>
<input type="hidden" name="product_id" value="{$produto->id}">
<input type="hidden" name="product_name" value="{$produto->name}">
<button type="submit" class="btn btn-success btn-sm" name="btnSave" value="true">Salvar Código</button>
<button type="submit" class="btn btn-danger btn-sm" name="btnDelete" value="true">Excluir Código</button>
</td>
</form>

</tr>
{/foreach}
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>

{literal}
<script>
$(document).ready( function () {
$('#productCodeTable').DataTable({
language: {
url: "https://cdn.datatables.net/plug-ins/1.11.3/i18n/pt_br.json"
},
order: [[0, "desc"]]
});
});
</script>
{/literal}

0 comments on commit 0c0e66a

Please sign in to comment.