<!--                                                                  -->
<!--  Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved     -->
<!--                                                                  -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:dyn="http://exslt.org/dynamic" extension-element-prefixes="dyn">
    <xsl:output method="html" indent="no" doctype-system="" encoding="utf-8" />

    <xsl:include href="/app/tools/bookpub/templates/global.xsl" />
    <xsl:include href="/app/tools/bookpub/templates/tracker/messages.xsl" />

    <xsl:variable name="currentPage">
        <xsl:choose>
            <xsl:when test="/Root/Results/page &gt; /Root/Results/pageCount">1</xsl:when>
            <xsl:otherwise><xsl:value-of select="/Root/Results/page" /></xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:variable name="sortBy" select="/Root/Form/SortBy" />
    <xsl:variable name="reverseSort" select="/Root/Form/ReverseSort" />

    <xsl:template name="revenueFileHeaderCell">
        <xsl:param name="sortName" />
        <xsl:param name="displayName" />
        <xsl:variable name="nextReverseSort">
            <xsl:choose>
                <xsl:when test="$sortBy = $sortName and $reverseSort = 0">1</xsl:when>
                <xsl:otherwise>0</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <th class="Table-col Table-col-nowrap">
            <xsl:if test="$displayName = 'Revenue' or $displayName = 'Records' or $displayName = 'Units'">
                <xsl:attribute name="class">Table-col Table-col-amount Table-col-nowrap</xsl:attribute>
            </xsl:if>
            <xsl:call-template name="uiTableSorting">
                <xsl:with-param name="caption" select="$displayName" />
                <xsl:with-param name="sortBy" select="$sortBy" />
                <xsl:with-param name="name" select="$sortName" />
                <xsl:with-param name="sortReverse" select="$reverseSort" />
                <xsl:with-param name="onClick">location.href='/bookpub/tracker?c=revenue_file_library&amp;page=1&amp;sortBy=<xsl:value-of select="$sortName" />&amp;reverseSort=<xsl:value-of select="$nextReverseSort" />&amp;serviceID=<xsl:value-of select="/Root/Form/ServiceID" />&amp;dateType=<xsl:value-of select="/Root/Form/DateType" />&amp;startDate=<xsl:value-of select="/Root/Form/StartDate" />&amp;endDate=<xsl:value-of select="/Root/Form/EndDate" />&amp;pageSize=<xsl:value-of select="/Root/Form/PageSize" />'</xsl:with-param>
            </xsl:call-template>
        </th>
    </xsl:template>

    <xsl:template name="truncateText">
        <xsl:param name="text" />
        <xsl:param name="maxLength" />
        <xsl:choose>
            <xsl:when test="string-length($text) &gt; $maxLength">
                <xsl:value-of select="substring($text, 1, $maxLength)" />
                <xsl:text> ...</xsl:text>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$text" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template name="revenueAmount">
        <xsl:choose>
            <xsl:when test="normalize-space(RevenueBase)">
                <xsl:value-of select="RevenueBase" />
            </xsl:when>
            <xsl:when test="normalize-space(Revenue)">
                <xsl:value-of select="Revenue" />
            </xsl:when>
            <xsl:when test="FileStatus = 4"><xsl:value-of select="/Root/Client/Locale/CurrencyFormat/Symbol" />0<xsl:value-of select="/Root/Client/Locale/NumericFormat/Decimal" />00</xsl:when>
        </xsl:choose>
    </xsl:template>

    <xsl:template name="renderSplitDialogControl">
        <xsl:if test="IsSplit = 1">
            <button type="button" id="split_history{FileID}" title="File has been split" style="background: none; border: 0; padding: 0; cursor: pointer;">
                <span class="Table-action-item">
                    <xsl:call-template name="icon">
                        <xsl:with-param name="type" select="'add'" />
                        <xsl:with-param name="role" select="'action'" />
                        <xsl:with-param name="cursor" select="'default'" />
                    </xsl:call-template>
                </span>
            </button>
            <div id="split_dialog{FileID}" title="splitHistoryModal">
                <xsl:call-template name="loader" />
            </div>
            <script type="text/javascript">
                $('#split_dialog<xsl:value-of select="FileID" />').dialog({
                    autoOpen : false,
                    minWidth: 720,
                    closeOnEscape: true,
                    modal : false,
                    title: 'Split File Details',
                    position: {
                        my: 'center top',
                        at: 'center bottom',
                        of: $('#split_history<xsl:value-of select="FileID" />')
                    },
                    open: function () {
                        $(this).load('/bookpub/tracker?c=revenue_file_library&amp;popup=split_history&amp;file=<xsl:value-of select="FileID" />');
                    }
                });
                $('#split_history<xsl:value-of select="FileID" />').on('click', function () {
                    $('#split_dialog<xsl:value-of select="FileID" />').dialog('open');
                    return false;
                });
            </script>
        </xsl:if>
    </xsl:template>

    <xsl:template name="renderFileRow">
        <tr class="Table-row Table-row-data">
            <td class="Table-col Table-col-icon">
                <xsl:if test="IsSplit = 1">
                    <span class="js-tooltip" title="File has been split">
                        <xsl:call-template name="icon">
                            <xsl:with-param name="type" select="'info'" />
                            <xsl:with-param name="role" select="'action'" />
                            <xsl:with-param name="cursor" select="'default'" />
                        </xsl:call-template>
                    </span>
                </xsl:if>
            </td>
            <td class="Table-col"><xsl:value-of select="ServiceName" /></td>
            <td class="Table-col">
                <a name="file{FileID}" />
                <xsl:choose>
                    <xsl:when test="/Root/Access/bookpub/tracker/saveas">
                        <a href="/bookpub/tracker?c=saveas&amp;file={FileID}&amp;period={PeriodID}" class="js-tooltip">
                            <xsl:if test="/Root/User/AccessLevel = 1 and VersionNum != 0">
                                <xsl:attribute name="title">Importer Version: <xsl:value-of select="VersionNum" /></xsl:attribute>
                            </xsl:if>
                            <xsl:value-of select="FileName" />
                        </a>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="FileName" />
                    </xsl:otherwise>
                </xsl:choose>
            </td>
            <td class="Table-col"><xsl:value-of select="Received" /></td>
            <td class="Table-col Table-col-amount"><xsl:value-of select="Units" /></td>
            <!-- Revenue column -->
            <td class="Table-col Table-col-amount">
                <xsl:variable name="clientCurrency" select="/Root/Client/Locale/CurrencyFormat/CurrencyCode" />
                <xsl:variable name="isRootRow" select="ParentFileID = '' and HasChild = 0" />
                <xsl:variable name="hasRevenue" select="normalize-space(Revenue)" />
                <xsl:variable name="hasInputRevenue" select="normalize-space(InputRevenue)" />
                <xsl:variable name="isForeignCurrency" select="CurrencyCode != $clientCurrency" />
                <xsl:variable name="canTrackRevenue" select="/Root/Access/bookpub/tracker/revenue = 1" />
                <xsl:variable name="canTrackCurrency" select="/Root/Access/bookpub/tracker/currency = 1" />

                <xsl:if test="$isForeignCurrency">
                    (<xsl:value-of select="translate(translate(Revenue, '-', '&#8209;'), ' ', '&#160;')" />&#160;<xsl:value-of select="CurrencyCode" />)
                </xsl:if>

                <xsl:choose>
                    <xsl:when test="
                        $canTrackRevenue
                        and $isRootRow
                        and (Revenue/@value = 0 or Revenue = '' or $hasInputRevenue)
                        and (
                            CurrencyCode = $clientCurrency
                            or ($hasRevenue and InputConversionRate != 0)
                        )">
                        <xsl:call-template name="revenueAmount" />
                    </xsl:when>

                    <xsl:when test="
                        $canTrackCurrency
                        and $isRootRow
                        and $hasRevenue
                        and (ConversionInput != 0 or InputConversionRate != 0)">
                        <xsl:call-template name="revenueAmount" />
                    </xsl:when>

                    <xsl:otherwise>
                        <xsl:if test="not(File/MultipleRevenue)">
                            <xsl:call-template name="revenueAmount" />
                        </xsl:if>

                        <xsl:if test="File/MultipleRevenue">
                            <xsl:call-template name="revenueAmount" />
                        </xsl:if>
                    </xsl:otherwise>
                </xsl:choose>
            </td>

            <!-- Alternate Currency Conversion = bit 11 of RSCOMMON.client_type (2048 = 2^11) -->
            <xsl:if test="floor( /Root/Client/TypeMask div 2048) mod 2 = 1">
                <td class="Table-col Table-col-icon">
                    <xsl:choose>
                        <xsl:when test="CurrencyCode != 'MUL'">
                            <xsl:if test="normalize-space(AlternateConversionRate) != ''">
                                <a class="js-tooltip" title="{translate(AlternateConversionRate*Revenue/@value,'$','')} {AlternateCurrencyCode}">
                                    <xsl:call-template name="icon">
                                        <xsl:with-param name="type" select="'currency'" />
                                        <xsl:with-param name="role" select="'action'" />
                                    </xsl:call-template>
                                </a>
                            </xsl:if>
                        </xsl:when>
                        <xsl:when test="CurrencyCode = 'MUL'">
                            <xsl:variable name="titleText">
                                <xsl:for-each select="AlternateCurrencyList/Currency[string(AlternateCurrencyTotal) != '']">
                                        <xsl:value-of select="concat(format-number(AlternateCurrencyTotal, '#.##'), ' ', AlternateCurrencyCode)"/>
                                        <xsl:if test="position() != last()">
                                            <xsl:text>, &#10;</xsl:text>
                                        </xsl:if>
                                </xsl:for-each>
                            </xsl:variable>
                            <xsl:if test="normalize-space(AlternateConversionRate) != ''">
                                <a class="js-tooltip" data-title="{$titleText}">
                                    <xsl:call-template name="icon">
                                        <xsl:with-param name="type" select="'currency'" />
                                        <xsl:with-param name="role" select="'action'" />
                                    </xsl:call-template>
                                </a>
                            </xsl:if>
                        </xsl:when>
                        <xsl:otherwise>
                            &#160;
                        </xsl:otherwise>
                    </xsl:choose>
                </td>
            </xsl:if>

            <td class="Table-col Table-col-amount Table-col-nowrap"><xsl:value-of select="Records" /></td>
            <td class="Table-col">
                <a href="/bookpub/tracker?period={PeriodID}" class="js-tooltip" title="{normalize-space(PeriodName)}" target="_blank">
                    <xsl:call-template name="truncateText">
                        <xsl:with-param name="text" select="PeriodName" />
                        <xsl:with-param name="maxLength" select="14" />
                    </xsl:call-template>
                </a>
            </td>
            <td class="Table-col Table-col-icon">
                <xsl:call-template name="renderSplitDialogControl" />
            </td>
            <td class="Table-col"><xsl:value-of select="Closed" /></td>
            <td class="Table-col">
                <span class="js-tooltip" data-title="{Notes}">
                    <xsl:call-template name="truncateText">
                        <xsl:with-param name="text" select="Notes" />
                        <xsl:with-param name="maxLength" select="3" />
                    </xsl:call-template>
                </span>
            </td>
        </tr>
    </xsl:template>

    <xsl:template name="pickLayout">
        <div class="Tracker-basecontainer">
            <xsl:call-template name="helpLink" />

            <xsl:if test="Messages/Message[Type = 'error']">
                <xsl:call-template name="trackerErrorMessages" />
            </xsl:if>

            <form action="/bookpub/tracker" method="get" name="revenue_file_library">
                <input type="hidden" name="c" value="revenue_file_library" />
                <input type="hidden" name="prevDateType" value="{Form/DateType}" />
                <input type="hidden" name="sortBy" value="{Form/SortBy}" />
                <input type="hidden" name="reverseSort" value="{Form/ReverseSort}" />

                <div class="Table-controls Table-controls-top">
                    <fieldset class="Form-fieldset">
                        <div class="Form-item">
                            <label class="Form-label">
                                <span class="Form-label-caption">Service</span>
                                <div class="Form-input-wrapper">
                                    <xsl:variable name="serviceOptions">
                                        <map>
                                            <xsl:for-each select="Form/ServiceList/Service">
                                                <mapEntry><value><xsl:value-of select="serviceID"/></value><name><xsl:value-of select="serviceName" /></name></mapEntry>
                                            </xsl:for-each>
                                        </map>
                                    </xsl:variable>
                                    <xsl:call-template name="uiDropdown">
                                        <xsl:with-param name="items" select="$serviceOptions"/>
                                        <xsl:with-param name="name" select="'serviceID'" />
                                        <xsl:with-param name="id" select="'serviceID'" />
                                        <xsl:with-param name="active" select="Form/ServiceID" />
                                        <xsl:with-param name="className" select="'Tracker-from-service-select'"/>
                                    </xsl:call-template>
                                </div>
                            </label>
                        </div>

                        <div class="Form-item">
                            <label class="Form-label">
                                <span class="Form-label-caption">Date Type</span>
                            </label>
                            <xsl:variable name="dateTypeReceived">
                                <xsl:if test="Form/DateType = 'received'">true</xsl:if>
                            </xsl:variable>
                            <xsl:variable name="dateTypeClosed">
                                <xsl:if test="Form/DateType = 'closed'">true</xsl:if>
                            </xsl:variable>

                            <div class="Form-item">
                                <xsl:call-template name="uiRadio">
                                    <xsl:with-param name="caption" select="'File Received Date'"/>
                                    <xsl:with-param name="name" select="'dateType'"/>
                                    <xsl:with-param name="value" select="'received'"/>
                                    <xsl:with-param name="checked" select="$dateTypeReceived"/>
                                </xsl:call-template>
                            </div>

                            <div class="Form-item">
                                <xsl:call-template name="uiRadio">
                                    <xsl:with-param name="caption" select="'Closed Period Date'"/>
                                    <xsl:with-param name="name" select="'dateType'"/>
                                    <xsl:with-param name="value" select="'closed'"/>
                                    <xsl:with-param name="checked" select="$dateTypeClosed"/>
                                </xsl:call-template>
                            </div>

                        </div>

                        <div class="Form-group Form-group-horizontal">
                            <div class="Form-item">
                                <label class="Form-label">
                                    <span class="Form-label-caption">Start Date</span>
                                    <div class="Form-input-wrapper">
                                        <xsl:call-template name="uiDatepicker">
                                            <xsl:with-param name="id" select="'startDate'" />
                                            <xsl:with-param name="name" select="'startDate'" />
                                            <xsl:with-param name="value">
                                                <xsl:value-of select="Form/StartDate" />
                                            </xsl:with-param>
                                        </xsl:call-template>
                                    </div>
                                </label>
                            </div>
                            <div class="Form-item">
                                <label class="Form-label">
                                    <span class="Form-label-caption">End Date</span>
                                    <div class="Form-input-wrapper">
                                        <xsl:call-template name="uiDatepicker">
                                            <xsl:with-param name="id" select="'endDate'" />
                                            <xsl:with-param name="name" select="'endDate'" />
                                            <xsl:with-param name="value">
                                                <xsl:value-of select="Form/EndDate" />
                                            </xsl:with-param>
                                        </xsl:call-template>
                                    </div>
                                </label>
                            </div>
                            <div class="Form-item">
                                <label class="Form-label">
                                    <span class="Form-label-caption">Files per page</span>
                                    <div class="Form-input-wrapper">
                                        <xsl:variable name="pageSizeOptions">
                                            <map>
                                                <mapEntry><value>25</value><name>25</name></mapEntry>
                                                <mapEntry><value>50</value><name>50</name></mapEntry>
                                                <mapEntry><value>100</value><name>100</name></mapEntry>
                                                <mapEntry><value>all</value><name>All</name></mapEntry>
                                            </map>
                                        </xsl:variable>
                                        <xsl:call-template name="uiDropdown">
                                            <xsl:with-param name="items" select="$pageSizeOptions"/>
                                            <xsl:with-param name="name" select="'pageSize'" />
                                            <xsl:with-param name="id" select="'pageSize'" />
                                            <xsl:with-param name="active" select="Form/PageSize" />
                                            <xsl:with-param name="className" select="'Tracker-files-count-select'"/>
                                        </xsl:call-template>
                                    </div>
                                </label>
                            </div>
                            <div class="Form-item">
                                <xsl:call-template name="uiButton">
                                    <xsl:with-param name="content">Apply Filter</xsl:with-param>
                                    <xsl:with-param name="elemType" select="'submit'" />
                                </xsl:call-template>
                            </div>
                            <div class="Form-item">
                                <xsl:call-template name="uiButton">
                                    <xsl:with-param name="content" select="'Clear'" />
                                    <xsl:with-param name="className" select="'Form-button'"/>
                                    <xsl:with-param name="url">/bookpub/tracker?c=revenue_file_library</xsl:with-param>
                                </xsl:call-template>
                            </div>
                        </div>
                    </fieldset>
                </div>
            </form>

            <table class="Table Table-with-highlight">
                <thead>
                    <tr class="Table-row Table-row-header">
                        <th class="Table-col Table-col-icon"></th>
                        <xsl:call-template name="revenueFileHeaderCell">
                            <xsl:with-param name="sortName" select="'service_name'" />
                            <xsl:with-param name="displayName" select="'Service'" />
                        </xsl:call-template>
                        <xsl:call-template name="revenueFileHeaderCell">
                            <xsl:with-param name="sortName" select="'orig_file_name'" />
                            <xsl:with-param name="displayName" select="'File Name'" />
                        </xsl:call-template>
                        <xsl:call-template name="revenueFileHeaderCell">
                            <xsl:with-param name="sortName" select="'date_created'" />
                            <xsl:with-param name="displayName" select="'Received'" />
                        </xsl:call-template>
                        <xsl:call-template name="revenueFileHeaderCell">
                            <xsl:with-param name="sortName" select="'units'" />
                            <xsl:with-param name="displayName" select="'Units'" />
                        </xsl:call-template>
                        <xsl:call-template name="revenueFileHeaderCell">
                            <xsl:with-param name="sortName" select="'revenue'" />
                            <xsl:with-param name="displayName" select="'Revenue'" />
                        </xsl:call-template>
                        <xsl:if test="floor( /Root/Client/TypeMask div 2048) mod 2 = 1">
                            <th class="Table-col Table-col-icon">
                                <span class="js-tooltip" title="Alternate Currency Conversion">Alt</span>
                            </th>
                        </xsl:if>
                        <xsl:call-template name="revenueFileHeaderCell">
                            <xsl:with-param name="sortName" select="'records'" />
                            <xsl:with-param name="displayName" select="'Records'" />
                        </xsl:call-template>
                        <xsl:call-template name="revenueFileHeaderCell">
                            <xsl:with-param name="sortName" select="'period_id'" />
                            <xsl:with-param name="displayName" select="'Period'" />
                        </xsl:call-template>
                        <th class="Table-col Table-col-icon"></th>
                        <xsl:call-template name="revenueFileHeaderCell">
                            <xsl:with-param name="sortName" select="'period_date_closed'" />
                            <xsl:with-param name="displayName" select="'Closed'" />
                        </xsl:call-template>
                        <xsl:call-template name="revenueFileHeaderCell">
                            <xsl:with-param name="sortName" select="'notes'" />
                            <xsl:with-param name="displayName" select="'Notes'" />
                        </xsl:call-template>
                    </tr>
                </thead>
                <tbody>
                    <xsl:choose>
                        <xsl:when test="Results/File">
                            <xsl:for-each select="Results/File">
                                <xsl:call-template name="renderFileRow" />
                            </xsl:for-each>
                        </xsl:when>
                        <xsl:otherwise>
                            <tr class="Table-row Table-row-data">
                                <td class="Table-col" colspan="{11 + (floor(/Root/Client/TypeMask div 2048) mod 2)}">No files found.</td>
                            </tr>
                        </xsl:otherwise>
                    </xsl:choose>
                </tbody>
            </table>

            <xsl:if test="Results/totalRecords > 0">
                <div class="Table-controls Table-controls-bottom">
                    <div class="Table-controls-item Table-controls-item-pagination">
                        <xsl:call-template name="uiPagination">
                            <xsl:with-param name="lastPage" select="Results/pageCount" />
                            <xsl:with-param name="totalCount" select="Results/totalRecords" />
                            <xsl:with-param name="currentPage" select="Results/page" />
                            <xsl:with-param name="searchParams">
                                <xsl:text>&amp;c=revenue_file_library</xsl:text>
                                <xsl:text>&amp;serviceID=</xsl:text><xsl:value-of select="Form/ServiceID" />
                                <xsl:text>&amp;dateType=</xsl:text><xsl:value-of select="Form/DateType" />
                                <xsl:text>&amp;startDate=</xsl:text><xsl:value-of select="Form/StartDate" />
                                <xsl:text>&amp;endDate=</xsl:text><xsl:value-of select="Form/EndDate" />
                                <xsl:text>&amp;pageSize=</xsl:text><xsl:value-of select="Form/PageSize" />
                                <xsl:text>&amp;sortBy=</xsl:text><xsl:value-of select="Form/SortBy" />
                                <xsl:text>&amp;reverseSort=</xsl:text><xsl:value-of select="Form/ReverseSort" />
                            </xsl:with-param>
                        </xsl:call-template>
                    </div>
                </div>
            </xsl:if>

            <div class="Table-controls Table-controls-top">
                <fieldset class="Form-fieldset Form-fieldset-buttons">
                    <xsl:call-template name="uiButton">
                        <xsl:with-param name="content" select="'Back to Summary'" />
                        <xsl:with-param name="className" select="'Form-button'"/>
                        <xsl:with-param name="url">/bookpub/tracker</xsl:with-param>
                    </xsl:call-template>
                </fieldset>
            </div>
        </div>
    </xsl:template>

</xsl:stylesheet>
