Facturacion

This commit is contained in:
2023-11-22 19:08:19 -03:00
parent b4742a501e
commit 9ab0515954
45 changed files with 1846 additions and 71 deletions

View File

@ -0,0 +1,29 @@
<?php
namespace Incoviba\Common\Implement\Database\Query;
use Incoviba\Common\Define;
class Builder implements Define\Query\Builder
{
public function create(string $table_name): Create
{
return (new Create())->table($table_name);
}
public function select(array|string $columns = '*'): Select
{
return (new Select())->columns($columns);
}
public function insert(): Insert
{
return new Insert();
}
public function update(string $table): Update
{
return (new Update())->table($table);
}
public function delete(): Delete
{
return new Delete();
}
}

View File

@ -0,0 +1,102 @@
<?php
namespace Incoviba\Common\Implement\Database\Query;
use Incoviba\Common\Define;
use Incoviba\Common\Ideal;
class Create extends Ideal\Query implements Define\Query\Create
{
protected string $name;
protected array $definitions;
protected array $options;
protected array $partitions;
public function table(string $name): Define\Query\Create
{
$this->name = $name;
return $this;
}
public function definitions(Define\Query\Create\CreateDefinition|array|string $create_definitions): Define\Query\Create
{
if (is_array($create_definitions)) {
foreach ($create_definitions as $definition) {
$this->addDefinition($definition);
}
return $this;
}
return $this->addDefinition($create_definitions);
}
public function options(array|string $table_options): Define\Query\Create
{
if (is_string($table_options)) {
return $this->addOption($table_options);
}
foreach ($table_options as $option) {
$this->addOption($option);
}
return $this;
}
public function partition(array|string $partition_options): Define\Query\Create
{
if (is_string($partition_options)) {
return $this->addPartition($partition_options);
}
foreach ($partition_options as $option) {
$this->addPartition($option);
}
return $this;
}
public function build(): string
{
$query = [
"CREATE TABLE {$this->name}",
$this->getDefinitions(),
$this->getOptions(),
$this->getPartitions()
];
return implode('', $query);
}
protected function addDefinition(string $definition): Create
{
if (!isset($this->definitions)) {
$this->definitions = [];
}
$this->definitions []= $definition;
return $this;
}
protected function addOption(string $option): Create
{
if (!isset($this->options)) {
$this->options = [];
}
$this->options []= $option;
return $this;
}
protected function addPartition(string $partition): Create
{
if (!isset($this->partitions)) {
$this->partitions = [];
}
$this->partitions []= $partition;
return $this;
}
protected function getDefinitions(): string
{
return ' (' . implode(', ', $this->definitions) . ')';
}
protected function getOptions(): string
{
if (!isset($this->options) or count($this->options) <= 0) {
return '';
}
return ' ' . implode(' ', $this->options);
}
protected function getPartitions(): string
{
if (!isset($this->partitions) or count($this->partitions) <= 0) {
return '';
}
return ' PARTITION BY (' . implode(', ', $this->partitions) . ')';
}
}

View File

