public object AlleviateTaxLot(AlleviateTaxLotDto obj)
 {
     return(controller.AlleviateTaxLot(obj));
 }
        public object AlleviateTaxLot(AlleviateTaxLotDto obj)
        {
            try
            {
                string symbol  = obj.ProspectiveTrade.Symbol;
                string side    = obj.ProspectiveTrade.Side;
                string message = "Tax Lot(s) Alleviated Successfully";
                if (side.Equals("COVER"))
                {
                    if (!obj.OpenTaxLots.All(x => x.Side.Equals("SHORT")))
                    {
                        message = "All selected lots are not SHORT";
                    }
                }
                else if (side.Equals("SELL"))
                {
                    if (!obj.OpenTaxLots.All(x => x.Side.Equals("BUY")))
                    {
                        message = "All selected lots are not BUY";
                    }
                }
                else
                {
                    message = "Incorrect side for this transaction";
                }

                int lotSum = obj.OpenTaxLots.Sum(x => Math.Abs(x.RemainingQuantity));
                int prospectiveTradeQuantity = Math.Abs(obj.ProspectiveTrade.RemainingQuantity);
                if (lotSum < prospectiveTradeQuantity)
                {
                    //message = "Quantity Mismatch";
                }

                List <TaxLot> taxLotList = new List <TaxLot>();
                foreach (var item in obj.OpenTaxLots)
                {
                    TaxLot t = new TaxLot();
                    if (prospectiveTradeQuantity == 0)
                    {
                        break;
                    }
                    else if (Math.Abs(item.RemainingQuantity) <= prospectiveTradeQuantity)
                    {
                        item.Status = "Closed";
                        if (Math.Abs(item.RemainingQuantity) < prospectiveTradeQuantity)
                        {
                            t.Quantity = Math.Abs(item.RemainingQuantity);
                        }
                        else
                        {
                            t.Quantity = prospectiveTradeQuantity;
                        }

                        prospectiveTradeQuantity -= Math.Abs(item.RemainingQuantity);
                        item.RemainingQuantity    = 0;
                    }
                    else if (Math.Abs(item.RemainingQuantity) > prospectiveTradeQuantity)
                    {
                        item.Status = "Partially Closed";
                        int absRemainingQuantity = Math.Abs(item.RemainingQuantity) - prospectiveTradeQuantity;
                        t.Quantity = prospectiveTradeQuantity;
                        prospectiveTradeQuantity = 0;
                        if (item.RemainingQuantity < 0)
                        {
                            item.RemainingQuantity = absRemainingQuantity * -1;
                        }
                        else
                        {
                            item.RemainingQuantity = absRemainingQuantity;
                        }
                    }

                    t.OpeningLotId = item.OpenLotId;
                    t.ClosingLotId = obj.ProspectiveTrade.LpOrderId;

                    //TODO. temporary assignment for now
                    t.BusinessDate     = DateTime.UtcNow;
                    t.RealizedPnl      = (obj.ProspectiveTrade.TradePrice - item.TradePrice) * t.Quantity;
                    t.CostBasis        = obj.ProspectiveTrade.TradePrice;
                    t.TradePrice       = item.TradePrice;
                    t.TradeDate        = DateTime.UtcNow;
                    t.Quantity         = SignedValueBasedOnSide(t.Quantity, obj.ProspectiveTrade.Side);
                    t.InvestmentAtCost = t.Quantity * t.TradePrice * -1;
                    taxLotList.Add(t);
                }

                SqlHelper sqlHelper = new SqlHelper(connectionString);
                sqlHelper.VerifyConnection();
                sqlHelper.SqlBeginTransaction();
                InsertTaxLot(taxLotList, sqlHelper);
                UpdateTaxLotStatus(obj.OpenTaxLots, sqlHelper);
                sqlHelper.SqlCommitTransaction();
                sqlHelper.CloseConnection();
                return(Utils.Wrap(true, null, HttpStatusCode.OK, message));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }