Exemplo n.º 1
0
        public ActionResult SalesOrderLine_Create([DataSourceRequest] DataSourceRequest request, salesOrderLineModel modelSalesOrderLine)
        {
            var iResult = new List <salesOrderLineModel>();

            if (string.IsNullOrEmpty(modelSalesOrderLine.itemNumber))
            {
                ModelState.AddModelError("", "Item nunber is required");
            }


            if (!ModelState.IsValid)
            {
                iResult.Add(modelSalesOrderLine);
            }
            else
            {
                try
                {
                    var documentId  = new Guid(modelSalesOrderLine.documentId);
                    var iUri        = new Uri(ODataWebService.BuildODataUrl());
                    var iWebService = new NAV(iUri)
                    {
                        Credentials = ODataWebService.CreateCredentials(iUri.ToString())
                    };
                    var iItem        = (from item in iWebService.items where item.number == modelSalesOrderLine.itemNumber select item).FirstOrDefault();
                    var lastSequence = (from salesOrderLine in iWebService.salesOrderLines
                                        orderby salesOrderLine.sequence descending
                                        where salesOrderLine.documentId == documentId
                                        select new  { id = salesOrderLine.id, lineNumber = salesOrderLine.sequence }).FirstOrDefault();
                    int lastLineNumber = 10000;

                    if (lastSequence != null)
                    {
                        lastLineNumber = (int)lastSequence.lineNumber + 10000;
                    }

                    if (iItem != null)
                    {
                        var newOrderLine = new Microsoft.NAV.salesOrderLine();
                        newOrderLine.id          = "";
                        newOrderLine.documentId  = documentId;
                        newOrderLine.sequence    = lastLineNumber;
                        newOrderLine.lineType    = "Item";
                        newOrderLine.itemId      = iItem.id;
                        newOrderLine.description = modelSalesOrderLine.itemDescription;
                        newOrderLine.quantity    = modelSalesOrderLine.quantity;
                        newOrderLine.unitPrice   = (decimal)modelSalesOrderLine.unitPrice;

                        iWebService.AddTosalesOrderLines(newOrderLine);
                        iWebService.SaveChanges();

                        iResult.Add(new salesOrderLineModel(newOrderLine));
                    }
                    else
                    {
                        ModelState.AddModelError("", "Invalid Item Number");
                        iResult.Add(modelSalesOrderLine);
                    }
                }
                catch (DataServiceRequestException ex)
                {
                    ModelState.AddModelError("", ex.Message);
                    iResult.Add(modelSalesOrderLine);
                }
            }

            return(Json(iResult.ToDataSourceResult(request, ModelState)));
        }