<?php
/**
* 优惠券后台处理操作
* @author moufer<moufer@163.com>
* @copyright www.modoer.com
* @version $Id coupon.inc.php 2 2008-08-14 07:18 $
*/
(!defined('IN_ADMIN') || !defined('IN_MUDDER')) && exit('Access Denied');

if(!$dosubmit) {

    if(empty($couponid)||$couponid<0) cpmsg('无效参数。');
    $detail = $db->get_one("SELECT * FROM {$dbpre}coupons WHERE couponid='$couponid'");
    empty($detail) && cpmsg('不存在。');
    extract($detail);
    $shop = $db->get_one("SELECT * FROM {$dbpre}shops WHERE sid='$shopid'");

    include cptpl('edit', MOD_ROOT.'./admin/templates/');

} else {

    $detail = $db->get_one("SELECT * FROM {$dbpre}coupons WHERE couponid='$couponid'");
    empty($detail) && cpmsg('优惠券不存在。');
    $shop = $db->get_one("SELECT * FROM {$dbpre}shops WHERE sid='$shopid'");
    empty($detail) && cpmsg('优惠商铺不存在。');

    if(!empty($_FILES['picture'])) {
        if(!class_exists('Upload_Image_calss')) require_once MUDDER_ROOT.'./include/upload.class.php';
        $uploadimg = new Upload_Image_calss($_FILES['picture']);
        if($uploadimg->error) cpmsg($uploadimg->error.'请返回。');

        $uploadimg->folder = 'coupon';
        $uploadimg->userWatermark = $_coupon['watermark'];
        $uploadimg->max_size = $_config['picture_upload_size'];
        $uploadimg->thumb = array ( 
            'thumb' => array ( 
                'prefix' => 'thumb_',  
                'width' => empty($_coupon['thumb_width'])?'72':$_coupon['thumb_width'],
                'height' => empty($_coupon['thumb_height'])?'47':$_coupon['thumb_height'],
            ),
        );
        $uploadimg->upload();

        if($uploadimg->error) cpmsg($uploadimg->error.'请返回。');
        $coupon['thumb'] = $uploadimg->upsubdir.'/'.$uploadimg->thumb_filenames['thumb']['filename'];
        $coupon['picture'] = $uploadimg->upsubdir.'/'.$uploadimg->filename;

        // 图片上传完毕的标记
        $picture = true;
    }

    $coupon['shopid'] = $shopid;
    $coupon['starttime'] = strtotime($coupon['starttime']);
    $coupon['endtime'] = strtotime($coupon['endtime']);
    $coupon['des'] = nl_textarea($coupon['des']);

    // 判断是否修改了商铺
    $shopchange = $shopid != $detail['shopid'];
    // 判断是否已经通过审核
    $checked = $detail['flag'] != 2;
    $newflag = intval($coupon['flag']);
    // 修改了商铺同时已经通过审核的
    if($shopchange && $checked) {
        // 更新原商铺优惠券数量
        $db->update("UPDATE {$dbpre}shops SET coupons=coupons-1 WHERE sid='{$detail['shopid']}'");
        // 删除用户行为
        $db->update("DELETE FROM {$dbpre}membereffect WHERE id='$couponid' AND idtype='coupon'");
    }

    //删除原来的图片
    if($picture) {
        @unlink(MUDDER_ROOT.'./uploads'.$detail['thumb']);
        @unlink(MUDDER_ROOT.'./uploads'.$detail['picture']);
    }

    $db->update_new($dbpre.'coupons', "couponid='$couponid'", $coupon);
    // 更新数量
    $newtotal = $db->get_value("SELECT COUNT(*) FROM {$dbpre}coupons WHERE shopid='$shopid' AND flag=1 AND endtime>UNIX_TIMESTAMP()");
    $db->update("UPDATE {$dbpre}shops SET coupons='$newtotal' WHERE sid='$shopid'");
    cpmsg('优惠券更新完毕！', 'admincp.php?action='.$action.'&file=list');

}
?>
