163 lines
4.2 KiB
PHP
163 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace frontend\models;
|
|
|
|
use Yii;
|
|
use yii\base\Model;
|
|
use common\models\Card;
|
|
use common\models\Customer;
|
|
use common\models\Product;
|
|
use common\models\Transfer;
|
|
use yii\base\Object;
|
|
use common\models\Account;
|
|
use common\models\Discount;
|
|
use common\models\Currency;
|
|
use common\models\UserSoldItem;
|
|
|
|
/**
|
|
* ContactForm is the model behind the contact form.
|
|
*/
|
|
class ProductSaleForm extends Model
|
|
{
|
|
|
|
public $append_to_sold_list;
|
|
|
|
public $id_product;
|
|
public $count;
|
|
public $id_currency;
|
|
public $id_account;
|
|
public $id_discount;
|
|
public $comment;
|
|
public $barcode;
|
|
public $product_number;
|
|
public $sale_price;
|
|
public $product_name;
|
|
|
|
|
|
public $accounts;
|
|
public $currencies;
|
|
public $discounts;
|
|
|
|
public $product;
|
|
public $account;
|
|
public $currency;
|
|
public $discount;
|
|
|
|
public $transfer;
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
public function rules()
|
|
{
|
|
return [
|
|
[['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],
|
|
[['id_product' ], 'validateProduct'],
|
|
[['count' ], 'validateCount'],
|
|
[['id_currency' ], 'validateCurrency'],
|
|
[['id_account' ], 'validateAccount'],
|
|
[['id_discount' ], 'validateDiscount'],
|
|
[['id_discount' ], 'validateDiscount'],
|
|
];
|
|
}
|
|
|
|
|
|
public function validateProduct($attribute,$params){
|
|
$this->product = Product::findOne($this->id_product);
|
|
|
|
if ( !isset( $this->product ) ){
|
|
$this->addError($attribute, Yii::t('frontend/product', 'Product not found!'));
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public function validateCount($attribute,$params){
|
|
if ( $this->product != null ){
|
|
if ( $this->product->stock < $this->count ){
|
|
$this->addError($attribute, Yii::t('frontend/product', 'Stock {stock} lower then {count}!', [ 'count' => $this->count, 'stock' => $this->product->stock ] ));
|
|
}
|
|
}
|
|
}
|
|
public function validateCurrency($attribute,$params){
|
|
if ( isset($this->id_currency ) ){
|
|
$this->currency = Currency::findOne($this->id_currency);
|
|
if ( !isset( $this->currency ) ){
|
|
$this->addError($attribute,Yii::t('frontend/product', 'Currency not found') );
|
|
}
|
|
}
|
|
}
|
|
|
|
public function validateAccount($attribute,$params){
|
|
$this->account = Account::findOne($this->id_account);
|
|
}
|
|
|
|
public function validateDiscount($attribute,$params){
|
|
if ( isset( $this->id_discount ) ){
|
|
$this->discount = Discount::findOne($this->id_discount);
|
|
if ( !isset( $this->discount ) ){
|
|
$this->addError($attribute,Yii::t('frontend/product', 'Discount not found') );
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
public function attributeLabels()
|
|
{
|
|
return [
|
|
'verifyCode' => 'Verification Code',
|
|
];
|
|
}
|
|
|
|
|
|
public function save(){
|
|
if ( $this->validate() ){
|
|
$this->saveTransfer();
|
|
$this->saveProduct();
|
|
$this->appendToSoldList();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
protected function saveTransfer(){
|
|
|
|
$this->transfer = Transfer::createProductTransfer($this->account, $this->discount, $this->currency, $this->count, $this->product);
|
|
/*
|
|
*/
|
|
$this->transfer->status = Transfer::STATUS_PAID;
|
|
if ( isset($this->comment)){
|
|
$this->transfer->comment = $this->comment;
|
|
}
|
|
$this->transfer->id_user = Yii::$app->user->id;
|
|
$this->transfer->save();
|
|
}
|
|
|
|
protected function saveProduct(){
|
|
Product::sellProduct($this->product, $this->count);
|
|
$this->product->save();
|
|
}
|
|
|
|
public function isAppendToList(){
|
|
$result = false;
|
|
if ( isset( $this->append_to_sold_list ) && $this->append_to_sold_list == 'append' ){
|
|
$result = true;
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
public function appendToSoldList(){
|
|
if ( $this->isAppendToList() ){
|
|
$item = new UserSoldItem();
|
|
$item->id_transfer = $this->transfer->id_transfer;
|
|
$item->id_user = Yii::$app->user->id;
|
|
$item->save(false);
|
|
}
|
|
}
|
|
}
|