Saving and more fun features
authorCameron Ball <cameron@getapproved.com.au>
Thu, 23 Jul 2015 08:39:34 +0000 (16:39 +0800)
committerCameron Ball <cameron@getapproved.com.au>
Thu, 23 Jul 2015 08:39:34 +0000 (16:39 +0800)
.gitignore
public_html/breakdown.html
public_html/index.html
public_html/upload.php

index 80f408e..3637fa8 100644 (file)
@@ -1 +1,2 @@
 nbproject/
+uploads/
index cb9689a..bd7909a 100644 (file)
                 clear: both;
             }
             
-            .sprite td.line-left {
+            .line-left {
                 border-left: 2px solid transparent;
             }
+
+            .line-right {
+                border-right: 2px solid transparent;
+            }
             
-            .sprite td.line-top {
+            .line-top {
                 border-top: 2px solid transparent;
             }
             
-            .sprite.measurements td.line-left {
+            .line-bottom {
+                border-bottom: 2px solid transparent;
+            }
+            
+            .sprite.measurements .line-left {
                 border-left: 2px solid black;
             }
             
-            .sprite.measurements td.line-top {
+            .sprite.measurements .line-right {
+                border-right: 2px solid black;
+            }
+            
+            .sprite.measurements .line-top {
                 border-top: 2px solid black;
             }
             
+            .sprite.measurements .line-bottom {
+                border-bottom: 2px solid black;
+            }
+            
+            .sprite.measurements .dimension-top {
+                transform: translate(50%, -50%);
+            }
+            
+            .sprite.measurements .dimension-bottom {
+                transform: translate(50%, 50%);
+            }
+
+            .sprite.measurements .dimension-left {
+                transform: translateY(50%);
+            }
+            
+            .sprite.measurements .dimension-right {
+                transform: translateY(50%);
+            }
+            
+            .sprite.measurements .dimension-left .line-left, .sprite.measurements .dimension-right .line-right {
+                height: 100%;
+            }
+            
             .sprite .height {
                 visibility: hidden;
                 padding-left: 5px;
             .sprite.measurements .height, .sprite.measurements .width {
                 visibility: visible;
             }
+            
+            .sprite .grid {
+                border: 1px solid red;
+            }
                         
             /*.sprite {
                 float: left;
             <ul class="nav nav-tabs">
                 <li class="nav active"><a href="#comparison" data-toggle="tab">Comparison</a></li>
                 <li class="nav"><a href="#materials" data-toggle="tab">Materials</a></li>
-                <li class="nav"><a href="#C" data-toggle="tab">C</a></li>
             </ul>
             
             <div class="tab-content">
                 </div>
                 <div class="tab-pane fade in" id="materials">
                     <div class="col-md-6">
-                        <h2>Dimensions</h2>
+                        <h2>Summary</h2>
                         <div class="sprite measurements col-md-8">
                             <?php echo $ironlak_sprite; ?>
                         </div>
-                        <div class="col-md-4 vertical-line">&nbsp;</div>
+                        <div class="col-md-4">
+                            <h3>Cost of materials</h3>
+                            <?php $materials = $cost_of_lumber + $cost_of_cans; ?>
+                            <?php $materials_used = $value_of_lumber_used + $value_of_can_used; ?>
+                            <table class="table">
+                                <tr>
+                                    <th>Wood</th>
+                                    <td><?php echo $cost_of_lumber; ?></td>
+                                </tr>
+                                <tr>
+                                    <th>Paint</th>
+                                    <td><?php echo $cost_of_cans; ?></td>
+                                </tr>
+                                <tr>
+                                    <td></td>
+                                    <td><strong><?php echo $materials; ?></strong></td>
+                                </tr>
+                            </table>
+                            
+                            <h3>Value of materials used</h3>
+                            <table class="table">
+                                <tr>
+                                    <th>Wood</th>
+                                    <td><?php echo $value_of_lumber_used; ?></td>
+                                </tr>
+                                <tr>
+                                    <th>Paint</th>
+                                    <td><?php echo $value_of_can_used; ?></td>
+                                </tr>
+                                <tr>
+                                    <td></td>
+                                    <td><strong><?php echo $materials_used; ?></strong></td>
+                                </tr>
+                            </table>
+                                
+                            <h3>Profit @ <?php echo $markup*100; ?>% Markup</h3>
+                            <table class="table">
+                                <tr>
+                                    <th>Sell price</th>
+                                    <td><?php echo $materials_used*$markup; ?></td>
+                                </tr>
+                                <tr>
+                                    <th>Less materials</th>
+                                    <td><?php echo $materials_used*$markup - $materials; ?></td>
+                                </tr>
+                            </table>
+                                
+                        </div>
                     </div>
                     <div class="col-md-6">
-                        <h2>Paint</h2>
+                        <h2>Materials Breakdown</h2>
                         <div class="col-md-12">
+                            <h3>Paint</h3>
                             <?php echo $spray_can_breakdown; ?>
                         </div>
                         
-                        <h2 class="clearfix">Wood</h2>
                         <div class="col-md-12">
+                            <h3>Paint</h3>
                             <?php echo $lumber_breakdown; ?>
                         </div>
                     </div>
                 </div>
-                <div class="tab-pane fade in" id="C">
-                    nice meme :]
-                </div>
             </div>
         </div>
         
                 var width = $(".sprite:first table tr:first td").length;
                 
                 $(sprite).find('tr').each(function() {
-                    $(this).append("<td></td>");
+                    $(this).append("<td class=\"added\"></td>");
                 });
                 
+                $(sprite).find('tr:first td:last').append('<div class="line-top"></div>');
+                $(sprite).find('tr:first td:last').addClass('dimension-top');
+                
+                $(sprite).find('tr:last td:last').append('<div class="line-bottom"></div>');
+                $(sprite).find('tr:last td:last').addClass('dimension-bottom');
+        
                 $(sprite).find('tr').each(function(){
-                    $(this).find('td').eq(0).before('<td></td>');
+                    $(this).find('td').eq(0).before('<td class="added"></td>');
                 });
             
                 for (var i=0; i<$(sprite).find("tr:last td").length; i++) {
-                    var cells = cells ? cells + '<td></td>' : '<tr><td></td>';
+                    var cells = cells ? cells + '<td class="added"></td>' : '<tr><td class="added"></td>';
                 }
-                cells += "<td></td></tr><tr><td></td><td class=\"line-top width\" colspan=\"" + width + "\"><?php echo $sprite_width_cm; ?></td></tr>";
+                cells += "<td class=\"added\"></td></tr><tr><td class=\"added\"></td><td class=\"line-top width added\" colspan=\"" + width + "\"><?php echo $sprite_width_cm; ?></td></tr>";
                 $(sprite).find("tr:last").after(cells);
                         
-                $(sprite).find("tr:first td:last").after("<td class=\"line-left\ height\" rowspan=\"" + height + "\"><?php echo $sprite_height_cm; ?></td>")
+                $(sprite).find('tr:nth-last-child(2) td:nth-last-child(2)').append('<div class="line-left"></div>');
+                $(sprite).find('tr:nth-last-child(2) td:nth-last-child(2)').addClass('dimension-left');
+
+                $(sprite).find('tr:nth-last-child(2) td:first').append('<div class="line-right"></div>');
+                $(sprite).find('tr:nth-last-child(2) td:first').addClass('dimension-right');
+
+                $(sprite).find("tr:first td:last").after("<td class=\"line-left\ height added\" rowspan=\"" + height + "\"><?php echo $sprite_height_cm; ?></td>")
             };
             
             padSprite($(".measurements"));
                 var colour = $(this).attr('colour');
                 $("." + colour).toggleClass("highlighted");
             });
+            
+            function nextTab(elem) {
+              $(elem + ' li.active')
+                .next()
+                .find('a[data-toggle="tab"]')
+                .click();
+            }
+            
+            $(document).keypress(function(e) {
+              if(e.which == 110) {
+                var active = $(".nav li.active");
+                var tabs = active.parent().children();
+                tabs.eq((tabs.index(active) + 1) % tabs.length).find('a[data-toggle="tab"]').click();
+              }
+              
+              if(e.which == 103) {
+                  $(".sprite tr td:not(.added)").toggleClass("grid");
+              }
+            });
         </script>
     </body>
 </html>
index 8dc8c4f..170c692 100644 (file)
@@ -17,6 +17,7 @@ and open the template in the editor.
             Wood size (for nxn enter n): <input type="text" name="wood_size"><br />
             Wood length: <input type="text" name="wood_length"><br />
             Wood cost/length: <input type="text" name="wood_cost"><br />
+            Markup % as int: <input type="text" name="markup"><br />
             <input type="submit" value="Upload Image" name="submit">
         </form>
     </body>
index 777c5f7..a37b476 100644 (file)
@@ -129,7 +129,8 @@ function count_pixels($array) {
 }
 
 function lumber_breakdown($cost_per_length, $wood_length, $wood_size, $num_blocks) {
-    $lengths_required = ceil($num_blocks*$wood_size/$wood_length);
+    $lumber_percent = $num_blocks*$wood_size/$wood_length;
+    $lengths_required = ceil($lumber_percent);
     $total_lumber_cost = $lengths_required * $cost_per_length;
     
     return array_to_table(
@@ -137,20 +138,25 @@ function lumber_breakdown($cost_per_length, $wood_length, $wood_size, $num_block
                 array(
                     'Num blocks',
                     'No of ' . $wood_length . 'm lengths required',
-                    'Cost @ ' . $cost_per_length . ' per length'
+                    '% used',
+                    'Cost @ ' . $cost_per_length . ' per length',
+                    '% Price'
                 ),
                 array(
                     $num_blocks,
                     $lengths_required,
-                    $total_lumber_cost
+                    round($lumber_percent,4),
+                    $total_lumber_cost,
+                    round($lumber_percent*$cost_per_length,4)
                 )
             )
     );
 }
 
-function lumber_cost($cost_per_length, $wood_length, $wood_size, $num_blocks) {
-    $lengths_required = ceil($num_blocks*$wood_size/$wood_length);
-    return $lengths_required * $cost_per_length;
+function lumber_cost($cost_per_length, $wood_length, $wood_size, $num_blocks, $percent_cost = FALSE) {
+    $percent = $num_blocks*$wood_size/$wood_length;
+    $lengths_required = ceil($percent);
+    return $percent_cost ? $percent * $cost_per_length : $lengths_required * $cost_per_length;
 }
 
 function colour_count($array, $colour_map = NULL) {
@@ -199,11 +205,12 @@ function colour_breakdown($colour_count, $block_size, $can_coverage, $coats, $co
     foreach($colour_count as $colour => $count) {
         $can_percent = $count * $block_size * $block_size * $coats / $can_coverage;
         $num_needed = ceil($can_percent);
+        $running_cost_percent = isset($running_cost_percent) ? $running_cost_percent + $can_percent*end($discount_matrix) : $can_percent*end($discount_matrix);
         if(isset($colour_names[$colour]))
-            $output .= '<tr class="colour-row" colour="' . $colour . '"><td bgcolor="' . $colour . '"></td><td>' . $colour_names[$colour] . '</td><td>' . $num_needed . '</td><td>' . $can_percent . '</td><td>' . $cost_per_can*$num_needed . '</td><td>' . $can_percent * $cost_per_can . '</td></tr>';
+            $output .= '<tr class="colour-row" colour="' . $colour . '"><td bgcolor="' . $colour . '"></td><td>' . $colour_names[$colour] . '</td><td>' . $num_needed . '</td><td>' . round($can_percent,4) . '</td><td>' . $cost_per_can*$num_needed . '</td><td>' . round($can_percent * $cost_per_can,4) . '</td></tr>';
     }
     
-    $output .= '</table>';
+    $output .= '<tr><td colspan="2"></td><td colspan="2"><strong>' . $total_cans . '</strong></strong></td><td><strong>'. $total_cans*$cost_per_can . '</strong></td><td><strong>' . round($running_cost_percent, 4) . '</strong></td></table>';
     
     return $output;
 }
@@ -221,10 +228,26 @@ function spray_can_cost($colour_count, $block_size, $can_coverage, $coats, $disc
             $cost_per_can = $cost;
     }
     
-    return $percent_cost ? end($discount_matrix)*$total_cans : $total_cans*$cost_per_can;
+    return round($percent_cost ? end($discount_matrix)*$total_cans : $total_cans*$cost_per_can,4);
 }
 
-if(isset($_POST["submit"])) {
+if(isset($_POST["submit"]) || isset($_GET['sprite'])) {
+    if(isset($_POST['submit'])) {
+        if(isset($_FILES['fileToUpload'])) {
+            $image = $_FILES['fileToUpload']['tmp_name'];
+        }
+        
+        if(isset($_POST['sprite'])) {
+            $image = "uploads/" . $_POST['sprite'] . '.png';
+        }
+    }
+    
+    if(isset($_GET['sprite'])) {
+        $image = 'uploads/' . $_GET['sprite'] . '.png';
+    }
+
+    $hash = substr(md5_file($image), 0, 8);
+    
     $ironlak_colours = array(
         "532f18" => "Earth",
         "753e00" => "Swiss",
@@ -329,30 +352,47 @@ if(isset($_POST["submit"])) {
         5 => 7.95 // 0-5 cans
     );
     
-    $pixel_array = image_to_array($_FILES["fileToUpload"]["tmp_name"]);
-    $sprite_width_cm = count($pixel_array[0]) * $_POST['wood_size'] * 100 . "cm";
-    $sprite_height_cm = count($pixel_array) * $_POST['wood_size'] * 100 . "cm";
+    if(isset($_POST['submit'])) {
+        $vars['wood_size'] = $_POST['wood_size'];
+        $vars['wood_length'] = $_POST['wood_length'];
+        $vars['wood_cost'] = $_POST['wood_cost'];
+        $vars['markup'] = $_POST['markup'];
+    } elseif(file_exists('uploads/'.$hash.'.txt')) {
+        $vars=  unserialize(file_get_contents('uploads/'.$hash.'.txt'));
+    }
+    
+    $pixel_array = image_to_array($image);
+    $sprite_width_cm = count($pixel_array[0]) * $vars['wood_size'] * 100 . "cm";
+    $sprite_height_cm = count($pixel_array) * $vars['wood_size'] * 100 . "cm";
     $colour_map = generate_colourmap($pixel_array, $ironlak_colours);
     $original_sprite = array_to_sprite($pixel_array);
     $ironlak_sprite =  array_to_sprite($pixel_array, $colour_map);
     
-    $lumber_breakdown = lumber_breakdown($_POST['wood_cost'], $_POST['wood_length'], $_POST['wood_size'], count_pixels($pixel_array));
+    $lumber_breakdown = lumber_breakdown($vars['wood_cost'], $vars['wood_length'], $vars['wood_size'], count_pixels($pixel_array));
     
     $original_colour_table = colour_count_table(colour_count($pixel_array));
     $ironlak_colour_table  = colour_count_table(colour_count($pixel_array, $colour_map));
     
-    $spray_can_breakdown = colour_breakdown(colour_count($pixel_array, $colour_map), $_POST['wood_size'], 2.2, 3, $ironlak_colours, $discount_matrix);
+    $spray_can_breakdown = colour_breakdown(colour_count($pixel_array, $colour_map), $vars['wood_size'], 2.2, 3, $ironlak_colours, $discount_matrix);
         
-    $cost_of_cans = spray_can_cost(colour_count($pixel_array, $colour_map), $_POST['wood_size'], 2.2, 3, $discount_matrix);
-    $value_of_can_used = spray_can_cost(colour_count($pixel_array, $colour_map), $_POST['wood_size'], 2.2, 3, $discount_matrix, TRUE);
-//    echo $original_sprite;
-//    echo $original_colour_table;
-//    
-//    echo $ironlak_sprite;
-//    echo $ironlak_colour_table;
-//    
-//    echo $lumber_breakdown;
-//    echo $spray_can_breakdown;
+    $cost_of_cans = spray_can_cost(colour_count($pixel_array, $colour_map), $vars['wood_size'], 2.2, 3, $discount_matrix);
+    $value_of_can_used = spray_can_cost(colour_count($pixel_array, $colour_map), $vars['wood_size'], 2.2, 3, $discount_matrix, TRUE);
+    $cost_of_lumber = lumber_cost($vars['wood_cost'], $vars['wood_length'], $vars['wood_size'], count_pixels($pixel_array));
+    $value_of_lumber_used = lumber_cost($vars['wood_cost'], $vars['wood_length'], $vars['wood_size'], count_pixels($pixel_array), TRUE);
+    
+    $markup = $vars['markup'];
+
+    $filename = 'uploads/' . $hash . '.png';
+    
+    if(!file_exists($filename)) {
+        move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $filename);
+        $fp = fopen('uploads/'. $hash . '.txt', 'w');
+        fwrite($fp, serialize($vars));
+    }
+    
+    if(!isset($_GET['sprite'])) {
+        header("location: upload.php?sprite=$hash");
+    }
     
     require_once('./breakdown.html');
 }