openstamanager/modules/articoli/src/API/v1/Articoli.php

148 lines
4.8 KiB
PHP
Raw Normal View History

2019-07-19 15:23:00 +02:00
<?php
2020-09-07 15:04:06 +02:00
/*
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
2021-01-20 15:08:51 +01:00
* Copyright (C) DevCode s.r.l.
2020-09-07 15:04:06 +02:00
*
* 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/>.
*/
2019-07-19 15:23:00 +02:00
namespace Modules\Articoli\API\v1;
use API\Interfaces\CreateInterface;
2019-07-19 15:23:00 +02:00
use API\Interfaces\RetrieveInterface;
use API\Interfaces\UpdateInterface;
2019-07-19 16:51:52 +02:00
use API\Resource;
use Modules\Articoli\Articolo;
use Modules\Articoli\Categoria;
2019-07-19 15:23:00 +02:00
class Articoli extends Resource implements RetrieveInterface, UpdateInterface, CreateInterface
2019-07-19 15:23:00 +02:00
{
public function retrieve($request)
{
$table = 'mg_articoli';
$select = [
'mg_articoli.*',
2024-03-13 10:14:41 +01:00
'`categorie`.`nome` AS categoria',
'`sottocategorie`.`nome` AS sottocategoria',
];
2024-02-29 15:10:55 +01:00
$joins[] = [
2024-03-20 16:15:37 +01:00
'mg_articoli_lang' => 'mg_articoli_lang.id_record = mg_articoli.id AND mg_articoli_lang.id_lang = '.\Models\Locale::getDefault()->id,
2024-03-22 15:52:24 +01:00
];
2024-02-29 15:10:55 +01:00
$joins[] = [
2024-03-13 10:14:41 +01:00
'`mg_categorie` AS categorie',
'`mg_articoli`.`id_categoria`',
'`categorie`.`id`',
];
2024-03-01 09:08:08 +01:00
$joins[] = [
2024-03-20 16:15:37 +01:00
'mg_categorie_lang AS categorie_lang' => '`mg_categorie_lang`.`id_record` = `categorie`.`id` AND `mg_categorie_lang`.`id_lang` = '.\Models\Locale::getDefault()->id,
2024-03-22 15:52:24 +01:00
];
2024-03-01 09:08:08 +01:00
$joins[] = [
2024-03-13 10:14:41 +01:00
'`mg_categorie` AS sottocategorie',
'`mg_articoli`.`id_sottocategoria`',
'`sottocategorie`.`id`',
];
2024-03-01 09:08:08 +01:00
$joins[] = [
2024-03-20 16:15:37 +01:00
'`mg_categorie_lang` AS sottocategorie_lang' => '`mg_categorie_lang`.`id_record` = `sottocategorie`.`id` AND `mg_categorie_lang`.`id_lang` = '.\Models\Locale::getDefault()->id,
2024-03-01 09:08:08 +01:00
];
2024-03-13 10:14:41 +01:00
$where[] = ['`mg_articoli`.`deleted_at`', '=', null];
2023-09-21 11:59:43 +02:00
$whereraw = [];
2024-03-13 10:14:41 +01:00
$order['`mg_articoli`.`id`'] = 'ASC';
2019-07-19 15:23:00 +02:00
return [
'table' => $table,
'select' => $select,
'joins' => $joins,
'where' => $where,
2023-09-21 11:59:43 +02:00
'whereraw' => $whereraw,
2023-09-21 17:34:30 +02:00
'order' => $order,
2019-07-19 15:23:00 +02:00
];
}
public function create($request)
{
$data = $request['data'];
// Gestione categoria
list($categoria, $sottocategoria) = $this->gestioneCategorie($data['categoria'], $data['sottocategoria']);
2024-03-19 18:18:11 +01:00
$articolo = Articolo::build($data['codice'], $categoria, $sottocategoria);
$articolo->setPrezzoVendita($data['prezzo_vendita'], $articolo->idiva_vendita);
2024-03-19 18:18:11 +01:00
$articolo->setTranslation('name', $data['descrizione']);
$articolo->save();
return [
'id' => $articolo->id,
];
}
public function update($request)
{
$data = $request['data'];
$articolo = Articolo::find($request['id']);
list($categoria, $sottocategoria) = $this->gestioneCategorie($data['categoria'], $data['sottocategoria']);
// Gestione categoria
if (!empty($categoria)) {
$articolo->categoria()->associate($categoria);
}
if (!empty($sottocategoria)) {
$articolo->sottocategoria()->associate($sottocategoria);
}
2024-03-19 18:18:11 +01:00
$articolo->setTranslation('name', $data['descrizione']);
$articolo->setPrezzoVendita($data['prezzo_vendita'], $articolo->idiva_vendita);
$articolo->save();
}
protected function gestioneCategorie($nome_categoria, $nome_sottocategoria)
{
$sottocategoria = null;
// Gestione categoria
$categoria = Categoria::where('nome', '=', $nome_categoria)
->first();
if (empty($categoria) && !empty($nome_categoria)) {
$categoria = Categoria::build($nome_categoria);
$categoria->save();
}
// Caso categoria inesistente
if (empty($categoria)) {
return [$categoria, $sottocategoria];
}
// Gestione sotto-categoria
$sottocategoria = Categoria::where('nome', '=', $nome_sottocategoria)
->where('parent', '=', $categoria->id)
->first();
if (empty($sottocategoria) && !empty($nome_sottocategoria)) {
$sottocategoria = Categoria::build($nome_sottocategoria);
$sottocategoria->parent = $categoria->id;
$sottocategoria->save();
}
return [$categoria, $sottocategoria];
}
2019-07-19 15:23:00 +02:00
}