Пример #1
0
        //// GET: Repairs/Delete/5
        public async Task <IActionResult> Delete(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var repair = await _repairRepository.GetByIdAsync(id.Value);

            if (repair == null)
            {
                return(NotFound());
            }

            return(View(repair));
        }
Пример #2
0
        public async Task <IActionResult> FinishRepair(FinishRepairViewModel model)
        {
            if (model == null)
            {
                return(View(model));
            }

            var repairShedule = await _repairScheduleRepository.GetRepairScheduleFinishAsync(model.RepairScheduleID);

            var scheduleDetail = await _scheduleDetailRepository.GetScheduleDetailByIdAsync(model.ScheduleDetailId);

            var repair = await _repairRepository.GetByIdAsync(model.RepairId);

            var activeSchedule = await _activeScheduleRepository.GetByIdAsync(scheduleDetail.ActiveSchedule.Id);

            var repairHistory = _converterHelper.ToRepairHistory(repairShedule, scheduleDetail);

            var vehicle = await _vehicleRepository.GetUserVehicle(model.VehicleId);

            var user = await _userHelper.GetUserByIdAsync(vehicle.User.Id);



            try
            {
                await _repairHistoryRepository.CreateAsync(repairHistory);

                await _repairScheduleRepository.DeleteAsync(repairShedule);

                await _scheduleDetailRepository.DeleteAsync(scheduleDetail);

                await _repairRepository.DeleteAsync(repair);

                await _activeScheduleRepository.DeleteAsync(activeSchedule);

                return(RedirectToAction("DealershipRepairs"));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.InnerException.Message);
            }

            if (user == null)
            {
                ModelState.AddModelError(string.Empty, "Unable to send email for the user");
            }
            else
            {
                _mailHelper.SendEmail(user.UserName, "Repair finished", $"<h1>Your repair information</h1></br><p>Your vehicle {vehicle.LicencePlate} is ready to be picked up at the workshop</p> </br>");
                ViewBag.Message = "An email was sent to the user with this information";
            }



            return(View(model));
        }
Пример #3
0
        public async Task <GetRepairDetailsResponse> GetRepairAsync(int id, CancellationToken cancellationToken)
        {
            var validator = new IdValidator();
            await validator.ValidateAndThrowAsync(id, null, cancellationToken);

            var result = await _repairRepository.GetByIdAsync(id, cancellationToken,
                                                              include : x => x
                                                              .Include(x => x.UsedParts)
                                                              .ThenInclude(x => x.Part)
                                                              .Include(x => x.RequiredRepairTypes)
                                                              .ThenInclude(x => x.RepairType)
                                                              .Include(x => x.Customer)
                                                              .Include(x => x.EmployeeRepairs)
                                                              .ThenInclude(x => x.User)
                                                              .Include(x => x.Invoice));

            if (result == null)
            {
                throw new ServiceException(ErrorCodes.RepairWithGivenIdNotFound, $"Repair with provided id doesn't exist");
            }

            return(_mapper.Map <GetRepairDetailsResponse>(result));
        }
        public async Task AssignRepairTypeToRepairAsync(AssignRepairTypeToRepairRequest request, CancellationToken cancellationToken)
        {
            var validator     = new IdValidator();
            var listValidator = new ListIdsValidator();
            await validator.ValidateAndThrowAsync(request.RepairId, null, cancellationToken);

            await listValidator.ValidateAndThrowAsync(request.RepairTypeIds, null, cancellationToken);

            var repairResult = await _repairRepository.GetByIdAsync(request.RepairId, cancellationToken);

            if (repairResult == null)
            {
                throw new ServiceException(ErrorCodes.RepairWithGivenIdNotFound, $"Repair with provided id {request.RepairId} doesn't exist");
            }

            foreach (var repairTypeId in request.RepairTypeIds)
            {
                var repairTypeResult = await _repairTypeRepository.GetByIdAsync(repairTypeId, cancellationToken);

                if (repairTypeResult == null)
                {
                    throw new ServiceException(ErrorCodes.RepairTypeWithGivenIdNotFound, $"Repair type with provided id {repairTypeId} doesn't exist");
                }

                if (await _requiredRepairTypeRepository.AnyAsync(x =>
                                                                 x.RepairId == request.RepairId &&
                                                                 x.RepairTypeId == repairTypeId, cancellationToken))
                {
                    throw new ServiceException(ErrorCodes.RepairTypeAlreadyAssignToRepair, $"Repair type with provided id {repairTypeId} already assign to given repair with id {request.RepairId}");
                }
            }

            List <RequiredRepairType> repairTypesToAssign = new List <RequiredRepairType>();

            foreach (var repairTypeId in request.RepairTypeIds)
            {
                RequiredRepairType repairType = new RequiredRepairType()
                {
                    RepairId     = request.RepairId,
                    RepairTypeId = repairTypeId
                };
                repairTypesToAssign.Add(repairType);
            }

            await _requiredRepairTypeRepository.AddRangeAsync(repairTypesToAssign, cancellationToken);
        }
