Location: PHPKode > projects > IRMA - Asset Inventory and Job Tracking > irma-1.0.6/includes/purchase-orders/classes.php
<?php

class PurchaseOrder {

    var $fields = array();
    var $updates = array();
    
    function getfromDB($id) {

        // Make new database object and fill variables
        $db = new DB;
        $query = <<<EOF
SELECT * 
FROM purchase_order 
WHERE (purchase_order_id = {$id})
EOF;

        $result = $db->query($query);

        if (! $result) {
            return false;
        }

        $data = mysql_fetch_array($result);

        for($i=0; $i < count($data); $i++) {
            list($key, $val) = each($data);

            if ($key == 'comments') {
                $this->$key = stripslashes($val);
            } else {
                $this->$key = $val;
            }
        }

        return true;
    }

    function updateInDB($field_names)  {

        $db = new DB;

        // Which fields need quotes
        $nq = array();
        $nq['purchase_order_id'] = 0;
        $nq['vendor_id'] = 0;
        $nq['order_number'] = 1;
        $nq['sent'] = 1;
        $nq['received'] = 1;
        $nq['modified'] = 1;
        $nq['comments'] = 1;

        foreach ($field_names as $name) {

            $value = $this->$name;

            if ($nq[$name] == 1) {
                $value = "'{$value}'";
            }

            $query = <<<EOF
UPDATE 
    purchase_order
SET 
    {$name} = {$value}
WHERE
    purchase_order_id = {$this->purchase_order_id}
EOF;
            $result = $db->query($query);
        }
        
    }
    
    function addToDB() {
        
        $db = new DB;

        $this->comments = addslashes($this->comments);

        $this->modified = date("Y-m-d H:i:s");
        $this->is_active = 1;
        
        $query = <<<EOF
INSERT INTO 
    purchase_order (vendor_id, order_number, sent,
                    received, modified, comments)
    VALUES ({$this->vendor_id}, 
            '{$this->order_number}',
            '{$this->sent}', 
            '{$this->received}', 
            '{$this->modified}',
            '{$this->comments}')
EOF;

        if ($result=$db->query($query)) {
            return true;
        } else {
            return false;
        }
    }

    function deleteFromDB($id) {

        $db = new DB;

        $q = "DELETE from purchase_order WHERE (purchase_order_id = {$id})";

        $rs = $db->query($q);

        if ($rs == null) {
            return false;
        }

        // Get all line item IDs, then delete all line items
        // and item_links .. MySQL 3.x doesn't support delete
        // using join .. yuck.

        $q2 = "SELECT line_item_id FROM line_item " .
              "WHERE (purchase_order_id = {$id})";
        $rs = $db->query($q2);

        // No line items
        if ($rs == null) {
            return true;
        }

        $count = $db->numrows($rs);

        if ($count > 0) {
            for ($i = 0; $i < $count; $i++) {
                $id = $db->result($rs, $i, 'line_item_id');

                // Delete line items
                $dli = "DELETE FROM line_item " .
                       "where (line_item_id = {$id})";
                $lrs = $db->query($dli);

                // Delete item links
                $dil = "DELETE FROM item_link " .
                       "where (line_item_id = {$id})";
                $irs = $db->query($dil);
            }
        }

       return true;
    }

}

class LineItem {

    var $fields = array();
    var $updates = array();
    
    function getfromDB($id) {

        // Make new database object and fill variables
        $db = new DB;
        $query = <<<EOF
SELECT 
    line_item_id, purchase_order_id, description, created,
    product_number, quantity, quantity_received, type
FROM 
    line_item
WHERE 
    (line_item_id = {$id})
EOF;

        $result = $db->query($query);

        if (! $result) {
            return false;
        }

        $data = mysql_fetch_array($result);

        for($i=0; $i < count($data); $i++) {
            list($key, $val) = each($data);

            if ($key == 'description') {
                $this->$key = stripslashes($val);
            } else {
                $this->$key = $val;
            }
        }

        return true;
    }

    function updateInDB($field_names)  {

        $db = new DB;

        // Which fields need quotes
        $nq = array();
        $nq['line_item_id'] = 0;
        $nq['product_number'] = 1;
        $nq['description'] = 1;
        $nq['purchase_order_id'] = 0;
        $nq['type'] = 1;
        $nq['quantity'] = 1;
        $nq['quantity_received'] = 1;

        foreach ($field_names as $name) {

            $value = $this->$name;

            if ($nq[$name] == 1) {
                $value = "'{$value}'";
            }

            $query = <<<EOF
UPDATE 
    line_item
SET 
    {$name} = {$value}
WHERE
    line_item_id = {$this->line_item_id}
EOF;
            $result = $db->query($query);
        }
        
    }
    
    function addToDB() {
        
        $db = new DB;

        $this->description = addslashes($this->description);
        $this->created = date("Y-m-d H:i:s");
        
        $query = <<<EOF
INSERT INTO 
    line_item (purchase_order_id,
               product_number,
               description,
               type,
               quantity,
               quantity_received,
               created)
    VALUES ({$this->purchase_order_id},
            '{$this->product_number}', 
            '{$this->description}', 
            '{$this->type}', 
            {$this->quantity}, 
            {$this->quantity_received},
            '{$this->created}')
EOF;

        if ($result=$db->query($query)) {
            return true;
        } else {
            return false;
        }
    }

    function deleteFromDB($id) {

        $db = new DB;

        $q = "DELETE from line_item WHERE (line_item_id = {$id})";

        if ($result = $db->query($q)) {
            $q2 = "DELETE from item_link WHERE (line_item_id = {$id})";
            $r2 = $db->query($q2);
            return true;
        } else {
            return false;
        }
    }

}

?>
Return current item: IRMA - Asset Inventory and Job Tracking