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
  • 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
  • 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: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458: 459: 460: 461: 462: 463: 464: 465: 466: 467: 468: 469: 470: 471: 472: 473: 474: 475: 476: 477: 478: 479: 480: 481: 482: 483: 484: 485: 486: 487: 488: 489: 490: 491: 492: 493: 494: 495: 496: 497: 498: 499: 500: 501: 502: 503: 504: 505: 506: 507: 508: 509: 510: 511: 512: 513: 514: 515: 516: 517: 518: 519: 520: 521: 522: 523: 524: 525: 526: 527: 528: 529: 530: 531: 532: 533: 534: 535: 536: 537: 538: 539: 540: 541: 542: 543: 544: 545: 546: 547: 548: 549: 550: 551: 552: 553: 554: 555: 556: 557: 558: 559: 560: 561: 562: 563: 564: 565: 566: 567: 568: 569: 570: 571: 572: 573: 574: 575: 576: 577: 578: 579: 580: 581: 582: 583: 584: 585: 586: 587: 588: 589: 590: 591: 592: 593: 594: 595: 596: 597: 598: 599: 600: 601: 602: 603: 604: 605: 606: 607: 608: 609: 610: 611: 612: 613: 614: 615: 616: 617: 618: 619: 620: 621: 622: 623: 624: 625: 626: 627: 628: 629: 630: 631: 632: 633: 634: 635: 636: 637: 638: 639: 640: 641: 642: 643: 644: 645: 646: 647: 648: 649: 650: 651: 652: 653: 654: 655: 656: 657: 658: 659: 660: 661: 662: 663: 664: 665: 666: 667: 668: 669: 670: 671: 672: 673: 674: 675: 676: 677: 678: 679: 680: 681: 682: 683: 684: 685: 686: 687: 688: 689: 690: 691: 692: 693: 694: 695: 696: 697: 698: 699: 700: 701: 702: 703: 704: 705: 706: 707: 708: 709: 710: 711: 712: 713: 714: 715: 716: 717: 718: 719: 720: 721: 722: 723: 724: 725: 726: 727: 728: 729: 730: 731: 732: 733: 734: 735: 736: 737: 738: 739: 740: 741: 742: 743: 744: 745: 746: 747: 748: 749: 750: 751: 752: 753: 754: 755: 756: 757: 758: 759: 760: 761: 762: 763: 764: 765: 766: 767: 768: 769: 770: 771: 772: 773: 774: 775: 776: 777: 778: 779: 780: 781: 782: 783: 784: 785: 786: 787: 788: 789: 790: 791: 792: 793: 794: 795: 796: 797: 798: 799: 800: 801: 802: 803: 804: 805: 806: 807: 808: 809: 810: 811: 812: 813: 814: 815: 
<?php
class Item extends CI_Model
{
    /*
    Determines if a given item_id is an item
    */
    public function exists($item_id, $ignore_deleted = FALSE, $deleted = FALSE)
    {
        if (ctype_digit($item_id))
        {
            $this->db->from('items');
            $this->db->where('item_id', (int) $item_id);
            if ($ignore_deleted == FALSE)
            {
                $this->db->where('deleted', $deleted);
            }

            return ($this->db->get()->num_rows() == 1);
        }

        return FALSE;
    }

    /*
    Determines if a given item_number exists
    */
    public function item_number_exists($item_number, $item_id = '')
    {
        $this->db->from('items');
        $this->db->where('item_number', (string) $item_number);
        if(ctype_digit($item_id))
        {
            $this->db->where('item_id !=', (int) $item_id);
        }

        return ($this->db->get()->num_rows() == 1);
    }

    /*
    Gets total of rows
    */
    public function get_total_rows()
    {
        $this->db->from('items');
        $this->db->where('deleted', 0);

        return $this->db->count_all_results();
    }

    /*
    Get number of rows
    */
    public function get_found_rows($search, $filters)
    {
        return $this->search($search, $filters)->num_rows();
    }

