<?php

namespace App\Services;

use App\Logistic;
use App\OracleActAllocationDetails;
use App\OracleBasCustomer;
use App\OracleDOCASNHeader;
use App\OracleDOCOrderDetail;
use App\OracleDOCOrderHeader;
use App\Order;
use App\OrderItems;
use App\OrderPackage;
use App\OrderPackageCommodities;
use App\Owner;
use App\Shop;

class OrderService
{
    public function __construct()
    {
        app()->bind("orderPackageService", OrderPackageService::class);
    }

//createOrFindBy
    public function createOrderByWMSOrderNo($oracleOrderNo)
    {
        $order = Order::where('code', $oracleOrderNo)->first();
        $orderHeader = OracleDOCOrderHeader::where('OrderNo', trim($oracleOrderNo, "'"))->first();
        if ($orderHeader == null) {
            return null;
        }
        $orderHeadAttr = $orderHeader->toArray();
        if ($order == null) {
            $customer_name = $orderHeadAttr['oracleBASCustomer_descr_c'];
            $owner = Owner::firstOrCreate(['name' => $customer_name, 'code' => $orderHeadAttr['customerid']]);
            $owner_id = $owner->id;
            $shop_name = $orderHeadAttr['issuepartyname'];
            $shop_Id = null;
            if ($shop_name) {
                $shop = Shop::firstOrCreate(['name' => $shop_name, 'owner_id' => $owner_id]);
                $shop_Id = $shop->id;
            }

            $logistics = Logistic::where(['name' => $orderHeadAttr['carrierid']])->first();
            if ($logistics == null) {
                $oracleBasCustomer = OracleBasCustomer::where(['CustomerID' => $orderHeadAttr['carrierid'], 'Customer_Type' => 'CA'])->first();
                if ($oracleBasCustomer) {
                    $logistics = Logistic::firstOrCreate(['code' => $oracleBasCustomer->customerid, 'name' => $oracleBasCustomer->descr_c]);
                }
            }

            $logistics_id = null;
            if ($logistics) {
                $logistics_id = $logistics->id;
            }
            $arr = [
                'code' => $orderHeadAttr['orderno'],
                'owner_id' => $owner_id,
                'wms_status' => $orderHeadAttr['oracleBASCode_codename_c'],
                'created_at' => $orderHeadAttr['addtime'],
                'logistic_id' => $logistics_id,
                'shop_id' => $shop_Id,
                'consignee_name' => $orderHeadAttr['c_contact'],
                'consignee_phone' => $orderHeadAttr['c_tel2'],
                'province' => $orderHeadAttr['c_province'],
                'city' => $orderHeadAttr['c_city'],
                'district' => $orderHeadAttr['c_district'],
                'address' => $orderHeadAttr['c_address1'],
            ];

            $order = Order::create($arr);
            // $order = Order::create([]);

            $orderPackageService = app('orderPackageService');
            $order->packages = $orderPackageService->createdByOrder($order, $orderHeader);
        } else {
            return $this->getOrderInfo($order['id']);
        }
        return $order;
    }

    public function getOrderInfoByLogisticNumberReturn($logisticNumberReturn){
        $ASNHeader =  OracleDOCASNHeader::where('ASNReference2',$logisticNumberReturn)->first();
        if(!$ASNHeader){
            return null;
        }
        $OrderHeader = OracleDOCOrderHeader::where('SOReference1',$ASNHeader->asnreference3)->first();
        if(!$OrderHeader){
            return null;
        }
        return $this->createOrderByWMSOrderNo($OrderHeader->orderno);
    }

    public function getOrderInfo($orderId)
    {
        return Order::with(['packages' => function ($query) {
            return $query->with(['commodities' => function ($query) {
                return $query->with(['commodity']);
            }]);
        }])->where('id', $orderId)->first();
    }

    public function getOrderInfoByWMSNo($code)
    {
        return Order::with(['packages' => function ($query) {
            return $query->with(['commodities' => function ($query) {
                return $query->with(['commodity']);
            }]);
        }])->where('code', $code)->first();
    }

    public function getWMSOrderInfoByWMSOrderNo($WMSOrderNo)
    {
        if(!OracleDOCOrderHeader::where('orderNo',$WMSOrderNo)->count()){
            return [];
        }
        $orderHeader = $this->getWMSOrderHeaderByWMSOrderNo($WMSOrderNo);
        $orderDetails = $this->getWMSOrderDetailsByWMSOrderNo($WMSOrderNo);
        $allocationDetails = $this->getWMSOrderAllDetailsByWMSOrderNo($WMSOrderNo);
        return compact('orderHeader','orderDetails','allocationDetails');
    }

    public function getWMSOrderHeaderByWMSOrderNo($WMSOrderNo){
        return OracleDOCOrderHeader::where('orderNo',$WMSOrderNo)->first();
    }

    public function getWMSOrderDetailsByWMSOrderNo($WMSOrderNo){
        return OracleDOCOrderDetail::where('orderNo',$WMSOrderNo)->get();
    }

    public function getWMSOrderAllDetailsByWMSOrderNo($WMSOrderNo){
        return OracleActAllocationDetails::where('orderNo',$WMSOrderNo)->get();
    }

}
