Amélioration de la fonction de recherche de ActiveRecordCommon.dev
@@ -80,6 +80,14 @@ class CommandeController extends BackendController | |||
if (!Yii::$app->user->isGuest) | |||
$id_etablissement = Yii::$app->user->identity->id_etablissement; | |||
$orders = Order::searchAll([ | |||
'date' => $date, | |||
'date_delete' => 'NULL' | |||
], | |||
[ | |||
'orderby' => 'commentaire_point_vente ASC, user.nom ASC' | |||
]) ; | |||
$commandes = Commande::findBy([ | |||
'date' => $date, | |||
'date_delete' => 'NULL', |
@@ -40,54 +40,94 @@ namespace common\components ; | |||
class ActiveRecordCommon extends \yii\db\ActiveRecord | |||
{ | |||
public static function findByGeneric($class, $pk, $with = [], $join_with = [], | |||
$params = [], $conditions = [], $orderby = '', $limit = 0) { | |||
const SEARCH_ALL = 'all' ; | |||
const SEARCH_ONE = 'one' ; | |||
const SEARCH_COUNT = 'count' ; | |||
/** | |||
* Méthode générique de recherche utilisée pour tous les modèles. Elle a | |||
* pour but de construire la requête et de retourner le résultat. | |||
* | |||
* @param array $params | |||
* @param array $options | |||
* @return mixed | |||
* @throws NotFoundHttpException | |||
*/ | |||
public static function search($params = [], $options = []) | |||
{ | |||
$class = get_called_class() ; | |||
$records = $class::find() ; | |||
$default_options = $class::defaultOptionsSearch() ; | |||
$options = array_merge($default_options, $options) ; | |||
$pk = $class::primaryKey() ; | |||
$pk = $class::tableName().'.'.$pk[0] ; | |||
if (!isset($params[$options['attribute_id_producer']]) && !Yii::$app->user->isGuest) { | |||
$params[$options['attribute_id_producer']] = Producer::getCurrent(); | |||
} | |||
if(!isset($options['type_search'])) { | |||
$options['type_search'] = self::SEARCH_ALL ; | |||
} | |||
$records = $options['class']::find() ; | |||
// With | |||
if(is_array($with) && count($with)) { | |||
$records = $records->with($with) ; | |||
if(is_array($options['with']) && count($options['with'])) { | |||
$records = $records->with($options['with']) ; | |||
} | |||
// Join with | |||
if(is_array($join_with) && count($join_with)) { | |||
$records = $records->joinWith($join_with) ; | |||
if(is_array($options['join_with']) && count($options['join_with'])) { | |||
$records = $records->joinWith($options['join_with']) ; | |||
} | |||
// Conditions | |||
if(is_array($conditions)) { | |||
if(count($conditions)) { | |||
foreach($conditions as $condition) { | |||
$records = $records->andWhere($condition); | |||
if(isset($options['conditions'])) { | |||
if(is_array($options['conditions'])) { | |||
if(count($options['conditions'])) { | |||
foreach($options['conditions'] as $condition) { | |||
$records = $records->andWhere($condition); | |||
} | |||
} | |||
} | |||
} | |||
else { | |||
if(strlen($conditions)) { | |||
$records = $records->andWhere($conditions); | |||
else { | |||
if(strlen($options['conditions'])) { | |||
$records = $records->andWhere($options['conditions']); | |||
} | |||
} | |||
} | |||
// Paramètres | |||
if(is_array($params) && count($params)) { | |||
foreach($params as $key => $val) { | |||
if(strpos($key, '.') === false) { | |||
unset($params[$key]) ; | |||
$key = $class::tableName().'.'.$key ; | |||
$params[$key] = $val ; | |||
} | |||
$records = $records->andWhere([$key => $val]); | |||
} | |||
} | |||
if(!isset($params[$pk])) { | |||
// Orderby | |||
if (isset($orderby) && strlen($orderby)) { | |||
$records = $records->orderBy($orderby); | |||
if (isset($options['orderby']) && strlen($options['orderby'])) { | |||
$records = $records->orderBy($options['orderby']); | |||
} | |||
// Limit | |||
if (isset($limit) && is_numeric($limit) && $limit > 0) { | |||
$records = $records->limit($limit); | |||
if (isset($options['limit']) && is_numeric($options['limit']) | |||
&& $options['limit'] > 0) | |||
{ | |||
$records = $records->limit($options['limit']); | |||
} | |||
} | |||
if(isset($params[$pk])) { | |||
if($options['type_search'] == self::SEARCH_ALL) { | |||
return $records->all(); | |||
} | |||
elseif($options['type_search'] == self::SEARCH_ONE) { | |||
$record = $records->one(); | |||
if($record) { | |||
return $record ; | |||
@@ -97,10 +137,61 @@ class ActiveRecordCommon extends \yii\db\ActiveRecord | |||
'Le modèle '.$class.' #'.((int) $params[$pk]).' n\'a pas été trouvé.'); | |||
} | |||
} | |||
else { | |||
$records = $records->all(); | |||
return $records ; | |||
elseif($options['type_search'] == self::SEARCH_COUNT) { | |||
return $records->count() ; | |||
} | |||
} | |||
/** | |||
* Recherche un enregistrement. | |||
* | |||
* @param array $params | |||
* @param array $options | |||
* @return mixed | |||
*/ | |||
public static function searchOne($params = [], $options = []) | |||
{ | |||
$options['type_search'] = self::SEARCH_ONE ; | |||
return self::searchDispatch($params, $options) ; | |||
} | |||
/** | |||
* Recherche tous les enregistrements. | |||
* | |||
* @param array $params | |||
* @param array $options | |||
* @return mixed | |||
*/ | |||
public static function searchAll($params = [], $options = []) | |||
{ | |||
$options['type_search'] = self::SEARCH_ALL ; | |||
return self::searchDispatch($params, $options) ; | |||
} | |||
/** | |||
* Recherche et compte le nombre de résultats. | |||
* | |||
* @param array $params | |||
* @param array $options | |||
* @return integer | |||
*/ | |||
public static function searchCount($params = [], $options = []) | |||
{ | |||
$options['type_search'] = self::SEARCH_COUNT ; | |||
return self::searchDispatch($params, $options) ; | |||
} | |||
/** | |||
* Appelle la méthode 'search' de la classe appellante. | |||
* | |||
* @param array $params | |||
* @param array $options | |||
* @return mixed | |||
*/ | |||
public static function searchDispatch($params = [], $options = []) | |||
{ | |||
$class = get_called_class() ; | |||
return $class::search($params, $options) ; | |||
} | |||
} |
@@ -1,283 +0,0 @@ | |||
<?php | |||
/** | |||
Copyright La boîte à pain (2018) | |||
contact@laboiteapain.net | |||
Ce logiciel est un programme informatique servant à aider les producteurs | |||
à distribuer leur production en circuits courts. | |||
Ce logiciel est régi par la licence CeCILL soumise au droit français et | |||
respectant les principes de diffusion des logiciels libres. Vous pouvez | |||
utiliser, modifier et/ou redistribuer ce programme sous les conditions | |||
de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA | |||
sur le site "http://www.cecill.info". | |||
En contrepartie de l'accessibilité au code source et des droits de copie, | |||
de modification et de redistribution accordés par cette licence, il n'est | |||
offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons, | |||
seule une responsabilité restreinte pèse sur l'auteur du programme, le | |||
titulaire des droits patrimoniaux et les concédants successifs. | |||
A cet égard l'attention de l'utilisateur est attirée sur les risques | |||
associés au chargement, à l'utilisation, à la modification et/ou au | |||
développement et à la reproduction du logiciel par l'utilisateur étant | |||
donné sa spécificité de logiciel libre, qui peut le rendre complexe à | |||
manipuler et qui le réserve donc à des développeurs et des professionnels | |||
avertis possédant des connaissances informatiques approfondies. Les | |||
utilisateurs sont donc invités à charger et tester l'adéquation du | |||
logiciel à leurs besoins dans des conditions permettant d'assurer la | |||
sécurité de leurs systèmes et ou de leurs données et, plus généralement, | |||
à l'utiliser et l'exploiter dans les mêmes conditions de sécurité. | |||
Le fait que vous puissiez accéder à cet en-tête signifie que vous avez | |||
pris connaissance de la licence CeCILL, et que vous en avez accepté les | |||
termes. | |||
*/ | |||
namespace common\models; | |||
use Yii; | |||
use common\models\Etablissement; | |||
use common\models\PointVente; | |||
use common\models\PointVenteUser; | |||
use common\models\Commande; | |||
use common\models\CommandeProduit; | |||
/** | |||
* This is the model class for table "commande_auto". | |||
* | |||
* @property integer $id | |||
* @property integer $id_user | |||
* @property integer $id_etablissement | |||
* @property integer $id_point_vente | |||
* @property string $date_debut | |||
* @property string $date_fin | |||
* @property integer $lundi | |||
* @property integer $mardi | |||
* @property integer $mercredi | |||
* @property integer $jeudi | |||
* @property integer $vendredi | |||
* @property integer $samedi | |||
* @property integer $dimanche | |||
* @property integer $periodicite_semaine | |||
* @property string $username | |||
* @property string $paiement_automatique | |||
*/ | |||
class CommandeAuto extends \yii\db\ActiveRecord | |||
{ | |||
/** | |||
* @inheritdoc | |||
*/ | |||
public static function tableName() | |||
{ | |||
return 'commande_auto'; | |||
} | |||
/** | |||
* @inheritdoc | |||
*/ | |||
public function rules() | |||
{ | |||
return [ | |||
[['id_etablissement', 'id_point_vente'], 'required'], | |||
[['id_user', 'id_etablissement', 'id_point_vente', 'lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche', 'periodicite_semaine'], 'integer'], | |||
[['paiement_automatique'], 'boolean'], | |||
[['date_debut', 'date_fin', 'username'], 'safe'], | |||
]; | |||
} | |||
/** | |||
* @inheritdoc | |||
*/ | |||
public function attributeLabels() | |||
{ | |||
return [ | |||
'id' => 'ID', | |||
'id_user' => 'Utilisateur', | |||
'id_etablissement' => 'Etablissement', | |||
'id_point_vente' => 'Point de vente', | |||
'date_debut' => 'Date de début', | |||
'date_fin' => 'Date de fin', | |||
'lundi' => 'Lundi', | |||
'mardi' => 'Mardi', | |||
'mercredi' => 'Mercredi', | |||
'jeudi' => 'Jeudi', | |||
'vendredi' => 'Vendredi', | |||
'samedi' => 'Samedi', | |||
'dimanche' => 'Dimanche', | |||
'periodicite_semaine' => 'Périodicité', | |||
'paiement_automatique' => 'Paiement automatique' | |||
]; | |||
} | |||
/* | |||
* Relations | |||
*/ | |||
public function getUser() | |||
{ | |||
return $this->hasOne(User::className(), ['id' => 'id_user']); | |||
} | |||
public function getEtablissement() | |||
{ | |||
return $this->hasOne(Etablissement::className(), ['id' => 'id_etablissement']); | |||
} | |||
public function getPointVente() | |||
{ | |||
return $this->hasOne(PointVente::className(), ['id' => 'id_point_vente']); | |||
} | |||
public function getCommandeAutoProduit() | |||
{ | |||
return $this->hasMany(CommandeAutoProduit::className(), ['id_commande_auto' => 'id'])->with('produit'); | |||
} | |||
/** | |||
* Retourne les commandes récurrentes pour une date donnée. | |||
* | |||
* @param string $date | |||
* @return array | |||
*/ | |||
public static function getAll($date) | |||
{ | |||
$date = date('Y-m-d', strtotime($date)); | |||
// commandes auto | |||
$commandes_auto = self::find() | |||
->with('commandeAutoProduit') | |||
->where(['id_etablissement' => Yii::$app->user->identity->id_etablissement]) | |||
->all(); | |||
$arr_commandes_auto = []; | |||
foreach ($commandes_auto as $c) { | |||
// vérif dates | |||
if ($date >= $c->date_debut && | |||
(!$c->date_fin || $date <= $c->date_fin)) | |||
{ | |||
// périodicite | |||
$nb_jours = (strtotime($date) - strtotime($c->date_debut)) / (24 * 60 * 60); | |||
if ($nb_jours % ($c->periodicite_semaine * 7) < 7) { | |||
// jour de la semaine | |||
$jour = date('N', strtotime($date)); | |||
switch ($jour) { | |||
case 1 : $jour = 'lundi'; | |||
break; | |||
case 2 : $jour = 'mardi'; | |||
break; | |||
case 3 : $jour = 'mercredi'; | |||
break; | |||
case 4 : $jour = 'jeudi'; | |||
break; | |||
case 5 : $jour = 'vendredi'; | |||
break; | |||
case 6 : $jour = 'samedi'; | |||
break; | |||
case 7 : $jour = 'dimanche'; | |||
break; | |||
} | |||
if ($c->$jour) { | |||
$arr_commandes_auto[] = $c; | |||
} | |||
} | |||
} | |||
} | |||
return $arr_commandes_auto; | |||
} | |||
/** | |||
* Ajoute les commandes pour une date donnée à partir des commnandes | |||
* récurrentes. | |||
* | |||
* @param string $date | |||
* @param boolean $force | |||
*/ | |||
public static function addAll($date, $force = false) | |||
{ | |||
// production | |||
$production = Production::findOne([ | |||
'date' => date('Y-m-d', strtotime($date)), | |||
'id_etablissement' => Yii::$app->user->identity->id_etablissement | |||
]); | |||
if ($production) { | |||
$count_commandes_prod = Commande::find() | |||
->where(['id_production' => $production->id]) | |||
->count(); | |||
if (!$count_commandes_prod || $force) { | |||
$commandes_auto = self::getAll($date); | |||
foreach ($commandes_auto as $c) { | |||
$c->add($date); | |||
} | |||
} | |||
} | |||
} | |||
/** | |||
* Ajoute la commande récurrente pour une date donnée. | |||
* | |||
* @param string $date | |||
*/ | |||
public function add($date) | |||
{ | |||
// production | |||
$production = Production::find() | |||
->where([ | |||
'date' => date('Y-m-d', strtotime($date)), | |||
'id_etablissement' => Yii::$app->user->identity->id_etablissement | |||
]) | |||
->with('productionProduit') | |||
->one(); | |||
if ($production && count($this->commandeAutoProduit)) { | |||
// commande | |||
$commande = new Commande; | |||
if (strlen($this->username)) { | |||
$commande->username = $this->username; | |||
$commande->id_user = 0; | |||
} else { | |||
$commande->id_user = $this->id_user; | |||
} | |||
$commande->date = date('Y-m-d H:i:s'); | |||
$commande->type = Commande::TYPE_AUTO; | |||
$commande->id_point_vente = $this->id_point_vente; | |||
$commande->id_production = $production->id; | |||
$commande->paiement_automatique = $this->paiement_automatique ; | |||
$point_vente_user = PointVenteUser::find() | |||
->where(['id_point_vente' => $this->id_point_vente, 'id_user' => $this->id_user]) | |||
->one(); | |||
if ($point_vente_user && strlen($point_vente_user->commentaire)) { | |||
$commande->commentaire_point_vente = $point_vente_user->commentaire; | |||
} | |||
$commande->save(); | |||
// produits | |||
$montant_total = 0; | |||
$produits_add = false; | |||
foreach ($this->commandeAutoProduit as $commande_auto_produit) { | |||
$commande_produit = new CommandeProduit; | |||
$commande_produit->id_commande = $commande->id; | |||
$commande_produit->id_produit = $commande_auto_produit->produit->id; | |||
$commande_produit->quantite = $commande_auto_produit->quantite; | |||
$commande_produit->prix = $commande_auto_produit->produit->prix; | |||
$commande_produit->save(); | |||
$produits_add = true; | |||
} | |||
if (!$produits_add) { | |||
$commande->delete(); | |||
} | |||
} | |||
} | |||
} |
@@ -44,7 +44,7 @@ use common\models\Producer; | |||
use common\components\ActiveRecordCommon ; | |||
/** | |||
* This is the model class for table "commande". | |||
* This is the model class for table "order". | |||
* | |||
* @property integer $id | |||
* @property integer $id_user | |||
@@ -52,13 +52,13 @@ use common\components\ActiveRecordCommon ; | |||
* @property string $date_update | |||
* @property integer $id_point_sale | |||
* @property integer $id_production | |||
* @property boolean $paiement_automatique | |||
* @property boolean $auto_payment | |||
*/ | |||
class Order extends ActiveRecordCommon | |||
{ | |||
var $amount = 0 ; | |||
var $paid_amount = 0 ; | |||
var $Weight = 0 ; | |||
var $weight = 0 ; | |||
const ORIGIN_AUTO = 'auto'; | |||
const ORIGIN_USER = 'user'; | |||
@@ -84,6 +84,34 @@ class Order extends ActiveRecordCommon | |||
{ | |||
return 'order'; | |||
} | |||
/** | |||
* @inheritdoc | |||
*/ | |||
public function rules() | |||
{ | |||
return [ | |||
[['id_user', 'date', 'id_point_sale', 'id_production'], 'required', 'message' => ''], | |||
[['id_user', 'id_point_sale', 'id_production'], 'integer'], | |||
[['auto_payment'], 'boolean'], | |||
[['date', 'date_update', 'comment', 'point_sale_comment'], 'safe'] | |||
]; | |||
} | |||
/** | |||
* @inheritdoc | |||
*/ | |||
public function attributeLabels() | |||
{ | |||
return [ | |||
'id' => 'ID', | |||
'id_user' => 'Id User', | |||
'date' => 'Date', | |||
'date_update' => 'Date de modification', | |||
'id_point_sale' => 'Point de vente', | |||
'id_production' => 'Date de production', | |||
]; | |||
} | |||
/* | |||
* Relations | |||
@@ -118,31 +146,18 @@ class Order extends ActiveRecordCommon | |||
} | |||
/** | |||
* @inheritdoc | |||
* Retourne les options de base nécessaires à la fonction de recherche. | |||
* | |||
* @return array | |||
*/ | |||
public function rules() | |||
{ | |||
public static function defaultOptionsSearch() { | |||
return [ | |||
[['id_user', 'date', 'id_point_sale', 'id_production'], 'required', 'message' => ''], | |||
[['id_user', 'id_point_sale', 'id_production'], 'integer'], | |||
[['payment_auto'], 'boolean'], | |||
[['date', 'date_update', 'comment', 'point_sale_comment'], 'safe'] | |||
]; | |||
} | |||
/** | |||
* @inheritdoc | |||
*/ | |||
public function attributeLabels() | |||
{ | |||
return [ | |||
'id' => 'ID', | |||
'id_user' => 'Id User', | |||
'date' => 'Date', | |||
'date_update' => 'Date de modification', | |||
'id_point_sale' => 'Point de vente', | |||
'id_production' => 'Date de production', | |||
]; | |||
'class' => 'Order', | |||
'with' => ['productOrder', 'creditHistory', 'pointSale'], | |||
'join_with' => ['production', 'user', 'user.userProducer'], | |||
'orderby' => 'order.date ASC', | |||
'attribute_id_producer' => 'production.id_producer' | |||
] ; | |||
} | |||
/** | |||
@@ -153,6 +168,8 @@ class Order extends ActiveRecordCommon | |||
{ | |||
$this->initAmount() ; | |||
$this->initPaidAmount() ; | |||
return $this ; | |||
} | |||
/** | |||
@@ -241,24 +258,24 @@ class Order extends ActiveRecordCommon | |||
*/ | |||
public function getDataJson() | |||
{ | |||
$order = Order::findBy(['order.id' => $this->id]) ; | |||
$order = Order::searchOne(['order.id' => $this->id]) ; | |||
$json_order = [] ; | |||
$jsonOrder = [] ; | |||
if($order) { | |||
$json_order = [ | |||
$jsonOrder = [ | |||
'products' => [], | |||
'amount' => $order->montant, | |||
'amount' => $order->amount, | |||
'str_amount' => $order->getAmount(self::TYPE_AMOUNT_TOTAL, true), | |||
'paid_amount' => $order->getPaidAmount(self::TYPE_AMOUNT_PAID), | |||
'comment' => $order->comment, | |||
]; | |||
foreach ($order->productOrder as $product_order) { | |||
$json_order['products'][$product_order->id_product] = $product_order->quantity; | |||
foreach ($order->productOrder as $productOrder) { | |||
$jsonOrder['products'][$productOrder->id_product] = $productOrder->quantity; | |||
} | |||
} | |||
return json_encode($json_order); | |||
return json_encode($jsonOrder); | |||
} | |||
/** | |||
@@ -270,18 +287,18 @@ class Order extends ActiveRecordCommon | |||
* @param integer $id_user | |||
* @param integer $id_user_action | |||
*/ | |||
public function saveCreditHistory($type, $amount, $id_producer, $id_user, $id_user_action) | |||
public function saveCreditHistory($type, $amount, $idProducer, $idUser, $idUserAction) | |||
{ | |||
$credit_history = new CreditHistorique; | |||
$credit_history->id_user = $this->id_user; | |||
$credit_history->id_order = $this->id; | |||
$credit_history->amount = $amount; | |||
$credit_history->type = $type; | |||
$credit_history->id_producer = $id_producer; | |||
$credit_history->id_user_action = $id_user_action; | |||
$credit_history->populateRelation('order', $this) ; | |||
$credit_history->populateRelation('user', User::find()->where(['id' => $this->id_user])->one()) ; | |||
$credit_history->save(); | |||
$creditHistory = new CreditHistory; | |||
$creditHistory->id_user = $this->id_user; | |||
$creditHistory->id_order = $this->id; | |||
$creditHistory->amount = $amount; | |||
$creditHistory->type = $type; | |||
$creditHistory->id_producer = $idProducer; | |||
$creditHistory->id_user_action = $idUserAction; | |||
$creditHistory->populateRelation('order', $this) ; | |||
$creditHistory->populateRelation('user', User::find()->where(['id' => $this->id_user])->one()) ; | |||
$creditHistory->save(); | |||
} | |||
/** | |||
@@ -413,28 +430,28 @@ class Order extends ActiveRecordCommon | |||
*/ | |||
public function getState() | |||
{ | |||
$order_delay = Producer::getConfig( | |||
$orderDelay = Producer::getConfig( | |||
'order_delay', | |||
$this->production->id_producer | |||
); | |||
$order_deadline = Producer::getConfig( | |||
$orderDeadline = Producer::getConfig( | |||
'order_deadline', | |||
$this->production->id_producer | |||
); | |||
$order_date = strtotime($this->production->date); | |||
$orderDate = strtotime($this->production->date); | |||
$today = strtotime(date('Y-m-d')); | |||
$today_hour = date('G'); | |||
$todayHour = date('G'); | |||
$nb_days = (int) (($date_order - $today) / (24 * 60 * 60)); | |||
$nbDays = (int) (($dateOrder - $today) / (24 * 60 * 60)); | |||
if ($nb_days <= 0) { | |||
return self::STATE_DELIVERED; | |||
} | |||
elseif ($nb_days >= $order_delay && | |||
($nb_days != $order_delay || | |||
($nb_days == $order_delay && $today_hour < $order_deadline))) | |||
elseif ($nbDays >= $orderDelay && | |||
($nbDays != $orderDelay || | |||
($nbDays == $orderDelay && $todayHour < $orderDeadline))) | |||
{ | |||
return self::STATE_OPEN; | |||
} | |||
@@ -449,26 +466,27 @@ class Order extends ActiveRecordCommon | |||
* @param boolean $with_label | |||
* @return string | |||
*/ | |||
public function getStrOrigin($with_label = false) | |||
public function getStrOrigin($withLabel = false) | |||
{ | |||
$class_label = ''; | |||
$classLabel = ''; | |||
$str = ''; | |||
if ($this->type == self::ORIGIN_USER) { | |||
$class_label = 'success'; | |||
$classLabel = 'success'; | |||
$str = 'Client'; | |||
} | |||
elseif ($this->type == self::ORIGIN_AUTO) { | |||
$class_label = 'default'; | |||
$classLabel = 'default'; | |||
$str = 'Auto'; | |||
} | |||
elseif ($this->type == self::ORIGIN_ADMIN) { | |||
$class_label = 'warning'; | |||
$classLabel = 'warning'; | |||
$str = 'Vous'; | |||
} | |||
if ($with_label) { | |||
return '<span class="label label-' . $class_label . '">' . $str . '</span>'; | |||
if ($withLabel) { | |||
return '<span class="label label-' . $classLabel . '">' | |||
. $str . '</span>'; | |||
} | |||
else { | |||
return $str; | |||
@@ -507,7 +525,10 @@ class Order extends ActiveRecordCommon | |||
] ; | |||
} | |||
$html = '<div class="small"><span class="'.$arr['class'].'"><span class="glyphicon glyphicon-'.$arr['glyphicon'].'"></span> '.$arr['str'].'</span> le <strong>'.date('d/m/Y à G\hi', strtotime($arr['date'])).'</strong></div>' ; | |||
$html = '<div class="small"><span class="'.$arr['class'].'">' | |||
. '<span class="glyphicon glyphicon-'.$arr['glyphicon'].'"></span> ' | |||
. $arr['str'].'</span> le <strong>' | |||
. date('d/m/Y à G\hi', strtotime($arr['date'])).'</strong></div>' ; | |||
return $html ; | |||
} | |||
@@ -532,12 +553,12 @@ class Order extends ActiveRecordCommon | |||
/** | |||
* Retourne la quantité d'un produit donné de plusieurs commandes. | |||
* | |||
* @param integer $id_produit | |||
* @param array $commandes | |||
* @param integer $idProduct | |||
* @param array $orders | |||
* | |||
* @return integer | |||
*/ | |||
public static function getProductQuantity($id_product, $orders) | |||
public static function getProductQuantity($idProduct, $orders) | |||
{ | |||
$quantity = 0; | |||
@@ -557,7 +578,7 @@ class Order extends ActiveRecordCommon | |||
} | |||
/** | |||
* Recherche des commandes. | |||
* Recherche et initialise des commandes. | |||
* | |||
* @param array $params | |||
* @param array $conditions | |||
@@ -565,22 +586,9 @@ class Order extends ActiveRecordCommon | |||
* @param integer $limit | |||
* @return array | |||
*/ | |||
public static function findBy($params = [], $conditions = [], $orderby = '', $limit = 0) | |||
public static function search($params = [], $options = []) | |||
{ | |||
if (!isset($params['production.id_producer']) && !Yii::$app->user->isGuest) { | |||
$params['production.id_producer'] = Producer::getCurrent(); | |||
} | |||
$orders = parent::findByGeneric( | |||
'Order', | |||
'order.id', | |||
['productOrder', 'creditHistory', 'pointSale'], | |||
['production', 'user', 'user.userProducer'], | |||
$params, | |||
$conditions, | |||
strlen($orderby) ? $orderby : 'date ASC', | |||
$limit | |||
) ; | |||
$orders = parent::search($params, $options) ; | |||
/* | |||
* Initialisation des commandes | |||
@@ -596,8 +604,7 @@ class Order extends ActiveRecordCommon | |||
} | |||
else { | |||
$order = $orders ; | |||
$order->init() ; | |||
return $order ; | |||
return $order->init() ; | |||
} | |||
return false ; |
@@ -39,7 +39,7 @@ termes. | |||
namespace common\models; | |||
use Yii; | |||
use common\models\Produit; | |||
use common\models\Product; | |||
/** | |||
* This is the model class for table "commande_auto_produit". | |||
@@ -49,7 +49,7 @@ use common\models\Produit; | |||
* @property integer $id_produit | |||
* @property double $quantite | |||
*/ | |||
class CommandeAutoProduit extends \yii\db\ActiveRecord | |||
class ProductSubscription extends \yii\db\ActiveRecord | |||
{ | |||
/** | |||
@@ -57,7 +57,7 @@ class CommandeAutoProduit extends \yii\db\ActiveRecord | |||
*/ | |||
public static function tableName() | |||
{ | |||
return 'commande_auto_produit'; | |||
return 'product_subscription'; | |||
} | |||
/** | |||
@@ -89,9 +89,9 @@ class CommandeAutoProduit extends \yii\db\ActiveRecord | |||
* Relations | |||
*/ | |||
public function getProduit() | |||
public function getProduct() | |||
{ | |||
return $this->hasOne(Produit::className(), ['id' => 'id_produit']); | |||
return $this->hasOne(Product::className(), ['id' => 'id_product']); | |||
} | |||
} |
@@ -0,0 +1,298 @@ | |||
<?php | |||
/** | |||
Copyright La boîte à pain (2018) | |||
contact@laboiteapain.net | |||
Ce logiciel est un programme informatique servant à aider les producteurs | |||
à distribuer leur production en circuits courts. | |||
Ce logiciel est régi par la licence CeCILL soumise au droit français et | |||
respectant les principes de diffusion des logiciels libres. Vous pouvez | |||
utiliser, modifier et/ou redistribuer ce programme sous les conditions | |||
de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA | |||
sur le site "http://www.cecill.info". | |||
En contrepartie de l'accessibilité au code source et des droits de copie, | |||
de modification et de redistribution accordés par cette licence, il n'est | |||
offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons, | |||
seule une responsabilité restreinte pèse sur l'auteur du programme, le | |||
titulaire des droits patrimoniaux et les concédants successifs. | |||
A cet égard l'attention de l'utilisateur est attirée sur les risques | |||
associés au chargement, à l'utilisation, à la modification et/ou au | |||
développement et à la reproduction du logiciel par l'utilisateur étant | |||
donné sa spécificité de logiciel libre, qui peut le rendre complexe à | |||
manipuler et qui le réserve donc à des développeurs et des professionnels | |||
avertis possédant des connaissances informatiques approfondies. Les | |||
utilisateurs sont donc invités à charger et tester l'adéquation du | |||
logiciel à leurs besoins dans des conditions permettant d'assurer la | |||
sécurité de leurs systèmes et ou de leurs données et, plus généralement, | |||
à l'utiliser et l'exploiter dans les mêmes conditions de sécurité. | |||
Le fait que vous puissiez accéder à cet en-tête signifie que vous avez | |||
pris connaissance de la licence CeCILL, et que vous en avez accepté les | |||
termes. | |||
*/ | |||
namespace common\models; | |||
use Yii; | |||
use common\models\Producer; | |||
use common\models\PointSale; | |||
use common\models\PointSaleUser; | |||
use common\models\Order; | |||
use common\models\ProductOrder; | |||
/** | |||
* This is the model class for table "commande_auto". | |||
* | |||
* @property integer $id | |||
* @property integer $id_user | |||
* @property integer $id_producer | |||
* @property integer $id_point_sale | |||
* @property string $date_begin | |||
* @property string $date_end | |||
* @property integer $monday | |||
* @property integer $tuesday | |||
* @property integer $wednesday | |||
* @property integer $thursday | |||
* @property integer $friday | |||
* @property integer $saturday | |||
* @property integer $sunday | |||
* @property integer $week_frequency | |||
* @property string $username | |||
* @property string $auto_payment | |||
*/ | |||
class Subscription extends ActiveRecordCommon | |||
{ | |||
/** | |||
* @inheritdoc | |||
*/ | |||
public static function tableName() | |||
{ | |||
return 'subscription'; | |||
} | |||
/** | |||
* @inheritdoc | |||
*/ | |||
public function rules() | |||
{ | |||
return [ | |||
[['id_producer', 'id_point_sale'], 'required'], | |||
[['id_user', 'id_producer', 'id_point_sale', 'monday', 'tuesday', | |||
'wednesday', 'thursday', 'friday', 'saterday', 'sunday', 'week_frequency'], 'integer'], | |||
[['auto_payment'], 'boolean'], | |||
[['date_begin', 'date_end', 'username'], 'safe'], | |||
]; | |||
} | |||
/** | |||
* @inheritdoc | |||
*/ | |||
public function attributeLabels() | |||
{ | |||
return [ | |||
'id' => 'ID', | |||
'id_user' => 'Utilisateur', | |||
'id_producer' => 'Etablissement', | |||
'id_point_sale' => 'Point de vente', | |||
'date_begin' => 'Date de début', | |||
'date_end' => 'Date de fin', | |||
'monday' => 'Lundi', | |||
'tuesday' => 'Mardi', | |||
'wednesday' => 'Mercredi', | |||
'thursday' => 'Jeudi', | |||
'friday' => 'Vendredi', | |||
'saterday' => 'Samedi', | |||
'sunday' => 'Dimanche', | |||
'week_frequency' => 'Périodicité', | |||
'auto_payment' => 'Paiement automatique' | |||
]; | |||
} | |||
/* | |||
* Relations | |||
*/ | |||
public function getUser() | |||
{ | |||
return $this->hasOne(User::className(), ['id' => 'id_user']); | |||
} | |||
public function getProducer() | |||
{ | |||
return $this->hasOne( | |||
Producer::className(), | |||
['id' => 'id_producer'] | |||
); | |||
} | |||
public function getPointSale() | |||
{ | |||
return $this->hasOne( | |||
PointSale::className(), | |||
['id' => 'id_point_sale'] | |||
); | |||
} | |||
public function getProductSubscription() | |||
{ | |||
return $this->hasMany( | |||
ProductSubscription::className(), | |||
['id_subscription' => 'id'] | |||
)->with('product'); | |||
} | |||
/** | |||
* Retourne les options de base nécessaires à la fonction de recherche. | |||
* | |||
* @return array | |||
*/ | |||
public static function defaultOptionsSearch() { | |||
return [ | |||
'class' => 'Subscription', | |||
'with' => ['producer', 'pointSale', 'productSubscription'], | |||
'join_with' => ['user'], | |||
'orderby' => 'user.name ASC', | |||
'attribute_id_producer' => 'subscription.id_producer' | |||
] ; | |||
} | |||
/** | |||
* Ajoute la commande pour une date donnée. | |||
* | |||
* @param string $date | |||
*/ | |||
public function add($date) | |||
{ | |||
// production | |||
$production = Production::searchOne([ | |||
'production.date' => date('Y-m-d', strtotime($date)) | |||
]) ; | |||
if ($production && count($this->productSubscription)) { | |||
// commande | |||
$order = new Order; | |||
if (strlen($this->username)) { | |||
$order->username = $this->username; | |||
$order->id_user = 0; | |||
} | |||
else { | |||
$order->id_user = $this->id_user; | |||
} | |||
$order->date = date('Y-m-d H:i:s'); | |||
$order->origin = Order::ORIGIN_AUTO; | |||
$order->id_point_sale = $this->id_point_sale; | |||
$order->id_production = $production->id; | |||
$order->auto_payment = $this->auto_payment ; | |||
$pointSaleUser = PointSaleUser::searchOne([ | |||
'id_point_sale' => $this->id_point_sale, | |||
'id_user' => $this->id_user | |||
]) ; | |||
if ($pointSaleUser && strlen($pointSaleUser->comment)) { | |||
$order->comment_point_sale = $pointSaleUser->comment; | |||
} | |||
$order->save(); | |||
// produits | |||
$amountTotal = 0; | |||
$productsAdd = false; | |||
foreach ($this->productSubscription as $productSubscription) { | |||
$productOrder = new ProductOrder; | |||
$productOrder->id_order = $order->id; | |||
$productOrder->id_product = $productSubscription->product->id; | |||
$productOrder->quantity = $productSubscription->quantity; | |||
$productOrder->price = $productSubscription->product->price; | |||
$productOrder->save(); | |||
$productsAdd = true; | |||
} | |||
if (!$products_add) { | |||
$order->delete(); | |||
} | |||
} | |||
} | |||
/** | |||
* Ajoute les commandes pour une date donnée à partir des abonnements. | |||
* | |||
* @param string $date | |||
* @param boolean $force | |||
*/ | |||
public static function addAll($date, $force = false) | |||
{ | |||
$production = Production::searchOne([ | |||
'date' => date('Y-m-d', strtotime($date)) | |||
]) ; | |||
if ($production) { | |||
$countOrdersProd = Order::searchCount([ | |||
Order::tableName().'.id_production' => $production->id | |||
]) ; | |||
if (!$countOrdersProd || $force) { | |||
$subscriptions = self::searchByDate($date); | |||
foreach ($subscriptions as $s) { | |||
$s->add($date); | |||
} | |||
} | |||
} | |||
} | |||
/** | |||
* Retourne les abonnements pour une date donnée. | |||
* | |||
* @param string $date | |||
* @return array | |||
*/ | |||
public static function searchByDate($date) | |||
{ | |||
$date = date('Y-m-d', strtotime($date)); | |||
$subscriptions = Subscription::searchAll() ; | |||
$arrSubscriptions = []; | |||
foreach ($subscriptions as $s) { | |||
// vérif dates | |||
if ($date >= $s->date_begin && | |||
(!$s->date_end || $date <= $s->date_end)) | |||
{ | |||
// périodicite | |||
$nbDays = (strtotime($date) - strtotime($s->date_begin)) / (24 * 60 * 60); | |||
if ($nbDays % ($c->week_frequency * 7) < 7) { | |||
// jour de la semaine | |||
$day = date('N', strtotime($date)); | |||
switch ($day) { | |||
case 1 : $day = 'monday'; | |||
break; | |||
case 2 : $day = 'tuesday'; | |||
break; | |||
case 3 : $day = 'wednesday'; | |||
break; | |||
case 4 : $day = 'thursday'; | |||
break; | |||
case 5 : $day = 'friday'; | |||
break; | |||
case 6 : $day = 'saturday'; | |||
break; | |||
case 7 : $day = 'sunday'; | |||
break; | |||
} | |||
if ($s->$day) { | |||
$arrSubscriptions[] = $s; | |||
} | |||
} | |||
} | |||
} | |||
return $arrSubscriptions; | |||
} | |||
} |
@@ -134,7 +134,7 @@ class SiteController extends FrontendController | |||
* @return mixed | |||
*/ | |||
public function actionIndex() | |||
{ | |||
{ | |||
return $this->render('index'); | |||
} | |||