PageRenderTime 26ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/wordpress/wp-content/plugins/thecartpress/daos/Orders.class.php

http://ownerpress.googlecode.com/
PHP | 304 lines | 255 code | 25 blank | 24 comment | 7 complexity | bb2e78aedd1d1241be1740be7d7b63c0 MD5 | raw file
Possible License(s): Apache-2.0, AGPL-1.0, GPL-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * This file is part of TheCartPress.
  4. *
  5. * TheCartPress is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * TheCartPress is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with TheCartPress. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. require_once( 'OrdersDetails.class.php' );
  19. class Orders {
  20. public static $ORDER_PENDING = 'PENDING';
  21. public static $ORDER_PROCESSING = 'PROCESSING';
  22. public static $ORDER_COMPLETED = 'COMPLETED';
  23. public static $ORDER_CANCELLED = 'CANCELLED';
  24. public static $ORDER_SUSPENDED = 'SUSPENDED';
  25. static function createTable() {
  26. global $wpdb;
  27. $sql = 'CREATE TABLE IF NOT EXISTS `' . $wpdb->prefix . 'tcp_orders` (
  28. `order_id` bigint(20) unsigned NOT NULL auto_increment,
  29. `created_at` datetime NOT NULL,
  30. `customer_id` bigint(20) unsigned NOT NULL,
  31. `weight` int(11) NOT NULL default 0,
  32. `shipping_method` varchar(100) NOT NULL,
  33. `status` varchar(50) NOT NULL,
  34. `order_currency_code` char(3) NOT NULL,
  35. `shipping_amount` decimal(13, 2) NOT NULL default 0,
  36. `discount_amount` decimal(13, 2) NOT NULL default 0,
  37. `payment_name` varchar(20) NOT NULL,
  38. `payment_method` varchar(100) NOT NULL default \'\',
  39. `payment_amount` decimal(13, 2) NOT NULL default 0,
  40. `comment` varchar(250) NOT NULL,
  41. `comment_internal` varchar(250) NOT NULL default \'\',
  42. `code_tracking` varchar(50) NOT NULL,
  43. `shipping_firstname` varchar(50) NOT NULL,
  44. `shipping_lastname` varchar(100) NOT NULL,
  45. `shipping_company` varchar(50) NOT NULL,
  46. `shipping_street` varchar(100) NOT NULL,
  47. `shipping_city` varchar(100) NOT NULL,
  48. `shipping_city_id` int(11) unsigned NOT NULL default 0,
  49. `shipping_region` varchar(100) NOT NULL,
  50. `shipping_region_id` int(11) unsigned NOT NULL default 0,
  51. `shipping_postcode` char(6) NOT NULL,
  52. `shipping_country` varchar(50) NOT NULL,
  53. `shipping_country_id` char(2) NOT NULL,
  54. `shipping_telephone_1` varchar(50) NOT NULL,
  55. `shipping_telephone_2` varchar(50) NOT NULL,
  56. `shipping_fax` varchar(50) NOT NULL,
  57. `shipping_email` varchar(255) NOT NULL,
  58. `billing_firstname` varchar(50) NOT NULL,
  59. `billing_lastname` varchar(100) NOT NULL,
  60. `billing_company` varchar(50) NOT NULL,
  61. `billing_street` varchar(100) NOT NULL,
  62. `billing_city` varchar(100) NOT NULL default 0,
  63. `billing_city_id` int(11) unsigned NOT NULL,
  64. `billing_region` varchar(100) NOT NULL,
  65. `billing_region_id` int(11) unsigned NOT NULL default 0,
  66. `billing_postcode` char(6) NOT NULL,
  67. `billing_country` varchar(50) NOT NULL,
  68. `billing_country_id` char(2) NOT NULL,
  69. `billing_telephone_1` varchar(50) NOT NULL,
  70. `billing_telephone_2` varchar(50) NOT NULL,
  71. `billing_fax` varchar(50) NOT NULL,
  72. `billing_email` varchar(255) NOT NULL,
  73. PRIMARY KEY (`order_id`)
  74. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;';
  75. $wpdb->query( $sql );
  76. }
  77. static function get( $order_id ) {
  78. global $wpdb;
  79. return $wpdb->get_row( $wpdb->prepare( 'select * from ' . $wpdb->prefix . 'tcp_orders where order_id = %d', $order_id ) );
  80. }
  81. static function is_owner( $order_id, $customer_id ) {
  82. global $wpdb;
  83. $count = $wpdb->get_var( $wpdb->prepare( 'select count(*) from ' . $wpdb->prefix . 'tcp_orders where order_id = %d and customer_id = %d', $order_id, $customer_id ) );
  84. return $count == 1;
  85. }
  86. /**
  87. * Inserts an order.
  88. *
  89. * @param $order is an array with all the values of the table orders.
  90. */
  91. static function insert( $order ) {
  92. global $wpdb;
  93. $wpdb->insert($wpdb->prefix . 'tcp_orders', array(
  94. 'created_at' => $order['created_at'],
  95. 'customer_id' => $order['customer_id'],
  96. 'weight' => $order['weight'],
  97. 'shipping_method' => $order['shipping_method'],
  98. 'status' => $order['status'],
  99. 'order_currency_code' => $order['order_currency_code'],
  100. 'shipping_amount' => $order['shipping_amount'],
  101. 'discount_amount' => $order['discount_amount'],
  102. 'payment_name' => $order['payment_name'],
  103. 'payment_method' => $order['payment_method'],
  104. 'payment_amount' => $order['payment_amount'],
  105. 'comment' => $order['comment'],
  106. 'comment_internal' => $order['comment_internal'],
  107. 'code_tracking' => $order['code_tracking'],
  108. 'shipping_firstname' => $order['shipping_firstname'],
  109. 'shipping_lastname' => $order['shipping_lastname'],
  110. 'shipping_company' => $order['shipping_company'],
  111. 'shipping_street' => $order['shipping_street'],
  112. 'shipping_city' => $order['shipping_city'],
  113. 'shipping_city_id' => $order['shipping_city_id'],
  114. 'shipping_region' => $order['shipping_region'],
  115. 'shipping_region_id' => $order['shipping_region_id'],
  116. 'shipping_postcode' => $order['shipping_postcode'],
  117. 'shipping_country' => $order['shipping_country'],
  118. 'shipping_country_id' => $order['shipping_country_id'],
  119. 'shipping_telephone_1' => $order['shipping_telephone_1'],
  120. 'shipping_telephone_2' => $order['shipping_telephone_2'],
  121. 'shipping_fax' => $order['shipping_fax'],
  122. 'shipping_email' => $order['shipping_email'],
  123. 'billing_firstname' => $order['billing_firstname'],
  124. 'billing_lastname' => $order['billing_lastname'],
  125. 'billing_company' => $order['billing_company'],
  126. 'billing_street' => $order['billing_street'],
  127. 'billing_city' => $order['billing_city'],
  128. 'billing_city_id' => $order['billing_city_id'],
  129. 'billing_region' => $order['billing_region'],
  130. 'billing_region_id' => $order['billing_region_id'],
  131. 'billing_postcode' => $order['billing_postcode'],
  132. 'billing_country' => $order['billing_country'],
  133. 'billing_country_id' => $order['billing_country_id'],
  134. 'billing_telephone_1' => $order['billing_telephone_1'],
  135. 'billing_telephone_2' => $order['billing_telephone_2'],
  136. 'billing_fax' => $order['billing_fax'],
  137. 'billing_email' => $order['billing_email'],
  138. ), array('%s', '%d', '%d', '%s', '%s', '%s', '%f', '%f', '%s', '%s', '%f', '%s', '%s', '%s',
  139. '%s', '%s', '%s', '%s', '%s', '%d', '%s', '%d', '%s', '%s', '%s', '%s', '%s', '%s',
  140. '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%s', '%d', '%s', '%s', '%s', '%s', '%s',
  141. '%s', '%s')
  142. );
  143. return $wpdb->insert_id;
  144. }
  145. static function getCountPendingOrders( $customer_id = -1 ) {
  146. return Orders::getCountOrdersByStatus( $customer_id, Orders::$ORDER_PENDING );
  147. }
  148. static function getCountProcessingOrders( $customer_id = -1) {
  149. return Orders::getCountOrdersByStatus( $customer_id, Orders::$ORDER_PROCESSING );
  150. }
  151. static function getCountCompletedOrders( $customer_id = -1) {
  152. return Orders::getCountOrdersByStatus( $customer_id, Orders::$ORDER_COMPLETED );
  153. }
  154. static function getCountCancelledOrders( $customer_id = -1) {
  155. return Orders::getCountOrdersByStatus( $customer_id, Orders::$ORDER_CANCELLED );
  156. }
  157. static function getCountSuspendedOrders( $customer_id = -1) {
  158. return Orders::getCountOrdersByStatus( $customer_id, Orders::$ORDER_SUSPENDED );
  159. }
  160. static function getCountOrdersByStatus( $customer_id = -1, $status = 'PENDING' ) {
  161. global $wpdb;
  162. $sql = 'select count(*) from ' . $wpdb->prefix . 'tcp_orders where status=%s';
  163. if ( $customer_id > -1 ) $sql .= ' and customer_id = %d';
  164. return $wpdb->get_var( $wpdb->prepare( $sql, $status, $customer_id ) );
  165. }
  166. /**
  167. * Returns a join between orders and details orders
  168. */
  169. static function getOrders( $status = 'PENDING', $customer_id = -1 ) {
  170. global $wpdb;
  171. $sql = 'select o.order_id, od.order_detail_id, shipping_firstname,
  172. shipping_lastname, created_at, status, post_id, price, tax,
  173. qty_ordered, shipping_amount, payment_method, payment_amount,
  174. order_currency_code, code_tracking, is_downloadable,
  175. max_downloads, expires_at
  176. from ' . $wpdb->prefix . 'tcp_orders o left join '.
  177. $wpdb->prefix . 'tcp_orders_details od
  178. on o.order_id = od.order_id';
  179. if ( strlen( $status ) > 0 )
  180. if ( $customer_id > -1 )
  181. $sql = $wpdb->prepare( $sql . ' where status = %s and customer_id = %d', $status, $customer_id );
  182. else
  183. $sql = $wpdb->prepare( $sql . ' where status = %s', $status );
  184. elseif ( $customer_id > -1 )
  185. $sql = $wpdb->prepare( $sql . ' where customer_id = %d', $customer_id );
  186. $sql .= ' order by created_at desc';
  187. return $wpdb->get_results( $sql );
  188. }
  189. static function quickEdit( $order_id, $new_status, $new_code_tracking ) {
  190. global $wpdb;
  191. $wpdb->update( $wpdb->prefix . 'tcp_orders',
  192. array(
  193. 'status' => $new_status,
  194. 'code_tracking' => $new_code_tracking,
  195. ),
  196. array(
  197. 'order_id' => $order_id,
  198. ),
  199. array( '%s', '%s' ), array( '%d' ) );
  200. Orders::edit_downloadable_details( $order_id, $new_status );
  201. }
  202. static function edit( $order_id, $new_status, $new_code_tracking, $comment, $comment_internal ) {
  203. global $wpdb;
  204. $wpdb->update( $wpdb->prefix . 'tcp_orders',
  205. array(
  206. 'status' => $new_status,
  207. 'code_tracking' => $new_code_tracking,
  208. 'comment' => $comment,
  209. 'comment_internal' => $comment_internal,
  210. ),
  211. array(
  212. 'order_id' => $order_id,
  213. ),
  214. array( '%s', '%s', '%s', '%s' ), array( '%d' ) );
  215. Orders::edit_downloadable_details( $order_id, $new_status );
  216. }
  217. static function editStatus( $order_id, $new_status, $comment_internal = '' ) {
  218. global $wpdb;
  219. $wpdb->update( $wpdb->prefix . 'tcp_orders',
  220. array(
  221. 'status' => $new_status,
  222. 'comment_internal' => $comment_internal,
  223. ),
  224. array(
  225. 'order_id' => $order_id,
  226. ),
  227. array( '%s', '%s' ), array( '%d' ) );
  228. Orders::edit_downloadable_details( $order_id, $new_status );
  229. }
  230. static function edit_downloadable_details( $order_id, $new_status ) {
  231. if ( $new_status == Orders::$ORDER_COMPLETED ) {
  232. $details = OrdersDetails::getDetails( $order_id );
  233. foreach( $details as $detail ) {
  234. $days_to_expire = get_post_meta( $detail->post_id, 'tcp_days_to_expire', true );
  235. if ( $days_to_expire > 0 ) {
  236. $today = date( 'Y-m-d' );
  237. $expires_at = date ( 'Y-m-d', strtotime( date( 'Y-m-d', strtotime( $today ) ) . " +$days_to_expire day" ) );
  238. OrdersDetails::edit_downloadable_data( $detail->order_detail_id, $expires_at );
  239. }
  240. }
  241. }
  242. }
  243. static function isDownloadable( $order_id ) {
  244. return OrdersDetails::isDownloadable( $order_id );
  245. }
  246. static function getProductsDownloadables( $customer_id ) {
  247. global $wpdb;
  248. $today = date ( 'Y-m-d' );
  249. $max_date = date ( 'Y-m-d', mktime( 0, 0, 0, 1, 1, 2000 ) );
  250. $sql = $wpdb->prepare( 'select o.order_id as order_id, order_detail_id, post_id, expires_at, max_downloads from
  251. ' . $wpdb->prefix . 'tcp_orders o left join ' . $wpdb->prefix . 'tcp_orders_details d
  252. on o.order_id = d.order_id
  253. where customer_id = %d and d.is_downloadable = \'Y\' and status=\'COMPLETED\'
  254. and ( ( d.expires_at > %s and ( d.max_downloads = -1 or d.max_downloads > 0 ) )
  255. or ( d.expires_at = %s and ( d.max_downloads > 0 or d.max_downloads = -1 ) ) )'
  256. , $customer_id, $today, $max_date );
  257. return $wpdb->get_results( $sql );
  258. }
  259. static function isProductDownloadable( $customer_id, $orders_details_id ) {
  260. global $wpdb;
  261. $today = date ( 'Y-m-d' );
  262. $max_date = date ( 'Y-m-d', mktime( 0, 0, 0, 1, 1, 2000 ) );
  263. $sql = $wpdb->prepare( 'select count(*) from ' . $wpdb->prefix . 'tcp_orders o
  264. left join ' . $wpdb->prefix . 'tcp_orders_details d on o.order_id = d.order_id
  265. where customer_id = %d and order_detail_id = %d and d.is_downloadable = \'Y \' and status=\'COMPLETED\'
  266. and ( ( d.expires_at > %s and ( d.max_downloads = -1 or d.max_downloads > 0 ) )
  267. or ( d.expires_at = %s and ( d.max_downloads > 0 or d.max_downloads = -1 ) ) )'
  268. , $customer_id, $orders_details_id, $today, $max_date );
  269. $count = $wpdb->get_var( $sql );
  270. return $count > 0;
  271. }
  272. static function takeAwayDownload( $order_detail_id ) {
  273. global $wpdb;
  274. $sql = 'update ' . $wpdb->prefix . 'tcp_orders_details set
  275. max_downloads = max_downloads - 1 where order_detail_id = %d';
  276. $wpdb->query( $wpdb->prepare( $sql, $order_detail_id ) );
  277. }
  278. }
  279. ?>