<commit_before><?php
/**
 * Created by PhpStorm.
 * User: luciano
 * Date: 22/04/16
 * Time: 01:56
 */

namespace ErpNET\App\Services;

use Carbon\Carbon;
use ErpNET\App\Interfaces\OrderServiceInterface;
use ErpNET\App\Interfaces\SummaryServiceInterface;
use ErpNET\App\Models\Doctrine\Entities\EntityBase;
use ErpNET\App\Models\RepositoryLayer\ItemOrderRepositoryInterface;
use ErpNET\App\Models\RepositoryLayer\OrderRepositoryInterface;
use ErpNET\App\Models\RepositoryLayer\OrderSharedStatRepositoryInterface;
use ErpNET\App\Models\RepositoryLayer\PartnerGroupRepositoryInterface;
use ErpNET\App\Models\RepositoryLayer\PartnerRepositoryInterface;
use ErpNET\App\Models\RepositoryLayer\AddressRepositoryInterface;
use ErpNET\App\Models\RepositoryLayer\ContactRepositoryInterface;
use ErpNET\App\Models\RepositoryLayer\ProductRepositoryInterface;
use ErpNET\App\Models\RepositoryLayer\SharedOrderPaymentRepositoryInterface;
use ErpNET\App\Models\RepositoryLayer\SharedOrderTypeRepositoryInterface;
use ErpNET\App\Models\RepositoryLayer\SharedCurrencyRepositoryInterface;
use ErpNET\App\Models\RepositoryLayer\SharedStatRepositoryInterface;
use ErpNET\App\Models\RepositoryLayer\SummaryRepositoryInterface;
use ErpNET\App\Models\RepositoryLayer\UserRepositoryInterface;
use Illuminate\Database\Eloquent\Model;

class OrderService implements OrderServiceInterface
{
    protected $summaryService;
    protected $userRepository;
    protected $partnerRepository;
    protected $partnerGroupRepository;
    protected $productRepository;
    protected $orderRepository;
    protected $itemOrderRepository;
    protected $sharedOrderPaymentRepository;
    protected $sharedOrderTypeRepository;
    protected $sharedCurrencyRepository;
    protected $sharedStatRepository;
    protected $addressRepository;
    protected $contactRepository;
    protected $summaryRepository;

    public $itemStock;
    protected $objectData;
    protected $orderRecord;

    public function __construct(
        SummaryServiceInterface $summaryService,
        UserRepositoryInterface $userRepository,
        PartnerRepositoryInterface $partnerRepository,
        PartnerGroupRepositoryInterface $partnerGroupRepository,
        ProductRepositoryInterface $productRepository,
        OrderRepositoryInterface $orderRepository,
        ItemOrderRepositoryInterface $itemOrderRepository,
        SharedOrderPaymentRepositoryInterface $sharedOrderPaymentRepository,
        SharedOrderTypeRepositoryInterface $sharedOrderTypeRepository,
        SharedCurrencyRepositoryInterface $sharedCurrencyRepository,
        SharedStatRepositoryInterface $sharedStatRepository,
        AddressRepositoryInterface $addressRepository,
        ContactRepositoryInterface $contactRepository,
        SummaryRepositoryInterface $summaryRepository
//        CostAllocateRepositoryInterface $costAllocateRepository,
//        Carbon $carbon
    )
    {
        $this->summaryService = $summaryService;
        $this->userRepository = $userRepository;
        $this->partnerRepository = $partnerRepository;
        $this->partnerGroupRepository = $partnerGroupRepository;
        $this->productRepository = $productRepository;
        $this->orderRepository = $orderRepository;
        $this->itemOrderRepository = $itemOrderRepository;
        $this->sharedOrderPaymentRepository = $sharedOrderPaymentRepository;
        $this->sharedOrderTypeRepository = $sharedOrderTypeRepository;
        $this->sharedCurrencyRepository = $sharedCurrencyRepository;
        $this->sharedStatRepository = $sharedStatRepository;
        $this->addressRepository = $addressRepository;
        $this->contactRepository = $contactRepository;
        $this->summaryRepository = $summaryRepository;
//        $this->costAllocateRepository = $costAllocateRepository;
//        $this->carbon = $carbon;

        $this->itemStock = $this->resumoDasOrdens();
    }

