/**
     * Overloading the doActions function : replacing the parent's function with the one below
     *
     * @param   array()         $parameters     Hook metadatas (context, etc...)
     * @param   CommonObject    &$object        The object to process (an invoice if you are in invoice module, a propale in propale's module, etc...)
     * @param   string          &$action        Current action (if set). Generally create or edit or null
     * @param   HookManager     $hookmanager    Hook manager propagated to allow calling another hook
     * @return  int                             < 0 on error, 0 on success, 1 to replace standard code
     */
    function formAddObjectLine($parameters, &$object, &$action, $hookmanager)
    {
        global $db, $langs, $user, $conf, $inputalsopricewithtax;
        $TContext = explode(':', $parameters['context']);
        if (in_array('propalcard', $TContext) || in_array('ordercard', $TContext) || in_array('invoicecard', $TContext)) {
            if ($user->rights->searchproductcategory->user->search) {
                //Charger les liste des projets de type feuille de temps pas encore facturé
                $colspan1 = 4;
                $colspan2 = 4;
                if (!empty($inputalsopricewithtax)) {
                    $colspan1++;
                    $colspan2++;
                }
                if (!empty($conf->global->PRODUCT_USE_UNITS)) {
                    $colspan1++;
                }
                if (!empty($conf->margin->enabled)) {
                    $colspan1++;
                    if ($user->rights->margins->creer && !empty($conf->global->DISPLAY_MARGIN_RATES)) {
                        $colspan1++;
                    }
                    if ($user->rights->margins->creer && !empty($conf->global->DISPLAY_MARK_RATES)) {
                        $colspan1++;
                    }
                }
                $langs->load('searchproductcategory@searchproductcategory');
                ?>
<script type="text/javascript">
	        		var spc_object_type = '<?php 
                echo $object->element;
                ?>
';
	        		var spc_object_id = '<?php 
                echo $object->id;
                ?>
';
	        	</script>
				<tr class="liste_titre nodrag nodrop">
					<td colspan="<?php 
                echo $colspan1;
                ?>
"><?php 
                echo $langs->trans('SearchByCategory');
                ?>
</td>
					<td align="right"><?php 
                echo $langs->trans('Qty');
                ?>
</td>
					<td align="center" colspan="<?php 
                echo $colspan2;
                ?>
">&nbsp;<?php 
                if (!empty($conf->global->SUBTOTAL_ALLOW_ADD_LINE_UNDER_TITLE)) {
                    echo $langs->trans('subtotal_title_to_add_under_title');
                }
                ?>
</td>
				</tr>
				<tr class="pair">
					<td colspan="<?php 
                echo $colspan1;
                ?>
">
						<div id="arboresenceCategoryProduct" spc-role="arbo-multiple">
							
						</div>
					</td>
					<td class="nobottom" align="right">
						<input id="qty_spc" type="text" value="1" size="5" class="flat" />
					</td>
					<td valign="middle" align="center" colspan="<?php 
                echo $colspan2;
                ?>
">
						<?php 
                if (!empty($conf->global->SUBTOTAL_ALLOW_ADD_LINE_UNDER_TITLE)) {
                    dol_include_once('/subtotal/class/subtotal.class.php');
                    $TTitle = TSubtotal::getAllTitleFromDocument($object);
                    echo getHtmlSelectTitle($object);
                }
                ?>
						<input id="addline_spc" class="button" type="button" name="addline_timesheet" value="<?php 
                echo $langs->trans('Add');
                ?>
">
					</td>
				</tr>
				
				<?php 
            }
        }
        return 0;
    }
/**
 * Si $level == 0 alors on ajoute tous les sous-totaux restants
 */
