// POST api/ProductCostingDescription public HttpResponseMessage PostProductCostingDescription(ProductCostingDescription productcostingdescription) { SalesQuotationDescription salesquotationdescription = db.SalesQuotationDescriptions.Where(q => (q.SalesQuotationID == productcostingdescription.SalesQuotationID) && (q.SalesSectionID == productcostingdescription.SalesSectionID) && (q.ProductID == productcostingdescription.ProductID)).FirstOrDefault(); if (ModelState.IsValid) { productcostingdescription.InsertBy = loginUser.UserID; db.Entry(productcostingdescription).State = productcostingdescription.ProductCostingDescriptionID == 0 ? EntityState.Added : EntityState.Modified; db.SaveChanges(); salesquotationdescription.FinalMRP = Convert.ToDecimal(productcostingdescription.FinalMRP); db.Entry(salesquotationdescription).State = EntityState.Modified; db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, productcostingdescription); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = productcostingdescription.ProductCostingDescriptionID })); return response; } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } }
// PUT api/ProductCostingDescription/5 public HttpResponseMessage PutProductCostingDescription(long id, ProductCostingDescription productcostingdescription) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } if (id != productcostingdescription.ProductCostingDescriptionID) { return Request.CreateResponse(HttpStatusCode.BadRequest); } productcostingdescription.UpdateBy = loginUser.UserID; db.Entry(productcostingdescription).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } return Request.CreateResponse(HttpStatusCode.OK); }
// POST api/ProductCosting public HttpResponseMessage PostProductCosting(ProductCosting productcosting) { if (ModelState.IsValid) { var pc = db.ProductCostings.Where(r => r.SalesQuotationID == productcosting.SalesQuotationID).SingleOrDefault(); if(pc==null) { var sq = db.SalesQuotations.Where(s => s.SalesQuotationID == productcosting.SalesQuotationID).SingleOrDefault(); string CustomCode = "PCT-" + DateTime.Now.ToString("yyyyMMdd"); int? MaxCode = Convert.ToInt32((db.ProductCostings.Where(r => r.ProductCostingCode.StartsWith(CustomCode)).Select(r => r.ProductCostingCode.Substring(CustomCode.Length, 4)).ToList()).Max()); string BOMCode = CustomCode + ((MaxCode + 1).ToString()).PadLeft(4, '0'); productcosting.ProductCostingCode = BOMCode; productcosting.SalesQuotationID = sq.SalesQuotationID; productcosting.CustomerID = sq.CustomerID; productcosting.CompanyID = sq.CompanyID; productcosting.InsertBy = loginUser.UserID; db.ProductCostings.Add(productcosting); db.SaveChanges(); } IEnumerable<SalesQuotationDescription> SalesQuotationDescription = db.SalesQuotationDescriptions.Where(r => r.SalesQuotationID == productcosting.SalesQuotationID).AsEnumerable(); foreach (var item in SalesQuotationDescription.ToList()) { ProductCostingDescription productCostingDescription = new ProductCostingDescription(); ProductCostingDescription PCD = db.ProductCostingDescriptions.Where(a => (a.SalesQuotationID == item.SalesQuotationID) && (a.ProductID == item.ProductID) && (a.SalesSectionID == item.SalesSectionID) && (a.SalesSectionName == item.SalesSectionName)).SingleOrDefault(); if (PCD != null) { productCostingDescription = PCD; } productCostingDescription.CustomerID = item.CustomerID; productCostingDescription.SalesQuotationID = item.SalesQuotationID; productCostingDescription.Description = item.Description; productCostingDescription.UOMID = item.UOMID; productCostingDescription.ProductID = item.ProductID; productCostingDescription.SalesSectionID = item.SalesSectionID; productCostingDescription.SalesSectionName = item.SalesSectionName; productCostingDescription.Quantity = item.Quantity; productCostingDescription.NetMaterials = item.CostPrice; db.Entry(productCostingDescription).State = productCostingDescription.ProductCostingDescriptionID == 0 ? EntityState.Added : EntityState.Modified; db.SaveChanges(); } HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, productcosting); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = productcosting.ProductCostingID })); return response; } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } }