Skip to content

Commit

Permalink
#19 [Hook] add: Priseo field on selling price
Browse files Browse the repository at this point in the history
  • Loading branch information
evarisk-charles committed Aug 26, 2024
1 parent 2c5573f commit eeaaa8a
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 5 deletions.
112 changes: 112 additions & 0 deletions class/actions_priseo.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
/* Copyright (C) 2024 EVARISK <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

/**
* \file class/actions_priseo.class.php
* \ingroup priseo
* \brief Priseo hook overload
*/

/**
* Class ActionsPriseo
*/
class ActionsPriseo
{
/**
* @var string Module name
*/
public $module = 'priseo';

/**
* @var DoliDB Database handler
*/
public $db;

/**
* @var string Error string
*/
public $error;

/**
* @var string[] Array of error strings
*/
public $errors = [];

/**
* @var array Hook results. Propagated to $hookmanager->resArray for later reuse
*/
public $results = [];

/**
* @var string|null String displayed by executeHook() immediately after return.
*/
public ?string $resprints;


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

/**
* Overloading the printCommonFooter function : replacing the parent's function with the one below
*
* @param array $parameters Hook metadatas (context, etc...)
* @return int 0 < on error, 0 on success, 1 to replace standard code
* @throws Exception
*/
public function printCommonFooter(array $parameters): int
{
global $object;

if (strpos($parameters['context'], 'productpricecard') !== false) {
require_once __DIR__ . './competitorprice.class.php';

$competitorPrice = new CompetitorPrice($this->db);

$competitorPrices = $competitorPrice->fetchAll('DESC', 'amount_ht', 0, 0, ['customsql' => 't.status >= ' . $competitorPrice::STATUS_DRAFT . ' AND t.fk_product = ' . $object->id]);
$lastCompetitorPrices = $competitorPrice->fetch('', '', ' ORDER BY t.rowid DESC');
if (is_array($competitorPrices) && !empty($competitorPrices)) {
$lastCompetitorPrice = array_shift($lastCompetitorPrices);
$maxPrices = array_shift($competitorPrices);
$minPrices = end($competitorPrices);
$pictopath = dol_buildpath('custom/priseo/img/priseo_color.png', 1);
$pictoPriseo = img_picto('', $pictopath, '', 1, 0, 0, '', 'pictoPriseo');

$out = '<tr><td>';
$out .= ucfirst($this->module) . ' (' . dol_print_date($lastCompetitorPrice->date_creation, 'day') . ')</td><td>' . $pictoPriseo . ' ';
$out .= $object->price < $minPrices->amount_ht ? price($object->price, 0, '', 1, -1, -1, 'auto') . ' HT': price($minPrices->amount_ht, 0, '', 1, -1, -1, 'auto') . ' HT';
$out .= ' <= ' . price($object->price, 0, '', 1, -1, -1, 'auto') . ' HT <= ';
$out .= $maxPrices->amount_ht < $object->price ? price($object->price, 0, '', 1, -1, -1, 'auto') . ' HT': price($maxPrices->amount_ht, 0, '', 1, -1, -1, 'auto') . ' HT';
$out .= '</td></tr>';
?>
<script>
$('.field_min_price').after(<?php echo json_encode($out); ?>);
</script>
<?php
}

}

return 0; // or return 1 to replace standard code
}
}
11 changes: 6 additions & 5 deletions class/competitorprice.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,14 @@ public function create(User $user, bool $notrigger = false): int
/**
* Load object in memory from the database
*
* @param int $id ID object
* @param string|null $ref Ref
* @return int 0 < if KO, 0 if not found, >0 if OK
* @param int|string $id ID object
* @param string|null $ref Ref
* @param string $morewhere More SQL filters (' AND ...')
* @return int 0 < if KO, 0 if not found, > 0 if OK
*/
public function fetch($id, string $ref = null): int
public function fetch($id, string $ref = null, string $morewhere = ''): int
{
return $this->fetchCommon($id, $ref);
return $this->fetchCommon($id, $ref, $morewhere);
}


Expand Down
1 change: 1 addition & 0 deletions core/modules/modPriseo.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public function __construct($db)
'js' => [],
// Set here all hooks context managed by module. To find available hook context, make a "grep -r '>initHooks(' *" on source code. You can also set hook context to 'all'
'hooks' => [
'productpricecard'
// 'data' => array(
// 'hookcontext1',
// 'hookcontext2',
Expand Down

0 comments on commit eeaaa8a

Please sign in to comment.