/[drupal]/contributions/modules/openresort/taxes/taxes.module
ViewVC logotype

Contents of /contributions/modules/openresort/taxes/taxes.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.5 - (show annotations) (download) (as text)
Wed Jan 2 22:14:26 2008 UTC (22 months, 3 weeks ago) by marcingy
Branch: MAIN
CVS Tags: DRUPAL-5--1-15-1, DRUPAL-5--1-15-2, DRUPAL-5--1-16-6, DRUPAL-5--1-16-4, DRUPAL-5--1-16-5, DRUPAL-5--1-16-2, DRUPAL-5--1-16-3, DRUPAL-5--1-16-1, DRUPAL-5--1-15, DRUPAL-5--1-16, HEAD
Changes since 1.4: +1 -1 lines
File MIME type: text/x-php
*** empty log message ***
1 <?php
2 // $Id: $
3
4 /********************************************************************
5 * Drupal Hooks
6 ********************************************************************/
7
8 /**
9 * Implementation of hook_menu().
10 *
11 */
12 function taxes_menu($may_cache) {
13 $items = array();
14
15 if ($may_cache) {
16 $items[] = array('path' => 'admin/business/taxes',
17 'title' => t('Taxes'),
18 'callback' => 'taxes_overview',
19 'access' => user_access('administer businesses'));
20 $items[] = array('path' => 'admin/business/taxes/list',
21 'title' => t('list'),
22 'type' => MENU_DEFAULT_LOCAL_TASK,
23 'weight' => -10);
24 $items[] = array('path' => 'admin/business/taxes/add/tax',
25 'title' => t('Add tax'),
26 'callback' => 'taxes_admin_tax_edit',
27 'access' => user_access('administer businesses'),
28 'type' => MENU_LOCAL_TASK);
29 }
30 else {
31 if (is_numeric(arg(3))) {
32 $items[] = array('path' => 'admin/business/taxes/' . arg(3),
33 'callback' => 'taxes_admin_tax_edit',
34 'callback arguments' => array(arg(3)),
35 'access' => user_access('administer businesses'),
36 'type' => MENU_CALLBACK);
37 }
38 }
39 return $items;
40 }
41
42 /**
43 * Display a list of all available terms in a section, with options to edit
44 * each one.
45 */
46 function taxes_overview() {
47 $destination = drupal_get_destination();
48 drupal_set_title(t("Taxes"));
49
50 $header = array(t('Condition name'), t('Operations'));
51
52
53 $result = db_query('SELECT * FROM {business_taxes} ORDER BY weight');
54 $rows = array();
55 while ($tax = db_fetch_object($result)) {
56 $rows[] = array(check_plain($tax->name),
57 l(t('edit'), "admin/business/taxes/$tax->tid", array(), $destination));
58 }
59 if (!$rows) {
60 $rows[] = array(array('data' => t('No taxes available.'), 'colspan' => '2'));
61 }
62
63 return theme('table', $header, $rows, array('id' => 'taxes'));
64 }
65
66 /**
67 * Page to add or edit a tax
68 */
69 function taxes_admin_tax_edit($tid = NULL) {
70 if ($tid) {
71 $edit = db_fetch_array(db_query('SELECT * FROM {business_taxes} WHERE tid = %d', $tid));
72 return drupal_get_form('taxes_form_tax', $edit);
73 }
74 else {
75 return drupal_get_form('taxes_form_tax');
76 }
77 }
78
79 /**
80 * Display form for adding and editing vocabularies.
81 */
82 function taxes_form_tax($edit = array()) {
83 $form['name'] = array('#type' => 'textfield',
84 '#title' => t('Tax name'),
85 '#default_value' => $edit['name'],
86 '#maxlength' => 20,
87 '#description' => t('The name of the tax. Example: "GST".'),
88 '#required' => TRUE,
89 );
90 $form['rate'] = array('#type' => 'textfield',
91 '#title' => t('Tax rate'),
92 '#default_value' => $edit['rate'],
93 '#maxlength' => 6,
94 '#description' => t('The tax rate as a percentage, without the % sign. Example: "6", to indicate a tax of 6%.'),
95 '#required' => TRUE,
96 );
97 $form['term_id'] = array(
98 '#type' => 'select',
99 '#title' => t('Term'),
100 '#options' => terms_form_policy_select(),
101 '#multiple' => FALSE,
102 '#default_value' => $edit['term_id'],
103 '#description' => t('The term that triggers the tax being added to transactions for a business.'),
104 '#required' => TRUE,
105 );
106 $form['weight'] = array('#type' => 'weight',
107 '#title' => t('Weight'),
108 '#default_value' => $edit['weight'],
109 '#description' => t('In listings, the heavier terms will sink and the lighter terms will be positioned nearer the top.')
110 );
111
112 $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
113 if ($edit['tid']) {
114 $form['delete'] = array('#type' => 'submit', '#value' => t('Delete'));
115 $form['tid'] = array('#type' => 'value', '#value' => $edit['tid']);
116 }
117 return $form;
118 }
119
120 /**
121 * Accept the form submission for a section and save the results.
122 */
123 function taxes_form_tax_submit($form_id, $form_values) {
124 switch (taxes_save_tax($form_values)) {
125 case SAVED_NEW:
126 drupal_set_message(t('Tax added'));
127 break;
128 case SAVED_UPDATED:
129 drupal_set_message(t('Tax updated'));
130 break;
131 case SAVED_DELETED:
132 drupal_set_message(t('Tax deleted'));
133 break;
134 }
135 return 'admin/business/taxes';
136 }
137
138 function taxes_save_tax(&$edit) {
139 $edit['op'] = $_POST['op'];
140
141 if ($edit['op'] == "Delete" && $edit['tid']) {
142 db_query("DELETE FROM {business_taxes} WHERE tid = '%d'", $edit['tid']);
143 $status = SAVED_DELETED;
144 }
145 else if ($edit['tid']) {
146 db_query("UPDATE {business_taxes} SET tid = '%d', term_id = '%d', name = '%s', rate = '%d', weight = '%d' WHERE tid = %d", $edit['tid'], $edit['term_id'], $edit['name'], $edit['rate'], $edit['weight'], $edit['tid']);
147 $status = SAVED_UPDATED;
148 } else {
149 $edit['tid'] = db_next_id('{business_taxes}_tid');
150 db_query("INSERT INTO {business_taxes} (tid, term_id, name, rate, weight) VALUES (%d, %d, '%s', '%d', '%d')", $edit['tid'], $edit['term_id'], $edit['name'], $edit['rate'], $edit['weight']);
151 $status = SAVED_NEW;
152 }
153 return $status;
154 }
155
156 /**
157 * Implementation of hook_checkoutapi().
158 */
159 function taxes_checkoutapi(&$txn, $op, $arg3 = NULL, $arg4 = NULL) {
160
161 switch ($op) {
162 case 'review':
163 if ($txn == 'taxes') return TRUE;
164
165 // Get all taxes
166 $taxes = taxes_get_taxes();
167 $taxable_amount = 0;
168 $taxables = array();
169 foreach ($txn->items as $item) {
170 $terms = terms_get_node_terms_only($item->nid);
171 $taxable_amount = store_adjust_misc($txn, $item) * $item->qty;
172 foreach ($taxes as $tid => $tax) {
173 if ($tax->term_id && $terms[$tax->term_id]) {
174 if(array_key_exists($tax->name,$taxables)){
175 $taxables[$tax->name] = $taxables[$tax->name] + $taxable_amount;
176
177 }else{
178 $taxables[$tax->name]=$taxable_amount;
179 }
180 }
181 }
182 }
183 $tax_amount = 0;
184 foreach ($taxes as $tid => $tax) {
185 if(array_key_exists($tax->name,$taxables)){
186 $tax_amount = taxes_calculate($taxables[$tax->name], "%", $tax->rate);
187 }
188 $misc = array(
189 'type' => 'tax',
190 'description' => $tax->name,
191 'price' => $tax_amount,
192 'weight' => 10
193 );
194 $txn->misc[] = (object)$misc;
195 }
196 return;
197 }
198 }
199
200 /**
201 * Return the tax cost for a given rule.
202 */
203 function taxes_calculate($gross, $operand, $op2) {
204 switch ($operand) {
205 case '+':
206 return $op2;
207 case '%':
208 return($gross * ($op2/100));
209 }
210 }
211
212 /**
213 * Returns the list of taxes, or just the specified tax
214 */
215 function taxes_get_taxes($tid = null) {
216 if ($tid) {
217 $result = db_query('SELECT * FROM {business_taxes} WHERE tid = %d ORDER BY weight, name', $tid);
218 }
219 else {
220 $result = db_query('SELECT * FROM {business_taxes} ORDER BY weight, name');
221 }
222 $taxes = array();
223 while ($tax = db_fetch_object($result)) {
224 $taxes[$tax->tid] = $tax;
225 }
226 return $taxes;
227 }

  ViewVC Help
Powered by ViewVC 1.1.2