2014-07-16 20:35:03 +02:00
|
|
|
using System;
|
|
|
|
|
using Yavsc;
|
|
|
|
|
using SalesCatalog;
|
|
|
|
|
using System.Web.Routing;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Web.Http;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Web;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Net;
|
2014-09-18 13:58:43 +02:00
|
|
|
using System.Web.Security;
|
2014-10-06 04:58:47 +02:00
|
|
|
using Yavsc.Model.WorkFlow;
|
2014-10-25 23:54:19 +02:00
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Yavsc.Model.RolesAndMembers;
|
|
|
|
|
using Yavsc.Controllers;
|
|
|
|
|
using Yavsc.Formatters;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Web.Profile;
|
2014-12-09 01:59:38 +01:00
|
|
|
using System.Collections.Specialized;
|
2015-01-23 13:26:34 +01:00
|
|
|
using Yavsc.Model;
|
2015-01-28 15:04:07 +01:00
|
|
|
using Yavsc.Model.FrontOffice;
|
2015-03-22 14:08:15 +01:00
|
|
|
using Yavsc.Helpers;
|
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-01-23 13:26:34 +01:00
|
|
|
string texest = null;
|
|
|
|
|
try {
|
2015-03-04 14:41:35 +01:00
|
|
|
texest = estimateToTex (id);
|
2015-01-29 20:59:50 +01:00
|
|
|
} catch (TemplateException ex) {
|
|
|
|
|
return new HttpResponseMessage (HttpStatusCode.OK) { Content =
|
2015-01-23 13:26:34 +01:00
|
|
|
new ObjectContent (typeof(string),
|
2015-01-29 20:59:50 +01:00
|
|
|
ex.Message, new ErrorHtmlFormatter (HttpStatusCode.NotAcceptable,
|
|
|
|
|
LocalizedText.DocTemplateException
|
|
|
|
|
))
|
|
|
|
|
};
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
return new HttpResponseMessage (HttpStatusCode.OK) { Content =
|
2015-01-23 13:26:34 +01:00
|
|
|
new ObjectContent (typeof(string),
|
2015-01-30 12:08:40 +01:00
|
|
|
ex.Message, new ErrorHtmlFormatter (HttpStatusCode.InternalServerError,
|
|
|
|
|
LocalizedText.DocTemplateException))
|
2015-01-29 20:59:50 +01:00
|
|
|
};
|
2015-01-23 13:26:34 +01:00
|
|
|
}
|
2014-11-03 16:55:09 +01:00
|
|
|
if (texest == null)
|
2015-01-23 13:26:34 +01:00
|
|
|
return new HttpResponseMessage (HttpStatusCode.OK) { Content =
|
2015-03-04 14:41:35 +01: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))
|
2015-01-23 13:26:34 +01:00
|
|
|
};
|
|
|
|
|
|
2014-10-25 23:54:19 +02:00
|
|
|
return new HttpResponseMessage () {
|
|
|
|
|
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-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);
|
2014-11-03 16:55:09 +01:00
|
|
|
|
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);
|
2014-11-03 16:55:09 +01:00
|
|
|
|
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);
|
2014-11-03 16:55:09 +01:00
|
|
|
tmpe.Session.Add ("from", prpro);
|
|
|
|
|
tmpe.Session.Add ("to", prcli);
|
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>
|
2014-10-25 23:54:19 +02:00
|
|
|
/// <param name="estimid">Estimid.</param>
|
2015-01-30 12:08:40 +01:00
|
|
|
[AcceptVerbs("GET")]
|
|
|
|
|
public HttpResponseMessage EstimateToPdf (long estimid)
|
2014-10-25 23:54:19 +02:00
|
|
|
{
|
2015-01-30 12:08:40 +01:00
|
|
|
string texest = null;
|
|
|
|
|
try {
|
|
|
|
|
texest = estimateToTex (estimid);
|
|
|
|
|
} 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 =
|
|
|
|
|
new ObjectContent (typeof(string), "Not an estimation id:" + estimid,
|
|
|
|
|
new ErrorHtmlFormatter (HttpStatusCode.NotFound,
|
|
|
|
|
LocalizedText.Estimate_not_found))
|
|
|
|
|
};
|
2014-10-25 23:54:19 +02:00
|
|
|
|
|
|
|
|
return new HttpResponseMessage () {
|
2015-01-30 12:08:40 +01:00
|
|
|
Content = new ObjectContent (typeof(string),
|
|
|
|
|
texest,
|
|
|
|
|
new TexToPdfFormatter ())
|
2014-10-25 23:54:19 +02:00
|
|
|
};
|
|
|
|
|
}
|
2015-04-02 23:40:03 +02:00
|
|
|
|
2015-03-22 14:08:15 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Register the specified model.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model">Model.</param>
|
|
|
|
|
/// <param name="isApprouved">if false, sends a registration validation e-mail.</param>
|
|
|
|
|
[Authorize(Roles="Admin")]
|
|
|
|
|
[ValidateAjaxAttribute]
|
|
|
|
|
public void Register ([FromBody] RegisterModel model, bool isApprouved=true)
|
|
|
|
|
{
|
|
|
|
|
MembershipCreateStatus mcs;
|
|
|
|
|
var user = Membership.CreateUser (
|
|
|
|
|
model.UserName,
|
|
|
|
|
model.Password,
|
|
|
|
|
model.Email,
|
|
|
|
|
null,
|
|
|
|
|
null,
|
|
|
|
|
isApprouved,
|
|
|
|
|
out mcs);
|
|
|
|
|
switch (mcs) {
|
|
|
|
|
case MembershipCreateStatus.DuplicateEmail:
|
|
|
|
|
ModelState.AddModelError ("Email", "Cette adresse e-mail correspond " +
|
|
|
|
|
"à un compte utilisateur existant");
|
|
|
|
|
return ;
|
|
|
|
|
case MembershipCreateStatus.DuplicateUserName:
|
|
|
|
|
ModelState.AddModelError ("UserName", "Ce nom d'utilisateur est " +
|
|
|
|
|
"déjà enregistré");
|
|
|
|
|
return ;
|
|
|
|
|
case MembershipCreateStatus.Success:
|
|
|
|
|
if (!isApprouved)
|
|
|
|
|
Yavsc.Helpers.YavscHelpers.SendActivationEmail (user);
|
|
|
|
|
return ;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
throw new Exception ( string.Format( "Unexpected membership creation status : {0}", mcs.ToString() ) );
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-16 20:35:03 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|