    /*
    Perform a search on items
    */
    public function search($search, $filters, $rows = 0, $limit_from = 0, $sort = 'items.name', $order = 'asc')
    {
        $this->db->select('MAX(items.name) as name');
        $this->db->select('MAX(items.category) as category');
        $this->db->select('MAX(items.supplier_id) as supplier_id');
        $this->db->select('MAX(items.item_number) as item_number');
        $this->db->select('MAX(items.description) as description');
        $this->db->select('MAX(items.cost_price) as cost_price');
        $this->db->select('MAX(items.unit_price) as unit_price');
        $this->db->select('MAX(items.reorder_level) as reorder_level');
        $this->db->select('MAX(items.receiving_quantity) as receiving_quantity');
        $this->db->select('items.item_id as item_id');
        $this->db->select('MAX(items.pic_filename) as pic_filename');
        $this->db->select('MAX(items.allow_alt_description) as allow_alt_description');
        $this->db->select('MAX(items.is_serialized) as is_serialized');
        $this->db->select('MAX(items.deleted) as deleted');
        $this->db->select('MAX(items.custom1) as custom1');
        $this->db->select('MAX(items.custom2) as custom2');
        $this->db->select('MAX(items.custom3) as custom3');
        $this->db->select('MAX(items.custom4) as custom4');
        $this->db->select('MAX(items.custom5) as custom5');
        $this->db->select('MAX(items.custom6) as custom6');
        $this->db->select('MAX(items.custom7) as custom7');
        $this->db->select('MAX(items.custom8) as custom8');
        $this->db->select('MAX(items.custom9) as custom9');
        $this->db->select('MAX(items.custom10) as custom10');

        $this->db->select('MAX(suppliers.person_id) as person_id');
        $this->db->select('MAX(suppliers.company_name) as company_name');
        $this->db->select('MAX(suppliers.agency_name) as agency_name');
        $this->db->select('MAX(suppliers.account_number) as account_number');
        $this->db->select('MAX(suppliers.deleted) as deleted');

        $this->db->select('MAX(inventory.trans_id) as trans_id');
        $this->db->select('MAX(inventory.trans_items) as trans_items');
        $this->db->select('MAX(inventory.trans_user) as trans_user');
        $this->db->select('MAX(inventory.trans_date) as trans_date');
        $this->db->select('MAX(inventory.trans_comment) as trans_comment');
        $this->db->select('MAX(inventory.trans_location) as trans_location');
        $this->db->select('MAX(inventory.trans_inventory) as trans_inventory');

        if($filters['stock_location_id'] > -1)
        {
            $this->db->select('MAX(item_quantities.item_id) as qty_item_id');
            $this->db->select('MAX(item_quantities.location_id) as location_id');
            $this->db->select('MAX(item_quantities.quantity) as quantity');
        }

        $this->db->from('items as items');
        $this->db->join('suppliers as suppliers', 'suppliers.person_id = items.supplier_id', 'left');
        $this->db->join('inventory as inventory', 'inventory.trans_items = items.item_id');

        if($filters['stock_location_id'] > -1)
        {
            $this->db->join('item_quantities as item_quantities', 'item_quantities.item_id = items.item_id');
            $this->db->where('location_id', $filters['stock_location_id']);
        }

        if(empty($this->config->item('date_or_time_format')))
        {
            $this->db->where('DATE_FORMAT(trans_date, "%Y-%m-%d") BETWEEN ' . $this->db->escape($filters['start_date']) . ' AND ' . $this->db->escape($filters['end_date']));
        }
        else
        {
            $this->db->where('trans_date BETWEEN ' . $this->db->escape(rawurldecode($filters['start_date'])) . ' AND ' . $this->db->escape(rawurldecode($filters['end_date'])));
        }

        if(!empty($search))
        {
            if($filters['search_custom'] == FALSE)
            {
                $this->db->group_start();
                    $this->db->like('name', $search);
                    $this->db->or_like('item_number', $search);
                    $this->db->or_like('items.item_id', $search);
                    $this->db->or_like('company_name', $search);
                    $this->db->or_like('category', $search);
                $this->db->group_end();
            }
            else
            {
                $this->db->group_start();
                    $this->db->like('custom1', $search);
                    $this->db->or_like('custom2', $search);
                    $this->db->or_like('custom3', $search);
                    $this->db->or_like('custom4', $search);
                    $this->db->or_like('custom5', $search);
                    $this->db->or_like('custom6', $search);
                    $this->db->or_like('custom7', $search);
                    $this->db->or_like('custom8', $search);
                    $this->db->or_like('custom9', $search);
                    $this->db->or_like('custom10', $search);
                $this->db->group_end();
            }
        }

        $this->db->where('items.deleted', $filters['is_deleted']);

        if($filters['empty_upc'] != FALSE)
        {
            $this->db->where('item_number', NULL);
        }
        if($filters['low_inventory'] != FALSE)
        {
            $this->db->where('quantity <=', 'reorder_level');
        }
        if($filters['is_serialized'] != FALSE)
        {
            $this->db->where('is_serialized', 1);
        }
        if($filters['no_description'] != FALSE)
        {
            $this->db->where('items.description', '');
        }

        // avoid duplicated entries with same name because of inventory reporting multiple changes on the same item in the same date range
        $this->db->group_by('items.item_id');

        // order by name of item
        $this->db->order_by($sort, $order);

        if($rows > 0)
        {
            $this->db->limit($rows, $limit_from);
        }

        return $this->db->get();
    }

