class-db-manager.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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->prefix}wc_product_attributes_lookup` `product_variations_attr`
  53. ON
  54. (
  55. `product_variations_attr`.`product_id` = `product_variations`.`ID` AND `product_variations_attr`.`product_or_parent_id` = `products`.`ID` AND `product_variations_attr`.`taxonomy` = 'pa_format'
  56. )
  57. LEFT JOIN `{$wpdb->terms}` `product_variations_name`
  58. ON
  59. (
  60. `product_variations_name`.`term_id` = `product_variations_attr`.`term_id`
  61. )
  62. )
  63. LEFT JOIN `{$wpdb->postmeta}` `product_meta`
  64. ON
  65. (
  66. `product_meta`.`post_id` = `products`.`ID` AND `product_meta`.`meta_key` = '_thumbnail_id'
  67. )
  68. )
  69. LEFT JOIN `{$wpdb->posts}` `images`
  70. ON
  71. (
  72. `images`.`ID` = `product_meta`.`meta_value` AND `images`.`post_type` = 'attachment'
  73. )
  74. JOIN `{$wpdb->term_relationships}` `tr`
  75. ON
  76. (`products`.`ID` = `tr`.`object_id`)
  77. )
  78. JOIN `{$wpdb->term_taxonomy}` `tt`
  79. ON
  80. (
  81. `tr`.`term_taxonomy_id` = `tt`.`term_taxonomy_id`
  82. )
  83. )
  84. JOIN `{$wpdb->terms}` `t`
  85. ON
  86. (`tt`.`term_id` = `t`.`term_id`)
  87. )
  88. WHERE
  89. `orders`.`id` IN (" . implode(',', array_map('intval', $order_ids)) . ")
  90. AND `tt`.`taxonomy` = 'product_cat'
  91. ORDER BY
  92. `orders`.`id`
  93. ";
  94. $results = $wpdb->get_results($query);
  95. // Error handling for database query failures
  96. if ($results === false) {
  97. UtilsLog::log('SQL Error: ' . $wpdb->last_error);
  98. return array();
  99. }
  100. return $results;
  101. }
  102. /**
  103. * Set order status to 'to-print'
  104. *
  105. * @param array $order_ids Array of order IDs
  106. * @return int Number of orders updated
  107. */
  108. public static function set_orders_to_prepare_printing($order_ids) {
  109. $updated_count = 0;
  110. foreach ($order_ids as $order_id) {
  111. UtilsLog::log('Setting status Order #' . $order_id . ' to "to-print"');
  112. $order = wc_get_order($order_id);
  113. if ($order) {
  114. $order->update_status('to-print');
  115. $order->update_meta_data('to_print_date', current_time('mysql'));
  116. $order->save();
  117. $updated_count++;
  118. UtilsLog::log('Order #' . $order_id . ' marked as "to-print"');
  119. UtilsLog::message('Order #' . $order_id . ' marked as "to-print"' );
  120. } else {
  121. UtilsLog::log('Order #' . $order_id . ' not found');
  122. }
  123. }
  124. return $updated_count;
  125. }
  126. /**
  127. * Set order status to 'in-print'
  128. *
  129. * @param array $order_ids Array of order IDs
  130. * @return int Number of orders updated
  131. */
  132. public static function set_orders_to_in_printing($order_ids) {
  133. $updated_count = 0;
  134. foreach ($order_ids as $order_id) {
  135. $order = wc_get_order($order_id);
  136. if ($order) {
  137. $order->update_status('in-print');
  138. $order->update_meta_data('in_print_date', current_time('mysql'));
  139. $order->save();
  140. $updated_count++;
  141. UtilsLog::message('Order #' . $order_id . ' marked as "in-print"' );
  142. }
  143. }
  144. return $updated_count;
  145. }
  146. /**
  147. * Import inprint protocol data
  148. *
  149. * @param array $csv_data Parsed CSV data
  150. * @return array Array with updated count and errors
  151. */
  152. public static function import_inprint_protocol($csv_data) {
  153. $updated_count = 0;
  154. $errors = array();
  155. foreach ($csv_data as $row) {
  156. if (!isset($row['order_no']) || !isset($row['externalorder']) || !isset($row['externalorderdate'])) {
  157. $errors[] = __('Invalid row format in CSV.', 'studiou-wc-ord-print-statuses');
  158. continue;
  159. }
  160. $order = wc_get_order($row['order_no']);
  161. if (!$order) {
  162. $errors[] = sprintf(__('Order %s not found.', 'studiou-wc-ord-print-statuses'), $row['order_no']);
  163. continue;
  164. }
  165. $order->update_status('in-print');
  166. $order->update_meta_data('in_print_date', current_time('mysql'));
  167. $order->update_meta_data('external_ref_ord_no', $row['externalorder']);
  168. $order->update_meta_data('external_ref_ord_date', $row['externalorderdate']);
  169. $order->save();
  170. $updated_count++;
  171. }
  172. return array(
  173. 'updated_count' => $updated_count,
  174. 'errors' => $errors
  175. );
  176. }
  177. /**
  178. * Import delivered protocol data
  179. *
  180. * @param array $csv_data Parsed CSV data
  181. * @return array Array with updated count and errors
  182. */
  183. public static function import_delivered_protocol($csv_data) {
  184. $updated_count = 0;
  185. $errors = array();
  186. foreach ($csv_data as $row) {
  187. if (!isset($row['order_no'])) {
  188. $errors[] = __('Invalid row format in CSV.', 'studiou-wc-ord-print-statuses');
  189. continue;
  190. }
  191. $order = wc_get_order($row['order_no']);
  192. if (!$order) {
  193. $errors[] = sprintf(__('Order %s not found.', 'studiou-wc-ord-print-statuses'), $row['order_no']);
  194. continue;
  195. }
  196. $order->update_status('completed');
  197. $order->update_meta_data('external_ref_ord_delivered', current_time('mysql'));
  198. $order->save();
  199. $updated_count++;
  200. }
  201. return array(
  202. 'updated_count' => $updated_count,
  203. 'errors' => $errors
  204. );
  205. }
  206. /**
  207. * Parse CSV file
  208. *
  209. * @param string $file_path Path to CSV file
  210. * @return array|false Parsed CSV data or false on error
  211. */
  212. public static function parse_csv($file_path) {
  213. $csv_data = array();
  214. if (($handle = fopen($file_path, "r")) !== FALSE) {
  215. $headers = fgetcsv($handle, 1000, ",");
  216. while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
  217. $csv_data[] = array_combine($headers, $data);
  218. }
  219. fclose($handle);
  220. }
  221. return $csv_data;
  222. }
  223. }