001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016
017package com.echothree.control.user.purchase.server.command;
018
019import com.echothree.control.user.purchase.common.form.CreatePurchaseInvoiceLineForm;
020import com.echothree.control.user.purchase.common.result.CreatePurchaseInvoiceLineResult;
021import com.echothree.control.user.purchase.common.result.PurchaseResultFactory;
022import com.echothree.model.control.accounting.server.control.AccountingControl;
023import com.echothree.model.control.invoice.common.InvoiceTypes;
024import com.echothree.model.control.invoice.server.control.InvoiceControl;
025import com.echothree.model.control.invoice.server.logic.InvoiceLogic;
026import com.echothree.model.control.invoice.server.logic.PurchaseInvoiceLogic;
027import com.echothree.model.control.party.common.PartyTypes;
028import com.echothree.model.control.security.common.SecurityRoleGroups;
029import com.echothree.model.control.security.common.SecurityRoles;
030import com.echothree.model.data.accounting.server.entity.GlAccount;
031import com.echothree.model.data.invoice.server.entity.Invoice;
032import com.echothree.model.data.invoice.server.entity.InvoiceLine;
033import com.echothree.model.data.invoice.server.entity.InvoiceLineDetail;
034import com.echothree.model.data.invoice.server.entity.InvoiceLineType;
035import com.echothree.model.data.invoice.server.entity.InvoiceType;
036import com.echothree.model.data.user.common.pk.UserVisitPK;
037import com.echothree.util.common.command.BaseResult;
038import com.echothree.util.common.message.ExecutionErrors;
039import com.echothree.util.common.validation.FieldDefinition;
040import com.echothree.util.common.validation.FieldType;
041import com.echothree.util.server.control.BaseSimpleCommand;
042import com.echothree.util.server.control.CommandSecurityDefinition;
043import com.echothree.util.server.control.PartyTypeDefinition;
044import com.echothree.util.server.control.SecurityRoleDefinition;
045import com.echothree.util.server.persistence.Session;
046import com.echothree.util.server.validation.Validator;
047import java.util.Arrays;
048import java.util.Collections;
049import java.util.List;
050
051public class CreatePurchaseInvoiceLineCommand
052        extends BaseSimpleCommand<CreatePurchaseInvoiceLineForm> {
053    
054    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
055    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
056    
057    static {
058        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
059                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
060                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
061                    new SecurityRoleDefinition(SecurityRoleGroups.PurchaseInvoiceLine.name(), SecurityRoles.Create.name())
062                    )))
063                )));
064        
065        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
066                new FieldDefinition("InvoiceName", FieldType.ENTITY_NAME, true, null, null),
067                new FieldDefinition("InvoiceLineSequence", FieldType.UNSIGNED_INTEGER, false, null, null),
068                new FieldDefinition("ParentInvoiceLine", FieldType.UNSIGNED_INTEGER, false, null, null),
069                new FieldDefinition("Amount", FieldType.COST_UNIT, true, null, null),
070                new FieldDefinition("InvoiceLineTypeName", FieldType.ENTITY_NAME, true, null, null),
071                new FieldDefinition("GlAccountName", FieldType.ENTITY_NAME, false, null, null),
072                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
073                ));
074    }
075    
076    /** Creates a new instance of CreatePurchaseInvoiceLineCommand */
077    public CreatePurchaseInvoiceLineCommand(UserVisitPK userVisitPK, CreatePurchaseInvoiceLineForm form) {
078        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
079    }
080    
081    @Override
082    protected void setupValidator(Validator validator) {
083        String invoiceName = form.getInvoiceName();
084        Invoice invoice = invoiceName == null? null: PurchaseInvoiceLogic.getInstance().getInvoiceByName(invoiceName);
085        
086        if(invoice != null) {
087            validator.setCurrency(InvoiceLogic.getInstance().getInvoiceCurrency(invoice));
088        }
089    }
090    
091    @Override
092    protected BaseResult execute() {
093        var invoiceControl = Session.getModelController(InvoiceControl.class);
094        CreatePurchaseInvoiceLineResult result = PurchaseResultFactory.getCreatePurchaseInvoiceLineResult();
095        InvoiceLine invoiceLine = null;
096        String invoiceName = form.getInvoiceName();
097        InvoiceType invoiceType = invoiceControl.getInvoiceTypeByName(InvoiceTypes.PURCHASE_INVOICE.name());
098        Invoice invoice = invoiceControl.getInvoiceByName(invoiceType, invoiceName);
099        
100        if(invoice != null) {
101            String rawParentInvoiceLine = form.getParentInvoiceLine();
102            Integer intParentInvoiceLine = rawParentInvoiceLine == null? null: Integer.valueOf(rawParentInvoiceLine);
103            InvoiceLine parentInvoiceLine = intParentInvoiceLine == null? null: invoiceControl.getInvoiceLine(invoice, intParentInvoiceLine);
104            
105            if(rawParentInvoiceLine == null || parentInvoiceLine != null) {
106                String invoiceTypeName = form.getInvoiceLineTypeName();
107                InvoiceLineType invoiceLineType = invoiceControl.getInvoiceLineTypeByName(invoiceType, invoiceTypeName);
108                
109                if(invoiceLineType != null) {
110                    var accountingControl = Session.getModelController(AccountingControl.class);
111                    String glAccountName = form.getGlAccountName();
112                    GlAccount glAccount = glAccountName == null? null: accountingControl.getGlAccountByName(glAccountName);
113                    
114                    if(glAccountName == null || glAccount != null) {
115                        String strInvoiceLineSequence = form.getInvoiceLineSequence();
116                        Integer invoiceLineSequence = strInvoiceLineSequence == null? null: Integer.valueOf(strInvoiceLineSequence);
117                        
118                        if(invoiceLineSequence == null || invoiceControl.getInvoiceLine(invoice, invoiceLineSequence) == null) {
119                            Long amount = Long.valueOf(form.getAmount());
120                            var description = form.getDescription();
121
122                            invoiceLine = PurchaseInvoiceLogic.getInstance().createInvoiceLine(this, invoice, invoiceLineSequence, parentInvoiceLine, amount, invoiceLineType, glAccount,
123                                    description, getPartyPK());
124                        } else {
125                            addExecutionError(ExecutionErrors.DuplicateInvoiceLineSequence.name(), invoiceName, strInvoiceLineSequence);
126                        }
127                    } else {
128                        addExecutionError(ExecutionErrors.UnknownGlAccountName.name(), glAccountName);
129                    }
130                } else {
131                    addExecutionError(ExecutionErrors.UnknownInvoiceLineTypeName.name(), InvoiceTypes.PURCHASE_INVOICE.name(), invoiceTypeName);
132                }
133            } else {
134                addExecutionError(ExecutionErrors.UnknownParentInvoiceLine.name(), invoiceName, rawParentInvoiceLine);
135            }
136        } else {
137            addExecutionError(ExecutionErrors.UnknownPurchaseInvoiceName.name(), invoiceName);
138        }
139        
140        if(invoiceLine != null) {
141            InvoiceLineDetail invoiceLineDetail = invoiceLine.getLastDetail();
142            
143            result.setEntityRef(invoiceLine.getPrimaryKey().getEntityRef());
144            result.setInvoiceName(invoiceLineDetail.getInvoice().getLastDetail().getInvoiceName());
145            result.setInvoiceLineSequence(invoiceLineDetail.getInvoiceLineSequence().toString());
146        }
147        
148        return result;
149    }
150    
151}