Exemplo n.º 1
0
        private async Task <Document> createDocument(TransferMoney transferMoney, string from, string to, int organId)
        {
            DocumentRule documentRule = new DocumentRule();

            FinanYearRule finanYearRule = new FinanYearRule();
            var           finanYears    = await finanYearRule.GetAllByOrganIdAsync(organId);

            var      finanYear = finanYears.Where(x => x.Closed == false && x.OrganId == organId).SingleOrDefault();
            Document document  = new Document();

            document.Credit      = transferMoney.Amount;
            document.DateTime    = DateTime.Now;
            document.Debit       = transferMoney.Amount;
            document.Description = " انتقال وجه از " + from + " به " + to;
            document.DisplayDate = PersianDateUtils.ToPersianDate(DateTime.Now);
            document.IsManual    = false;
            document.Number      = await documentRule.createNumberDocumentAsync(organId);

            document.Number2 = await documentRule.createNumberDocumentAsync(organId);

            document.Status       = ZhivarEnums.DocumentStatus.TaeedShode;
            document.StatusString = "تایید شده";
            document.OrganId      = organId;
            document.FinanYear    = finanYear;
            document.FinanYearId  = finanYear.ID;
            document.Type         = ZhivarEnums.NoeDoc.Transfer;
            List <Transaction> transactions = new List <Transaction>();

            transactions = await RegisterTransaction(document, transferMoney, organId);

            document.Transactions = transactions;

            return(document);
        }
Exemplo n.º 2
0
        public virtual async Task <HttpResponseMessage> SaveFinanYear([FromBody] RequestFinanYear requestFinanYear)
        {
            var organId = Convert.ToInt32(SecurityManager.CurrentUserContext.OrganizationId);

            FinanYearRule finanYearRule = new FinanYearRule();
            var           finanQuery    = await finanYearRule.GetAllByOrganIdAsync(organId);

            var finanYear = finanQuery.Where(x => x.FirstYear == true).SingleOrDefault();


            return(Request.CreateResponse(HttpStatusCode.OK, new { resultCode = ZhivarEnums.ResultCode.Successful, data = requestFinanYear.finanYear }));
        }
Exemplo n.º 3
0
        private async Task <Document> createDocument(PayRecevie payRecevie, int organId, int numberDocument)
        {
            FinanYearRule finanYearRule = new FinanYearRule();
            var           finanYears    = await finanYearRule.GetAllByOrganIdAsync(organId);

            var finanYear = finanYears.Where(x => x.Closed == false).SingleOrDefault();

            Document document = new Document();

            document.Credit      = payRecevie.Amount;
            document.DateTime    = PersianDateUtils.ToDateTime(payRecevie.DisplayDate);
            document.Debit       = payRecevie.Amount;
            document.Description = payRecevie.Description;
            document.DisplayDate = payRecevie.DisplayDate;
            document.IsManual    = false;
            document.Status      = ZhivarEnums.DocumentStatus.TaeedShode;
            document.ObjectState = OMF.Common.Enums.ObjectState.Added;
            if (numberDocument != 0)
            {
                document.Number  = numberDocument;
                document.Number2 = numberDocument;
            }
            else
            {
                DocumentRule documentRule = new DocumentRule();
                document.Number = await documentRule.createNumberDocumentAsync(organId);

                document.Number2 = await documentRule.createNumberDocumentAsync(organId);
            }

            document.Status       = ZhivarEnums.DocumentStatus.TaeedShode;
            document.StatusString = "تایید شده";
            document.OrganId      = organId;
            document.FinanYear    = finanYear;
            document.FinanYearId  = finanYear.ID;
            List <Transaction> transactions = new List <Transaction>();

            if (payRecevie.IsReceive)
            {
                document.Type = ZhivarEnums.NoeDoc.Recive;
                transactions  = await RegisterTransactionRecevie(document, payRecevie, organId);
            }
            else
            {
                document.Type = ZhivarEnums.NoeDoc.Pay;
                transactions  = await RegisterTransactionPay(document, payRecevie, organId);
            }


            document.Transactions = transactions;

            return(document);
        }
Exemplo n.º 4
0
        public virtual async Task <HttpResponseMessage> GetCurrentUserAndBusinesses()
        {
            var userId = SecurityManager.CurrentUserContext.UserId;

            PersonRule personRule = new PersonRule();

            var person = personRule.GetPersonByUserId(Convert.ToInt32(userId));

            FinanYearRule finanYearRule = new FinanYearRule();

            var finanYears = await finanYearRule.GetAllByOrganIdAsync(person.ID);

            BussinessSiteRule bussinessSiteRule = new BussinessSiteRule();
            var bussinesses = await bussinessSiteRule.GetAllByOrganIdAsync(person.ID);

            var responseCurrentUserAndBusinesses = new ResponseCurrentUserAndBusinesses();

            responseCurrentUserAndBusinesses.appVersion = "3.1";
            responseCurrentUserAndBusinesses.businesses = new List <Bussiness>();
            responseCurrentUserAndBusinesses.businesses = bussinesses.ToList();

            responseCurrentUserAndBusinesses.businessInfo            = new BusinessInfo();
            responseCurrentUserAndBusinesses.businessInfo.business   = bussinesses.SingleOrDefault();
            responseCurrentUserAndBusinesses.businessInfo.finanYear  = finanYears.Where(x => x.Closed == false).SingleOrDefault();
            responseCurrentUserAndBusinesses.businessInfo.finanYears = finanYears.ToList();

            responseCurrentUserAndBusinesses.showCloseFinanYearAlert = false;
            responseCurrentUserAndBusinesses.todayDate = Utilities.PersianDateUtils.ToPersianDate(DateTime.Now);
            responseCurrentUserAndBusinesses.user      = new Common.User()
            {
                FirstName   = person.Nam,
                Id          = person.ID,
                IsConfirmed = true,
                LastName    = person.NamKhanvadegi,
                Phone       = person.Tel,
                Name        = person.Nam + " " + person.NamKhanvadegi,
            };

            return(Request.CreateResponse(HttpStatusCode.OK, new { resultCode = (int)Enums.ResultCode.Successful, data = responseCurrentUserAndBusinesses }));
        }
