Example #1
0
 public function contrat(array $data, $disposition = null, $test = false)
 {
     $disposition = is_null($disposition) ? 'attachment' : $disposition;
     $tpl = File::read(APPLICATION_PATH . DS . 'templates/contrat_myzelift.html');
     $bucket = new Bucket(SITE_NAME, 'http://zelift.com/bucket');
     $affiliation = isAke($data, 'affiliation', 'resto');
     $univers = isAke($data, 'univers', 'resto');
     $zechallenge_id = isAke($data, 'compte_zechallenge', 1);
     $contrat = Model::FacturationContrat()->refresh()->where(['platform', '=', 'MyZeLift'])->where(['affiliation', '=', $affiliation])->where(['univers', '=', $univers])->where(['zechallenge_id', '=', (int) $zechallenge_id])->first(true);
     if ($contrat && !$test) {
         $return = true;
         if ($disposition == 'attachment') {
             if (!fnmatch('http://*', $contrat->url)) {
                 $contrat->delete();
                 $return = false;
             }
             if ($return) {
                 return ['url' => $contrat->url, 'error' => false];
             }
         }
         if ($return) {
             $pdf = dwn($contrat->url);
             header('Content-Type: application/pdf');
             header('Content-Length: ' . strlen($pdf));
             header('Content-Disposition: ' . $disposition . '; filename="Contrat-ZeChallenge.pdf"');
             header('Cache-Control: private, max-age=0, must-revalidate');
             header('Pragma: public');
             ini_set('zlib.output_compression', '0');
             die($pdf);
         }
     }
     $contratZC = Model::FacturationContrat()->refresh()->where(['platform', '=', 'ZeChallenge'])->where(['zechallenge_id', '=', (int) $zechallenge_id])->first(true);
     if ($contratZC) {
         $zc = Model::Zechallenge()->find((int) $zechallenge_id);
         if ($zc || $test) {
             if (!$test) {
                 $univers = lib('zechallenge')->getContext((int) $zc->reseller_id);
                 $contract = lib('contrat')->store($univers, 'MyZeLift', $affiliation, $zechallenge_id);
                 $contratZC->end = strtotime('+1 year -1 day');
                 $contratZC->save();
                 if ($univers == 'resto') {
                     $code_contrat = 'RES';
                 } elseif ($univers == 'services') {
                     $code_contrat = 'SER';
                 }
                 $code_contrat .= '-MZ-' . $contract->id;
                 $data['code_contrat'] = $code_contrat;
             }
             foreach ($data as $k => $v) {
                 if ($k == 'univers') {
                     $tpl = str_replace("##Univers##", ucfirst($v), $tpl);
                     $tpl = str_replace("##univers##", $v, $tpl);
                 } else {
                     $tpl = str_replace("##{$k}##", $v, $tpl);
                 }
             }
             $pdf = pdfFile($tpl, 'Contrat-MyZeLift', 'portrait');
             if (!$test) {
                 $url = $bucket->data($pdf, 'pdf');
                 $contract->url = $url;
                 if (!fnmatch('http://*', $url)) {
                     return ['url' => false, 'error' => $contract->url];
                 }
                 $contract->save();
             }
             if ($disposition == 'attachment') {
                 return ['url' => $url, 'error' => false];
             }
             header('Content-Type: application/pdf');
             header('Content-Length: ' . strlen($pdf));
             header('Content-Disposition: ' . $disposition . '; filename="Contrat-MyZeLift.pdf"');
             header('Cache-Control: private, max-age=0, must-revalidate');
             header('Pragma: public');
             ini_set('zlib.output_compression', '0');
             die($pdf);
         }
     } else {
         if ($disposition == 'attachment') {
             return ['url' => false, 'error' => 'pas de contrat zechallenge actif.'];
         }
     }
 }