    /**
     * @param string $data
     * @return string
     */
    public function createDeliverySalesOrderWithJson($data)
    {
        if (!env('DELIVERY_OPEN', false)) {
            $return = json_encode([
                'error' => true,
                'message' => 'Estamos em manutenção no momento, retornaremos novamente em: '.env('DELIVERY_RETURN'),
            ]);
            return $return;
        }
        try{
            $this->objectData = json_decode($data);

            $this->orderRecord = $this->processOrder();

            $this->sharedStatRecord = $this->prcessStatus();

            $this->processSharedOrder();

            $sharedCurrencyRecord = $this->processSharedCurrency();

            $this->processSharedOrderPayment();

            $partnerRecord = $this->processPartner();

            $this->processUser($partnerRecord);

            $this->processContact('email', $partnerRecord);
            $this->processContact('telefone', $partnerRecord);
            $this->processContact('whatsapp', $partnerRecord);

            $this->processAddress($partnerRecord);

            $this->processItems($sharedCurrencyRecord);

            $addedOrder = $this->orderRepository->find($this->orderRecord->id);

            $jsonFields = [
                'error' => false,
                'message' => 'Ordem nº ' . $addedOrder->id . ' criada.',
                'id' => $addedOrder->id,
                'posted_at' => $addedOrder->posted_at,
                'valor_total' => $addedOrder->valor_total,
                'observacao' => $addedOrder->observacao,
                'endereco' => $addedOrder->address->logradouro.', '.$addedOrder->address->numero.' - '.$addedOrder->address->bairro.' / CEP:'.$addedOrder->address->cep,
                'endereco_compl' => $addedOrder->address->complemento,
                'endereco_obs' => $addedOrder->address->obs,
            ];

            $return = json_encode($jsonFields);

            return $return;
        }
        catch (\Exception $e){
            $return = json_encode([
                'error' => true,
                'message' => $e->getMessage(),
            ]);
            return $return;
        }

    }

