Example #1
0
        public ResultAndError <bool> CreateOrderByProvider(CartList listado)
        {
            ResultAndError <bool> result = new ResultAndError <bool>();

            try
            {
                var grupo = listado.ProductList.GroupBy(o => o.ProviderId).Select(o => new { ProviderId = o.Key, Items = o.ToList() });

                foreach (var providerList in grupo)
                {
                    //Obtenemos el proveedor
                    Provider oProvider = pBS.Get(providerList.ProviderId);
                    //Generamos un objeto de Cabecera pedido
                    Document oCabecera = new Document
                    {
                        BuyFromVendorNo    = oProvider.VendorNo,
                        BuyFromVendorName  = oProvider.Name,
                        BuyFromVendorName2 = oProvider.Name2,
                        BuyFromAddress     = oProvider.Address,
                        BuyFromAddress2    = oProvider.Address2,
                        BuyFromCity        = oProvider.City,
                        BuyFromContact     = oProvider.Contact,
                        BuyFromCounty      = oProvider.County,
                        BuyFromPostCode    = oProvider.PostCode,
                        OrderDate          = DateTime.Now,
                        Estado             = 1,//Confirmado
                        OrigenPedido       = "WEB",
                        Amount             = 0,
                        Centro             = "", //Se tiene que rellenar con el centro seleccionado TODO
                        Zona = ""                //Se rellena con la zona asociada al centro TODO
                    };
                    //Creamos la cabecera
                    ResultAndError <Document> cabecera = Update(oCabecera, true);
                    if (cabecera.HasErrors)
                    {
                        return(result.AddError(cabecera.Errors, HttpStatusCode.InternalServerError));
                    }
                    decimal?totalAmount = 0, totalAmountVAT = 0;
                    //Recorremos los productos para crear las lineas
                    foreach (ProductList product in providerList.Items)
                    {
                        decimal?unitPrice = 0;
                        if (product.Product.ProviderRates != null)
                        {
                            unitPrice = product.Product.ProviderRates.SingleOrDefault(o => o.VendorNo == providerList.ProviderId).DirectUnitCost;
                            if (product.Product.DiscountLineInvoice != null && product.Product.DiscountLineInvoice.LineDiscount > 0)
                            {
                                unitPrice *= (1 - (product.Product.DiscountLineInvoice.LineDiscount / 100));
                            }
                        }
                        decimal VAT = Convert.ToDecimal(product.Product.VATProdPostingGroup.Replace(".", ","));
                        decimal?providerDiscount = oProvider.InvoiceProviderDiscount.Discount;
                        decimal?amount           = (product.Quantity * unitPrice) * (1 - (providerDiscount / 100));
                        decimal?amountVAT        = amount * ((VAT / 100) + 1);
                        totalAmount    += amount;
                        totalAmountVAT += amountVAT;
                        //Generamos la linea
                        Line oLine = new Line()
                        {
                            DocumentNo          = Convert.ToInt32(oCabecera.No),
                            No                  = product.ProductId,
                            LocationCode        = "",
                            Description         = product.Product.Description,
                            UnitOfMeasure       = product.Product.PurchUnitOfMeasure,
                            Quantity            = product.Quantity,
                            DirectUnitCost      = unitPrice,
                            VAT                 = VAT,
                            LineDiscount        = providerDiscount,
                            Amount              = amount,
                            AmountIncludingVAT  = amountVAT,
                            ExpectedReceiptDate = null,//Fecha la mete el usuario
                            OutstandingQuantity = product.Quantity,
                            QuantityToReceive   = product.Quantity,
                            QuantityReceived    = null
                        };
                        db.LineRequests.Add(oLine);
                    }
                    db.SaveChanges();
                    //Actualizamos estos campos de la cabecera
                    oCabecera.Amount             = totalAmount;
                    oCabecera.AmountIncludingVAT = totalAmountVAT;
                    Update(oCabecera);
                }
                return(result.AddResult(true));
            }
            catch (Exception e)
            {
                return(result.AddError(e, HttpStatusCode.InternalServerError));
            }
        }
 public IActionResult GetProviderById([FromQuery] string id)
 {
     return(Ok(pBS.Get(id)));
 }