Ejemplo n.º 1
0
 /**
  * @param array $product_info infomation of product $product_info[$primary_key_value] = [
  *   $primary_key => '',
  *   $title => '',
  *   $price => '',
  *   $special_price => '',
  *   'quantity' => 1
  * ]
  * @return bolean
  */
 public function addCart($product_info = [])
 {
     if (empty($product_info)) {
         return false;
     }
     $ecommerce = Ecommerce::module();
     $arrProduct = !empty($this->product) ? $this->product : [];
     if (!empty($arrProduct)) {
         // Get id of product add to cart
         $arrIdProduct = array_keys($product_info);
         foreach ($arrProduct as $id => $info) {
             // Check product had cart
             if (in_array($id, $arrIdProduct) and $ecommerce->multiple) {
                 // Qty of product add to cart
                 $qty_product_add = (int) ArrayHelper::getValue($product_info, $id . '.quantity', 1);
                 // Update qty product had in cart
                 $arrProduct[$id]['quantity'] = isset($arrProduct[$id]['quantity']) ? (int) $arrProduct[$id]['quantity'] + $qty_product_add : $qty_product_add;
                 // Remove product had in cart
                 unset($product_info[$id]);
             }
         }
     }
     $this->product = $arrProduct + $product_info;
     $numberItemCart = 0;
     foreach ($this->product as $id => $info) {
         $qty_product = (int) ArrayHelper::getValue($info, 'quantity', 0);
         $numberItemCart += $qty_product;
     }
     $saveCart = $this->save();
     if ($saveCart) {
         Yii::$app->session->set('numberItemCart', $numberItemCart);
     }
     return $saveCart;
 }
 /**
  * @inheritdoc
  */
 public function beforeSave($insert)
 {
     $attributes = array_keys($this->getAttributes());
     // Get namespace of model
     $ecommerce = Ecommerce::module();
     // User name field
     $username = ArrayHelper::getValue($ecommerce->userTable, 'nameField');
     // date time
     $now = new MongoDate();
     if (in_array('created_at', $attributes) and empty($this->created_at)) {
         $this->created_at = $now;
     }
     if (in_array('updated_at', $attributes)) {
         $this->updated_at = $now;
     }
     if ($this->isNewRecord) {
         // creator
         if (in_array('creator', $attributes)) {
             $this->creator = Yii::$app->user->id;
         }
         // ID
         if (empty($this->_id)) {
             $this->_id = uniqid();
         }
         // ecommerce_id
         if (empty($this->ecommerce_id)) {
             $this->ecommerce_id = uniqid('EM');
         }
         // status
         if (empty($this->status)) {
             $this->status = $ecommerce::STATUS_NEW;
         }
     }
     // Write log order
     $this->buildLogOrder($attributes, $username, $now);
     // Note admin
     if (in_array('note_admin', $attributes) and in_array('note_admin_content', $attributes) and !empty($this->note_admin_content)) {
         if ($this->isNewRecord) {
             $this->note_admin = [['content' => $this->note_admin_content, 'creator' => Yii::$app->user->id, 'creator_name' => Yii::$app->user->identity->{$username}, 'created_at' => $now]];
         } else {
             $this->note_admin = ArrayHelper::merge($this->note_admin, [['content' => $this->note_admin_content, 'creator' => Yii::$app->user->id, 'creator_name' => Yii::$app->user->identity->{$username}, 'created_at' => $now]]);
         }
         $this->note_admin_content = '';
     }
     return parent::beforeSave($insert);
 }