Exemplo n.º 5
0
        private async Task <Document> CreateDocumentChangeChequeStatus(ChequesAndStatsVM chequesAndStatsVM, int organId)
        {
            FinanYearRule finanYearRule = new FinanYearRule();

            var finanYears = await finanYearRule.GetAllByOrganIdAsync(organId);

            var        finanYear  = finanYears.Where(x => x.Closed == false).SingleOrDefault();
            ChequeRule chequeRule = new ChequeRule();
            var        cheque     = await chequeRule.FindAsync(chequesAndStatsVM.chequeId);

            DocumentRule documentRule = new DocumentRule();

            Document document = new Document();

            document.Credit      = cheque.Amount;
            document.DateTime    = PersianDateUtils.ToDateTime(chequesAndStatsVM.date);
            document.Debit       = cheque.Amount;
            document.Description = chequesAndStatsVM.description;
            document.DisplayDate = chequesAndStatsVM.date;
            document.IsManual    = false;
            document.Status      = ZhivarEnums.DocumentStatus.TaeedShode;
            document.Number      = await documentRule.createNumberDocumentAsync(organId);

            document.Number2 = await documentRule.createNumberDocumentAsync(organId);

            document.StatusString = "تایید شده";
            document.OrganId      = organId;
            document.FinanYear    = finanYear;
            document.FinanYearId  = finanYear.ID;
            document.Type         = ZhivarEnums.NoeDoc.Recive;

            List <Transaction> transactions = new List <Transaction>();

            AccountRule accountRule = new AccountRule();
            var         accounts    = await accountRule.GetAllByOrganIdAsync(organId);

            switch (chequesAndStatsVM.receiveType)
            {
            case "cash":
            {
                var accountCash = accounts.Where(x => x.ComplteCoding == "1101" + chequesAndStatsVM.detailAccount.Code).SingleOrDefault();

                transactions.Add(new Transaction()
                    {
                        //AccDocument = document,
                        AccountId   = accountCash.ID,
                        Amount      = cheque.Amount,
                        Description = chequesAndStatsVM.description,
                        Credit      = 0,
                        Debit       = cheque.Amount,
                        IsCredit    = false,
                        IsDebit     = true,
                        DocumentId  = document.ID,
                        Date        = PersianDateUtils.ToDateTime(chequesAndStatsVM.date),
                        DisplayDate = chequesAndStatsVM.date
                    });
                break;
            }

            case "bank":
            {
                var accountBank = accounts.Where(x => x.ComplteCoding == "1103" + chequesAndStatsVM.detailAccount.Code).SingleOrDefault();

                transactions.Add(new Transaction()
                    {
                        //AccDocument = document,
                        AccountId   = accountBank.ID,
                        Amount      = cheque.Amount,
                        Description = chequesAndStatsVM.description,
                        Credit      = 0,
                        Debit       = cheque.Amount,
                        IsCredit    = false,
                        IsDebit     = true,
                        DocumentId  = document.ID,
                        Date        = PersianDateUtils.ToDateTime(chequesAndStatsVM.date),
                        DisplayDate = chequesAndStatsVM.date
                    });
                break;
            }
            }

            var asnadDreaftaniAccount = accounts.Where(x => x.ComplteCoding == "1105" + cheque.Contact.Code).SingleOrDefault();

            transactions.Add(new Transaction()
            {
                //AccDocument = document,
                AccountId   = asnadDreaftaniAccount.ID,
                Amount      = cheque.Amount,
                Credit      = cheque.Amount,
                Debit       = 0,
                Description = chequesAndStatsVM.description,
                DocumentId  = document.ID,
                IsCredit    = true,
                IsDebit     = false,
                Date        = PersianDateUtils.ToDateTime(chequesAndStatsVM.date),
                DisplayDate = chequesAndStatsVM.date
            });


            document.Transactions = transactions;

            return(document);
        }
