Beispiel #1
0
        public async Task <IActionResult> NewOffer(OfferViewModel model)
        {
            if (ModelState.IsValid)
            {
                Users user = UserRepository.FindUserByEmail(User.Identity.Name);
                //var customer = wefactconnector.GetCustomerInfo(model.DebtorNumber);
                var settingdocument = SettingsRepository.getSpecificSetting("DocumentCode");
                int documentcode    = int.Parse(settingdocument.Value);
                var code            = documentcode.ToString("000");

                var offerte = new Offers
                {
                    IndexContent = model.IndexContent,
                    ProjectName  = model.ProjectName,
                    //DebtorNumber = model.DebtorNumber,
                    CreatedBy       = user,
                    CreatedAt       = DateTime.Now,
                    LastUpdatedAt   = DateTime.Now,
                    UpdatedBy       = user,
                    DocumentCode    = "PV" + code,
                    DocumentVersion = 1
                };

                await OfferRepository.AddAsync(offerte);

                documentcode++;
                settingdocument.Value = documentcode.ToString();
                await SettingsRepository.SaveChangesAsync();

                var est = new Estimates
                {
                };

                await EstimateRepository.AddAsync(est);

                foreach (var line in model.EstimateLines)
                {
                    var lin = new EstimateLines
                    {
                        HourCost      = line.HourCost,
                        Hours         = line.Hours,
                        Specification = line.Specification,
                        TotalCost     = line.TotalCost
                    };

                    var connect = new EstimateConnects
                    {
                        Estimate      = est,
                        EstimateLines = lin
                    };

                    await EstimateLinesRepository.AddAsync(lin);

                    await EstimateConnectsRepository.AddAsync(connect);
                }

                offerte.Estimate = est;
                await OfferRepository.UpdateAsync(offerte);

                return(Ok());
            }
            else
            {
                return(BadRequest(ModelState));
            }
        }