use common\models\Subscription; | use common\models\Subscription; | ||||
use common\helpers\Price; | use common\helpers\Price; | ||||
use common\models\PointSaleDistribution; | use common\models\PointSaleDistribution; | ||||
use common\models\UserProducer; | |||||
use DateTime; | use DateTime; | ||||
class DistributionController extends BackendController | class DistributionController extends BackendController | ||||
if(!$deliveryNote) { | if(!$deliveryNote) { | ||||
$deliveryNote = new DeliveryNote; | $deliveryNote = new DeliveryNote; | ||||
$deliveryNote->name = 'Bon de livraison ' . $firstOrder->pointSale->name . ' (' . date('d/m/Y', strtotime($firstOrder->distribution->date)) . ')'; | $deliveryNote->name = 'Bon de livraison ' . $firstOrder->pointSale->name . ' (' . date('d/m/Y', strtotime($firstOrder->distribution->date)) . ')'; | ||||
$deliveryNote->id_user = 1; | |||||
$deliveryNote->id_producer = GlobalParam::getCurrentProducerId(); | $deliveryNote->id_producer = GlobalParam::getCurrentProducerId(); | ||||
if($firstOrder->pointSale->id_user) { | |||||
$deliveryNote->id_user = $firstOrder->pointSale->id_user; | |||||
} | |||||
else { | |||||
$user = new User ; | |||||
$user->type = User::TYPE_LEGAL_PERSON ; | |||||
$user->name_legal_person = $firstOrder->pointSale->name ; | |||||
$user->address = $firstOrder->pointSale->address ; | |||||
$user->id_producer = 0 ; | |||||
$user->setPassword(Password::generate()); | |||||
$user->generateAuthKey(); | |||||
$user->email = '' ; | |||||
if (!strlen($user->email)) { | |||||
$user->username = 'inconnu@opendistrib.net'; | |||||
} | |||||
$user->save() ; | |||||
$userProducer = new UserProducer; | |||||
$userProducer->id_user = $user->id; | |||||
$userProducer->id_producer = GlobalParam::getCurrentProducerId(); | |||||
$userProducer->credit = 0; | |||||
$userProducer->active = 1; | |||||
$userProducer->save(); | |||||
$firstOrder->pointSale->id_user = $user->id ; | |||||
$firstOrder->pointSale->save() ; | |||||
$deliveryNote->id_user = $user->id; | |||||
} | |||||
if(!isset($user)) { | |||||
$user = User::searchOne([ | |||||
'id' => $deliveryNote->id_user | |||||
]) ; | |||||
} | |||||
$deliveryNote->address = $user->getFullAddress() ; | |||||
$deliveryNote->save(); | $deliveryNote->save(); | ||||
} | } | ||||
else { | else { |
if ($model->load(Yii::$app->request->post()) && $model->validate() && YII_ENV != 'demo') { | if ($model->load(Yii::$app->request->post()) && $model->validate() && YII_ENV != 'demo') { | ||||
// save use | |||||
// save user | |||||
$password = Password::generate(); | $password = Password::generate(); | ||||
$model->id_producer = 0 ; | $model->id_producer = 0 ; | ||||
$model->setPassword($password); | $model->setPassword($password); |
]) | ]) | ||||
->dropDownList( | ->dropDownList( | ||||
ArrayHelper::map($usersArray, 'user_id', function ($model) { | ArrayHelper::map($usersArray, 'user_id', function ($model) { | ||||
return $model['lastname'] . ' ' . $model['name']; | |||||
if(isset($model['name_legal_person']) && strlen($model['name_legal_person'])) { | |||||
return $model['name_legal_person'] ; | |||||
} | |||||
else { | |||||
return $model['lastname'] . ' ' . $model['name']; | |||||
} | |||||
}), | }), | ||||
[ | [ | ||||
'@change' => 'changeUser', | '@change' => 'changeUser', |
'attribute' => 'username', | 'attribute' => 'username', | ||||
'label' => 'Nom', | 'label' => 'Nom', | ||||
'value' => function($model) { | 'value' => function($model) { | ||||
return $model['lastname'].' '.$model['name'] ; | |||||
if(isset($model['name_legal_person'])) { | |||||
return $model['name_legal_person'] ; | |||||
} | |||||
else { | |||||
return $model['lastname'].' '.$model['name'] ; | |||||
} | |||||
} | } | ||||
], | ], | ||||
[ | [ |
public function getFullAddress() | public function getFullAddress() | ||||
{ | { | ||||
$address = ''; | $address = ''; | ||||
$address .= $this->lastname . ' ' . $this->name . "\n"; | |||||
if(isset($this->lastname) && isset($this->name) && strlen($this->lastname) && strlen($this->name)) { | |||||
$address .= $this->lastname . ' ' . $this->name . "\n"; | |||||
} | |||||
if(isset($this->name_legal_person) && strlen($this->name_legal_person)) { | |||||
$address .= $this->name_legal_person. "\n"; | |||||
} | |||||
$address .= $this->address; | $address .= $this->address; | ||||
return $address; | return $address; | ||||
} | } |
. '`user`.mail_distribution_sunday, ' | . '`user`.mail_distribution_sunday, ' | ||||
. '`user`.id_producer, ' | . '`user`.id_producer, ' | ||||
. '`user`.date_last_connection, ' | . '`user`.date_last_connection, ' | ||||
. '`user`.name_legal_person, ' | |||||
. '(SELECT COUNT(*) FROM `order` WHERE `user`.id = `order`.id_user) AS count_orders'); | . '(SELECT COUNT(*) FROM `order` WHERE `user`.id = `order`.id_user) AS count_orders'); | ||||
$dataProvider = new ActiveDataProvider([ | $dataProvider = new ActiveDataProvider([ | ||||
$query->andFilterWhere([ | $query->andFilterWhere([ | ||||
'or', | 'or', | ||||
['like', 'user.lastname', $this->username], | ['like', 'user.lastname', $this->username], | ||||
['like', 'user.name', $this->username] | |||||
['like', 'user.name', $this->username], | |||||
['like', 'user.name_legal_person', $this->username], | |||||
]); | ]); | ||||
return $dataProvider; | return $dataProvider; |