Пример #5
0
        public async Task UpdateRepairUsedPartsAsync(UpdateRepairUsedPartsRequest request, CancellationToken cancellationToken)
        {
            var validator = new UpdateRepairUsedPartsRequestValidator();
            await validator.ValidateAndThrowAsync(request, null, cancellationToken);

            var repairResult = await _repairRepository.GetByIdAsync(request.RepairId, cancellationToken);

            if (repairResult == null)
            {
                throw new ServiceException(ErrorCodes.RepairWithGivenIdNotFound, $"Repair with provided id doesn't exist");
            }

            var partResult = await _partRepository.GetByIdAsync(request.PartId, cancellationToken);

            if (partResult == null)
            {
                throw new ServiceException(ErrorCodes.PartWithGivenIdNotFound, $"Part with provided id doesn't exist");
            }

            if (partResult.Quantity - request.UsedPartQuantity < 0)
            {
                throw new ServiceException(ErrorCodes.NotEnoughtPartsInWarehouse, $"Not enought parts in warehouse");
            }

            if (await _usedPartRepository.AnyAsync(x =>
                                                   x.RepairId == request.RepairId &&
                                                   x.PartId == request.PartId, cancellationToken))
            {
                throw new ServiceException(ErrorCodes.UsedPartAlreadyAssignToRepair, $"Part with provided id {request.PartId} already assign to given repair with id {request.RepairId}");
            }

            UsedPart usedPart = new UsedPart()
            {
                RepairId = request.RepairId,
                PartId   = request.PartId,
                Quantity = request.UsedPartQuantity
            };

            await _usedPartRepository.AddAsync(usedPart, cancellationToken);

            partResult.Quantity -= request.UsedPartQuantity;

            await _partRepository.UpdateAsync(cancellationToken, partResult);
        }
        public async Task <IActionResult> CreateInvoice(int repairId, CancellationToken cancellationToken)
        {
            //Create PDF with PDF/A-3b conformance.
            PdfDocument document = new PdfDocument(PdfConformanceLevel.Pdf_A3B);

            //Set ZUGFeRD profile.
            document.ZugferdConformanceLevel = ZugferdConformanceLevel.Basic;

            //Create border color.
            PdfColor borderColor    = new PdfColor(Color.FromArgb(255, 142, 170, 219));
            PdfBrush lightBlueBrush = new PdfSolidBrush(new PdfColor(Color.FromArgb(255, 91, 126, 215)));

            PdfBrush darkBlueBrush = new PdfSolidBrush(new PdfColor(Color.FromArgb(255, 65, 104, 209)));

            PdfBrush whiteBrush = new PdfSolidBrush(new PdfColor(Color.FromArgb(255, 255, 255, 255)));
            PdfPen   borderPen  = new PdfPen(borderColor, 1f);

            string path       = _webHostEnvironment.ContentRootPath + "/arial.ttf";
            Stream fontStream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);

            //Create TrueType font.
            PdfTrueTypeFont headerFont       = new PdfTrueTypeFont(fontStream, 30, PdfFontStyle.Regular);
            PdfTrueTypeFont arialRegularFont = new PdfTrueTypeFont(fontStream, 9, PdfFontStyle.Regular);
            PdfTrueTypeFont arialBoldFont    = new PdfTrueTypeFont(fontStream, 11, PdfFontStyle.Regular);

            const float margin       = 30;
            const float lineSpace    = 7;
            const float headerHeight = 90;

            //Add page to the PDF.
            PdfPage page = document.Pages.Add();

            PdfGraphics graphics = page.Graphics;

            //Get the page width and height.
            float pageWidth  = page.GetClientSize().Width;
            float pageHeight = page.GetClientSize().Height;

            //Draw page border
            graphics.DrawRectangle(borderPen, new RectangleF(0, 0, pageWidth, pageHeight));

            //Fill the header with light Brush.
            graphics.DrawRectangle(lightBlueBrush, new RectangleF(0, 0, pageWidth, headerHeight));

            RectangleF headerAmountBounds = new RectangleF(400, 0, pageWidth - 400, headerHeight);

            graphics.DrawString("INVOICE", headerFont, whiteBrush, new PointF(margin, headerAmountBounds.Height / 3));

            graphics.DrawRectangle(darkBlueBrush, headerAmountBounds);

            graphics.DrawString("Amount", arialRegularFont, whiteBrush, headerAmountBounds, new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle));

            var repair = await _repairService.GetRepairAsync(repairId, cancellationToken);

            PdfTextElement textElement = new PdfTextElement("Invoice Number: " + repair.Id, arialRegularFont);

            PdfLayoutResult layoutResult = textElement.Draw(page, new PointF(headerAmountBounds.X - (margin + 10), 120));

            textElement.Text = "Date : " + DateTime.Now.ToString("dddd dd, MMMM yyyy");
            textElement.Draw(page, new PointF(layoutResult.Bounds.X, layoutResult.Bounds.Bottom + lineSpace));

            textElement.Text = "Bill To:";
            layoutResult     = textElement.Draw(page, new PointF(margin, 120));

            textElement.Text = repair.CustomerFirstName + " " + repair.CustomerLastName;
            layoutResult     = textElement.Draw(page, new PointF(margin, layoutResult.Bounds.Bottom + lineSpace));
            textElement.Text = repair.CustomerEmail;
            layoutResult     = textElement.Draw(page, new PointF(margin, layoutResult.Bounds.Bottom + lineSpace));
            textElement.Text = repair.CustomerPhoneNumber;
            layoutResult     = textElement.Draw(page, new PointF(margin, layoutResult.Bounds.Bottom + lineSpace));

            PdfGrid grid = new PdfGrid();

            List <CreateInvoicePDFModel> model = new List <CreateInvoicePDFModel>();
            int           i = 1;
            const decimal TaxNetValueValue  = 0.77M;
            const decimal VATValue          = 0.23M;
            decimal       summaryPartsPrice = 0;

            foreach (var u in repair.UsedParts)
            {
                decimal TaxValueForList1 = u.PartBoughtPrice * u.Quantity * VATValue;
                decimal NetPriceForList1 = u.PartBoughtPrice * TaxNetValueValue;
                decimal NetValueForList1 = u.PartBoughtPrice * u.Quantity * TaxNetValueValue;
                TaxValueForList1 = Math.Round(TaxValueForList1, 2);
                NetPriceForList1 = Math.Round(NetPriceForList1, 2);
                NetValueForList1 = Math.Round(NetValueForList1, 2);
                model.Add(new CreateInvoicePDFModel
                {
                    Id           = i,
                    Name         = u.Name,
                    Quantity     = (int)u.Quantity,
                    NetPrice     = NetPriceForList1,
                    NetValue     = NetValueForList1,
                    Tax          = (VATValue * 100).ToString() + "%",
                    TaxValue     = TaxValueForList1,
                    SummaryPrice = u.PartBoughtPrice * u.Quantity
                });

                summaryPartsPrice += u.PartBoughtPrice * u.Quantity;
                i++;
            }
            string allRepairNames = "";

            foreach (var repairType in repair.RepairTypes)
            {
                allRepairNames += repairType.Name + "\n";
            }

            decimal TaxValueForList2 = repair.RepairCost * VATValue;
            decimal NetPriceForList2 = repair.RepairCost * TaxNetValueValue;
            decimal NetValueForList2 = repair.RepairCost * TaxNetValueValue;

            TaxValueForList2 = Math.Round(TaxValueForList2, 2);
            NetPriceForList2 = Math.Round(NetPriceForList2, 2);
            NetValueForList2 = Math.Round(NetValueForList2, 2);
            model.Add(new CreateInvoicePDFModel
            {
                Id           = i,
                Name         = allRepairNames,
                Quantity     = 1,
                NetPrice     = NetPriceForList2,
                NetValue     = NetValueForList2,
                Tax          = (VATValue * 100).ToString() + "%",
                TaxValue     = TaxValueForList2,
                SummaryPrice = repair.RepairCost
            });

            grid.DataSource = model;

            grid.Columns[1].Width      = 150;
            grid.Style.Font            = arialRegularFont;
            grid.Style.CellPadding.All = 5;

            grid.ApplyBuiltinStyle(PdfGridBuiltinStyle.ListTable4Accent5);

            layoutResult = grid.Draw(page, new PointF(0, layoutResult.Bounds.Bottom + 40));

            textElement.Text = "Grand Total: ";
            textElement.Font = arialBoldFont;
            layoutResult     = textElement.Draw(page, new PointF(headerAmountBounds.X - 40, layoutResult.Bounds.Bottom + lineSpace));

            decimal totalAmount = repair.RepairCost + summaryPartsPrice;

            textElement.Text = totalAmount.ToString() + "PLN";
            layoutResult     = textElement.Draw(page, new PointF(layoutResult.Bounds.Right + 4, layoutResult.Bounds.Y));

            graphics.DrawString(totalAmount.ToString() + "PLN", arialBoldFont, whiteBrush, new RectangleF(400, lineSpace, pageWidth - 400, headerHeight + 15), new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle));


            borderPen.DashStyle   = PdfDashStyle.Custom;
            borderPen.DashPattern = new float[] { 3, 3 };

            PdfLine line = new PdfLine(borderPen, new PointF(0, 0), new PointF(pageWidth, 0));

            layoutResult = line.Draw(page, new PointF(0, pageHeight - 100));

            textElement.Text = "Computer Services Adam Paluch";
            textElement.Font = arialRegularFont;
            layoutResult     = textElement.Draw(page, new PointF(margin, layoutResult.Bounds.Bottom + (lineSpace * 3)));
            textElement.Text = "ul. Krakowska 19/2\n" +
                               "02-20 Warsaw\n" +
                               "NIP: 8127749027";
            layoutResult     = textElement.Draw(page, new PointF(margin, layoutResult.Bounds.Bottom + lineSpace));
            textElement.Text = "Any Questions? [email protected]";
            layoutResult     = textElement.Draw(page, new PointF(margin, layoutResult.Bounds.Bottom + lineSpace));

            var        fileName   = "Invoice" + repair.Id + ".pdf";
            FileStream fileStream = new FileStream(fileName, FileMode.CreateNew, FileAccess.ReadWrite);

            document.Save(fileStream);
            var filePath = fileStream.Name;

            document.Close(true);
            fileStream.Close();

            var invoiceId = await _invoiceService.AddInvoiceAsync(filePath, cancellationToken);

            var repairToUpdate = await _repairRepository.GetByIdAsync(repairId, cancellationToken);

            repairToUpdate.Status         = EnumStatus.Finished;
            repairToUpdate.FinishDateTime = DateTime.Now;
            repairToUpdate.InvoiceId      = invoiceId;
            await _repairRepository.UpdateAsync(cancellationToken, repairToUpdate);

            return(Ok());
        }