    /*
    Returns all the items
    */
    public function get_all($stock_location_id = -1, $rows = 0, $limit_from = 0)
    {
        $this->db->from('items');
        $this->db->join('suppliers', 'suppliers.person_id = items.supplier_id', 'left');

        if($stock_location_id > -1)
        {
            $this->db->join('item_quantities', 'item_quantities.item_id = items.item_id');
            $this->db->where('location_id', $stock_location_id);
        }

        $this->db->where('items.deleted', 0);

        // order by name of item
        $this->db->order_by('items.name', 'asc');

        if($rows > 0)
        {
            $this->db->limit($rows, $limit_from);
        }

        return $this->db->get();
    }

    /*
    Gets information about a particular item
    */
    public function get_info($item_id)
    {
        $this->db->select('items.*');
        $this->db->select('suppliers.company_name');
        $this->db->from('items');
        $this->db->join('suppliers', 'suppliers.person_id = items.supplier_id', 'left');
        $this->db->where('item_id', $item_id);

        $query = $this->db->get();

        if($query->num_rows() == 1)
        {
            return $query->row();
        }
        else
        {
            //Get empty base parent object, as $item_id is NOT an item
            $item_obj = new stdClass();

            //Get all the fields from items table
            foreach($this->db->list_fields('items') as $field)
            {
                $item_obj->$field = '';
            }

            return $item_obj;
        }
    }

    /*
    Gets information about a particular item by item id or number
    */
    public function get_info_by_id_or_number($item_id)
    {
        $this->db->from('items');

        if (ctype_digit($item_id))
        {
            $this->db->group_start();
                $this->db->where('item_id', (int) $item_id);
                $this->db->or_where('items.item_number', $item_id);
            $this->db->group_end();
        }
        else
        {
            $this->db->where('item_number', $item_id);
        }

        $this->db->where('items.deleted', 0);

        $query = $this->db->get();

        if($query->num_rows() == 1)
        {
            return $query->row();
        }

        return '';
    }

    /*
    Get an item id given an item number
    */
    public function get_item_id($item_number, $ignore_deleted = FALSE, $deleted = FALSE)
    {
        $this->db->from('items');
        $this->db->join('suppliers', 'suppliers.person_id = items.supplier_id', 'left');
        $this->db->where('item_number', $item_number);
        if($ignore_deleted == FALSE)
        {
            $this->db->where('items.deleted', $deleted);
        }

        $query = $this->db->get();

        if($query->num_rows() == 1)
        {
            return $query->row()->item_id;
        }

        return FALSE;
    }

