add customer cart

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

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() ){
$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);
}
}