Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ : Price per balloon #77

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 82 additions & 2 deletions admin/vol.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

require_once '../../core/lib/admin.lib.php';
dol_include_once("/flightlog/class/bbctypes.class.php");
dol_include_once("/flightlog/flightlog.inc.php");

global $langs, $user, $db, $conf;

Expand All @@ -17,9 +18,15 @@
accessforbidden();
}

$balloons = new Bbc_ballons($db);
$balloons->fetchAll();

$balloonTypeService = new BbcBalloonTypeService($db);

$flightType = new Bbctypes($db);
$action = GETPOST('action', 'alpha', 2);
$services = GETPOST('idprod', 'array', 2);
$balloonTypes = GETPOST('balloon_types', 'array', 2);

/*
* Actions
Expand All @@ -36,6 +43,30 @@
dolibarr_set_const($db, 'BBC_POINTS_BONUS_'.$flightTypeId, GETPOST('points_bonus_'.$flightTypeId), 'chaine', 0, 'Points pour le vol T'.$flightTypeId, $conf->entity);
}

foreach ($balloonTypes as $balloonId => $balloonTypesPost){
foreach($balloonTypesPost as $balloonTypeId => $serviceId){
$currElt = $balloonTypeService->fetchByBalloonIdAndTypeId($balloonId, $balloonTypeId);
if(-1 == $serviceId){
$currElt->delete($user);
continue;
}

$currElt->idBalloon = $balloonId;
$currElt->idType = $balloonTypeId;

if(-1 === $currElt->fkService){
$currElt->fkService = $serviceId;
$currElt->create($user);
continue;
}

$currElt->fkService = $serviceId;
$currElt->update($user);
}
}



dolibarr_set_const($db, 'BBC_FLIGHT_TYPE_CUSTOMER', GETPOST('customer_product'), 'chaine', 0, '', $conf->entity);
dolibarr_set_const($db, 'BBC_FLIGHT_DEFAULT_CUSTOMER', GETPOST('defaultCustomer'), 'chaine', 0, '', $conf->entity);

Expand Down Expand Up @@ -105,7 +136,56 @@
</table>


<table class="noborder mt-2" width="100%">
<!-- Flight Type / Balloon / Service -->
<table class="noborder" width="100%">
<tr class="liste_titre">
<th><?= $langs->trans("Ballons") ?></th>
<th><?= $langs->trans("Types de vol.") ?></th>
<th><?= $langs->trans("Service / Produit") ?></th>
</tr>

<?php
/** @var Bbc_ballons $currentBalloon */
foreach ($balloons->lines as $currentBalloon): ?>
<?php $newBalloon = true; ?>
<?php
/** @var BbctypesLine $currentFlightType */
foreach ($flightType->lines as $currentFlightType): ?>
<tr>
<?php if($newBalloon): ?>
<?php if($currentBalloon->is_disable): ?>
<td rowspan="<?php echo count($flightType->lines); ?>>"><s><?php echo $currentBalloon->immat; ?></s></td>
<?php else: ?>
<td rowspan="<?php echo count($flightType->lines); ?>>"><?php echo $currentBalloon->immat; ?></td>
<?php endif; ?>
<?php endif; ?>
<td>(T<?= $currentFlightType->numero ?>) - <?= $currentFlightType->nom ?></td>
<td>
<?php $form->select_produits(
$balloonTypeService->fetchByBalloonIdAndTypeId($currentBalloon->id, $currentFlightType->id)->fkService,
'balloon_types['.$currentBalloon->id.']['.$currentFlightType->idType.']',
'',
$conf->product->limit_size,
$buyer->price_level,
1,
2,
'',
1,
[],
$buyer->id
); ?>
</td>
</tr>
<?php $newBalloon = false; ?>
<?php endforeach; ?>
<?php endforeach; ?>



</table>


<table class="noborder mt-2" width="100%">
<tr class="liste_titre">
<th><?= $langs->trans("Champ") ?></th>
<th><?= $langs->trans("Valeur") ?></th>
Expand Down Expand Up @@ -207,4 +287,4 @@
<?php
llxFooter();
$db->close();
?>
?>
226 changes: 226 additions & 0 deletions class/BbcBalloonTypeService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
<?php

