Project

General

Profile

Statistics
| Branch: | Revision:

colonymech / docs / www / colonyscout / internal / includes / internalFunctions.php @ f59acf11

History | View | Annotate | Download (9.58 KB)

1
<?php
2
include_once($docRoot."internal/do_login.php");
3

    
4
function getPartInfo($partnumber) {
5
        //part info from database, image from disk, link to vendor page
6
        //must already be logged in
7
        $get_inventory_sql = "SELECT ID,VendorID,VendorPartNo,Name,Description,Qty,InStock,WhereUsed,ManufacturerName,ManufacturerPartNo FROM inventory WHERE vendorpartno='".$partnumber."' LIMIT 1";
8
        $get_inventory_res = mysqli_query($GLOBALS['mysqli'], $get_inventory_sql) or die(mysqli_error($GLOBALS['mysqli']));
9

    
10
        while ($r = mysqli_fetch_array($get_inventory_res)) {
11
                $partdata["data"]        =        $r;
12
                $partdata["vendor"]        =        getVendorNameFromID($r['VendorID']);        
13
                $partdata["thumb"]        =        getThumbFromDisk($partnumber);
14
                $partdata["url"]        =        getURL($partnumber,$partdata["vendor"]);
15
                $partdata["price"]        =        getPriceFromDatabase($partnumber);
16
                $partdata["datasheet"] = getDatasheetFromDisk($r['ManufacturerPartNo']);
17
                return $partdata;
18
        }
19
        return 0;
20
}
21

    
22
function getSystemInfo($systemID) {
23
        //must already be logged in
24
        $get_sys_sql = "SELECT * FROM systems WHERE id='".$systemID."' LIMIT 1";
25
        $get_sys_res = mysqli_query($GLOBALS['mysqli'], $get_sys_sql) or die(mysqli_error($GLOBALS['mysqli']));
26

    
27
        while ($r = mysqli_fetch_array($get_sys_res)) {
28
                $systemdata["data"]                =        $r;
29
                $systemdata["thumb"]        =        getSystemThumbFromDisk($r["Name"]."_".$r["Revision"]);
30
                return $systemdata;
31
        }
32
        return 0;
33
}
34

    
35
function getVendorID($vendorName) {
36
        //note must already be logged in
37
        $find_vendor_sql = "SELECT ID from vendors WHERE name='$vendorName' LIMIT 1";
38
        $find_vendor_res = mysqli_query($GLOBALS['mysqli'], $find_vendor_sql) or die(mysqli_error($GLOBALS['mysqli']));
39
                
40
        while ($vendor_info = mysqli_fetch_array($find_vendor_res)) {
41
                return $vendor_info['ID'];
42
        }
43
        
44
        return -1; //vendor not found
45
}
46

    
47
function getVendorNameFromID($vendorID) {
48
        //note must already be logged in
49
        $find_vendor_sql = "SELECT name from vendors WHERE ID='$vendorID' LIMIT 1";
50
        $find_vendor_res = mysqli_query($GLOBALS['mysqli'], $find_vendor_sql) or die(mysqli_error($GLOBALS['mysqli']));
51
                
52
        while ($vendor_info = mysqli_fetch_array($find_vendor_res)) {
53
                return $vendor_info['name'];
54
        }
55
        
56
        return -1; //vendor not found
57

    
58
//select name, (select name from vendors where vendors.id=inventory.vendorid) as vendorname from inventory;
59
}
60

    
61
function getVendorNameFromPartNumber($partnumber) {
62
        //note must already be logged in
63
        $find_vendor_sql = "select (select name from vendors WHERE vendors.id=inventory.vendorid) as vendorname from inventory WHERE vendorPartNo='".$partnumber."'";
64
        $find_vendor_res = mysqli_query($GLOBALS['mysqli'], $find_vendor_sql) or die(mysqli_error($GLOBALS['mysqli']));
65
                
66
        while ($vendor_info = mysqli_fetch_array($find_vendor_res)) {
67
                return $vendor_info['vendorname'];
68
        }
69
        return -1; //vendor not found
70
}
71

    
72

    
73
//used to return URL for part linking
74
function getURL($partnumber,$vendorname) {
75
        switch ($vendorname) {
76
                case "Digikey":
77
                        $search_term = str_replace("%2F","/",urlencode($partnumber)); //format for url, but keep slashes
78
                        return 'http://search.digikey.com/scripts/DkSearch/dksus.dll?Detail&name='.$search_term;                
79
                        break;
80
                case "SparkFun":
81
                        preg_match('@^(.+-)([^/]+)@i',$partnumber, $matches); //parts pages only use part number after category+dash
82
                        if (count($matches)<2) return 0; $search_term = urlencode($matches[2]);
83
                        return 'http://www.sparkfun.com/commerce/product_info.php?products_id='.$search_term;
84
                        break;
85
                case "Mouser": //doesn't get you to the product page, but does present it in search results
86
                        return 'http://www.mouser.com/Search/Refine.aspx?Keyword='.urlencode($partnumber);
87
                        break;
88
                case "Newark": //gets right to page, ready for parsing. their real search has session ids
89
                        return 'http://www.newark.com/'.urlencode($partnumber);
90
                        break;
91
                case "Pololu":
92
                        return 'http://www.pololu.com/catalog/product/'.urlencode($partnumber);
93
                        break;
94
                case "Gumstix":
95
                        return 'http://www.gumstix.com/store/catalog/advanced_search_result.php?osCsid=&keywords='.urlencode($partnumber);
96
                        break;
97
                case "Jameco":
98
                        return 'http://www.jameco.com/webapp/wcs/stores/servlet/ProductDisplay?langId=-1&productId='.urlencode($partnumber);
99
                        break;
100
                case "AllElectronics":
101
                        return 'http://www.allelectronics.com/index.php?page=search&search_query='.urlencode($partnumber);
102
                        break;
103
                case "Allied Electronics":
104
                        return 'http://www.alliedelec.com/Search/ProductDetail.aspx?SKU='.urlencode($partnumber);
105
                        break;
106
                case "":
107
                        return 'http://www.mcmaster.com/'.urlencode($partnumber);
108
                        break;
109
                default:
110
                        return 0;
111
                        break;
112
        }
113
}
114

    
115
function getPriceFromDatabase($partnumber) {
116
        //must already be logged in
117
        $get_inventory_sql = "SELECT cost_1,cost_10,cost_25,cost_50,cost_100 FROM inventory WHERE vendorpartno='".$partnumber."' LIMIT 1";
118
        $get_inventory_res = mysqli_query($GLOBALS['mysqli'], $get_inventory_sql) or die(mysqli_error($GLOBALS['mysqli']));
119
                                
120
        if (mysqli_num_rows($get_inventory_res)>0) {
121
                while ($r = mysqli_fetch_array($get_inventory_res)) {
122
                        //compile price table
123
                        $price_table[1]=$r["cost_1"];
124
                        $price_table[10]=$r["cost_10"];
125
                        $price_table[25]=$r["cost_25"];
126
                        $price_table[50]=$r["cost_50"];
127
                        $price_table[100]=$r["cost_100"];
128
                }
129
                return $price_table;
130
        }else {
131
                return 0;
132
        }
133
}
134

    
135
function getPriceTable($partnumber) {
136
        //must already be logged in
137
        $get_inventory_sql = "SELECT cost_1,cost_10,cost_25,cost_50,cost_100 FROM inventory WHERE vendorpartno='".$partnumber."' LIMIT 1";
138
        $get_inventory_res = mysqli_query($GLOBALS['mysqli'], $get_inventory_sql) or die(mysqli_error($GLOBALS['mysqli']));
139
                                
140
        if (mysqli_num_rows($get_inventory_res)>0) {
141
                while ($r = mysqli_fetch_array($get_inventory_res)) {
142
                        //change 0's to "-"
143
                        $r["cost_1"]=($r["cost_1"]==0) ? "-" : "$".$r["cost_1"];
144
                        $r["cost_10"]=($r["cost_10"]==0) ? "-" : "$".$r["cost_10"];
145
                        $r["cost_25"]=($r["cost_25"]==0) ? "-" : "$".$r["cost_25"];
146
                        $r["cost_50"]=($r["cost_50"]==0) ? "-" : "$".$r["cost_50"];
147
                        $r["cost_100"]=($r["cost_100"]==0) ? "-" : "$".$r["cost_100"];
148
                        
149
                        //compile price table
150
                        $price_html="
151
                        <table class=\"price\">
152
                        <tr><th><b>Qty</b></th><th><b>Extended Price</b></th></tr>
153
                        <tr><td>1</td><td>".$r["cost_1"]."</td></tr>
154
                        <tr><td>10</td><td>".$r["cost_10"]."</td></tr>
155
                        <tr><td>25</td><td>".$r["cost_25"]."</td></tr>
156
                        <tr><td>50</td><td>".$r["cost_50"]."</td></tr>
157
                        <tr><td>100</td><td>".$r["cost_100"]."</td></tr>
158
                        </table>";
159
                }
160
                return $price_html;
161
        }else {
162
                return 0;
163
        }
164
}
165

    
166
function getThumbFromDisk($partnumber) {
167
        //check disk for urlencode($partnumber)
168
        $file = "thumbs/".urlencode($partnumber).".jpg";
169
        return (file_exists($file)) ? $file : "thumbs/PhotoUnavailable.jpg";
170
}
171

    
172
function getSystemThumbFromDisk($systemnumber) { //consists of name+revision
173
        //check disk for urlencode($partnumber)
174
        $file = "thumbs/".$systemnumber.".jpg";
175
        return (file_exists($file)) ? $file : "thumbs/PhotoUnavailable.jpg";
176
}
177

    
178
function getAssemblyThumbFromDisk($stepnumber) { 
179
        //check disk for urlencode($partnumber)
180
        $file = "thumbs/".$stepnumber."_1.jpg";
181
        return (file_exists($file)) ? $file : "thumbs/PhotoUnavailable.jpg";
182
}
183

    
184
function getThumbForAssemblyStep($assemblyID,$stepID) { //consists of assemID+stepID
185
        $a = "<div class=\"pic\"><a href=\"\" rel=\"lightbox\"><img src=\"thumbs/assembly/assm.jpg\"></a></div>";
186
        return $a;
187
}
188

    
189
function saveThumb($img,$fullpath){
190
    $ch = curl_init ($img);
191
    curl_setopt($ch, CURLOPT_HEADER, 0);
192
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
193
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
194
    $rawdata=curl_exec($ch);
195
    curl_close ($ch);
196
    if(file_exists($fullpath)){
197
        unlink($fullpath);
198
    }
199
    $fp = fopen($fullpath,'x');
200
    fwrite($fp, $rawdata);
201
    fclose($fp);
202
}
203

    
204
function getDatasheetFromDisk($manufacturerpartnumber) {
205
        //check disk for urlencode($partnumber)
206
        $file = "datasheets/".$manufacturerpartnumber.".pdf";
207
        return (file_exists($file)) ? $file : "";
208
}
209

    
210
function getPriceForQty($partnumber, $qtyNeeded) {
211
        //returns the price for a certain quantity of parts (calcuated by the highest price break)
212
        $p = getPriceFromDatabase($partnumber); //cast everything as an int
213
        $p_int[10] = (int) $p[10]; $p_int[25] = (int) $p[25]; $p_int[50] = (int) $p[50]; $p_int[100] = (int) $p[100];
214
        
215
        if(($qtyNeeded>=100) && !empty($p_int[100])) {
216
                return ($qtyNeeded/100)*$p[100];
217
        } else if(($qtyNeeded>=50) && !empty($p_int[50])) {
218
                return ($qtyNeeded/50)*$p[50];
219
        } else if(($qtyNeeded>=25) && !empty($p_int[25])) {
220
                return ($qtyNeeded/25)*$p[25];
221
        } else if(($qtyNeeded>=10) && !empty($p_int[10])) {
222
                return ($qtyNeeded/10)*$p[10];
223
        } else { //use 1 price break
224
                return ($qtyNeeded)*$p[1];
225
        }
226
}
227

    
228
function getSystemsForPart($partID) {
229
        //get all the names of all systems that reference $partID
230
        //must already be logged in
231
        $get_sys_sql = "SELECT (SELECT name from systems where syspartlinks.systemid=systems.id AND systems.active=true) as SystemName from syspartlinks where partID=".$partID;
232
        $get_sys_res = mysqli_query($GLOBALS['mysqli'], $get_sys_sql) or die(mysqli_error($GLOBALS['mysqli']));
233
                                
234
        if (mysqli_num_rows($get_sys_res)>0) {
235
                while ($r = mysqli_fetch_array($get_sys_res)) {
236
                        //compile price table
237
                        $systems[]=$r["SystemName"];
238
                }
239
                return $systems;
240
        }else {
241
                return 0;
242
        }
243
}
244

    
245
function getPartCountForSystem($systemID, $distinct=false) {
246
        //get the total (not distinct) number of parts used by each system for the passed $systemID
247
        //must already be logged in
248
        $get_sys_sql = "SELECT count(systemID) as partCount from syspartlinks where systemID=".$systemID;
249
        $get_sys_res = mysqli_query($GLOBALS['mysqli'], $get_sys_sql) or die(mysqli_error($GLOBALS['mysqli']));
250
                                
251
        if (mysqli_num_rows($get_sys_res)>0) {
252
                while ($r = mysqli_fetch_array($get_sys_res)) {
253
                        //compile price table
254
                        $systems=$r["partCount"];
255
                }
256
                return $systems;
257
        }else {
258
                return 0;
259
        }
260
}
261

    
262

    
263
?>