예제 #1
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);
        }