2025-02-18 16:02:10 -03:00
< ? php
namespace Incoviba\Repository\Proyecto\Broker ;
use Incoviba\Common ;
use Incoviba\Repository ;
use Incoviba\Model ;
class Contract extends Common\Ideal\Repository
{
public function __construct ( Common\Define\Connection $connection , protected Repository\Proyecto\Broker $brokerRepository ,
protected Repository\Proyecto $proyectoRepository )
{
parent :: __construct ( $connection );
}
2025-02-24 12:39:42 -03:00
public function getTable () : string
{
return 'broker_contracts' ;
}
2025-02-18 16:02:10 -03:00
public function create ( ? array $data = null ) : Model\Proyecto\Broker\Contract
{
$map = ( new Common\Implement\Repository\MapperParser ([ 'commission' ]))
-> register ( 'broker_rut' , ( new Common\Implement\Repository\Mapper ())
-> setProperty ( 'broker' )
-> setFunction ( function ( $data ) {
return $this -> brokerRepository -> fetchById ( $data [ 'broker_rut' ]);
})
)
-> register ( 'proyecto_id' , ( new Common\Implement\Repository\Mapper ())
-> setProperty ( 'proyecto' )
-> setFunction ( function ( $data ) {
return $this -> proyectoRepository -> fetchById ( $data [ 'proyecto_id' ]);
})
);
return $this -> parseData ( new Model\Proyecto\Broker\Contract (), $data , $map );
}
public function save ( Common\Define\Model $model ) : Model\Proyecto\Broker\Contract
{
$model -> id = $this -> saveNew (
[ 'broker_rut' , 'proyecto_id' , 'commission' ],
[ $model -> broker -> rut , $model -> proyecto -> id , $model -> commission ]);
return $model ;
}
2025-03-03 14:55:57 -03:00
/**
* @ param Common\Define\Model $model
* @ param array $new_data
* @ return Model\Proyecto\Broker\Contract
* @ throws Common\Implement\Exception\EmptyResult
*/
2025-02-18 16:02:10 -03:00
public function edit ( Common\Define\Model $model , array $new_data ) : Model\Proyecto\Broker\Contract
{
return $this -> update ( $model , [ 'broker_rut' , 'proyecto_id' , 'commission' ], $new_data );
}
2025-03-03 14:55:57 -03:00
/**
* @ param int $brokerRut
* @ return array
* @ throws Common\Implement\Exception\EmptyResult
*/
2025-02-18 16:02:10 -03:00
public function fetchByBroker ( int $brokerRut ) : array
{
$query = $this -> connection -> getQueryBuilder ()
-> select ()
-> from ( $this -> getTable ())
-> where ( 'broker_rut = :broker_rut' );
return $this -> fetchMany ( $query , [ 'broker_rut' => $brokerRut ]);
}
2025-03-03 14:55:57 -03:00
/**
* @ param int $proyecto_id
* @ return array
* @ throws Common\Implement\Exception\EmptyResult
*/
2025-02-18 16:02:10 -03:00
public function fetchByProject ( int $proyecto_id ) : array
{
$query = $this -> connection -> getQueryBuilder ()
-> select ()
-> from ( $this -> getTable ())
-> where ( 'proyecto_id = :proyecto_id' );
return $this -> fetchMany ( $query , [ 'proyecto_id' => $proyecto_id ]);
}
2025-03-03 14:55:57 -03:00
/**
* @ param int $brokerRut
* @ return array
* @ throws Common\Implement\Exception\EmptyResult
*/
2025-02-18 16:02:10 -03:00
public function fetchActiveByBroker ( int $brokerRut ) : array
{
$query = $this -> connection -> getQueryBuilder ()
-> select ( 'a.*' )
-> from ( " { $this -> getTable () } a " )
-> joined ( $this -> statusJoin ())
-> where ( 'a.broker_rut = :broker_rut AND bcs.state = :state' );
return $this -> fetchMany ( $query , [ 'broker_rut' => $brokerRut , 'state' => Model\Proyecto\Broker\Contract\Type :: ACTIVE ]);
}
2025-03-03 14:55:57 -03:00
/**
* @ param int $proyecto_id
* @ return array
* @ throws Common\Implement\Exception\EmptyResult
*/
2025-02-18 16:02:10 -03:00
public function fetchActiveByProject ( int $proyecto_id ) : array
{
$query = $this -> connection -> getQueryBuilder ()
-> select ( 'a.*' )
-> from ( " { $this -> getTable () } a " )
-> joined ( $this -> statusJoin ())
-> where ( 'a.proyecto_id = :proyecto_id AND bcs.state = :state' );
return $this -> fetchMany ( $query , [ 'proyecto_id' => $proyecto_id , 'state' => Model\Proyecto\Broker\Contract\Type :: ACTIVE ]);
}
2025-03-03 14:55:57 -03:00
/**
* @ param int $proyecto_id
* @ param int $brokerRut
* @ return Model\Proyecto\Broker\Contract
* @ throws Common\Implement\Exception\EmptyResult
*/
2025-02-18 16:02:10 -03:00
public function fetchActiveByProjectAndBroker ( int $proyecto_id , int $brokerRut ) : Model\Proyecto\Broker\Contract
{
$query = $this -> connection -> getQueryBuilder ()
-> select ( 'a.*' )
-> from ( " { $this -> getTable () } a " )
-> joined ( $this -> statusJoin ())
-> where ( 'a.proyecto_id = :proyecto_id AND a.broker_rut = :broker_rut AND bcs.state = :state' );
return $this -> fetchOne ( $query , [ 'proyecto_id' => $proyecto_id , 'broker_rut' => $brokerRut , 'state' => Model\Proyecto\Broker\Contract\Type :: ACTIVE ]);
}
protected function statusJoin () : string
{
return 'INNER JOIN (SELECT bcs1.* FROM broker_contract_states bcs1 INNER JOIN (SELECT MAX(id) AS id, contract_id FROM broker_contract_states GROUP BY contract_id) bcs0 ON bcs0.id = bcs1.id) bcs ON bcs.contract = a.id' ;
}
2025-02-24 12:39:42 -03:00
}