@ -0,0 +1,104 @@
<?php
namespace ProVM\Implement\Data\Query\Create;
use ProVM\Define\Query\Create\CreateDefinition;
class Definition implements CreateDefinition
{
protected string $name;
protected string $type;
protected int $size;
protected bool $primary;
protected bool $auto_increment;
protected bool $unsigned;
protected mixed $default;
protected string $foreign_table;
protected string $foreign_key;
protected int $foreign_delete;
protected int $foreign_update;
public function name(string $name): CreateDefinition
{
$this->name = $name;
return $this;
}
public function type(string $type, ?int $size = null): CreateDefinition
{
$this->type = $type;
if ($size !== null) {
$this->size = $size;
}
return $this;
}
public function primary(): CreateDefinition
{
$this->primary = true;
return $this;
}
public function autoIncrement(): CreateDefinition
{
$this->auto_increment = true;
return $this;
}
public function unsigned(): CreateDefinition
{
$this->unsigned = true;
return $this;
}
public function default(mixed $value): CreateDefinition
{
$this->default = $value;
return $this;
}
public function foreign(string $reference_table, string $reference_column = 'id', int $on_delete = CreateDefinition::CASCADE, int $on_update = CreateDefinition::CASCADE): CreateDefinition
{
$this->foreign_table = $reference_table;
$this->foreign_key = $reference_column;
$this->foreign_delete = $on_delete;
$this->foreign_update = $on_update;
return $this;
}
public function __toString(): string
{
$type = $this->type ?? 'int';
if (isset($this->size)) {
$type = "{$type}({$this->size})";
} elseif (in_array($type, ['varchar'])) {
$type = "{$type}(255)";
}
$output = [
"`{$this->name}`",
$type
];
if (isset($this->unsigned)) {
$output []= 'UNSIGNED';
}
if (isset($this->default)) {
$default = $this->default;
if (in_array($this->type, ['varchar', 'text', 'char'])) {
$default = "'{$default}'";
}
$output []= "DEFAULT {$default}";
}
if (isset($this->auto_increment)) {
$output []= 'AUTO_INCREMENT';
}
if (isset($this->primary)) {
$output []= 'PRIMARY KEY';
}
if (isset($this->foreign_table)) {
$output []= "REFERENCES `{$this->foreign_table}` (`{$this->foreign_key}`)";
$on = [
'RESTRICT',
'CASCADE',
'SET_NULL',
'NO_ACTION',
'SET_DEFAULT'
];
$output []= "ON DELETE {$on[$this->foreign_delete]}";
$output []= "ON UPDATE {$on[$this->foreign_update]}";
}
return implode(' ', $output);
}
}

View File

@ -0,0 +1,89 @@
<?php
namespace Incoviba\Common\Implement\Database\Query;
use Incoviba\Common\Define;
use Incoviba\Common\Ideal;
class Delete extends Ideal\Query implements Define\Query\Delete
{
protected string $table;
protected array $conditions;
protected array $sorts;
protected int $limit;
public function from(string $table): Delete
{
$this->table = $table;
return $this;
}
public function where(array|string $conditions): Delete
{
if (is_string($conditions)) {
return $this->addCondition($conditions);
}
foreach ($conditions as $condition) {
$this->addCondition($condition);
}
return $this;
}
public function order(array|string $sorting): Delete
{
if (is_string($sorting)) {
return $this->addOrder($sorting);
}
foreach ($sorting as $order) {
$this->addOrder($order);
}
return $this;
}
public function limit(int $limit): Delete
{
$this->limit = $limit;
return $this;
}
public function build(): string
{
$query = [
"DELETE FROM {$this->table}",
$this->getConditions(),
$this->getSorting(),
$this->getLimit()
];
return implode('', $query);
}
protected function addCondition(string $condition): Delete
{
if (!isset($this->conditions)) {
$this->conditions = [];
}
$this->conditions []= $condition;
return $this;
}
protected function getConditions(): string
{
return ' WHERE ' . implode(' AND ', $this->conditions);
}
protected function addOrder(string $order): Delete
{
if (!isset($this->sorts)) {
$this->sorts = [];
}
$this->sorts []= $order;
return $this;
}
protected function getSorting(): string
{
if (!isset($this->sorts) or count($this->sorts) === 0) {
return '';
}
return ' ORDER BY ' . implode(', ', $this->sorts);
}
protected function getLimit(): string
{
if (!isset($this->limit) or $this->limit <= 0) {
return '';
}
return " LIMIT {$this->limit}";
}
}

View File

