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; using System.Web.Security; using Yavsc.Model.WorkFlow; using System.Reflection; using System.Collections.Generic; using Yavsc.Model.RolesAndMembers; using Yavsc.Controllers; using Yavsc.Formatters; using System.Text; using System.Web.Profile; using System.Collections.Specialized; using Yavsc.Model; using Yavsc.Model.FrontOffice; using Yavsc.Helpers; namespace Yavsc.ApiControllers { /// /// Front office controller. /// public class FrontOfficeController : ApiController { /// /// The wfmgr. /// protected WorkFlowManager wfmgr = null; /// /// Initialize the specified controllerContext. /// /// Controller context. protected override void Initialize (System.Web.Http.Controllers.HttpControllerContext controllerContext) { base.Initialize (controllerContext); wfmgr = new WorkFlowManager (); } /// /// Catalog this instance. /// [AcceptVerbs ("GET")] public Catalog Catalog () { Catalog c = CatalogManager.GetCatalog (); return c; } /// /// Gets the product categorie. /// /// The product categorie. /// Brand name. /// Prod categorie. [AcceptVerbs ("GET")] public ProductCategory GetProductCategorie (string brandName, string prodCategorie) { return CatalogManager.GetCatalog ().GetBrand (brandName).GetProductCategory (prodCategorie); } /// /// Gets the estimate. /// /// The estimate. /// Estimate Id. [Authorize] [HttpGet] public Estimate GetEstimate (long id) { Estimate est = wfmgr.ContentProvider.GetEstimate (id); return est; } /// /// Gets the estim tex. /// /// The estim tex. /// Estimate id. [AcceptVerbs ("GET")] public HttpResponseMessage EstimateToTex (long id) { string texest = null; try { texest = estimateToTex (id); } catch (TemplateException ex) { return new HttpResponseMessage (HttpStatusCode.OK) { Content = new ObjectContent (typeof(string), ex.Message, new ErrorHtmlFormatter (HttpStatusCode.NotAcceptable, LocalizedText.DocTemplateException )) }; } 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:" + id, new ErrorHtmlFormatter (HttpStatusCode.NotFound, LocalizedText.Estimate_not_found)) }; return new HttpResponseMessage () { Content = new ObjectContent (typeof(string), texest, new SimpleFormatter ("text/x-tex")) }; } private string estimateToTex (long estimid) { Yavsc.templates.Estim tmpe = new Yavsc.templates.Estim (); Estimate e = wfmgr.GetEstimate (estimid); tmpe.Session = new Dictionary (); tmpe.Session.Add ("estim", e); Profile prpro = new Profile (ProfileBase.Create (e.Responsible)); if (!prpro.HasBankAccount) throw new TemplateException ("NotBankable:" + e.Responsible); Profile prcli = new Profile (ProfileBase.Create (e.Client)); if (!prcli.IsBillable) throw new TemplateException ("NotBillable:" + e.Client); tmpe.Session.Add ("from", prpro); tmpe.Session.Add ("to", prcli); tmpe.Init (); return tmpe.TransformText (); } /// /// Gets the estimate in pdf format from tex generation. /// /// The to pdf. /// Estimid. [AcceptVerbs("GET")] public HttpResponseMessage EstimateToPdf (long estimid) { 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, LocalizedText.DocTemplateException )) }; } 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)) }; return new HttpResponseMessage () { Content = new ObjectContent (typeof(string), texest, new TexToPdfFormatter ()) }; } /// /// Register the specified model. /// /// Model. /// if false, sends a registration validation e-mail. [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() ) ); } } } }