Exemplo n.º 6
0
        private async Task <Document> CreateDocumentPaidChequeReceipt(ChequesAndStatsVM chequesAndStatsVM, int organId)
        {
            FinanYearRule finanYearRule = new FinanYearRule();
            var           finanYears    = await finanYearRule.GetAllByOrganIdAsync(organId);

            var        finanYear  = finanYears.Where(x => x.Closed == false).SingleOrDefault();
            ChequeRule chequeRule = new ChequeRule();
            var        cheque     = await chequeRule.FindAsync(chequesAndStatsVM.chequeId);

            //var chequeBanks = await _chequeBankService.GetAllByOrganIdAsync(organId);
            var chequeBanks = this.BusinessRule.UnitOfWork.RepositoryAsync <ChequeBank>().Queryable().Where(x => x.OrganId == organId);
            var chequeBank  = chequeBanks.Where(x => x.ChequeId == cheque.ID).SingleOrDefault();

            BankRule bankRule = new BankRule();
            var      bank     = await bankRule.FindAsync(chequeBank.BankId);

            DocumentRule documentRule = new DocumentRule();

            Document document = new Document();

            document.Credit      = cheque.Amount;
            document.DateTime    = PersianDateUtils.ToDateTime(chequesAndStatsVM.date);
            document.Debit       = cheque.Amount;
            document.Description = chequesAndStatsVM.description;
            document.DisplayDate = chequesAndStatsVM.date;
            document.IsManual    = false;
            document.Status      = ZhivarEnums.DocumentStatus.TaeedShode;
            document.Number      = await documentRule.createNumberDocumentAsync(organId);

            document.Number2 = await documentRule.createNumberDocumentAsync(organId);

            document.StatusString = "تایید شده";
            document.OrganId      = organId;
            document.FinanYear    = finanYear;
            document.FinanYearId  = finanYear.ID;
            document.Type         = ZhivarEnums.NoeDoc.Recive;

            List <Transaction> transactions = new List <Transaction>();

            AccountRule accountRule = new AccountRule();
            var         accounts    = await accountRule.GetAllByOrganIdAsync(organId);

            var accountPay = accounts.Where(x => x.ComplteCoding == "2102" + bank.Code).SingleOrDefault();

            transactions.Add(new Transaction()
            {
                //AccDocument = document,
                AccountId   = accountPay.ID,
                Amount      = cheque.Amount,
                Description = chequesAndStatsVM.description,
                Credit      = 0,
                Debit       = cheque.Amount,
                IsCredit    = false,
                IsDebit     = true,
                DocumentId  = document.ID,
                Date        = PersianDateUtils.ToDateTime(chequesAndStatsVM.date),
                DisplayDate = chequesAndStatsVM.date
            });

            var accountBank = accounts.Where(x => x.ComplteCoding == "1103" + bank.Code).SingleOrDefault();

            transactions.Add(new Transaction()
            {
                //AccDocument = document,
                AccountId   = accountBank.ID,
                Amount      = cheque.Amount,
                Credit      = cheque.Amount,
                Debit       = 0,
                Description = chequesAndStatsVM.description,
                DocumentId  = document.ID,
                IsCredit    = true,
                IsDebit     = false,
                Date        = PersianDateUtils.ToDateTime(chequesAndStatsVM.date),
                DisplayDate = chequesAndStatsVM.date
            });


            document.Transactions = transactions;

            return(document);
        }