    /*
    Gets information about multiple items
    */
    public function get_multiple_info($item_ids, $location_id)
    {
        $this->db->from('items');
        $this->db->join('suppliers', 'suppliers.person_id = items.supplier_id', 'left');
        $this->db->join('item_quantities', 'item_quantities.item_id = items.item_id', 'left');
        $this->db->where('location_id', $location_id);
        $this->db->where_in('items.item_id', $item_ids);

        return $this->db->get();
    }

    /*
    Inserts or updates a item
    */
    public function save(&$item_data, $item_id = FALSE)
    {
        if(!$item_id || !$this->exists($item_id, TRUE))
        {
            if($this->db->insert('items', $item_data))
            {
                $item_data['item_id'] = $this->db->insert_id();

                return TRUE;
            }

            return FALSE;
        }

        $this->db->where('item_id', $item_id);

        return $this->db->update('items', $item_data);
    }

    /*
    Updates multiple items at once
    */
    public function update_multiple($item_data, $item_ids)
    {
        $this->db->where_in('item_id', explode(':', $item_ids));

        return $this->db->update('items', $item_data);
    }

    /*
    Deletes one item
    */
    public function delete($item_id)
    {
        //Run these queries as a transaction, we want to make sure we do all or nothing
        $this->db->trans_start();

        // set to 0 quantities
        $this->Item_quantity->reset_quantity($item_id);
        $this->db->where('item_id', $item_id);
        $success = $this->db->update('items', array('deleted'=>1));

        $this->db->trans_complete();

        $success &= $this->db->trans_status();

        return $success;
    }

    /*
    Undeletes one item
    */
    public function undelete($item_id)
    {
        $this->db->where('item_id', $item_id);

        return $this->db->update('items', array('deleted'=>0));
    }

    /*
    Deletes a list of items
    */
    public function delete_list($item_ids)
    {
        //Run these queries as a transaction, we want to make sure we do all or nothing
        $this->db->trans_start();

        // set to 0 quantities
        $this->Item_quantity->reset_quantity_list($item_ids);
        $this->db->where_in('item_id', $item_ids);
        $success = $this->db->update('items', array('deleted'=>1));

        $this->db->trans_complete();

        $success &= $this->db->trans_status();

        return $success;
    }

    public function get_search_suggestions($search, $filters = array('is_deleted' => FALSE, 'search_custom' => FALSE), $unique = FALSE, $limit = 25)
    {
        $suggestions = array();

        $this->db->select('item_id, name');
        $this->db->from('items');
        $this->db->where('deleted', $filters['is_deleted']);
        $this->db->where("item_type = '0'"); // standard, exclude kit items since kits will be picked up later
        $this->db->like('name', $search);
        $this->db->order_by('name', 'asc');
        foreach($this->db->get()->result() as $row)
        {
            $suggestions[] = array('value' => $row->item_id, 'label' => $row->name);
        }

        $this->db->select('item_id, item_number');
        $this->db->from('items');
        $this->db->where('deleted', $filters['is_deleted']);
        $this->db->where("item_type = '0'"); // standard, exclude kit items since kits will be picked up later
        $this->db->like('item_number', $search);
        $this->db->order_by('item_number', 'asc');
        foreach($this->db->get()->result() as $row)
        {
            $suggestions[] = array('value' => $row->item_id, 'label' => $row->item_number);
        }

        if(!$unique)
        {
            //Search by category
            $this->db->select('category');
            $this->db->from('items');
            $this->db->where('deleted', $filters['is_deleted']);
            $this->db->where("item_type = '0'"); // standard, exclude kit items since kits will be picked up later
            $this->db->distinct();
            $this->db->like('category', $search);
            $this->db->order_by('category', 'asc');
            foreach($this->db->get()->result() as $row)
            {
                $suggestions[] = array('label' => $row->category);
            }

            //Search by supplier
            $this->db->select('company_name');
            $this->db->from('suppliers');
            $this->db->like('company_name', $search);
            // restrict to non deleted companies only if is_deleted is FALSE
            $this->db->where('deleted', $filters['is_deleted']);
            $this->db->where("item_type = '0'"); // standard, exclude kit items since kits will be picked up later
            $this->db->distinct();
            $this->db->order_by('company_name', 'asc');
            foreach($this->db->get()->result() as $row)
            {
                $suggestions[] = array('label' => $row->company_name);
            }

            //Search by description
            $this->db->select('item_id, name, description');
            $this->db->from('items');
            $this->db->where('deleted', $filters['is_deleted']);
            $this->db->where("item_type = '0'"); // standard, exclude kit items since kits will be picked up later
            $this->db->like('description', $search);
            $this->db->order_by('description', 'asc');
            foreach($this->db->get()->result() as $row)
            {
                $entry = array('value' => $row->item_id, 'label' => $row->name);
                if(!array_walk($suggestions, function($value, $label) use ($entry) { return $entry['label'] != $label; } ))
                {
                    $suggestions[] = $entry;
                }
            }

            //Search by custom fields
            if($filters['search_custom'] != FALSE)
            {
                $this->db->from('items');
                $this->db->group_start();
                    $this->db->like('custom1', $search);
                    $this->db->or_like('custom2', $search);
                    $this->db->or_like('custom3', $search);
                    $this->db->or_like('custom4', $search);
                    $this->db->or_like('custom5', $search);
                    $this->db->or_like('custom6', $search);
                    $this->db->or_like('custom7', $search);
                    $this->db->or_like('custom8', $search);
                    $this->db->or_like('custom9', $search);
                    $this->db->or_like('custom10', $search);
                $this->db->group_end();
                $this->db->where('deleted', $filters['is_deleted']);
                $this->db->where("item_type = '0'"); // standard, exclude kit items since kits will be picked up later
                foreach($this->db->get()->result() as $row)
                {
                    $suggestions[] = array('value' => $row->item_id, 'label' => $row->name);
                }
            }
        }


        //only return $limit suggestions
        if(count($suggestions > $limit))
        {
            $suggestions = array_slice($suggestions, 0,$limit);
        }

        return $suggestions;
    }


