Esempio n. 1
0
        public async Task <HttpResponseMessage> DeleteCosts([FromBody] string strIds)
        {
            try
            {
                var organId = Convert.ToInt32(SecurityManager.CurrentUserContext.OrganizationId);

                string failurs = "";

                string[] values = strIds.Split(',');
                for (int i = 0; i < values.Length - 1; i++)
                {
                    var id = Convert.ToInt32(values[i].Trim());

                    PayRecevieRule payRecevieRule  = new PayRecevieRule();
                    var            payRecevieQuery = await payRecevieRule.GetByCostIdAsync(id);

                    if (payRecevieQuery.Any())
                    {
                        failurs += "<br/>" + "برای این صورت هزینه دریافت/ پرداخت انجام شده است برای حذف باید دریافت و پرداخت حذف گردد.";
                    }
                    else
                    {
                        CostRule costRule = new CostRule();
                        await costRule.DeleteAsync(id);

                        await costRule.SaveChangesAsync();
                    }
                }



                if (!string.IsNullOrEmpty(failurs))
                {
                    return(Request.CreateResponse(HttpStatusCode.OK, new { resultCode = ZhivarEnums.ResultCode.ValidationError, data = failurs }));
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.OK, new { resultCode = ZhivarEnums.ResultCode.Successful, data = "" }));
                }
            }

            catch (Exception ex)
            {
                throw;
            }
        }