Example #2
0
 public function firstZechallenge($reseller_id, $disposition = null)
 {
     $bucket = new Bucket(SITE_NAME, 'http://zelift.com/bucket');
     $disposition = is_null($disposition) ? 'attachment' : $disposition;
     $reseller = Model::Reseller()->find((int) $reseller_id);
     if ($reseller) {
         $zid = $reseller->zechallenge_id;
         $zechallenge = Model::Zechallenge()->find((int) $zid);
         if ($zechallenge) {
             $contract = Model::FacturationContrat()->where(['zechallenge_id', '=', (int) $zid])->where(['platform', '=', 'ZeChallenge'])->first(true);
             if (!$contract) {
                 exception("facture", 'Aucun contrat zechallenge trouvé.');
             }
             $products = lib('facturation')->abonnementChallenge(lib('zechallenge')->getContext($reseller->id));
             $toBilled = $acomptes = $hasAcomptes = [];
             foreach ($products as $product) {
                 $haveTobeBilled = Model::FacturationAcompte()->where(['status', '=', 'UNBILLED'])->where(['product_id', '=', (int) $product['id']])->where(['zechallenge_id', '=', (int) $zid])->with('product');
                 foreach ($haveTobeBilled as $hasToBilled) {
                     $acomptes[] = $hasToBilled;
                     $hasAcomptes[$hasToBilled['product_id']] = true;
                 }
                 $haveTobeBilled = Model::FacturationPurchase()->where(['status', '=', 'UNBILLED'])->where(['product_id', '=', (int) $product['id']])->where(['zechallenge_id', '=', (int) $zid])->with('product');
                 foreach ($haveTobeBilled as $hasToBilled) {
                     if (!isset($hasAcomptes[$hasToBilled['product_id']])) {
                         $toBilled[] = $hasToBilled;
                     }
                 }
             }
             $total = 0;
             $details = [];
             foreach ($toBilled as $hastoBilled) {
                 $sum = $hastoBilled['quantity'] * $hastoBilled['product']['amount'];
                 $total += $sum;
                 $details[] = '<tr style="border:1px solid #a4a4a4;border-top:none;">
                 <td style="width:500px;">
                     <div style="width:500px; text-indent:30px; height:25px; line-height:25px;">
                         ' . $hastoBilled['product']['name'] . '
                     </div>
                 </td>
                 <td style="width:300px; font-size:0; padding:0;">
                     <div style="width:75px; display:inline-block; font-size:15px; text-align:center;">' . number_format($hastoBilled['product']['amount'], 2) . '€</div>
                     <div style="width:75px; display:inline-block; font-size:15px; text-align:center;">' . $hastoBilled['quantity'] . '</div>
                     <div style="width:75px; display:inline-block; font-size:15px; text-align:center;"></div>
                     <div style="width:75px; display:inline-block; font-size:15px; text-align:center;">
                         ' . number_format($sum, 2) . '€
                     </div>
                 </td>
             </tr>';
             }
             foreach ($acomptes as $hastoBilled) {
                 $sum = $hastoBilled['quantity'] * $hastoBilled['amount'];
                 $total += $sum;
                 $details[] = '<tr style="border:1px solid #a4a4a4;border-top:none;">
                 <td style="width:500px;">
                     <div style="width:500px; text-indent:30px; height:25px; line-height:25px;">
                         ' . $hastoBilled['product']['name'] . ' (Acompte)
                     </div>
                 </td>
                 <td style="width:300px; font-size:0; padding:0;">
                     <div style="width:75px; display:inline-block; font-size:15px; text-align:center;">' . number_format($hastoBilled['amount'], 2) . '€</div>
                     <div style="width:75px; display:inline-block; font-size:15px; text-align:center;">' . $hastoBilled['quantity'] . '</div>
                     <div style="width:75px; display:inline-block; font-size:15px; text-align:center;"></div>
                     <div style="width:75px; display:inline-block; font-size:15px; text-align:center;">
                         ' . number_format($sum, 2) . '€
                     </div>
                 </td>
             </tr>';
             }
             $facture = Model::FacturationFacture()->firstOrCreate(['zechallenge_id' => $zid, 'products' => $toBilled]);
             $tva = number_format(round($total * 0.2, 2), 2);
             $ttc = number_format($total + $tva, 2);
             $details = implode("\n", $details);
             $tpl = File::read(APPLICATION_PATH . DS . 'templates/premiere_facture_zechallenge.html');
             $ib = Model::Inovibackend()->find((int) $reseller->inovibackend_id);
             $tpl = str_replace(['##typoColor##', '##typo##', '##no_facture##', '##compte_inovi##', '##no_contrat##', '##univers##', '##affil##', '##nom_client##', '##adresse_client##', '##cp_client##', '##ville_client##', '##lieu_contrat##', '##date_contrat##', '##date_debut##', '##date_fin##', '##date_jour##', '##total##', '##total_tva##', '##total_ttc##', '##details##'], ['#6e358b', 'ZeChallenge', $facture->id, $reseller->inovibackend_id, $zid, lib('zechallenge')->getMarket($reseller->id), lib('segment')->getAffiliation($reseller->id, false), $ib->name, $ib->address, $ib->zip, $ib->city, $ib->city, date('d/m/Y', $contract->contract_date), date('d/m/Y', $contract->contract_date), date('d/m/Y', $contract->end), date('d/m/Y'), number_format($total, 2), $tva, $ttc, $details], $tpl);
             $pdf = pdfFile($tpl, 'Facture-ZeChallenge', 'portrait');
             $url = $bucket->data($pdf, 'pdf');
             if (!fnmatch('http://*', $url)) {
                 return ['url' => false, 'error' => $url];
             }
             if ($disposition == 'attachment') {
                 return ['url' => $url, 'error' => false];
             }
             header('Content-Type: application/pdf');
             header('Content-Length: ' . strlen($pdf));
             header('Content-Disposition: ' . $disposition . '; filename="Contrat-ZeChallenge.pdf"');
             header('Cache-Control: private, max-age=0, must-revalidate');
             header('Pragma: public');
             ini_set('zlib.output_compression', '0');
             die($pdf);
         }
     }
 }
