From 9a1def21cc35da3ff49ff8f7bfac836d641aea22 Mon Sep 17 00:00:00 2001 From: FrancescoUK Date: Tue, 21 Jun 2016 09:44:24 +0100 Subject: [PATCH] Code tidy up (#635) --- application/libraries/Receiving_lib.php | 121 +++++++++++++----------- application/libraries/Sale_lib.php | 51 +++++----- 2 files changed, 89 insertions(+), 83 deletions(-) diff --git a/application/libraries/Receiving_lib.php b/application/libraries/Receiving_lib.php index 629ac0862..5c7809056 100644 --- a/application/libraries/Receiving_lib.php +++ b/application/libraries/Receiving_lib.php @@ -11,56 +11,63 @@ class Receiving_lib function get_cart() { if(!$this->CI->session->userdata('cartRecv')) + { $this->set_cart(array()); + } return $this->CI->session->userdata('cartRecv'); } function set_cart($cart_data) { - $this->CI->session->set_userdata('cartRecv',$cart_data); + $this->CI->session->set_userdata('cartRecv', $cart_data); } function get_supplier() { if(!$this->CI->session->userdata('supplier')) + { $this->set_supplier(-1); + } return $this->CI->session->userdata('supplier'); } function set_supplier($supplier_id) { - $this->CI->session->set_userdata('supplier',$supplier_id); + $this->CI->session->set_userdata('supplier', $supplier_id); } function get_mode() { if(!$this->CI->session->userdata('recv_mode')) + { $this->set_mode('receive'); + } return $this->CI->session->userdata('recv_mode'); } function set_mode($mode) { - $this->CI->session->set_userdata('recv_mode',$mode); + $this->CI->session->set_userdata('recv_mode', $mode); } function get_stock_source() { if(!$this->CI->session->userdata('recv_stock_source')) { - $location_id = $this->CI->Stock_location->get_default_location_id(); - $this->set_stock_source($location_id); + $this->set_stock_source($this->CI->Stock_location->get_default_location_id()); } + return $this->CI->session->userdata('recv_stock_source'); } function get_comment() { - // avoid returning a null that results in a 0 in the comment if nothing is set/available + // avoid returning a NULL that results in a 0 in the comment if nothing is set/available $comment = $this->CI->session->userdata('comment'); + return empty($comment) ? '' : $comment; } @@ -92,7 +99,7 @@ class Receiving_lib function is_print_after_sale() { return $this->CI->session->userdata('recv_print_after_sale') == 'true' || - $this->CI->session->userdata('recv_print_after_sale') == '1'; + $this->CI->session->userdata('recv_print_after_sale') == '1'; } function set_print_after_sale($print_after_sale) @@ -114,15 +121,15 @@ class Receiving_lib { if(!$this->CI->session->userdata('recv_stock_destination')) { - $location_id = $this->CI->Stock_location->get_default_location_id(); - $this->set_stock_destination($location_id); + $this->set_stock_destination($this->CI->Stock_location->get_default_location_id()); } + return $this->CI->session->userdata('recv_stock_destination'); } function set_stock_destination($stock_destination) { - $this->CI->session->set_userdata('recv_stock_destination',$stock_destination); + $this->CI->session->set_userdata('recv_stock_destination', $stock_destination); } function clear_stock_destination() @@ -130,7 +137,7 @@ class Receiving_lib $this->CI->session->unset_userdata('recv_stock_destination'); } - function add_item($item_id,$quantity=1,$item_location=null,$discount=0,$price=null,$description=null,$serialnumber=null,$receiving_quantity=null) + function add_item($item_id, $quantity = 1, $item_location = NULL, $discount = 0, $price = NULL, $description = NULL, $serialnumber = NULL, $receiving_quantity = NULL) { //make sure item exists in database. if(!$this->CI->Item->exists($item_id)) @@ -139,7 +146,9 @@ class Receiving_lib $item_id = $this->CI->Item->get_item_id($item_id); if(!$item_id) - return false; + { + return FALSE; + } } //Get items in the receiving so far. @@ -150,12 +159,12 @@ class Receiving_lib //We also need to get the next key that we are going to use in case we need to add the //item to the list. Since items can be deleted, we can't use a count. we use the highest key + 1. - $maxkey=0; //Highest key so far - $itemalreadyinsale=FALSE; //We did not find the item yet. - $insertkey=0; //Key to use for new entry. - $updatekey=0; //Key to use to update(quantity) + $maxkey = 0; //Highest key so far + $itemalreadyinsale = FALSE; //We did not find the item yet. + $insertkey = 0; //Key to use for new entry. + $updatekey = 0; //Key to use to update(quantity) - foreach ($items as $item) + foreach($items as $item) { //We primed the loop so maxkey is 0 the first time. //Also, we have stored the key in the element itself so we can compare. @@ -167,54 +176,53 @@ class Receiving_lib $maxkey = $item['line']; } - if($item['item_id']==$item_id && $item['item_location']==$item_location) + if($item['item_id'] == $item_id && $item['item_location'] == $item_location) { - $itemalreadyinsale=TRUE; - $updatekey=$item['line']; + $itemalreadyinsale = TRUE; + $updatekey = $item['line']; } } - $insertkey=$maxkey+1; - $item_info=$this->CI->Item->get_info($item_id,$item_location); + $insertkey = $maxkey+1; + $item_info = $this->CI->Item->get_info($item_id,$item_location); //array records are identified by $insertkey and item_id is just another field. - $price=$price!=null ? $price: $item_info->cost_price; - $item = array(($insertkey)=> - array( - 'item_id'=>$item_id, - 'item_location'=>$item_location, - 'stock_name'=>$this->CI->Stock_location->get_location_name($item_location), - 'line'=>$insertkey, - 'name'=>$item_info->name, - 'description'=>$description!=null ? $description: $item_info->description, - 'serialnumber'=>$serialnumber!=null ? $serialnumber: '', - 'allow_alt_description'=>$item_info->allow_alt_description, - 'is_serialized'=>$item_info->is_serialized, - 'quantity'=>$quantity, - 'discount'=>$discount, - 'in_stock'=>$this->CI->Item_quantity->get_item_quantity($item_id, $item_location)->quantity, - 'price'=>$price, - 'receiving_quantity'=>$receiving_quantity!=null ? $receiving_quantity : $item_info->receiving_quantity, - 'total'=>$this->get_item_total($quantity, $price, $discount) + $price = $price != NULL ? $price : $item_info->cost_price; + $item = array($insertkey => array( + 'item_id' => $item_id, + 'item_location' => $item_location, + 'stock_name' => $this->CI->Stock_location->get_location_name($item_location), + 'line' => $insertkey, + 'name' => $item_info->name, + 'description' => $description!=NULL ? $description: $item_info->description, + 'serialnumber' => $serialnumber!=NULL ? $serialnumber: '', + 'allow_alt_description' => $item_info->allow_alt_description, + 'is_serialized' => $item_info->is_serialized, + 'quantity' => $quantity, + 'discount' => $discount, + 'in_stock' => $this->CI->Item_quantity->get_item_quantity($item_id, $item_location)->quantity, + 'price' => $price, + 'receiving_quantity' => $receiving_quantity!=NULL ? $receiving_quantity : $item_info->receiving_quantity, + 'total' => $this->get_item_total($quantity, $price, $discount) ) ); //Item already exists if($itemalreadyinsale) { - $items[$updatekey]['quantity']+=$quantity; + $items[$updatekey]['quantity'] += $quantity; } else { //add to existing array - $items+=$item; + $items += $item; } $this->set_cart($items); - return true; + return TRUE; } - function edit_item($line,$description,$serialnumber,$quantity,$discount,$price) + function edit_item($line, $description, $serialnumber, $quantity, $discount, $price) { $items = $this->get_cart(); if(isset($items[$line])) @@ -229,7 +237,7 @@ class Receiving_lib $this->set_cart($items); } - return false; + return FALSE; } function is_valid_receipt($receipt_receiving_id) @@ -254,26 +262,25 @@ class Receiving_lib //KIT # $pieces = explode(' ',$item_kit_id); - if(count($pieces)==2) + if(count($pieces) == 2) { return $this->CI->Item_kit->exists($pieces[1]); } - return false; + return FALSE; } function return_entire_receiving($receipt_receiving_id) { //RECV # $pieces = explode(' ',$receipt_receiving_id); - if (preg_match("/(RECV|KIT)/", $pieces[0])) + if(preg_match("/(RECV|KIT)/", $pieces[0])) { $receiving_id = $pieces[1]; } else { - $receiving = $this->CI->Receiving->get_receiving_by_reference($receipt_receiving_id)->row(); - $receiving_id = $receiving->receiving_id; + $receiving_id = $this->CI->Receiving->get_receiving_by_reference($receipt_receiving_id)->row()->receiving_id; } $this->empty_cart(); @@ -282,18 +289,18 @@ class Receiving_lib foreach($this->CI->Receiving->get_receiving_items($receiving_id)->result() as $row) { - $this->add_item($row->item_id,-$row->quantity_purchased,$row->item_location,$row->discount_percent,$row->item_unit_price,$row->description,$row->serialnumber,$row->receiving_quantity); + $this->add_item($row->item_id, -$row->quantity_purchased, $row->item_location, $row->discount_percent, $row->item_unit_price, $row->description, $row->serialnumber, $row->receiving_quantity); } $this->set_supplier($this->CI->Receiving->get_supplier($receiving_id)->person_id); } - function add_item_kit($external_item_kit_id,$item_location) + function add_item_kit($external_item_kit_id, $item_location) { //KIT # $pieces = explode(' ',$external_item_kit_id); $item_kit_id = $pieces[1]; - foreach ($this->CI->Item_kit_items->get_info($item_kit_id) as $item_kit_item) + foreach($this->CI->Item_kit_items->get_info($item_kit_id) as $item_kit_item) { $this->add_item($item_kit_item['item_id'],$item_kit_item['quantity'], $item_location); } @@ -309,13 +316,12 @@ class Receiving_lib $this->add_item($row->item_id, $row->quantity_purchased, $row->item_location, $row->discount_percent, $row->item_unit_price, $row->description, $row->serialnumber, $row->receiving_quantity); } $this->set_supplier($this->CI->Receiving->get_supplier($receiving_id)->person_id); - $receiving_info = $this->CI->Receiving->get_info($receiving_id); - //$this->set_reference($receiving_info->row()->reference); + //$this->set_reference($this->CI->Receiving->get_info($receiving_id)->row()->reference); } function delete_item($line) { - $items=$this->get_cart(); + $items = $this->get_cart(); unset($items[$line]); $this->set_cart($items); } @@ -348,7 +354,8 @@ class Receiving_lib { $total = bcmul($quantity, $price, PRECISION); $discount_fraction = bcdiv($discount_percentage, 100, PRECISION); - $discount_amount = bcmul($total, $discount_fraction, PRECISION); + $discount_amount = bcmul($total, $discount_fraction, PRECISION); + return bcsub($total, $discount_amount, PRECISION); } diff --git a/application/libraries/Sale_lib.php b/application/libraries/Sale_lib.php index 24f51d964..b3677c8a5 100644 --- a/application/libraries/Sale_lib.php +++ b/application/libraries/Sale_lib.php @@ -212,15 +212,14 @@ class Sale_lib function set_mode($mode) { - $this->CI->session->set_userdata('sale_mode',$mode); + $this->CI->session->set_userdata('sale_mode', $mode); } function get_sale_location() { if(!$this->CI->session->userdata('sale_location')) { - $location_id = $this->CI->Stock_location->get_default_location_id(); - $this->set_sale_location($location_id); + $this->set_sale_location($this->CI->Stock_location->get_default_location_id()); } return $this->CI->session->userdata('sale_location'); @@ -228,7 +227,7 @@ class Sale_lib function set_sale_location($location) { - $this->CI->session->set_userdata('sale_location',$location); + $this->CI->session->set_userdata('sale_location', $location); } function clear_sale_location() @@ -238,7 +237,7 @@ class Sale_lib function set_giftcard_remainder($value) { - $this->CI->session->set_userdata('giftcard_remainder',$value); + $this->CI->session->set_userdata('giftcard_remainder', $value); } function get_giftcard_remainder() @@ -251,7 +250,7 @@ class Sale_lib $this->CI->session->unset_userdata('giftcard_remainder'); } - function add_item($item_id, $quantity=1, $item_location, $discount=0, $price=NULL, $description=NULL, $serialnumber=NULL) + function add_item($item_id, $quantity = 1, $item_location, $discount = 0, $price = NULL, $description = NULL, $serialnumber = NULL) { //make sure item exists if($this->validate_item($item_id) == FALSE) @@ -304,22 +303,22 @@ class Sale_lib if(!$itemalreadyinsale || $item_info->is_serialized) { $item = array($insertkey => array( - 'item_id'=>$item_id, - 'item_location'=>$item_location, - 'stock_name'=>$this->CI->Stock_location->get_location_name($item_location), - 'line'=>$insertkey, - 'name'=>$item_info->name, - 'item_number'=>$item_info->item_number, - 'description'=>$description!=NULL ? $description: $item_info->description, - 'serialnumber'=>$serialnumber!=NULL ? $serialnumber: '', - 'allow_alt_description'=>$item_info->allow_alt_description, - 'is_serialized'=>$item_info->is_serialized, - 'quantity'=>$quantity, - 'discount'=>$discount, - 'in_stock'=>$this->CI->Item_quantity->get_item_quantity($item_id, $item_location)->quantity, - 'price'=>$price, - 'total'=>$total, - 'discounted_total'=>$discounted_total, + 'item_id' => $item_id, + 'item_location' => $item_location, + 'stock_name' => $this->CI->Stock_location->get_location_name($item_location), + 'line' => $insertkey, + 'name' => $item_info->name, + 'item_number' => $item_info->item_number, + 'description' => $description!=NULL ? $description: $item_info->description, + 'serialnumber' => $serialnumber!=NULL ? $serialnumber: '', + 'allow_alt_description' => $item_info->allow_alt_description, + 'is_serialized' => $item_info->is_serialized, + 'quantity' => $quantity, + 'discount' => $discount, + 'in_stock' => $this->CI->Item_quantity->get_item_quantity($item_id, $item_location)->quantity, + 'price' => $price, + 'total' => $total, + 'discounted_total' => $discounted_total, ) ); //add to existing array @@ -464,7 +463,7 @@ class Sale_lib $this->set_customer($this->CI->Sale->get_customer($sale_id)->person_id); } - function add_item_kit($external_item_kit_id,$item_location) + function add_item_kit($external_item_kit_id, $item_location) { //KIT # $pieces = explode(' ', $external_item_kit_id); @@ -605,7 +604,7 @@ class Sale_lib return to_currency_no_money($subtotal); } - function get_item_total_tax_exclusive($item_id, $quantity, $price, $discount_percentage, $include_discount=FALSE) + function get_item_total_tax_exclusive($item_id, $quantity, $price, $discount_percentage, $include_discount = FALSE) { $tax_info = $this->CI->Item_taxes->get_info($item_id); $item_price = $this->get_item_total($quantity, $price, $discount_percentage, $include_discount); @@ -619,7 +618,7 @@ class Sale_lib return $item_price; } - function get_item_total($quantity, $price, $discount_percentage, $include_discount=FALSE) + function get_item_total($quantity, $price, $discount_percentage, $include_discount = FALSE) { $total = bcmul($quantity, $price, PRECISION); if($include_discount) @@ -657,7 +656,7 @@ class Sale_lib return bcmul($price, $tax_fraction, PRECISION); } - function calculate_subtotal($include_discount=FALSE, $exclude_tax=FALSE) + function calculate_subtotal($include_discount = FALSE, $exclude_tax = FALSE) { $subtotal = 0; foreach($this->get_cart() as $item)