Exemplo n.º 1
0
        public static InvocationTransaction GetDeployContractTransaction(ContractModel contractModel, out string scriptHash, out string scriptBytesString)
        {
            MainWindow mainWindow = (MainWindow)App.Current.MainWindow;

            scriptHash        = null;
            scriptBytesString = null;

            if (mainWindow.ActiveDocument == null)
            {
                return(null);
            }

            RIPEMD160 myRIPEMD160    = RIPEMD160Managed.Create();
            string    uniqueTempHash = BitConverter.ToString(myRIPEMD160.ComputeHash(Guid.NewGuid().ToByteArray()));

            mainWindow.ActiveDocument.scintilla.Text = mainWindow.ActiveDocument.scintilla.Text.Replace("MI4m2tqy+RPxxQfKGdKhg1Hb62s=", uniqueTempHash);
            mainWindow.ActiveDocument.Save();
            if (!SmartContract.Build())
            {
                return(null);
            }

            string contractFileName = Path.Combine(Path.GetDirectoryName(mainWindow.ActiveDocument.FilePath), Path.GetFileNameWithoutExtension(mainWindow.ActiveDocument.FilePath), ".avm");

            if (!File.Exists(contractFileName))
            {
                return(null);
            }

            if (!contractModel.Validate())
            {
                return(null);
            }

            byte[] script = File.ReadAllBytes(contractFileName);
            scriptHash = script.ToScriptHash().ToString();

            byte[] parameter_list = contractModel.ParameterHexValue.HexToBytes();

            ContractParameterType return_type = contractModel.ReturnTypeHexValue.HexToBytes().Select(p => (ContractParameterType?)p).FirstOrDefault() ?? ContractParameterType.Void;
            ContractPropertyState properties  = ContractPropertyState.NoProperty;

            if (contractModel.IsStorageRequired)
            {
                properties |= ContractPropertyState.HasStorage;
            }
            if (contractModel.HasDynamicInvoke)
            {
                properties |= ContractPropertyState.HasDynamicInvoke;
            }

            using (ScriptBuilder sb = new ScriptBuilder())
            {
                sb.EmitSysCall("Neo.Contract.Create", script, parameter_list, return_type, properties, contractModel.Name, contractModel.Version, contractModel.Author,
                               contractModel.Email, contractModel.Description);
                return(new InvocationTransaction
                {
                    Script = sb.ToArray()
                });
            }
        }
Exemplo n.º 2
0
        private int SaveContract()
        {
            ContractModel.Validate();

            if (ContractModel.HasErrors)
            {
                System.Windows.Forms.MessageBox.Show(@"Пожалуйста, введите все красные цветовые поля", @"Проверка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return(0);
            }

            if (ContractModel.IsTreaguare != true &&
                ContractModel.ContractDate == null)
            {
                System.Windows.Forms.MessageBox.Show(@"Выберите дату контракта", @"Дата", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return(0);
            }

            try {
                var contractViewModel = new ContractViewModel {
                    Id             = ContractModel.Id,
                    BranchId       = ContractModel.BranchId,
                    CategoryId     = ContractModel.CategoryId,
                    ContractNumber = ContractModel.ContractNumber,
                    OrganizationId = ContractModel.Organization.Id,
                    ContractAmount = ContractModel.ContractAmount,
                    ContractDate   = ContractModel.ContractDate,
                    ObjectName     = ContractModel.ObjectName
                };
                var contractId = _contractService.CreateOrUpdate(contractViewModel);

                //category
                if (contractViewModel.Id == 0)
                {
                    var category = _categoryService.GetById(contractViewModel.CategoryId ?? 0);

                    category.Counter = category.Counter + 1;

                    _categoryService.CreateOrUpdate(category);
                }

                //work types
                var workTypes = new ContractWorkPaymentViewModel[]
                {
                    #region items
                    new ContractWorkPaymentViewModel
                    {
                        Amount     = ContractModel.Amount1,
                        PeriodId   = ContractModel.PeriodId1,
                        ContractId = contractId,
                        WorkTypeId = ContractModel.WorkTypeId1
                    },
                    new ContractWorkPaymentViewModel
                    {
                        Amount     = ContractModel.Amount2,
                        PeriodId   = ContractModel.PeriodId2,
                        ContractId = contractId,
                        WorkTypeId = ContractModel.WorkTypeId2
                    },
                    new ContractWorkPaymentViewModel
                    {
                        Amount     = ContractModel.Amount3,
                        PeriodId   = ContractModel.PeriodId3,
                        ContractId = contractId,
                        WorkTypeId = ContractModel.WorkTypeId3
                    },
                    new ContractWorkPaymentViewModel
                    {
                        Amount     = ContractModel.Amount4,
                        PeriodId   = ContractModel.PeriodId4,
                        ContractId = contractId,
                        WorkTypeId = ContractModel.WorkTypeId4
                    },
                    new ContractWorkPaymentViewModel
                    {
                        Amount     = ContractModel.Amount5,
                        PeriodId   = ContractModel.PeriodId5,
                        ContractId = contractId,
                        WorkTypeId = ContractModel.WorkTypeId5
                    }
                    #endregion
                };

                //work type
                foreach (var workType in workTypes)
                {
                    var workTypeItem = _workPaymentService.Get(f => f.ContractId == ContractModel.Id &&
                                                               f.WorkTypeId == workType.WorkTypeId);

                    if (workTypeItem != null)
                    {
                        workType.Id = workTypeItem.Id;
                    }
                    _workPaymentService.CreateOrUpdate(workType);
                }

                if (ContractModel.IsTreaguare == true && contractViewModel.Id == 0)
                {
                    //payment
                    var payment = new PaymentViewModel {
                        Amount     = ContractModel.ContractAmount * ContractUIViewModel.TREAGUARE_PERCENT_FOR_AMOUNT,
                        Date       = ContractModel.ContractDate,
                        ContractId = contractId
                    };

                    _paymentService.CreateOrUpdate(payment);
                }


                //nakladnoy
                if (ContractModel.HasNakladnoy == true)
                {
                    var nakladnoy = new NakladnoyViewModel {
                        ContractId = contractId,
                        Date       = ContractModel.ContractDate,
                        Number     = ContractModel.ContractNumber
                    };
                    var nakladnoyItem = _nakladnoyService.Get(f => f.ContractId == ContractModel.Id);

                    if (nakladnoyItem != null)
                    {
                        nakladnoy.Id = nakladnoyItem.Id;
                    }

                    _nakladnoyService.CreateOrUpdate(nakladnoy);
                }

                // act invoice
                if (ContractModel.HasActInvoice == true)
                {
                    var actInvoice = new ActInvoiceViewModel {
                        ContractId = contractId,
                        Number     = ContractModel.ContractNumber,
                        Amount     = ContractModel.ContractAmount
                    };
                    var actInvoiceItem = _actInvoiceService.Get(f => f.ContractId == ContractModel.Id);

                    if (actInvoiceItem != null)
                    {
                        actInvoice.Id = actInvoiceItem.Id;
                    }

                    _actInvoiceService.CreateOrUpdate(actInvoice);
                }


                System.Windows.Forms.MessageBox.Show(@"Успешно сохранено", @"Сохранить", MessageBoxButtons.OK, MessageBoxIcon.Information);

                Grid1.DataContext = new ContractUIViewModel();

                DialogResult = true;

                return(contractId);
            } catch (Exception exception) {
                System.Windows.Forms.MessageBox.Show(exception.Message, @"Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            return(0);
        }