refactor: aggiunta classe marchi e allineamento codice

This commit is contained in:
valentina 2024-10-16 16:43:58 +02:00
parent 3af079143c
commit 82e4925269
7 changed files with 108 additions and 19 deletions

View File

@ -37,10 +37,9 @@ echo '
<div class="col-md-9">';
$marchio = $dbo->fetchOne('SELECT `link`, `name` FROM mg_marchi WHERE id = '.$articolo->id_marchio);
// Articolo
echo '
'.($articolo->id_marchio ? '<p class="float-right"><i class="fa fa-tag"></i> '.($marchio['link']? '<a href="'.$marchio['link'].'" target="_blank" rel="noopener noreferrer"> '.$marchio['name'].'</a>' : $marchio['name']).'</p>' : '').
'.($articolo->id_marchio ? '<p class="float-right"><i class="fa fa-tag"></i> '.($articolo->marchio->name? '<a href="'.$articolo->marchio->link.'" target="_blank" rel="noopener noreferrer"> '.$articolo->marchio->name.'</a>' : $articolo->marchio->name).'</p>' : '').
($articolo->id_categoria ? '<p class="text-muted"> '.$articolo->categoria->getTranslation('title') : '').($articolo->id_sottocategoria ? ' <small><i class="fa fa-chevron-right"></i></small> '.$articolo->sottocategoria->getTranslation('title') : '').'</p>
<h4><b>'.$articolo->getTranslation('title').'</b> '.($articolo->attivo ? '<i class="fa fa-check text-success"></i>' : '<i class="fa fa-times text-danger"></i> ').'</h4>
<p><b>'.$articolo->codice.'</b> '.($articolo->barcode ? ' - <i class="fa fa-barcode"></i> '.$articolo->barcode.'</p>' : '').'</p>

View File

@ -330,6 +330,11 @@ class Articolo extends Model
return $this->belongsTo(Categoria::class, 'id_sottocategoria');
}
public function marchio()
{
return $this->belongsTo(Marchio::class, 'id_marchio');
}
public function dettaglioFornitori()
{
return $this->hasMany(DettaglioFornitore::class, 'id_articolo');

View File

@ -0,0 +1,54 @@
<?php
/*
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
* Copyright (C) DevCode s.r.l.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace Modules\Articoli;
use Common\SimpleModelTrait;
use Illuminate\Database\Eloquent\Model;
use Traits\HierarchyTrait;
use Traits\RecordTrait;
class Marchio extends Model
{
use SimpleModelTrait;
use HierarchyTrait;
use RecordTrait;
protected $table = 'mg_marchi';
public static function build($nome = null)
{
$model = new static();
$model->name = $nome;
$model->save();
return $model;
}
public function articoli()
{
return $this->hasMany(Articolo::class, 'id_marchio');
}
public function getModuleAttribute()
{
return 'Marchi';
}
}

View File

@ -19,27 +19,55 @@
include_once __DIR__.'/../../core.php';
use Modules\Articoli\Marchio;
switch (post('op')) {
// Aggiorno informazioni di base marchio
case 'update':
$dbo->update('mg_marchi', [
'name' => post('name'),
'link' => post('link'),
], ['id' => $id_record]);
$nome = filter('name');
$link = filter('link');
flash()->info(tr('Informazioni salvate correttamente!'));
$marchio_new = Marchio::where('name', '=', $nome)->first();
if (!empty($marchio_new)) {
flash()->error(tr('Questo nome è già stato utilizzato per un altro marchio.'));
} else {
$marchio = Marchio::find($id_record);
$marchio->name = $nome;
$marchio->link = $link;
$marchio->save();
flash()->info(tr('Marchio aggiornato!'));
}
if (isAjaxRequest()) {
echo json_encode(['id' => $id_record, 'text' => $nome]);
}
break;
// Aggiungo marchio
case 'add':
$dbo->insert('mg_marchi', [
'name' => post('name'),
'link' => post('link'),
]);
$id_record = $dbo->lastInsertedID();
$nome = filter('name');
$link = filter('link');
flash()->info(tr('Aggiunto nuovo marchio!'));
$marchio_new = Marchio::where('name', '=', $nome)->first();
if (!empty($marchio_new)) {
flash()->error(tr('Questo nome è già stato utilizzato per un altro marchio.'));
} else {
$marchio = Marchio::build($nome);
$id_record = $dbo->lastInsertedID();
$marchio->link = $link;
$marchio->save();
flash()->info(tr('Aggiunto nuovo marchio'));
}
if (isAjaxRequest()) {
echo json_encode(['id' => $id_record, 'text' => $nome]);
}
break;

View File

@ -27,7 +27,7 @@ $id_anagrafica = filter('id_anagrafica');
<div class="row">
<div class="col-md-4">
{[ "type": "text", "label": "<?php echo tr('name'); ?>", "name": "name", "required": 1, "validation": "name" ]}
{[ "type": "text", "label": "<?php echo tr('Nome'); ?>", "name": "name", "required": 1, "validation": "name" ]}
</div>
<div class="col-md-4">
{[ "type": "text", "label": "<?php echo tr('Link produttore'); ?>", "name": "link", "value":"$link$"]}

View File

@ -45,15 +45,15 @@ include_once __DIR__.'/../../core.php';
</form>
<?php
$elementi = $dbo->fetchArray('SELECT `mg_articoli`.`id`, CONCAT(`mg_articoli`.`codice`, " - ", `mg_articoli_lang`.`title`) AS `descrizione` FROM `mg_articoli` INNER JOIN mg_articoli_lang ON (`mg_articoli_lang`.`id_record` = `mg_articoli`.`id` AND `mg_articoli_lang`.`id_lang` = '.prepare(Models\Locale::getDefault()->id).') WHERE `mg_articoli`.`id_marchio` = '.prepare($id_record));
$articoli = $marchio->articoli;
$class = '';
if (!empty($elementi)) {
if (!empty($articoli)) {
echo '
<div class="card card-warning collapsable collapsed-card">
<div class="card-header with-border">
<h3 class="card-title"><i class="fa fa-warning"></i> '.tr('Articoli collegati: _NUM_', [
'_NUM_' => count($elementi),
'_NUM_' => count($articoli),
]).'</h3>
<div class="card-tools pull-right">
<button type="button" class="btn btn-tool" data-card-widget="collapse"><i class="fa fa-plus"></i></button>
@ -62,9 +62,9 @@ if (!empty($elementi)) {
<div class="card-body">
<ul>';
foreach ($elementi as $elemento) {
foreach ($articoli as $articolo) {
echo '
<li>'.Modules::link('Articoli', $elemento['id'], $elemento['descrizione']).'</li>';
<li>'.Modules::link('Articoli', $articolo->id, $articolo->codice.' - '.$articolo->getTranslation('title')).'</li>';
}
$class = 'disabled';

View File

@ -18,7 +18,10 @@
*/
include_once __DIR__.'/../../core.php';
use Modules\Articoli\Marchio;
if (isset($id_record)) {
$record = $dbo->fetchOne('SELECT * FROM mg_marchi WHERE id='.prepare($id_record));
$marchio = Marchio::find($id_record);
}