Skip to content

Commit

Permalink
Eoxia#46 Eoxia#47 [CompetitorPrice] add: status management and fix in…
Browse files Browse the repository at this point in the history
…clude
  • Loading branch information
nicolas-eoxia committed Sep 11, 2024
1 parent 1a49682 commit a68fb94
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 31 deletions.
16 changes: 7 additions & 9 deletions class/actions_priseo.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,19 @@ public function printCommonFooter(array $parameters): int
global $object;

if (strpos($parameters['context'], 'productpricecard') !== false) {
require_once __DIR__ . './competitorprice.class.php';
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');
$competitorPrices = $competitorPrice->fetchAll('DESC', 'amount_ht', 0, 0, ['customsql' => 't.status = ' . $competitorPrice::STATUS_VALIDATED . ' AND t.fk_product = ' . $object->id]);
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');
$maxPrices = array_shift($competitorPrices);
$minPrices = end($competitorPrices);

$competitorPrice->fetch('', '', ' ORDER BY t.rowid DESC');

$out = '<tr><td>';
$out .= ucfirst($this->module) . ' (' . dol_print_date($lastCompetitorPrice->date_creation, 'day') . ')</td><td>' . $pictoPriseo . ' ';
$out .= img_picto('', $competitorPrice->picto . '_1.2em', 'class="pictoPriseo"') . ucfirst($this->module) . ' (' . dol_print_date($competitorPrice->date_creation, 'day') . ') - ' . price($competitorPrice->getAverage($object->id), 0, '', 1, -1, -1, 'auto') . ' HT</td><td>';
$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';
Expand Down
32 changes: 30 additions & 2 deletions class/competitorprice.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class CompetitorPrice extends SaturneObject
/**
* @var string String with name of icon for competitorprice. Must be the part after the 'object_' into object_competitorprice.png
*/
public $picto = 'competitorprice@priseo';
public $picto = 'fontawesome_fa-chart-line_fas_#63ACC9';

/**
* @var array Label status of const.
Expand Down Expand Up @@ -113,7 +113,7 @@ class CompetitorPrice extends SaturneObject
'entity' => ['type' => 'integer', 'label' => 'Entity', 'enabled' => '1', 'position' => 30, 'notnull' => 1, 'visible' => 0],
'date_creation' => ['type' => 'datetime', 'label' => 'DateCreation', 'enabled' => '1', 'position' => 40, 'notnull' => 1, 'visible' => 0],
'tms' => ['type' => 'timestamp', 'label' => 'DateModification', 'enabled' => '1', 'position' => 50, 'notnull' => 0, 'visible' => 0],
'status' => ['type' => 'integer', 'label' => 'Status', 'enabled' => '1', 'position' => 60, 'notnull' => 1, 'visible' => 0, 'default' => '1', 'index' => 1, 'arrayofkeyval' => ['0' => 'Draft', '1' => 'Validate']],
'status' => ['type' => 'integer', 'label' => 'Status', 'enabled' => '1', 'position' => 60, 'notnull' => 1, 'visible' => 0, 'default' => 1, 'index' => 1, 'arrayofkeyval' => ['0' => 'Draft', '1' => 'Validate']],
'label' => ['type' => 'varchar(255)', 'label' => 'Label', 'enabled' => '1', 'position' => 110, 'notnull' => 0, 'visible' => 1, 'searchall' => 1, 'css' => 'minwidth200'],
'amount_ht' => ['type' => 'price', 'label' => 'CompetitorPriceHT', 'enabled' => '1', 'position' => 80, 'notnull' => 0, 'visible' => 1, 'default' => 'null', 'isameasure' => '1'],
'amount_ttc' => ['type' => 'price', 'label' => 'CompetitorPriceTTC', 'enabled' => '1', 'position' => 90, 'notnull' => 0, 'visible' => 1, 'default' => 'null', 'isameasure' => '1'],
Expand Down Expand Up @@ -189,6 +189,7 @@ public function __construct(DoliDB $db)
*/
public function create(User $user, bool $notrigger = false): int
{
$this->status = 1;
return $this->createCommon($user, $notrigger);
}

Expand Down Expand Up @@ -556,6 +557,33 @@ public function info(int $id): void
}
}

