{extends file=$smarty.const.SMARTY_LAYOUT_DASHBOARD_WIDGET_DEFAULT}

{block name="template-head" append}
	{$CSS->loadGroup("widgetTable")}
{/block}

{block name="content"}
	{$colsAssoc = $widget['data']['columnsList']}

	{if $widget['data']['invoicesList']}

		<table class="simpleList w100p">
			<thead>
				<tr>
					<th style="text-align:right;">#</th>
					{foreach $colsAssoc as $col=>$nothing}
						<th class="center">{$widgetLang['options']['columnsList'][$col]}</th>
					{/foreach}
				</tr>
			</thead>

			<tbody>
				{$index = 0}
				{foreach $widget['data']['invoicesList'] as $idInvoice => $inv}
					{$invoiceEntity = \Entity\Invoice::select($idInvoice)}
					{if !$invoiceEntity->cumulatedOnInvoiceId}
						{$index = $index + 1}
						<tr class="{cycle values="odd,even"} {if $invoiceEntity->idVoidUser} disabled {/if}">
							<td>{$index}</td>
							{foreach $colsAssoc as $col=>$nothing}
								{if $col=='number'}
									<td class="center">
										{if $invoiceEntity->invoiceNumber}
											{$invoiceEntity->invoiceNumber}
										{else}
											-
										{/if}
									</td>
								{/if}

								{if $col=='date'}
									<td class="center">{$invoiceEntity->invoiceDate|dateTimeFormat}</td>
								{/if}

								{if $col=='due'}
									<td class="center">
										{if $invoiceEntity->dueDate and $invoiceEntity->dueDate != '0000-00-00'}
											{$invoiceEntity->dueDate|dateTimeFormat}
										{else}
											-
										{/if}
									</td>
								{/if}

								{if $col=='duePassedDays'}
									<td class="center">
										{if $invoiceEntity->duePassedDays > 0}
											{$invoiceEntity->duePassedDays}
										{else}
											-
										{/if}
									</td>
								{/if}

								{if $col=='client'}
									<td>
										{if $invoiceEntity->idCorporate} {corporateLabel ref=$invoiceEntity->idCorporate}
										{else} {customerLabel ref=$invoiceEntity->idCustomer} {/if}
									</td>
								{/if}

								{if $col=='value'}
									<td class="right {if $invoiceEntity->printTotal<=0}red{/if}">
										<div style="text-align: right;">{$invoiceEntity->printTotal|priceFormat:$invoiceEntity->currency}</div>
										<div>
											{progressbar value=$invoiceEntity->cashedPercentDisplay/100
												label=sprintf("%s%%", $invoiceEntity->cashedPercentDisplay)
												tooltip="
													{if $invoiceEntity->cashedStatus=='n'}
														{$widgetLang['tooltip']['cashed_n']}
													{elseif $invoiceEntity->cashedStatus=='t'}
														{$widgetLang['tooltip']['cashed_t']}
													{elseif $invoiceEntity->cashedStatus=='o'}
														<div>{$widgetLang['tooltip']['cashed_o']} <b>({$invoiceEntity->cashedPercentDisplay}%)</b></div>
														<div>{$widgetLang['tooltip']['invoicePayedOverDiff']}: <b>{($invoiceEntity->cashedTotal-$invoiceEntity->printTotal)|priceFormat:$invoiceEntity->currency}</b></div>
													{else}
														<div>{$widgetLang['tooltip']['cashed_p']} <b>({$invoiceEntity->cashedPercentDisplay}%)</b></div>
														<div>{$widgetLang['tooltip']['invoiceCashedPartialDiff']}: <b>{($invoiceEntity->printTotal-$invoiceEntity->cashedTotal)|priceFormat:$invoiceEntity->currency}</b></div>
													{/if}
												"}
										</div>
									</td>
								{/if}

								{if $col=='responsible'}
									<td class="left">
										<div>{userLabel ref=$invoiceEntity->idUser} / {officeLabel ref=$invoiceEntity->idOffice}</div>
									 </td>
								{/if}

								{if $col=='issued'}
									<td class="left">
										{if $invoiceEntity->issueTime}
											<div>
												<var class="icon-info" tooltip={strip}"
													<div><b>{$widgetLang['options']['status']['indicators']['issuedDate']}</b>: {$invoiceEntity->issueTime|dateTimeFormat:'dateWithHMS'}</div>
													<div><b>{$widgetLang['options']['status']['indicators']['issuedUser']}</b>: {userLabel ref=$invoiceEntity->idIssueUser hideLink="1" default="-"}</div>
												"{/strip}>
												</var>
												<span class="status" style="margin-left:2px">{$widgetLang['options']['status']['issued']}</span>
											</div>
										{else}
											-
										{/if}
									</td>
								{/if}

								{if $col=='voided'}
									<td class="left">
										{if $invoiceEntity->voidTime}
											{***duplicated on invoice id***}
											{if $invoiceEntity->duplicatedOnInvoiceId}
												{$invObj = \Entity\Invoice::select($invoiceEntity->duplicatedOnInvoiceId)}
												{$duplicatedOnInvoiceNumber = $invoiceEntity->invoiceNumber}
												{$duplicatedOnInvoiceDate   = $invoiceEntity->invoiceDate}
											{/if}

											<div>
												<var class="icon-info" tooltip={strip}"
													<div><b>{$widgetLang['options']['status']['indicators']['voidDate']}</b>: {$invoiceEntity->voidTime|dateTimeFormat:'dateWithHMS'}</div>
													<div><b>{$widgetLang['options']['status']['indicators']['voidUser']}</b>: {userLabel ref=$invoiceEntity->idVoidUser hideLink="1"}</div>

													{if $invoiceEntity->duplicatedOnInvoiceId}
														<div>
															<b>{$widgetLang['options']['status']['indicators']['duplicatedOnInvoice']}</b>:
															{if $duplicatedOnInvoiceNumber}
																{$duplicatedOnInvoiceNumber}
															{else}
																?
															{/if}
																/ {$duplicatedOnInvoiceDate|dateTimeFormat}
														</div>
													{/if}
												"{/strip}>
												</var>
												<span class="status" style="margin-left:2px">{$widgetLang['options']['status']['voided']}</span>
											</div>
										{else}
											-
										{/if}
									</td>
								{/if}

								{if $col=='temporary'}
									<td class="left">
										{if $invoiceEntity->temporary}
											<span class="status" style="margin-left:2px">{$widgetLang['options']['status']['indicators']['temporary']}</span>
										{else}
											{if $invoiceEntity->cumulativeInvoice}
												<span class="status" style="margin-left:2px">{$widgetLang['options']['status']['indicators']['cumulated']}</span>
											{else}
												-
											{/if}
										{/if}
									</td>
								{/if}


								{if $col=='viewOption'}
								
									<td class="optionsBox sectionRow" style="width:8%;">
										<ul class="submenu Simple submenu-depth-0">
											
											{*GENERATE DOCUMENTS*}
											{$issueTime = \Entity\Invoice::select($idInvoice)->issueTime}
											
											{$tooltip = ""}
											{$isDisabled = false}
											{if $widget['data']['buttonGeneralStatusRestriction']['adminDocuments_noAccess']}
												{$tooltip = $_LANG['noAccessToOption']}
												{$isDisabled = true}										
											{/if}
											{if $widget['data']['buttonGeneralStatusRestriction']['adminDocuments_noDocs']}
												{$tooltip = $widget['data']['lang']['options']['reasons']['manageDocuments']['noDocuments']}
												{$isDisabled = true}										
											{/if}
											
																						
											{if !$issueTime}
											
												{if !$isDisabled}
													<li data-id="generateDocuments" class="menuItem has-icon-true" tooltip="<div class='has-description'><div class='description'>{$widgetLang['buttons']['adminDocuments']}</div></div>">
													<var class="icon-documents" data-icon="documents"
														onclick="loadPivotGenerateDoc(this, 'Generate documents', 'invoice', '{$idInvoice}');"
													></var>
													</li>
												{else}
													<li data-id="generateDocuments" class="menuItem has-icon-true" tooltip="<div class='has-description'><div class='description'>{$tooltip}</div></div>">
														<var class="icon-documents-disabled" data-icon="documents"></var>
													</li>
												{/if}
											
											
											{else}
												{if !$isDisabled}
													<li data-id="generateDocuments" class="menuItem has-icon-true" tooltip="<div class='has-description'><div class='description'>{$widgetLang['buttons']['adminDocuments']}</div></div>">
														<var class="icon-issuedDoc" data-icon="issuedDoc"
															onclick="loadPivotGenerateDoc(this, 'Generate documents', 'invoice', '{$idInvoice}');"
														></var>
													</li>
												{else}
													<li data-id="generateDocuments" class="menuItem has-icon-true" tooltip="<div class='has-description'><div class='description'>{$tooltip}</div></div>">
														<var class="icon-issuedDoc-disabled" data-icon="issuedDoc"></var>
													</li>
												{/if}
											{/if}
											
											{*ADD CASHING*}
											<li data-id="cashInvoice" class="menuItem has-icon-true" tooltip="<div class='has-description'><div class='description'>{$widgetLang['buttons']['cashInvoice']}</div></div>">
												<var class="icon-cashing" data-icon="cashing"
													onclick="Popup2.create().setId('addInvCashDoc').setUrl(MVC.appUrl('Invoices/Cashings/Management', 'index', { 'filters[idInvoice]': '{$idInvoice}' })).setDimensions(800, 500).show();"
												></var>
											</li>
										
											{*VIEW INVOICE*}
											<li data-id="viewInvoice" class="menuItem has-icon-true" tooltip="<div class='has-description'><div class='description'>{$widgetLang['buttons']['viewInvoice']}</div></div>">
												<var class="icon-view" data-icon="view"
													onclick="clientInvoiceView('{$idInvoice}');"
												></var>
											</li>
										
										</ul>
									</td>
							
								{/if}
							{/foreach}
						</tr>
					{/if}
				{/foreach}

                <tr>
                    <td colspan="6" class="left no-wrap" style="font-weight:bold;">{$_LANG['dashboard']['widgets']['_GENERAL_']['generalTotal']}</td>

                    {foreach $colsAssoc as $col=>$nothing}
                        {if $col=='value'}
                            <td class="right nowrap" style="font-weight:bold;">
                                {$separator=""}
                                {foreach $widget['data']['groupedInvoicedTotals'] as $currency => $total}
                                    {if $total > 0.001 || $total < -0.001}
                                        {$separator}
                                        {$total|priceFormat:$currency}
                                        {$separator="</br>+ "}
                                    {/if}
                                {/foreach}
                            </td>
                        {/if}
                    {/foreach}
                </tr>
			</tbody>
		</table>
	{else}
		{if $widget['data']['errorMessage']}
			<div class="messageDivContainer layout-inline">
				{message type='warning' message=$widget['data']['errorMessage']}
			</div>
		{else}
			<div class="messageDivContainer layout-inline">
				{message type='info' message=$widgetLang['messages']['noResults']}
			</div>
		{/if}

	{/if}
{/block}