Exemplo n.º 7
0
        public async Task <HttpResponseMessage> GetRequiredDataToClosingFinanYear()
        {
            var organId = Convert.ToInt32(SecurityManager.CurrentUserContext.OrganizationId);

            TransactionRule transactionRule = new TransactionRule();
            var             transactions    = await transactionRule.GetAllByOrganIdAsync(organId);

            AccountRule accountRule = new AccountRule();
            var         accounts    = await accountRule.GetAllByOrganIdAsync(organId);

            var incomeAccount = accounts.Where(x => x.ComplteCoding == "71" || x.ComplteCoding == "72").Select(x => x.ID).ToList();
            var costAccount   = accounts.Where(x => x.ComplteCoding == "81" || x.ComplteCoding == "82" || x.ComplteCoding == "83").Select(x => x.ID).ToList();

            decimal incomeAmount = 0;
            decimal costAmount   = 0;

            var accountsMoienQuery = accounts.AsQueryable().Where(x => incomeAccount.Contains(x.ParentId));

            var allAccountQuery = accounts.AsQueryable();

            List <int> childIds = (from account in accountsMoienQuery
                                   select account.ID).ToList();

            List <int> childChildIds = (from account in allAccountQuery
                                        join accountsMoien in accountsMoienQuery
                                        on account.ParentId equals accountsMoien.ID
                                        select account.ID).ToList();


            var selected = transactions.Where(a => incomeAccount.Contains(a.AccountId) || childIds.Contains(a.AccountId) || childChildIds.Contains(a.AccountId)).ToList();

            if (selected.Any())
            {
                var credit = selected.Sum(x => x.Credit);
                var debit  = selected.Sum(x => x.Debit);
                incomeAmount = credit - debit;
            }


            var accountsMoienCostQuery = accounts.AsQueryable().Where(x => costAccount.Contains(x.ParentId));


            List <int> childIdsCost = (from account in accountsMoienCostQuery
                                       select account.ID).ToList();

            List <int> childChildIdsCost = (from account in allAccountQuery
                                            join accountsMoien in accountsMoienCostQuery
                                            on account.ParentId equals accountsMoien.ID
                                            select account.ID).ToList();


            var selectedCost = transactions.Where(a => costAccount.Contains(a.AccountId) || childIdsCost.Contains(a.AccountId) || childChildIdsCost.Contains(a.AccountId)).ToList();

            if (selectedCost.Any())
            {
                var credit = selectedCost.Sum(x => x.Credit);
                var debit  = selectedCost.Sum(x => x.Debit);
                costAmount = credit - debit;
            }

            FinanYearRule finanYearRule = new FinanYearRule();
            var           finanQuery    = await finanYearRule.GetAllByOrganIdAsync(organId);

            var finanYear = finanQuery.Where(x => x.FirstYear == true).SingleOrDefault();

            ShareholderRule shareholderRule = new ShareholderRule();
            var             shareHolders    = await shareholderRule.GetAllByOrganIdAsync(organId);

            var startDate = PersianDateUtils.ToDateTime(finanYear.DisplayStartDate);
            var endDate   = PersianDateUtils.ToDateTime(finanYear.DisplayEndDate);

            double diff2 = (endDate - startDate).TotalDays;

            var startDateNew = endDate.AddDays(1);
            var endDateNew   = endDate.AddDays(diff2);

            var displayEndDateNew   = PersianDateUtils.ToPersianDateTime(endDateNew);
            var displayStartDateNew = PersianDateUtils.ToPersianDateTime(startDateNew);

            var startYear  = Convert.ToInt32(displayStartDateNew.Substring(0, 4));
            var startMonth = Convert.ToInt32(displayStartDateNew.Substring(5, 2));
            var startDay   = Convert.ToInt32(displayStartDateNew.Substring(8, 2));


            var endYear  = Convert.ToInt32(displayEndDateNew.Substring(0, 4));
            var endMonth = Convert.ToInt32(displayEndDateNew.Substring(5, 2));
            var endDay   = Convert.ToInt32(displayEndDateNew.Substring(8, 2));

            var isMoreThanYear = false;

            if (diff2 > 365)
            {
                isMoreThanYear = true;
            }

            var resualt = new DataToClosingFinanYear();

            resualt.closingDate = finanYear.DisplayEndDate;
            resualt.netIncome   = incomeAmount - costAmount;

            resualt.newFinanYear = new FinanYearVM()
            {
                Closed           = false,
                DisplayEndDate   = displayEndDateNew,
                DisplayStartDate = displayStartDateNew,
                EndDate          = endDateNew,
                EndDateDay       = endDay,
                EndDateMonth     = endMonth,
                EndDateYear      = endYear,
                FirstYear        = false,
                Id = 0,
                IsMoreThanOneYear = isMoreThanYear,
                Name           = endYear + " سال مال منتهی به",
                Note           = "",
                StartDate      = startDate,
                StartDateDay   = startDay,
                StartDateMonth = startMonth,
                StartDateYear  = startYear,
            };
            resualt.shareholders = new List <ShareholderVM>();

            foreach (var shareHolder in shareHolders)
            {
                resualt.shareholders.Add(new ShareholderVM()
                {
                    Address      = shareHolder.Address,
                    City         = shareHolder.City,
                    Code         = shareHolder.Code,
                    ContactEmail = "",
                    ContactType  = shareHolder.ContactType,
                    Credits      = shareHolder.Credits,
                    //DetailAccount = new DetailAccount()
                    //{
                    //    Accounts = null,
                    //    Balance = 0,
                    //    BalanceType = 0,
                    //    Code = "000001",
                    //    Id = 1,
                    //    Name = "مونا ابراهیمی",
                    //    Node = new Node()
                    //    {
                    //        FamilyTree = "اشخاص",
                    //        Id = 1,
                    //        Name = "اشخاص",
                    //        Parent = null,
                    //        Parents = ",1,",
                    //        SystemAccount = 1
                    //    },
                    //    RelatedAccounts = ",6,22,7,32,33,34,35,",
                    //    credit = 0,
                    //    debit = 0,
                    //},
                    Email              = shareHolder.Email,
                    Fax                = shareHolder.Fax,
                    FirstName          = shareHolder.FirstName,
                    Id                 = (int)shareHolder.ID,
                    IsCustomer         = shareHolder.IsCustomer,
                    IsEmployee         = shareHolder.IsEmployee,
                    IsShareHolder      = shareHolder.IsShareHolder,
                    IsVendor           = shareHolder.IsVendor,
                    LastName           = shareHolder.LastName,
                    Liability          = shareHolder.Liability,
                    Mobile             = shareHolder.Mobile,
                    Name               = shareHolder.Name,
                    NationalCode       = shareHolder.NationalCode,
                    Note               = shareHolder.Note,
                    People             = shareHolder.People,
                    Phone              = shareHolder.Phone,
                    PostalCode         = shareHolder.PostalCode,
                    Rating             = shareHolder.Rating,
                    RegistrationDate   = shareHolder.RegistrationDate,
                    RegistrationNumber = shareHolder.RegistrationNumber,
                    SharePercent       = shareHolder.SharePercent,
                    State              = shareHolder.State,
                    Website            = shareHolder.Website,
                });
            }
            return(Request.CreateResponse(HttpStatusCode.OK, new { resualtCode = (int)ZhivarEnums.ResultCode.Successful, data = resualt }));
        }
