Example #1
0
 public function paid($offerout_id)
 {
     $error = [];
     try {
         $offerout = Model::Offerout()->findOrFail((int) $offerout_id);
     } catch (Exception $e) {
         $error['error'] = 'no_offer_out';
         return $error;
     }
     try {
         $offerin = Model::Offerin()->findOrFail((int) $offerout->offerin_id);
     } catch (Exception $e) {
         $error['error'] = 'no_offer_in';
         return $error;
     }
     return $this->status('PAID', $offerin, $offerout);
 }
Example #2
0
 public function get($id)
 {
     $offer = Model::Offerin()->findOrFail((int) $id);
     return $offer->toArray();
 }
Example #3
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 #4
0
 public function calculateBasket($offerInId, $is_calendar = true)
 {
     $now = time();
     $collection = lib('collection');
     $offerIn = Model::Offerin()->find($offerInId);
     if ($offerIn) {
         $quantity = $offerIn->quantity;
         $options = $offerIn->options;
         $language = $offerIn->language;
         $distance_maxB = $offerIn->distance_max;
         $start = $offerIn->start;
         $tolerance = $offerIn->tolerance;
         $oresellerid = $offerIn->reseller_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;
         $model = $this->getStaticModel((int) $offerIn->segment_id);
         $segment_id = $offerIn->segment_id;
         if (empty($model)) {
             return [];
         }
         $modelType = Arrays::get($model, 'formulaire_achat.elements.quantity.type', 'min_max');
         if ($modelType == 'catalog') {
             return $this->catalog($offerIn, $model, $is_calendar);
         }
         if ($modelType == 'list_between') {
             return $this->listBetween($offerIn, $model, $is_calendar);
         }
         $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) {
                 if ($modelType == 'list_between' && !is_array($price)) {
                     continue;
                 }
                 switch ($modelType) {
                     case 'multiple':
                     case 'multiple_one':
                         $one = $modelType == 'multiple_one';
                         if (empty($optionsPrice)) {
                             list($amount, $item) = $this->calculateAmountWithMultiple($quantity, $price, $list, $one);
                         } else {
                             if (empty($options) || !is_array($price)) {
                                 return [];
                             }
                             list($amount, $item) = $this->calculateOptionsPrice($quantity, $optionsPrice, $options, $price, $one);
                         }
                         break;
                     case 'list_quantified':
                         $findIndex = false;
                         $item = [];
                         foreach ($list as $indexList => $keyList) {
                             $checkIndex = isset($quantity[$indexList]);
                             $checkPrice = isset($price[$indexList]);
                             if ($checkIndex && $checkPrice) {
                                 $amount = (double) $price[$indexList] * $quantity[$indexList];
                                 $findIndex = true;
                                 break;
                             }
                             if (false === $findIndex) {
                                 $amount = 0;
                             }
                         }
                         break;
                     case 'list_one':
                         $findIndex = false;
                         $item = [];
                         foreach ($list as $indexList => $keyList) {
                             $checkIndex = isset($quantity[$indexList]);
                             $checkPrice = isset($price[$indexList]);
                             if ($checkIndex && $checkPrice) {
                                 $amount = (double) $price[$indexList];
                                 $findIndex = true;
                                 break;
                             }
                             if (false === $findIndex) {
                                 $amount = 0;
                             }
                         }
                         break;
                     default:
                         $amount = (double) $price * $quantity;
                         $item = [];
                         break;
                 }
                 $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, $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['segment_id'] = $segment_id;
                 $data['account_id'] = $offerIn->account_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();
 }