Difference between revisions of "He/שימוש ב API"

From GnuCash
Jump to: navigation, search
(כללי: Translation)
(תיעוד: Translation)
Line 27: Line 27:
 
* ספר (typedef name: QofBook)
 
* ספר (typedef name: QofBook)
  
The only place that is for sure up-to-date are the header files:
+
המקום היחיד בו ניתן למצוא מידע עדכני לבטח הם בכותרות הקבצים:
 
* libgnucash/engine/Split.h
 
* libgnucash/engine/Split.h
 
* libgnucash/engine/Transaction.h
 
* libgnucash/engine/Transaction.h

Revision as of 16:47, 11 May 2021

שפות אנגלית גרמנית ספרדית עִברִית פורטוגזית צרפתית

כללי

עמוד זה מרכז מספר דוגמאות אודות אופן השימוש ב- C API של גנוקאש לצורך מניפולציה בסיסית בסוגי נתונים. C API (מכונה גם "המנוע") הוא שכבה שהפשטה מכל שרת. כלומר, מתכנת של מנשק המשתמש או מתכנת המתקעים לעולם לא יוכלו לגשת לאף אחד מתוכניות ה-XML או ה-SQL ישירות, במקום זאת הוא משתמש רק בפונקציות של מנשק ה- API כדי לגשת לנתונים.

תיעוד

As for documentation: Unfortunately there doesn't exist any up-to-date coherent set of documentation. Part of it is in דוקסיג'ן, but part of it are only in the C header files.

A module overview is available for those parts that are documented in doxygen. For example, there is a module group entitled "GnuCash Engine: Core, Non-GUI Accounting Functions" which contains the doxygen-ized parts of this, including the "Account", but not including Transaction and Split which are the other important basic data types.

Entity-Relationship Diagram is an up-to-date (May 2011, but the schema is unchanged in mid-2017) Entity-Relationship-Diagram (ERD) which shows those most important types of gnucash (in the form of tables, as common for an ERD):

  • פיצול
  • תנועה
  • חשבון
  • סחורה (typedef name: gnc_commodity)
  • ספר (typedef name: QofBook)

המקום היחיד בו ניתן למצוא מידע עדכני לבטח הם בכותרות הקבצים:

  • libgnucash/engine/Split.h
  • libgnucash/engine/Transaction.h
  • libgnucash/engine/Account.h
  • libgnucash/engine/gnc-commodity.h
  • libgnucash/engine/qofbook.h

יצירת תנועה חדשה

Here's the code to create a new transaction (abbreviated by "txn") and populate it with data, using "almost correct" C code (in reality, all the variable declarations must appear on top and not within the code):

 Account *account = ...; // get this from somewhere
 QofBook *book = gnc_account_get_book(account);
 
 Transaction *transaction = xaccMallocTransaction(book); // Creating the new transaction
 xaccTransBeginEdit(transaction); // Opening it for editing

The txn is created. We can set the txn-related data fields:

 xaccTransSetDatePostedSecs(transaction, 1234567); // Date of this txn
 xaccTransSetNum(transaction, "X23");
 xaccTransSetNotes(transaction, "Some notes, bla bla");

As gnucash is capable of multi-currency handling, every txn must have a "transaction currency" set:

 Commodity *currency = xaccAccountGetCommodity(account);
 xaccTransSetCurrency(transaction, currency); // Txn must have a commodity/currency set

The actual amount/value of a transaction is given by its two or more splits. We create the first split:

 Split *split = xaccMallocSplit(book);
 xaccTransAppendSplit(transaction, split); // Split belongs to this txn
 xaccAccountInsertSplit(account, split); // Split belongs to this account
 gnc_numeric amount = gnc_numeric_create(123, 100); // 1.23
 xaccSplitSetValue(split, amount);
 xaccSplitSetAmount(split, amount);

As for the SetValue and SetAmount: In multi-currency or stock trading txn, the amount is different from the value and vice versa. As long as you're dealing with exactly one currency, both value and amount are set to be the same value.

Then we create the second split so that we have a normal two-split transaction, as usual in the double-entry bookkeeping:

 Account* other_account = ...; // need to get this from somewhere
 Split *split2 = xaccMallocSplit(book);
 xaccTransAppendSplit(transaction, split2); // Split belongs to this txn
 xaccAccountInsertSplit(other_account, split2); // Split belongs to this account
 gnc_numeric other_amount = gnc_numeric_create(-123, 100); // -1.23, the negative value for above
 xaccSplitSetValue(split, other_amount);
 xaccSplitSetAmount(split, other_amount);

In the end, we must finish the transaction editing by calling the CommitEdit function:

 xaccTransCommitEdit(transaction); // We are finished editing

דוגמאות שימוש

ניתן למצא מספר דוגמאות ב- Python_shell#Usage_examples.