| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; // Exit if accessed directly |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Get all product_cats |
| 9 |
* |
| 10 |
* @return mixed |
| 11 |
*/ |
| 12 |
function erp_acct_get_all_product_cats() { |
| 13 |
global $wpdb; |
| 14 |
|
| 15 |
$row = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'erp_acct_product_categories', ARRAY_A ); |
| 16 |
|
| 17 |
return $row; |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Get an single product |
| 22 |
* |
| 23 |
* @param $product_cat_no |
| 24 |
* |
| 25 |
* @return mixed |
| 26 |
*/ |
| 27 |
function erp_acct_get_product_cat( $product_cat_id ) { |
| 28 |
global $wpdb; |
| 29 |
|
| 30 |
$row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}erp_acct_product_categories WHERE id = %d GROUP BY parent", $product_cat_id ), ARRAY_A ); |
| 31 |
|
| 32 |
return $row; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Insert product data |
| 37 |
* |
| 38 |
* @param $data |
| 39 |
* |
| 40 |
* @return int |
| 41 |
*/ |
| 42 |
function erp_acct_insert_product_cat( $data ) { |
| 43 |
global $wpdb; |
| 44 |
|
| 45 |
$created_by = get_current_user_id(); |
| 46 |
$data['created_at'] = date( 'Y-m-d H:i:s' ); |
| 47 |
$data['created_by'] = $created_by; |
| 48 |
|
| 49 |
try { |
| 50 |
$wpdb->query( 'START TRANSACTION' ); |
| 51 |
$product_cat_data = erp_acct_get_formatted_product_cat_data( $data ); |
| 52 |
|
| 53 |
$wpdb->insert( |
| 54 |
$wpdb->prefix . 'erp_acct_product_categories', |
| 55 |
[ |
| 56 |
'name' => $product_cat_data['name'], |
| 57 |
'parent' => isset($product_cat_data['parent']['id']) ? $product_cat_data['parent']['id'] : 0, |
| 58 |
'created_at' => $product_cat_data['created_at'], |
| 59 |
'created_by' => $product_cat_data['created_by'], |
| 60 |
'updated_at' => $product_cat_data['updated_at'], |
| 61 |
'updated_by' => $product_cat_data['updated_by'], |
| 62 |
] |
| 63 |
); |
| 64 |
|
| 65 |
$product_cat_id = $wpdb->insert_id; |
| 66 |
|
| 67 |
$wpdb->query( 'COMMIT' ); |
| 68 |
} catch ( Exception $e ) { |
| 69 |
$wpdb->query( 'ROLLBACK' ); |
| 70 |
|
| 71 |
return new WP_error( 'product-exception', $e->getMessage() ); |
| 72 |
} |
| 73 |
|
| 74 |
return $product_cat_id; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Update product data |
| 79 |
* |
| 80 |
* @param $data |
| 81 |
* |
| 82 |
* @return int |
| 83 |
*/ |
| 84 |
function erp_acct_update_product_cat( $data, $id ) { |
| 85 |
global $wpdb; |
| 86 |
|
| 87 |
$updated_by = get_current_user_id(); |
| 88 |
$data['updated_at'] = date( 'Y-m-d H:i:s' ); |
| 89 |
$data['updated_by'] = $updated_by; |
| 90 |
|
| 91 |
try { |
| 92 |
$wpdb->query( 'START TRANSACTION' ); |
| 93 |
$product_cat_data = erp_acct_get_formatted_product_cat_data( $data ); |
| 94 |
|
| 95 |
$wpdb->update( |
| 96 |
$wpdb->prefix . 'erp_acct_product_categories', |
| 97 |
[ |
| 98 |
'name' => $product_cat_data['name'], |
| 99 |
'parent' => $product_cat_data['parent'], |
| 100 |
'created_at' => $product_cat_data['created_at'], |
| 101 |
'created_by' => $product_cat_data['created_by'], |
| 102 |
'updated_at' => $product_cat_data['updated_at'], |
| 103 |
'updated_by' => $product_cat_data['updated_by'], |
| 104 |
], |
| 105 |
[ |
| 106 |
'id' => $id, |
| 107 |
] |
| 108 |
); |
| 109 |
|
| 110 |
$wpdb->query( 'COMMIT' ); |
| 111 |
} catch ( Exception $e ) { |
| 112 |
$wpdb->query( 'ROLLBACK' ); |
| 113 |
|
| 114 |
return new WP_error( 'product-exception', $e->getMessage() ); |
| 115 |
} |
| 116 |
|
| 117 |
return $id; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Get formatted product data |
| 122 |
* |
| 123 |
* @param $data |
| 124 |
* @param $voucher_no |
| 125 |
* |
| 126 |
* @return mixed |
| 127 |
*/ |
| 128 |
function erp_acct_get_formatted_product_cat_data( $data ) { |
| 129 |
$product_cat_data['name'] = isset( $data['name'] ) ? $data['name'] : ''; |
| 130 |
$product_cat_data['parent'] = isset( $data['parent'] ) ? $data['parent'] : 0; |
| 131 |
$product_cat_data['created_at'] = isset( $data['created_at'] ) ? $data['created_at'] : ''; |
| 132 |
$product_cat_data['created_by'] = isset( $data['created_by'] ) ? $data['created_by'] : ''; |
| 133 |
$product_cat_data['updated_at'] = isset( $data['updated_at'] ) ? $data['updated_at'] : ''; |
| 134 |
$product_cat_data['updated_by'] = isset( $data['updated_by'] ) ? $data['updated_by'] : ''; |
| 135 |
|
| 136 |
return $product_cat_data; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Delete an product |
| 141 |
* |
| 142 |
* @param $product_cat_no |
| 143 |
* |
| 144 |
* @return void |
| 145 |
*/ |
| 146 |
function erp_acct_delete_product_cat( $product_cat_id ) { |
| 147 |
global $wpdb; |
| 148 |
|
| 149 |
$wpdb->delete( $wpdb->prefix . 'erp_acct_product_categories', [ 'id' => $product_cat_id ] ); |
| 150 |
} |
| 151 |
|