Skip to content

Commit

Permalink
Evarisk#268 [Mod] fix: DOLICAR_EXTRAFIELDS_BACKWARD_COMPATIBILITY and…
Browse files Browse the repository at this point in the history
… hook
  • Loading branch information
nicolas-eoxia committed Sep 24, 2024
1 parent f7c50dd commit 42c418f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 15 deletions.
41 changes: 34 additions & 7 deletions class/actions_dolicar.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ public function addHtmlHeader(array $parameters): int
*/
public function doActions(array $parameters, $object, string $action): int
{
global $user;
global $extrafields, $user;

if (preg_match('/invoicecard|propalcard|ordercard/', $parameters['context'])) {
if (preg_match('/propalcard|ordercard|invoicecard/', $parameters['context'])) {
$registrationCertificateFr = new RegistrationCertificateFr($this->db);

if ($action == 'add') {
Expand All @@ -107,11 +107,16 @@ public function doActions(array $parameters, $object, string $action): int

$product = new Product($this->db);

$extraFieldsNames = ['vehicle_model', 'registration_number', 'linked_product', 'linked_lot', 'first_registration_date', 'VIN_number'];
foreach ($extraFieldsNames as $extraFieldsName) {
$extrafields->attributes[$object->element]['list'][$extraFieldsName] = 1;
}

$registrationCertificateFr->fetch(GETPOST('options_registrationcertificatefr'));
$product->fetch($registrationCertificateFr->fk_product);

$_POST['options_registration_number'] = $registrationCertificateFr->a_registration_number;
$_POST['options_vehicle_model'] = $product->label;
$_POST['options_registration_number'] = $registrationCertificateFr->a_registration_number;
$_POST['options_linked_product'] = $registrationCertificateFr->fk_product;
$_POST['options_linked_lot'] = $registrationCertificateFr->fk_lot;
$_POST['options_first_registration_date'] = $registrationCertificateFr->b_first_registration_date;
Expand All @@ -122,8 +127,8 @@ public function doActions(array $parameters, $object, string $action): int
if ($action == 'update_extras') {
if (GETPOST('attribute') == 'registrationcertificatefr' && !empty(GETPOST('options_registrationcertificatefr'))) {
$registrationCertificateFr->fetch(GETPOST('options_registrationcertificatefr'));
$object->array_options['options_registration_number'] = $registrationCertificateFr->a_registration_number;
$object->array_options['options_vehicle_model'] = $registrationCertificateFr->d3_vehicle_model;
$object->array_options['options_registration_number'] = $registrationCertificateFr->a_registration_number;
$object->array_options['options_linked_product'] = $registrationCertificateFr->fk_product;
$object->array_options['options_linked_lot'] = $registrationCertificateFr->fk_lot;
$object->array_options['options_first_registration_date'] = $registrationCertificateFr->b_first_registration_date;
Expand All @@ -148,7 +153,29 @@ public function formObjectOptions(array $parameters, $object): int
{
global $extrafields, $langs;

if (preg_match('/invoicecard|propalcard|ordercard/', $parameters['context'])) {
if (preg_match('/propalcard|ordercard|invoicecard/', $parameters['context'])) {
$picto = img_picto('', 'dolicar_color@dolicar', 'class="pictofixedwidth paddingright"');
$extraFieldsNames = ['registrationcertificatefr', 'vehicle_model', 'mileage', 'registration_number', 'linked_product', 'linked_lot', 'first_registration_date', 'VIN_number'];
foreach ($extraFieldsNames as $extraFieldsName) {
$extrafields->attributes[$object->element]['label'][$extraFieldsName] = $picto . $langs->transnoentities($extrafields->attributes[$object->element]['label'][$extraFieldsName]);
}
}

return 0; // or return 1 to replace standard code
}

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

if (preg_match('/propallist|orderlist|invoicelist/', $parameters['context'])) {
$picto = img_picto('', 'dolicar_color@dolicar', 'class="pictofixedwidth paddingright"');
$extraFieldsNames = ['registrationcertificatefr', 'vehicle_model', 'mileage', 'registration_number', 'linked_product', 'linked_lot', 'first_registration_date', 'VIN_number'];
foreach ($extraFieldsNames as $extraFieldsName) {
Expand Down Expand Up @@ -190,15 +217,15 @@ public function beforePDFCreation(array $parameters, $object): int
{
global $langs;

if (preg_match('/invoicecard|propalcard|ordercard/', $parameters['context'])) {
if (preg_match('/propalcard|ordercard|invoicecard/', $parameters['context'])) {
if ($object->array_options['options_registrationcertificatefr'] > 0) {
$registrationCertificateFr = new RegistrationCertificateFr($this->db);

$registrationCertificateFr->fetch($object->array_options['options_registrationcertificatefr']);

$object->note_public = dol_strlen($object->array_options['options_registration_number']) > 0 ? $langs->transnoentities('RegistrationNumber') . ' : ' . $object->array_options['options_registration_number'] . '<br>' : '';
$object->note_public .= dol_strlen($object->array_options['options_vehicle_model']) > 0 ? $langs->transnoentities('VehicleModel') . ' : ' . $object->array_options['options_vehicle_model'] . '<br>' : '';
$object->note_public .= $object->array_options['options_VIN_number'] > 0 ? $langs->transnoentities('VINNumber') . ' : ' . $object->array_options['options_VIN_number'] . '<br>' : '';
$object->note_public .= dol_strlen($object->array_options['options_VIN_number']) > 0 ? $langs->transnoentities('VINNumber') . ' : ' . $object->array_options['options_VIN_number'] . '<br>' : '';
$object->note_public .= $object->array_options['options_first_registration_date'] > 0 ? $langs->transnoentities('FirstRegistrationDate') . ' : ' . dol_print_date($object->array_options['options_first_registration_date'], 'day') . '<br>' : '';
$object->note_public .= $object->array_options['options_mileage'] > 0 ? $langs->transnoentities('Mileage') . ' : ' . price($object->array_options['options_mileage'], 0,'',1, 0) . ' ' . $langs->trans('km') . '<br>' : '';
}
Expand Down
39 changes: 31 additions & 8 deletions core/modules/modDoliCar.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ public function __construct($db)
'registrationcertificatefrcard',
'dolicar_quickcreation',
'get_sheet_linkable_objects',
'propallist',
'orderlist',
'invoicelist'
]
],
// Set this to 1 if features of module are opened to external users
Expand Down Expand Up @@ -464,25 +467,45 @@ public function init($options = ''): int

$extraFieldsArrays = [
'registrationcertificatefr' => ['Label' => 'RegistrationCertificateFr', 'type' => 'link', 'length' => '', 'elementtype' => ['propal', 'commande', 'facture'], 'position' => 10, 'params' => 'a:1:{s:7:"options";a:1:{s:75:"RegistrationCertificateFr:dolicar/class/registrationcertificatefr.class.php";N;}}', 'alwayseditable' => 1, 'list' => 1, 'help' => '', 'entity' => 0, 'langfile' => 'dolicar@dolicar', 'enabled' => "isModEnabled('dolicar')"],
'vehicle_model' => ['Label' => 'VehicleModel', 'type' => 'varchar', 'length' => 255, 'elementtype' => ['propal', 'commande', 'facture'], 'position' => 20, 'params' => '', 'alwayseditable' => 1, 'list' => 1, 'help' => '', 'entity' => 0, 'langfile' => 'dolicar@dolicar', 'enabled' => "isModEnabled('dolicar')"],
'mileage' => ['Label' => 'Mileage', 'type' => 'int', 'length' => '', 'elementtype' => ['propal', 'commande', 'facture'], 'position' => 30, 'params' => '', 'alwayseditable' => 1, 'list' => 1, 'help' => '', 'entity' => 0, 'langfile' => 'dolicar@dolicar', 'enabled' => "isModEnabled('dolicar')"],
'registration_number' => ['Label' => 'RegistrationNumber', 'type' => 'varchar', 'length' => 255, 'elementtype' => ['propal', 'commande', 'facture'], 'position' => 40, 'params' => '', 'alwayseditable' => 1, 'list' => 1, 'help' => '', 'entity' => 0, 'langfile' => 'dolicar@dolicar', 'enabled' => "isModEnabled('dolicar')"],
'linked_product' => ['Label' => 'LinkedProduct', 'type' => 'link', 'length' => '', 'elementtype' => ['propal', 'commande', 'facture'], 'position' => 50, 'params' => 'a:1:{s:7:"options";a:1:{s:93:"Product:product/class/product.class.php:0:(t.entity:=:__ENTITY__) AND (t.fk_product_type:=:0)";N;}}', 'alwayseditable' => 1, 'list' => 1, 'help' => '', 'entity' => 0, 'langfile' => 'dolicar@dolicar', 'enabled' => "isModEnabled('dolicar')"],
'linked_lot' => ['Label' => 'LinkedProductBatch', 'type' => 'link', 'length' => '', 'elementtype' => ['propal', 'commande', 'facture'], 'position' => 60, 'params' => 'a:1:{s:7:"options";a:1:{s:75:"ProductLot:product/stock/class/productlot.class.php:(t.entity:=:__ENTITY__)";N;}}', 'alwayseditable' => 1, 'list' => 1, 'help' => '', 'entity' => 0, 'langfile' => 'dolicar@dolicar', 'enabled' => "isModEnabled('dolicar')"],
'first_registration_date' => ['Label' => 'FirstRegistrationDate', 'type' => 'date', 'length' => '', 'elementtype' => ['propal', 'commande', 'facture'], 'position' => 70, 'params' => '', 'alwayseditable' => 1, 'list' => 1, 'help' => '', 'entity' => 0, 'langfile' => 'dolicar@dolicar', 'enabled' => "isModEnabled('dolicar')"],
'VIN_number' => ['Label' => 'VINNumber', 'type' => 'varchar', 'length' => 128, 'elementtype' => ['propal', 'commande', 'facture'], 'position' => 80, 'params' => '', 'alwayseditable' => 1, 'list' => 1, 'help' => '', 'entity' => 0, 'langfile' => 'dolicar@dolicar', 'enabled' => "isModEnabled('dolicar')"],
'vehicle_model' => ['Label' => 'VehicleModel', 'type' => 'varchar', 'length' => 255, 'elementtype' => ['propal', 'commande', 'facture'], 'position' => 20, 'params' => '', 'list' => 5, 'help' => '', 'entity' => 0, 'langfile' => 'dolicar@dolicar', 'enabled' => "isModEnabled('dolicar')"],
'mileage' => ['Label' => 'Mileage', 'type' => 'int', 'length' => '', 'elementtype' => ['propal', 'commande', 'facture'], 'position' => 30, 'params' => '', 'alwayseditable' => 1, 'list' => 4, 'help' => '', 'entity' => 0, 'langfile' => 'dolicar@dolicar', 'enabled' => "isModEnabled('dolicar')"],
'registration_number' => ['Label' => 'RegistrationNumber', 'type' => 'varchar', 'length' => 255, 'elementtype' => ['propal', 'commande', 'facture'], 'position' => 40, 'params' => '', 'list' => 5, 'help' => '', 'entity' => 0, 'langfile' => 'dolicar@dolicar', 'enabled' => "isModEnabled('dolicar')"],
'linked_product' => ['Label' => 'LinkedProduct', 'type' => 'link', 'length' => '', 'elementtype' => ['propal', 'commande', 'facture'], 'position' => 50, 'params' => 'a:1:{s:7:"options";a:1:{s:93:"Product:product/class/product.class.php:0:(t.entity:=:__ENTITY__) AND (t.fk_product_type:=:0)";N;}}', 'list' => 5, 'help' => '', 'entity' => 0, 'langfile' => 'dolicar@dolicar', 'enabled' => "isModEnabled('dolicar')"],
'linked_lot' => ['Label' => 'LinkedProductBatch', 'type' => 'link', 'length' => '', 'elementtype' => ['propal', 'commande', 'facture'], 'position' => 60, 'params' => 'a:1:{s:7:"options";a:1:{s:75:"ProductLot:product/stock/class/productlot.class.php:(t.entity = __ENTITY__)";N;}}', 'list' => 5, 'help' => '', 'entity' => 0, 'langfile' => 'dolicar@dolicar', 'enabled' => "isModEnabled('dolicar')"],
'first_registration_date' => ['Label' => 'FirstRegistrationDate', 'type' => 'date', 'length' => '', 'elementtype' => ['propal', 'commande', 'facture'], 'position' => 70, 'params' => '', 'list' => 5, 'help' => '', 'entity' => 0, 'langfile' => 'dolicar@dolicar', 'enabled' => "isModEnabled('dolicar')"],
'VIN_number' => ['Label' => 'VINNumber', 'type' => 'varchar', 'length' => 128, 'elementtype' => ['propal', 'commande', 'facture'], 'position' => 80, 'params' => '', 'list' => 5, 'help' => '', 'entity' => 0, 'langfile' => 'dolicar@dolicar', 'enabled' => "isModEnabled('dolicar')"],
'starting_mileage' => ['Label' => 'StartingMileage', 'type' => 'int', 'length' => '', 'elementtype' => ['actioncomm'], 'position' => 10, 'params' => '', 'alwayseditable' => 1, 'list' => 1, 'help' => '', 'entity' => 0, 'langfile' => 'dolicar@dolicar', 'enabled' => "isModEnabled('dolicar')"],
'arrival_mileage' => ['Label' => 'ArrivalMileage', 'type' => 'int', 'length' => '', 'elementtype' => ['actioncomm'], 'position' => 20, 'params' => '', 'alwayseditable' => 1, 'list' => 1, 'help' => '', 'entity' => 0, 'langfile' => 'dolicar@dolicar', 'enabled' => "isModEnabled('dolicar')"],
'json' => ['Label' => 'JSON', 'type' => 'text', 'length' => '', 'elementtype' => ['actioncomm'], 'position' => 30, 'params' => '', 'alwayseditable' => 1, 'list' => 0, 'help' => '', 'entity' => 0, 'langfile' => 'dolicar@dolicar', 'enabled' => "isModEnabled('dolicar')"]
];

foreach ($extraFieldsArrays as $key => $extraField) {
foreach ($extraField['elementtype'] as $extraFieldElementType) {
$extraFields->update($key, $extraField['Label'], $extraField['type'], $extraField['length'], $extraFieldElementType, 0, 0, $this->numero . $extraField['position'], $extraField['params'], '', '', $extraField['list'], ($extraField['help'][$extraFieldElementType] ?? $extraField['help']), '', '', $extraField['entity'], $extraField['langfile'], $extraField['enabled'] . ' && isModEnabled("' . $extraFieldElementType . '")', 0, 0, $extraField['css']);
$extraFields->update($key, $extraField['Label'], $extraField['type'], $extraField['length'], $extraFieldElementType, 0, 0, $this->numero . $extraField['position'], $extraField['params'], $extraField['alwayseditable'], '', $extraField['list'], ($extraField['help'][$extraFieldElementType] ?? $extraField['help']), '', '', $extraField['entity'], $extraField['langfile'], $extraField['enabled'] . ' && isModEnabled("' . $extraFieldElementType . '")', 0, 0, $extraField['css']);
$extraFields->addExtraField($key, $extraField['Label'], $extraField['type'], $this->numero . $extraField['position'], $extraField['length'], $extraFieldElementType, 0, 0, '', $extraField['params'], $extraField['alwayseditable'], '', $extraField['list'], $extraField['help'], '', $extraField['entity'], $extraField['langfile'], $extraField['enabled'] . ' && isModEnabled("' . $extraFieldElementType . '")', 0, 0, $extraField['css']);
}
}

if (getDolGlobalInt('DOLICAR_EXTRAFIELDS_BACKWARD_COMPATIBILITY') == 0) {
$extraFieldsArrays = [
'registrationcertificatefr' => ['elementtype' => ['propaldet', 'commandedet', 'facturedet']],
'vehicle_model' => ['elementtype' => ['propaldet', 'commandedet', 'facturedet']],
'mileage' => ['elementtype' => ['propaldet', 'commandedet', 'facturedet']],
'registration_number' => ['elementtype' => ['propaldet', 'commandedet', 'facturedet']],
'linked_product' => ['elementtype' => ['propaldet', 'commandedet', 'facturedet']],
'linked_lot' => ['elementtype' => ['propaldet', 'commandedet', 'facturedet']],
'first_registration_date' => ['elementtype' => ['propaldet', 'commandedet', 'facturedet']],
'VIN_number' => ['elementtype' => ['propaldet', 'commandedet', 'facturedet']]
];

foreach ($extraFieldsArrays as $key => $extraField) {
foreach ($extraField['elementtype'] as $extraFieldElementType) {
$extraFields->delete($key, $extraFieldElementType);
}
}
dolibarr_set_const($this->db, 'DOLICAR_EXTRAFIELDS_BACKWARD_COMPATIBILITY', 1, 'integer', 0, '', $conf->entity);
}

// Warehouse
if (getDolGlobalInt('DOLICAR_DEFAULT_WAREHOUSE_ID') <= 0) {
require_once DOL_DOCUMENT_ROOT . '/product/stock/class/entrepot.class.php';
Expand Down

0 comments on commit 42c418f

Please sign in to comment.