/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package erp.mod.log.form;

import erp.lib.SLibConstants;
import erp.mod.SModConsts;
import erp.mod.SModSysConsts;
import erp.mod.log.db.SDbShipment;
import erp.mod.log.db.SDbShipmentDestiny;
import sa.lib.SLibConsts;
import sa.lib.SLibUtils;
import sa.lib.db.SDbRegistry;
import sa.lib.gui.SGuiClient;
import sa.lib.gui.SGuiConsts;
import sa.lib.gui.SGuiParams;
import sa.lib.gui.SGuiUtils;
import sa.lib.gui.SGuiValidation;

/**
 *
 * @author Juan Barajas
 */
public class SFormShipmentDelivery extends sa.lib.gui.bean.SBeanForm {

    private SDbShipmentDestiny moRegistry;
    
    /**
     * Creates new form SFormShipmentDelivery
     */
    public SFormShipmentDelivery(SGuiClient client, String title) {
        setFormSettings(client, SGuiConsts.BEAN_FORM_EDIT, SModConsts.LOGX_SHIP_DLY, SLibConstants.UNDEFINED, title);
        initComponents();
        initComponentsCustom();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jPanel23 = new javax.swing.JPanel();
        jPanel25 = new javax.swing.JPanel();
        jlCompanyBranch = new javax.swing.JLabel();
        moKeyCompanyBranch = new sa.lib.gui.bean.SBeanFieldKey();
        jPanel26 = new javax.swing.JPanel();
        jlShipmentType = new javax.swing.JLabel();
        moKeyShipmentType = new sa.lib.gui.bean.SBeanFieldKey();
        jPanel27 = new javax.swing.JPanel();
        jlDeliveryType = new javax.swing.JLabel();
        moKeyDeliveryType = new sa.lib.gui.bean.SBeanFieldKey();
        jPanel28 = new javax.swing.JPanel();
        jlNumber = new javax.swing.JLabel();
        moTextNumber = new sa.lib.gui.bean.SBeanFieldText();
        jPanel30 = new javax.swing.JPanel();
        jlDate = new javax.swing.JLabel();
        moDateDate = new sa.lib.gui.bean.SBeanFieldDate();
        jPanel5 = new javax.swing.JPanel();
        jPanel24 = new javax.swing.JPanel();
        jPanel33 = new javax.swing.JPanel();
        jlSpot = new javax.swing.JLabel();
        moKeySpot = new sa.lib.gui.bean.SBeanFieldKey();
        jPanel32 = new javax.swing.JPanel();
        jlCustomer = new javax.swing.JLabel();
        moKeyCustomer = new sa.lib.gui.bean.SBeanFieldKey();
        jPanel35 = new javax.swing.JPanel();
        jlCustomerBranch = new javax.swing.JLabel();
        moKeyCustomerBranch = new sa.lib.gui.bean.SBeanFieldKey();
        jPanel36 = new javax.swing.JPanel();
        jlCustomerBranchAddress = new javax.swing.JLabel();
        moKeyCustomerBranchAddress = new sa.lib.gui.bean.SBeanFieldKey();
        jPanel37 = new javax.swing.JPanel();
        jlWarehouseCompanyBranch = new javax.swing.JLabel();
        moKeyWarehouseCompanyBranch = new sa.lib.gui.bean.SBeanFieldKey();
        jPanel38 = new javax.swing.JPanel();
        jlWarehouseEntity = new javax.swing.JLabel();
        moKeyWarehouseEntity = new sa.lib.gui.bean.SBeanFieldKey();
        jPanel22 = new javax.swing.JPanel();
        jlDateDelivery = new javax.swing.JLabel();
        moDateDateDelivery = new sa.lib.gui.bean.SBeanFieldDate();
        jPanel34 = new javax.swing.JPanel();
        jlDateDeliveryReal = new javax.swing.JLabel();
        moDateDateDeliveryReal = new sa.lib.gui.bean.SBeanFieldDate();

        setTitle("Asignación fecha de entrega real");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos del registro:"));
        jPanel1.setLayout(new java.awt.BorderLayout(0, 5));

        jPanel2.setLayout(new java.awt.BorderLayout());

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos del embarque:"));
        jPanel4.setLayout(new java.awt.BorderLayout());

        jPanel23.setLayout(new java.awt.GridLayout(8, 1, 0, 5));

        jPanel25.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 5, 0));

        jlCompanyBranch.setText("Sucursal:");
        jlCompanyBranch.setEnabled(false);
        jlCompanyBranch.setPreferredSize(new java.awt.Dimension(100, 23));
        jPanel25.add(jlCompanyBranch);

        moKeyCompanyBranch.setEnabled(false);
        moKeyCompanyBranch.setFocusable(true);
        moKeyCompanyBranch.setPreferredSize(new java.awt.Dimension(200, 23));
        jPanel25.add(moKeyCompanyBranch);

        jPanel23.add(jPanel25);

        jPanel26.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 5, 0));

        jlShipmentType.setText("Tipo embarque:");
        jlShipmentType.setEnabled(false);
        jlShipmentType.setPreferredSize(new java.awt.Dimension(100, 23));
        jPanel26.add(jlShipmentType);

        moKeyShipmentType.setEnabled(false);
        moKeyShipmentType.setFocusable(true);
        moKeyShipmentType.setPreferredSize(new java.awt.Dimension(200, 23));
        jPanel26.add(moKeyShipmentType);

        jPanel23.add(jPanel26);

        jPanel27.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 5, 0));

        jlDeliveryType.setText("Tipo entrega:");
        jlDeliveryType.setEnabled(false);
        jlDeliveryType.setPreferredSize(new java.awt.Dimension(100, 23));
        jPanel27.add(jlDeliveryType);

        moKeyDeliveryType.setEnabled(false);
        moKeyDeliveryType.setFocusable(true);
        moKeyDeliveryType.setPreferredSize(new java.awt.Dimension(200, 23));
        jPanel27.add(moKeyDeliveryType);

        jPanel23.add(jPanel27);

        jPanel28.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 5, 0));

        jlNumber.setText("Folio:");
        jlNumber.setEnabled(false);
        jlNumber.setPreferredSize(new java.awt.Dimension(100, 23));
        jPanel28.add(jlNumber);

        moTextNumber.setEditable(false);
        moTextNumber.setFocusable(true);
        jPanel28.add(moTextNumber);

        jPanel23.add(jPanel28);

        jPanel30.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 5, 0));

        jlDate.setText("Fecha:");
        jlDate.setEnabled(false);
        jlDate.setPreferredSize(new java.awt.Dimension(100, 23));
        jPanel30.add(jlDate);

        moDateDate.setEditable(false);
        moDateDate.setFocusable(true);
        jPanel30.add(moDateDate);

        jPanel23.add(jPanel30);

        jPanel4.add(jPanel23, java.awt.BorderLayout.NORTH);

        jPanel2.add(jPanel4, java.awt.BorderLayout.WEST);

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos del destino:"));
        jPanel5.setLayout(new java.awt.BorderLayout());

        jPanel24.setLayout(new java.awt.GridLayout(9, 1, 0, 5));

        jPanel33.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 5, 0));

        jlSpot.setText("Destino:");
        jlSpot.setEnabled(false);
        jlSpot.setPreferredSize(new java.awt.Dimension(100, 23));
        jPanel33.add(jlSpot);

        moKeySpot.setEnabled(false);
        moKeySpot.setFocusable(true);
        moKeySpot.setPreferredSize(new java.awt.Dimension(250, 23));
        jPanel33.add(moKeySpot);

        jPanel24.add(jPanel33);

        jPanel32.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 5, 0));

        jlCustomer.setText("Cliente:");
        jlCustomer.setEnabled(false);
        jlCustomer.setPreferredSize(new java.awt.Dimension(100, 23));
        jPanel32.add(jlCustomer);

        moKeyCustomer.setEnabled(false);
        moKeyCustomer.setFocusable(true);
        moKeyCustomer.setPreferredSize(new java.awt.Dimension(250, 23));
        jPanel32.add(moKeyCustomer);

        jPanel24.add(jPanel32);

        jPanel35.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 5, 0));

        jlCustomerBranch.setText("Sucursal:");
        jlCustomerBranch.setEnabled(false);
        jlCustomerBranch.setPreferredSize(new java.awt.Dimension(100, 23));
        jPanel35.add(jlCustomerBranch);

        moKeyCustomerBranch.setEnabled(false);
        moKeyCustomerBranch.setFocusable(true);
        moKeyCustomerBranch.setPreferredSize(new java.awt.Dimension(250, 23));
        jPanel35.add(moKeyCustomerBranch);

        jPanel24.add(jPanel35);

        jPanel36.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 5, 0));

        jlCustomerBranchAddress.setText("Domicilio entrega:");
        jlCustomerBranchAddress.setEnabled(false);
        jlCustomerBranchAddress.setPreferredSize(new java.awt.Dimension(100, 23));
        jPanel36.add(jlCustomerBranchAddress);

        moKeyCustomerBranchAddress.setEnabled(false);
        moKeyCustomerBranchAddress.setFocusable(true);
        moKeyCustomerBranchAddress.setPreferredSize(new java.awt.Dimension(250, 23));
        jPanel36.add(moKeyCustomerBranchAddress);

        jPanel24.add(jPanel36);

        jPanel37.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 5, 0));

        jlWarehouseCompanyBranch.setText("Sucursal:");
        jlWarehouseCompanyBranch.setEnabled(false);
        jlWarehouseCompanyBranch.setPreferredSize(new java.awt.Dimension(100, 23));
        jPanel37.add(jlWarehouseCompanyBranch);

        moKeyWarehouseCompanyBranch.setEnabled(false);
        moKeyWarehouseCompanyBranch.setFocusable(true);
        moKeyWarehouseCompanyBranch.setPreferredSize(new java.awt.Dimension(250, 23));
        jPanel37.add(moKeyWarehouseCompanyBranch);

        jPanel24.add(jPanel37);

        jPanel38.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 5, 0));

        jlWarehouseEntity.setText("Almacén:");
        jlWarehouseEntity.setEnabled(false);
        jlWarehouseEntity.setPreferredSize(new java.awt.Dimension(100, 23));
        jPanel38.add(jlWarehouseEntity);

        moKeyWarehouseEntity.setEnabled(false);
        moKeyWarehouseEntity.setFocusable(true);
        moKeyWarehouseEntity.setPreferredSize(new java.awt.Dimension(250, 23));
        jPanel38.add(moKeyWarehouseEntity);

        jPanel24.add(jPanel38);

        jPanel22.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 5, 0));

        jlDateDelivery.setText("Fecha entrega:");
        jlDateDelivery.setEnabled(false);
        jlDateDelivery.setPreferredSize(new java.awt.Dimension(100, 23));
        jPanel22.add(jlDateDelivery);

        moDateDateDelivery.setEditable(false);
        moDateDateDelivery.setFocusable(true);
        jPanel22.add(moDateDateDelivery);

        jPanel24.add(jPanel22);

        jPanel34.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 5, 0));

        jlDateDeliveryReal.setText("Fecha ent. real: *");
        jlDateDeliveryReal.setPreferredSize(new java.awt.Dimension(100, 23));
        jPanel34.add(jlDateDeliveryReal);
        jPanel34.add(moDateDateDeliveryReal);

        jPanel24.add(jPanel34);

        jPanel5.add(jPanel24, java.awt.BorderLayout.CENTER);

        jPanel2.add(jPanel5, java.awt.BorderLayout.CENTER);

        jPanel1.add(jPanel2, java.awt.BorderLayout.NORTH);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel22;
    private javax.swing.JPanel jPanel23;
    private javax.swing.JPanel jPanel24;
    private javax.swing.JPanel jPanel25;
    private javax.swing.JPanel jPanel26;
    private javax.swing.JPanel jPanel27;
    private javax.swing.JPanel jPanel28;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel30;
    private javax.swing.JPanel jPanel32;
    private javax.swing.JPanel jPanel33;
    private javax.swing.JPanel jPanel34;
    private javax.swing.JPanel jPanel35;
    private javax.swing.JPanel jPanel36;
    private javax.swing.JPanel jPanel37;
    private javax.swing.JPanel jPanel38;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JLabel jlCompanyBranch;
    private javax.swing.JLabel jlCustomer;
    private javax.swing.JLabel jlCustomerBranch;
    private javax.swing.JLabel jlCustomerBranchAddress;
    private javax.swing.JLabel jlDate;
    private javax.swing.JLabel jlDateDelivery;
    private javax.swing.JLabel jlDateDeliveryReal;
    private javax.swing.JLabel jlDeliveryType;
    private javax.swing.JLabel jlNumber;
    private javax.swing.JLabel jlShipmentType;
    private javax.swing.JLabel jlSpot;
    private javax.swing.JLabel jlWarehouseCompanyBranch;
    private javax.swing.JLabel jlWarehouseEntity;
    private sa.lib.gui.bean.SBeanFieldDate moDateDate;
    private sa.lib.gui.bean.SBeanFieldDate moDateDateDelivery;
    private sa.lib.gui.bean.SBeanFieldDate moDateDateDeliveryReal;
    private sa.lib.gui.bean.SBeanFieldKey moKeyCompanyBranch;
    private sa.lib.gui.bean.SBeanFieldKey moKeyCustomer;
    private sa.lib.gui.bean.SBeanFieldKey moKeyCustomerBranch;
    private sa.lib.gui.bean.SBeanFieldKey moKeyCustomerBranchAddress;
    private sa.lib.gui.bean.SBeanFieldKey moKeyDeliveryType;
    private sa.lib.gui.bean.SBeanFieldKey moKeyShipmentType;
    private sa.lib.gui.bean.SBeanFieldKey moKeySpot;
    private sa.lib.gui.bean.SBeanFieldKey moKeyWarehouseCompanyBranch;
    private sa.lib.gui.bean.SBeanFieldKey moKeyWarehouseEntity;
    private sa.lib.gui.bean.SBeanFieldText moTextNumber;
    // End of variables declaration//GEN-END:variables

    private void initComponentsCustom() {
        SGuiUtils.setWindowBounds(this, 720, 450);
        
        moKeyCompanyBranch.setKeySettings(miClient, SGuiUtils.getLabelName(jlCompanyBranch.getText()), true);
        moKeyShipmentType.setKeySettings(miClient, SGuiUtils.getLabelName(jlShipmentType.getText()), true);
        moKeyDeliveryType.setKeySettings(miClient, SGuiUtils.getLabelName(jlDeliveryType.getText()), true);
        moDateDate.setDateSettings(miClient, SGuiUtils.getLabelName(jlDate.getText()), true);
        moTextNumber.setTextSettings(SGuiUtils.getLabelName(jlNumber.getText()), 15);
        moKeySpot.setKeySettings(miClient, SGuiUtils.getLabelName(jlSpot.getText()), true);
        moKeyCustomer.setKeySettings(miClient, SGuiUtils.getLabelName(jlCustomer.getText()), false);
        moKeyCustomerBranch.setKeySettings(miClient, SGuiUtils.getLabelName(jlCustomerBranch.getText()), false);
        moKeyCustomerBranchAddress.setKeySettings(miClient, SGuiUtils.getLabelName(jlCustomerBranchAddress.getText()), false);
        moKeyWarehouseCompanyBranch.setKeySettings(miClient, SGuiUtils.getLabelName(jlWarehouseCompanyBranch.getText()), false);
        moKeyWarehouseEntity.setKeySettings(miClient, SGuiUtils.getLabelName(jlWarehouseEntity.getText()), false);
        moDateDateDelivery.setDateSettings(miClient, SGuiUtils.getLabelName(jlDateDelivery.getText()), true);
        moDateDateDeliveryReal.setDateSettings(miClient, SGuiUtils.getLabelName(jlDateDeliveryReal.getText()), true);
        
        moFields.addField(moKeyCompanyBranch);
        moFields.addField(moKeyShipmentType);
        moFields.addField(moKeyDeliveryType);
        moFields.addField(moDateDate);
        moFields.addField(moTextNumber);
        moFields.addField(moKeySpot);
        moFields.addField(moKeyCustomer);
        moFields.addField(moKeyCustomerBranch);
        moFields.addField(moKeyCustomerBranchAddress);
        moFields.addField(moKeyWarehouseCompanyBranch);
        moFields.addField(moKeyWarehouseEntity);
        moFields.addField(moDateDateDelivery);
        moFields.addField(moDateDateDeliveryReal);
        
        moFields.setFormButton(jbSave);
    }

    @Override
    public void addAllListeners() {
        
    }

    @Override
    public void removeAllListeners() {
        
    }

    @Override
    public void reloadCatalogues() {
        SGuiParams params = new SGuiParams(new int[] { SModSysConsts.BPSS_CT_BP_CUS });
        
        miClient.getSession().populateCatalogue(moKeyCompanyBranch, SModConsts.BPSU_BPB, SLibConsts.UNDEFINED, null);        
        miClient.getSession().populateCatalogue(moKeyShipmentType, SModConsts.LOGS_TP_SHIP, SLibConsts.UNDEFINED, null);        
        miClient.getSession().populateCatalogue(moKeyDeliveryType, SModConsts.LOGS_TP_DLY, SLibConsts.UNDEFINED, null); 
        miClient.getSession().populateCatalogue(moKeySpot, SModConsts.LOGU_SPOT, SLibConsts.UNDEFINED, null);
        miClient.getSession().populateCatalogue(moKeyCustomer, SModConsts.BPSU_BP, SLibConsts.UNDEFINED, params);
        miClient.getSession().populateCatalogue(moKeyCustomerBranch, SModConsts.BPSU_BPB, SLibConsts.UNDEFINED, null);
        miClient.getSession().populateCatalogue(moKeyCustomerBranchAddress, SModConsts.BPSU_BPB_ADD, SLibConsts.UNDEFINED, null);
        miClient.getSession().populateCatalogue(moKeyWarehouseCompanyBranch, SModConsts.BPSU_BPB, SLibConsts.UNDEFINED, null);
        miClient.getSession().populateCatalogue(moKeyWarehouseEntity, SModConsts.CFGU_COB_ENT, SLibConsts.UNDEFINED, null);
    }

    @Override
    public void setRegistry(SDbRegistry registry) throws Exception {
        moRegistry = (SDbShipmentDestiny) registry;
        SDbShipment shipment = null;

        mnFormResult = SLibConsts.UNDEFINED;
        mbFirstActivation = true;

        removeAllListeners();
        reloadCatalogues();
        
        if (moRegistry.isRegistryNew()) {
            moRegistry.initPrimaryKey();
            jtfRegistryKey.setText("");
        }
        else {
            jtfRegistryKey.setText(SLibUtils.textKey(moRegistry.getPrimaryKey()));
        }
        
        shipment = (SDbShipment) miClient.getSession().readRegistry(SModConsts.LOG_SHIP, new int[] { moRegistry.getPkShipmentId() });
        
        moKeySpot.setValue(new int[] { moRegistry.getFkSpotId() });
        moKeyCustomer.setValue(new int[] { moRegistry.getFkCustomerId_n() });
        moKeyCustomerBranch.setValue(new int[] { moRegistry.getFkCustomerBranchId_n() });
        moKeyCustomerBranchAddress.setValue(new int[] { moRegistry.getFkCustomerBranchId_n(), moRegistry.getFkCustomerBranchAddressId_n()});
        moKeyWarehouseCompanyBranch.setValue(new int[] { moRegistry.getFkWarehouseCompanyBranchId_n() });
        moKeyWarehouseEntity.setValue(new int[] { moRegistry.getFkWarehouseCompanyBranchId_n(), moRegistry.getFkWarehouseEntityId_n() });
        moDateDateDelivery.setValue(moRegistry.getDateDelivery());
        moDateDateDeliveryReal.setValue(moRegistry.getDateDeliveryReal());
        
        moKeyCompanyBranch.setValue(new int[] { shipment.getFkCompanyBranchId() });
        moKeyShipmentType.setValue(new int[] { shipment.getFkShipmentTypeId() });
        moKeyDeliveryType.setValue(new int[] { shipment.getFkDeliveryTypeId() });
        moDateDate.setValue(shipment.getDate() );
        moTextNumber.setValue(shipment.getNumber());
        
        setFormEditable(true);
        
        moKeySpot.setEnabled(false);
        moKeyCustomer.setEnabled(false);
        moKeyCustomerBranch.setEnabled(false);
        moKeyCustomerBranchAddress.setEnabled(false);
        moKeyWarehouseCompanyBranch.setEnabled(false);
        moKeyWarehouseEntity.setEnabled(false);
        moDateDateDelivery.setEnabled(false);
        moDateDateDeliveryReal.setEditable(true);
        
        moKeyCompanyBranch.setEnabled(false);
        moKeyShipmentType.setEnabled(false);
        moKeyDeliveryType.setEnabled(false);
        moDateDate.setEditable(false);
        moTextNumber.setEditable(false);
        
        if (moRegistry.isRegistryNew()) { 
            moDateDateDeliveryReal.setValue(miClient.getSession().getCurrentDate());
        }
        
        addAllListeners();
    }

    @Override
    public SDbRegistry getRegistry() throws Exception {
        SDbShipmentDestiny registry = moRegistry.clone();
        
        if (registry.isRegistryNew()) {}

        registry.getShipmentDestinyEntries().clear();
        
        registry.setDateDeliveryReal(moDateDateDeliveryReal.getValue());
        registry.setDelivery(true);
        
        return registry;
    }

    @Override
    public SGuiValidation validateForm() {
        SGuiValidation validation = moFields.validateFields();

        if (validation.isValid()) {
            if (moDateDateDeliveryReal.getValue().before(moDateDateDelivery.getValue())) {
                validation.setMessage("El valor para el campo '"  + SGuiUtils.getLabelName(jlDateDeliveryReal) + "' no puede se anterior al campo '" + SGuiUtils.getLabelName(jlDateDelivery) + "'.");
                validation.setComponent(moDateDateDeliveryReal);
            }
        }
        
        return validation;
    }
}
