170 lines
5.0 KiB
C#
170 lines
5.0 KiB
C#
10 years ago
|
using System;
|
||
9 years ago
|
using System.Collections.Generic;
|
||
10 years ago
|
using System.Net;
|
||
9 years ago
|
using System.Net.Http;
|
||
|
using System.Net.Http.Headers;
|
||
|
using System.Web.Http;
|
||
|
using System.Web.Profile;
|
||
10 years ago
|
using System.Web.Security;
|
||
10 years ago
|
using Yavsc.Formatters;
|
||
9 years ago
|
using Yavsc.Helpers;
|
||
10 years ago
|
using Yavsc.Model;
|
||
10 years ago
|
using Yavsc.Model.FrontOffice;
|
||
9 years ago
|
using Yavsc.Model.RolesAndMembers;
|
||
|
using Yavsc.Model.WorkFlow;
|
||
|
using System.IO;
|
||
10 years ago
|
|
||
|
namespace Yavsc.ApiControllers
|
||
|
{
|
||
10 years ago
|
/// <summary>
|
||
|
/// Front office controller.
|
||
|
/// </summary>
|
||
10 years ago
|
public class FrontOfficeController : ApiController
|
||
|
{
|
||
10 years ago
|
/// <summary>
|
||
|
/// The wfmgr.
|
||
|
/// </summary>
|
||
10 years ago
|
protected WorkFlowManager wfmgr = null;
|
||
|
|
||
10 years ago
|
/// <summary>
|
||
|
/// Initialize the specified controllerContext.
|
||
|
/// </summary>
|
||
|
/// <param name="controllerContext">Controller context.</param>
|
||
10 years ago
|
protected override void Initialize (System.Web.Http.Controllers.HttpControllerContext controllerContext)
|
||
|
{
|
||
|
base.Initialize (controllerContext);
|
||
|
wfmgr = new WorkFlowManager ();
|
||
|
}
|
||
|
|
||
10 years ago
|
/// <summary>
|
||
|
/// Catalog this instance.
|
||
|
/// </summary>
|
||
10 years ago
|
[AcceptVerbs ("GET")]
|
||
10 years ago
|
public Catalog Catalog ()
|
||
|
{
|
||
10 years ago
|
Catalog c = CatalogManager.GetCatalog ();
|
||
10 years ago
|
return c;
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
/// <summary>
|
||
|
/// Gets the product categorie.
|
||
|
/// </summary>
|
||
|
/// <returns>The product categorie.</returns>
|
||
|
/// <param name="brandName">Brand name.</param>
|
||
|
/// <param name="prodCategorie">Prod categorie.</param>
|
||
10 years ago
|
[AcceptVerbs ("GET")]
|
||
10 years ago
|
public ProductCategory GetProductCategorie (string brandName, string prodCategorie)
|
||
|
{
|
||
10 years ago
|
return CatalogManager.GetCatalog ().GetBrand (brandName).GetProductCategory (prodCategorie);
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
/// <summary>
|
||
10 years ago
|
/// Gets the estimate.
|
||
10 years ago
|
/// </summary>
|
||
10 years ago
|
/// <returns>The estimate.</returns>
|
||
10 years ago
|
/// <param name="id">Estimate Id.</param>
|
||
10 years ago
|
[Authorize]
|
||
|
[HttpGet]
|
||
10 years ago
|
public Estimate GetEstimate (long id)
|
||
10 years ago
|
{
|
||
10 years ago
|
Estimate est = wfmgr.ContentProvider.GetEstimate (id);
|
||
10 years ago
|
return est;
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
/// <summary>
|
||
|
/// Gets the estim tex.
|
||
|
/// </summary>
|
||
|
/// <returns>The estim tex.</returns>
|
||
10 years ago
|
/// <param name="id">Estimate id.</param>
|
||
10 years ago
|
[AcceptVerbs ("GET")]
|
||
10 years ago
|
public HttpResponseMessage EstimateToTex (long id)
|
||
10 years ago
|
{
|
||
10 years ago
|
string texest = estimateToTex (id);
|
||
10 years ago
|
if (texest == null)
|
||
10 years ago
|
throw new InvalidOperationException (
|
||
|
"Not an estimate");
|
||
9 years ago
|
HttpResponseMessage result = new HttpResponseMessage () {
|
||
10 years ago
|
Content = new ObjectContent (typeof(string),
|
||
10 years ago
|
texest,
|
||
10 years ago
|
new SimpleFormatter ("text/x-tex"))
|
||
10 years ago
|
};
|
||
9 years ago
|
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue ("attachment") {
|
||
|
FileName = "estimate-" + id.ToString () + ".tex"
|
||
|
};
|
||
|
return result;
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
private string estimateToTex (long estimid)
|
||
10 years ago
|
{
|
||
10 years ago
|
Yavsc.templates.Estim tmpe = new Yavsc.templates.Estim ();
|
||
10 years ago
|
Estimate e = wfmgr.GetEstimate (estimid);
|
||
10 years ago
|
tmpe.Session = new Dictionary<string,object> ();
|
||
10 years ago
|
tmpe.Session.Add ("estim", e);
|
||
10 years ago
|
Profile prpro = new Profile (ProfileBase.Create (e.Responsible));
|
||
10 years ago
|
if (!prpro.HasBankAccount)
|
||
10 years ago
|
throw new TemplateException ("NotBankable:" + e.Responsible);
|
||
9 years ago
|
if (!prpro.HasPostalAddress)
|
||
|
throw new TemplateException ("NoPostalAddress:" + e.Responsible);
|
||
|
|
||
10 years ago
|
Profile prcli = new Profile (ProfileBase.Create (e.Client));
|
||
10 years ago
|
if (!prcli.IsBillable)
|
||
10 years ago
|
throw new TemplateException ("NotBillable:" + e.Client);
|
||
9 years ago
|
|
||
|
|
||
10 years ago
|
tmpe.Session.Add ("from", prpro);
|
||
|
tmpe.Session.Add ("to", prcli);
|
||
9 years ago
|
tmpe.Session.Add ("efrom", Membership.GetUser (e.Responsible).Email);
|
||
|
tmpe.Session.Add ("efrom", Membership.GetUser (e.Client).Email);
|
||
10 years ago
|
tmpe.Init ();
|
||
|
return tmpe.TransformText ();
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
/// <summary>
|
||
|
/// Gets the estimate in pdf format from tex generation.
|
||
|
/// </summary>
|
||
10 years ago
|
/// <returns>The to pdf.</returns>
|
||
9 years ago
|
/// <param name="id">Estimid.</param>
|
||
10 years ago
|
[AcceptVerbs("GET")]
|
||
10 years ago
|
public HttpResponseMessage EstimateToPdf (long id)
|
||
10 years ago
|
{
|
||
10 years ago
|
string texest = null;
|
||
|
try {
|
||
10 years ago
|
texest = estimateToTex (id);
|
||
10 years ago
|
} catch (TemplateException ex) {
|
||
|
return new HttpResponseMessage (HttpStatusCode.OK) { Content =
|
||
|
new ObjectContent (typeof(string),
|
||
|
ex.Message, new ErrorHtmlFormatter (HttpStatusCode.NotAcceptable,
|
||
10 years ago
|
LocalizedText.DocTemplateException
|
||
10 years ago
|
))
|
||
|
};
|
||
|
} catch (Exception ex) {
|
||
|
return new HttpResponseMessage (HttpStatusCode.OK) { Content =
|
||
|
new ObjectContent (typeof(string),
|
||
|
ex.Message, new ErrorHtmlFormatter (HttpStatusCode.InternalServerError,
|
||
|
LocalizedText.DocTemplateException))
|
||
|
};
|
||
|
}
|
||
|
if (texest == null)
|
||
|
return new HttpResponseMessage (HttpStatusCode.OK) { Content =
|
||
10 years ago
|
new ObjectContent (typeof(string), "Not an estimation id:" + id,
|
||
10 years ago
|
new ErrorHtmlFormatter (HttpStatusCode.NotFound,
|
||
|
LocalizedText.Estimate_not_found))
|
||
|
};
|
||
10 years ago
|
|
||
9 years ago
|
var memPdf = new MemoryStream ();
|
||
|
HttpResponseMessage result = new HttpResponseMessage ();
|
||
|
new TexToPdfFormatter ().WriteToStream (
|
||
|
typeof(string), texest, memPdf,null);
|
||
|
memPdf.Position = 0;
|
||
|
var sr = new StreamReader(memPdf);
|
||
|
var str = sr.ReadToEnd();
|
||
|
result.Content = new StringContent (str);
|
||
|
TexToPdfFormatter.SetFileName (result.Content.Headers, "estimate-" + id.ToString ());
|
||
|
result.Content.Headers.ContentType = new MediaTypeHeaderValue("text/x-tex");
|
||
9 years ago
|
return result;
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|