mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-10-01 18:03:13 +00:00
142 lines
5 KiB
C#
142 lines
5 KiB
C#
using Data.SeaHavenIndustries;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.JSInterop;
|
|
using SeaHavenIndustries.Data.IRepository;
|
|
using SeaHavenIndustries.Helper;
|
|
using SeaHavenIndustries.Helper.ViewModels;
|
|
using System.Text.Json;
|
|
|
|
namespace SeaHavenIndustries.Data.Services
|
|
{
|
|
public class QuotesService : IQuotesRepository
|
|
{
|
|
private readonly ApplicationDbContext _context;
|
|
private readonly SendMessage _sendemail;
|
|
|
|
public QuotesService(ApplicationDbContext context, SendMessage sendemail)
|
|
{
|
|
_context = context;
|
|
_sendemail = sendemail;
|
|
}
|
|
|
|
public async Task<string> Addquotes(Quotes model)
|
|
{
|
|
_context.ChangeTracker.Clear();
|
|
|
|
try
|
|
{
|
|
var exist = _context.Quotes.Where(w => w.Id == model.Id).FirstOrDefault();
|
|
if (exist != null)
|
|
{
|
|
exist.Date = model.Date;
|
|
exist.WorkorderId = model.WorkorderId;
|
|
exist.ContactId = model.ContactId;
|
|
exist.PO = model.PO;
|
|
exist.TT = model.TT;
|
|
exist.ProjectName = model.ProjectName;
|
|
exist.SiteName = model.SiteName;
|
|
exist.SiteName = model.SiteName;
|
|
exist.SiteId = model.SiteId;
|
|
exist.BillingAddress = model.BillingAddress;
|
|
exist.ShippingAddress = model.ShippingAddress;
|
|
exist.LineItems=model.LineItems;
|
|
_context.Update(exist);
|
|
}
|
|
else
|
|
{
|
|
_context.Quotes.Add(model);
|
|
}
|
|
await _context.SaveChangesAsync();
|
|
return "success";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ex.Message;
|
|
}
|
|
}
|
|
public async Task<List<Quotes>> GetQuotes()
|
|
{
|
|
var list = await _context.Quotes.Include(i=>i.WorkOrder).ThenInclude(w=> w.Locations).Include(i=>i.Contacts).Include(i=>i.LineItems).ToListAsync();
|
|
return list;
|
|
}
|
|
|
|
public async Task<IQueryable<QuotesViewModel>> GetQuotesqueryable()
|
|
{
|
|
var queryable = _context.Quotes
|
|
.Include(i => i.WorkOrder)
|
|
.ThenInclude(w => w.Locations)
|
|
.Include(i => i.Contacts)
|
|
.Include(i => i.LineItems).Select(s=>new QuotesViewModel
|
|
{
|
|
Id=s.Id,
|
|
QouteId="QO-"+s.QuotId,
|
|
WOID =s.WorkOrder!=null?s.WorkOrder.WorkerOrderNumber:"",
|
|
SiteName=s.SiteName,
|
|
ProjectName=s.ProjectName,
|
|
TotalAmount="$"+s.LineItems.Sum(s=>s.Lineitem.Value).ToString(),
|
|
Status=s.WorkOrder.Status,
|
|
|
|
})
|
|
.AsQueryable();
|
|
|
|
return queryable;
|
|
}
|
|
|
|
public async Task<Quotes> GetQuotesById(int id)
|
|
{
|
|
var data = await _context.Quotes.Include(i => i.WorkOrder).ThenInclude(w => w.Locations).Include(i => i.Contacts).Include(i => i.LineItems).Where(w => w.Id == id).FirstOrDefaultAsync();
|
|
return data ?? new Quotes();
|
|
}
|
|
public async Task<string> DeleteQuotes(int id)
|
|
{
|
|
_context.ChangeTracker.Clear();
|
|
|
|
try
|
|
{
|
|
var data = await _context.Quotes.Where(w => w.Id == id).FirstOrDefaultAsync();
|
|
if (data != null)
|
|
{
|
|
var existlineitem=_context.QuotesLines.Where(w=>w.QuoteId==id).ToList();
|
|
if(existlineitem.Any())
|
|
{
|
|
_context.QuotesLines.RemoveRange(existlineitem);
|
|
}
|
|
_context.Quotes.Remove(data);
|
|
}
|
|
await _context.SaveChangesAsync();
|
|
return "success";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ex.Message;
|
|
}
|
|
}
|
|
public async Task<string> SendEmailQuote(byte[] pdfBytes, int Id)
|
|
{
|
|
try
|
|
{
|
|
var data = _context.Quotes.Include(w=>w.WorkOrder).ThenInclude(i=>i.AssignToUser).Where(w => w.Id==Id).FirstOrDefault();
|
|
if (data != null && data.WorkOrder!=null && data.WorkOrder.AssignTo != null)
|
|
{
|
|
var subject = "Quote Details";
|
|
var response = await _sendemail.SendEMailAttachment(data.WorkOrder.AssignToUser.Email ?? "", subject, pdfBytes);
|
|
if (response)
|
|
{
|
|
return "success";
|
|
}
|
|
else
|
|
{
|
|
return "Some thing went wrong";
|
|
}
|
|
}
|
|
|
|
return "Email Not Found";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ex.Message;
|
|
}
|
|
}
|
|
}
|
|
}
|