Example #1
0
 public function validate($offerout_id, $reselleremployee_id)
 {
     $error = [];
     try {
         $offerout = Model::Offerout()->findOrFail((int) $offerout_id);
     } catch (Exception $e) {
         $error['error'] = 'no_offer_out';
         return $error;
     }
     try {
         $reselleremployee = Model::Reselleremployee()->findOrFail((int) $reselleremployee_id);
     } catch (Exception $e) {
         $error['error'] = 'employee_unknown';
         return $error;
     }
     try {
         $offerin = Model::Offerin()->findOrFail((int) $offerout->offerin_id);
     } catch (Exception $e) {
         $error['error'] = 'no_offer_out';
         return $error;
     }
     if ($reselleremployee->reseller_id != $offerout->reseller_id) {
         $error['error'] = 'employee_unknown';
         return $error;
     }
     $offerout->reselleremployee_id = (int) $reselleremployee_id;
     if (isset($offerout->start) && isset($offerout->end)) {
         $isLock = redis()->get('lock.offerout.' . $reselleremployee_id);
         if (!empty($isLock)) {
             for ($i = 0; $i < 10; $i++) {
                 $isLock = redis()->get('lock.offerout.' . $reselleremployee_id);
                 if (empty($isLock)) {
                     break;
                 }
                 sleep(0.2);
             }
             if (!empty($isLock)) {
                 $error['error'] = 'is_lock';
                 return $error;
             }
         } else {
             redis()->set('lock.offerout.' . $reselleremployee_id, true);
             redis()->expire('lock.offerout.' . $reselleremployee_id, 10);
         }
         $check = $this->employeeIsYetAvailable((int) $offerout_id, (int) $reselleremployee_id);
         if (!$check) {
             $error['error'] = 'already_taken';
             redis()->del('lock.offerout.' . $reselleremployee_id);
             return $error;
         } else {
             lib('agenda')->addAppointment((int) $offerout->start, (int) $offerout->end, (int) $reselleremployee->reseller_id, (int) $reselleremployee_id, (int) $offerout_id);
             redis()->del('lock.offerout.' . $reselleremployee_id);
         }
     }
     if (isset($offerin->account_id)) {
         $offerout->account_id = (int) $offerin->account_id;
         $offerout = $offerout->save();
     }
     if (isset($offerin->company_id)) {
         $offerout->company_id = (int) $offerin->account_id;
         $offerout = $offerout->save();
     }
     return $this->status('TO_PAY', $offerin, $offerout);
 }
Example #2
0
 public function setOfferOut($offerin_id, $category, $reselleremployee_id, array $articles)
 {
     $reselleremployee = Model::Reselleremployee()->find($reselleremployee_id);
     $offerin = Model::Offerin()->find($offerin_id);
     if ($reselleremployee && $offerin) {
         $check = Model::Offerout()->where(['reselleremployee_id', '=', $reselleremployee_id])->where(['offerin_id', '=', $offerin_id])->where(['category', '=', $category])->count();
         if ($check > 0) {
             return 0;
         }
         $offer = Model::Offerout()->create(['status_id' => getStatus('NOT_COMPLETED'), 'reseller_id' => $reselleremployee->reseller_id, 'reselleremployee_id' => $reselleremployee_id, 'offerin_id' => $offerin_id, 'category' => $category, 'zip' => $offerin->zip, 'company_id' => $offerin->company_id, 'people_id' => $offerin->people_id, 'date_offerin' => $offerin->date, 'expiration_offerin' => $offerin->expiration, 'global' => $offerin->global, 'statusofferin' => $offerin->status_id, 'market' => $offerin->market, 'universe' => $offerin->universe])->save();
         foreach ($articles as $articlein_id) {
             $article = Model::Articlein()->find($articlein_id);
             if ($article) {
                 if ($article->offerin_id == $offerin_id) {
                     $a = $article->assoc();
                     unset($a['id']);
                     unset($a['created_at']);
                     unset($a['updated_at']);
                     unset($a['deleted_at']);
                     $a['offerout_id'] = $offer->id;
                     $a['price'] = 0;
                     if ($a['item_id'] > 0) {
                         $seg = jmodel('segment')->find($a['item_id']);
                         if ($seg) {
                             $a['name'] = $seg->name;
                         }
                     }
                     Model::Articleout()->create($a)->save();
                 } else {
                     $offer->delete();
                     return 'Article inexistant';
                 }
             } else {
                 $offer->delete();
                 return 'Article inexistant';
             }
         }
         return $offer->id;
     } else {
         return 'offre inconnue';
     }
 }
