Exemplo n.º 1
0
        internal static Models.Invoice GetInvoice(Guid organizationId, int departmentId, string invoiceId, bool isFullDetails = true)
        {
            int invoice_id = 0;

            if (!int.TryParse(invoiceId, out invoice_id))
            {
                DataRow iRow = bigWebApps.bigWebDesk.Data.Invoice.SelectInvoiceByPseudoId(organizationId, departmentId, invoiceId);
                if (iRow != null)
                {
                    invoice_id = int.Parse(iRow["Id"].ToString());
                }
            }
            if (invoice_id > 0)
            {
                DataRow iRow = bigWebApps.bigWebDesk.Data.Invoice.SelectInvoice(organizationId, departmentId, invoice_id);
                if (iRow != null)
                {
                    var invoice = new Invoice(iRow);
                    invoice.recipients = AccountUsers.GetAccountUsers(organizationId, departmentId, invoice.AccountId);
                    if (isFullDetails)
                    {
                        invoice.AddDetails(organizationId, departmentId, DateTime.UtcNow.AddDays(-365), DateTime.UtcNow.Date.AddDays(1).AddSeconds(-1), true);
                    }
                    return(invoice);
                }
            }
            throw new HttpError(HttpStatusCode.NotFound, "Invoice not found");
        }
Exemplo n.º 2
0
        public static List <AccountUser> GetAccountUsers(Guid organizationId, int departmentId, int accountId)
        {
            AccountUsers accountUsers = new AccountUsers(bigWebApps.bigWebDesk.Data.Accounts.SelectUsers(organizationId, departmentId, accountId, "", false));

            return(accountUsers.List);
        }
Exemplo n.º 3
0
        public static Account_Details GetAccountDetails(ApiUser user, int accountId, bool is_with_statistics)
        {
            Models.Account_Details account = null;
            Instance_Config        config  = new Instance_Config(user);
            DataRow accountDetails         = bigWebApps.bigWebDesk.Data.Accounts.SelectOne(user.DepartmentId, accountId, user.OrganizationId);

            if (accountDetails != null)
            {
                account = new Account_Details(accountDetails);
            }
            else if (accountId == -1 || accountId == 0)
            {
                DataTable table = new DataTable();
                table.Columns.Add("Name", typeof(string));
                table.Columns.Add("Id", typeof(int));

                account      = new Account_Details(table.NewRow());
                account.Name = user.DepartmentName;
                account.Id   = -1;
            }
            else
            {
                throw new HttpError(HttpStatusCode.NotFound, "Incorrect account id");
            }
            if (config.LocationTracking)
            {
                account.Locations = new Locations(bigWebApps.bigWebDesk.Data.Accounts.SelectLocations(user.OrganizationId, user.DepartmentId, accountId)).List;
            }

            /*
             * if (account.ClientContractID.HasValue)
             * {
             *  DataRow row = bigWebApps.bigWebDesk.Data.Accounts.SelectAccountContract(user.OrganizationId, user.DepartmentId, account.ClientContractID.Value);
             *  account.contract_renewal_date = Functions.DisplayDate((DateTime)row["EndDate"], 0, false);
             * }
             */

            account.Primary_Contact = Address.GetAccountAddress(user.OrganizationId, user.DepartmentId, accountId);

            account.Users = AccountUsers.GetAccountUsers(user.OrganizationId, user.DepartmentId, accountId);

            //Assets
            if (config.AssetTracking)
            {
                account.Assets = Models.Assets.AccountAssets(user, config.Assets, accountId);
            }

            if (config.ProjectTracking)
            {
                account.Projects = AccountProjects.GetAccountProjects(user.OrganizationId, user.DepartmentId, accountId);
            }

            //Custom fields
            if (accountId > 0)
            {
                List <KeyValuePair <string, string> > customFields = new List <KeyValuePair <string, string> >();
                for (int i = 1; i < 16; i++)
                {
                    if ((bool)accountDetails["btCust" + i.ToString() + "On"])
                    {
                        customFields.Add(new KeyValuePair <string, string>(accountDetails["vchCust" + i.ToString() + "Cap"].ToString(), accountDetails["vchCust" + i.ToString()].ToString()));
                    }
                }

                if (!accountDetails.IsNull("btDateCust1On") && (bool)accountDetails["btDateCust1On"])
                {
                    customFields.Add(new KeyValuePair <string, string>(accountDetails["vchDateCust1Cap"].ToString(), accountDetails.IsNull("dtCust1") ? "" : Functions.DisplayDate((DateTime)accountDetails["dtCust1"], user.TimeZoneOffset, false)));
                }
                if (!accountDetails.IsNull("btDateCust2On") && (bool)accountDetails["btDateCust2On"])
                {
                    customFields.Add(new KeyValuePair <string, string>(accountDetails["vchDateCust2Cap"].ToString(), accountDetails.IsNull("dtCust2") ? "" : Functions.DisplayDate((DateTime)accountDetails["dtCust2"], user.TimeZoneOffset, false)));
                }

                account.CustomFields = customFields.ToList();
            }

            account.Files = Models.Files.GetFiles(user.InstanceId, accountId, user.OrganizationId, "accounts-accounts-files");

            string accountLogoUrl = "";

            if (accountId > 0)
            {
                accountLogoUrl = Models.Files.GetAccountLogoUrl(user.InstanceId, accountId, user.OrganizationId);
            }
            else
            {
                accountLogoUrl = string.IsNullOrEmpty(user.InstanceLogoImageUrl) ? user.OrganizationLogoImageUrl : user.InstanceLogoImageUrl;
            }

            account.Logo = accountLogoUrl;

            if (is_with_statistics)
            {
                account.account_statistics = AccountStats.GetStatistics(accountId, user.OrganizationId, user.DepartmentId, user.UserId);
            }

            return(account);
        }
