class-db-manager.php 9.1 KB

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