Esempio n. 1
0
        /*
         * internal static string DeleteCashOutReceipt(int receiptId, int invID, string deleteReason)
         * {
         *
         *  string isSaved = "false";
         *
         *  if (receiptId == 0)
         *      return isSaved;
         *
         *  AccountingEntities db = new AccountingEntities();
         *
         *  CashOutReceipt cashOutDb = new CashOutReceipt();
         *  cashOutDb = db.CashOutReceipts
         *          .Where(x => x.ReceiptId == receiptId).FirstOrDefault();
         *
         *  CashOutReceiptCheck cashOutCheckDb = new CashOutReceiptCheck();
         *  cashOutCheckDb = db.CashOutReceiptChecks
         *          .Where(x => x.ReceiptId == receiptId).FirstOrDefault();
         *
         *  CashOutReceiptInv cashOutInvkDb = new CashOutReceiptInv();
         *  cashOutInvkDb = db.CashOutReceiptInvs
         *          .Where(x => x.ReceiptId == receiptId).FirstOrDefault();
         *
         *
         *
         *  int? transID = null;
         *  if (cashOutDb.TransId != null)
         *      transID = cashOutDb.TransId.Value;
         *  cashOutDb.IsDeleted = true;
         *  cashOutDb.DeleteReason = deleteReason;
         *  cashOutDb.DeletedBy = EasyFreight.DAL.AdminHelper.GetCurrentUserId();
         *  cashOutDb.TransId = null;
         *
         *
         *  if (cashOutCheckDb != null)
         *      db.CashOutReceiptChecks.Remove(cashOutCheckDb);
         *
         *  if (cashOutInvkDb != null)
         *      db.CashOutReceiptInvs.Remove(cashOutInvkDb);
         *
         *  try
         *  {
         *      db.SaveChanges();
         *
         *      if (transID.HasValue)
         *          AccountingHelper.DeleteTransaction(transID.Value);
         *
         *      List<CashInInvoiceVm> cashOutVmObj = GetCashInvList(invID);
         *
         *      if (invID != 0)
         *      {
         *          if (cashOutVmObj.Count == 0)
         *              InvoiceHelper.ChangeInvStatus(invID, InvStatusEnum.Approved, true);
         *          else
         *          {
         *
         *              InvoiceAP invDb = db.InvoiceAPs.Where(x => x.InvoiceId == invID).FirstOrDefault();
         *
         *              decimal InvoiceTotals = invDb.InvoiceTotalAPs.Sum(s => s.TotalAmount);
         *
         *              decimal? totalPaidAmount = cashOutVmObj.Sum(s => s.PaidAmount);
         *
         *              if (totalPaidAmount == null)
         *                  InvoiceHelper.ChangeInvStatus(invID, InvStatusEnum.Approved, true);
         *              else
         *              {
         *                  if (totalPaidAmount == InvoiceTotals)
         *                      InvoiceHelper.ChangeInvStatus(invID, InvStatusEnum.Paid, true);
         *                  if (totalPaidAmount < InvoiceTotals)
         *                      InvoiceHelper.ChangeInvStatus(invID, InvStatusEnum.PartiallyPaid, true);
         *              }
         *          }
         *      }
         *
         *      isSaved = "true";
         *  }
         *  catch
         *  {
         *      isSaved = "false";
         *  }
         *
         *
         *
         *  return isSaved;
         *
         * }
         */

        internal static string AddEditOpenCashReceipt(CashInVm cashInVmObj, out int savedReceiptId, bool addToTrans = true)
        {
            string isSaved = "true";

            AccountingEntities db = new AccountingEntities();



            int receiptId = cashInVmObj.ReceiptId;

            savedReceiptId = receiptId;



            CashOutReceipt cashDbObj;

            if (receiptId == 0)
            {
                cashDbObj = new CashOutReceipt();
            }
            else
            {
                cashDbObj = db.CashOutReceipts.Include("CashOutReceiptChecks")
                            .Include("CashOutReceiptInvs")
                            .Where(x => x.ReceiptId == receiptId).FirstOrDefault();

                //Delete invoice list .. will insert it again
                var invList = cashDbObj.CashOutReceiptInvs.ToList();
                foreach (var item in invList)
                {
                    cashDbObj.CashOutReceiptInvs.Remove(item);
                }

                //Delete check list .. will insert it again
                var checkList = cashDbObj.CashOutReceiptChecks.ToList();
                foreach (var item in checkList)
                {
                    cashDbObj.CashOutReceiptChecks.Remove(item);
                }
            }

            Mapper.CreateMap <CashInVm, CashOutReceipt>()
            .ForMember(x => x.CashOutReceiptInvs, y => y.Ignore())
            .ForMember(x => x.CashOutReceiptChecks, y => y.Ignore())
            .IgnoreAllNonExisting();
            Mapper.Map(cashInVmObj, cashDbObj);

            CashOutReceiptCheck cashCheckDb;

            foreach (var item in cashInVmObj.CashInReceiptChecks)
            {
                if (!string.IsNullOrEmpty(item.CheckNumber))
                {
                    cashCheckDb = new CashOutReceiptCheck();
                    Mapper.CreateMap <CashInCheckVm, CashOutReceiptCheck>().IgnoreAllNonExisting();
                    Mapper.Map(item, cashCheckDb);

                    cashDbObj.CashOutReceiptChecks.Add(cashCheckDb);
                }
            }

            using (TransactionScope transaction = new TransactionScope())
            {
                try
                {
                    if (receiptId == 0)
                    {
                        cashDbObj.ReceiptCode = AdminHelper.GeneratePrefixCode(PrefixForEnum.CashOut, true);
                        db.CashOutReceipts.Add(cashDbObj);
                    }

                    db.SaveChanges();

                    cashInVmObj.ReceiptId   = cashDbObj.ReceiptId;
                    cashInVmObj.ReceiptCode = cashDbObj.ReceiptCode;

                    savedReceiptId = cashInVmObj.ReceiptId;

                    #region Add To Transaction Table
                    if (addToTrans)
                    {
                        string creditAccountId = "";

                        if (cashInVmObj.ShipperId != null)
                        {
                            creditAccountId = AccountingChartHelper.GetAccountIdByPkAndTbName(cashInVmObj.ShipperId.Value, "Shipper", "ShipperId");
                        }
                        if (cashInVmObj.CarrierId != null)
                        {
                            creditAccountId = AccountingChartHelper.GetAccountIdByPkAndTbName(cashInVmObj.CarrierId.Value, "Carrier", "CarrierId");
                        }
                        if (cashInVmObj.ContractorId != null)
                        {
                            creditAccountId = AccountingChartHelper.GetAccountIdByPkAndTbName(cashInVmObj.ContractorId.Value, "Contractor", "ContractorId");
                        }
                        if (cashInVmObj.AgentId != null)
                        {
                            creditAccountId = AccountingChartHelper.GetAccountIdByPkAndTbName(cashInVmObj.AgentId.Value, "Agent", "AgentId");
                        }
                        if (cashInVmObj.ConsigneeId != null)
                        {
                            creditAccountId = AccountingChartHelper.GetAccountIdByPkAndTbName(cashInVmObj.ConsigneeId.Value, "Consignee", "ConsigneeId");
                        }

                        //AccTransaction accTran = new AccTransaction()
                        //{
                        //    CreateBy = AdminHelper.GetCurrentUserId(),
                        //    CreateDate = DateTime.Now,
                        //    TransactionName = "pay open balance"
                        //};

                        //AccTransactionDetail accTranDetail = new AccTransactionDetail();

                        //accTranDetail.AccountId = creditAccountId;
                        //accTranDetail.DebitAmount = cashInVmObj.ReceiptAmount.Value;
                        //accTranDetail.CurrencyId = cashInVmObj.CurrencyId;

                        //accTran.AccTransactionDetails.Add(accTranDetail);

                        //db.AccTransactions.Add(accTran);

                        //db.SaveChanges();

                        AddReceiptToTransTable(creditAccountId, cashInVmObj, true);
                    }

                    #endregion

                    transaction.Complete();
                }
                catch (DbEntityValidationException e)
                {
                    isSaved = "false " + e.Message;

                    //AdminHelper.LastIdRemoveOne(PrefixForEnum.AccountingInvoice);
                }
                catch (Exception e)
                {
                    isSaved = "false " + e.Message;
                    //AdminHelper.LastIdRemoveOne(PrefixForEnum.AccountingInvoice);
                }
            }

            return(isSaved);
        }
