12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472 |
- <?php
- /** CLIENT **/
- //Récuperation d'une liste de client
- Action::register('client_client_search',function(&$response){
- global $myUser,$_,$myFirm;
- //on verifie que l'utilisateur a un read complet sur les client ou acces a au moins une fiche
- if(!$myUser->can('client','read') && !$myUser->can('client_sheet','read',0,array('contains'=>true))) throw new Exception("Permission non accordée");
- require_once(__DIR__.SLASH.'Client.class.php');
- // OPTIONS DE RECHERCHE, A ACTIVER POUR UNE RECHERCHE AVANCEE
- $query = 'SELECT DISTINCT main.*,phone.value as phone,mail.value as mail,parent.id parentId,parent.label parentLabel,address.street,address.complement,address.zip,address.city,'.Firm::joinString('fi').','.Dictionary::joinString('category').' FROM {{table}} main
- LEFT JOIN '.Client::tableName().' parent ON main.parent=parent.id
- LEFT JOIN '.Firm::tableName().' fi ON main.firm=fi.id
- LEFT JOIN '.Dictionary::tableName().' category ON main.category=category.id
- LEFT JOIN '.Contact::tableName().' phone ON phone.uid=main.id AND phone.scope="client" AND phone.type="mobile"
- LEFT JOIN '.Contact::tableName().' mail ON mail.uid=main.id AND mail.scope="client" AND mail.type="professional_mail"
- LEFT JOIN '.Address::tableName().' address ON address.uid=main.id AND address.scope="client" AND address.type="'.Address::MAIN.'"
- WHERE 1';
- $data = array();
- $allowedFields = array();
- $fields = array();
- $hasDynamicPlugin = $myFirm->has_plugin('fr.core.dynamicform') || ($myFirm->id==-1 && Plugin::is_active('fr.core.dynamicform'));
- //ajout des champs dynamiques dans la recherche
- if($hasDynamicPlugin){
- Plugin::need('dynamicform/DynamicForm');
- //le premier argument contient le slug du formulaire contenant toutes les colonnes possibles, le second les colonnes non choisies,la requete, l'alias si nécessaire
- $dynamicFields = DynamicForm::list('client-sheet-custom',array('firm'=>$myFirm->id));
- DynamicForm::query_column_add($dynamicFields,$query,'main');
- //On récupère les types de champs qui possèdent une propriété onLoad afin de l'appliquer si on a un champ dynamique
- $fieldTypes = array();
- $fieldCustom = array();
- foreach($dynamicFields as $field){
- $fieldTypes[$field['slug']] = $field['type'];
- $fieldCustom[$field['slug']] = $field;
- $allowedFields[] = 'dynamicField_'.$field['id'].'.value';
- }
- }
- //selection des colonnes à récuperer
- // le premier argument contient toutes les colonnes possibles, le second les colonnes non choisies
- column_secure_query(Client::fields(),$_,$query);
- //Recherche simple
- if(!empty($_['filters']['keyword'])){
- $query .= ' AND ( (main.type=? AND (main.label LIKE ? OR main.pseudonym LIKE ?) ) OR (main.type=? AND (main.name LIKE ? OR main.firstname LIKE ?)) OR main.comment LIKE ?)';
- $data[] = 'firm';
- $data[] = '%'.$_['filters']['keyword'].'%';
- $data[] = '%'.$_['filters']['keyword'].'%';
- $data[] = 'individual';
- $data[] = '%'.$_['filters']['keyword'].'%';
- $data[] = '%'.$_['filters']['keyword'].'%';
- $data[] = '%'.$_['filters']['keyword'].'%';
- }
- if(isset($_['selected'])){
- $query .= ' AND main.id IN ('.implode(',',array_fill(0, count($_['selected']), '?')).')';
- foreach ($_['selected'] as $selected)
- $data[] = $selected;
- }
- $query .= ' AND main.state != ?';
- $data[] = Client::INACTIVE;
- //on vérifie les droits spéciaux qui peuvent bypasser
- //récuperation des droits spéciaux sur les fiches
- //on initialise a 0 pour eviter le plantage si aucun droit
- $rights = array(0);
- if(isset($myUser->rights['client_sheet'][0])) $rights += $myUser->rights['client_sheet'][0];
- if(isset($myUser->rights['client_sheet'][$myFirm->id])) $rights += $myUser->rights['client_sheet'][$myFirm->id];
- //tableau des id deletables
- $deletableSheets = array();
- //var_dump($rights);
- foreach($rights as $uid => $right){
- $data[] = $uid;
- if(isset($right['delete']) && $right['delete']===true) $deletableSheets[] = $uid;
- }
- $query .= ' AND (main.id IN('.implode(',',array_fill(0, count($rights), '?')).') OR main.firm IN(?,?) ) ';
- $data[] = 0;
- $data[] = $myFirm->id;
- Plugin::callHook('client_search',array(&$query,&$data));
- //Recherche avancée
- if(isset($_['filters']['advanced'])){
- //traitement filtre custom pour recherche par contact interne
- $_['filters']['advanced'] = filter_alteration($_['filters']['advanced'],'internal_contact',function($filter) use(&$query,&$data){
- global $_;
- switch($filter['operator']){
- case '=':
- case '!=':
- $query.=' AND main.id '.($filter['operator']=='!='?' NOT ':'').' IN (SELECT uid FROM '.ContactPerson::tablename().' cp WHERE cp.scope="client" AND type="user" AND account=?) ';
- $data[] = $filter['value'][0];
- break;
- case 'null':
- case 'not null':
- $query.=' AND (SELECT count(id) FROM '.ContactPerson::tablename().' cp WHERE cp.scope="client" AND type="user" AND uid=main.id) '.($filter['operator']=='null'?'=':'!=').' 0 ';
- break;
- }
- return null;
- });
- filter_secure_query($_['filters']['advanced'],array_merge($allowedFields,array('main.id','main.label','address.city','address.zip','main.type','main.condition','main.creator','main.job','main.code','main.siret','main.created','mail.value','phone.value','main.comment','fi.id')),$query,$data);
- }
- //Tri des colonnes
- if(isset($_['sort'])) sort_secure_query($_['sort'],array_merge($allowedFields,array('main.label','main.condition','main.type','main.state','mail.value','address.city','phone.value','fi.label')),$query,$data);
- //Pagination
- $itemPerPage = 20;
- if($_['export'] == 'true') $itemPerPage = 5000;
- $response['pagination'] = Client::paginate($itemPerPage,(!empty($_['page'])?$_['page']:0),$query,$data,'main');
- $clients = Client::staticQuery($query,$data,true,1);
- $ids = array();
- foreach($clients as $client)
- $ids[]= $client->id;
- $subsites = array();
- if(!empty($ids)){
- foreach(Client::loadAll(array('parent:IN'=>$ids,'state'=>Client::ACTIVE)) as $subsite){
- if(!isset($subsites[$subsite->parent])) $subsites[$subsite->parent] = array();
- $subsites[$subsite->parent][] = $subsite->toArray();
- }
- }
- foreach($clients as $client){
- $row = $client->toArray();
- $row['deletable'] = true;
- if($myFirm->id!=$client->firm && !in_array($client->id, $deletableSheets)) $row['deletable'] = false;
- if(!$myUser->can('client','delete')) $row['deletable'] = false;
- $row['phone'] = $client->foreign('phone');
- $row['mail'] = $client->foreign('mail');
- $row['category'] = $client->join('category');
- $firm = $client->firm!= 0 ? $client->join('firm') : new Firm();
- $row['firm'] = $firm->toArray();
- $mainAddress = new Address();
- $mainAddress->street = $client->foreign('street');
- $mainAddress->complement = $client->foreign('complement');
- $mainAddress->zip = $client->foreign('zip');
- $mainAddress->city = $client->foreign('city');
- $row['address'] = $mainAddress->toArray();
- $row['address']['mapurl'] = $mainAddress->mapUrl();
- $row['type'] = $client->type();
- $row['condition'] = $client->condition();
- $row['logo'] = $client->logo();
- $row['holding'] = false;
- $row['affiliate'] = false;
- $row['meta'] = $client->meta();
- if($client->parent != 0){
- $holding = new Client();
- $holding->id = $client->foreign('parentId');
- $holding->label = $client->foreign('parentLabel');
- if($_['export'] == 'true'){
- $row['holding'] = $holding->toArray();
- }else{
- $row['parent'] = $holding->label;
- }
- }
- if($_['export'] == 'true'){
- $row['created'] = date('d-m-Y',$row['created']);
- $row['updated'] = date('d-m-Y',$row['updated']);
- $row['state'] = $row['state'] == Client::ACTIVE ? 'Actif' : 'Supprimé' ;
- }
- //Gestion des champs dynamiques
- if($hasDynamicPlugin){
- DynamicForm::search_values($row,array(
- 'slugs'=> $client->foreign(),
- 'types'=> $fieldTypes,
- 'scope' => 'client',
- 'force-raw' => $_['export'] == 'true'
- ));
- }
- if(isset($subsites[$client->id])){
- $row['affiliate']['rows'] = $subsites[$client->id];
- $row['affiliate']['count'] = count($subsites[$client->id]);
- }
- $response['rows']['client-'.$client->id] = $row;
- }
- /* Mode export */
- if($_['export'] == 'true'){
- $fieldsMapping = array();
- foreach (Client::fields(false) as $key => $value)
- $fieldsMapping[$value['label']] = $key ;
- if($myFirm->has_plugin('fr.core.dynamicform')){
- foreach ($fieldCustom as $slug => $value)
- $fieldsMapping[$value['label']] = $slug;
- }
- $fieldsMapping['E-mail'] = 'mail';
- $fieldsMapping['Téléphone'] = 'phone';
- $stream = Excel::exportArray($response['rows'],$fieldsMapping,'Export');
- File::downloadStream($stream,'export-clients-'.date('d-m-Y').'.xlsx');
- exit();
- }
- });
- Action::register('client_client_mail_copy',function(&$response){
- global $myUser,$_;
- if(!$myUser->can('client','read')) throw new Exception('Vous n\'avez pas la permission de copier cet e-mail');
- if(count($_['ids'])==0) return;
- $response['mails'] = array();
- foreach(Contact::loadAll(array('scope'=>'client','uid:IN'=>$_['ids'],'type'=>'professional_mail')) as $contact){
- $response['mails'][] = $contact->value;
- }
- });
- //Fusion de deux clients
- Action::register('client_client_merge',function(&$response){
- global $myUser,$_,$conf;
- User::check_access('client','configure');
- require_once(__DIR__.SLASH.'Client.class.php');
- $response['rows'] = array();
- if(empty($_['left'])) throw new Exception($conf->get('client_label_singular')." de base non définis");
- if(empty($_['right'])) throw new Exception($conf->get('client_label_singular')." a fusionner non définis");
- if($_['right'] == $_['left']) throw new Exception("Vous ne pouvez pas fusionner un ".$conf->get('client_label_singular')." avec lui même");
- $left = Client::getById($_['left']);
- $right = Client::getById($_['right']);
- if(empty($left->id)) throw new Exception($conf->get('client_label_singular')." de base introuvable");
- if(empty($right->id)) throw new Exception($conf->get('client_label_singular')." de a fusionner introuvable");
- if(empty($_['keep'])) throw new Exception("Tableau d'écrasement non définis");
- $response['logs'] = $left->merge($right,$_['keep']);
- });
- //Vérifie les duplicatas possibles avec les clients existants
- Action::register('client_client_check_duplicate',function(&$response){
- global $myUser,$_;
- if(!$myUser->can('client','read')) throw new Exception("Permission non accordée");
- require_once(__DIR__.SLASH.'Client.class.php');
- $response['rows'] = array();
- if(empty($_['label'])) return $response;
- if(!empty($_['id'])) return $response;
- $response['rows'] = Client::check_duplicate($_['field'],$_['label'], isset($_['id'])?$_['id']:null );
- });
- Action::register('client_relation_add',function(&$response){
- global $myUser,$_,$conf;
- if(!$myUser->can('client','read')) throw new Exception("Permission non accordée");
- require_once(__DIR__.SLASH.'Client.class.php');
- if(empty($_['id'])) throw new Exception($conf->get('client_label_singular')." non spécifié");
- if(empty($_['relation'])) throw new Exception("Relation non spécifiée");
- if(empty($_['level'])) throw new Exception("Type de relation non spécifiée");
- $site = Client::getById($_['id']);
- $relation = Client::getById($_['relation']);
- if(!$site || $site->id == 0) throw new Exception($conf->get('client_label_singular')." introuvable en base de donnée");
- if(!$relation || $relation->id == 0) throw new Exception($conf->get('client_label_singular')." à lier introuvable en base de donnée");
- if($_['level'] == 'holding'){
- if($relation->parent == $site->id) throw new Exception("Impossible d'ajouter en holding un établissement déjà filiale");
- $site->parent = $relation->id;
- $site->save();
- }else{
- if($site->parent == $relation->id) throw new Exception("Impossible d'ajouter en filiale un établissement déjà holding");
- $relation->parent = $site->id;
- $relation->save();
- }
- });
- Action::register('client_relation_delete',function(&$response){
- global $myUser,$_,$conf;
- User::check_access('client','read');
- require_once(__DIR__.SLASH.'Client.class.php');
- if(empty($_['id'])) throw new Exception($conf->get('client_label_singular')." non spécifié");
- if(empty($_['relation'])) throw new Exception("Relation non spécifiée");
- if(empty($_['level'])) throw new Exception("Type de relation non spécifiée");
- switch($_['level']){
- case 'holding':
- $site = Client::getById($_['id']);
- $relation = Client::getById($_['relation']);
- if(!$site || $site->id == 0) throw new Exception($conf->get('client_label_singular')." introuvable en base de donnée");
- if(!$relation || $relation->id == 0) throw new Exception($conf->get('client_label_singular')." à lier introuvable en base de donnée");
- $site->parent = null;
- $site->save();
- break;
- case 'subsite':
- $site = Client::getById($_['id']);
- $relation = Client::getById($_['relation']);
- if(!$site || $site->id == 0) throw new Exception($conf->get('client_label_singular')." introuvable en base de donnée");
- if(!$relation || $relation->id == 0) throw new Exception($conf->get('client_label_singular')." à lier introuvable en base de donnée");
- $relation->parent = null;
- $relation->save();
- break;
- case 'memberof':
- ContactPerson::remove($_['relation']);
- break;
- }
- });
- //Retourne la liste des relations (filiales et holding)
- Action::register('client_relation_search',function(&$response){
- global $myUser,$_,$myFirm;
- if(!$myUser->can('client','read') && $_['client']!=0 && !$myUser->can('client_sheet','read',$_['client'])) throw new Exception("Permission non accordée sur cette fiche");
- require_once(__DIR__.SLASH.'Client.class.php');
- $response['rows'] = array();
- if(empty($_['client'])) return $response['rows'];
- $filters = array('parent'=>$_['client'],'firm:IN'=>array(0,$myFirm->id),'state'=>Client::ACTIVE);
- //si l'user n'est pas en acces tout client, on ne lui affiche que les relation client auxquelles il a acces
- if(!$myUser->can('client','read')){
- $allowedSheets = array();
- foreach($myUser->rights['client_sheet'] as $firm => $rights){
- foreach($rights as $uid=>$right){
- if($right['read']) $allowedSheets[] = $uid;
- }
- }
- $filters['id:IN'] = $allowedSheets;
- }
- $clients = Client::loadAll($filters,array('label'));
- foreach($clients as $client){
- $row = $client->toArray();
- $addresses = $client->addresses('global');
- $row['address'] = isset($addresses[0]) ? $addresses[0] : new Address();
- $row['level'] = 'subsite';
- $response['rows'][] = $row;
- }
- $client = Client::getById($_['client']);
- if(!empty($client->parent)){
- $holdingObject = $client->holding();
- $holding = $holdingObject->toArray();
- $addresses = $holdingObject->addresses('global');
- $holding['address'] = isset($addresses[0]) ? $addresses[0] : new Address();
- $holding['level'] = 'holding';
- $response['rows'][] = $holding;
- }
- foreach(Client::staticQuery('SELECT c.*,cp.id as relation_id FROM '.ContactPerson::tablename().' cp LEFT JOIN {{table}} c ON c.id=cp.uid WHERE cp.account=? AND cp.scope=? and cp.type=?',array($_['client'],'client','client'),true) as $client){
- $row = $client->toArray();
- $addresses = $client->addresses('global');
- $row['address'] = isset($addresses[0]) ? $addresses[0] : new Address();
- $row['level'] = 'memberof';
- $row['id'] = $client->foreign('relation_id');
- $response['rows'][] = $row;
- }
- });
- Action::register('client_api_search',function(&$response){
- global $myUser,$_,$conf;
- if(!$myUser->can('client','read')) throw new Exception("Permission non accordée");
- require_once(__DIR__.SLASH.'Client.class.php');
- if(empty($conf->get('client_api_search_url'))) throw new Exception("L'api de recherche ".$conf->get('client_label_singular')." n'est pas configurée, merci de contacter un administrateur");
- //ex : https://entreprise.data.gouv.fr/api/sirene/v1/full_text/toxgen
- $url = str_replace('{{label}}',rawurlencode($_['label']),$conf->get('client_api_search_url'));
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
- $stream = curl_exec($ch);
- $info = curl_getinfo($ch);
- curl_close($ch);
- $json = json_decode($stream,true);
- if(!$json) throw new Exception("Impossible de contacter l'api de recherche ".$conf->get('client_label_singular'));
- if($info['http_code'] == 404 || !isset($json['etablissement']) || count($json['etablissement']) ==0 ) throw new Exception($conf->get('client_label_singular')." non trouvé avec ce libellé.");
- foreach ($json['etablissement'] as $row) {
- $line = array();
- $line['label'] = $row['nom_raison_sociale'];
- $line['siren'] = $row['siren'];
- $line['siret'] = $row['siret'];
- $line['created'] = $row['date_creation'];
- if(strlen($line['created'])== 8){
- $line['created'] = substr($line['created'], -2).'/'.substr($line['created'], -4,2).'/'.substr($line['created'], 0,4);
- }else{
- $line['created'] = '';
- }
- $line['street'] = $row['numero_voie'].' '.$row['type_voie'].' '.$row['libelle_voie'];
- $line['city'] = $row['libelle_commune'];
- $line['zip'] = $row['code_postal'];
- $line['size'] = $row['categorie_entreprise'];
- $line['status'] = $row['libelle_nature_juridique_entreprise'];
- $line['employees'] = $row['tranche_effectif_salarie'];
- $line['job'] = $row['libelle_activite_principale'];
- $response['rows'][] = $line;
- }
- });
- Action::register('client_tab_load',function(&$response){
- global $myUser,$_;
- if(!$myUser->can('client','read') && $_['id']!=0 && !$myUser->can('client_sheet','read',$_['id'])) throw new Exception("Permission non accordée sur cette fiche");
- $tab = $_['tab'];
- Plugin::callHook('client_page',array($tab));
- exit();
- });
- //Ajout ou modification d'élément client
- Action::register('client_client_save',function(&$response){
- global $myUser, $_, $conf,$myFirm;
- User::check_access('client','edit');
- require_once(__DIR__.SLASH.'Client.class.php');
- $item = Client::provide();
- $oldItem = clone $item;
- if($item->id!=0 && $item->firm!=$myFirm->id && !$myUser->can('client_sheet','edit',$item->id) ) throw new Exception("Vous n'avez pas la permission pour editer cette fiche");
- if(empty($_['label'])) throw new Exception("Libellé obligatoire");
- $item->label = $_['label'];
- if(isset($_['firstname']) && !empty($_['firstname'])) $item->firstname = ucfirst(mb_strtolower(trim($_['firstname'])));
- if(isset($_['name']) && !empty($_['name'])) $item->name = mb_strtoupper(trim($_['name']));
- if(isset($_['rate']) && !empty($_['rate'])) $item->rate = $_['rate'];
- if(isset($_['siret'])) $item->siret = $_['siret'];
- if(isset($_['comment'])) $item->comment = trim($_['comment']);
- if(isset($_['parent']) && !empty($_['parent'])) $item->parent = $_['parent'];
- if(isset($_['type']) && !empty($_['type'])) $item->type = $_['type'];
- if(!empty($conf->get('client_condition')) && $conf->get('client_condition')!='both'){
- $item->condition = $conf->get('client_condition');
- }else{
- if(isset($_['condition']) && !empty($_['condition'])) $item->condition = $_['condition'];
- }
- if(isset($_['category'])) $item->category = $_['category'];
- if(isset($_['job'])) $item->job = $_['job'];
- if(isset($_['pseudonym'])) $item->pseudonym = $_['pseudonym'];
- $item->state = Client::ACTIVE;
- $item->firm = $myFirm->id;
- Plugin::callHook('client_save',array(&$item,$oldItem));
- $item->save();
- Plugin::callHook('client_saved',array(&$item,$oldItem));
- //Gestion des champs dynamiques
- if($myFirm->has_plugin('fr.core.dynamicform')){
- Plugin::need('dynamicform/DynamicForm');
- Dynamicform::record('client-sheet-custom',array(
- 'scope'=>'client',
- 'uid'=>$item->id
- ),$_);
- }
- //trigger pour utilisation sur le workflow
- if($myFirm->has_plugin('fr.core.workflow')){
- Plugin::need('workflow/WorkflowEvent');
- WorkflowEvent::trigger('client-client-'.($oldItem->id==0?'create':'update'),array('old'=>$oldItem,'current'=>$item));
- }
- $history = new History();
- $history->scope = 'client';
- $history->uid = $item->id;
- if(empty($_['id'])){
- $history->comment = "Création ".$conf->get('client_label_singular');
- $history->save();
- }else{
- $history->comment = "Modification ".$conf->get('client_label_singular')." :";
- $differences = Entity::compare($oldItem,$item);
- $fields = Client::fields(false);
- foreach ($differences as $difference) {
- $field = $difference['field'];
- if(isset($fields[$difference['field']])) $field = $fields[$difference['field']]['label'];
- $history->comment.="\n".$field.' : de <code>'.truncate($difference['value1']).'</code> à <code>'.truncate($difference['value2'].'</code>');
- }
- if(count($differences)>0) $history->save();
- }
- if(!empty($_FILES['logo'])){
- //Suppression ancien logo
- foreach (glob($item->dir()."logo.*") as $filename)
- unlink($filename);
- $logo = File::upload('logo','client'.SLASH.$item->id.SLASH.'logo.{{ext}}',104857060,array('jpg','png','jpeg','gif'));
- Image::resize($logo['absolute'],150,150);
- }
- if(!empty($_FILES['cover'])){
- //Suppression ancienne bannière
- foreach (glob($item->dir()."cover.*") as $filename)
- unlink($filename);
- $cover = File::upload('cover','client'.SLASH.$item->id.SLASH.'cover.{{ext}}',104857060,array('jpg','png','jpeg','gif'));
- $path = Image::toJpg($cover['absolute']);
- Image::resize($path,500,500,true,false);
- }
- $response = $item->toArray();
- if(isset($_['phone']) || isset($_['mail'])){
- $contacts = $item->contacts();
- if(isset($_['phone'])){
- $phone = isset($contacts[Contact::MOBILE]) ? $contacts[Contact::MOBILE] : new Contact();
- if(empty($_['phone']) && $phone->id !=0){
- Contact::deleteById($phone->id);
- }else{
- $phone->type = Contact::MOBILE;
- $phone->state = Contact::ACTIVE;
- $phone->value = str_replace(' ', '', $_['phone']);
- $phone->uid = $item->id ;
- $phone->scope = 'client' ;
- $phone->save();
- }
- }
- if(isset($_['mail'])){
- $mail = isset($contacts[Contact::PROFESSIONAL_MAIL]) ? $contacts[Contact::PROFESSIONAL_MAIL] : new Contact();
- if(empty($_['mail']) && $mail->id !=0){
- Contact::deleteById($mail->id);
- }else{
- $mail->type = Contact::PROFESSIONAL_MAIL;
- $mail->state = Contact::ACTIVE;
- $mail->value = trim($_['mail']);
- $mail->uid = $item->id ;
- $mail->scope = 'client' ;
- $mail->save();
- }
- }
- }
- });
- //Ajout ou modification d'élément client
- Action::register('client_assets_load',function(&$response){
- global $myUser,$_;
- if(!$myUser->can('client','read') && !$myUser->can('client_sheet','read',0,array('contains'=>true))) throw new Exception("Permission non accordée");
- require_once(__DIR__.SLASH.'Client.class.php');
- switch($_['type']){
- case 'logo':
- $client = new Client;
- if(isset($_['client']) && !empty($_['client'])) $client->id = $_['client'];
- $path = $client->logo(true);
- break;
- case 'cover':
- $client = new Client;
- if(isset($_['client']) && !empty($_['client'])) $client->id = $_['client'];
- $path = $client->cover(true);
- break;
- case 'contact':
- $contact = ContactPerson::getById($_['contact']);
- $path = $contact->avatar();
- break;
- default:
- $path = '';
- break;
- }
- File::downloadFile($path);
- });
- //Suppression d'élement client
- Action::register('client_client_delete',function(&$response){
- global $myUser,$_,$myFirm;
- User::check_access('client','delete');
- require_once(__DIR__.SLASH.'Client.class.php');
- $client = Client::getById($_['id']);
- if($client->firm!=$myFirm->id && !$myUser->can('client_sheet','delete',$client->id) ) throw new Exception("Vous n'avez pas la permission pour supprimer cette fiche");
- $client->remove($_['recursive']==1);
- });
- //Sauvegarde des configurations de client
- Action::register('client_setting_save',function(&$response){
- global $myUser,$_,$conf;
- User::check_access('client','configure');
- //Si input file "multiple", possibilité de normlaiser le
- //tableau $_FILES récupéré avec la fonction => normalize_php_files();
- foreach(Configuration::setting('client') as $key=>$value){
- if(!is_array($value)) continue;
- $allowed[] = $key;
- }
- foreach ($_['fields'] as $key => $value) {
- if(in_array($key, $allowed))
- $conf->put($key,$value);
- }
- });
- /** CLIENTCONTACT **/
- //Récuperation d'une liste de clientcontact
- Action::register('client_contact_search',function(&$response){
- global $myUser,$_,$myFirm;
- require_once(__DIR__.SLASH.'Client.class.php');
- if(!$myUser->can('client','read') && $_['client']!=0 && !$myUser->can('client_sheet','read',$_['client'])) throw new Exception("Permission non accordée sur cette fiche");
- if(empty($_['client'])) return $response;
- $client = Client::getById($_['client']);
- //if($client->id!=0 && !in_array($client->firm,array(0,$myFirm->id))) throw new Exception("Ces contacts de fiche ne sont pas disponibles pour votre etablissement");
- //si l'user n'est pas en acces tout client, on ne lui affiche que les relation client auxquelles il a acces
- if(!$myUser->can('client','read')){
- $allowedSheets = array();
- foreach($myUser->rights['client_sheet'] as $firm => $rights){
- foreach($rights as $uid=>$right){
- if($right['read']) $allowedSheets[] = $uid;
- }
- }
- }
- foreach(ContactPerson::getAll(array('where'=>array('scope'=>'client','uid'=>$client->id))) as $clientcontact){
- if($clientcontact->type=="client" && isset($allowedSheets) && !in_array($clientcontact->account,$allowedSheets)) continue;
- $row = $clientcontact->toArray();
- $row['fullName'] = $clientcontact->fullName();
- $row['avatar'] = 'action.php?action=client_assets_load&type=contact&contact='.$clientcontact->id;
- $row['tag'] = $clientcontact->getTags();
- $row['hasTag'] = !empty($row['tag']);
- switch($clientcontact->type){
- case 'client':
- require_once(__DIR__.SLASH.'Client.class.php');
- $client = Client::getById($clientcontact->account);
- if(!$client) continue 2;
- $row['avatar'] = $client->logo();
- $row['fullName'] = $client->label();
- $row['link'] = 'index.php?module=client&page=sheet.client&id='.$row['account'];
- $contacts = $client->contacts();
- if(isset($contacts['professional_mail'])) $row['mail'] = $contacts['professional_mail']->value;
- if(isset($contacts['professional_mail'])) $row['mobile'] = $contacts['mobile']->value;
- if(!empty($client->job)) $row['job'] = $client->job;
- break;
- case 'user':
- $user = User::byLogin($clientcontact->account);
- $row['avatar'] = $user->getAvatar();
- $row['fullName'] = $user->fullName();
- $row['mobile'] = $user->mobile;
- $row['job'] = $user->function;
- $row['phone'] = $user->phone;
- $row['mail'] = $user->mail;
- break;
- }
- $response['rows'][] = $row;
- }
- });
- //Récuperation ou edition d'élément clientcontact
- Action::register('client_contact_edit',function(&$response){
- global $myUser,$_;
- User::check_access('client','edit');
- $contact = ContactPerson::provide();
- $contact->meta();
- $response = $contact->toArray();
- $response['tag'] = $contact->getTags();
- });
- //Ajout ou modification d'élément clientcontact
- Action::register('client_contact_save',function(&$response){
- global $myUser,$_,$conf;
- User::check_access('client','edit');
- Plugin::need('client/Client');
- $item = ContactPerson::provide();
- $oldItem = clone $item;
- $item->type = $_['type'];
- //Contact référent utilisteur ou client interne
- if(!empty($_['account'])) $item->account = $_['account'];
- if($item->type=='external'){
- if(empty($_['name']) && empty($_['firstname']) && empty($_['mail']) && empty($_['phone'])) throw new Exception("Au moins l'un des champs Prénom, Nom, Mail ou Téléphone est requis");
- $item->name = $_['name'];
- $item->firstname = $_['firstname'];
- $item->civility = $_['civility'];
- $item->meta['type'] = 'contact';
- if(!empty($_['mail']) && !check_mail(trim($_['mail']))) throw new Exception("Mail invalide");
- $item->mail = trim($_['mail']);
- if(!empty($_['phone']) && !check_phone_number($_['phone'])) throw new Exception("Téléphone invalide");
- $item->phone = normalize_phone_number($_['phone']);
- if(!empty($_['mobile']) && !check_phone_number($_['mobile'])) throw new Exception("Téléphone mobile invalide");
- $item->mobile = normalize_phone_number($_['mobile']);
- }
- $item->scope = 'client';
- $item->uid = $_['client'];
- $item->job = $_['job'];
- $item->comment = $_['comment'];
- $item->setTags($_['tag']);
- $item->state = ContactPerson::ACTIVE;
- $item->save_all();
- $history = new History();
- $history->scope = 'client';
- $history->uid = $_['client'];
- if(empty($_['id'])){
- $history->comment = "Création du contact ".$conf->get('client_label_singular')." ".$item->fullName();
- $history->save();
- }else{
- $history->comment = "Modification du contact ".$conf->get('client_label_singular')." ".$item->fullName().' : ';
- $differences = Entity::compare($oldItem,$item);
- $fields = Client::fields(false);
- foreach ($differences as $difference) {
- $field = $difference['field'];
- if(isset($fields[$difference['field']])) $field = $fields[$difference['field']]['label'];
- $history->comment.="\n".$field.' : de <code>'.truncate($difference['value1']).'</code> à <code>'.truncate($difference['value2'].'</code>');
- }
- if(count($differences)>0) $history->save();
- }
- });
- //Suppression d'élement clientcontact
- Action::register('client_contact_delete',function(&$response){
- global $myUser,$_,$conf;
- User::check_access('client','delete');
- $item = ContactPerson::provide();
- $history = new History();
- $history->scope = 'client';
- $history->uid = $item->uid;
- $history->comment = "Suppression du contact ".$conf->get('client_label_singular')." ".$item->fullName();
- $history->save();
- ContactPerson::remove($item->id);
- });
- //Récupération card d'un contact
- Action::register('client_card',function(&$response){
- global $myUser,$myFirm,$_;
- require_once(__DIR__.SLASH.'Client.class.php');
- $client = Client::provide();
- if(!$myUser->can('client','read') && $client->id!=0 && !$myUser->can('client_sheet','read',$client->id)) throw new Exception("Permission non accordée sur cette fiche");
- ob_start();
- require_once(__DIR__.SLASH.'card.client.php');
- $stream = ob_get_clean();
- $response['content'] = $stream;
- });
- //composant client
- Action::register('client_autocomplete',function(&$response){
- global $myUser,$_,$myFirm;
- require_once(__DIR__.SLASH.'Client.class.php');
- if (!$myUser->connected()) throw new Exception("Vous devez être connecté", 401);
- $response['rows'] = array();
- $data = array("%".$_['keyword']."%","%".$_['keyword']."%",Client::ACTIVE);
- //retourne en priorité les matchs à 100%, pour les match keyword%, puis les autres
- $query = 'SELECT c.*,(SELECT label FROM {{table}} p WHERE p.id=c.parent) parentLabel FROM {{table}} c WHERE (c.label LIKE ? OR pseudonym LIKE ?) AND c.state=? ';
- if(isset($_['data']['parent']) && !empty($_['data']['parent']) && is_numeric($_['data']['parent'])){
- $query .= ' AND c.parent=? ';
- $data[] = $_['data']['parent'];
- }
- $query .= ' AND c.firm=? ';
- if(isset($_['data']['firm']) && !empty($_['data']['firm']) && is_numeric($_['data']['firm'])){
- $data[] = $_['data']['firm'];
- }else{
- $data[] = $myFirm->id;
- }
- $query .= ' ORDER BY
- CASE
- WHEN c.label = ? THEN 1
- WHEN c.label LIKE ? THEN 2
- ELSE 3
- END
- LIMIT 10';
- $data[] = $_['keyword'];
- $data[] = '%'.$_['keyword'];
- $clients = Client::staticQuery($query,$data,true);
- foreach($clients as $client){
- $response['rows'][] = array(
- 'label'=>html_entity_decode($client->label, ENT_QUOTES).(!empty($client->pseudonym) ? ' ('.html_entity_decode($client->pseudonym, ENT_QUOTES).')' :''),
- 'parentLabel'=>html_entity_decode($client->foreign('parentLabel'), ENT_QUOTES),
- 'id'=>$client->id,
- 'logo' => $client->logo()
- );
- }
- if(isset($_['data']) && isset($_['data']['before']) && isset($_['data']['before'])!=''){
- $list = json_decode(html_entity_decode($_['data']['before']),true);
- if(is_array($list)){
- foreach ($list as $key=>$value) {
- if(preg_match('/'.$_['keyword'].'/i', $value))
- array_unshift($response['rows'],array('label'=>$value,'id'=>$key));
- }
- }
- }
- });
- Action::register('client_by_uid',function(&$response){
- global $myUser,$_;
- if (!$myUser->connected()) throw new Exception("Vous devez être connecté",401);
- require_once(__DIR__.SLASH.'Client.class.php');
- $response['items'] = array();
- $query = 'SELECT main.*,p.label as parentLabel FROM {{table}} main LEFT JOIN {{table}} p ON main.parent=p.id WHERE main.id IN(';
- $query .= implode(',', array_fill(0, count($_['items']), '?'));
- $query .= ')';
- foreach(Client::staticQuery($query,$_['items'],true) as $client) {
- $row = $client->toArray();
- unset($row['parent']);
- if(!empty($client->foreign('parentLabel'))) $row['parent'] = array('label'=>$client->foreign('parentLabel'));
- $row['label'] = html_entity_decode($row['label'], ENT_QUOTES);
- if(!empty($row['pseudonym'])) $row['label'].= ' ('.html_entity_decode($row['pseudonym'], ENT_QUOTES).')' ;
- $response['items'][$row['id']] = $row;
- }
- if(isset($_['before']) && $_['before']!=''){
- $list = json_decode(html_entity_decode($_['before']),true);
- if(is_array($list)){
- if(isset($list[$_['id']])) $response['items'][] = array('label' => $list[$_['id']], 'id'=>$_['id']);
- }
- }
- });
- //ADDRESSES
- //Récuperation d'une liste d'adresses
- Action::register('client_address_search',function(&$response){
- global $myUser,$_,$myFirm;
- require_once(__DIR__.SLASH.'Client.class.php');
- if(!$myUser->can('client','read') && $_['client']!=0 && !$myUser->can('client_sheet','read',$_['client'])) throw new Exception("Permission non accordée sur cette fiche");
- if(empty($_['client'])) return $response;
- $client = Client::getById($_['client']);
- //if($client->id!=0 && !in_array($client->firm,array(0,$myFirm->id))) throw new Exception("Ces addresses de fiche ne sont pas disponibles pour votre etablissement");
- $addresses = Address::loadAll(array('scope'=>'client','uid'=>$_['client']));
- foreach($addresses as $address){
- $row = $address->toArray();
- $row['type'] = Address::types($row['type']);
- $row['type']['slug'] = $address->type;
- $response['rows'][] = $row;
- }
- });
- //Récuperation ou edition d'élément addresse
- Action::register('client_address_edit',function(&$response){
- global $myUser,$_;
- User::check_access('client','edit');
- $address = Address::provide();
- if($address->id!=0 && $address->scope != 'client') throw new Exception("Entité non conforme");
- $response = $address->toArray();
- });
- //Ajout ou modification d'élément clientcontact
- Action::register('client_address_save',function(&$response){
- global $myUser,$_;
- require_once(__DIR__.SLASH.'Client.class.php');
- User::check_access('client','edit');
- if(empty($_['client'])) throw new Exception("Client non spécifié");
- if(empty($_['street']) && empty($_['zip']) && empty($_['city'])) throw new Exception("Vous devez spécifier au minimum une ville, un code postal ou une rue");
- $address = Address::provide();
- $address->fromArray($_);
- $address->scope = 'client' ;
- $address->uid = $_['client'] ;
- $address->save();
- //Enregistrement des coordonées gps dans les méta client si l'adresse est la principale
- if($address->type==Address::MAIN){
- $coordinates = $address->locationCoordinates();
- $client = Client::getById($address->uid);
- $client->meta('longitude', $coordinates['longitude']);
- $client->meta('latitude', $coordinates['latitude']);
- $client->save();
- }
- History::put('client',$_['client'],(empty($_['id']) ? 'Création ': ' Modification ' )." d'une adresse de type : ".Address::types($address->type)['label'].' : '.$address->fullName());
- });
- //Suppression d'élement clientcontact
- Action::register('client_address_delete',function(&$response){
- global $myUser,$_;
- User::check_access('client','delete');
- require_once(__DIR__.SLASH.'Client.class.php');
- $address = Address::getById($_['id']);
- if($address->scope!='client') throw new Exception("Entité spécifiée non conforme",403);
- Address::deleteById($address->id);
- History::put('client',$address->uid,"Suppression d'une adresse de type : ".Address::types($address->type)['label']);
- });
- /*WIDGET*/
- Action::register('client_widget_load',function(&$response){
- global $myUser,$_;
- if(!$myUser->can('client','read')) throw new Exception("Permission non accordée");
- Plugin::need('dashboard/DashboardWidget');
- $widget = DashboardWidget::current();
- $widget->title = 'Widget Client';
- ob_start();
- //Décommenter après avoir créé widget.php
- require_once(__DIR__.SLASH.'client.widget.php');
- $widget->content = ob_get_clean();
- echo json_encode($widget);
- exit;
- });
- /** HISTORY **/
- //Récuperation d'une liste de history
- Action::register('client_core_history_search',function(&$response){
- global $myUser,$_;
- if(!$myUser->can('client_history','read')) throw new Exception("Permission non accordée sur cette fiche");
- if(!$myUser->can('client','read') && $_['id']!=0 && !$myUser->can('client_sheet','read',$_['id'])) throw new Exception("Permission non accordée sur cette fiche");
- // OPTIONS DE RECHERCHE, A ACTIVER POUR UNE RECHERCHE AVANCEE
- $query = 'SELECT * FROM {{table}} WHERE 1';
- $data = array();
- //scopé sur le client
- $query .= ' AND scope = ? AND uid = ?';
- $data[] = 'client';
- $data[] = $_['id'];
- //Recherche simple
- if(!empty($_['filters']['keyword'])){
- $query .= ' AND comment LIKE ?';
- $data[] = '%'.$_['filters']['keyword'].'%';
- }
- //Recherche avancée
- if(isset($_['filters']['advanced'])) filter_secure_query($_['filters']['advanced'],array('comment','created','creator'),$query,$data);
- $query .= ' ORDER BY created DESC';
- //Pagination
- $response['pagination'] = History::paginate(20,(!empty($_['page'])?$_['page']:0),$query,$data);
- $historys = History::staticQuery($query,$data,true,0);
- foreach($historys as $history){
- $row = $history->toArray();
- $monthName = month_name(date('m',$history->created));
- $row['created'] = array(
- 'dayShortName' => day_name(date('N',$history->created)),
- 'day' => date('d',$history->created),
- 'monthName' => strlen($monthName)>4 ? substr(month_name(date('m',$history->created)), 0, 4).'.' : $monthName,
- 'time' => date('H:i',$history->created)
- );
- $row['comment'] = html_entity_decode($row['comment']);
- if($_['export'] == 'true'){
- $row['created'] = date('d-m-Y',$history->created);
- $row['updated'] = date('d-m-Y',$history->updated);
- }
- $creator = User::byLogin($row['creator']);
- $row['creator'] = $creator->toArray();
- $row['editable'] = $row['type'] == History::TYPE_COMMENT ;
- $row['creator']['fullName'] = $creator->fullName();
- $response['rows'][] = $row;
- }
- /* Mode export */
- if($_['export'] == 'true'){
- $fieldsMapping = array();
- foreach (History::fields(false) as $key => $value)
- $fieldsMapping[$value['label']] = $key ;
- $stream = Excel::exportArray($response['rows'],$fieldsMapping,'Export');
- File::downloadStream($stream,'historique-client-'.date('d-m-Y').'.xlsx');
- exit();
- }
- });
- Action::register('client_core_history_save',function(&$response){
- global $myUser,$_,$myFirm;
- User::check_access('client_history','edit');
- if(!isset($_['client'])) throw new Exception('Id client manquant');
- if(!isset($_['comment'])) throw new Exception('Message manquant');
- $history = History::provide();
- $history->scope = 'client';
- $history->uid = $_['client'];
- $history->comment = $_['comment'];
- $history->type = History::TYPE_COMMENT;
- $history->save();
- });
- Action::register('client_history_edit',function(&$response){
- global $myUser,$_,$myFirm;
- User::check_access('client_history','edit');
- require_once(__DIR__.SLASH.'Client.class.php');
- $history = History::getById($_['id']);
- if($history->scope!='client') throw new Exception('Vous ne pouvez editer ce type d\'historique');
- $response = $history->toArray();
- $response['comment'] = html_entity_decode($response['comment']);
- });
- Action::register('client_core_history_delete',function(&$response){
- global $myUser,$_,$myFirm;
- User::check_access('client_history','delete');
- require_once(__DIR__.SLASH.'Client.class.php');
- $history = History::getById($_['id']);
- if($history->scope!='client') throw new Exception('Vous ne pouvez supprimer ce type d\'historique');
- History::deleteById($history->id);
- });
- Action::register('client_import_component',function(&$response){
- global $myUser,$_;
- if(!$myUser->can('client','configure')) throw new Exception("Permission non accordée");
- File::handle_component(array(
- 'namespace' => 'client', //stockés dans file/client/*.*
- 'access' => 'client', // crud sur summary,
- 'size' => '1000000000', // taille max
- 'extension' => 'csv',
- 'storage' => 'client/import/*' //chemin complet vers le fichier stocké
- ),$response);
- });
- Action::register('client_import',function(&$response){
- global $myUser,$_,$myFirm;
- if(!$myUser->can('client','configure')) throw new Exception("Permission non accordée");
- if(count($_['attachments'])==0) throw new Exception("Merci d'envoyer un fichier au format csv avant d'appuyer sur le boutton import");
- if($_['attachments'][0]['extension']!='csv') throw new Exception("Extension invalide (accepté: csv)");
- if($_['attachments'][0]['size'] > 1000000000 ) throw new Exception("Taille trop importante (max accepté: 1Go)");
- require_once(__DIR__.SLASH.'Client.class.php');
- $clientManager = new Client();
- $keyLabelMap = array();
- foreach ($clientManager->fields as $key => $meta) {
- $keyLabelMap[$meta['label']] = $key;
- }
- $isDynamic = array();
- if($myFirm->has_plugin('fr.core.dynamicform')){
- Plugin::need('dynamicform/DynamicForm');
- foreach (DynamicForm::list('client-sheet-custom') as $key => $meta) {
- $keyLabelMap[$meta['label']] = $meta['slug'];
- $isDynamic[$meta['slug']] = $meta;
- }
- }
- $keyLabelMap['Contacts'] = 'contacts';
- $keyLabelMap['Adresses'] = 'addresses';
- $keyLabelMap['Client Parent'] = 'parent';
- $lines = explode("\r\n",utf8_encode(file_get_contents(File::temp().str_replace('/tmp/','',$_['attachments'][0]['path']))));
- if(count($lines)<2) throw new Exception("Le fichier d'import est vide");
- $headers = array_shift($lines);
- $headers = explode(';',$headers);
- $headersColumnMap = array();
- foreach($headers as $i => $header){
- if(!isset($keyLabelMap[$header])) continue;
- $headersColumnMap[$i] = $keyLabelMap[html_entity_decode($header)];
- }
- $response['warnings'] = array();
- $transformMapping = array();
- //convertion des types label -> bdd
- $typesLabel = array();
- foreach (Client::types() as $typeKey=>$type)
- $typesLabel[$type['label']] = $typeKey;
- $transformMapping['type'] = function($value,$client) use ($typesLabel){
- return isset($typesLabel[$value]) ? $typesLabel[$value] : '';
- };
- //convertion des conditions label -> bdd
- $conditionsLabel = array();
- foreach (Client::conditions() as $conditionKey=>$condition)
- $conditionsLabel[$condition['label']] = $conditionKey;
- $transformMapping['condition'] = function($value,$client) use ($conditionsLabel){
- return isset($conditionsLabel[$value])? $conditionsLabel[$value] : '';
- };
- //convertion des catégories label -> bdd
- $categoriesLabel = array();
- $categoriesDictionary = Dictionary::bySlug('client_category');
- foreach (Dictionary::slugToArray('client_category',true) as $categoryKey=>$category)
- $categoriesLabel[$category->label] = $categoryKey;
- //Convertion des adresses
- /*
- le format des adresse doit être le suivant dans le csv : add1\nadd2[:type], ex :
- "122 avenue de saint emilion 33600 Martignas sur jalles
- 344 rue frédéric sévène 33400 Talence:facturation"
- */
- $transformMapping['addresses'] = function($value,$client){
- //suppression des "" de chaque coté de la valeur si existants
- if(preg_match('/^"(.*)"$/si', $value,$match))
- $value = $match[1];
- foreach(explode("\n",$value) as $rawAddress){
- $address = new Address();
- $type = Address::MAIN;
- $rawAddress = explode(':',$rawAddress);
- if(isset($rawAddress[1])) $type = $rawAddress[1];
- $street = '';
- $zip = '';
- $city = '';
- $country = '';
- $complement = '';
- if(preg_match('/([0-9]*)? ?(.*) ([0-9]{5}) ([^,]*),?\s*(.*)?/i', $rawAddress[0],$match)){
- if(!empty($match[1]) || !empty($match[2])){
- $street = (!empty($match[1]) ? $match[1] : '').(!empty($match[2]) ? ' '.$match[2] : '');
- if(!empty($match[3])) $zip = $match[3];
- if(!empty($match[4])) $city = $match[4];
- if(!empty($match[5])) $country = $match[5];
- }
- }else{
- //Si on ne parviens pas a parser l'adresse
- //On essaye de récuperer le CP
- if(preg_match('/ ([0-9]{5}) /i', $rawAddress[0],$match)){
- $zip = $match[1];
- }
- //on stoque tout dans la rue
- $street = $rawAddress[0];
- }
- $address->scope = 'client';
- $address->label = '';
- $address->uid = $client->id;
- $address->type = $type;
- $address->street = $street;
- $address->complement = $complement;
- $address->city = $city;
- $address->zip = $zip;
- $address->country = $country;
- $address->state = Address::ACTIVE;
- //récuperation latitude et longitude
- //Enregistrement des coordonées gps dans les méta client si l'adresse est la principale
- if($address->type==Address::MAIN && isset($coordinates['longitude']) && isset($coordinates['latitude'])){
- $coordinates = $address->locationCoordinates();
- $client->meta('longitude', $coordinates['longitude']);
- $client->meta('latitude', $coordinates['latitude']);
- $client->save();
- }
- $address->save();
- }
- //on returne false pour indiquer de ne pas remplir l'objet client avec cette info
- return false;
- };
- //Convertion des contacts
- /*
- Exemple de format de contact dans la case du csv
- "jessica ALBA:jalba@alba.com:0681129753:0781129753:PDG:beauty,star
- angelina JOLIE:angie@jolie.com"
- */
- $transformMapping['contacts'] = function($value,$client){
- //suppression des "" de chaque coté de la valeur si existants
- if(preg_match('/^"(.*)"$/si', $value,$match))
- $value = $match[1];
- foreach(explode("\n",$value) as $rawContact){
- $contact = new ContactPerson();
- $rawContact = explode(':',$rawContact);
- if(isset($rawContact[1])) $type = $rawContact[1];
- $nameFirstName = explode(' ',$rawContact[0]);
- $contact->firstname = $nameFirstName[0];
- if(isset($nameFirstName[1])) $contact->name = $nameFirstName[1];
- if(!empty($rawContact[1])) $contact->mail = $rawContact[1];
- if(!empty($rawContact[2])) $contact->phone = $rawContact[2];
- if(!empty($rawContact[3])) $contact->mobile = $rawContact[3];
- if(!empty($rawContact[4])) $contact->job = $rawContact[4];
- if(!empty($rawContact[5])) $contact->tag = $rawContact[5];
- $contact->scope = 'client';
- $contact->uid = $client->id;
- $contact->type = ContactPerson::TYPE_EXTERNAL;
- $contact->state = ContactPerson::ACTIVE;
- $contact->save();
- }
- //on returne false pour indiquer de ne pas remplir l'objet client avec cette info
- return false;
- };
- //transformation du libellé parent en libellé enfant
- $transformMapping['parent'] = function($value,$client){
- if(empty($value)) return '';
- $parent = Client::load(array('label'=>$value));
- if(!$parent) return;
- return $parent->id;
- };
- $transformMapping['category'] = function($value,$client) use ($categoriesLabel,$categoriesDictionary){
- if(!isset($categoriesLabel[$value])){
- $dictionary = new Dictionary();
- $dictionary->label = $value;
- $dictionary->state = Dictionary::ACTIVE;
- $dictionary->slug = $categoriesDictionary->slug.'_'.slugify($value);
- $dictionary->parent = $categoriesDictionary->id;
- $dictionary->save();
- $categoriesLabel[$value] = $dictionary->id;
- }
- return $categoriesLabel[$value];
- };
- foreach ($lines as $i=>$line) {
- if(empty($line)) continue;
- try{
- $columns = explode(';',$line);
- if(count($columns)==0) throw new Exception("Colonnes vides");
- $newClient = new Client();
- $newClient->save(); //on presave pour obtenir un id
- foreach ($columns as $u=>$column) {
- try{
- if(!isset($headersColumnMap[$u])) throw new Exception("La colonne '".$headers[$u]."' ne correspond a rien dans l'erp");
- $key = $headersColumnMap[$u];
- if(isset($transformMapping[$key])){
- $method = $transformMapping[$key];
- $column = $method($column,$newClient);
- //si false, ne pas remplir l'objet client avec cette info (info relationnelle)
- if($column === false) continue;
- }
- if($myFirm->has_plugin('fr.core.dynamicform') && isset($isDynamic[$key]) ){
- Plugin::need('dynamicform/DynamicValue');
- $dynamicValue = new DynamicValue();
- $dynamicValue->field = $isDynamic[$key]['id'];
- if(property_exists($isDynamic[$key]['type'],"fromRawDisplay")){
- $method = $isDynamic[$key]['type']->fromRawDisplay;
- $options = array();
- if($isDynamic[$key]['type']->slug =='dictionary'){
- $parent = Dictionary::bySlug($isDynamic[$key]['meta']->slug);
- $options['slug'] = $parent->id;
- }
- $column = $method($column,$options);
- }
- $dynamicValue->value= $column;
- $dynamicValue->firm = $myFirm->id;
- $dynamicValue->scope = 'client';
- $dynamicValue->uid = $newClient->id;
- $dynamicValue->save();
- }else{
- $newClient->$key = $column;
- }
- }catch(Exception $e){
- $response['warnings'][] = 'L'.($i+1).' Col'.($u+1).':'.$e->getMessage();
- }
- if(empty($newClient->code)) $newClient->code = CLient::code($newClient);
- if(empty($newClient->slug)) $newClient->slug = slugify($client->label);
- $newClient->save();
- }
- }catch(Exception $e){
- //si un pb sur la ligne on supprime le client qu'on a commencé a enregistrer en base
- if($newClient->id!=0) Client::deleteById($newClient->id);
- $response['warnings'][] = 'L'.($i+1).':'.$e->getMessage();
- }
- }
- });
- Action::register('client_import_model',function(&$response){
- global $myUser,$_,$myFirm;
- if(!$myUser->can('client','configure')) throw new Exception("Permission non accordée");
- require_once(__DIR__.SLASH.'Client.class.php');
- $client = new Client();
- $stream = '';
- foreach ($client->fields as $key => $meta) {
- if( in_array($key, array('id','state','slug','meta'))) continue;
- $stream .= $meta['label'].';';
- }
- $stream .= 'Contacts;';
- $stream .= 'Adresses;';
- $stream .= 'Client Parent;';
- if($myFirm->has_plugin('fr.core.dynamicform')){
- Plugin::need('dynamicform/DynamicForm');
- foreach (DynamicForm::list('client-sheet-custom') as $key => $meta) {
- $stream .= $meta['label'].';';
- }
- }
- $stream .= PHP_EOL;
- File::downloadStream(utf8_decode($stream),'Trame import client.csv', 'application/octet-stream');
- exit();
- });
- ?>
|