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: 816: 817: 818: 819: 820: 821: 822: 823: 824: 825: 826: 827: 828: 829: 830: 831: 832: 833: 834: 835: 836: 837: 838: 
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once("Secure_Controller.php");

class Config extends Secure_Controller 
{
    public function __construct()
    {
        parent::__construct('config');

        $this->load->library('barcode_lib');
        $this->load->library('sale_lib');
    }

    /*
    * This function loads all the licenses starting with the first one being OSPOS one
    */
    private function _licenses()
    {
        $i = 0;
        $bower = FALSE;
        $composer = FALSE;
        $license = array();

        $license[$i]['title'] = 'Open Source Point Of Sale ' . $this->config->item('application_version');

        if(file_exists('license/LICENSE'))
        {
            $license[$i]['text'] = $this->xss_clean(file_get_contents('license/LICENSE', NULL, NULL, 0, 2000));
        }
        else
        {
            $license[$i]['text'] = 'LICENSE file must be in OSPOS license directory. You are not allowed to use OSPOS application until the distribution copy of LICENSE file is present.';
        }

        // read all the files in the dir license
        $dir = new DirectoryIterator('license');

        foreach($dir as $fileinfo)
        {
            // license files must be in couples: .version (name & version) & .license (license text)
            if($fileinfo->isFile())
            {
                if($fileinfo->getExtension() == 'version')
                {
                    ++$i;

                    $basename = 'license/' . $fileinfo->getBasename('.version');

                    $license[$i]['title'] = $this->xss_clean(file_get_contents($basename . '.version', NULL, NULL, 0, 100));

                    $license_text_file = $basename . '.license';

                    if(file_exists($license_text_file))
                    {
                        $license[$i]['text'] = $this->xss_clean(file_get_contents($license_text_file , NULL, NULL, 0, 2000));
                    }
                    else
                    {
                        $license[$i]['text'] = $license_text_file . ' file is missing';
                    }
                }
                elseif($fileinfo->getBasename() == 'bower.LICENSES')
                {
                    // set a flag to indicate that the JS Plugin bower.LICENSES file is available and needs to be attached at the end
                    $bower = TRUE;
                }
                elseif($fileinfo->getBasename() == 'composer.LICENSES')
                {
                    // set a flag to indicate that the composer.LICENSES file is available and needs to be attached at the end
                    $composer = TRUE;
                }
            }
        }

        // attach the licenses from the LICENSES file generated by bower
        if($composer)
        {
            ++$i;
            $license[$i]['title'] = 'Composer Libraries';
            $license[$i]['text'] = '';

            $file = file_get_contents('license/composer.LICENSES');
            $array = json_decode($file, TRUE);

            foreach($array as $key => $val)
            {
                if(is_array($val) && $key == 'dependencies')
                {   
                    foreach($val as $key1 => $val1)
                    {
                        if(is_array($val1))
                        {   
                            $license[$i]['text'] .= 'component: ' . $key1 . "\n";

                            foreach($val1 as $key2 => $val2)
                            {
                                if(is_array($val2))
                                {
                                    $license[$i]['text'] .= $key2 . ': ';

                                    foreach($val2 as $key3 => $val3)
                                    {
                                        $license[$i]['text'] .= $val3 . ' ';
                                    }

                                    $license[$i]['text'] .= "\n";
                                }
                                else
                                {
                                    $license[$i]['text'] .= $key2 . ': ' . $val2 . "\n";
                                }
                            }

                            $license[$i]['text'] .= "\n";
                        }
                        else
                        {
                            $license[$i]['text'] .= $key1 . ': ' . $val1 . "\n";
                        }
                    }
                }
            }
            
            $license[$i]['text'] = $this->xss_clean($license[$i]['text']);
        }

        // attach the licenses from the LICENSES file generated by bower
        if($bower)
        {
            ++$i;
            $license[$i]['title'] = 'JS Plugins';
            $license[$i]['text'] = '';

            $file = file_get_contents('license/bower.LICENSES');
            $array = json_decode($file, TRUE);

            foreach($array as $key => $val)
            {
                if(is_array($val))
                {
                    $license[$i]['text'] .= 'component: ' . $key . "\n";
                    
                    foreach($val as $key1 => $val1)
                    {
                        if(is_array($val1))
                        {
                            $license[$i]['text'] .= $key1 . ': ';

                            foreach($val1 as $key2 => $val2)
                            {
                                $license[$i]['text'] .= $val2 . ' ';
                            }

                            $license[$i]['text'] .= "\n";
                        }
                        else
                        {
                            $license[$i]['text'] .= $key1 . ': ' . $val1 . "\n";
                        }
                    }

                    $license[$i]['text'] .= "\n";
                }
            }

            $license[$i]['text'] = $this->xss_clean($license[$i]['text']);
        }

        return $license;
    }

