He/שימוש ב API

From GnuCash
Revision as of 07:23, 13 May 2021 by Avma (talk | contribs) (יצירת תנועה חדשה)
Jump to: navigation, search
שפות אנגלית גרמנית ספרדית עִברִית פורטוגזית צרפתית

כללי

עמוד זה מרכז מספר דוגמאות אודות אופן השימוש ב- 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

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

להלן הקוד ליצירת תנועה חדשה (בקיצור "txn") ואכלסה בנתונים תוך שימוש בקוד C "כמעט אמיתי" (במציאות כל הצהרות המשתנים חייבות להופיע בתחילת הקוד בראש התסריט ולא משולב בתוך שורות הקוד):

 Account *account = ...; // לקבל זאת מהיכן שהו
 QofBook *book = gnc_account_get_book(account);
 
 Transaction *transaction = xaccMallocTransaction(book); // יצירת התנועה החדשה
 xaccTransBeginEdit(transaction); // פתיחת התנועה לעריכה

קוד txn נוצר. כעת ניתן להגדיר את שדות המידע הקשורים לקוד ה-txn:

 xaccTransSetDatePostedSecs(transaction, 1234567); // ה-txn תאריך
 xaccTransSetNum(transaction, "X23");
 xaccTransSetNotes(transaction, "הערות כל שהן...");

מכיוון שגנוקאש מסוגלת לטפל ברבוי-מטבעות , כל txn חייב לכלול גם את הגדרת מטבע תנועה:

 Commodity *currency = xaccAccountGetCommodity(account);
 xaccTransSetCurrency(transaction, currency); //   חייב לכלול הגדרות /סחורה/מטבעTxn

הסכום/הערך בפועל של התנועה יתקבל על ידי שנים (או יותר) הפיצולים שלה. הקוד ייצור את הפיצול הראשון:

 Split *split = xaccMallocSplit(book);
 xaccTransAppendSplit(transaction, split); // הפיצול שייך  זהtxn ל-
 xaccAccountInsertSplit(account, split); // הפיצול שייך לחזבון זה
 gnc_numeric amount = gnc_numeric_create(123, 100); // 1.23
 xaccSplitSetValue(split, amount);
 xaccSplitSetAmount(split, amount);

באשר ל- SetValue ו- SetAmount: ב-txn למסחר רב-מטבעי או במסחר במניות, הסכום והערך אינם בהכרך זהים, להפך. כל עוד מתמודדים עם מטבע אחד בלבד, הערך והסכום מוגדרים להיות זהים. Tכעת נדרש לייצור גם את הפיצול השני כך שהתנועה תהיה מאוזנת ובעלת שני פיצולים לפחות (שורת חובה ושורת זכות), בדיוק כפי שתנועות נרשמות בהנהלת חשבונות כפולה:

 Account* other_account = ...; // נדרש לקבל זאת מהיכן שהו
 Split *split2 = xaccMallocSplit(book);
 xaccTransAppendSplit(transaction, split2); // הפיצול שייך ל- txn הזה
 xaccAccountInsertSplit(other_account, split2); // הפיצול שייך לחשבון זה
 gnc_numeric other_amount = gnc_numeric_create(-123, 100); // -1.23, הסכום מהפיצול הקודם אבל בסימן הפוך(חמייצג חובה / זכות)
 xaccSplitSetValue(split, other_amount);
 xaccSplitSetAmount(split, other_amount);

לבסוף, סיום עריכת התנועה על ידי קריאה לפונקציה CommitEdit:

 xaccTransCommitEdit(transaction); // סיום העריכה

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

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