class BbcBalloonTypeService extends CommonObject
{

/**
* @var string Id to identify managed objects
*/
public $element = 'bbcballoontypeproduct';
/**
* @var string Name of table without prefix where object is stored
*/
public $table_element = 'bbc_balloon_type_product';

/**
* @var int
*/
public $idBalloon;

/**
* @var int
*/
public $idType;

/**
* @var int
*/
public $fkService = -1 ;

/**
* Constructor
*
* @param DoliDb $db Database handler
*/
public function __construct(DoliDB $db)
{
$this->db = $db;
}

public function getIdBalloon(): int
{
return $this->idBalloon;
}

public function setIdBalloon(int $idBalloon): void
{
$this->idBalloon = $idBalloon;
}

public function getIdType(): int
{
return $this->idType;
}

public function setIdType(int $idType): void
{
$this->idType = $idType;
}

public function getFkService(): int
{
return $this->fkService;
}

public function setFkService(int $fkService): void
{
$this->fkService = $fkService;
}

public function fetchByBalloonIdAndTypeId(int $idBalloon, int $idType): self
{
$sql = "SELECT * FROM " . MAIN_DB_PREFIX . $this->table_element . " WHERE fk_bbc_balloon = " . $idBalloon . " AND fk_bbc_flight_type = " . $idType;
$resql = $this->db->query($sql);
$obj = new BbcBalloonTypeService($this->db);

while ($res = $this->db->fetch_object($resql)) {
$obj->fkService = $res->fk_product;
$obj->idBalloon = $res->fk_bbc_balloon;
$obj->idType = $res->fk_bbc_flight_type;

return $obj;
}

return $obj;
}

/**
* Update object into database
*
* @param User $user User that modifies
* @param bool $notrigger false=launch triggers after, true=disable triggers
*
* @return int <0 if KO, >0 if OK
*/
public function update(User $user, $notrigger = false)
{
$error = 0;
dol_syslog(__METHOD__, LOG_DEBUG);

// Update request
$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element . ' SET';

$sql .= ' fk_bbc_flight_type = ' . $this->idType . ',';
$sql .= ' fk_bbc_balloon = ' . $this->idBalloon . ',';
$sql .= ' fk_product = ' . $this->fkService ;

$sql .= ' WHERE fk_bbc_flight_type=' . $this->idType . ' AND fk_bbc_balloon=' . $this->idBalloon;

$this->db->begin();

$resql = $this->db->query($sql);
if (!$resql) {
$error++;
$this->errors[] = 'Error ' . $this->db->lasterror();
dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
}

if (!$error && !$notrigger) {
$this->call_trigger('BBC_FLIGHT_TYPE_MODIFY', $user);
}

// Commit or rollback
if ($error) {
$this->db->rollback();

return -1 * $error;
} else {
$this->db->commit();

return 1;
}
}

public function delete(User $user, $notrigger = false){
$error = 0;

$sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
$sql .= ' WHERE fk_bbc_flight_type=' . $this->idType . ' AND fk_bbc_balloon=' . $this->idBalloon;

$this->db->begin();

dol_syslog(get_class($this)."::delete sql=".$sql);
$resql = $this->db->query($sql);
if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }

// Commit or rollback
if ($error)
{
foreach ($this->errors as $errmsg)
{
dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
$this->error .= ($this->error ? ', '.$errmsg : $errmsg);
}
$this->db->rollback();
return -1 * $error;
} else
{
$this->db->commit();
return 1;
}
}



/**
* Create object into database
*
* @param User $user User that creates
* @param bool $notrigger false=launch triggers after, true=disable triggers
*
* @return int <0 if KO, Id of created object if OK
*/
public function create(User $user, $notrigger = false)
{
dol_syslog(__METHOD__, LOG_DEBUG);

$error = 0;

$sql = 'INSERT INTO ' . MAIN_DB_PREFIX . $this->table_element . '(';

$sql .= 'fk_bbc_flight_type,';
$sql .= 'fk_bbc_balloon,';
$sql .= 'fk_product';


$sql .= ') VALUES (';

$sql .= ' ' . $this->idType . ',';
$sql .= ' ' . $this->idBalloon. ',';
$sql .= ' ' . $this->fkService . ' ';
$sql .= ')';

$this->db->begin();

$resql = $this->db->query($sql);
if (!$resql) {
$error++;
$this->errors[] = 'Error ' . $this->db->lasterror();
dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
}

if (!$notrigger) {
// $this->call_trigger('BBC_FLIGHT_TYPE_CREATE', $user);
}

if (!$error) {
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
}

// Commit or rollback
if ($error) {
$this->db->rollback();

return -1 * $error;
} else {
$this->db->commit();

return $this->id;
}
}

public function hasService():bool
{
return $this->fkService > 0;
}
}
20 changes: 13 additions & 7 deletions class/flight/FlightPoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,17 @@ private function __construct($amount)
*
* @return FlightPoints
*/
public static function create($initialAmount)
public static function create(int $initialAmount)
{
return new FlightPoints($initialAmount);
}

/**
public static function zero():self
{
return new FlightPoints(0);
}

/**
* @param int $factor
*
* @return FlightPoints
Expand All @@ -44,12 +49,13 @@ public function multiply($factor)
return new FlightPoints($this->amount * $factor);
}

/**
* @return int
*/
public function getValue()
public function add(FlightPoints $points):self{
return new FlightPoints($this->amount + $points->getValue());
}

public function getValue():int
{
return $this->amount;
}

}
}
Loading
Loading