@ -0,0 +1,87 @@
<?php
namespace Incoviba\Common\Implement\Database\Query;
use Incoviba\Common\Define;
use Incoviba\Common\Ideal;
class Insert extends Ideal\Query implements Define\Query\Insert
{
protected string $table;
protected array $columns;
protected array $values;
protected Define\Query\Select $select;
public function into(string $table, ?array $columns = null): Insert
{
$this->table = $table;
if ($columns !== null) {
return $this->columns($columns);
}
return $this;
}
public function columns(array $columns): Insert
{
foreach ($columns as $column) {
$this->addColumn($column);
}
return $this;
}
public function values(array $values): Insert
{
foreach ($values as $value) {
$this->addValue($value);
}
return $this;
}
public function select(Define\Query\Select $select): Insert
{
$this->select = $select;
return $this;
}
public function build(): string
{
$query = [
"INSERT INTO {$this->table}"
];
if (isset($this->select)) {
$query []= " {$this->select}";
return implode('', $query);
}
$query []= $this->getColumns();
$query []= $this->getValues();
return implode('', $query);
}
protected function addColumn(string $column): Insert
{
if (!isset($this->columns)) {
$this->columns = [];
}
$this->columns []= $column;
return $this;
}
protected function addValue(mixed $value): Insert
{
if (!isset($this->values)) {
$this->values = [];
}
$this->values []= $value;
return $this;
}
protected function getColumns(): string
{
return ' (' . implode(', ', array_map(function(string $column) {return "`{$column}`";}, $this->columns)) . ')';
}
protected function getValues(): string
{
return ' VALUES (' . implode(', ', array_map(function($value) {
if ($value === '?') {
return $value;
}
if ($value === (int) $value) {
return $value;
}
return "'{$value}'";
}, $this->values)) . ')';
}
}

View File

@ -0,0 +1,215 @@
<?php
namespace Incoviba\Common\Implement\Database\Query;
use Incoviba\Common\Define;
use Incoviba\Common\Ideal;
class Select extends Ideal\Query implements Define\Query\Select
{
protected array $columns;
protected string $table;
protected array $joins;
protected array $conditions;
protected array $groups;
protected array $haves;
protected array $orders;
protected int $limit;
protected int $offset;
public function columns(array|string $expressions): Select
{
if (is_string($expressions)) {
return $this->addColumn($expressions);
}
foreach ($expressions as $expression) {
$this->addColumn($expression);
}
return $this;
}
public function from(string $table): Select
{
$this->table = $table;
return $this;
}
public function joined(array|string $joins): Select
{
if (is_string($joins)) {
return $this->addJoin($joins);
}
foreach ($joins as $join) {
$this->addJoin($join);
}
return $this;
}
public function where(array|string $conditions): Select
{
if (is_string($conditions)) {
return $this->addCondition($conditions);
}
foreach ($conditions as $condition) {
$this->addCondition($condition);
}
return $this;
}
public function group(array|string $grouping): Select
{
if (is_string($grouping)) {
return $this->addGroup($grouping);
}
foreach ($grouping as $group) {
$this->addGroup($group);
}
return $this;
}
public function having(array|string $conditions): Select
{
if (is_string($conditions)) {
return $this->addCondition($conditions);
}
foreach ($conditions as $condition) {
$this->addCondition($condition);
}
return $this;
}
public function order(array|string $sorting): Select
{
if (is_string($sorting)) {
return $this->addOrder($sorting);
}
foreach ($sorting as $order) {
$this->addOrder($order);
}
return $this;
}
public function limit(int $limit, ?int $offset = null): Select
{
$this->limit = $limit;
if ($offset !== null) {
return $this->offset($offset);
}
return $this;
}
public function offset(int $offset): Select
{
$this->offset = $offset;
return $this;
}
public function build(): string
{
$query = [
"SELECT {$this->getColumns()} FROM {$this->table}",
$this->getJoins(),
$this->getConditions(),
$this->getGroups(),
$this->getHaving(),
$this->getOrder(),
$this->getLimit()
];
return implode('', $query);
}
protected function addColumn(string $expression): Select
{
if (!isset($this->columns)) {
$this->columns = [];
}
$this->columns []= $expression;
return $this;
}
protected function addJoin(string $join): Select
{
if (!isset($this->joins)) {
$this->joins = [];
}
$this->joins []= $join;
return $this;
}
protected function addCondition(string $condition): Select
{
if (!isset($this->coditions)) {
$this->conditions = [];
}
$this->conditions []= $condition;
return $this;
}
protected function addGroup(string $group): Select
{
if (!isset($this->groups)) {
$this->groups = [];
}
$this->groups []= $group;
return $this;
}
protected function addHaving(string $having): Select
{
if (!isset($this->haves)) {
$this->haves = [];
}
$this->haves []= $having;
return $this;
}
protected function addOrder(string $order): Select
{
if (!isset($this->orders)) {
$this->orders = [];
}
$this->orders []= $order;
return $this;
}
protected function getColumns(): string
{
if (!isset($this->columns) or count($this->columns) === 0) {
return '*';
}
return implode(', ', $this->columns);
}
protected function getJoins(): string
{
if (!isset($this->joins) or count($this->joins) === 0) {
return '';
}
return ' ' . implode(' ', $this->joins);
}
protected function getConditions(): string
{
if (!isset($this->conditions) or count($this->conditions) === 0) {
return '';
}
return ' WHERE ' . implode(' AND ', $this->conditions);
}
protected function getGroups(): string
{
if (!isset($this->groups) or count($this->groups) === 0) {
return '';
}
return ' GROUP BY ' . implode(', ', $this->groups);
}
protected function getHaving(): string
{
if (!isset($this->haves) or count($this->haves) === 0) {
return '';
}
return ' HAVING ' . implode(' AND ', $this->haves);
}
protected function getOrder(): string
{
if (!isset($this->orders) or count($this->orders) === 0) {
return '';
}
return ' ORDER BY ' . implode(', ', $this->orders);
}
protected function getLimit(): string
{
if (!isset($this->limit) or $this->limit <= 0) {
return '';
}
return " LIMIT {$this->limit}{$this->getOffset()}";
}
protected function getOffset(): string
{
if (!isset($this->offset) or $this->offset <= 0) {
return '';
}
return " OFFSET {$this->offset}";
}
}

