<?php

global $object, $action, $mysoc, $soc, $lineid, $user, $conf, $hookmanager;

$result = $object->getLinesArray();

global $rental_lines;

if ( ! empty($object->array_options['options_rental_doc']) && $conf->global->RENTAL_DONTMIX_LINES )
{
	$langs->load('rental@rental');

	print '<div id="rentalLines" class="div-table-responsive-no-min">';
	print '<table id="tablelines" class="tablelines noborder noshadow draftedit" width="100%">';

	$rental_lines = true;
	print '<span class="draft_lines_title">'.$langs->trans("RentalLines").'</span>';

	printObjectLines($action, $mysoc, $soc, $lineid, 1);

	print '</table>';
	print '</div>';


	print '<div id="saleLines" class="div-table-responsive-no-min">';
	print '<table id="tablelines" class="tablelines noborder noshadow draftedit" width="100%">';

	$rental_lines = false; // lignes de vente

	print '<span class="draft_lines_title">'.$langs->trans("SalesLines").'</span>';

	printObjectLines($action, $mysoc, $soc, $lineid, 1);

}
else
{
	print '<div class="div-table-responsive-no-min" id="mixLines">';
	print '<table id="tablelines" class="tablelines noborder noshadow draftedit" width="100%">';

	printObjectLines($action, $mysoc, $soc, $lineid, 1);

}

/*
 * Form to add new line
 */
$object->formAddObjectLine(1, $mysoc, $soc);

$parameters = array();
// Note that $action and $object may be modified by hook
$reshook = $hookmanager->executeHooks('formAddObjectLine', $parameters, $object, $action);

print '</table>';
print '</div>';

	function printObjectLines($action, $seller, $buyer, $selected = 0, $dateSelector = 0, $defaulttpldir = '/core/tpl')
	{
		global $conf, $hookmanager, $langs, $user, $object, $form;
		// TODO We should not use global var for this
		global $inputalsopricewithtax, $usemargins, $disableedit, $disablemove, $disableremove, $outputalsopricetotalwithtax;

		// Define usemargins
		$usemargins=0;
		if (! empty($conf->margin->enabled) && ! empty($object->element) && in_array($object->element, array('facture','facturerec','propal','commande'))) $usemargins=1;

		$num = count($object->lines);

		// Line extrafield
		require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
		$extrafieldsline = new ExtraFields($object->db);
		$extralabelslines=$extrafieldsline->fetch_name_optionals_label($object->table_element_line);

		$parameters = array('num'=>$num,'i'=>$i,'dateSelector'=>$dateSelector,'seller'=>$seller,'buyer'=>$buyer,'selected'=>$selected, 'extrafieldsline'=>$extrafieldsline);
		$reshook = $hookmanager->executeHooks('printObjectLineTitle', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
		if (empty($reshook))
		{
			// Output template part (modules that overwrite templates must declare this into descriptor)
			// Use global variables + $dateSelector + $seller and $buyer
			$dirtpls=array_merge($conf->modules_parts['tpl'], array($defaulttpldir));
			foreach($dirtpls as $module => $reldir)
			{
				if (!empty($module))
				{
					$tpl = dol_buildpath($reldir.'/objectline_title.tpl.php');
				}
				else
				{
					$tpl = DOL_DOCUMENT_ROOT.$reldir.'/objectline_title.tpl.php';
				}
				if (empty($conf->file->strict_mode)) {
					$res=@include $tpl;
				} else {
					$res=include $tpl; // for debug
				}
				if ($res) break;
			}
		}

		$var = true;
		$i	 = 0;

		print "<tbody>\n";
		foreach ($object->lines as $line)
		{
			//Line extrafield
			$line->fetch_optionals();

			include(dol_buildpath('/dolitools/class/drafts/printobjectline.inc.php'));
			$i++;
		}
		print "</tbody><!-- end printObjectLines() -->\n";
	}
