Overview

Namespaces

  • emberlabs
    • Barcode
  • None

Classes

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

Functions

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

/**
 * Receiving class
 *
 * @link    github.com/jekkos/opensourcepos
 * @since   1.0
 * @author  N/A
 */

class Receiving extends CI_Model
{
    public function get_info($receiving_id)
    {
        $this->db->from('receivings');
        $this->db->join('people', 'people.person_id = receivings.supplier_id', 'LEFT');
        $this->db->join('suppliers', 'suppliers.person_id = receivings.supplier_id', 'LEFT');
        $this->db->where('receiving_id', $receiving_id);

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

    public function get_receiving_by_reference($reference)
    {
        $this->db->from('receivings');
        $this->db->where('reference', $reference);

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

    public function is_valid_receipt($receipt_receiving_id)
    {
        if(!empty($receipt_receiving_id))
        {
            //RECV #
            $pieces = explode(' ', $receipt_receiving_id);

            if(count($pieces) == 2 && preg_match('/(RECV|KIT)/', $pieces[0]))
            {
                return $this->exists($pieces[1]);
            }
            else
            {
                return $this->get_receiving_by_reference($receipt_receiving_id)->num_rows() > 0;
            }
        }

        return FALSE;
    }

    public function exists($receiving_id)
    {
        $this->db->from('receivings');
        $this->db->where('receiving_id', $receiving_id);

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

    public function update($receiving_data, $receiving_id)
    {
        $this->db->where('receiving_id', $receiving_id);

        return $this->db->update('receivings', $receiving_data);
    }

    public function save($items, $supplier_id, $employee_id, $comment, $reference, $payment_type, $receiving_id = FALSE)
    {
        if(count($items) == 0)
        {
            return -1;
        }

        $receivings_data = array(
            'receiving_time' => date('Y-m-d H:i:s'),
            'supplier_id' => $this->Supplier->exists($supplier_id) ? $supplier_id : NULL,
            'employee_id' => $employee_id,
            'payment_type' => $payment_type,
            'comment' => $comment,
            'reference' => $reference
        );

        //Run these queries as a transaction, we want to make sure we do all or nothing
        $this->db->trans_start();

        $this->db->insert('receivings', $receivings_data);
        $receiving_id = $this->db->insert_id();

        foreach($items as $line=>$item)
        {
            $cur_item_info = $this->Item->get_info($item['item_id']);

            $receivings_items_data = array(
                'receiving_id' => $receiving_id,
                'item_id' => $item['item_id'],
                'line' => $item['line'],
                'description' => $item['description'],
                'serialnumber' => $item['serialnumber'],
                'quantity_purchased' => $item['quantity'],
                'receiving_quantity' => $item['receiving_quantity'],
                'discount_percent' => $item['discount'],
                'item_cost_price' => $cur_item_info->cost_price,
                'item_unit_price' => $item['price'],
                'item_location' => $item['item_location']
            );

            $this->db->insert('receivings_items', $receivings_items_data);

            $items_received = $item['receiving_quantity'] != 0 ? $item['quantity'] * $item['receiving_quantity'] : $item['quantity'];

            // update cost price, if changed AND is set in config as wanted
            if($cur_item_info->cost_price != $item['price'] && $this->config->item('receiving_calculate_average_price') != FALSE)
            {
                $this->Item->change_cost_price($item['item_id'], $items_received, $item['price'], $cur_item_info->cost_price);
            }

            //Update stock quantity
            $item_quantity = $this->Item_quantity->get_item_quantity($item['item_id'], $item['item_location']);
            $this->Item_quantity->save(array('quantity' => $item_quantity->quantity + $items_received, 'item_id' => $item['item_id'],
                                              'location_id' => $item['item_location']), $item['item_id'], $item['item_location']);

            $recv_remarks = 'RECV ' . $receiving_id;
            $inv_data = array(
                'trans_date' => date('Y-m-d H:i:s'),
                'trans_items' => $item['item_id'],
                'trans_user' => $employee_id,
                'trans_location' => $item['item_location'],
                'trans_comment' => $recv_remarks,
                'trans_inventory' => $items_received
            );

            $this->Inventory->insert($inv_data);

            $supplier = $this->Supplier->get_info($supplier_id);
        }

        $this->db->trans_complete();

        if($this->db->trans_status() === FALSE)
        {
            return -1;
        }

        return $receiving_id;
    }

    public function delete_list($receiving_ids, $employee_id, $update_inventory = TRUE)
    {
        $success = TRUE;

        // start a transaction to assure data integrity
        $this->db->trans_start();

        foreach($receiving_ids as $receiving_id)
        {
            $success &= $this->delete($receiving_id, $employee_id, $update_inventory);
        }

        // execute transaction
        $this->db->trans_complete();

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

        return $success;
    }

    public function delete($receiving_id, $employee_id, $update_inventory = TRUE)
    {
        // start a transaction to assure data integrity
        $this->db->trans_start();

        if($update_inventory)
        {
            // defect, not all item deletions will be undone??
            // get array with all the items involved in the sale to update the inventory tracking
            $items = $this->get_receiving_items($receiving_id)->result_array();
            foreach($items as $item)
            {
                // create query to update inventory tracking
                $inv_data = array(
                    'trans_date' => date('Y-m-d H:i:s'),
                    'trans_items' => $item['item_id'],
                    'trans_user' => $employee_id,
                    'trans_comment' => 'Deleting receiving ' . $receiving_id,
                    'trans_location' => $item['item_location'],
                    'trans_inventory' => $item['quantity_purchased'] * -1
                );
                // update inventory
                $this->Inventory->insert($inv_data);

                // update quantities
                $this->Item_quantity->change_quantity($item['item_id'], $item['item_location'], $item['quantity_purchased'] * -1);
            }
        }

        // delete all items
        $this->db->delete('receivings_items', array('receiving_id' => $receiving_id));
        // delete sale itself
        $this->db->delete('receivings', array('receiving_id' => $receiving_id));

        // execute transaction
        $this->db->trans_complete();
    
        return $this->db->trans_status();
    }

    public function get_receiving_items($receiving_id)
    {
        $this->db->from('receivings_items');
        $this->db->where('receiving_id', $receiving_id);

        return $this->db->get();
    }
    
    public function get_supplier($receiving_id)
    {
        $this->db->from('receivings');
        $this->db->where('receiving_id', $receiving_id);

        return $this->Supplier->get_info($this->db->get()->row()->supplier_id);
    }

    public function get_payment_options()
    {
        return array(
            $this->lang->line('sales_cash') => $this->lang->line('sales_cash'),
            $this->lang->line('sales_check') => $this->lang->line('sales_check'),
            $this->lang->line('sales_debit') => $this->lang->line('sales_debit'),
            $this->lang->line('sales_credit') => $this->lang->line('sales_credit')
        );
    }

    /*
    We create a temp table that allows us to do easy report/receiving queries
    */
    public function create_temp_table(array $inputs)
    {
        if(empty($inputs['receiving_id']))
        {
            if(empty($this->config->item('date_or_time_format')))
            {
                $where = 'WHERE DATE(receiving_time) BETWEEN ' . $this->db->escape($inputs['start_date']) . ' AND ' . $this->db->escape($inputs['end_date']);
            }
            else
            {
                $where = 'WHERE receiving_time BETWEEN ' . $this->db->escape(rawurldecode($inputs['start_date'])) . ' AND ' . $this->db->escape(rawurldecode($inputs['end_date']));
            }
        }
        else
        {
            $where = 'WHERE receivings_items.receiving_id = ' . $this->db->escape($inputs['receiving_id']);
        }

        $this->db->query('CREATE TEMPORARY TABLE IF NOT EXISTS ' . $this->db->dbprefix('receivings_items_temp') .
            ' (INDEX(receiving_date), INDEX(receiving_time), INDEX(receiving_id))
            (
                SELECT 
                    MAX(DATE(receiving_time)) AS receiving_date,
                    MAX(receiving_time) AS receiving_time,
                    receivings_items.receiving_id,
                    MAX(comment) AS comment,
                    MAX(item_location) AS item_location,
                    MAX(reference) AS reference,
                    MAX(payment_type) AS payment_type,
                    MAX(employee_id) AS employee_id, 
                    items.item_id,
                    MAX(receivings.supplier_id) AS supplier_id,
                    MAX(quantity_purchased) AS quantity_purchased,
                    MAX(receivings_items.receiving_quantity) AS receiving_quantity,
                    MAX(item_cost_price) AS item_cost_price,
                    MAX(item_unit_price) AS item_unit_price,
                    MAX(discount_percent) AS discount_percent,
                    receivings_items.line,
                    MAX(serialnumber) AS serialnumber,
                    MAX(receivings_items.description) AS description,
                    MAX(item_unit_price * quantity_purchased * receivings_items.receiving_quantity - item_unit_price * quantity_purchased * receivings_items.receiving_quantity * discount_percent / 100) AS subtotal,
                    MAX(item_unit_price * quantity_purchased * receivings_items.receiving_quantity - item_unit_price * quantity_purchased * receivings_items.receiving_quantity * discount_percent / 100) AS total,
                    MAX((item_unit_price * quantity_purchased * receivings_items.receiving_quantity - item_unit_price * quantity_purchased * receivings_items.receiving_quantity * discount_percent / 100) - (item_cost_price * quantity_purchased)) AS profit,
                    MAX(item_cost_price * quantity_purchased * receivings_items.receiving_quantity ) AS cost
                FROM ' . $this->db->dbprefix('receivings_items') . ' AS receivings_items
                INNER JOIN ' . $this->db->dbprefix('receivings') . ' AS receivings
                    ON receivings_items.receiving_id = receivings.receiving_id
                INNER JOIN ' . $this->db->dbprefix('items') . ' AS items
                    ON receivings_items.item_id = items.item_id
                ' . "
                $where
                " . '
                GROUP BY receivings_items.receiving_id, items.item_id, receivings_items.line
            )'
        );
    }
}
?>
API documentation generated by ApiGen