Example #1
0
 private function listBetween($offerIn, $model, $is_calendar = true)
 {
     $now = time();
     $collection = lib('collection');
     $quantity = $offerIn->quantity;
     $options = $offerIn->options;
     $language = $offerIn->language;
     $distance_maxB = $offerIn->distance_max;
     $start = $offerIn->start;
     $tolerance = $offerIn->tolerance;
     $oresellerid = $offerIn->reseller_id;
     $segment_id = $offerIn->segment_id;
     $start = is_null($start) ? $now + 4 * 3600 : $start;
     $tolerance = is_null($tolerance) ? 0 : $tolerance;
     $distance_maxB = is_null($distance_maxB) ? 0 : (double) $distance_maxB;
     $delai = $start - $now;
     $livraison = false;
     if (isset($options['shipping_costs'])) {
         if (isset($options['shipping_costs']['default'])) {
             $livraison = 'oui' == $options['shipping_costs']['default'] ? true : false;
         }
     }
     if ($delai <= 0) {
         return [];
     }
     $delai /= 3600;
     $list = Arrays::get($model, 'formulaire_achat.elements.quantity.values', []);
     $unite = Arrays::get($model, 'formulaire_achat.elements.quantity.unite', 'heure');
     $optionsPrice = Arrays::get($model, 'formulaire_achat.elements.quantity.options.price', []);
     $optionsDiscount = Arrays::get($model, 'formulaire_achat.elements.quantity.options.discount', []);
     $optionsFixedCosts = Arrays::get($model, 'formulaire_achat.elements.quantity.options.fixed_costs', []);
     $optionsTravelCosts = Arrays::get($model, 'formulaire_achat.elements.quantity.options.travel_costs', []);
     $optionsShippingCosts = Arrays::get($model, 'formulaire_achat.elements.quantity.options.shipping_costs', []);
     $locOfferIn = getLocation($offerIn);
     $queryproducts = Model::Product()->where(['segment_id', '=', $segment_id])->where(['sellzone_id', '=', $offerIn->sellzone_id]);
     if (!is_null($oresellerid)) {
         $queryproducts->where(['reseller_id', '=', (int) $oresellerid]);
     }
     $products = $queryproducts->exec(true);
     foreach ($products as $product) {
         $price = $product->price;
         $fixed_costs = $product->fixed_costs;
         $shipping_costs = $product->shipping_costs;
         $travel_costs = $product->travel_costs;
         $discount = $product->discount;
         $delai_presta = lib('option')->get('delai.intervention', $product, false);
         $montant_min = (double) lib('option')->get('montant.intervention', $product, 0);
         $distance_max = (double) lib('option')->get('zone.intervention', $product, 0);
         if (false !== $delai_presta) {
             if ($delai < $delai_presta) {
                 continue;
             }
         }
         $fixed_costs_free_from_price = 0;
         if (is_null($fixed_costs) || !is_array($fixed_costs)) {
             $fixed_costs = 0;
         } else {
             if (isset($fixed_costs['default'])) {
                 if (isset($fixed_costs['default']['value'])) {
                     $fixed_costs = (double) $fixed_costs['default']['value'];
                 }
                 if (isset($fixed_costs['default']['free_from_price'])) {
                     $fixed_costs_free_from_price = (double) $fixed_costs['default']['free_from_price'];
                 }
             } else {
                 $fixed_costs = 0;
             }
         }
         $travel_costs_free_from_price = 0;
         if (is_null($travel_costs) || !is_array($travel_costs)) {
             $travel_costs = 0;
         } else {
             if (isset($travel_costs['default'])) {
                 if (isset($travel_costs['default']['value'])) {
                     $travel_costs = (double) $travel_costs['default']['value'];
                 }
                 if (isset($travel_costs['default']['free_from_price'])) {
                     $travel_costs_free_from_price = (double) $travel_costs['default']['free_from_price'];
                 }
             } else {
                 $travel_costs = 0;
             }
         }
         if (true === $livraison) {
             $shipping_costs_free_from_price = 0;
             if (is_null($shipping_costs) || !is_array($shipping_costs)) {
                 $shipping_costs = 0;
             } else {
                 if (isset($shipping_costs['default'])) {
                     if (isset($shipping_costs['default']['value'])) {
                         $shipping_costs = (double) $shipping_costs['default']['value'];
                     }
                     if (isset($shipping_costs['default']['free_from_price'])) {
                         $shipping_costs_free_from_price = (double) $shipping_costs['default']['free_from_price'];
                     }
                 } else {
                     $shipping_costs = 0;
                 }
             }
         }
         if (!is_null($discount)) {
             $discount_quantity = 0;
             $discount_amount = 0;
             if (isset($discount['default'])) {
                 if (isset($discount['default']['quantity'])) {
                     $discount_quantity = (double) $discount['default']['quantity'];
                 }
                 if (isset($discount['default']['amount'])) {
                     $discount_amount = (double) $discount['default']['amount'];
                 }
             }
         } else {
             $discount_quantity = 0;
             $discount_amount = 0;
         }
         $reseller = Model::Reseller()->find($product->reseller_id);
         if ($reseller) {
             $min = isAke($quantity, 'min', false);
             $max = isAke($quantity, 'max', false);
             if (false === $min || false === $max) {
                 return [];
             }
             foreach ($price as $productIndex => $productInfos) {
                 $cost = isAke($productInfos, 'cost', false);
                 if ($unite == 'minute') {
                     $duration = isAke($productInfos, 'duration', false);
                     if (false === $duration || false === $cost) {
                         continue;
                     }
                     $min = (int) $min;
                     $max = (int) $max;
                     $duration = (int) $duration;
                     $cost = (double) $cost;
                     $checkContinue = $duration >= $min && $duration <= $max;
                 } else {
                     $cost = (double) $cost;
                     $min = (double) $min;
                     $max = (double) $max;
                     $checkContinue = $cost >= $min && $cost <= $max;
                 }
                 if ($checkContinue) {
                     $item = [];
                     $amount = $cost;
                     $amount = (double) $amount;
                     if (true == $livraison && $shipping_costs > 0 && $shipping_costs_free_from_price > 0) {
                         if ($shipping_costs_free_from_price <= $amount) {
                             $shipping_costs = 0;
                         }
                     }
                     if (true === $livraison) {
                         $item['shipping_costs'] = $shipping_costs;
                     }
                     if ($travel_costs > 0 && $travel_costs_free_from_price > 0) {
                         if ($travel_costs_free_from_price <= $amount) {
                             $travel_costs = 0;
                         }
                     }
                     if ($fixed_costs > 0 && $fixed_costs_free_from_price > 0) {
                         if ($fixed_costs_free_from_price <= $amount) {
                             $fixed_costs = 0;
                         }
                     }
                     /* on calcule la ristourne sur la quantité si elle existe */
                     $everDiscount = false;
                     if ($quantity >= $discount_quantity && 0 < $discount_amount) {
                         $discountAmount = (double) ($amount * $discount_amount) / 100;
                         $amount = (double) $amount - $discountAmount;
                         $everDiscount = true;
                     }
                     /* si pas de ristourne quantité on regarde s'il ya une ristourne de prix */
                     if (!is_null($discount) && is_array($discount)) {
                         $discountPrice = isAke($discount, 'price', false);
                         if (false !== $discountPrice) {
                             $discount_quantity = 0;
                             $discount_amount = 0;
                             if (isset($discountPrice['quantity'])) {
                                 $discount_quantity = (double) $discountPrice['quantity'];
                             }
                             if (isset($discountPrice['amount'])) {
                                 $discount_amount = (double) $discountPrice['amount'];
                             }
                             if ($amount >= $discount_quantity && 0 < $discount_amount) {
                                 $discountAmount = (double) ($amount * $discount_amount) / 100;
                                 $amount = (double) $amount - $discountAmount;
                                 $everDiscount = true;
                             }
                         }
                     }
                     /* on peut ajouter les frais fixes et de livraison */
                     $amount = $amount + $fixed_costs + $shipping_costs;
                     $discountAmount = 0;
                     $hasAgenda = lib('option')->get('agenda.partage.' . $segment_id, $reseller, false);
                     /* on regarde si le revendeur a spécifié un montant minumum pour accepter une presta */
                     if (0 == $montant_min) {
                         $montant_min = (double) lib('option')->get('montant.intervention.' . $segment_id, $reseller, 0);
                     }
                     /* si tel est le cas on s'assure que le montant minimum est atteint */
                     if (0 < $montant_min) {
                         if ($amount < $montant_min) {
                             continue;
                         }
                     }
                     /* on regarde si le revendeur a spécifié un délai minumum pour accepter une presta */
                     if (false === $delai_presta) {
                         $delai_presta = lib('option')->get('delai.intervention.' . $segment_id, $reseller, false);
                         /* si tel est le cas on s'assure que le délai minimum est respecté */
                         if (false !== $delai_presta) {
                             if ($delai < $delai_presta) {
                                 continue;
                             }
                         }
                     }
                     /* si une langue est demandée on vérifie si le revendeur la parle, sinon, on passe au suivant */
                     if (!is_null($language)) {
                         $speak = lib('option')->get('langue.' . $segment_id . '.' . $language, $reseller, false);
                         if (false === $speak) {
                             continue;
                         }
                     }
                     if (o == $distance_max) {
                         $distance_max = lib('option')->get('zone.intervention.' . $segment_id, $reseller, 0);
                     }
                     $locReseller = getLocation($reseller);
                     $distance = distanceKmMiles($locOfferIn['lng'], $locOfferIn['lat'], $locReseller['lng'], $locReseller['lat']);
                     $km = (double) $distance['km'];
                     /* si la distance maximale demandée par l'acheteur est supérieure à la distance calculée
                           on ne prend pas cette offre
                        */
                     if ($distance_maxB > 0) {
                         if ($km > $distance_maxB) {
                             continue;
                         }
                     }
                     /* si la distance maximale demandée par le vendeur est supérieure à la distance calculée
                           on ne prend pas cette offre
                        */
                     if ($distance_max > 0) {
                         if ($km > $distance_max) {
                             continue;
                         }
                     }
                     /* on ajoute les frais de déplacement le cas échéant */
                     if (0 < $travel_costs) {
                         $travel_costs = (double) $travel_costs * $km;
                         $amount += $travel_costs;
                     }
                     $item['quantity'] = $quantity;
                     $item['amount'] = $amount;
                     $item['discount'] = $discountAmount;
                     $item['fixed_costs'] = $fixed_costs;
                     $item['travel_costs'] = $travel_costs;
                     $item['distance'] = $km;
                     $item['reseller_id'] = $reseller->id;
                     /* si c'est une presta calendrier et que le revendeur n'a pas de calendrier, on ajoute un attribut à l'item */
                     if (true === $is_calendar && false === $hasAgenda) {
                         $item['reseller_calendar'] = false;
                     }
                     if (true === $is_calendar && false !== $hasAgenda) {
                         $item['reseller_calendar'] = true;
                         $duration = $this->duration($quantity, $unite, $offerIn->segment_id, $reseller->id);
                         $startMin = (int) $start - $tolerance;
                         $startMax = (int) $start + $tolerance;
                         $endMin = (int) $startMin + $duration;
                         $endMax = (int) $startMax + $duration;
                         $availabilities = lib('agenda')->getAvailabilitiesByResellerId($startMin, $endMax, $reseller->id);
                         if (!empty($availabilities)) {
                             if (!is_null($language)) {
                                 $find = false;
                                 foreach ($availabilities as $availability) {
                                     $employee = Model::Reselleremployee()->find($availability['reselleremployee_id']);
                                     if ($employee) {
                                         $speak = lib('option')->get('langue.' . $language, $employee, false);
                                         if (false !== $speak) {
                                             $item['availability_id'] = $availability['id'];
                                             $item['availability_start'] = $availability['start'];
                                             $item['availability_end'] = $availability['end'];
                                             $item['reselleremployee_id'] = $availability['reselleremployee_id'];
                                             $find = true;
                                             break;
                                         }
                                     }
                                 }
                                 if (false === $find) {
                                     continue;
                                 }
                             } else {
                                 $availability = Arrays::first($availabilities);
                                 $item['availability_id'] = $availability['id'];
                                 $item['availability_start'] = $availability['start'];
                                 $item['availability_end'] = $availability['end'];
                                 $item['reselleremployee_id'] = $availability['reselleremployee_id'];
                             }
                         } else {
                             /* si pas de dispo on passe */
                             continue;
                         }
                     }
                     /* on peut créer l'offre out et ajouter l'offre au retour */
                     $data = $item;
                     $data['offerin_id'] = $offerIn->id;
                     $data['account_id'] = $offerIn->account_id;
                     $data['segment_id'] = $segment_id;
                     $data['status_id'] = (int) lib('status')->getId('offerout', 'SHOWN');
                     $offerOut = Model::Offerout()->create($data)->save();
                     $item['offerout_id'] = $offerOut->id;
                     $collection[] = $item;
                 }
             }
         }
     }
     /* on ordonne les offres par distance décroissante si à domicile sinon par prix */
     if ($collection->count() > 0) {
         if ($livraison) {
             $collection->sortBy('distance');
         } else {
             $collection->sortBy('amount');
         }
     }
     return $collection->toArray();
 }