    /*
    * This function loads all the available themes in the dist/bootswatch directory
    */
    private function _themes()
    {
        $themes = array();

        // read all themes in the dist folder
        $dir = new DirectoryIterator('dist/bootswatch');

        foreach($dir as $dirinfo)
        {
            if($dirinfo->isDir() && !$dirinfo->isDot() && $dirinfo->getFileName() != 'fonts')
            {
                $file = $this->xss_clean($dirinfo->getFileName());
                $themes[$file] = $file;
            }
        }

        asort($themes);

        return $themes;
    }

    public function index()
    {
        $data['stock_locations'] = $this->Stock_location->get_all()->result_array();
        $data['dinner_tables'] = $this->Dinner_table->get_all()->result_array();
        $data['customer_rewards'] = $this->Customer_rewards->get_all()->result_array();
        $data['support_barcode'] = $this->barcode_lib->get_list_barcodes();
        $data['logo_exists'] = $this->config->item('company_logo') != '';
        $data['line_sequence_options'] = $this->sale_lib->get_line_sequence_options();
        $data['register_mode_options'] = $this->sale_lib->get_register_mode_options();
        $data['rounding_options'] = Rounding_code::get_rounding_options();

        $data = $this->xss_clean($data);

        // load all the license statements, they are already XSS cleaned in the private function
        $data['licenses'] = $this->_licenses();
        // load all the themes, already XSS cleaned in the private function
        $data['themes'] = $this->_themes();

        $data['mailchimp'] = array();
        if($this->_check_encryption())
        {
            $data['mailchimp']['api_key'] = $this->encryption->decrypt($this->config->item('mailchimp_api_key'));
            $data['mailchimp']['list_id'] = $this->encryption->decrypt($this->config->item('mailchimp_list_id'));
        }
        else
        {
            $data['mailchimp']['api_key'] = '';
            $data['mailchimp']['list_id'] = '';     
        }

        // load mailchimp lists associated to the given api key, already XSS cleaned in the private function
        $data['mailchimp']['lists'] = $this->_mailchimp();

        $this->load->view("configs/manage", $data);
    }

    public function save_info()
    {
        $upload_success = $this->_handle_logo_upload();
        $upload_data = $this->upload->data();

        $batch_save_data = array(
            'company' => $this->input->post('company'),
            'address' => $this->input->post('address'),
            'phone' => $this->input->post('phone'),
            'email' => $this->input->post('email'),
            'fax' => $this->input->post('fax'),
            'website' => $this->input->post('website'), 
            'return_policy' => $this->input->post('return_policy')
        );

        if (!empty($upload_data['orig_name']))
        {
            // XSS file image sanity check
            if ($this->xss_clean($upload_data['raw_name'], TRUE) === TRUE)
            {
                $batch_save_data['company_logo'] = $upload_data['raw_name'] . $upload_data['file_ext'];
            }
        }

        $result = $this->Appconfig->batch_save($batch_save_data);
        $success = $upload_success && $result ? TRUE : FALSE;
        $message = $this->lang->line('config_saved_' . ($success ? '' : 'un') . 'successfully');
        $message = $upload_success ? $message : strip_tags($this->upload->display_errors());

        echo json_encode(array(
            'success' => $success,
            'message' => $message
        ));
    }

