public function afterSave($created) { parent::afterSave($created); if (isset($this->data['CabinetOrder']['resource_type']) && $this->data['CabinetOrder']['resource_type'] == 'cabinet') { App::import('Model', 'Inventory.Cabinet'); $cabinet_model = new Cabinet(); $cabinet_model->recursive = 1; $cabinet_detail = $cabinet_model->find('first', array('conditions' => array('Cabinet.id' => $this->data['CabinetOrder']['resource_id']))); if (!empty($cabinet_detail['CabinetsItem']) && is_array($cabinet_detail['CabinetsItem'])) { foreach ($cabinet_detail['CabinetsItem'] as $key => $cabinet_item) { $this->data['CabinetOrderItem'][$key] = array('cabinet_order_id' => $this->data['CabinetOrder']['id'], 'quote_id' => $this->data['CabinetOrder']['quote_id'], 'cabinet_id' => $this->data['CabinetOrder']['resource_id'], 'quantity' => $this->data['CabinetOrder']['quantity'] * $cabinet_item['item_quantity'], 'item_id' => $cabinet_item['item_id'], 'code' => "{$cabinet_item['item_id']}|item", 'type' => 'Cabinet Order'); } } } if (isset($this->data['CabinetOrderItem'])) { // delete CabinetOrderItem $this->CabinetOrderItem->deleteAll(array('cabinet_order_id' => $this->id)); if (is_array($this->data['CabinetOrderItem'])) { // save CabinetOrderItem // $cabinetOrderItems = array(); // $index = 0; // foreach ($this->data['CabinetOrderItem'] as $cabinetOrderItem) { // if (trim($cabinetOrderItem['quantity']) == '' || trim($cabinetOrderItem['code']) == '') { // continue; // skip if no data // } // $cabinetOrderItems[$index]['cabinet_order_id'] = $this->id; // $cabinetOrderItems[$index]['item_id'] = trim($cabinetOrderItem['item_id']); // $cabinetOrderItems[$index]['cabinet_id'] = trim($cabinetOrderItem['cabinet_id']); //// $cabinetOrderItems[$index]['door_id'] = trim($cabinetOrderItem['door_id']); //// $cabinetOrderItems[$index]['door_information'] = trim($cabinetOrderItem['door_information']); //// $cabinetOrderItems[$index]['open_frame_door'] = trim($cabinetOrderItem['open_frame_door']); //// $cabinetOrderItems[$index]['do_not_drill_door'] = trim($cabinetOrderItem['do_not_drill_door']); //// $cabinetOrderItems[$index]['no_doors'] = trim($cabinetOrderItem['no_doors']); // $cabinetOrderItems[$index]['quantity'] = trim($cabinetOrderItem['quantity']); // $cabinetOrderItems[$index]['code'] = trim($cabinetOrderItem['code']); // $cabinetOrderItems[$index]['type'] = "Cabinet Order"; // // $index++; // } $this->CabinetOrderItem->saveAll($this->data['CabinetOrderItem']); } } }
function getCabinetItems() { set_time_limit(0); $this->autoRender = false; $sql = "SELECT * FROM cabinetparts;"; $datas = $this->Cabinet->query($sql); App::uses("CabinetsItem", "Inventory.Model"); App::uses("Cabinet", "Inventory.Model"); $Cabinet_model = new Cabinet(); App::uses("Item", "Inventory.Model"); $Item_model = new Item(); foreach ($datas as $data) { $CabinetsItem_model = new CabinetsItem(); $cabinet_data['CabinetsItem']['lineid'] = $data['cabinetparts']['lineid']; $cabinet_data = $Cabinet_model->find('first', array('fields' => array('id', 'name'), 'conditions' => array('Cabinet.name' => $data['cabinetparts']['cabinet']), 'recursive' => -1)); $cabinet_data['CabinetsItem']['cabinet_id'] = !empty($cabinet_data['Cabinet']['id']) ? $cabinet_data['Cabinet']['id'] : "0"; $Item_data = $Item_model->find('first', array('fields' => array('id', 'number'), 'conditions' => array('Item.number' => $data['cabinetparts']['number']), 'recursive' => -1)); $cabinet_data['CabinetsItem']['item_id'] = !empty($Item_data['Item']['id']) ? $Item_data['Item']['id'] : "0"; if (!empty($Item_data)) { $item['Item']['id'] = $Item_data['Item']['id']; $item['Item']['item_department_id'] = 3; $item['Item']['Department'] = 'Accessories'; if (!$Item_model->save($item)) { pr($Item_model->validationErrors); } } $cabinet_data['CabinetsItem']['item_quantity'] = !empty($data['cabinetparts']['quantity']) ? $data['cabinetparts']['quantity'] : "1"; $cabinet_data['CabinetsItem']['component'] = $data['cabinetparts']['component']; $cabinet_data['CabinetsItem']['category'] = $data['cabinetparts']['category']; $cabinet_data['CabinetsItem']['number'] = $data['cabinetparts']['number']; $cabinet_data['CabinetsItem']['accessories'] = 1; if (!$CabinetsItem_model->save($cabinet_data)) { pr($CabinetsItem_model->validationErrors); } } }
function findTotalAmountQuote($id = null) { $quote = $this->Quote->read(null, $id); $total_quote_price = 0; $total_quote_price_cabinet = 0; $total_quote_price_installation = 0; $total_quote_price_discount = 0; $cabinets = array(); if ($quote['CabinetOrder'] && is_array($quote['CabinetOrder'])) { App::import("Model", "Inventory.Cabinet"); App::import("Model", "Inventory.Item"); foreach ($quote['CabinetOrder'] as $cabinet_order) { $cabinet = new Cabinet(); $item_model = new Item(); $resource_detail = array('Resource' => array('name' => '', 'description' => '')); switch ($cabinet_order['resource_type']) { case 'cabinet': $resource_detail = $cabinet->find('first', array('conditions' => array('id' => $cabinet_order['resource_id']))); $resource_detail['Resource']['name'] = $resource_detail['Cabinet']['name']; $resource_detail['Resource']['description'] = $resource_detail['Cabinet']['description']; $cabinets[] = $resource_detail; break; case 'item': $resource_detail = $item_model->find('first', array('conditions' => array('Item.id' => $cabinet_order['resource_id']))); $resource_detail['Resource']['name'] = $resource_detail['Cabinet'][0]['name']; $resource_detail['Resource']['description'] = $resource_detail['Cabinet'][0]['description']; $cabinets[] = $resource_detail; break; default: break; } $sub_total = $cabinet_order['total_cost']; $total_quote_price_cabinet += $sub_total; } } if ($quote['Quote']['installation'] == 'We Installed' && !empty($cabinets) && is_array($cabinets)) { $installation_summery_list = array(); foreach ($cabinets as $cabinet) { if (!empty($cabinet['CabinetInstallation']) && is_array($cabinet['CabinetInstallation'])) { foreach ($cabinet['CabinetInstallation'] as $installation) { if (isset($installation_summery_list[$installation['name']]['quantity'])) { $installation_summery_list[$installation['name']]['quantity']++; } else { $installation_summery_list[$installation['name']]['name'] = $installation['name']; $installation_summery_list[$installation['name']]['price_unit'] = $installation['price_unit']; $installation_summery_list[$installation['name']]['price'] = $installation['price']; $installation_summery_list[$installation['name']]['quantity'] = 1; } } } } if (!empty($installation_summery_list)) { foreach ($installation_summery_list as $installation) { $sub_total = $installation['price'] * $installation['quantity']; $total_quote_price_installation += $sub_total; } } } $total_quote_price = $total_quote_price_cabinet + $total_quote_price_installation; if ($quote['Quote']['delivery'] == '5 – 10 Weeks Delivery') { $total_quote_price_discount = $total_quote_price * 0.25; // 25% discount for late delivery } $total_quote_price -= $total_quote_price_discount; return $total_quote_price; }
function CabinetName2ID($name = null, $reverse = false) { // App::uses("Cabinet", "Inventory.Model"); App::import('Model', 'Inventory.Cabinet'); $cabinet = new Cabinet(); $cabinet->recursive = 0; if (!$reverse) { $cabinet_info = $cabinet->find("first", array("fields" => array("id", "name"), 'conditions' => array('Cabinet.name' => $name))); } else { $cabinet_info = $cabinet->find("first", array("fields" => array("id", "name"), 'conditions' => array('Cabinet.id' => $name))); } if (isset($cabinet_info['Cabinet']['id'])) { if (!$reverse) { return $cabinet_info['Cabinet']['id']; } else { return $cabinet_info['Cabinet']['name']; } } else { return null; } }
function cabinet_json() { $id = $this->request->query['term']; App::uses("Cabinet", "Inventory.Model"); $cabinet = new Cabinet(); $cabinet_list = array(); $cabinet_list = $cabinet->find("all", array('conditions' => array('Cabinet.id' => $id), 'order' => array('Cabinet.name' => 'asc'), 'limit' => 1)); $return_data = array(); foreach ($cabinet_list as $id => $cabinet_detail) { if ($cabinet_detail) { $return_data['id'] = $cabinet_detail['Cabinet']['id']; $return_data['text'] = $cabinet_detail['Cabinet']['name']; $return_data['item_type'] = 'cabinet'; $return_data['detail'] = $cabinet_detail['Cabinet']; $return_data['door_count'] = (int) $cabinet_detail['Cabinet']['top_door_count'] + (int) $cabinet_detail['Cabinet']['bottom_door_count'] + (int) $cabinet_detail['Cabinet']['top_drawer_front_count'] + (int) $cabinet_detail['Cabinet']['middle_drawer_front_count'] + (int) $cabinet_detail['Cabinet']['bottom_drawer_front_count'] + (int) $cabinet_detail['Cabinet']['dummy_drawer_front_count']; $return_data['quote_color_required'] = (int) $cabinet_detail['Cabinet']['quote_color_required']; $return_data['quote_material_required'] = (int) $cabinet_detail['Cabinet']['quote_material_required']; break; } } print json_encode($return_data); exit; }
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; }