void buildRecordType000()
        {
            var lsField = new LSFieldsOUT();

            lineNumber++;
            lsField.LineNumber = lineNumber;

            lsField.RecordType = DirectDebitPaymentHelper.RECORDTYPE_000;

            lsField.Filler01 = DirectDebitPaymentHelper.processStringNum(5);
            lsField.Filler02 = DirectDebitPaymentHelper.processStringNum(15);
            lsField.Filler03 = DirectDebitPaymentHelper.processStringAlpha(7);

            lsField.DataSupplierNo   = DirectDebitPaymentHelper.processStringAlpha(6, comp.CompanyId.ToString());
            lsField.DataDeliveryType = DirectDebitPaymentHelper.processStringNum(2, DirectDebitPaymentHelper.DELIVERYTYPE_40);

            lsField.Year  = DateTime.Today.ToString("yy");
            lsField.Month = DateTime.Today.ToString("MM");
            lsField.Date  = DateTime.Today.ToString("dd");

            lsField.Filler04 = DirectDebitPaymentHelper.processStringAlpha(1);
            lsField.Filler05 = DirectDebitPaymentHelper.processStringAlpha(4, DirectDebitPaymentHelper.DELIVERY_TEST);

            lsField.Filler06 = DirectDebitPaymentHelper.processStringAlpha(1);
            lsField.Filler07 = DirectDebitPaymentHelper.processStringAlpha(3);
            lsField.Filler08 = DirectDebitPaymentHelper.processStringAlpha(9);

            lsField.DataSupplierCVR      = DirectDebitPaymentHelper.processStringNum(8, DirectDebitPaymentHelper.UNICONTA_CVR);
            lsField.DataSupplierCVRCheck = DirectDebitPaymentHelper.processStringAlpha(1);

            lsField.Filler09 = DirectDebitPaymentHelper.processStringAlpha(9);

            listOfTrans.Add(lsField);
        }
        void buildRecordType540()
        {
            var lsField = new LSFieldsOUT();

            lineNumber++;
            lsField.LineNumber = lineNumber;

            lsField.RecordType = DirectDebitPaymentHelper.RECORDTYPE_540;

            lsField.CreditorNo = DirectDebitPaymentHelper.processStringNum(5, DirectDebitPaymentHelper.CREDITORNUMBER);
            lsField.CustomerNo = DirectDebitPaymentHelper.processStringAlpha(15, debtorAccount);

            lsField.Filler01 = DirectDebitPaymentHelper.processStringNum(33);
            lsField.Filler02 = DirectDebitPaymentHelper.processStringAlpha(24);

            listOfTrans.Add(lsField);
        }
        void buildRecordType585()
        {
            var lsField = new LSFieldsOUT();

            lineNumber++;
            lsField.LineNumber = lineNumber;

            lsField.RecordType = DirectDebitPaymentHelper.RECORDTYPE_585;

            lsField.CreditorNo = DirectDebitPaymentHelper.processStringNum(5, DirectDebitPaymentHelper.CREDITORNUMBER);
            lsField.CustomerNo = DirectDebitPaymentHelper.processStringAlpha(15, debtorAccount); //TODO:Spørg NETS om de kan håndtere Kontonr. "000015" dvs. foranstillede Nul

            lsField.Filler01  = DirectDebitPaymentHelper.processStringNum(22);
            lsField.AmountStr = DirectDebitPaymentHelper.processStringNum(11, paymentAmountStr);
            lsField.Filler02  = DirectDebitPaymentHelper.processStringAlpha(24);

            listOfTrans.Add(lsField);
        }
        void buildRecordType595()
        {
            var lsField = new LSFieldsOUT();

            lineNumber++;
            lsField.LineNumber = lineNumber;

            lsField.RecordType = DirectDebitPaymentHelper.RECORDTYPE_595;

            lsField.CreditorNo = DirectDebitPaymentHelper.processStringNum(5, DirectDebitPaymentHelper.CREDITORNUMBER);
            lsField.CustomerNo = DirectDebitPaymentHelper.processStringAlpha(15, debtorAccount);

            lsField.NewCreditorNo = DirectDebitPaymentHelper.processStringNum(5, DirectDebitPaymentHelper.CREDITORNUMBER);
            lsField.NewCustomerNo = DirectDebitPaymentHelper.processStringNum(15, "88881"); //TODO:TEST

            lsField.Filler01 = DirectDebitPaymentHelper.processStringAlpha(37);

            listOfTrans.Add(lsField);
        }
        void buildRecordType510()
        {
            var lsField = new LSFieldsOUT();

            lineNumber++;
            lsField.LineNumber = lineNumber;

            lsField.RecordType = DirectDebitPaymentHelper.RECORDTYPE_510;

            lsField.CreditorNo = DirectDebitPaymentHelper.processStringNum(5, DirectDebitPaymentHelper.CREDITORNUMBER);
            lsField.CustomerNo = DirectDebitPaymentHelper.processStringAlpha(15, debtorAccount);

            lsField.DebtorRegNum    = DirectDebitPaymentHelper.processStringNum(4, debtorRegNum);
            lsField.DebtorAccNumber = DirectDebitPaymentHelper.processStringNum(10, debtorBankAccount);
            lsField.DebtorCVR       = DirectDebitPaymentHelper.processStringNum(8, debtorCvrNumber);

            lsField.Filler01 = DirectDebitPaymentHelper.processStringNum(11);
            lsField.Filler02 = DirectDebitPaymentHelper.processStringAlpha(24);

            listOfTrans.Add(lsField);
        }
        void buildRecordType001()
        {
            var lsField = new LSFieldsOUT();

            lineNumber++;
            lsField.LineNumber = lineNumber;

            lsField.RecordType = DirectDebitPaymentHelper.RECORDTYPE_001;

            lsField.CreditorNo = DirectDebitPaymentHelper.processStringNum(5, DirectDebitPaymentHelper.CREDITORNUMBER);
            lsField.Filler01   = DirectDebitPaymentHelper.processStringNum(15);

            lsField.Year  = paymentDate.ToString("yy");
            lsField.Month = paymentDate.ToString("MM");
            lsField.Date  = paymentDate.ToString("dd");

            lsField.Filler02 = DirectDebitPaymentHelper.processStringNum(14);
            lsField.Filler03 = DirectDebitPaymentHelper.processStringAlpha(37);

            lsField.CustomerNo = DirectDebitPaymentHelper.processStringAlpha(15, debtorAccount);

            listOfTrans.Add(lsField);
        }
        void buildRecordType999()
        {
            var lsField = new LSFieldsOUT();

            lineNumber++;
            lsField.LineNumber = lineNumber;

            var amountCollectionTotalStr   = NumberConvert.ToLong(Math.Abs(amountCollectionTotal) * 100d).ToString();
            var amountDisbursementTotalStr = NumberConvert.ToLong(Math.Abs(amountDisbursementTotal) * 100d).ToString();

            lsField.RecordType = DirectDebitPaymentHelper.RECORDTYPE_999;

            lsField.CreditorNo = DirectDebitPaymentHelper.processStringNum(5, DirectDebitPaymentHelper.CREDITORNUMBER);

            lsField.Filler01 = DirectDebitPaymentHelper.processStringAlpha(15);

            lsField.NumberOfDebtorsStr    = DirectDebitPaymentHelper.processStringNum(7, countDebtors.ToString());
            lsField.AmountCollectionStr   = DirectDebitPaymentHelper.processStringNum(13, amountCollectionTotalStr);
            lsField.AmountDisbursementStr = DirectDebitPaymentHelper.processStringNum(13, amountDisbursementTotalStr);

            lsField.Filler02 = DirectDebitPaymentHelper.processStringAlpha(24);

            listOfTrans.Add(lsField);
        }
        /// <summary>
        /// Read a NETS Leverandørservice file.
        /// </summary>
        internal void StreamFromFile(CrudAPI api, string filePath, IEnumerable <DebtorTransDirectDebit> lstDebTrans)
        {
            int lineNumber = 0;

            lstDebtorTransDirectDebit = lstDebTrans;
            capi = api;
            //var filePath = @"C:\Users\lasse\OneDrive\Dokumenter\Uniconta\NETS\LS\Filer\In\";

            string[] files = Directory.GetFiles(filePath);

            LSFieldsIN fieldIN;

            var  dataDeliveryType = string.Empty;
            bool headerFooter;

            foreach (string file in files)
            {
                string[] lines = File.ReadAllLines(file);

                foreach (string line in lines)
                {
                    fieldIN = new LSFieldsIN();
                    lineNumber++;
                    headerFooter       = true;
                    fieldIN.LineNumber = lineNumber;
                    fieldIN.RecordType = line.Substring(2, 3);

                    switch (fieldIN.RecordType)
                    {
                    case DirectDebitPaymentHelper.RECORDTYPE_000:     //Data Delivery Start - (Registered and Cancelled mandates)
                        dataDeliveryType             = line.Substring(36, 2);
                        fieldIN.DataDeliveryType     = dataDeliveryType;
                        fieldIN.CreationDate         = DirectDebitPaymentHelper.ConvToDate(string.Format("{0}{1}{2}", line.Substring(38, 2), line.Substring(40, 2), line.Substring(42, 2)));
                        fieldIN.TestDelivery         = line.Substring(45, 4) == DirectDebitPaymentHelper.DELIVERY_TEST ? true : false;
                        fieldIN.DataSupplierNo       = line.Substring(62, 8);
                        fieldIN.DataSupplierCVRCheck = line.Substring(70, 1);
                        break;

                    case DirectDebitPaymentHelper.RECORDTYPE_002:     //Data Delivery Start - (Receipt and Remarks) and (Payment Information)
                        fieldIN.DataSupplierNo   = line.Substring(5, 8);
                        dataDeliveryType         = line.Substring(16, 4);
                        fieldIN.DataDeliveryType = dataDeliveryType;
                        fieldIN.TestDelivery     = line.Substring(45, 4) == DirectDebitPaymentHelper.DELIVERY_TEST ? true : false;
                        fieldIN.CreationDate     = DirectDebitPaymentHelper.ConvToDate(line.Substring(49, 6));
                        break;

                    case DirectDebitPaymentHelper.RECORDTYPE_992:     //Data Delivery End - (Receipt and Remarks) and (Payment Information)
                        fieldIN.DataSupplierNo     = line.Substring(5, 8);
                        dataDeliveryType           = line.Substring(16, 4);
                        fieldIN.DataDeliveryType   = dataDeliveryType;
                        fieldIN.NumberOfRecType042 = DirectDebitPaymentHelper.ConvToInt(line.Substring(31, 11));
                        break;

                    case DirectDebitPaymentHelper.RECORDTYPE_999:     //Data Delivery End - (Registered and Cancelled mandates)
                        fieldIN.CreditorNo      = line.Substring(3, 5);
                        fieldIN.NumberOfDebtors = DirectDebitPaymentHelper.ConvToInt(line.Substring(30, 7));
                        break;

                    default:
                        fieldIN.DataDeliveryType = dataDeliveryType;
                        headerFooter             = false;
                        break;
                    }

                    if (headerFooter == false)
                    {
                        if (dataDeliveryType == DirectDebitPaymentHelper.DELIVERYTYPE_0690) //Receipt and Remarks
                        {
                            fieldIN.TransactionCode = line.Substring(13, 4);

                            switch (fieldIN.TransactionCode)
                            {
                            case DirectDebitPaymentHelper.TRANSACTIONCODE_0900:
                                fieldIN.CreditorNo         = line.Substring(8, 5);
                                fieldIN.NumberMandates     = DirectDebitPaymentHelper.ConvToInt(line.Substring(17, 7));
                                fieldIN.PaymentDate        = DirectDebitPaymentHelper.ConvToDate(line.Substring(24, 6));
                                fieldIN.CreditorRegNum     = line.Substring(30, 4);
                                fieldIN.CreditorAccNumber  = line.Substring(34, 10);
                                fieldIN.NumberCollection   = DirectDebitPaymentHelper.ConvToInt(line.Substring(44, 7));
                                fieldIN.AmountCollection   = DirectDebitPaymentHelper.ConvToDouble(line.Substring(51, 13));
                                fieldIN.NumberDisbursement = DirectDebitPaymentHelper.ConvToInt(line.Substring(64, 7));
                                fieldIN.AmountDisbursement = DirectDebitPaymentHelper.ConvToDouble(line.Substring(71, 13));
                                break;

                            case DirectDebitPaymentHelper.TRANSACTIONCODE_0910:
                                fieldIN.CreditorNo        = line.Substring(8, 5);
                                fieldIN.RemarkCommentCode = line.Substring(17, 3);
                                fieldIN.CustomerNo        = DirectDebitPaymentHelper.ConvToStr(line.Substring(20, 15));
                                fieldIN.DebtorCVR         = line.Substring(35, 8);
                                fieldIN.DebtorRegNum      = line.Substring(43, 4);
                                fieldIN.DebtorAccNumber   = line.Substring(47, 10);
                                fieldIN.PaymentDate       = DirectDebitPaymentHelper.ConvToDate(line.Substring(57, 6));
                                fieldIN.Amount            = DirectDebitPaymentHelper.ConvToDouble(line.Substring(63, 13));
                                fieldIN.ErrorNumber       = line.Substring(76, 4);
                                break;

                            case DirectDebitPaymentHelper.TRANSACTIONCODE_0920:
                                fieldIN.CreditorNo        = line.Substring(8, 5);
                                fieldIN.RemarkCommentCode = line.Substring(17, 3);
                                fieldIN.CustomerNo        = DirectDebitPaymentHelper.ConvToStr(line.Substring(20, 15));
                                fieldIN.PaymentDate       = DirectDebitPaymentHelper.ConvToDate(line.Substring(35, 6));
                                fieldIN.Amount            = DirectDebitPaymentHelper.ConvToDouble(line.Substring(41, 13));
                                fieldIN.ErrorNumber       = line.Substring(54, 4);
                                fieldIN.CreditorRegNum    = line.Substring(58, 4);
                                fieldIN.CreditorAccNumber = line.Substring(62, 10);
                                break;

                            case DirectDebitPaymentHelper.TRANSACTIONCODE_0930:
                                fieldIN.CreditorNo    = line.Substring(8, 5);
                                fieldIN.ChangeDate    = DirectDebitPaymentHelper.ConvToDate(line.Substring(17, 6));
                                fieldIN.CurCreditorNo = line.Substring(23, 5);
                                fieldIN.CurCustomerNo = DirectDebitPaymentHelper.ConvToStr(line.Substring(28, 15));
                                fieldIN.NewCreditorNo = line.Substring(43, 5);
                                fieldIN.NewCustomerNo = DirectDebitPaymentHelper.ConvToStr(line.Substring(48, 15));
                                break;
                            }
                        }
                        else if (dataDeliveryType == DirectDebitPaymentHelper.DELIVERYTYPE_0602) //Payment Information
                        {
                            fieldIN.TransactionCode = line.Substring(13, 4);

                            switch (fieldIN.TransactionCode)
                            {
                            case DirectDebitPaymentHelper.TRANSACTIONCODE_0500:
                                fieldIN.CreditorNo        = line.Substring(8, 5);
                                fieldIN.TaskNumber        = line.Substring(17, 8);
                                fieldIN.Sign1             = line.Substring(40, 1);
                                fieldIN.Amount1           = DirectDebitPaymentHelper.ConvToDouble(line.Substring(41, 13));
                                fieldIN.Sign2             = line.Substring(54, 1);
                                fieldIN.Amount2           = DirectDebitPaymentHelper.ConvToDouble(line.Substring(55, 13));
                                fieldIN.Sign3             = line.Substring(68, 1);
                                fieldIN.Amount3           = DirectDebitPaymentHelper.ConvToDouble(line.Substring(69, 13));
                                fieldIN.CreditorRegNum    = line.Substring(82, 4);
                                fieldIN.CreditorAccNumber = line.Substring(86, 10);
                                break;

                            case DirectDebitPaymentHelper.TRANSACTIONCODE_0580:
                            case DirectDebitPaymentHelper.TRANSACTIONCODE_0585:
                            case DirectDebitPaymentHelper.TRANSACTIONCODE_0530:
                            case DirectDebitPaymentHelper.TRANSACTIONCODE_0540:
                            case DirectDebitPaymentHelper.TRANSACTIONCODE_0555:
                                fieldIN.CreditorNo        = line.Substring(8, 5);
                                fieldIN.TaskNumber        = line.Substring(17, 8);
                                fieldIN.CustomerNo        = DirectDebitPaymentHelper.ConvToStr(line.Substring(25, 15));
                                fieldIN.PaymentDate       = DirectDebitPaymentHelper.ConvToDate(line.Substring(49, 6));
                                fieldIN.Sign              = line.Substring(55, 1);
                                fieldIN.Amount            = DirectDebitPaymentHelper.ConvToDouble(line.Substring(56, 13));
                                fieldIN.CreditorRegNum    = line.Substring(69, 4);
                                fieldIN.CreditorAccNumber = line.Substring(73, 10);
                                break;
                            }
                        }
                        else if (dataDeliveryType == DirectDebitPaymentHelper.DELIVERYTYPE_30) //Registered and Cancelled mandates
                        {
                            fieldIN.RecordType = line.Substring(0, 3);

                            switch (fieldIN.RecordType)
                            {
                            case DirectDebitPaymentHelper.RECORDTYPE_001:
                                fieldIN.CreditorNo     = line.Substring(3, 5);
                                fieldIN.DataSupplierNo = line.Substring(23, 13);
                                break;

                            case DirectDebitPaymentHelper.RECORDTYPE_510:
                            case DirectDebitPaymentHelper.RECORDTYPE_540:
                            case DirectDebitPaymentHelper.RECORDTYPE_500:
                                fieldIN.CreditorNo   = line.Substring(3, 5);
                                fieldIN.CustomerNo   = DirectDebitPaymentHelper.ConvToStr(line.Substring(8, 15));
                                fieldIN.CreationDate = DirectDebitPaymentHelper.ConvToDate(string.Format("{0}{1}{2}", line.Substring(23, 2), line.Substring(25, 2), line.Substring(27, 2)));
                                break;
                            }
                        }
                    }

                    listOfTrans.Add(fieldIN);
                }
            }

            updateReceipt();
        }
        //TODO:Validering skal checke om PaymentType er angivet til VendorBankAccount
        //TODO:Der skal valideres for at Debtor CVR nummer er udfyldt
        //Hvad sker der hvis bankkonto ændres uden at NETS har fået besked - der kommer sandsynligvis fejlmelding. Kan det fanges inden ved at kigge i NETS oplysninger angående kunden?
        //TODO:Der må kun være en Collection or Disbursement pr. Debitor pr. dag - det skal der valideres for


        /// <summary>
        /// Creates a NETS Leverandørservice file.
        /// </summary>
        internal void /*DirectDebitGenerateResult*/ StreamToFile(Company company, IEnumerable <DebtorTransDirectDebit> lstDebtorTransDirectDebit, SQLCache DebtorCache, StreamWriter sw)
        {
            comp = company;
            var oldPaymentDate = DateTime.MinValue;

            buildRecordType000();
            foreach (var trans in lstDebtorTransDirectDebit.OrderBy(s => s._PaymentDate).ThenBy(s => s._PaymentAmount).ThenBy(s => s.Account))
            {
                paymentDate   = trans.PaymentDate;
                debtorAccount = trans.Account;
                var debtor = (Debtor)DebtorCache.Get(trans.Account);

                debtorRegNum      = DirectDebitPaymentHelper.DebtorBBAN(debtor._PaymentId).Item1;
                debtorBankAccount = DirectDebitPaymentHelper.DebtorBBAN(debtor._PaymentId).Item2;
                debtorCvrNumber   = debtor?._LegalIdent == null ? "99999999" : Regex.Replace(debtor?._LegalIdent, "[^0-9]", ""); //TODO:SKAL RETTES
                paymentAmountStr  = NumberConvert.ToLong(Math.Abs(trans._PaymentAmount) * 100d).ToString();

                paymentAmountTotal += trans._PaymentAmount;
                countDebtors++;

                if (trans.PaymentDate != oldPaymentDate)
                {
                    buildRecordType001();
                }

                //TODO: Registration and Cancellation of mandate - skal håndteres i egen sektion
                //ls.buildRecordType510();
                //ls.buildRecordType540();

                if (trans._PaymentAmount > 0)
                {
                    amountCollectionTotal += trans._PaymentAmount;
                    buildRecordType580();
                }
                else
                {
                    amountDisbursementTotal += trans._PaymentAmount;
                    buildRecordType585();
                }

                //TODO: Change of mandate - skal håndteres i egen sektion
                //ls.buildRecordType595();

                oldPaymentDate = trans.PaymentDate;
            }
            buildRecordType999();

            if (listOfTrans.Count > 0)
            {
                IEnumerable <LSFieldsOUT> queryTrans = listOfTrans.AsEnumerable();//.OrderBy(s => s.LineNumber);

                foreach (var trans in queryTrans)
                {
                    writeRecordType000(sw, trans);
                    writeRecordType001(sw, trans);
                    //ls.writeRecordType510(writer, trans);
                    //ls.writeRecordType540(writer, trans);
                    writeRecordType580(sw, trans);
                    writeRecordType585(sw, trans);
                    writeRecordType595(sw, trans);
                    writeRecordType999(sw, trans);
                }
            }

            //return new DirectDebitGenerateResult(sw, countDebtors);
        }