-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDFeArquivosConfigBase.cs
More file actions
250 lines (203 loc) · 8.24 KB
/
DFeArquivosConfigBase.cs
File metadata and controls
250 lines (203 loc) · 8.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// ***********************************************************************
// Assembly : OpenAC.Net.DFe.Core
// Author : RFTD
// Created : 01-31-2016
//
// Last Modified By : RFTD
// Last Modified On : 06-07-2016
// ***********************************************************************
// <copyright file="DFeArquivosConfigBase.cs" company="OpenAC .Net">
// The MIT License (MIT)
// Copyright (c) 2014-2022 Grupo OpenAC.Net
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using OpenAC.Net.Core.Extensions;
namespace OpenAC.Net.DFe.Core.Common;
public abstract class DFeArquivosConfigBase<TSchemas> : DFeArquivosConfigBase where TSchemas : Enum
{
#region Methods
/// <summary>
/// Metodo que retorna o caminho para o tipo de schema solicitado.
/// </summary>
/// <param name="schema"></param>
/// <returns></returns>
public abstract string GetSchema(TSchemas schema);
#endregion Methods
}
public abstract class DFeArquivosConfigBase
{
#region Fields
private string pathSalvar;
private string arquivoServicos;
private string pathSchemas;
#endregion Fields
#region Constructors
/// <summary>
/// Inicializa uma nova instancia da classe <see cref="DFeArquivosConfigBase{TSchemas}"/>.
/// </summary>
protected DFeArquivosConfigBase()
{
var path = Path.GetDirectoryName((Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly()).Location) ?? string.Empty;
PathSalvar = Path.Combine(path, "Docs");
PathSchemas = Path.Combine(path, "Schemas");
arquivoServicos = string.Empty;
Salvar = true;
AdicionarLiteral = false;
SepararPorCNPJ = false;
SepararPorModelo = false;
SepararPorAno = false;
SepararPorMes = false;
SepararPorDia = false;
OrdenacaoPath = new List<TagOrdenacaoPath>();
}
#endregion Constructors
#region Properties
/// <summary>
/// Define/retorna o caminho onde deve ser salvo os arquivos.
/// </summary>
public string PathSalvar { get; set; }
/// <summary>
/// Define/retorna o caminho onde estão so schemas.
/// </summary>
public string PathSchemas { get; set; }
/// <summary>
/// Define/retorna o arquivo com os dados dos serviços.
/// </summary>
public string ArquivoServicos
{
get => arquivoServicos;
set
{
if (value == arquivoServicos) return;
arquivoServicos = value ?? string.Empty;
ArquivoServicoChange();
}
}
/// <summary>
/// Define/retorna se deve salvar os arquivos xml, trata-se de arquivos com validade jurídica.
/// </summary>
public bool Salvar { get; set; }
/// <summary>
/// Define se os nomes dos arquivos devem ser padronizados,
/// utilizando a chave da NFSe, Id do DPS ou NSU, conforme o tipo de operação.
/// Quando <c>false</c>, utiliza o comportamento padrão da biblioteca,
/// cujo formato pode variar e não segue um padrão fixo.
/// </summary>
public bool PadronizarNomes { get; set; }
/// <summary>
/// Define/retorna se deve ser adicionado um literal ao caminho de salvamento.
/// </summary>
public bool AdicionarLiteral { get; set; }
/// <summary>
/// Define/retorna se deve ser adicionado o CNPJ ao caminho de salvamento.
/// </summary>
public bool SepararPorCNPJ { get; set; }
/// <summary>
/// Define/retorna se deve ser adicionado o numero do
/// modelo do arquivo DFe ao caminho de salvamento.
/// </summary>
public bool SepararPorModelo { get; set; }
/// <summary>
/// Define/retorna se deve ser adicionado o ano ao caminho de salvamento.
/// </summary>
public bool SepararPorAno { get; set; }
/// <summary>
/// Define/retorna se deve ser adicionado o mês ao caminho de salvamento.
/// </summary>
public bool SepararPorMes { get; set; }
/// <summary>
/// Define/retorna se deve ser adicionado o dia ao caminho de salvamento.
/// </summary>
public bool SepararPorDia { get; set; }
/// <summary>
/// Retorna a ordem de criação dos caminhos para salvamento dos arquivos.
/// </summary>
public List<TagOrdenacaoPath> OrdenacaoPath { get; }
#endregion Properties
#region Methods
/// <summary>
/// Metodo chamado quando muda o caminho do arquivo de serviços.
/// </summary>
protected abstract void ArquivoServicoChange();
/// <summary>
/// Gera um path de salvamento.
/// </summary>
/// <param name="aPath"></param>
/// <param name="aLiteral"></param>
/// <param name="cnpj"></param>
/// <param name="data"></param>
/// <param name="modeloDescr"></param>
/// <returns></returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
protected virtual string GetPath(string aPath, string aLiteral, string cnpj = "", DateTime? data = null, string modeloDescr = "")
{
var dir = aPath.IsEmpty() ? PathSalvar : aPath;
if (!OrdenacaoPath.Any())
{
if (SepararPorCNPJ) OrdenacaoPath.Add(TagOrdenacaoPath.CNPJ);
if (SepararPorModelo) OrdenacaoPath.Add(TagOrdenacaoPath.Modelo);
if (SepararPorAno || SepararPorMes || SepararPorDia) OrdenacaoPath.Add(TagOrdenacaoPath.Data);
if (AdicionarLiteral) OrdenacaoPath.Add(TagOrdenacaoPath.Literal);
}
foreach (var ordenacaoPath in OrdenacaoPath)
{
{
switch (ordenacaoPath)
{
case TagOrdenacaoPath.CNPJ:
if (cnpj.IsEmpty()) continue;
dir = Path.Combine(dir, cnpj.OnlyNumbers());
break;
case TagOrdenacaoPath.Modelo:
if (modeloDescr.IsEmpty()) continue;
dir = Path.Combine(dir, modeloDescr);
break;
case TagOrdenacaoPath.Data:
if (!data.HasValue) data = DateTime.Now;
if (SepararPorAno)
dir = Path.Combine(dir, data.Value.ToString("yyyy"));
if (SepararPorMes)
dir = Path.Combine(dir, data.Value.ToString("MM"));
if (SepararPorDia)
dir = Path.Combine(dir, data.Value.ToString("dd"));
break;
case TagOrdenacaoPath.Literal:
if (aLiteral.IsEmpty()) continue;
if (!dir.ToLower().Contains(aLiteral.ToLower()))
dir = Path.Combine(dir, aLiteral);
break;
case TagOrdenacaoPath.Nenhum:
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
return dir;
}
#endregion Methods
}