82 lines
2.0 KiB
PHP
82 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace common\models;
|
|
|
|
use Yii;
|
|
|
|
/**
|
|
* This is the model class for table "shopping_cart".
|
|
*
|
|
* @property integer $id_shopping_cart
|
|
* @property integer $id_customer
|
|
* @property integer $id_transfer
|
|
*/
|
|
class ShoppingCart extends \yii\db\ActiveRecord
|
|
{
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
public static function tableName()
|
|
{
|
|
return 'shopping_cart';
|
|
}
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
public function rules()
|
|
{
|
|
return [
|
|
[['id_customer', 'id_transfer'], 'integer']
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
public function attributeLabels()
|
|
{
|
|
return [
|
|
'id_shopping_cart' => Yii::t('common/shopping-cart', 'Id Shopping Cart'),
|
|
'id_customer' => Yii::t('common/shopping-cart', 'Id Customer'),
|
|
'id_transfer' => Yii::t('common/shopping-cart', 'Id Transfer'),
|
|
];
|
|
}
|
|
|
|
public function getTransfer(){
|
|
return $this->hasOne(Transfer::className(), ['id_transfer' => 'id_transfer']);
|
|
}
|
|
public function getProduct(){
|
|
return $this->hasOne(Product::className(), ['id_product' => 'id_product'])->via('transfer');
|
|
}
|
|
|
|
/**
|
|
* @param $customer common\models\Customer
|
|
* */
|
|
public static function readCustomerCart($customer = null){
|
|
$transfers = [];
|
|
if ( isset($customer)){
|
|
$query = ShoppingCart::find()->andWhere( ['id_customer' => $customer->id_customer] );
|
|
$query->with('product');
|
|
$transfers = $query->all();
|
|
}
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|