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:  839:  840:  841:  842:  843:  844:  845:  846:  847:  848:  849:  850:  851:  852:  853:  854:  855:  856:  857:  858:  859:  860:  861:  862:  863:  864:  865:  866:  867:  868:  869:  870:  871:  872:  873:  874:  875:  876:  877:  878:  879:  880:  881:  882:  883:  884:  885:  886:  887:  888:  889:  890:  891:  892:  893:  894:  895:  896:  897:  898:  899:  900:  901:  902:  903:  904:  905:  906:  907:  908:  909:  910:  911:  912:  913:  914:  915:  916:  917:  918:  919:  920:  921:  922:  923:  924:  925:  926:  927:  928:  929:  930:  931:  932:  933:  934:  935:  936:  937:  938:  939:  940:  941:  942:  943:  944:  945:  946:  947:  948:  949:  950:  951:  952:  953:  954:  955:  956:  957:  958:  959:  960:  961:  962:  963:  964:  965:  966:  967:  968:  969:  970:  971:  972:  973:  974:  975:  976:  977:  978:  979:  980:  981:  982:  983:  984:  985:  986:  987:  988:  989:  990:  991:  992:  993:  994:  995:  996:  997:  998:  999: 1000: 1001: 1002: 1003: 1004: 1005: 1006: 1007: 1008: 1009: 1010: 1011: 1012: 1013: 1014: 1015: 1016: 1017: 1018: 1019: 1020: 1021: 1022: 1023: 1024: 1025: 1026: 1027: 1028: 1029: 1030: 1031: 1032: 1033: 1034: 1035: 1036: 1037: 1038: 1039: 1040: 1041: 1042: 1043: 1044: 1045: 1046: 1047: 1048: 1049: 1050: 1051: 1052: 1053: 1054: 1055: 1056: 1057: 1058: 1059: 1060: 1061: 1062: 1063: 1064: 1065: 1066: 1067: 1068: 1069: 1070: 1071: 1072: 1073: 1074: 1075: 1076: 1077: 1078: 1079: 1080: 1081: 1082: 1083: 1084: 1085: 1086: 1087: 1088: 1089: 1090: 1091: 1092: 1093: 1094: 1095: 1096: 1097: 1098: 1099: 1100: 1101: 1102: 1103: 1104: 1105: 1106: 1107: 1108: 1109: 1110: 1111: 1112: 1113: 1114: 1115: 1116: 1117: 1118: 1119: 1120: 1121: 1122: 1123: 1124: 1125: 1126: 1127: 1128: 1129: 1130: 1131: 1132: 1133: 1134: 1135: 1136: 1137: 1138: 1139: 1140: 1141: 1142: 1143: 1144: 1145: 1146: 1147: 1148: 1149: 1150: 1151: 1152: 1153: 1154: 1155: 1156: 1157: 1158: 1159: 1160: 1161: 1162: 1163: 1164: 1165: 1166: 1167: 1168: 1169: 1170: 1171: 1172: 1173: 1174: 1175: 1176: 1177: 1178: 1179: 1180: 1181: 1182: 1183: 1184: 1185: 1186: 1187: 1188: 1189: 1190: 1191: 
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once('Secure_Controller.php');

class Reports extends Secure_Controller
{
    public function __construct()
    {
        parent::__construct('reports');

        $method_name = $this->uri->segment(2);
        $exploder = explode('_', $method_name);

        if(sizeof($exploder) > 1)
        {
            preg_match('/(?:inventory)|([^_.]*)(?:_graph|_row)?$/', $method_name, $matches);
            preg_match('/^(.*?)([sy])?$/', array_pop($matches), $matches);
            $submodule_id = $matches[1] . ((count($matches) > 2) ? $matches[2] : 's');

            $this->track_page('reports/' . $submodule_id, 'reports_' . $submodule_id);

            // check access to report submodule
            if(!$this->Employee->has_grant('reports_' . $submodule_id, $this->Employee->get_logged_in_employee_info()->person_id))
            {
                redirect('no_access/reports/reports_' . $submodule_id);
            }
        }

        $this->load->helper('report');
    }

    //Initial report listing screen
    public function index()
    {
        $data['grants'] = $this->xss_clean($this->Employee->get_employee_grants($this->session->userdata('person_id')));
        
        $this->load->view('reports/listing', $data);
    }

    //Summary sales report
    public function summary_sales($start_date, $end_date, $sale_type, $location_id = 'all')
    {
        $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id);

        $this->load->model('reports/Summary_sales');
        $model = $this->Summary_sales;

        $report_data = $model->getData($inputs);
        $summary = $this->xss_clean($model->getSummaryData($inputs));

        $tabular_data = array();
        foreach($report_data as $row)
        {
            $tabular_data[] = $this->xss_clean(array(
                'sale_date' => $row['sale_date'],
                'quantity' => to_quantity_decimals($row['quantity_purchased']),
                'subtotal' => to_currency($row['subtotal']),
                'tax' => to_currency($row['tax']),
                'total' => to_currency($row['total']),
                'cost' => to_currency($row['cost']),
                'profit' => to_currency($row['profit'])
            ));
        }

        $data = array(
            'title' => $this->lang->line('reports_sales_summary_report'),
            'subtitle' => $this->_get_subtitle_report(array('start_date' => $start_date, 'end_date' => $end_date)),
            'headers' => $this->xss_clean($model->getDataColumns()),
            'data' => $tabular_data,
            'summary_data' => $summary
        );

