mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
261 lines
9.1 KiB
PHP
Executable File
261 lines
9.1 KiB
PHP
Executable File
<?php
|
|
namespace App\Models\Tarifas;
|
|
|
|
class TarifaEnvioPrecioModel extends \App\Models\GoBaseModel
|
|
{
|
|
protected $table = "tarifas_envios_precios";
|
|
|
|
/**
|
|
* Whether primary key uses auto increment.
|
|
*
|
|
* @var bool
|
|
*/
|
|
protected $useAutoIncrement = true;
|
|
|
|
const SORTABLE = [
|
|
0 => "t1.proveedor",
|
|
1 => "t1.tipo_envio",
|
|
2 => "t1.peso_min",
|
|
3 => "t1.peso_max",
|
|
4 => "t1.precio_min",
|
|
5 => "t1.precio_max",
|
|
6 => "t1.precio_adicional",
|
|
7 => "t1.margen"
|
|
];
|
|
|
|
protected $allowedFields = [
|
|
"tarifa_envio_id",
|
|
"proveedor_id",
|
|
"tipo_envio",
|
|
"peso_min",
|
|
"peso_max",
|
|
"precio_min",
|
|
"precio_max",
|
|
"precio_adicional",
|
|
"margen",
|
|
"deleted_at",
|
|
"is_deleted",
|
|
];
|
|
protected $returnType = "App\Entities\Tarifas\TarifaEnvioPrecioEntity";
|
|
|
|
protected $useTimestamps = true;
|
|
protected $useSoftDeletes = false;
|
|
|
|
protected $createdField = "created_at";
|
|
|
|
protected $updatedField = "updated_at";
|
|
|
|
public static $labelField = "tarifa_envio_id";
|
|
|
|
protected $validationRules = [
|
|
"peso_max" => [
|
|
"label" => "TarifasEnviosPrecios.pesoMax",
|
|
"rules" => "required|decimal",
|
|
],
|
|
"peso_min" => [
|
|
"label" => "TarifasEnviosPrecios.pesoMin",
|
|
"rules" => "required|decimal",
|
|
],
|
|
"precio_min" => [
|
|
"label" => "TarifasEnviosPrecios.precio",
|
|
"rules" => "required|decimal",
|
|
],
|
|
"precio_max" => [
|
|
"label" => "TarifasEnviosPrecios.precio",
|
|
"rules" => "required|decimal",
|
|
],
|
|
"precio_adicional" => [
|
|
"label" => "TarifasEnviosPrecios.precioAdicional",
|
|
"rules" => "required|decimal",
|
|
],
|
|
"margen" => [
|
|
"label" => "TarifasEnviosPrecios.margen",
|
|
"rules" => "required|decimal",
|
|
],
|
|
"tipo_envio" => [
|
|
"label" => "TarifasEnviosPrecios.tipoEnvio",
|
|
"rules" => "required|in_list[cajas,palets]",
|
|
],
|
|
];
|
|
|
|
protected $validationMessages = [
|
|
"peso_max" => [
|
|
"decimal" => "TarifasEnviosPrecios.validation.peso_max.decimal",
|
|
"required" => "TarifasEnviosPrecios.validation.peso_max.required",
|
|
],
|
|
"peso_min" => [
|
|
"decimal" => "TarifasEnviosPrecios.validation.peso_min.decimal",
|
|
"required" => "TarifasEnviosPrecios.validation.peso_min.required",
|
|
],
|
|
"precio_min" => [
|
|
"decimal" => "TarifasEnviosPrecios.validation.precio.decimal",
|
|
"required" => "TarifasEnviosPrecios.validation.precio.required",
|
|
],
|
|
"precio_max" => [
|
|
"decimal" => "TarifasEnviosPrecios.validation.precio.decimal",
|
|
"required" => "TarifasEnviosPrecios.validation.precio.required",
|
|
],
|
|
"precio_adicional" => [
|
|
"decimal" => "TarifasEnviosPrecios.validation.precio_adicional.decimal",
|
|
"required" => "TarifasEnviosPrecios.validation.precio_adicional.required",
|
|
],
|
|
"margen" => [
|
|
"decimal" => "TarifasEnviosPrecios.margen.margen.decimal",
|
|
"required" => "TarifasEnviosPrecios.margen.margen.required",
|
|
],
|
|
"tipo_envio" => [
|
|
"in_list" => "TarifasEnviosPrecios.validation.tipo_envio.in_list",
|
|
"required" => "TarifasEnviosPrecios.validation.tipo_envio.required",
|
|
],
|
|
];
|
|
public function findAllWithAllRelations(string $selcols = "*", int $limit = null, int $offset = 0)
|
|
{
|
|
$sql =
|
|
"SELECT t1." .
|
|
$selcols .
|
|
", t2.id AS tarifa_envio, t3.id AS proveedor FROM " .
|
|
$this->table .
|
|
" t1 LEFT JOIN lg_tarifas_envios t2 ON t1.tarifa_envio_id = t2.id LEFT JOIN lg_proveedores t3 ON t1.proveedor_id = t3.id";
|
|
if (!is_null($limit) && intval($limit) > 0) {
|
|
$sql .= " LIMIT " . intval($limit);
|
|
}
|
|
|
|
if (!is_null($offset) && intval($offset) > 0) {
|
|
$sql .= " OFFSET " . intval($offset);
|
|
}
|
|
|
|
$query = $this->db->query($sql);
|
|
$result = $query->getResultObject();
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Get resource data.
|
|
*
|
|
* @param string $search
|
|
*
|
|
* @return \CodeIgniter\Database\BaseBuilder
|
|
*/
|
|
public function getResource(string $search = "", $tarifa_envio_id = -1)
|
|
{
|
|
$builder = $this->db
|
|
->table($this->table . " t1")
|
|
->select(
|
|
"t1.id AS id, t1.tipo_envio AS tipo_envio, t1.peso_min AS peso_min, t1.peso_max AS peso_max, t1.precio_min AS precio_min, t1.precio_max AS precio_max,
|
|
t1.precio_adicional AS precio_adicional, t1.margen AS margen, t2.id AS tarifa_envio, t3.id AS proveedor_id, t3.nombre AS proveedor"
|
|
);
|
|
$builder->join("tarifas_envios_zonas t2", "t1.tarifa_envio_id = t2.id", "left");
|
|
$builder->join("lg_proveedores t3", "t1.proveedor_id = t3.id", "left");
|
|
$builder->where("t1.tarifa_envio_id", $tarifa_envio_id);
|
|
|
|
//JJO
|
|
$builder->where("t1.is_deleted", 0);
|
|
|
|
return empty($search)
|
|
? $builder
|
|
: $builder
|
|
->groupStart()
|
|
->like("t1.tipo_envio", $search)
|
|
->orLike("t1.peso_min", $search)
|
|
->orLike("t1.peso_max", $search)
|
|
->orLike("t1.precio_min", $search)
|
|
->orLike("t1.precio_max", $search)
|
|
->orLike("t1.precio_adicional", $search)
|
|
->orLike("t1.margen", $search)
|
|
->orLike("t3.nombre", $search)
|
|
->orLike("t1.tipo_envio", $search)
|
|
->orLike("t1.peso_min", $search)
|
|
->orLike("t1.peso_max", $search)
|
|
->orLike("t1.precio_min", $search)
|
|
->orLike("t1.precio_max", $search)
|
|
->orLike("t1.precio_adicional", $search)
|
|
->orLike("t1.margen", $search)
|
|
->orLike("t3.nombre", $search)
|
|
->groupEnd();
|
|
}
|
|
|
|
|
|
public function checkIntervals($data = [], $precio_id = null, $zona_id = null){
|
|
|
|
helper('general');
|
|
|
|
if(floatval($data["peso_min"])>= floatval($data["peso_max"])){
|
|
return lang('TarifasEnviosPrecios.validation.error_precio_range');
|
|
}
|
|
|
|
$rows = $this->db
|
|
->table($this->table)
|
|
->select("id, proveedor_id, tipo_envio, peso_min, peso_max")
|
|
->where("is_deleted", 0)
|
|
->where("tarifa_envio_id", $zona_id)
|
|
->get()->getResultObject();
|
|
|
|
|
|
foreach ($rows as $row) {
|
|
if (!is_null($precio_id)){
|
|
if($row->id == $precio_id){
|
|
continue;
|
|
}
|
|
}
|
|
if($row->proveedor_id == $data["proveedor_id"] && $row->tipo_envio == $data["tipo_envio"]){
|
|
if(check_overlap(floatval($data["peso_min"]), floatval($data["peso_max"]),
|
|
$row->peso_min, $row->peso_max)){
|
|
return lang('TarifasEnviosPrecios.validation.error_precio_overlap');
|
|
}
|
|
}
|
|
}
|
|
|
|
return "";
|
|
}
|
|
|
|
public function removeAllPrecioLineas($tarifa_envio_id = -1, $datetime = null, $delete_flag=1){
|
|
|
|
$builder = $this->db
|
|
->table($this->table)
|
|
->set(['deleted_at' => $datetime->format('Y-m-d H:i:s'),
|
|
'is_deleted' => $delete_flag])
|
|
->where('tarifa_envio_id',$tarifa_envio_id)
|
|
->update();
|
|
|
|
return $builder;
|
|
}
|
|
|
|
public function getEnvioPrecio($tarifa_envio_id, $peso, $tipo_envio){
|
|
// primero se checkea que el peso no sea mayor
|
|
// que el valor mas grande
|
|
$builder = $this->db
|
|
->table($this->table . " t1")
|
|
->selectMax('t1.peso_max')
|
|
->where('t1.tipo_envio', $tipo_envio)
|
|
->where("t1.tarifa_envio_id", $tarifa_envio_id)
|
|
->where('t1.is_deleted', 0);
|
|
$peso_maximo = $builder->get()->getResult();
|
|
if(count($peso_maximo) > 0)
|
|
$peso_maximo = floatval($peso_maximo[0]->peso_max);
|
|
else // no hay resultados
|
|
return [];
|
|
|
|
|
|
$builder = $this->db
|
|
->table($this->table . " t1")
|
|
->select(
|
|
"t1.id AS id, t1.tipo_envio AS tipo_envio, t1.peso_min AS peso_min, t1.peso_max AS peso_max, t1.precio_min AS precio_min, t1.precio_max AS precio_max,
|
|
t1.precio_adicional AS precio_adicional, t1.margen AS margen, t2.id AS proveedor_id, t2.nombre AS proveedor"
|
|
);
|
|
$builder->join("lg_proveedores t2", "t1.proveedor_id = t2.id", "left");
|
|
$builder->where("t1.tarifa_envio_id", $tarifa_envio_id);
|
|
$builder->where("t1.tipo_envio", $tipo_envio);
|
|
$builder->where('t1.is_deleted', 0);
|
|
|
|
if($peso_maximo<$peso){
|
|
$builder->where('t1.peso_max', $peso_maximo);
|
|
}
|
|
else{
|
|
$builder->where('t1.peso_min <=', $peso);
|
|
$builder->where('t1.peso_max >=', $peso);
|
|
}
|
|
|
|
return $builder->get()->getResultObject();
|
|
}
|
|
}
|