|
|
|
|
|
|
|
|
|
|
|
|
|
|
use Yii; |
|
|
use Yii; |
|
|
use common\models\User; |
|
|
use common\models\User; |
|
|
use common\models\Etablissement; |
|
|
|
|
|
|
|
|
use common\models\Producer; |
|
|
use yii\base\Model; |
|
|
use yii\base\Model; |
|
|
use common\models\UserEtablissement; |
|
|
|
|
|
|
|
|
use common\models\UserProducer; |
|
|
use common\helpers\Password; |
|
|
use common\helpers\Password; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
|
|
|
|
|
|
public $phone; |
|
|
public $phone; |
|
|
public $is_producer; |
|
|
public $is_producer; |
|
|
public $siret; |
|
|
public $siret; |
|
|
public $nom_magasin; |
|
|
|
|
|
public $code_postal; |
|
|
|
|
|
public $ville; |
|
|
|
|
|
public $id_etablissement; |
|
|
|
|
|
public $option_client_boulanger; |
|
|
|
|
|
|
|
|
public $name_producer; |
|
|
|
|
|
public $postcode; |
|
|
|
|
|
public $city; |
|
|
|
|
|
public $id_producer; |
|
|
|
|
|
public $option_user_producer; |
|
|
public $cgv; |
|
|
public $cgv; |
|
|
public $code; |
|
|
public $code; |
|
|
public $type; |
|
|
public $type; |
|
|
public $prix_libre ; |
|
|
|
|
|
|
|
|
public $free_price ; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* @inheritdoc |
|
|
* @inheritdoc |
|
|
|
|
|
|
|
|
['email', 'required', 'message' => 'Champs obligatoire'], |
|
|
['email', 'required', 'message' => 'Champs obligatoire'], |
|
|
['email', 'email'], |
|
|
['email', 'email'], |
|
|
['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'Cet email est déjà utilisé'], |
|
|
['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'Cet email est déjà utilisé'], |
|
|
[['nom', 'prenom', 'telephone'], 'required', 'message' => 'Champs obligatoire'], |
|
|
|
|
|
[['nom', 'prenom', 'telephone', 'option_client_boulanger'], 'string', 'min' => 2, 'max' => 255], |
|
|
|
|
|
|
|
|
[['name', 'lastname', 'phone'], 'required', 'message' => 'Champs obligatoire'], |
|
|
|
|
|
[['name', 'lastname', 'phone', 'option_user_producer'], 'string', 'min' => 2, 'max' => 255], |
|
|
['password', 'required', 'message' => 'Champs obligatoire'], |
|
|
['password', 'required', 'message' => 'Champs obligatoire'], |
|
|
['password', 'string', 'min' => 6, 'tooShort' => 'Votre mot de passe doit contenir au moins 6 caractères'], |
|
|
['password', 'string', 'min' => 6, 'tooShort' => 'Votre mot de passe doit contenir au moins 6 caractères'], |
|
|
['is_boulanger', 'boolean'], |
|
|
|
|
|
|
|
|
['is_producer', 'boolean'], |
|
|
['cgv', 'boolean'], |
|
|
['cgv', 'boolean'], |
|
|
['cgv', function($attribute, $params) { |
|
|
['cgv', function($attribute, $params) { |
|
|
$cgv = $this->$attribute; |
|
|
$cgv = $this->$attribute; |
|
|
|
|
|
|
|
|
if ($this->option_client_boulanger == 'boulanger' && !$cgv) { |
|
|
|
|
|
|
|
|
if ($this->option_user_producer == 'producer' && !$cgv) { |
|
|
$this->addError($attribute, 'Vous devez accepter les conditions générales de vente pour vous inscrire.'); |
|
|
$this->addError($attribute, 'Vous devez accepter les conditions générales de vente pour vous inscrire.'); |
|
|
} |
|
|
} |
|
|
}], |
|
|
}], |
|
|
['code_postal', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) { |
|
|
|
|
|
return $model->option_client_boulanger == 'boulanger'; |
|
|
|
|
|
|
|
|
['postcode', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) { |
|
|
|
|
|
return $model->option_user_producer == 'producer'; |
|
|
}, 'whenClient' => "function (attribute, value) { |
|
|
}, 'whenClient' => "function (attribute, value) { |
|
|
return $('#option-boulanger').prop('checked') ; |
|
|
|
|
|
|
|
|
return $('#option-producer').prop('checked') ; |
|
|
}"], |
|
|
}"], |
|
|
['ville', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) { |
|
|
|
|
|
return $model->option_client_boulanger == 'boulanger'; |
|
|
|
|
|
|
|
|
['city', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) { |
|
|
|
|
|
return $model->option_user_producer == 'producer'; |
|
|
}, 'whenClient' => "function (attribute, value) { |
|
|
}, 'whenClient' => "function (attribute, value) { |
|
|
return $('#option-boulanger').prop('checked') ; |
|
|
|
|
|
|
|
|
return $('#option-producer').prop('checked') ; |
|
|
}"], |
|
|
}"], |
|
|
['nom_magasin', 'string'], |
|
|
|
|
|
['nom_magasin', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) { |
|
|
|
|
|
return $model->option_client_boulanger == 'boulanger'; |
|
|
|
|
|
|
|
|
['name_producer', 'string'], |
|
|
|
|
|
['name_producer', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) { |
|
|
|
|
|
return $model->option_user_producer == 'producer'; |
|
|
}, 'whenClient' => "function (attribute, value) { |
|
|
}, 'whenClient' => "function (attribute, value) { |
|
|
return $('#option-boulanger').prop('checked') ; |
|
|
|
|
|
|
|
|
return $('#option-producer').prop('checked') ; |
|
|
}"], |
|
|
}"], |
|
|
['type', 'string'], |
|
|
['type', 'string'], |
|
|
['type', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) { |
|
|
['type', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) { |
|
|
return $model->option_client_boulanger == 'boulanger'; |
|
|
|
|
|
|
|
|
return $model->option_user_producer == 'producer'; |
|
|
}, 'whenClient' => "function (attribute, value) { |
|
|
}, 'whenClient' => "function (attribute, value) { |
|
|
return $('#option-boulanger').prop('checked') ; |
|
|
|
|
|
|
|
|
return $('#option-producer').prop('checked') ; |
|
|
}"], |
|
|
}"], |
|
|
['siret', 'string'], |
|
|
['siret', 'string'], |
|
|
['siret', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) { |
|
|
['siret', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) { |
|
|
return $model->option_client_boulanger == 'boulanger'; |
|
|
|
|
|
|
|
|
return $model->option_user_producer == 'producer'; |
|
|
}, 'whenClient' => "function (attribute, value) { |
|
|
}, 'whenClient' => "function (attribute, value) { |
|
|
return $('#option-boulanger').prop('checked') ; |
|
|
|
|
|
|
|
|
return $('#option-producer').prop('checked') ; |
|
|
}"], |
|
|
}"], |
|
|
['siret', function($attribute, $params) { |
|
|
['siret', function($attribute, $params) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (($sum % 10) != 0) |
|
|
if (($sum % 10) != 0) |
|
|
$this->addError($attribute, 'Numéro SIRET invalide'); |
|
|
$this->addError($attribute, 'Numéro SIRET invalide'); |
|
|
}], |
|
|
}], |
|
|
['id_etablissement', 'integer'], |
|
|
|
|
|
['id_etablissement', function($attribute, $params) { |
|
|
|
|
|
if ($this->id_etablissement) { |
|
|
|
|
|
$etablissement = Etablissement::findOne($this->id_etablissement); |
|
|
|
|
|
if (!$etablissement) { |
|
|
|
|
|
|
|
|
['id_producer', 'integer'], |
|
|
|
|
|
['id_producer', function($attribute, $params) { |
|
|
|
|
|
if ($this->id_producer) { |
|
|
|
|
|
$producer = Producer::findOne($this->id_producer); |
|
|
|
|
|
if (!$producer) { |
|
|
$this->addError($attribute, 'Ce producteur n\'existe pas.'); |
|
|
$this->addError($attribute, 'Ce producteur n\'existe pas.'); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
}], |
|
|
}], |
|
|
['code', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) { |
|
|
['code', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) { |
|
|
$etablissement = Etablissement::findOne($this->id_etablissement); |
|
|
|
|
|
if ($etablissement) { |
|
|
|
|
|
return strlen($etablissement->code); |
|
|
|
|
|
|
|
|
$producer = Producer::findOne($this->id_producer); |
|
|
|
|
|
if ($producer) { |
|
|
|
|
|
return strlen($producer->code); |
|
|
} else { |
|
|
} else { |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
}], |
|
|
}], |
|
|
['code', function($attribute, $params) { |
|
|
['code', function($attribute, $params) { |
|
|
$code = $this->$attribute; |
|
|
$code = $this->$attribute; |
|
|
$etablissement = Etablissement::findOne($this->id_etablissement); |
|
|
|
|
|
|
|
|
$producer = Producer::findOne($this->id_producer); |
|
|
|
|
|
|
|
|
if ($etablissement && strtolower(trim($code)) != strtolower(trim($etablissement->code))) { |
|
|
|
|
|
|
|
|
if ($producer && strtolower(trim($code)) != strtolower(trim($producer->code))) { |
|
|
$this->addError($attribute, 'Code incorrect'); |
|
|
$this->addError($attribute, 'Code incorrect'); |
|
|
} |
|
|
} |
|
|
}], |
|
|
}], |
|
|
['prix_libre', 'number'], |
|
|
|
|
|
|
|
|
['free_price', 'number'], |
|
|
]; |
|
|
]; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'username' => 'Identifiant', |
|
|
'username' => 'Identifiant', |
|
|
'password' => 'Mot de passe', |
|
|
'password' => 'Mot de passe', |
|
|
'rememberMe' => 'Se souvenir de moi', |
|
|
'rememberMe' => 'Se souvenir de moi', |
|
|
'prenom' => 'Prénom', |
|
|
|
|
|
'telephone' => 'Téléphone', |
|
|
|
|
|
'is_boulanger' => "Je suis professionnel et souhaite mettre en place un système de réservation dans mon établissement", |
|
|
|
|
|
'nom_magasin' => 'Nom de l\'établissement', |
|
|
|
|
|
|
|
|
'name' => 'Prénom', |
|
|
|
|
|
'lastname' => 'Nom', |
|
|
|
|
|
'phone' => 'Téléphone', |
|
|
|
|
|
'is_producer' => "Je suis professionnel et souhaite mettre en place un système de réservation dans mon établissement", |
|
|
|
|
|
'name_producer' => 'Nom du producteur', |
|
|
'siret' => 'Numéro SIRET', |
|
|
'siret' => 'Numéro SIRET', |
|
|
'code_postal' => 'Code postal', |
|
|
|
|
|
'ville' => 'Commune', |
|
|
|
|
|
'id_etablissement' => 'Producteur', |
|
|
|
|
|
'type' => 'Type d\'établissement', |
|
|
|
|
|
'prix_libre' => 'Prix libre' |
|
|
|
|
|
|
|
|
'postcode' => 'Code postal', |
|
|
|
|
|
'city' => 'Commune', |
|
|
|
|
|
'id_producer' => 'Producteur', |
|
|
|
|
|
'type' => 'Type de producteur', |
|
|
|
|
|
'free_price' => 'Prix libre' |
|
|
]; |
|
|
]; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$user = new User(); |
|
|
$user = new User(); |
|
|
$user->username = $this->email; |
|
|
$user->username = $this->email; |
|
|
$user->email = $this->email; |
|
|
$user->email = $this->email; |
|
|
$user->nom = $this->nom; |
|
|
|
|
|
$user->prenom = $this->prenom; |
|
|
|
|
|
$user->telephone = $this->telephone; |
|
|
|
|
|
$user->confiance = 1; |
|
|
|
|
|
|
|
|
$user->name = $this->name; |
|
|
|
|
|
$user->lastname = $this->lastname; |
|
|
|
|
|
$user->phone = $this->phone; |
|
|
|
|
|
|
|
|
if ($this->option_client_boulanger == 'boulanger') { |
|
|
|
|
|
|
|
|
if ($this->option_user_producer == 'producer') { |
|
|
// etablissement |
|
|
// etablissement |
|
|
$etablissement = new Etablissement; |
|
|
|
|
|
$etablissement->nom = $this->nom_magasin; |
|
|
|
|
|
$etablissement->type = $this->type; |
|
|
|
|
|
$etablissement->siret = $this->siret; |
|
|
|
|
|
$etablissement->code_postal = $this->code_postal; |
|
|
|
|
|
$etablissement->ville = $this->ville; |
|
|
|
|
|
$etablissement->heure_limite_commande = 20; |
|
|
|
|
|
$etablissement->delai_commande = 1; |
|
|
|
|
|
$etablissement->gratuit = 1; |
|
|
|
|
|
$etablissement->prix_libre = (float) abs($this->prix_libre); |
|
|
|
|
|
$etablissement->slug = \common\helpers\Url::slugify($this->nom_magasin) ; |
|
|
|
|
|
|
|
|
$producer = new Producer; |
|
|
|
|
|
$producer->name = $this->name_producer; |
|
|
|
|
|
$producer->type = $this->type; |
|
|
|
|
|
$producer->siret = $this->siret; |
|
|
|
|
|
$producer->postcode = $this->postcode; |
|
|
|
|
|
$producer->city = $this->city; |
|
|
|
|
|
$producer->order_deadline = 20; |
|
|
|
|
|
$producer->order_delay = 1; |
|
|
|
|
|
$producer->free_price = (float) abs($this->free_price); |
|
|
|
|
|
$producer->slug = \common\helpers\Url::slugify($this->name_producer) ; |
|
|
|
|
|
|
|
|
// génération d'un code |
|
|
// génération d'un code |
|
|
do { |
|
|
do { |
|
|
$code = Password::generate(); |
|
|
$code = Password::generate(); |
|
|
$etablissement->code = $code; |
|
|
|
|
|
} while (Etablissement::findOne(['code' => $code])); |
|
|
|
|
|
|
|
|
$producer->code = $code; |
|
|
|
|
|
} while (Producer::findOne(['code' => $code])); |
|
|
|
|
|
|
|
|
$etablissement->save(); |
|
|
|
|
|
|
|
|
$producer->save(); |
|
|
|
|
|
|
|
|
// user |
|
|
// user |
|
|
$user->id_etablissement = $etablissement->id; |
|
|
|
|
|
|
|
|
$user->id_producer = $producer->id; |
|
|
$user->status = User::STATUS_PRODUCER; |
|
|
$user->status = User::STATUS_PRODUCER; |
|
|
|
|
|
|
|
|
// envoi d'un email à l'administrateur pour le prévenir |
|
|
// envoi d'un email à l'administrateur pour le prévenir |
|
|
Yii::$app->mailer->compose( |
|
|
Yii::$app->mailer->compose( |
|
|
[ |
|
|
[ |
|
|
'html' => 'new-boulanger-html', |
|
|
|
|
|
'text' => 'new-boulanger-text' |
|
|
|
|
|
|
|
|
'html' => 'new-producer-html', |
|
|
|
|
|
'text' => 'new-producer-text' |
|
|
], [ |
|
|
], [ |
|
|
'etablissement' => $etablissement |
|
|
|
|
|
|
|
|
'producer' => $producer |
|
|
]) |
|
|
]) |
|
|
->setTo(Yii::$app->params['adminEmail']) |
|
|
->setTo(Yii::$app->params['adminEmail']) |
|
|
->setFrom([Yii::$app->params['adminEmail'] => 'La boîte à pain']) |
|
|
->setFrom([Yii::$app->params['adminEmail'] => 'La boîte à pain']) |
|
|
|
|
|
|
|
|
if ($user->save()) { |
|
|
if ($user->save()) { |
|
|
|
|
|
|
|
|
// on ajoute l'établissement sélectionnée par l'user en favoris |
|
|
// on ajoute l'établissement sélectionnée par l'user en favoris |
|
|
if ($this->option_client_boulanger == 'client') { |
|
|
|
|
|
if ($this->id_etablissement) { |
|
|
|
|
|
$etablissement = Etablissement::find()->where(['id' => $this->id_etablissement])->one(); |
|
|
|
|
|
if ($etablissement) { |
|
|
|
|
|
Etablissement::addUser($user->id, $this->id_etablissement) ; |
|
|
|
|
|
|
|
|
if ($this->option_user_producer == 'producer') { |
|
|
|
|
|
if ($this->id_producer) { |
|
|
|
|
|
$producer = Producer::find()->where(['id' => $this->id_producer])->one(); |
|
|
|
|
|
if ($producer) { |
|
|
|
|
|
Producer::addUser($user->id, $this->id_producer) ; |
|
|
|
|
|
|
|
|
// envoi d'un email à l'utilisateur |
|
|
// envoi d'un email à l'utilisateur |
|
|
Yii::$app->mailer->compose( |
|
|
Yii::$app->mailer->compose( |
|
|
|
|
|
|
|
|
], |
|
|
], |
|
|
[ |
|
|
[ |
|
|
'user' => $user, |
|
|
'user' => $user, |
|
|
'etablissement' => $etablissement |
|
|
|
|
|
|
|
|
'producer' => $producer |
|
|
]) |
|
|
]) |
|
|
->setTo($user->email) |
|
|
->setTo($user->email) |
|
|
->setFrom([Yii::$app->params['adminEmail'] => 'La boîte à pain']) |
|
|
->setFrom([Yii::$app->params['adminEmail'] => 'La boîte à pain']) |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if ($this->option_client_boulanger == 'boulanger') { |
|
|
|
|
|
$etab_user = new UserEtablissement; |
|
|
|
|
|
$etab_user->id_etablissement = $etablissement->id; |
|
|
|
|
|
$etab_user->id_user = $user->id; |
|
|
|
|
|
$etab_user->credit = 0; |
|
|
|
|
|
$etab_user->actif = 1; |
|
|
|
|
|
$etab_user->save(); |
|
|
|
|
|
|
|
|
if ($this->option_user_producer == 'producer') { |
|
|
|
|
|
$userProducer = new UserProducer; |
|
|
|
|
|
$userProducer->id_producer = $producer->id; |
|
|
|
|
|
$userProducer->id_user = $user->id; |
|
|
|
|
|
$userProducer->credit = 0; |
|
|
|
|
|
$userProducer->active = 1; |
|
|
|
|
|
$userProducer->save(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return $user; |
|
|
return $user; |