View File

@ -0,0 +1,113 @@
<?php
namespace Incoviba\Common\Implement\Database\Query;
use Incoviba\Common\Define;
use Incoviba\Common\Ideal;
class Update extends Ideal\Query implements Define\Query\Update
{
protected string $table;
protected array $setPairs;
protected array $conditions;
protected array $orders;
protected int $limit;
public function table(string $table): Define\Query\Update
{
$this->table = $table;
return $this;
}
public function set(array|string $column_pairs): Define\Query\Update
{
if (is_string($column_pairs)) {
return $this->addSet($column_pairs);
}
foreach ($column_pairs as $pair) {
$this->addSet($pair);
}
return $this;
}
public function where(array|string $conditions): Define\Query\Update
{
if (is_string($conditions)) {
return $this->addCondition($conditions);
}
foreach ($conditions as $condition) {
$this->addCondition($condition);
}
return $this;
}
public function order(array|string $ordering): Define\Query\Update
{
if (is_string($ordering)) {
return $this->addOrder($ordering);
}
foreach ($ordering as $order) {
$this->addOrder($order);
}
return $this;
}
public function limit(int $limit): Define\Query\Update
{
$this->limit = $limit;
return $this;
}
public function build(): string
{
$query = [
"UPDATE {$this->table}",
$this->getSet(),
$this->getConditions(),
$this->getOrder(),
$this->getLimit()
];
return implode('', $query);
}
protected function addSet(string $pair): Update
{
if (!isset($this->setPairs)) {
$this->setPairs = [];
}
$this->setPairs []= $pair;
return $this;
}
protected function addCondition(string $condition): Update
{
if (!isset($this->conditions)) {
$this->conditions = [];
}
$this->conditions []= $condition;
return $this;
}
protected function addOrder(string $order): Update
{
if (!isset($this->orders)) {
$this->orders = [];
}
$this->orders []= $order;
return $this;
}
protected function getSet(): string
{
return ' SET ' . implode(', ', $this->setPairs);
}
protected function getConditions(): string
{
return ' WHERE ' . implode(' AND ', $this->conditions);
}
protected function getOrder(): string
{
if (!isset($this->orders) or count($this->orders) === 0) {
return '';
}
return ' ORDER BY ' . implode(', ', $this->orders);
}
protected function getLimit(): string
{
if (!isset($this->limit) or $this->limit <= 0) {
return '';
}
return " LIMIT {$this->limit}";
}
}