;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; vat-return.scm: UK VAT Return report ;; by Clint Redwood ;; 2010.04.25 - 2010.04.30 ;; ;; loosely based on income-statement.scm - income statement (a.k.a. Profit & Loss) ;; by David Montenegro ;; 2004.07.13 - 2004.07.14 ;; ;; * BUGS: ;; lots probably - it's not been tested with a full size set of accounts yet. ;; ;; ;; This program is free software; you can redistribute it and/or ;; modify it under the terms of the GNU General Public License as ;; published by the Free Software Foundation; either version 2 of ;; the License, or (at your option) any later version. ;; ;; This program is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; ;; You should have received a copy of the GNU General Public License ;; along with this program; if not, contact: ;; ;; Free Software Foundation Voice: +1-617-542-5942 ;; 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 ;; Boston, MA 02110-1301, USA gnu@gnu.org ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define-module (gnucash report vat-return)) (use-modules (gnucash main)) ;; FIXME: delete after we finish modularizing. (use-modules (ice-9 slib)) (use-modules (gnucash gnc-module)) (use-modules (srfi srfi-1)) (use-modules (gnucash report business-reports)) (gnc:module-load "gnucash/report/report-system" 0) (gnc:module-load "gnucash/gnome-utils" 0) ; for the html routines (gnc:module-load "gnucash/business-utils" 0) ;(debug-enable 'debug) ;(debug-enable 'backtrace) (define reportname (N_ "VAT Return")) ;; define all option's names and help text so that they are properly ;; defined in *one* place. (define optname-report-title (N_ "Report Title")) (define opthelp-report-title (N_ "Title for this report")) (define optname-party-name (N_ "Company name")) (define opthelp-party-name (N_ "Name of company/individual")) (define optname-start-date (N_ "Start Date")) (define optname-end-date (N_ "End Date")) ;; used for default box accounts (define (filter-accountlist-fullname namelist accounts) (filter (lambda (a) (member (xaccAccountGetFullName a) namelist)) accounts ) ) ;; list of VAT Return boxes ;; each box is a list with entries ;; ( name description closing-string closing-case-sensitive closing-regexp ;; (acc-full-name-1 acc-full-name2 ...) include-subaccounts invert-value) (define box-list (list (list (N_ "Box 1") (N_ "VAT due on sales and outputs") #f #f #f (list "VAT:Output:Sales" ) #f #t) (list (N_ "Box 2") (N_ "VAT due on aquisitions from EU") #f #f #f (list "VAT:Output:EC" ) #f #t) (list (N_ "Box 3") (N_ "Total VAT due") (N_ "VAT (Rep|P)ayment") #f #f (list "VAT:Output" ) #t #t) (list (N_ "Box 4") (N_ "Input VAT") (N_ "VAT (Rep|P)ayment") #f #t (list "VAT:Input" ) #f #f) (list (N_ "Box 5") (N_ "Net VAT to be paid (reclaimed)") (N_ "VAT (Rep|P)ayment") #f #t (list "VAT" ) #t #t) (list (N_ "Box 6") (N_ "Total value of sales and outputs excluding VAT") #f #f #f (list "Income" "Assets:Capital Equipment:EC Reverse VAT Purchase" ) #t #t) (list (N_ "Box 7") (N_ "Total values of purchases and inputs excluding VAT" ) (N_ "Depreciation") #f #f (list "Assets:Capital Equipment" "Expenses:VAT Purchases" ) #t #f) (list (N_ "Box 8") (N_ "Total value of supplies of goods to EU") #f #f #f (list "Income:Sales:EEC:Goods" ) #t #t) (list (N_ "Box 9") (N_ "Total value of aquisitions of goods etc. excluding VAT from EU") #f #f #f (list "Expenses:VAT Purchases:EEC Reverse VAT:Goods" ) #t #f) )) (define optname-accounts (N_ "Accounts to include")) (define opthelp-accounts (N_ "Report on these accounts, if display depth allows.")) (define optname-include-subaccounts (N_ "Include sub-accounts?")) (define opthelp-include-subaccounts (N_ "Include sub-accounts in the totals")) (define pagename-commodities (N_ "Commodities")) (define optname-report-commodity (N_ "Report's currency")) (define optname-price-source (N_ "Price Source")) (define optname-closing-pattern (N_ "Closing Entries pattern")) (define opthelp-closing-pattern (N_ "Any text in the Description column which identifies closing entries")) (define optname-closing-casing (N_ "Closing Entries pattern is case-sensitive")) (define opthelp-closing-casing (N_ "Causes the Closing Entries Pattern match to be case-sensitive")) (define optname-closing-regexp (N_ "Closing Entries Pattern is regular expression")) (define opthelp-closing-regexp (N_ "Causes the Closing Entries Pattern to be treated as a regular expression")) ;; options generator (define (vat-return-options-generator) (let* ((options (gnc:new-options)) (add-option (lambda (new-option) (gnc:register-option options new-option)))) (add-option (gnc:make-string-option gnc:pagename-general optname-report-title "a" opthelp-report-title (_ reportname))) (add-option (gnc:make-string-option gnc:pagename-general optname-party-name "b" opthelp-party-name ; "")) (let* ( (slots (gnc-book-get-slots (gnc-get-current-book))) (name (kvp-frame-get-slot-path-gslist slots (append gnc:*kvp-option-path* (list gnc:*business-label* gnc:*company-name*)))) ) name ) )) ; It doesn't appear you can use the interval function without using the ; default start and end values, so I've had to define the interval as ; a separate start and end date. (add-option (gnc:make-date-option gnc:pagename-general optname-start-date "ca" (N_ "Start of reporting period") (lambda () (cons 'relative 'start-prev-quarter)) #f 'both '( today start-this-month start-prev-month start-current-quarter start-prev-quarter start-cal-year start-prev-year start-accounting-period ))) (add-option (gnc:make-date-option gnc:pagename-general optname-end-date "cb" (N_ "End of reporting period") (lambda () (cons 'relative 'end-prev-quarter)) #f 'both '( today end-this-month end-prev-month end-current-quarter end-prev-quarter end-cal-year end-prev-year end-accounting-period ))) ;; accounts to work on (for-each (lambda (x) (add-option (gnc:make-account-list-option (list-ref x 0) optname-accounts "a" opthelp-accounts (lambda () (filter-accountlist-fullname (list-ref x 5) (gnc-account-get-descendants-sorted (gnc-get-current-root-account)) ) ) #f #t )) (add-option (gnc:make-simple-boolean-option (list-ref x 0) optname-include-subaccounts "b" opthelp-include-subaccounts (list-ref x 6) )) (add-option (gnc:make-string-option (list-ref x 0) optname-closing-pattern "c" opthelp-closing-pattern (if (list-ref x 2) (list-ref x 2) (N_ "Closing Entries")))) (add-option (gnc:make-simple-boolean-option (list-ref x 0) optname-closing-casing "d" opthelp-closing-casing (list-ref x 3))) (add-option (gnc:make-simple-boolean-option (list-ref x 0) optname-closing-regexp "e" opthelp-closing-regexp (list-ref x 4))) ) box-list ) ;; all about currencies (gnc:options-add-currency! options pagename-commodities optname-report-commodity "a") (gnc:options-add-price-source! options pagename-commodities optname-price-source "b" 'average-cost) ;; Set the accounts page as default option tab (gnc:options-set-default-section options gnc:pagename-general) options)) ;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; vat-return-renderer ;; set up the document and add the table ;;;;;;;;;;;;;;;;;;;;;;;;;;; (define (vat-return-renderer report-obj) (define (get-option pagename optname) (gnc:option-value (gnc:lookup-option (gnc:report-options report-obj) pagename optname))) (gnc:report-starting reportname) ;; get general option's value (i.e. not box specific) (let* ( (report-title (get-option gnc:pagename-general optname-report-title)) (company-name (get-option gnc:pagename-general optname-party-name)) (start-date-printable (gnc:date-option-absolute-time (get-option gnc:pagename-general optname-start-date))) (start-date-tp (gnc:timepair-start-day-time (gnc:date-option-absolute-time (get-option gnc:pagename-general optname-start-date)))) (end-date-tp (gnc:timepair-end-day-time (gnc:date-option-absolute-time (get-option gnc:pagename-general optname-end-date)))) (report-commodity (get-option pagename-commodities optname-report-commodity)) (price-source (get-option pagename-commodities optname-price-source)) (box-table (gnc:make-html-table)) (doc (gnc:make-html-document)) (exchange-fn (gnc:case-exchange-fn price-source report-commodity end-date-tp)) ) (gnc:html-document-set-title! doc (sprintf #f (string-append "%s %s " (_ "For Period Covering %s to %s")) company-name report-title (gnc-print-date start-date-printable) (gnc-print-date end-date-tp))) (for-each (lambda (box) (let* ( ; options specific to each box (box-name (list-ref box 0)) (box-desc (list-ref box 1)) (box-accounts (get-option box-name optname-accounts)) (box-inc-sub (get-option box-name optname-include-subaccounts)) (box-accounts (if box-inc-sub (append box-accounts (gnc:acccounts-get-all-subaccounts box-accounts)) box-accounts )) (box-inv (list-ref box 7)) (closing-str (get-option box-name optname-closing-pattern)) (closing-cased (get-option box-name optname-closing-casing)) (closing-regexp (get-option box-name optname-closing-regexp)) (closing-pattern (list (list 'str closing-str) (list 'cased closing-cased) (list 'regexp closing-regexp) ) ) (value-closing #f) (value-total #f) ) ; let defs ;; sum revenues and expenses (set! value-closing (gnc:account-get-trans-type-balance-interval box-accounts closing-pattern start-date-tp end-date-tp) ) ;; this is norm positive (debit) (set! value-total (gnc:accountlist-get-comm-balance-interval box-accounts start-date-tp end-date-tp)) (value-total 'minusmerge value-closing #f) ;; Workaround to force gtkhtml into displaying wide ;; enough columns. (let ((space (make-list 6 "     \      \      ") )) (gnc:html-table-append-row! box-table space)) (gnc:html-table-add-labeled-amount-line! box-table #f #f #f (string-append box-name " - " box-desc) 0 4 "text-cell" (gnc:gnc-monetary-amount (let* ( (value (gnc:sum-collector-commodity value-total report-commodity exchange-fn))) (if box-inv (gnc:monetary-neg value) value))) 5 1 "number-cell") ) ;let ) ; lambda box-list ) (gnc:html-table-set-style! box-table "td" 'attribute '("align" "left") 'attribute '("valign" "top")) (gnc:html-document-add-object! doc box-table ) (gnc:report-percent-done 100) (gnc:report-finished) doc ) ; let ) ; define (gnc:define-report 'version 1 'name reportname 'menu-path (list gnc:menuname-business-reports) 'options-generator vat-return-options-generator 'renderer vat-return-renderer ) ;; END