Esempio n. 2
0
        public async Task <HttpResponseMessage> LoadInvoiceTransObj([FromBody] int id)
        {
            try
            {
                var organId = Convert.ToInt32(SecurityManager.CurrentUserContext.OrganizationId);

                var invoice = await Rule.FindAsync(id);

                InvoiceTransObj invoiceTransObj = new InvoiceTransObj();

                invoiceTransObj.invoice      = Mapper.Map <InvoiceVM>(invoice);
                invoiceTransObj.invoiceItems = invoiceTransObj.invoice.InvoiceItems;
                //invoiceTransObj.payments =  await transactionRule.GetAllByInvoiceIdAsync(id,true,false);
                PayRecevieRule payRecevieRule = new PayRecevieRule();
                invoiceTransObj.tempPayments = await payRecevieRule.GetTempPaymentsByInvoiceIdAsync(id);

                for (int i = 0; i < invoiceTransObj.invoiceItems.Count; i++)
                {
                    ItemRule itemRule = new ItemRule();
                    invoiceTransObj.invoiceItems[i].Item = Mapper.Map <ItemVM>(await itemRule.FindAsync(invoiceTransObj.invoiceItems[i].ItemId));
                }

                ContactRule contactRule = new ContactRule();
                invoiceTransObj.invoice.Contact = Mapper.Map <ContactVM>(await contactRule.FindAsync(invoice.ContactId));
                CashRule cashRule = new CashRule();
                var      cashes   = await cashRule.GetAllByOrganIdAsync(Convert.ToInt32(organId));

                invoiceTransObj.cashes = Mapper.Map <List <CashVM> >(cashes);

                BankRule bankRule = new BankRule();
                var      banks    = await bankRule.GetAllByOrganIdAsync(Convert.ToInt32(organId));

                invoiceTransObj.banks = Mapper.Map <List <BankVM> >(banks);


                return(Request.CreateResponse(HttpStatusCode.OK, new { resultCode = ZhivarEnums.ResultCode.Successful, data = invoiceTransObj }));
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Esempio n. 3
0
        public virtual async Task <HttpResponseMessage> ChangeChequeStatus([FromBody] ChequesAndStatsVM chequesAndStatsVM)
        {
            var organId = Convert.ToInt32(SecurityManager.CurrentUserContext.OrganizationId);

            ChequeVM chequeVM = new ChequeVM();

            switch (chequesAndStatsVM.change)
            {
            // وصول چک پرداختی
            case "PaidChequeReceipt":
            {
                chequesAndStatsVM.description = " وصول چک پرداختی";
                var doucument = await CreateDocumentPaidChequeReceipt(chequesAndStatsVM, organId);

                ChequeRule chequeRule = new ChequeRule();
                var        cheque     = await chequeRule.FindAsync(chequesAndStatsVM.chequeId);

                cheque.Status = ZhivarEnums.ChequeStatus.Passed;
                chequeRule.Update(cheque);
                await chequeRule.SaveChangesAsync();

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

                await documentRule.SaveChangesAsync();



                //AccountNumber: ""
                chequeVM.Amount     = cheque.Amount;
                chequeVM.BankBranch = cheque.BankBranch;
                //chequeVM.BankDetailAccount: null
                chequeVM.BankName     = cheque.BankName;
                chequeVM.ChequeNumber = cheque.ChequeNumber;
                chequeVM.Contact      = cheque.Contact;
                chequeVM.Date         = cheque.Date;
                chequeVM.DepositBank  = cheque.DepositBank;
                //chequeVM.DepositDate = cheque.DepositBank;
                chequeVM.DisplayDate = cheque.DisplayDate;
                chequeVM.ID          = cheque.ID;
                //chequeVM.MyCheque = cheque.DepositBank;
                //chequeVM.Overdue = cheque.Overdue;
                //chequeVM.Payee = cheque.Payee;
                //chequeVM.Payer = cheque.Payer;
                chequeVM.ReceiptDate = cheque.ReceiptDate;
                //chequeVM.ReturnDate = cheque.ReturnDate;
                chequeVM.Status       = cheque.Status;
                chequeVM.StatusString = "وصول شده";
                break;
            }

            // وصول چک دریافتی
            case "ReceivedChequeReceipt":
            {
                chequesAndStatsVM.description = " وصول چک دریافتی";

                var doucument = await CreateDocumentChangeChequeStatus(chequesAndStatsVM, organId);


                ChequeRule chequeRule = new ChequeRule();
                var        cheque     = await chequeRule.FindAsync(chequesAndStatsVM.chequeId);

                cheque.Status = ZhivarEnums.ChequeStatus.Passed;
                chequeRule.Update(cheque);

                await chequeRule.SaveChangesAsync();

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

                await documentRule.SaveChangesAsync();


                //AccountNumber: ""
                chequeVM.Amount     = cheque.Amount;
                chequeVM.BankBranch = cheque.BankBranch;
                //chequeVM.BankDetailAccount: null
                chequeVM.BankName     = cheque.BankName;
                chequeVM.ChequeNumber = cheque.ChequeNumber;
                chequeVM.Contact      = cheque.Contact;
                chequeVM.Date         = cheque.Date;
                chequeVM.DepositBank  = cheque.DepositBank;
                //chequeVM.DepositDate = cheque.DepositBank;
                chequeVM.DisplayDate = cheque.DisplayDate;
                chequeVM.ID          = cheque.ID;
                //chequeVM.MyCheque = cheque.DepositBank;
                //chequeVM.Overdue = cheque.Overdue;
                //chequeVM.Payee = cheque.Payee;
                //chequeVM.Payer = cheque.Payer;
                chequeVM.ReceiptDate = cheque.ReceiptDate;
                //chequeVM.ReturnDate = cheque.ReturnDate;
                chequeVM.Status       = cheque.Status;
                chequeVM.StatusString = "وصول شده";
                break;
            }

            // وصول چک واگذار شده به بانک
            case "DepositChequeReceipt":
            {
                chequesAndStatsVM.description = "وصول چک واگذار شده به بانک";
                var doucument = await CreateDocumentDepositChequeReceipt(chequesAndStatsVM, organId);

                ChequeRule chequeRule = new ChequeRule();
                var        cheque     = await chequeRule.FindAsync(chequesAndStatsVM.chequeId);

                cheque.Status = ZhivarEnums.ChequeStatus.Passed;
                chequeRule.Update(cheque);

                await chequeRule.SaveChangesAsync();

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

                await documentRule.SaveChangesAsync();


                //AccountNumber: ""
                chequeVM.Amount     = cheque.Amount;
                chequeVM.BankBranch = cheque.BankBranch;
                //chequeVM.BankDetailAccount: null
                chequeVM.BankName     = cheque.BankName;
                chequeVM.ChequeNumber = cheque.ChequeNumber;
                chequeVM.Contact      = cheque.Contact;
                chequeVM.Date         = cheque.Date;
                chequeVM.DepositBank  = cheque.DepositBank;
                //chequeVM.DepositDate = cheque.DepositBank;
                chequeVM.DisplayDate = cheque.DisplayDate;
                chequeVM.ID          = cheque.ID;
                //chequeVM.MyCheque = cheque.DepositBank;
                //chequeVM.Overdue = cheque.Overdue;
                //chequeVM.Payee = cheque.Payee;
                //chequeVM.Payer = cheque.Payer;
                chequeVM.ReceiptDate = cheque.ReceiptDate;
                //chequeVM.ReturnDate = cheque.ReturnDate;
                chequeVM.Status       = cheque.Status;
                chequeVM.StatusString = "وصول شده";
                break;
            }

            // واگذاری چک به بانک
            case "ReceivedChequeDeposit":
            {
                chequesAndStatsVM.description = "واگذاری چک به بانک";

                var doucument = await CreateDocumentReceivedChequeDeposit(chequesAndStatsVM, organId);

                ChequeRule chequeRule = new ChequeRule();
                var        cheque     = await chequeRule.FindAsync(chequesAndStatsVM.chequeId);

                cheque.Status = ZhivarEnums.ChequeStatus.InProgress;

                BankRule bankRule = new BankRule();
                var      banks    = await bankRule.GetAllByOrganIdAsync(organId);

                var bank = banks.Where(x => x.Code == chequesAndStatsVM.detailAccount.Code).SingleOrDefault();
                cheque.DepositBankId = bank.ID;

                chequeRule.Update(cheque);
                await chequeRule.SaveChangesAsync();

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

                await documentRule.SaveChangesAsync();



                //AccountNumber: ""
                chequeVM.Amount     = cheque.Amount;
                chequeVM.BankBranch = cheque.BankBranch;
                //chequeVM.BankDetailAccount: null
                chequeVM.BankName     = cheque.BankName;
                chequeVM.ChequeNumber = cheque.ChequeNumber;
                chequeVM.Contact      = cheque.Contact;
                chequeVM.Date         = cheque.Date;
                chequeVM.DepositBank  = cheque.DepositBank;
                //chequeVM.DepositDate = cheque.DepositBank;
                chequeVM.DisplayDate = cheque.DisplayDate;
                chequeVM.ID          = cheque.ID;
                //chequeVM.MyCheque = cheque.DepositBank;
                //chequeVM.Overdue = cheque.Overdue;
                //chequeVM.Payee = cheque.Payee;
                //chequeVM.Payer = cheque.Payer;
                chequeVM.ReceiptDate = cheque.ReceiptDate;
                //chequeVM.ReturnDate = cheque.ReturnDate;
                chequeVM.Status       = cheque.Status;
                chequeVM.StatusString = "وصول شده";
                break;
            }

            // عودت دادن چک پرداختی
            case "PaidChequeReturn":
            {
                //chequesAndStatsVM.description = "عودت دادن چک پرداختی";

                var doucument = await CreateDocumentPaidChequeReturn(chequesAndStatsVM, organId);

                ChequeRule chequeRule = new ChequeRule();
                var        cheque     = await chequeRule.FindAsync(chequesAndStatsVM.chequeId);

                cheque.Status = ZhivarEnums.ChequeStatus.ChequeReturn;
                chequeRule.Update(cheque);

                await chequeRule.SaveChangesAsync();

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

                await documentRule.SaveChangesAsync();


                //AccountNumber: ""
                chequeVM.Amount     = cheque.Amount;
                chequeVM.BankBranch = cheque.BankBranch;
                //chequeVM.BankDetailAccount: null
                chequeVM.BankName     = cheque.BankName;
                chequeVM.ChequeNumber = cheque.ChequeNumber;
                chequeVM.Contact      = cheque.Contact;
                chequeVM.Date         = cheque.Date;
                chequeVM.DepositBank  = cheque.DepositBank;
                //chequeVM.DepositDate = cheque.DepositBank;
                chequeVM.DisplayDate = cheque.DisplayDate;
                chequeVM.ID          = cheque.ID;
                //chequeVM.MyCheque = cheque.DepositBank;
                //chequeVM.Overdue = cheque.Overdue;
                //chequeVM.Payee = cheque.Payee;
                //chequeVM.Payer = cheque.Payer;
                chequeVM.ReceiptDate = cheque.ReceiptDate;
                //chequeVM.ReturnDate = cheque.ReturnDate;
                chequeVM.Status       = cheque.Status;
                chequeVM.StatusString = "وصول شده";
                break;
            }

            // عودت دادن چک دریافتی
            case "ReceivedChequeReturn":
            {
                chequesAndStatsVM.description = "عودت دادن چک دریافتی";

                var doucument = await CreateDocumentReceivedChequeReturn(chequesAndStatsVM, organId);

                ChequeRule chequeRule = new ChequeRule();
                var        cheque     = await chequeRule.FindAsync(chequesAndStatsVM.chequeId);

                cheque.Status = ZhivarEnums.ChequeStatus.ChequeReturn;
                chequeRule.Update(cheque);

                await chequeRule.SaveChangesAsync();

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

                await documentRule.SaveChangesAsync();


                //AccountNumber: ""
                chequeVM.Amount     = cheque.Amount;
                chequeVM.BankBranch = cheque.BankBranch;
                //chequeVM.BankDetailAccount: null
                chequeVM.BankName     = cheque.BankName;
                chequeVM.ChequeNumber = cheque.ChequeNumber;
                chequeVM.Contact      = cheque.Contact;
                chequeVM.Date         = cheque.Date;
                chequeVM.DepositBank  = cheque.DepositBank;
                //chequeVM.DepositDate = cheque.DepositBank;
                chequeVM.DisplayDate = cheque.DisplayDate;
                chequeVM.ID          = cheque.ID;
                //chequeVM.MyCheque = cheque.DepositBank;
                //chequeVM.Overdue = cheque.Overdue;
                //chequeVM.Payee = cheque.Payee;
                //chequeVM.Payer = cheque.Payer;
                chequeVM.ReceiptDate = cheque.ReceiptDate;
                //chequeVM.ReturnDate = cheque.ReturnDate;
                chequeVM.Status       = cheque.Status;
                chequeVM.StatusString = "وصول شده";
                break;
            }

            // تغییر وضعیت چک پرداختی به پاس نشده
            case "PaidChequeToNotPass":
            {
                chequesAndStatsVM.description = "تغییر وضعیت چک پرداختی به پاس نشده";
                chequesAndStatsVM.date        = PersianDateUtils.ToPersianDate(DateTime.Now);

                var doucument = await CreateDocumentPaidChequeToNotPass(chequesAndStatsVM, organId);

                ChequeRule chequeRule = new ChequeRule();
                var        cheque     = await chequeRule.FindAsync(chequesAndStatsVM.chequeId);

                cheque.Status = ZhivarEnums.ChequeStatus.Normal;
                chequeRule.Update(cheque);
                await chequeRule.SaveChangesAsync();

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

                await documentRule.SaveChangesAsync();


                //AccountNumber: ""
                chequeVM.Amount     = cheque.Amount;
                chequeVM.BankBranch = cheque.BankBranch;
                //chequeVM.BankDetailAccount: null
                chequeVM.BankName     = cheque.BankName;
                chequeVM.ChequeNumber = cheque.ChequeNumber;
                chequeVM.Contact      = cheque.Contact;
                chequeVM.Date         = cheque.Date;
                chequeVM.DepositBank  = cheque.DepositBank;
                //chequeVM.DepositDate = cheque.DepositBank;
                chequeVM.DisplayDate = cheque.DisplayDate;
                chequeVM.ID          = cheque.ID;
                //chequeVM.MyCheque = cheque.DepositBank;
                //chequeVM.Overdue = cheque.Overdue;
                //chequeVM.Payee = cheque.Payee;
                //chequeVM.Payer = cheque.Payer;
                chequeVM.ReceiptDate = cheque.ReceiptDate;
                //chequeVM.ReturnDate = cheque.ReturnDate;
                chequeVM.Status       = cheque.Status;
                chequeVM.StatusString = "وصول شده";
                break;
            }

            // تغییر وضعیت چک دریافتی به وصول نشده
            case "ReceivedChequeToNotPass":
            {
                chequesAndStatsVM.description = "تغییر وضعیت چک دریافتی به وصول نشده";

                ChequeRule chequeRule = new ChequeRule();
                var        cheque     = await chequeRule.FindAsync(chequesAndStatsVM.chequeId);

                PayRecevieRule  payRecevieRule = new PayRecevieRule();
                List <Document> doucuments     = await payRecevieRule.GetDoucumentIDByChequeIdAsync(chequesAndStatsVM.chequeId);

                cheque.Status = ZhivarEnums.ChequeStatus.Normal;
                chequeRule.Update(cheque);

                Document     newDocument  = new Document();
                DocumentRule documentRule = new DocumentRule();

                foreach (var document in doucuments)
                {
                    newDocument = new Document()
                    {
                        Credit         = document.Credit,
                        Description    = chequesAndStatsVM.description,
                        Debit          = document.Debit,
                        DateTime       = DateTime.Now,
                        DisplayDate    = PersianDateUtils.ToPersianDate(DateTime.Now),
                        FinanYear      = document.FinanYear,
                        FinanYearId    = document.FinanYearId,
                        IsFirsDocument = document.IsFirsDocument,
                        IsManual       = document.IsManual,
                        Number         = await documentRule.createNumberDocumentAsync(organId),
                        Number2        = await documentRule.createNumberDocumentAsync(organId),
                        OrganId        = organId,
                        Status         = ZhivarEnums.DocumentStatus.TaeedShode,
                        StatusString   = document.StatusString,
                        Type           = ZhivarEnums.NoeDoc.Pay
                    };
                    TransactionRule transactionRule = new TransactionRule();
                    var             transactions    = await transactionRule.GetAllByOrganIdAsync(organId);

                    transactions = transactions.Where(x => x.DocumentId == document.ID).ToList();

                    newDocument.Transactions = new List <Transaction>();
                    Transaction newTransaction = new Transaction();

                    foreach (var transaction in transactions)
                    {
                        //newTransaction.AccDocument = newDocument;
                        //newTransaction.Account = transaction.Account;
                        newTransaction.AccountId             = transaction.AccountId;
                        newTransaction.Amount                = transaction.Amount;
                        newTransaction.Cheque                = transaction.Cheque;
                        newTransaction.ChequeId              = transaction.ChequeId;
                        newTransaction.ContactId             = transaction.ContactId;
                        newTransaction.Credit                = transaction.Debit;
                        newTransaction.Date                  = DateTime.Now;
                        newTransaction.Debit                 = transaction.Credit;
                        newTransaction.Description           = chequesAndStatsVM.description;
                        newTransaction.DisplayDate           = PersianDateUtils.ToPersianDate(DateTime.Now);
                        newTransaction.DocumentId            = newDocument.ID;
                        newTransaction.InvoiceId             = transaction.InvoiceId;
                        newTransaction.IsCredit              = transaction.IsDebit;
                        newTransaction.IsDebit               = transaction.IsCredit;
                        newTransaction.PaymentMethod         = transaction.PaymentMethod;
                        newTransaction.PaymentMethodString   = transaction.PaymentMethodString;
                        newTransaction.Reference             = transaction.Reference;
                        newTransaction.RefTrans              = transaction.RefTrans;
                        newTransaction.Remaining             = transaction.Remaining;
                        newTransaction.RemainingType         = transaction.RemainingType;
                        newTransaction.RowNumber             = transaction.RowNumber;
                        newTransaction.Stock                 = transaction.Stock;
                        newTransaction.TransactionTypeString = transaction.TransactionTypeString;
                        newTransaction.Type                  = transaction.Type;
                        newTransaction.UnitPrice             = transaction.UnitPrice;

                        newDocument.Transactions.Add(newTransaction);
                    }
                }

                await documentRule.InsertAsync(newDocument, organId);

                await documentRule.SaveChangesAsync();


                //AccountNumber: ""
                chequeVM.Amount     = cheque.Amount;
                chequeVM.BankBranch = cheque.BankBranch;
                //chequeVM.BankDetailAccount: null
                chequeVM.BankName     = cheque.BankName;
                chequeVM.ChequeNumber = cheque.ChequeNumber;
                chequeVM.Contact      = cheque.Contact;
                chequeVM.Date         = cheque.Date;
                chequeVM.DepositBank  = cheque.DepositBank;
                //chequeVM.DepositDate = cheque.DepositBank;
                chequeVM.DisplayDate = cheque.DisplayDate;
                chequeVM.ID          = cheque.ID;
                //chequeVM.MyCheque = cheque.DepositBank;
                //chequeVM.Overdue = cheque.Overdue;
                //chequeVM.Payee = cheque.Payee;
                //chequeVM.Payer = cheque.Payer;
                chequeVM.ReceiptDate = cheque.ReceiptDate;
                //chequeVM.ReturnDate = cheque.ReturnDate;
                chequeVM.Status       = cheque.Status;
                chequeVM.StatusString = "وصول شده";
                break;
            }


            default:
                break;
            }

            return(Request.CreateResponse(HttpStatusCode.OK, new { resultCode = ZhivarEnums.ResultCode.Successful, data = chequeVM }));
        }
Esempio n. 4
0
        public async Task <HttpResponseMessage> SaveReceiveAndPay([FromBody] PayRecevie payRecevie)
        {
            try
            {
                var organId = Convert.ToInt32(SecurityManager.CurrentUserContext.OrganizationId);

                FinanYearRule finanYearRule = new FinanYearRule();
                var           finanYerar    = await finanYearRule.GetCurrentFinanYear(organId);

                var documentNumber   = 0;
                int payRecevieNumber = 0;

                if (payRecevie.ID > 0)
                {
                    PayRecevie temp = new PayRecevie();
                    temp = payRecevie;
                    PayRecevieRule payRecevieRule = new PayRecevieRule();
                    payRecevieRule.Delete(payRecevie.ID);

                    documentNumber   = payRecevie.Document.Number;
                    payRecevieNumber = payRecevie.Number;

                    await payRecevieRule.SaveChangesAsync();
                }

                payRecevie.ID = -1;

                if (payRecevie.Contact != null)
                {
                    payRecevie.ContactId           = payRecevie.Contact.ID;
                    payRecevie.Contact.ObjectState = OMF.Common.Enums.ObjectState.Unchanged;
                }


                if (payRecevie.Invoice != null)
                {
                    payRecevie.InvoiceId           = payRecevie.Invoice.ID;
                    payRecevie.Invoice.ObjectState = OMF.Common.Enums.ObjectState.Unchanged;
                }

                if (payRecevie.Cost != null)
                {
                    payRecevie.CostId           = payRecevie.Cost.ID;
                    payRecevie.Cost.ObjectState = OMF.Common.Enums.ObjectState.Unchanged;
                }


                payRecevie.OrganId = organId;

                payRecevie.Type        = payRecevie.Type;
                payRecevie.Date        = PersianDateUtils.ToDateTime(payRecevie.DisplayDate);
                payRecevie.DisplayDate = payRecevie.DisplayDate;
                // payRecevie.Date = DateTime.Now;
                //payRecevie.DisplayDate = Utilities.PersianDateUtils.ToPersianDate(DateTime.Now);
                decimal amount = 0;
                foreach (var item in payRecevie.Items)
                {
                    amount += item.Amount;

                    if (item.Cash != null)
                    {
                        item.CashId = item.Cash.ID;
                    }
                    else if (item.Bank != null)
                    {
                        item.BankId = item.Bank.ID;
                    }
                    else if (item.Cheque != null && item.Type == ZhivarEnums.DetailPayReceiveType.Cheque)
                    {
                        Cheque cheque = new Cheque()
                        {
                            Amount       = item.Amount,
                            BankBranch   = item.Cheque.BankBranch,
                            BankName     = item.Cheque.BankName,
                            ChequeNumber = item.Cheque.ChequeNumber,
                            Contact      = payRecevie.Contact,
                            ContactId    = payRecevie.Contact.ID,
                            Date         = PersianDateUtils.ToDateTime(item.Cheque.DisplayDate),
                            OrganId      = organId,
                            Status       = ZhivarEnums.ChequeStatus.Normal,
                            DisplayDate  = item.Cheque.DisplayDate,
                        };

                        if (payRecevie.IsReceive)
                        {
                            cheque.Type = ZhivarEnums.ChequeType.Dareaftani;
                        }
                        else
                        {
                            cheque.Type = ZhivarEnums.ChequeType.Pardakhtani;
                        }

                        if (item.Cheque.ReceiptDate != null)
                        {
                            cheque.ReceiptDate = item.Cheque.ReceiptDate;
                        }


                        item.Cheque             = cheque;
                        item.ChequeId           = item.Cheque.ID;
                        item.Cheque.ObjectState = OMF.Common.Enums.ObjectState.Added;

                        if (!payRecevie.IsReceive)
                        {
                            var temp = item.ChequeBank.ID;

                            item.ChequeBank = new ChequeBank()
                            {
                                BankId   = temp,
                                ChequeId = item.Cheque.ID,
                                OrganId  = organId
                            };

                            item.ChequeBank.ObjectState = OMF.Common.Enums.ObjectState.Added;
                            item.ChequeBankId           = item.ChequeBank.ID;
                        }

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

                    if (item.Type == ZhivarEnums.DetailPayReceiveType.KharjCheque)
                    {
                        ChequeRule chequeRule = new ChequeRule();
                        var        cheque     = await chequeRule.FindAsync(item.Cheque.ID);

                        cheque.Status = ZhivarEnums.ChequeStatus.Sold;
                        chequeRule.Update(cheque);
                        await chequeRule.SaveChangesAsync();

                        item.Cheque.ContactId = item.Cheque.Contact.ID;
                    }

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

                payRecevie.Amount = amount;

                if (payRecevie.Invoice != null)
                {
                    //if (payRecevie.Invoice.Contact != null)
                    payRecevie.Invoice.ContactId = payRecevie.Invoice.ContactId;

                    //payRecevie.Invoice.FinanYear = finanYerar;
                    //payRecevie.Invoice.FinanYearId = finanYerar.ID;
                    var invoice = payRecevie.Invoice;
                    invoice.Rest -= amount;
                    invoice.Paid += amount;
                    if (invoice.Rest <= 0)
                    {
                        invoice.Status = ZhivarEnums.NoeInsertFactor.Received;
                    }
                    this.BusinessRule.UnitOfWork.RepositoryAsync <Invoice>().Update(invoice);
                }

                if (payRecevie.Cost != null)
                {
                    //if (payRecevie.Cost.Contact != null)
                    payRecevie.Cost.ContactId = payRecevie.Cost.ContactId;


                    var cost = payRecevie.Cost;
                    cost.Rest -= amount;
                    cost.Paid += amount;
                    if (cost.Rest <= 0)
                    {
                        cost.Status = ZhivarEnums.CostStatus.Paid;
                    }


                    this.BusinessRule.UnitOfWork.RepositoryAsync <Cost>().Update(cost);
                }

                payRecevie.Document = await createDocument(payRecevie, organId, documentNumber);

                if (payRecevieNumber > 0)
                {
                    payRecevie.Number = payRecevieNumber;
                }
                else
                {
                    var payRecevies = await Rule.GetAllByOrganIdAsync(organId);

                    var lastPayRecevies = payRecevies.OrderByDescending(x => x.ID).FirstOrDefault();

                    if (lastPayRecevies != null)
                    {
                        payRecevie.Number = lastPayRecevies.Number + 1;
                    }
                    else
                    {
                        payRecevie.Number = 1;
                    }
                }

                foreach (var transaction in payRecevie.Document.Transactions)
                {
                    transaction.ObjectState = OMF.Common.Enums.ObjectState.Added;
                    //if (transaction.AccDocument != null)
                    //    transaction.AccDocument.ObjectState = OMF.Common.Enums.ObjectState.Unchanged;

                    if (transaction.Account != null)
                    {
                        transaction.Account.ObjectState = OMF.Common.Enums.ObjectState.Unchanged;
                    }

                    // transaction.Transactions = null;
                }
                payRecevie.Document.ObjectState = OMF.Common.Enums.ObjectState.Added;

                payRecevie.ObjectState = OMF.Common.Enums.ObjectState.Added;
                this.BusinessRule.UnitOfWork.RepositoryAsync <PayRecevie>().InsertOrUpdateGraph(payRecevie);

                await this.BusinessRule.UnitOfWork.SaveChangesAsync();

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