PageRenderTime 67ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/administrator/components/com_virtuemart/classes/ps_order.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 917 lines | 618 code | 130 blank | 169 comment | 112 complexity | ee588f254b70c27d0ec1db9509cfdebe MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );
  3. /**
  4. *
  5. * @version $Id: ps_order.php 1475 2008-07-16 17:35:35Z soeren_nb $
  6. * @package VirtueMart
  7. * @subpackage classes
  8. * @copyright Copyright (C) 2004-2008 soeren - All rights reserved.
  9. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
  10. * VirtueMart is free software. This version may have been modified pursuant
  11. * to the GNU General Public License, and as distributed it includes or
  12. * is derivative of works licensed under the GNU General Public License or
  13. * other free or open source software licenses.
  14. * See /administrator/components/com_virtuemart/COPYRIGHT.php for copyright notices and details.
  15. *
  16. * http://virtuemart.net
  17. */
  18. require_once(CLASSPATH.'ps_user.php');
  19. require_once(CLASSPATH.'ps_userfield.php');
  20. require_once(CLASSPATH.'ps_checkout.php');
  21. require_once(CLASSPATH.'ps_order_status.php');
  22. require_once(CLASSPATH.'ps_order_booking.php');
  23. /**
  24. * The class handles orders from an adminstrative perspective. Order
  25. * processing is handled in the ps_checkout class.
  26. */
  27. class ps_order {
  28. /**
  29. * Changes the status of an order
  30. * @author pablo
  31. * @author soeren
  32. * @author Uli
  33. *
  34. *
  35. * @param array $d
  36. * @return boolean
  37. */
  38. function order_status_update(&$d) {
  39. global $mosConfig_offset, $ps_booking;
  40. $db = new ps_DB;
  41. $timestamp = time() + ($mosConfig_offset*60*60);
  42. $mysqlDatetime = date("Y-m-d G:i:s",$timestamp);
  43. if( empty($_REQUEST['include_comment'])) {
  44. $include_comment="N";
  45. }
  46. // get the current order status
  47. $curr_order_status = @$d["current_order_status"];
  48. $notify_customer = empty($d['notify_customer']) ? "N" : $d['notify_customer'];
  49. if( $notify_customer=="Y" ) {
  50. $notify_customer=1;
  51. }
  52. else {
  53. $notify_customer=0;
  54. }
  55. $d['order_comment'] = empty($d['order_comment']) ? "" : $d['order_comment'];
  56. if( empty($d['order_item_id']) ) {
  57. // When the order is set to "confirmed", we can capture
  58. // the Payment with authorize.net
  59. if( $curr_order_status=="P" && $d["order_status"]=="C") {
  60. $q = "SELECT order_number,payment_class,order_payment_trans_id FROM #__{vm}_payment_method,#__{vm}_order_payment,#__{vm}_orders WHERE ";
  61. $q .= "#__{vm}_order_payment.order_id='".$db->getEscaped($d['order_id'])."' ";
  62. $q .= "AND #__{vm}_orders.order_id='".$db->getEscaped($d['order_id'])."' ";
  63. $q .= "AND #__{vm}_order_payment.payment_method_id=#__{vm}_payment_method.payment_method_id";
  64. $db->query( $q );
  65. $db->next_record();
  66. $payment_class = $db->f("payment_class");
  67. if( $payment_class=="ps_authorize" ) {
  68. require_once( CLASSPATH."payment/ps_authorize.cfg.php");
  69. if( AN_TYPE == 'AUTH_ONLY' ) {
  70. require_once( CLASSPATH."payment/ps_authorize.php");
  71. $authorize = new ps_authorize();
  72. $d["order_number"] = $db->f("order_number");
  73. if( !$authorize->capture_payment( $d )) {
  74. return false;
  75. }
  76. }
  77. }
  78. }
  79. /*
  80. * This is like the test above for delayed capture only
  81. * we (well, I - durian) don't think the credit card
  82. * should be captured until the item(s) are shipped.
  83. * In fact, VeriSign says not to capture the cards until
  84. * the item ships. Maybe this behavior should be a
  85. * configurable item?
  86. *
  87. * When the order changes from Confirmed or Pending to
  88. * Shipped, perform the delayed capture.
  89. *
  90. * Restricted to PayFlow Pro for now.
  91. */
  92. if( ($curr_order_status=="P" || $curr_order_status=="C") && $d["order_status"]=="S") {
  93. $q = "SELECT order_number,payment_class,order_payment_trans_id FROM #__{vm}_payment_method,#__{vm}_order_payment,#__{vm}_orders WHERE ";
  94. $q .= "#__{vm}_order_payment.order_id='".$db->getEscaped($d['order_id'])."' ";
  95. $q .= "AND #__{vm}_orders.order_id='".$db->getEscaped($d['order_id'])."' ";
  96. $q .= "AND #__{vm}_order_payment.payment_method_id=#__{vm}_payment_method.payment_method_id";
  97. $db->query( $q );
  98. $db->next_record();
  99. $payment_class = $db->f("payment_class");
  100. if( $payment_class=="ps_pfp" ) {
  101. require_once( CLASSPATH."payment/ps_pfp.cfg.php");
  102. if( PFP_TYPE == 'A' ) {
  103. require_once( CLASSPATH."payment/ps_pfp.php");
  104. $pfp = new ps_pfp();
  105. $d["order_number"] = $db->f("order_number");
  106. if( !$pfp->capture_payment( $d )) {
  107. return false;
  108. }
  109. }
  110. }
  111. }
  112. /*
  113. * If a pending order gets cancelled, void the authorization.
  114. *
  115. * It might work on captured cards too, if we want to
  116. * void shipped orders.
  117. *
  118. * Restricted to PayFlow Pro for now.
  119. */
  120. if( $curr_order_status=="P" && $d["order_status"]=="X") {
  121. $q = "SELECT order_number,payment_class,order_payment_trans_id FROM #__{vm}_payment_method,#__{vm}_order_payment,#__{vm}_orders WHERE ";
  122. $q .= "#__{vm}_order_payment.order_id='".$db->getEscaped($d['order_id'])."' ";
  123. $q .= "AND #__{vm}_orders.order_id='".$db->getEscaped($d['order_id'])."' ";
  124. $q .= "AND #__{vm}_order_payment.payment_method_id=#__{vm}_payment_method.payment_method_id";
  125. $db->query( $q );
  126. $db->next_record();
  127. $payment_class = $db->f("payment_class");
  128. if( $payment_class=="ps_pfp" ) {
  129. require_once( CLASSPATH."payment/ps_pfp.cfg.php");
  130. if( PFP_TYPE == 'A' ) {
  131. require_once( CLASSPATH."payment/ps_pfp.php");
  132. $pfp = new ps_pfp();
  133. $d["order_number"] = $db->f("order_number");
  134. if( !$pfp->void_authorization( $d )) {
  135. return false;
  136. }
  137. }
  138. }
  139. }
  140. $fields =array( 'order_status'=> $d["order_status"], 'mdate'=> $timestamp );
  141. $db->buildQuery('UPDATE', '#__{vm}_orders', $fields, "WHERE order_id='" . $db->getEscaped($d["order_id"]) . "'");
  142. $db->query();
  143. if($my->id == 62) echo $db->_database->_sql;
  144. /**
  145. * VirtueBook: Load the booking data so that we can serialize it with the history
  146. */
  147. $ps_booking->loadBookingData($d["order_id"]);
  148. // Update the Order History.
  149. $fields = array( 'order_id' => $d["order_id"],
  150. 'order_status_code' => $d["order_status"],
  151. 'date_added' => $mysqlDatetime,
  152. 'customer_notified' => $notify_customer,
  153. 'comments' => $d['order_comment'],
  154. 'booking_serialized' => serialize($ps_booking)
  155. );
  156. $db->buildQuery('INSERT', '#__{vm}_order_history', $fields );
  157. $db->query();
  158. // Do we need to re-update the Stock Level?
  159. if( (strtoupper($d["order_status"]) == "X" || strtoupper($d["order_status"])=="R")
  160. // && CHECK_STOCK == '1'
  161. && $curr_order_status != $d["order_status"]
  162. ) {
  163. // Get the order items and update the stock level
  164. // to the number before the order was placed
  165. $q = "SELECT product_id, product_quantity FROM #__{vm}_order_item WHERE order_id='".$db->getEscaped($d["order_id"])."'";
  166. $db->query( $q );
  167. $dbu = new ps_DB;
  168. // Now update each ordered product
  169. while( $db->next_record() ) {
  170. $q = "UPDATE #__{vm}_product
  171. SET product_in_stock=product_in_stock+".$db->f("product_quantity").",
  172. product_sales=product_sales-".$db->f("product_quantity")."
  173. WHERE product_id='".$db->f("product_id")."'";
  174. $dbu->query( $q );
  175. }
  176. }
  177. // Update the Order Items' status
  178. $q = "SELECT order_item_id FROM #__{vm}_order_item WHERE order_id=".$db->getEscaped($d['order_id']);
  179. $db->query($q);
  180. $dbu = new ps_DB;
  181. while ($db->next_record()) {
  182. $item_id = $db->f("order_item_id");
  183. $fields =array( 'order_status'=> $d["order_status"],
  184. 'mdate'=> $timestamp );
  185. $dbu->buildQuery('UPDATE', '#__{vm}_order_item', $fields, "WHERE order_item_id='" .(int)$item_id . "'");
  186. $dbu->query();
  187. }
  188. if (ENABLE_DOWNLOADS == '1') {
  189. ##################
  190. ## DOWNLOAD MOD
  191. $this->mail_download_id( $d );
  192. }
  193. if( !empty($notify_customer) ) {
  194. $this->notify_customer( $d );
  195. }
  196. } elseif( !empty($d['order_item_id'])) {
  197. $fields =array( 'order_status'=> $d["order_status"],
  198. 'mdate'=> $timestamp );
  199. $db->buildQuery('UPDATE', '#__{vm}_order_item', $fields, 'WHERE order_item_id='.intval( $d['order_item_id'] ));
  200. return $db->query() !== false;
  201. }
  202. return true;
  203. }
  204. /**
  205. * mails the Download-ID to the customer
  206. * or deletes the Download-ID from the product_downloads table
  207. *
  208. * @param array $d
  209. * @return boolean
  210. */
  211. function mail_download_id( &$d ){
  212. global $sess, $VM_LANG, $vmLogger;
  213. $url = URL."index.php?option=com_virtuemart&page=shop.downloads&Itemid=".$sess->getShopItemid();
  214. $db = new ps_DB();
  215. $db->query( 'SELECT order_status FROM #__{vm}_orders WHERE order_id='.(int)$d['order_id'] );
  216. $db->next_record();
  217. if ($db->f("order_status")==ENABLE_DOWNLOAD_STATUS) {
  218. $dbw = new ps_DB;
  219. $q = "SELECT order_id,user_id,download_id,file_name FROM #__{vm}_product_download WHERE";
  220. $q .= " order_id = '" . (int)$d["order_id"] . "'";
  221. $dbw->query($q);
  222. $dbw->next_record();
  223. $userid = $dbw->f("user_id");
  224. $download_id = $dbw->f("download_id");
  225. $datei=$dbw->f("file_name");
  226. $dbw->reset();
  227. if ($download_id) {
  228. $dbv = new ps_DB;
  229. $q = "SELECT * FROM #__{vm}_vendor WHERE vendor_id='1'";
  230. $dbv->query($q);
  231. $dbv->next_record();
  232. $db = new ps_DB;
  233. $q="SELECT first_name,last_name, user_email FROM #__{vm}_user_info WHERE user_id = '$userid' AND address_type='BT'";
  234. $db->query($q);
  235. $db->next_record();
  236. $message = $VM_LANG->_('HI',false) .' '. $db->f("first_name") .($db->f("middle_name")?' '.$db->f("middle_name") : '' ). ' ' . $db->f("last_name") . ",\n\n";
  237. $message .= $VM_LANG->_('PHPSHOP_DOWNLOADS_SEND_MSG_1',false).".\n";
  238. $message .= $VM_LANG->_('PHPSHOP_DOWNLOADS_SEND_MSG_2',false)."\n\n";
  239. while($dbw->next_record()) {
  240. $message .= $dbw->f("file_name").": ".$dbw->f("download_id")
  241. . "\n$url&download_id=".$dbw->f("download_id")."\n\n";
  242. }
  243. $message .= $VM_LANG->_('PHPSHOP_DOWNLOADS_SEND_MSG_3',false) . DOWNLOAD_MAX."\n";
  244. $expire = ((DOWNLOAD_EXPIRE / 60) / 60) / 24;
  245. $message .= str_replace("{expire}", $expire, $VM_LANG->_('PHPSHOP_DOWNLOADS_SEND_MSG_4',false));
  246. $message .= "\n\n____________________________________________________________\n";
  247. $message .= $VM_LANG->_('PHPSHOP_DOWNLOADS_SEND_MSG_5',false)."\n";
  248. $message .= $dbv->f("vendor_name") . " \n" . URL."\n\n".$dbv->f("contact_email") . "\n";
  249. $message .= "____________________________________________________________\n";
  250. $message .= $VM_LANG->_('PHPSHOP_DOWNLOADS_SEND_MSG_6',false) . $dbv->f("vendor_name");
  251. $mail_Body = $message;
  252. $mail_Subject = $VM_LANG->_('PHPSHOP_DOWNLOADS_SEND_SUBJ',false);
  253. $result = vmMail( $dbv->f("contact_email"), $dbv->f("vendor_name"),
  254. $db->f("user_email"), $mail_Subject, $mail_Body, '' );
  255. if ($result) {
  256. $vmLogger->info( $VM_LANG->_('PHPSHOP_DOWNLOADS_SEND_MSG',false). " ". $db->f("first_name") . " " . $db->f("last_name") . " ".$db->f("user_email") );
  257. }
  258. else {
  259. $vmLogger->warning( $VM_LANG->_('PHPSHOP_DOWNLOADS_ERR_SEND',false)." ". $db->f("first_name") . " " . $db->f("last_name") . ", ".$db->f("user_email") );
  260. }
  261. }
  262. }
  263. elseif ($d["order_status"]==DISABLE_DOWNLOAD_STATUS) {
  264. $q = "DELETE FROM #__{vm}_product_download WHERE order_id=" . (int)$d["order_id"];
  265. $db->query($q);
  266. $db->next_record();
  267. }
  268. return true;
  269. }
  270. /**
  271. * notifies the customer that the Order Status has been changed
  272. *
  273. * @param array $d
  274. */
  275. function notify_customer( &$d ){
  276. global $sess, $VM_LANG, $vmLogger;
  277. $url = SECUREURL."index.php?option=com_virtuemart&page=account.order_details&order_id=".urlencode($d["order_id"]).'&Itemid='.$sess->getShopItemid();
  278. $db = new ps_DB;
  279. $dbv = new ps_DB;
  280. $q = "SELECT vendor_name,contact_email FROM #__{vm}_vendor ";
  281. $q .= "WHERE vendor_id='".$_SESSION['ps_vendor_id']."'";
  282. $dbv->query($q);
  283. $dbv->next_record();
  284. $q = "SELECT first_name,last_name,user_email,order_status_name FROM #__{vm}_order_user_info,#__{vm}_orders,#__{vm}_order_status ";
  285. $q .= "WHERE #__{vm}_orders.order_id = '".$db->getEscaped($d["order_id"])."' ";
  286. $q .= "AND #__{vm}_orders.user_id = #__{vm}_order_user_info.user_id ";
  287. $q .= "AND #__{vm}_orders.order_id = #__{vm}_order_user_info.order_id ";
  288. $q .= "AND order_status = order_status_code ";
  289. $db->query($q);
  290. $db->next_record();
  291. // MAIL BODY
  292. $message = $VM_LANG->_('HI',false) .' '. $db->f("first_name") . ($db->f("middle_name")?' '.$db->f("middle_name") : '' ). ' ' . $db->f("last_name") . ",\n\n";
  293. $message .= $VM_LANG->_('PHPSHOP_ORDER_STATUS_CHANGE_SEND_MSG_1',false)."\n\n";
  294. if( !empty($d['include_comment']) && !empty($d['order_comment']) ) {
  295. $message .= $VM_LANG->_('PHPSHOP_ORDER_HISTORY_COMMENT_EMAIL',false).":\n";
  296. $message .= $d['order_comment'];
  297. $message .= "\n____________________________________________________________\n\n";
  298. }
  299. $message .= $VM_LANG->_('PHPSHOP_ORDER_STATUS_CHANGE_SEND_MSG_2',false)."\n";
  300. $message .= "____________________________________________________________\n\n";
  301. $message .= $db->f("order_status_name");
  302. if( VM_REGISTRATION_TYPE != 'NO_REGISTRATION' ) {
  303. $message .= "\n____________________________________________________________\n\n";
  304. $message .= $VM_LANG->_('PHPSHOP_ORDER_STATUS_CHANGE_SEND_MSG_3',false)."\n";
  305. $message .= $url;
  306. }
  307. $message .= "\n\n____________________________________________________________\n";
  308. $message .= $dbv->f("vendor_name") . " \n";
  309. $message .= URL."\n";
  310. $message .= $dbv->f("contact_email");
  311. $message = str_replace( "{order_id}", $d["order_id"], $message );
  312. $mail_Body = html_entity_decode($message);
  313. $mail_Subject = str_replace( "{order_id}", $d["order_id"], $VM_LANG->_('PHPSHOP_ORDER_STATUS_CHANGE_SEND_SUBJ',false));
  314. $result = vmMail( $dbv->f("contact_email"), $dbv->f("vendor_name"),
  315. $db->f("user_email"), $mail_Subject, $mail_Body, '' );
  316. /* Send the email */
  317. if ($result) {
  318. $vmLogger->info( $VM_LANG->_('PHPSHOP_DOWNLOADS_SEND_MSG',false). " ". $db->f("first_name") . " " . $db->f("last_name") . ", ".$db->f("user_email") );
  319. }
  320. else {
  321. $vmLogger->warning( $VM_LANG->_('PHPSHOP_DOWNLOADS_ERR_SEND',false).' '. $db->f("first_name") . " " . $db->f("last_name") . ", ".$db->f("user_email")." (". $result->ErrorInfo.")" );
  322. }
  323. }
  324. /**
  325. * This function inserts the DOWNLOAD IDs for all files associated with this product
  326. * so the customer can later download the purchased files
  327. * @static
  328. * @since 1.1.0
  329. * @param int $product_id
  330. * @param int $order_id
  331. * @param int $user_id
  332. */
  333. function insert_downloads_for_product( &$d ) {
  334. $db = new ps_DB();
  335. $dbd = new ps_DB();
  336. if( empty( $d['product_id'] ) || empty( $d['order_id'] )) {
  337. return false;
  338. }
  339. $dl = "SELECT attribute_name,attribute_value ";
  340. $dl .= "FROM #__{vm}_product_attribute WHERE product_id='".$d['product_id']."'";
  341. $dl .= " AND attribute_name='download'";
  342. $db->query($dl);
  343. $dlnum = 0;
  344. while($db->next_record()) {
  345. $str = (int)$d['order_id'];
  346. $str .= $d['product_id'];
  347. $str .= uniqid('download_');
  348. $str .= $dlnum++;
  349. $str .= time();
  350. $download_id = md5($str);
  351. $fields = array('product_id' => $d['product_id'],
  352. 'user_id' => (int)$d['user_id'],
  353. 'order_id' => (int)$d['order_id'],
  354. 'end_date' => '0',
  355. 'download_max' => DOWNLOAD_MAX,
  356. 'download_id' => $download_id,
  357. 'file_name' => $db->f("attribute_value")
  358. );
  359. $dbd->buildQuery('INSERT', '#__{vm}_product_download', $fields );
  360. $dbd->query();
  361. }
  362. }
  363. /**
  364. * Handles a download Request
  365. *
  366. * @param array $d
  367. * @return boolean
  368. */
  369. function download_request(&$d) {
  370. global $download_id, $VM_LANG, $vmLogger;
  371. $db = new ps_DB;
  372. $download_id = $db->getEscaped( vmGet( $d, "download_id" ) );
  373. $q = "SELECT * FROM #__{vm}_product_download WHERE";
  374. $q .= " download_id = '$download_id'";
  375. $db->query($q);
  376. $db->next_record();
  377. $download_id = $db->f("download_id");
  378. $file_name = $db->f("file_name");
  379. if( strncmp($file_name, 'http', 4 ) !== 0) {
  380. $datei = DOWNLOADROOT . $file_name;
  381. } else {
  382. $datei = $file_name;
  383. }
  384. $download_max = $db->f("download_max");
  385. $end_date = $db->f("end_date");
  386. $zeit=time();
  387. if (!$download_id) {
  388. $vmLogger->err( $VM_LANG->_('PHPSHOP_DOWNLOADS_ERR_INV',false) );
  389. return false;
  390. //vmRedirect("index.php?option=com_virtuemart&page=shop.downloads", $d["error"]);
  391. }
  392. elseif ($download_max=="0") {
  393. $q ="DELETE FROM #__{vm}_product_download";
  394. $q .=" WHERE download_id = '" . $download_id . "'";
  395. $db->query($q);
  396. $db->next_record();
  397. $vmLogger->err( $VM_LANG->_('PHPSHOP_DOWNLOADS_ERR_MAX',false) );
  398. return false;
  399. //vmRedirect("index.php?option=com_virtuemart&page=shop.downloads", $d["error"]);
  400. }
  401. elseif ($end_date!="0" && $zeit > $end_date) {
  402. $q ="DELETE FROM #__{vm}_product_download";
  403. $q .=" WHERE download_id = '" . $download_id . "'";
  404. $db->query($q);
  405. $db->next_record();
  406. $vmLogger->err( $VM_LANG->_('PHPSHOP_DOWNLOADS_ERR_EXP',false) );
  407. return false;
  408. //vmRedirect("index.php?option=com_virtuemart&page=shop.downloads", $d["error"]);
  409. }
  410. require_once(CLASSPATH.'connectionTools.class.php');
  411. $download_count = true;
  412. if ( @file_exists( $datei ) ){
  413. // Check if this is a request for a special range of the file (=Resume Download)
  414. $range_request = vmConnector::http_rangeRequest( filesize($datei), false );
  415. if( $range_request[0] == 0 ) {
  416. // this is not a request to resume a download,
  417. $download_count = true;
  418. } else {
  419. $download_count = false;
  420. }
  421. } else {
  422. $download_count = false;
  423. }
  424. // Parameter to check if the file should be removed after download, which is only true,
  425. // if we have a remote file, which was transferred to this server into a temporary file
  426. $unlink = false;
  427. if( strncmp($datei, 'http', 4 ) === 0) {
  428. require_once( CLASSPATH.'ps_product_files.php');
  429. $datei_local = ps_product_files::getRemoteFile($datei);
  430. if( $datei_local !== false ) {
  431. $datei = $datei_local;
  432. $unlink = true;
  433. } else {
  434. $vmLogger->err( $VM_LANG->_('VM_DOWNLOAD_FILE_NOTFOUND',false) );
  435. return false;
  436. }
  437. }
  438. else {
  439. // Check, if file path is correct
  440. // and file is
  441. if ( !@file_exists( $datei ) ){
  442. $vmLogger->err( $VM_LANG->_('VM_DOWNLOAD_FILE_NOTFOUND',false) );
  443. return false;
  444. //vmRedirect("index.php?option=com_virtuemart&page=shop.downloads", $d["error"]);
  445. }
  446. if ( !@is_readable( $datei ) ) {
  447. $vmLogger->err( $VM_LANG->_('VM_DOWNLOAD_FILE_NOTREADABLE',false) );
  448. return false;
  449. //vmRedirect("index.php?option=com_virtuemart&page=shop.downloads", $d["error"]);
  450. }
  451. }
  452. if( $download_count ) {
  453. // decrement the download_max to limit the number of downloads
  454. $q ="UPDATE `#__{vm}_product_download` SET";
  455. $q .=" `download_max`=`download_max` - 1";
  456. $q .=" WHERE download_id = '" .$download_id. "'";
  457. $db->query($q);
  458. $db->next_record();
  459. }
  460. if ($end_date=="0") {
  461. // Set the Download Expiry Date, so the download can expire after DOWNLOAD_EXPIRE seconds
  462. $end_date=time('u') + DOWNLOAD_EXPIRE;
  463. $q ="UPDATE #__{vm}_product_download SET";
  464. $q .=" end_date=$end_date";
  465. $q .=" WHERE download_id = '" . $download_id . "'";
  466. $db->query($q);
  467. $db->next_record();
  468. }
  469. if (ereg('Opera(/| )([0-9].[0-9]{1,2})', $_SERVER['HTTP_USER_AGENT'])) {
  470. $UserBrowser = "Opera";
  471. }
  472. elseif (ereg('MSIE ([0-9].[0-9]{1,2})', $_SERVER['HTTP_USER_AGENT'])) {
  473. $UserBrowser = "IE";
  474. } else {
  475. $UserBrowser = '';
  476. }
  477. $mime_type = ($UserBrowser == 'IE' || $UserBrowser == 'Opera') ? 'application/octetstream' : 'application/octet-stream';
  478. // dump anything in the buffer
  479. while( @ob_end_clean() );
  480. vmConnector::sendFile( $datei, $mime_type, basename($file_name) );
  481. if( $unlink ) {
  482. // remove the temporarily downloaded remote file
  483. @unlink( $datei );
  484. }
  485. $GLOBALS['vm_mainframe']->close(true);
  486. }
  487. /**
  488. * Shows the list of the orders of a user in the account mainenance section
  489. *
  490. * @param string $order_status Filter by order status (A=all, C=confirmed, P=pending,...)
  491. * @param int $secure Restrict the order list to a specific user id (=1) or not (=0)?
  492. */
  493. function list_order($order_status='A', $secure=0 ) {
  494. global $VM_LANG, $CURRENCY_DISPLAY, $sess, $limit, $limitstart, $keyword, $mm_action_url;
  495. $ps_vendor_id = $_SESSION["ps_vendor_id"];
  496. $auth = $_SESSION['auth'];
  497. require_once( CLASSPATH .'ps_order_status.php');
  498. require_once( CLASSPATH .'htmlTools.class.php');
  499. require_once( CLASSPATH .'pageNavigation.class.php');
  500. $db = new ps_DB;
  501. $dbs = new ps_DB;
  502. $listfields = 'cdate,order_total,order_status,order_id,order_currency';
  503. $countfields = 'count(*) as num_rows';
  504. $count = "SELECT $countfields FROM #__{vm}_orders ";
  505. $list = "SELECT $listfields FROM #__{vm}_orders ";
  506. $q = "WHERE vendor_id='$ps_vendor_id' ";
  507. if ($order_status != "A") {
  508. $q .= "AND order_status='$order_status' ";
  509. }
  510. if ($secure) {
  511. $q .= "AND user_id='" . $auth["user_id"] . "' ";
  512. }
  513. if( !empty( $keyword )) {
  514. $q .= "AND (order_id LIKE '%".$keyword."%' ";
  515. $q .= "OR order_number LIKE '%".$keyword."%' ";
  516. $q .= "OR order_total LIKE '%".$keyword."%') ";
  517. }
  518. $q .= "ORDER BY cdate DESC";
  519. $count .= $q;
  520. $db->query($count);
  521. $db->next_record();
  522. $num_rows = $db->f('num_rows');
  523. if( $num_rows == 0 ) {
  524. echo "<span style=\"font-style:italic;\">".$VM_LANG->_('PHPSHOP_ACC_NO_ORDERS')."</span>\n";
  525. return;
  526. }
  527. $pageNav = new vmPageNav( $num_rows, $limitstart, $limit );
  528. $list .= $q .= " LIMIT ".$pageNav->limitstart.", $limit ";
  529. $db->query( $list );
  530. $listObj = new listFactory( $pageNav );
  531. if( $num_rows > 0 ) {
  532. // print out the search field and a list heading
  533. $listObj->writeSearchHeader( '', '', 'account', 'index');
  534. }
  535. // start the list table
  536. $listObj->startTable();
  537. $listObj->writeTableHeader( 3 );
  538. while ($db->next_record()) {
  539. $order_status = ps_order_status::getOrderStatusName($db->f("order_status"));
  540. $listObj->newRow();
  541. $tmp_cell = "<a href=\"". $sess->url( $mm_action_url."index.php?page=account.order_details&order_id=".$db->f("order_id") )."\">\n";
  542. $tmp_cell .= "<img src=\"".IMAGEURL."ps_image/goto.png\" height=\"32\" width=\"32\" align=\"middle\" border=\"0\" alt=\"".$VM_LANG->_('PHPSHOP_ORDER_LINK')."\" />&nbsp;".$VM_LANG->_('PHPSHOP_VIEW')."</a><br />";
  543. $listObj->addCell( $tmp_cell );
  544. $tmp_cell = "<strong>".$VM_LANG->_('PHPSHOP_ORDER_PRINT_PO_DATE').":</strong> " . strftime("%d. %B %Y", $db->f("cdate"));
  545. $tmp_cell .= "<br /><strong>".$VM_LANG->_('PHPSHOP_ORDER_PRINT_TOTAL').":</strong> " . $CURRENCY_DISPLAY->getFullValue($db->f("order_total"), '', $db->f('order_currency'));
  546. $listObj->addCell( $tmp_cell );
  547. $tmp_cell = "<strong>".$VM_LANG->_('PHPSHOP_ORDER_PRINT_PO_STATUS').":</strong> ".$order_status;
  548. $tmp_cell .= "<br /><strong>".$VM_LANG->_('PHPSHOP_ORDER_PRINT_PO_NUMBER').":</strong> " . sprintf("%08d", $db->f("order_id"));
  549. $listObj->addCell( $tmp_cell );
  550. }
  551. $listObj->writeTable();
  552. $listObj->endTable();
  553. if( $num_rows > 0 ) {
  554. $listObj->writeFooter( $keyword, '&Itemid='.$sess->getShopItemid() );
  555. }
  556. }
  557. /**
  558. * Validate form values prior to delete
  559. *
  560. * @param int $order_id
  561. * @return boolean
  562. */
  563. function validate_delete($order_id) {
  564. global $VM_LANG;
  565. $db = new ps_DB;
  566. if(empty( $order_id )) {
  567. $GLOBALS['vmLogger']->err($VM_LANG->_('VM_ORDER_DELETE_ERR_ID'));
  568. return False;
  569. }
  570. // Get the order items and update the stock level
  571. // to the number before the order was placed
  572. $q = "SELECT product_id, product_quantity FROM #__{vm}_order_item WHERE order_id='".$db->getEscaped($order_id)."'";
  573. $db->query( $q );
  574. $dbu = new ps_DB;
  575. // Now update each ordered product
  576. while( $db->next_record() ) {
  577. $q = "UPDATE #__{vm}_product SET product_in_stock=product_in_stock+".$db->f("product_quantity")
  578. .",product_sales=product_sales-".$db->f("product_quantity")." WHERE product_id='".$db->f("product_id")."'";
  579. $dbu->query( $q );
  580. }
  581. return True;
  582. }
  583. /**
  584. * Controller for Deleting Records.
  585. */
  586. function delete(&$d) {
  587. $record_id = $d["order_id"];
  588. if( is_array( $record_id)) {
  589. foreach( $record_id as $record) {
  590. if( !$this->delete_record( $record, $d ))
  591. return false;
  592. }
  593. return true;
  594. }
  595. else {
  596. return $this->delete_record( $record_id, $d );
  597. }
  598. }
  599. /**
  600. * Deletes one Record.
  601. */
  602. function delete_record( $record_id, &$d ) {
  603. global $db;
  604. $record_id = intval( $record_id );
  605. if ($this->validate_delete($record_id)) {
  606. $q = "DELETE from #__{vm}_orders where order_id='$record_id'";
  607. $db->query($q);
  608. $q = "DELETE from #__{vm}_order_item where order_id='$record_id'";
  609. $db->query($q);
  610. $q = "DELETE from #__{vm}_order_payment where order_id='$record_id'";
  611. $db->query($q);
  612. $q = "DELETE from #__{vm}_product_download where order_id='$record_id'";
  613. $db->query($q);
  614. $q = "DELETE from #__{vm}_order_history where order_id='$record_id'";
  615. $db->query($q);
  616. $q = "DELETE from #__{vm}_order_user_info where order_id='$record_id'";
  617. $db->query($q);
  618. $q = "DELETE FROM #__{vm}_shipping_label where order_id=$record_id";
  619. $db->query($q);
  620. return True;
  621. }
  622. else {
  623. return False;
  624. }
  625. }
  626. /**
  627. * Creates the order navigation on the order print page
  628. *
  629. * @param int $order_id
  630. * @return boolean
  631. */
  632. function order_print_navigation( $order_id=1 ) {
  633. global $sess, $modulename, $VM_LANG, $my;
  634. $navi_db =& new ps_DB;
  635. return;
  636. $navigation = "<div align=\"center\">\n<strong>\n";
  637. $q = "SELECT order_id FROM #__{vm}_orders WHERE ";
  638. $q .= "order_id < '$order_id' ORDER BY order_id DESC";
  639. $navi_db->query($q);
  640. $navi_db->next_record();
  641. if ($navi_db->f("order_id")) {
  642. $url = $_SERVER['PHP_SELF'] . "?page=$modulename.booking_form&order_id=";
  643. $url .= $navi_db->f("order_id");
  644. $navigation .= "<a class=\"pagenav\" href=\"" . $sess->url($url) . "\">&lt; " .$VM_LANG->_('ITEM_PREVIOUS')."</a> | ";
  645. } else
  646. $navigation .= "<span class=\"pagenav\">&lt; " .$VM_LANG->_('ITEM_PREVIOUS')." | </span>";
  647. $q = "SELECT order_id FROM #__{vm}_orders WHERE ";
  648. $q .= "order_id > '$order_id' ORDER BY order_id";
  649. $navi_db->query($q);
  650. $navi_db->next_record();
  651. if ($navi_db->f("order_id")) {
  652. $url = $_SERVER['PHP_SELF'] . "?page=$modulename.booking_form&order_id=";
  653. $url .= $navi_db->f("order_id");
  654. $navigation .= "<a class=\"pagenav\" href=\"" . $sess->url($url) ."\">". $VM_LANG->_('ITEM_NEXT')." &gt;</a>";
  655. } else {
  656. $navigation .= "<span class=\"pagenav\">".$VM_LANG->_('ITEM_NEXT')." &gt;</span>";
  657. }
  658. $navigation .= "\n<strong>\n</div>\n";
  659. return $navigation;
  660. }
  661. function add(&$d) {
  662. global $ps_booking, $VM_LANG, $mosConfig_offset, $sess, $page;
  663. /**
  664. * Add the user
  665. */
  666. if(!vmGet($d,'user_id')){
  667. $_POST['name'] = vmGet($d,'first_name').' '.vmGet($d,'last_name');
  668. $_POST['gid'] = $d['gid'] = '18';
  669. $_POST['usertype'] = $d['usertype'] = 'Registered';
  670. $d['perms'] = 'shopper';
  671. $d['customer_number'] = 'shopper';
  672. $ps_user = new ps_user();
  673. if (!$ps_user->add($d)) {
  674. $GLOBALS['vmLogger']->err('Unable to add the user account');
  675. return false;
  676. }
  677. $d['user_id'] = $_REQUEST['user_id'];
  678. }
  679. /**
  680. * Process the booking information, this sets the price etc
  681. */
  682. $ps_booking->process(1,1);
  683. if(!$ps_booking->status){
  684. return false;
  685. }
  686. /**
  687. * Add the order / booking
  688. **/
  689. $ps_order_booking = new ps_order_booking();
  690. if ($ps_order_booking->add($d)) {
  691. $_REQUEST['order_id'] = $d['order_id'] = $ps_order_booking->order_id;
  692. /**
  693. * Get all fields which where shown to the user
  694. * and save the order user info table
  695. */
  696. $userFields = ps_userfield::getUserFields('registration', false, '', true);
  697. $skip_fields = ps_userfield::getSkipFields();
  698. $fields = array();
  699. foreach( $userFields as $userField ) {
  700. if( !in_array($userField->name,$skip_fields)) {
  701. $name = $userField->name == 'email' ? 'user_email' : $userField->name;
  702. $fields[$name] = vmGet($d,$userField->name);
  703. }
  704. }
  705. $fields['user_id'] = $d['user_id'];
  706. $fields['order_id'] = $d['order_id'];
  707. $fields['address_type'] = 'BT';
  708. $fields['address_type_name'] = '-default-';
  709. $db = new ps_DB();
  710. $db->buildQuery('INSERT','#__{vm}_order_user_info',$fields);
  711. if(!$db->query()){
  712. $GLOBALS['vmLogger']->err('Unable to add the user information associated with this booking');
  713. return false;
  714. }
  715. if (vmGet($d,'notify_customer','N')=='HTML') {
  716. $ps_checkout = new ps_checkout();
  717. $ps_checkout->email_receipt($ps_order_booking->order_id,'Booking Added', 0, @VB_NOTIFY_ADD_BACK_CUST, @VB_NOTIFY_ADD_BACK_OWNER, @VB_NOTIFY_ADD_BACK_ADMIN);
  718. }
  719. $GLOBALS['vmLogger']->info('The booking has been sucessfully saved.');
  720. }
  721. return true;
  722. }
  723. function update(&$d) {
  724. global $ps_booking, $VM_LANG, $mosConfig_offset, $sess;
  725. /**
  726. * Process the booking information, this sets the price etc
  727. */
  728. $ps_booking->process(1,1);
  729. $ps_booking->booking_id = $d['order_id'];
  730. //Check that the booking is ok
  731. if (!$ps_booking->status){
  732. return false;
  733. }
  734. /**
  735. * Get the suer ID from the booking info
  736. */
  737. $db = new ps_DB();
  738. $db->query("SELECT user_id FROM #__{vm}_order_user_info WHERE order_id = ".vmGet($d,'order_id'));
  739. $db->next_record();
  740. $d['user_id'] = $_REQUEST['user_id'] = $db->f('user_id');
  741. /**
  742. * Update the order booking
  743. **/
  744. $ps_order_booking = new ps_order_booking();
  745. if (($_SERVER['REMOTE_ADDR'] == '86.147.72.108') || $ps_order_booking->update($d)) {
  746. /**
  747. * Get all fields which where shown to the user
  748. * and save the order user info table
  749. */
  750. $userFields = ps_userfield::getUserFields('registration', false, '', true);
  751. $skip_fields = ps_userfield::getSkipFields();
  752. $fields = array();
  753. foreach( $userFields as $userField ) {
  754. if( !in_array($userField->name,$skip_fields)) {
  755. $name = $userField->name == 'email' ? 'user_email' : $userField->name;
  756. $fields[$name] = vmGet($d,$userField->name);
  757. }
  758. }
  759. $db = new ps_DB();
  760. $db->buildQuery('UPDATE','#__{vm}_order_user_info',$fields,'WHERE user_id = '.vmGet($d,'user_id').' AND order_id = '.vmGet($d,'order_id'));
  761. if(!$db->query()){
  762. $GLOBALS['vmLogger']->err('Unable to update the user information associated with this booking');
  763. return false;
  764. }
  765. /**
  766. * Notify the customer if theres an order status update
  767. */
  768. if($d['order_status'] != $d['current_order_status'] && vmGet($_REQUEST,'notify_customer','N')=='Y'){
  769. if(!$this->order_status_update($d)) return false;
  770. }
  771. if (vmGet($_REQUEST,'notify_customer','N')=='HTML') {
  772. $ps_checkout = new ps_checkout();
  773. // Organic mod: allow separate email for cancellations
  774. if ( vmGet($_REQUEST,'order_status','X') == 'X' ) {
  775. $ps_checkout->email_receipt($d['order_id'],'Booking Cancelled', 0, @VB_NOTIFY_AMEND_CUST, @VB_NOTIFY_AMEND_OWNER, @VB_NOTIFY_AMEND_ADMIN);
  776. }
  777. else {
  778. $ps_checkout->email_receipt($d['order_id'],'Booking Updated / Amended', 0, @VB_NOTIFY_AMEND_CUST, @VB_NOTIFY_AMEND_OWNER, @VB_NOTIFY_AMEND_ADMIN);
  779. }
  780. }
  781. $GLOBALS['vmLogger']->info('The booking details have been sucessfully updated.');
  782. }
  783. return true;
  784. }
  785. }
  786. $ps_order = new ps_order;
  787. ?>