Example #1
0
        private static JToken GetBody(HttpRequest request)
        {
            return(request.HasFormContentType ? ReadForm() : ReadBody());

            JToken ReadForm()
            {
                var obj = new JObject();

                foreach (var(key, value) in request.Form)
                {
                    var propertyValue = ParseRawValue(value);
                    obj.Add(key, propertyValue);
                }

                var fileProperties = request.Form.Files.ToDictionary(x => x.Name, GetFilePropertyValue);

                obj.AddRange(fileProperties, overwriteDuplicateKeys: false);

                return(obj);
            }

            JToken ReadBody()
            {
                using (var reader = new StreamReader(request.Body))
                {
                    var body = reader.ReadToEnd();
                    return(ParseRawValue(body));
                }
            }
        }
Example #2
0
        /// <summary>
        /// Ensure a record matching an enum value exists
        /// </summary>
        /// <param name="args">Additional field values: name, value, name, value, ...</param>
        void ensureRecordExists(Table table, object enumValue, params object [] args)
        {
            JObject d = new JObject().AddRange(table.PrimaryKey.Name, (int)enumValue,
                                               table.Indexes[1].Fields[0].Name, enumValue.UnCamel());

            d.AddRange(args);
            updateIfChanged(table, d);
        }
Example #3
0
        /// <summary>
        /// Get a Buy or Sell document for editing
        /// </summary>
        public void Document(int id, DocType type)
        {
            Title = Title.Replace("Document", type.UnCamel());
            InvestmentDocument header = getDocument <InvestmentDocument>(id);

            if (header.idDocument == null)
            {
                header.DocumentTypeId = (int)type;
                header.DocType        = type.UnCamel();
                header.DocumentDate   = Utils.Today;
                header.DocumentName   = "";
                if (GetParameters["acct"].IsInteger())
                {
                    FullAccount acct = Database.QueryOne <FullAccount>("*", "WHERE idAccount = " + GetParameters["acct"], "Account");
                    if (acct.idAccount != null)
                    {
                        header.DocumentAccountId   = (int)acct.idAccount;
                        header.DocumentAccountName = acct.AccountName;
                        header.FeeAccount          = Database.QueryOne("SELECT idAccount FROM Account WHERE AccountName = " + Database.Quote(acct.AccountName + " fees")).AsInt("idAccount");
                    }
                }
            }
            else
            {
                checkDocType(header.DocumentTypeId, DocType.Buy, DocType.Sell);
                List <JObject> journals = Database.Query(@"SELECT *
FROM Journal
LEFT JOIN StockTransaction ON idStockTransaction = idJournal 
LEFT JOIN Security ON idSecurity = SecurityId
WHERE JournalNum > 1 
AND DocumentId = " + id).ToList();
                header.SecurityId   = journals[0].AsInt("SecurityId");
                header.SecurityName = journals[0].AsString("SecurityName");
                header.Quantity     = journals[0].AsDouble("Quantity");
                header.Price        = journals[0].AsDouble("Price");
                if (journals.Count > 1)
                {
                    header.FeeAccount = journals[1].AsInt("AccountId");
                    header.Fee        = journals[1].AsDecimal("Amount");
                    header.FeeMemo    = journals[1].AsString("Memo");
                }
                if (type == DocType.Sell)
                {
                    header.Quantity = -header.Quantity;
                }
            }
            JObject record = new JObject().AddRange("header", header);

            nextPreviousDocument(record, "JOIN Journal ON DocumentId = idDocument WHERE DocumentTypeId "
                                 + Database.In(DocType.Buy, DocType.Sell)
                                 + (header.DocumentAccountId > 0 ? " AND AccountId = " + header.DocumentAccountId : ""));
            record.AddRange("Accounts", SelectExpenseAccounts(),
                            "Names", SelectOthers(),
                            "Securities", SelectSecurities());
            Record = record;
        }
Example #4
0
        /// <summary>
        /// Get a document for editing
        /// </summary>
        public void Document(int id, DocType type)
        {
            Title = Title.Replace("Document", type.UnCamel());
            JObject record = GetDocument(id, type);
            dynamic header = ((dynamic)record).header;

            nextPreviousDocument(record, "JOIN Journal ON DocumentId = idDocument WHERE DocumentTypeId = " + (int)type
                                 + (header.DocumentAccountId > 0 ? " AND AccountId = " + header.DocumentAccountId : ""));
            record.AddRange("Accounts", SelectAccounts(),
                            "VatCodes", SelectVatCodes(),
                            "Names", SelectOthers());
            Record = record;
        }
Example #5
0
        void addMenuOptionstoRecord(JObject record, Account account)
        {
            List <JObject> plus  = new List <JObject>();
            List <JObject> minus = new List <JObject>();

            if (account.AccountTypeId == (int)AcctType.CreditCard)
            {
                plus.Add(new JObject().AddRange("text", "Card Credit",
                                                "href", "document?id=0&type=" + (int)DocType.CreditCardCredit + "&acct=" + account.idAccount));
                minus.Add(new JObject().AddRange("text", "Card Charge",
                                                 "href", "document?id=0&type=" + (int)DocType.CreditCardCharge + "&acct=" + account.idAccount));
            }
            else
            {
                plus.Add(new JObject().AddRange("text", "Deposit",
                                                "href", "document?id=0&type=" + (int)DocType.Deposit + "&acct=" + account.idAccount));
                minus.Add(new JObject().AddRange("text", "Withdrawal",
                                                 "href", "document?id=0&type=" + (int)DocType.Withdrawal + "&acct=" + account.idAccount));
            }
            JObject c = Database.Query("SELECT COUNT(*) AS numAccounts FROM Account WHERE AccountTypeId " + Database.In((int)AcctType.Bank, (int)AcctType.CreditCard,
                                                                                                                        AcctType.OtherAsset, AcctType.OtherLiability)).FirstOrDefault();

            if (c != null && c.AsInt("numAccounts") > 1)
            {
                plus.Add(new JObject().AddRange("text", "Transfer",
                                                "href", "transfer?id=0&acct2=" + account.idAccount));
                minus.Add(new JObject().AddRange("text", "Transfer",
                                                 "href", "transfer?id=0&acct=" + account.idAccount));
            }
            if (Settings.Customers)
            {
                plus.Add(new JObject().AddRange("text", "Customer Payment",
                                                "href", "/customer/payment?id=0&acct=" + account.idAccount));
            }
            if (Settings.Suppliers)
            {
                minus.Add(new JObject().AddRange("text", "Bill Payment",
                                                 "href", "/supplier/payment?id=0&acct=" + account.idAccount));
            }
            if (Settings.Members)
            {
                plus.Add(new JObject().AddRange("text", "Subscriptions",
                                                "href", "/members/document?id=0&acct=" + account.idAccount));
            }
            record.AddRange("plus", plus, "minus", minus);
        }
Example #6
0
        /// <summary>
        /// Get the last document of the given type with NameAddressId == id
        /// </summary>
        public object DocumentLast(int id, DocType type)
        {
            JObject           result = new JObject();
            Extended_Document header = Database.QueryOne <Extended_Document>("SELECT * FROM Extended_Document WHERE DocumentTypeId = " + (int)type
                                                                             + " AND DocumentNameAddressId = " + id
                                                                             + " ORDER BY DocumentDate DESC, idDocument DESC");

            if (header.idDocument != null)
            {
                if (Utils.ExtractNumber(header.DocumentIdentifier) > 0)
                {
                    header.DocumentIdentifier = "";
                }
                result.AddRange("header", header,
                                "detail", Database.Query("idJournal, DocumentId, Line.VatCodeId, VatRate, JournalNum, Journal.AccountId, Memo, LineAmount, VatAmount, LineAmount + VatAmount AS Gross",
                                                         "WHERE Journal.DocumentId = " + header.idDocument + " AND idLine IS NOT NULL ORDER BY JournalNum",
                                                         "Document", "Journal", "Line"));
            }
            return(result);
        }
Example #7
0
        /// <summary>
        /// Retrieve a General Ledger Journal by id, for editing
        /// </summary>
        public void Document(int id)
        {
            Extended_Document header = getDocument <Extended_Document>(id);

            if (header.idDocument == null)
            {
                header.DocumentTypeId     = (int)DocType.GeneralJournal;
                header.DocType            = DocType.GeneralJournal.UnCamel();
                header.DocumentDate       = Utils.Today;
                header.DocumentName       = "";
                header.DocumentIdentifier = Settings.NextJournalNumber.ToString();
                if (GetParameters["acct"].IsInteger())
                {
                    FullAccount acct = Database.QueryOne <FullAccount>("*", "WHERE idAccount = " + GetParameters["acct"], "Account");
                    if (acct.idAccount != null)
                    {
                        header.DocumentAccountId   = (int)acct.idAccount;
                        header.DocumentAccountName = acct.AccountName;
                    }
                }
            }
            else
            {
                checkDocType(header.DocumentTypeId, DocType.GeneralJournal);
            }
            JObject record = new JObject().AddRange("header", header,
                                                    "detail", Database.Query("idJournal, DocumentId, JournalNum, AccountId, Memo, Amount, NameAddressId, Name",
                                                                             "WHERE Journal.DocumentId = " + id + " ORDER BY JournalNum",
                                                                             "Document", "Journal"));

            nextPreviousDocument(record, "JOIN Journal ON DocumentId = idDocument WHERE DocumentTypeId = " + (int)DocType.GeneralJournal
                                 + (header.DocumentAccountId > 0 ? " AND AccountId = " + header.DocumentAccountId : ""));
            record.AddRange("Accounts", SelectAllAccounts(),
                            "VatCodes", SelectVatCodes(),
                            "Names", SelectNames());
            Record = record;
        }
Example #8
0
        /// <summary>
        /// Retrieve document, or prepare new one
        /// </summary>
        public JObject document(int id, DocType type)
        {
            Title = Title.Replace("Document", type.UnCamel());
            Extended_Document header = getDocument <Extended_Document>(id);

            if (header.idDocument == null)
            {
                header.DocumentTypeId     = (int)type;
                header.DocType            = type.UnCamel();
                header.DocumentDate       = Utils.Today;
                header.DocumentName       = "";
                header.DocumentIdentifier = Settings.NextNumber(type).ToString();
                if (GetParameters["name"].IsInteger())
                {
                    JObject name = Database.QueryOne("*", "WHERE Type = " + Database.Quote(NameType) + " AND idNameAddress = " + GetParameters["name"], "NameAddress");
                    if (name != null)
                    {
                        checkNameType(name.AsString("Type"), NameType);
                        header.DocumentNameAddressId = name.AsInt("idNameAddress");
                        header.DocumentAddress       = name.AsString("Address");
                        header.DocumentName          = name.AsString("Name");
                    }
                }
            }
            else
            {
                checkDocType(header.DocumentTypeId, type);
                checkNameType(header.DocumentNameAddressId, NameType);
            }
            JObject record = new JObject().AddRange("header", header);

            nextPreviousDocument(record, "WHERE DocumentTypeId = " + (int)type);
            record.AddRange("VatCodes", SelectVatCodes(),
                            "Names", SelectNames(NameType));
            return(record);
        }
        public static bool TryFormatJsonFeed(JsonFeed feedToFormat, out JObject feedObject, ExtensionManifestDirectory extensionManifestDirectory = null)
        {
            feedObject = default;

            if (feedToFormat == null)
            {
                return(false);
            }

            feedObject = new JObject
            {
                new JProperty("version", new JValue(JsonFeedConstants.Version)),
            };

            if (extensionManifestDirectory == null)
            {
                extensionManifestDirectory = ExtensionManifestDirectory.DefaultForJsonFeed;
            }

            if (TryFormatJsonFeedRequiredStringProperty("title", feedToFormat.Title, out var titleProperty))
            {
                feedObject.Add(titleProperty);
            }

            if (TryFormatJsonFeedOptionalStringProperty("home_page_url", feedToFormat.HomePageUrl, out var homePageUrlProperty))
            {
                feedObject.Add(homePageUrlProperty);
            }

            if (TryFormatJsonFeedOptionalStringProperty("feed_url", feedToFormat.FeedUrl, out var feedUrlProperty))
            {
                feedObject.Add(feedUrlProperty);
            }

            if (TryFormatJsonFeedOptionalStringProperty("description", feedToFormat.Description, out var descriptionProperty))
            {
                feedObject.Add(descriptionProperty);
            }

            if (TryFormatJsonFeedOptionalStringProperty("user_comment", feedToFormat.UserComment, out var userCommentProperty))
            {
                feedObject.Add(userCommentProperty);
            }

            if (TryFormatJsonFeedOptionalStringProperty("next_url", feedToFormat.NextUrl, out var nextUrlProperty))
            {
                feedObject.Add(nextUrlProperty);
            }

            if (TryFormatJsonFeedOptionalStringProperty("icon", feedToFormat.Icon, out var iconProperty))
            {
                feedObject.Add(iconProperty);
            }

            if (TryFormatJsonFeedOptionalStringProperty("favicon", feedToFormat.Favicon, out var faviconProperty))
            {
                feedObject.Add(faviconProperty);
            }

            if (TryFormatJsonFeedAuthorProperty("author", feedToFormat.Author, out var authorProperty))
            {
                feedObject.Add(authorProperty);
            }

            if (TryFormatJsonFeedOptionalBoolProperty("expired", feedToFormat.Expired, out var expiredProperty))
            {
                feedObject.Add(expiredProperty);
            }

            if (TryFormatJsonFeedHubsProperty("hubs", feedToFormat.Hubs, out var hubsProperty))
            {
                feedObject.Add(hubsProperty);
            }

            if (TryFormatJsonFeedItemsProperty("items", feedToFormat.Items, extensionManifestDirectory, out var itemsProperty))
            {
                feedObject.Add(itemsProperty);
            }

            // extensions
            if (ExtensibleEntityFormatter.TryFormatJObjectExtensions(feedToFormat, extensionManifestDirectory, out var extensionTokens))
            {
                feedObject.AddRange(extensionTokens);
            }

            return(true);
        }
        private static bool TryFormatJsonFeedItemObject(JsonFeedItem itemToFormat, ExtensionManifestDirectory extensionManifestDirectory, out JObject itemObject)
        {
            itemObject = default;

            if (itemToFormat == null)
            {
                return(false);
            }

            itemObject = new JObject();

            if (TryFormatJsonFeedRequiredStringProperty("id", itemToFormat.Id, out var idProperty))
            {
                itemObject.Add(idProperty);
            }

            if (TryFormatJsonFeedOptionalStringProperty("url", itemToFormat.Url, out var urlProperty))
            {
                itemObject.Add(urlProperty);
            }

            if (TryFormatJsonFeedOptionalStringProperty("external_url", itemToFormat.ExternalUrl, out var externalUrlProperty))
            {
                itemObject.Add(externalUrlProperty);
            }

            if (TryFormatJsonFeedOptionalStringProperty("title", itemToFormat.Title, out var titleProperty))
            {
                itemObject.Add(titleProperty);
            }

            if (TryFormatJsonFeedOptionalStringProperty("content_html", itemToFormat.ContentHtml, out var contentHtmlProperty))
            {
                itemObject.Add(contentHtmlProperty);
            }

            if (TryFormatJsonFeedOptionalStringProperty("content_text", itemToFormat.ContentText, out var contentTextProperty))
            {
                itemObject.Add(contentTextProperty);
            }

            if (TryFormatJsonFeedOptionalStringProperty("summary", itemToFormat.Summary, out var summaryProperty))
            {
                itemObject.Add(summaryProperty);
            }

            if (TryFormatJsonFeedOptionalStringProperty("image", itemToFormat.Image, out var imageProperty))
            {
                itemObject.Add(imageProperty);
            }

            if (TryFormatJsonFeedOptionalStringProperty("banner_image", itemToFormat.BannerImage, out var bannerImageProperty))
            {
                itemObject.Add(bannerImageProperty);
            }

            if (TryFormatJsonFeedOptionalTimestampProperty("date_published", itemToFormat.DatePublished, out var datePublishedProperty))
            {
                itemObject.Add(datePublishedProperty);
            }

            if (TryFormatJsonFeedOptionalTimestampProperty("date_modified", itemToFormat.DateModified, out var dateModifiedProperty))
            {
                itemObject.Add(dateModifiedProperty);
            }

            if (TryFormatJsonFeedAuthorProperty("author", itemToFormat.Author, out var authorProperty))
            {
                itemObject.Add(authorProperty);
            }

            if (TryFormatJsonFeedTagsProperty("tags", itemToFormat.Tags, out var tagsProperty))
            {
                itemObject.Add(tagsProperty);
            }

            if (TryFormatJsonFeedAttachmentsProperty("attachments", itemToFormat.Attachments, out var attachmentsProperty))
            {
                itemObject.Add(attachmentsProperty);
            }

            // extensions
            if (ExtensibleEntityFormatter.TryFormatJObjectExtensions(itemToFormat, extensionManifestDirectory, out var extensionTokens))
            {
                itemObject.AddRange(extensionTokens);
            }

            return(true);
        }