<?php
/**
 * Description of ClienteForm
 *
 * @author carlos
 */

class CustomerPaymentForm extends Form
{
    public function __construct()
    {
        $this->setActionForm('CustomerPayment.php');
        $this->setEnctype('multipart/form-data');
        $this->setName('pago');
        $this->setMethod('post');
        $this->setClass('pago');
        $this->setDefaultFormLabelsColSize('4');
        $this->setDefaultFormElementsColSize('8');
        parent::__construct();
        $this->init();
    }

    public function init()
    {
        $this->addElement(
            array(
                'type' => 'hidden',
                'name' => 'status',
            )
        );

        $this->addElement(
            array(
                'type' => 'text',
                'name' => 'date',
                'label' => 'Fecha',
                'validators' => array('date'),
                'required' => true,
                'class' => '_datetimepicker'
            )
        );

        $this->addElement(
            array(
                'type' => 'select',
                'name' => 'payment_method',
                'label' => 'Forma de pago',
                'multiOptions' => $this->getListPaymentMethod(),
                'required' => true
            )
        );
        $this->addElement(
            array(
                'type' => 'select',
                'name' => 'store_payment_type_id',
                'label' => 'Tipo de pago',
                'multiOptions' => $this->getListStore_payment_type(),
                'required' => true
            )
        );


        $this->addElement(
            array(
                'type' => 'text',
                'name' => 'operation_num',
                'label' => 'Num. operacion',
                'required' => false
            )
        );

        $this->addElement(
            array(
                'type' => 'hidden',
                'name' => 'monto_original',
            )
        );

        $this->addElement(
            array(
                'type' => 'hidden',
                'name' => 'saldo_id',
            )
        );


        $this->addElement(
            array(
                'type' => 'hidden',
                'name' => 'suma_de_pagos',
                'value' => ''
            )
        );

        $this->addElement(
            array(
                'type' => 'text',
                'name' => 'amount',
                'label' => 'Monto',
                'value' => '',
                'class' => '_maskDouble',
                'required' => true
            )
        );

        $this->addElement(
            array(
                'type' => 'select',
                'name' => 'customer',
                'label' => 'Customer',
                'multiOptions' => $this->getListCustomers(),

                'required' => false,
                'value' => '1952' //UG default PARIS BAKERY
            )
        );
        $this->addElement(
            array(
                'type' => 'select',
                'name' => 'store_payment',
                'label' => 'Sucursal',
                'multiOptions' => $this->getListStores(),
                'optionals' => array('onchange' => 'getListFacturasCustomers()'),
                'required' => true
            )
        );


        $this->addElement(
            array(
                'type' => 'textarea',
                'name' => 'notas',
                'label' => 'Comentarios',
                'required' => false
            )
        );
        $this->addElement(
            array(
                'type' => 'textarea',
                'name' => 'memo',
                'label' => 'Memo',
                'required' => false
            )
        );
        $this->addElement(
            array(
                'id' => 'attachments',
                'type' => 'file',
                'name' => "attachments[]",
                'label' => 'Documentos adjuntos',
                'class' => 'file upload',
                'required' => false,
                'optionals' => array(
                    'title' => 'Documentos adjuntos',
                    'multiple' => '',
                    'data-show-preview' => false,
                    'data-show-upload' => false
                )
            )
        );

        $this->addElement(
            array(
                'type' => 'submit',
                'name' => 'send',
                'value' => $this->_getTranslation('Guardar'),
                'class' => 'btn btn-primary'
            )
        );
    }

    public function addItem($idCompra)
    {
        $this->addElement(
            array(
                'type' => 'text',
                'name' => "pago[$idCompra]",
                'optionals' => array('style' => 'text-align:right', 'readOnly' => 'true'),
                'class' => 'ammountPymt',
                'col-size-element' => '12',
                'required' => true
            )
        );
    }

    #Para pagos de consignas.
    #En info consigna guardo serializada la info de la consigna que se paga, esto es para no tener que consultar otra vez.
    #Especialmente consultar y obtener la info de liquidacion de la consigna es tardado.
    #Por eso de opto por guardar asi la info.

    public function addItems($elementId, $consignaData)
    {
        $this->addElement(
            array(
                'type' => 'text',
                'name' => "pago[$elementId]",
                'optionals' => array('style' => 'width:90px;text-align:right'),
                'class' => 'ammountPymt',
                'required' => true
            )
        );

        $this->addElement(array('type' => 'hidden', 'name' => "infoConsigna[$elementId]", 'value' => addslashes(serialize($consignaData))));
    }

    public function setFacturasAPagar($facturas)
    {
        if (!is_array($facturas)) {
            return null;
        }
        foreach ($facturas as $factura => $value) {
            $this->addItem($factura);
            $this->setValueToElement($value, "pago[$factura]");
        }
    }

    public function getListCustomers()
    {
        $repository = new CustomerRepository();
        $result = $repository->getListSelectCustomers();

        if ($result) {
            $array = array(null => 'Selecciona una opcion');
            foreach ($result as $key => $value) {
                $array[$key] = $value;
            }
            return $array;
        }
    }

    public function getListPaymentMethod()
    {
        $repository = new PagoRepository();
        $result = $repository->getListFormaPago();

        if (count($result) > 1) {
            $array = array('' => 'Seleccionar una opcion...');
        }
        if ($result) {
            foreach ($result as $key => $value) {
                $array[$key] = $value;
            }
            return $array;
        }
    }

    
    public function getListStore_payment_type()//Falta el select
    {
        $repository = new CustomerPaymentRepository();
        $result = $repository->getListStorePaymentType();

        if (count($result) > 1) {
            $array = array('' => 'Seleccionar una opcion...');
        }
        if ($result) {
            foreach ($result as $key => $value) {
                $array[$key] = $value;
            }
            return $array;
        }
    }

    public function getListStores()
    {
        $repo = new StoreRepository();
        $result = $repo->getListSelectStores();

        if (count($result) > 1) {
            $array = array('' => 'Seleccionar una opcion...');
        }
        if ($result) {
            foreach ($result as $key => $value) {
                $array[$key] = $value;
            }
            return $array;
        }
    }
    public function populate($data)
    {
        $tools = new Tools();
        if ($tools->isValidaDateYYYMMDD($data['date'])) {
            $data['date'] = $tools->setFormatDateToForm($data['date']);
        }

        parent::populate($data);
    }

    public function isValid()
    {
        $repo = new CustomerPaymentRepository();
        $inputs = $repo->inputs_double;
        $inputs[] = 'suma_de_pagos';
        $this->_rawNumber(array('amount'));

        $sumaPagos = $this->getValueElement('suma_de_pagos');
        $monto = $this->getValueElement('amount');

        $isValid = parent::isValid();

        $data = $this->formatDouble($this->getNameValuesElements(), $repo->inputs_double);
        $this->populate($data);

        #UG 23.03.24 ahora se permiten hacer pagos (amount) > a sumaPagos 
        // if ($isValid) {
        //     if ($sumaPagos < $monto) {
        //         $flashmessenger = new FlashMessenger();
        //         $flashmessenger->addMessage(array('danger' => 'Suma de pagos debe ser igual al monto de pago.'));
        //         return null;
        //     }
        // }

        return $isValid;
    }
}