2014-07-16 20:35:03 +02:00
|
|
|
using System;
|
2015-06-07 01:04:09 +02:00
|
|
|
using System.Collections.Generic;
|
2014-07-16 20:35:03 +02:00
|
|
|
using System.Net;
|
2015-06-07 01:04:09 +02:00
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Net.Http.Headers;
|
|
|
|
|
using System.Web.Http;
|
|
|
|
|
using System.Web.Profile;
|
2014-09-18 13:58:43 +02:00
|
|
|
using System.Web.Security;
|
2014-10-25 23:54:19 +02:00
|
|
|
using Yavsc.Formatters;
|
2015-06-07 01:04:09 +02:00
|
|
|
using Yavsc.Helpers;
|
2015-01-23 13:26:34 +01:00
|
|
|
using Yavsc.Model;
|
2015-01-28 15:04:07 +01:00
|
|
|
using Yavsc.Model.FrontOffice;
|
2015-06-07 01:04:09 +02:00
|
|
|
using Yavsc.Model.RolesAndMembers;
|
|
|
|
|
using Yavsc.Model.WorkFlow;
|
|
|
|
|
using System.IO;
|
2014-07-16 20:35:03 +02:00
|
|
|
|
|
|
|
|
namespace Yavsc.ApiControllers
|
|
|
|
|
{
|
2015-01-27 00:41:20 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Front office controller.
|
|
|
|
|
/// </summary>
|
2014-07-16 20:35:03 +02:00
|
|
|
public class FrontOfficeController : ApiController
|
|
|
|
|
{
|
2015-01-27 00:41:20 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// The wfmgr.
|
|
|
|
|
/// </summary>
|
2014-12-20 02:11:51 +01:00
|
|
|
protected WorkFlowManager wfmgr = null;
|
|
|
|
|
|
2015-01-27 00:41:20 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Initialize the specified controllerContext.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="controllerContext">Controller context.</param>
|
2014-12-20 02:11:51 +01:00
|
|
|
protected override void Initialize (System.Web.Http.Controllers.HttpControllerContext controllerContext)
|
|
|
|
|
{
|
|
|
|
|
base.Initialize (controllerContext);
|
|
|
|
|
wfmgr = new WorkFlowManager ();
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-27 00:41:20 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Catalog this instance.
|
|
|
|
|
/// </summary>
|
2015-01-29 20:59:50 +01:00
|
|
|
[AcceptVerbs ("GET")]
|
2014-07-16 20:35:03 +02:00
|
|
|
public Catalog Catalog ()
|
|
|
|
|
{
|
2015-03-04 14:41:35 +01:00
|
|
|
Catalog c = CatalogManager.GetCatalog ();
|
2014-09-23 12:25:41 +02:00
|
|
|
return c;
|
2014-07-16 20:35:03 +02:00
|
|
|
}
|
2015-01-29 20:59:50 +01:00
|
|
|
|
2015-01-27 00:41:20 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the product categorie.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>The product categorie.</returns>
|
|
|
|
|
/// <param name="brandName">Brand name.</param>
|
|
|
|
|
/// <param name="prodCategorie">Prod categorie.</param>
|
2015-01-29 20:59:50 +01:00
|
|
|
[AcceptVerbs ("GET")]
|
2014-07-16 20:35:03 +02:00
|
|
|
public ProductCategory GetProductCategorie (string brandName, string prodCategorie)
|
|
|
|
|
{
|
2015-03-04 14:41:35 +01:00
|
|
|
return CatalogManager.GetCatalog ().GetBrand (brandName).GetProductCategory (prodCategorie);
|
2014-07-16 20:35:03 +02:00
|
|
|
}
|
|
|
|
|
|
2014-10-01 15:34:00 +02:00
|
|
|
/// <summary>
|
2014-10-06 03:05:57 +02:00
|
|
|
/// Gets the estimate.
|
2014-10-01 15:34:00 +02:00
|
|
|
/// </summary>
|
2014-10-06 03:05:57 +02:00
|
|
|
/// <returns>The estimate.</returns>
|
2015-03-04 14:41:35 +01:00
|
|
|
/// <param name="id">Estimate Id.</param>
|
2015-01-27 00:41:20 +01:00
|
|
|
[Authorize]
|
|
|
|
|
[HttpGet]
|
2015-03-04 14:41:35 +01:00
|
|
|
public Estimate GetEstimate (long id)
|
2014-09-18 13:58:43 +02:00
|
|
|
{
|
2015-03-04 14:41:35 +01:00
|
|
|
Estimate est = wfmgr.ContentProvider.GetEstimate (id);
|
2014-10-06 03:05:57 +02:00
|
|
|
return est;
|
2014-09-18 13:58:43 +02:00
|
|
|
}
|
2014-10-06 03:05:57 +02:00
|
|
|
|
2015-01-27 00:41:20 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the estim tex.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>The estim tex.</returns>
|
2015-03-04 14:41:35 +01:00
|
|
|
/// <param name="id">Estimate id.</param>
|
2015-01-29 20:59:50 +01:00
|
|
|
[AcceptVerbs ("GET")]
|
2015-03-04 14:41:35 +01:00
|
|
|
public HttpResponseMessage EstimateToTex (long id)
|
2014-10-25 23:54:19 +02:00
|
|
|
{
|
2015-04-17 00:01:20 +02:00
|
|
|
string texest = estimateToTex (id);
|
2014-11-03 16:55:09 +01:00
|
|
|
if (texest == null)
|
2015-04-17 00:01:20 +02:00
|
|
|
throw new InvalidOperationException (
|
|
|
|
|
"Not an estimate");
|
2015-06-06 01:43:36 +02:00
|
|
|
HttpResponseMessage result = new HttpResponseMessage () {
|
2014-10-25 23:54:19 +02:00
|
|
|
Content = new ObjectContent (typeof(string),
|
2014-11-03 16:55:09 +01:00
|
|
|
texest,
|
2014-10-31 11:41:28 +01:00
|
|
|
new SimpleFormatter ("text/x-tex"))
|
2014-10-25 23:54:19 +02:00
|
|
|
};
|
2015-06-06 01:43:36 +02:00
|
|
|
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue ("attachment") {
|
|
|
|
|
FileName = "estimate-" + id.ToString () + ".tex"
|
|
|
|
|
};
|
|
|
|
|
return result;
|
2014-10-25 23:54:19 +02:00
|
|
|
}
|
|
|
|
|
|
2015-01-30 12:08:40 +01:00
|
|
|
private string estimateToTex (long estimid)
|
2014-10-25 23:54:19 +02:00
|
|
|
{
|
2015-01-29 20:59:50 +01:00
|
|
|
Yavsc.templates.Estim tmpe = new Yavsc.templates.Estim ();
|
2014-12-20 02:11:51 +01:00
|
|
|
Estimate e = wfmgr.GetEstimate (estimid);
|
2015-01-29 20:59:50 +01:00
|
|
|
tmpe.Session = new Dictionary<string,object> ();
|
2014-10-25 23:54:19 +02:00
|
|
|
tmpe.Session.Add ("estim", e);
|
2015-01-29 20:59:50 +01:00
|
|
|
Profile prpro = new Profile (ProfileBase.Create (e.Responsible));
|
2015-01-24 17:30:21 +01:00
|
|
|
if (!prpro.HasBankAccount)
|
2015-01-29 20:59:50 +01:00
|
|
|
throw new TemplateException ("NotBankable:" + e.Responsible);
|
2015-06-06 01:06:38 +02:00
|
|
|
if (!prpro.HasPostalAddress)
|
|
|
|
|
throw new TemplateException ("NoPostalAddress:" + e.Responsible);
|
|
|
|
|
|
2015-01-29 20:59:50 +01:00
|
|
|
Profile prcli = new Profile (ProfileBase.Create (e.Client));
|
2014-11-03 16:55:09 +01:00
|
|
|
if (!prcli.IsBillable)
|
2015-01-29 20:59:50 +01:00
|
|
|
throw new TemplateException ("NotBillable:" + e.Client);
|
2015-06-06 01:06:38 +02:00
|
|
|
|
|
|
|
|
|
2014-11-03 16:55:09 +01:00
|
|
|
tmpe.Session.Add ("from", prpro);
|
|
|
|
|
tmpe.Session.Add ("to", prcli);
|
2015-06-10 23:38:18 +02:00
|
|
|
tmpe.Session.Add ("efrom", Membership.GetUser (e.Responsible).Email);
|
|
|
|
|
tmpe.Session.Add ("efrom", Membership.GetUser (e.Client).Email);
|
2014-10-25 23:54:19 +02:00
|
|
|
tmpe.Init ();
|
|
|
|
|
return tmpe.TransformText ();
|
|
|
|
|
}
|
2014-11-03 16:55:09 +01:00
|
|
|
|
2014-10-25 23:54:19 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the estimate in pdf format from tex generation.
|
|
|
|
|
/// </summary>
|
2015-01-30 12:08:40 +01:00
|
|
|
/// <returns>The to pdf.</returns>
|
2015-05-16 00:50:11 +02:00
|
|
|
/// <param name="id">Estimid.</param>
|
2015-01-30 12:08:40 +01:00
|
|
|
[AcceptVerbs("GET")]
|
2015-04-17 00:01:20 +02:00
|
|
|
public HttpResponseMessage EstimateToPdf (long id)
|
2014-10-25 23:54:19 +02:00
|
|
|
{
|
2015-01-30 12:08:40 +01:00
|
|
|
string texest = null;
|
|
|
|
|
try {
|
2015-04-17 00:01:20 +02:00
|
|
|
texest = estimateToTex (id);
|
2015-01-30 12:08:40 +01:00
|
|
|
} catch (TemplateException ex) {
|
|
|
|
|
return new HttpResponseMessage (HttpStatusCode.OK) { Content =
|
|
|
|
|
new ObjectContent (typeof(string),
|
|
|
|
|
ex.Message, new ErrorHtmlFormatter (HttpStatusCode.NotAcceptable,
|
2015-03-22 14:08:15 +01:00
|
|
|
LocalizedText.DocTemplateException
|
2015-01-30 12:08:40 +01:00
|
|
|
))
|
|
|
|
|
};
|
|
|
|
|
} 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 =
|
2015-04-17 00:01:20 +02:00
|
|
|
new ObjectContent (typeof(string), "Not an estimation id:" + id,
|
2015-01-30 12:08:40 +01:00
|
|
|
new ErrorHtmlFormatter (HttpStatusCode.NotFound,
|
|
|
|
|
LocalizedText.Estimate_not_found))
|
|
|
|
|
};
|
2014-10-25 23:54:19 +02:00
|
|
|
|
2015-06-07 01:04:09 +02:00
|
|
|
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");
|
2015-06-06 01:43:36 +02:00
|
|
|
return result;
|
2014-10-25 23:54:19 +02:00
|
|
|
}
|
2015-04-02 23:40:03 +02:00
|
|
|
|
2015-04-15 03:03:20 +02:00
|
|
|
private HttpResponseMessage DefaultResponse()
|
|
|
|
|
{
|
|
|
|
|
return ModelState.IsValid ?
|
|
|
|
|
Request.CreateResponse (System.Net.HttpStatusCode.OK) :
|
|
|
|
|
Request.CreateResponse (System.Net.HttpStatusCode.BadRequest,
|
|
|
|
|
ValidateAjaxAttribute.GetErrorModelObject (ModelState));
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-22 14:08:15 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Register the specified model.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model">Model.</param>
|
2015-04-15 03:03:20 +02:00
|
|
|
[Authorize()]
|
2015-03-22 14:08:15 +01:00
|
|
|
[ValidateAjaxAttribute]
|
2015-04-17 00:01:20 +02:00
|
|
|
public HttpResponseMessage Register ([FromBody] RegisterClientModel model)
|
2015-03-22 14:08:15 +01:00
|
|
|
{
|
2015-04-15 03:03:20 +02:00
|
|
|
if (ModelState.IsValid) {
|
|
|
|
|
if (model.IsApprouved)
|
|
|
|
|
if (!Roles.IsUserInRole ("Admin"))
|
|
|
|
|
if (!Roles.IsUserInRole ("FrontOffice")) {
|
2015-06-14 23:47:34 +02:00
|
|
|
ModelState.AddModelError ("Register",
|
2015-04-15 03:03:20 +02:00
|
|
|
"Since you're not member of Admin or FrontOffice groups, " +
|
|
|
|
|
"you cannot ask for a pre-approuved registration");
|
|
|
|
|
return DefaultResponse ();
|
|
|
|
|
}
|
|
|
|
|
MembershipCreateStatus mcs;
|
|
|
|
|
var user = Membership.CreateUser (
|
|
|
|
|
model.UserName,
|
|
|
|
|
model.Password,
|
|
|
|
|
model.Email,
|
|
|
|
|
null,
|
|
|
|
|
null,
|
|
|
|
|
model.IsApprouved,
|
|
|
|
|
out mcs);
|
|
|
|
|
switch (mcs) {
|
|
|
|
|
case MembershipCreateStatus.DuplicateEmail:
|
|
|
|
|
ModelState.AddModelError ("Email", "Cette adresse e-mail correspond " +
|
2015-03-22 14:08:15 +01:00
|
|
|
"à un compte utilisateur existant");
|
2015-04-15 03:03:20 +02:00
|
|
|
break;
|
|
|
|
|
case MembershipCreateStatus.DuplicateUserName:
|
|
|
|
|
ModelState.AddModelError ("UserName", "Ce nom d'utilisateur est " +
|
2015-03-22 14:08:15 +01:00
|
|
|
"déjà enregistré");
|
2015-04-15 03:03:20 +02:00
|
|
|
break;
|
|
|
|
|
case MembershipCreateStatus.Success:
|
|
|
|
|
if (!model.IsApprouved)
|
|
|
|
|
Yavsc.Helpers.YavscHelpers.SendActivationEmail (user);
|
2015-04-17 00:01:20 +02:00
|
|
|
ProfileBase prtu = ProfileBase.Create (model.UserName);
|
|
|
|
|
prtu.SetPropertyValue("Name",model.Name);
|
|
|
|
|
prtu.SetPropertyValue("Address",model.Address);
|
|
|
|
|
prtu.SetPropertyValue("CityAndState",model.CityAndState);
|
|
|
|
|
prtu.SetPropertyValue("Mobile",model.Mobile);
|
|
|
|
|
prtu.SetPropertyValue("Phone",model.Phone);
|
|
|
|
|
prtu.SetPropertyValue("ZipCode",model.ZipCode);
|
2015-04-15 03:03:20 +02:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2015-03-22 14:08:15 +01:00
|
|
|
}
|
2015-04-15 03:03:20 +02:00
|
|
|
return DefaultResponse ();
|
2015-03-22 14:08:15 +01:00
|
|
|
}
|
2014-07-16 20:35:03 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|