<?php

namespace Export;

use XBase\Record;

/**
 * Own class for DBF export.
 */
class ExportDataDBFUcto extends ExportDataDBF
{
    protected function columnDefs()
    {
        return [
            ['Veta', Record::DBFFIELD_TYPE_NUMERIC, 6, 0], // datum vystavení
            ['DatumVyst', Record::DBFFIELD_TYPE_DATE], // datum vystavení
            ['DatumSpl', Record::DBFFIELD_TYPE_DATE], // datum splatnosti
            ['Datum', Record::DBFFIELD_TYPE_DATE], // datum zaplacení - většinou prázdné

            ['Doklad', Record::DBFFIELD_TYPE_CHAR, 30], // Doklad
            ['DokladKH', Record::DBFFIELD_TYPE_CHAR, 20],
            ['TypD', Record::DBFFIELD_TYPE_CHAR, 3],
            ['Text', Record::DBFFIELD_TYPE_CHAR, 30], // text
            ['Druh', Record::DBFFIELD_TYPE_CHAR, 3], // klic do ciselniku druhu
            ['Firma', Record::DBFFIELD_TYPE_CHAR, 5], // cislo firmy v adresari ucta
            ['Ico', Record::DBFFIELD_TYPE_CHAR, 10], // ico firmy
            ['Vykon', Record::DBFFIELD_TYPE_CHAR, 5], // klic do ciselniku vykonu

            ['Plat', Record::DBFFIELD_TYPE_CHAR, 1], // platba: B-banka, H-hotovos
            ['Celkem', Record::DBFFIELD_TYPE_FLOATING, 11, 2], // celkova castka

            ['DatumDPH', Record::DBFFIELD_TYPE_DATE], // datum zd. pl
            ['DatumKH', Record::DBFFIELD_TYPE_DATE],

            ['BezDaneZ', Record::DBFFIELD_TYPE_FLOATING, 11, 2], // dph
            ['DphZ', Record::DBFFIELD_TYPE_FLOATING, 11, 2], // dph
            ['BezDaneS', Record::DBFFIELD_TYPE_FLOATING, 11, 2], // dph
            ['DphS', Record::DBFFIELD_TYPE_FLOATING, 11, 2], // dph
            ['BezDaneS2', Record::DBFFIELD_TYPE_FLOATING, 11, 2], // dph
            ['DphS2', Record::DBFFIELD_TYPE_FLOATING, 11, 2], // dph
            ['BezDaneO', Record::DBFFIELD_TYPE_FLOATING, 11, 2], // dph
            ['Zaloha', Record::DBFFIELD_TYPE_FLOATING, 11, 2], // dph

            ['Prikaz', Record::DBFFIELD_TYPE_LOGICAL], // nevyplnovat, vyber pro prikaz k uhrade
            ['DatumPrik', Record::DBFFIELD_TYPE_DATE], // nevyplnovat, datum pro prikaz k uhrade

            ['Pozn', Record::DBFFIELD_TYPE_MEMO], // poznámka (memo)
            ['Ozn', Record::DBFFIELD_TYPE_LOGICAL], // označené věty

            ['Měna2', Record::DBFFIELD_TYPE_CHAR, 3], // Měna2
            ['Kč2', Record::DBFFIELD_TYPE_FLOATING, 11, 2], // Kč2

            ['Naz0', Record::DBFFIELD_TYPE_CHAR, 30], // označené věty
            ['Dic0', Record::DBFFIELD_TYPE_CHAR, 14], // označené věty
        ];
    }

    /**
     * @param $row_pointer Record
     * @param $order       \Order
     */
    protected function appendToRow($row_pointer, $order)
    {
        //        $row_pointer->setObjectByName('Veta', '');
        $row_pointer->setObjectByName('DatumVyst', $order->date_handle ? strtotime($order->date_handle->format('Ymd')) : '');
        //        $row_pointer->setObjectByName('DatumPrik', '');

        /* TODO: dořešit due_days, nejlíp na order */
        $row_pointer->setObjectByName('DatumSpl', ($order->date_handle) ? strtotime($order->date_handle->modify('+14 days')->format('Ymd')) : '');
        //        $row_pointer->setObjectByName('Datum', '');
        $row_pointer->setObjectByName('Doklad', 'f/'.$order->order_no);
        $row_pointer->setObjectByName('DokladKH', $order->order_no);
        $row_pointer->setObjectByName('TypD', ' ');
        $row_pointer->setObjectByName('Text', $this->encodeData($order->invoice_firm ? $order->invoice_firm : $order->invoice_name.' '.$order->invoice_surname));
        $row_pointer->setObjectByName('Druh', 'PZ ');
        $row_pointer->setObjectByName('Firma', ' ');
        $row_pointer->setObjectByName('Ico', strtoupper($order->invoice_ico));
        $row_pointer->setObjectByName('Vykon', ' ');

        $pay_type = $order->getDeliveryType()->getPayment();

        if ($pay_type && $pay_type->getPayMethod() == \Payment::METHOD_CASH) {
            $pay_type = 'H';
        } else {
            $pay_type = 'B';
        }

        $row_pointer->setObjectByName('Plat', $this->encodeData($pay_type));
        $row_pointer->setObjectByName('Celkem', !$order->status_storno ? $order->total_price->printFloatValue(2) : 0);

        /* TODO: datum zd. plneni */
        $row_pointer->setObjectByName('DatumDPH', $order->date_handle ? strtotime($order->date_handle->format('Ymd')) : '');
        //        $row_pointer->setObjectByName('DatumKH', '');

        if (!empty($order->vats[21]) && !$order->status_storno) {
            $row_pointer->setObjectByName('BezDaneZ', $order->vats[21]['total_price_without_vat']->printFloatValue(2));
            $row_pointer->setObjectByName('DphZ', $order->vats[21]['tax']['value_vat']->printFloatValue(2));
        } else {
            $row_pointer->setObjectByName('BezDaneZ', 0);
            $row_pointer->setObjectByName('DphZ', 0);
        }

        //        if (!empty($order->vats[15])) {
        //            $row_pointer->setObjectByName('BezDaneS', $order->vats[15]['total_price_without_vat']->printFloatValue(2));
        //            $row_pointer->setObjectByName('DphS', $order->vats[15]['tax']['value_vat']->printFloatValue(2));
        //        } else {
        //            $row_pointer->setObjectByName('BezDaneS', 0);
        //            $row_pointer->setObjectByName('DphS', 0);
        //        }

        //        $row_pointer->setObjectByName('BezDaneS2', 0);
        //        $row_pointer->setObjectByName('DphS2', 0);
        //        $row_pointer->setObjectByName('BezDaneO', 0);
        //        $row_pointer->setObjectByName('Zaloha', 0);

        $row_pointer->setObjectByName('Prikaz', false);
        //        $row_pointer->setObjectByName('DatumPrik', 0);
        //        $row_pointer->setObjectByName('Pozn', '');
        $row_pointer->setObjectByName('Ozn', false);
        $row_pointer->setObjectByName('Měna2', ' ');
        //        $row_pointer->setObjectByName('Kč2', $order->currency_rate);
        $row_pointer->setObjectByName('Naz0', ' ');
        $row_pointer->setObjectByName('Dic0', strtoupper($order->invoice_dic));
    }
}
