2021-03-05 20:06:49 +00:00
/ *
*
* ( c ) Copyright Ascensio System Limited 2010 - 2018
*
* This program is freeware . You can redistribute it and / or modify it under the terms of the GNU
* General Public License ( GPL ) version 3 as published by the Free Software Foundation ( https : //www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7 ( a ) of the GNU GPL its Section 15 shall be amended to the effect that
* Ascensio System SIA expressly excludes the warranty of non - infringement of any third - party rights .
*
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY ; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE . For more details , see GNU GPL at https : //www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales @onlyoffice . com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices , as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3 ( b ) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software . If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons , you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute .
* Pursuant to Section 7 § 3 ( e ) we decline to grant you any rights under trademark law for use of our trademarks .
*
* /
2021-03-17 11:50:13 +00:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
2022-02-10 18:25:40 +00:00
using System.Threading.Tasks ;
2021-03-17 11:50:13 +00:00
2021-03-05 20:06:49 +00:00
using ASC.Api.Core ;
2021-03-17 11:50:13 +00:00
using ASC.Api.CRM ;
2021-03-05 20:06:49 +00:00
using ASC.Common.Web ;
2021-03-17 11:50:13 +00:00
using ASC.Core.Common.Settings ;
using ASC.CRM.ApiModels ;
2021-06-28 09:42:42 +00:00
2021-03-05 20:06:49 +00:00
using ASC.CRM.Classes ;
using ASC.CRM.Core ;
2021-03-17 11:50:13 +00:00
using ASC.CRM.Core.Dao ;
2021-03-05 20:06:49 +00:00
using ASC.CRM.Core.Entities ;
using ASC.CRM.Core.Enums ;
using ASC.CRM.Resources ;
2022-05-19 15:47:59 +00:00
using ASC.Files.Core.ApiModels.ResponseDto ;
using ASC.MessagingSystem.Core ;
using ASC.MessagingSystem.Models ;
2021-03-05 20:06:49 +00:00
using ASC.Web.CRM.Classes ;
2021-03-17 11:50:13 +00:00
using AutoMapper ;
2021-03-05 20:06:49 +00:00
2021-03-17 11:50:13 +00:00
using Microsoft.AspNetCore.Mvc ;
2021-03-05 20:06:49 +00:00
2021-03-10 15:38:56 +00:00
namespace ASC.CRM.Api
2021-03-05 20:06:49 +00:00
{
2021-03-10 15:38:56 +00:00
public class InvoicesController : BaseApiController
2021-03-05 20:06:49 +00:00
{
2021-03-19 16:56:26 +00:00
private readonly PdfQueueWorker _pdfQueueWorker ;
private readonly Global _global ;
2022-05-19 15:47:59 +00:00
private readonly FileDtoHelper _fileDtoHelper ;
2021-03-19 16:56:26 +00:00
private readonly PdfCreator _pdfCreator ;
private readonly SettingsManager _settingsManager ;
private readonly ApiDateTimeHelper _apiDateTimeHelper ;
private readonly ApiContext _apiContext ;
private readonly MessageService _messageService ;
private readonly MessageTarget _messageTarget ;
2021-03-23 15:41:56 +00:00
private readonly CurrencyProvider _currencyProvider ;
2021-03-19 16:56:26 +00:00
2021-05-05 14:09:05 +00:00
public InvoicesController ( CrmSecurity crmSecurity ,
2021-03-10 15:38:56 +00:00
DaoFactory daoFactory ,
ApiContext apiContext ,
MessageTarget messageTarget ,
MessageService messageService ,
ApiDateTimeHelper apiDateTimeHelper ,
SettingsManager settingsManager ,
2022-05-19 15:47:59 +00:00
FileDtoHelper fileDtoHelper ,
2021-03-10 15:38:56 +00:00
PdfCreator pdfCreator ,
Global global ,
2021-03-17 11:50:13 +00:00
PdfQueueWorker pdfQueueWorker ,
2021-03-23 15:41:56 +00:00
CurrencyProvider currencyProvider ,
2021-03-17 11:50:13 +00:00
IMapper mapper )
2021-03-19 16:56:26 +00:00
: base ( daoFactory , crmSecurity , mapper )
2021-03-10 15:38:56 +00:00
{
2021-03-19 16:56:26 +00:00
_apiContext = apiContext ;
_messageTarget = messageTarget ;
_messageService = messageService ;
_apiDateTimeHelper = apiDateTimeHelper ;
_settingsManager = settingsManager ;
_pdfCreator = pdfCreator ;
2022-05-19 15:47:59 +00:00
_fileDtoHelper = fileDtoHelper ;
2021-03-19 16:56:26 +00:00
_global = global ;
_pdfQueueWorker = pdfQueueWorker ;
2021-03-17 11:50:13 +00:00
_mapper = mapper ;
2021-03-23 15:41:56 +00:00
_currencyProvider = currencyProvider ;
2021-03-10 15:38:56 +00:00
}
2021-03-19 16:56:26 +00:00
2021-03-05 20:06:49 +00:00
/// <summary>
/// Returns the detailed information about the invoice with the ID specified in the request
/// </summary>
/// <param name="invoiceid">Invoice ID</param>
/// <short>Get invoice by ID</short>
/// <category>Invoices</category>
/// <returns>Invoice</returns>
2022-05-31 14:01:58 +00:00
[HttpGet(@"invoice/{invoiceid:int}")]
2021-03-09 15:37:16 +00:00
public InvoiceDto GetInvoiceByID ( int invoiceid )
2021-03-05 20:06:49 +00:00
{
if ( invoiceid < = 0 ) throw new ArgumentException ( ) ;
2021-03-19 16:56:26 +00:00
var invoice = _daoFactory . GetInvoiceDao ( ) . GetByID ( invoiceid ) ;
2021-03-05 20:06:49 +00:00
if ( invoice = = null ) throw new ItemNotFoundException ( ) ;
2021-03-19 16:56:26 +00:00
if ( ! _crmSecurity . CanAccessTo ( invoice ) )
2021-03-05 20:06:49 +00:00
{
2021-03-19 16:56:26 +00:00
throw _crmSecurity . CreateSecurityException ( ) ;
2021-03-05 20:06:49 +00:00
}
2021-03-19 16:56:26 +00:00
return _mapper . Map < InvoiceDto > ( invoice ) ;
2021-03-05 20:06:49 +00:00
}
/// <summary>
/// Returns the detailed information about the invoice sample
/// </summary>
/// <short>Get invoice sample</short>
/// <category>Invoices</category>
/// <returns>Invoice</returns>
2022-05-31 14:01:58 +00:00
[HttpGet(@"invoice/sample")]
2021-03-09 15:37:16 +00:00
public InvoiceDto GetInvoiceSample ( )
2021-03-05 20:06:49 +00:00
{
2021-05-05 14:09:05 +00:00
var crmSettings = _settingsManager . Load < CrmSettings > ( ) ;
2021-03-23 15:41:56 +00:00
var defaultCurrency = _currencyProvider . Get ( crmSettings . DefaultCurrency ) ;
2021-03-19 16:56:26 +00:00
2021-03-09 15:37:16 +00:00
var sample = InvoiceDto . GetSample ( ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
sample . Number = _daoFactory . GetInvoiceDao ( ) . GetNewInvoicesNumber ( ) ;
sample . Terms = _daoFactory . GetInvoiceDao ( ) . GetSettings ( ) . Terms ? ? string . Empty ;
sample . IssueDate = _apiDateTimeHelper . Get ( DateTime . UtcNow ) ;
sample . DueDate = _apiDateTimeHelper . Get ( DateTime . UtcNow . AddDays ( 30 ) ) ;
sample . CreateOn = _apiDateTimeHelper . Get ( DateTime . UtcNow ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
sample . Currency = _mapper . Map < CurrencyInfoDto > ( defaultCurrency ) ;
2021-03-05 20:06:49 +00:00
sample . InvoiceLines . First ( ) . Quantity = 1 ;
return sample ;
}
/// <summary>
/// Returns the json data of the invoice with the ID specified in the request
/// </summary>
/// <param name="invoiceid">Invoice ID</param>
/// <short>Get invoice json data</short>
/// <category>Invoices</category>
/// <returns>Json Data</returns>
2022-05-31 14:01:58 +00:00
[HttpGet(@"invoice/jsondata/{invoiceid:int}")]
2021-03-05 20:06:49 +00:00
public string GetInvoiceJsonData ( int invoiceid )
{
2021-03-19 16:56:26 +00:00
var invoice = _daoFactory . GetInvoiceDao ( ) . GetByID ( invoiceid ) ;
2021-03-05 20:06:49 +00:00
if ( invoice = = null ) throw new ItemNotFoundException ( ) ;
2021-03-19 16:56:26 +00:00
if ( ! _crmSecurity . CanAccessTo ( invoice ) )
2021-03-05 20:06:49 +00:00
{
2021-03-19 16:56:26 +00:00
throw _crmSecurity . CreateSecurityException ( ) ;
2021-03-05 20:06:49 +00:00
}
return invoice . JsonData ;
}
/// <summary>
/// Returns the list of invoices matching the creteria specified in the request
/// </summary>
/// <param name="status">Invoice status</param>
/// <param name="issueDateFrom">Invoice issue date from</param>
/// <param name="issueDateTo">Invoice issue date to</param>
/// <param name="dueDateFrom">Invoice due date from</param>
/// <param name="dueDateTo">Invoice due date to</param>
/// <param name="entityType">Invoice entity type</param>
/// <param name="entityid">Invoice entity ID</param>
/// <param name="currency" remark="Allowed values: EUR, RUB etc. You can get the whole list of available currencies by api">Invoice currency</param>
/// <short>Get invoice list</short>
/// <category>Invoices</category>
/// <returns>Invoice list</returns>
2022-05-31 14:01:58 +00:00
[HttpGet(@"invoice/filter")]
2021-03-09 15:37:16 +00:00
public IEnumerable < InvoiceBaseDto > GetInvoices (
2021-03-05 20:06:49 +00:00
InvoiceStatus ? status ,
ApiDateTime issueDateFrom ,
ApiDateTime issueDateTo ,
ApiDateTime dueDateFrom ,
ApiDateTime dueDateTo ,
String entityType ,
int entityid ,
String currency
)
{
if ( ! String . IsNullOrEmpty ( entityType ) & & ! (
2022-01-12 15:42:03 +00:00
string . Equals ( entityType , "contact" , StringComparison . CurrentCultureIgnoreCase ) | |
string . Equals ( entityType , "opportunity" , StringComparison . CurrentCultureIgnoreCase ) | |
string . Equals ( entityType , "case" , StringComparison . CurrentCultureIgnoreCase ) ) )
2021-03-05 20:06:49 +00:00
throw new ArgumentException ( ) ;
2021-03-09 15:37:16 +00:00
IEnumerable < InvoiceBaseDto > result ;
2021-03-05 20:06:49 +00:00
InvoiceSortedByType sortBy ;
OrderBy invoiceOrderBy ;
2021-03-19 16:56:26 +00:00
var searchString = _apiContext . FilterValue ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
if ( InvoiceSortedByType . TryParse ( _apiContext . SortBy , true , out sortBy ) )
2021-03-05 20:06:49 +00:00
{
2021-03-19 16:56:26 +00:00
invoiceOrderBy = new OrderBy ( sortBy , ! _apiContext . SortDescending ) ;
2021-03-05 20:06:49 +00:00
}
2021-03-19 16:56:26 +00:00
else if ( String . IsNullOrEmpty ( _apiContext . SortBy ) )
2021-03-05 20:06:49 +00:00
{
invoiceOrderBy = new OrderBy ( InvoiceSortedByType . Number , true ) ;
}
else
{
invoiceOrderBy = null ;
}
2021-03-19 16:56:26 +00:00
var fromIndex = ( int ) _apiContext . StartIndex ;
var count = ( int ) _apiContext . Count ;
2021-03-05 20:06:49 +00:00
if ( invoiceOrderBy ! = null )
{
2021-03-09 15:37:16 +00:00
result = ToListInvoiceBaseDtos (
2021-03-19 16:56:26 +00:00
_daoFactory . GetInvoiceDao ( ) . GetInvoices (
2021-03-05 20:06:49 +00:00
searchString ,
status ,
issueDateFrom , issueDateTo ,
dueDateFrom , dueDateTo ,
ToEntityType ( entityType ) , entityid ,
currency ,
fromIndex , count ,
invoiceOrderBy ) ) ;
2021-03-19 16:56:26 +00:00
_apiContext . SetDataPaginated ( ) ;
_apiContext . SetDataFiltered ( ) ;
_apiContext . SetDataSorted ( ) ;
2021-03-05 20:06:49 +00:00
}
else
{
2021-03-09 15:37:16 +00:00
result = ToListInvoiceBaseDtos (
2021-03-19 16:56:26 +00:00
_daoFactory . GetInvoiceDao ( ) . GetInvoices (
2021-03-05 20:06:49 +00:00
searchString ,
status ,
issueDateFrom , issueDateTo ,
dueDateFrom , dueDateTo ,
ToEntityType ( entityType ) , entityid ,
currency ,
0 ,
0 ,
null ) ) ;
}
int totalCount ;
if ( result . Count ( ) < count )
{
totalCount = fromIndex + result . Count ( ) ;
}
else
{
2021-03-19 16:56:26 +00:00
totalCount = _daoFactory . GetInvoiceDao ( ) . GetInvoicesCount (
2021-03-05 20:06:49 +00:00
searchString ,
status ,
issueDateFrom , issueDateTo ,
dueDateFrom , dueDateTo ,
ToEntityType ( entityType ) , entityid ,
currency ) ;
}
2021-03-19 16:56:26 +00:00
_apiContext . SetTotalCount ( totalCount ) ;
2021-03-05 20:06:49 +00:00
return result ;
}
/// <summary>
/// Returns the list of all invoices associated with the entity with the ID and type specified in the request
/// </summary>
/// <param name="entityType">Invoice entity type</param>
/// <param name="entityid">Invoice entity ID</param>
/// <short>Get entity invoices</short>
/// <category>Invoices</category>
/// <returns>Invoice list</returns>
2022-05-31 14:01:58 +00:00
[HttpGet(@"{entityType:regex(contact|person|company|opportunity)}/invoicelist/{entityid:int}")]
2021-03-09 15:37:16 +00:00
public IEnumerable < InvoiceBaseDto > GetEntityInvoices ( String entityType , int entityid )
2021-03-05 20:06:49 +00:00
{
if ( String . IsNullOrEmpty ( entityType ) | | entityid < = 0 ) throw new ArgumentException ( ) ;
2021-03-19 16:56:26 +00:00
return ToListInvoiceBaseDtos ( _daoFactory . GetInvoiceDao ( ) . GetEntityInvoices ( ToEntityType ( entityType ) , entityid ) ) ;
2021-03-05 20:06:49 +00:00
}
/// <summary>
/// Updates the status of invoices with the IDs specified in the request
/// </summary>
/// <param name="invoiceids">Invoice ID list</param>
/// <param name="status">Status</param>
/// <short>Update invoice group status</short>
/// <category>Invoices</category>
/// <returns>KeyValuePair of Invoices and InvoiceItems</returns>
2022-05-31 14:01:58 +00:00
[HttpPut(@"invoice/status/{status:int}")]
2021-03-09 15:37:16 +00:00
public KeyValuePair < IEnumerable < InvoiceBaseDto > , IEnumerable < InvoiceItemDto > > UpdateInvoiceBatchStatus (
2021-03-05 20:06:49 +00:00
int [ ] invoiceids ,
InvoiceStatus status
)
{
if ( invoiceids = = null | | ! invoiceids . Any ( ) ) throw new ArgumentException ( ) ;
2021-03-19 16:56:26 +00:00
var oldInvoices = _daoFactory . GetInvoiceDao ( ) . GetByID ( invoiceids ) . Where ( _crmSecurity . CanAccessTo ) . ToList ( ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
var updatedInvoices = _daoFactory . GetInvoiceDao ( ) . UpdateInvoiceBatchStatus ( oldInvoices . ToList ( ) . Select ( i = > i . ID ) . ToArray ( ) , status ) ;
2021-03-05 20:06:49 +00:00
// detect what really changed
var realUpdatedInvoices = updatedInvoices
. Select ( t = > oldInvoices . FirstOrDefault ( x = > x . ID = = t . ID & & x . Status ! = t . Status ) )
. Where ( inv = > inv ! = null )
. ToList ( ) ;
if ( realUpdatedInvoices . Any ( ) )
{
2021-03-19 16:56:26 +00:00
_messageService . Send ( MessageAction . InvoicesUpdatedStatus , _messageTarget . Create ( realUpdatedInvoices . Select ( x = > x . ID ) ) , realUpdatedInvoices . Select ( x = > x . Number ) , status . ToLocalizedString ( ) ) ;
2021-03-05 20:06:49 +00:00
}
var invoiceItemsUpdated = new List < InvoiceItem > ( ) ;
if ( status = = InvoiceStatus . Sent | | status = = InvoiceStatus . Rejected )
{
2021-03-19 16:56:26 +00:00
var invoiceItemsAll = _daoFactory . GetInvoiceItemDao ( ) . GetAll ( ) ;
2021-03-05 20:06:49 +00:00
var invoiceItemsWithTrackInventory = invoiceItemsAll . Where ( item = > item . TrackInventory ) . ToList ( ) ;
if ( status = = InvoiceStatus . Sent & & invoiceItemsWithTrackInventory ! = null & & invoiceItemsWithTrackInventory . Count ! = 0 )
{
foreach ( var inv in updatedInvoices )
{
if ( inv . Status = = InvoiceStatus . Sent )
{
//could be changed
var oldInv = oldInvoices . FirstOrDefault ( i = > i . ID = = inv . ID ) ;
if ( oldInv ! = null & & oldInv . Status = = InvoiceStatus . Draft )
{
//was changed to Sent
2021-03-19 16:56:26 +00:00
var invoiceLines = _daoFactory . GetInvoiceLineDao ( ) . GetInvoiceLines ( inv . ID ) ;
2021-03-05 20:06:49 +00:00
foreach ( var line in invoiceLines )
{
var item = invoiceItemsWithTrackInventory . FirstOrDefault ( ii = > ii . ID = = line . InvoiceItemID ) ;
if ( item ! = null )
{
item . StockQuantity - = line . Quantity ;
2021-03-19 16:56:26 +00:00
_daoFactory . GetInvoiceItemDao ( ) . SaveOrUpdateInvoiceItem ( item ) ;
2021-03-05 20:06:49 +00:00
var oldItem = invoiceItemsUpdated . Find ( i = > i . ID = = item . ID ) ;
if ( oldItem ! = null )
{
invoiceItemsUpdated . Remove ( oldItem ) ;
}
invoiceItemsUpdated . Add ( item ) ;
}
}
}
}
}
}
if ( status = = InvoiceStatus . Rejected & & invoiceItemsWithTrackInventory ! = null & & invoiceItemsWithTrackInventory . Count ! = 0 )
{
foreach ( var inv in updatedInvoices )
{
if ( inv . Status = = InvoiceStatus . Rejected )
{
//could be changed
var oldInv = oldInvoices . FirstOrDefault ( i = > i . ID = = inv . ID ) ;
if ( oldInv ! = null & & oldInv . Status = = InvoiceStatus . Sent )
{
//was changed from Sent to Rejectes
2021-03-19 16:56:26 +00:00
var invoiceLines = _daoFactory . GetInvoiceLineDao ( ) . GetInvoiceLines ( inv . ID ) ;
2021-03-05 20:06:49 +00:00
foreach ( var line in invoiceLines )
{
var item = invoiceItemsWithTrackInventory . FirstOrDefault ( ii = > ii . ID = = line . InvoiceItemID ) ;
if ( item ! = null )
{
item . StockQuantity + = line . Quantity ;
2021-03-19 16:56:26 +00:00
_daoFactory . GetInvoiceItemDao ( ) . SaveOrUpdateInvoiceItem ( item ) ;
2021-03-05 20:06:49 +00:00
var oldItem = invoiceItemsUpdated . Find ( i = > i . ID = = item . ID ) ;
2021-03-19 16:56:26 +00:00
2021-03-05 20:06:49 +00:00
if ( oldItem ! = null )
{
invoiceItemsUpdated . Remove ( oldItem ) ;
}
2021-03-19 16:56:26 +00:00
2021-03-05 20:06:49 +00:00
invoiceItemsUpdated . Add ( item ) ;
}
}
}
}
}
}
}
2021-03-09 15:37:16 +00:00
var listInvoiceBaseDtos = ToListInvoiceBaseDtos ( updatedInvoices ) ;
2021-03-05 20:06:49 +00:00
2021-03-17 11:50:13 +00:00
return new KeyValuePair < IEnumerable < InvoiceBaseDto > , IEnumerable < InvoiceItemDto > > (
listInvoiceBaseDtos ,
_mapper . Map < List < InvoiceItem > , List < InvoiceItemDto > > ( invoiceItemsUpdated ) ) ;
2021-03-05 20:06:49 +00:00
}
/// <summary>
/// Delete the invoice with the ID specified in the request
/// </summary>
/// <param name="invoiceid">Invoice ID</param>
/// <short>Delete invoice</short>
/// <category>Invoices</category>
/// <returns>Invoice</returns>
2022-05-31 14:01:58 +00:00
[HttpDelete(@"invoice/{invoiceid:int}")]
2021-03-09 15:37:16 +00:00
public InvoiceBaseDto DeleteInvoice ( int invoiceid )
2021-03-05 20:06:49 +00:00
{
if ( invoiceid < = 0 ) throw new ArgumentException ( ) ;
2021-03-19 16:56:26 +00:00
var invoice = _daoFactory . GetInvoiceDao ( ) . DeleteInvoice ( invoiceid ) ;
2021-03-17 11:50:13 +00:00
2021-03-05 20:06:49 +00:00
if ( invoice = = null ) throw new ItemNotFoundException ( ) ;
2021-03-19 16:56:26 +00:00
_messageService . Send ( MessageAction . InvoiceDeleted , _messageTarget . Create ( invoice . ID ) , invoice . Number ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
return _mapper . Map < InvoiceBaseDto > ( invoice ) ;
2021-03-05 20:06:49 +00:00
}
/// <summary>
/// Deletes the group of invoices with the IDs specified in the request
/// </summary>
/// <param name="invoiceids">Invoice ID list</param>
/// <short>Delete invoice group</short>
/// <category>Invoices</category>
/// <returns>Invoice list</returns>
2022-05-31 14:01:58 +00:00
[HttpDelete(@"invoice")]
2021-03-09 15:37:16 +00:00
public IEnumerable < InvoiceBaseDto > DeleteBatchInvoices ( IEnumerable < int > invoiceids )
2021-03-05 20:06:49 +00:00
{
if ( invoiceids = = null | | ! invoiceids . Any ( ) ) throw new ArgumentException ( ) ;
2021-03-19 16:56:26 +00:00
var invoices = _daoFactory . GetInvoiceDao ( ) . DeleteBatchInvoices ( invoiceids . ToArray ( ) ) ;
_messageService . Send ( MessageAction . InvoicesDeleted , _messageTarget . Create ( invoices . Select ( x = > x . ID ) ) , invoices . Select ( x = > x . Number ) ) ;
2021-03-05 20:06:49 +00:00
2021-03-09 15:37:16 +00:00
return ToListInvoiceBaseDtos ( invoices ) ;
2021-03-05 20:06:49 +00:00
}
/// <summary>
/// Creates the invoice with the parameters (contactId, consigneeId, etc.) specified in the request
/// </summary>
/// <param optional="false" name="number">Invoice number</param>
/// <param optional="false" name="issueDate">Invoice issue date</param>
/// <param optional="true" name="templateType">Invoice template type</param>
/// <param optional="false" name="contactId">Invoice contact ID</param>
/// <param optional="true" name="consigneeId">Invoice consignee ID</param>
/// <param optional="true" name="entityId">Invoice entity ID</param>
/// <param optional="true" name="billingAddressID">Invoice billing address ID</param>
/// <param optional="true" name="deliveryAddressID">Invoice delivery address ID</param>
/// <param optional="false" name="dueDate">Invoice due date</param>
/// <param optional="false" name="language">Invoice language</param>
/// <param optional="false" name="currency" remark="Allowed values: EUR, RUB etc. You can get the whole list of available currencies by api">Invoice currency</param>
/// <param optional="false" name="exchangeRate">Invoice exchange rate</param>
/// <param optional="true" name="purchaseOrderNumber">Invoice purchase order number</param>
/// <param optional="false" name="terms">Invoice terms</param>
/// <param optional="true" name="description">Invoice description</param>
/// <param optional="false" name="invoiceLines">Invoice lines list</param>
/// <short>Create invoice</short>
/// <category>Invoices</category>
/// <returns>Invoice</returns>
/// <example>
/// <![CDATA[
///
/// Data transfer in application/json format:
///
/// data: {
/// number: "invoice000001",
/// issueDate: "2015-06-01T00:00:00",
/// contactId: 10,
/// dueDate: "2025-06-01T00:00:00",
/// language: "es-ES",
/// currency: "rub",
/// exchangeRate: 54.32,
/// terms: "Terms for this invoice",
/// invoiceLines:
/// [{
/// invoiceItemID: 1,
/// invoiceTax1ID: 1,
/// invoiceTax2ID: 2,
/// description: "description for invoice line 1",
/// quantity: 100,
/// price: 7.7,
/// discount: 25
/// }]
/// }
///
/// where invoiceItemID, invoiceTax1ID, invoiceTax2ID - ids of the real existing invoice item and invoice taxes,
/// contactId - id of the existing contact
///
/// ]]>
/// </example>
2022-05-31 14:01:58 +00:00
[HttpPost(@"invoice")]
2021-03-09 15:37:16 +00:00
public InvoiceDto CreateInvoice (
2021-03-17 11:50:13 +00:00
CreateOrUpdateInvoiceRequestDto inDto
2021-03-05 20:06:49 +00:00
)
{
string number = inDto . Number ;
ApiDateTime issueDate = inDto . IssueDate ;
int templateType = inDto . TemplateType ;
int contactId = inDto . ContactId ;
int consigneeId = inDto . ConsigneeId ;
int entityId = inDto . EntityId ;
int billingAddressID = inDto . BillingAddressID ;
int deliveryAddressID = inDto . DeliveryAddressID ;
ApiDateTime dueDate = inDto . DueDate ;
string language = inDto . Language ;
string currency = inDto . Currency ;
decimal exchangeRate = inDto . ExchangeRate ;
string purchaseOrderNumber = inDto . PurchaseOrderNumber ;
string terms = inDto . Terms ;
string description = inDto . Description ;
IEnumerable < InvoiceLine > invoiceLines = inDto . InvoiceLines ;
var invoiceLinesList = invoiceLines ! = null ? invoiceLines . ToList ( ) : new List < InvoiceLine > ( ) ;
if ( ! invoiceLinesList . Any ( ) | | ! IsLinesForInvoiceCorrect ( invoiceLinesList ) ) throw new ArgumentException ( ) ;
var invoice = new Invoice
{
Status = InvoiceStatus . Draft ,
Number = number ,
IssueDate = issueDate ,
TemplateType = ( InvoiceTemplateType ) templateType ,
ContactID = contactId ,
ConsigneeID = consigneeId ,
EntityType = EntityType . Opportunity ,
EntityID = entityId ,
DueDate = dueDate ,
Language = language ,
Currency = ! String . IsNullOrEmpty ( currency ) ? currency . ToUpper ( ) : null ,
ExchangeRate = exchangeRate ,
PurchaseOrderNumber = purchaseOrderNumber ,
Terms = terms ,
Description = description
} ;
2021-03-19 16:56:26 +00:00
_crmSecurity . DemandCreateOrUpdate ( invoice ) ;
2021-03-05 20:06:49 +00:00
if ( billingAddressID > 0 )
{
2021-03-19 16:56:26 +00:00
var address = _daoFactory . GetContactInfoDao ( ) . GetByID ( billingAddressID ) ;
2021-03-05 20:06:49 +00:00
if ( address = = null | | address . InfoType ! = ContactInfoType . Address | | address . Category ! = ( int ) AddressCategory . Billing | | address . ContactID ! = contactId )
throw new ArgumentException ( ) ;
}
if ( deliveryAddressID > 0 )
{
2021-03-19 16:56:26 +00:00
var address = _daoFactory . GetContactInfoDao ( ) . GetByID ( deliveryAddressID ) ;
2021-03-05 20:06:49 +00:00
if ( address = = null | | address . InfoType ! = ContactInfoType . Address | | address . Category ! = ( int ) AddressCategory . Postal | | address . ContactID ! = consigneeId )
throw new ArgumentException ( ) ;
}
2021-03-19 16:56:26 +00:00
invoice . ID = _daoFactory . GetInvoiceDao ( ) . SaveOrUpdateInvoice ( invoice ) ;
2021-03-05 20:06:49 +00:00
CreateInvoiceLines ( invoiceLinesList , invoice ) ;
2021-03-19 16:56:26 +00:00
_daoFactory . GetInvoiceDao ( ) . UpdateInvoiceJsonData ( invoice , billingAddressID , deliveryAddressID ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
return _mapper . Map < InvoiceDto > ( invoice ) ;
2021-03-05 20:06:49 +00:00
}
private bool IsLinesForInvoiceCorrect ( List < InvoiceLine > invoiceLines )
{
foreach ( var line in invoiceLines )
{
if ( line . InvoiceItemID < = 0 | |
line . Quantity < 0 | | line . Price < 0 | |
line . Discount < 0 | | line . Discount > 100 | |
line . InvoiceTax1ID < 0 | | line . InvoiceTax2ID < 0 )
return false ;
2021-03-19 16:56:26 +00:00
if ( ! _daoFactory . GetInvoiceItemDao ( ) . IsExist ( line . InvoiceItemID ) )
2021-03-05 20:06:49 +00:00
return false ;
2021-03-19 16:56:26 +00:00
if ( line . InvoiceTax1ID > 0 & & ! _daoFactory . GetInvoiceTaxDao ( ) . IsExist ( line . InvoiceTax1ID ) )
2021-03-05 20:06:49 +00:00
return false ;
2021-03-19 16:56:26 +00:00
if ( line . InvoiceTax2ID > 0 & & ! _daoFactory . GetInvoiceTaxDao ( ) . IsExist ( line . InvoiceTax2ID ) )
2021-03-05 20:06:49 +00:00
return false ;
}
return true ;
}
private List < InvoiceLine > CreateInvoiceLines ( List < InvoiceLine > invoiceLines , Invoice invoice )
{
var result = new List < InvoiceLine > ( ) ;
for ( var i = 0 ; i < invoiceLines . Count ; i + + )
{
var line = new InvoiceLine
{
ID = 0 ,
InvoiceID = invoice . ID ,
InvoiceItemID = invoiceLines [ i ] . InvoiceItemID ,
InvoiceTax1ID = invoiceLines [ i ] . InvoiceTax1ID ,
InvoiceTax2ID = invoiceLines [ i ] . InvoiceTax2ID ,
SortOrder = i ,
Description = invoiceLines [ i ] . Description ,
Quantity = invoiceLines [ i ] . Quantity ,
Price = invoiceLines [ i ] . Price ,
Discount = Convert . ToInt32 ( invoiceLines [ i ] . Discount )
} ;
2021-03-19 16:56:26 +00:00
line . ID = _daoFactory . GetInvoiceLineDao ( ) . SaveOrUpdateInvoiceLine ( line ) ;
2021-03-05 20:06:49 +00:00
result . Add ( line ) ;
}
return result ;
}
/// <summary>
/// Updates the selected invoice with the parameters (contactId, consigneeId, etc.) specified in the request
/// </summary>
/// <param optional="false" name="id">Invoice ID</param>
/// <param optional="false" name="issueDate">Invoice issue date</param>
/// <param optional="true" name="templateType">Invoice template type</param>
/// <param optional="false" name="contactId">Invoice contact ID</param>
/// <param optional="true" name="consigneeId">Invoice consignee ID</param>
/// <param optional="true" name="entityId">Invoice entity ID</param>
/// <param optional="true" name="billingAddressID">Invoice billing address ID</param>
/// <param optional="true" name="deliveryAddressID">Invoice delivery address ID</param>
/// <param name="dueDate">Invoice due date</param>
/// <param optional="false" name="language">Invoice language</param>
/// <param optional="false" name="currency" remark="Allowed values: EUR, RUB etc. You can get the whole list of available currencies by api">Invoice currency</param>
/// <param optional="false" name="exchangeRate">Invoice exchange rate</param>
/// <param optional="true" name="purchaseOrderNumber">Invoice purchase order number</param>
/// <param optional="false" name="terms">Invoice terms</param>
/// <param optional="true" name="description">Invoice description</param>
/// <param optional="false" name="invoiceLines">Invoice lines list</param>
/// <short>Update invoice</short>
/// <category>Invoices</category>
/// <returns>Invoice</returns>
/// <example>
/// <![CDATA[
///
/// Data transfer in application/json format:
///
/// data: {
/// id: 5,
/// issueDate: "2015-06-01T00:00:00",
/// contactId: 10,
/// dueDate: "2025-06-01T00:00:00",
/// language: "es-ES",
/// currency: "rub",
/// exchangeRate: 54.32,
/// terms: "Terms for this invoice",
/// invoiceLines:
/// [{
/// invoiceItemID: 1,
/// invoiceTax1ID: 1,
/// invoiceTax2ID: 2,
/// description: "description for invoice line 1",
/// quantity: 100,
/// price: 7.7,
/// discount: 25
/// }]
/// }
///
/// where invoiceItemID, invoiceTax1ID, invoiceTax2ID - ids of the real existing invoice item and invoice taxes,
/// contactId - id of the existing contact
///
/// ]]>
/// </example>
2022-05-31 14:01:58 +00:00
[HttpPut(@"invoice/{id:int}")]
2021-03-09 15:37:16 +00:00
public InvoiceDto UpdateInvoice (
2021-03-05 20:06:49 +00:00
int id ,
2021-03-17 11:50:13 +00:00
CreateOrUpdateInvoiceRequestDto inDto )
2021-03-19 16:56:26 +00:00
2021-03-05 20:06:49 +00:00
{
ApiDateTime issueDate = inDto . IssueDate ;
int templateType = inDto . TemplateType ;
int contactId = inDto . ContactId ;
int consigneeId = inDto . ConsigneeId ;
int entityId = inDto . EntityId ;
int billingAddressID = inDto . BillingAddressID ;
int deliveryAddressID = inDto . DeliveryAddressID ;
ApiDateTime dueDate = inDto . DueDate ;
string language = inDto . Language ;
string currency = inDto . Currency ;
decimal exchangeRate = inDto . ExchangeRate ;
string purchaseOrderNumber = inDto . PurchaseOrderNumber ;
string terms = inDto . Terms ;
string description = inDto . Description ;
IEnumerable < InvoiceLine > invoiceLines = inDto . InvoiceLines ;
var invoiceLinesList = invoiceLines ! = null ? invoiceLines . ToList ( ) : new List < InvoiceLine > ( ) ;
if ( ! invoiceLinesList . Any ( ) | | ! IsLinesForInvoiceCorrect ( invoiceLinesList ) ) throw new ArgumentException ( ) ;
2021-03-19 16:56:26 +00:00
var invoice = _daoFactory . GetInvoiceDao ( ) . GetByID ( id ) ;
if ( invoice = = null | | ! _crmSecurity . CanEdit ( invoice ) ) throw new ItemNotFoundException ( ) ;
2021-03-05 20:06:49 +00:00
invoice . IssueDate = issueDate ;
invoice . TemplateType = ( InvoiceTemplateType ) templateType ;
invoice . ContactID = contactId ;
invoice . ConsigneeID = consigneeId ;
invoice . EntityType = EntityType . Opportunity ;
invoice . EntityID = entityId ;
invoice . DueDate = dueDate ;
invoice . Language = language ;
invoice . Currency = ! String . IsNullOrEmpty ( currency ) ? currency . ToUpper ( ) : null ; ;
invoice . ExchangeRate = exchangeRate ;
invoice . PurchaseOrderNumber = purchaseOrderNumber ;
invoice . Terms = terms ;
invoice . Description = description ;
invoice . JsonData = null ;
2021-03-19 16:56:26 +00:00
_crmSecurity . DemandCreateOrUpdate ( invoice ) ;
2021-03-05 20:06:49 +00:00
if ( billingAddressID > 0 )
{
2021-03-19 16:56:26 +00:00
var address = _daoFactory . GetContactInfoDao ( ) . GetByID ( billingAddressID ) ;
2021-03-05 20:06:49 +00:00
if ( address = = null | | address . InfoType ! = ContactInfoType . Address | | address . Category ! = ( int ) AddressCategory . Billing | | address . ContactID ! = contactId )
throw new ArgumentException ( ) ;
}
if ( deliveryAddressID > 0 )
{
2021-03-19 16:56:26 +00:00
var address = _daoFactory . GetContactInfoDao ( ) . GetByID ( deliveryAddressID ) ;
2021-03-05 20:06:49 +00:00
if ( address = = null | | address . InfoType ! = ContactInfoType . Address | | address . Category ! = ( int ) AddressCategory . Postal | | address . ContactID ! = consigneeId )
throw new ArgumentException ( ) ;
}
2021-03-19 16:56:26 +00:00
_daoFactory . GetInvoiceDao ( ) . SaveOrUpdateInvoice ( invoice ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
_daoFactory . GetInvoiceLineDao ( ) . DeleteInvoiceLines ( invoice . ID ) ;
2021-03-05 20:06:49 +00:00
CreateInvoiceLines ( invoiceLinesList , invoice ) ;
2021-03-19 16:56:26 +00:00
_daoFactory . GetInvoiceDao ( ) . UpdateInvoiceJsonData ( invoice , billingAddressID , deliveryAddressID ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
if ( _global . CanDownloadInvoices )
2021-03-05 20:06:49 +00:00
{
2021-03-19 16:56:26 +00:00
_pdfQueueWorker . StartTask ( invoice . ID ) ;
2021-03-05 20:06:49 +00:00
}
2021-03-19 16:56:26 +00:00
return _mapper . Map < InvoiceDto > ( invoice ) ;
2021-03-05 20:06:49 +00:00
}
/// <summary>
/// Returns the pdf file associated with the invoice with the ID specified in the request
/// </summary>
/// <param name="invoiceid">Invoice ID</param>
/// <short>Get invoice pdf file</short>
/// <category>Invoices</category>
/// <returns>File</returns>
2022-05-31 14:01:58 +00:00
[HttpGet(@"invoice/{invoiceid:int}/pdf")]
2022-05-19 15:47:59 +00:00
public Task < FileDto < int > > GetInvoicePdfExistOrCreateAsync ( int invoiceid )
2021-03-05 20:06:49 +00:00
{
if ( invoiceid < = 0 ) throw new ArgumentException ( ) ;
2021-03-19 16:56:26 +00:00
var invoice = _daoFactory . GetInvoiceDao ( ) . GetByID ( invoiceid ) ;
2021-03-05 20:06:49 +00:00
if ( invoice = = null ) throw new ItemNotFoundException ( ) ;
2021-03-19 16:56:26 +00:00
if ( ! _crmSecurity . CanAccessTo ( invoice ) )
2021-03-05 20:06:49 +00:00
{
2021-03-19 16:56:26 +00:00
throw _crmSecurity . CreateSecurityException ( ) ;
2021-03-05 20:06:49 +00:00
}
2022-02-16 12:57:37 +00:00
return internalGetInvoicePdfExistOrCreateAsync ( invoice ) ;
}
2022-05-19 15:47:59 +00:00
private async Task < FileDto < int > > internalGetInvoicePdfExistOrCreateAsync ( Invoice invoice )
2022-02-16 12:57:37 +00:00
{
2022-05-19 15:47:59 +00:00
return await _fileDtoHelper . GetAsync ( await GetInvoicePdfExistingOrCreateAsync ( invoice ) ) ;
2021-03-05 20:06:49 +00:00
}
2022-02-10 18:25:40 +00:00
private async Task < ASC . Files . Core . File < int > > GetInvoicePdfExistingOrCreateAsync ( ASC . CRM . Core . Entities . Invoice invoice )
2021-03-05 20:06:49 +00:00
{
2021-03-19 16:56:26 +00:00
var existingFile = invoice . GetInvoiceFile ( _daoFactory ) ;
2021-03-05 20:06:49 +00:00
if ( existingFile ! = null )
{
return existingFile ;
}
else
{
2022-02-10 18:25:40 +00:00
var newFile = await _pdfCreator . CreateFileAsync ( invoice , _daoFactory ) ;
2021-03-05 20:06:49 +00:00
2022-05-19 15:47:59 +00:00
invoice . FileID = Int32 . Parse ( newFile . Id . ToString ( ) ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
_daoFactory . GetInvoiceDao ( ) . UpdateInvoiceFileID ( invoice . ID , invoice . FileID ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
_daoFactory . GetRelationshipEventDao ( ) . AttachFiles ( invoice . ContactID , invoice . EntityType , invoice . EntityID , new [ ] { invoice . FileID } ) ;
2021-03-05 20:06:49 +00:00
return newFile ;
}
}
/// <summary>
/// Returns information about the generation of the pdf file of the invoice
/// </summary>
/// <param name="invoiceId">Invoice ID</param>
/// <param name="storageUrl">Storage Url</param>
/// <param name="revisionId">Revision ID</param>
/// <short>Check invoice pdf file</short>
/// <category>Invoices</category>
/// <returns>ConverterData</returns>
2022-05-31 14:01:58 +00:00
[HttpPost(@"invoice/converter/data")]
2022-02-16 12:57:37 +00:00
public Task < ConverterData > CreateInvoiceConverterDataAsync (
2021-06-28 09:42:42 +00:00
[FromBody] CreateInvoiceConverterDataRequestDto inDto )
2021-03-05 20:06:49 +00:00
{
2021-06-28 09:42:42 +00:00
var invoiceId = inDto . InvoiceId ;
var storageUrl = inDto . StorageUrl ;
var revisionId = inDto . RevisionId ;
2021-03-05 20:06:49 +00:00
if ( invoiceId < = 0 ) throw new ArgumentException ( ) ;
2021-03-19 16:56:26 +00:00
var invoice = _daoFactory . GetInvoiceDao ( ) . GetByID ( invoiceId ) ;
2021-03-05 20:06:49 +00:00
if ( invoice = = null ) throw new ItemNotFoundException ( ) ;
2021-03-19 16:56:26 +00:00
if ( ! _crmSecurity . CanAccessTo ( invoice ) )
2021-03-05 20:06:49 +00:00
{
2021-03-19 16:56:26 +00:00
throw _crmSecurity . CreateSecurityException ( ) ;
2021-03-05 20:06:49 +00:00
}
var converterData = new ConverterData
{
StorageUrl = storageUrl ,
RevisionId = revisionId ,
InvoiceId = invoiceId
} ;
2021-03-19 16:56:26 +00:00
var existingFile = invoice . GetInvoiceFile ( _daoFactory ) ;
2021-03-05 20:06:49 +00:00
if ( existingFile ! = null )
{
converterData . FileId = invoice . FileID ;
2022-02-16 12:57:37 +00:00
return System . Threading . Tasks . Task . FromResult ( converterData ) ;
2021-03-05 20:06:49 +00:00
}
2022-02-16 12:57:37 +00:00
return InternalCreateInvoiceConverterDataAsync ( converterData , invoice ) ;
}
private async Task < ConverterData > InternalCreateInvoiceConverterDataAsync ( ConverterData converterData , Invoice invoice )
{
var storageUrl = converterData . StorageUrl ;
var revisionId = converterData . RevisionId ;
2021-03-05 20:06:49 +00:00
if ( string . IsNullOrEmpty ( storageUrl ) | | string . IsNullOrEmpty ( revisionId ) )
{
2022-02-10 18:25:40 +00:00
return await _pdfCreator . StartCreationFileAsync ( invoice ) ;
2021-03-05 20:06:49 +00:00
}
else
{
2022-02-10 18:25:40 +00:00
var convertedFile = await _pdfCreator . GetConvertedFileAsync ( converterData , _daoFactory ) ;
2021-03-05 20:06:49 +00:00
if ( convertedFile ! = null )
{
2022-05-19 15:47:59 +00:00
invoice . FileID = Int32 . Parse ( convertedFile . Id . ToString ( ) ) ;
2021-03-19 16:56:26 +00:00
_daoFactory . GetInvoiceDao ( ) . UpdateInvoiceFileID ( invoice . ID , invoice . FileID ) ;
_daoFactory . GetRelationshipEventDao ( ) . AttachFiles ( invoice . ContactID , invoice . EntityType , invoice . EntityID , new [ ] { invoice . FileID } ) ;
2021-03-05 20:06:49 +00:00
converterData . FileId = invoice . FileID ;
return converterData ;
}
else
{
return converterData ;
}
}
}
/// <summary>
/// Returns the existence of the invoice with the Number specified in the request
/// </summary>
/// <param name="number">Invoice number</param>
/// <short>Check invoice existence by number</short>
/// <category>Invoices</category>
/// <returns>IsExist</returns>
2022-05-31 14:01:58 +00:00
[HttpGet(@"invoice/bynumber/exist")]
2021-03-05 20:06:49 +00:00
public Boolean GetInvoiceByNumberExistence ( string number )
{
if ( String . IsNullOrEmpty ( number ) ) throw new ArgumentException ( ) ;
2021-03-19 16:56:26 +00:00
return _daoFactory . GetInvoiceDao ( ) . IsExist ( number ) ;
2021-03-05 20:06:49 +00:00
}
/// <summary>
/// Returns the detailed information about the invoice with the Number specified in the request
/// </summary>
/// <param name="number">Invoice number</param>
/// <short>Get invoice by number</short>
/// <category>Invoices</category>
/// <returns>Invoice</returns>
2022-05-31 14:01:58 +00:00
[HttpGet(@"invoice/bynumber")]
2021-03-09 15:37:16 +00:00
public InvoiceDto GetInvoiceByNumber ( string number )
2021-03-05 20:06:49 +00:00
{
if ( String . IsNullOrEmpty ( number ) ) throw new ArgumentException ( ) ;
2021-03-19 16:56:26 +00:00
var invoice = _daoFactory . GetInvoiceDao ( ) . GetByNumber ( number ) ;
2021-03-05 20:06:49 +00:00
if ( invoice = = null ) throw new ItemNotFoundException ( ) ;
2021-03-19 16:56:26 +00:00
if ( ! _crmSecurity . CanAccessTo ( invoice ) )
2021-03-05 20:06:49 +00:00
{
2021-03-19 16:56:26 +00:00
throw _crmSecurity . CreateSecurityException ( ) ;
2021-03-05 20:06:49 +00:00
}
2021-03-19 16:56:26 +00:00
return _mapper . Map < InvoiceDto > ( invoice ) ;
2021-03-05 20:06:49 +00:00
}
/// <summary>
/// Returns the list of invoice items matching the creteria specified in the request
/// </summary>
/// <param name="status">Status</param>
/// <param optional="true" name="inventoryStock">InventoryStock</param>
/// <short>Get invoice item list</short>
/// <category>Invoices</category>
/// <returns>InvoiceItem list</returns>
2022-05-31 14:01:58 +00:00
[HttpGet(@"invoiceitem/filter")]
2021-03-09 15:37:16 +00:00
public IEnumerable < InvoiceItemDto > GetInvoiceItems ( int status , bool? inventoryStock )
2021-03-05 20:06:49 +00:00
{
2021-03-09 15:37:16 +00:00
IEnumerable < InvoiceItemDto > result ;
2021-03-05 20:06:49 +00:00
InvoiceItemSortedByType sortBy ;
OrderBy invoiceOrderBy ;
2021-03-19 16:56:26 +00:00
var searchString = _apiContext . FilterValue ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
if ( InvoiceItemSortedByType . TryParse ( _apiContext . SortBy , true , out sortBy ) )
2021-03-05 20:06:49 +00:00
{
2021-03-19 16:56:26 +00:00
invoiceOrderBy = new OrderBy ( sortBy , ! _apiContext . SortDescending ) ;
2021-03-05 20:06:49 +00:00
}
2021-03-19 16:56:26 +00:00
else if ( String . IsNullOrEmpty ( _apiContext . SortBy ) )
2021-03-05 20:06:49 +00:00
{
invoiceOrderBy = new OrderBy ( InvoiceItemSortedByType . Name , true ) ;
}
else
{
invoiceOrderBy = null ;
}
2021-03-19 16:56:26 +00:00
var fromIndex = ( int ) _apiContext . StartIndex ;
var count = ( int ) _apiContext . Count ;
2021-03-05 20:06:49 +00:00
if ( invoiceOrderBy ! = null )
{
2021-03-19 16:56:26 +00:00
var resultFromDao = _daoFactory . GetInvoiceItemDao ( ) . GetInvoiceItems (
2021-03-05 20:06:49 +00:00
searchString ,
status ,
inventoryStock ,
fromIndex , count ,
2021-03-17 11:50:13 +00:00
invoiceOrderBy ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
result = _mapper . Map < List < InvoiceItem > , List < InvoiceItemDto > > ( resultFromDao ) ;
_apiContext . SetDataPaginated ( ) ;
_apiContext . SetDataFiltered ( ) ;
_apiContext . SetDataSorted ( ) ;
2021-03-05 20:06:49 +00:00
}
else
{
2021-03-19 16:56:26 +00:00
var resultFromDao = _daoFactory . GetInvoiceItemDao ( ) . GetInvoiceItems (
searchString ,
status ,
inventoryStock ,
0 , 0 ,
null ) ;
2021-03-17 11:50:13 +00:00
result = _mapper . Map < List < InvoiceItem > , List < InvoiceItemDto > > ( resultFromDao ) ;
2021-03-05 20:06:49 +00:00
}
int totalCount ;
if ( result . Count ( ) < count )
{
totalCount = fromIndex + result . Count ( ) ;
}
else
{
2021-03-19 16:56:26 +00:00
totalCount = _daoFactory . GetInvoiceItemDao ( ) . GetInvoiceItemsCount (
2021-03-05 20:06:49 +00:00
searchString ,
status ,
inventoryStock ) ;
}
2021-03-19 16:56:26 +00:00
_apiContext . SetTotalCount ( totalCount ) ;
2021-03-05 20:06:49 +00:00
return result ;
}
/// <summary>
/// Returns the detailed information about the invoice item with the ID specified in the request
/// </summary>
/// <param name="invoiceitemid">Invoice Item ID</param>
/// <short>Get invoice item by ID</short>
/// <category>Invoices</category>
/// <returns>Invoice Item</returns>
2022-05-31 14:01:58 +00:00
[HttpGet(@"invoiceitem/{invoiceitemid:int}")]
2021-03-09 15:37:16 +00:00
public InvoiceItemDto GetInvoiceItemByID ( int invoiceitemid )
2021-03-05 20:06:49 +00:00
{
if ( invoiceitemid < = 0 ) throw new ArgumentException ( ) ;
2021-03-19 16:56:26 +00:00
var invoiceItem = _daoFactory . GetInvoiceItemDao ( ) . GetByID ( invoiceitemid ) ;
2021-03-05 20:06:49 +00:00
if ( invoiceItem = = null ) throw new ItemNotFoundException ( ) ;
2021-03-17 11:50:13 +00:00
return _mapper . Map < InvoiceItemDto > ( invoiceItem ) ;
2021-03-05 20:06:49 +00:00
}
/// <summary>
/// Creates the invoice line with the parameters (invoiceId, invoiceItemId, etc.) specified in the request
/// </summary>
/// <param optional="false" name="invoiceId">Invoice ID</param>
/// <param optional="false" name="invoiceItemId">Invoice item ID</param>
/// <param optional="true" name="invoiceTax1Id">First invoice tax ID</param>
/// <param optional="true" name="invoiceTax2Id">Second invoice tax ID</param>
/// <param optional="true" name="sortOrder">Sort Order</param>
/// <param optional="true" name="description">Description</param>
/// <param optional="true" name="quantity">Quantity</param>
/// <param optional="true" name="price">Price</param>
/// <param optional="true" name="discount">Discount</param>
/// <short>Create invoice line</short>
/// <category>Invoices</category>
/// <returns>InvoiceLine</returns>
2022-05-31 14:01:58 +00:00
[HttpPost(@"invoiceline")]
2021-03-09 15:37:16 +00:00
public InvoiceLineDto CreateInvoiceLine (
2021-03-17 11:50:13 +00:00
CreateOrUpdateInvoiceLineRequestDto inDto
2021-03-05 20:06:49 +00:00
)
{
int invoiceId = inDto . InvoiceId ;
int invoiceItemId = inDto . InvoiceItemId ;
int invoiceTax1Id = inDto . InvoiceTax1Id ;
int invoiceTax2Id = inDto . InvoiceTax2Id ;
int sortOrder = inDto . SortOrder ;
string description = inDto . Description ;
int quantity = inDto . Quantity ;
decimal price = inDto . Price ;
int discount = inDto . Discount ;
var invoiceLine = new InvoiceLine
{
InvoiceID = invoiceId ,
InvoiceItemID = invoiceItemId ,
InvoiceTax1ID = invoiceTax1Id ,
InvoiceTax2ID = invoiceTax2Id ,
SortOrder = sortOrder ,
Description = description ,
Quantity = quantity ,
Price = price ,
Discount = discount
} ;
if ( invoiceId < = 0 )
throw new ArgumentException ( ) ;
2021-03-19 16:56:26 +00:00
var invoice = _daoFactory . GetInvoiceDao ( ) . GetByID ( invoiceId ) ;
_crmSecurity . DemandCreateOrUpdate ( invoiceLine , invoice ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
invoiceLine . ID = _daoFactory . GetInvoiceLineDao ( ) . SaveOrUpdateInvoiceLine ( invoiceLine ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
_daoFactory . GetInvoiceDao ( ) . UpdateInvoiceJsonDataAfterLinesUpdated ( invoice ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
if ( _global . CanDownloadInvoices )
2021-03-05 20:06:49 +00:00
{
2021-03-19 16:56:26 +00:00
_pdfQueueWorker . StartTask ( invoice . ID ) ;
2021-03-05 20:06:49 +00:00
}
2021-03-19 16:56:26 +00:00
return _mapper . Map < InvoiceLineDto > ( invoiceLine ) ;
2021-03-05 20:06:49 +00:00
}
/// <summary>
/// Updates the selected invoice line with the parameters (invoiceId, invoiceItemId, etc.) specified in the request
/// </summary>
/// <param optional="false" name="id">Line ID</param>
/// <param optional="false" name="invoiceId">Invoice ID</param>
/// <param optional="false" name="invoiceItemId">Invoice item ID</param>
/// <param optional="true" name="invoiceTax1Id">First invoice tax ID</param>
/// <param optional="true" name="invoiceTax2Id">Second invoice tax ID</param>
/// <param optional="true" name="sortOrder">Sort Order</param>
/// <param optional="true" name="description">Description</param>
/// <param optional="true" name="quantity">Quantity</param>
/// <param optional="true" name="price">Price</param>
/// <param optional="true" name="discount">Discount</param>
/// <short>Update invoice line</short>
/// <category>Invoices</category>
/// <returns>InvoiceLine</returns>
2022-05-31 14:01:58 +00:00
[HttpPut(@"invoiceline/{id:int}")]
2021-03-19 16:56:26 +00:00
public InvoiceLineDto UpdateInvoiceLine ( int id , CreateOrUpdateInvoiceLineRequestDto inDto )
2021-03-05 20:06:49 +00:00
{
int invoiceId = inDto . InvoiceId ;
int invoiceItemId = inDto . InvoiceItemId ;
int invoiceTax1Id = inDto . InvoiceTax1Id ;
int invoiceTax2Id = inDto . InvoiceTax2Id ;
int sortOrder = inDto . SortOrder ;
string description = inDto . Description ;
int quantity = inDto . Quantity ;
decimal price = inDto . Price ;
int discount = inDto . Discount ;
if ( invoiceId < = 0 )
throw new ArgumentException ( ) ;
2021-03-19 16:56:26 +00:00
var invoiceLine = _daoFactory . GetInvoiceLineDao ( ) . GetByID ( id ) ;
2021-03-05 20:06:49 +00:00
if ( invoiceLine = = null | | invoiceLine . InvoiceID ! = invoiceId ) throw new ItemNotFoundException ( ) ;
invoiceLine . InvoiceID = invoiceId ;
invoiceLine . InvoiceItemID = invoiceItemId ;
invoiceLine . InvoiceTax1ID = invoiceTax1Id ;
invoiceLine . InvoiceTax2ID = invoiceTax2Id ;
invoiceLine . SortOrder = sortOrder ;
invoiceLine . Description = description ;
invoiceLine . Quantity = quantity ;
invoiceLine . Price = price ;
invoiceLine . Discount = discount ;
2021-03-19 16:56:26 +00:00
var invoice = _daoFactory . GetInvoiceDao ( ) . GetByID ( invoiceId ) ;
_crmSecurity . DemandCreateOrUpdate ( invoiceLine , invoice ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
_daoFactory . GetInvoiceLineDao ( ) . SaveOrUpdateInvoiceLine ( invoiceLine ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
_daoFactory . GetInvoiceDao ( ) . UpdateInvoiceJsonDataAfterLinesUpdated ( invoice ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
if ( _global . CanDownloadInvoices )
2021-03-05 20:06:49 +00:00
{
2021-03-19 16:56:26 +00:00
_pdfQueueWorker . StartTask ( invoice . ID ) ;
2021-03-05 20:06:49 +00:00
}
2021-03-19 16:56:26 +00:00
return _mapper . Map < InvoiceLineDto > ( invoiceLine ) ;
2021-03-05 20:06:49 +00:00
}
/// <summary>
/// Deletes the invoice line with the ID specified in the request
/// </summary>
/// <param optional="false" name="id">Line ID</param>
/// <short>Delete invoice line</short>
/// <category>Invoices</category>
/// <returns>Line ID</returns>
2022-05-31 14:01:58 +00:00
[HttpDelete(@"invoiceline/{id:int}")]
2021-03-05 20:06:49 +00:00
public int DeleteInvoiceLine ( int id )
{
2021-03-19 16:56:26 +00:00
var invoiceLine = _daoFactory . GetInvoiceLineDao ( ) . GetByID ( id ) ;
2021-03-05 20:06:49 +00:00
if ( invoiceLine = = null ) throw new ItemNotFoundException ( ) ;
2021-03-19 16:56:26 +00:00
if ( ! _daoFactory . GetInvoiceLineDao ( ) . CanDelete ( invoiceLine . ID ) ) throw new Exception ( "Can't delete invoice line" ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
var invoice = _daoFactory . GetInvoiceDao ( ) . GetByID ( invoiceLine . InvoiceID ) ;
2021-03-05 20:06:49 +00:00
if ( invoice = = null ) throw new ItemNotFoundException ( ) ;
2021-03-19 16:56:26 +00:00
if ( ! _crmSecurity . CanEdit ( invoice ) ) throw _crmSecurity . CreateSecurityException ( ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
_daoFactory . GetInvoiceLineDao ( ) . DeleteInvoiceLine ( id ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
_daoFactory . GetInvoiceDao ( ) . UpdateInvoiceJsonDataAfterLinesUpdated ( invoice ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
if ( _global . CanDownloadInvoices )
2021-03-05 20:06:49 +00:00
{
2021-03-19 16:56:26 +00:00
_pdfQueueWorker . StartTask ( invoice . ID ) ;
2021-03-05 20:06:49 +00:00
}
return id ;
}
/// <summary>
/// Creates the invoice item with the parameters (title, description, price, etc.) specified in the request
/// </summary>
/// <param optional="false" name="title">Item title</param>
/// <param optional="true" name="description">Item description</param>
/// <param optional="false" name="price">Item price</param>
/// <param optional="true" name="sku">Item stock keeping unit</param>
/// <param optional="true" name="quantity">Item quantity</param>
/// <param optional="true" name="stockQuantity">Item stock quantity</param>
/// <param optional="true" name="trackInventory">Track inventory</param>
/// <param optional="true" name="invoiceTax1id">Item first invoice tax ID</param>
/// <param optional="true" name="invoiceTax2id">Item second invoice tax ID</param>
/// <short>Create invoice item</short>
/// <category>Invoices</category>
/// <returns>InvoiceItem</returns>
2022-05-31 14:01:58 +00:00
[HttpPost(@"invoiceitem")]
2021-03-09 15:37:16 +00:00
public InvoiceItemDto CreateInvoiceItem (
2021-03-17 11:50:13 +00:00
CreateOrUpdateInvoiceItemRequestDto inDto
2021-03-05 20:06:49 +00:00
)
{
string title = inDto . Title ;
string description = inDto . Description ;
decimal price = inDto . Price ;
string sku = inDto . Sku ;
int quantity = inDto . Quantity ;
int stockQuantity = inDto . StockQuantity ;
bool trackInventory = inDto . TrackInventory ;
int invoiceTax1id = inDto . InvoiceTax1id ;
int invoiceTax2id = inDto . InvoiceTax2id ;
2021-03-19 16:56:26 +00:00
if ( ! _crmSecurity . IsAdmin )
2021-03-05 20:06:49 +00:00
{
2021-03-19 16:56:26 +00:00
throw _crmSecurity . CreateSecurityException ( ) ;
2021-03-05 20:06:49 +00:00
}
if ( String . IsNullOrEmpty ( title ) | | price < = 0 ) throw new ArgumentException ( ) ;
var invoiceItem = new InvoiceItem
{
Title = title ,
Description = description ,
Price = price ,
StockKeepingUnit = sku ,
StockQuantity = stockQuantity ,
TrackInventory = trackInventory ,
InvoiceTax1ID = invoiceTax1id ,
InvoiceTax2ID = invoiceTax2id
} ;
2021-03-19 16:56:26 +00:00
invoiceItem = _daoFactory . GetInvoiceItemDao ( ) . SaveOrUpdateInvoiceItem ( invoiceItem ) ;
2021-03-05 20:06:49 +00:00
2021-03-19 16:56:26 +00:00
_messageService . Send ( MessageAction . InvoiceItemCreated , _messageTarget . Create ( invoiceItem . ID ) , invoiceItem . Title ) ;
2021-03-05 20:06:49 +00:00
2021-03-17 11:50:13 +00:00
return _mapper . Map < InvoiceItemDto > ( invoiceItem ) ;
2021-03-05 20:06:49 +00:00
}
/// <summary>
/// Updates the selected invoice item with the parameters (title, description, price, etc.) specified in the request
/// </summary>
/// <param optional="false" name="id">Item ID</param>
/// <param optional="false" name="title">Item title</param>
/// <param optional="true" name="description">Item description</param>
/// <param optional="false" name="price">Item price</param>
/// <param optional="true" name="sku">Item stock keeping unit</param>
/// <param optional="true" name="quantity">Item quantity</param>
/// <param optional="true" name="stockQuantity">Item stock quantity</param>
/// <param optional="true" name="trackInventory">Track inventory</param>
/// <param optional="true" name="invoiceTax1id">Item first invoice tax ID</param>
/// <param optional="true" name="invoiceTax2id">Item second invoice tax ID</param>
/// <short>Update invoice item</short>
/// <category>Invoices</category>
/// <returns>InvoiceItem</returns>
2022-05-31 14:01:58 +00:00
[HttpPut(@"invoiceitem/{id:int}")]
2021-03-09 15:37:16 +00:00
public InvoiceItemDto UpdateInvoiceItem ( int id ,
2021-03-17 11:50:13 +00:00
CreateOrUpdateInvoiceItemRequestDto inDto
2021-03-05 20:06:49 +00:00
)
{
string title = inDto . Title ;
string description = inDto . Description ;
decimal price = inDto . Price ;
string sku = inDto . Sku ;
int quantity = inDto . Quantity ;
int stockQuantity = inDto . StockQuantity ;
bool trackInventory = inDto . TrackInventory ;
int invoiceTax1id = inDto . InvoiceTax1id ;
int invoiceTax2id = inDto . InvoiceTax2id ;
2021-03-19 16:56:26 +00:00
if ( ! _crmSecurity . IsAdmin )
2021-03-05 20:06:49 +00:00
{
2021-03-19 16:56:26 +00:00
throw _crmSecurity . CreateSecurityException ( ) ;
2021-03-05 20:06:49 +00:00
}
if ( id < = 0 | | String . IsNullOrEmpty ( title ) | | price < = 0 ) throw new ArgumentException ( ) ;
2021-03-19 16:56:26 +00:00
if ( ! _daoFactory . GetInvoiceItemDao ( ) . IsExist ( id ) ) throw new ItemNotFoundException ( ) ;
2021-03-05 20:06:49 +00:00
var invoiceItem = new InvoiceItem
{
ID = id ,
Title = title ,
Description = description ,
Price = price ,
StockKeepingUnit = sku ,
StockQuantity = stockQuantity ,
TrackInventory = trackInventory ,
InvoiceTax1ID = invoiceTax1id ,
InvoiceTax2ID = invoiceTax2id
} ;
2021-03-19 16:56:26 +00:00
invoiceItem = _daoFactory . GetInvoiceItemDao ( ) . SaveOrUpdateInvoiceItem ( invoiceItem ) ;
_messageService . Send ( MessageAction . InvoiceItemUpdated , _messageTarget . Create ( invoiceItem . ID ) , invoiceItem . Title ) ;
2021-03-05 20:06:49 +00:00
2021-03-17 11:50:13 +00:00
return _mapper . Map < InvoiceItemDto > ( invoiceItem ) ;
2021-03-05 20:06:49 +00:00
}
/// <summary>
/// Deletes the invoice item with the ID specified in the request
/// </summary>
/// <param name="id">Item ID</param>
/// <short>Delete invoice item</short>
/// <category>Invoices</category>
/// <returns>InvoiceItem</returns>
2022-05-31 14:01:58 +00:00
[HttpDelete(@"invoiceitem/{id:int}")]
2021-03-09 15:37:16 +00:00
public InvoiceItemDto DeleteInvoiceItem ( int id )
2021-03-05 20:06:49 +00:00
{
2021-03-19 16:56:26 +00:00
if ( ! _crmSecurity . IsAdmin )
2021-03-05 20:06:49 +00:00
{
2021-03-19 16:56:26 +00:00
throw _crmSecurity . CreateSecurityException ( ) ;
2021-03-05 20:06:49 +00:00
}
if ( id < = 0 ) throw new ArgumentException ( ) ;
2021-03-19 16:56:26 +00:00
var invoiceItem = _daoFactory . GetInvoiceItemDao ( ) . DeleteInvoiceItem ( id ) ;
2021-03-05 20:06:49 +00:00
if ( invoiceItem = = null ) throw new ItemNotFoundException ( ) ;
2021-03-19 16:56:26 +00:00
_messageService . Send ( MessageAction . InvoiceItemDeleted , _messageTarget . Create ( invoiceItem . ID ) , invoiceItem . Title ) ;
2021-03-05 20:06:49 +00:00
2021-03-17 11:50:13 +00:00
return _mapper . Map < InvoiceItemDto > ( invoiceItem ) ;
2021-03-05 20:06:49 +00:00
}
/// <summary>
/// Deletes the group of invoice items with the IDs specified in the request
/// </summary>
/// <param name="ids">Item ID list</param>
/// <short>Delete Invoice item group</short>
/// <category>Invoices</category>
/// <returns>InvoiceItem list</returns>
2022-05-31 14:01:58 +00:00
[HttpDelete(@"invoiceitem")]
2021-03-09 15:37:16 +00:00
public IEnumerable < InvoiceItemDto > DeleteBatchItems ( IEnumerable < int > ids )
2021-03-05 20:06:49 +00:00
{
2021-03-19 16:56:26 +00:00
if ( ! _crmSecurity . IsAdmin )
2021-03-05 20:06:49 +00:00
{
2021-03-19 16:56:26 +00:00
throw _crmSecurity . CreateSecurityException ( ) ;
2021-03-05 20:06:49 +00:00
}
if ( ids = = null ) throw new ArgumentException ( ) ;
ids = ids . Distinct ( ) ;
2021-03-19 16:56:26 +00:00
var items = _daoFactory . GetInvoiceItemDao ( ) . DeleteBatchInvoiceItems ( ids . ToArray ( ) ) ;
_messageService . Send ( MessageAction . InvoiceItemsDeleted , _messageTarget . Create ( ids ) , items . Select ( x = > x . Title ) ) ;
2021-03-05 20:06:49 +00:00
2021-03-17 11:50:13 +00:00
return _mapper . Map < List < InvoiceItem > , List < InvoiceItemDto > > ( items ) ;
2021-03-05 20:06:49 +00:00
}
/// <summary>
/// Returns the list of invoice taxes
/// </summary>
/// <short>Get invoice taxes list</short>
/// <category>Invoices</category>
/// <returns>InvoiceTax list</returns>
2022-05-31 14:01:58 +00:00
[HttpGet(@"invoice/tax")]
2021-03-09 15:37:16 +00:00
public IEnumerable < InvoiceTaxDto > GetInvoiceTaxes ( )
2021-03-05 20:06:49 +00:00
{
2021-03-19 16:56:26 +00:00
var responceFromDao = _daoFactory . GetInvoiceTaxDao ( ) . GetAll ( ) ;
2021-03-17 11:50:13 +00:00
return _mapper . Map < List < InvoiceTax > , List < InvoiceTaxDto > > ( responceFromDao ) ;
2021-03-05 20:06:49 +00:00
}
/// <summary>
/// Creates the invoice tax with the parameters (name, description, rate) specified in the request
/// </summary>
/// <param name="name">Tax name</param>
/// <param name="description">Tax description</param>
/// <param name="rate">Tax rate</param>
/// <short>Create invoice tax</short>
/// <category>Invoices</category>
/// <returns>InvoiceTax</returns>
2022-05-31 14:01:58 +00:00
[HttpPost(@"invoice/tax")]
2021-03-09 15:37:16 +00:00
public InvoiceTaxDto CreateInvoiceTax (
2021-03-17 11:50:13 +00:00
[FromBody] CreateOrUpdateInvoiceTaxRequestDto inDto )
2021-03-05 20:06:49 +00:00
{
string name = inDto . Name ;
string description = inDto . Description ;
decimal rate = inDto . Rate ;
2021-03-19 16:56:26 +00:00
if ( ! _crmSecurity . IsAdmin )
2021-03-05 20:06:49 +00:00
{
2021-03-19 16:56:26 +00:00
throw _crmSecurity . CreateSecurityException ( ) ;
2021-03-05 20:06:49 +00:00
}
if ( String . IsNullOrEmpty ( name ) ) throw new ArgumentException ( CRMInvoiceResource . EmptyTaxNameError ) ;
2021-03-19 16:56:26 +00:00
if ( _daoFactory . GetInvoiceTaxDao ( ) . IsExist ( name ) ) throw new ArgumentException ( CRMInvoiceResource . ExistTaxNameError ) ;
2021-03-05 20:06:49 +00:00
var invoiceTax = new InvoiceTax
{
Name = name ,
Description = description ,
Rate = rate
} ;
2021-03-19 16:56:26 +00:00
invoiceTax = _daoFactory . GetInvoiceTaxDao ( ) . SaveOrUpdateInvoiceTax ( invoiceTax ) ;
_messageService . Send ( MessageAction . InvoiceTaxCreated , _messageTarget . Create ( invoiceTax . ID ) , invoiceTax . Name ) ;
2021-03-05 20:06:49 +00:00
2021-03-17 11:50:13 +00:00
return _mapper . Map < InvoiceTaxDto > ( invoiceTax ) ;
2021-03-05 20:06:49 +00:00
}
/// <summary>
/// Updates the selected invoice tax with the parameters (name, description, rate) specified in the request
/// </summary>
/// <param name="id">Tax ID</param>
/// <param name="name">Tax name</param>
/// <param name="description">Tax description</param>
/// <param name="rate">Tax rate</param>
/// <short>Update invoice tax</short>
/// <category>Invoices</category>
/// <returns>InvoiceTax</returns>
2022-05-31 14:01:58 +00:00
[HttpPut(@"invoice/tax/{id:int}")]
2021-03-09 15:37:16 +00:00
public InvoiceTaxDto UpdateInvoiceTax (
2021-03-05 20:06:49 +00:00
int id ,
2021-03-17 11:50:13 +00:00
CreateOrUpdateInvoiceTaxRequestDto inDto )
2021-03-05 20:06:49 +00:00
{
string name = inDto . Name ;
string description = inDto . Description ;
decimal rate = inDto . Rate ;
2021-03-19 16:56:26 +00:00
if ( ! _crmSecurity . IsAdmin )
2021-03-05 20:06:49 +00:00
{
2021-03-19 16:56:26 +00:00
throw _crmSecurity . CreateSecurityException ( ) ;
2021-03-05 20:06:49 +00:00
}
if ( id < = 0 | | String . IsNullOrEmpty ( name ) ) throw new ArgumentException ( CRMInvoiceResource . EmptyTaxNameError ) ;
2021-03-19 16:56:26 +00:00
if ( ! _daoFactory . GetInvoiceTaxDao ( ) . IsExist ( id ) ) throw new ItemNotFoundException ( ) ;
2021-03-05 20:06:49 +00:00
var invoiceTax = new InvoiceTax
{
ID = id ,
Name = name ,
Description = description ,
Rate = rate
} ;
2021-03-19 16:56:26 +00:00
invoiceTax = _daoFactory . GetInvoiceTaxDao ( ) . SaveOrUpdateInvoiceTax ( invoiceTax ) ;
_messageService . Send ( MessageAction . InvoiceTaxUpdated , _messageTarget . Create ( invoiceTax . ID ) , invoiceTax . Name ) ;
2021-03-05 20:06:49 +00:00
2021-03-17 11:50:13 +00:00
return _mapper . Map < InvoiceTaxDto > ( invoiceTax ) ;
2021-03-05 20:06:49 +00:00
}
/// <summary>
/// Delete the invoice tax with the ID specified in the request
/// </summary>
/// <param name="id">Tax ID</param>
/// <short>Delete invoice tax</short>
/// <category>Invoices</category>
/// <returns>InvoiceTax</returns>
2022-05-31 14:01:58 +00:00
[HttpDelete(@"invoice/tax/{id:int}")]
2021-03-09 15:37:16 +00:00
public InvoiceTaxDto DeleteInvoiceTax ( int id )
2021-03-05 20:06:49 +00:00
{
2021-03-19 16:56:26 +00:00
if ( ! _crmSecurity . IsAdmin )
2021-03-05 20:06:49 +00:00
{
2021-03-19 16:56:26 +00:00
throw _crmSecurity . CreateSecurityException ( ) ;
2021-03-05 20:06:49 +00:00
}
if ( id < = 0 ) throw new ArgumentException ( ) ;
2021-03-19 16:56:26 +00:00
var invoiceTax = _daoFactory . GetInvoiceTaxDao ( ) . DeleteInvoiceTax ( id ) ;
2021-03-05 20:06:49 +00:00
if ( invoiceTax = = null ) throw new ItemNotFoundException ( ) ;
2021-03-19 16:56:26 +00:00
_messageService . Send ( MessageAction . InvoiceTaxDeleted , _messageTarget . Create ( invoiceTax . ID ) , invoiceTax . Name ) ;
2021-03-05 20:06:49 +00:00
2021-03-17 11:50:13 +00:00
return _mapper . Map < InvoiceTaxDto > ( invoiceTax ) ;
2021-03-05 20:06:49 +00:00
}
/// <summary>
/// Get default invoice settings
/// </summary>
/// <short>Get default invoice settings</short>
/// <category>Invoices</category>
/// <returns>InvoiceSetting</returns>
2022-05-31 14:01:58 +00:00
[HttpGet(@"invoice/settings")]
2021-03-05 20:06:49 +00:00
public InvoiceSetting GetSettings ( )
{
2021-03-19 16:56:26 +00:00
return _daoFactory . GetInvoiceDao ( ) . GetSettings ( ) ;
2021-03-05 20:06:49 +00:00
}
/// <summary>
/// Save default invoice number
/// </summary>
/// <param name="autogenerated">Is autogenerated</param>
/// <param name="prefix">Prefix</param>
/// <param name="number">Number</param>
/// <short>Save default invoice number</short>
/// <category>Invoices</category>
/// <returns>InvoiceSetting</returns>
2022-05-31 14:01:58 +00:00
[HttpPut(@"invoice/settings/name")]
2021-03-05 20:06:49 +00:00
public InvoiceSetting SaveNumberSettings (
2021-03-17 11:50:13 +00:00
SaveNumberSettingsRequestDto inDto
2021-03-05 20:06:49 +00:00
)
{
var autogenerated = inDto . AutoGenerated ;
var number = inDto . Number ;
var prefix = inDto . Prefix ;
2021-03-19 16:56:26 +00:00
if ( ! _crmSecurity . IsAdmin ) throw _crmSecurity . CreateSecurityException ( ) ;
2021-03-05 20:06:49 +00:00
if ( autogenerated & & string . IsNullOrEmpty ( number ) )
throw new ArgumentException ( ) ;
2021-03-19 16:56:26 +00:00
if ( autogenerated & & _daoFactory . GetInvoiceDao ( ) . IsExist ( prefix + number ) )
2021-03-05 20:06:49 +00:00
throw new ArgumentException ( ) ;
var invoiceSetting = GetSettings ( ) ;
invoiceSetting . Autogenerated = autogenerated ;
invoiceSetting . Prefix = prefix ;
invoiceSetting . Number = number ;
2021-03-19 16:56:26 +00:00
var settings = _daoFactory . GetInvoiceDao ( ) . SaveInvoiceSettings ( invoiceSetting ) ;
_messageService . Send ( MessageAction . InvoiceNumberFormatUpdated ) ;
2021-03-05 20:06:49 +00:00
return settings ;
}
/// <summary>
/// Save default invoice terms
/// </summary>
/// <param name="terms">Terms</param>
/// <short>Save default invoice terms</short>
/// <category>Invoices</category>
/// <returns>InvoiceSetting</returns>
2022-05-31 14:01:58 +00:00
[HttpPut(@"invoice/settings/terms")]
2021-03-05 20:06:49 +00:00
public InvoiceSetting SaveTermsSettings ( string terms )
{
2021-03-19 16:56:26 +00:00
if ( ! _crmSecurity . IsAdmin ) throw _crmSecurity . CreateSecurityException ( ) ;
2021-03-05 20:06:49 +00:00
var invoiceSetting = GetSettings ( ) ;
invoiceSetting . Terms = terms ;
2021-03-19 16:56:26 +00:00
var result = _daoFactory . GetInvoiceDao ( ) . SaveInvoiceSettings ( invoiceSetting ) ;
_messageService . Send ( MessageAction . InvoiceDefaultTermsUpdated ) ;
2021-03-05 20:06:49 +00:00
return result ;
}
/// <visible>false</visible>
2022-05-31 14:01:58 +00:00
[HttpPut(@"invoice/{invoiceid:int}/creationdate")]
2021-03-05 20:06:49 +00:00
public void SetInvoiceCreationDate ( int invoiceid , ApiDateTime creationDate )
{
2021-03-19 16:56:26 +00:00
var dao = _daoFactory . GetInvoiceDao ( ) ;
2021-03-05 20:06:49 +00:00
var invoice = dao . GetByID ( invoiceid ) ;
2021-03-19 16:56:26 +00:00
if ( invoice = = null | | ! _crmSecurity . CanAccessTo ( invoice ) )
2021-03-05 20:06:49 +00:00
throw new ItemNotFoundException ( ) ;
dao . SetInvoiceCreationDate ( invoiceid , creationDate ) ;
}
/// <visible>false</visible>
2022-05-31 14:01:58 +00:00
[HttpPut(@"invoice/{invoiceid:int}/lastmodifeddate")]
2021-03-05 20:06:49 +00:00
public void SetInvoiceLastModifedDate ( int invoiceid , ApiDateTime lastModifedDate )
{
2021-03-19 16:56:26 +00:00
var dao = _daoFactory . GetInvoiceDao ( ) ;
2021-03-05 20:06:49 +00:00
var invoice = dao . GetByID ( invoiceid ) ;
2021-03-19 16:56:26 +00:00
if ( invoice = = null | | ! _crmSecurity . CanAccessTo ( invoice ) )
{
2021-03-05 20:06:49 +00:00
throw new ItemNotFoundException ( ) ;
2021-03-19 16:56:26 +00:00
}
2021-03-05 20:06:49 +00:00
dao . SetInvoiceLastModifedDate ( invoiceid , lastModifedDate ) ;
}
2021-03-09 15:37:16 +00:00
private IEnumerable < InvoiceBaseDto > ToListInvoiceBaseDtos ( ICollection < Invoice > items )
2021-03-05 20:06:49 +00:00
{
2021-03-09 15:37:16 +00:00
if ( items = = null | | items . Count = = 0 ) return new List < InvoiceDto > ( ) ;
2021-03-05 20:06:49 +00:00
2021-03-09 15:37:16 +00:00
var result = new List < InvoiceBaseDto > ( ) ;
2021-03-05 20:06:49 +00:00
var contactIDs = items . Select ( item = > item . ContactID ) ;
2021-03-19 16:56:26 +00:00
2021-03-05 20:06:49 +00:00
contactIDs . ToList ( ) . AddRange ( items . Select ( item = > item . ConsigneeID ) ) ;
2021-03-19 16:56:26 +00:00
var contacts = _daoFactory . GetContactDao ( ) . GetContacts ( contactIDs . Distinct ( ) . ToArray ( ) )
. ToDictionary ( item = > item . ID , x = > _mapper . Map < ContactBaseWithEmailDto > ( x ) ) ;
2020-04-16 19:41:37 +00:00
2021-03-05 20:06:49 +00:00
foreach ( var invoice in items )
{
2021-03-19 16:56:26 +00:00
var invoiceDto = _mapper . Map < InvoiceBaseDto > ( invoice ) ;
2020-04-16 19:41:37 +00:00
2021-03-05 20:06:49 +00:00
if ( contacts . ContainsKey ( invoice . ContactID ) )
{
2021-03-09 15:37:16 +00:00
invoiceDto . Contact = contacts [ invoice . ContactID ] ;
2021-03-05 20:06:49 +00:00
}
2020-04-16 19:41:37 +00:00
2021-03-05 20:06:49 +00:00
if ( contacts . ContainsKey ( invoice . ConsigneeID ) )
{
2021-03-09 15:37:16 +00:00
invoiceDto . Consignee = contacts [ invoice . ContactID ] ;
2021-03-05 20:06:49 +00:00
}
2021-03-02 16:29:07 +00:00
2021-03-05 20:06:49 +00:00
if ( invoice . EntityID > 0 )
{
2021-03-09 15:37:16 +00:00
invoiceDto . Entity = ToEntityDto ( invoice . EntityType , invoice . EntityID ) ; //Need to optimize
2021-03-05 20:06:49 +00:00
}
2021-03-02 16:29:07 +00:00
2021-03-19 16:56:26 +00:00
invoiceDto . Cost = invoice . GetInvoiceCost ( _daoFactory ) ;
2021-03-02 16:29:07 +00:00
2021-03-09 15:37:16 +00:00
result . Add ( invoiceDto ) ;
2021-03-05 20:06:49 +00:00
}
2021-03-02 16:29:07 +00:00
2021-03-05 20:06:49 +00:00
return result ;
}
2021-03-10 15:38:56 +00:00
private EntityDto ToEntityDto ( EntityType entityType , int entityID )
{
if ( entityID = = 0 ) return null ;
var result = new EntityDto
{
EntityId = entityID
} ;
switch ( entityType )
{
case EntityType . Case :
2021-03-19 16:56:26 +00:00
var caseObj = _daoFactory . GetCasesDao ( ) . GetByID ( entityID ) ;
2021-03-10 15:38:56 +00:00
if ( caseObj = = null )
return null ;
result . EntityType = "case" ;
result . EntityTitle = caseObj . Title ;
break ;
case EntityType . Opportunity :
2021-03-19 16:56:26 +00:00
var dealObj = _daoFactory . GetDealDao ( ) . GetByID ( entityID ) ;
2021-03-10 15:38:56 +00:00
if ( dealObj = = null )
return null ;
result . EntityType = "opportunity" ;
result . EntityTitle = dealObj . Title ;
break ;
default :
return null ;
}
return result ;
}
2021-03-05 20:06:49 +00:00
}
}