Exemple #1
0
        public IMarketFileExportResult[] Export(CancellationToken token)
        {
            var cspDunsPorts = clientDataAccess.ListCspDunsPort();
            var prismPorts   = cspDunsPorts
                               .Where(x => x.ProviderId == 1)
                               .ToArray();

            var context = new Prism810Context();

            foreach (var prismPort in prismPorts)
            {
                if (token.IsCancellationRequested)
                {
                    break;
                }

                var headers = marketDataAccess.ListUnprocessed(prismPort.LdcDuns, prismPort.Duns, 1);
                if (headers.Length == 0)
                {
                    logger.TraceFormat("Zero 810 Prism records found to export for TDSP Duns \"{0}\" and CR Duns \"{1}\".",
                                       prismPort.LdcDuns, prismPort.Duns);
                    continue;
                }

                logger.DebugFormat("Exporting {0} unprocessed 810 record(s) for TDSP Duns \"{1}\" and CR Duns \"{2}\".",
                                   headers.Length, prismPort.LdcDuns, prismPort.Duns);

                var cspDuns = clientDataAccess.LoadDunsByCspDunsId(prismPort.CspDunsId);

                foreach (var header in headers)
                {
                    if (!header.HeaderKey.HasValue)
                    {
                        continue;
                    }

                    if (token.IsCancellationRequested)
                    {
                        break;
                    }

                    var headerKey = header.HeaderKey.Value;
                    context.Initialize();

                    var ldcModel = clientDataAccess.LoadLdcByTdspDuns(header.TdspDuns);
                    if (ldcModel == null)
                    {
                        logger.ErrorFormat("Failed to load LDC for DUNS \"{0}\".", header.TdspDuns);
                        return(context.Models);
                    }

                    context.SetMarket(ldcModel.MarketId);
                    context.SetFileProperties(prismPort, header.TdspDuns, "INV");
                    context.IsCustomerInvoice = false;
                    context.BillFromName      = header.TdspName;
                    context.BillFromDuns      = header.TdspDuns;

                    if (!string.IsNullOrWhiteSpace(cspDuns) && cspDuns.Equals("RYDER0000", StringComparison.Ordinal))
                    {
                        context.SetFileNamePrefix("ECPCUSTINVRYDER");
                    }

                    if (!string.IsNullOrEmpty(header.CustomerDUNS))
                    {
                        var seekDuns = header.TdspDuns;
                        if (seekDuns.Length > 9)
                        {
                            seekDuns = seekDuns.Substring(0, 9);
                        }

                        var partnerId = context.TradingPartnerId.Replace(seekDuns, header.CustomerDUNS);
                        context.SetTradingPartnerId(partnerId);

                        var cspDunsList = clientDataAccess.ListCspDuns();
                        if (cspDunsList == null || cspDunsList.Length == 0)
                        {
                            logger.Error("No CSP DUNS exist or none could be found.");
                            return(context.Models);
                        }

                        var cspDunsItem = cspDunsList.First();
                        context.IsCustomerInvoice = true;
                        context.BillFromName      = cspDunsItem.Description;
                        context.BillFromDuns      = cspDunsItem.Duns;

                        if (header.CustomerDUNS.Equals("055458350"))
                        {
                            var crdPartnerId      = context.TradingPartnerId;
                            var fixedCrdPartnerId = string.Concat("CRD",
                                                                  crdPartnerId.Substring(3, crdPartnerId.Length - 3));

                            context.SetTradingPartnerId(fixedCrdPartnerId);
                        }

                        if (header.CustomerDUNS.Equals("PROKARMAI", StringComparison.Ordinal))
                        {
                            var lptPartnerId      = context.TradingPartnerId;
                            var fixedLptPartnerId = string.Concat("LPT",
                                                                  lptPartnerId.Substring(3, lptPartnerId.Length - 3));

                            var zzDuns = string.Concat("ZZ", header.CustomerDUNS);
                            context.SetFileProperties(prismPort, zzDuns, fixedLptPartnerId, header.CustomerDUNS, string.Empty);
                        }
                    }

                    context.SetHeaderId(headerKey);

                    var invoiceConfig = clientDataAccess
                                        .LoadCustomerInvoiceConfig(header.CustomerDUNS, ldcModel.LdcId);

                    if (invoiceConfig != null)
                    {
                        var zzPartnerId      = context.TradingPartnerId;
                        var fixedZzPartnerId = string.Concat(zzPartnerId.Substring(0, 3), "ZZ",
                                                             zzPartnerId.Substring(5, zzPartnerId.Length - 5));

                        context.SetTradingPartnerId(fixedZzPartnerId);

                        var shouldSkipMeterReads   = false;
                        var configuredCustomerDuns = clientDataAccess
                                                     .LoadParameterConfigurationValue("CustomerDunsConfiguredForSkip30Record");
                        if (!string.IsNullOrWhiteSpace(configuredCustomerDuns))
                        {
                            shouldSkipMeterReads = configuredCustomerDuns.Equals(invoiceConfig.CustomerDuns);
                        }

                        WriteCustInvHeader(context, invoiceConfig, header);
                        WriteCustInvAccount(context, invoiceConfig, header);
                        WriteCustInvAddress(context, invoiceConfig, header);
                        WriteCustInvRemitanceAddress(context, invoiceConfig, header);
                        WriteCustInvDetail(context, invoiceConfig, header);
                        WriteCustInvAccountTaxCharges(context, invoiceConfig, header);
                        if (!shouldSkipMeterReads)
                        {
                            WriteCustInvMeterReads(context, invoiceConfig, header);
                        }
                        WriteCustInvCharges(context, invoiceConfig, header);
                        WriteCustInvTax(context, invoiceConfig, header);
                        WriteCustInvSummary(context, invoiceConfig, header);
                    }
                    else
                    {
                        if (context.TradingPartnerId.Equals("CRDTX055458350INV", StringComparison.Ordinal))
                        {
                            var txPartnerId      = context.TradingPartnerId;
                            var fixedTxPartnerId = txPartnerId.Replace("CRDTX", "CRDZZ");

                            context.SetTradingPartnerId(fixedTxPartnerId);

                            WriteNiscHeader(context, header);
                            WriteNiscAccount(context, header);
                            WriteNiscRemitanceAddress(context, header);
                            WriteNiscDetail(context, header);
                            WriteNiscAccountTaxCharges(context, header);
                            WriteNiscMeterReads(context, header);
                            WriteNiscCharges(context, header);
                            WriteNiscTax(context, header);
                            WriteNiscSummary(context, header);
                        }
                        else
                        {
                            WriteHeader(context, header);
                            WriteAccount(context, header);
                            WriteRemitanceAddress(context, header);
                            WriteDetail(context, header);
                            WriteMeterReads(context, header);
                            WriteCharges(context, header);
                            WriteTax(context, header);
                            WriteSummary(context, header);
                        }
                    }
                }
            }

            return(context.Models);
        }