예제 #1
0
        //invoices
        public static List <ProjectInvoiceModel> GetInvoiceReport(this UnitOfWork unit, int year, int month)
        {
            var listInvoices = unit.Projects.Get().SelectMany(x => x.Assignments)
                               .Where(x => x.Day.Date.Value.Month == month && x.Day.Date.Value.Year == year)
                               .GroupBy(y => y.Project)
                               .Select(w => new ProjectInvoiceModel()
            {
                ProjectName   = w.Key.Name,
                CustomerName  = w.Key.Customer.Name,
                CustomerEmail = w.Key.Customer.Email,
                InvoiceDate   = DateTime.Now.Day.ToString() + "/" + DateTime.Now.Month.ToString() + "/" + DateTime.Now.Year.ToString(),
                Amount        = w.Key.Amount,
                Roles         = w.Key.Team.Engagements
                                .Where(t => t.Team.Id == w.Key.TeamId)
                                .GroupBy(r => r.Role)
                                .Select(x => new RoleInvoiceModel()
                {
                    Description = x.Key.Name,
                    Quantity    = x.Key.Engagements
                                  .Where(t => t.Team.Id == w.Key.TeamId)
                                  .Select(em => em.Employee)
                                  .SelectMany(d => d.Days)
                                  .Where(g => g.Date.Value.Month == month && g.Date.Value.Year == year && g.Category.Description == "Working day")
                                  .SelectMany(a => a.Assignments)
                                  .Where(p => p.Project.Id == w.Key.Id)
                                  .Select(h => h.Hours)
                                  .DefaultIfEmpty(0)
                                  .Sum(),
                    Unit      = "Hours",
                    UnitPrice = x.Key.HourlyRate,
                    SubTotal  = x.Key.HourlyRate * x.Key.Engagements
                                .Where(t => t.Team.Id == w.Key.TeamId)
                                .Select(em => em.Employee)
                                .SelectMany(d => d.Days)
                                .Where(g => g.Date.Value.Month == month && g.Date.Value.Year == year && g.Category.Description == "Working day")
                                .SelectMany(a => a.Assignments)
                                .Where(p => p.Project.Id == w.Key.Id)
                                .Select(h => h.Hours)
                                .DefaultIfEmpty(0)
                                .Sum(),
                    Status = "Included"
                })
                                .ToList(),
                InvoiceNumber = w.Key.Name.ToString().Substring(0, 2).ToUpper() + "-" + w.Key.Customer.Name.ToString().Substring(0, 2).ToUpper() + "-" + w.Key.TeamId.ToString() + "-" + DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString().Substring(2, 4) + "-" + month + "/" + year.ToString().Substring(2, 4),
                Status        = "Not sent"
            })
                               .ToList();


            var conString = "mongodb://localhost:27017";

            var client = new MongoClient(conString);

            var DB         = client.GetDatabase("Billings");
            var collection = DB.GetCollection <EmailCustomerModel>("Invoices");

            foreach (var invoice in listInvoices)
            {
                int exist = (int)collection.Find(x => x.InvoiceNumber == invoice.InvoiceNumber).Count();
                if (exist == 0)
                {
                    string MailToSend = "Dear " + invoice.CustomerName + " we are sending You invoice for "
                                        + invoice.ProjectName + " for " + invoice.InvoiceDate + ". InvoiceNumber: " + invoice.InvoiceNumber;
                    EmailCustomerModel invoiceToAdd = new EmailCustomerModel()
                    {
                        Name             = invoice.CustomerName,
                        Email            = invoice.CustomerEmail,
                        Roles            = invoice.Roles,
                        InvoiceDate      = invoice.InvoiceDate,
                        MailToSend       = MailToSend,
                        ProjectName      = invoice.ProjectName,
                        InvoiceNumber    = invoice.InvoiceNumber,
                        Status           = invoice.Status,
                        SentOrCancelDate = "None",
                        Amount           = invoice.Amount
                    };
                    collection.InsertOne(invoiceToAdd);
                }
            }

            //GetFromMongo();
            return(null);
            //return listInvoices;
        }
