Example #1
0
 public function makeOffersOut($offerin)
 {
     $collection = [];
     $errors = [];
     $infos = [];
     $resellers = [];
     $minStartDelay = Config::get('offerin.delay', 115) * 60;
     $model = lib('model')->getStaticModel((int) $offerin->segment_id);
     $isCalendar = isAke($model, 'is_calendar', true);
     $now = time();
     $isPro = false;
     if (!isset($offerin->sellzone_id)) {
         $offerin->sellzone_id = 1;
         $offerin->save();
     }
     if (isset($offerin->company_id)) {
         $address = Model::Companyaddress()->findOrFail((int) $offerin->address_id);
         $isPro = true;
     } else {
         $address = Model::Accountaddress()->findOrFail((int) $offerin->address_id);
     }
     if (true === $isPro) {
         if (!isset($offerin->company_id)) {
             $offerin->company_id = (int) $address->company_id;
             $offerin = $offerin->save();
         }
     } else {
         if (!isset($offerin->account_id)) {
             $offerin->account_id = (int) $address->account_id;
             $offerin = $offerin->save();
         }
     }
     $locationOffer = lib('utils')->remember('get.location.address.' . $address->id, function ($address) {
         return getLocation($address);
     }, $isPro ? Model::Companyaddress()->getAge() : Model::Accountaddress()->getAge(), [$address]);
     if (empty($locationOffer)) {
         $coords = lib('geo')->getCoords($address->address . ' ' . $address->zip . ' ' . $address->city, 250);
         setLocation($address, $coords['lng'], $coords['lat']);
         $locationOffer = ['lng' => $coords['lng'], 'lat' => $coords['lat']];
     }
     $segment_id = (int) $offerin->segment_id;
     $family = repo('segment')->getFamilyfromItem((int) $segment_id);
     if (!empty($family)) {
         $market = current($family);
         $market_id = (int) $market['id'];
     } else {
         $market_id = $segment_id;
     }
     $offer = $offerin->toArray();
     if (isset($offer['date']) && isset($offer['time'])) {
         list($y, $m, $d) = explode('-', $offer['date'], 3);
         list($h, $i, $s) = explode(':', $offer['time'], 3);
         $start = mktime($h, $i, $s, $m, $d, $y);
     } else {
         $start = $offerin->created_at;
     }
     if ($start < $now) {
         $errors['starttime_error'] = 'Start time [' . date('Y-m-d H:i:s', $start) . '] is before now.';
     }
     $delaySeconds = $start - $now;
     $delai = (double) round($delaySeconds / 3600, 2);
     if ($delaySeconds < $minStartDelay) {
         $errors['delay_incorrect'] = 'The delay is incorrect [' . $delaySeconds . ' seconds]. The delay me be at less egual to ' . $minStartDelay . ' seconds.';
         return ['resellers' => $resellers, 'offers' => $collection, 'errors' => $errors, 'infos' => $infos];
     }
     $langue = $offerin->langue;
     if (!strlen($langue) || empty($langue) || strtolower($langue) == 'non') {
         $langue = false;
     }
     if ($langue) {
         $langues = lib('forms')->getLanguagesFromSegmentId($segment_id);
         if (!in_array($langue, $langues)) {
             $langue = false;
         }
     }
     $this->cleanToPay($offerin->segment_id);
     $products = Model::Productdata()->where(['segment_id', '=', (int) $offerin->segment_id])->where(['sellzone_id', '=', (int) $offerin->sellzone_id])->cursor();
     foreach ($products as $product) {
         $item = [];
         $reseller = Model::Reseller()->model($product['reseller']);
         $company = Model::Company()->model($product['company']);
         // $company    = Model::Company()->where(['reseller_id', '=', (int) $product['reseller_id']])->first(true);
         if ($reseller && $company) {
             // $statusId = (int) lib('status')->getId('reseller', 'REGISTER');
             if (false === $product['status']) {
                 $infos[$reseller->id]['status'] = true;
                 continue;
             }
             $infos[$reseller->id] = [];
             $options = $product['options'];
             $hasAgenda = isAke($options, 'has_agenda', false);
             // $hasAgenda = lib('option')->get('agenda.partage', $reseller, false);
             $item['reseller_calendar'] = $hasAgenda;
             $item['reseller_id'] = $reseller->id;
             $item['segment_id'] = $segment_id;
             if (true === $isPro) {
                 $item['company_id'] = $offer['company_id'];
             } else {
                 $item['account_id'] = $offer['account_id'];
             }
             $locationReseller = lib('utils')->remember('has.locations.companies.' . $product['reseller_id'], function ($reseller_id) {
                 $company = Model::Company()->where(['reseller_id', '=', (int) $reseller_id])->first(true);
                 $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']]);
             $quantity = (double) $this->getQuantity($offer);
             /* si une langue est demandée on vérifie si le revendeur la parle, sinon, on passe au suivant */
             if ($langue) {
                 // $speak = lib('option')->get('langue.' . $market_id . '.' . $langue, $reseller, false);
                 $speak = isAke($options['languages'], $langue, false);
                 if (false === $speak) {
                     $infos[$reseller->id]['language'] = true;
                     continue;
                 }
             }
             $distanceMaxOffer = isset($offer['distance_max']) ? (double) $offer['distance_max'] : 0;
             // $distanceMaxReseller    = (double) lib('option')->get('zone.intervention.' . $market_id, $reseller, 0);
             $distanceMaxReseller = (double) isAke($options, 'distance_max', 0);
             $distance = distanceKmMiles($locationOffer['lng'], $locationOffer['lat'], $locationReseller['lng'], $locationReseller['lat']);
             $km = (double) $distance['km'];
             $item['distance'] = $km;
             if (0 < $distanceMaxOffer || 0 < $distanceMaxReseller) {
                 if ($distanceMaxOffer < $km && $distanceMaxOffer > 0) {
                     $infos[$reseller->id]['distance_reseller'] = true;
                     continue;
                 }
                 if ($distanceMaxReseller < $km && $distanceMaxReseller > 0) {
                     $infos[$reseller->id]['distance_buyer'] = true;
                     continue;
                 }
             }
             // $delai_presta = lib('option')->get('delai.intervention.' . $market_id, $reseller, 0);
             $delai_presta = isAke($options, 'delai_min', 0);
             $item['date_max_offer'] = (int) $start - 3600;
             if (0 < $delai_presta) {
                 if ($delai < $delai_presta) {
                     $infos[$reseller->id]['delay_reseller'] = true;
                     continue;
                 } else {
                     $item['date_max_offer'] = (int) $start - $delai_presta * 3600;
                 }
             }
             if ($hasAgenda) {
                 $item['date_max_offer'] = $item['date_max_offer'] > $now + 3600 ? $now + 3600 : $item['date_max_offer'];
             } else {
                 $item['date_max_offer'] = $item['date_max_offer'] > $now + 1800 ? $now + 1800 : $item['date_max_offer'];
             }
             $makePrice = $amount = (double) $this->makePrice($offer, $product['product']);
             $minAmount = isAke($options, 'amount_min', 0);
             if ($minAmount > $amount) {
                 $infos[$reseller->id]['amount_reseller'] = true;
                 continue;
             }
             $tva = isAke($product['product'], 'tva', 1);
             $discount_default_quantity = isAke($product['product'], 'discount_default_quantity', 0);
             $discount_default_amount = (double) isAke($product['product'], 'discount_default_amount', 0);
             $discount_price_quantity = isAke($product['product'], 'discount_price_quantity', 0);
             $discount_price_amount = (double) isAke($product['product'], 'discount_price_amount', 0);
             $fixed_costs_default = (double) isAke($product['product'], 'fixed_costs_default', 0);
             $travel_costs_default = (double) isAke($product['product'], 'travel_costs_default', 0);
             $shipping_costs_default = (double) isAke($product['product'], 'shipping_costs_default', 0);
             $item['fixed_costs'] = $fixed_costs_default;
             $item['travel_costs'] = $travel_costs_default;
             $item['shipping_costs'] = $shipping_costs_default;
             $item['discount'] = 0;
             $item['tva_id'] = (int) $tva;
             if ($discount_default_quantity > 0 && $discount_default_amount > 0) {
                 if ($quantity >= $discount_default_quantity) {
                     $discount = (double) round($amount * $discount_default_amount / 100, 2);
                     $amount -= $discount;
                     $item['discount'] = $discount;
                 }
             } else {
                 if ($discount_price_quantity > 0 && $discount_price_amount > 0) {
                     if ($amount >= $discount_price_quantity) {
                         $discount = (double) round($amount * $discount_price_amount / 100, 2);
                         $amount -= $discount;
                         $item['discount'] = $discount;
                     }
                 }
             }
             if (0 < $fixed_costs_default) {
                 $amount += $fixed_costs_default;
             }
             if (0 < $shipping_costs_default) {
                 $amount += $shipping_costs_default;
             }
             if (0 < $travel_costs_default) {
                 $travel_costs = (double) $travel_costs_default * $km;
                 $amount += $travel_costs;
                 $item['travel_costs'] = $travel_costs;
             }
             $optionsOffer = $this->extractOptions($offer);
             if (!empty($optionsOffer)) {
                 foreach ($optionsOffer as $opt) {
                     $value = isAke($offer, $opt, 0);
                     if ($value > 0) {
                         $price = isAke($product['product'], $this->transform($opt), 0);
                         $amountOption = (double) $price * $quantity;
                         $amount += $amountOption;
                         $item[$this->transform($opt)] = $amountOption;
                     }
                 }
             }
             $resellers[] = $reseller->id;
             $item['amount'] = (double) $amount;
             $item['offerin_id'] = (int) $offer['id'];
             if (true === $isCalendar) {
                 if (isset($product['product']['taux_horaire'])) {
                     $duration = (double) $makePrice / $product['product']['taux_horaire'];
                 } else {
                     $duration = $quantity;
                 }
                 $end = $start + 3600 * $duration;
                 $item['duration'] = $end - $start;
                 $employees = $this->getEmployeesCan((int) $start, (int) $end, (int) $product['reseller_id']);
                 if (empty($employees)) {
                     $infos[$reseller->id]['no_employee'] = true;
                     Model::offerouttrash()->create($item)->save();
                     continue;
                 }
                 $item['reselleremployees'] = $employees;
                 $item['start'] = $start;
                 $item['end'] = $end;
             } else {
                 $employees = Model::Reselleremployee()->where(['reseller_id', '=', (int) $reseller->id])->select('id')->cursor()->toArray();
                 $item['reselleremployees'] = $employees;
             }
             $item['status_id'] = (int) lib('status')->getId('offerout', 'SHOWN');
             $offerOut = Model::Offerout()->create($item)->save();
             $item['offerout_id'] = $offerOut->id;
             $collection[] = $item;
         }
     }
     $return = [];
     $return['resellers'] = $resellers;
     $return['offers'] = $collection;
     if (!empty($errors)) {
         $return['errors'] = $errors;
     }
     if (!empty($infos)) {
         $return['infos'] = $infos;
     }
     $offerin->resellers = $resellers;
     if (true === $isCalendar) {
         $offerin->start = $start;
     }
     $offerin->save();
     return $return;
 }