{extends file=$smarty.const.SMARTY_LAYOUT_DASHBOARD_WIDGET_DEFAULT}

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

{block name="localCSS" append}
    <style>
        span.no-wrap {
            white-space: nowrap;
        }
    </style>
{/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 => $invDet}
                    {$index = $index + 1}
                    <tr class="{cycle values="odd,even"} {if $invDet['idVoidUser']} disabled {/if}">
                        <td>{$index}</td>
                        {foreach $colsAssoc as $col=>$nothing}
                            {if $col=='number'}
                                <td class="center">
                                    {if $invDet['invoiceNumber']}
                                        {invoiceLabel ref=$idInvoice format="%invoiceNumber%"}
                                        <div><i>#{$idInvoice}</i></div>
                                    {else}
                                        {invoiceLabel ref=$idInvoice}
                                    {/if}
                                </td>
                            {/if}

                            {if $col=='date'}
                                <td class="center">{$invDet['invoiceDate']|dateTimeFormat}</td>
                            {/if}

                            {if $col=='due'}
                                <td class="center">
                                    {if $invDet['dueDate']}
                                        {$invDet['dueDate']|dateTimeFormat}
                                    {else}
                                        -
                                    {/if}
                                </td>
                            {/if}

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

                            {if $col=='client'}
                                <td>
                                    {if $invDet['idCorporate']} {corporateLabel ref=$invDet['idCorporate']}
                                    {else} {customerLabel ref=$invDet['idCustomer']} {/if}
                                </td>
                            {/if}

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

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

                            {if $col=='issued'}
                                <td class="left">
                                    {if $invDet['issueTime']}
                                        <div>
                                            <var class="icon-info" tooltip={strip}"
                                                <div><b>{$widgetLang['options']['status']['indicators']['issuedDate']}</b>: {$invDet['issueTime']|dateTimeFormat:'dateWithHMS'}</div>
                                                <div><b>{$widgetLang['options']['status']['indicators']['issuedUser']}</b>: {userLabel ref=$invDet['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 $invDet['voidTime']}
                                        {***duplicated on invoice id***}
                                        {if $invDet['duplicatedOnInvoiceId']}
                                            {$invObj = \Entity\Invoice::select($invDet['duplicatedOnInvoiceId'])}
                                            {$duplicatedOnInvoiceNumber = $invDet['invoiceNumber']}
                                            {$duplicatedOnInvoiceDate   = $invDet['invoiceDate']}
                                        {/if}

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

                                                {if $invDet['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 $invDet['temporary']}
                                        <span class="status" style="margin-left:2px">{$widgetLang['options']['status']['indicators']['temporary']}</span>
                                    {else}
                                        {if $invDet['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*}
                                        {$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 !$invDet['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>
				{/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}
                                        {$separator}
                                        <span class="no-wrap">{$total|priceFormat:$currency}</span>
                                        {$separator=" + "}
                                    {/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}
