Overview

Namespaces

  • emberlabs
    • Barcode
  • None

Classes

  • Appconfig
  • Barcode_lib
  • Config
  • Customer
  • Customer_rewards
  • Customers
  • Detailed_receivings
  • Detailed_sales
  • Dinner_table
  • Email_lib
  • emberlabs\Barcode\BarcodeBase
  • emberlabs\Barcode\Code128
  • emberlabs\Barcode\Code39
  • emberlabs\Barcode\Ean13
  • emberlabs\Barcode\Ean8
  • Employee
  • Employees
  • Giftcard
  • Giftcards
  • Home
  • Inventory
  • Inventory_low
  • Inventory_summary
  • Item
  • Item_kit
  • Item_kit_items
  • Item_kits
  • Item_lib
  • Item_quantity
  • Item_taxes
  • Items
  • Login
  • Mailchimp_lib
  • MailchimpConnector
  • Messages
  • Migrate
  • Migration_Sales_Tax_Data
  • Module
  • MY_Lang
  • No_Access
  • Person
  • Persons
  • Receiving
  • Receiving_lib
  • Receivings
  • Report
  • Reports
  • Rewards
  • Rounding_code
  • Sale
  • Sale_lib
  • Sale_suspended
  • Sales
  • Secure_Controller
  • Sms_lib
  • Specific_customer
  • Specific_discount
  • Specific_employee
  • Stock_location
  • Summary_categories
  • Summary_customers
  • Summary_discounts
  • Summary_employees
  • Summary_items
  • Summary_payments
  • Summary_report
  • Summary_sales
  • Summary_suppliers
  • Summary_taxes
  • Supplier
  • Suppliers
  • Tax
  • Tax_lib
  • Taxes
  • Token
  • Token_customer
  • Token_invoice_count
  • Token_invoice_sequence
  • Token_lib
  • Token_quote_sequence
  • Token_suspended_invoice_count
  • Token_year_invoice_count
  • Tracking_lib

Functions

  • currency_side
  • current_language
  • current_language_code
  • dateformat_bootstrap
  • dateformat_momentjs
  • db_log_queries
  • get_customer_data_row
  • get_customer_manage_table_headers
  • get_giftcard_data_row
  • get_giftcards_manage_table_headers
  • get_item_data_row
  • get_item_kit_data_row
  • get_item_kits_manage_table_headers
  • get_items_manage_table_headers
  • get_people_manage_table_headers
  • get_person_data_row
  • get_sale_data_last_row
  • get_sale_data_row
  • get_sales_manage_payments_summary
  • get_sales_manage_table_headers
  • get_supplier_data_row
  • get_suppliers_manage_table_headers
  • get_tax_data_row
  • get_taxes_manage_table_headers
  • load_config
  • load_language_files
  • load_stats
  • parse_decimals
  • pdf_create
  • quantity_decimals
  • show_report
  • show_report_if_allowed
  • tax_decimals
  • to_currency
  • to_currency_no_money
  • to_decimals
  • to_quantity_decimals
  • to_tax_decimals
  • totals_decimals
  • transform_headers
  • transform_headers_readonly
  • Overview
  • Namespace
  • Class
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Receiving library
 *
 * Library with utilities to manage receivings
 *
 * @link    github.com/jekkos/opensourcepos
 * @since   1.0
 * @author  N/A
 */

class Receiving_lib
{
    private $CI;

    public function __construct()
    {
        $this->CI =& get_instance();
    }

    public function get_cart()
    {
        if(!$this->CI->session->userdata('recv_cart'))
        {
            $this->set_cart(array());
        }

        return $this->CI->session->userdata('recv_cart');
    }

    public function set_cart($cart_data)
    {
        $this->CI->session->set_userdata('recv_cart', $cart_data);
    }

    public function empty_cart()
    {
        $this->CI->session->unset_userdata('recv_cart');
    }

    public function get_supplier()
    {
        if(!$this->CI->session->userdata('recv_supplier'))
        {
            $this->set_supplier(-1);
        }

        return $this->CI->session->userdata('recv_supplier');
    }

    public function set_supplier($supplier_id)
    {
        $this->CI->session->set_userdata('recv_supplier', $supplier_id);
    }

    public function remove_supplier()
    {
        $this->CI->session->unset_userdata('recv_supplier');
    }

    public function get_mode()
    {
        if(!$this->CI->session->userdata('recv_mode'))
        {
            $this->set_mode('receive');
        }

        return $this->CI->session->userdata('recv_mode');
    }

    public function set_mode($mode)
    {
        $this->CI->session->set_userdata('recv_mode', $mode);
    }
    
    public function clear_mode()
    {
        $this->CI->session->unset_userdata('recv_mode');
    }

    public function get_stock_source()
    {
        if(!$this->CI->session->userdata('recv_stock_source'))
        {
            $this->set_stock_source($this->CI->Stock_location->get_default_location_id());
        }

        return $this->CI->session->userdata('recv_stock_source');
    }
    
    public function get_comment()
    {
        // avoid returning a NULL that results in a 0 in the comment if nothing is set/available
        $comment = $this->CI->session->userdata('recv_comment');

        return empty($comment) ? '' : $comment;
    }
    
    public function set_comment($comment)
    {
        $this->CI->session->set_userdata('recv_comment', $comment);
    }
    
    public function clear_comment()
    {
        $this->CI->session->unset_userdata('recv_comment');
    }
   
