Skip to content

Commit

Permalink
#120 [RegistrationCertificate] add: select productlot refresh with pr…
Browse files Browse the repository at this point in the history
…oduct post
  • Loading branch information
Théo David committed Mar 29, 2023
1 parent 3e53157 commit b283b9b
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 9 deletions.
2 changes: 1 addition & 1 deletion class/actions_dolicar.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ public function printCommonFooter($parameters)
let array_ids = <?php echo json_encode($product_ids); ?>;
jQuery('#fk_product').find('option').each(function() {
if ($(this).attr('value') != -1) {
Object.values(array_ids).includes($(this).attr('value')) ? console.log('oui') : $(this).remove()
Object.values(array_ids).includes($(this).attr('value')) ? '' : $(this).remove()
}
})
let newProductHref = jQuery('.field_fk_product .valuefieldcreate').find('.butActionNew').attr('href')
Expand Down
2 changes: 1 addition & 1 deletion js/dolicar.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 42 additions & 5 deletions js/modules/registrationcertificate.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ window.dolicar.registrationcertificate.init = function() {
*/
window.dolicar.registrationcertificate.event = function() {
$( document ).on( 'change', '#fk_product', window.dolicar.registrationcertificate.actualizeBrand );
$(document).ready(() => {
$( document ).on( 'change', '#fk_product', window.dolicar.registrationcertificate.actualizeProductlot );
$( document ).ready(() => {
let url = $('.lot-creation-url').val()
$(document).find('.field_fk_lot .butActionNew').attr('target', '_blank')
$(document).find('.field_fk_lot .butActionNew').attr('href', url)
})

}


/**
* Actualize
* Actualize brand input
*
* @since 0.0.2
* @version 0.0.2
Expand All @@ -51,9 +53,7 @@ window.dolicar.registrationcertificate.actualizeBrand = function( event ) {
var form = document.getElementById('registrationcertificatefr_create')? document.getElementById('registrationcertificatefr_create') : document.getElementById('registrationcertificatefr_edit')
var formData = new FormData(form);
let productId = formData.get('fk_product');
console.log(productId)
let querySeparator = '?'
document.URL.match(/\?/) ? querySeparator = '&' : 1
let querySeparator = window.saturne.toolbox.getQuerySeparator(document.URL)

$.ajax({
url: document.URL + querySeparator + 'subaction=getProductBrand&token='+token,
Expand All @@ -69,3 +69,40 @@ window.dolicar.registrationcertificate.actualizeBrand = function( event ) {
},
});
};

/**
* Actualize productlot selector
*
* @since 0.0.2
* @version 0.0.2
*
* @return {void}
*/
window.dolicar.registrationcertificate.actualizeProductlot = function( event ) {

let token = $('input[name="token"]').val();

var form = document.getElementById('registrationcertificatefr_create')? document.getElementById('registrationcertificatefr_create') : document.getElementById('registrationcertificatefr_edit')
var formData = new FormData(form);
let productId = formData.get('fk_product');
let action = formData.get('action');

if (action == 'update') {
action = 'edit';
}

let querySeparator = window.saturne.toolbox.getQuerySeparator(document.URL)

window.saturne.loader.display($('.lot-container'));

$.ajax({
url: document.URL + querySeparator + 'action=' + action + '&fk_product=' + productId + '&token='+token,
type: "POST",
processData: false,
contentType: false,
success: function ( resp ) {
$('.lot-container').html($(resp).find('.lot-content'))
$('.wpeo-loader').removeClass('wpeo-loader');
},
});
};
119 changes: 119 additions & 0 deletions lib/dolicar_functions.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,122 @@ function get_vehicle_brand($productId) {

return $brand_name;
}
/**
* Return HTML code of the SELECT of list of all product_lots (for a third party or all).
* This also set the number of product_lotssfound into $this->num
*
* @since 9.0 Add afterSelectContactOptions hook
*
* @param int $socid Id ot third party or 0 for all or -1 for empty list
* @param array|int $selected Array of ID of pre-selected product_lots id
* @param string $htmlname Name of HTML field ('none' for a not editable field)
* @param int $showempty 0=no empty value, 1=add an empty value, 2=add line 'Internal' (used by user edit), 3=add an empty value only if more than one record into list
* @param string $exclude List of product_lotss id to exclude
* @param string $limitto Disable answers that are not id in this array list
* @param integer $showfunction Add function into label
* @param string $moreclass Add more class to class style
* @param bool $options_only Return options only (for ajax treatment)
* @param integer $showsoc Add company into label
* @param int $forcecombo Force to use combo box (so no ajax beautify effect)
* @param array $events Event options. Example: array(array('method'=>'getContacts', 'url'=>dol_buildpath('/core/ajax/product_lotss.php',1), 'htmlname'=>'product_lotsid', 'params'=>array('add-customer-product_lots'=>'disabled')))
* @param string $moreparam Add more parameters onto the select tag. For example 'style="width: 95%"' to avoid select2 component to go over parent container
* @param string $htmlid Html id to use instead of htmlname
* @param bool $multiple add [] in the name of element and add 'multiple' attribut
* @param integer $disableifempty Set tag 'disabled' on select if there is no choice
* @return int <0 if KO, Nb of product_lots in list if OK
*/
function dolicar_select_product_lots($productid = -1, $selected = '', $htmlname = 'fk_lot', $showempty = 0, $exclude = '', $limitto = '', $showfunction = 0, $moreclass = '', $options_only = false, $showsoc = 0, $forcecombo = 0, $events = array(), $moreparam = '', $htmlid = '', $multiple = false, $disableifempty = 0, $exclude_already_add = '')
{
global $conf, $langs, $hookmanager, $action, $db;

saturne_load_langs(["companies"]);

if (empty($htmlid)) $htmlid = $htmlname;
$num = 0;

if ($selected === '') $selected = array();
elseif ( ! is_array($selected)) $selected = array($selected);
$out = '';

if ( ! is_object($hookmanager)) {
include_once DOL_DOCUMENT_ROOT . '/core/class/hookmanager.class.php';
$hookmanager = new HookManager($db);
}

// We search third parties
$sql = "SELECT pl.rowid, pl.fk_product, pl.batch";
$sql .= " FROM " . MAIN_DB_PREFIX . "product_lot as pl";
$sql .= " LEFT OUTER JOIN " . MAIN_DB_PREFIX . "product as p ON p.rowid=pl.fk_product";
$sql .= " WHERE pl.entity IN (" . getEntity('productlot') . ")";
if ($productid > 0 || $productid == -1) $sql .= " AND pl.fk_product=" . $productid;
$sql .= " ORDER BY pl.batch ASC";

//dol_syslog(get_class($this)."::select_product_lotss", LOG_DEBUG);
$resql = $db->query($sql);

if ($resql) {
$num = $db->num_rows($resql);

if ($conf->use_javascript_ajax && ! $forcecombo && ! $options_only) {
include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php';
$out .= ajax_combobox($htmlid, $events, $conf->global->CONTACT_USE_SEARCH_TO_SELECT);
}

if ($htmlname != 'none' && ! $options_only) {
$out .= '<select class="flat' . ($moreclass ? ' ' . $moreclass : '') . '" id="' . $htmlid . '" name="' . $htmlname . (($num || empty($disableifempty)) ? '' : ' disabled') . ($multiple ? '[]' : '') . '" ' . ($multiple ? 'multiple' : '') . ' ' . ( ! empty($moreparam) ? $moreparam : '') . '>';
}

if (($showempty == 1 || ($showempty == 3 && $num > 1)) && ! $multiple) $out .= '<option value="0"' . (in_array(0, $selected) ? ' selected' : '') . '>&nbsp;</option>';
if ($showempty == 2) $out .= '<option value="0"' . (in_array(0, $selected) ? ' selected' : '') . '>-- ' . $langs->trans("Internal") . ' --</option>';

$i = 0;
if ($num) {
include_once DOL_DOCUMENT_ROOT . '/product/stock/class/productlot.class.php';
$productLotStatic = new Productlot($db);

while ($i < $num) {
$obj = $db->fetch_object($resql);

$productLotStatic->id = $obj->rowid;
$productLotStatic->batch = $obj->batch;
if (empty($outputmode)) {
if (in_array($obj->rowid, $selected)) {
$out .= '<option value="' . $obj->rowid . '" selected>' . $obj->batch . '</option>';
} else {
$out .= '<option value="' . $obj->rowid . '">' . $obj->batch . '</option>';
}
} else {
array_push($outarray, array('key' => $obj->rowid, 'value' => $obj->batch, 'label' => $obj->batch));
}

$i++;
if (($i % 10) == 0) $out .= "\n";
}
} else {
$labeltoshow = ($productid != -1) ? ($langs->trans($productid ? "NoLotForThisProduct" : "NoLotDefined")) : $langs->trans('SelectAProductFirst');
$out .= '<option class="disabled" value="-1"' . (($showempty == 2 || $multiple) ? '' : ' selected') . ' disabled="disabled">';
$out .= $labeltoshow;
$out .= '</option>';
}

$parameters = array(
'socid' => $productid,
'htmlname' => $htmlname,
'resql' => $resql,
'out' => &$out,
'showfunction' => $showfunction,
'showsoc' => $showsoc,
);

//$reshook = $hookmanager->executeHooks('afterSelectContactOptions', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks

if ($htmlname != 'none' && ! $options_only) {
$out .= '</select>';
}

return $out;
} else {
dol_print_error($db);
return -1;
}
}
64 changes: 62 additions & 2 deletions view/registrationcertificatefr/registrationcertificatefr_card.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,37 @@
//if (! GETPOSTISSET('fieldname')) $_POST['fieldname'] = 'myvalue';

print '<table class="border centpercent tableforfieldcreate">'."\n";

// Common attributes
unset($object->fields['fk_lot']);
unset($object->fields['fk_product']);
unset($object->fields['a_registration_number']);

//Registration Number
print '<tr><td class="fieldrequired">' . $langs->trans('RegistrationNumber') . '</td><td>';
print '<input class="maxwidth500 widthcentpercentminusxx" id="a_registration_number" name="a_registration_number" value="'. GETPOST("a_registration_number") .'">';
print '</td></tr>';

//Fk_product
$productPost = GETPOST('fk_product') ?: 0;
print '<tr><td class="">' . $langs->trans('LinkedProduct') . '</td><td>';
$form->select_produits($productPost, 'fk_product', '', 0, 1, -1, 2, '', '', '', '', 'SelectProductsOrServices', 0, 'maxwidth500 widthcentpercentminusxx', 1);
print '<a class="butActionNew" href="' . DOL_URL_ROOT . '/product/card.php?action=create&backtopage=' . urlencode($_SERVER['PHP_SELF'] . '?action=create') . '" target="_blank"><span class="fa fa-plus-circle valignmiddle paddingleft" title="' . $langs->trans('AddProduct') . '"></span></a>';
print '</td></tr>';

//Fk_lot
$productLotPost = GETPOST('fk_lot') ?: 0;
print'<tr id=""><td >';
print $langs->trans('DolicarBatch');
print '</td><td class="lot-container">';
print '<span class="lot-content">';

dol_strlen(GETPOST('fk_product')) > 0 ? $product->fetch(GETPOST('fk_product')) : 0;

print dolicar_select_product_lots($productPost, $productLotPost, 'fk_lot', 1, '', '', 0, 'maxwidth500 widthcentpercentminusxx', false, 0, array(), false, '', 'fk_lot');
print '<a class="butActionNew" href="' . DOL_URL_ROOT . '/product/stock/productlot_card.php?action=create' . ((GETPOST('fk_product') > 0) ? '&fk_product=' . GETPOST('fk_product') : '') . '&backtopage=' . urlencode($_SERVER['PHP_SELF'] . '?action=create') . '" target="_blank"><span class="fa fa-plus-circle valignmiddle paddingleft" title="' . $langs->trans('AddProductLot') . '"></span></a>';
print '</span>';
print '</td></tr>';

include DOL_DOCUMENT_ROOT.'/core/tpl/commonfields_add.tpl.php';

// Other attributes
Expand Down Expand Up @@ -273,6 +302,38 @@

print '<table class="border centpercent tableforfieldedit">'."\n";

// Common attributes
unset($object->fields['ref']);
unset($object->fields['fk_lot']);
unset($object->fields['fk_product']);
unset($object->fields['a_registration_number']);

//Registration Number
print '<tr><td class="fieldrequired">' . $langs->trans('RegistrationNumber') . '</td><td>';
print '<input class="maxwidth500 widthcentpercentminusxx" id="a_registration_number" name="a_registration_number" value="'. ($object->a_registration_number ?: GETPOST("a_registration_number")) .'">';
print '</td></tr>';

//Fk_product
$productPost = GETPOST('fk_product') ?: $object->fk_product ;
print '<tr><td class="">' . $langs->trans('LinkedProduct') . '</td><td>';
$form->select_produits($productPost, 'fk_product', '', 0, 1, -1, 2, '', '', '', '', 'SelectProductsOrServices', 0, 'maxwidth500 widthcentpercentminusxx', 1);
print '<a class="butActionNew" href="' . DOL_URL_ROOT . '/product/card.php?action=create&backtopage=' . urlencode($_SERVER['PHP_SELF'] . '?action=create') . '" target="_blank"><span class="fa fa-plus-circle valignmiddle paddingleft" title="' . $langs->trans('AddProduct') . '"></span></a>';
print '</td></tr>';

//Fk_lot
$productLotPost = GETPOST('fk_lot') ?: $object->fk_lot;
print'<tr id=""><td>';
print $langs->trans('DolicarBatch');
print '</td><td class="lot-container">';
print '<span class="lot-content">';

dol_strlen(GETPOST('fk_product')) > 0 ? $product->fetch(GETPOST('fk_product')) : 0;

print dolicar_select_product_lots($productPost, $productLotPost, 'fk_lot', 1, '', '', 0, 'maxwidth500 widthcentpercentminusxx', false, 0, array(), false, '', 'fk_lot');
print '<a class="butActionNew" href="' . DOL_URL_ROOT . '/product/stock/productlot_card.php?action=create' . ((GETPOST('fk_product') > 0) ? '&fk_product=' . GETPOST('fk_product') : '') . '&backtopage=' . urlencode($_SERVER['PHP_SELF'] . '?action=create') . '" target="_blank"><span class="fa fa-plus-circle valignmiddle paddingleft" title="' . $langs->trans('AddProductLot') . '"></span></a>';
print '</span>';
print '</td></tr>';

// Common attributes
include DOL_DOCUMENT_ROOT.'/core/tpl/commonfields_edit.tpl.php';

Expand Down Expand Up @@ -336,7 +397,6 @@

// Common attributes
include DOL_DOCUMENT_ROOT.'/core/tpl/commonfields_view.tpl.php';

// Other attributes. Fields from hook formObjectOptions and Extrafields.
include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_view.tpl.php';

Expand Down

0 comments on commit b283b9b

Please sign in to comment.