示例#1
0
        private void SaveDataToDb()
        {
            using (TenderContext tc = new TenderContext())
            {
                currentEstimate.Name = estimateNameTextBox.Text.Trim();
                if (currentEstimate.Id > 0)
                {
                    tc.Estimates.Attach(currentEstimate);
                    tc.Entry <Estimate>(currentEstimate).State = System.Data.Entity.EntityState.Modified;

                    // Удаляем старые записи сметы
                    var oldBalanceChanges = tc.BalanceChanges.Where(p => (p.EstimateId == currentEstimate.Id && p.PrimaryKekvSum >= 0 && p.SecondaryKekvSum >= 0));
                    tc.BalanceChanges.RemoveRange(oldBalanceChanges);

                    tc.SaveChanges();
                }
                else
                {
                    tc.TenderYears.Attach(currentEstimate.Year);
                    tc.Estimates.Add(currentEstimate);
                    tc.SaveChanges();
                }

                for (int i = 0; i < estimateMoneyList.Count; i++)
                {
                    for (int j = 0; j < kekvsList.Count; j++)
                    {
                        for (int k = 0; k < monthes.Length; k++)
                        {
                            BalanceChanges incommingMoney = new BalanceChanges();
                            incommingMoney.DateOfReceiving  = new DateTime((int)currentEstimate.Year.Year, (k + 1), 1, 0, 0, 0);
                            incommingMoney.EstimateId       = currentEstimate.Id;
                            incommingMoney.MoneySourceId    = estimateMoneyList[i].Source.Id;
                            incommingMoney.PrimaryKekvId    = incommingMoney.SecondaryKekvId = kekvsList[j].Id;
                            incommingMoney.PrimaryKekvSum   = estimateMoneyList[i].PrimarySumValues[j, k];
                            incommingMoney.SecondaryKekvSum = estimateMoneyList[i].SecondarySumValues[j, k];
                            tc.BalanceChanges.Add(incommingMoney);
                        }
                    }
                }

                tc.SaveChanges();

                FileManager.UpdateRelatedFilesOfEntity(tc, currentEstimate.RelatedFiles, relatedFiles, deletingFiles);

                wasDbChanged = true;
            }
        }
        protected virtual async Task <APIResultCodes> ValidateReceiveTransAmountAsync(DagSystem sys, ReceiveTransferBlock block, BalanceChanges receiveTransaction)
        {
            //find the corresponding send block and validate the added transaction amount
            var srcBlock = await sys.Storage.FindBlockByHashAsync(block.SourceHash);

            if (srcBlock is TransactionBlock sourceBlock)
            {
                if (sourceBlock == null)
                {
                    return(APIResultCodes.SourceSendBlockNotFound);
                }

                // find the actual amount of transaction
                BalanceChanges sendTransaction;
                if (block.BlockType == BlockTypes.ReceiveTransfer || block.BlockType == BlockTypes.OpenAccountWithReceiveTransfer ||
                    block.BlockType == BlockTypes.PoolDeposit || block.BlockType == BlockTypes.PoolSwapIn ||
                    block.BlockType == BlockTypes.Staking || block.BlockType == BlockTypes.Profiting ||
                    block.BlockType == BlockTypes.ReceiveAsFee ||
                    block.BlockType == BlockTypes.DexRecvToken ||
                    block.BlockType == BlockTypes.OrgnizationRecv ||
                    block.BlockType == BlockTypes.OrgnizationChange ||
                    block.BlockType == BlockTypes.OTCOrderRecv ||
                    block.BlockType == BlockTypes.OTCTradeRecv ||
                    block.BlockType == BlockTypes.OTCTradeResolutionRecv ||
                    block.BlockType == BlockTypes.Voting
                    )  // temp code. should use getbalancechanges
                {
                    if ((sourceBlock as SendTransferBlock).DestinationAccountId != block.AccountID)
                    {
                        // first check if the transfer was aimed to imported account
                        if (!await sys.Storage.WasAccountImportedAsync((sourceBlock as SendTransferBlock).DestinationAccountId, block.AccountID))
                        {
                            return(APIResultCodes.InvalidDestinationAccountId);
                        }
                    }

                    TransactionBlock prevToSendBlock = await sys.Storage.FindBlockByHashAsync(sourceBlock.PreviousHash) as TransactionBlock;

                    if (prevToSendBlock == null)
                    {
                        return(APIResultCodes.CouldNotTraceSendBlockChain);
                    }

                    sendTransaction = sourceBlock.GetBalanceChanges(prevToSendBlock);

                    if (!sourceBlock.ValidateTransaction(prevToSendBlock))
                    {
                        return(APIResultCodes.SendTransactionValidationFailed);
                    }
                    //originallySentAmount = sendTransaction.Amount;
                    //originallySentAmount =
                    //    prevToSendBlock.Balances[LyraGlobal.LYRA_TICKER_CODE] - sourceBlock.Balances[LyraGlobal.LYRA_TICKER_CODE] - (sourceBlock as IFeebleBlock).Fee;
                }
                else
                if (block.BlockType == BlockTypes.ReceiveFee || block.BlockType == BlockTypes.OpenAccountWithReceiveFee)
                {
                    sendTransaction = new BalanceChanges();
                    sendTransaction.Changes.Add(LyraGlobal.OFFICIALTICKERCODE, sourceBlock.Fee);
                }
                else
                {
                    return(APIResultCodes.InvalidBlockType);
                }

                if (block.BlockType == BlockTypes.ReceiveAsFee)
                {
                    var send = sendTransaction.Changes[LyraGlobal.OFFICIALTICKERCODE];
                    var recv = receiveTransaction.Changes.Count == 0 ? 0 : receiveTransaction.Changes[LyraGlobal.OFFICIALTICKERCODE];
                    var fee  = block.Fee;
                    if (fee != send || recv != 0)
                    {
                        return(APIResultCodes.InvalidFeeAmount);
                    }
                }
                else
                {
                    if (!sendTransaction.Changes.OrderBy(kvp => kvp.Key)
                        .SequenceEqual(receiveTransaction.Changes.OrderBy(kvp => kvp.Key)))
                    {
                        return(APIResultCodes.TransactionAmountDoesNotMatch);
                    }
                }

                //if (sendTransaction.Amount != receiveTransaction.Amount)
                //    return APIResultCodes.TransactionAmountDoesNotMatch;

                //if (sendTransaction.TokenCode != receiveTransaction.TokenCode)
                //    return APIResultCodes.TransactionTokenDoesNotMatch;
            }
            else if (srcBlock == null)
            {
                if (block is ReceiveNodeProfitBlock)
                {
                    return(APIResultCodes.Success);
                }
            }
            else
            {
                return(APIResultCodes.UnsupportedBlockType);
            }

            return(APIResultCodes.Success);
        }