Esempio n. 2
0
        internal static string AddEditExpenseCashReceipt(CashInVm cashInVmObj)
        {
            string isSaved = "true";

            AccountingEntities db    = new AccountingEntities();
            int            receiptId = cashInVmObj.ReceiptId;
            CashOutReceipt cashDbObj;

            if (receiptId == 0)
            {
                cashDbObj = new CashOutReceipt();
            }
            else
            {
                cashDbObj = db.CashOutReceipts.Include("CashOutReceiptExpenses")
                            .Where(x => x.ReceiptId == receiptId).FirstOrDefault();

                //Delete expenses list .. will insert it again
                var invList = cashDbObj.CashOutReceiptExpenses.ToList();
                foreach (var item in invList)
                {
                    cashDbObj.CashOutReceiptExpenses.Remove(item);
                }
            }

            Mapper.CreateMap <CashInVm, CashOutReceipt>()
            .ForMember(x => x.CashOutReceiptInvs, y => y.Ignore())
            .ForMember(x => x.CashOutReceiptChecks, y => y.Ignore());

            Mapper.CreateMap <CashOutExpense, CashOutReceiptExpense>();
            Mapper.Map(cashInVmObj, cashDbObj);

            using (TransactionScope transaction = new TransactionScope())
            {
                try
                {
                    if (receiptId == 0)
                    {
                        cashDbObj.ReceiptCode = AdminHelper.GeneratePrefixCode(PrefixForEnum.CashOut, true);
                        db.CashOutReceipts.Add(cashDbObj);
                    }

                    db.SaveChanges();

                    cashInVmObj.ReceiptId   = cashDbObj.ReceiptId;
                    cashInVmObj.ReceiptCode = cashDbObj.ReceiptCode;

                    AddExpenseReceiptToTrans(cashInVmObj);

                    transaction.Complete();
                }
                catch (DbEntityValidationException e)
                {
                    isSaved = "false " + e.Message;

                    //AdminHelper.LastIdRemoveOne(PrefixForEnum.AccountingInvoice);
                }
                catch (Exception e)
                {
                    isSaved = "false " + e.Message;
                    //AdminHelper.LastIdRemoveOne(PrefixForEnum.AccountingInvoice);
                }
            }

            return(isSaved);
        }
