<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<entitymodel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/entitymodel.xsd">
    <!-- ========================================================= -->
    <!-- ======================== Defaults ======================= -->
    <!-- ========================================================= -->
    <title>Entity of an Apache OFBiz Component</title>
    <description>None</description>
    <version>1.0</version>

    <!-- ========================================================= -->
    <!-- ======================== Data Model ===================== -->
    <!-- The modules in this file are as follows:                  -->
    <!--  - org.apache.ofbiz.marketing.campaign -->
    <!--  - org.apache.ofbiz.marketing.contact -->
    <!--  - org.apache.ofbiz.marketing.segment -->
    <!--  - org.apache.ofbiz.marketing.tracking -->
    <!--  - org.apache.ofbiz.marketing.opportunity -->
    <!-- ========================================================= -->

    <!-- ========================================================= -->
    <!-- org.apache.ofbiz.marketing.campaign -->
    <!-- ========================================================= -->

    <entity entity-name="MarketingCampaign"
            package-name="org.apache.ofbiz.marketing.campaign"
            title="Marketing Campaign">
        <field name="marketingCampaignId" type="id"/>
        <field name="parentCampaignId" type="id"/>
        <field name="statusId" type="id"/>
        <field name="campaignName" type="name"/>
        <field name="campaignSummary" type="very-long"/>
        <field name="budgetedCost" type="currency-amount"/>
        <field name="actualCost" type="currency-amount"/>
        <field name="estimatedCost" type="currency-amount"/>
        <field name="currencyUomId" type="id"/>
        <field name="fromDate" type="date-time"/>
        <field name="thruDate" type="date-time"/>
        <field name="isActive" type="indicator"/>
        <field name="convertedLeads" type="id"/>
        <field name="expectedResponsePercent" type="floating-point"/>
        <field name="expectedRevenue" type="currency-amount"/>
        <field name="numSent" type="numeric"/>
        <field name="startDate" type="date-time"/>
        <field name="createdByUserLogin" type="id-vlong"/>
        <field name="lastModifiedByUserLogin" type="id-vlong"/>
        <prim-key field="marketingCampaignId"/>
        <relation type="one" fk-name="MKTGCPN_PRNT" title="Parent" rel-entity-name="MarketingCampaign">
            <key-map field-name="parentCampaignId" rel-field-name="marketingCampaignId"/>
        </relation>
        <relation type="one" fk-name="MKTGCPN_STS" rel-entity-name="StatusItem">
            <key-map field-name="statusId"/>
        </relation>
        <relation type="one" fk-name="MKTGCPN_CUOM" rel-entity-name="Uom">
            <key-map field-name="currencyUomId" rel-field-name="uomId"/>
        </relation>
    </entity>
    <entity entity-name="MarketingCampaignNote"
            package-name="org.apache.ofbiz.marketing.campaign"
            title="Marketing Campaign Note">
        <field name="marketingCampaignId" type="id"/>
        <field name="noteId" type="id"/>
        <prim-key field="marketingCampaignId"/>
        <prim-key field="noteId"/>
        <relation type="one" fk-name="MKTGCPN_NOTE_CMPN" rel-entity-name="MarketingCampaign">
            <key-map field-name="marketingCampaignId"/>
        </relation>
        <relation type="one" fk-name="MKTGCPN_NOTE_NOTE" rel-entity-name="NoteData">
            <key-map field-name="noteId"/>
        </relation>
    </entity>
    <entity entity-name="MarketingCampaignPrice"
            package-name="org.apache.ofbiz.marketing.campaign"
            title="Marketing Campaign Price">
        <field name="marketingCampaignId" type="id"/>
        <field name="productPriceRuleId" type="id"/>
        <field name="fromDate" type="date-time"/>
        <field name="thruDate" type="date-time"/>
        <prim-key field="marketingCampaignId"/>
        <prim-key field="productPriceRuleId"/>
        <prim-key field="fromDate"/>
        <relation type="one" fk-name="MKTGCPN_PRICE_MC" rel-entity-name="MarketingCampaign">
            <key-map field-name="marketingCampaignId"/>
        </relation>
        <relation type="one" fk-name="MKTGCPN_PRICE_PP" rel-entity-name="ProductPriceRule">
            <key-map field-name="productPriceRuleId"/>
        </relation>
    </entity>
    <entity entity-name="MarketingCampaignPromo"
            package-name="org.apache.ofbiz.marketing.campaign"
            title="Marketing Campaign Promo">
        <field name="marketingCampaignId" type="id"/>
        <field name="productPromoId" type="id"/>
        <field name="fromDate" type="date-time"/>
        <field name="thruDate" type="date-time"/>
        <prim-key field="marketingCampaignId"/>
        <prim-key field="productPromoId"/>
        <prim-key field="fromDate"/>
        <relation type="one" fk-name="MKTGCPN_PROMO_MC" rel-entity-name="MarketingCampaign">
            <key-map field-name="marketingCampaignId"/>
        </relation>
        <relation type="one" fk-name="MKTGCPN_PROMO_PP" rel-entity-name="ProductPromo">
            <key-map field-name="productPromoId"/>
        </relation>
    </entity>
    <entity entity-name="MarketingCampaignRole"
            package-name="org.apache.ofbiz.marketing.campaign"
            title="Marketing Campaign Role">
        <field name="marketingCampaignId" type="id"/>
        <field name="partyId" type="id"/>
        <field name="roleTypeId" type="id"/>
        <field name="fromDate" type="date-time"/>
        <field name="thruDate" type="date-time"/>
        <prim-key field="marketingCampaignId"/>
        <prim-key field="partyId"/>
        <prim-key field="roleTypeId"/>
        <prim-key field="fromDate"/>
        <relation type="one" fk-name="MKTGCPN_ROLE_MC" rel-entity-name="MarketingCampaign">
            <key-map field-name="marketingCampaignId"/>
        </relation>
        <relation type="one" fk-name="MKTGCPN_ROLE_PR" rel-entity-name="PartyRole">
            <key-map field-name="partyId"/>
            <key-map field-name="roleTypeId"/>
        </relation>
        <relation type="one-nofk" rel-entity-name="Party">
            <key-map field-name="partyId"/>
        </relation>
        <relation type="one-nofk" rel-entity-name="RoleType">
            <key-map field-name="roleTypeId"/>
        </relation>
    </entity>

    <!-- ========================================================= -->
    <!-- org.apache.ofbiz.marketing.contact -->
    <!-- ========================================================= -->

    <entity entity-name="ContactList"
            package-name="org.apache.ofbiz.marketing.contact"
            title="Contact List">
        <field name="contactListId" type="id"/>
        <field name="contactListTypeId" type="id"/>
        <field name="contactMechTypeId" type="id"/>
        <field name="marketingCampaignId" type="id"/>
        <field name="contactListName" type="name"/>
        <field name="description" type="description"/>
        <field name="comments" type="comment"/>
        <field name="isPublic" type="indicator"/>
        <field name="singleUse" type="indicator"><description>Whether members of the list should be contacted only once.</description></field>
        <field name="ownerPartyId" type="id"/>
        <field name="verifyEmailFrom" type="long-varchar"/>
        <field name="verifyEmailScreen" type="long-varchar"/>
        <field name="verifyEmailSubject" type="long-varchar"/>
        <field name="verifyEmailWebSiteId" type="id"/>
        <field name="optOutScreen" type="long-varchar"/>
        <field name="createdByUserLogin" type="id-vlong"/>
        <field name="lastModifiedByUserLogin" type="id-vlong"/>
        <prim-key field="contactListId"/>
        <relation type="one" fk-name="CNCT_LST_MKCMPN" rel-entity-name="MarketingCampaign">
            <key-map field-name="marketingCampaignId"/>
        </relation>
        <relation type="one" fk-name="CNCT_LST_TYP" rel-entity-name="ContactListType">
            <key-map field-name="contactListTypeId"/>
        </relation>
        <relation type="one" fk-name="CNCT_LST_CMCHTP" rel-entity-name="ContactMechType">
            <key-map field-name="contactMechTypeId"/>
        </relation>
        <relation type="one" fk-name="CNCT_LST_CBUL" title="CreatedBy" rel-entity-name="UserLogin">
            <key-map field-name="createdByUserLogin" rel-field-name="userLoginId"/>
        </relation>
        <relation type="one" fk-name="CNCT_LST_LMUL" title="LastModifiedBy" rel-entity-name="UserLogin">
            <key-map field-name="lastModifiedByUserLogin" rel-field-name="userLoginId"/>
        </relation>
        <relation type="one" fk-name="CNCT_LST_OPTY" title="Owner" rel-entity-name="Party">
            <key-map field-name="ownerPartyId" rel-field-name="partyId"/>
        </relation>
    </entity>
    <entity entity-name="WebSiteContactList" 
            package-name="org.apache.ofbiz.marketing.contact"
            title="Web Site Contact List">
        <field name="webSiteId" type="id"/>
        <field name="contactListId" type="id"/>
        <field name="fromDate" type="date-time"/>
        <field name="thruDate" type="date-time"/>
        <prim-key field="webSiteId"/>
        <prim-key field="contactListId"/>
        <prim-key field="fromDate"/>
        <relation type="one" fk-name="WEB_SITE_CNTCT_LST" rel-entity-name="WebSite">
            <key-map field-name="webSiteId"/>
        </relation>
        <relation type="one" fk-name="CNTCT_LST_WEB_SITE" rel-entity-name="ContactList">
            <key-map field-name="contactListId"/>
        </relation>
    </entity>
    <entity entity-name="ContactListCommStatus"
            package-name="org.apache.ofbiz.marketing.contact"
            title="Contact List">
        <field name="contactListId" type="id"/>
        <field name="communicationEventId" type="id"/>
        <field name="contactMechId" type="id"/>
        <field name="partyId" type="id"/>
        <field name="messageId" type="value"/>
        <field name="statusId" type="id"/>
        <field name="changeByUserLoginId" type="id-vlong"/>
        <prim-key field="contactListId"/>
        <prim-key field="communicationEventId"/>
        <prim-key field="contactMechId"/>
        <relation type="one" fk-name="CNCT_LST_CST_CL" rel-entity-name="ContactList">
            <key-map field-name="contactListId"/>
        </relation>
        <relation type="one" fk-name="CNCT_LST_CST_CE" rel-entity-name="CommunicationEvent">
            <key-map field-name="communicationEventId"/>
        </relation>
        <relation type="one" fk-name="CNCT_LST_CST_CM" rel-entity-name="ContactMech">
            <key-map field-name="contactMechId"/>
        </relation>
        <relation type="one" fk-name="CNCT_LST_CST_PT" rel-entity-name="Party">
            <key-map field-name="partyId"/>
        </relation>
        <relation type="one" fk-name="CNCT_LST_CST_ST" rel-entity-name="StatusItem">
            <key-map field-name="statusId"/>
        </relation>
        <relation type="one" fk-name="CNCT_LST_CST_ST_UL" title="ChangeBy" rel-entity-name="UserLogin">
            <key-map field-name="changeByUserLoginId" rel-field-name="userLoginId"/>
        </relation>
        <index name="CNTLSTCST_MSG_ID" unique="true">
            <index-field name="messageId"/>
        </index>
    </entity>
    <entity entity-name="ContactListParty"
            package-name="org.apache.ofbiz.marketing.contact"
            title="Contact List Party">
        <field name="contactListId" type="id"/>
        <field name="partyId" type="id"/>
        <field name="fromDate" type="date-time"/>
        <field name="thruDate" type="date-time"/>
        <field name="statusId" type="id"/>
        <field name="preferredContactMechId" type="id"/>
        <prim-key field="contactListId"/>
        <prim-key field="partyId"/>
        <prim-key field="fromDate"/>
        <relation type="one" fk-name="CNCT_LSTPTY_CLST" rel-entity-name="ContactList">
            <key-map field-name="contactListId"/>
        </relation>
        <relation type="one" fk-name="CNCT_LSTPTY_PTY" rel-entity-name="Party">
            <key-map field-name="partyId"/>
        </relation>
        <relation type="one" fk-name="CNCT_LSTPTY_STS" rel-entity-name="StatusItem">
            <key-map field-name="statusId"/>
        </relation>
        <relation type="one" fk-name="CNCT_LSTPTY_PCM" title="Preferred" rel-entity-name="ContactMech">
            <key-map field-name="preferredContactMechId" rel-field-name="contactMechId"/>
        </relation>
    </entity>
    <view-entity entity-name="ContactListPartyAndStatus"
                 package-name="org.apache.ofbiz.marketing.contact">
        <member-entity entity-alias="CLPS" entity-name="ContactListPartyStatus"/>
        <member-entity entity-alias="SI" entity-name="StatusItem"/>
        <alias-all entity-alias="CLPS"/>
        <alias-all entity-alias="SI"/>
        <view-link entity-alias="CLPS" rel-entity-alias="SI">
            <key-map field-name="statusId"/>
        </view-link>
    </view-entity>
    <entity entity-name="ContactListPartyStatus"
            package-name="org.apache.ofbiz.marketing.contact"
            title="Contact List Party Status">
        <field name="contactListId" type="id"/>
        <field name="partyId" type="id"/>
        <field name="fromDate" type="date-time"/>
        <field name="statusDate" type="date-time"/>
        <field name="statusId" type="id"/>
        <field name="setByUserLoginId" type="id-vlong"/>
        <field name="optInVerifyCode" type="short-varchar"/><!-- if statusId is CLPT_PENDING, set optInVerifyCode; else if statusId is CLPT_ACCEPTED, make sure optInVerifyCode is the same as the last CLPT_PENDING -->
        <prim-key field="contactListId"/>
        <prim-key field="partyId"/>
        <prim-key field="fromDate"/>
        <prim-key field="statusDate"/>
        <relation type="one" fk-name="CTLSTPTST_CLP" rel-entity-name="ContactListParty">
            <key-map field-name="contactListId"/>
            <key-map field-name="partyId"/>
            <key-map field-name="fromDate"/>
        </relation>
    </entity>
    <entity entity-name="ContactListType"
            package-name="org.apache.ofbiz.marketing.contact"
            default-resource-name="MarketingEntityLabels"
            title="Contact List Type">
        <field name="contactListTypeId" type="id"/>
        <field name="description" type="description"/>
        <prim-key field="contactListTypeId"/>
    </entity>

    <!-- ========================================================= -->
    <!-- org.apache.ofbiz.marketing.segment -->
    <!-- ========================================================= -->

    <entity entity-name="SegmentGroup"
            package-name="org.apache.ofbiz.marketing.segment"
            title="Segment Group">
        <field name="segmentGroupId" type="id"/>
        <field name="segmentGroupTypeId" type="id"/>
        <field name="description" type="description"/>
        <field name="productStoreId" type="id"/>
        <prim-key field="segmentGroupId"/>
        <relation type="one" fk-name="SGMTGRP_TYPE" rel-entity-name="SegmentGroupType">
            <key-map field-name="segmentGroupTypeId"/>
        </relation>
        <relation type="one" fk-name="SGMTGRP_PRST" rel-entity-name="ProductStore">
            <key-map field-name="productStoreId"/>
        </relation>
    </entity>
    <entity entity-name="SegmentGroupClassification"
            package-name="org.apache.ofbiz.marketing.segment"
            title="Segment Group Classification">
        <field name="segmentGroupId" type="id"/>
        <field name="partyClassificationGroupId" type="id"/>
        <prim-key field="segmentGroupId"/>
        <prim-key field="partyClassificationGroupId"/>
        <relation type="one" fk-name="SGMTGRPCLS_SGGP" rel-entity-name="SegmentGroup">
            <key-map field-name="segmentGroupId"/>
        </relation>
        <relation type="one" fk-name="SGMTGRPCLS_PCGP" rel-entity-name="PartyClassificationGroup">
            <key-map field-name="partyClassificationGroupId"/>
        </relation>
    </entity>
    <entity entity-name="SegmentGroupGeo"
            package-name="org.apache.ofbiz.marketing.segment"
            title="Segment Group Geo">
        <field name="segmentGroupId" type="id"/>
        <field name="geoId" type="id"/>
        <prim-key field="segmentGroupId"/>
        <prim-key field="geoId"/>
        <relation type="one" fk-name="SGMTGRPGEO_SGGP" rel-entity-name="SegmentGroup">
            <key-map field-name="segmentGroupId"/>
        </relation>
        <relation type="one" fk-name="SGMTGRPGEO_GEO" rel-entity-name="Geo">
            <key-map field-name="geoId"/>
        </relation>
    </entity>
    <entity entity-name="SegmentGroupRole"
            package-name="org.apache.ofbiz.marketing.segment"
            title="Segment Group Role">
        <field name="segmentGroupId" type="id"/>
        <field name="partyId" type="id"/>
        <field name="roleTypeId" type="id"/>
        <prim-key field="segmentGroupId"/>
        <prim-key field="partyId"/>
        <prim-key field="roleTypeId"/>
        <relation type="one" fk-name="SGMTGRPRL_SGGP" rel-entity-name="SegmentGroup">
            <key-map field-name="segmentGroupId"/>
        </relation>
        <relation type="one" fk-name="SGMTGRPRL_PRLE" rel-entity-name="PartyRole">
            <key-map field-name="partyId"/>
            <key-map field-name="roleTypeId"/>
        </relation>
        <relation type="one-nofk" rel-entity-name="Party">
            <key-map field-name="partyId"/>
        </relation>
        <relation type="one-nofk" rel-entity-name="RoleType">
            <key-map field-name="roleTypeId"/>
        </relation>
    </entity>
    <entity entity-name="SegmentGroupType"
            package-name="org.apache.ofbiz.marketing.segment"
            default-resource-name="MarketingEntityLabels"
            title="Segment Group Type">
        <field name="segmentGroupTypeId" type="id"/>
        <field name="description" type="description"/>
        <prim-key field="segmentGroupTypeId"/>
    </entity>
    <view-entity entity-name="SegmentGroupViewRelatedParties" package-name="org.apache.ofbiz.marketing.segment">
        <description>
            This view entity models the following options:
            - SegmentGroupRole(SGR) -> SegmentGroupRole(SGRTO)
            - SegmentGroupRole(SGR) -> SegmentGroupRole(SGRTO) -> PartyRelationship(PRSGR)
            - SegmentGroupRole(SGR) -> SegmentGroupClassification(SGC) -> PartyClassification(PC)
            - SegmentGroupRole(SGR) -> SegmentGroupClassification(SGC) -> PartyClassification(PC) -> PartyRelationship(PRPC)
            Typical fields to constrain:
            - partyId of User -> sgrPartyId (SGR)
            - partyId of Customer -> sgrToPartyId (SGRTO) AND prSgrPartyIdTo (PRSGR) AND pcPartyId (PC) AND prPcPartyIdTo (PRPC)
              NOTE: these 4 partyIds represent the 4 options for entity relationship paths listed above
              NOTE: because not all of these will exist, each needs to be the given partyId(s) OR null
            - roleTypeId of User -> sgrRoleTypeId (SGR) - ex: SALES_REP, MANAGER, etc
            - roleTypeId of Customer -> sgrToRoleTypeId (SGRTO)
              NOTE: because not all of these will exist, each needs to be the given roleTypeId(s) OR null
            - roleTypeId of _Employer_ in Employee/Employer relationship -> prSgrRoleTypeIdFrom (PRSGR) AND prPcRoleTypeIdFrom (PRPC) - INTERNAL_ORGANIZATIO
              NOTE: constraining these fields is optional as the EMPLOYEE roleTypeIdTo is often sufficient
              NOTE: because not all of these will exist, each needs to be the given partyId(s) OR null
            - roleTypeId of _Employee_ in Employee/Employer relationship -> prSgrRoleTypeIdTo (PRSGR) AND prPcRoleTypeIdTo (PRPC) - EMPLOYEE
              NOTE: because not all of these will exist, each needs to be the given partyId(s) OR null
            - partyRelationshipTypeId in Employee/Employer relationship -> prSgrPartyRelationshipTypeId (PRSGR) AND prPcPartyRelationshipTypeId (PRPC)
              NOTE: because not all of these will exist, each needs to be the given partyId(s) OR null
        </description>
        <member-entity entity-alias="SGR" entity-name="SegmentGroupRole"/>
        <member-entity entity-alias="SGRTO" entity-name="SegmentGroupRole"/>
        <member-entity entity-alias="PRSGR" entity-name="PartyRelationship"/>
        <member-entity entity-alias="SGC" entity-name="SegmentGroupClassification"/>
        <member-entity entity-alias="PC" entity-name="PartyClassification"/>
        <member-entity entity-alias="PRPC" entity-name="PartyRelationship"/>
        <alias-all entity-alias="SGR" prefix="sgr"/>
        <alias-all entity-alias="SGRTO" prefix="sgrTo"/>
        <alias-all entity-alias="PRSGR" prefix="prSgr"/>
        <alias-all entity-alias="SGC" prefix="sgc"/>
        <alias-all entity-alias="PC" prefix="pc"/>
        <alias-all entity-alias="PRPC" prefix="prPc"/>
        <view-link entity-alias="SGR" rel-entity-alias="SGRTO" rel-optional="true">
            <key-map field-name="segmentGroupId"/>
        </view-link>
        <view-link entity-alias="SGRTO" rel-entity-alias="PRSGR" rel-optional="true">
            <key-map field-name="partyId" rel-field-name="partyIdFrom"/>
        </view-link>
        <view-link entity-alias="SGR" rel-entity-alias="SGC" rel-optional="true">
            <key-map field-name="segmentGroupId"/>
        </view-link>
        <view-link entity-alias="SGC" rel-entity-alias="PC">
            <key-map field-name="partyClassificationGroupId"/>
        </view-link>
        <view-link entity-alias="PC" rel-entity-alias="PRPC" rel-optional="true">
            <key-map field-name="partyId" rel-field-name="partyIdFrom"/>
        </view-link>
    </view-entity>

    <!-- ========================================================= -->
    <!-- org.apache.ofbiz.marketing.tracking -->
    <!-- ========================================================= -->

    <entity entity-name="TrackingCode"
            package-name="org.apache.ofbiz.marketing.tracking"
            title="Tracking Code">
        <field name="trackingCodeId" type="id"/>
        <field name="trackingCodeTypeId" type="id"/>
        <field name="marketingCampaignId" type="id"/>
        <field name="redirectUrl" type="url"/>
        <field name="overrideLogo" type="url"/>
        <field name="overrideCss" type="url"/>
        <field name="prodCatalogId" type="id"/>
        <field name="comments" type="comment"/>
        <field name="description" type="description"/>
        <field name="trackableLifetime" type="numeric"/>
        <field name="billableLifetime" type="numeric"/>
        <field name="fromDate" type="date-time"/>
        <field name="thruDate" type="date-time"/>
        <field name="groupId" type="id"/>
        <field name="subgroupId" type="id"/>
        <field name="createdDate" type="date-time"/>
        <field name="createdByUserLogin" type="id-vlong"/>
        <field name="lastModifiedDate" type="date-time"/>
        <field name="lastModifiedByUserLogin" type="id-vlong"/>
        <prim-key field="trackingCodeId"/>
        <relation type="one" fk-name="TKNG_COD_MKCMPN" rel-entity-name="MarketingCampaign">
            <key-map field-name="marketingCampaignId"/>
        </relation>
        <relation type="one" fk-name="TKNG_COD_TYP" rel-entity-name="TrackingCodeType">
            <key-map field-name="trackingCodeTypeId"/>
        </relation>
    </entity>
    <entity entity-name="TrackingCodeOrder"
            package-name="org.apache.ofbiz.marketing.tracking"
            title="Tracking Code Visit">
        <field name="orderId" type="id"/>
        <field name="trackingCodeTypeId" type="id"/>
        <field name="trackingCodeId" type="id"/>
        <field name="isBillable" type="indicator"/>
        <field name="siteId" type="long-varchar"/>
        <field name="hasExported" type="indicator"/>
        <field name="affiliateReferredTimeStamp" type="date-time"/>
        <prim-key field="orderId"/>
        <prim-key field="trackingCodeTypeId"/>
        <relation type="one" fk-name="TKNG_CODODR_ODR" rel-entity-name="OrderHeader">
            <key-map field-name="orderId"/>
        </relation>
        <relation type="one" fk-name="TKNG_CODODR_TKCD" rel-entity-name="TrackingCode">
            <key-map field-name="trackingCodeId"/>
        </relation>
        <relation type="one" fk-name="TKNG_CODODR_TYP" rel-entity-name="TrackingCodeType">
            <key-map field-name="trackingCodeTypeId"/>
        </relation>
    </entity>
    <entity entity-name="TrackingCodeOrderReturn"
            package-name="org.apache.ofbiz.marketing.tracking"
            title="Tracking Code And Order Return">
        <field name="returnId" type="id"/>
        <field name="orderId" type="id"/>
        <field name="orderItemSeqId" type="id"/>
        <field name="trackingCodeTypeId" type="id"/>
        <field name="trackingCodeId" type="id"/>
        <field name="isBillable" type="indicator"/>
        <field name="siteId" type="long-varchar"/>
        <field name="hasExported" type="indicator"/>
        <field name="affiliateReferredTimeStamp" type="date-time"/>
        <prim-key field="returnId"/>
        <prim-key field="orderId"/>
        <prim-key field="trackingCodeTypeId"/>
        <relation type="one" fk-name="TKNG_CODODR_RTN" rel-entity-name="ReturnHeader">
            <key-map field-name="returnId"/>
        </relation>
        <relation type="one" fk-name="TKNG_CODODR_ODRTN" rel-entity-name="OrderHeader">
            <key-map field-name="orderId"/>
        </relation>
        <relation type="one" fk-name="TKNG_CODODR_RTNTCD" rel-entity-name="TrackingCode">
            <key-map field-name="trackingCodeId"/>
        </relation>
        <relation type="one" fk-name="TKNG_CODODR_RTNTYP" rel-entity-name="TrackingCodeType">
            <key-map field-name="trackingCodeTypeId"/>
        </relation>
    </entity>
    <entity entity-name="TrackingCodeType"
            package-name="org.apache.ofbiz.marketing.tracking"
            default-resource-name="MarketingEntityLabels"
            title="Tracking Code Type">
        <field name="trackingCodeTypeId" type="id"/>
        <field name="description" type="description"/>
        <prim-key field="trackingCodeTypeId"/>
    </entity>
    <entity entity-name="TrackingCodeVisit"
            package-name="org.apache.ofbiz.marketing.tracking"
            title="Tracking Code Visit">
        <field name="trackingCodeId" type="id"/>
        <field name="visitId" type="id"/>
        <field name="fromDate" type="date-time"/>
        <field name="sourceEnumId" type="id"/>
        <prim-key field="trackingCodeId"/>
        <prim-key field="visitId"/>
        <prim-key field="fromDate"/>
        <relation type="one" fk-name="TKNG_CODVST_TKCD" rel-entity-name="TrackingCode">
            <key-map field-name="trackingCodeId"/>
        </relation>
        <relation type="one" fk-name="TKNG_CODVST_SRCEM" rel-entity-name="Enumeration">
            <key-map field-name="sourceEnumId" rel-field-name="enumId"/>
        </relation>
    </entity>

    <!-- ========================================================= -->
    <!-- org.apache.ofbiz.marketing.reports -->
    <!-- ========================================================= -->

    <view-entity entity-name="TrackingCodeAndVisit"
                 package-name="org.apache.ofbiz.marketing.reports"
                 title="View entity for reporting number of visits for a tracking code">
        <member-entity entity-alias="TC" entity-name="TrackingCode"/>
        <member-entity entity-alias="TCV" entity-name="TrackingCodeVisit"/>
        <alias entity-alias="TC" name="trackingCodeId" group-by="true"/>
        <alias entity-alias="TCV" name="visitId" function="count"/>
        <!-- this means we want to use TrackingCodeVisit.fromDate but not group-by it.  because of this, we cannot have fromDate as one of the fields
            of a select on this view-entity.  this means we must explicitly specify the fields to select by, and the "view all" button in webtools won't
            work.  but it is important to have this date here: it can be a condition of searches. -->
        <alias entity-alias="TCV" name="fromDate" group-by="false"/>
        <!-- the sequence of view-links is important.  if reversed here, it means we won't see tracking codes for which there were no visits -->
        <view-link entity-alias="TC" rel-entity-alias="TCV" rel-optional="true">
            <key-map field-name="trackingCodeId"/>
        </view-link>
    </view-entity>
    <view-entity entity-name="TrackingCodeAndOrderHeader"
                 package-name="org.apache.ofbiz.marketing.reports"
                 title="View entity for reporting number of orders and total order amounts">
        <member-entity entity-alias="TCO" entity-name="TrackingCodeOrder"/>
        <member-entity entity-alias="OH" entity-name="OrderHeader"/>
        <alias entity-alias="OH" name="grandTotal" function="sum"/>
        <alias entity-alias="TCO" name="orderId" function="count"/>
        <alias entity-alias="OH" name="orderDate" group-by="false"/>
        <alias entity-alias="TCO" name="trackingCodeId" group-by="true"/>
        <view-link entity-alias="TCO" rel-entity-alias="OH">
            <key-map field-name="orderId"/>
        </view-link>
    </view-entity>
    <view-entity entity-name="TrackingCodeOrderAndOrderHeader"
                 package-name="org.apache.ofbiz.marketing.reports"
                 never-cache="true"
                 title="Order Header And Tracking Code Order View">
        <member-entity entity-alias="TCO" entity-name="TrackingCodeOrder"/>
        <member-entity entity-alias="OH" entity-name="OrderHeader"/>
        <alias entity-alias="TCO" name="orderId"/>
        <alias entity-alias="TCO" name="trackingCodeId"/>
        <alias entity-alias="TCO" name="siteId"/>
        <alias entity-alias="TCO" name="hasExported"/>
        <alias entity-alias="TCO" name="affiliateReferredTimeStamp"/>
        <alias entity-alias="OH" name="statusId"/>
        <view-link entity-alias="TCO" rel-entity-alias="OH">
            <key-map field-name="orderId"/>
        </view-link>
        <relation type="one" rel-entity-name="OrderHeader">
            <key-map field-name="orderId"/>
        </relation>
    </view-entity>
    <view-entity entity-name="TrackingCodeOrderReturnAndReturnHeader"
                 package-name="org.apache.ofbiz.marketing.reports"
                 never-cache="true"
                 title="Return Header And Tracking Code Order Return View">
        <member-entity entity-alias="TCO" entity-name="TrackingCodeOrderReturn"/>
        <member-entity entity-alias="RH" entity-name="ReturnHeader"/>
        <alias entity-alias="TCO" name="returnId"/>
        <alias entity-alias="TCO" name="orderId"/>
        <alias entity-alias="TCO" name="orderItemSeqId"/>
        <alias entity-alias="TCO" name="trackingCodeId"/>
        <alias entity-alias="TCO" name="siteId"/>
        <alias entity-alias="TCO" name="hasExported"/>
        <alias entity-alias="TCO" name="affiliateReferredTimeStamp"/>
        <alias entity-alias="RH" name="statusId"/>
        <view-link entity-alias="TCO" rel-entity-alias="RH">
            <key-map field-name="returnId"/>
        </view-link>
        <relation type="one" rel-entity-name="ReturnHeader">
            <key-map field-name="returnId"/>
        </relation>
    </view-entity>
    <view-entity entity-name="MarketingCampaignAndVisit"
                 package-name="org.apache.ofbiz.marketing.reports"
                 title="View entity for reporting number of visits for a marketing campaign.  Note that because
         marketingCampaignId is a field of TrackingCode, this is really very similar to TrackingCodeAndVisit,
         except the group-by is with marketingCampaignId instead of trackingCodeId">
        <member-entity entity-alias="TC" entity-name="TrackingCode"/>
        <member-entity entity-alias="TCV" entity-name="TrackingCodeVisit"/>
        <alias entity-alias="TC" name="marketingCampaignId" group-by="true"/>
        <alias entity-alias="TCV" name="visitId" function="count"/>
        <alias entity-alias="TCV" name="fromDate" group-by="false"/>
        <view-link entity-alias="TC" rel-entity-alias="TCV" rel-optional="true">
            <key-map field-name="trackingCodeId"/>
        </view-link>
    </view-entity>
    <view-entity entity-name="MarketingCampaignAndOrderHeader"
                 package-name="org.apache.ofbiz.marketing.reports"
                 title="View entity for reporting number of orders and total order amounts">
        <member-entity entity-alias="TC" entity-name="TrackingCode"/>
        <member-entity entity-alias="TCO" entity-name="TrackingCodeOrder"/>
        <member-entity entity-alias="OH" entity-name="OrderHeader"/>
        <alias entity-alias="OH" name="grandTotal" function="sum"/>
        <alias entity-alias="TCO" name="orderId" function="count"/>
        <alias entity-alias="OH" name="orderDate" group-by="false"/>
        <alias entity-alias="TC" name="marketingCampaignId" group-by="true"/>
        <view-link entity-alias="TC" rel-entity-alias="TCO">
            <key-map field-name="trackingCodeId"/>
        </view-link>
        <view-link entity-alias="TCO" rel-entity-alias="OH">  <!-- the sequence of TCO and OH in this view-link is important -->
            <key-map field-name="orderId"/>
        </view-link>
    </view-entity>

    <!-- ========================================================= -->
    <!-- org.apache.ofbiz.marketing.opportunity -->
    <!-- ========================================================= -->

    <entity entity-name="SalesOpportunity"
            package-name="org.apache.ofbiz.marketing.opportunity"
            title="Main entity of information about sales opportunities">
        <field name="salesOpportunityId" type="id"/>
        <field name="opportunityName" type="name"/>
        <field name="description" type="very-long"/>
        <field name="nextStep" type="very-long"/>
        <field name="nextStepDate" type="date-time"/>
        <field name="estimatedAmount" type="currency-amount"/>
        <field name="estimatedProbability" type="fixed-point"/>
        <field name="currencyUomId" type="id"/>
        <field name="marketingCampaignId" type="id"/>
        <field name="dataSourceId" type="id"/>
        <field name="estimatedCloseDate" type="date-time"/>
        <field name="opportunityStageId" type="id"/>
        <field name="typeEnumId" type="id"/>
        <field name="createdByUserLogin" type="id-vlong"/>
        <prim-key field="salesOpportunityId"/>
        <relation fk-name="SLSOPP_CRNCY_UOM" rel-entity-name="Uom" type="one">
            <key-map field-name="currencyUomId" rel-field-name="uomId"/>
        </relation>
        <relation fk-name="SLSOPP_STAGE" rel-entity-name="SalesOpportunityStage" type="one">
            <key-map field-name="opportunityStageId"/>
        </relation>
        <relation title="Type" fk-name="SLSOPP_TYP_ENUM" rel-entity-name="Enumeration" type="one">
            <key-map field-name="typeEnumId" rel-field-name="enumId"/>
        </relation>
        <relation fk-name="SLSOPP_MKTGCMPG" rel-entity-name="MarketingCampaign" type="one">
            <key-map field-name="marketingCampaignId"/>
        </relation>
        <relation fk-name="SLSOPP_USRLGN" rel-entity-name="UserLogin" type="one">
            <key-map field-name="createdByUserLogin" rel-field-name="userLoginId"/>
        </relation>
    </entity>

    <entity entity-name="SalesOpportunityHistory"
            package-name="org.apache.ofbiz.marketing.opportunity"
            title="Tracks a history of sales opportunity information">
        <field name="salesOpportunityHistoryId" type="id"/>
        <field name="salesOpportunityId" type="id"/>
        <field name="description" type="very-long"/>
        <field name="nextStep" type="very-long"/>
        <field name="estimatedAmount" type="currency-amount"/>
        <field name="estimatedProbability" type="fixed-point"/>
        <field name="currencyUomId" type="id"/>
        <field name="estimatedCloseDate" type="date-time"/>
        <field name="opportunityStageId" type="id"/>
        <field name="changeNote" type="very-long">
            <description>Used to track a reason for this change</description> <!-- maybe one day this should be a link to NoteData -->
        </field>
        <field name="modifiedByUserLogin" type="id-vlong"/>
        <field name="modifiedTimestamp" type="date-time"/>
        <prim-key field="salesOpportunityHistoryId"/>
        <relation fk-name="SLOPHI_CRNCY_UOM" rel-entity-name="Uom" type="one">
            <key-map field-name="currencyUomId" rel-field-name="uomId"/>
        </relation>
        <relation fk-name="SLOPHI_STAGE" rel-entity-name="SalesOpportunityStage" type="one">
            <key-map field-name="opportunityStageId"/>
        </relation>
        <relation fk-name="SLOPHI_SLSOPP" rel-entity-name="SalesOpportunity" type="one">
            <key-map field-name="salesOpportunityId"/>
        </relation>
        <relation fk-name="SLOPHI_USRLGN" rel-entity-name="UserLogin" type="one">
            <key-map field-name="modifiedByUserLogin" rel-field-name="userLoginId"/>
        </relation>
    </entity>

    <entity entity-name="SalesOpportunityRole"
            package-name="org.apache.ofbiz.marketing.opportunity"
            title="Describes roles of different parties involved in a sales opportunity">
        <field name="salesOpportunityId" type="id"/>
        <field name="partyId" type="id"/>
        <field name="roleTypeId" type="id"/>
        <!-- it may be nice to add fromDate and thruDate at some point to track changing roles if that's ever important -->
        <prim-key field="salesOpportunityId"/>
        <prim-key field="partyId"/>
        <prim-key field="roleTypeId"/>
        <relation fk-name="SLSOPPRL_SLSOPP" rel-entity-name="SalesOpportunity" type="one">
            <key-map field-name="salesOpportunityId"/>
        </relation>
        <relation fk-name="SLSOPPRL_PARTY" rel-entity-name="Party" type="one">
            <key-map field-name="partyId"/>
        </relation>
        <relation fk-name="SLSOPPRL_ROLETYPE" rel-entity-name="RoleType" type="one">
            <key-map field-name="roleTypeId"/>
        </relation>
        <relation fk-name="SLSOPPRL_PTYROLE" rel-entity-name="PartyRole" type="one"> <!-- make sure the party actually is in the role specified -->
            <key-map field-name="partyId"/>
            <key-map field-name="roleTypeId"/>
        </relation>
    </entity>

    <view-entity entity-name="SalesOpportunityAndRole" package-name="org.apache.ofbiz.marketing.opportunity" title="SalesOpportunity And Role View">
        <member-entity entity-alias="SO" entity-name="SalesOpportunity"/>
        <member-entity entity-alias="SR" entity-name="SalesOpportunityRole"/>
        <alias-all entity-alias="SO"/>
        <alias name="partyId" entity-alias="SR"/>
        <alias name="roleTypeId" entity-alias="SR"/>
        <view-link entity-alias="SO" rel-entity-alias="SR" rel-optional="true">
            <key-map field-name="salesOpportunityId"/>
        </view-link>
    </view-entity>

    <entity entity-name="SalesOpportunityStage"
            package-name="org.apache.ofbiz.marketing.opportunity"
            title="Describes stages of a sales opportunity with associated probability factors.">
        <field name="opportunityStageId" type="id"/>
        <field name="description" type="description"/>
        <field name="defaultProbability" type="fixed-point"/>
        <field name="sequenceNum" type="numeric"/>
        <prim-key field="opportunityStageId"/>
    </entity>

    <entity entity-name="SalesOpportunityWorkEffort"
            package-name="org.apache.ofbiz.marketing.opportunity"
            title="Relates sales opportunities to their work efforts.">
        <field name="salesOpportunityId" type="id"/>
        <field name="workEffortId" type="id"/>
        <prim-key field="salesOpportunityId"/>
        <prim-key field="workEffortId"/>
        <relation type="one" fk-name="SOPPWEFF_SOPP" rel-entity-name="SalesOpportunity">
            <key-map field-name="salesOpportunityId"/>
        </relation>
        <relation type="one" fk-name="SOPPWEFF_WEFF" rel-entity-name="WorkEffort">
            <key-map field-name="workEffortId"/>
        </relation>
    </entity>

    <entity entity-name="SalesOpportunityQuote"
            package-name="org.apache.ofbiz.marketing.opportunity"
            title="Relates sales opportunities to their quotes.">
        <field name="salesOpportunityId" type="id"/>
        <field name="quoteId" type="id"/>
        <prim-key field="salesOpportunityId"/>
        <prim-key field="quoteId"/>
        <relation type="one" fk-name="SOPPQTE_SOPP" rel-entity-name="SalesOpportunity">
            <key-map field-name="salesOpportunityId"/>
        </relation>
        <relation type="one" fk-name="SOPPQTE_QTE" rel-entity-name="Quote">
            <key-map field-name="quoteId"/>
        </relation>
    </entity>

    <entity entity-name="SalesForecast"
            package-name="org.apache.ofbiz.marketing.opportunity"
            title="Stores sales forecast data for sales opportunities.">
        <field name="salesForecastId" type="id"/>
        <field name="parentSalesForecastId" type="id"/>
        <field name="organizationPartyId" type="id"/>
        <field name="internalPartyId" type="id"/>
        <field name="customTimePeriodId" type="id"/>
        <field name="currencyUomId" type="id"/>
        <field name="quotaAmount" type="currency-amount"/>
        <field name="forecastAmount" type="currency-amount"/>
        <field name="bestCaseAmount" type="currency-amount"/>
        <field name="closedAmount" type="currency-amount"/>
        <field name="percentOfQuotaForecast" type="fixed-point"/>
        <field name="percentOfQuotaClosed" type="fixed-point"/>
        <field name="pipelineAmount" type="currency-amount"/>
        <field name="createdByUserLoginId" type="id-vlong"/>
        <field name="modifiedByUserLoginId" type="id-vlong"/>
        <prim-key field="salesForecastId"/>
        <relation type="one" fk-name="SALES4C_PARENT" title="Parent" rel-entity-name="SalesForecast">
            <key-map field-name="parentSalesForecastId" rel-field-name="salesForecastId"/>
        </relation>
        <relation type="one" fk-name="SALES4C_ORG_PARTY" title="Organization" rel-entity-name="Party">
            <key-map field-name="organizationPartyId" rel-field-name="partyId"/>
        </relation>
        <relation type="one" fk-name="SALES4C_INT_PARTY" title="Internal" rel-entity-name="Party">
            <key-map field-name="internalPartyId" rel-field-name="partyId"/>
        </relation>
        <relation type="one" fk-name="SALES4C_TIME_PER" rel-entity-name="CustomTimePeriod">
            <key-map field-name="customTimePeriodId"/>
        </relation>
        <relation type="one" fk-name="SALES4C_CUR_UOM" rel-entity-name="Uom">
            <key-map field-name="currencyUomId" rel-field-name="uomId"/>
        </relation>
        <relation type="one" fk-name="SALES4C_CRT_USER" title="CreatedBy" rel-entity-name="UserLogin">
            <key-map field-name="createdByUserLoginId" rel-field-name="userLoginId"/>
        </relation>
        <relation type="one" fk-name="SALES4C_MOD_USER" title="ModifiedBy" rel-entity-name="UserLogin">
            <key-map field-name="modifiedByUserLoginId" rel-field-name="userLoginId"/>
        </relation>
    </entity>

    <entity entity-name="SalesForecastDetail"
            package-name="org.apache.ofbiz.marketing.opportunity"
            title="Stores Details of Resourses of Sales Forecast for simulation of MRP">
        <field name="salesForecastId" type="id"/>
        <field name="salesForecastDetailId" type="id"/>
        <field name="amount" type="currency-amount"/>
        <field name="quantityUomId" type="id"/>
        <field name="quantity" type="fixed-point"/>
        <field name="productId" type="id"/>
        <field name="productCategoryId" type="id"/>
        <prim-key field="salesForecastId"/>
        <prim-key field="salesForecastDetailId"/>
        <relation type="one" fk-name="SALES4CDTL_SALES4C" rel-entity-name="SalesForecast">
            <key-map field-name="salesForecastId"/>
        </relation>
        <relation type="one" fk-name="SALES4CDTL_QTY_UOM" title="Quantity" rel-entity-name="Uom">
            <key-map field-name="quantityUomId" rel-field-name="uomId"/>
        </relation>
        <relation type="one" fk-name="SALES4CDTL_PROD" rel-entity-name="Product">
            <key-map field-name="productId"/>
        </relation>
        <relation type="one" fk-name="SALES4CDTL_PCTGRY" rel-entity-name="ProductCategory">
            <key-map field-name="productCategoryId"/>
        </relation>
    </entity>

    <entity entity-name="SalesForecastHistory"
            package-name="org.apache.ofbiz.marketing.opportunity"
            title="Keeps a record of changes to a sales forecast.">
        <field name="salesForecastHistoryId" type="id"/>
        <field name="salesForecastId" type="id"/>
        <field name="parentSalesForecastId" type="id"/>
        <field name="organizationPartyId" type="id"/>
        <field name="internalPartyId" type="id"/>
        <field name="customTimePeriodId" type="id"/>
        <field name="currencyUomId" type="id"/>
        <field name="quotaAmount" type="currency-amount"/>
        <field name="forecastAmount" type="currency-amount"/>
        <field name="bestCaseAmount" type="currency-amount"/>
        <field name="closedAmount" type="currency-amount"/>
        <field name="percentOfQuotaForecast" type="fixed-point"/>
        <field name="percentOfQuotaClosed" type="fixed-point"/>
        <field name="changeNote" type="very-long">
            <description>Used to track a reason for this change</description> <!-- maybe one day this should be a link to NoteData -->
        </field>
        <field name="modifiedByUserLoginId" type="id-vlong"/>
        <field name="modifiedTimestamp" type="date-time"/>
        <prim-key field="salesForecastHistoryId"/>
        <relation type="one" fk-name="SALES4CH_SALES4C" rel-entity-name="SalesForecast">
            <key-map field-name="salesForecastId"/>
        </relation>
        <relation type="one" fk-name="SALES4CH_ORG_PARTY" title="Organization" rel-entity-name="Party">
            <key-map field-name="organizationPartyId" rel-field-name="partyId"/>
        </relation>
        <relation type="one" fk-name="SALES4CH_INT_PARTY" title="Internal" rel-entity-name="Party">
            <key-map field-name="internalPartyId" rel-field-name="partyId"/>
        </relation>
        <relation type="one" fk-name="SALES4CH_TIME_PER" rel-entity-name="CustomTimePeriod">
            <key-map field-name="customTimePeriodId"/>
        </relation>
        <relation type="one" fk-name="SALES4CH_CUR_UOM" rel-entity-name="Uom">
            <key-map field-name="currencyUomId" rel-field-name="uomId"/>
        </relation>
        <relation type="one" fk-name="SALES4CH_MOD_USER" title="ModifiedBy" rel-entity-name="UserLogin">
            <key-map field-name="modifiedByUserLoginId" rel-field-name="userLoginId"/>
        </relation>
    </entity>
    <entity entity-name="SalesOpportunityCompetitor"
            package-name="org.apache.ofbiz.marketing.opportunity"
            title="Sales opportunity competitors record">
        <field name="salesOpportunityId" type="id"/>
        <field name="competitorPartyId" type="id"/>
        <field name="positionEnumId" type="id"/>
        <field name="strengths" type="very-long"/>
        <field name="weaknesses" type="very-long"/>
        <prim-key field="salesOpportunityId"/>
        <prim-key field="competitorPartyId"/>
        <relation type="one" fk-name="SOPPCOMP_SOPP" rel-entity-name="SalesOpportunity">
            <key-map field-name="salesOpportunityId"/>
        </relation>
    </entity>
    <entity entity-name="SalesOpportunityTrckCode"
            package-name="org.apache.ofbiz.marketing.opportunity"
            title="Sales opportunity traking code">
        <field name="salesOpportunityId" type="id"/>
        <field name="trackingCodeId" type="id"/>
        <field name="receivedDate" type="date-time"/>
        <prim-key field="salesOpportunityId"/>
        <prim-key field="trackingCodeId"/>
        <relation type="one" fk-name="SOPPTRKCD_SOPP" rel-entity-name="SalesOpportunity">
            <key-map field-name="salesOpportunityId"/>
        </relation>
    </entity>
    <view-entity entity-name="SalesForecastAndCustomTimePeriod"
                 package-name="org.apache.ofbiz.marketing.opportunity"
                 title="View for selecting the forecast with its time period.">
        <member-entity entity-alias="SF" entity-name="SalesForecast"/>
        <member-entity entity-alias="CTP" entity-name="CustomTimePeriod"/>
        <alias-all entity-alias="SF"/>
        <alias-all entity-alias="CTP">
            <exclude field="organizationPartyId"/>
        </alias-all>
        <view-link entity-alias="SF" rel-entity-alias="CTP">
            <key-map field-name="customTimePeriodId"/>
        </view-link>
    </view-entity>

</entitymodel>