        $this->load->view('reports/tabular', $data);
    }

    //Summary categories report
    public function summary_categories($start_date, $end_date, $sale_type, $location_id = 'all')
    {
        $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id);

        $this->load->model('reports/Summary_categories');
        $model = $this->Summary_categories;

        $report_data = $model->getData($inputs);
        $summary = $this->xss_clean($model->getSummaryData($inputs));

        $tabular_data = array();
        foreach($report_data as $row)
        {
            $tabular_data[] = $this->xss_clean(array(
                'category' => $row['category'],
                'quantity' => to_quantity_decimals($row['quantity_purchased']),
                'subtotal' => to_currency($row['subtotal']),
                'tax' => to_currency($row['tax']),
                'total' => to_currency($row['total']),
                'cost' => to_currency($row['cost']),
                'profit' => to_currency($row['profit'])
            ));
        }

        $data = array(
            'title' => $this->lang->line('reports_categories_summary_report'),
            'subtitle' => $this->_get_subtitle_report(array('start_date' => $start_date, 'end_date' => $end_date)),
            'headers' => $this->xss_clean($model->getDataColumns()),
            'data' => $tabular_data,
            'summary_data' => $summary
        );

        $this->load->view('reports/tabular', $data);
    }

    //Summary customers report
    public function summary_customers($start_date, $end_date, $sale_type, $location_id = 'all')
    {
        $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id);

        $this->load->model('reports/Summary_customers');
        $model = $this->Summary_customers;

        $report_data = $model->getData($inputs);
        $summary = $this->xss_clean($model->getSummaryData($inputs));

        $tabular_data = array();
        foreach($report_data as $row)
        {
            $tabular_data[] = $this->xss_clean(array(
                'customer_name' => $row['customer'],
                'quantity' => to_quantity_decimals($row['quantity_purchased']),
                'subtotal' => to_currency($row['subtotal']),
                'tax' => to_currency($row['tax']),
                'total' => to_currency($row['total']),
                'cost' => to_currency($row['cost']),
                'profit' => to_currency($row['profit'])
            ));
        }

        $data = array(
            'title' => $this->lang->line('reports_customers_summary_report'),
            'subtitle' => $this->_get_subtitle_report(array('start_date' => $start_date, 'end_date' => $end_date)),
            'headers' => $this->xss_clean($model->getDataColumns()),
            'data' => $tabular_data,
            'summary_data' => $summary
        );

        $this->load->view('reports/tabular', $data);
    }

    //Summary suppliers report
    public function summary_suppliers($start_date, $end_date, $sale_type, $location_id = 'all')
    {
        $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id);

        $this->load->model('reports/Summary_suppliers');
        $model = $this->Summary_suppliers;

        $report_data = $model->getData($inputs);
        $summary = $this->xss_clean($model->getSummaryData($inputs));

        $tabular_data = array();
        foreach($report_data as $row)
        {
            $tabular_data[] = $this->xss_clean(array(
                'supplier_name' => $row['supplier'],
                'quantity' => to_quantity_decimals($row['quantity_purchased']),
                'subtotal' => to_currency($row['subtotal']),
                'tax' => to_currency($row['tax']),
                'total' => to_currency($row['total']),
                'cost' => to_currency($row['cost']),
                'profit' => to_currency($row['profit'])
            ));
        }

        $data = array(
            'title' => $this->lang->line('reports_suppliers_summary_report'),
            'subtitle' => $this->_get_subtitle_report(array('start_date' => $start_date, 'end_date' => $end_date)),
            'headers' => $this->xss_clean($model->getDataColumns()),
            'data' => $tabular_data,
            'summary_data' => $summary
        );

        $this->load->view('reports/tabular', $data);
    }

    //Summary items report
    public function summary_items($start_date, $end_date, $sale_type, $location_id = 'all')
    {
        $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id);

        $this->load->model('reports/Summary_items');
        $model = $this->Summary_items;

        $report_data = $model->getData($inputs);
        $summary = $this->xss_clean($model->getSummaryData($inputs));

        $tabular_data = array();
        foreach($report_data as $row)
        {
            $tabular_data[] = $this->xss_clean(array(
                'item_name' => $row['name'],
                'quantity' => to_quantity_decimals($row['quantity_purchased']),
                'subtotal' => to_currency($row['subtotal']),
                'tax'  => to_currency($row['tax']),
                'total' => to_currency($row['total']),
                'cost' => to_currency($row['cost']),
                'profit' => to_currency($row['profit'])
            ));
        }

        $data = array(
            'title' => $this->lang->line('reports_items_summary_report'),
            'subtitle' => $this->_get_subtitle_report(array('start_date' => $start_date, 'end_date' => $end_date)),
            'headers' => $this->xss_clean($model->getDataColumns()),
            'data' => $tabular_data,
            'summary_data' => $summary
        );

        $this->load->view('reports/tabular', $data);
    }

    //Summary employees report
    public function summary_employees($start_date, $end_date, $sale_type, $location_id = 'all')
    {
        $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id);

        $this->load->model('reports/Summary_employees');
        $model = $this->Summary_employees;

        $report_data = $model->getData($inputs);
        $summary = $this->xss_clean($model->getSummaryData($inputs));

        $tabular_data = array();
        foreach($report_data as $row)
        {
            $tabular_data[] = $this->xss_clean(array(
                'employee_name' => $row['employee'],
                'quantity' => to_quantity_decimals($row['quantity_purchased']),
                'subtotal' => to_currency($row['subtotal']),
                'tax' => to_currency($row['tax']),
                'total' => to_currency($row['total']),
                'cost' => to_currency($row['cost']),
                'profit' => to_currency($row['profit'])
            ));
        }

        $data = array(
            'title' => $this->lang->line('reports_employees_summary_report'),
            'subtitle' => $this->_get_subtitle_report(array('start_date' => $start_date, 'end_date' => $end_date)),
            'headers' => $this->xss_clean($model->getDataColumns()),
            'data' => $tabular_data,
            'summary_data' => $summary
        );

        $this->load->view('reports/tabular', $data);
    }

    //Summary taxes report
    public function summary_taxes($start_date, $end_date, $sale_type, $location_id = 'all')
    {
        $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id);

        $this->load->model('reports/Summary_taxes');
        $model = $this->Summary_taxes;

        $report_data = $model->getData($inputs);
        $summary = $this->xss_clean($model->getSummaryData($inputs));

        $tabular_data = array();
        foreach($report_data as $row)
        {
            $tabular_data[] = $this->xss_clean(array(
                'tax_percent' => $row['percent'],
                'report_count' => $row['count'],
                'subtotal' => to_currency($row['subtotal']),
                'tax' => to_currency($row['tax']),
                'total' => to_currency($row['total'])
            ));
        }

        $data = array(
            'title' => $this->lang->line('reports_taxes_summary_report'),
            'subtitle' => $this->_get_subtitle_report(array('start_date' => $start_date, 'end_date' => $end_date)),
            'headers' => $this->xss_clean($model->getDataColumns()),
            'data' => $tabular_data,
            'summary_data' => $summary
        );

        $this->load->view('reports/tabular', $data);
    }

    //Summary discounts report
    public function summary_discounts($start_date, $end_date, $sale_type, $location_id = 'all')
    {
        $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id);

        $this->load->model('reports/Summary_discounts');
        $model = $this->Summary_discounts;

        $report_data = $model->getData($inputs);
        $summary = $this->xss_clean($model->getSummaryData($inputs));

        $tabular_data = array();
        foreach($report_data as $row)
        {
            $tabular_data[] = $this->xss_clean(array(
                'discount' => $row['discount_percent'],
                'count' => $row['count']
            ));
        }

        $data = array(
            'title' => $this->lang->line('reports_discounts_summary_report'),
            'subtitle' => $this->_get_subtitle_report(array('start_date' => $start_date, 'end_date' => $end_date)),
            'headers' => $this->xss_clean($model->getDataColumns()),
            'data' => $tabular_data,
            'summary_data' => $summary
        );

        $this->load->view('reports/tabular', $data);
    }

    //Summary payments report
    public function summary_payments($start_date, $end_date, $sale_type, $location_id = 'all')
    {
        $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id);

        $this->load->model('reports/Summary_payments');
        $model = $this->Summary_payments;

        $report_data = $model->getData($inputs);
        $summary = $this->xss_clean($model->getSummaryData($inputs));

        $tabular_data = array();
        foreach($report_data as $row)
        {
            $tabular_data[] = $this->xss_clean(array(
                'payment_type' => $row['payment_type'],
                'report_count' => $row['count'],
                'amount_tendered' => to_currency($row['payment_amount'])
            ));
        }

        $data = array(
            'title' => $this->lang->line('reports_payments_summary_report'),
            'subtitle' => $this->_get_subtitle_report(array('start_date' => $start_date, 'end_date' => $end_date)),
            'headers' => $this->xss_clean($model->getDataColumns()),
            'data' => $tabular_data,
            'summary_data' => $summary
        );

        $this->load->view('reports/tabular', $data);
    }

    //Input for reports that require only a date range. (see routes.php to see that all graphical summary reports route here)
    public function date_input()
    {
        $data = array();
        $stock_locations = $data = $this->xss_clean($this->Stock_location->get_allowed_locations('sales'));
        $stock_locations['all'] = $this->lang->line('reports_all');
        $data['stock_locations'] = array_reverse($stock_locations, TRUE);
        $data['mode'] = 'sale';

        $this->load->view('reports/date_input', $data);
    }

    //Input for reports that require only a date range. (see routes.php to see that all graphical summary reports route here)
    public function date_input_sales()
    {
        $data = array();
        $stock_locations = $data = $this->xss_clean($this->Stock_location->get_allowed_locations('sales'));
        $stock_locations['all'] =  $this->lang->line('reports_all');
        $data['stock_locations'] = array_reverse($stock_locations, TRUE);
        $data['mode'] = 'sale';

        $this->load->view('reports/date_input', $data);
    }

    public function date_input_recv()
    {
        $data = array();
        $stock_locations = $data = $this->xss_clean($this->Stock_location->get_allowed_locations('receivings'));
        $stock_locations['all'] =  $this->lang->line('reports_all');
        $data['stock_locations'] = array_reverse($stock_locations, TRUE);
        $data['mode'] = 'receiving';

        $this->load->view('reports/date_input', $data);
    }

    //Graphical summary sales report
    public function graphical_summary_sales($start_date, $end_date, $sale_type, $location_id = 'all')
    {
        $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id);

        $this->load->model('reports/Summary_sales');
        $model = $this->Summary_sales;

        $report_data = $model->getData($inputs);
        $summary = $this->xss_clean($model->getSummaryData($inputs));

        $labels = array();
        $series = array();
        foreach($report_data as $row)
        {
            $row = $this->xss_clean($row);

            $date = date($this->config->item('dateformat'), strtotime($row['sale_date']));
            $labels[] = $date;
            $series[] = array('meta' => $date, 'value' => $row['total']);
        }

        $data = array(
            'title' => $this->lang->line('reports_sales_summary_report'),
            'subtitle' => $this->_get_subtitle_report(array('start_date' => $start_date, 'end_date' => $end_date)),
            'chart_type' => 'reports/graphs/line',
            'labels_1' => $labels,
            'series_data_1' => $series,
            'summary_data_1' => $summary,
            'yaxis_title' => $this->lang->line('reports_revenue'),
            'xaxis_title' => $this->lang->line('reports_date'),
            'show_currency' => TRUE
        );

        $this->load->view('reports/graphical', $data);
    }

    //Graphical summary items report
    public function graphical_summary_items($start_date, $end_date, $sale_type, $location_id = 'all')
    {
        $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id);

        $this->load->model('reports/Summary_items');
        $model = $this->Summary_items;

        $report_data = $model->getData($inputs);
        $summary = $this->xss_clean($model->getSummaryData($inputs));

        $labels = array();
        $series = array();
        foreach($report_data as $row)
        {
            $row = $this->xss_clean($row);

            $labels[] = $row['name'];
            $series[] = $row['total'];
        }

        $data = array(
            'title' => $this->lang->line('reports_items_summary_report'),
            'subtitle' => $this->_get_subtitle_report(array('start_date' => $start_date, 'end_date' => $end_date)),
            'chart_type' => 'reports/graphs/hbar',
            'labels_1' => $labels,
            'series_data_1' => $series,
            'summary_data_1' => $summary,
            'yaxis_title' => $this->lang->line('reports_items'),
            'xaxis_title' => $this->lang->line('reports_revenue'),
            'show_currency' => TRUE
        );

        $this->load->view('reports/graphical', $data);
    }

    //Graphical summary customers report
    public function graphical_summary_categories($start_date, $end_date, $sale_type, $location_id = 'all')
    {
        $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id);

        $this->load->model('reports/Summary_categories');
        $model = $this->Summary_categories;

        $report_data = $model->getData($inputs);
        $summary = $this->xss_clean($model->getSummaryData($inputs));

        $labels = array();
        $series = array();
        foreach($report_data as $row)
        {
            $row = $this->xss_clean($row);

            $labels[] = $row['category'];
            $series[] = array('meta' => $row['category'] . ' ' . round($row['total'] / $summary['total'] * 100, 2) . '%', 'value' => $row['total']);
        }

        $data = array(
            'title' => $this->lang->line('reports_categories_summary_report'),
            'subtitle' => $this->_get_subtitle_report(array('start_date' => $start_date, 'end_date' => $end_date)),
            'chart_type' => 'reports/graphs/pie',
            'labels_1' => $labels,
            'series_data_1' => $series,
            'summary_data_1' => $summary,
            'show_currency' => TRUE
        );

        $this->load->view('reports/graphical', $data);
    }

    //Graphical summary suppliers report
    public function graphical_summary_suppliers($start_date, $end_date, $sale_type, $location_id = 'all')
    {
        $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id);

        $this->load->model('reports/Summary_suppliers');
        $model = $this->Summary_suppliers;

        $report_data = $model->getData($inputs);
        $summary = $this->xss_clean($model->getSummaryData($inputs));

        $labels = array();
        $series = array();
        foreach($report_data as $row)
        {
            $row = $this->xss_clean($row);

            $labels[] = $row['supplier'];
            $series[] = array('meta' => $row['supplier'] . ' ' . round($row['total'] / $summary['total'] * 100, 2) . '%', 'value' => $row['total']);
        }

        $data = array(
            'title' => $this->lang->line('reports_suppliers_summary_report'),
            'subtitle' => $this->_get_subtitle_report(array('start_date' => $start_date, 'end_date' => $end_date)),
            'chart_type' => 'reports/graphs/pie',
            'labels_1' => $labels,
            'series_data_1' => $series,
            'summary_data_1' => $summary,
            'show_currency' => TRUE
        );

        $this->load->view('reports/graphical', $data);
    }

    //Graphical summary employees report
    public function graphical_summary_employees($start_date, $end_date, $sale_type, $location_id = 'all')
    {
        $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id);

        $this->load->model('reports/Summary_employees');
        $model = $this->Summary_employees;
        
        $report_data = $model->getData($inputs);
        $summary = $this->xss_clean($model->getSummaryData($inputs));

        $labels = array();
        $series = array();
        foreach($report_data as $row)
        {
            $row = $this->xss_clean($row);

            $labels[] = $row['employee'];
            $series[] = array('meta' => $row['employee'] . ' ' . round($row['total'] / $summary['total'] * 100, 2) . '%', 'value' => $row['total']);
        }

        $data = array(
            'title' => $this->lang->line('reports_employees_summary_report'),
            'subtitle' => $this->_get_subtitle_report(array('start_date' => $start_date, 'end_date' => $end_date)),
            'chart_type' => 'reports/graphs/pie',
            'labels_1' => $labels,
            'series_data_1' => $series,
            'summary_data_1' => $summary,
            'show_currency' => TRUE
        );

        $this->load->view('reports/graphical', $data);
    }

    //Graphical summary taxes report
    public function graphical_summary_taxes($start_date, $end_date, $sale_type, $location_id = 'all')
    {
        $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id);

        $this->load->model('reports/Summary_taxes');
        $model = $this->Summary_taxes;
        
        $report_data = $model->getData($inputs);
        $summary = $this->xss_clean($model->getSummaryData($inputs));

        $labels = array();
        $series = array();
        foreach($report_data as $row)
        {
            $row = $this->xss_clean($row);

            $labels[] = $row['percent'];
            $series[] = array('meta' => $row['percent'] . ' ' . round($row['total'] / $summary['total'] * 100, 2) . '%', 'value' => $row['total']);
        }

        $data = array(
            'title' => $this->lang->line('reports_taxes_summary_report'),
            'subtitle' => $this->_get_subtitle_report(array('start_date' => $start_date, 'end_date' => $end_date)),
            'chart_type' => 'reports/graphs/pie',
            'labels_1' => $labels,
            'series_data_1' => $series,
            'summary_data_1' => $summary,
            'show_currency' => TRUE
        );

        $this->load->view('reports/graphical', $data);
    }

    //Graphical summary customers report
    public function graphical_summary_customers($start_date, $end_date, $sale_type, $location_id = 'all')
    {
        $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id);
        
        $this->load->model('reports/Summary_customers');
        $model = $this->Summary_customers;
        
        $report_data = $model->getData($inputs);
        $summary = $this->xss_clean($model->getSummaryData($inputs));

        $labels = array();
        $series = array();
        foreach($report_data as $row)
        {
            $row = $this->xss_clean($row);

            $labels[] = $row['customer'];
            $series[] = $row['total'];
        }

        $data = array(
            'title' => $this->lang->line('reports_customers_summary_report'),
            'subtitle' => $this->_get_subtitle_report(array('start_date' => $start_date, 'end_date' => $end_date)),
            'chart_type' => 'reports/graphs/hbar',
            'labels_1' => $labels,
            'series_data_1' => $series,
            'summary_data_1' => $summary,
            'yaxis_title' => $this->lang->line('reports_customers'),
            'xaxis_title' => $this->lang->line('reports_revenue'),
            'show_currency' => TRUE
        );

        $this->load->view('reports/graphical', $data);
    }

    //Graphical summary discounts report
    public function graphical_summary_discounts($start_date, $end_date, $sale_type, $location_id = 'all')
    {
        $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id);

        $this->load->model('reports/Summary_discounts');
        $model = $this->Summary_discounts;
        
        $report_data = $model->getData($inputs);
        $summary = $this->xss_clean($model->getSummaryData($inputs));

        $labels = array();
        $series = array();
        foreach($report_data as $row)
        {
            $row = $this->xss_clean($row);

            $labels[] = $row['discount_percent'];
            $series[] = $row['count'];
        }

        $data = array(
            'title' => $this->lang->line('reports_discounts_summary_report'),
            'subtitle' => $this->_get_subtitle_report(array('start_date' => $start_date, 'end_date' => $end_date)),
            'chart_type' => 'reports/graphs/bar',
            'labels_1' => $labels,
            'series_data_1' => $series,
            'summary_data_1' => $summary,
            'yaxis_title' => $this->lang->line('reports_count'),
            'xaxis_title' => $this->lang->line('reports_discount_percent'),
            'show_currency' => FALSE
        );

        $this->load->view('reports/graphical', $data);
    }

    //Graphical summary payments report
    public function graphical_summary_payments($start_date, $end_date, $sale_type, $location_id = 'all')
    {
        $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id);
        
        $this->load->model('reports/Summary_payments');
        $model = $this->Summary_payments;
        
        $report_data = $model->getData($inputs);
        $summary = $this->xss_clean($model->getSummaryData($inputs));

        $labels = array();
        $series = array();
        foreach($report_data as $row)
        {
            $row = $this->xss_clean($row);

            $labels[] = $row['payment_type'];
            $series[] = array('meta' => $row['payment_type'] . ' ' . round($row['payment_amount'] / $summary['total'] * 100, 2) . '%', 'value' => $row['payment_amount']);
        }

        $data = array(
            'title' => $this->lang->line('reports_payments_summary_report'),
            'subtitle' => $this->_get_subtitle_report(array('start_date' => $start_date, 'end_date' => $end_date)),
            'chart_type' => 'reports/graphs/pie',
            'labels_1' => $labels,
            'series_data_1' => $series,
            'summary_data_1' => $summary,
            'show_currency' => TRUE
        );

        $this->load->view('reports/graphical', $data);
    }

    public function specific_customer_input()
    {
        $data = array();
        $data['specific_input_name'] = $this->lang->line('reports_customer');

        $customers = array();
        foreach($this->Customer->get_all()->result() as $customer)
        {       
            $customers[$customer->person_id] = $this->xss_clean($customer->first_name . ' ' . $customer->last_name);
        }
        $data['specific_input_data'] = $customers;

        $this->load->view('reports/specific_input', $data);
    }

    public function specific_customer($start_date, $end_date, $customer_id, $sale_type)
    {
        $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'customer_id' => $customer_id, 'sale_type' => $sale_type);
        
        $this->load->model('reports/Specific_customer');
        $model = $this->Specific_customer;

        $model->create($inputs);

        $headers = $this->xss_clean($model->getDataColumns());
        $report_data = $model->getData($inputs);

        $summary_data = array();
        $details_data = array();
        $details_data_rewards = array();

        foreach($report_data['summary'] as $key => $row)
        {
            $summary_data[] = $this->xss_clean(array(
                'id' => anchor('sales/receipt/'.$row['sale_id'], 'POS '.$row['sale_id'], array('target'=>'_blank')),
                'sale_date' => $row['sale_date'],
                'quantity' => to_quantity_decimals($row['items_purchased']),
                'employee_name' => $row['employee_name'],
                'subtotal' => to_currency($row['subtotal']),
                'tax' => to_currency($row['tax']),
                'total' => to_currency($row['total']),
                'cost' => to_currency($row['cost']),
                'profit' => to_currency($row['profit']),
                'payment_type' => $row['payment_type'],
                'comment' => $row['comment']));

            foreach($report_data['details'][$key] as $drow)
            {
                $details_data[$row['sale_id']][] = $this->xss_clean(array($drow['name'], $drow['category'], $drow['serialnumber'], $drow['description'], to_quantity_decimals($drow['quantity_purchased']), to_currency($drow['subtotal']), to_currency($drow['tax']), to_currency($drow['total']), to_currency($drow['cost']), to_currency($drow['profit']), $drow['discount_percent'].'%'));
            }
            if(isset($report_data['rewards'][$key])){
                foreach($report_data['rewards'][$key] as $drow)
                {
                    $details_data_rewards[$row['sale_id']][] = $this->xss_clean(array($drow['used'], $drow['earned']));
                }
            }
        }

        $customer_info = $this->Customer->get_info($customer_id);
        $data = array(
            'title' => $this->xss_clean($customer_info->first_name . ' ' . $customer_info->last_name . ' ' . $this->lang->line('reports_report')),
            'subtitle' => $this->_get_subtitle_report(array('start_date' => $start_date, 'end_date' => $end_date)),
            'headers' => $headers,
            'summary_data' => $summary_data,
            'details_data' => $details_data,
            'details_data_rewards' => $details_data_rewards,
            'overall_summary_data' => $this->xss_clean($model->getSummaryData($inputs))
        );

        $this->load->view('reports/tabular_details', $data);
    }

    public function specific_employee_input()
    {
        $data = array();
        $data['specific_input_name'] = $this->lang->line('reports_employee');

        $employees = array();
        foreach($this->Employee->get_all()->result() as $employee)
        {
            $employees[$employee->person_id] = $this->xss_clean($employee->first_name . ' ' . $employee->last_name);
        }
        $data['specific_input_data'] = $employees;

        $this->load->view('reports/specific_input', $data);
    }

    public function specific_employee($start_date, $end_date, $employee_id, $sale_type)
    {
        $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'employee_id' => $employee_id, 'sale_type' => $sale_type);

        $this->load->model('reports/Specific_employee');
        $model = $this->Specific_employee;

        $model->create($inputs);

        $headers = $this->xss_clean($model->getDataColumns());
        $report_data = $model->getData($inputs);

        $summary_data = array();
        $details_data = array();

        foreach($report_data['summary'] as $key => $row)
        {
            $summary_data[] = $this->xss_clean(array(
                'id' => anchor('sales/receipt/'.$row['sale_id'], 'POS '.$row['sale_id'], array('target'=>'_blank')),
                'sale_date' => $row['sale_date'],
                'quantity' => to_quantity_decimals($row['items_purchased']),
                'customer_name' => $row['customer_name'],
                'subtotal' => to_currency($row['subtotal']),
                'tax' => to_currency($row['tax']),
                'total' => to_currency($row['total']),
                'cost' => to_currency($row['cost']),
                'profit' => to_currency($row['profit']),
                'payment_type' => $row['payment_type'],
                'comment' => $row['comment']));

            foreach($report_data['details'][$key] as $drow)
            {
                $details_data[$row['sale_id']][] = $this->xss_clean(array($drow['name'], $drow['category'], $drow['serialnumber'], $drow['description'], to_quantity_decimals($drow['quantity_purchased']), to_currency($drow['subtotal']), to_currency($drow['tax']), to_currency($drow['total']), to_currency($drow['cost']), to_currency($drow['profit']), $drow['discount_percent'].'%'));
            }
        }

        $employee_info = $this->Employee->get_info($employee_id);
        $data = array(
            'title' => $this->xss_clean($employee_info->first_name . ' ' . $employee_info->last_name . ' ' . $this->lang->line('reports_report')),
            'subtitle' => $this->_get_subtitle_report(array('start_date' => $start_date, 'end_date' => $end_date)),
            'headers' => $headers,
            'summary_data' => $summary_data,
            'details_data' => $details_data,
            'overall_summary_data' => $this->xss_clean($model->getSummaryData($inputs))
        );

        $this->load->view('reports/tabular_details', $data);
    }

    public function specific_discount_input()
    {
        $data = array();
        $data['specific_input_name'] = $this->lang->line('reports_discount');

        $discounts = array();
        for ($i = 0; $i <= 100; $i += 10)
        {
            $discounts[$i] = $i . '%';
        }
        $data['specific_input_data'] = $discounts;
        
        $data = $this->xss_clean($data);

        $this->load->view('reports/specific_input', $data);
    }

    public function specific_discount($start_date, $end_date, $discount, $sale_type)
    {
        $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'discount' => $discount, 'sale_type' => $sale_type);
        
        $this->load->model('reports/Specific_discount');
        $model = $this->Specific_discount;

        $model->create($inputs);

        $headers = $this->xss_clean($model->getDataColumns());
        $report_data = $model->getData($inputs);

        $summary_data = array();
        $details_data = array();

        foreach($report_data['summary'] as $key => $row)
        {
            $summary_data[] = $this->xss_clean(array(
                'id' => anchor('sales/receipt/'.$row['sale_id'], 'POS '.$row['sale_id'], array('target'=>'_blank')),
                'sale_date' => $row['sale_date'],
                'quantity' => to_quantity_decimals($row['items_purchased']),
                'customer_name' => $row['customer_name'],
                'subtotal' => to_currency($row['subtotal']),
                'tax' => to_currency($row['tax']),
                'total' => to_currency($row['total']),
                'profit' => to_currency($row['profit']),
                'payment_type' => $row['payment_type'],
                'comment' => $row['comment']
            ));

            foreach($report_data['details'][$key] as $drow)
            {
                $details_data[$row['sale_id']][] = $this->xss_clean(array($drow['name'], $drow['category'], $drow['serialnumber'], $drow['description'], to_quantity_decimals($drow['quantity_purchased']), to_currency($drow['subtotal']), to_currency($drow['tax']), to_currency($drow['total']), to_currency($drow['profit']), $drow['discount_percent'].'%'));
            }
        }

        $data = array(
            'title' => $discount . '% ' . $this->lang->line('reports_discount') . ' ' . $this->lang->line('reports_report'),
            'subtitle' => $this->_get_subtitle_report(array('start_date' => $start_date, 'end_date' => $end_date)),
            'headers' => $headers,
            'summary_data' => $summary_data,
            'details_data' => $details_data,
            'overall_summary_data' => $this->xss_clean($model->getSummaryData($inputs))
        );

        $this->load->view('reports/tabular_details', $data);
    }

    public function get_detailed_sales_row($sale_id)
    {
        $inputs = array('sale_id' => $sale_id);
        
        $this->load->model('reports/Detailed_sales');
        $model = $this->Detailed_sales;

        $model->create($inputs);

        $report_data = $model->getDataBySaleId($sale_id);

        $summary_data = $this->xss_clean(array(
            'sale_id' => $report_data['sale_id'],
            'sale_date' => $report_data['sale_date'],
            'quantity' => to_quantity_decimals($report_data['items_purchased']),
            'employee_name' => $report_data['employee_name'],
            'customer_name' => $report_data['customer_name'],
            'subtotal' => to_currency($report_data['subtotal']),
            'tax' => to_currency($report_data['tax']),
            'total' => to_currency($report_data['total']),
            'cost' => to_currency($report_data['cost']),
            'profit' => to_currency($report_data['profit']),
            'payment_type' => $report_data['payment_type'],
            'comment' => $report_data['comment'],
            'edit' => anchor('sales/edit/'. $report_data['sale_id'], '<span class="glyphicon glyphicon-edit"></span>',
                array('class'=>'modal-dlg print_hide', 'data-btn-delete' => $this->lang->line('common_delete'), 'data-btn-submit' => $this->lang->line('common_submit'), 'title' => $this->lang->line('sales_update'))
            )
        ));

        echo json_encode(array($sale_id => $summary_data));
    }

    public function detailed_sales($start_date, $end_date, $sale_type, $location_id = 'all')
    {
        $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'sale_type' => $sale_type, 'location_id' => $location_id);
        
        $this->load->model('reports/Detailed_sales');
        $model = $this->Detailed_sales;

        $model->create($inputs);

        $headers = $this->xss_clean($model->getDataColumns());

        $report_data = $model->getData($inputs);

        $summary_data = array();
        $details_data = array();
        $details_data_rewards = array();

        $show_locations = $this->xss_clean($this->Stock_location->multiple_locations());

        foreach($report_data['summary'] as $key => $row)
        {
            $summary_data[] = $this->xss_clean(array(
                'id' => $row['sale_id'],
                'sale_date' => $row['sale_date'],
                'quantity' => to_quantity_decimals($row['items_purchased']),
                'employee_name' => $row['employee_name'],
                'customer_name' => $row['customer_name'],
                'subtotal' => to_currency($row['subtotal']),
                'tax' => to_currency($row['tax']),
                'total' => to_currency($row['total']),
                'cost' => to_currency($row['cost']),
                'profit' => to_currency($row['profit']),
                'payment_type' => $row['payment_type'],
                'comment' => $row['comment'],
                'edit' => anchor('sales/edit/'.$row['sale_id'], '<span class="glyphicon glyphicon-edit"></span>',
                    array('class' => 'modal-dlg print_hide', 'data-btn-delete' => $this->lang->line('common_delete'), 'data-btn-submit' => $this->lang->line('common_submit'), 'title' => $this->lang->line('sales_update'))
                )
            ));

            foreach($report_data['details'][$key] as $drow)
            {
                $quantity_purchased = to_quantity_decimals($drow['quantity_purchased']);
                if($show_locations)
                {
                    $quantity_purchased .= ' [' . $this->Stock_location->get_location_name($drow['item_location']) . ']';
                }
                $details_data[$row['sale_id']][] = $this->xss_clean(array($drow['name'], $drow['category'], $drow['serialnumber'], $drow['description'], $quantity_purchased, to_currency($drow['subtotal']), to_currency($drow['tax']), to_currency($drow['total']), to_currency($drow['cost']), to_currency($drow['profit']), $drow['discount_percent'].'%'));
            }
            if(isset($report_data['rewards'][$key])){
                foreach($report_data['rewards'][$key] as $drow)
                {
                    $details_data_rewards[$row['sale_id']][] = $this->xss_clean(array($drow['used'], $drow['earned']));
                }
            }
        }

        $data = array(
            'title' => $this->lang->line('reports_detailed_sales_report'),
            'subtitle' => $this->_get_subtitle_report(array('start_date' => $start_date, 'end_date' => $end_date)),
            'headers' => $headers,
            'editable' => 'sales',
            'summary_data' => $summary_data,
            'details_data' => $details_data,
            'details_data_rewards' => $details_data_rewards,
            'overall_summary_data' => $this->xss_clean($model->getSummaryData($inputs))
        );
        $this->load->view('reports/tabular_details', $data);
    }

    public function get_detailed_receivings_row($receiving_id)
    {
        $inputs = array('receiving_id' => $receiving_id);

        $this->load->model('reports/Detailed_receivings');
        $model = $this->Detailed_receivings;
        
        $model->create($inputs);

        $report_data = $model->getDataByReceivingId($receiving_id);

        $summary_data = $this->xss_clean(array(
            'receiving_id' => $report_data['receiving_id'],
            'receiving_date' => $report_data['receiving_date'],
            'quantity' => to_quantity_decimals($report_data['items_purchased']),
            'employee_name' => $report_data['employee_name'],
            'supplier_name' => $report_data['supplier_name'],
            'total' => to_currency($report_data['total']),
            'payment_type' => $report_data['payment_type'],
            'reference' => $report_data['reference'],
            'comment' => $report_data['comment'],
            'edit' => anchor('receivings/edit/'. $report_data['receiving_id'], '<span class="glyphicon glyphicon-edit"></span>',
                array('class'=>'modal-dlg print_hide', 'data-btn-submit' => $this->lang->line('common_submit'), 'data-btn-delete' => $this->lang->line('common_delete'), 'title' => $this->lang->line('receivings_update'))
            )
        ));

        echo json_encode(array($receiving_id => $summary_data));
    }

    public function detailed_receivings($start_date, $end_date, $receiving_type, $location_id = 'all')
    {
        $inputs = array('start_date' => $start_date, 'end_date' => $end_date, 'receiving_type' => $receiving_type, 'location_id' => $location_id);
        
        $this->load->model('reports/Detailed_receivings');
        $model = $this->Detailed_receivings;
        
        $model->create($inputs);

        $headers = $this->xss_clean($model->getDataColumns());
        $report_data = $model->getData($inputs);

        $summary_data = array();
        $details_data = array();

        $show_locations = $this->xss_clean($this->Stock_location->multiple_locations());

        foreach($report_data['summary'] as $key => $row)
        {
            $summary_data[] = $this->xss_clean(array(
                'id' => $row['receiving_id'],
                'receiving_date' => $row['receiving_date'],
                'quantity' => to_quantity_decimals($row['items_purchased']),
                'employee_name' => $row['employee_name'],
                'supplier_name' => $row['supplier_name'],
                'total' => to_currency($row['total']),
                'profit' => to_currency($row['profit']),
                'payment_type' => $row['payment_type'],
                'reference' => $row['reference'],
                'comment' => $row['comment'],
                'edit' => anchor('receivings/edit/' . $row['receiving_id'], '<span class="glyphicon glyphicon-edit"></span>',
                    array('class' => 'modal-dlg print_hide', 'data-btn-delete' => $this->lang->line('common_delete'), 'data-btn-submit' => $this->lang->line('common_submit'), 'title' => $this->lang->line('receivings_update'))
                )
            ));

            foreach($report_data['details'][$key] as $drow)
            {
                $quantity_purchased = $drow['receiving_quantity'] > 1 ? to_quantity_decimals($drow['quantity_purchased']) . ' x ' . to_quantity_decimals($drow['receiving_quantity']) : to_quantity_decimals($drow['quantity_purchased']);
                if ($show_locations)
                {
                    $quantity_purchased .= ' [' . $this->Stock_location->get_location_name($drow['item_location']) . ']';
                }
                $details_data[$row['receiving_id']][] = $this->xss_clean(array($drow['item_number'], $drow['name'], $drow['category'], $quantity_purchased, to_currency($drow['total']), $drow['discount_percent'].'%'));
            }
        }

        $data = array(
            'title' => $this->lang->line('reports_detailed_receivings_report'),
            'subtitle' => $this->_get_subtitle_report(array('start_date' => $start_date, 'end_date' => $end_date)),
            'headers' => $headers,
            'editable' => 'receivings',
            'summary_data' => $summary_data,
            'details_data' => $details_data,
            'overall_summary_data' => $this->xss_clean($model->getSummaryData($inputs))
        );

        $this->load->view('reports/tabular_details', $data);
    }

    public function inventory_low()
    {
        $inputs = array();

        $this->load->model('reports/Inventory_low');
        $model = $this->Inventory_low;

        $report_data = $model->getData($inputs);
        
        $tabular_data = array();
        foreach($report_data as $row)
        {
            $tabular_data[] = $this->xss_clean(array(
                'item_name' => $row['name'],
                'item_number' => $row['item_number'],
                'quantity' => to_quantity_decimals($row['quantity']),
                'reorder_level' => to_quantity_decimals($row['reorder_level']),
                'location_name' => $row['location_name']
            ));
        }

        $data = array(
            'title' => $this->lang->line('reports_inventory_low_report'),
            'subtitle' => '',
            'headers' => $this->xss_clean($model->getDataColumns()),
            'data' => $tabular_data,
            'summary_data' => $this->xss_clean($model->getSummaryData($inputs))
        );

        $this->load->view('reports/tabular', $data);
    }

    public function inventory_summary_input()
    {
        $this->load->model('reports/Inventory_summary');
        $model = $this->Inventory_summary;
        
        $data = array();
        $data['item_count'] = $model->getItemCountDropdownArray();

        $stock_locations = $this->xss_clean($this->Stock_location->get_allowed_locations());
        $stock_locations['all'] = $this->lang->line('reports_all');
        $data['stock_locations'] = array_reverse($stock_locations, TRUE);

        $this->load->view('reports/inventory_summary_input', $data);
    }

    public function inventory_summary($location_id = 'all', $item_count = 'all')
    {
        $inputs = array('location_id' => $location_id, 'item_count' => $item_count);
        
        $this->load->model('reports/Inventory_summary');
        $model = $this->Inventory_summary;

        $report_data = $model->getData($inputs);

        $tabular_data = array();
        foreach($report_data as $row)
        {
            $tabular_data[] = $this->xss_clean(array(
                'item_name' => $row['name'],
                'item_number' => $row['item_number'],
                'quantity' => to_quantity_decimals($row['quantity']),
                'reorder_level' => to_quantity_decimals($row['reorder_level']),
                'location_name' => $row['location_name'],
                'cost_price' => to_currency($row['cost_price']),
                'unit_price' => to_currency($row['unit_price']),
                'subtotal' => to_currency($row['sub_total_value'])
            ));
        }

        $data = array(
            'title' => $this->lang->line('reports_inventory_summary_report'),
            'subtitle' => '',
            'headers' => $this->xss_clean($model->getDataColumns()),
            'data' => $tabular_data,
            'summary_data' => $this->xss_clean($model->getSummaryData($report_data))
        );

        $this->load->view('reports/tabular', $data);
    }
    //  Returns subtitle for the reports
    private function _get_subtitle_report($inputs)
    {
        $subtitle = '';

        if(empty($this->config->item('date_or_time_format')))
        {
            $subtitle .= date($this->config->item('dateformat'), strtotime($inputs['start_date'])) . ' - ' .date($this->config->item('dateformat'), strtotime($inputs['end_date']));
        }
        else
        {
            $subtitle .= date($this->config->item('dateformat').' '.$this->config->item('timeformat'), strtotime(rawurldecode($inputs['start_date']))) . ' - ' . date($this->config->item('dateformat').' '.$this->config->item('timeformat'), strtotime(rawurldecode($inputs['end_date'])));
        }
        
        return $subtitle;
    }
}
?>
API documentation generated by ApiGen