예제 #2
0
        public IHttpActionResult PostInvoice([FromBody] EmailCustomerModel invoice)
        {
            var conString = "mongodb://localhost:27017";

            var client = new MongoClient(conString);

            var DB         = client.GetDatabase("Billings");
            var collection = DB.GetCollection <EmailCustomerModel>("Invoices");

            if (invoice.Status == "Canceled")
            {
                collection.DeleteOne(Builders <EmailCustomerModel> .Filter.Eq("InvoiceNumber", invoice.InvoiceNumber));
                return(Ok("Invoice canceled"));
            }

            StringBuilder           BodyContent       = new StringBuilder();
            decimal?                TotalBill         = 0.00m;
            int                     billStatusCounter = 0;
            List <RoleInvoiceModel> Roless            = new List <RoleInvoiceModel>();

            foreach (var role in invoice.Roles)
            {
                if (role.Status != "Not included" && role.Status != "Canceled")
                {
                    BodyContent.Append(role.Description.TrimEnd() + "(" + role.Quantity + " hours), SubTotal: " + role.SubTotal + "$" + "\n\r");
                    TotalBill += role.SubTotal;
                    billStatusCounter++;

                    Roless.Add(role);
                }
                else
                {
                    continue;
                }
            }

            string MailToSend = "Dear " + invoice.Name + " we are sending You invoice for "
                                + invoice.ProjectName + " for " + invoice.InvoiceDate + ". InvoiceNumber: " + invoice.InvoiceNumber + "\n\r" + BodyContent.ToString() + "\n\r" + "Total: " + TotalBill + " $";

            if (billStatusCounter == 0)
            {
                invoice.Status = "Nothing Sent";
            }
            else if (billStatusCounter == invoice.Roles.Count())
            {
                invoice.Status = "Full bill sent";
            }
            else if (billStatusCounter > 0 && billStatusCounter < invoice.Roles.Count())
            {
                invoice.Status = "Partially sent";
            }

            EmailCustomerModel invoiceToAdd = new EmailCustomerModel()
            {
                Name             = invoice.Name,
                Email            = invoice.Email,
                Roles            = invoice.Roles,
                InvoiceDate      = invoice.InvoiceDate,
                MailToSend       = MailToSend,
                ProjectName      = invoice.ProjectName,
                InvoiceNumber    = invoice.InvoiceNumber,
                SentOrCancelDate = DateTime.Now.ToString(),
                Status           = invoice.Status,
                Amount           = invoice.Amount
            };



            collection.DeleteOne(Builders <EmailCustomerModel> .Filter.Eq("InvoiceNumber", invoice.InvoiceNumber));
            collection.InsertOne(invoiceToAdd);


            //pocetak send buffer-a
            var invoiceSendBuffer = DB.GetCollection <EmailCustomerModel>("InvoiceSendBuffer");

            invoiceToAdd.Roles = Roless;
            invoiceSendBuffer.InsertOne(invoiceToAdd);

            //StringBuilder invSendAppender = new StringBuilder();

            //foreach (var role in invoice.Roles)
            //{
            //    if (role.Status == "Sent")
            //    {
            //        string[] split = role.Description.TrimEnd().Split(' ');
            //        foreach (string s in split)
            //        {
            //            invSendAppender.Append(s.Substring(0, 1).ToUpper());
            //        }

            //        invoiceToAdd.InvoiceNumber = invoiceToAdd.InvoiceNumber + "-" + invSendAppender;

            //        //backupcollection.InsertOne(invoiceToAdd);
            //    }
            //    else { continue; }
            //}

            //invoiceToAdd.SentOrCancelDate = DateTime.Now.Day.ToString() + "/" + DateTime.Now.Month.ToString() + "/" + DateTime.Now.Year.ToString() + " at " + DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Second.ToString();

            //invoiceSendBuffer.InsertOne(invoiceToAdd);
            //kraj send buffer-a


            //var DBackup = client.GetDatabase("BillingsBackup");
            var backupcollection = DB.GetCollection <EmailCustomerModel>("InvoicesBackup");

            //int exist = (int)backupcollection.Find(x => x.InvoiceNumber == invoice.InvoiceNumber).Count();

            StringBuilder invNumAppender = new StringBuilder();

            //if (exist == 0)
            //{

            foreach (var role in invoice.Roles)
            {
                if (role.Status == "Sent" || role.Status == "Canceled")
                {
                    string[] split = role.Description.TrimEnd().Split(' ');
                    foreach (string s in split)
                    {
                        invNumAppender.Append(s.Substring(0, 1).ToUpper());
                    }

                    invoiceToAdd.InvoiceNumber = invoiceToAdd.InvoiceNumber + "-" + invNumAppender;

                    //backupcollection.InsertOne(invoiceToAdd);
                }
                else
                {
                    continue;
                }
            }

            invoiceToAdd.SentOrCancelDate = DateTime.Now.Day.ToString() + "/" + DateTime.Now.Month.ToString() + "/" + DateTime.Now.Year.ToString() + " at " + DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Second.ToString();

            backupcollection.InsertOne(invoiceToAdd);
            //}

            return(Ok(invoice));
        }