    public function get_stock_search_suggestions($search, $filters = array('is_deleted' => FALSE, 'search_custom' => FALSE), $unique = FALSE, $limit = 25)
    {
        $suggestions = array();

        $this->db->select('item_id, name');
        $this->db->from('items');
        $this->db->where('deleted', $filters['is_deleted']);
        $this->db->where("item_type = '0'"); // standard, exclude kit items since kits will be picked up later
        $this->db->where("stock_type = '0'"); // stocked items only
        $this->db->like('name', $search);
        $this->db->order_by('name', 'asc');
        foreach($this->db->get()->result() as $row)
        {
            $suggestions[] = array('value' => $row->item_id, 'label' => $row->name);
        }

        $this->db->select('item_id, item_number');
        $this->db->from('items');
        $this->db->where('deleted', $filters['is_deleted']);
        $this->db->where("item_type = '0'"); // standard, exclude kit items since kits will be picked up later
        $this->db->where("stock_type = '0'"); // stocked items only
        $this->db->like('item_number', $search);
        $this->db->order_by('item_number', 'asc');
        foreach($this->db->get()->result() as $row)
        {
            $suggestions[] = array('value' => $row->item_id, 'label' => $row->item_number);
        }

        if(!$unique)
        {
            //Search by category
            $this->db->select('category');
            $this->db->from('items');
            $this->db->where('deleted', $filters['is_deleted']);
            $this->db->where("item_type = '0'"); // standard, exclude kit items since kits will be picked up later
            $this->db->where("stock_type = '0'"); // stocked items only
            $this->db->distinct();
            $this->db->like('category', $search);
            $this->db->order_by('category', 'asc');
            foreach($this->db->get()->result() as $row)
            {
                $suggestions[] = array('label' => $row->category);
            }

            //Search by supplier
            $this->db->select('company_name');
            $this->db->from('suppliers');
            $this->db->like('company_name', $search);
            // restrict to non deleted companies only if is_deleted is FALSE
            $this->db->where('deleted', $filters['is_deleted']);
            $this->db->distinct();
            $this->db->order_by('company_name', 'asc');
            foreach($this->db->get()->result() as $row)
            {
                $suggestions[] = array('label' => $row->company_name);
            }

            //Search by description
            $this->db->select('item_id, name, description');
            $this->db->from('items');
            $this->db->where('deleted', $filters['is_deleted']);
            $this->db->where("item_type = '0'"); // standard, exclude kit items since kits will be picked up later
            $this->db->where("stock_type = '0'"); // stocked items only
            $this->db->like('description', $search);
            $this->db->order_by('description', 'asc');
            foreach($this->db->get()->result() as $row)
            {
                $entry = array('value' => $row->item_id, 'label' => $row->name);
                if(!array_walk($suggestions, function($value, $label) use ($entry) { return $entry['label'] != $label; } ))
                {
                    $suggestions[] = $entry;
                }
            }

            //Search by custom fields
            if($filters['search_custom'] != FALSE)
            {
                $this->db->from('items');
                $this->db->group_start();
                $this->db->like('custom1', $search);
                $this->db->or_like('custom2', $search);
                $this->db->or_like('custom3', $search);
                $this->db->or_like('custom4', $search);
                $this->db->or_like('custom5', $search);
                $this->db->or_like('custom6', $search);
                $this->db->or_like('custom7', $search);
                $this->db->or_like('custom8', $search);
                $this->db->or_like('custom9', $search);
                $this->db->or_like('custom10', $search);
                $this->db->group_end();
                $this->db->where("item_type = '0'"); // standard, exclude kit items since kits will be picked up later
                $this->db->where("stock_type = '0'"); // stocked items only
                $this->db->where('deleted', $filters['is_deleted']);
                foreach($this->db->get()->result() as $row)
                {
                    $suggestions[] = array('value' => $row->item_id, 'label' => $row->name);
                }
            }
        }


        //only return $limit suggestions
        if(count($suggestions > $limit))
        {
            $suggestions = array_slice($suggestions, 0,$limit);
        }

        return $suggestions;
    }