function _addSousTotaux(&$langs, &$object, &$TLastLevelTitleAdded, $level = 0)
{
    $lastIndex = count($TLastLevelTitleAdded) - 1;
    if ($lastIndex < 0) {
        $lastIndex = 0;
    }
    if ($level <= $TLastLevelTitleAdded[$lastIndex]) {
        for ($i = $lastIndex; $i >= 0; $i--) {
            if (is_null($TLastLevelTitleAdded[$i])) {
                continue;
            }
            if ($level > $TLastLevelTitleAdded[$i]) {
                break;
            }
            // Add sous-total
            TSubtotal::addSubTotalLine($object, $langs->trans('SubTotal'), 100 - $TLastLevelTitleAdded[$i]);
            $TLastLevelTitleAdded[$i] = null;
            // Nettoyage du tableau
        }
    }
}
    /**
     * Overloading the doActions function : replacing the parent's function with the one below
     *
     * @param   array()         $parameters     Hook metadatas (context, etc...)
     * @param   CommonObject    &$object        The object to process (an invoice if you are in invoice module, a propale in propale's module, etc...)
     * @param   string          &$action        Current action (if set). Generally create or edit or null
     * @param   HookManager     $hookmanager    Hook manager propagated to allow calling another hook
     * @return  int                             < 0 on error, 0 on success, 1 to replace standard code
     */
    function formAddObjectLine($parameters, &$object, &$action, $hookmanager)
    {
        global $db, $langs, $user, $conf, $inputalsopricewithtax;
        $langs->load('contratconcurrent@contratconcurrent');
        $TContext = explode(':', $parameters['context']);
        if (in_array('propalcard', $TContext)) {
            dol_include_once('/core/class/html.form.class.php');
            $form = new Form($db);
            $TContratConcurrent = $this->getTContratConcurrent($object);
            $usemargins = 0;
            if (!empty($conf->margin->enabled) && !empty($object->element)) {
                $usemargins = 1;
            }
            $colspan = 4;
            if (!empty($inputalsopricewithtax)) {
                $colspan++;
            }
            ?>
        	
        	<tr class="liste_titre nodrag nodrop">
        		<td<?php 
            echo !empty($conf->global->MAIN_VIEW_LINE_NUMBER) ? ' colspan="2"' : '';
            ?>
><?php 
            echo $langs->trans('ImportContratLine');
            ?>
</td>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
				<?php 
            if (!empty($inputalsopricewithtax)) {
                ?>
				<td align="right">&nbsp;</td>
				<?php 
            }
            ?>
				<td align="right">&nbsp;</td>
				<?php 
            if (!empty($conf->global->PRODUCT_USE_UNITS)) {
                ?>
				<td align="left">&nbsp;</td>
				<?php 
            }
            ?>
				<td align="right">&nbsp;</td>
				<?php 
            if ($object->situation_cycle_ref) {
                print '<td align="right">&nbsp;</td>';
            }
            if (!empty($usemargins)) {
                ?>
					<td align="right">&nbsp;</td>
					<?php 
                if ($user->rights->margins->creer && !empty($conf->global->DISPLAY_MARGIN_RATES)) {
                    echo '<td align="right">&nbsp;</td>';
                }
                if ($user->rights->margins->creer && !empty($conf->global->DISPLAY_MARK_RATES)) {
                    echo '<td align="right">&nbsp;</td>';
                }
            }
            ?>
				<td align="center" colspan="<?php 
            echo $colspan;
            ?>
">&nbsp;<?php 
            if (!empty($conf->global->SUBTOTAL_ALLOW_ADD_LINE_UNDER_TITLE)) {
                echo $langs->trans('subtotal_title_to_add_under_title');
            }
            ?>
</td>
			</tr>
			<tr class="pair">
				<script type="text/javascript">
					function checkInputRadioContratConcurrent() 
					{
						$('input#prod_entry_mode_import_line_contrat_concurrent').click();
						$('#select_type option[value="-1"]').attr('selected', true).prop('selected', true);
						$('#idprod').val('');
						$('#search_idprod').val('');
					}
				</script>
				<td>
					<label>
						<input id="prod_entry_mode_import_line_contrat_concurrent" type="radio" value="contrat_line" name="prod_entry_mode">
						<?php 
            echo $langs->trans('add_fk_contrat_line_in_propal');
            ?>
					</label>
					
					<?php 
            $moreparam = 'onchange="checkInputRadioContratConcurrent();" style="min-width:150px;"';
            print Form::selectarray('fk_line_contrat_origin', $TContratConcurrent, '', 1, 0, 0, $moreparam, 0, 0, 0, '', '', 1);
            ?>
				</td>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
				<?php 
            if (!empty($inputalsopricewithtax)) {
                ?>
				<td align="right">&nbsp;</td>
				<?php 
            }
            ?>
				<td align="right">&nbsp;</td>
				<?php 
            if (!empty($conf->global->PRODUCT_USE_UNITS)) {
                ?>
				<td align="left">&nbsp;</td>
				<?php 
            }
            ?>
				<td align="right">&nbsp;</td>
				<?php 
            if ($object->situation_cycle_ref) {
                print '<td align="right">&nbsp;</td>';
            }
            if (!empty($usemargins)) {
                ?>
					<td align="right">&nbsp;</td>
					<?php 
                if ($user->rights->margins->creer && !empty($conf->global->DISPLAY_MARGIN_RATES)) {
                    echo '<td align="right">&nbsp;</td>';
                }
                if ($user->rights->margins->creer && !empty($conf->global->DISPLAY_MARK_RATES)) {
                    echo '<td align="right">&nbsp;</td>';
                }
            }
            ?>
				<td valign="middle" align="center" colspan="<?php 
            echo $colspan;
            ?>
">
					<?php 
            if (!empty($conf->global->SUBTOTAL_ALLOW_ADD_LINE_UNDER_TITLE)) {
                dol_include_once('/subtotal/class/subtotal.class.php');
                $TTitle = TSubtotal::getAllTitleFromDocument($object);
                echo getHtmlSelectTitle($object);
            }
            ?>
					<input type="submit" id="addcontratline" name="addcontratline" value="Ajouter" class="button">
				</td>
			
			</tr>
        	<?php 
        }
    }
 $default_tva = 0;
 if (!empty($conf->global->IMPORTPROPAL_FORCE_TVA)) {
     $default_tva = $conf->global->IMPORTPROPAL_FORCE_TVA;
 }
 $TLastLevelTitleAdded = array();
 // Tableau pour empiler et dépiller les niveaux de titre pour ensuite ajouter les sous-totaux
 $TData = $_REQUEST['TData'];
 $last_line_id = null;
 $last_line_product = null;
 foreach ($TData as $k => $row) {
     if (empty($row['to_import'])) {
         continue;
     } elseif (!empty($conf->subtotal->enabled) && $row['type'] == 'title') {
         _addSousTotaux($langs, $object, $TLastLevelTitleAdded, $row['level']);
         // Add title ou sub-title
         TSubtotal::addSubTotalLine($object, $row['label'], 0 + $row['level']);
         $TLastLevelTitleAdded[] = $row['level'];
     } else {
         if (!empty($conf->nomenclature->enabled) && ($row['type'] == 'nomenclature' || $row['type'] == 'workstation')) {
             //var_dump($last_line_product,$last_line_id);exit;
             if ($last_line_id > 0) {
                 $nomenclature = new TNomenclature();
                 $workstation = new TWorkstation();
                 if ($last_line_product > 0 && !empty($conf->global->CREATE_PRODUCT_FROM_IMPORT)) {
                     $nomenclature->loadByObjectId($PDOdb, $last_line_product, 'product');
                     $nomenclature->fk_object = $last_line_product;
                     $nomenclature->fk_nomenclature_parent = 0;
                     $nomenclature->is_default = 0;
                     $nomenclature->object_type = 'product';
                     $nomenclature->save($PDOdb);
                 } else {