วันจันทร์ที่ 2 กันยายน พ.ศ. 2556

การคำนวณจำนวนวันจากการเลือกวันที่ 2 วัน StatDate - EndDate

#เลือกวันที่เริ่มต้น ในการเลือกวันที่เริ่มต้นนั้น Jquery จะทำการระบุวันที่ สิ้นสุดให้ และระบุจำนวนให้ 1 วัน และเมื่อทำการเลือกข้อมูลวันที่ สิ้นสุด ก็จะมีการคำนวณ จำนวนวันใหม่อีกครั้ง
โดยจำนวนวันจะถูกจัดเก็บใน textbox ID CarRental_rentDays
 <?php
     $this->widget('zii.widgets.jui.CJuiDatePicker', array(
         'model' => $carRental,
         'attribute' => 'begin_date',
         'options' => array(
             'showAnim' => 'fold',
             'dateFormat' => 'dd/mm/yy',
             //'minDate' => '0',
             'minDate' => 'new Date()',
             'changeYear' => 'true', // แสดง Dropdown เลือกปี
             'changeMonth' => 'true', // แสดง Dropdown เลือกเดือน
             'onSelect' => 'js:function( selectedDate ) {
             $( "#' . CHtml::activeId($carRental, 'end_date') . '" ).datepicker("option", "minDate", selectedDate); //set the end date cjuidatepiker minDate and its working fine
                
$("#CarRental_end_date").val(selectedDate);
var startDate = $("#CarRental_begin_date").val().split("/");
var begin_date = new Date(startDate[2], startDate[1] - 1, startDate[0]);
   
var endDate = $("#CarRental_end_date").val().split("/");
var end_date = new Date(endDate[2], endDate[1] - 1, endDate[0]);
var days = (end_date - begin_date) / (1000*60*60*24);
$("#CarRental_rentDays").val(days+1);
   
}',
         ),
         'htmlOptions' => array(
             'style' => 'width:105px;vertical-align:top;font-size:0.9em;'
         ),
     ));
     ?>

<?php
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
      
    'model'=>$carRental,
  //  'value' =>($carRental->end_date)?$carRental->begin_date:date('d/m/Y', strtotime("+1 day")),
                               
        'attribute'=>'end_date',   
        'options'=>array(
                'showAnim'=>'fold',
                'dateFormat'=>'dd/mm/yy',
              'changeYear' => 'true', // แสดง Dropdown เลือกปี
        'changeMonth' => 'true', // แสดง Dropdown เลือกเดือน
                'onSelect'=> 'js:function(selectedDate) {
                   
$("#CarRental_end_date").val(selectedDate); // Why do you need this?
 
var startDate = $("#CarRental_begin_date").val().split("/");
var endDate = $("#CarRental_end_date").val().split("/");
var begin_date = new Date(startDate[2], startDate[1] - 1, startDate[0]);
var end_date = new Date(endDate[2], endDate[1] - 1, endDate[0]);
var days = (end_date - begin_date) / (1000*60*60*24);
$("#CarRental_rentDays").val(days+1);


                }',                            
        ),
        'htmlOptions'=>array(
                'style'=>'width:105px;vertical-align:top;font-size:0.9em;'
        ),
)); ?>
    

วันจันทร์ที่ 1 กรกฎาคม พ.ศ. 2556

Query Data Replace On AR Yii createCommand

#Query Data Replace 
$connection = yii::app()->db;
$sql = "replace into tborder_details(
OrderNo,
ProductID,
Qty,
Price,
TotalPrice,
OrderDate
)
select
'".$NewOrderNo."',
ctmp.ProductID,
Qty,
IF(promotion = 'Y',price_promotion,price_cash) AS Price,
IF(promotion = 'Y',price_promotion*Qty,price_cash*Qty) AS TotalPrice,
OrderDate
From  cart  ctmp
inner join tbproducts prd ON (prd.ProductID = ctmp.ProductID)
where SessionOrder = '" . $SessionID . "' and OrderDate = '" . date('Y-m-d') . "'  and  OrderIP ='" . $IPadd . "'
";
$command = $connection->createCommand($sql);
$command->execute();

//ลบข้อมูลออกจากระบบ
$connection = yii::app()->db;
$sql1 = " delete from cart where SessionOrder = '" . $SessionID . "' and OrderDate = '" . date('Y-m-d') . "'  and  OrderIP ='" . $IPadd . "' ";
$command = $connection->createCommand($sql1);
$command->execute();

วันพฤหัสบดีที่ 27 มิถุนายน พ.ศ. 2556

Yii Shoping Cart2

public function actionAddToCart($ProductID) {
         
           $SessionID = Yii::app()->session->sessionID;
           $IPadd = Yii::app()->request->userHostAddress;
           $Cart = New Cart();
           $ProductID = $_GET['ProductID'];
         
            $Cart->ProductID = $ProductID;           
            $Cart->SessionOrder = $SessionID;
            $Cart->OrderIP = $IPadd;
            $Cart->OrderDate = date('Y-m-d H:i:s');
         
         
           $chkOrderList = Yii::app()->db->createCommand()
                ->select()
                ->from('cart')
                ->where('ProductID = :ProductID and SessionOrder = :SessionID', array(':ProductID' => $_GET['ProductID'], ':SessionID' => $SessionID))
                ->queryScalar();

        if(!empty($_GET['ProductID'])){
           
                //Loop ตราจสอบข้อมูลการสั่งซื้อ
              if($_GET['ProductID'] = $chkOrderList['ProductID'] and $SessionID = $chkOrderList['SessionOrder']  ){
                 // Update ข้อมูลกรณีที่มีการสั่งซื้อสินค้า รหัสนี้แล้วจาก Session เดียวกัน
                  $Cart = Cart::model()->findBypk(array('ProductID' => $ProductID, 'SessionOrder' => $SessionID));
             
                 
              }
             
            $Cart->Qty = $Cart->Qty + 1;
            if($Cart->save()){
             
               $this->redirect(array("site/OrderList"));
          }
                    }
            }

Yii Basic Shoping Cart

#MySQL DataDase

CREATE TABLE `cart` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `ProductID` varchar(10) NOT NULL,
  `Qty` varchar(10) NOT NULL,
  `SessionOrder` varchar(100) NOT NULL,
  `OrderIP` varchar(100) NOT NULL,
  `OrderDate` date NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `ProductID` (`ProductID`,`SessionOrder`,`OrderIP`,`OrderDate`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;


#Controller - > ActionCart

public function actionAddToCart($ProductID) {
         
           $SessionID = Yii::app()->session->sessionID;
           $IPadd = Yii::app()->request->userHostAddress;
           $Cart = New Cart();
         
      
          if(!empty($_GET)){
             
          $ProductID = $_GET['ProductID'];
           $Cart->Qty =  1; 
           $Cart->ProductID = $_GET['ProductID'] ;
           $Cart->SessionOrder =  $SessionID ;
           $Cart->OrderIP =  $IPadd;
           $Cart->OrderDate = date('Y-m-d H:i:s');   
      
if($ProductID = '5600003' and $SessionID = '83b96840542c1b43553462272a089258' ){
                 // $Cart = Cart::model()->findByPk(2);
                 
                  $Cart = Cart::model()->findByAttributes(array('ProductID' => '5600003', 'SessionOrder' => '83b96840542c1b43553462272a089258'));
                  $Cart->Qty =  $Cart->Qty+1; 

              }

          if($Cart->save()){
               $this->redirect(array("site/OrderList"));
          }
           
                    }
         
        }