Exemplo n.º 8
0
        public async Task <List <string> > SaveContract(ContractVM contractVM)
        {
            try
            {
                var organId = Convert.ToInt32(SecurityManager.CurrentUserContext.OrganizationId);

                FinanYearRule finanYearRule = new FinanYearRule();
                var           finanYears    = await finanYearRule.GetAllByOrganIdAsync(organId);

                var finanYear = finanYears.Where(x => x.Closed == false && x.OrganId == organId);

                contractVM.Contract_Sazes = contractVM.Contract_Sazes.Where(x => x.Saze != null).ToList();

                foreach (var contract_Saze in contractVM.Contract_Sazes)
                {
                    if (contract_Saze.Saze != null)
                    {
                        contract_Saze.SazeId = contract_Saze.Saze.ID;
                    }

                    if (contract_Saze.NoeEjare != null)
                    {
                        contract_Saze.NoeEjareId = contract_Saze.NoeEjare.ID;
                    }

                    if (contract_Saze.DisplayTarikhShorou != null)
                    {
                        contract_Saze.TarikhShorou = PersianDateUtils.ToDateTime(contract_Saze.DisplayTarikhShorou);



                        if (contract_Saze.NoeEjareId == 1)
                        {
                            contract_Saze.TarikhPayan = contract_Saze.TarikhShorou.AddDays((double)contract_Saze.Quantity);
                        }
                        else if (contract_Saze.NoeEjareId == 2)
                        {
                            PersianCalendar pc = new PersianCalendar();
                            contract_Saze.TarikhPayan        = pc.AddMonths(contract_Saze.TarikhShorou, (int)contract_Saze.Quantity);
                            contract_Saze.DisplayTarikhPayan = PersianDateUtils.ToPersianDate(contract_Saze.TarikhPayan);
                        }
                    }
                }

                if (contractVM.Contact != null)
                {
                    contractVM.ContactId = contractVM.Contact.ID;
                }

                DomainClasses.Contract.Contract contract = new DomainClasses.Contract.Contract();
                Mapper.Map(contractVM, contract);
                contract.OrganId  = organId;
                contract.DateTime = PersianDateUtils.ToDateTime(contract.DisplayDate);


                ContractValidate validator = new ContractValidate();
                FluentValidation.Results.ValidationResult results = validator.Validate(contract);

                List <string> failurs = new List <string>();;

                if (!results.IsValid)
                {
                    foreach (var error in results.Errors)
                    {
                        failurs.Add(error.ErrorMessage);
                    }
                    return(failurs);
                }

                //  contract = savePaymentForContract(contract, contractVM);
                if (SecurityManager.CurrentUserContext.Roles.Any(x => x.RoleCode == "Manager") &&
                    contractVM.ContractType == ContractType.RentFrom)
                {
                    contract.Status = Status.ConfirmationContract;
                }
                else if (SecurityManager.CurrentUserContext.Roles.Any(x => x.RoleCode == "Manager") &&
                         contractVM.ContractType == ContractType.PreContract)
                {
                    contract.Status = Status.ConfirmationPreContract;

                    contract.ContractType = ContractType.RentTo;

                    // contract.Contract_Sazes = uow.Repository<Contract_Saze>().Queryable().Where(x => x.ContractID == contract.ID).ToList();

                    foreach (var contract_Saze in contract.Contract_Sazes ?? new List <Contract_Saze>())
                    {
                        contract_Saze.Status = Status.ConfirmationPreContract;
                        //contract_Saze.ObjectState = OMF.Common.Enums.ObjectState.Modified;
                    }
                }
                else if (contractVM.ContractType == ContractType.PreContract && contractVM.Status == Status.SendPreContract)
                {
                    //nothing
                }
                else
                {
                    contract.Status = Status.Temporary;
                }

                if (contract.ID > 0)
                {
                    foreach (var contractSaze in contract.Contract_Sazes)
                    {
                        contractSaze.TarikhShorou = PersianDateUtils.ToDateTime(contractSaze.DisplayTarikhShorou);
                        contractSaze.ContractID   = contract.ID;

                        if (contractSaze.ID > 0)
                        {
                            contractSaze.ContractID  = contract.ID;
                            contractSaze.ObjectState = OMF.Common.Enums.ObjectState.Modified;
                        }

                        else
                        {
                            contractSaze.ContractID  = contract.ID;
                            contractSaze.ObjectState = OMF.Common.Enums.ObjectState.Added;
                        }


                        foreach (var contarct_Saze_Bazareab in contractSaze.Contarct_Saze_Bazareabs)
                        {
                            contractSaze.HasBazareab = true;
                            if (contarct_Saze_Bazareab.ID > 0)
                            {
                                contarct_Saze_Bazareab.ContarctSazeID = contractSaze.ID;
                                contarct_Saze_Bazareab.ObjectState    = ObjectState.Modified;
                            }
                            else
                            {
                                contarct_Saze_Bazareab.ContarctSazeID = contractSaze.ID;
                                contarct_Saze_Bazareab.ObjectState    = ObjectState.Added;
                            }
                        }

                        foreach (var contract_Saze_Tarah in contractSaze.Contract_Saze_Tarahs)
                        {
                            contractSaze.HasTarah = true;

                            if (contract_Saze_Tarah.ID > 0)
                            {
                                contract_Saze_Tarah.ContarctSazeID = contractSaze.ID;
                                contract_Saze_Tarah.ObjectState    = ObjectState.Modified;
                            }

                            else
                            {
                                contract_Saze_Tarah.ContarctSazeID = contractSaze.ID;
                                contract_Saze_Tarah.ObjectState    = ObjectState.Added;
                            }
                        }

                        foreach (var contract_Saze_Chapkhane in contractSaze.Contract_Saze_Chapkhanes)
                        {
                            contractSaze.HasChap = true;

                            if (contract_Saze_Chapkhane.ID > 0)
                            {
                                contract_Saze_Chapkhane.ContarctSazeID = contractSaze.ID;
                                contract_Saze_Chapkhane.ObjectState    = ObjectState.Modified;
                            }

                            else
                            {
                                contract_Saze_Chapkhane.ContarctSazeID = contractSaze.ID;
                                contract_Saze_Chapkhane.ObjectState    = ObjectState.Added;
                            }
                        }

                        foreach (var contract_Saze_Nasab in contractSaze.Contract_Saze_Nasabs)
                        {
                            contractSaze.HasNasab = true;
                            if (contract_Saze_Nasab.ID > 0)
                            {
                                contract_Saze_Nasab.ContarctSazeID = contractSaze.ID;
                                contract_Saze_Nasab.ObjectState    = ObjectState.Modified;
                            }

                            else
                            {
                                contract_Saze_Nasab.ContarctSazeID = contractSaze.ID;
                                contract_Saze_Nasab.ObjectState    = ObjectState.Added;
                            }
                        }
                    }


                    foreach (var contract_PayRecevie in contract.Contract_PayRecevies)
                    {
                        if (contract_PayRecevie.ID > 0)
                        {
                            contract_PayRecevie.ContractId  = contract.ID;
                            contract_PayRecevie.ObjectState = OMF.Common.Enums.ObjectState.Modified;
                        }

                        else
                        {
                            contract_PayRecevie.ContractId = contract.ID;
                            contract_PayRecevie.IsReceive  = true;
                            contract_PayRecevie.OrganId    = organId;
                            contract_PayRecevie.Status     = Status.Temporary;
                            contract_PayRecevie.Type       = PayRecevieType.Sir;
                            contract_PayRecevie.Date       = DateTime.Now;

                            contract_PayRecevie.ContactId   = contract.ID;
                            contract_PayRecevie.ObjectState = OMF.Common.Enums.ObjectState.Added;
                        }


                        foreach (var contract_DetailPayRecevie in contract_PayRecevie.Contract_DetailPayRecevies)
                        {
                            if (contract_DetailPayRecevie.ID > 0)
                            {
                                contract_DetailPayRecevie.Contract_PayRecevieId = contract_PayRecevie.ID;
                                contract_DetailPayRecevie.ObjectState           = ObjectState.Modified;
                            }
                            else
                            {
                                contract_DetailPayRecevie.Contract_PayRecevieId = contract_PayRecevie.ID;
                                contract_DetailPayRecevie.ObjectState           = ObjectState.Added;
                            }
                        }
                    }
                    contract.ObjectState = OMF.Common.Enums.ObjectState.Modified;
                }

                else
                {
                    foreach (var contractSaze in contract.Contract_Sazes)
                    {
                        contractSaze.ContractID   = contract.ID;
                        contractSaze.TarikhShorou = PersianDateUtils.ToDateTime(contractSaze.DisplayTarikhShorou);

                        if (contractSaze.ID > 0)
                        {
                            contractSaze.ContractID  = contract.ID;
                            contractSaze.ObjectState = OMF.Common.Enums.ObjectState.Modified;
                        }

                        else
                        {
                            contractSaze.ContractID  = contract.ID;
                            contractSaze.ObjectState = OMF.Common.Enums.ObjectState.Added;
                        }


                        foreach (var contarct_Saze_Bazareab in contractSaze.Contarct_Saze_Bazareabs)
                        {
                            contractSaze.HasBazareab = true;

                            if (contarct_Saze_Bazareab.NoeMozdBazryab == NoeMozd.Month)
                            {
                                contarct_Saze_Bazareab.Hazine = 0;
                            }

                            if (contarct_Saze_Bazareab.ID > 0)
                            {
                                contarct_Saze_Bazareab.ContarctSazeID = contractSaze.ID;
                                contarct_Saze_Bazareab.ObjectState    = ObjectState.Modified;
                            }
                            else
                            {
                                contarct_Saze_Bazareab.ContarctSazeID = contractSaze.ID;
                                contarct_Saze_Bazareab.ObjectState    = ObjectState.Added;
                            }
                        }

                        foreach (var contract_Saze_Tarah in contractSaze.Contract_Saze_Tarahs)
                        {
                            contractSaze.HasTarah = true;

                            if (contract_Saze_Tarah.NoeMozdTarah == NoeMozd.Month)
                            {
                                contract_Saze_Tarah.Hazine = 0;
                            }

                            if (contract_Saze_Tarah.ID > 0)
                            {
                                contract_Saze_Tarah.ContarctSazeID = contractSaze.ID;
                                contract_Saze_Tarah.ObjectState    = ObjectState.Modified;
                            }

                            else
                            {
                                contract_Saze_Tarah.ContarctSazeID = contractSaze.ID;
                                contract_Saze_Tarah.ObjectState    = ObjectState.Added;
                            }
                        }

                        foreach (var contract_Saze_Chapkhane in contractSaze.Contract_Saze_Chapkhanes)
                        {
                            contractSaze.HasChap = true;



                            if (contract_Saze_Chapkhane.ID > 0)
                            {
                                contract_Saze_Chapkhane.ContarctSazeID = contractSaze.ID;
                                contract_Saze_Chapkhane.ObjectState    = ObjectState.Modified;
                            }

                            else
                            {
                                contract_Saze_Chapkhane.ContarctSazeID = contractSaze.ID;
                                contract_Saze_Chapkhane.ObjectState    = ObjectState.Added;
                            }
                        }

                        foreach (var contract_Saze_Nasab in contractSaze.Contract_Saze_Nasabs)
                        {
                            contractSaze.HasNasab = true;

                            if (contract_Saze_Nasab.NoeMozdNasab == NoeMozd.Month)
                            {
                                contract_Saze_Nasab.Hazine = 0;
                            }

                            if (contract_Saze_Nasab.ID > 0)
                            {
                                contract_Saze_Nasab.ContarctSazeID = contractSaze.ID;
                                contract_Saze_Nasab.ObjectState    = ObjectState.Modified;
                            }

                            else
                            {
                                contract_Saze_Nasab.ContarctSazeID = contractSaze.ID;
                                contract_Saze_Nasab.ObjectState    = ObjectState.Added;
                            }
                        }
                    }

                    if (contract.Contract_PayRecevies != null && contract.Contract_PayRecevies.Count() > 0 &&
                        contract.Contract_PayRecevies[0].Contract_DetailPayRecevies != null && contract.Contract_PayRecevies[0].Contract_DetailPayRecevies.Count() > 0)
                    {
                        foreach (var contract_PayRecevie in contract.Contract_PayRecevies)
                        {
                            contract_PayRecevie.ContractId = contract.ID;
                            contract_PayRecevie.Date       = DateTime.Now;
                            contract_PayRecevie.IsReceive  = true;


                            if (contract_PayRecevie.ID > 0)
                            {
                                contract_PayRecevie.ContractId  = contract.ID;
                                contract_PayRecevie.ObjectState = OMF.Common.Enums.ObjectState.Modified;
                            }

                            else
                            {
                                contract_PayRecevie.ContractId  = contract.ID;
                                contract_PayRecevie.ObjectState = OMF.Common.Enums.ObjectState.Added;
                            }


                            foreach (var contract_DetailPayRecevie in contract_PayRecevie.Contract_DetailPayRecevies)
                            {
                                if (contract_DetailPayRecevie.ID > 0)
                                {
                                    contract_DetailPayRecevie.Contract_PayRecevieId = contract_PayRecevie.ID;
                                    contract_DetailPayRecevie.ObjectState           = ObjectState.Modified;
                                }
                                else
                                {
                                    contract_DetailPayRecevie.Contract_PayRecevieId = contract_PayRecevie.ID;
                                    contract_DetailPayRecevie.ObjectState           = ObjectState.Added;
                                }
                            }
                        }
                    }
                    else
                    {
                        contract.Contract_PayRecevies = null;
                    }
                    contract.ObjectState = OMF.Common.Enums.ObjectState.Added;
                }


                this.UnitOfWork.RepositoryAsync <DomainClasses.Contract.Contract>().InsertOrUpdateGraph(contract);



                //  ContactRule contactRule = new ContactRule();
                //   await contactRule.UpdateContact(invoice.InvoiceType, invoice.ContactId);



                await this.UnitOfWork.SaveChangesAsync();

                if (contractVM.ID <= 0)
                {
                    if ((SecurityManager.CurrentUserContext.Roles.Any(x => x.RoleCode == "Manager") &&
                         contractVM.ContractType == ContractType.PreContract) || (SecurityManager.CurrentUserContext.Roles.Any(x => x.RoleCode == "Manager") &&
                                                                                  contractVM.Status == Status.ConfirmationContract))
                    {
                        InvoiceRule invoiceRule = new InvoiceRule();
                        var         invoice     = invoiceRule.ConvertContractToInvoice(contract.ID, NoeFactor.RentTo);

                        var document = invoiceRule.RegisterDocument(invoice, invoice.OrganId);

                        DocumentRule documentRule = new DocumentRule();
                        await documentRule.InsertAsync(document, invoice.OrganId);

                        await documentRule.SaveChangesAsync();

                        invoice.DocumentID = document.ID;
                        invoiceRule.Update(invoice);
                        await invoiceRule.SaveChangesAsync();
                    }
                    else if (SecurityManager.CurrentUserContext.Roles.Any(x => x.RoleCode == "Manager") &&
                             contractVM.ContractType == ContractType.RentTo)
                    {
                        InvoiceRule invoiceRule = new InvoiceRule();
                        invoiceRule.ConvertContractToInvoice(contract.ID, NoeFactor.RentFrom);
                    }
                }



                return(failurs);
            }
            catch (Exception ex)
            {
                var p = ex;
                throw;
            }
        }
