﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">

    <xsl:import href="EmailGenericTemplate.xslt"/>

    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
        <xsl:variable name="SalesUserName" select="CRMOpportunities/CRMOpportunity/DisplayName"></xsl:variable>
        <xsl:variable name="SalesUserFirstName" select="CRMOpportunities/CRMOpportunity/FirstName"></xsl:variable>
        <xsl:variable name="PortalName" select="CRMOpportunities/CRMOpportunity/MasterPortalName"></xsl:variable>
        <xsl:variable name="LogoURL">
            <xsl:choose>
                <xsl:when test="CRMOpportunities/MasterBrandSetting[SettingKey = 'PipelineNotificationLogoURLPath']/SettingValue[text()]">
                    <xsl:value-of select="CRMOpportunities/MasterBrandSetting[SettingKey = 'PipelineNotificationLogoURLPath']/SettingValue"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="CRMOpportunities/MasterBrandSetting[SettingKey = 'NotificationLogoURLPath']/SettingValue"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:variable name="JobDate" select="CRMOpportunities/JobDate"></xsl:variable>
        <xsl:variable name="MasterBrandTargetURL" select="CRMOpportunities/MasterBrandSetting/TargetURL"></xsl:variable>
        <xsl:variable name="FullWidthHeaderLogo">
            <xsl:choose>
                <xsl:when test="CRMOpportunities/MasterBrandSetting[SettingKey = 'PipelineNotificationFullWidthHeaderLogo']/SettingValue[text()]">
                    <xsl:value-of select="CRMOpportunities/MasterBrandSetting[SettingKey = 'PipelineNotificationFullWidthHeaderLogo']/SettingValue"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:number value="0"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:call-template name="EmailGenericTemplate">
            <xsl:with-param name="SalesUserName" select="$SalesUserFirstName"></xsl:with-param>
            <xsl:with-param name="PortalName" select="$PortalName"></xsl:with-param>
            <xsl:with-param name="LogoURL" select="$LogoURL"></xsl:with-param>
            <xsl:with-param name="MasterBrandTargetURL" select="$MasterBrandTargetURL"></xsl:with-param>

            <xsl:with-param name="ContentHTML">
                <xsl:call-template name="EmailBodyContent">
                    <xsl:with-param name="JobDate" select="$JobDate"></xsl:with-param>
                    <xsl:with-param name="CRMOpportunities" select="CRMOpportunities/."></xsl:with-param>
                    <xsl:with-param name="MasterBrandTargetURL" select="$MasterBrandTargetURL"></xsl:with-param>
                </xsl:call-template>
            </xsl:with-param>

            <xsl:with-param name="FullWidthHeaderLogo" select="$FullWidthHeaderLogo"></xsl:with-param>
        </xsl:call-template>

    </xsl:template>

    <xsl:template name="EmailBodyContent">
        <xsl:param name="JobDate" select="'N/A'"></xsl:param>
        <xsl:param name="CRMOpportunities" select="'N/A'"></xsl:param>
        <xsl:param name="MasterBrandTargetURL" select="'N/A'"></xsl:param>

        <div style="border: none; border-bottom: none; padding: 0in 0in 2.0pt 0in;margin-bottom: 5.5pt;">

            <span style="font-size: 11.5pt; font-family: Arial,sans-serif; color: #6D6E71; font-weight: normal;">
                Below please find your pipeline summary for <xsl:value-of select="$JobDate"/>
            </span>

        </div>
        <div style="border: none; border-bottom: none; padding: 0in 0in 2.0pt 0in">
            <h1 style="margin-right: 0in; margin-top: 10pt; margin-bottom: 0pt; margin-left: 0in; border: none;padding: 0in">
                <span style="font-size: 11.5pt; font-family: Arial,sans-serif; color: #6D6E71; font-weight: normal">
                    You can click on any section below to view/edit your pipeline
                </span>
            </h1>
        </div>
        <xsl:call-template name="TemplateOpportunityBlockHeader">
            <xsl:with-param name="HeaderText">
                <xsl:text>Opportunities added this week</xsl:text>
            </xsl:with-param>
        </xsl:call-template>

        <xsl:choose>
            <xsl:when test="count($CRMOpportunities/CRMOpportunity[(CreatedDiffDate &lt;= 7 or CurrentDiffDays &lt;= 7) and OppCreatedModified = 'Created']) = 0">
                <div style="border: none; border-bottom: none; padding: 0in 0in 2.0pt 0.1in">
                    <a href="{$MasterBrandTargetURL}?returnurl={$MasterBrandTargetURL}/Home.aspx?Panel=MyPipeline"
                       style="font-size: 10.5pt; font-family: Arial,sans-serif; color: #6D6E71; font-weight: normal; text-decoration:none;">
                        <span style="font-size: 10.5pt; font-family: Arial,sans-serif; color: #6D6E71; font-weight: normal;text-decoration:none;">
                            <xsl:text>0 Added</xsl:text>
                        </span>
                    </a>
                </div>
            </xsl:when>
            <xsl:otherwise>
                <xsl:for-each select="$CRMOpportunities/CRMOpportunity[(CreatedDiffDate &lt;= 7 or CurrentDiffDays &lt;= 7) and OppCreatedModified = 'Created']">
                    <xsl:call-template name="TemplateOpportunityDisplay">
                        <xsl:with-param name="ClientCompany" select="ClientCompany"></xsl:with-param>
                        <xsl:with-param name="OpportunityName" select="OpportunityName"></xsl:with-param>
                        <xsl:with-param name="StatusDisplayValue" select="StatusDisplayValue"></xsl:with-param>
                        <xsl:with-param name="DisplayUpdateDate" select="DisplayUpdateDate"></xsl:with-param>
                        <xsl:with-param name="TargetURL" select="TargetURL"></xsl:with-param>
                        <xsl:with-param name="OppCreatedModified" select="OppCreatedModified"></xsl:with-param>
                    </xsl:call-template>
                </xsl:for-each>
            </xsl:otherwise>
        </xsl:choose>

        <xsl:call-template name="TemplateOpportunityBlockHeader">
            <xsl:with-param name="HeaderText">
                <xsl:text>Opportunities modified this week</xsl:text>
            </xsl:with-param>
        </xsl:call-template>

        <xsl:choose>
            <xsl:when test="count($CRMOpportunities/CRMOpportunity[(CreatedDiffDate &lt;= 7 or CurrentDiffDays &lt;= 7) and OppCreatedModified = 'Modified']) = 0">
                <div style="border: none; border-bottom: none; padding: 0in 0in 2.0pt 0.1in">
                    <a href="{$MasterBrandTargetURL}?returnurl={$MasterBrandTargetURL}/Home.aspx?Panel=MyPipeline"
                       style="font-size: 10.5pt; font-family: Arial,sans-serif; color: #6D6E71; font-weight: normal; text-decoration:none;">
                        <span style="font-size: 10.5pt; font-family: Arial,sans-serif; color: #6D6E71; font-weight: normal;text-decoration:none;">
                            <xsl:text>0 Modified</xsl:text>
                        </span>
                    </a>
                </div>
            </xsl:when>
            <xsl:otherwise>
                <xsl:for-each select="$CRMOpportunities/CRMOpportunity[(CreatedDiffDate &lt;= 7 or CurrentDiffDays &lt;= 7) and OppCreatedModified = 'Modified']">
                    <xsl:call-template name="TemplateOpportunityDisplay">
                        <xsl:with-param name="ClientCompany" select="ClientCompany"></xsl:with-param>
                        <xsl:with-param name="OpportunityName" select="OpportunityName"></xsl:with-param>
                        <xsl:with-param name="StatusDisplayValue" select="StatusDisplayValue"></xsl:with-param>
                        <xsl:with-param name="DisplayUpdateDate" select="DisplayUpdateDate"></xsl:with-param>
                        <xsl:with-param name="TargetURL" select="TargetURL"></xsl:with-param>
                    </xsl:call-template>
                </xsl:for-each>
            </xsl:otherwise>
        </xsl:choose>        
        
        <xsl:call-template name="TemplateOpportunityBlockHeader">
            <xsl:with-param name="HeaderText">
                <xsl:text>Opportunities with target dates that are past due</xsl:text>
            </xsl:with-param>
        </xsl:call-template>
        <xsl:choose>
            <xsl:when test="count($CRMOpportunities/CRMOpportunity[TotalDaysOverToCloseDate &gt;= 0]) = 0">
                <div style="border: none; border-bottom: none; padding: 0in 0in 2.0pt 0.1in">
                    <a href="{$MasterBrandTargetURL}?returnurl={$MasterBrandTargetURL}/Home.aspx?Panel=MyPipeline"
                       style="font-size: 10.5pt; font-family: Arial,sans-serif; color: #6D6E71; font-weight: normal; text-decoration:none;">
                        <span style="font-size: 10.5pt; font-family: Arial,sans-serif; color: #6D6E71; font-weight: normal; text-decoration:none;">
                            <xsl:text> 0 Opportunities are past due</xsl:text>
                        </span>
                    </a>
                </div>
            </xsl:when>
            <xsl:otherwise>
                <xsl:for-each select="$CRMOpportunities/CRMOpportunity[TotalDaysOverToCloseDate &gt;= 0]">
                    <xsl:call-template name="TemplateOpportunityDisplay">
                        <xsl:with-param name="ClientCompany" select="ClientCompany"></xsl:with-param>
                        <xsl:with-param name="OpportunityName" select="OpportunityName"></xsl:with-param>
                        <xsl:with-param name="StatusDisplayValue" select="StatusDisplayValue"></xsl:with-param>
                        <xsl:with-param name="DisplayUpdateDate" select="DisplayUpdateDate"></xsl:with-param>
                        <xsl:with-param name="TargetURL" select="TargetURL"></xsl:with-param>
                    </xsl:call-template>
                </xsl:for-each>
            </xsl:otherwise>
        </xsl:choose>
        <xsl:call-template name="TemplateOpportunityBlockHeader">
            <xsl:with-param name="HeaderText">
                <xsl:text>Opportunities that have not changed in 30 days</xsl:text>
            </xsl:with-param>
        </xsl:call-template>
        <xsl:choose>
            <xsl:when test="count($CRMOpportunities/CRMOpportunity[CurrentDiffDays &gt;= 30 and CurrentDiffDays &lt;= 60]) = 0">
                <div style="border: none; border-bottom: none; padding: 0in 0in 2.0pt 0.1in">
                    <a href="{$MasterBrandTargetURL}?returnurl={$MasterBrandTargetURL}/Home.aspx?Panel=MyPipeline"
                       style="font-size: 10.5pt; font-family: Arial,sans-serif; color: #6D6E71; font-weight: normal; text-decoration:none;">
                        <span style="font-size: 10.5pt; font-family: Arial,sans-serif; color: #6D6E71; font-weight: normal;  text-decoration:none;">
                            <xsl:text>0 Unchanged</xsl:text>
                        </span>
                    </a>
                </div>
            </xsl:when>
            <xsl:otherwise>
                <xsl:for-each select="$CRMOpportunities/CRMOpportunity[CurrentDiffDays &gt;= 30 and CurrentDiffDays &lt;= 60]">
                    <xsl:call-template name="TemplateOpportunityDisplay">
                        <xsl:with-param name="ClientCompany" select="ClientCompany"></xsl:with-param>
                        <xsl:with-param name="OpportunityName" select="OpportunityName"></xsl:with-param>
                        <xsl:with-param name="StatusDisplayValue" select="StatusDisplayValue"></xsl:with-param>
                        <xsl:with-param name="DisplayUpdateDate" select="DisplayUpdateDate"></xsl:with-param>
                        <xsl:with-param name="TargetURL" select="TargetURL"></xsl:with-param>
                    </xsl:call-template>
                </xsl:for-each>
            </xsl:otherwise>
        </xsl:choose>
        <xsl:call-template name="TemplateOpportunityBlockHeader">
            <xsl:with-param name="HeaderText">
                <xsl:text>Opportunities that have not changed in 60 days</xsl:text>
            </xsl:with-param>
        </xsl:call-template>
        <xsl:choose>
            <xsl:when test="count($CRMOpportunities/CRMOpportunity[CurrentDiffDays &gt;= 60]) = 0">
                <div style="border: none; border-bottom: none; padding: 0in 0in 2.0pt 0.1in">
                    <a href="{$MasterBrandTargetURL}?returnurl={$MasterBrandTargetURL}/Home.aspx?Panel=MyPipeline"
                       style="font-size: 10.5pt; font-family: Arial,sans-serif; color: #6D6E71; font-weight: normal; text-decoration:none;">
                        <span style="font-size: 10.5pt; font-family: Arial,sans-serif; color: #6D6E71; font-weight: normal;  text-decoration:none;">
                            <xsl:text>0 Unchanged</xsl:text>
                        </span>
                    </a>
                </div>
            </xsl:when>
            <xsl:otherwise>
                <xsl:for-each select="$CRMOpportunities/CRMOpportunity[CurrentDiffDays &gt;= 60]">
                    <xsl:call-template name="TemplateOpportunityDisplay">
                        <xsl:with-param name="ClientCompany" select="ClientCompany"></xsl:with-param>
                        <xsl:with-param name="OpportunityName" select="OpportunityName"></xsl:with-param>
                        <xsl:with-param name="StatusDisplayValue" select="StatusDisplayValue"></xsl:with-param>
                        <xsl:with-param name="DisplayUpdateDate" select="DisplayUpdateDate"></xsl:with-param>
                        <xsl:with-param name="TargetURL" select="TargetURL"></xsl:with-param>
                    </xsl:call-template>
                </xsl:for-each>
            </xsl:otherwise>
        </xsl:choose>
        <div style="border: none; border-bottom: none; padding: 0in 0in 2.0pt 0in;margin-right: 0in; margin-top: 25pt; margin-bottom: 0pt; margin-left: 0in; border: none;padding: 0in;">
            <span style="font-size: 11.5pt; font-family: Arial,sans-serif; color: #6D6E71; font-weight: normal;">
                Don’t forget, your pipeline is your business bloodline so review it regularly and keep it up to date.
            </span>
        </div>
        <div style="border: none; border-bottom: none; padding: 0in 0in 2.0pt 0in;margin-right: 0in; margin-top: 25pt; margin-bottom: 0pt; margin-left: 0in; border: none;padding: 0in;">

            <span style="font-size: 11.5pt; font-family: Arial,sans-serif; color: #6D6E71; font-weight: normal; text-decoration:none;">
                Also, if you have not already done so, 
            </span>
            <a href="{$MasterBrandTargetURL}"
               style="font-size: 10.5pt; font-family: Arial,sans-serif; color: #2518B5; font-weight: normal; text-decoration:none;">
                <span style="font-size: 11.5pt; font-family: Arial,sans-serif; color: #2518B5; font-weight: normal; text-decoration:none;">
                    please log in and post your weekly metrics,
                </span>
            </a>
            <span style="font-size: 11.5pt; font-family: Arial,sans-serif; color: #6D6E71; font-weight: normal; text-decoration:none;">
                check off completed action items and enter update comments on each of your respective goals.
            </span>
        </div>
    </xsl:template>

    <xsl:template name="TemplateOpportunityDisplay">
        <xsl:param name="ClientCompany" select="'N/A'"></xsl:param>
        <xsl:param name="OpportunityName" select="'N/A'"></xsl:param>
        <xsl:param name="StatusDisplayValue" select="'N/A'"></xsl:param>
        <xsl:param name="DisplayUpdateDate" select="'N/A'"></xsl:param>
        <xsl:param name="TargetURL" select="'N/A'"></xsl:param>
        <xsl:param name="OppCreatedModified" select="'N/A'"></xsl:param>

        <div style="border: none; border-bottom: none; padding: 0in 0in 2.0pt 0.1in">
            <a href="{$TargetURL}?returnurl={$TargetURL}/Home.aspx?Panel=MyPipeline"
               style="font-size: 10.5pt; font-family: Arial,sans-serif; color: #6D6E71; font-weight: normal; text-decoration:none;">
                <span style="font-size: 10.5pt; font-family: Arial,sans-serif; color: #6D6E71; font-weight: normal; text-decoration:none;">
                    <xsl:choose>
                        <xsl:when test="$DisplayUpdateDate = 'N/A' or $DisplayUpdateDate = '' or not($DisplayUpdateDate) or $OppCreatedModified = 'Created'">
                            <xsl:value-of select="$ClientCompany"/> - <xsl:value-of select="$OpportunityName"/> - <xsl:value-of select="$StatusDisplayValue"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="$ClientCompany"/> - <xsl:value-of select="$OpportunityName"/> - <xsl:value-of select="$StatusDisplayValue"/> - Modified : <xsl:value-of select="$DisplayUpdateDate"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </span>
            </a>
        </div>
    </xsl:template>

    <xsl:template name="TemplateOpportunityBlockHeader">
        <xsl:param name="HeaderText" select="'N/A'"></xsl:param>

        <div style="border: none; border-bottom: none; padding: 0in 0in 2.0pt 0in">
            <h1 style="margin-right: 0in; margin-bottom: 5.5pt; margin-left: 0in; border: none;padding: 0in">
                <span style="font-size: 11.5pt; font-family: Arial,sans-serif; color: #6D6E71; font-weight: normal; text-decoration:underline;">
                    <xsl:value-of select="$HeaderText"/>
                </span>
            </h1>
        </div>
    </xsl:template>

</xsl:stylesheet>