Example #2
0
 public function consolidate($reseller_id = null)
 {
     $q = Model::Product();
     if (!is_null($reseller_id)) {
         $q->where(['reseller_id', '=', (int) $reseller_id]);
     }
     $products = $q->cursor();
     foreach ($products as $product) {
         if (isset($product['reseller_id']) && isset($product['segment_id']) && isset($product['sellzone_id'])) {
             $row = Model::Productdata()->firstOrCreate(['reseller_id' => (int) $product['reseller_id'], 'segment_id' => (int) $product['segment_id'], 'sellzone_id' => (int) $product['sellzone_id']]);
             unset($product['_id']);
             $row->product = $product;
             $family = repo('segment')->getFamilyfromItem((int) $product['segment_id']);
             if (!empty($family)) {
                 $market = current($family);
                 $market_id = (int) $market['id'];
             } else {
                 $market_id = $segment_id;
             }
             $company = Model::Company()->where(['reseller_id', '=', (int) $product['reseller_id']])->first(false);
             $reseller = Model::Reseller()->find((int) $product['reseller_id'], false);
             $segment = Model::Segment()->find((int) $product['segment_id'], false);
             $location = lib('utils')->remember('has.locations.companies.' . $product['reseller_id'], function ($reseller_id) {
                 $company = Model::Company()->where(['reseller_id', '=', (int) $reseller_id])->first(true);
                 if ($company) {
                     $coords = lib('geo')->getCoordsMap($company->address . ' ' . $company->zip . ' ' . $company->city);
                     $loc = ['lng' => $coords['lng'], 'lat' => $coords['lat']];
                 }
                 return $loc;
             }, Model::Company()->getAge(), [$product['reseller_id']]);
             $status = (int) lib('status')->getId('reseller', 'REGISTER') == $reseller['status_id'];
             $row->company = $company;
             $row->segment = $segment;
             $row->status = $status;
             $row->location = $location;
             $row->reseller = $reseller;
             $options = [];
             $resellerObj = Model::Reseller()->model($reseller);
             $hasAgenda = lib('option')->get('agenda.partage', $resellerObj, false);
             $distanceMaxReseller = (double) lib('option')->get('zone.intervention.' . $market_id, $resellerObj, 0);
             $delai_presta = lib('option')->get('delai.intervention.' . $market_id, $resellerObj, 0);
             $minAmount = lib('option')->get('montant.intervention.' . $market_id, $resellerObj, 0);
             $options['has_agenda'] = $hasAgenda;
             $options['distance_max'] = $distanceMaxReseller;
             $options['delai_min'] = $delai_presta;
             $options['amount_min'] = $minAmount;
             $options['languages'] = [];
             $languages = ['anglais', 'espagnol', 'allemand', 'italien', 'néerlandais', 'portugais', 'russe', 'japonais', 'chinois'];
             foreach ($languages as $language) {
                 $speak = lib('option')->get('langue.' . $market_id . '.' . $language, $resellerObj, false);
                 $options['languages'][$language] = $speak;
             }
             $row->options = $options;
             $row->save();
         }
     }
 }
