add customer cart

This commit is contained in:
rocho 2015-10-23 20:39:10 +02:00
parent 23a0390a27
commit 0c92fdf167
11 changed files with 293 additions and 81 deletions

View File

@ -9,7 +9,7 @@ use Yii;
*
* @property integer $id_shopping_cart
* @property integer $id_customer
* @property integer $id_sale
* @property integer $id_transfer
*/
class ShoppingCart extends \yii\db\ActiveRecord
{
@ -27,7 +27,7 @@ class ShoppingCart extends \yii\db\ActiveRecord
public function rules()
{
return [
[['id_customer', 'id_sale'], 'integer']
[['id_customer', 'id_transfer'], 'integer']
];
}
@ -39,28 +39,43 @@ class ShoppingCart extends \yii\db\ActiveRecord
return [
'id_shopping_cart' => Yii::t('common/shopping-cart', 'Id Shopping Cart'),
'id_customer' => Yii::t('common/shopping-cart', 'Id Customer'),
'id_sale' => Yii::t('common/shopping-cart', 'Id Sale'),
'id_transfer' => Yii::t('common/shopping-cart', 'Id Transfer'),
];
}
public function getSale(){
$this->hasOne(Sale::className(), ['id_sale' => 'id_sale']);
public function getTransfer(){
return $this->hasOne(Transfer::className(), ['id_transfer' => 'id_transfer']);
}
public function getProduct(){
$this->hasOne(Product::className(), ['id_product' => 'id_product'])->via('sale');
return $this->hasOne(Product::className(), ['id_product' => 'id_product'])->via('transfer');
}
/**
* @param $customer common\models\Customer
* */
public static function readCustomerCart($customer = null){
$sales = [];
$transfers = [];
if ( isset($customer)){
$query = ShoppingCart::find()->andWhere( ['id_customer' => $customer->id_customer] );
$query->with('product');
$sales = $query->all();
$transfers = $query->all();
}
return $sales;
return $transfers;
}
public static function payout( $customer) {
//apply transfer object
//delete cart
$sql = "UPDATE transfer AS t
INNER JOIN shopping_cart AS s ON t.id_transfer = s.id_transfer
SET t.status = " . Transfer::STATUS_PAID
. " WHERE t.status = " . Transfer::STATUS_NOT_PAID;
$q1 = Yii::$app->db->createCommand($sql);
$q1->execute();
ShoppingCart::deleteAll(['id_customer' => $customer->id_customer]);
}
}

View File

