<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Costpriceupdate_model extends CI_Model {

    public function __construct() {
        parent::__construct();
    }
// UPDATE LEASE,AVERAGE,MAXIMUM AND LAST PRICES AS STANDARD PRICE   HUSSAIN 04/03/2012
    function updatecostprice($datefrom, $dateto, $type) {

        $this->db->select("PRODUCTS.ID");
        $this->db->from('PRODUCTS');

        $rows = $this->db->get()->result_array();

        
        foreach ($rows as $row) {
// AVERAGE PRICE
            if ($type == '1') {
                $this->db->select("SUM(STOCKDIARY.PRICE)/COUNT(*) AS NEWPRICE");
                $this->db->from('STOCKDIARY');
                if ($datefrom) {
                    $this->db->where('STOCKDIARY.DATENEW >=', $datefrom);
                }
                if ($dateto) {
                    $this->db->where('STOCKDIARY.DATENEW <=', $dateto.'+1 day');
                }
                $where = 'STOCKDIARY.PRODUCT = "' . $row['ID'] . '" AND STOCKDIARY.REASON = 1';
                $this->db->where($where);
                $result = $this->db->get()->result();
            }
 // LEAST PRICE
            else if ($type == '0') {
                $this->db->select("MIN(STOCKDIARY.PRICE) AS NEWPRICE");
                $this->db->from('STOCKDIARY');
                if ($datefrom) {
                    $this->db->where('STOCKDIARY.DATENEW >=', $datefrom);
                }
                if ($dateto) {
                    $this->db->where('STOCKDIARY.DATENEW <=', $dateto);
                }
                $where = 'STOCKDIARY.PRODUCT = "' . $row['ID'] . '" AND STOCKDIARY.REASON = 1';
                $this->db->where($where);
                $result = $this->db->get()->result();
            }
 // MAXIMUM PRICE
            else if ($type == '2') {
                $this->db->select("MAX(STOCKDIARY.PRICE) AS NEWPRICE");
                $this->db->from('STOCKDIARY');
                if ($datefrom) {
                    $this->db->where('STOCKDIARY.DATENEW >=', $datefrom);
                }
                if ($dateto) {
                    $this->db->where('STOCKDIARY.DATENEW <=', $dateto.'+1 day');
                }
                $where = 'STOCKDIARY.PRODUCT = "' . $row['ID'] . '" AND STOCKDIARY.REASON = 1';
                $this->db->where($where);
                $result = $this->db->get()->result();
            }
 // LAST PRICE
            else if ($type == '3') {
                $this->db->select("STOCKDIARY.PRICE AS NEWPRICE");
                $this->db->from('STOCKDIARY');
           
//                 $this->db->where('LAST(STOCKDIARY.DATENEW)');

                $where = 'STOCKDIARY.PRODUCT = "' . $row['ID'] . '" AND STOCKDIARY.REASON = 1';
                $this->db->where($where);
                   $this->db->order_by("STOCKDIARY.DATENEW", "desc");
                   $this->db->limit(1);
                $result = $this->db->get()->result();
            }
// UPDATE PRICEBUY INTO PRODUCTS TABLE
            if ($result) {
                $this->db->set('PRICEBUY', (int) ($result[0]->NEWPRICE));
                $this->db->where('PRODUCTS.ID', $row['ID']);
                $this->db->update('PRODUCTS');
            }



            
        }
        return json_encode((int) ($result[0]->NEWPRICE));
    }

}