-
Notifications
You must be signed in to change notification settings - Fork 8
/
inventory.php
executable file
·247 lines (213 loc) · 11.1 KB
/
inventory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php
//Exit if not called in proper context
if (!defined('ABSPATH')) exit();
//Save Inventory Values - AJAX
add_action('wp_ajax_save_inventory_values', 'foxyshop_save_inventory_values_ajax');
function foxyshop_save_inventory_values_ajax() {
if (!check_admin_referer('update-foxyshop-inventory')) return;
foxyshop_inventory_count_update(sanitize_text_field($_POST['code']), sanitize_text_field($_POST['new_count']), sanitize_text_field($_POST['product_id']), true);
die;
}
add_action('admin_init', 'foxyshop_inventory_update');
function foxyshop_inventory_update() {
//Saving Values From Uploaded Data
if (isset($_POST['foxyshop_inventory_updates'])) {
if (!check_admin_referer('import-foxyshop-inventory-updates')) return;
$lines = preg_split("/(\r\n|\n|\r)/", sanitize_textarea_field($_POST['foxyshop_inventory_updates']));
$save_count = 0;
foreach ($lines as $line) {
$line = explode("\t", $line);
if (count($line) < 5) continue;
if ($line[0] == "ID") continue;
$productid = (int)$line[0];
$productcode = $line[2];
$newcount = (int)$line[4];
//Update
foxyshop_inventory_count_update($productcode, $newcount, $productid);
$save_count++;
}
wp_redirect('edit.php?post_type=foxyshop_product&page=foxyshop_inventory_management_page&importcompleted='.$save_count);
die;
}
}
add_action('admin_menu', 'foxyshop_inventory_menu');
function foxyshop_inventory_menu() {
add_submenu_page('edit.php?post_type=foxyshop_product', __('Inventory Management'), __('Inventory'), apply_filters('foxyshop_inventory_perm', 'manage_options'), 'foxyshop_inventory_management_page', 'foxyshop_inventory_management_page');
}
function foxyshop_inventory_management_page() {
global $foxyshop_settings, $wp_version;
?>
<div class="wrap">
<div class="icon32" id="icon-tools"><br></div>
<h2>Manage Inventory Levels</h2>
<?php
//Confirmation Saved
if (isset($_GET['saved'])) echo '<div class="updated"><p>' . __('Your New Inventory Levels Have Been Saved.') . '</p></div>';
//Import Completed
if (isset($_GET['importcompleted'])) echo '<div class="updated"><p>'
. sprintf(
__('Import completed: %s records updated.'),
(int)sanitize_text_field($_GET['importcompleted'])
) . '</p></div>';
?>
<table cellpadding="0" cellspacing="0" border="0" class="wp-list-table widefat foxyshop-list-table" id="inventory_level" style="margin-top: 14px;">
<thead>
<tr>
<th><span><?php _e('ID'); ?></span><span class="sorting-indicator"></span></th>
<th><span><?php _e('Name'); ?></span><span class="sorting-indicator"></span></th>
<th><span><?php _e('Code'); ?></span><span class="sorting-indicator"></span></th>
<th><span><?php _e('Variation'); ?></span><span class="sorting-indicator"></span></th>
<th><span><?php _e('Update'); ?></span><span class="sorting-indicator"></span></th>
<th><span><?php _e('Stock Lvl'); ?></span><span class="sorting-indicator"></span></th>
<th><span><?php _e('Alert Lvl'); ?></span><span class="sorting-indicator"></span></th>
</tr>
</thead>
<tfoot>
<tr>
<th><?php _e('ID'); ?></th>
<th><?php _e('Name'); ?></th>
<th><?php _e('Code'); ?></th>
<th><?php _e('Variation'); ?></th>
<th><?php _e('Update'); ?></th>
<th><?php _e('Stock Lvl'); ?></th>
<th><?php _e('Alert Lvl'); ?></th>
</tr>
</tfoot>
<tbody>
<?php
$args = array('post_type' => 'foxyshop_product', 'post_status' => 'publish', 'numberposts' => "-1", "orderby" => "id", "order" => "ASC", "meta_key" => "_inventory_levels", "meta_compare" => "!=", "meta_value" => "");
$product_list = get_posts($args);
$exported = "ID\tName\tCode\tVariation\tInventory";
$i = 0;
$alternate = "";
foreach ($product_list as $single_product) {
$product = foxyshop_setup_product($single_product, true);
$inventory_levels = get_post_meta($single_product->ID,'_inventory_levels',TRUE);
if (!is_array($inventory_levels)) $inventory_levels = array();
foreach ($inventory_levels as $ivcode => $iv) {
$i++;
if (!isset($iv['alert'])) $iv['alert'] = $foxyshop_settings['inventory_alert_level'];
$inventory_alert = (int)($iv['alert'] == '' ? $foxyshop_settings['inventory_alert_level'] : $iv['alert']);
$inventory_count = str_replace(",", "", $iv['count']);
$variation = " ";
foreach ($product['variations'] as $product_variation) {
$product_variation1 = preg_split("/(\r\n|\n)/", $product_variation['value']);
foreach ($product_variation1 as $product_variation2) {
if (strpos($product_variation2, "c:" . $ivcode) !== false) {
$variation = str_replace("*", "", substr($product_variation2,0,strpos($product_variation2,"{")));
}
}
}
$exported .= "\n";
$exported .= $product['id'] . "\t";
$exported .= str_replace("\t", "", $product['name']) . "\t";
$exported .= str_replace("\t", "", $ivcode) . "\t";
$exported .= str_replace("\t", "", $variation) . "\t";
$exported .= $inventory_count;
$grade = "A";
if ($inventory_count <= $inventory_alert) $grade = "X";
if ($inventory_count <= 0) $grade = "U";
echo '<tr>'."\n";
echo '<td><strong>' . esc_html($product['id']) . '</strong></td>'."\n";
echo '<td><strong><a href="post.php?post=' . esc_attr($product['id']) . '&action=edit" tabindex="1">' . wp_kses_post($product['name']) . '</a></strong></td>'."\n";
echo '<td>' . esc_html($ivcode) . '</td>'."\n";
echo '<td>' . esc_html($variation) . '</td>'."\n";
//The Form
echo '<td>';
echo '<form>';
echo '<input type="hidden" name="original_count_' . esc_attr($i) . '" id="original_count_' . esc_attr($i) . '" value="' . esc_attr($inventory_count) . '" />';
echo '<input type="hidden" name="productid_' . esc_attr($i) . '" id="productid_' . esc_attr($i) . '" value="' . esc_attr($single_product->ID) . '" />';
echo '<input type="hidden" name="code_' . esc_attr($i) . '" id="code_' . esc_attr($i) . '" value="' . esc_attr($ivcode) . '" />';
echo '<input type="text" name="new_count_' . esc_attr($i) . '" id="new_count_' . esc_attr($i) . '" value="' . esc_attr($inventory_count) . '" data-id="' . esc_attr($i) . '" class="inventory_update_width" autocomplete="off" />';
echo '<div class="foxyshop_wait" id="wait_' . esc_attr($i) . '"></div>';
echo "</form>\n";
echo "</td>\n";
echo '<td id="current_inventory_' . esc_attr($i) . '" class="inventory' . esc_attr($grade) . '">' . esc_html($inventory_count) . '</td>'."\n";
echo '<td id="current_inventory_alert_' . esc_attr($i) . '">' . esc_html($inventory_alert) . '</td>'."\n";
echo '</tr>'."\n";
}
}
?>
</tbody>
</table>
<br /><br />
<?php
$export_icon = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAACH0lEQVR4nKWSTUhUYRSGnzv33snRdEY0M0dJaSDTzRBkiZhZkNCiTZC1CIQQahFEq9q3aBFRqzZFkUQRuayFEKlZhv0ZGsUw6VD4N5rj5KjT3O9+p4VphCVKZ3PO5nl4Obzwn2M0tLSdBE6vk7vedfvETQDjQOs9qT1Suy66t72XEn8y2Hb51KglIiwk59YlEJHl29paMMPEm4/4sn2rQspOkAi2Exg9TGizQ372/ByAVZSX5tihbYTD4X/C8fkxWp80URZSjA1/JnesGiZ2LSZYS+Svs0MEcjZSWDTNpiKDl11vSXmmioHkqoKekQ4exe4ykorxLT1Dmcpg2x727PXR3Rn90PigouSvAldcLjxrITbXjzdvGl+xy3Y/OMrGMAy8tkFDY8Ds6ZqN/iEQEdJqnrOdzUwuRCktzKK8oA7LtDFNzZDqxDAMDMNDqRlGpC/LAtBa47ouIsKtwatMJL+wu6Se0fQkjwfekevNI60WCFULhqEoZAcPu185rnZLPUsCpRRKKSKJQcr9lbye6qcpeJz7+19wo66D6vydJGc8+N0Kevri5KS21Dw9Ohy3AJRSOI6D1hqfmY3X3sD58DVMLLQStHZIOd+pyjrIwKcogalKiuM1w9DNCsG5qkuICNrVKFForRERzoQucid2hXBuPbOJIBmc3z2IRCJkMhlEZBH+BS1Vdmnvoxl+wHMdWX78omA8SWT8/Vo6tWJ+AquVAo19QSjUAAAAAElFTkSuQmCC";
?>
<form method="post" name="foxyshop_inventory_import_form" action="edit.php?post_type=foxyshop_product&page=foxyshop_inventory_management_page">
<table class="widefat">
<thead>
<tr>
<th><img src="<?php echo esc_url($export_icon); ?>" alt="" /><?php _e('Import New Inventory Values'); ?></th>
</tr>
</thead>
<tbody>
<tr>
<td>
<p>
Copy and paste these values into Excel. Make your changes, then copy and paste back in and click update.<br />
You can also add new inventory levels by using the template to add new rows with code and inventory fields.
</p>
<textarea id="foxyshop_inventory_updates" name="foxyshop_inventory_updates" wrap="auto" style="float: left; width:650px;height: 200px;"><?php echo wp_kses($exported, []); ?></textarea>
<div style="clear: both;"></div>
<p><input type="submit" class="button-primary" value="<?php _e('Update Inventory Values'); ?>" /></p>
</td>
</tr>
</tbody>
</table>
<?php wp_nonce_field('import-foxyshop-inventory-updates'); ?>
</form>
</div>
<?php
function foxyshop_inline_inventory_js() {
echo "<script type='text/javascript'>
jQuery(document).ready(function($){
$(\".inventory_update_width\").blur(function() {
current_field_id = $(this).attr(\"id\");
current_id = $(\"#\" + current_field_id).attr(\"data-id\");
new_count = $(\"#\" + current_field_id).val();
$(\"#\" + current_field_id).val(new_count);
$(\"#\" + current_field_id).parents(\"tr\").removeClass(\"inventory_update_width_highlight\");
if (new_count != $(\"#original_count_\" + current_id).val()) {
var data = {
action: 'save_inventory_values',
\"_wpnonce\": \"" . wp_create_nonce('update-foxyshop-inventory') . "\",
\"code\": $(\"#code_\" + current_id).val(),
\"product_id\": $(\"#productid_\" + current_id).val(),
\"new_count\": new_count
};
$(\"#wait_\" + current_id).addClass(\"waiting\");
$.post(ajaxurl, data, function() {
$(\"#wait_\" + current_id).removeClass(\"waiting\");
$(\"#original_count_\" + current_id).val(new_count);
$(\"#current_inventory_\" + current_id).text(new_count);
if (new_count <= 0) {
$(\"#current_inventory_\" + current_id).removeClass().addClass(\"inventoryU\");
} else if (new_count <= parseInt($(\"#current_inventory_alert_\" + current_id).text())) {
$(\"#current_inventory_\" + current_id).removeClass().addClass(\"inventoryX\");
} else {
$(\"#current_inventory_\" + current_id).removeClass().addClass(\"inventoryA\");
}
});
}
});
$(\".inventory_update_width\").keypress(function(e) {
if (e.which == 13) {
$(this).trigger(\"blur\");
return false;
}
});
$(\".inventory_update_width\").focus(function() {
$(this).parents(\"tr\").addClass(\"inventory_update_width_highlight\");
});
$(\"#inventory_level\").tablesorter({
'cssDesc': 'asc sorted',
'cssAsc': 'desc sorted'
});
});
function foxyshop_format_number_single(num) { num = num.toString().replace(/\$|\,/g,''); if(isNaN(num)) num = \"0\"; sign = (num == (num = Math.abs(num))); num = Math.floor(num*100+0.50000000001); cents = num%100; num = Math.floor(num/100).toString(); if(cents<10) cents = \"0\" + cents; for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3)); return (((sign)?'':'-') + num); }
</script>";
}
add_action( 'admin_print_footer_scripts', 'foxyshop_inline_inventory_js' );
}
?>