function getDoorStyleImg($door_style = null)
 {
     App::import('Model', 'Inventory.Door');
     $Door_Model = new Door();
     $data = $Door_Model->find('first', array('conditions' => array('Door.door_style' => $door_style)));
     return $data['Door']['door_image'];
 }
 function getOtherImage()
 {
     set_time_limit(0);
     $this->autoRender = false;
     $sql = "SELECT DoorStyle, ProfileOutsideImage, ProfileInsideImage FROM doorstyles";
     $datas = $this->Cabinet->query($sql);
     App::uses("Door", "Inventory.Model");
     $Door_model = new Door();
     $i = 1;
     foreach ($datas as $data) {
         $door_data = $Door_model->find('first', array('fields' => array('id', 'door_style', 'door_image', 'door_image_dir'), 'conditions' => array('Door.door_style' => $data['doorstyles']['DoorStyle']), 'recursive' => -1));
         $id = $door_data['Door']['id'];
         if (!empty($data['doorstyles']['ProfileOutsideImage'])) {
             mkdir(WWW_ROOT . 'files' . DS . 'door' . DS . 'outside_profile_image' . DS . $door_data['Door']['id'], 0777, TRUE);
             file_put_contents(WWW_ROOT . "files/door/outside_profile_image/{$id}/thumb_outside_profile_image.jpg", $data['doorstyles']['ProfileOutsideImage']);
             file_put_contents(WWW_ROOT . "files/door/outside_profile_image/{$id}/xvga_outside_profile_image.jpg", $data['doorstyles']['ProfileOutsideImage']);
             file_put_contents(WWW_ROOT . "files/door/outside_profile_image/{$id}/vga_outside_profile_image.jpg", $data['doorstyles']['ProfileOutsideImage']);
             $out_door_image_data['Door']['id'] = $door_data['Door']['id'];
             $out_door_image_data['Door']['outside_profile_image'] = "outside_profile_image.jpg";
             $out_door_image_data['Door']['outside_profile_image_dir'] = $id;
             $Door_image_model = new Door();
             $Door_image_model->save($out_door_image_data);
         }
         if (!empty($data['doorstyles']['ProfileInsideImage'])) {
             mkdir(WWW_ROOT . 'files' . DS . 'door' . DS . 'inside_profile_image' . DS . $door_data['Door']['id'], 0777, TRUE);
             file_put_contents(WWW_ROOT . "files/door/inside_profile_image/{$id}/thumb_inside_profile_image.jpg", $data['doorstyles']['ProfileInsideImage']);
             file_put_contents(WWW_ROOT . "files/door/inside_profile_image/{$id}/xvga_inside_profile_image.jpg", $data['doorstyles']['ProfileInsideImage']);
             file_put_contents(WWW_ROOT . "files/door/inside_profile_image/{$id}/vga_inside_profile_image.jpg", $data['doorstyles']['ProfileInsideImage']);
             $in_door_image_data['Door']['id'] = $door_data['Door']['id'];
             $in_door_image_data['Door']['inside_profile_image'] = "inside_profile_image.jpg";
             $in_door_image_data['Door']['inside_profile_image_dir'] = $id;
             $Door_image_model = new Door();
             $Door_image_model->save($in_door_image_data);
         }
     }
 }
 public function calculateCabinetPriceForBuilder($cabinet_id, $cabinet_color, $material_id, $door_id, $door_color, $drawer_id, $drawer_slide_id, $quantity = 1, $delivery_option = '', $delivery_charge = 0)
 {
     App::import("Model", "Inventory.Cabinet");
     App::uses("Material", "Inventory.Model");
     App::uses("Door", "Inventory.Model");
     App::uses("Color", "Inventory.Model");
     App::import("Model", "Inventory.Item");
     App::import("Model", "Inventory.CabinetsItem");
     App::import("Model", "Inventory.InventoryLookup");
     $cabinet_id = Sanitize::escape($cabinet_id);
     $material_id = Sanitize::escape($material_id);
     $door_id = Sanitize::escape($door_id);
     $door_color = Sanitize::escape($door_color);
     $cabinet_color = Sanitize::escape($cabinet_color);
     $drawer_id = Sanitize::escape($drawer_id);
     $drawer_slide_id = Sanitize::escape($drawer_slide_id);
     $debug_calculation = '';
     $cabinets_items_model = new CabinetsItem();
     $cabinets_items_model->recursive = 1;
     $cabinets_items = $cabinets_items_model->find('all', array('conditions' => array('cabinet_id' => $cabinet_id, 'accessories' => '0')));
     $cabinets_accessories = $cabinets_items_model->find('all', array('conditions' => array('cabinet_id' => $cabinet_id, 'accessories' => '1')));
     if (!empty($cabinet_id)) {
         $cabinet = new Cabinet();
         $cabinet_detail = $cabinet->find('first', array('conditions' => array('id' => $cabinet_id)));
     }
     // predefined values
     $debug_calculation .= "Blum Up Charge = {$this->bulm_up_charge} <br />";
     $debug_calculation .= "Default Markup Factor = {$this->default_markup} <br />";
     $debug_calculation .= "<br />";
     // calculate cabinet sqft
     $item_model = new Item();
     $cabinet_sqft = $this->calculateCabinetSQFTForBuilder($cabinets_items, $item_model, $material_id);
     $debug_calculation .= "Cabinet SQFT = {$cabinet_sqft} <br />";
     $debug_calculation .= "<br />";
     // calculate cabinet sqft
     $panel_sqft = $this->calculatePanelSQFTForBuilder($cabinets_items);
     $debug_calculation .= "Panel SQFT = {$panel_sqft} <br />";
     $debug_calculation .= "<br />";
     // calculate cabinet sqft
     $box_total = $this->calculateBoxTotalForBuilder($cabinets_items, $door_id, $drawer_id, $drawer_slide_id);
     $debug_calculation .= "<b>Box Total Price = {$box_total} </b><br />";
     $debug_calculation .= "<br />";
     // get door color price
     $door_color_price = 0;
     $door_color_detail = array();
     if (!empty($door_color)) {
         $color = new Color();
         $door_color_detail = $color->find('first', array('conditions' => array('id' => $door_color)));
         if (isset($door_color_detail['ColorSection']) && !empty($door_color_detail['ColorSection']) && is_array($door_color_detail['ColorSection'])) {
             foreach ($door_color_detail['ColorSection'] as $color_section) {
                 if ($color_section['type'] == 'door_material') {
                     $door_color_price = $color_section['price'];
                     break;
                 }
             }
         }
         unset($door_color_detail);
         // clean up
         unset($color);
         // clean up
     }
     // get cabinet color price
     $cabinet_color_price = 0;
     $cabinet_price_color = 0;
     $panel_price_color = 0;
     $cabinet_color_detail = array();
     if (!empty($cabinet_color)) {
         $color = new Color();
         $cabinet_color_detail = $color->find('first', array('conditions' => array('id' => $cabinet_color)));
         if (isset($cabinet_color_detail['ColorSection']) && !empty($cabinet_color_detail['ColorSection']) && is_array($cabinet_color_detail['ColorSection'])) {
             foreach ($cabinet_color_detail['ColorSection'] as $color_section) {
                 //          if ($color_section['type'] == 'cabinet_material') {
                 if ($color_section['type'] == 'cabinate_material') {
                     $cabinet_color_price = $color_section['price'];
                     $cabinet_price_color = $cabinet_color_price * $cabinet_sqft;
                     $debug_calculation .= "Cabinet Color Price = {$cabinet_color_price} <br />";
                     $debug_calculation .= "<b>Cabinet Price (Color) = {$cabinet_price_color} </b><br />";
                     $panel_price_color = $cabinet_color_price * $panel_sqft;
                     $debug_calculation .= "<b>Panel Price (Color) = {$panel_price_color} </b><br />";
                     $debug_calculation .= "<br />";
                     break;
                 }
             }
         }
         unset($cabinet_color_detail);
         // clean up
         unset($color);
         // clean up
     }
     $doors_drawers_price = 0;
     if (!empty($door_id)) {
         $door = new Door();
         $door_detail = $door->find('first', array('conditions' => array('Door.id' => $door_id)));
         $door_cost_markup = $door_detail['Door']['cost_markup_factor'];
         $door_drawers = array();
         $debug_calculation .= "Door/Drawer Cost Markup = {$door_cost_markup} <br />";
         $debug_calculation .= "<br />";
         $door_drawers['top_door'] = array('sqft' => 0, 'price' => 0);
         if (!empty($cabinet_detail['Cabinet']['top_door_count'])) {
             $height = (double) $cabinet_detail['Cabinet']['top_door_height'];
             $width = (double) $cabinet_detail['Cabinet']['top_door_width'];
             $count = (double) $cabinet_detail['Cabinet']['top_door_count'];
             $price_sqft = (double) $door_detail['Door']['wall_door_price'];
             $price_each = (double) $door_detail['Door']['wall_door_price_each'];
             $label = 'Top Door';
             $door_drawers['top_door'] = $this->calculateDoorDrawerForBuilder($debug_calculation, $label, $height, $width, $count, $door_cost_markup, $price_sqft, $price_each);
             $debug_calculation .= "<br />";
         }
         $door_drawers['bottom_door'] = array('sqft' => 0, 'price' => 0);
         if (!empty($cabinet_detail['Cabinet']['bottom_door_count'])) {
             $height = (double) $cabinet_detail['Cabinet']['bottom_door_height'];
             $width = (double) $cabinet_detail['Cabinet']['bottom_door_width'];
             $count = (double) $cabinet_detail['Cabinet']['bottom_door_count'];
             $price_sqft = (double) $door_detail['Door']['door_price'];
             $price_each = (double) $door_detail['Door']['door_price_each'];
             $label = 'Bottom Door';
             $door_drawers['bottom_door'] = $this->calculateDoorDrawerForBuilder($debug_calculation, $label, $height, $width, $count, $door_cost_markup, $price_sqft, $price_each);
             $debug_calculation .= "<br />";
         }
         $door_drawers['top_drawer_front'] = array('sqft' => 0, 'price' => 0);
         if (!empty($cabinet_detail['Cabinet']['top_drawer_front_count'])) {
             $height = (double) $cabinet_detail['Cabinet']['top_drawer_front_height'];
             $width = (double) $cabinet_detail['Cabinet']['top_drawer_front_width'];
             $count = (double) $cabinet_detail['Cabinet']['top_drawer_front_count'];
             $price_sqft = (double) $door_detail['Door']['drawer_price'];
             $price_each = (double) $door_detail['Door']['drawer_price_each'];
             $label = 'Top Drawer Front';
             $door_drawers['top_drawer_front'] = $this->calculateDoorDrawerForBuilder($debug_calculation, $label, $height, $width, $count, $door_cost_markup, $price_sqft, $price_each, true);
             $debug_calculation .= "<br />";
         }
         $door_drawers['middle_drawer_front'] = array('sqft' => 0, 'price' => 0);
         if (!empty($cabinet_detail['Cabinet']['middle_drawer_front_count'])) {
             $height = (double) $cabinet_detail['Cabinet']['middle_drawer_front_height'];
             $width = (double) $cabinet_detail['Cabinet']['middle_drawer_front_width'];
             $count = (double) $cabinet_detail['Cabinet']['middle_drawer_front_count'];
             $price_sqft = (double) $door_detail['Door']['lower_drawer_price'];
             $price_each = (double) $door_detail['Door']['lower_drawer_price_each'];
             $label = 'Middle Drawer Front';
             $door_drawers['middle_drawer_front'] = $this->calculateDoorDrawerForBuilder($debug_calculation, $label, $height, $width, $count, $door_cost_markup, $price_sqft, $price_each, true);
             $debug_calculation .= "<br />";
         }
         $door_drawers['bottom_drawer_front'] = array('sqft' => 0, 'price' => 0);
         if (!empty($cabinet_detail['Cabinet']['bottom_drawer_front_count'])) {
             $height = (double) $cabinet_detail['Cabinet']['bottom_drawer_front_height'];
             $width = (double) $cabinet_detail['Cabinet']['bottom_drawer_front_width'];
             $count = (double) $cabinet_detail['Cabinet']['bottom_drawer_front_count'];
             $price_sqft = (double) $door_detail['Door']['lower_drawer_price'];
             $price_each = (double) $door_detail['Door']['lower_drawer_price_each'];
             $label = 'Bottom Drawer Front';
             $door_drawers['bottom_drawer_front'] = $this->calculateDoorDrawerForBuilder($debug_calculation, $label, $height, $width, $count, $door_cost_markup, $price_sqft, $price_each, true);
             $debug_calculation .= "<br />";
         }
         $door_drawers['dummy_drawer_front'] = array('sqft' => 0, 'price' => 0);
         if (!empty($cabinet_detail['Cabinet']['dummy_drawer_front_count'])) {
             $height = (double) $cabinet_detail['Cabinet']['dummy_drawer_front_height'];
             $width = (double) $cabinet_detail['Cabinet']['dummy_drawer_front_width'];
             $count = (double) $cabinet_detail['Cabinet']['dummy_drawer_front_count'];
             $price_sqft = (double) $door_detail['Door']['drawer_price'];
             $price_each = (double) $door_detail['Door']['drawer_price_each'];
             $label = 'Dummy Drawer Front';
             $door_drawers['dummy_drawer_front'] = $this->calculateDoorDrawerForBuilder($debug_calculation, $label, $height, $width, $count, $door_cost_markup, $price_sqft, $price_each, true);
             $debug_calculation .= "<br />";
         }
         //      $debug = var_export($door_drawers, true);
         //      $debug_calculation .= "Door/Drawer Price = {$debug} <br />";
         $doors_drawers_price = $this->sumDoorDrawer($door_drawers);
         $debug_calculation .= "<b>Door/Drawer Price = {$doors_drawers_price} </b><br />";
         $debug_calculation .= "<b>Bulm up Charge Total Price = {$this->bulm_up_charge_total} </b><br />";
         $debug_calculation .= "<br />";
         if ($door_color_price) {
             $debug_calculation .= "Door Color Price = {$door_color_price} <br />";
             $debug_calculation .= "<br />";
             $doors_drawers_price_color = $this->sumDoorDrawerColorForBuilder($debug_calculation, $door_drawers, $door_color_price);
             $doors_drawers_price += $doors_drawers_price_color;
             $debug_calculation .= "<br />";
             $debug_calculation .= "<b>Door/Drawer Price (Color) = {$doors_drawers_price_color} </b><br />";
             $debug_calculation .= "<br />";
         }
         unset($door);
         // clean up
     }
     $inventory_lookup = new InventoryLookup();
     $inventory_lookup->recursive = -1;
     $drawer_departments = array();
     $inventory_lookup_detail = $inventory_lookup->find('all', array('conditions' => array('InventoryLookup.lookup_type' => 'drawer')));
     foreach ($inventory_lookup_detail as $inventory_lookup_row) {
         $departments = $inventory_lookup->SearchCache2Array($inventory_lookup_row['InventoryLookup']['department_id']);
         foreach ($departments as $department) {
             if (isset($drawer_departments[$department])) {
                 $drawer_departments[$department][] = $inventory_lookup_row['InventoryLookup']['id'];
             } else {
                 $drawer_departments[$department] = array($inventory_lookup_row['InventoryLookup']['id']);
             }
         }
     }
     $item = new Item();
     $items_price = 0;
     $discount = 0;
     $items = array();
     if (!empty($cabinet_detail['CabinetsItem'])) {
         foreach ($cabinet_detail['CabinetsItem'] as $cabinet_item) {
             if ($cabinet_item['accessories']) {
                 continue;
             }
             // skip the accessories
             $item_detail = $item->find('first', array('conditions' => array('Item.id' => $cabinet_item['item_id'])));
             if (array_key_exists($item_detail['Item']['item_department_id'], $drawer_departments)) {
                 if (!in_array($drawer_id, $drawer_departments[$item_detail['Item']['item_department_id']])) {
                     continue;
                     // skip the un-selected drawer
                 }
             }
             $item_price = $item_detail['Item']['builder_price'];
             //				$item_price = $item_detail['Item']['price'];
             $ItemMaterialPrice = null;
             if (isset($material_id) && !empty($material_id)) {
                 $ItemMaterialPrice = $this->getItemMaterialPriceForBuilder($item, $cabinet_item['item_id'], $material_id, $debug_calculation);
                 $item_price = $ItemMaterialPrice['item_price'];
             }
             //Discount
             //                if( $delivery_option == '5 – 10 Weeks Delivery' ) {
             //                    $discount = $item_price * $this->default_discount;
             //                    $item_price = $item_price - $discount;
             //                }
             $item_calculated_price = $cabinet_item['item_quantity'] * $item_price;
             $items_price += $item_calculated_price;
             if ($ItemMaterialPrice['sub_item']) {
                 $debug_calculation .= "Cabinet Item ({$ItemMaterialPrice['sub_item']['Item']['item_title']}) Price = {$ItemMaterialPrice['material_data']['Material']['price']} => Quantity = {$cabinet_item['item_quantity']} => Total = {$item_calculated_price} <br />";
             } else {
                 $debug_calculation .= "Cabinet Item ({$item_detail['Item']['item_title']}) Price = {$item_price} => Quantity = {$cabinet_item['item_quantity']} => Total = {$item_calculated_price} <br />";
             }
             //        $items[] = array(
             //            'id' => $item_detail['Item']['id'],
             //            'item_title' => $item_detail['Item']['item_title'],
             //            'price' => $item_price,
             //            'base_price' => $item_detail['Item']['price'],
             //            'quantity' => $cabinet_item['item_quantity'],
             //        );
         }
         $debug_calculation .= "<b>Cabinet Item Price = {$items_price} </b><br />";
         $debug_calculation .= "<br />";
         unset($cabinet_detail);
         // clean up
     }
     $total_price = ($items_price + $cabinet_price_color + $panel_price_color + $doors_drawers_price + $this->bulm_up_charge_total) * $this->default_markup * $quantity;
     $discount_rate = $this->getProductionTime($delivery_option);
     $discount = $total_price * $discount_rate;
     $total_price = $total_price - $discount;
     $debug_calculation .= "<b>Discount = {$discount} </b><br />";
     $debug_calculation .= "<b>Total Price = {$total_price} </b><br />";
     $result = array('items_price' => $items_price, 'doors_price' => $doors_drawers_price, 'cabinet_color_price_color' => $cabinet_price_color, 'total_price' => $total_price, 'debug_calculation' => $debug_calculation, 'conditions' => array('resource_id' => $cabinet_id, 'resource_type' => 'cabinet', 'material_id' => $material_id, 'door_id' => $door_id, 'door_color' => $door_color, 'cabinet_color' => $cabinet_color, 'drawer_slide_id' => $drawer_slide_id, 'drawer_id' => $drawer_id, 'quantity' => $quantity));
     return $result;
 }