Ejemplo n.º 3
0
 /**
  * Finds the model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param string $id
  * @return the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     // Get namespace of model
     $ecommerce = Ecommerce::module();
     $namespaceModel = $ecommerce->itemModule;
     if (($model = $namespaceModel::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Ejemplo n.º 4
0
 /**
  * Function generate log order
  * @return string
  */
 public function generateLogOrder()
 {
     $template = null;
     if (is_array($this->log) and !empty($this->log)) {
         foreach ($this->log as $log) {
             // Declare infomation log
             $created_at = ArrayHelper::getValue($log, 'created_at');
             $action = ArrayHelper::getValue($log, 'action');
             $note = ArrayHelper::getValue($log, 'note');
             $creator = ArrayHelper::getValue($log, 'creator');
             $logCreator = ArrayHelper::getValue($log, 'creator_name');
             // Get namespace of model
             $ecommerce = Ecommerce::module();
             // User field
             $linkUser = ArrayHelper::getValue($ecommerce->userTable, 'linkInfo');
             $idUser = ArrayHelper::getValue($ecommerce->userTable, 'idField');
             $template .= Html::beginTag('div', ['class' => 'timeline-item']);
             $template .= Html::beginTag('div', ['class' => 'row']);
             $template .= Html::beginTag('div', ['class' => 'col-xs-3 date']);
             $template .= Html::tag('i', '', ['class' => 'fa ' . ArrayHelper::getValue(\sya\ecommerce\Module::$logStatus, $action)]);
             $template .= date('H:i a', $created_at->sec);
             $template .= Html::tag('br');
             $template .= Html::tag('small', Yii::$app->formatter->asRelativeTime($created_at->sec), ['class' => 'text-navy']);
             $template .= Html::endTag('div');
             $template .= Html::beginTag('div', ['class' => 'col-xs-7 content no-top-border']);
             $template .= Html::tag('p', Html::tag('strong', ucfirst($action)), ['class' => 'm-b-xs']);
             $template .= Html::tag('p', Html::a($logCreator, Url::to([$linkUser, $idUser => $creator])) . ' ' . $note);
             $template .= Html::endTag('div');
             $template .= Html::endTag('div');
             $template .= Html::endTag('div');
         }
     }
     return $template;
 }
Ejemplo n.º 5
0
<?php

