Bill.php 12.3 KB
Newer Older
Piyawat jangin's avatar
Piyawat jangin committed
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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
<?php

/**
 * This is the model class for table "bill".
 *
 * The followings are the available columns in table 'bill':
 * @property integer $id
 * @property string $code
 * @property integer $mid
 * @property double $lati
 * @property double $longi
 * @property string $pay_date
 * @property string $send_date
 * @property string $status
 * @property integer $idpay
 * @property string $detail
 * @property string $created
 * @property integer $statusAll
 */
class Bill extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'bill';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
//			array('mid, created', 'required'),
//			array('mid, idpay, statusAll', 'numerical', 'integerOnly'=>true),
//			array('lati, longi', 'numerical'),
//			array('code', 'length', 'max'=>50),
//			array('status', 'length', 'max'=>4),
//			array('pay_date, send_date, detail', 'safe'),
//			// The following rule is used by search().
//			// @todo Please remove those attributes that should not be searched.
//			array('id, code, mid, lati, longi, pay_date, send_date, status, idpay, detail, created, statusAll', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'member' => array(self::BELONGS_TO, 'member', 'mid'),
            'bankfood' => array(self::BELONGS_TO, 'bankfood', 'detail'),
            'orders' => array(self::BELONGS_TO, 'orders', 'id'),
             'payment' => array(self::BELONGS_TO, 'payment', 'idpay')
        );
    }

    /**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'code' => 'Code',
			'mid' => 'Mid',
			'lati' => 'Lati',
			'longi' => 'Longi',
			'pay_date' => 'Pay Date',
			'send_date' => 'Send Date',
			'status' => 'Status',
			'idpay' => 'Idpay',
			'detail' => 'Detail',
			'created' => 'Created',
			'statusAll' => 'Status All',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('code',$this->code,true);
		$criteria->compare('mid',$this->mid);
		$criteria->compare('lati',$this->lati);
		$criteria->compare('longi',$this->longi);
		$criteria->compare('pay_date',$this->pay_date,true);
		$criteria->compare('send_date',$this->send_date,true);
		$criteria->compare('status',$this->status,true);
		$criteria->compare('idpay',$this->idpay);
		$criteria->compare('detail',$this->detail,true);
		$criteria->compare('created',$this->created,true);
		$criteria->compare('statusAll',$this->statusAll);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
        public function countOrders($bill) {
        $sum=  Yii::app()->db->createCommand()
                ->select('count(orders.bill_id) as num')
                ->from('orders')
                ->where('bill_id='.$bill)
                //->join('typefood','food.type_id=typefood.id')
                //->join('orders','orders.food_id=food.id')
                  ->group('orders.bill_id')      
                //->where('statusAll=3')
                ->queryRow();
        return $sum['num'];
    }


        public function sumOrdersPrice($bill) {
        $sum=  Yii::app()->db->createCommand()
                ->select('SUM( food.price * orders.qty ) AS my_sum')
                ->from('orders')
                ->join('bill','orders.bill_id=bill.id')
                ->join('food','orders.food_id=food.id')
                        
                ->where(' bill_id='.$bill)
                ->queryRow();
        return $sum['my_sum'];
    }


        static function getStatus($data) {
        if ($data == 'wait') {
            return 'รอ';
        } else if ($data == 'pay') {
            return 'จ่าย';
        } else {
            return 'ส่ง';
        }
    }
    
      static function getStatusPay($data) {
        if ($data != 'pay') {
            return "รอชำระเงิน";//CHtml::link("ยังไม่จ่ายเงิน",array("Site/payForm","id"=>$data->id,"idbank"=>$data->detail));
       
        } else {
            return 'ชำระเงินแล้ว';
        } 
    }
static function getStatusPayAdmin($data) {
        if ($data == null) {
            return "";//CHtml::link("ยังไม่จ่ายเงิน",array("Site/payForm","id"=>$data->id,"idbank"=>$data->detail));
       
        } else {
            return "ชำระเงินแล้ว";
        } 
    }
    
     static function getStatusAll($data) {
        if ($data == '0') {
            return 'รอ';
        } else if ($data == '1') {
            return 'กำลังทำอหาร';
        } else if ($data == '2') {
            return 'กำลังส่ง';
        }else{
            return 'เสร็จสิ้น';
        }
    }
    
    static function getStatusimg($data) {
        if ($data == '0') {
            return "<br><b>รอรับรายการ".CHtml::image('images/icon/cheff.png',"",array("width"=>80));
        } else if ($data == '1') {
            return "<br><b>กำลังทำอาหาร".CHtml::image('images/icon/cooking3.png',"",array("width"=>80));
        } else {
            return "<br><b>กำลังจัดส่ง".CHtml::image('images/icon/delivery_food.png',"",array("width"=>80));
        }
    }

    //รายการรอชำระ
    public static function getLinkViewBill($id){
        //echo $id."<br>";
        if($id == 'จ่ายตอนรับ'){
            return "Site/CheckStatusFood";//.Yii::app()->createUrl("Site/CheckStatusFood");
        }else{
            return "Site/payForm";//.Yii::app()->createUrl("Site/payForm",array("id"=>$data->id,"idbank"=>$data->detail));
            
        }
    }

    public static function getImageViewBill($id){
        //echo "images/icon/atm.png"."<br>";
        if($id == 'จ่ายตอนรับ'){
            return "atm.png";
        }else{
            return "cooking3.png";
        }
    }
    
    public static function getTest($data){
        if($data != 5){
            return "".CHtml::image("images/icon/atm.png" . $data->bankfood->imgBnk,"",array("width"=>"150"));

        }else{
            return "5";
        }
    }
    //ค้นหารายการซ่อมกรองตาม วัน เดือน ปี ได้
    public function searchFood($year,$month){
        $year-543;
        $criteria=new CDbCriteria();
        $criteria->addCondition("YEAR(created) = $year");
         $criteria->addCondition("MONTH(created) = $month");
         return new CActiveDataProvider($this,array(
             "criteria"=>$criteria,
             "pagination"=>false
             
             ));
    }

    public function countPay($month, $year) {
        $criteria = new CDbCriteria();
        $criteria->addCondition("YEAR(created) = $year");
        $criteria->addCondition("MONTH(created) = $month");
        $criteria->addCondition("status IN ('pay')");
        
        return $this->count($criteria);
    }
    
     public function countComplete($month, $year) {
        $criteria = new CDbCriteria();
        $criteria->addCondition("YEAR(created) = $year");
        $criteria->addCondition("MONTH(created) = $month");
        $criteria->addCondition("statusAll IN ('3')");
        
        return $this->count($criteria);
    }
    
     public function countWait($month, $year) {
        $criteria = new CDbCriteria();
        $criteria->addCondition("YEAR(created) = $year");
        $criteria->addCondition("MONTH(created) = $month");
        $criteria->addCondition("statusAll NOT IN ('3','2','1')");
        
        return $this->count($criteria);
    }
    
      public function countMoney($month, $year) {
        $sum=  Yii::app()->db->createCommand()
                ->select('SUM( food.price * orders.qty ) AS my_sum')
                ->from('orders')
                ->join('bill','orders.bill_id=bill.id')
                ->join('food','orders.food_id=food.id')
                        
                ->where('YEAR( bill.created )='.$year.' AND MONTH(bill.created)='.$month)
                ->queryRow();
        return $sum['my_sum'];
    }
    
    ///******************** ค้นหาตามช่วง เวลา วันเริมต้นถึงวันที่สิ้นสุด ******/////
    public function countCompleteDate($dateStart, $dateEnd) {
       $criteria = new CDbCriteria();
        $criteria->addCondition("created >= '$dateStart'");
        $criteria->addCondition("created <= '$dateEnd'");
        $criteria->addCondition("statusAll  IN ('3')");
        
        return $this->count($criteria);
    }
    
    public function searchFoodDate($dateStart,$dateEnd){
       $criteria = new CDbCriteria();
        $criteria->condition = 'created >= :dateStart and created <= :dateEnd';
        $criteria->params = array(
            'dateStart' => $dateStart,'dateEnd'=>$dateEnd);
         return new CActiveDataProvider($this,array(
             "criteria"=>$criteria,
             "pagination"=>false
             
             ));
    }
    
     public function countPayDate($dateStart, $dateEnd) {
        $criteria = new CDbCriteria();
        $criteria->addCondition("created >= '$dateStart'");
        $criteria->addCondition("created <= '$dateEnd'");
        $criteria->addCondition("status IN ('pay')");
        
        return $this->count($criteria);
    }
    public function countWaitDate($dateStart, $dateEnd) {
        $criteria = new CDbCriteria();
        $criteria->addCondition("created >= '$dateStart'");
        $criteria->addCondition("created <= '$dateEnd'");
        $criteria->addCondition("statusAll IN ('0')");
        
        return $this->count($criteria);
    }
    
     public function countWaitDate2($dateStart, $dateEnd) {
        $criteria = new CDbCriteria();
        $criteria->addCondition("created >= '$dateStart'");
        $criteria->addCondition("created <= '$dateEnd'");
        $criteria->addCondition("statusAll IN ('1')");
        
        return $this->count($criteria);
    }
    
     public function countMoneyDate($dateStart, $dateEnd) {
        $sum=  Yii::app()->db->createCommand()
                ->select('SUM( food.price * orders.qty ) AS my_sum')
                ->from('orders')
                ->join('bill','orders.bill_id=bill.id')
                ->join('food','orders.food_id=food.id')
                        
                ->where(' bill.created >='."'$dateStart'".' AND bill.created <='."'$dateEnd'")
                ->queryRow();
        return $sum['my_sum'];
    }
    
     public function countSumAll() {
        $sum=  Yii::app()->db->createCommand()
                ->select('SUM( food.price * orders.qty ) AS my_sum')
                ->from('orders')
                ->join('bill','orders.bill_id=bill.id')
                ->join('food','orders.food_id=food.id')
                        
                ->where('statusAll=3')
                ->queryRow();
        return $sum['my_sum'];
    }
    
    public function countSumWait() {
        $criteria = new CDbCriteria();
        $criteria->addCondition("statusAll IN ('0','1','2','3')");
        
        return $this->count($criteria);
    }
    
    public function countSumMemberWait() {
        $criteria = new CDbCriteria();
        $criteria->addCondition("statusAll IN ('0')");
        
        return $this->count($criteria);
    }
  
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}