Add "GetValue()" method to an Entry and move the value computation

into the Entry from the Invoice.
Add more hooks to post an Invoice to an Account and lookup
an invoice from a transaction


git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@6615 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Derek Atkins 2002-01-13 04:25:55 +00:00
parent b33d7793ad
commit 5564e42695
3 changed files with 76 additions and 36 deletions

View File

@ -341,6 +341,51 @@ GncEntry * gncEntryLookup (GNCBook *book, const GUID *guid)
guid, _GNC_MOD_NAME); guid, _GNC_MOD_NAME);
} }
void gncEntryGetValue (GncEntry *entry, gnc_numeric *value,
gnc_numeric *tax_value)
{
gnc_numeric subtotal;
gnc_numeric disc;
gnc_numeric this_value;
gint type;
if (!entry) return;
/* Compute the value */
type = gncEntryGetDiscountType (entry);
disc = gncEntryGetDiscount (entry);
subtotal = gnc_numeric_mul (gncEntryGetQuantity (entry),
gncEntryGetPrice (entry),
100, /* XXX */
GNC_RND_ROUND);
if (GNC_ENTRY_INTERP_IS_PERCENT (type))
disc = gnc_numeric_mul (subtotal, disc, 100 /* XXX */, GNC_RND_ROUND);
this_value = gnc_numeric_sub_fixed (subtotal, disc);
if (type & GNC_ENTRY_PRETAX_FLAG)
subtotal = this_value;
if (value != NULL)
*value = this_value;
/* Compute the tax value */
if (tax_value != NULL) {
gnc_numeric tax = gncEntryGetTax (entry);
type = gncEntryGetTaxType (entry);
if (GNC_ENTRY_INTERP_IS_PERCENT (type))
tax = gnc_numeric_mul (subtotal, tax, 100 /* XXX */, GNC_RND_ROUND);
*tax_value = tax;
}
return;
}
void gncEntryCommitEdit (GncEntry *entry) void gncEntryCommitEdit (GncEntry *entry)
{ {
if (!entry) return; if (!entry) return;

View File

@ -63,6 +63,12 @@ gnc_numeric gncEntryGetDiscount (GncEntry *entry);
gint gncEntryGetDiscountType (GncEntry *entry); gint gncEntryGetDiscountType (GncEntry *entry);
const char * gncEntryGetDiscountTypeStr (gint type); const char * gncEntryGetDiscountTypeStr (gint type);
/* Compute the Entry value and tax-value numbers, based on the
* quantity, price, discount, tax, and discount/tax types
*/
void gncEntryGetValue (GncEntry *entry, gnc_numeric *value,
gnc_numeric *tax_value);
gint gncEntryGetTypeFromStr (const char *type); gint gncEntryGetTypeFromStr (const char *type);
Account * gncEntryGetAccount (GncEntry *entry); Account * gncEntryGetAccount (GncEntry *entry);

View File

@ -317,6 +317,7 @@ void gncInvoiceAttachInvoiceToTxn (GncInvoice *invoice, Transaction *txn)
value = kvp_value_new_guid (gncInvoiceGetGUID (invoice)); value = kvp_value_new_guid (gncInvoiceGetGUID (invoice));
kvp_frame_set_slot_path (kvp, value, GNC_INVOICE_ID, GNC_INVOICE_GUID, NULL); kvp_frame_set_slot_path (kvp, value, GNC_INVOICE_ID, GNC_INVOICE_GUID, NULL);
kvp_value_delete (value); kvp_value_delete (value);
xaccTransSetTxnType (txn, TXN_TYPE_INVOICE);
xaccTransCommitEdit (txn); xaccTransCommitEdit (txn);
gncInvoiceSetPostedTxn (invoice, txn); gncInvoiceSetPostedTxn (invoice, txn);
@ -346,7 +347,7 @@ Transaction * gncInvoicePostToAccount (GncInvoice *invoice, Account *acc,
GList *iter; GList *iter;
GList *splitinfo = NULL; GList *splitinfo = NULL;
gnc_numeric total; gnc_numeric total;
gnc_commodity *commonCommodity = NULL; gnc_commodity *commonCommodity = NULL; /* XXX: FIXME */
struct acct_val { struct acct_val {
Account * acc; Account * acc;
gnc_numeric val; gnc_numeric val;
@ -354,12 +355,10 @@ Transaction * gncInvoicePostToAccount (GncInvoice *invoice, Account *acc,
if (!invoice || !acc) return NULL; if (!invoice || !acc) return NULL;
/* XXX: Need to obtain the book */
txn = xaccMallocTransaction (invoice->book); txn = xaccMallocTransaction (invoice->book);
xaccTransBeginEdit (txn); xaccTransBeginEdit (txn);
/* Figure out the common currency */ /* XXX: Figure out the common currency */
/* XXX */
/* Set Transaction Description (customer), Num (invoice ID), Currency */ /* Set Transaction Description (customer), Num (invoice ID), Currency */
xaccTransSetDescription xaccTransSetDescription
@ -374,51 +373,41 @@ Transaction * gncInvoicePostToAccount (GncInvoice *invoice, Account *acc,
xaccTransSetDatePostedTS (txn, date); xaccTransSetDatePostedTS (txn, date);
} }
/* Set the txn due date to be equal to the invoice */
{
Timespec ddue = gncInvoiceGetDateDue (invoice);
xaccTransSetDateDueTS (txn, &ddue);
}
/* Iterate through the entries; sum up everything for each account. /* Iterate through the entries; sum up everything for each account.
* then create the appropriate splits in this txn. * then create the appropriate splits in this txn.
*/ */
total = gnc_numeric_zero(); total = gnc_numeric_zero();
for (iter = gncInvoiceGetEntries(invoice); iter; iter = iter->next) { for (iter = gncInvoiceGetEntries(invoice); iter; iter = iter->next) {
gnc_numeric value, tax;
GncEntry * entry = iter->data; GncEntry * entry = iter->data;
Account *this_acc = gncEntryGetAccount (entry); Account *this_acc;
gnc_numeric disc = gncEntryGetDiscount (entry);
gnc_numeric subtotal = gnc_numeric_mul (gncEntryGetQuantity (entry),
gncEntryGetPrice (entry),
100, /* XXX */
GNC_RND_ROUND);
/* Find the account value for this_acc. If we haven't seen this /* Obtain the Entry Value and TaxValue */
* account before, create a new total and add to list gncEntryGetValue (entry, &value, &tax);
*/
GET_OR_ADD_ACCVAL (splitinfo, this_acc, acc_val);
/* Now compute the split value and add it to the totals */ /* add the value for the account split */
{ this_acc = gncEntryGetAccount (entry);
gint disc_type = gncEntryGetDiscountType (entry); if (this_acc) {
gnc_numeric value; /* Find the account value for this_acc. If we haven't seen this
* account before, create a new total and add to list
if (GNC_ENTRY_INTERP_IS_PERCENT (disc_type)) */
disc = gnc_numeric_mul (subtotal, disc, 100 /* XXX */, GNC_RND_ROUND); GET_OR_ADD_ACCVAL (splitinfo, this_acc, acc_val);
value = gnc_numeric_sub_fixed (subtotal, disc);
if (disc_type & GNC_ENTRY_PRETAX_FLAG)
subtotal = value;
acc_val->val = gnc_numeric_add_fixed (acc_val->val, value); acc_val->val = gnc_numeric_add_fixed (acc_val->val, value);
total = gnc_numeric_add_fixed (total, value); total = gnc_numeric_add_fixed (total, value);
} }
/* Repeat for the Entry Tax */ /* Repeat for the TaxValue */
this_acc = gncEntryGetTaxAccount (entry); this_acc = gncEntryGetTaxAccount (entry);
if (this_acc) { if (this_acc) {
gnc_numeric tax = gncEntryGetTax (entry);
gint tax_type = gncEntryGetTaxType (entry);
GET_OR_ADD_ACCVAL (splitinfo, this_acc, acc_val); GET_OR_ADD_ACCVAL (splitinfo, this_acc, acc_val);
if (GNC_ENTRY_INTERP_IS_PERCENT (tax_type))
tax = gnc_numeric_mul (subtotal, tax, 100 /* XXX */, GNC_RND_ROUND);
acc_val->val = gnc_numeric_add_fixed (acc_val->val, tax); acc_val->val = gnc_numeric_add_fixed (acc_val->val, tax);
total = gnc_numeric_add_fixed (total, tax); total = gnc_numeric_add_fixed (total, tax);
} }
@ -450,8 +439,9 @@ Transaction * gncInvoicePostToAccount (GncInvoice *invoice, Account *acc,
xaccTransAppendSplit (txn, split); xaccTransAppendSplit (txn, split);
} }
/* Now attach this invoice to the txn and account */
gncInvoiceAttachInvoiceToTxn (invoice, txn);
gncInvoiceSetPostedAcc (invoice, acc); gncInvoiceSetPostedAcc (invoice, acc);
gncInvoiceSetPostedTxn (invoice, txn);
xaccTransCommitEdit (txn); xaccTransCommitEdit (txn);
@ -463,17 +453,16 @@ GncInvoice * gncInvoiceGetInvoiceFromTxn (Transaction *txn)
kvp_frame *kvp; kvp_frame *kvp;
kvp_value *value; kvp_value *value;
GUID *guid; GUID *guid;
GNCBook *book = NULL; /* XXX: FIXME */ GNCBook *book;
if (!txn) return NULL; if (!txn) return NULL;
book = xaccTransGetBook (txn);
kvp = xaccTransGetSlots (txn); kvp = xaccTransGetSlots (txn);
value = kvp_frame_get_slot_path (kvp, GNC_INVOICE_ID, GNC_INVOICE_GUID, NULL); value = kvp_frame_get_slot_path (kvp, GNC_INVOICE_ID, GNC_INVOICE_GUID, NULL);
if (!value) return NULL; if (!value) return NULL;
guid = kvp_value_get_guid (value); guid = kvp_value_get_guid (value);
/* XXX: Need to get GNCBook from Transaction */
/* XXX: lookup invoice from session/guid */
return xaccLookupEntity (gnc_book_get_entity_table (book), return xaccLookupEntity (gnc_book_get_entity_table (book),
guid, _GNC_MOD_NAME); guid, _GNC_MOD_NAME);