From 0c92fdf167cc7d60defc531130b63c8ea83e6a73 Mon Sep 17 00:00:00 2001 From: rocho Date: Fri, 23 Oct 2015 20:39:10 +0200 Subject: [PATCH] add customer cart --- common/models/ShoppingCart.php | 33 +++-- common/models/Transfer.php | 16 ++ ...lter__column__id_sale__to__id_transfer.php | 30 ++++ frontend/controllers/ProductController.php | 44 +++++- frontend/models/ProductSaleForm.php | 74 ++++++--- frontend/views/product/_customer_cart.php | 2 +- frontend/views/product/_sale_form.php | 9 +- .../{_sold_items.php => _user_cart.php} | 5 +- frontend/views/product/sale.php | 20 +-- frontend/web/js/product.sell.js | 140 +++++++++++++----- frontend/web/js/transferlist.js | 1 - 11 files changed, 293 insertions(+), 81 deletions(-) create mode 100644 console/migrations/m151021_204300_alter__table__shopping_cart__alter__column__id_sale__to__id_transfer.php rename frontend/views/product/{_sold_items.php => _user_cart.php} (74%) diff --git a/common/models/ShoppingCart.php b/common/models/ShoppingCart.php index 5af2b9b..5349e7d 100644 --- a/common/models/ShoppingCart.php +++ b/common/models/ShoppingCart.php @@ -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]); + } } diff --git a/common/models/Transfer.php b/common/models/Transfer.php index 037bfbc..7c4b6cb 100644 --- a/common/models/Transfer.php +++ b/common/models/Transfer.php @@ -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 [ diff --git a/console/migrations/m151021_204300_alter__table__shopping_cart__alter__column__id_sale__to__id_transfer.php b/console/migrations/m151021_204300_alter__table__shopping_cart__alter__column__id_sale__to__id_transfer.php new file mode 100644 index 0000000..d22b463 --- /dev/null +++ b/console/migrations/m151021_204300_alter__table__shopping_cart__alter__column__id_sale__to__id_transfer.php @@ -0,0 +1,30 @@ +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() + { + } + */ +} diff --git a/frontend/controllers/ProductController.php b/frontend/controllers/ProductController.php index 3202a6e..c135d35 100644 --- a/frontend/controllers/ProductController.php +++ b/frontend/controllers/ProductController.php @@ -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) { @@ -97,16 +98,18 @@ class ProductController extends Controller } } - $userTransfers = Transfer::modelsToArray( Transfer::readUserSoldTransfers($user) ); + $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; + } + + } diff --git a/frontend/models/ProductSaleForm.php b/frontend/models/ProductSaleForm.php index f49502c..a71254f 100644 --- a/frontend/models/ProductSaleForm.php +++ b/frontend/models/ProductSaleForm.php @@ -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() ){ - $this->saveSale(); - $this->saveTransfer(); - $this->saveProduct(); - $this->appendToUserCart(); - $this->appendToCustomerCart(); + $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; + + $this->sale = Sale::createSale($this->account, $this->discount, $this->currency, $this->count, $this->product ); 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); } } diff --git a/frontend/views/product/_customer_cart.php b/frontend/views/product/_customer_cart.php index b5b5dd6..3ccf07d 100644 --- a/frontend/views/product/_customer_cart.php +++ b/frontend/views/product/_customer_cart.php @@ -33,4 +33,4 @@ use yii\bootstrap\Html; - 'btn_paid', 'class' => 'btn btn-primary' ]) ?> \ No newline at end of file + 'btn_pay_customer_cart', 'class' => 'btn btn-primary' ]) ?> \ No newline at end of file diff --git a/frontend/views/product/_sale_form.php b/frontend/views/product/_sale_form.php index 3c23e63..ebd9850 100644 --- a/frontend/views/product/_sale_form.php +++ b/frontend/views/product/_sale_form.php @@ -69,7 +69,8 @@ $discountOptions = mkOptions( ArrayHelper::map($discounts, 'id_discount', 'name' ] ); ?> - + +
@@ -104,7 +105,11 @@ $discountOptions = mkOptions( ArrayHelper::map($discounts, 'id_discount', 'name' 'btn btn-success', 'id' => 'btn_sell_append'] );?>
- 'btn btn-success', 'id' => 'btn_add_to_customer_cart'] );?> + card) ){ + echo Html::a(Yii::t("frontend/product","Write up"),null,['class' => 'btn btn-success', 'id' => 'btn_add_to_customer_cart'] ); + } + ?>
\ No newline at end of file diff --git a/frontend/views/product/_sold_items.php b/frontend/views/product/_user_cart.php similarity index 74% rename from frontend/views/product/_sold_items.php rename to frontend/views/product/_user_cart.php index fddf34c..6b095ba 100644 --- a/frontend/views/product/_sold_items.php +++ b/frontend/views/product/_user_cart.php @@ -3,10 +3,10 @@ use yii\bootstrap\Html; ?> - +

-
+
@@ -33,3 +33,4 @@ use yii\bootstrap\Html; + 'btn_pay_user_cart', 'class' => 'btn btn-primary' ]) ?> \ No newline at end of file diff --git a/frontend/views/product/sale.php b/frontend/views/product/sale.php index 0173e78..66476a4 100644 --- a/frontend/views/product/sale.php +++ b/frontend/views/product/sale.php @@ -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).');' ); render('_sale_form' ,['model' =>$model , 'lookupModel' =>$lookupModel, 'currencies' => $currencies, 'accounts' => $accounts,'discounts' => $discounts ]) ?>
- render('_customer_cart' ) ?> - -

- render('_sold_items' ) ?> - 'btn_paid', 'class' => 'btn btn-primary' ]) ?> - - - + + render('_customer_cart' ) ?> + + render('_user_cart' ) ?>
diff --git a/frontend/web/js/product.sell.js b/frontend/web/js/product.sell.js index b58e6db..c7f5055 100644 --- a/frontend/web/js/product.sell.js +++ b/frontend/web/js/product.sell.js @@ -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,36 +395,60 @@ 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.user_cart = response.transfers; + refreshUserCart(); + $.notify(app.defaults.message_paid, { 'type' : 'success' }); + } + } + } + ); + }); + } + 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.sold_items= response.transfers; - refreshSoldItemsList(); - $.notify(app.defaults.message_paid, { 'type' : '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 }); } diff --git a/frontend/web/js/transferlist.js b/frontend/web/js/transferlist.js index 465cc7d..7c48c49 100644 --- a/frontend/web/js/transferlist.js +++ b/frontend/web/js/transferlist.js @@ -136,7 +136,6 @@ $.widget( "fitness.transferList", { self._refresh(); }, }; - // base this._super(key, value);