// parse TLV for fiscal receipt
        internal void Deserialize(byte[] Data, LeveledFileLogger LeveledLog)
        {
            var NextLeveledLog = LeveledLog.CloneNextLevel();
              PaymentsReceived = false;

              byte Tag = 0;
              byte[] Value = null;
              TLVReader FiscalReader = new TLVReader(Data);
              while (FiscalReader.NextValue(out Tag, out Value))
            switch ((CMPFiscalReceiptTags)Tag)
            {
              case CMPFiscalReceiptTags.Article: // может быть несколько позиций чека
            FiscalArticle Article = new FiscalArticle(this);
            LeveledLog.Message("Article = {");
            Article.Deserialize(Value, NextLeveledLog); // в артикуле у нас все полученные значения
            LeveledLog.Message("}");
            this.Articles.Add(Article);
            break;
              case CMPFiscalReceiptTags.Flags:
            Flags.Receive((FiscalReceiptFlags)TLVReader.ValueToByte(Value));
            LeveledLog.Message("Flags = {0} ({1})", Flags.Value, (int)Flags.Value);
            break;
              case CMPFiscalReceiptTags.AmountWithoutDiscount:
            AmountWithoutDiscount.Receive(TLVReader.ValueToInt32(Value));
            LeveledLog.Message("AmountWithoutDiscount = {0}", AmountWithoutDiscount.Value);
            break;
              case CMPFiscalReceiptTags.DiscountForAmount:
            DiscountForAmount.Receive(TLVReader.ValueToInt32(Value));
            LeveledLog.Message("DiscountForAmount = {0}", DiscountForAmount.Value);
            break;
              case CMPFiscalReceiptTags.Payments: // performed payments
            LeveledLog.Message("Payments = {");
            Payments.Deserialize(Value, NextLeveledLog);
            LeveledLog.Message("}");
            PaymentsReceived = true;
            break;
              default:
            throw new Exception(string.Format("Unknown tag 0x{0:X} received", Tag));
            }
        }
        public byte[] Serialize(LeveledFileLogger LeveledLog)
        {
            var NextLeveledLog = LeveledLog.CloneNextLevel();
              TLVWriter FiscalWriter = new TLVWriter();

              if (Quantity > 0 && Articles.Count > 0) // сериализуем только если есть позиции в чеке и они ненулевые
              {
            foreach (FiscalArticle Article in Articles)
            {
              LeveledLog.Message("Article = {");
              FiscalWriter.AppendTLV((byte)CMPFiscalReceiptTags.Article, Article.Serialize(NextLeveledLog));
              LeveledLog.Message("}");
            }

            if (Flags.Value != FiscalReceiptFlags.None)
            {
              FiscalWriter.AppendTLVByte((byte)CMPFiscalReceiptTags.Flags, (byte)Flags.Value);
              LeveledLog.Message("Flags = {0} ({1})", Flags.Value, (byte)Flags.Value);
            }
            if (AmountWithoutDiscount.Value != 0 || Articles.Count > 0)
            {
              FiscalWriter.AppendTLVInt32((byte)CMPFiscalReceiptTags.AmountWithoutDiscount, (Int32)AmountWithoutDiscount.Value);
              LeveledLog.Message("AmountWithoutDiscount = {0}", AmountWithoutDiscount.Value);
            }
            if (DiscountForAmount.Value != 0)
            {
              FiscalWriter.AppendTLVInt32((byte)CMPFiscalReceiptTags.DiscountForAmount, (Int32)DiscountForAmount.Value);
              LeveledLog.Message("DiscountForAmount = {0}", DiscountForAmount.Value);
            }
            LeveledLog.Message("Payments = {");
            FiscalWriter.AppendTLV((byte)CMPFiscalReceiptTags.Payments, Payments.Serialize(NextLeveledLog));
            LeveledLog.Message("}");
              }

              return FiscalWriter.Bytes;
        }