@ -144,6 +144,10 @@ class Transfer extends \common\models\BaseFitnessActiveRecord
return $this->hasOne( UserSoldItem::className(), ["id_transfer" =>"id_transfer" ] );
}
public function getCustomerCart(){
return $this->hasOne( ShoppingCart::className(), ["id_transfer" =>"id_transfer" ] );
}
public function getSale(){
return $this->hasOne( Sale::className(), ["id_sale" =>"id_object" ] ) ;
}
@ -398,6 +402,18 @@ class Transfer extends \common\models\BaseFitnessActiveRecord
return $transfers;
}
public static function readCustomerCart($customer){
$transfers = [];
if ( isset($customer) ){
$query = Transfer::find();
$query->innerJoinWith('customerCart');
$query->andWhere(['shopping_cart.id_customer' => $customer->id_customer ]);
$transfers = $query->all();
}
return $transfers;
}
public static function types( ) {
return [

View File

@ -0,0 +1,30 @@
<?php
use yii\db\Schema;
use yii\db\Migration;
class m151021_204300_alter__table__shopping_cart__alter__column__id_sale__to__id_transfer extends Migration
{
public function up()
{
$this->renameColumn("shopping_cart", "id_sale", "id_transfer");
}
public function down()
{
echo "m151021_204300_alter__table__sale__alter__column__id_sale__to__id_transfer cannot be reverted.\n";
return false;
}
/*
// Use safeUp/safeDown to run migration code within a transaction
public function safeUp()
{
}
public function safeDown()
{
}
*/
}

View File

@ -42,7 +42,7 @@ class ProductController extends Controller
],
'access' => [
'class' => \yii\filters\AccessControl::className(),
'only' => [ 'sale','clear-list', 'lookup'],
'only' => [ 'sale','payout-customer-cart','payout-user-cart', 'lookup'],
'rules' => [
// allow authenticated users
[
@ -73,6 +73,7 @@ class ProductController extends Controller
$user = User::findOne(Yii::$app->user->id );
$model->customer = $this->customer;
$model->card = $this->card;
if (Yii::$app->request->isAjax) {
@ -98,15 +99,17 @@ class ProductController extends Controller
}
$userTransfers = Transfer::modelsToArray( Transfer::readUserSoldTransfers( $user ) );
$customerCart = Transfer::modelsToArray( Transfer::readCustomerCart( $this->customer ) );
$result['transfers'] = $userTransfers;
$result['customer_cart'] = $customerCart;
return $result;
}else{
// $userTransfers = Transfer::readUserSoldTransfers( $user );
$model->customerCart = Transfer::modelsToArray( Transfer::readCustomerCart($this->customer) );
$userTransfers = Transfer::modelsToArray( Transfer::readUserSoldTransfers($user) );
$model->customerCart = ShoppingCart::readCustomerCart( $this->customer );
return $this->render("sale",[
'customer' => $this->customer,
@ -122,7 +125,7 @@ class ProductController extends Controller
}
public function actionClearList(){
public function actionPayoutUserCart(){
if (Yii::$app->request->isAjax) {
$result = [];
@ -140,6 +143,39 @@ class ProductController extends Controller
}
}
public function actionPayoutCustomerCart($number){
$cart = [];
$code = 'error';
$message = 'Hiba történt';
if (Yii::$app->request->isAjax) {
$result = [];
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$this->findByNumber($number);
if ( isset($this->customer)){
$connection = \Yii::$app->db;
$transaction = $connection->beginTransaction();
try {
ShoppingCart::payout($this->customer);
$transaction->commit();
$cart = Transfer::modelsToArray( Transfer::readCustomerCart($this->customer) );
$code = 'success';
$message = 'Bevásárlókoosár fizetve: ' .$this->card->number;
} catch(Exception $e) {
$transaction->rollback();
}
}
$result['customer_cart'] = $cart;
$result['code'] = $code;
$result['message'] = $message;
return $result;
}
}

View File

@ -18,11 +18,35 @@ use common\models\ShoppingCart;
/**
* ContactForm is the model behind the contact form.
*
* @property integer $id_product
* @property integer $count
* @property integer $id_currency
* @property integer $id_discount
* @property string $comment
* @property string $barcode
* @property string $product_number
* @property integer $sale_price
* @property string $product_name
* @property common\models\Account[] $accounts
* @property common\models\Currency[] $currencies
* @property common\models\Discount[] $discounts
* @property common\models\Product $product
* @property common\models\Account $account
* @property common\models\Currency $currency
* @property common\models\Discount $discount
* @property common\models\Customer $customer
* @property common\models\Card $card
* @property common\models\Transfer $transfer
* @property common\models\Sale $sale
* @property common\models\ShoppingCart[] $customerCart
*
*
*/
class ProductSaleForm extends Model
{
public $append_to_sold_list;
public $cart;
public $id_product;
public $count;
@ -45,6 +69,7 @@ class ProductSaleForm extends Model
public $currency;
public $discount;
public $customer;
public $card;
public $transfer;
public $sale;
@ -60,7 +85,7 @@ class ProductSaleForm extends Model
[['id_product','count','id_account'], 'required'],
[['id_product','id_currency','id_account', 'id_discount','count'], 'integer'],
[['comment'], 'string' ,'max' => 255],
[['append_to_sold_list'], 'string' ,'max' => 10],
[['cart'], 'string' ,'max' => 20],
[['id_product' ], 'validateProduct'],
[['count' ], 'validateCount'],
[['id_currency' ], 'validateCurrency'],
@ -127,11 +152,21 @@ class ProductSaleForm extends Model
public function save(){
if ( $this->validate() ){
$connection = \Yii::$app->db;
$transaction = $connection->beginTransaction();
try {
$this->saveSale();
$this->saveTransfer();
$this->saveProduct();
$this->appendToUserCart();
$this->appendToCustomerCart();
$transaction->commit();
return true;
} catch(Exception $e) {
$transaction->rollback();
return false;
}
return true;
}
return false;
@ -139,10 +174,8 @@ class ProductSaleForm extends Model
protected function saveSale(){
$this->sale = Sale::createSale($this->account, $this->discount, $this->currency, $this->count, $this->product );
/*
*/
$this->sale->status = Sale::STATUS_PAID;
if ( isset($this->comment)){
$this->sale->comment = $this->comment;
}
@ -151,11 +184,14 @@ class ProductSaleForm extends Model
}
protected function saveTransfer(){
$status = Transfer::STATUS_PAID;
if ( $this->isAppendToUserCart() ){
$status = Transfer::STATUS_NOT_PAID;
}else if ( $this->isAppendToCustomerCart() ){
$status = Transfer::STATUS_NOT_PAID;
}
$this->transfer = Transfer::createProductTransfer($this->sale,$this->account, $this->discount, $this->currency, $this->count, $this->product);
/*
*/
$this->transfer->status = Transfer::STATUS_PAID;
$this->transfer = Transfer::createProductTransfer($this->sale,$this->account, $this->discount, $this->currency, $this->count, $this->product,$status);
if ( isset($this->comment)){
$this->transfer->comment = $this->comment;
}
@ -170,7 +206,7 @@ class ProductSaleForm extends Model
public function isAppendToUserCart(){
$result = false;
if ( isset( $this->append_to_sold_list ) && $this->append_to_sold_list == 'append' ){
if ( isset( $this->cart ) && $this->cart == 'user' ){
$result = true;
}
return $result;
@ -186,17 +222,19 @@ class ProductSaleForm extends Model
}
public function isAppendToCustomerCart(){
$result = false;
if ( isset( $this->add_to_customer ) && $this->add_to_customer == 'append' ){
if ( isset( $this->cart ) && $this->cart == 'customer' ){
$result = true;
}
return $result;
}
public function appendToCustomerCart(){
// print_r("cart: ".$this->cart );
// print_r("customer:: ".$this->customer );
if ( $this->isAppendToCustomerCart() && isset($this->customer) ){
$item = new ShoppingCart();
$item->id_customer = $this->customer->id_customer;
$item->id_sale = $this->sale->id_sale;
$item->id_transfer = $this->transfer->id_transfer;
$item->save(false);
}
}

View File

@ -33,4 +33,4 @@ use yii\bootstrap\Html;
</div>
</div>
</div>
<?php echo Html::a(Yii::t('frontend/product', "Fizetve"),null,[ 'id' => 'btn_paid', 'class' => 'btn btn-primary' ]) ?>
<?php echo Html::a(Yii::t('frontend/product', "Fizetve"),null,[ 'id' => 'btn_pay_customer_cart', 'class' => 'btn btn-primary' ]) ?>

View File

@ -69,7 +69,8 @@ $discountOptions = mkOptions( ArrayHelper::map($discounts, 'id_discount', 'name'
]
); ?>
<?php echo Html::activeHiddenInput($model, 'id_product') ?>
<?php echo Html::activeHiddenInput($model, 'append_to_sold_list') ?>
<?php echo Html::activeHiddenInput($model, 'cart') ?>
<div class="row">
<div class='col-md-12'>
@ -104,7 +105,11 @@ $discountOptions = mkOptions( ArrayHelper::map($discounts, 'id_discount', 'name'
<?php echo Html::a(Yii::t("frontend/product","To cart"),null,['class' => 'btn btn-success', 'id' => 'btn_sell_append'] );?>
</div>
<div class='col-md-4'>
<?php echo Html::a(Yii::t("frontend/product","Write up"),null,['class' => 'btn btn-success', 'id' => 'btn_add_to_customer_cart'] );?>
<?php
if ( isset($model->card) ){
echo Html::a(Yii::t("frontend/product","Write up"),null,['class' => 'btn btn-success', 'id' => 'btn_add_to_customer_cart'] );
}
?>
</div>
</div>
<?php ActiveForm::end(); ?>

View File

@ -3,10 +3,10 @@ use yii\bootstrap\Html;
?>
<h1><?php echo Yii::t('frontend/product','User Cart')?></h1>
<div class="row">
<div class='col-md-12 '>
<div class="sold-items-container">
<div class="user-cart">
<table class="table table-bordered table-striped">
<thead>
<tr>
@ -33,3 +33,4 @@ use yii\bootstrap\Html;
</div>
</div>
</div>
<?php echo Html::a(Yii::t('frontend/product', "Paid"),null,[ 'id' => 'btn_pay_user_cart', 'class' => 'btn btn-primary' ]) ?>

View File

@ -19,8 +19,12 @@ ProductSellAsset::register($this);
$options = [];
$options['lookup_product_url'] = Url::toRoute(['product/lookup']);
$options['clear_list_url'] = Url::toRoute(['product/clear-list']);
$options['sold_items'] = $userTransfers;
$options['url_pay_user_cart'] = Url::toRoute(['product/payout-user-cart']);
$options['user_cart'] = $userTransfers;
if ( isset($model->card) ){
$options['url_pay_customer_card'] = Url::toRoute(['product/payout-customer-cart','number' => $model->card->number]);
$options['customer_cart'] = $model->customerCart;
}
$options['discounts'] = Discount::modelsToArray($discounts);
@ -77,14 +81,10 @@ $this->registerJs ( 'new ProductSell( '. json_encode($options).');' );
<?php echo $this->render('_sale_form' ,['model' =>$model , 'lookupModel' =>$lookupModel, 'currencies' => $currencies, 'accounts' => $accounts,'discounts' => $discounts ]) ?>
</div>
<div class='col-md-6'>
<?php if ( isset($card) ){ ?>
<?php echo $this->render('_customer_cart' ) ?>
<h1><?php echo Yii::t('frontend/product','Cart')?></h1>
<?php echo $this->render('_sold_items' ) ?>
<?php echo Html::a(Yii::t('frontend/product', "Paid"),null,[ 'id' => 'btn_paid', 'class' => 'btn btn-primary' ]) ?>
<?php }?>
<?php echo $this->render('_user_cart' ) ?>
</div>
</div>

View File

@ -3,24 +3,31 @@ function ProductSell(o){
/**reference for the instance*/
var app = this;
/**currently loaded product*/
var product = null;
this.product = null;
this.defaults = {
/**id of filter text*/
selector_filter_text: '#filter_text',
/**selector for customer cart container*/
selector_customer_cart: '.customer-cart',
selector_user_cart: '.user-cart',
selector_btn_pay_customer_cart: '#btn_pay_customer_cart',
selector_btn_pay_user_cart: '#btn_pay_user_cart',
url_pay_customer_card: '-',
/** ajax url for lookup service*/
lookup_product_url: '',
/**mark list paid url*/
clear_list_url: '',
url_pay_user_cart: '',
/**the id of form*/
selector_form: '#product_form',
/**form contains error text*/
form_invalid: 'Az ürlap hibákat tartalmaz',
message_paid: 'Tételek fizetve',
/**list of sold items by current user*/
sold_items: [],
user_cart: [],
discounts: [],
customer_cart: []
customer_cart: [],
};
init();
@ -30,20 +37,60 @@ function ProductSell(o){
app.keyDate = null;
app.keySeq = '';
app.enterPressed = 0;
addBehaviourBtnSell();
addBehaviourBtnSellAndAppendToBill();
addBehaviorAjaxSubmit();
addSellBehaiours();
setFocus();
addEnterPressedBehaviours();
addBehaviorCountChangedListener();
addBehaviourDisocuntChanged();
createUserSoldItemsTable();
createCustomerCartTable();
addBehaviourBtnPaid();
addListenerBehaviours();
createCarts();
addPayoutButtons();
productChanged();
addDocumentKeypressedListener();
}
/**
* payout out user or customer cart
* */
function addPayoutButtons(){
addBehaviourPayoutUserCart();
addBehaviourPayoutCustomerCart();
}
/**
* display user and customer cart on page load
* */
function createCarts(){
createUserCartTable();
createCustomerCartTable();
}
/**
* add change and enter pressed listeners
* */
function addListenerBehaviours(){
addEnterPressedBehaviours();
addBehaviorCountChangedListener();
addBehaviourDisocuntChanged();
}
/**
* sell a product. there are three type of sell:
* - sell inmadietly
* - sell and put it to user cart
* - sell and put it to customer cart
* */
function addSellBehaiours(){
addBehaviourBtnSell();
addBehaviourBtnSellAndAppendToBill();
addBehaviourBtnAddToCustomerCart();
addBehaviorAjaxSubmit();
}
function addBehaviourDisocuntChanged(){
$("#productsaleform-id_discount").change(refreshCalculatedValues);
}
@ -103,6 +150,9 @@ function ProductSell(o){
}
}
/**
* Add traversing between input elements on enter key pressed
* */
function addEnterPressedBehaviours(){
addBehaviorEnterPressedListener();
addBehaviorCountEnterPressedListener();
@ -277,24 +327,20 @@ function ProductSell(o){
}
function submitSell(){
$('#productsaleform-append_to_sold_list').val('');
$('#productsaleform-cart').val('');
$('#product_form').submit();
}
function submitSellAndAppend(){
$('#productsaleform-append_to_sold_list').val('append');
$('#productsaleform-cart').val('user');
$('#product_form').submit();
}
function submitAddToCustomerCart(){
$('#productsaleform-append_to_sold_list').val('append');
$('#productsaleform-cart').val('customer');
$('#product_form').submit();
}
function refreshSoldUseritems(){
$('.sold-items-container').transferList('option','transfers' , app.defaults.sold_items );
}
function addBehaviorAjaxSubmit(){
$('body').on('afterValidate', '#product_form', function () {
@ -327,8 +373,10 @@ function ProductSell(o){
refreshCalculatedValues();
$("#filter_text").val('');
setFocus();
app.defaults.sold_items = response.transfers;
refreshSoldItemsList();
app.defaults.user_cart = response.transfers;
app.defaults.customer_cart = response.customer_cart;
refreshUserCart();
refreshCustomerCart();
}else if ( response.code == 'invalid'){
if ( response.errors ){
$.each(response.errors, function (key, value){
@ -347,21 +395,24 @@ function ProductSell(o){
}
function refreshSoldItemsList(){
$('.sold-items-container').transferList( 'option', 'transfers' , app.defaults.sold_items );
function refreshUserCart(){
$(app.defaults.selector_user_cart).transferList( 'option', 'transfers' , app.defaults.user_cart );
}
function refreshCustomerCart(){
$(app.defaults.selector_customer_cart ).transferList( 'option', 'transfers' , app.defaults.customer_cart );
}
function addBehaviourBtnPaid( ){
$('#btn_paid').on('click',function(){
function addBehaviourPayoutUserCart( ){
$( app.defaults.selector_btn_pay_user_cart ).on('click',function(){
$.ajax({
url: app.defaults.clear_list_url,
url: app.defaults.url_pay_user_cart,
type: 'post',
data: {},
success: function (response) {
if ( response.code == 'success'){
app.defaults.sold_items= response.transfers;
refreshSoldItemsList();
app.defaults.user_cart = response.transfers;
refreshUserCart();
$.notify(app.defaults.message_paid, { 'type' : 'success' });
}
}
@ -369,14 +420,35 @@ function ProductSell(o){
);
});
}
function addBehaviourPayoutCustomerCart( ){
$( app.defaults.selector_btn_pay_customer_cart ).on('click',function(){
alert('ok');
$.ajax({
url: app.defaults.url_pay_customer_card,
type: 'post',
data: {},
success: function (response) {
if ( response.code == 'success'){
app.defaults.customer_cart = response.customer_cart;
refreshCustomerCart();
}
$.notify(response.message, { 'type' : response.code });
},
error: function(){
alert('Hiba történt');
}
}
);
});
}
function createUserSoldItemsTable(){
$('.sold-items-container').transferList({
'transfers' : app.defaults.sold_items
function createUserCartTable(){
$(app.defaults.selector_user_cart).transferList({
'transfers' : app.defaults.user_cart
});
}
function createCustomerCartTable(){
$('.customer-cart').transferList({
$(app.defaults.selector_customer_cart).transferList({
'transfers' : app.defaults.customer_cart
});
}

View File

@ -136,7 +136,6 @@ $.widget( "fitness.transferList", {
self._refresh();
},
};
// base
this._super(key, value);