PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/ofs_includes/func.get_product.php

https://gitlab.com/Henaway/CLFC
PHP | 159 lines | 131 code | 7 blank | 21 comment | 8 complexity | c85b36be90b992a375fa1a259ac5e149 MD5 | raw file
  1. <?php
  2. // If the product exists, the subroutine returns the product information.
  3. // Call with: get_product ($product_id, $product_version, $pvid)
  4. // Must supply (product_id, product_version) AND/OR (pvid)
  5. function get_product ($product_id, $product_version, $pvid)
  6. {
  7. // Expose additional parameters as they become needed.
  8. global $connection;
  9. $products_fields = array (
  10. 'pvid',
  11. 'product_id',
  12. 'product_version',
  13. NEW_TABLE_PRODUCTS.'.producer_id',
  14. 'product_name',
  15. 'product_description',
  16. 'account_number',
  17. 'inventory_pull',
  18. 'inventory_id',
  19. 'quantity AS inventory_quantity', // <------------- information from inventory table
  20. 'subcategory_id',
  21. 'future_delivery',
  22. 'future_delivery_type',
  23. 'production_type_id',
  24. 'unit_price',
  25. 'pricing_unit',
  26. 'ordering_unit',
  27. 'random_weight',
  28. 'meat_weight_type',
  29. 'minimum_weight',
  30. 'maximum_weight',
  31. 'extra_charge',
  32. 'product_fee_percent',
  33. 'image_id',
  34. 'listing_auth_type',
  35. 'confirmed',
  36. 'retail_staple',
  37. 'staple_type',
  38. 'created',
  39. 'modified',
  40. 'tangible',
  41. 'sticky',
  42. 'hide_from_invoice',
  43. 'storage_id',
  44. TABLE_CATEGORY.'.category_id',
  45. TABLE_CATEGORY.'.category_name',
  46. TABLE_SUBCATEGORY.'.subcategory_name',
  47. TABLE_PRODUCT_TYPES.'.prodtype'
  48. );
  49. $query = '
  50. SELECT
  51. '.implode (",\n ", $products_fields).'
  52. FROM '.NEW_TABLE_PRODUCTS.'
  53. LEFT JOIN
  54. '.TABLE_INVENTORY.' USING(inventory_id)
  55. LEFT JOIN
  56. '.TABLE_PRODUCT_TYPES.' USING(production_type_id)
  57. LEFT JOIN
  58. '.TABLE_SUBCATEGORY.' USING(subcategory_id)
  59. LEFT JOIN
  60. '.TABLE_CATEGORY.' USING(category_id)
  61. LEFT JOIN
  62. '.TABLE_PRODUCER.' ON '.NEW_TABLE_PRODUCTS.'.producer_id = '.TABLE_PRODUCER.'.producer_id
  63. WHERE
  64. ('.NEW_TABLE_PRODUCTS.'.product_id = "'.mysql_real_escape_string ($product_id).'"
  65. AND '.NEW_TABLE_PRODUCTS.'.product_version = "'.mysql_real_escape_string ($product_version).'")
  66. OR
  67. ('.NEW_TABLE_PRODUCTS.'.pvid = "'.mysql_real_escape_string ($pvid).'"
  68. AND '.NEW_TABLE_PRODUCTS.'.pvid != "0")';
  69. $result = mysql_query($query, $connection) or die(debug_print ("ERROR: 754004 ", array ($query,mysql_error()), basename(__FILE__).' LINE '.__LINE__));
  70. if ($row = mysql_fetch_array($result))
  71. {
  72. return ($row);
  73. }
  74. }
  75. // This function is a little convoluted, but there is no easy way to get the next product_id when
  76. // product_id is not an auto-increment field. So here is the process...
  77. // 1. ask for the maximum product_id and add +1
  78. // 2. insert our new row and hope no other process has added a product ahead of us
  79. // 3. check if our product_id is unique (i.e. we got the only one) -- a good chance
  80. // 4. if *not* unique, then increment it until it is
  81. // The function will reserve the row by setting pvid, product_id, and product_version=1 and
  82. // will return an associative array with pvid and product_id
  83. // For this to work, the products table index (product_id-product_version) must not be unique
  84. function get_next_product_id ($producer_id)
  85. {
  86. global $connection;
  87. // Get the next product_id as it stands currently
  88. $query = '
  89. SELECT
  90. MAX(product_id) + 1 AS product_id
  91. FROM
  92. '.NEW_TABLE_PRODUCTS;
  93. $result = mysql_query($query, $connection) or die (debug_print ("ERROR: 856249 ", array ($query,mysql_error()), basename(__FILE__).' LINE '.__LINE__));
  94. if ($row = mysql_fetch_array($result))
  95. {
  96. $product_id = $row['product_id'];
  97. }
  98. // Now insert that product_id to reserve our place
  99. // Set product_version = 1 since it is a new product
  100. $query = '
  101. INSERT INTO
  102. '.NEW_TABLE_PRODUCTS.'
  103. SET
  104. product_id = "'.mysql_real_escape_string($product_id).'",
  105. product_version = "1",
  106. producer_id = "'.mysql_real_escape_string($producer_id).'"';
  107. $result = mysql_query($query, $connection) or die (debug_print ("ERROR: 460569 ", array ($query,mysql_error()), basename(__FILE__).' LINE '.__LINE__));
  108. $pvid = mysql_insert_id();
  109. // Now increment the product_id until it is unique in the database
  110. $need_to_test = true;
  111. while ($need_to_test == true)
  112. {
  113. $query = '
  114. SELECT
  115. COUNT(product_id) AS count
  116. FROM
  117. '.NEW_TABLE_PRODUCTS.'
  118. WHERE
  119. product_id = "'.mysql_real_escape_string($product_id).'"';
  120. $result = mysql_query($query, $connection) or die (debug_print ("ERROR: 311047 ", array ($query,mysql_error()), basename(__FILE__).' LINE '.__LINE__));
  121. if ($row = mysql_fetch_array($result))
  122. {
  123. // Check if we are done
  124. if ($row['count'] == 1)
  125. {
  126. $need_to_test = false;
  127. }
  128. // Otherwise increment product_id for our row in the database
  129. else
  130. {
  131. $query = '
  132. UPDATE
  133. '.NEW_TABLE_PRODUCTS.'
  134. SET
  135. product_id = product_id + 1
  136. WHERE
  137. pvid = "'.mysql_real_escape_string($pvid).'"';
  138. $result = mysql_query($query, $connection) or die (debug_print ("ERROR: 939523 ", array ($query,mysql_error()), basename(__FILE__).' LINE '.__LINE__));
  139. // And sync our external product_id variable
  140. $product_id ++;
  141. }
  142. }
  143. // if no row or overflow, then prevent an infinite loop by bailing
  144. else
  145. {
  146. die (debug_print ("ERROR: 784914 ", 'No product rows found', basename(__FILE__).' LINE '.__LINE__));
  147. }
  148. if ($overflow_count ++ > 8) die (debug_print ("ERROR: 489063 ", 'Overflow while finding next product', basename(__FILE__).' LINE '.__LINE__));
  149. }
  150. return array ('pvid' => $pvid, 'product_id' => $product_id);
  151. }
  152. ?>