Example #3
0
 function consolidate($reseller_id = null)
 {
     Save::clean();
     // $szs = Model::Sellzone()->cursor();
     // foreach ($szs as $sz) {
     //     $coll = [];
     //     $zips = Model::Coveredcity()->where(['sellzone_id', '=', (int) $sz['id']])->cursor();
     //     foreach ($zips as $zip) {
     //         $cs = Model::City()->where(['zip', '=', (string) $zip['zip']])->cursor();
     //         foreach ($cs as $c) {
     //             $n      = str_replace(' ', '_', Inflector::unaccent(Inflector::lower($zip['name'])));
     //             $n2     = str_replace(' ', '_', Inflector::unaccent(Inflector::lower($c['name'])));
     //             if ($n == $n2) {
     //                 $coll[] = [
     //                     'insee' => $c['insee'],
     //                     'zip'   => $zip['zip'],
     //                     'name'  => $c['name'],
     //                 ];
     //             }
     //         }
     //     }
     //     $coll = array_values(array_unique($coll));
     //     redis()->set('insees.' . $sz['id'], serialize($coll));
     // }
     $plats = Model::Mealgeo()->cursor();
     $geos = [];
     foreach ($plats as $plat) {
         if (!isset($geos[$plat['resto_geo_id']])) {
             $geos[$plat['resto_geo_id']] = [];
         }
         $geos[$plat['resto_geo_id']][] = $plat['segment_id'];
     }
     foreach ($geos as $id => $tab) {
         redis()->set('geos.spes.' . $id, serialize($tab));
     }
     $ids = repo('segment')->getAllFamilyIds(1941);
     $names = [];
     $segs = Model::Segment()->where(['id', 'IN', implode(',', $ids)])->cursor();
     foreach ($segs as $seg) {
         $data = repo('segment')->getData((int) $seg['id']);
         $is_item = isAke($data, 'is_item', false);
         if (false !== $is_item) {
             $names[] = $seg['name'];
         }
     }
     asort($names);
     redis()->set('sucres', serialize(array_values($names)));
     $suggestIds = [];
     $segments = Model::Segment()->reset()->where(['segmenttype_id', '=', 9])->cursor();
     $collection = [];
     foreach ($segments as $segment) {
         $data = repo('segment')->getData((int) $segment['id']);
         $ordre = isAke($data, 'ordre', 1);
         $segment['ordre'] = $ordre;
         $collection[] = $segment;
     }
     $collection = lib('collection', [$collection]);
     $collection->sortBy('ordre');
     $segments = $collection->toArray();
     foreach ($segments as $segment) {
         $data = repo('segment')->getData((int) $segment['id']);
         $segmentsItem = isAke($data, 'segments', false);
         if (false !== $segmentsItem) {
             $tab = explode(',', $segmentsItem);
             $item = [];
             $item['segments'] = [];
             $item['plats'] = [];
             foreach ($tab as $itId) {
                 $seg = Model::Segment()->find($itId);
                 if ($seg) {
                     if (!in_array($itId, $suggestIds)) {
                         $suggestIds[] = $itId;
                     }
                     $s = [];
                     $s['id'] = (int) $itId;
                     $s['name'] = $seg->name;
                     unset($s['created_at']);
                     unset($s['updated_at']);
                     $type = Model::segmenttype()->find($seg->segmenttype_id);
                     if ($type) {
                         $s['type'] = $type->name;
                     }
                     if (fnmatch('*non*', $type->name)) {
                         $plats = Model::Mealnonauto()->where(['resto_nonauto_id', '=', (int) $itId])->cursor();
                         foreach ($plats as $plat) {
                             $cat = Model::Catalog()->find((int) $plat['catalog_id']);
                             if ($cat) {
                                 $item['plats'][] = $cat->segment_id . '::' . $cat->reseller_id;
                             }
                         }
                     }
                     if (fnmatch('*_type*', $type->name)) {
                         $plats = Model::Mealtype()->where(['resto_type_id', '=', (int) $itId])->cursor();
                         foreach ($plats as $plat) {
                             $item['plats'][] = $plat['segment_id'];
                         }
                     }
                     if (fnmatch('*geo*', $type->name)) {
                         $plats = Model::Mealgeo()->where(['resto_geo_id', '=', (int) $itId])->cursor();
                         foreach ($plats as $plat) {
                             $item['plats'][] = $plat['segment_id'];
                         }
                     }
                 }
                 $item['segments'][] = $s;
             }
             $row = Model::Suggestion()->firstOrCreate(['segment_id' => (int) $segment['id']])->setName($segment['name'])->setSegments($item['segments'])->setPlats($item['plats'])->save();
         }
     }
     $days = ['lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche'];
     $when = ['am_start', 'am_end', 'pm_start', 'pm_end'];
     $q = Model::Optionsrestaurant();
     if (!is_null($reseller_id)) {
         $q->where(['reseller_id', '=', (int) $reseller_id]);
     }
     $restos = $q->cursor();
     foreach ($restos as $resto) {
         $schedules = [];
         $schedules['sur_place'] = [];
         $schedules['a_emporter'] = [];
         $schedules['en_livraison'] = [];
         $row = Model::Restodata()->firstOrCreate(['reseller_id' => (int) $resto['reseller_id'], 'sellzone_id' => (int) $resto['sellzone_id']]);
         $reseller_id = $resto['reseller_id'];
         $loc = isAke($row->toArray(), 'loc', []);
         $lat = isAke($loc, 'lat', false);
         $lng = isAke($loc, 'lng', false);
         if (!$lat || !$lng) {
             $company = Model::Company()->refresh()->where(['reseller_id', '=', (int) $reseller_id])->first(true);
             $coords = lib('geo')->getCoords($company->address . ' ' . $company->zip . ' ' . $company->city);
             $locationReseller = ['lng' => $coords['lng'], 'lat' => $coords['lat']];
         } else {
             $locationReseller = ['lng' => floatval($lng), 'lat' => floatval($lat)];
         }
         $options = $resto;
         unset($options['_id']);
         unset($options['reseller_id']);
         unset($options['sellzone_id']);
         unset($options['created_at']);
         unset($options['updated_at']);
         $pattern = 'type_restauration_sur_place_horaires_services_##day##_##when##';
         foreach ($days as $day) {
             if (!isset($collection[$day])) {
                 $schedules['sur_place'][$day] = [];
             }
             foreach ($when as $moment) {
                 $key = str_replace(['##day##', '##when##'], [$day, $moment], $pattern);
                 $schedules['sur_place'][$day][$moment] = str_replace(':', '_', $options[$key]);
             }
         }
         $pattern = 'type_restauration_a_emporter_horaires_services_##day##_##when##';
         foreach ($days as $day) {
             if (!isset($collection[$day])) {
                 $schedules['a_emporter'][$day] = [];
             }
             foreach ($when as $moment) {
                 $key = str_replace(['##day##', '##when##'], [$day, $moment], $pattern);
                 $schedules['a_emporter'][$day][$moment] = str_replace(':', '_', $options[$key]);
             }
         }
         $pattern = 'type_restauration_en_livraison_horaires_services_##day##_##when##';
         foreach ($days as $day) {
             if (!isset($collection[$day])) {
                 $schedules['en_livraison'][$day] = [];
             }
             foreach ($when as $moment) {
                 $key = str_replace(['##day##', '##when##'], [$day, $moment], $pattern);
                 $schedules['en_livraison'][$day][$moment] = str_replace(':', '_', isAke($options, $key, ''));
             }
         }
         $catalog_names = $plats_names = $families_types_auto_dispo = $families_types_non_auto_dispo = $families_plats_dispo = $pois_dispo = $families_pois_dispo = $specialites_dispo = $families_specialites_dispo = $spe_plats = $auto_plats = $non_auto_plats = $types_auto_dispo = $types_non_auto_dispo = $allPlats = $restoPlats = $composed = $assocs = $families = $names = $fathers = $jours = $services = $contraintes = $orders = $carte = $descriptions = $accompagnements = $pricePlats = [];
         $catalagCategories = [1 => [402, 406], 2 => [519, 500, 518, 521, 525, 522, 696, 1538, 2100], 3 => [668, 526, 690, 1530], 4 => [1528], 5 => [1952, 1958, 1548], 6 => [2167, 2168], 7 => [1942]];
         $tuplesPlats = [];
         $carte[1] = $carte[2] = $carte[3] = $carte[4] = $carte[5] = $carte[6] = $carte[7] = [];
         if ($row->carte) {
             foreach ($row->carte as $t => $ps) {
                 $carte[$t] = $ps;
                 foreach ($ps as $platCarte) {
                     $tuplesPlats[] = $platCarte['id'];
                 }
             }
         }
         $cursor = Model::Catalog()->where(['reseller_id', '=', (int) $resto['reseller_id']])->where(['is_challenge', '=', 1])->cursor();
         $collection = [];
         foreach ($cursor as $plat) {
             $segId = isAke($plat, 'segment_id', 0);
             if ($segId < 1) {
                 continue;
             }
             $plats = isAke($plat, 'plats', []);
             $accompagnements[$plat['id']] = isAke($plat, 'accompagnement', null);
             $descriptions[$plat['id']] = isAke($plat, 'description', null);
             if (!empty($plats)) {
                 $composed[$plat['segment_id']] = $plats;
             }
             $data = repo('segment')->getData((int) $plat['segment_id']);
             $ordre = isAke($data, 'ordre', 1);
             $plat['ordre'] = $ordre;
             $collection[] = $plat;
             $fathers[$plat['segment_id']] = repo('segment')->getFather($plat['segment_id'])['id'];
             $family = repo('segment')->getFamily($plat['segment_id']);
             foreach ($catalagCategories as $idCat => $segsCat) {
                 foreach ($family as $child) {
                     if (!isset($families[$plat['segment_id']])) {
                         $families[$plat['segment_id']] = [];
                     }
                     $families[$plat['segment_id']][$child['id']] = $child;
                     if (!in_array($plat['id'], $tuplesPlats)) {
                         if (in_array($child['id'], $segsCat)) {
                             unset($plat['_id']);
                             if ($plat['segment_id'] == 1548) {
                                 $carte[5][] = $plat;
                                 $p = Model::Catalog()->find((int) $plat['id']);
                                 $p->catalogcategory_id = 5;
                                 $p->save();
                             } else {
                                 $carte[$idCat][] = $plat;
                                 $p = Model::Catalog()->find((int) $plat['id']);
                                 $p->catalogcategory_id = (int) $idCat;
                                 $p->save();
                             }
                         }
                     }
                 }
             }
         }
         $collection = lib('collection', [$collection]);
         $collection->sortBy('ordre');
         $cursor = array_values($collection->toArray());
         $contraintes['jours'] = [];
         $contraintes['services'] = [];
         foreach ($cursor as $plat) {
             $s = Model::Segment()->find((int) $plat['segment_id']);
             if (!$s) {
                 continue;
             }
             $jours[$plat['id']] = isAke($plat, 'jours', []);
             $services[$plat['id']] = isAke($plat, 'services', []);
             if (!isset($contraintes['jours'][$plat['segment_id']])) {
                 $contraintes['jours'][$plat['segment_id']] = [];
             }
             if (!isset($contraintes['services'][$plat['segment_id']])) {
                 $contraintes['services'][$plat['segment_id']] = [];
             }
             $contraintes['jours'][$plat['segment_id']][$plat['id']] = isAke($plat, 'jours', []);
             $contraintes['services'][$plat['segment_id']][$plat['id']] = isAke($plat, 'services', []);
             if (!isset($assocs[$plat['id']])) {
                 $assocs[$plat['id']] = [];
                 $assocs[$plat['id']]['family'] = [];
                 $assocs[$plat['id']]['geo'] = [];
                 $assocs[$plat['id']]['auto'] = [];
                 $assocs[$plat['id']]['non_auto'] = [];
             }
             if (!isset($allPlats[$plat['segment_id']])) {
                 $allPlats[$plat['segment_id']] = [];
             }
             $restoPlats[$plat['segment_id']] = (int) $plat['id'];
             $pricePlats[$plat['id']] = (double) $plat['price'];
             $plat['data'] = repo('segment')->getData((int) $plat['segment_id']);
             $allPlats[$plat['segment_id']][] = $plat;
             $names[$plat['segment_id']] = $plats_names[$plat['segment_id']] = Model::Segment()->find((int) $plat['segment_id'])->name;
             $orders[$plat['segment_id']] = $plat['ordre'];
             $catalog_names[$plat['id']] = $plat['name'];
             $family = repo('segment')->getFamily((int) $plat['segment_id']);
             $assocs[$plat['id']]['family'][] = $plat['segment_id'];
             foreach ($family as $seg) {
                 if (!in_array($seg['id'], $families_plats_dispo)) {
                     $families_plats_dispo[] = $assocs[$plat['id']]['family'][] = $seg['id'];
                     $names[$seg['id']] = $seg['name'];
                 }
             }
             $mgeos = Model::Mealgeo()->where(['segment_id', '=', (int) $plat['segment_id']])->models();
             if ($mgeos->count() > 0) {
                 foreach ($mgeos as $mgeo) {
                     $s = Model::Segment()->find((int) $mgeo->resto_geo_id);
                     if (!$s) {
                         continue;
                     }
                     // if (!in_array($mgeo->resto_geo_id, $specialites_dispo)) {
                     if (!isset($spe_plats[$mgeo->resto_geo_id])) {
                         $spe_plats[$mgeo->resto_geo_id] = [];
                     }
                     $specialites_dispo[] = (int) $mgeo->resto_geo_id;
                     $names[$mgeo->resto_geo_id] = Model::Segment()->find((int) $mgeo->resto_geo_id)->name;
                     $family = repo('segment')->getFamily($mgeo->resto_geo_id);
                     $assocs[$plat['id']]['geo'][] = $mgeo->resto_geo_id;
                     $spe_plats[$mgeo->resto_geo_id][] = $plat['segment_id'];
                     foreach ($family as $seg) {
                         if (!isset($spe_plats[$seg['id']])) {
                             $spe_plats[$seg['id']] = [];
                         }
                         // $spe_plats[$seg['id']][] = $plat['segment_id'];
                         // $spe_plats[$mgeo->resto_geo_id][] = $plat['segment_id'];
                         if (!in_array($seg['id'], $families_specialites_dispo)) {
                             $families_specialites_dispo[] = $assocs[$plat['id']]['geo'][] = $seg['id'];
                             $names[$seg['id']] = $seg['name'];
                         }
                     }
                     // }
                 }
             }
             $mtypeautos = Model::Mealtype()->where(['segment_id', '=', (int) $plat['segment_id']])->models();
             if ($mtypeautos->count() > 0) {
                 foreach ($mtypeautos as $mtypeauto) {
                     // if (!in_array($mtypeauto->resto_type_id, $types_auto_dispo)) {
                     if (!isset($auto_plats[$mtypeauto->resto_type_id])) {
                         $auto_plats[$mtypeauto->resto_type_id] = [];
                     }
                     $auto_plats[$mtypeauto->resto_type_id][] = $plat['segment_id'];
                     $types_auto_dispo[] = (int) $mtypeauto->resto_type_id;
                     $names[$mtypeauto->resto_type_id] = Model::Segment()->find((int) $mtypeauto->resto_type_id)->name;
                     $family = repo('segment')->getFamily($mtypeauto->resto_type_id);
                     $assocs[$plat['id']]['auto'][] = $mtypeauto->resto_type_id;
                     foreach ($family as $seg) {
                         if (!isset($auto_plats[$seg['id']])) {
                             $auto_plats[$seg['id']] = [];
                         }
                         $auto_plats[$seg['id']][] = $plat['segment_id'];
                         $auto_plats[$mtypeauto->resto_type_id][] = $plat['segment_id'];
                         if (!in_array($seg['id'], $families_types_auto_dispo)) {
                             $families_types_auto_dispo[] = $assocs[$plat['id']]['auto'][] = $seg['id'];
                             $names[$seg['id']] = $seg['name'];
                         }
                     }
                     // }
                 }
             }
             $mtypeautos = Model::Mealnonauto()->where(['catalog_id', '=', (int) $plat['id']])->models();
             if ($mtypeautos->count() > 0) {
                 foreach ($mtypeautos as $mtypeauto) {
                     // if (!in_array($mtypeauto->resto_nonauto_id, $types_non_auto_dispo)) {
                     if (!isset($non_auto_plats[$mtypeauto->resto_nonauto_id])) {
                         $non_auto_plats[$mtypeauto->resto_nonauto_id] = [];
                     }
                     $non_auto_plats[$mtypeauto->resto_nonauto_id][] = $plat['segment_id'];
                     $types_non_auto_dispo[] = (int) $mtypeauto->resto_nonauto_id;
                     $names[$mtypeauto->resto_nonauto_id] = Model::Segment()->find((int) $mtypeauto->resto_nonauto_id)->name;
                     $family = repo('segment')->getFamily($mtypeauto->resto_nonauto_id);
                     $assocs[$plat['id']]['non_auto'][] = $mtypeauto->resto_nonauto_id;
                     foreach ($family as $seg) {
                         if (!isset($non_auto_plats[$seg['id']])) {
                             $non_auto_plats[$seg['id']] = [];
                         }
                         $non_auto_plats[$seg['id']][] = $plat['segment_id'];
                         // $non_auto_plats[$mtypeauto->resto_nonauto_id][] = $plat['segment_id'];
                         if (!in_array($seg['id'], $families_types_non_auto_dispo)) {
                             $families_types_non_auto_dispo[] = $assocs[$plat['id']]['non_auto'][] = $seg['id'];
                             $names[$seg['id']] = $seg['name'];
                         }
                     }
                     // }
                 }
             }
             $assocs[$plat['id']]['family'] = array_values(array_unique($assocs[$plat['id']]['family']));
             $assocs[$plat['id']]['geo'] = array_values(array_unique($assocs[$plat['id']]['geo']));
             $assocs[$plat['id']]['auto'] = array_values(array_unique($assocs[$plat['id']]['auto']));
             $assocs[$plat['id']]['non_auto'] = array_values(array_unique($assocs[$plat['id']]['non_auto']));
         }
         $pois = Model::Restopoi()->where(['sellzone_id', '=', (int) $resto['sellzone_id']])->where(['reseller_id', '=', (int) $resto['reseller_id']])->cursor();
         $distances = [];
         foreach ($pois as $poi) {
             if (!in_array($poi['segment_id'], $pois_dispo)) {
                 $poiSeg = Model::Segment()->find((int) $poi['segment_id']);
                 if ($poiSeg && (double) $poi['distance'] <= 0.5) {
                     $distances[$poi['segment_id']] = (double) $poi['distance'];
                     $pois_dispo[] = $poi['segment_id'];
                     $names[$poi['segment_id']] = $poiSeg->name;
                     $family = repo('segment')->getFamily((int) $poi['segment_id']);
                     foreach ($family as $seg) {
                         if (!in_array($seg['id'], $families_pois_dispo) && !in_array($seg['id'], $pois_dispo)) {
                             $families_pois_dispo[] = $seg['id'];
                             $names[$seg['id']] = $seg['name'];
                         }
                     }
                 }
             }
         }
         $row->reseller = Model::Reseller()->find((int) $resto['reseller_id'], false);
         $row->company = Model::Company()->where(['reseller_id', '=', (int) $resto['reseller_id']])->first(false);
         $zc = Model::Zechallenge()->where(['reseller_id', '=', (int) $resto['reseller_id']])->cursor()->first(true);
         if ($zc) {
             $status_zechallenge = $zc->status;
         } else {
             $status_zechallenge = 'WAITING';
             // Model::Zechallenge()->firstOrCreate([
             //     'reseller_id'   => (int) $resto['reseller_id']
             // ])->setStatus('WAITING')->save();
         }
         $segmentDatas = [];
         foreach ($names as $key => $n) {
             $datasK = repo('segment')->getData((int) $key);
             $segmentDatas[$key] = $datasK;
         }
         $orders = $grandfathers = [];
         foreach ($families as $s => $fathers) {
             $f = 0;
             foreach ($fathers as $father) {
                 if ($f == 2) {
                     $orders[$s] = isAke($father['data'], 'ordre', 1);
                     $grandfathers[$s] = $father;
                 }
                 $f++;
             }
         }
         $row->composed = $composed;
         $row->grandfathers = $grandfathers;
         $row->orders = $orders;
         $row->datas = $segmentDatas;
         $row->families = $families;
         $row->names = $names;
         $row->all_plats = $allPlats;
         $row->loc = $locationReseller;
         $row->options = $options;
         $row->schedules = $schedules;
         $row->status_zechallenge = $status_zechallenge;
         $q = Model::Zenews()->where(['status', '=', 'ACTIVE'])->where(['context', '=', 'resto'])->where(['reseller_id', '=', (int) $resto['reseller_id']])->cursor()->count();
         $hmz = Model::Myzelift()->where(['reseller_id', '=', (int) $resto['reseller_id']])->where(['status', '=', 'ACTIVE'])->first(true);
         if (!$hmz) {
             $hmz = false;
         } else {
             $hmz = true;
         }
         $row->has_myzelift = $hmz;
         $row->has_news = $q > 0 ? true : false;
         $row->pois = ['dispos' => $pois_dispo, 'distances' => $distances, 'families' => $families_pois_dispo];
         $row->specialites = ['dispos' => $specialites_dispo, 'families' => $families_specialites_dispo, 'plats' => $spe_plats];
         $row->auto = ['dispos' => $types_auto_dispo, 'families' => $families_types_auto_dispo, 'plats' => $auto_plats];
         $row->non_auto = ['dispos' => $types_non_auto_dispo, 'families' => $families_types_non_auto_dispo, 'plats' => $non_auto_plats];
         /* Rating */
         $rates = Model::Rating()->where(['reseller_id', '=', (int) $resto['reseller_id']])->cursor();
         $totalRate = 0;
         if ($rates->count() == 0) {
             $row->rate = 0;
         } else {
             foreach ($rates as $rateRow) {
                 $totalRate += (double) $rateRow['rate'];
             }
             $row->rate = (double) $totalRate / $rates->count();
         }
         $row->plats = ['ordre' => $orders, 'prices' => $pricePlats, 'segments' => array_keys($pricePlats), 'catalog' => $restoPlats, 'families' => $families_plats_dispo, 'names' => $plats_names, 'catalog_names' => $catalog_names, 'fathers' => $fathers];
         $resas = [];
         $customers = [];
         $old = Model::Restoreservation()->where(['reseller_id', '=', (int) $resto['reseller_id']])->where(['updated_at', '>=', strtotime('-6 month')])->cursor();
         foreach ($old as $resa) {
             $resas[] = ['account_id' => $resa['account_id'], 'date' => $resa['updated_at'], 'filter' => $resa['filter']];
             if (!isset($customers[$resa['account_id']])) {
                 $customers[$resa['account_id']] = 1;
             } else {
                 $customers[$resa['account_id']]++;
             }
         }
         $row->assocs = $assocs;
         $row->resas = $resas;
         $row->customers = $customers;
         $row->carte = $carte;
         $nc = $c = [];
         $categories = Model::Catalogcategory()->cursor();
         foreach ($categories as $cat) {
             $collection = lib('collection', [$carte[$cat['id']]])->sortBy('ordre')->toArray();
             $nc[$cat['name']] = array_values($collection);
         }
         $row->plats_ranges = $nc;
         $row->themes = lib('myzelift')->getThemesAffil($resto['reseller_id']);
         $extras = Model::Extradata()->where(['reseller_id', '=', (int) $resto['reseller_id']])->cursor()->first();
         if (empty($extras)) {
             $extras = ['access' => 'Prendre Tram 1 descendre à l\'arrêt Godrans.'];
         }
         $row->extras = $extras;
         $ups = [];
         $uplifts = Model::Uplift()->where(['status', '=', 'ACTIVE'])->where(['start', '<=', time()])->where(['reseller_id', '=', (int) $resto['reseller_id']])->cursor();
         foreach ($uplifts as $uplift) {
             unset($uplift['_id']);
             unset($uplift['created_at']);
             unset($uplift['updated_at']);
             // unset($uplift['reseller_id']);
             $type = Model::Uplifttype()->find($uplift['uplifttype_id'], false);
             unset($type['created_at']);
             unset($type['updated_at']);
             unset($type['id']);
             $uplift['type'] = $type;
             if (!isset($uplift['duration']) && isset($uplift['upliftduration_id'])) {
                 $duration = Model::Upliftduration()->find($uplift['upliftduration_id'], false);
                 unset($duration['created_at']);
                 unset($duration['updated_at']);
                 unset($duration['id']);
                 $uplift['duration'] = $duration;
             }
             $views = isset($uplift['views']) ? $uplift['views'] : 0;
             if (100 >= $views) {
                 $ups[] = $uplift;
             } else {
                 if ($uplift['end'] > time()) {
                     $ups[] = $uplift;
                 }
             }
         }
         $row->uplifts = $ups;
         $row->jours = $jours;
         $row->services = $services;
         $row->contraintes = $contraintes;
         $row->accompagnements = $accompagnements;
         $row->descriptions = $descriptions;
         $row->contraintes = $contraintes;
         $row->save();
     }
 }