Exemplo n.º 4
0
        internal static object SendInvoice(ApiUser hdUser, string invoice_id, string recipients, bool isPDFOnly = false)
        {
            Guid   organizationId = hdUser.OrganizationId;
            int    departmentId   = hdUser.DepartmentId;
            int    userId         = hdUser.UserId;
            string Email          = hdUser.LoginEmail;
            string userName       = hdUser.FullName;
            string department     = hdUser.DepartmentName;

            Models.Invoice invoice    = GetInvoice(organizationId, departmentId, invoice_id, false);
            int            AccountId  = invoice.AccountId;
            List <int>     intUserIds = new List <int>();

            string[] emails = recipients.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).ToArray();
            recipients         = "";
            invoice.recipients = AccountUsers.GetAccountUsers(organizationId, departmentId, invoice.AccountId);
            foreach (string email in emails)
            {
                if (!Utils.IsValidEmail(email))
                {
                    continue;
                }

                bool        isAccountingContact = true;
                int         userID    = 0;
                string      new_email = "";
                AccountUser user      = invoice.recipients.Find(r => r.Email.ToLower() == email.ToLower());
                if (user != null)
                {
                    isAccountingContact = user.AccountingContact;
                    userID    = user.Id;
                    new_email = user.Email;
                }
                else
                {
                    userId              = bigWebApps.bigWebDesk.Data.Accounts.InsertUserIntoAccount(hdUser.OrganizationId, hdUser.DepartmentId, AccountId, email, 0, false);
                    new_email           = email;
                    isAccountingContact = false;
                }
                if (!isAccountingContact)
                {
                    bigWebApps.bigWebDesk.Data.Accounts.UpdateAccountContact(hdUser.OrganizationId, hdUser.DepartmentId, AccountId, userID, true);
                }
                recipients += new_email + ";";
            }

            int    ProjectId = invoice.ProjectId;
            int    invoiceID = invoice.Id.Value;
            string subject   = invoice.Customer + " | Invoice #" + invoiceID;
            string from      = "\"" + userName + " - " + department + "\"<" + Email + ">";

            if (!string.IsNullOrWhiteSpace(recipients))
            {
                Instance_Config instanceConfig = new Models.Instance_Config(hdUser);
                string          currency       = string.IsNullOrWhiteSpace(instanceConfig.Currency) ? "$" : instanceConfig.Currency;
                try
                {
                    string filename     = "Invoice-" + invoiceID.ToString() + ".pdf";
                    string logoURL      = string.Empty;
                    string logoImageUrl = Files.GetOrganizationLargeLogoUrl(organizationId);
                    if (!String.IsNullOrEmpty(logoImageUrl))
                    {
                        logoURL = logoImageUrl;
                    }
                    logoImageUrl = Files.GetInstanceLargeLogoUrl(hdUser.InstanceId);
                    if (!String.IsNullOrEmpty(logoImageUrl))
                    {
                        logoURL = logoImageUrl;
                    }
                    byte[] pdfBytes = null;
                    string body     = "";
                    try
                    {
                        pdfBytes = bigWebApps.bigWebDesk.Data.Invoice.ExportPDF(organizationId, hdUser.InstanceId, departmentId, userId, invoiceID, "https://app.sherpadesk.com", currency,
                                                                                instanceConfig.Names.tech.a, instanceConfig.Names.ticket.a, instanceConfig.ProjectTracking, instanceConfig.Names.user.a, instanceConfig.QBUseQBInvoiceNumber, logoURL);
                        if (isPDFOnly)
                        {
                            var sfile = new System.IO.MemoryStream(pdfBytes);
                            return(new BWA.bigWebDesk.Api.Services.FilesService.FileResult(sfile, "application/pdf", filename));
                        }
                        body = bigWebApps.bigWebDesk.Data.Invoice.ExportHtml(organizationId, hdUser.InstanceId, departmentId, userId, invoiceID, "https://app.sherpadesk.com", currency, instanceConfig.Names.tech.a,
                                                                             instanceConfig.Names.ticket.a, instanceConfig.ProjectTracking, instanceConfig.Names.user.a, instanceConfig.QBUseQBInvoiceNumber);
                    }
                    catch
                    {
                        throw new HttpError(HttpStatusCode.NotFound, "Cannot create invoice with provided data.");
                    }
                    MailNotification _mail_notification = new MailNotification(organizationId, departmentId, userId, from, recipients, subject, body);
                    if (pdfBytes != null)
                    {
                        bigWebApps.bigWebDesk.Data.FileItem[] _files = new bigWebApps.bigWebDesk.Data.FileItem[1];
                        _files[0] = new bigWebApps.bigWebDesk.Data.FileItem(0, filename, pdfBytes.Length, DateTime.Now, string.Empty, pdfBytes);
                        _mail_notification.AttachedFiles = _files;
                    }
                    string _return_string = _mail_notification.Commit(true);
                }
                catch
                {
                    throw new HttpError(HttpStatusCode.NotFound, "Email error.");
                }
            }
            else
            {
                throw new HttpError(HttpStatusCode.NotFound, "No recepients selected.");
            }
            return(invoice);
        }