use yii\bootstrap\Modal;
use kartik\grid\GridView;
use yii\helpers\ArrayHelper;
use yii\bootstrap\Html;
use sya\ecommerce\Ecommerce;
$ecommerce = Ecommerce::module();
// Declare column in product
$defaultColumns = [['attribute' => '_id', 'hAlign' => 'center', 'vAlign' => 'middle', 'contentOptions' => ['class' => 'productId']]];
$productColumns = ArrayHelper::merge($defaultColumns, $productColumns);
$productColumns[] = ['header' => Yii::t('ecommerce', 'Quantity'), 'hAlign' => 'center', 'vAlign' => 'middle', 'contentOptions' => ['class' => 'productQty'], 'value' => function ($model, $key, $index, $widget) use($ecommerce) {
    $options = ['class' => 'form-control qty_' . $model->_id];
    if ($ecommerce->multiple) {
        $options['onkeyup'] = 'return productQtyOrder(this);';
    } else {
        $options['readonly'] = '';
    }
    return Html::textInput('qty', 1, $options);
}, 'format' => 'raw'];
// Modal product
Modal::begin(['id' => 'product_modal', 'size' => 'modal-lg', 'header' => '<h4 class="modal-title" id="myModalLabel">' . Yii::t('ecommerce', 'List Product') . '</h4>', 'toggleButton' => ['label' => '<i class="fa fa-plus"></i>&nbsp;' . Yii::t('ecommerce', 'Create') . ' ' . Yii::t('ecommerce', 'Product'), 'class' => 'btn btn-xs btn-primary pull-right'], 'footer' => '<button type="button" onclick="addProduct();" class="btn btn-primary">' . Yii::t('ecommerce', 'Create') . ' ' . Yii::t('ecommerce', 'Product') . '</button>']);
echo GridView::widget(['panel' => ['heading' => Yii::t('ecommerce', 'Product')], 'id' => 'product-grid', 'pjax' => TRUE, 'dataProvider' => $productDataProvider, 'filterModel' => $productSearchModel, 'columns' => $productColumns, 'responsive' => true, 'hover' => true, 'toolbar' => []]);
Modal::end();
echo Html::hiddenInput('product_list', \sya\ecommerce\Module::getProductList($model->product), ['id' => 'product_list']);
echo Html::hiddenInput(\yii\helpers\StringHelper::basename(get_class($model)) . '[product_text]', $model->product_text, ['id' => 'product_text']);
// Register js code
$this->registerJs("\r\n    // Add or remove product when click product\r\n    function productOrder(){\r\n        \$('#product-grid-container table tbody tr').click(function(){\r\n            // Get id of product and qty\r\n            var id = \$(this).find('.productId').text();\r\n            var qty = \$(this).find('.productQty input');\r\n            \r\n            // Get id and qty selected\r\n            var product_list = new Array();\r\n            if(\$('#product_list').val()){\r\n                var productSelected = \$('#product_list').val().split(',');\r\n            }else{\r\n                var productSelected = null;\r\n            }\r\n            \r\n            if(\$(this).hasClass('selected')){ // remove from hidden field\r\n                removeProductId(productSelected, product_list, id, qty, this);\r\n            } else { // add too hidden field\r\n                addProductId(productSelected, product_list, id, qty, this);\r\n            }\r\n        });\r\n    }\r\n    \r\n    // Add or remove product when qty = 0\r\n    function productQtyOrder(element){\r\n        // Get id of product and qty\r\n        var id = \$(element).parent().parent().find('.productId').text();\r\n        var qty = \$(element);\r\n        \r\n        // Get id and qty selected\r\n        var product_list = new Array();\r\n        if(\$('#product_list').val()){\r\n            var productSelected = \$('#product_list').val().split(',');\r\n        }else{\r\n            var productSelected = null;\r\n        }\r\n\r\n        if(qty.val() == 0){ // remove from hidden field\r\n            removeProductId(productSelected, product_list, id, qty, \$(element).parent().parent());\r\n        } else { // add too hidden field\r\n            addProductId(productSelected, product_list, id, qty, \$(element).parent().parent());\r\n        }\r\n    }\r\n    \r\n    // Function remove id product\r\n    function removeProductId(productSelected, product_list, id, qty, element){\r\n        if(productSelected.length){\r\n            j = 0;\r\n            for(i =0;i< productSelected.length;i++){\r\n                info = productSelected[i].split(':');\r\n                if(info[0]!=id){\r\n                    product_list[j] = info[0]+':'+info[1];\r\n                    j++;\r\n                }\r\n            }\r\n        }\r\n\r\n        \$('#product_list').val(product_list.length?product_list.join():'');\r\n        \$(element).removeClass('selected');\r\n        qty.val(0);\r\n    }\r\n    \r\n    // Function add id product\r\n    function addProductId(productSelected, product_list, id, qty, element){\r\n        if (qty.val() == 0) qty.val(1);\r\n        \$(element).addClass('selected');\r\n\r\n        if(productSelected){\r\n            updateValue = false;\r\n            for(i =0;i< productSelected.length;i++){\r\n                info = productSelected[i].split(':');\r\n                if(info[0]==id){\r\n                    product_list[i] =info[0]+':'+qty.val();\r\n                    updateValue = true;\r\n                }else{\r\n                    product_list[i]=info[0]+':'+info[1];\r\n                }\r\n            }\r\n            if(!updateValue){\r\n                product_list[product_list.length] = id+':'+qty.val();\r\n            }\r\n        }\r\n        \$('#product_list').val(product_list.length?product_list.join():(id+':'+qty.val()));\r\n    }\r\n    \r\n    // Selected product\r\n    function setSelect(){\r\n        var product_list = \$('#product_list').val();\r\n        if(product_list){\r\n            var field = product_list.split(',');\r\n            var total = field.length;\r\n            for(var i=0; i<total; i++){\r\n                var product = field[i].split(':');\r\n                \$('.qty_'+product[0]).val(product[1]);\r\n                \$('.qty_'+product[0]).parent().parent().addClass('selected');\r\n            }\t\t\r\n        }\r\n    }\r\n    \r\n    // Add product in order\r\n    function addProduct(){\r\n        var shipping = \$('#syaShipping').val();\r\n        \$.ajax({\r\n            url: '" . \yii\helpers\Url::to(['/ecommerce/ajax/addproduct']) . "',\r\n            type: 'post',\r\n            dataType: 'json',\r\n            data: {data: \$('#product_list').val(), shipping: shipping},\r\n        }).done(function (data) {\r\n            \$('#product_info').html(data.template);\r\n            \$('#product_text').val(data.titles);\r\n            totalProduct();\r\n            \$('#product_modal').modal('hide');\r\n        });\r\n    }\r\n", yii\web\View::POS_END);
$this->registerJs("\r\n    productOrder();\r\n    setSelect();\r\n    \$(document).on('pjax:complete', function () {\r\n        productOrder();\r\n        setSelect();\r\n    });\r\n", yii\web\View::POS_READY);