<?php
/**
 * Copyright (C) LJCMS All rights reserved.
 * <E-Mail：aspcms@qq.com>
 * Url http://www.liangjing.org
 *     http://www.8CMS.com
 * Update 2016.03.19
 */

class purchaseSModel extends X {

        //添加采购订单
        public function doAdd($array) {
            unset($array['opmode']);
            $array['create_user']= $_SESSION['sale_id'];
            $array['create_user_name']= $_SESSION['sale_name'];
            $array['add_time']= time();
            return parent::$obj->insert(DB_PREFIXSA.'purchase', $array); 
        }

        //获取当前所有订单数量
        public function popup(){
            $id = parent::$obj->fetch_newid('SELECT MAX(id) FROM '.DB_PREFIXSA.'purchase', 1);
            return $id;
        }

        //获取未审核采购单
        public function notReview($items)
        {
            $where  = " WHERE review=0 ";
            $start      = ($items['page']-1)*$items['pagesize'];
            $countsql   = "SELECT COUNT(*) AS my_count FROM ".DB_PREFIXSA."purchase ".$where;
            $total      = parent::$obj->fetch_count($countsql);
            $sql        = "SELECT sale_purchase.*, 8cms_product.productname FROM ".DB_PREFIXSA."purchase, 8cms_product ".$where." AND sale_purchase.product_id = 8cms_product.productid  ORDER BY id asc LIMIT ".$start.", ".$items['pagesize']."";

            $array = parent::$obj->getall($sql);
            return array($total, $array);
        }

        //获取审核通过未入库采购单
        public function storage($items)
        {
            $where  = " WHERE review=1 AND status=0 ";
            $start      = ($items['page']-1)*$items['pagesize'];
            $countsql   = "SELECT COUNT(*) AS my_count FROM ".DB_PREFIXSA."purchase ".$where;
            $total      = parent::$obj->fetch_count($countsql);
            $sql        = "SELECT sale_purchase.*, 8cms_product.productname FROM ".DB_PREFIXSA."purchase, 8cms_product ".$where." AND sale_purchase.product_id = 8cms_product.productid  ORDER BY id asc LIMIT ".$start.", ".$items['pagesize']."";

            $array = parent::$obj->getall($sql);
            return array($total, $array);

        }

        //获取审核通过已入库采购单
        public function endPurchase($items)
        {
            $where  = " WHERE review=1 AND status=1 ";
            $start      = ($items['page']-1)*$items['pagesize'];
            $countsql   = "SELECT COUNT(*) AS my_count FROM ".DB_PREFIXSA."purchase ".$where;
            $total      = parent::$obj->fetch_count($countsql);
            $sql        = "SELECT sale_purchase.*, 8cms_product.productname FROM ".DB_PREFIXSA."purchase, 8cms_product ".$where." AND sale_purchase.product_id = 8cms_product.productid  ORDER BY id asc LIMIT ".$start.", ".$items['pagesize']."";

            $array = parent::$obj->getall($sql);
            return array($total, $array);
        }

        //根据id查询订单
        public function get_data($id){
            $sql = "SELECT  sale_purchase.*, 8cms_product.productname FROM ".DB_PREFIXSA."purchase, 8cms_product  WHERE id='".$id."' AND sale_purchase.product_id = 8cms_product.productid";
            return parent::$obj->fetch_first($sql);
        }

        //修改订单审核状态
        public function doStatus($id)
        {
            $arr['review'] = 1;
            $result = parent::$obj->update(DB_PREFIXSA.'purchase', $arr, 'id='.$id.'');
            return $result;
        }
        
        //驳回订单
        public function noStatus($id)
        {
            $arr['review'] = -1;
            $result = parent::$obj->update(DB_PREFIXSA.'purchase', $arr, 'id='.$id.'');
            return $result;
        }


        //修改订单收货状态
        public function doStorage($array)
        {
            $arr['status'] = 1;
            $arr['receiptTime'] = time();
            $result = parent::$obj->update(DB_PREFIXSA.'purchase', $arr, 'id='.$array['id'].'');

            $result1 = parent::$obj->update(DB_PREFIX.'product', array('stock'=>'[[stock+'.$array['num'].']]'), 'productname="'.$array['productname'].'"');

            if ($result === $result1) {
                return $result;
            } else {
                return 0;
            }

        }

        public function del($id){
        $sql = "DELETE FROM ".DB_PREFIXSA."purchase WHERE id='".intval($id)."'";
        return parent::$obj->query($sql);
      }
      
        
}
?>
