context: // ReSharper disable All
  using System.Collections.Generic;
  using System.Dynamic;
  using System.Net;
  using System.Net.Http;
  using System.Web.Http;
  using MixERP.Net.Api.Framework;
  using MixERP.Net.ApplicationState.Cache;
  using MixERP.Net.Common.Extensions;
  using MixERP.Net.EntityParser;
  using MixERP.Net.Framework;
  using Newtonsoft.Json;
  using Newtonsoft.Json.Linq;
  using PetaPoco;
  using MixERP.Net.Schemas.Transactions.Data;
  
  namespace MixERP.Net.Api.Transactions
  {
      /// <summary>
      ///     Provides a direct HTTP access to perform various tasks such as adding, editing, and removing Stock Details.
      /// </summary>
      [RoutePrefix("api/v1.5/transactions/stock-detail")]
      public class StockDetailController : ApiController
      {
          /// <summary>
          ///     The StockDetail repository.
          /// </summary>
          private readonly IStockDetailRepository StockDetailRepository;
  
          public StockDetailController()
          {
              this._LoginId = AppUsers.GetCurrent().View.LoginId.ToLong();
              this._UserId = AppUsers.GetCurrent().View.UserId.ToInt();
              this._OfficeId = AppUsers.GetCurrent().View.OfficeId.ToInt();
              this._Catalog = AppUsers.GetCurrentUserDB();
  
              this.StockDetailRepository = new MixERP.Net.Schemas.Transactions.Data.StockDetail
              {
                  _Catalog = this._Catalog,
                  _LoginId = this._LoginId,
                  _UserId = this._UserId
              };
          }
  
          public StockDetailController(IStockDetailRepository repository, string catalog, LoginView view)
          {
              this._LoginId = view.LoginId.ToLong();
              this._UserId = view.UserId.ToInt();
              this._OfficeId = view.OfficeId.ToInt();
              this._Catalog = catalog;
  
              this.StockDetailRepository = repository;
          }
  
          public long _LoginId { get; }
          public int _UserId { get; private set; }
          public int _OfficeId { get; private set; }
          public string _Catalog { get; }
  
          /// <summary>
          ///     Creates meta information of "stock detail" entity.
          /// </summary>
          /// <returns>Returns the "stock detail" meta information to perform CRUD operation.</returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("meta")]
          [Route("~/api/transactions/stock-detail/meta")]
          public EntityView GetEntityView()
          {
              if (this._LoginId == 0)
              {
                  return new EntityView();
              }
  
              return new EntityView
              {
                  PrimaryKey = "stock_detail_id",
                  Columns = new List<EntityColumn>()
                                  {
                                          new EntityColumn { ColumnName = "stock_detail_id",  PropertyName = "StockDetailId",  DataType = "long",  DbDataType = "int8",  IsNullable = false,  IsPrimaryKey = true,  IsSerial = true,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "value_date",  PropertyName = "ValueDate",  DataType = "DateTime",  DbDataType = "date",  IsNullable = false,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "stock_master_id",  PropertyName = "StockMasterId",  DataType = "long",  DbDataType = "int8",  IsNullable = false,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "tran_type",  PropertyName = "TranType",  DataType = "string",  DbDataType = "transaction_type",  IsNullable = false,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "store_id",  PropertyName = "StoreId",  DataType = "int",  DbDataType = "int4",  IsNullable = true,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "item_id",  PropertyName = "ItemId",  DataType = "int",  DbDataType = "int4",  IsNullable = false,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "quantity",  PropertyName = "Quantity",  DataType = "int",  DbDataType = "int4",  IsNullable = false,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "unit_id",  PropertyName = "UnitId",  DataType = "int",  DbDataType = "int4",  IsNullable = false,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "base_quantity",  PropertyName = "BaseQuantity",  DataType = "decimal",  DbDataType = "numeric",  IsNullable = false,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "base_unit_id",  PropertyName = "BaseUnitId",  DataType = "int",  DbDataType = "int4",  IsNullable = false,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "price",  PropertyName = "Price",  DataType = "decimal",  DbDataType = "money_strict",  IsNullable = false,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "cost_of_goods_sold",  PropertyName = "CostOfGoodsSold",  DataType = "decimal",  DbDataType = "money_strict2",  IsNullable = false,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "discount",  PropertyName = "Discount",  DataType = "decimal",  DbDataType = "money_strict2",  IsNullable = false,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "shipping_charge",  PropertyName = "ShippingCharge",  DataType = "decimal",  DbDataType = "money_strict2",  IsNullable = false,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "sales_tax_id",  PropertyName = "SalesTaxId",  DataType = "int",  DbDataType = "int4",  IsNullable = true,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "tax",  PropertyName = "Tax",  DataType = "decimal",  DbDataType = "money_strict2",  IsNullable = false,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "audit_user_id",  PropertyName = "AuditUserId",  DataType = "int",  DbDataType = "int4",  IsNullable = true,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "audit_ts",  PropertyName = "AuditTs",  DataType = "DateTime",  DbDataType = "timestamptz",  IsNullable = true,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 }
                                  }
              };
          }
  
          /// <summary>
          ///     Counts the number of stock details.
          /// </summary>
          /// <returns>Returns the count of the stock details.</returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("count")]
          [Route("~/api/transactions/stock-detail/count")]
          public long Count()
          {
              try
              {
                  return this.StockDetailRepository.Count();
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
                      Content = new StringContent(ex.Message),
                      StatusCode = HttpStatusCode.InternalServerError
                  });
              }
              catch
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
              }
          }
  
          /// <summary>
          ///     Returns all collection of stock detail.
          /// </summary>
          /// <returns></returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("all")]
          [Route("~/api/transactions/stock-detail/all")]
          public IEnumerable<MixERP.Net.Entities.Transactions.StockDetail> GetAll()
          {
              try
              {
                  return this.StockDetailRepository.GetAll();
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
                      Content = new StringContent(ex.Message),
                      StatusCode = HttpStatusCode.InternalServerError
                  });
              }
              catch
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
              }
          }
  
          /// <summary>
          ///     Returns collection of stock detail for export.
          /// </summary>
          /// <returns></returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("export")]
          [Route("~/api/transactions/stock-detail/export")]
          public IEnumerable<dynamic> Export()
          {
              try
              {
                  return this.StockDetailRepository.Export();
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
                      Content = new StringContent(ex.Message),
                      StatusCode = HttpStatusCode.InternalServerError
                  });
              }
              catch
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
              }
          }
  
          /// <summary>
          ///     Returns an instance of stock detail.
          /// </summary>
          /// <param name="stockDetailId">Enter StockDetailId to search for.</param>
          /// <returns></returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("{stockDetailId}")]
          [Route("~/api/transactions/stock-detail/{stockDetailId}")]
          public MixERP.Net.Entities.Transactions.StockDetail Get(long stockDetailId)
          {
              try
              {
                  return this.StockDetailRepository.Get(stockDetailId);
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
                      Content = new StringContent(ex.Message),
                      StatusCode = HttpStatusCode.InternalServerError
                  });
              }
              catch
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
              }
          }
  
          [AcceptVerbs("GET", "HEAD")]
          [Route("get")]
          [Route("~/api/transactions/stock-detail/get")]
          public IEnumerable<MixERP.Net.Entities.Transactions.StockDetail> Get([FromUri] long[] stockDetailIds)
          {
              try
              {
                  return this.StockDetailRepository.Get(stockDetailIds);
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
                      Content = new StringContent(ex.Message),
                      StatusCode = HttpStatusCode.InternalServerError
                  });
              }
              catch
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
              }
          }
  
          /// <summary>
          ///     Creates a paginated collection containing 10 stock details on each page, sorted by the property StockDetailId.
          /// </summary>
          /// <returns>Returns the first page from the collection.</returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("")]
          [Route("~/api/transactions/stock-detail")]
          public IEnumerable<MixERP.Net.Entities.Transactions.StockDetail> GetPaginatedResult()
          {
              try
              {
                  return this.StockDetailRepository.GetPaginatedResult();
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
                      Content = new StringContent(ex.Message),
                      StatusCode = HttpStatusCode.InternalServerError
                  });
              }
              catch
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
              }
          }
  
          /// <summary>
          ///     Creates a paginated collection containing 10 stock details on each page, sorted by the property StockDetailId.
          /// </summary>
          /// <param name="pageNumber">Enter the page number to produce the resultset.</param>
          /// <returns>Returns the requested page from the collection.</returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("page/{pageNumber}")]
          [Route("~/api/transactions/stock-detail/page/{pageNumber}")]
          public IEnumerable<MixERP.Net.Entities.Transactions.StockDetail> GetPaginatedResult(long pageNumber)
          {
              try
              {
                  return this.StockDetailRepository.GetPaginatedResult(pageNumber);
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
                      Content = new StringContent(ex.Message),
                      StatusCode = HttpStatusCode.InternalServerError
                  });
              }
              catch
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
              }
          }
  
          /// <summary>
          ///     Counts the number of stock details using the supplied filter(s).
          /// </summary>
          /// <param name="filters">The list of filter conditions.</param>
          /// <returns>Returns the count of filtered stock details.</returns>
          [AcceptVerbs("POST")]
          [Route("count-where")]
          [Route("~/api/transactions/stock-detail/count-where")]
          public long CountWhere([FromBody]JArray filters)
          {
              try
              {
                  List<EntityParser.Filter> f = filters.ToObject<List<EntityParser.Filter>>(JsonHelper.GetJsonSerializer());
                  return this.StockDetailRepository.CountWhere(f);
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
  
gt:                     Content = new StringContent(ex.Message),