PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_virtuemart/classes/ps_order_booking.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 484 lines | 334 code | 111 blank | 39 comment | 44 complexity | 0e36e1e3ad0dd29eff6a9e15800a462c 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. class ps_order_booking {
  4. var $order_id;
  5. function validate_add(&$d) {
  6. if(!$d['property_id']){
  7. $GLOBALS['vmLogger']->err('You must supply a property id.');
  8. }
  9. return true;
  10. }
  11. function validate_update(&$d) {
  12. global $VM_LANG;
  13. if(!$d['property_id']){
  14. $GLOBALS['vmLogger']->err('You must supply a property id.');
  15. }
  16. return true;
  17. }
  18. function get_order_number($user_id) {
  19. /* Generated a unique order number */
  20. $str = session_id();
  21. $str .= (string)time();
  22. $order_number = $user_id .'_'. md5($str);
  23. return($order_number);
  24. }
  25. function add(&$d) {
  26. global $VM_LANG, $ps_booking, $mosConfig_offset;
  27. $timestamp = time() + ($mosConfig_offset*60*60);
  28. $db = new ps_DB;
  29. $ps_vendor_id = $_SESSION["ps_vendor_id"];
  30. if (!$this->validate_add($d)) {
  31. return false;
  32. }
  33. if ($d['user_id']) {
  34. $dbui = new ps_DB();
  35. $dbui->query('select user_info_id from #__{vm}_user_info where user_id='.$d['user_id'].' and address_type=\'BT\'');
  36. $dbui->next_record();
  37. }
  38. // Get the IP Address
  39. if (!empty($_SERVER['REMOTE_ADDR'])) {
  40. $ip = $_SERVER['REMOTE_ADDR'];
  41. }
  42. else {
  43. $ip = 'unknown';
  44. }
  45. // Collect all fields and values to store them!
  46. $fields = array(
  47. 'user_id' => $d['user_id'],
  48. 'vendor_id' => $_SESSION["ps_vendor_id"],
  49. 'order_number' => $this->get_order_number($d['user_id']),
  50. 'user_info_id' => $dbui->f('user_info_id'),
  51. 'ship_method_id' => @urldecode($d["shipping_rate_id"]),
  52. 'order_total' => $ps_booking->total,
  53. 'order_subtotal' => $ps_booking->subtotal,
  54. 'order_tax' => $ps_booking->tax,
  55. 'order_currency' => $GLOBALS['product_currency'],
  56. 'order_status' => $d['order_status'],
  57. 'cdate' => $timestamp,
  58. 'mdate' => $timestamp,
  59. 'customer_note' => htmlspecialchars(strip_tags($d['order_comment']), ENT_QUOTES ), //Probably should provide a notes field
  60. 'ip_address' => $ip
  61. );
  62. // Insert the main order information
  63. $db->buildQuery( 'INSERT', '#__{vm}_orders', $fields );
  64. if (!$db->query()){
  65. $GLOBALS['vmLogger']->err('Unable to update the order details');
  66. return false;
  67. } else {
  68. $this->order_id = $db->last_insert_id();
  69. }
  70. if (isset($this->order_id) && $this->order_id!='') {
  71. /**
  72. * Set the booking details
  73. */
  74. $fields = array('order_id' => $this->order_id,
  75. 'property_id' => $ps_booking->property_id,
  76. 'people' => $ps_booking->people,
  77. 'arrival' => $ps_booking->dateFrom,
  78. 'departure' => $ps_booking->dateTo,
  79. 'total' => $ps_booking->total,
  80. 'subtotal' => $ps_booking->subtotal,
  81. 'tax_total' => $ps_booking->tax,
  82. 'tax_state' => $ps_booking->tax_state,
  83. 'tax_resort' => $ps_booking->tax_resort,
  84. 'original' => $ps_booking->original,
  85. 'insurance' => $ps_booking->securityDeposit,
  86. 'cleaning' => $ps_booking->cleaning
  87. );
  88. $db->buildQuery('INSERT', '#__{vm}_order_booking', $fields);
  89. if( !$db->query() ){
  90. $GLOBALS['vmLogger']->err('Unable to add the booking details');
  91. return false;
  92. }
  93. } else {
  94. $GLOBALS['vmLogger']->err('Unable to add the booking details');
  95. return false;
  96. }
  97. /**
  98. * Loop through and save the stage payment info
  99. */
  100. $payment_stages = $ps_booking->getStagePayment();
  101. foreach($payment_stages->stages as $payment){
  102. // Payment number is encrypted using mySQL encryption functions.
  103. $fields = array(
  104. 'order_id' => $this->order_id,
  105. 'payment_amount' => $payment->payment_amount,
  106. 'payment_due' => $payment->payment_due,
  107. 'payment_stage' => $payment->payment_stage
  108. );
  109. //Add to DB
  110. $db->buildQuery( 'INSERT', '#__{vm}_order_payment', $fields, '' );
  111. $db->query();
  112. }
  113. /**
  114. * Update the Order History
  115. */
  116. $notify_customer = empty($d['notify_customer']) ? "N" : $d['notify_customer'];
  117. if ($notify_customer=="Y" ) {
  118. $notify_customer=1;
  119. } else {
  120. $notify_customer=0;
  121. }
  122. //Set the timestamp
  123. $timestamp = time() + ($mosConfig_offset*60*60);
  124. $fields = array( 'order_id' => $this->order_id,
  125. 'order_status_code' => $d["order_status"],
  126. 'date_added' => date("Y-m-d G:i:s", $timestamp),
  127. 'customer_notified' => $notify_customer,
  128. 'comments' => $d['order_comment'],
  129. 'booking_serialized' => serialize($ps_booking)
  130. );
  131. $db->buildQuery('INSERT', '#__{vm}_order_history', $fields );
  132. if (!$db->query()) {
  133. $GLOBALS['vmLogger']->err('Unable to update the order history');
  134. return false;
  135. }
  136. return true;
  137. }
  138. /**
  139. * Updates an Order Status
  140. *
  141. * @param array $d
  142. * @return boolean
  143. */
  144. function update(&$d) {
  145. global $VM_LANG, $ps_booking, $mosConfig_offset;
  146. $timestamp = time() + ($mosConfig_offset*60*60);
  147. $db = new ps_DB;
  148. $ps_vendor_id = $_SESSION["ps_vendor_id"];
  149. if (!$this->validate_update($d)) {
  150. return false;
  151. }
  152. /**
  153. * Update the final stage payment if required
  154. */
  155. require_once(CLASSPATH.'ps_payments.php');
  156. $psp = new ps_payments();
  157. $stages = $psp->getStagePayments(null, $d['order_id']);
  158. $final_stage = $stages->stages[ count( $stages->stages ) - 1 ];
  159. $final_cleared = $final_stage->payment_submitted;
  160. if ($final_cleared) {
  161. $GLOBALS['vmLogger']->err('Unable to update the final payment stage, payment already recieved');
  162. } else {
  163. $old_total = 0;
  164. foreach ($stages->stages as $p_stage) {
  165. $old_total += $p_stage->payment_amount;
  166. }
  167. if ($old_total != $ps_booking->total) {
  168. $difference = $old_total - $ps_booking->total;
  169. if ($difference > $final_stage->payment_amount) {
  170. $difference = $final_stage->payment_amount - 1;
  171. $_REQUEST['use_new_price'] = 1;
  172. $_REQUEST['new_total'] = $old_total - ($final_stage->payment_amount + 1);
  173. $ps_booking->setPrice();
  174. $GLOBALS['vmLogger']->err('Price reduction greater than final stage payment amount. Final stage payment set to 1');
  175. }
  176. $new_payment = $final_stage->payment_amount - $difference;
  177. $stage_field = array('payment_amount' => $new_payment);
  178. $db->buildQuery('UPDATE', '#__{vm}_order_payment', $stage_field, "WHERE payment_id=".$final_stage->payment_id);
  179. if (!$db->query()) {
  180. $GLOBALS['vmLogger']->err('Unable to update the stage payment details');
  181. return false;
  182. }
  183. }
  184. }
  185. /**
  186. * update the orders table
  187. */
  188. $order_fields = array('order_total' => $ps_booking->total,
  189. 'order_subtotal' => $ps_booking->subtotal,
  190. 'order_tax' => $ps_booking->tax,
  191. 'order_status' => $d['order_status'],
  192. 'mdate' => $timestamp
  193. );
  194. $db->buildQuery('UPDATE', '#__{vm}_orders', $order_fields, "WHERE order_id=".$d['order_id'] );
  195. if (!$db->query()){
  196. $GLOBALS['vmLogger']->err('Unable to update the order details');
  197. return false;
  198. }
  199. /**
  200. * Update the booking details
  201. */
  202. $fields = array('arrival' => $ps_booking->dateFrom,
  203. 'departure' => $ps_booking->dateTo,
  204. 'people' => vmGet($d,'people'),
  205. 'total' => $ps_booking->total,
  206. 'subtotal' => $ps_booking->subtotal,
  207. 'tax_total' => $ps_booking->tax,
  208. 'tax_state' => $ps_booking->tax_state,
  209. 'tax_resort' => $ps_booking->tax_resort,
  210. 'original' => $ps_booking->original
  211. );
  212. $db->buildQuery('UPDATE', '#__{vm}_order_booking', $fields, "WHERE order_id=".$d['order_id'] );
  213. if( !$db->query() ){
  214. $GLOBALS['vmLogger']->err('Unable to update the booking details');
  215. return false;
  216. }
  217. /**
  218. * Update the Order History
  219. */
  220. $notify_customer = empty($d['notify_customer']) ? "N" : $d['notify_customer'];
  221. if( $notify_customer=="Y" ) {
  222. $notify_customer=1;
  223. }
  224. else {
  225. $notify_customer=0;
  226. }
  227. //Set the timestamp
  228. $timestamp = time() + ($mosConfig_offset*60*60);
  229. $fields = array( 'order_id' => $d["order_id"],
  230. 'order_status_code' => $d["order_status"],
  231. 'date_added' => date("Y-m-d G:i:s", $timestamp),
  232. 'customer_notified' => $notify_customer,
  233. 'comments' => $d['order_comment'],
  234. 'booking_serialized' => serialize($ps_booking)
  235. );
  236. $db->buildQuery('INSERT', '#__{vm}_order_history', $fields );
  237. if (!$db->query()) {
  238. $GLOBALS['vmLogger']->err('Unable to update the order history');
  239. return false;
  240. }
  241. return true;
  242. }
  243. /**
  244. * Controller for Deleting Records.
  245. */
  246. function delete(&$d) {
  247. if (!$this->validate_delete($d)) {
  248. return False;
  249. }
  250. $record_id = $d["order_status_id"];
  251. if( is_array( $record_id)) {
  252. foreach( $record_id as $record) {
  253. if( !$this->delete_record( $record, $d ))
  254. return false;
  255. }
  256. return true;
  257. }
  258. else {
  259. return $this->delete_record( $record_id, $d );
  260. }
  261. }
  262. function delete_record( $record_id, &$d ) {
  263. global $db;
  264. $ps_vendor_id = $_SESSION["ps_vendor_id"];
  265. $q = 'DELETE FROM `'.$this->_table_name.'` WHERE order_status_id='.(int)$record_id;
  266. $q .= " AND vendor_id='$ps_vendor_id'";
  267. return $db->query($q);
  268. }
  269. function countOrders($state = '', $vbDateFrom = '', $vbDateTo = '', $property_id = ''){
  270. $db = new ps_DB();
  271. $count = "SELECT count(*) as num_rows FROM #__{vm}_orders AS `order`
  272. LEFT JOIN #__{vm}_order_booking AS booking ON booking.order_id = order.order_id
  273. LEFT JOIN #__hp_properties AS p ON p.id = booking.property_id
  274. LEFT JOIN #__{vm}_user_info AS u ON u.user_id = order.user_id
  275. WHERE order.order_id = booking.order_id AND ";
  276. $count .= $this->getOrdersQuery($state, $vbDateFrom, $vbDateTo, $property_id);
  277. $db->query($count);
  278. $db->next_record();
  279. return $db->f("num_rows");
  280. }
  281. function getOrders($state = '', $vbDateFrom = '', $vbDateTo = '', $property_id = '', $showAllProps = 0, $onlyEnding = false){
  282. global $limitstart, $limit, $page;
  283. $db = new ps_DB();
  284. $list = "SELECT `order`.*, ui.first_name, ui.last_name, booking.*, p.name as property, IF((booking.departure BETWEEN '$vbDateFrom' AND '$vbDateTo'), 1, 0) AS ends_now
  285. FROM #__{vm}_orders AS `order`
  286. LEFT JOIN #__{vm}_order_user_info AS ui ON ui.user_id = order.user_id AND ui.order_id = order.order_id AND ui.address_type = 'BT'
  287. LEFT JOIN #__{vm}_order_booking AS booking ON booking.order_id = order.order_id
  288. LEFT JOIN #__{vm}_user_info AS u ON u.user_id = order.user_id
  289. LEFT JOIN #__hp_properties AS p ON p.id = booking.property_id
  290. WHERE "; //Organic ADD
  291. $list .= $this->getOrdersQuery($state, $vbDateFrom, $vbDateTo, $property_id, $showAllProps, $onlyEnding) . ($page == 'order.booking_list' ? " LIMIT $limitstart, " . $limit : '');
  292. $db->query($list);
  293. return $db;
  294. }
  295. function getOrdersQuery($state = '', $vbDateFrom = '', $vbDateTo = '', $property_id = '', $showAllProps = 0, $onlyEnding = false){
  296. global $keyword;
  297. $q = "";
  298. if (!empty($keyword)) {
  299. $q .= "(order.order_id LIKE '%$keyword%' ";
  300. $q .= "OR order.order_status LIKE '%$keyword%' ";
  301. $q .= "OR ui.first_name LIKE '%$keyword%' ";
  302. $q .= "OR ui.last_name LIKE '%$keyword%' ";
  303. $q .= "OR CONCAT(ui.`first_name`, ' ', ui.`last_name`) LIKE '%$keyword%' ";
  304. $q .= "OR u.first_name LIKE '%$keyword%' ";
  305. $q .= "OR u.last_name LIKE '%$keyword%' ";
  306. $q .= "OR CONCAT(u.`first_name`, ' ', u.`last_name`) LIKE '%$keyword%' ";
  307. $q .= "OR p.name LIKE '%$keyword%' ";
  308. $q .= ") AND ";
  309. }
  310. if (!empty($state)) {
  311. $q .= "order_status = '$state' AND ";
  312. }
  313. if(!empty($property_id)){
  314. $q .= "p.id = $property_id AND ";
  315. }
  316. $q .= "order.vendor_id='".$_SESSION['ps_vendor_id']."' ";
  317. $q .= $showAllProps ? '' : "AND booking.property_id IN(".implode(',',$this->getAllowedProperties()).") "; /* Organic add, only show agents bookings */
  318. if(($vbDateFrom && $vbDateTo)){
  319. $q .= "AND ((booking.departure BETWEEN '$vbDateFrom' AND '$vbDateTo')
  320. ".(
  321. $onlyEnding ? '' : " OR
  322. ('$vbDateFrom' BETWEEN booking.arrival AND booking.departure) OR
  323. (booking.arrival BETWEEN '$vbDateFrom' AND '$vbDateTo') OR
  324. ('$vbDateTo' BETWEEN booking.arrival AND booking.departure)
  325. ").")";
  326. }
  327. $q .= "ORDER BY booking.arrival ASC ";
  328. return $q;
  329. }
  330. function getAllowedProperties(){
  331. global $my, $vmuser, $perm;
  332. $db = new ps_DB();
  333. $prop_ids = array();
  334. //Get either all properties or only for this agent/owner
  335. if(in_array($vmuser->gid, array(23,24,25,31))){
  336. $q = "SELECT p.id from #__hp_properties AS p"
  337. .($vmuser->gid == 31 ? ", #__hp_agents as a WHERE a.id = p.agent AND a.user = '$my->id'" : '');
  338. $db->query($q);
  339. while($db->next_record()){
  340. $prop_ids[] = $db->f("id");
  341. }
  342. }else if($perm->check( "maintenance")){
  343. $q = "SELECT properties FROM #__{vm}_maintenance_props WHERE user_id = $my->id";
  344. $db->query($q);
  345. $db->next_record();
  346. $prop_ids = explode(',',$db->f('properties'));
  347. }
  348. return $prop_ids;
  349. }
  350. function getPropertiesList($selected = 0, $name = 'property_id', $extra = ''){
  351. $props = ps_order_booking::getAllowedProperties();
  352. if(count($props) > 1){
  353. $q = "SELECT * FROM #__hp_properties WHERE published = 1 AND id IN (".implode(',',$props).") ORDER BY name";
  354. $dbp = new ps_DB();
  355. $dbp->query($q);
  356. $props = array(0=>'-- Select Property --');
  357. while($dbp->next_record()){
  358. $props[$dbp->f('id')] = $dbp->f('name');
  359. }
  360. return ps_html::selectList($name, $selected, $props,1,'', "id='$name' $extra");
  361. }
  362. }
  363. function validateOwner(){
  364. global $my, $vmuser;
  365. if(in_array($vmuser->gid, array(23,24,25,31))){
  366. return true;
  367. }else{
  368. $GLOBALS['vmLogger']->err( 'You do not have permission to manage bookings.' );
  369. return false;
  370. }
  371. }
  372. }
  373. ?>