    public function get_reference()
    {
        return $this->CI->session->userdata('recv_reference');
    }
    
    public function set_reference($reference)
    {
        $this->CI->session->set_userdata('recv_reference', $reference);
    }
    
    public function clear_reference()
    {
        $this->CI->session->unset_userdata('recv_reference');
    }
    
    public function is_print_after_sale()
    {
        return $this->CI->session->userdata('recv_print_after_sale') == 'true' ||
                $this->CI->session->userdata('recv_print_after_sale') == '1';
    }
    
    public function set_print_after_sale($print_after_sale)
    {
        return $this->CI->session->set_userdata('recv_print_after_sale', $print_after_sale);
    }
    
    public function set_stock_source($stock_source)
    {
        $this->CI->session->set_userdata('recv_stock_source', $stock_source);
    }
    
    public function clear_stock_source()
    {
        $this->CI->session->unset_userdata('recv_stock_source');
    }
    
    public function get_stock_destination()
    {
        if(!$this->CI->session->userdata('recv_stock_destination'))
        {
            $this->set_stock_destination($this->CI->Stock_location->get_default_location_id());
        }

        return $this->CI->session->userdata('recv_stock_destination');
    }

    public function set_stock_destination($stock_destination)
    {
        $this->CI->session->set_userdata('recv_stock_destination', $stock_destination);
    }
    
    public function clear_stock_destination()
    {
        $this->CI->session->unset_userdata('recv_stock_destination');
    }

    public function add_item($item_id, $quantity = 1, $item_location = NULL, $discount = 0, $price = NULL, $description = NULL, $serialnumber = NULL, $receiving_quantity = NULL, $include_deleted = FALSE)
    {
        //make sure item exists in database.
        if(!$this->CI->Item->exists($item_id, $include_deleted))
        {
            //try to get item id given an item_number
            $item_id = $this->CI->Item->get_item_id($item_id, $include_deleted);

            if(!$item_id)
            {
                return FALSE;
            }
        }

        //Get items in the receiving so far.
        $items = $this->get_cart();

        //We need to loop through all items in the cart.
        //If the item is already there, get it's key($updatekey).
        //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)

        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.
            //There is an array public function to get the associated key for an element, but I like it better
            //like that!

            if($maxkey <= $item['line'])
            {
                $maxkey = $item['line'];
            }

            if($item['item_id'] == $item_id && $item['item_location'] == $item_location)
            {
                $itemalreadyinsale = TRUE;
                $updatekey = $item['line'];
            }
        }

        $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, $receiving_quantity)
            )
        );

        //Item already exists
        if($itemalreadyinsale)
        {
            $items[$updatekey]['quantity'] += $quantity;
            $items[$updatekey]['total'] = $this->get_item_total($items[$updatekey]['quantity'], $price, $discount);
        }
        else
        {
            //add to existing array
            $items += $item;
        }

        $this->set_cart($items);

        return TRUE;
    }

    public function edit_item($line, $description, $serialnumber, $quantity, $discount, $price)
    {
        $items = $this->get_cart();
        if(isset($items[$line]))
        {
            $line = &$items[$line];
            $line['description'] = $description;
            $line['serialnumber'] = $serialnumber;
            $line['quantity'] = $quantity;
            $line['discount'] = $discount;
            $line['price'] = $price;
            $line['total'] = $this->get_item_total($quantity, $price, $discount); 
            $this->set_cart($items);
        }

        return FALSE;
    }

    public function delete_item($line)
    {
        $items = $this->get_cart();
        unset($items[$line]);
        $this->set_cart($items);
    }

    public function return_entire_receiving($receipt_receiving_id)
    {
        //RECV #
        $pieces = explode(' ', $receipt_receiving_id);
        if(preg_match("/(RECV|KIT)/", $pieces[0]))
        {
            $receiving_id = $pieces[1];
        } 
        else 
        {
            $receiving_id = $this->CI->Receiving->get_receiving_by_reference($receipt_receiving_id)->row()->receiving_id;
        }

        $this->empty_cart();
        $this->remove_supplier();
        $this->clear_comment();

        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, TRUE);
        }

        $this->set_supplier($this->CI->Receiving->get_supplier($receiving_id)->person_id);
    }

    public 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)
        {
            $this->add_item($item_kit_item['item_id'],$item_kit_item['quantity'], $item_location);
        }
    }

    public function copy_entire_receiving($receiving_id)
    {
        $this->empty_cart();
        $this->remove_supplier();

        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, TRUE);
        }

        $this->set_supplier($this->CI->Receiving->get_supplier($receiving_id)->person_id);
        //$this->set_reference($this->CI->Receiving->get_info($receiving_id)->row()->reference);
    }

    public function clear_all()
    {
        $this->clear_mode();
        $this->empty_cart();
        $this->remove_supplier();
        $this->clear_comment();
        $this->clear_reference();
    }

    public function get_item_total($quantity, $price, $discount_percentage)
    {
        $total = bcmul($quantity, $price);
        $discount_fraction = bcdiv($discount_percentage, 100);
        $discount_amount = bcmul($total, $discount_fraction);

        return bcsub($total, $discount_amount);
    }

    public function get_total()
    {
        $total = 0;
        foreach($this->get_cart() as $item)
        {
            $total = bcadd($total, $this->get_item_total(($item['quantity']* $item['receiving_quantity']), $item['price'], $item['discount']));
        }
        
        return $total;
    }
}

?>
API documentation generated by ApiGen