| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using System.Linq; |
| | | 3 | | using System.Threading.Tasks; |
| | | 4 | | using Microsoft.EntityFrameworkCore; |
| | | 5 | | using Microsoft.Extensions.Logging; |
| | | 6 | | using WinSolutions.Sveta.Server.Data.DataModel.Contexts; |
| | | 7 | | using WinSolutions.Sveta.Server.Data.DataModel.Entities; |
| | | 8 | | using WinSolutions.Sveta.Server.Services.Interfaces; |
| | | 9 | | using WinSolutions.Sveta.Common; |
| | | 10 | | using WinSolutions.Sveta.Common.Extensions; |
| | | 11 | | |
| | | 12 | | namespace WinSolutions.Sveta.Server.Services.Implements |
| | | 13 | | { |
| | | 14 | | public class BankAccountService : SvetaServiceBase, IBankAccountService |
| | | 15 | | { |
| | | 16 | | private readonly SvetaDbContext _db; |
| | | 17 | | private readonly ILogger<BankAccountService> _logger; |
| | | 18 | | public BankAccountService(SvetaDbContext db, ILogger<BankAccountService> logger, IAuthenticationService authenti |
| | 4 | 19 | | : base(authenticationService) |
| | 4 | 20 | | { |
| | 4 | 21 | | _db = db; |
| | 4 | 22 | | _logger = logger; |
| | 4 | 23 | | } |
| | | 24 | | |
| | 6 | 25 | | public async Task<bool> ContragentExists(long contragentId) => await _db.Contragents |
| | 6 | 26 | | .AsNoTracking() |
| | 6 | 27 | | .Where(e => e.Id == contragentId) |
| | 6 | 28 | | .Where(e => !e.IsDeleted) |
| | 6 | 29 | | .AnyAsync(); |
| | | 30 | | |
| | | 31 | | public async Task<List<BankAccount>> GetBankAccounts(int page, int? limit, string filter, long? contragentId = n |
| | 10 | 32 | | { |
| | 10 | 33 | | var items = _db.BankAccounts |
| | 10 | 34 | | .Include(x => x.Contragent) |
| | 10 | 35 | | .Include(d => d.RecState) |
| | 10 | 36 | | .AsNoTracking() |
| | 10 | 37 | | .Where(d => !d.IsDeleted) |
| | 10 | 38 | | .Where(c=> contragentId == null || c.Contragent.Id == contragentId) |
| | 10 | 39 | | .Where(x => string.IsNullOrWhiteSpace(filter) || x.BankName.ToUpper().Contains(filter.ToUpper()) |
| | 10 | 40 | | || x.Contragent.FullName.ToUpper().Contains(filter.ToUp |
| | 10 | 41 | | || x.Contragent.ShortName.ToUpper().Contains(filter.ToU |
| | | 42 | | |
| | 10 | 43 | | items = Sort(items, sort?.ToLower()); |
| | 10 | 44 | | if(limit != null) |
| | 10 | 45 | | { |
| | 10 | 46 | | items = items.Skip(page * (int)limit) |
| | 10 | 47 | | .Take((int)limit); |
| | 10 | 48 | | } |
| | 10 | 49 | | return |
| | 10 | 50 | | await items |
| | 10 | 51 | | .ToListAsync(); |
| | 10 | 52 | | } |
| | | 53 | | |
| | | 54 | | public async Task<List<BankAccount>> GetBankAccountsForContragent(long contragentId, int page, int limit, string |
| | 6 | 55 | | { |
| | 6 | 56 | | if (!(await ContragentExists(contragentId))) |
| | 0 | 57 | | { |
| | 0 | 58 | | throw new KeyNotFoundException($"Contragent record #{contragentId} not found"); |
| | | 59 | | } |
| | | 60 | | |
| | 6 | 61 | | var items = _db.BankAccounts |
| | 6 | 62 | | .Include(x => x.Contragent) |
| | 6 | 63 | | .Include(d => d.RecState) |
| | 6 | 64 | | .AsNoTracking() |
| | 6 | 65 | | .Where(d => !d.IsDeleted) |
| | 6 | 66 | | .Where(c => c.Contragent.Id == contragentId) |
| | 6 | 67 | | .Where(x => string.IsNullOrWhiteSpace(filter) || (x.BankName.ToUpper().Contains(filter.ToUpper()) |
| | 6 | 68 | | || x.Contragent.FullName.ToUpper().Contains(filter.ToUp |
| | 6 | 69 | | || x.Contragent.ShortName.ToUpper().Contains(filter.ToU |
| | | 70 | | |
| | 6 | 71 | | items = Sort(items, sort?.ToLower()); |
| | | 72 | | |
| | 6 | 73 | | return |
| | 6 | 74 | | await items |
| | 6 | 75 | | .Skip(page * limit) |
| | 6 | 76 | | .Take(limit) |
| | 6 | 77 | | .ToListAsync(); |
| | 6 | 78 | | } |
| | | 79 | | |
| | 8 | 80 | | public async Task<BankAccount> GetBankAccount(long id, long? contragentId = null) => await _db.BankAccounts |
| | 8 | 81 | | .Include(x => x.Contragent) |
| | 8 | 82 | | .Include(d => d.RecState) |
| | 8 | 83 | | .Where(e => !e.IsDeleted) |
| | 8 | 84 | | .Where(c => contragentId == null || c.Contragent.Id == contragentId) |
| | 8 | 85 | | .FirstOrDefaultAsync(n => n.Id == id); |
| | | 86 | | |
| | | 87 | | public async Task<BankAccount> FindBankAccount(long id, long contragentId) |
| | 4 | 88 | | { |
| | 4 | 89 | | return await _db.BankAccounts |
| | 4 | 90 | | .Include(x => x.Contragent) |
| | 4 | 91 | | .Include(d => d.RecState) |
| | 4 | 92 | | .Where(x => !x.IsDeleted && !x.Contragent.IsDeleted) |
| | 4 | 93 | | .FirstOrDefaultAsync(x => x.Id == id && x.Contragent.Id == contragentId); |
| | 4 | 94 | | } |
| | | 95 | | |
| | | 96 | | public async Task<BankAccount> CreateBankAccount(BankAccount data) |
| | 12 | 97 | | { |
| | 12 | 98 | | if(_db.BankAccounts.Any(x => !x.IsDeleted |
| | 12 | 99 | | && x.Bik.ToLower() == data.Bik.NormalizeName().ToLower() |
| | 12 | 100 | | && x.SettlementAccount.ToLower() == data.SettlementAccount.NormalizeName().ToLo |
| | 0 | 101 | | { |
| | 0 | 102 | | throw new System.Exception("Такой счет уже существует"); |
| | | 103 | | } |
| | | 104 | | |
| | 12 | 105 | | _db.BankAccounts.Add(data); |
| | 12 | 106 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 12 | 107 | | return data; |
| | 12 | 108 | | } |
| | | 109 | | |
| | | 110 | | public async Task<BankAccount> UpdateBankAccount(BankAccount data) |
| | 1 | 111 | | { |
| | 1 | 112 | | if (_db.BankAccounts.Any(x => !x.IsDeleted |
| | 1 | 113 | | && x.Bik.ToLower() == data.Bik.NormalizeName().ToLower() |
| | 1 | 114 | | && x.SettlementAccount.ToLower() == data.SettlementAccount.NormalizeName().ToL |
| | 1 | 115 | | && x.Id != data.Id)) |
| | 0 | 116 | | { |
| | 0 | 117 | | throw new System.Exception("Такой счет уже существует"); |
| | | 118 | | } |
| | | 119 | | |
| | 1 | 120 | | _db.BankAccounts.Update(data); |
| | 1 | 121 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 1 | 122 | | return data; |
| | 1 | 123 | | } |
| | | 124 | | |
| | | 125 | | public async Task DeleteBankAccount(long id) |
| | 1 | 126 | | { |
| | 1 | 127 | | var account = _db.BankAccounts.Find(id); |
| | 1 | 128 | | if (account == null) |
| | 0 | 129 | | { |
| | 0 | 130 | | throw new KeyNotFoundException($"Bank account record #{id} not found"); |
| | | 131 | | } |
| | 1 | 132 | | account.IsDeleted = true; |
| | 1 | 133 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 1 | 134 | | } |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// Сортирует BankAccount по полям |
| | | 138 | | /// </summary> |
| | | 139 | | /// <param name="items"></param> |
| | | 140 | | /// <param name="sort">сортировка по умолчанию |
| | | 141 | | /// по id - id|desc, |
| | | 142 | | /// bankName,bankName|desc |
| | | 143 | | /// bik,bik|desc |
| | | 144 | | /// settlementAccount,settlementAccount|desc |
| | | 145 | | /// correspondentAccount,correspondentAccount|desc</param> |
| | | 146 | | /// <returns></returns> |
| | 16 | 147 | | private IQueryable<BankAccount> Sort(IQueryable<BankAccount> items, string sort = default) => (sort ?? "").ToLow |
| | 16 | 148 | | { |
| | 16 | 149 | | "bankname" => items.OrderBy(d => d.BankName), |
| | 16 | 150 | | "bankname|desc" => items.OrderByDescending(d => d.BankName), |
| | 16 | 151 | | "bik" => items.OrderBy(d => d.Bik), |
| | 16 | 152 | | "bik|desc" => items.OrderByDescending(d => d.Bik), |
| | 16 | 153 | | "settlementaccount" => items.OrderBy(d => d.SettlementAccount), |
| | 16 | 154 | | "settlementaccount|desc" => items.OrderByDescending(d => d.SettlementAccount), |
| | 16 | 155 | | "correspondentaccount" => items.OrderBy(d => d.CorrespondentAccount), |
| | 16 | 156 | | "correspondentaccount|desc" => items.OrderByDescending(d => d.CorrespondentAccount), |
| | 16 | 157 | | "id|desc" => items.OrderByDescending(d => d.Id), |
| | 16 | 158 | | _ => items.OrderBy(d => d.Id), |
| | 16 | 159 | | }; |
| | | 160 | | } |
| | | 161 | | } |