class-db-manager.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * Database operations manager for the Studiou WC Order Print Statuses plugin.
  4. *
  5. * This class centralizes database queries for better maintainability.
  6. */
  7. defined('ABSPATH') || exit;
  8. class Studiou_DB_Manager {
  9. /**
  10. * Get orders data for printing export
  11. *
  12. * @param array $order_ids Array of order IDs
  13. * @return array Array of order data
  14. */
  15. public static function get_orders_for_printing($order_ids) {
  16. global $wpdb;
  17. $query = "
  18. SELECT
  19. `orders`.`id` AS `order_no`,
  20. `t`.`name` AS `prod_cat`,
  21. `products`.`post_title` AS `prod_name`,
  22. `product_variations`.`post_title` AS `prod_var`,
  23. `product_variations_name`.`name` AS `prod_var_type`,
  24. `images`.`guid` AS `prod_img_url`,
  25. `order_products`.`product_qty` AS `qty`,
  26. `orders`.`billing_email` AS `email`
  27. FROM (
  28. (
  29. (
  30. (
  31. (
  32. (
  33. (
  34. `{$wpdb->prefix}wc_orders` `orders`
  35. JOIN `{$wpdb->prefix}wc_order_product_lookup` `order_products`
  36. ON
  37. (
  38. `orders`.`id` = `order_products`.`order_id`
  39. )
  40. )
  41. JOIN `{$wpdb->posts}` `products`
  42. ON
  43. (
  44. `products`.`ID` = `order_products`.`product_id`
  45. )
  46. )
  47. JOIN `{$wpdb->posts}` `product_variations`
  48. ON
  49. (
  50. `product_variations`.`ID` = `order_products`.`variation_id`
  51. )
  52. LEFT JOIN `{$wpdb->postmeta}` `product_variations_attr`
  53. ON
  54. (
  55. `product_variations_attr`.`post_id` = `product_variations`.`ID`
  56. AND `product_variations_attr`.`meta_key` = 'attribute_pa_format'
  57. )
  58. LEFT JOIN `{$wpdb->terms}` `product_variations_name`
  59. ON
  60. (
  61. `product_variations_name`.`name` = `product_variations_attr`.`meta_value`
  62. )
  63. )
  64. LEFT JOIN `{$wpdb->postmeta}` `product_meta`
  65. ON
  66. (
  67. `product_meta`.`post_id` = `products`.`ID` AND `product_meta`.`meta_key` = '_thumbnail_id'
  68. )
  69. )
  70. LEFT JOIN `{$wpdb->posts}` `images`
  71. ON
  72. (
  73. `images`.`ID` = `product_meta`.`meta_value` AND `images`.`post_type` = 'attachment'
  74. )
  75. JOIN `{$wpdb->term_relationships}` `tr`
  76. ON
  77. (`products`.`ID` = `tr`.`object_id`)
  78. )
  79. JOIN `{$wpdb->term_taxonomy}` `tt`
  80. ON
  81. (
  82. `tr`.`term_taxonomy_id` = `tt`.`term_taxonomy_id`
  83. )
  84. )
  85. JOIN `{$wpdb->terms}` `t`
  86. ON
  87. (`tt`.`term_id` = `t`.`term_id`)
  88. )
  89. WHERE
  90. `orders`.`id` IN (" . implode(',', array_map('intval', $order_ids)) . ")
  91. AND `tt`.`taxonomy` = 'product_cat'
  92. ORDER BY
  93. `orders`.`id`
  94. ";
  95. $results = $wpdb->get_results($query);
  96. // Error handling for database query failures
  97. if ($results === false) {
  98. UtilsLog::log('SQL Error: ' . $wpdb->last_error);
  99. return array();
  100. }
  101. return $results;
  102. }
  103. /**
  104. * Set order status to 'to-print'
  105. *
  106. * @param array $order_ids Array of order IDs
  107. * @return int Number of orders updated
  108. */
  109. public static function set_orders_to_prepare_printing($order_ids) {
  110. $updated_count = 0;
  111. foreach ($order_ids as $order_id) {
  112. UtilsLog::log('Setting status Order #' . $order_id . ' to "to-print"');
  113. $order = wc_get_order($order_id);
  114. if ($order) {
  115. $order->update_status('to-print');
  116. $order->update_meta_data('to_print_date', current_time('mysql'));
  117. $order->save();
  118. $updated_count++;
  119. UtilsLog::log('Order #' . $order_id . ' marked as "to-print"');
  120. UtilsLog::message('Order #' . $order_id . ' marked as "to-print"' );
  121. } else {
  122. UtilsLog::log('Order #' . $order_id . ' not found');
  123. }
  124. }
  125. return $updated_count;
  126. }
  127. /**
  128. * Set order status to 'in-print'
  129. *
  130. * @param array $order_ids Array of order IDs
  131. * @return int Number of orders updated
  132. */
  133. public static function set_orders_to_in_printing($order_ids) {
  134. $updated_count = 0;
  135. foreach ($order_ids as $order_id) {
  136. $order = wc_get_order($order_id);
  137. if ($order) {
  138. $order->update_status('in-print');
  139. $order->update_meta_data('in_print_date', current_time('mysql'));
  140. $order->save();
  141. $updated_count++;
  142. UtilsLog::message('Order #' . $order_id . ' marked as "in-print"' );
  143. }
  144. }
  145. return $updated_count;
  146. }
  147. /**
  148. * Import inprint protocol data
  149. *
  150. * @param array $csv_data Parsed CSV data
  151. * @return array Array with updated count and errors
  152. */
  153. public static function import_inprint_protocol($csv_data) {
  154. $updated_count = 0;
  155. $errors = array();
  156. foreach ($csv_data as $row) {
  157. if (!isset($row['order_no']) || !isset($row['externalorder']) || !isset($row['externalorderdate'])) {
  158. $errors[] = __('Invalid row format in CSV.', 'studiou-wc-ord-print-statuses');
  159. continue;
  160. }
  161. $order = wc_get_order($row['order_no']);
  162. if (!$order) {
  163. $errors[] = sprintf(__('Order %s not found.', 'studiou-wc-ord-print-statuses'), $row['order_no']);
  164. continue;
  165. }
  166. $order->update_status('in-print');
  167. $order->update_meta_data('in_print_date', current_time('mysql'));
  168. $order->update_meta_data('external_ref_ord_no', $row['externalorder']);
  169. $order->update_meta_data('external_ref_ord_date', $row['externalorderdate']);
  170. $order->save();
  171. $updated_count++;
  172. }
  173. return array(
  174. 'updated_count' => $updated_count,
  175. 'errors' => $errors
  176. );
  177. }
  178. /**
  179. * Import delivered protocol data
  180. *
  181. * @param array $csv_data Parsed CSV data
  182. * @return array Array with updated count and errors
  183. */
  184. public static function import_delivered_protocol($csv_data) {
  185. $updated_count = 0;
  186. $errors = array();
  187. foreach ($csv_data as $row) {
  188. if (!isset($row['order_no'])) {
  189. $errors[] = __('Invalid row format in CSV.', 'studiou-wc-ord-print-statuses');
  190. continue;
  191. }
  192. $order = wc_get_order($row['order_no']);
  193. if (!$order) {
  194. $errors[] = sprintf(__('Order %s not found.', 'studiou-wc-ord-print-statuses'), $row['order_no']);
  195. continue;
  196. }
  197. $order->update_status('completed');
  198. $order->update_meta_data('external_ref_ord_delivered', current_time('mysql'));
  199. $order->save();
  200. $updated_count++;
  201. }
  202. return array(
  203. 'updated_count' => $updated_count,
  204. 'errors' => $errors
  205. );
  206. }
  207. /**
  208. * Parse CSV file
  209. *
  210. * @param string $file_path Path to CSV file
  211. * @return array|false Parsed CSV data or false on error
  212. */
  213. public static function parse_csv($file_path) {
  214. $csv_data = array();
  215. if (($handle = fopen($file_path, "r")) !== FALSE) {
  216. $headers = fgetcsv($handle, 1000, ",");
  217. while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
  218. $csv_data[] = array_combine($headers, $data);
  219. }
  220. fclose($handle);
  221. }
  222. return $csv_data;
  223. }
  224. }