Ejemplo n.º 1
0
        internal static string AddEditInvoice(InvoiceVm invoiceVm)
        {
            string isSaved = "true";

            AccountingEntities db = new AccountingEntities();
            int  invoiceId        = invoiceVm.InvoiceId;
            byte invoiceType      = invoiceVm.InvoiceType;
            // int hbId = invoiceVm.HouseBillId;

            Invoice invDb;

            if (invoiceId == 0)
            {
                invDb = new Invoice();
            }
            else
            {
                invDb = db.Invoices.Include("InvoiceDetails").Include("InvoiceTotals")
                        .Where(x => x.InvoiceId == invoiceId).FirstOrDefault();
                //Delete Invoice details and totals .. will insert it again
                var invDbTotals  = invDb.InvoiceTotals;
                var invDbDetails = invDb.InvoiceDetails;
                foreach (var item in invDbDetails)
                {
                    invDb.InvoiceDetails.Remove(item);
                }
                foreach (var item in invDbTotals)
                {
                    invDb.InvoiceTotals.Remove(item);
                }
            }


            Mapper.CreateMap <InvoiceVm, Invoice>()
            .ForMember(x => x.InvoiceTotals, y => y.Ignore())
            .ForMember(x => x.InvoiceDetails, y => y.Ignore())
            .IgnoreAllNonExisting();
            Mapper.Map(invoiceVm, invDb);

            InvoiceDetail invDetail;

            Mapper.CreateMap <InvoiceDetailVm, InvoiceDetail>().IgnoreAllNonExisting();
            foreach (var item in invoiceVm.InvoiceDetails)
            {
                if (item.IsSelected == true)
                {
                    invDetail = new InvoiceDetail();
                    Mapper.Map(item, invDetail);
                    invDb.InvoiceDetails.Add(invDetail);
                }
            }

            InvoiceTotal invTotalDb;

            foreach (var item in invoiceVm.InvoiceTotals)
            {
                invTotalDb = new InvoiceTotal()
                {
                    CurrencyId       = item.CurrencyId,
                    TotalAmount      = item.TotalAmount,
                    CurrencySign     = item.CurrencySign,
                    TaxDepositAmount = item.TaxDepositAmount,
                    TotalBeforeTax   = item.TotalBeforeTax,
                    VatTaxAmount     = item.VatTaxAmount
                };

                invDb.InvoiceTotals.Add(invTotalDb);
            }



            using (TransactionScope transaction = new TransactionScope())
            {
                try
                {
                    //Add shipper or consignee to accounting chart
                    string accountId = GetAccountId(invoiceVm.OrderFrom, invoiceVm.ShipperId, invoiceVm.ConsigneeId);

                    if (invoiceId == 0)
                    {
                        invDb.InvoiceCode = AdminHelper.GeneratePrefixCode(PrefixForEnum.AccountingInvoice, true);
                        db.Invoices.Add(invDb);
                    }

                    db.SaveChanges();

                    invoiceVm.InvoiceId   = invDb.InvoiceId;
                    invoiceVm.InvoiceCode = invDb.InvoiceCode;


                    //Change HB status
                    if (invoiceId == 0)
                    {
                        //Add invoice to accounting transactions table
                        AddInvToTransTable(accountId, invoiceVm);

                        HouseBillHelper.ChangeHBStatus(invDb.HouseBillId, (byte)StatusEnum.InvoiceIssued);
                        OperationHelper.ChangeOperationStatus(invDb.OperationId, (byte)StatusEnum.InvoiceIssued);
                    }

                    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);
        }
Ejemplo n.º 2
0
        internal static string AddEditAgentNote(AgentNoteVm agentNoteVm)
        {
            string             isSaved = "true";
            AccountingEntities db      = new AccountingEntities();
            AgentNote          agNoteDb;
            int agNoteId = agentNoteVm.AgentNoteId;

            if (agNoteId == 0)
            {
                agNoteDb = new AgentNote();
            }
            else
            {
                agNoteDb = db.AgentNotes.Where(x => x.AgentNoteId == agNoteId).FirstOrDefault();
            }

            Mapper.CreateMap <AgentNoteVm, AgentNote>()
            .ForMember(x => x.AgentNoteDetails, y => y.Ignore())
            .IgnoreAllNonExisting();

            Mapper.Map(agentNoteVm, agNoteDb);

            AgentNoteDetail agNoteDetail;

            Mapper.CreateMap <AgentNoteDetailVm, AgentNoteDetail>().IgnoreAllNonExisting();

            foreach (var item in agentNoteVm.AgentNoteDetails)
            {
                if (item.IsSelected)
                {
                    agNoteDetail = new AgentNoteDetail();
                    Mapper.Map(item, agNoteDetail);
                    agNoteDb.AgentNoteDetails.Add(agNoteDetail);
                }
            }

            using (TransactionScope transaction = new TransactionScope())
            {
                try
                {
                    string agentAccId = AccountingChartHelper.GetAccountIdByPkAndTbName(agNoteDb.AgentId, "Agent", "AgentId");
                    if (string.IsNullOrEmpty(agentAccId))
                    {
                        agentAccId = AccountingChartHelper.AddAgentToChart(agNoteDb.AgentId, agNoteDb.AgentNoteType);
                    }


                    if (agNoteId == 0)
                    {
                        agNoteDb.AgentNoteCode = AdminHelper.GeneratePrefixCode(PrefixForEnum.AgentNote, true);
                        db.AgentNotes.Add(agNoteDb);
                    }

                    db.SaveChanges();

                    if (agNoteId == 0)
                    {
                        agentNoteVm.AgentId       = agNoteDb.AgentId;
                        agentNoteVm.AgentNoteCode = agNoteDb.AgentNoteCode;
                        //Add invoice to accounting transactions table
                        AddAgentToTransTable(agentAccId, agentNoteVm);


                        OperationHelper.ChangeOperationStatus(agentNoteVm.OperationId, (byte)StatusEnum.InvoiceIssued);
                    }
                    isSaved = "true" + agNoteDb.AgentNoteId;
                    transaction.Complete();
                }
                catch (DbEntityValidationException e)
                {
                    isSaved = "false " + e.Message;
                    AdminHelper.LastIdRemoveOne(PrefixForEnum.AgentNote);
                }
                catch (Exception e)
                {
                    isSaved = "false " + e.Message;
                    AdminHelper.LastIdRemoveOne(PrefixForEnum.AgentNote);
                }
            }


            return(isSaved);
        }
Ejemplo n.º 3
0
        internal static string AddEditInvoice(InvoiceVm invoiceVm)
        {
            string isSaved = "true";

            AccountingEntities db = new AccountingEntities();
            int  invoiceId        = invoiceVm.InvoiceId;
            byte invFor           = invoiceVm.InvoiceType;
            //  int hbId = invoiceVm.HouseBillId;

            InvoiceAP invDb;

            if (invoiceId == 0)
            {
                invDb = new InvoiceAP();
            }
            else
            {
                invDb = db.InvoiceAPs.Include("InvoiceDetailAPs").Include("InvoiceTotalAPs")
                        .Where(x => x.InvoiceId == invoiceId).FirstOrDefault();
                //Delete Invoice details and totals .. will insert it again
                var invDbTotals  = invDb.InvoiceTotalAPs;
                var invDbDetails = invDb.InvoiceDetailAPs;
                foreach (var item in invDbDetails)
                {
                    invDb.InvoiceDetailAPs.Remove(item);
                }
                foreach (var item in invDbTotals)
                {
                    invDb.InvoiceTotalAPs.Remove(item);
                }
            }


            Mapper.CreateMap <InvoiceVm, InvoiceAP>()
            .ForMember(x => x.InvoiceTotalAPs, y => y.Ignore())
            .ForMember(x => x.InvoiceDetailAPs, y => y.Ignore())
            .IgnoreAllNonExisting();
            Mapper.Map(invoiceVm, invDb);

            InvoiceDetailAP invDetail;

            Mapper.CreateMap <InvoiceDetailVm, InvoiceDetailAP>().IgnoreAllNonExisting();
            foreach (var item in invoiceVm.InvoiceDetails)
            {
                if (item.IsSelected == true)
                {
                    invDetail = new InvoiceDetailAP();
                    Mapper.Map(item, invDetail);
                    invDb.InvoiceDetailAPs.Add(invDetail);
                }
            }

            InvoiceTotalAP invTotalDb;

            foreach (var item in invoiceVm.InvoiceTotals)
            {
                invTotalDb = new InvoiceTotalAP()
                {
                    CurrencyId   = item.CurrencyId,
                    TotalAmount  = item.TotalAmount,
                    CurrencySign = item.CurrencySign
                };

                invDb.InvoiceTotalAPs.Add(invTotalDb);
            }



            using (TransactionScope transaction = new TransactionScope())
            {
                try
                {
                    //Add shipper or consignee to accounting chart
                    string creditAccountId = "";
                    if (invFor == 1) //carrier
                    {
                        creditAccountId = AccountingChartHelper
                                          .GetAccountIdByPkAndTbName(invoiceVm.CarrierId.Value, "Carrier", "CarrierId");
                        if (string.IsNullOrEmpty(creditAccountId))
                        {
                            creditAccountId = AccountingChartHelper.AddCarrierToChart(invoiceVm.CarrierId.Value);
                        }
                    }
                    else if (invFor == 2) //Contractor
                    {
                        creditAccountId = AccountingChartHelper
                                          .GetAccountIdByPkAndTbName(invoiceVm.ContractorId.Value, "Contractor", "ContractorId");
                        if (string.IsNullOrEmpty(creditAccountId))
                        {
                            creditAccountId = AccountingChartHelper.AddContractorToChart(invoiceVm.ContractorId.Value);
                        }
                    }
                    else if (invFor == 3) //Custom Clearance
                    {
                        creditAccountId = AccountingChartHelper.GetCCCashDepAccountId(invoiceVm.OperationId);

                        if (!string.IsNullOrEmpty(creditAccountId))
                        {
                            invDb.InvStatusId = 4; //paid with cash deposit
                        }
                        else // In this case there was no cash deposit paid .. I added an account to sotre
                             //any credit amount for custom clearance
                        {
                            creditAccountId = ((int)AccountingChartEnum.CustomClearanceSupplier).ToString();
                        }
                    }

                    if (invoiceId == 0)
                    {
                        invDb.InvoiceCode = AdminHelper.GeneratePrefixCode(PrefixForEnum.APInvoice, true);
                        db.InvoiceAPs.Add(invDb);
                    }

                    db.SaveChanges();

                    invoiceVm.InvoiceId   = invDb.InvoiceId;
                    invoiceVm.InvoiceCode = invDb.InvoiceCode;

                    //Change HB status
                    if (invoiceId == 0)
                    {
                        //Add invoice to accounting transactions table
                        if (!string.IsNullOrEmpty(creditAccountId))
                        {
                            AddAPInvToTransTable(creditAccountId, invoiceVm);
                        }

                        //if (invFor == 3) //Custom Clearance
                        if (invFor == 3) //will link the cash out receipts with same currency to this invoice as it is paid
                        {
                            int     operId     = invoiceVm.OperationId;
                            int     savedInvId = invoiceVm.InvoiceId;
                            int     currId     = invoiceVm.InvCurrencyId;
                            decimal invAmount  = invoiceVm.InvoiceTotals.FirstOrDefault().TotalAmount;
                            //Get cashout receiptIds
                            var cashoutReceiptIds = db.CashOutReceipts.Include("CashOutReceiptInvs")
                                                    .Where(x => x.OperationId == operId && x.CurrencyId == currId)
                                                    .ToList();

                            CashOutReceiptInv cashOutReceiptInv;
                            decimal           prevPaidFromReceipt, receiptAmount, amountToPay = 0;

                            foreach (var item in cashoutReceiptIds)
                            {
                                receiptAmount = item.ReceiptAmount;

                                //Get what paid before by this receipt
                                prevPaidFromReceipt = item.CashOutReceiptInvs.Sum(x => x.PaidAmount);
                                amountToPay         = receiptAmount - prevPaidFromReceipt;
                                if (amountToPay <= 0) //This receipt total amount is used by another invoices
                                {
                                    continue;
                                }

                                if (invAmount <= 0) //This Invoice is total paid
                                {
                                    break;
                                }

                                if (amountToPay > invAmount)
                                {
                                    amountToPay = invAmount;
                                }

                                //Add cash out receipt invoice
                                cashOutReceiptInv            = new CashOutReceiptInv();
                                cashOutReceiptInv.ReceiptId  = item.ReceiptId;
                                cashOutReceiptInv.InvoiceId  = savedInvId;
                                cashOutReceiptInv.PaidAmount = amountToPay;

                                db.CashOutReceiptInvs.Add(cashOutReceiptInv);

                                invAmount = invAmount - amountToPay;
                            }

                            db.SaveChanges();
                        }
                        // HouseBillHelper.ChangeHBStatus(invDb.HouseBillId, (byte)StatusEnum.InvoiceIssued);
                        OperationHelper.ChangeOperationStatus(invDb.OperationId, (byte)StatusEnum.InvoiceIssued);
                    }

                    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);
        }