2024-02-07 23:48:31 -03:00
< ? php
2024-03-26 09:38:20 -03:00
namespace Incoviba\Repository\Contabilidad ;
2024-02-07 23:48:31 -03:00
use DateTimeInterface ;
use Incoviba\Common\Define ;
use Incoviba\Common\Ideal ;
use Incoviba\Common\Implement ;
use Incoviba\Model ;
2024-03-26 09:38:20 -03:00
use Incoviba\Repository\Inmobiliaria ;
2024-02-07 23:48:31 -03:00
class Movimiento extends Ideal\Repository
{
public function __construct ( Define\Connection $connection , protected Inmobiliaria\Cuenta $cuentaRepository )
{
parent :: __construct ( $connection );
$this -> setTable ( 'movimientos' );
}
2024-03-26 09:38:20 -03:00
public function create ( ? array $data = null ) : Model\Contabilidad\Movimiento
2024-02-07 23:48:31 -03:00
{
$map = ( new Implement\Repository\MapperParser ([ 'cargo' , 'abono' , 'saldo' , 'glosa' , 'documento' ]))
-> register ( 'fecha' , new Implement\Repository\Mapper\DateTime ( 'fecha' ))
-> register ( 'cuenta_id' , ( new Implement\Repository\Mapper ())
-> setProperty ( 'cuenta' )
-> setFunction ( function ( $data ) {
return $this -> cuentaRepository -> fetchById ( $data [ 'cuenta_id' ]);
})
);
2024-03-26 09:38:20 -03:00
return $this -> parseData ( new Model\Contabilidad\Movimiento (), $data , $map );
2024-02-07 23:48:31 -03:00
}
2024-03-26 09:38:20 -03:00
public function save ( Define\Model $model ) : Model\Contabilidad\Movimiento
2024-02-07 23:48:31 -03:00
{
$model -> id = $this -> saveNew ([
'cuenta_id' ,
'fecha' ,
'glosa' ,
'documento' ,
'cargo' ,
'abono' ,
'saldo'
], [
$model -> cuenta -> id ,
$model -> fecha -> format ( 'Y-m-d' ),
$model -> glosa ,
$model -> documento ,
$model -> cargo ,
$model -> abono ,
$model -> saldo
]);
return $model ;
}
2024-03-26 09:38:20 -03:00
public function edit ( Define\Model $model , array $new_data ) : Model\Contabilidad\Movimiento
2024-02-07 23:48:31 -03:00
{
return $this -> update ( $model , [ 'cuenta_id' , 'fecha' , 'glosa' , 'documento' , 'cargo' , 'abono' , 'saldo' ], $new_data );
}
2024-08-27 14:47:21 -04:00
public function fetchByCuenta ( int $cuenta_id ) : array
{
$query = $this -> connection -> getQueryBuilder ()
-> select ()
-> from ( $this -> getTable ())
-> where ( 'cuenta_id = ?' );
return $this -> fetchMany ( $query , [ $cuenta_id ]);
}
2024-02-13 01:16:17 -03:00
public function fetchByCuentaAndFecha ( int $cuenta_id , DateTimeInterface $fecha ) : array
{
$query = $this -> connection -> getQueryBuilder ()
-> select ()
-> from ( $this -> getTable ())
-> where ( 'cuenta_id = ? AND fecha = ?' );
return $this -> fetchMany ( $query , [ $cuenta_id , $fecha -> format ( 'Y-m-d' )]);
}
2024-07-17 22:33:33 -04:00
public function fetchByCuentaAndFechaAndGlosaAndCargoAndAbonoAndSaldo ( int $cuenta_id , DateTimeInterface $fecha , string $glosa , int $cargo , int $abono , int $saldo ) : Model\Contabilidad\Movimiento
2024-02-07 23:48:31 -03:00
{
2024-10-10 18:48:44 -03:00
$len = ( int ) round ( strlen ( $glosa ) * . 75 );
2024-02-07 23:48:31 -03:00
$query = $this -> connection -> getQueryBuilder ()
-> select ()
-> from ( $this -> getTable ())
2024-10-10 18:48:44 -03:00
-> where ( " cuenta_id = ? AND fecha = ? AND SUBSTRING(LOWER(LTRIM(glosa)), 0, { $len } ) = SUBSTRING(LOWER(LTRIM(?)), 0, { $len } ) AND cargo = ? AND abono = ? AND saldo = ? " );
2024-07-17 22:33:33 -04:00
return $this -> fetchOne ( $query , [ $cuenta_id , $fecha -> format ( 'Y-m-d' ), $glosa , $cargo , $abono , $saldo ]);
2024-02-07 23:48:31 -03:00
}
2024-04-10 21:18:33 -04:00
public function fetchAmountStartingFrom ( int $start , int $amount ) : array
{
$query = $this -> connection -> getQueryBuilder ()
-> select ()
-> from ( $this -> getTable ())
-> limit ( $amount , $start );
return $this -> fetchMany ( $query );
}
2024-05-13 16:54:59 -04:00
public function fetchAmountBySociedadAndMes ( int $sociedad_rut , DateTimeInterface $mes , ? int $amount ) : array
{
$query = $this -> connection -> getQueryBuilder ()
-> select ( 'a.*' )
-> from ( " { $this -> getTable () } a " )
-> joined ( 'JOIN cuenta b ON a.cuenta_id = b.id' )
-> where ( 'b.inmobiliaria = ? AND a.fecha BETWEEN ? AND ?' );
if ( $amount !== null ) {
$query -> limit ( $amount );
}
return $this -> fetchMany ( $query , [ $sociedad_rut , $mes -> format ( 'Y-m-01' ), $mes -> format ( 'Y-m-t' )]);
}
2024-02-07 23:48:31 -03:00
}