Example #3
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 #4
0
 public function getEmployeesCan($start, $end, $reseller_id)
 {
     if (!is_integer($start) || !is_integer($end) || !is_integer($reseller_id)) {
         throw new Exception('All arguments of this method must be of type integer');
     }
     if ($end < $start) {
         throw new Exception('End must be greater than start.');
     }
     $collection = [];
     $date = lib('time')->createFromTimestamp((int) $start);
     $midnight = (int) $date->startOfDay()->getTimestamp();
     $day = (string) $date->frenchDay();
     $row = Model::Schedule()->where(['day', '=', (string) $day])->where(['reseller_id', '=', (int) $reseller_id])->first(true);
     $reseller = Model::findOrFail((int) $reseller_id);
     if ($row) {
         $amStart = lib('agenda')->transform((string) $row->am_start, (int) $midnight);
         $amEnd = lib('agenda')->transform((string) $row->am_end, (int) $midnight);
         $pmStart = lib('agenda')->transform((string) $row->pm_start, (int) $midnight);
         $pmEnd = lib('agenda')->transform((string) $row->pm_end, (int) $midnight);
         $continue = true;
         if ($start > $pmEnd) {
             $continue = false;
         } elseif ($start > $amEnd && $start < $pmStart) {
             $continue = false;
         } elseif ($start < $amStart) {
             $continue = false;
         }
         if (true === $continue) {
             if ($end > $pmEnd) {
                 $continue = false;
             } elseif ($end > $amEnd && $end < $pmStart) {
                 $continue = false;
             }
         }
         if (true === $continue) {
             $employees = Model::Reselleremployee()->where(['reseller_id', '=', (int) $reseller_id])->cursor();
             foreach ($employees as $employee) {
                 $hasAppointment = lib('agenda')->hasAppointments((int) $start, (int) $end, (int) $employee['id']);
                 $hasVacations = lib('agenda')->hasVacations((int) $start, (int) $end, (int) $employee['id']);
                 if (false === $hasAppointment && false === $hasVacations) {
                     $collection[] = (int) $employee['id'];
                 }
             }
         }
     }
     return $collection;
 }
Example #5
0
 public function delVacation($start, $end, $reselleremployee_id)
 {
     if (!is_integer($start)) {
         throw new Exception("start must be an integer timestamp.");
     }
     if (!is_integer($end)) {
         throw new Exception("end must be an integer timestamp.");
     }
     if (!is_integer($reselleremployee_id)) {
         throw new Exception("reselleremployee_id must be an integer id.");
     }
     if ($start > $end) {
         throw new Exception("Start must be lower than end.");
     }
     $employee = Model::Reselleremployee()->find((int) $reselleremployee_id);
     if ($employee) {
         $employee = $employee->assoc();
         $vacations = Model::Vacation()->where(['start', '=', (int) $start])->where(['end', '=', (int) $end])->where(['reselleremployee_id', '=', (int) $reselleremployee_id])->exec(true);
         foreach ($vacations as $vacation) {
             $pivots = $vacation->pivots(Model::Availability()->model())->exec(true);
             if (!empty($pivots)) {
                 foreach ($pivots as $pivot) {
                     $availability = $pivot->availability();
                     if ($availability) {
                         $availability->detach($vacation);
                         $availability->delete();
                     }
                 }
             }
             $time = lib('time');
             $day = (string) $time->createFromTimestamp((int) $vacation->start)->frenchDay();
             $midnight = (int) $time->createFromTimestamp((int) $vacation->start)->startOfDay()->getTimestamp();
             $vacation->delete();
             /* On récupère les horaires pour le jour */
             $schedules = Model::Schedule()->where(['day', '=', (string) $day])->where(['reseller_id', '=', (int) $employee['reseller_id']])->exec();
             foreach ($schedules as $schedule) {
                 $amStart = $this->transform($schedule['am_start'], (int) $midnight);
                 $amEnd = $this->transform($schedule['am_end'], (int) $midnight);
                 $pmStart = $this->transform($schedule['pm_start'], (int) $midnight);
                 $pmEnd = $this->transform($schedule['pm_end'], (int) $midnight);
                 /* on récupère les events du matin et on vérifie si les dispos sont à jour sinon on les ajoute */
                 $amEvents = Model::Appointment()->where(['reselleremployee_id', '=', (int) $reselleremployee_id])->between('start', (int) $amStart, (int) $amEnd)->between('end', (int) $amStart, (int) $amEnd)->exec();
                 foreach ($amEvents as $amEvent) {
                     if ($amEvent['start'] != $amStart) {
                         $availability = $this->addAvailability((int) $amStart, (int) $amEvent['start'], (int) $employee['reseller_id'], (int) $reselleremployee_id);
                         $availability->attach(Model::Schedule()->find((int) $schedule['id']), ['start' => (int) $amStart, 'end' => (int) $amEvent['start']]);
                     }
                     if ($amEvent['end'] != $amEnd) {
                         $availability = $this->addAvailability((int) $amEvent['start'], (int) $amEnd, (int) $employee['reseller_id'], (int) $reselleremployee_id);
                         $availability->attach(Model::Schedule()->find((int) $schedule['id']), ['start' => (int) $amEvent['start'], 'end' => (int) $amEnd]);
                     }
                 }
                 /* on récupère les events de l'après-midi et on vérifie si les dispos sont à jour sinon on les ajoute */
                 $pmEvents = Model::Appointment()->where(['reselleremployee_id', '=', (int) $reselleremployee_id])->between('start', (int) $pmStart, (int) $pmEnd)->between('end', (int) $pmStart, (int) $pmEnd)->exec();
                 foreach ($pmEvents as $pmEvent) {
                     if ($pmEvents['start'] != $pmStart) {
                         $availability = $this->addAvailability((int) $pmStart, (int) $pmEvent['start'], (int) $employee['reseller_id'], (int) $reselleremployee_id);
                         $availability->attach(Model::Schedule()->find((int) $schedule['id']), ['start' => (int) $pmStart, 'end' => (int) $amEnd]);
                     }
                     if ($pmEvents['end'] != $pmEnd) {
                         $availability = $this->addAvailability((int) $pmEvent['end'], (int) $pmEnd, (int) $employee['reseller_id'], (int) $reselleremployee_id);
                         $availability->attach(Model::Schedule()->find((int) $schedule['id']), ['start' => (int) $pmEvent['end'], 'end' => (int) $pmEnd]);
                     }
                 }
                 return true;
             }
         }
     }
     return false;
 }