Quick Search:

View

Revision:

Diff

Diff from 1862 to:

Annotations

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

Annotated File View

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