/**
* Return average of amount_ht
*
* @param int $fkProductID ID of product
* @return float $average Average of amount_ht
*/
public function getAverage(int $fkProductID = 0): float
{
$sql = 'SELECT AVG(amount_ht) AS moyenne_amount_ht';
$sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t';
$sql .= ' WHERE status = 1 AND fk_product = ' . $fkProductID;

$average = 0;
$result = $this->db->query($sql);
if ($result) {
if ($this->db->num_rows($result)) {
$obj = $this->db->fetch_object($result);
$average = $obj->moyenne_amount_ht;
}
$this->db->free($result);
} else {
dol_print_error($this->db);
}

return $average;
}

/**
* Initialise object with example values
* ID must be 0 if object instance is a specimen
Expand Down
36 changes: 18 additions & 18 deletions sql/llx_priseo_competitorprice.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- Copyright (C) 2022 Florian HENRY <[email protected]>
-- Copyright (C) 2022-2023 EOXIA <dev@eoxia.com>
-- Copyright (C) 2022-2024 EVARISK <technique@evarisk.com>
--
-- 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
Expand All @@ -15,20 +15,20 @@
-- along with this program. If not, see https://www.gnu.org/licenses/.

CREATE TABLE llx_priseo_competitorprice(
rowid integer AUTO_INCREMENT PRIMARY KEY NOT NULL,
ref varchar(128) NOT NULL,
entity integer DEFAULT 1 NOT NULL,
date_creation datetime NOT NULL,
tms timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
status integer NOT NULL,
label varchar(255),
amount_ht double DEFAULT NULL,
amount_ttc double DEFAULT NULL,
vat double DEFAULT NULL,
url_competitor varchar(1024),
competitor_date datetime,
fk_product integer NOT NULL,
fk_soc integer NOT NULL,
fk_user_creat integer NOT NULL,
fk_user_modif integer
) ENGINE=innodb;
rowid integer AUTO_INCREMENT PRIMARY KEY NOT NULL,
ref varchar(128) NOT NULL,
entity integer DEFAULT 1 NOT NULL,
date_creation datetime NOT NULL,
tms timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
status integer DEFAULT 1 NOT NULL,
label varchar(255),
amount_ht double DEFAULT NULL,
amount_ttc double DEFAULT NULL,
vat double DEFAULT NULL,
url_competitor varchar(1024),
competitor_date datetime,
fk_product integer NOT NULL,
fk_soc integer NOT NULL,
fk_user_creat integer NOT NULL,
fk_user_modif integer
) ENGINE=innodb;
18 changes: 18 additions & 0 deletions sql/update.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- 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/.

-- 1.2.0
ALTER TABLE `llx_priseo_competitorprice` CHANGE `status` `status` INT NOT NULL DEFAULT '1';
UPDATE `llx_priseo_competitorprice` SET `status` = 1 WHERE `status` = 0;
11 changes: 9 additions & 2 deletions view/competitorprice_card.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,13 @@

// Action to add record
if ($action == 'add' && $permissiontoadd) {
$object->ref = $refCompetitorPriceMod->getNextValue($object);
$object->ref = $refCompetitorPriceMod->getNextValue($object);
$competitorPrices = $object->fetchAll('', '', 0, 0, ['customsql' => 't.fk_soc = ' . GETPOST('fk_soc') . ' AND t.fk_product = ' . $object->fk_product]);
if (is_array($competitorPrices) && !empty($competitorPrices)) {
foreach ($competitorPrices as $competitorPrice) {
$competitorPrice->setValueFrom('status', 0, '', '', 'int', '', $user);
}
}
}

$noback = 1;
Expand Down Expand Up @@ -424,7 +430,7 @@

if (!empty($comptetitorPrices)) {
foreach ($comptetitorPrices as $competitorPriceDetail) {
print '<tr class="oddeven">';
print '<tr class="oddeven ' . ($competitorPriceDetail->status == 0 ? 'opacitymedium' : '') . '">';
foreach ($competitorPriceDetail->fields as $key => $val) {
$cssforfield = (empty($val['csslist']) ? (empty($val['css']) ? '' : $val['css']) : $val['csslist']);
if (in_array($val['type'], ['date', 'datetime', 'timestamp'])) {
Expand All @@ -444,6 +450,7 @@
'status']) && empty($val['arrayofkeyval'])) {
$cssforfield .= ($cssforfield ? ' ' : '') . 'right';
}

//if (in_array($key, array('fk_soc', 'fk_user', 'fk_warehouse'))) $cssforfield = 'tdoverflowmax100';
//var_dump($val, $key);
if (!empty($arrayfields[$key]['checked'])) {
Expand Down

0 comments on commit a68fb94

Please sign in to comment.