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' ] ); ?> - + +