Quick Search:

View

Revision:

Diff

Diff from 1498 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/osCommerce/trunk/oscommerce/includes/classes/order.php

Annotated File View

hpdl
1
1 <?php
2 /*
hpdl
20
3   $Id: order.php 1498 2007-03-29 14:04:50Z hpdl $
hpdl
1
4
5   osCommerce, Open Source E-Commerce Solutions
6   http://www.oscommerce.com
7
hpdl
554
8   Copyright (c) 2006 osCommerce
hpdl
1
9
hpdl
1498
10   This program is free software; you can redistribute it and/or modify
11   it under the terms of the GNU General Public License v2 (1991)
12   as published by the Free Software Foundation.
hpdl
1
13 */
14
hpdl
554
15   class osC_Order {
hpdl
1
16     var $info, $totals, $products, $customer, $delivery, $content_type;
17
hpdl
368
18 /* Private variables */
19
20     var $_id;
21
22 /* Class constructor */
23
hpdl
554
24     function osC_Order($order_id = '') {
hpdl
368
25       if (is_numeric($order_id)) {
26         $this->_id = $order_id;
27       }
28
hpdl
1
29       $this->info = array();
30       $this->totals = array();
31       $this->products = array();
32       $this->customer = array();
33       $this->delivery = array();
34
hpdl
757
35       if (!empty($order_id)) {
hpdl
1
36         $this->query($order_id);
37       } else {
38         $this->cart();
39       }
40     }
41
hpdl
368
42 /* Public methods */
43
hpdl
554
44     function getStatusID($id) {
45       global $osC_Database;
46
47       $Qorder = $osC_Database->query('select orders_status from :table_orders where orders_id = :orders_id');
48       $Qorder->bindTable(':table_orders', TABLE_ORDERS);
49       $Qorder->bindInt(':orders_id', $id);
50       $Qorder->execute();
51
52       if ($Qorder->numberOfRows()) {
53         return $Qorder->valueInt('orders_status');
54       }
55
56       return false;
57     }
58
59     function remove($id) {
60       global $osC_Database;
61
62       $Qcheck = $osC_Database->query('select orders_status from :table_orders where orders_id = :orders_id');
63       $Qcheck->bindTable(':table_orders', TABLE_ORDERS);
64       $Qcheck->bindInt(':orders_id', $id);
65       $Qcheck->execute();
66
67       if ($Qcheck->valueInt('orders_status') === 4) {
68         $Qdel = $osC_Database->query('delete from :table_orders_products_download where orders_id = :orders_id');
69         $Qdel->bindTable(':table_orders_products_download', TABLE_ORDERS_PRODUCTS_DOWNLOAD);
70         $Qdel->bindInt(':orders_id', $id);
71         $Qdel->execute();
72
73         $Qdel = $osC_Database->query('delete from :table_orders_products_attributes where orders_id = :orders_id');
hpdl
1060
74         $Qdel->bindTable(':table_orders_products_attributes', TABLE_ORDERS_PRODUCTS_ATTRIBUTES);
hpdl
554
75         $Qdel->bindInt(':orders_id', $id);
76         $Qdel->execute();
77
78         $Qdel = $osC_Database->query('delete from :table_orders_products where orders_id = :orders_id');
79         $Qdel->bindTable(':table_orders_products', TABLE_ORDERS_PRODUCTS);
80         $Qdel->bindInt(':orders_id', $id);
81         $Qdel->execute();
82
83         $Qdel = $osC_Database->query('delete from :table_orders_status_history where orders_id = :orders_id');
84         $Qdel->bindTable(':table_orders_status_history', TABLE_ORDERS_STATUS_HISTORY);
85         $Qdel->bindInt(':orders_id', $id);
86         $Qdel->execute();
87
88         $Qdel = $osC_Database->query('delete from :table_orders_total where orders_id = :orders_id');
89         $Qdel->bindTable(':table_orders_total', TABLE_ORDERS_TOTAL);
90         $Qdel->bindInt(':orders_id', $id);
91         $Qdel->execute();
92
93         $Qdel = $osC_Database->query('delete from :table_orders where orders_id = :orders_id');
94         $Qdel->bindTable(':table_orders', TABLE_ORDERS);
95         $Qdel->bindInt(':orders_id', $id);
96         $Qdel->execute();
97       }
98
99       if (isset($_SESSION['prepOrderID'])) {
100         unset($_SESSION['prepOrderID']);
101       }
102     }
103
104     function insert() {
105       global $osC_Database, $osC_Customer, $osC_Language, $osC_Currencies, $osC_ShoppingCart;
106
107       if (isset($_SESSION['prepOrderID'])) {
108         $_prep = explode('-', $_SESSION['prepOrderID']);
109
110         if ($_prep[0] == $osC_ShoppingCart->getCartID()) {
111           return $_prep[1]; // order_id
112         } else {
113           if (osC_Order::getStatusID($_prep[1]) === 4) {
114             osC_Order::remove($_prep[1]);
115           }
116         }
117       }
118
hpdl
757
119       $Qorder = $osC_Database->query('insert into :table_orders (customers_id, customers_name, customers_company, customers_street_address, customers_suburb, customers_city, customers_postcode, customers_state, customers_state_code, customers_country, customers_country_iso2, customers_country_iso3, customers_telephone, customers_email_address, customers_address_format, customers_ip_address, delivery_name, delivery_company, delivery_street_address, delivery_suburb, delivery_city, delivery_postcode, delivery_state, delivery_state_code, delivery_country, delivery_country_iso2, delivery_country_iso3, delivery_address_format, billing_name, billing_company, billing_street_address, billing_suburb, billing_city, billing_postcode, billing_state, billing_state_code, billing_country, billing_country_iso2, billing_country_iso3, billing_address_format, payment_method, payment_module, date_purchased, orders_status, currency, currency_value) values (:customers_id, :customers_name, :customers_company, :customers_street_address, :customers_suburb, :customers_city, :customers_postcode, :customers_state, :customers_state_code, :customers_country, :customers_country_iso2, :customers_country_iso3, :customers_telephone, :customers_email_address, :customers_address_format, :customers_ip_address, :delivery_name, :delivery_company, :delivery_street_address, :delivery_suburb, :delivery_city, :delivery_postcode, :delivery_state, :delivery_state_code, :delivery_country, :delivery_country_iso2, :delivery_country_iso3, :delivery_address_format, :billing_name, :billing_company, :billing_street_address, :billing_suburb, :billing_city, :billing_postcode, :billing_state, :billing_state_code, :billing_country, :billing_country_iso2, :billing_country_iso3, :billing_address_format, :payment_method, :payment_module, now(), :orders_status, :currency, :currency_value)');
hpdl
554
120       $Qorder->bindTable(':table_orders', TABLE_ORDERS);
121       $Qorder->bindInt(':customers_id', $osC_Customer->getID());
122       $Qorder->bindValue(':customers_name', $osC_Customer->getName());
123       $Qorder->bindValue(':customers_company', '' /*$order->customer['company']*/);
124       $Qorder->bindValue(':customers_street_address', '' /*$order->customer['street_address']*/);
125       $Qorder->bindValue(':customers_suburb', '' /*$order->customer['suburb']*/);
126       $Qorder->bindValue(':customers_city', '' /*$order->customer['city']*/);
127       $Qorder->bindValue(':customers_postcode', '' /*$order->customer['postcode']*/);
128       $Qorder->bindValue(':customers_state', '' /*$order->customer['state']*/);
hpdl
757
129       $Qorder->bindValue(':customers_state_code', '');
hpdl
554
130       $Qorder->bindValue(':customers_country', '' /*$order->customer['country']['title']*/);
hpdl
757
131       $Qorder->bindValue(':customers_country_iso2', '');
132       $Qorder->bindValue(':customers_country_iso3', '');
hpdl
554
133       $Qorder->bindValue(':customers_telephone', '' /*$order->customer['telephone']*/);
134       $Qorder->bindValue(':customers_email_address', $osC_Customer->getEmailAddress());
hpdl
757
135       $Qorder->bindValue(':customers_address_format', '');
136       $Qorder->bindValue(':customers_ip_address', osc_get_ip_address());
hpdl
554
137       $Qorder->bindValue(':delivery_name', $osC_ShoppingCart->getShippingAddress('firstname') . ' ' . $osC_ShoppingCart->getShippingAddress('lastname'));
138       $Qorder->bindValue(':delivery_company', $osC_ShoppingCart->getShippingAddress('company'));
139       $Qorder->bindValue(':delivery_street_address', $osC_ShoppingCart->getShippingAddress('street_address'));
140       $Qorder->bindValue(':delivery_suburb', $osC_ShoppingCart->getShippingAddress('suburb'));
141       $Qorder->bindValue(':delivery_city', $osC_ShoppingCart->getShippingAddress('city'));
142       $Qorder->bindValue(':delivery_postcode', $osC_ShoppingCart->getShippingAddress('postcode'));
143       $Qorder->bindValue(':delivery_state', $osC_ShoppingCart->getShippingAddress('state'));
hpdl
757
144       $Qorder->bindValue(':delivery_state_code', $osC_ShoppingCart->getShippingAddress('zone_code'));
hpdl
554
145       $Qorder->bindValue(':delivery_country', $osC_ShoppingCart->getShippingAddress('country_title'));
hpdl
757
146       $Qorder->bindValue(':delivery_country_iso2', $osC_ShoppingCart->getShippingAddress('country_iso_code_2'));
147       $Qorder->bindValue(':delivery_country_iso3', $osC_ShoppingCart->getShippingAddress('country_iso_code_3'));
148       $Qorder->bindValue(':delivery_address_format', $osC_ShoppingCart->getShippingAddress('format'));
hpdl
554
149       $Qorder->bindValue(':billing_name', $osC_ShoppingCart->getBillingAddress('firstname') . ' ' . $osC_ShoppingCart->getBillingAddress('lastname'));
150       $Qorder->bindValue(':billing_company', $osC_ShoppingCart->getBillingAddress('company'));
151       $Qorder->bindValue(':billing_street_address', $osC_ShoppingCart->getBillingAddress('street_address'));
152       $Qorder->bindValue(':billing_suburb', $osC_ShoppingCart->getBillingAddress('suburb'));
153       $Qorder->bindValue(':billing_city', $osC_ShoppingCart->getBillingAddress('city'));
154       $Qorder->bindValue(':billing_postcode', $osC_ShoppingCart->getBillingAddress('postcode'));
155       $Qorder->bindValue(':billing_state', $osC_ShoppingCart->getBillingAddress('state'));
hpdl
757
156       $Qorder->bindValue(':billing_state_code', $osC_ShoppingCart->getBillingAddress('zone_code'));
hpdl
554
157       $Qorder->bindValue(':billing_country', $osC_ShoppingCart->getBillingAddress('country_title'));
hpdl
757
158       $Qorder->bindValue(':billing_country_iso2', $osC_ShoppingCart->getBillingAddress('country_iso_code_2'));
159       $Qorder->bindValue(':billing_country_iso3', $osC_ShoppingCart->getBillingAddress('country_iso_code_3'));
160       $Qorder->bindValue(':billing_address_format', $osC_ShoppingCart->getBillingAddress('format'));
hpdl
554
161       $Qorder->bindValue(':payment_method', $osC_ShoppingCart->getBillingMethod('title'));
162       $Qorder->bindValue(':payment_module', $GLOBALS['osC_Payment_' . $osC_ShoppingCart->getBillingMethod('id')]->getCode());
163       $Qorder->bindInt(':orders_status', 4);
164       $Qorder->bindValue(':currency', $osC_Currencies->getCode());
165       $Qorder->bindValue(':currency_value', $osC_Currencies->value($osC_Currencies->getCode()));
166       $Qorder->execute();
167
168       $insert_id = $osC_Database->nextID();
169
170       foreach ($osC_ShoppingCart->getOrderTotals() as $module) {
171         $Qtotals = $osC_Database->query('insert into :table_orders_total (orders_id, title, text, value, class, sort_order) values (:orders_id, :title, :text, :value, :class, :sort_order)');
172         $Qtotals->bindTable(':table_orders_total', TABLE_ORDERS_TOTAL);
173         $Qtotals->bindInt(':orders_id', $insert_id);
174         $Qtotals->bindValue(':title', $module['title']);
175         $Qtotals->bindValue(':text', $module['text']);
176         $Qtotals->bindValue(':value', $module['value']);
177         $Qtotals->bindValue(':class', $module['code']);
178         $Qtotals->bindInt(':sort_order', $module['sort_order']);
179         $Qtotals->execute();
180       }
181
182       $Qstatus = $osC_Database->query('insert into :table_orders_status_history (orders_id, orders_status_id, date_added, customer_notified, comments) values (:orders_id, :orders_status_id, now(), :customer_notified, :comments)');
183       $Qstatus->bindTable(':table_orders_status_history', TABLE_ORDERS_STATUS_HISTORY);
184       $Qstatus->bindInt(':orders_id', $insert_id);
185       $Qstatus->bindInt(':orders_status_id', 4);
186       $Qstatus->bindInt(':customer_notified', '0');
187       $Qstatus->bindValue(':comments', (isset($_SESSION['comments']) ? $_SESSION['comments'] : ''));
188       $Qstatus->execute();
189
190       foreach ($osC_ShoppingCart->getProducts() as $products) {
191         $Qproducts = $osC_Database->query('insert into :table_orders_products (orders_id, products_id, products_model, products_name, products_price, final_price, products_tax, products_quantity) values (:orders_id, :products_id, :products_model, :products_name, :products_price, :final_price, :products_tax, :products_quantity)');
192         $Qproducts->bindTable(':table_orders_products', TABLE_ORDERS_PRODUCTS);
193         $Qproducts->bindInt(':orders_id', $insert_id);
hpdl
757
194         $Qproducts->bindInt(':products_id', osc_get_product_id($products['id']));
hpdl
554
195         $Qproducts->bindValue(':products_model', '' /*$products['model']*/);
196         $Qproducts->bindValue(':products_name', $products['name']);
197         $Qproducts->bindValue(':products_price', $products['price']);
198         $Qproducts->bindValue(':final_price', $products['final_price']);
199         $Qproducts->bindValue(':products_tax', '' /*$products['tax']*/);
200         $Qproducts->bindInt(':products_quantity', $products['quantity']);
201         $Qproducts->execute();
202
203         $order_products_id = $osC_Database->nextID();
204
205         if ($osC_ShoppingCart->hasAttributes($products['id'])) {
206           foreach ($osC_ShoppingCart->getAttributes($products['id']) as $attributes) {
207             if (DOWNLOAD_ENABLED == '1') {
208               $Qattributes = $osC_Database->query('select popt.products_options_name, poval.products_options_values_name, pa.options_values_price, pa.price_prefix, pad.products_attributes_maxdays, pad.products_attributes_maxcount, pad.products_attributes_filename from :table_products_options popt, :table_products_options_values poval, :table_products_attributes pa left join :table_products_attributes_download pad on (pa.products_attributes_id = pad.products_attributes_id) where pa.products_id = :products_id and pa.options_id = :options_id and pa.options_id = popt.products_options_id and pa.options_values_id = :options_values_id and pa.options_values_id = poval.products_options_values_id and popt.language_id = :popt_language_id and poval.language_id = :poval_language_id');
209               $Qattributes->bindTable(':table_products_options', TABLE_PRODUCTS_OPTIONS);
210               $Qattributes->bindTable(':table_products_options_values', TABLE_PRODUCTS_OPTIONS_VALUES);
211               $Qattributes->bindTable(':table_products_attributes', TABLE_PRODUCTS_ATTRIBUTES);
212               $Qattributes->bindTable(':table_products_attributes_download', TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD);
213               $Qattributes->bindInt(':products_id', $products['id']);
214               $Qattributes->bindInt(':options_id', $attributes['options_id']);
215               $Qattributes->bindInt(':options_values_id', $attributes['options_values_id']);
216               $Qattributes->bindInt(':popt_language_id', $osC_Language->getID());
217               $Qattributes->bindInt(':poval_language_id', $osC_Language->getID());
218             } else {
219               $Qattributes = $osC_Database->query('select popt.products_options_name, poval.products_options_values_name, pa.options_values_price, pa.price_prefix from :table_products_options popt, :table_products_options_values poval, :table_products_attributes pa where pa.products_id = :products_id and pa.options_id = :options_id and pa.options_id = popt.products_options_id and pa.options_values_id = :options_values_id and pa.options_values_id = poval.products_options_values_id and popt.language_id = :popt_language_id and poval.language_id = :poval_language_id');
220               $Qattributes->bindTable(':table_products_options', TABLE_PRODUCTS_OPTIONS);
221               $Qattributes->bindTable(':table_products_options_values', TABLE_PRODUCTS_OPTIONS_VALUES);
222               $Qattributes->bindTable(':table_products_attributes', TABLE_PRODUCTS_ATTRIBUTES);
223               $Qattributes->bindInt(':products_id', $products['id']);
224               $Qattributes->bindInt(':options_id', $attributes['options_id']);
225               $Qattributes->bindInt(':options_values_id', $attributes['options_values_id']);
226               $Qattributes->bindInt(':popt_language_id', $osC_Language->getID());
227               $Qattributes->bindInt(':poval_language_id', $osC_Language->getID());
228             }
229             $Qattributes->execute();
230
231             $Qopa = $osC_Database->query('insert into :table_orders_products_attributes (orders_id, orders_products_id, products_options, products_options_values, options_values_price, price_prefix) values (:orders_id, :orders_products_id, :products_options, :products_options_values, :options_values_price, :price_prefix)');
232             $Qopa->bindTable(':table_orders_products_attributes', TABLE_ORDERS_PRODUCTS_ATTRIBUTES);
233             $Qopa->bindInt(':orders_id', $insert_id);
234             $Qopa->bindInt(':orders_products_id', $order_products_id);
235             $Qopa->bindValue(':products_options', $Qattributes->value('products_options_name'));
236             $Qopa->bindValue(':products_options_values', $Qattributes->value('products_options_values_name'));
237             $Qopa->bindValue(':options_values_price', $Qattributes->value('options_values_price'));
238             $Qopa->bindValue(':price_prefix', $Qattributes->value('price_prefix'));
239             $Qopa->execute();
240
241             if ((DOWNLOAD_ENABLED == '1') && (strlen($Qattributes->value('products_attributes_filename')) > 0)) {
242               $Qopd = $osC_Database->query('insert into :table_orders_products_download (orders_id, orders_products_id, orders_products_filename, download_maxdays, download_count) values (:orders_id, :orders_products_id, :orders_products_filename, :download_maxdays, :download_count)');
243               $Qopd->bindTable(':table_orders_products_download', TABLE_ORDERS_PRODUCTS_DOWNLOAD);
244               $Qopd->bindInt(':orders_id', $insert_id);
245               $Qopd->bindInt(':orders_products_id', $order_products_id);
246               $Qopd->bindValue(':orders_products_filename', $Qattributes->value('products_attributes_filename'));
247               $Qopd->bindValue(':download_maxdays', $Qattributes->value('products_attributes_maxdays'));
248               $Qopd->bindValue(':download_count', $Qattributes->value('products_attributes_maxcount'));
249               $Qopd->execute();
250             }
251           }
252         }
253       }
254
255       $_SESSION['prepOrderID'] = $osC_ShoppingCart->getCartID() . '-' . $insert_id;
256
257       return $insert_id;
258     }
259
260     function process($order_id, $status_id = '') {
261       global $osC_Database;
262
263       if (empty($status_id) || (is_numeric($status_id) === false)) {
264         $status_id = DEFAULT_ORDERS_STATUS_ID;
265       }
266
267       $Qstatus = $osC_Database->query('insert into :table_orders_status_history (orders_id, orders_status_id, date_added, customer_notified, comments) values (:orders_id, :orders_status_id, now(), :customer_notified, :comments)');
268       $Qstatus->bindTable(':table_orders_status_history', TABLE_ORDERS_STATUS_HISTORY);
269       $Qstatus->bindInt(':orders_id', $order_id);
270       $Qstatus->bindInt(':orders_status_id', $status_id);
271       $Qstatus->bindInt(':customer_notified', (SEND_EMAILS == '1') ? '1' : '0');
272       $Qstatus->bindValue(':comments', '');
273       $Qstatus->execute();
274
275       $Qupdate = $osC_Database->query('update :table_orders set orders_status = :orders_status where orders_id = :orders_id');
276       $Qupdate->bindTable(':table_orders', TABLE_ORDERS);
277       $Qupdate->bindInt(':orders_status', $status_id);
278       $Qupdate->bindInt(':orders_id', $order_id);
279       $Qupdate->execute();
280
281       $Qproducts = $osC_Database->query('select products_id, products_quantity from :table_orders_products where orders_id = :orders_id');
282       $Qproducts->bindTable(':table_orders_products', TABLE_ORDERS_PRODUCTS);
283       $Qproducts->bindInt(':orders_id', $order_id);
284       $Qproducts->execute();
285
286       while ($Qproducts->next()) {
287         if (STOCK_LIMITED == '1') {
288
289 /********** HPDL ; still uses logic from the shopping cart class
290           if (DOWNLOAD_ENABLED == '1') {
291             $Qstock = $osC_Database->query('select products_quantity, pad.products_attributes_filename from :table_products p left join :table_products_attributes pa on (p.products_id = pa.products_id) left join :table_products_attributes_download pad on (pa.products_attributes_id = pad.products_attributes_id) where p.products_id = :products_id');
292             $Qstock->bindTable(':table_products', TABLE_PRODUCTS);
293             $Qstock->bindTable(':table_products_attributes', TABLE_PRODUCTS_ATTRIBUTES);
294             $Qstock->bindTable(':table_products_attributes_download', TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD);
295             $Qstock->bindInt(':products_id', $Qproducts->valueInt('products_id'));
296
297 // Will work with only one option for downloadable products otherwise, we have to build the query dynamically with a loop
298             if ($osC_ShoppingCart->hasAttributes($products['id'])) {
299               $products_attributes = $osC_ShoppingCart->getAttributes($products['id']);
300               $products_attributes = array_shift($products_attributes);
301
302               $Qstock->appendQuery('and pa.options_id = :options_id and pa.options_values_id = :options_values_id');
303               $Qstock->bindInt(':options_id', $products_attributes['options_id']);
304               $Qstock->bindInt(':options_values_id', $products_attributes['options_values_id']);
305             }
306           } else {
307 ************/
308             $Qstock = $osC_Database->query('select products_quantity from :table_products where products_id = :products_id');
309             $Qstock->bindTable(':table_products', TABLE_PRODUCTS);
310             $Qstock->bindInt(':products_id', $Qproducts->valueInt('products_id'));
311 // HPDL          }
312
313           $Qstock->execute();
314
315           if ($Qstock->numberOfRows() > 0) {
316             $stock_left = $Qstock->valueInt('products_quantity');
317
318 // do not decrement quantities if products_attributes_filename exists
319 // HPDL            if ((DOWNLOAD_ENABLED == '-1') || ((DOWNLOAD_ENABLED == '1') && (strlen($Qstock->value('products_attributes_filename')) < 1))) {
320               $stock_left = $stock_left - $Qproducts->valueInt('products_quantity');
321
322               $Qupdate = $osC_Database->query('update :table_products set products_quantity = :products_quantity where products_id = :products_id');
323               $Qupdate->bindTable(':table_products', TABLE_PRODUCTS);
324               $Qupdate->bindInt(':products_quantity', $stock_left);
325               $Qupdate->bindInt(':products_id', $Qproducts->valueInt('products_id'));
326               $Qupdate->execute();
327 // HPDL            }
328
329             if ((STOCK_ALLOW_CHECKOUT == '-1') && ($stock_left < 1)) {
330               $Qupdate = $osC_Database->query('update :table_products set products_status = 0 where products_id = :products_id');
331               $Qupdate->bindTable(':table_products', TABLE_PRODUCTS);
332               $Qupdate->bindInt(':products_id', $Qproducts->valueInt('products_id'));
333               $Qupdate->execute();
334             }
335           }
336         }
337
338 // Update products_ordered (for bestsellers list)
339         $Qupdate = $osC_Database->query('update :table_products set products_ordered = products_ordered + :products_ordered where products_id = :products_id');
340         $Qupdate->bindTable(':table_products', TABLE_PRODUCTS);
341         $Qupdate->bindInt(':products_ordered', $Qproducts->valueInt('products_quantity'));
342         $Qupdate->bindInt(':products_id', $Qproducts->valueInt('products_id'));
343         $Qupdate->execute();
344       }
345
346       osC_Order::sendEmail($order_id);
347
348       unset($_SESSION['prepOrderID']);
349     }
350
351     function sendEmail($id) {
352       global $osC_Database, $osC_Language, $osC_Currencies;
353
354       $Qorder = $osC_Database->query('select * from :table_orders where orders_id = :orders_id limit 1');
355       $Qorder->bindTable(':table_orders', TABLE_ORDERS);
356       $Qorder->bindInt(':orders_id', $id);
357       $Qorder->execute();
358
359       if ($Qorder->numberOfRows() === 1) {
360         $email_order = STORE_NAME . "\n" .
361                        $osC_Language->get('email_order_separator') . "\n" .
362                        sprintf($osC_Language->get('email_order_order_number'), $id) . "\n" .
hpdl
754
363                        sprintf($osC_Language->get('email_order_invoice_url'), osc_href_link(FILENAME_ACCOUNT, 'orders=' . $id, 'SSL', false, true, true)) . "\n" .
hpdl
554
364                        sprintf($osC_Language->get('email_order_date_ordered'), osC_DateTime::getLong()) . "\n\n" .
365                        $osC_Language->get('email_order_products') . "\n" .
366                        $osC_Language->get('email_order_separator') . "\n";
367
368         $Qproducts = $osC_Database->query('select orders_products_id, products_model, products_name, final_price, products_tax, products_quantity from :table_orders_products where orders_id = :orders_id order by orders_products_id');
369         $Qproducts->bindTable(':table_orders_products', TABLE_ORDERS_PRODUCTS);
370         $Qproducts->bindInt(':orders_id', $id);
371         $Qproducts->execute();
372
373         while ($Qproducts->next()) {
374           $email_order .= $Qproducts->valueInt('products_quantity') . ' x ' . $Qproducts->value('products_name') . ' (' . $Qproducts->value('products_model') . ') = ' . $osC_Currencies->displayPriceWithTaxRate($Qproducts->value('final_price'), $Qproducts->value('products_tax'), $Qproducts->valueInt('products_quantity'), $Qorder->value('currency'), $Qorder->value('currency_value')) . "\n";
375
376           $Qattributes = $osC_Database->query('select products_options, products_options_values from :table_orders_products_attributes where orders_id = :orders_id and orders_products_id = :orders_products_id order by orders_products_attributes_id');
377           $Qattributes->bindTable(':table_orders_products_attributes', TABLE_ORDERS_PRODUCTS_ATTRIBUTES);
378           $Qattributes->bindInt(':orders_id', $id);
379           $Qattributes->bindInt(':orders_products_id', $Qproducts->valueInt('orders_products_id'));
380           $Qattributes->execute();
381
382           while ($Qattributes->next()) {
383             $email_order .= "\t" . $Qattributes->value('products_options') . ': ' . $Qattributes->value('products_options_values') . "\n";
384           }
385         }
386
387         unset($Qproducts);
388         unset($Qattributes);
389
390         $email_order .= $osC_Language->get('email_order_separator') . "\n";
391
392         $Qtotals = $osC_Database->query('select title, text from :table_orders_total where orders_id = :orders_id order by sort_order');
393         $Qtotals->bindTable(':table_orders_total', TABLE_ORDERS_TOTAL);
394         $Qtotals->bindInt(':orders_id', $id);
395         $Qtotals->execute();
396
397         while ($Qtotals->next()) {
398           $email_order .= strip_tags($Qtotals->value('title') . ' ' . $Qtotals->value('text')) . "\n";
399         }
400
401         unset($Qtotals);
402
403         if ( (osc_empty($Qorder->value('delivery_name') === false)) && (osc_empty($Qorder->value('street_address') === false)) ) {
404           $address = array('name' => $Qorder->value('delivery_name'),
405                            'company' => $Qorder->value('delivery_company'),
406                            'street_address' => $Qorder->value('delivery_street_address'),
407                            'suburb' => $Qorder->value('delivery_suburb'),
408                            'city' => $Qorder->value('delivery_city'),
409                            'state' => $Qorder->value('delivery_state'),
hpdl
928
410                            'zone_code' => $Qorder->value('delivery_state_code'),
411                            'country_title' => $Qorder->value('delivery_country'),
412                            'country_iso2' => $Qorder->value('delivery_country_iso2'),
413                            'country_iso3' => $Qorder->value('delivery_country_iso3'),
hpdl
1444
414                            'postcode' => $Qorder->value('delivery_postcode'),
415                            'format' => $Qorder->value('delivery_address_format'));
hpdl
554
416
417           $email_order .= "\n" . $osC_Language->get('email_order_delivery_address') . "\n" .
418                           $osC_Language->get('email_order_separator') . "\n" .
hpdl
757
419                           osC_Address::format($address, "\n") . "\n";
hpdl
554
420
421           unset($address);
422         }
423
424         $address = array('name' => $Qorder->value('billing_name'),
425                          'company' => $Qorder->value('billing_company'),
426                          'street_address' => $Qorder->value('billing_street_address'),
427                          'suburb' => $Qorder->value('billing_suburb'),
428                          'city' => $Qorder->value('billing_city'),
429                          'state' => $Qorder->value('billing_state'),
hpdl
928
430                          'zone_code' => $Qorder->value('billing_state_code'),
431                          'country_title' => $Qorder->value('billing_country'),
432                          'country_iso2' => $Qorder->value('billing_country_iso2'),
433                          'country_iso3' => $Qorder->value('billing_country_iso3'),
hpdl
1444
434                          'postcode' => $Qorder->value('billing_postcode'),
435                          'format' => $Qorder->value('billing_address_format'));
hpdl
554
436
437         $email_order .= "\n" . $osC_Language->get('email_order_billing_address') . "\n" .
438                         $osC_Language->get('email_order_separator') . "\n" .
hpdl
757
439                         osC_Address::format($address, "\n") . "\n\n";
hpdl
554
440
441         unset($address);
442
443         $Qstatus = $osC_Database->query('select orders_status_name from :table_orders_status where orders_status_id = :orders_status_id and language_id = :language_id');
444         $Qstatus->bindTable(':table_orders_status', TABLE_ORDERS_STATUS);
445         $Qstatus->bindInt(':orders_status_id', $Qorder->valueInt('orders_status'));
446         $Qstatus->bindInt(':language_id', $osC_Language->getID());
447         $Qstatus->execute();
448
449         $email_order .= sprintf($osC_Language->get('email_order_status'), $Qstatus->value('orders_status_name')) . "\n" .
450                         $osC_Language->get('email_order_separator') . "\n";
451
452         unset($Qstatus);
453
454         $Qstatuses = $osC_Database->query('select date_added, comments from :table_orders_status_history where orders_id = :orders_id and comments != "" order by orders_status_history_id');
455         $Qstatuses->bindTable(':table_orders_status_history', TABLE_ORDERS_STATUS_HISTORY);
456         $Qstatuses->bindInt(':orders_id', $id);
457         $Qstatuses->execute();
458
459         while ($Qstatuses->next()) {
460           $email_order .= osC_DateTime::getLong($Qstatuses->value('date_added')) . "\n\t" . wordwrap(str_replace("\n", "\n\t", $Qstatuses->value('comments')), 60, "\n\t", 1) . "\n\n";
461         }
462
463         unset($Qstatuses);
464
465 //        if (is_object($GLOBALS[$payment])) {
466 //          $email_order .= $osC_Language->get('email_order_payment_method') . "\n" .
467 //                          $osC_Language->get('email_order_separator') . "\n";
468
469 //          $email_order .= $osC_ShoppingCart->getBillingMethod('title') . "\n\n";
470 //          if (isset($GLOBALS[$payment]->email_footer)) {
471 //            $email_order .= $GLOBALS[$payment]->email_footer . "\n\n";
472 //          }
473 //        }
474
hpdl
757
475         osc_email($Qorder->value('customers_name'), $Qorder->value('customers_email_address'), $osC_Language->get('email_order_subject'), $email_order, STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS);
hpdl
554
476
477 // send emails to other people
478         if (SEND_EXTRA_ORDER_EMAILS_TO != '') {
hpdl
757
479           osc_email('', SEND_EXTRA_ORDER_EMAILS_TO, $osC_Language->get('email_order_subject'), $email_order, STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS);
hpdl
554
480         }
481       }
482
483       unset($Qorder);
484     }
485
hpdl
368
486     function &getListing($limit = null, $page_keyword = 'page') {
hpdl
383
487       global $osC_Database, $osC_Customer, $osC_Language;
hpdl
368
488
hpdl
443
489       $Qorders = $osC_Database->query('select o.orders_id, o.date_purchased, o.delivery_name, o.delivery_country, o.billing_name, o.billing_country, ot.text as order_total, s.orders_status_name from :table_orders o, :table_orders_total ot, :table_orders_status s where o.customers_id = :customers_id and o.orders_id = ot.orders_id and ot.class = "total" and o.orders_status = s.orders_status_id and s.language_id = :language_id order by orders_id desc');
hpdl
368
490       $Qorders->bindTable(':table_orders', TABLE_ORDERS);
491       $Qorders->bindTable(':table_orders_total', TABLE_ORDERS_TOTAL);
492       $Qorders->bindTable(':table_orders_status', TABLE_ORDERS_STATUS);
493       $Qorders->bindInt(':customers_id', $osC_Customer->getID());
hpdl
383
494       $Qorders->bindInt(':language_id', $osC_Language->getID());
hpdl
368
495
496       if (is_numeric($limit)) {
497         $Qorders->setBatchLimit(isset($_GET[$page_keyword]) && is_numeric($_GET[$page_keyword]) ? $_GET[$page_keyword] : 1, $limit);
498       }
499
500       $Qorders->execute();
501
502       return $Qorders;
503     }
504
505     function &getStatusListing($id = null) {
hpdl
383
506       global $osC_Database, $osC_Language;
hpdl
368
507
508       if ( ($id === null) && isset($this) ) {
509         $id = $this->_id;
510       }
511
512       $Qstatus = $osC_Database->query('select os.orders_status_name, osh.date_added, osh.comments from :table_orders_status os, :table_orders_status_history osh where osh.orders_id = :orders_id and osh.orders_status_id = os.orders_status_id and os.language_id = :language_id order by osh.date_added');
513       $Qstatus->bindTable(':table_orders_status', TABLE_ORDERS_STATUS);
514       $Qstatus->bindTable(':table_orders_status_history', TABLE_ORDERS_STATUS_HISTORY);
515       $Qstatus->bindInt(':orders_id', $id);
hpdl
383
516       $Qstatus->bindInt(':language_id', $osC_Language->getID());
hpdl
368
517
518       return $Qstatus;
519     }
520
521     function getCustomerID($id = null) {
522       global $osC_Database;
523
524       if ( ($id === null) && isset($this) ) {
525         $id = $this->_id;
526       }
527
528       $Qcustomer = $osC_Database->query('select customers_id from :table_orders where orders_id = :orders_id');
529       $Qcustomer->bindTable(':table_orders', TABLE_ORDERS);
530       $Qcustomer->bindInt(':orders_id', $id);
531       $Qcustomer->execute();
532
533       return $Qcustomer->valueInt('customers_id');
534     }
535
536     function numberOfEntries() {
537       global $osC_Database, $osC_Customer;
538       static $total_entries;
539
540       if (is_numeric($total_entries) === false) {
541         if ($osC_Customer->isLoggedOn()) {
542           $Qorders = $osC_Database->query('select count(*) as total from :table_orders where customers_id = :customers_id');
543           $Qorders->bindTable(':table_orders', TABLE_ORDERS);
544           $Qorders->bindInt(':customers_id', $osC_Customer->getID());
545           $Qorders->execute();
546
547           $total_entries = $Qorders->valueInt('total');
548         } else {
549           $total_entries = 0;
550         }
551       }
552
553       return $total_entries;
554     }
555
556     function numberOfProducts($id = null) {
557       global $osC_Database;
558
559       if ( ($id === null) && isset($this) ) {
560         $id = $this->_id;
561       }
562
563       $Qproducts = $osC_Database->query('select count(*) as total from :table_orders_products where orders_id = :orders_id');
564       $Qproducts->bindTable(':table_orders_products', TABLE_ORDERS_PRODUCTS);
565       $Qproducts->bindInt(':orders_id', $id);
566       $Qproducts->execute();
567
568       return $Qproducts->valueInt('total');
569     }
570
hpdl
554
571     function exists($id, $customer_id = null) {
572       global $osC_Database;
hpdl
368
573
hpdl
554
574       $Qorder = $osC_Database->query('select orders_id from :table_orders where orders_id = :orders_id');
hpdl
368
575
hpdl
554
576       if (isset($customer_id) && is_numeric($customer_id)) {
577         $Qorder->appendQuery('and customers_id = :customers_id');
578         $Qorder->bindInt(':customers_id', $customer_id);
579       }
580
581       $Qorder->appendQuery('limit 1');
582       $Qorder->bindTable(':table_orders', TABLE_ORDERS);
583       $Qorder->bindInt(':orders_id', $id);
584       $Qorder->execute();
585
586       return ($Qorder->numberOfRows() === 1);
587     }
588
hpdl
1
589     function query($order_id) {
hpdl
383
590       global $osC_Database, $osC_Language;
hpdl
1
591
hpdl
757
592       $Qorder = $osC_Database->query('select * from :table_orders where orders_id = :orders_id');
hpdl
20
593       $Qorder->bindTable(':table_orders', TABLE_ORDERS);
594       $Qorder->bindInt(':orders_id', $order_id);
595       $Qorder->execute();
hpdl
1
596
hpdl
20
597       $Qtotals = $osC_Database->query('select title, text, class from :table_orders_total where orders_id = :orders_id order by sort_order');
598       $Qtotals->bindTable(':table_orders_total', TABLE_ORDERS_TOTAL);
599       $Qtotals->bindInt(':orders_id', $order_id);
600       $Qtotals->execute();
hpdl
1
601
hpdl
20
602       $shipping_method_string = '';
603       $order_total_string = '';
hpdl
1
604
hpdl
20
605       while ($Qtotals->next()) {
606         $this->totals[] = array('title' => $Qtotals->value('title'),
607                                 'text' => $Qtotals->value('text'));
hpdl
1
608
hpdl
443
609         if ($Qtotals->value('class') == 'shipping') {
hpdl
20
610           $shipping_method_string = strip_tags($Qtotals->value('title'));
hpdl
1
611
hpdl
20
612           if (substr($shipping_method_string, -1) == ':') {
613             $shipping_method_string = substr($Qtotals->value('title'), 0, -1);
614           }
615         }
hpdl
1
616
hpdl
443
617         if ($Qtotals->value('class') == 'total') {
hpdl
20
618           $order_total_string = strip_tags($Qtotals->value('text'));
619         }
620       }
hpdl
1
621
hpdl
20
622       $Qstatus = $osC_Database->query('select orders_status_name from :table_orders_status where orders_status_id = :orders_status_id and language_id = :language_id');
623       $Qstatus->bindTable(':table_orders_status', TABLE_ORDERS_STATUS);
hpdl
46
624       $Qstatus->bindInt(':orders_status_id', $Qorder->valueInt('orders_status'));
hpdl
383
625       $Qstatus->bindInt(':language_id', $osC_Language->getID());
hpdl
20
626       $Qstatus->execute();
hpdl
1
627
hpdl
20
628       $this->info = array('currency' => $Qorder->value('currency'),
629                           'currency_value' => $Qorder->value('currency_value'),
630                           'payment_method' => $Qorder->value('payment_method'),
631                           'date_purchased' => $Qorder->value('date_purchased'),
632                           'orders_status' => $Qstatus->value('orders_status_name'),
633                           'last_modified' => $Qorder->value('last_modified'),
634                           'total' => $order_total_string,
635                           'shipping_method' => $shipping_method_string);
hpdl
1
636
hpdl
20
637       $this->customer = array('id' => $Qorder->valueInt('customers_id'),
638                               'name' => $Qorder->valueProtected('customers_name'),
639                               'company' => $Qorder->valueProtected('customers_company'),
640                               'street_address' => $Qorder->valueProtected('customers_street_address'),
641                               'suburb' => $Qorder->valueProtected('customers_suburb'),
642                               'city' => $Qorder->valueProtected('customers_city'),
643                               'postcode' => $Qorder->valueProtected('customers_postcode'),
644                               'state' => $Qorder->valueProtected('customers_state'),
hpdl
926
645                               'zone_code' => $Qorder->value('customers_state_code'),
646                               'country_title' => $Qorder->valueProtected('customers_country'),
hpdl
757
647                               'country_iso2' => $Qorder->value('customers_country_iso2'),
648                               'country_iso3' => $Qorder->value('customers_country_iso3'),
649                               'format' => $Qorder->value('customers_address_format'),
hpdl
20
650                               'telephone' => $Qorder->valueProtected('customers_telephone'),
651                               'email_address' => $Qorder->valueProtected('customers_email_address'));
652
653       $this->delivery = array('name' => $Qorder->valueProtected('delivery_name'),
654                               'company' => $Qorder->valueProtected('delivery_company'),
655                               'street_address' => $Qorder->valueProtected('delivery_street_address'),
656                               'suburb' => $Qorder->valueProtected('delivery_suburb'),
657                               'city' => $Qorder->valueProtected('delivery_city'),
658                               'postcode' => $Qorder->valueProtected('delivery_postcode'),
659                               'state' => $Qorder->valueProtected('delivery_state'),
hpdl
926
660                               'zone_code' => $Qorder->value('delivery_state_code'),
661                               'country_title' => $Qorder->valueProtected('delivery_country'),
hpdl
757
662                               'country_iso2' => $Qorder->value('delivery_country_iso2'),
663                               'country_iso3' => $Qorder->value('delivery_country_iso3'),
664                               'format' => $Qorder->value('delivery_address_format'));
hpdl
20
665
hpdl
1
666       if (empty($this->delivery['name']) && empty($this->delivery['street_address'])) {
667         $this->delivery = false;
668       }
669
hpdl
20
670       $this->billing = array('name' => $Qorder->valueProtected('billing_name'),
671                              'company' => $Qorder->valueProtected('billing_company'),
672                              'street_address' => $Qorder->valueProtected('billing_street_address'),
673                              'suburb' => $Qorder->valueProtected('billing_suburb'),
674                              'city' => $Qorder->valueProtected('billing_city'),
675                              'postcode' => $Qorder->valueProtected('billing_postcode'),
676                              'state' => $Qorder->valueProtected('billing_state'),
hpdl
926
677                              'zone_code' => $Qorder->value('billing_state_code'),
678                              'country_title' => $Qorder->valueProtected('billing_country'),
hpdl
757
679                              'country_iso2' => $Qorder->value('billing_country_iso2'),
680                              'country_iso3' => $Qorder->value('billing_country_iso3'),
681                              'format' => $Qorder->value('billing_address_format'));
hpdl
1
682
hpdl
20
683       $Qproducts = $osC_Database->query('select orders_products_id, products_id, products_name, products_model, products_price, products_tax, products_quantity, final_price from :table_orders_products where orders_id = :orders_id');
684       $Qproducts->bindTable(':table_orders_products', TABLE_ORDERS_PRODUCTS);
685       $Qproducts->bindInt(':orders_id', $order_id);
686       $Qproducts->execute();
687
hpdl
1
688       $index = 0;
689
hpdl
20
690       while ($Qproducts->next()) {
hpdl
1
691         $subindex = 0;
692
hpdl
20
693         $this->products[$index] = array('qty' => $Qproducts->valueInt('products_quantity'),
694                                         'id' => $Qproducts->valueInt('products_id'),
695                                         'name' => $Qproducts->value('products_name'),
696                                         'model' => $Qproducts->value('products_model'),
697                                         'tax' => $Qproducts->value('products_tax'),
698                                         'price' => $Qproducts->value('products_price'),
699                                         'final_price' => $Qproducts->value('final_price'));
700
701         $Qattributes = $osC_Database->query('select products_options, products_options_values, options_values_price, price_prefix from :table_orders_products_attributes where orders_id = :orders_id and orders_products_id = :orders_products_id');
702         $Qattributes->bindTable(':table_orders_products_attributes', TABLE_ORDERS_PRODUCTS_ATTRIBUTES);
703         $Qattributes->bindInt(':orders_id', $order_id);
704         $Qattributes->bindInt(':orders_products_id', $Qproducts->valueInt('orders_products_id'));
705         $Qattributes->execute();
706
707         if ($Qattributes->numberOfRows()) {
708           while ($Qattributes->next()) {
709             $this->products[$index]['attributes'][$subindex] = array('option' => $Qattributes->value('products_options'),
710                                                                      'value' => $Qattributes->value('products_options_values'),
711                                                                      'prefix' => $Qattributes->value('price_prefix'),
712                                                                      'price' => $Qattributes->value('options_values_price'));
713
hpdl
1
714             $subindex++;
715           }
716         }
717
718         $this->info['tax_groups']["{$this->products[$index]['tax']}"] = '1';
719
720         $index++;
721       }
722     }
723   }
724 ?>