    public function get_kit_search_suggestions($search, $filters = array('is_deleted' => FALSE, 'search_custom' => FALSE), $unique = FALSE, $limit = 25)
    {
        $suggestions = array();

        $this->db->select('item_id, name');
        $this->db->from('items');
        $this->db->where('deleted', $filters['is_deleted']);
        $this->db->where("item_type = '1'"); // standard, exclude kit items since kits will be picked up later
        $this->db->like('name', $search);
        $this->db->order_by('name', 'asc');
        foreach($this->db->get()->result() as $row)
        {
            $suggestions[] = array('value' => $row->item_id, 'label' => $row->name);
        }

        $this->db->select('item_id, item_number');
        $this->db->from('items');
        $this->db->where('deleted', $filters['is_deleted']);
        $this->db->like('item_number', $search);
        $this->db->order_by('item_number', 'asc');
        foreach($this->db->get()->result() as $row)
        {
            $suggestions[] = array('value' => $row->item_id, 'label' => $row->item_number);
        }

        if(!$unique)
        {
            //Search by category
            $this->db->select('category');
            $this->db->from('items');
            $this->db->where('deleted', $filters['is_deleted']);
            $this->db->distinct();
            $this->db->like('category', $search);
            $this->db->order_by('category', 'asc');
            foreach($this->db->get()->result() as $row)
            {
                $suggestions[] = array('label' => $row->category);
            }

            //Search by supplier
            $this->db->select('company_name');
            $this->db->from('suppliers');
            $this->db->like('company_name', $search);
            // restrict to non deleted companies only if is_deleted is FALSE
            $this->db->where('deleted', $filters['is_deleted']);
            $this->db->distinct();
            $this->db->order_by('company_name', 'asc');
            foreach($this->db->get()->result() as $row)
            {
                $suggestions[] = array('label' => $row->company_name);
            }

            //Search by description
            $this->db->select('item_id, name, description');
            $this->db->from('items');
            $this->db->where('deleted', $filters['is_deleted']);
            $this->db->like('description', $search);
            $this->db->order_by('description', 'asc');
            foreach($this->db->get()->result() as $row)
            {
                $entry = array('value' => $row->item_id, 'label' => $row->name);
                if(!array_walk($suggestions, function($value, $label) use ($entry) { return $entry['label'] != $label; } ))
                {
                    $suggestions[] = $entry;
                }
            }

            //Search by custom fields
            if($filters['search_custom'] != FALSE)
            {
                $this->db->from('items');
                $this->db->group_start();
                $this->db->like('custom1', $search);
                $this->db->or_like('custom2', $search);
                $this->db->or_like('custom3', $search);
                $this->db->or_like('custom4', $search);
                $this->db->or_like('custom5', $search);
                $this->db->or_like('custom6', $search);
                $this->db->or_like('custom7', $search);
                $this->db->or_like('custom8', $search);
                $this->db->or_like('custom9', $search);
                $this->db->or_like('custom10', $search);
                $this->db->group_end();
                $this->db->where('deleted', $filters['is_deleted']);
                foreach($this->db->get()->result() as $row)
                {
                    $suggestions[] = array('value' => $row->item_id, 'label' => $row->name);
                }
            }
        }

        //only return $limit suggestions
        if(count($suggestions > $limit))
        {
            $suggestions = array_slice($suggestions, 0,$limit);
        }

        return $suggestions;
    }

