001// -------------------------------------------------------------------------------- 002// Copyright 2002-2026 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.PurchaseResultFactory; 021import com.echothree.model.control.accounting.server.control.AccountingControl; 022import com.echothree.model.control.invoice.common.InvoiceTypes; 023import com.echothree.model.control.invoice.server.control.InvoiceControl; 024import com.echothree.model.control.invoice.server.logic.InvoiceLogic; 025import com.echothree.model.control.invoice.server.logic.PurchaseInvoiceLogic; 026import com.echothree.model.control.party.common.PartyTypes; 027import com.echothree.model.control.security.common.SecurityRoleGroups; 028import com.echothree.model.control.security.common.SecurityRoles; 029import com.echothree.model.data.invoice.server.entity.InvoiceLine; 030import com.echothree.model.data.user.common.pk.UserVisitPK; 031import com.echothree.util.common.command.BaseResult; 032import com.echothree.util.common.message.ExecutionErrors; 033import com.echothree.util.common.validation.FieldDefinition; 034import com.echothree.util.common.validation.FieldType; 035import com.echothree.util.server.control.BaseSimpleCommand; 036import com.echothree.util.server.control.CommandSecurityDefinition; 037import com.echothree.util.server.control.PartyTypeDefinition; 038import com.echothree.util.server.control.SecurityRoleDefinition; 039import com.echothree.util.server.validation.Validator; 040import java.util.List; 041import javax.enterprise.context.Dependent; 042import javax.inject.Inject; 043 044@Dependent 045public class CreatePurchaseInvoiceLineCommand 046 extends BaseSimpleCommand<CreatePurchaseInvoiceLineForm> { 047 048 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 049 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 050 051 static { 052 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 053 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 054 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 055 new SecurityRoleDefinition(SecurityRoleGroups.PurchaseInvoiceLine.name(), SecurityRoles.Create.name()) 056 )) 057 )); 058 059 FORM_FIELD_DEFINITIONS = List.of( 060 new FieldDefinition("InvoiceName", FieldType.ENTITY_NAME, true, null, null), 061 new FieldDefinition("InvoiceLineSequence", FieldType.UNSIGNED_INTEGER, false, null, null), 062 new FieldDefinition("ParentInvoiceLine", FieldType.UNSIGNED_INTEGER, false, null, null), 063 new FieldDefinition("Amount", FieldType.COST_UNIT, true, null, null), 064 new FieldDefinition("InvoiceLineTypeName", FieldType.ENTITY_NAME, true, null, null), 065 new FieldDefinition("GlAccountName", FieldType.ENTITY_NAME, false, null, null), 066 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 067 ); 068 } 069 070 @Inject 071 AccountingControl accountingControl; 072 073 @Inject 074 InvoiceControl invoiceControl; 075 076 @Inject 077 InvoiceLogic invoiceLogic; 078 079 @Inject 080 PurchaseInvoiceLogic purchaseInvoiceLogic; 081 082 083 /** Creates a new instance of CreatePurchaseInvoiceLineCommand */ 084 public CreatePurchaseInvoiceLineCommand() { 085 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 086 } 087 088 @Override 089 protected void setupValidator(Validator validator) { 090 var invoiceName = form.getInvoiceName(); 091 var invoice = invoiceName == null? null: purchaseInvoiceLogic.getInvoiceByName(invoiceName); 092 093 if(invoice != null) { 094 validator.setCurrency(invoiceLogic.getInvoiceCurrency(invoice)); 095 } 096 } 097 098 @Override 099 protected BaseResult execute() { 100 var result = PurchaseResultFactory.getCreatePurchaseInvoiceLineResult(); 101 InvoiceLine invoiceLine = null; 102 var invoiceName = form.getInvoiceName(); 103 var invoiceType = invoiceControl.getInvoiceTypeByName(InvoiceTypes.PURCHASE_INVOICE.name()); 104 var invoice = invoiceControl.getInvoiceByName(invoiceType, invoiceName); 105 106 if(invoice != null) { 107 var rawParentInvoiceLine = form.getParentInvoiceLine(); 108 var intParentInvoiceLine = rawParentInvoiceLine == null? null: Integer.valueOf(rawParentInvoiceLine); 109 var parentInvoiceLine = intParentInvoiceLine == null? null: invoiceControl.getInvoiceLine(invoice, intParentInvoiceLine); 110 111 if(rawParentInvoiceLine == null || parentInvoiceLine != null) { 112 var invoiceTypeName = form.getInvoiceLineTypeName(); 113 var invoiceLineType = invoiceControl.getInvoiceLineTypeByName(invoiceType, invoiceTypeName); 114 115 if(invoiceLineType != null) { 116 var glAccountName = form.getGlAccountName(); 117 var glAccount = glAccountName == null? null: accountingControl.getGlAccountByName(glAccountName); 118 119 if(glAccountName == null || glAccount != null) { 120 var strInvoiceLineSequence = form.getInvoiceLineSequence(); 121 var invoiceLineSequence = strInvoiceLineSequence == null? null: Integer.valueOf(strInvoiceLineSequence); 122 123 if(invoiceLineSequence == null || invoiceControl.getInvoiceLine(invoice, invoiceLineSequence) == null) { 124 var amount = Long.valueOf(form.getAmount()); 125 var description = form.getDescription(); 126 127 invoiceLine = purchaseInvoiceLogic.createInvoiceLine(this, invoice, invoiceLineSequence, parentInvoiceLine, amount, invoiceLineType, glAccount, 128 description, getPartyPK()); 129 } else { 130 addExecutionError(ExecutionErrors.DuplicateInvoiceLineSequence.name(), invoiceName, strInvoiceLineSequence); 131 } 132 } else { 133 addExecutionError(ExecutionErrors.UnknownGlAccountName.name(), glAccountName); 134 } 135 } else { 136 addExecutionError(ExecutionErrors.UnknownInvoiceLineTypeName.name(), InvoiceTypes.PURCHASE_INVOICE.name(), invoiceTypeName); 137 } 138 } else { 139 addExecutionError(ExecutionErrors.UnknownParentInvoiceLine.name(), invoiceName, rawParentInvoiceLine); 140 } 141 } else { 142 addExecutionError(ExecutionErrors.UnknownPurchaseInvoiceName.name(), invoiceName); 143 } 144 145 if(invoiceLine != null) { 146 var invoiceLineDetail = invoiceLine.getLastDetail(); 147 148 result.setEntityRef(invoiceLine.getPrimaryKey().getEntityRef()); 149 result.setInvoiceName(invoiceLineDetail.getInvoice().getLastDetail().getInvoiceName()); 150 result.setInvoiceLineSequence(invoiceLineDetail.getInvoiceLineSequence().toString()); 151 } 152 153 return result; 154 } 155 156}