Exemplo n.º 9
0
        public async Task <HttpResponseMessage> SaveCost([FromBody] CostVM costVM)
        {
            try
            {
                bool ShouldSend = false;

                var organId = Convert.ToInt32(SecurityManager.CurrentUserContext.OrganizationId);

                FinanYearRule finanYearRule = new FinanYearRule();
                var           finanYears    = await finanYearRule.GetAllByOrganIdAsync(organId);

                var finanYear = finanYears.Where(x => x.Closed == false && x.OrganId == organId);

                costVM.CostItems = costVM.CostItems.Where(x => x.Item != null && x.Sum > 0).ToList();
                foreach (var costItem in costVM.CostItems)
                {
                    if (costItem.Item != null)
                    {
                        costItem.ItemId = costItem.Item.ID;
                    }
                }
                Cost cost = new Cost();
                Mapper.Map(costVM, cost);


                cost.OrganId = organId;

                if (costVM.Contact != null)
                {
                    cost.ContactId = costVM.Contact.ID;//.DetailAccount.Node.Id;
                }
                cost.CostItems = cost.CostItems.Where(x => x.ItemId != 0 && x.Sum > 0).ToList();

                CostValidate validator = new CostValidate();
                FluentValidation.Results.ValidationResult results = validator.Validate(cost);

                string failurs = "";

                if (!results.IsValid)
                {
                    foreach (var error in results.Errors)
                    {
                        failurs += "<br/>" + error.ErrorMessage;
                    }
                    return(Request.CreateResponse(HttpStatusCode.OK, new { resultCode = ZhivarEnums.ResultCode.ValidationError, data = failurs }));
                }

                if (SecurityManager.CurrentUserContext.Roles.Any(x => x.RoleCode == "Manager"))
                {
                    cost.Status = ZhivarEnums.CostStatus.WaitingToPay;
                }
                else if (costVM.Status == ZhivarEnums.CostStatus.WaitingToPay)
                {
                    ShouldSend = true;
                }
                else
                {
                    cost.Status = ZhivarEnums.CostStatus.Temporary;
                }

                //if (cost.Status == ZhivarEnums.CostStatus.WaitingToPay)
                //{
                //    var document = await Rule.RegisterDocument(costVM, organId);
                //    DocumentRule documentRule = new DocumentRule();
                //    await documentRule.InsertAsync(document, organId);
                //}

                if (cost.ID > 0)
                {
                    foreach (var costItem in cost.CostItems)
                    {
                        costItem.ObjectState = OMF.Common.Enums.ObjectState.Modified;
                        costItem.CostId      = cost.ID;
                    }

                    cost.ObjectState = OMF.Common.Enums.ObjectState.Modified;
                }
                else
                {
                    foreach (var costItem in cost.CostItems)
                    {
                        costItem.ObjectState = OMF.Common.Enums.ObjectState.Added;
                    }

                    cost.ObjectState = OMF.Common.Enums.ObjectState.Added;
                }

                Rule.InsertOrUpdateGraph(cost);



                if (costVM.ID <= 0)
                {
                    if (SecurityManager.CurrentUserContext.Roles.Any(x => x.RoleCode == "Manager"))
                    {
                        var document = await Rule.RegisterDocument(costVM, organId);

                        DocumentRule documentRule = new DocumentRule();
                        await documentRule.InsertAsync(document, organId);

                        await documentRule.SaveChangesAsync();

                        cost.DocumentId = document.ID;
                    }
                }

                await this.BusinessRule.UnitOfWork.SaveChangesAsync();

                if (ShouldSend)
                {
                    await StartWorkFlow(new WorkFlowBusiClass()
                    {
                        Code           = "Cost",
                        ID             = cost.ID,
                        OrganizationID = 22,
                        InstanceTitle  = cost.Explain
                    }, true);
                }

                return(Request.CreateResponse(HttpStatusCode.OK, new { resultCode = ZhivarEnums.ResultCode.Successful, data = cost }));
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Exemplo n.º 10
0
        public async Task <HttpResponseMessage> SaveInvoice([FromBody] InvoiceVM invoiceVM)
        {
            try
            {
                var organId = Convert.ToInt32(SecurityManager.CurrentUserContext.OrganizationId);

                FinanYearRule finanYearRule = new FinanYearRule();
                var           finanYears    = await finanYearRule.GetAllByOrganIdAsync(organId);

                var finanYear = finanYears.Where(x => x.Closed == false && x.OrganId == organId);

                invoiceVM.InvoiceItems = invoiceVM.InvoiceItems.Where(x => x.Item != null).ToList();

                foreach (var invoiceItem in invoiceVM.InvoiceItems)
                {
                    if (invoiceItem.Item != null)
                    {
                        invoiceItem.ItemId = invoiceItem.Item.ID;
                    }
                }

                if (invoiceVM.Contact != null)
                {
                    invoiceVM.ContactId = invoiceVM.Contact.ID;
                }

                Invoice invoice = new Invoice();
                Mapper.Map(invoiceVM, invoice);
                invoice.OrganId = organId;

                //if (invoice.Contact != null)
                //{

                //    invoice.ContactId = invoice.Contact.ID;
                //}

                //   invoice.InvoiceItems = invoice.InvoiceItems.Where(x => x.ItemId != 0).ToList();



                InvoiceValidate validator = new InvoiceValidate();
                FluentValidation.Results.ValidationResult results = validator.Validate(invoice);

                string failurs = "";

                if (!results.IsValid)
                {
                    foreach (var error in results.Errors)
                    {
                        failurs += "<br/>" + error.ErrorMessage;
                    }
                    return(Request.CreateResponse(HttpStatusCode.OK, new { resultCode = ZhivarEnums.ResultCode.ValidationError, data = failurs }));
                }



                if (invoice.ID > 0)
                {
                    foreach (var invoiceItem in invoice.InvoiceItems)
                    {
                        invoiceItem.InvoiceId = invoice.ID;

                        if (invoiceItem.ID > 0)
                        {
                            invoiceItem.ObjectState = OMF.Common.Enums.ObjectState.Modified;
                        }
                        else
                        {
                            invoiceItem.ObjectState = OMF.Common.Enums.ObjectState.Added;
                        }

                        //if (invoiceItem.Item != null)
                        //    invoiceItem.Item = null;//.ObjectState = OMF.Common.Enums.ObjectState.Unchanged;
                    }

                    invoice.ObjectState = OMF.Common.Enums.ObjectState.Modified;
                }

                else
                {
                    foreach (var invoiceItem in invoice.InvoiceItems)
                    {
                        invoiceItem.ObjectState = OMF.Common.Enums.ObjectState.Added;

                        //if (invoiceItem.Item != null)
                        //    invoiceItem.Item = null;//.ObjectState = OMF.Common.Enums.ObjectState.Unchanged;
                    }

                    invoice.ObjectState = OMF.Common.Enums.ObjectState.Added;
                }

                this.BusinessRule.UnitOfWork.RepositoryAsync <Invoice>().InsertOrUpdateGraph(invoice);
                ContactRule contactRule = new ContactRule();
                await contactRule.UpdateContact(invoice.InvoiceType, invoice.ContactId);

                //await this.BusinessRule.UnitOfWork.SaveChangesAsync();

                if (invoice.Status == ZhivarEnums.NoeInsertFactor.WaitingToReceive)
                {
                    var document = await Rule.RegisterDocumentAsync(invoiceVM, organId);

                    DocumentRule documentRule = new DocumentRule();
                    await documentRule.InsertAsync(document, organId);

                    await documentRule.SaveChangesAsync();

                    invoice.DocumentID = document.ID;
                    this.BusinessRule.UnitOfWork.RepositoryAsync <Invoice>().Update(invoice);
                    //await this.BusinessRule.UnitOfWork.SaveChangesAsync();
                }
                await this.BusinessRule.UnitOfWork.SaveChangesAsync();

                // await RegisterDocument(invoice, organId);

                return(Request.CreateResponse(HttpStatusCode.OK, new { resultCode = ZhivarEnums.ResultCode.Successful, data = invoice }));
            }
            catch (Exception ex)
            {
                throw;
            }
        }