public async Task <Client> AddGfiToClient(string clientId, Client client, GfiUpload gfi)
        {
            var filter = Builders <User> .Filter.Eq("ClientId", clientId);

            var clients = await this._usersCollection.Find(filter).Project(user => user.Clients).FirstOrDefaultAsync();

            LogCosmosDBRequestCharge();

            List <Client> clientsTmp = clients != null?clients.ToList <Client>() : new List <Client>();

            int index = clientsTmp.FindIndex(c => c.Oib == client.Oib);

            if (index != -1)
            {
                clientsTmp[index].Update(client.Name, client.Place, client.County, client.City);
                clientsTmp[index].AddGfiUpload(gfi);
            }
            else
            {
                client.AddGfiUpload(gfi);
                clientsTmp.Add(client);
            }

            var update = Builders <User> .Update.Set($"Clients", clientsTmp.AsEnumerable <Client>());

            await this._usersCollection.UpdateOneAsync(filter, update);

            LogCosmosDBRequestCharge();

            return(client);
        }
Beispiel #2
0
        public async Task <IActionResult> UploadGfiDocument(IFormFile gfiExcel)
        {
            string            clientId = User.GetClientId();
            GodisnjiIzvjestaj gi       = null;

            try
            {
                if (gfiExcel.Length <= 0)
                {
                    throw new Exception("Empty gfi file.");
                }
                Stream s1 = new MemoryStream();
                Stream s2 = new MemoryStream();

                await gfiExcel.CopyToAsync(s1);

                await gfiExcel.CopyToAsync(s2);

                s1.Position = 0;
                s2.Position = 0;

                gi = GodisnjiIzvjestaj.LoadFromGFI(s1);

                Client c = new Client()
                {
                    Oib    = gi.Obveznik.OIB,
                    Name   = gi.Obveznik.Naziv,
                    City   = gi.Obveznik.NazivGradaIliOpcine,
                    County = gi.Obveznik.NazivZupanije,
                    Place  = gi.Obveznik.NazivNaselja
                };

                GfiUpload gfi = new GfiUpload()
                {
                    Filename        = $"GFI-{gi?.Obveznik?.OIB}-{gi?.Godina.ToString()}.xls",
                    Year            = gi.Godina,
                    ActivityCode    = gi.Obveznik.NKDSifra,
                    ActivityName    = gi.Obveznik.NKDOpis,
                    CompanyName     = gi.Obveznik.Naziv,
                    Oib             = gi.Obveznik.OIB,
                    Period          = $"{gi.DatumOd.ToString("dd.MM.yyyy.")} - {gi.DatumDo.ToString("dd.MM.yyyy.")}",
                    SubjectTypeCode = gi.Obveznik.SifraVrstePoslovnogSubjekta,
                    SubjectTypeName = gi.Obveznik.NazivVrstePoslovnogSubjekta
                };

                await this._usersService.AddGfiToClient(clientId, c, gfi);

                s2.Position = 0;
                await this._fileStorageService.UploadFile(s2, clientId, gfi.Filename);
            }
            catch (Exception ex)
            {
                return(BadRequest(new { Message = $"Došlo je do pogreške. [Output]: {ex.Message}" }));
            }

            return(Ok(new { Oib = gi.Obveznik.OIB, Year = gi.Godina }));
        }