PageRenderTime 58ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/public_html/ajax/customer_list.php

https://gitlab.com/Henaway/CLFC
PHP | 280 lines | 232 code | 17 blank | 31 comment | 34 complexity | bf6e6043d6c7245a03cd9a92c74e9be6 MD5 | raw file
  1. <?php
  2. include_once('config_foodcoop.php');
  3. include_once('general_functions.php');
  4. session_start();
  5. valid_auth('member');
  6. include_once('func.open_basket.php');
  7. // Get values for this operation
  8. // ... from the environment
  9. $member_id = $_SESSION['member_id'];
  10. $delivery_id = ActiveCycle::delivery_id();
  11. $basket_id = CurrentBasket::basket_id();
  12. // ... from add/subtract from basket
  13. $product_id = $_POST['product_id'];
  14. $product_version = $_POST['product_version'];
  15. $action = $_POST['action'];
  16. // ... from update message
  17. $message = $_POST['message'];
  18. // If a basket is not already open, then open one...
  19. if (! $basket_id)
  20. {
  21. $basket_info = open_basket (array (
  22. 'member_id' => $member_id,
  23. 'delivery_id' => $delivery_id,
  24. ));
  25. if ($basket_info == 'delcode_id not set')
  26. {
  27. echo 'delcode_id not set';
  28. exit (1);
  29. }
  30. $basket_id = $basket_info['basket_id'];
  31. }
  32. // Make sure the number we think is in the basket is the number that really is in the basket
  33. $query = '
  34. SELECT
  35. (
  36. SELECT
  37. CONCAT(bpid,":",quantity)
  38. FROM
  39. '.NEW_TABLE_BASKET_ITEMS.'
  40. WHERE
  41. basket_id = "'.mysql_real_escape_string (CurrentBasket::basket_id()).'"
  42. AND product_id = "'.mysql_real_escape_string ($product_id).'"
  43. AND product_version = "'.mysql_real_escape_string ($product_version).'"
  44. ) AS bpid_quantity,
  45. '.NEW_TABLE_PRODUCTS.'.inventory_id,
  46. '.NEW_TABLE_PRODUCTS.'.inventory_pull,
  47. FLOOR('.TABLE_INVENTORY.'.quantity / '.NEW_TABLE_PRODUCTS.'.inventory_pull) AS inventory_quantity
  48. FROM
  49. '.NEW_TABLE_PRODUCTS.'
  50. LEFT JOIN '.TABLE_INVENTORY.' ON '.TABLE_INVENTORY.'.inventory_id = '.NEW_TABLE_PRODUCTS.'.inventory_id
  51. WHERE
  52. '.NEW_TABLE_PRODUCTS.'.product_id = "'.mysql_real_escape_string ($product_id).'"
  53. AND '.NEW_TABLE_PRODUCTS.'.product_version = "'.mysql_real_escape_string ($product_version).'"';
  54. $result = @mysql_query($query, $connection) or die(debug_print ("ERROR: 738102 ", array ($query,mysql_error()), basename(__FILE__).' LINE '.__LINE__));
  55. if ( $row = mysql_fetch_object($result) )
  56. {
  57. list ($bpid,$basket_quantity) = explode(':', $row->bpid_quantity);
  58. // $basket_quantity = $row->quantity;
  59. $inventory_quantity = $row->inventory_quantity;
  60. $inventory_id = $row->inventory_id;
  61. $inventory_pull = $row->inventory_pull;
  62. // There seems to be a problem with basket_id -- at least for 'admin'
  63. debug_print ("INFO: ", array (
  64. 'member_id'=>$member_id,
  65. 'delivery_id'=>$delivery_id,
  66. 'basket_id'=>$basket_id,
  67. 'product_id'=>$product_id,
  68. 'product_version'=>$product_version,
  69. 'action'=>$action,
  70. 'bpid'=>$bpid,
  71. 'basket_id'=>$basket_quantity,
  72. 'inventory_quantity'=>$inventory_quantity,
  73. 'inventory_id'=>$inventory_id,
  74. 'inventory_pull'=>$inventory_pull
  75. ), basename(__FILE__).' LINE '.__LINE__);
  76. }
  77. // Abort the operation if we do not have important information
  78. if (! $delivery_id ||
  79. ! $member_id ||
  80. ! $basket_id ||
  81. ! $product_id ||
  82. ! $product_version ||
  83. ! $action)
  84. {
  85. die(debug_print ("ERROR: 545721 ", 'Call without necessary information.', basename(__FILE__).' LINE '.__LINE__));
  86. }
  87. if ($action == "add")
  88. {
  89. // Create new basket item
  90. if ($basket_quantity == 0 &&
  91. (($inventory_id && $inventory_quantity > 0) ||
  92. ! $inventory_id))
  93. {
  94. $add_basket_item = true;
  95. // Alert that a new item has been added to the basket
  96. // $alert = 'Product has been added to the basket';
  97. $basket_quantity = 1;
  98. $inventory_quantity = $inventory_quantity - 1; // inventory_quantity is adjusted for THIS product
  99. $update_basket_item = false;
  100. }
  101. // No available inventory... do nothing
  102. elseif ($inventory_id && $inventory_quantity <= 0)
  103. {
  104. $add_basket_item = false;
  105. // Alert that there are not enough in inventory
  106. // $alert = 'Insufficient inventory is available';
  107. // $basket_quantity = $basket_quantity; // no change
  108. $update_basket_item = false;
  109. }
  110. // Add to an existing basket item
  111. else
  112. {
  113. $add_basket_item = false;
  114. // $alert = 'Product quantity has been updated';
  115. $basket_quantity = $basket_quantity + 1;
  116. $inventory_quantity = $inventory_quantity - 1;
  117. $update_basket_item = true;
  118. }
  119. }
  120. elseif ($action == "sub")
  121. {
  122. // Only one basket item, so remove it
  123. if ($basket_quantity <= 1)
  124. {
  125. // Alert that the basket has been emptied
  126. // $alert = 'Product has been removed from the basket';
  127. $basket_quantity = 0;
  128. $inventory_quantity = $inventory_quantity + 1;
  129. $remove_basket_item = true;
  130. $update_basket_item = false; // no need for update since the item will be removed
  131. }
  132. elseif ($basket_quantity > 1)
  133. {
  134. // Alert that the basket is already empty
  135. // $alert = 'The item was not in your basket';
  136. $basket_quantity = $basket_quantity - 1;
  137. $inventory_quantity = $inventory_quantity + 1;
  138. $remove_basket_item = false;
  139. $update_basket_item = true;
  140. }
  141. }
  142. // First add the basket item, if needed
  143. if ($add_basket_item == true)
  144. {
  145. $query = '
  146. INSERT INTO
  147. '.NEW_TABLE_BASKET_ITEMS.' (
  148. basket_id,
  149. product_id,
  150. product_version,
  151. quantity,
  152. product_fee_percent,
  153. subcategory_fee_percent,
  154. producer_fee_percent,
  155. out_of_stock,
  156. date_added )
  157. SELECT
  158. "'.mysql_real_escape_string ($basket_id).'",
  159. '.NEW_TABLE_PRODUCTS.'.product_id,
  160. '.NEW_TABLE_PRODUCTS.'.product_version,
  161. "1",
  162. '.NEW_TABLE_PRODUCTS.'.product_fee_percent,
  163. '.TABLE_SUBCATEGORY.'.subcategory_fee_percent,
  164. '.TABLE_PRODUCER.'.producer_fee_percent,
  165. "0",
  166. "'.date('Y-m-d H:i:s',time()).'"
  167. FROM
  168. '.NEW_TABLE_PRODUCTS.'
  169. LEFT JOIN
  170. '.TABLE_SUBCATEGORY.' USING(subcategory_id)
  171. LEFT JOIN
  172. '.TABLE_PRODUCER.' USING(producer_id)
  173. WHERE
  174. product_id = "'.mysql_real_escape_string ($product_id).'"
  175. AND product_version = "'.mysql_real_escape_string ($product_version).'"';
  176. $result = @mysql_query($query, $connection) or die(debug_print ("ERROR: 155816 ", array ($query,mysql_error()), basename(__FILE__).' LINE '.__LINE__));
  177. $bpid= mysql_insert_id();
  178. }
  179. // Then update the quantity, if needed
  180. if ($update_basket_item == true)
  181. {
  182. $query = '
  183. UPDATE
  184. '.NEW_TABLE_BASKET_ITEMS.'
  185. SET
  186. quantity = "'.mysql_real_escape_string ($basket_quantity).'"
  187. WHERE
  188. basket_id = "'.mysql_real_escape_string ($basket_id).'"
  189. AND product_id = "'.mysql_real_escape_string ($product_id).'"
  190. AND product_version = "'.mysql_real_escape_string ($product_version).'"';
  191. $result = @mysql_query($query, $connection) or die(debug_print ("ERROR: 731034 ", array ($query,mysql_error()), basename(__FILE__).' LINE '.__LINE__));
  192. }
  193. if ($inventory_id && ($action == 'add' || $action == 'sub'))
  194. {
  195. if ($action == 'add')
  196. {
  197. $inventory_function = '-';
  198. }
  199. elseif ($action == 'sub')
  200. {
  201. $inventory_function = '+';
  202. }
  203. $query = '
  204. UPDATE
  205. '.TABLE_INVENTORY.'
  206. SET
  207. quantity = quantity '.$inventory_function.' '.mysql_real_escape_string ($inventory_pull).'
  208. WHERE
  209. inventory_id = '.mysql_real_escape_string ($inventory_id);
  210. $result = @mysql_query($query, $connection) or die(debug_print ("ERROR: 066934 ", array ($query,mysql_error()), basename(__FILE__).' LINE '.__LINE__));
  211. }
  212. if ($remove_basket_item == true)
  213. {
  214. $query = '
  215. DELETE FROM
  216. '.NEW_TABLE_BASKET_ITEMS.'
  217. WHERE
  218. basket_id = "'.mysql_real_escape_string ($basket_id).'"
  219. AND product_id = "'.mysql_real_escape_string ($product_id).'"
  220. AND product_version = "'.mysql_real_escape_string ($product_version).'"';
  221. $result = @mysql_query($query, $connection) or die(debug_print ("ERROR: 267490 ", array ($query,mysql_error()), basename(__FILE__).' LINE '.__LINE__));
  222. }
  223. // Handle messages
  224. // First remove all messages, no matter what. Without this process, additional messages
  225. // keep getting added.
  226. if (isset ($bpid))
  227. { // Delete if necessary
  228. $query = '
  229. DELETE FROM
  230. '.NEW_TABLE_MESSAGES.'
  231. WHERE
  232. referenced_key1 = '.mysql_real_escape_string($bpid).'
  233. AND message_type_id =
  234. (
  235. SELECT message_type_id
  236. FROM '.NEW_TABLE_MESSAGE_TYPES.'
  237. WHERE description = "customer notes to producer"
  238. )';
  239. $result = @mysql_query($query, $connection) or die(debug_print ("ERROR: 285097 ", array ($query,mysql_error()), basename(__FILE__).' LINE '.__LINE__));
  240. // Now post the message back, as needed
  241. if ($message != '' && $remove_basket_item != true)
  242. { // Update message
  243. $query = '
  244. INSERT INTO '.NEW_TABLE_MESSAGES.'
  245. SET
  246. message = "'.mysql_real_escape_string($message).'",
  247. message_type_id =
  248. (
  249. SELECT message_type_id
  250. FROM '.NEW_TABLE_MESSAGE_TYPES.'
  251. WHERE description = "customer notes to producer"
  252. ),
  253. referenced_key1 = "'.mysql_real_escape_string($bpid).'"';
  254. $result = @mysql_query($query, $connection) or die(debug_print ("ERROR: 925223 ", array ($query,mysql_error()), basename(__FILE__).' LINE '.__LINE__));
  255. }
  256. }
  257. // The following is necessary because this is also called when javascript/ajax is turned off and
  258. // we don't want to send extraneous data back to the output page.
  259. if ($non_ajax_query == false)
  260. {
  261. echo number_format($basket_quantity, 0).':'.number_format($inventory_quantity, 0).':'.$alert;
  262. }
  263. ?>