Example #3
0
 public function getItemVendeur($segment_id)
 {
     $user = session('user')->getUser();
     if ($user) {
         if (Arrays::is($user)) {
             if (!is_integer($segment_id)) {
                 return [];
             }
             if (!isset($user['employee'])) {
                 return false;
             }
             if (!isset($user['settings'])) {
                 return false;
             }
             if (!isset($user['settings']['employee_index'])) {
                 return false;
             }
             if (!isset($user['employee'][$user['settings']['employee_index']])) {
                 return false;
             }
             // if (!isset($user['employee'][$user['settings']['employee_index']]['id'])) {
             //     return false;
             // }
             if (!isset($user['employee'][$user['settings']['employee_index']]['reseller_id'])) {
                 return false;
             }
             // $reselleremployee_id    = $user['employee'][$user['settings']['employee_index']]['id'];
             $reseller_id = $user['employee'][$user['settings']['employee_index']]['reseller_id'];
             $reseller = Model::Reseller()->find((int) $reseller_id);
             // $reselleremployee   = Model::Reselleremployee()->find((int) $reselleremployee_id);
             if ($reseller) {
                 // if ($reseller && $reselleremployee) {
                 $item = Model::Product()->where(['reseller_id', '=', (int) $reseller->id])->where(['segment_id', '=', (int) $segment_id])->first(true);
                 if ($item) {
                     $tab = $item->toArray();
                     unset($tab['id']);
                     unset($tab['created_at']);
                     unset($tab['updated_at']);
                     unset($tab['reseller_id']);
                     return $tab;
                 }
             }
         }
     }
     return [];
 }