Base
This commit is contained in:
18
app/Alias/Excel/Style/Mes.php
Normal file
18
app/Alias/Excel/Style/Mes.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
namespace App\Alias\Excel\Style;
|
||||
|
||||
use Slam\Excel\Helper\CellStyleInterface;
|
||||
use Slam\Excel\Pear\Writer\Format;
|
||||
|
||||
class Mes implements CellStyleInterface
|
||||
{
|
||||
public function decorateValue($value)
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
public function styleCell(Format $format): void
|
||||
{
|
||||
$format->setNumFormat('mmm-YY');
|
||||
$format->setAlign('center');
|
||||
}
|
||||
}
|
12
app/Alias/Format.php
Normal file
12
app/Alias/Format.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
use App\Helper\Format as F;
|
||||
class Format
|
||||
{
|
||||
public static function __callstatic($name, $params)
|
||||
{
|
||||
if (method_exists(F, $name)) {
|
||||
return F::$name($params);
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
87
app/Alias/Model.php
Normal file
87
app/Alias/Model.php
Normal file
@ -0,0 +1,87 @@
|
||||
<?php
|
||||
namespace App\Alias;
|
||||
|
||||
use Stringy\Stringy;
|
||||
use App\Contract\Auth;
|
||||
|
||||
class Model extends \Model
|
||||
{
|
||||
public function getTable()
|
||||
{
|
||||
return parent::_get_table_name(static::class);
|
||||
}
|
||||
protected function log()
|
||||
{
|
||||
if (strpos(get_called_class(), 'Incoviba\\common\\') !== false) {
|
||||
return;
|
||||
}
|
||||
$user = Auth::User()->id;
|
||||
$orm = $this->orm;
|
||||
$ref = new \ReflectionObject($orm);
|
||||
if (!$ref->hasProperty('_dirty_fields')) {
|
||||
return;
|
||||
}
|
||||
$dirty = $ref->getProperty('_dirty_fields');
|
||||
$dirty->setAccessible(true);
|
||||
$new_values = $dirty->getValue($orm);
|
||||
$changes = array_combine(array_keys($new_values), array_fill(0, count($new_values), ['old' => '', 'new' => '']));
|
||||
if ($this->isNew()) {
|
||||
$old = (object) array_combine(array_keys($new_values), array_fill(0, count($new_values), ''));
|
||||
} else {
|
||||
$old = model(get_called_class())->findOne($this->{$this->getId()});
|
||||
}
|
||||
foreach ($new_values as $column => $value) {
|
||||
$changes[$column] = ['column' => $column, 'old' => $old->$column, 'new' => $value];
|
||||
}
|
||||
$action = '[' . get_called_class() . ']';
|
||||
doLog($user, $action, $changes);
|
||||
}
|
||||
public function getId()
|
||||
{
|
||||
if (property_exists(get_called_class(), '_id_column')) {
|
||||
return static::$_id_column;
|
||||
}
|
||||
return $this->id;
|
||||
}
|
||||
public function save()
|
||||
{
|
||||
$ref = new \ReflectionObject($this);
|
||||
if ($ref->hasProperty('_timestamps')) {
|
||||
$ref = $ref->getProperty('_timestamps');
|
||||
$ref->setAccessible(true);
|
||||
if ($ref->getValue()) {
|
||||
if ($this->is_new()) {
|
||||
$this->setExpr('created_at', 'NOW()');
|
||||
}
|
||||
$this->setExpr('updated_at', 'NOW()');
|
||||
}
|
||||
}
|
||||
if (!\ORM::getDb()->inTransaction()) {
|
||||
\ORM::getDb()->beginTransaction();
|
||||
}
|
||||
try {
|
||||
parent::save();
|
||||
if (\ORM::getDb()->inTransaction()) {
|
||||
\ORM::getDb()->commit();
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
if (\ORM::getDb()->inTransaction()) {
|
||||
\ORM::getDb()->rollBack();
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
$this->log();
|
||||
}
|
||||
public function __call($method, $args)
|
||||
{
|
||||
if (!method_exists($this, $method)) {
|
||||
$str = '' . Stringy::create($method)->underscored();
|
||||
if (method_exists($this, $str)) {
|
||||
return call_user_func_array([$this, $str], $args);
|
||||
}
|
||||
throw new \BadMethodCallException($method . ' not found in ' . get_class($this));
|
||||
}
|
||||
return call_user_func_array([$this, $str], $args);
|
||||
}
|
||||
}
|
||||
?>
|
20
app/Alias/NewEstado.php
Normal file
20
app/Alias/NewEstado.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
namespace App\Alias;
|
||||
|
||||
use Carbon\Carbon;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Aldarien
|
||||
* @property int id
|
||||
* @property Date fecha
|
||||
*
|
||||
*/
|
||||
class NewEstado extends NewModel
|
||||
{
|
||||
public function fecha()
|
||||
{
|
||||
return Carbon::parse($this->fecha, config('app.timezone'));
|
||||
}
|
||||
}
|
||||
?>
|
9
app/Alias/NewModel.php
Normal file
9
app/Alias/NewModel.php
Normal file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
namespace App\Alias;
|
||||
|
||||
class NewModel extends Model
|
||||
{
|
||||
protected static $_connection_name = 'mysql_copy';
|
||||
protected static $_timestamps = true;
|
||||
}
|
||||
?>
|
15
app/Alias/NewTipo.php
Normal file
15
app/Alias/NewTipo.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
namespace App\Alias;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Aldarien
|
||||
* @property int id
|
||||
* @property string descripcion
|
||||
*
|
||||
*/
|
||||
class NewTipo extends NewModel
|
||||
{
|
||||
protected static $_timestamps = true;
|
||||
}
|
||||
?>
|
8
app/Alias/OldModel.php
Normal file
8
app/Alias/OldModel.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
namespace App\Alias;
|
||||
|
||||
class OldModel extends Model
|
||||
{
|
||||
protected static $_connection_name = 'mysql';
|
||||
}
|
||||
?>
|
155
app/Alias/PHPExcel.php
Normal file
155
app/Alias/PHPExcel.php
Normal file
@ -0,0 +1,155 @@
|
||||
<?php
|
||||
namespace App\Alias;
|
||||
|
||||
use Slam\Excel\Helper as ExcelHelper;
|
||||
|
||||
class PHPExcel
|
||||
{
|
||||
protected $name;
|
||||
protected $filename;
|
||||
protected $columns;
|
||||
protected $data;
|
||||
|
||||
public function __construct($name, $filename)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->filename = $filename;
|
||||
}
|
||||
public function addColumns($fields)
|
||||
{
|
||||
$columns = [];
|
||||
foreach ($fields as $i => $field) {
|
||||
if (is_object($field)) {
|
||||
if (isset($field->style)) {
|
||||
$style = $this->getExcelStyle($field->style);
|
||||
} else {
|
||||
$style = $this->getExcelStyle();
|
||||
}
|
||||
$column = new ExcelHelper\Column($field->name, $field->name, 10, $style);
|
||||
} elseif (is_array($field)) {
|
||||
if (isset($field['style'])) {
|
||||
$style = $this->getExcelStyle($field['style']);
|
||||
} else {
|
||||
$style = $this->getExcelStyle();
|
||||
}
|
||||
$column = new ExcelHelper\Column($field['name'], $field['name'], 10, $style);
|
||||
} else {
|
||||
$style = $this->getExcelStyle();
|
||||
$column = new ExcelHelper\Column($field, $field, 10, $style);
|
||||
}
|
||||
$columns []= $column;
|
||||
}
|
||||
$collection = new ExcelHelper\ColumnCollection($columns);
|
||||
$this->columns = $collection;
|
||||
}
|
||||
protected function getExcelStyle($style = 'text')
|
||||
{
|
||||
switch (strtolower($style)) {
|
||||
case 'date':
|
||||
return new ExcelHelper\CellStyle\Date();
|
||||
case 'mes':
|
||||
return new Excel\Style\Mes();
|
||||
case 'currency':
|
||||
case 'amount':
|
||||
return new ExcelHelper\CellStyle\Amount();
|
||||
case 'number':
|
||||
case 'integer':
|
||||
return new ExcelHelper\CellStyle\Integer();
|
||||
case 'percent':
|
||||
case 'percentage':
|
||||
return new ExcelHelper\CellStyle\Percentage();
|
||||
case 'text':
|
||||
case 'string':
|
||||
default:
|
||||
return new ExcelHelper\CellStyle\Text();
|
||||
}
|
||||
}
|
||||
public function addData($data)
|
||||
{
|
||||
if ($this->data == null) {
|
||||
$this->data = [];
|
||||
}
|
||||
$this->data = array_merge($data);
|
||||
}
|
||||
public function addRow($rowData)
|
||||
{
|
||||
if ($this->data == null) {
|
||||
$this->data = [];
|
||||
}
|
||||
$this->data []= $rowData;
|
||||
}
|
||||
public function addTotals($totals)
|
||||
{
|
||||
$columns = (array) $this->columns;
|
||||
$columns = array_pop($columns);
|
||||
$ts = [];
|
||||
foreach ($columns as $column) {
|
||||
$col = $column->getHeading();
|
||||
if (isset($totals[$col])) {
|
||||
$ts[$col] = $this->getTotal($col, $totals[$col]);
|
||||
continue;
|
||||
}
|
||||
$ts[$col] = '';
|
||||
}
|
||||
$this->data []= $ts;
|
||||
}
|
||||
protected function getTotal($col, $aggr)
|
||||
{
|
||||
$col_num = $this->getColNumber($col);
|
||||
$col = $this->getColName($col_num);
|
||||
switch(strtolower($aggr)) {
|
||||
case 'sum':
|
||||
$num = 109;
|
||||
break;
|
||||
case 'count':
|
||||
$num = 102;
|
||||
break;
|
||||
case 'counta':
|
||||
$num = 103;
|
||||
break;
|
||||
default:
|
||||
$num = 0;
|
||||
}
|
||||
if ($num > 0) {
|
||||
$end = count($this->data) + 2;
|
||||
$str = "=SUBTOTAL({$num};{$col}3:{$col}{$end})";
|
||||
return $str;
|
||||
}
|
||||
return $aggr;
|
||||
}
|
||||
protected function getColNumber($col)
|
||||
{
|
||||
$columns = (array) $this->columns;
|
||||
$columns = array_keys(array_pop($columns));
|
||||
return array_search($col, $columns);
|
||||
}
|
||||
protected function getColName($col_num)
|
||||
{
|
||||
$cols = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
$N = strlen($cols);
|
||||
$name = '';
|
||||
if ($col_num > $N) {
|
||||
$name .= $cols[floor($col_num / $N)];
|
||||
$col_num = $N * ($col_num / $N - floor($col_num / $N));
|
||||
}
|
||||
$name .= $cols[$col_num];
|
||||
return $name;
|
||||
}
|
||||
public function informe()
|
||||
{
|
||||
header("Content-Type: application/octet-stream; charset=utf-8");
|
||||
header('Content-Transfer-Encoding: binary');
|
||||
header('Content-Disposition: attachment; filename="' . $this->filename . '"');
|
||||
header('Cache-Control: max-age=0');
|
||||
|
||||
$pE = new ExcelHelper\TableWorkbook('php://output');
|
||||
$ws = $pE->addWorksheet($this->name);
|
||||
|
||||
$table = new ExcelHelper\Table($ws, 0, 0, $this->name, new \ArrayIterator($this->data));
|
||||
$table->setColumnCollection($this->columns);
|
||||
|
||||
$pE->writeTable($table);
|
||||
|
||||
$pE->close();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user