Esempio n. 3
0
        internal static string AddEditCashReceipt(CashInVm cashInVmObj, out int savedReceiptId, bool addToTrans = true)
        {
            string isSaved = "true";

            AccountingEntities db = new AccountingEntities();
            int receiptId         = cashInVmObj.ReceiptId;

            savedReceiptId = receiptId;
            CashOutReceipt cashDbObj;

            if (receiptId == 0)
            {
                cashDbObj = new CashOutReceipt();
            }
            else
            {
                cashDbObj = db.CashOutReceipts.Include("CashOutReceiptChecks")
                            .Include("CashOutReceiptInvs")
                            .Where(x => x.ReceiptId == receiptId).FirstOrDefault();

                //Delete invoice list .. will insert it again
                var invList = cashDbObj.CashOutReceiptInvs.ToList();
                foreach (var item in invList)
                {
                    cashDbObj.CashOutReceiptInvs.Remove(item);
                }

                //Delete check list .. will insert it again
                var checkList = cashDbObj.CashOutReceiptChecks.ToList();
                foreach (var item in checkList)
                {
                    cashDbObj.CashOutReceiptChecks.Remove(item);
                }
            }

            Mapper.CreateMap <CashInVm, CashOutReceipt>()
            .ForMember(x => x.CashOutReceiptInvs, y => y.Ignore())
            .ForMember(x => x.CashOutReceiptChecks, y => y.Ignore())
            .IgnoreAllNonExisting();
            Mapper.Map(cashInVmObj, cashDbObj);

            CashOutReceiptCheck cashCheckDb;

            foreach (var item in cashInVmObj.CashInReceiptChecks)
            {
                if (!string.IsNullOrEmpty(item.CheckNumber))
                {
                    cashCheckDb = new CashOutReceiptCheck();
                    Mapper.CreateMap <CashInCheckVm, CashOutReceiptCheck>().IgnoreAllNonExisting();
                    Mapper.Map(item, cashCheckDb);

                    cashDbObj.CashOutReceiptChecks.Add(cashCheckDb);
                }
            }

            if (cashInVmObj.OperationId != null) //CC Cash Deposit
            {
                CashOutCCCashDeposit cashDeposit = new CashOutCCCashDeposit();
                cashDeposit.OperationId = cashInVmObj.OperationId.Value;
                cashDeposit.ReceiptId   = cashInVmObj.ReceiptId;
                cashDbObj.CashOutCCCashDeposits.Add(cashDeposit);
            }

            else if (cashInVmObj.AgentId == null) //Cash Receipt for invoice
            {
                //Add Receipt invoices
                CashOutReceiptInv cashInvDb;
                foreach (var item in cashInVmObj.CashInReceiptInvs)
                {
                    if (item.IsSelected)
                    {
                        cashInvDb = new CashOutReceiptInv();
                        Mapper.CreateMap <CashInInvoiceVm, CashOutReceiptInv>().IgnoreAllNonExisting();
                        item.CashInReceipt = null;
                        Mapper.Map(item, cashInvDb);

                        cashDbObj.CashOutReceiptInvs.Add(cashInvDb);
                    }
                }
            }
            else //Cash Receipt for Agent Note
            {
                //Add Receipt Agent Notes
                CashOutReceiptAgNote cashAgNoteDb;
                foreach (var item in cashInVmObj.CashInReceiptInvs)
                {
                    if (item.IsSelected)
                    {
                        cashAgNoteDb = new CashOutReceiptAgNote();
                        Mapper.CreateMap <CashInInvoiceVm, CashOutReceiptAgNote>().IgnoreAllNonExisting();
                        item.CashInReceipt = null;
                        Mapper.Map(item, cashAgNoteDb);

                        cashDbObj.CashOutReceiptAgNotes.Add(cashAgNoteDb);
                    }
                }
            }


            using (TransactionScope transaction = new TransactionScope())
            {
                try
                {
                    if (receiptId == 0)
                    {
                        cashDbObj.ReceiptCode = AdminHelper.GeneratePrefixCode(PrefixForEnum.CashOut, true);
                        db.CashOutReceipts.Add(cashDbObj);
                    }

                    db.SaveChanges();

                    cashInVmObj.ReceiptId   = cashDbObj.ReceiptId;
                    cashInVmObj.ReceiptCode = cashDbObj.ReceiptCode;

                    savedReceiptId = cashInVmObj.ReceiptId;

                    #region Add To Transaction Table
                    if (addToTrans)
                    {
                        //Add shipper or consignee to accounting chart
                        string debitAccountId = "";
                        if (cashInVmObj.OperationId != null) //CC Cash Deposit
                        {
                            debitAccountId = AccountingChartHelper.GetCCCashDepAccountId(cashInVmObj.OperationId.Value);
                            if (string.IsNullOrEmpty(debitAccountId))
                            {
                                debitAccountId = AccountingChartHelper.AddCCCashDepositToChart(cashInVmObj.OperationId.Value);
                            }
                        }
                        else if (!string.IsNullOrEmpty(cashInVmObj.PartnerAccountId)) //Partner Drawing
                        {
                            debitAccountId = cashInVmObj.PartnerAccountId;
                        }
                        else if (cashInVmObj.AgentId == null) //Cash Receipt for invoice
                        {
                            if (cashInVmObj.InvoiceType == 1) //carrier
                            {
                                debitAccountId = AccountingChartHelper
                                                 .GetAccountIdByPkAndTbName(cashInVmObj.CarrierId.Value, "Carrier", "CarrierId");
                                if (string.IsNullOrEmpty(debitAccountId))
                                {
                                    debitAccountId = AccountingChartHelper.AddCarrierToChart(cashInVmObj.CarrierId.Value);
                                }
                            }
                            else if (cashInVmObj.InvoiceType == 2) //contractor
                            {
                                debitAccountId = AccountingChartHelper
                                                 .GetAccountIdByPkAndTbName(cashInVmObj.ContractorId.Value, "Contractor", "ContractorId");
                                if (string.IsNullOrEmpty(debitAccountId))
                                {
                                    debitAccountId = AccountingChartHelper.AddContractorToChart(cashInVmObj.ContractorId.Value);
                                }
                            }
                            else if (cashInVmObj.InvoiceType == 3) //custom clearance .. 22/11/2016
                            {
                                debitAccountId = ((int)AccountingChartEnum.CustomClearanceSupplier).ToString();
                            }
                        }
                        else //Cash Receipt for Agent Note
                        {
                            debitAccountId = AccountingChartHelper.GetAccountIdByPkAndTbName(cashInVmObj.AgentId.Value, "Agent", "AgentId");
                            if (string.IsNullOrEmpty(debitAccountId))
                            {
                                debitAccountId = AccountingChartHelper.AddAgentToChart(cashInVmObj.AgentId.Value, 2); //AgentNoteType = 2 .. Credit Agent
                            }
                        }

                        if (receiptId == 0)
                        {
                            //Add invoice to accounting transactions table

                            AddReceiptToTransTable(debitAccountId, cashInVmObj);

                            foreach (var item in cashInVmObj.CashInReceiptInvs)
                            {
                                if (item.IsSelected)
                                {
                                    if (item.InvoiceId != 0) //Cash Receipt for invoice
                                    {
                                        //Change Invoice status
                                        if (item.AmountDue == 0)
                                        {
                                            InvoiceHelper.ChangeInvStatus(item.InvoiceId, InvStatusEnum.Paid, true);
                                        }
                                        else if (item.CollectedAmount != 0 && item.AmountDue != 0)
                                        {
                                            InvoiceHelper.ChangeInvStatus(item.InvoiceId, InvStatusEnum.PartiallyPaid, true);
                                        }
                                    }
                                    else //Cash Receipt for Agent Note
                                    {
                                        //Change Agent Note status
                                        if (item.AmountDue == 0)
                                        {
                                            AgentNoteHelper.ChangeAgNoteStatus(item.AgentNoteId, InvStatusEnum.Paid);
                                        }
                                        else if (item.CollectedAmount != 0 && item.AmountDue != 0)
                                        {
                                            AgentNoteHelper.ChangeAgNoteStatus(item.AgentNoteId, InvStatusEnum.PartiallyPaid);
                                        }
                                    }
                                }
                            }
                        }
                    }

                    #endregion

                    transaction.Complete();
                }
                catch (DbEntityValidationException e)
                {
                    isSaved = "false " + e.Message;

                    //AdminHelper.LastIdRemoveOne(PrefixForEnum.AccountingInvoice);
                }
                catch (Exception e)
                {
                    isSaved = "false " + e.Message;
                    //AdminHelper.LastIdRemoveOne(PrefixForEnum.AccountingInvoice);
                }
            }

            return(isSaved);
        }