AIMS_BD_IATI.DAL.AimsDAL.ConvertAimsToIati C# (CSharp) Method

ConvertAimsToIati() private method

private ConvertAimsToIati ( tblProjectInfo project ) : iatiactivity
project tblProjectInfo
return iatiactivity
        private iatiactivity ConvertAimsToIati(tblProjectInfo project)
        {
            var iatiActivityObj = new iatiactivity();

            iatiActivityObj.IsDataSourceAIMS = true;
            iatiActivityObj.IsCofinancedProject = project.IsCofundedProject ?? false;

            iatiActivityObj.AllID = project.FundSourceId + "~" + (project.tblFundSource?.IATICode ?? "") + "~"
                                + (int)ExecutingAgencyType.DP + "~"
                                + project.tblFundSource?.FundSourceCategoryId;

            iatiActivityObj.ProjectId = project.Id;
            //iati-activity
            iatiActivityObj.lastupdateddatetime = DateTime.Now;
            iatiActivityObj.lang = "en";
            iatiActivityObj.defaultcurrency = Statix.Currency;
            iatiActivityObj.hierarchy = 1;
            //linked-data-uri

            //iati-identifier
            iatiActivityObj.iatiidentifier = new iatiidentifier { Value = getIdentifer(project) };

            //reporting-org
            iatiActivityObj.reportingorg = new reportingorg
            {
                @ref = project.tblFundSource?.IATICode,
                type = project.tblFundSource?.tblFundSourceCategory?.IATICode,
                //secondary-reporter
                narrative = Statix.getNarativeArray(project.tblFundSource?.FundSourceName),
            };

            //title
            iatiActivityObj.title = new textRequiredType { narrative = Statix.getNarativeArray(project.Title) };
            //description
            iatiActivityObj.description = new iatiactivityDescription[1] { new iatiactivityDescription { narrative = Statix.getNarativeArray(project.Objective) } };

            //participating-org
            List<participatingorg> participatingorgList = new List<participatingorg>();
            participatingorgList.Add(new participatingorg
            {
                narrative = Statix.getNarativeArray(project.tblFundSource?.FundSourceGroup),
                role = "1",
                @ref = project.tblFundSource?.IATICode,
                type = project.tblFundSource?.tblFundSourceCategory?.IATICode,
            });

            participatingorgList.Add(new participatingorg
            {
                narrative = Statix.getNarativeArray(project.tblFundSource?.FundSourceName),
                role = "3",
                @ref = project.tblFundSource?.IATICode,
                type = project.tblFundSource?.tblFundSourceCategory?.IATICode,
            });

            if (project.tblProjectExecutingAgencies != null)
                foreach (var executingAgency in project.tblProjectExecutingAgencies)
                {
                    participatingorgList.Add(new participatingorg
                    {
                        narrative = Statix.getNarativeArray("N/A"),
                        role = "4",
                        @ref = "N/A",
                        type = "N/A",
                    });
                }

            //ToDo
            //iatiActivity.participatingorg[2] = new participatingorg
            //{
            //    narrative = getNarativeArray(project.tblFundSource.FundSourceName),
            //    role = "1",
            //    @ref = project.tblFundSource.IATICode,
            //    type = "10"
            //};
            iatiActivityObj.participatingorg = participatingorgList.ToArray();

            //other-identifier

            //activity-status
            iatiActivityObj.activitystatus = new activitystatus { code = project.tblImplementationStatu.IATICode };

            //activity-date
            List<activitydate> activitydateList = new List<activitydate>();
            activitydateList.Add(new activitydate { type = "1", isodate = project.PlannedProjectStartDate ?? default(DateTime) });
            activitydateList.Add(new activitydate { type = "2", isodate = project.ActualProjectStartDate ?? default(DateTime) });
            activitydateList.Add(new activitydate { type = "3", isodate = project.PlannedProjectCompletionDate ?? default(DateTime) });
            activitydateList.Add(new activitydate { type = "4", isodate = project.RevisedProjectCompletionDate ?? default(DateTime) });
            iatiActivityObj.activitydate = activitydateList.ToArray();

            //contact-info
            List<contactinfo> contactinfoList = new List<contactinfo>();
            contactinfoList.Add(new contactinfo //DP
            {
                type = "1",
                organisation = new textRequiredType { narrative = Statix.getNarativeArray(project.tblFundSource?.FundSourceName) },
                department = new textRequiredType { narrative = Statix.getNarativeArray(project.tblFundSource?.FundSourceName) },
                personname = new textRequiredType { narrative = Statix.getNarativeArray(project.FocalPointDPContactName) },
                jobtitle = new textRequiredType { narrative = Statix.getNarativeArray(project.FocalPointDPContactDesignation) },
                telephone = new List<contactinfoTelephone> { new contactinfoTelephone { Value = project.FocalPointDPContactTelephone } }.ToArray(),
                email = new List<contactinfoEmail> { new contactinfoEmail { Value = project.FocalPointDPContactEmail } }.ToArray(),
                website = new List<contactinfoWebsite> { new contactinfoWebsite { Value = project.FocalPointDPContactAddress } }.ToArray(),
                mailingaddress = new List<textRequiredType> { new textRequiredType { narrative = Statix.getNarativeArray(project.FocalPointDPContactAddress) } }.ToArray()
            });
            contactinfoList.Add(new contactinfo //GoB
            {
                type = "2",
                organisation = new textRequiredType { narrative = Statix.getNarativeArray(Statix.RecipientCountryName) },
                department = new textRequiredType { narrative = Statix.getNarativeArray("PD") },
                personname = new textRequiredType { narrative = Statix.getNarativeArray(project.FocalPointGoBContactName) },
                jobtitle = new textRequiredType { narrative = Statix.getNarativeArray(project.FocalPointGoBContactDesignation) },
                telephone = new List<contactinfoTelephone> { new contactinfoTelephone { Value = project.FocalPointGoBContactTelephone } }.ToArray(),
                email = new List<contactinfoEmail> { new contactinfoEmail { Value = project.FocalPointGoBContactEmail } }.ToArray(),
                website = new List<contactinfoWebsite> { new contactinfoWebsite { Value = project.FocalPointGoBContactAddress } }.ToArray(),
                mailingaddress = new List<textRequiredType> { new textRequiredType { narrative = Statix.getNarativeArray(project.FocalPointGoBContactAddress) } }.ToArray()
            });
            iatiActivityObj.contactinfo = contactinfoList.ToArray();

            //activity-scope

            //recipient-country
            List<recipientcountry> recipientcountryList = new List<recipientcountry>();
            recipientcountryList.Add(new recipientcountry { code = Statix.RecipientCountry, narrative = Statix.getNarativeArray(Statix.RecipientCountryName), percentage = 100 });
            iatiActivityObj.recipientcountry = recipientcountryList.ToArray();

            //recipient-region

            //location
            List<location> locationList = new List<location>();
            var locations = project.tblProjectGeographicAllocations.ToList();
            foreach (var location in locations)
            {
                locationList.Add(new location
                {
                    name = new textRequiredType { narrative = Statix.getNarativeArray(location.DistrictId.ToString()) },
                });
            }
            iatiActivityObj.location = locationList.ToArray();

            //sector
            List<sector> sectorList = new List<sector>();
            var sectors = project.tblProjectSectoralAllocations.ToList();
            foreach (var sector in sectors)
            {
                sectorList.Add(new sector
                {
                    narrative = Statix.getNarativeArray(sector.TotalCommitmentPercent.ToString())
                });
            }
            iatiActivityObj.sector = sectorList.ToArray();

            //country-budget-items

            //humanitarian-scope

            //policy-marker

            //collaboration-type

            //default-flow-type

            //default-finance-type

            //default-aid-type
            iatiActivityObj.defaultaidtype = new defaultaidtype { code = project.tblAssistanceType?.IATICode };

            //default-tied-status

            //budget

            //planned-disbursement
            List<planneddisbursement> planneddisbursementList = new List<planneddisbursement>();
            var planneddisbursements = project.tblProjectFundingPlannedDisbursements.ToList();
            foreach (var pd in planneddisbursements)
            {
                planneddisbursementList.Add(new planneddisbursement
                {
                    type = "1", //1=Origin, 2=Revised 
                    periodstart = new planneddisbursementPeriodstart { isodate = pd.PlannedDisbursementPeriodFromDate ?? DateTime.MinValue },
                    periodend = new planneddisbursementPeriodend { isodate = pd.PlannedDisbursementPeriodToDate ?? DateTime.MinValue },
                    value = new currencyType { currency = Statix.Currency, Value = pd.PlannedDisburseAmountInUSD ?? 0, valuedate = pd.PlannedDisbursementPeriodFromDate ?? DateTime.MinValue, ValueInUSD = pd.PlannedDisburseAmountInUSD ?? 0 },

                    providerorg = new planneddisbursementProviderorg
                    {
                        @ref = pd.tblFundSource?.IATICode,
                        provideractivityid = project.IatiIdentifier,
                        narrative = Statix.getNarativeArray(pd.tblFundSource?.FundSourceName),
                        type = pd.tblFundSource?.tblFundSourceCategory?.IATICode
                    },

                    receiverorg = new planneddisbursementReceiverorg
                    {
                        receiveractivityid = project.IatiIdentifier,
                        @ref = project.tblFundSource?.IATICode,
                        narrative = Statix.getNarativeArray(project.tblFundSource?.FundSourceName),
                        type = project.tblFundSource?.tblFundSourceCategory?.IATICode,
                    }

                });
            }

            iatiActivityObj.planneddisbursement = planneddisbursementList.ToArray();

            //capital-spend

            #region Transaction
            //Transaction
            List<transaction> transactions = new List<transaction>();

            //Commitment
            var commitments = project.tblProjectFundingCommitments.ToList();
            foreach (var commitment in commitments)
            {
                transaction tr = new transaction();
                tr.transactiontype = new transactionTransactiontype { code = ConvertIATIv2.gettransactionCode("C") };
                var date = commitment.CommitmentAgreementSignDate ?? project.AgreementSignDate;
                tr.transactiondate = new transactionTransactiondate { isodate = date };
                tr.value = new currencyType { currency = Statix.Currency, valuedate = date, Value = Convert.ToDecimal(commitment.CommittedAmountInUSD), ValueInUSD = Convert.ToDecimal(commitment.CommittedAmountInUSD) }; //commitment.tblCurrency.IATICode

                tr.description = new textRequiredType { narrative = Statix.getNarativeArray(commitment.Remarks) };
                tr.providerorg = new transactionProviderorg
                {
                    @ref = commitment.tblFundSource?.IATICode,
                    provideractivityid = project.IatiIdentifier,
                    narrative = Statix.getNarativeArray(commitment.tblFundSource?.FundSourceName),
                    type = commitment.tblFundSource?.tblFundSourceCategory?.IATICode
                };
                tr.receiverorg = new transactionReceiverorg
                {
                    @ref = project.tblFundSource?.IATICode,
                    receiveractivityid = project.IatiIdentifier,
                    narrative = Statix.getNarativeArray(project.tblFundSource?.FundSourceName),
                    type = project.tblFundSource?.tblFundSourceCategory?.IATICode
                };

                //<disbursement-channel code="1" />
                tr.disbursementchannel = new transactionDisbursementchannel { code = Statix.DisbursementChannel }; //Money is disbursed directly to the implementing institution and managed through a separate bank account

                //<sector vocabulary="2" code="111" />

                //<recipient-country code="AF" />  <!--Note: only a recipient-region OR a recipient-country is expected-->
                tr.recipientcountry = new transactionRecipientcountry { code = Statix.RecipientCountry };

                //<recipient-region code="456" vocabulary="1" />

                //<flow-type code="10" />
                tr.flowtype = new transactionFlowtype { code = Statix.FlowType };

                //<finance-type code="110" /> //110= Aid grant excluding debt reorganisation, 410 = Aid loan excluding debt reorganisation
                tr.financetype = new transactionFinancetype { code = commitment.tblAidCategory?.IATICode };

                //<aid-type code="A01" /> 
                tr.aidtype = new transactionAidtype { code = project.tblAssistanceType?.IATICode };

                //<tied-status code="3" />
                tr.tiedstatus = new transactionTiedstatus { code = project.tblAIDEffectivenessIndicators.Where(q => q.AEISurveyYear == date.Year).ToList().n(0).tblAIDEffectivenessResourceTiedType?.IATICode };


                iatiActivityObj.IsTrustFundedProject = commitment.IsCommitmentTrustFund ?? false;

                transactions.Add(tr);
            }

            //Actual Disbusement
            var actualDisbursements = project.tblProjectFundingActualDisbursements.ToList();
            foreach (var actualDisbursement in actualDisbursements)
            {
                transaction tr = new transaction();
                tr.transactiontype = new transactionTransactiontype { code = ConvertIATIv2.gettransactionCode("D") };
                var date = actualDisbursement.DisbursementToDate ?? actualDisbursement.DisbursementDate;
                tr.transactiondate = new transactionTransactiondate { isodate = date };
                tr.value = new currencyType { currency = Statix.Currency, valuedate = date, Value = Convert.ToDecimal(actualDisbursement.DisbursedAmountInUSD), ValueInUSD = Convert.ToDecimal(actualDisbursement.DisbursedAmountInUSD) }; //actualDisbursement.tblCurrency.IATICode

                tr.description = new textRequiredType { narrative = Statix.getNarativeArray(actualDisbursement.Remarks) };
                tr.providerorg = new transactionProviderorg
                {
                    @ref = actualDisbursement.tblFundSource?.IATICode,
                    provideractivityid = project.IatiIdentifier,
                    narrative = Statix.getNarativeArray(actualDisbursement.tblFundSource?.FundSourceName),
                    type = actualDisbursement.tblFundSource?.tblFundSourceCategory?.IATICode

                };
                tr.receiverorg = new transactionReceiverorg
                {
                    @ref = project.tblFundSource?.IATICode,
                    receiveractivityid = project.IatiIdentifier,
                    narrative = Statix.getNarativeArray(project.tblFundSource?.FundSourceName),
                    type = project.tblFundSource?.tblFundSourceCategory?.IATICode

                };

                //<disbursement-channel code="1" />
                tr.disbursementchannel = new transactionDisbursementchannel { code = Statix.DisbursementChannel };

                //<sector vocabulary="2" code="111" />

                //<recipient-country code="AF" />  <!--Note: only a recipient-region OR a recipient-country is expected-->
                tr.recipientcountry = new transactionRecipientcountry { code = Statix.RecipientCountry };

                //<recipient-region code="456" vocabulary="1" />

                //<flow-type code="10" />
                tr.flowtype = new transactionFlowtype { code = Statix.FlowType };

                //<finance-type code="110" /> //110= Aid grant excluding debt reorganisation, 410 = Aid loan excluding debt reorganisation
                tr.financetype = new transactionFinancetype { code = actualDisbursement.tblAidCategory?.IATICode };

                //<aid-type code="A01" /> 
                tr.aidtype = new transactionAidtype { code = project.tblAssistanceType?.IATICode };

                //<tied-status code="3" />
                tr.tiedstatus = new transactionTiedstatus { code = project.tblAIDEffectivenessIndicators.Where(q => q.AEISurveyYear == date.Year).ToList().n(0).tblAIDEffectivenessResourceTiedType?.IATICode };

                transactions.Add(tr);
            }

            //Expenditure
            var expenditures = project.tblProjectFundingExpenditures.ToList();
            foreach (var expenditure in expenditures)
            {
                transaction tr = new transaction();
                tr.transactiontype = new transactionTransactiontype { code = ConvertIATIv2.gettransactionCode("E") };
                var date = expenditure.ExpenditureReportingPeriodToDate; //?? expenditure.ExpenditureReportingPeriodFromDate;
                tr.transactiondate = new transactionTransactiondate { isodate = date };
                tr.value = new currencyType { currency = Statix.Currency, valuedate = date, Value = expenditure.ExpenditureAmountInUSD ?? 0, ValueInUSD = expenditure.ExpenditureAmountInUSD ?? 0 }; //expenditure.tblCurrency.IATICode

                tr.description = new textRequiredType { narrative = Statix.getNarativeArray(expenditure.Remarks) };
                tr.providerorg = new transactionProviderorg
                {
                    @ref = expenditure.tblFundSource?.IATICode,
                    provideractivityid = project.IatiIdentifier,
                    narrative = Statix.getNarativeArray(expenditure.tblFundSource?.FundSourceName),
                    type = expenditure.tblFundSource?.tblFundSourceCategory?.IATICode

                };
                tr.receiverorg = new transactionReceiverorg
                {
                    @ref = project.tblFundSource?.IATICode,
                    receiveractivityid = project.IatiIdentifier,
                    narrative = Statix.getNarativeArray(project.tblFundSource?.FundSourceName),
                    type = project.tblFundSource?.tblFundSourceCategory?.IATICode
                };

                //<disbursement-channel code="1" />
                tr.disbursementchannel = new transactionDisbursementchannel { code = Statix.DisbursementChannel };

                //<sector vocabulary="2" code="111" />

                //<recipient-country code="AF" />  <!--Note: only a recipient-region OR a recipient-country is expected-->
                tr.recipientcountry = new transactionRecipientcountry { code = Statix.RecipientCountry };

                //<recipient-region code="456" vocabulary="1" />

                //<flow-type code="10" />
                tr.flowtype = new transactionFlowtype { code = Statix.FlowType };

                //<finance-type code="110" /> //110= Aid grant excluding debt reorganisation, 410 = Aid loan excluding debt reorganisation
                tr.financetype = new transactionFinancetype { code = expenditure.tblAidCategory?.IATICode };

                //<aid-type code="A01" /> 
                tr.aidtype = new transactionAidtype { code = project.tblAssistanceType?.IATICode };

                //<tied-status code="3" />
                tr.tiedstatus = new transactionTiedstatus { code = project.tblAIDEffectivenessIndicators.Where(q => q.AEISurveyYear == date.Year).ToList().n(0).tblAIDEffectivenessResourceTiedType?.IATICode };

                transactions.Add(tr);
            }

            //Assign all transaction
            iatiActivityObj.transaction = transactions.ToArray();
            #endregion

            //document-link
            List<documentlink> documentlinkList = new List<documentlink>();
            var documents = project.tblProjectAttachments.ToList();
            foreach (var document in documents)
            {
                List<documentlinkLanguage> documentlinkLanguageList = new List<documentlinkLanguage>();
                documentlinkLanguageList.Add(new documentlinkLanguage { code = Statix.Language });

                List<documentlinkCategory> documentlinkCategoryList = new List<documentlinkCategory>();
                documentlinkCategoryList.Add(new documentlinkCategory { code = document.tblDocumentCategory?.IATICode });

                documentlinkList.Add(new documentlink
                {
                    url = document.AttachmentFileURL ?? Statix.DocumentURL + document.Id,
                    //format = 
                    title = new textRequiredType { narrative = Statix.getNarativeArray(document.AttachmentTitle) },
                    language = documentlinkLanguageList.ToArray(),
                    category = documentlinkCategoryList.ToArray()
                });
            }
            iatiActivityObj.documentlink = documentlinkList.ToArray();

            //related-activity

            //legacy-data

            //conditions

            //result

            //crs-add

            //fss


            return iatiActivityObj;
        }
        private string getIdentifer(tblProjectInfo project)