    protected function resumoDasOrdens(){
//        $ultimoFechamento = $this->periodRepository->getLastPeriodEndDate();
        $ultimoFechamento = $this->summaryService->lastEnd();<commit_msg>v0.1.2 master - fix eloquent map relation to array<commit_after><?php
/**
 * Created by PhpStorm.
 * User: luciano
 * Date: 22/04/16
 * Time: 01:56
 */

namespace ErpNET\App\Services;

use Carbon\Carbon;
use ErpNET\App\Interfaces\OrderServiceInterface;
use ErpNET\App\Interfaces\SummaryServiceInterface;
use ErpNET\App\Models\Doctrine\Entities\EntityBase;
use ErpNET\App\Models\RepositoryLayer\ItemOrderRepositoryInterface;
use ErpNET\App\Models\RepositoryLayer\OrderRepositoryInterface;
use ErpNET\App\Models\RepositoryLayer\OrderSharedStatRepositoryInterface;
use ErpNET\App\Models\RepositoryLayer\PartnerGroupRepositoryInterface;
use ErpNET\App\Models\RepositoryLayer\PartnerRepositoryInterface;
use ErpNET\App\Models\RepositoryLayer\AddressRepositoryInterface;
use ErpNET\App\Models\RepositoryLayer\ContactRepositoryInterface;
use ErpNET\App\Models\RepositoryLayer\ProductRepositoryInterface;
use ErpNET\App\Models\RepositoryLayer\SharedOrderPaymentRepositoryInterface;
use ErpNET\App\Models\RepositoryLayer\SharedOrderTypeRepositoryInterface;
use ErpNET\App\Models\RepositoryLayer\SharedCurrencyRepositoryInterface;
use ErpNET\App\Models\RepositoryLayer\SharedStatRepositoryInterface;
use ErpNET\App\Models\RepositoryLayer\SummaryRepositoryInterface;
use ErpNET\App\Models\RepositoryLayer\UserRepositoryInterface;
use Illuminate\Database\Eloquent\Model;

class OrderService implements OrderServiceInterface
{
    protected $summaryService;
    protected $userRepository;
    protected $partnerRepository;
    protected $partnerGroupRepository;
    protected $productRepository;
    protected $orderRepository;
    protected $itemOrderRepository;
    protected $sharedOrderPaymentRepository;
    protected $sharedOrderTypeRepository;
    protected $sharedCurrencyRepository;
    protected $sharedStatRepository;
    protected $addressRepository;
    protected $contactRepository;
    protected $summaryRepository;

    public $itemStock;
    protected $objectData;
    protected $orderRecord;

    public function __construct(
        SummaryServiceInterface $summaryService,
        UserRepositoryInterface $userRepository,
        PartnerRepositoryInterface $partnerRepository,
        PartnerGroupRepositoryInterface $partnerGroupRepository,
        ProductRepositoryInterface $productRepository,
        OrderRepositoryInterface $orderRepository,
        ItemOrderRepositoryInterface $itemOrderRepository,
        SharedOrderPaymentRepositoryInterface $sharedOrderPaymentRepository,
        SharedOrderTypeRepositoryInterface $sharedOrderTypeRepository,
        SharedCurrencyRepositoryInterface $sharedCurrencyRepository,
        SharedStatRepositoryInterface $sharedStatRepository,
        AddressRepositoryInterface $addressRepository,
        ContactRepositoryInterface $contactRepository,
        SummaryRepositoryInterface $summaryRepository
//        CostAllocateRepositoryInterface $costAllocateRepository,
//        Carbon $carbon
    )
    {
        $this->summaryService = $summaryService;
        $this->userRepository = $userRepository;
        $this->partnerRepository = $partnerRepository;
        $this->partnerGroupRepository = $partnerGroupRepository;
        $this->productRepository = $productRepository;
        $this->orderRepository = $orderRepository;
        $this->itemOrderRepository = $itemOrderRepository;
        $this->sharedOrderPaymentRepository = $sharedOrderPaymentRepository;
        $this->sharedOrderTypeRepository = $sharedOrderTypeRepository;
        $this->sharedCurrencyRepository = $sharedCurrencyRepository;
        $this->sharedStatRepository = $sharedStatRepository;
        $this->addressRepository = $addressRepository;
        $this->contactRepository = $contactRepository;
        $this->summaryRepository = $summaryRepository;
//        $this->costAllocateRepository = $costAllocateRepository;
//        $this->carbon = $carbon;

        $this->itemStock = $this->resumoDasOrdens();
    }

    /**
     * @param string $data
     * @return string
     */
    public function createDeliverySalesOrderWithJson($data)
    {
        if (!env('DELIVERY_OPEN', false)) {
            $return = json_encode([
                'error' => true,
                'message' => 'Estamos em manutenção no momento, retornaremos novamente em: '.env('DELIVERY_RETURN'),
            ]);
            return $return;
        }
        try{
            $this->objectData = json_decode($data);

            $this->orderRecord = $this->processOrder();

            $this->sharedStatRecord = $this->prcessStatus();

            $this->processSharedOrder();

            $sharedCurrencyRecord = $this->processSharedCurrency();

            $this->processSharedOrderPayment();

            $partnerRecord = $this->processPartner();

            $this->processUser($partnerRecord);

            $this->processContact('email', $partnerRecord);
            $this->processContact('telefone', $partnerRecord);
            $this->processContact('whatsapp', $partnerRecord);

            $this->processAddress($partnerRecord);

            $this->processItems($sharedCurrencyRecord);

            $addedOrder = $this->orderRepository->find($this->orderRecord->id);

            $items = [];
            if (isset($addedOrder['itemOrders']) && count($addedOrder->itemOrders)>0)
                foreach ($addedOrder->itemOrders as $itemOrder) {
                    $items[] = $itemOrder->quantidade . ' x ' . $itemOrder->valor_unitario . ' - '. $itemOrder->product->nome;
                }

            $jsonFields = [
                'error' => false,
                'message' => 'Ordem nº ' . $addedOrder->id . ' criada.',
                'id' => $addedOrder->id,
                'posted_at' => $addedOrder->posted_at,
                'valor_total' => $addedOrder->valor_total,
                'observacao' => $addedOrder->observacao,
                'endereco' => $addedOrder->address->logradouro.', '.$addedOrder->address->numero.' - '.$addedOrder->address->bairro.' / CEP:'.$addedOrder->address->cep,
                'endereco_compl' => $addedOrder->address->complemento,
                'endereco_obs' => $addedOrder->address->obs,
                'items' => $items,
            ];

            $return = json_encode($jsonFields);

            return $return;
        }
        catch (\Exception $e){
            $return = json_encode([
                'error' => true,
                'message' => $e->getMessage(),
            ]);
            return $return;
        }

    }

    protected function resumoDasOrdens(){
//        $ultimoFechamento = $this->periodRepository->getLastPeriodEndDate();
        $ultimoFechamento = $this->summaryService->lastEnd();
{'fraction_non_alphanumeric': 0.10581440123219099, 'fraction_numerical': 0.0018482864844050829, 'mean_word_length': 3.2382506527415145, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 155, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2109310', 'n_tokens_mistral': 3352, 'n_tokens_neox': 3266, 'n_words': 531}