    public function save_general()
    {
        $batch_save_data = array(
            'theme' => $this->input->post('theme'),
            'default_tax_1_rate' => parse_decimals($this->input->post('default_tax_1_rate')),
            'default_tax_1_name' => $this->input->post('default_tax_1_name'),
            'default_tax_2_rate' => parse_decimals($this->input->post('default_tax_2_rate')),
            'default_tax_2_name' => $this->input->post('default_tax_2_name'),
            'tax_included' => $this->input->post('tax_included') != NULL,
            'customer_sales_tax_support' => $this->input->post('customer_sales_tax_support') != NULL,
            'default_origin_tax_code' => $this->input->post('default_origin_tax_code'),
            'receiving_calculate_average_price' => $this->input->post('receiving_calculate_average_price') != NULL,
            'lines_per_page' => $this->input->post('lines_per_page'),
            'default_sales_discount' => $this->input->post('default_sales_discount'),
            'notify_horizontal_position' => $this->input->post('notify_horizontal_position'),
            'notify_vertical_position' => $this->input->post('notify_vertical_position'),
            'custom1_name' => $this->input->post('custom1_name'),
            'custom2_name' => $this->input->post('custom2_name'),
            'custom3_name' => $this->input->post('custom3_name'),
            'custom4_name' => $this->input->post('custom4_name'),
            'custom5_name' => $this->input->post('custom5_name'),
            'custom6_name' => $this->input->post('custom6_name'),
            'custom7_name' => $this->input->post('custom7_name'),
            'custom8_name' => $this->input->post('custom8_name'),
            'custom9_name' => $this->input->post('custom9_name'),
            'custom10_name' => $this->input->post('custom10_name'),
            'statistics' => $this->input->post('statistics') != NULL,
            'giftcard_number' => $this->input->post('giftcard_number'),
        );

        $result = $this->Appconfig->batch_save($batch_save_data);
        $success = $result ? TRUE : FALSE;

        echo json_encode(array(
            'success' => $success,
            'message' => $this->lang->line('config_saved_' . ($success ? '' : 'un') . 'successfully')
        ));
    }