    public function get_category_suggestions($search)
    {
        $suggestions = array();
        $this->db->distinct();
        $this->db->select('category');
        $this->db->from('items');
        $this->db->like('category', $search);
        $this->db->where('deleted', 0);
        $this->db->order_by('category', 'asc');
        foreach($this->db->get()->result() as $row)
        {
            $suggestions[] = array('label' => $row->category);
        }

        return $suggestions;
    }

    public function get_location_suggestions($search)
    {
        $suggestions = array();
        $this->db->distinct();
        $this->db->select('location');
        $this->db->from('items');
        $this->db->like('location', $search);
        $this->db->where('deleted', 0);
        $this->db->order_by('location', 'asc');
        foreach($this->db->get()->result() as $row)
        {
            $suggestions[] = array('label' => $row->location);
        }

        return $suggestions;
    }

    public function get_custom_suggestions($search, $field_no)
    {
        $suggestions = array();
        $this->db->distinct();
        $this->db->select('custom'.$field_no);
        $this->db->from('items');
        $this->db->like('custom'.$field_no, $search);
        $this->db->where('deleted', 0);
        $this->db->order_by('custom'.$field_no, 'asc');
        foreach($this->db->get()->result() as $row)
        {
            $row_array = (array) $row;
            $suggestions[] = array('label' => $row_array['custom'.$field_no]);
        }

        return $suggestions;
    }

    public function get_categories()
    {
        $this->db->select('category');
        $this->db->from('items');
        $this->db->where('deleted', 0);
        $this->db->distinct();
        $this->db->order_by('category', 'asc');

        return $this->db->get();
    }

    /*
     * changes the cost price of a given item
     * calculates the average price between received items and items on stock
     * $item_id : the item which price should be changed
     * $items_received : the amount of new items received
     * $new_price : the cost-price for the newly received items
     * $old_price (optional) : the current-cost-price
     *
     * used in receiving-process to update cost-price if changed
     * caution: must be used before item_quantities gets updated, otherwise the average price is wrong!
     *
     */
    public function change_cost_price($item_id, $items_received, $new_price, $old_price = null)
    {
        if($old_price === null)
        {
            $item_info = $this->get_info($item_id);
            $old_price = $item_info->cost_price;
        }

        $this->db->from('item_quantities');
        $this->db->select_sum('quantity');
        $this->db->where('item_id', $item_id);
        $this->db->join('stock_locations', 'stock_locations.location_id=item_quantities.location_id');
        $this->db->where('stock_locations.deleted', 0);
        $old_total_quantity = $this->db->get()->row()->quantity;

        $total_quantity = $old_total_quantity + $items_received;
        $average_price = bcdiv(bcadd(bcmul($items_received, $new_price), bcmul($old_total_quantity, $old_price)), $total_quantity);

        $data = array('cost_price' => $average_price);

        return $this->save($data, $item_id);
    }
}
?>
API documentation generated by ApiGen