    public function check_number_locale()
    {
        $number_locale = $this->input->post('number_locale');
        $fmt = new \NumberFormatter($number_locale, \NumberFormatter::CURRENCY);
        $currency_symbol = empty($this->input->post('currency_symbol')) ? $fmt->getSymbol(\NumberFormatter::CURRENCY_SYMBOL) : $this->input->post('currency_symbol');
        if ($this->input->post('thousands_separator') == 'false')
        {
            $fmt->setAttribute(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL, '');
        }
        $fmt->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, $currency_symbol);
        $number_local_example = $fmt->format(1234567890.12300);
        echo json_encode(array(
            'success' => $number_local_example != FALSE,
            'number_locale_example' => $number_local_example,
            'currency_symbol' => $currency_symbol,
            'thousands_separator' => $fmt->getAttribute(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL) != ''
        ));
    }

    public function save_locale()
    {
        $exploded = explode(":", $this->input->post('language'));
        $batch_save_data = array(
            'currency_symbol' => $this->input->post('currency_symbol'),
            'language_code' => $exploded[0],
            'language' => $exploded[1],
            'timezone' => $this->input->post('timezone'),
            'dateformat' => $this->input->post('dateformat'),
            'timeformat' => $this->input->post('timeformat'),
            'thousands_separator' => $this->input->post('thousands_separator'),
            'number_locale' => $this->input->post('number_locale'), 
            'currency_decimals' => $this->input->post('currency_decimals'),
            'tax_decimals' => $this->input->post('tax_decimals'),
            'quantity_decimals' => $this->input->post('quantity_decimals'),
            'country_codes' => $this->input->post('country_codes'),
            'payment_options_order' => $this->input->post('payment_options_order'),
            'date_or_time_format' => $this->input->post('date_or_time_format'),
            'cash_decimals' => $this->input->post('cash_decimals'),
            'cash_rounding_code' => $this->input->post('cash_rounding_code'),
            'financial_year' => $this->input->post('financial_year')
        );

        $result = $this->Appconfig->batch_save($batch_save_data);
        $success = $result ? TRUE : FALSE;

        echo json_encode(array(
            'success' => $success,
            'message' => $this->lang->line('config_saved_' . ($success ? '' : 'un') . 'successfully')
        ));
    }

    public function save_email()
    {
        $password = '';

        if($this->_check_encryption())
        {
            $password = $this->encryption->encrypt($this->input->post('smtp_pass'));
        }
        
        $batch_save_data = array(
            'protocol' => $this->input->post('protocol'),
            'mailpath' => $this->input->post('mailpath'),
            'smtp_host' => $this->input->post('smtp_host'),
            'smtp_user' => $this->input->post('smtp_user'),
            'smtp_pass' => $password,
            'smtp_port' => $this->input->post('smtp_port'),
            'smtp_timeout' => $this->input->post('smtp_timeout'),
            'smtp_crypto' => $this->input->post('smtp_crypto')
        );

        $result = $this->Appconfig->batch_save($batch_save_data);
        $success = $result ? TRUE : FALSE;

        echo json_encode(array(
            'success' => $success,
            'message' => $this->lang->line('config_saved_' . ($success ? '' : 'un') . 'successfully')
        ));
    }

    public function save_message()
    {
        $password = '';

        if($this->_check_encryption())
        {
            $password = $this->encryption->encrypt($this->input->post('msg_pwd'));
        }

        $batch_save_data = array(   
            'msg_msg' => $this->input->post('msg_msg'),
            'msg_uid' => $this->input->post('msg_uid'),
            'msg_pwd' => $password,
            'msg_src' => $this->input->post('msg_src')
        );
    
        $result = $this->Appconfig->batch_save($batch_save_data);
        $success = $result ? TRUE : FALSE;

        echo json_encode(array(
            'success' => $success,
            'message' => $this->lang->line('config_saved_' . ($success ? '' : 'un') . 'successfully')
        ));
    }

    /*
    * This function fetches all the available lists from Mailchimp for the given API key
    */
    private function _mailchimp($api_key = '')
    {
        $this->load->library('mailchimp_lib', array('api_key' => $api_key));

        $result = array();

        if(($lists = $this->mailchimp_lib->getLists()) !== FALSE)
        {
            if(is_array($lists) && !empty($lists['lists']) && is_array($lists['lists']))
            {
                foreach($lists['lists'] as $list)
                {
                    $list = $this->xss_clean($list);
                    $result[$list['id']] = $list['name'] . ' [' . $list['stats']['member_count'] . ']';
                }
            }
        }
        
        return $result;
    }

    /*
    AJAX call from mailchimp config form to fetch the Mailchimp lists when a valid API key is inserted
    */
    public function ajax_check_mailchimp_api_key()
    {   
        // load mailchimp lists associated to the given api key, already XSS cleaned in the private function
        $lists = $this->_mailchimp($this->input->post('mailchimp_api_key'));
        $success = count($lists) > 0 ? TRUE : FALSE;

        echo json_encode(array(
            'success' => $success,
            'message' => $this->lang->line('config_mailchimp_key_' . ($success ? '' : 'un') . 'successfully'),
            'mailchimp_lists' => $lists
        ));
    }

    public function save_mailchimp()
    {
        $api_key = '';
        $list_id = '';

        if($this->_check_encryption())
        {
            $api_key = $this->encryption->encrypt($this->input->post('mailchimp_api_key'));
            $list_id = $this->encryption->encrypt($this->input->post('mailchimp_list_id'));
        }

        $batch_save_data = array(
            'mailchimp_api_key' => $api_key,
            'mailchimp_list_id' => $list_id
        );
    
        $result = $this->Appconfig->batch_save($batch_save_data);
        $success = $result ? TRUE : FALSE;

        echo json_encode(array(
            'success' => $success,
            'message' => $this->lang->line('config_saved_' . ($success ? '' : 'un') . 'successfully')
        ));
    }
    
    public function stock_locations() 
    {
        $stock_locations = $this->Stock_location->get_all()->result_array();
        
        $stock_locations = $this->xss_clean($stock_locations);

        $this->load->view('partial/stock_locations', array('stock_locations' => $stock_locations));
    } 
    
    public function dinner_tables() 
    {
        $dinner_tables = $this->Dinner_table->get_all()->result_array();
        
        $dinner_tables = $this->xss_clean($dinner_tables);

        $this->load->view('partial/dinner_tables', array('dinner_tables' => $dinner_tables));
    }

    public function customer_rewards() 
    {
        $customer_rewards = $this->Customer_rewards->get_all()->result_array();
        
        $customer_rewards = $this->xss_clean($customer_rewards);

        $this->load->view('partial/customer_rewards', array('customer_rewards' => $customer_rewards));
    }
    
    private function _clear_session_state()
    {
        $this->sale_lib->clear_sale_location();
        $this->sale_lib->clear_table();
        $this->sale_lib->clear_all();
        $this->load->library('receiving_lib');
        $this->receiving_lib->clear_stock_source();
        $this->receiving_lib->clear_stock_destination();
        $this->receiving_lib->clear_all();
    }
    
    public function save_locations() 
    {
        $this->db->trans_start();
        
        $deleted_locations = $this->Stock_location->get_allowed_locations();
        foreach($this->input->post() as $key => $value)
        {
            if (strstr($key, 'stock_location'))
            {
                $location_id = preg_replace("/.*?_(\d+)$/", "$1", $key);
                unset($deleted_locations[$location_id]);
                // save or update
                $location_data = array('location_name' => $value);
                if ($this->Stock_location->save($location_data, $location_id))
                {
                    $this->_clear_session_state();
                }
            }
        }

        // all locations not available in post will be deleted now
        foreach ($deleted_locations as $location_id => $location_name)
        {
            $this->Stock_location->delete($location_id);
        }

        $this->db->trans_complete();
        
        $success = $this->db->trans_status();
        
        echo json_encode(array('success' => $success, 'message' => $this->lang->line('config_saved_' . ($success ? '' : 'un') . 'successfully')));
    }

    public function save_tables() 
    {
        $this->db->trans_start();
        
        $this->Appconfig->save('dinner_table_enable',$this->input->post('dinner_table_enable'));

        $deleted_tables = $this->Dinner_table->get_all()->result_array();
        $not_to_delete = array();
        foreach($this->input->post() as $key => $value)
        {
            if (strstr($key, 'dinner_table') && $key != 'dinner_table_enable')
            {

                $dinner_table_id = preg_replace("/.*?_(\d+)$/", "$1", $key);
                $not_to_delete[] = $dinner_table_id;

                // save or update
                $table_data = array('name' => $value);
                if ($this->Dinner_table->save($table_data, $dinner_table_id))
                {
                    $this->_clear_session_state();
                }
            }
        }

        // all locations not available in post will be deleted now
        foreach ($deleted_tables as $dinner_table)
        {
            if(!in_array($dinner_table['dinner_table_id'],$not_to_delete))
            {           
                $this->Dinner_table->delete($dinner_table['dinner_table_id']);
            }
        }

        $this->db->trans_complete();
        
        $success = $this->db->trans_status();
        
        echo json_encode(array('success' => $success, 'message' => $this->lang->line('config_saved_' . ($success ? '' : 'un') . 'successfully')));
    }

    public function save_rewards() 
    {
        $this->db->trans_start();
        
        $this->Appconfig->save('customer_reward_enable',$this->input->post('customer_reward_enable'));

        $deleted_packages = $this->Customer_rewards->get_all()->result_array();
        $not_to_delete = array();
        $array_save = array();
        foreach($this->input->post() as $key => $value)
        {   
            if (strstr($key, 'reward_points') && $key != 'customer_reward_enable')
            {
                $customer_reward_id = preg_replace("/.*?_(\d+)$/", "$1", $key);
                $not_to_delete[] = $customer_reward_id;
                $array_save[$customer_reward_id]['points_percent'] = $value;
            }
            if (strstr($key, 'customer_reward') && $key != 'customer_reward_enable')
            {
                $customer_reward_id = preg_replace("/.*?_(\d+)$/", "$1", $key);
                $not_to_delete[] = $customer_reward_id;
                $array_save[$customer_reward_id]['package_name'] = $value;
            }
        }
        if(!empty($array_save))
        foreach ($array_save as $key => $value) {
            // save or update
                $table_data = array('package_name' => $value['package_name'],'points_percent' => $value['points_percent']);
                if ($this->Customer_rewards->save($table_data, $key))
                {
                    $this->_clear_session_state();
                }
        }

        // all locations not available in post will be deleted now
        foreach ($deleted_packages as $customer_reward)
        {
            if(!in_array($customer_reward['customer_reward_id'],$not_to_delete))
            {           
                $this->Customer_rewards->delete($customer_reward['customer_reward_id']);
            }
        }

        $this->db->trans_complete();
        
        $success = $this->db->trans_status();
        
        echo json_encode(array(
            'success' => $success,
            'message' => $this->lang->line('config_saved_' . ($success ? '' : 'un') . 'successfully')
        ));
    }

    public function save_barcode()
    {
        $batch_save_data = array(
            'barcode_type' => $this->input->post('barcode_type'),
            'barcode_quality' => $this->input->post('barcode_quality'),
            'barcode_width' => $this->input->post('barcode_width'),
            'barcode_height' => $this->input->post('barcode_height'),
            'barcode_font' => $this->input->post('barcode_font'),
            'barcode_font_size' => $this->input->post('barcode_font_size'),
            'barcode_first_row' => $this->input->post('barcode_first_row'),
            'barcode_second_row' => $this->input->post('barcode_second_row'),
            'barcode_third_row' => $this->input->post('barcode_third_row'),
            'barcode_num_in_row' => $this->input->post('barcode_num_in_row'),
            'barcode_page_width' => $this->input->post('barcode_page_width'),
            'barcode_page_cellspacing' => $this->input->post('barcode_page_cellspacing'),
            'barcode_generate_if_empty' => $this->input->post('barcode_generate_if_empty') != NULL,
            'barcode_content' => $this->input->post('barcode_content')
        );

        $result = $this->Appconfig->batch_save($batch_save_data);
        $success = $result ? TRUE : FALSE;
        
        echo json_encode(array(
            'success' => $success,
            'message' => $this->lang->line('config_saved_' . ($success ? '' : 'un') . 'successfully')
        ));
    }

    public function save_receipt()
    {
        $batch_save_data = array (
            'receipt_template' => $this->input->post('receipt_template'),
            'receipt_show_company_name' => $this->input->post('receipt_show_company_name') != NULL,
            'receipt_show_taxes' => $this->input->post('receipt_show_taxes') != NULL,
            'receipt_show_total_discount' => $this->input->post('receipt_show_total_discount') != NULL,
            'receipt_show_description' => $this->input->post('receipt_show_description') != NULL,
            'receipt_show_serialnumber' => $this->input->post('receipt_show_serialnumber') != NULL,
            'print_silently' => $this->input->post('print_silently') != NULL,
            'print_header' => $this->input->post('print_header') != NULL,
            'print_footer' => $this->input->post('print_footer') != NULL,
            'print_top_margin' => $this->input->post('print_top_margin'),
            'print_left_margin' => $this->input->post('print_left_margin'),
            'print_bottom_margin' => $this->input->post('print_bottom_margin'),
            'print_right_margin' => $this->input->post('print_right_margin')
        );

        $result = $this->Appconfig->batch_save($batch_save_data);
        $success = $result ? TRUE : FALSE;

        echo json_encode(array(
            'success' => $success,
            'message' => $this->lang->line('config_saved_' . ($success ? '' : 'un') . 'successfully')
        ));
    }

    public function save_invoice()
    {
        $batch_save_data = array (
            'invoice_enable' => $this->input->post('invoice_enable') != NULL,
            'default_register_mode' => $this->input->post('default_register_mode'),
            'sales_invoice_format' => $this->input->post('sales_invoice_format'),
            'sales_quote_format' => $this->input->post('sales_quote_format'),
            'recv_invoice_format' => $this->input->post('recv_invoice_format'),
            'invoice_default_comments' => $this->input->post('invoice_default_comments'),
            'invoice_email_message' => $this->input->post('invoice_email_message'),
            'line_sequence' => $this->input->post('line_sequence'),
            'last_used_invoice_number' =>$this->input->post('last_used_invoice_number'),
            'last_used_quote_number' =>$this->input->post('last_used_quote_number')
        );

        $result = $this->Appconfig->batch_save($batch_save_data);
        $success = $result ? TRUE : FALSE;

        // Update the register mode with the latest change so that if the user
        // switches immediately back to the register the mode reflects the change
        if ($success == TRUE)
        {
            if ($this->config->item('invoice_enable') == '1')
            {
                $this->sale_lib->set_mode($batch_save_data['default_register_mode']);
            }
            else
            {
                $this->sale_lib->set_mode('sale');
            }
        }

        echo json_encode(array(
            'success' => $success,
            'message' => $this->lang->line('config_saved_' . ($success ? '' : 'un') . 'successfully')
        ));
    }

    public function remove_logo()
    {
        $result = $this->Appconfig->batch_save(array('company_logo' => ''));
        
        echo json_encode(array('success' => $result));
    }

    private function _handle_logo_upload()
    {
        $this->load->helper('directory');

        // load upload library
        $config = array('upload_path' => './uploads/',
                'allowed_types' => 'gif|jpg|png',
                'max_size' => '1024',
                'max_width' => '800',
                'max_height' => '680',
                'file_name' => 'company_logo');
        $this->load->library('upload', $config);
        $this->upload->do_upload('company_logo');

        return strlen($this->upload->display_errors()) == 0 || !strcmp($this->upload->display_errors(), '<p>'.$this->lang->line('upload_no_file_selected').'</p>');
    }
    
    private function _check_encryption()
    {
        $encryption_key = $this->config->item('encryption_key');

        // check if the encryption_key config item is the default one
        if($encryption_key == '' || $encryption_key == 'YOUR KEY')
        {
            // Config path
            $config_path = APPPATH . 'config/config.php';

            // Open the file
            $config = file_get_contents($config_path);

            // $key will be assigned a 32-byte (256-bit) hex-encoded random key
            $key = bin2hex($this->encryption->create_key(32));

            // set the encryption key in the config item
            $this->config->set_item('encryption_key', $key);

            // replace the empty placeholder with a real randomly generated encryption key
            $config = preg_replace("/(.*encryption_key.*)('');/", "$1'$key';", $config);

            $result = FALSE;

            // Chmod the file
            @chmod($config_path, 0777);
            
            // Write the new config.php file
            $handle = fopen($config_path, 'w+');

            // Verify file permissions
            if(is_writable($config_path))
            {
                // Write the file
                $result = (fwrite($handle, $config) === FALSE) ? FALSE : TRUE;
            }

            fclose($handle);

            // Chmod the file
            @chmod($config_path, 0444);
            
            return $result;
        }
        
        return TRUE;
    }

    public function backup_db()
    {
        $employee_id = $this->Employee->get_logged_in_employee_info()->person_id;
        if($this->Employee->has_module_grant('config', $employee_id))
        {
            $this->load->dbutil();

            $prefs = array(
                'format' => 'zip',
                'filename' => 'ospos.sql'
            );

            $backup = $this->dbutil->backup($prefs);

            $file_name = 'ospos-' . date("Y-m-d-H-i-s") .'.zip';
            $save = 'uploads/' . $file_name;
            $this->load->helper('download');
            while(ob_get_level())
            {
                ob_end_clean();
            }

            force_download($file_name, $backup);
        }
        else
        {
            redirect('no_access/config');
        }
    }
}
?>
API documentation generated by ApiGen