Logistics Automation - Invoice Generation
Tags: powerfuldevs, powerplatforms, logistics, automation
Published: 2023-02-08 09:31:58

**Need for Digital Invoice Generation**:
Invoice generation process for small business generally is a hassle, as it is time-consuming and prone to errors. The process involves spending countless hours on manual data entry, printing and mailing invoices, and chasing payments. The organisation and their customers would also benefiting from a more efficient and convenient billing process, leading to a better overall experience. 


**Conceptual Architecture View**:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l6hda29wj7jqn9pwtxws.png)


**<u>Platform Setup</u>**:

-  Connect the data connectors needed for the App
      a) **Sharepoint Connector** - Add the sharepoint list as a data source to PowerApp. The sharepoint list has some details that would be pulled to generate data related to invoices  
      b) **Office365Outlook Connector** - This connector would be leveraged to 
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h3uh9yf7871jcbgpz1x7.png)

- Enable PDF function (preview) which would help you to create PDF outputs with screen / container controls 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bmv1n2nbkzd915iqqyi8.png)


**Setup and Tricks for Configuration**

1. _Data selection scoping_:  In order to have smaller data transaction, the control leveraged is a drop down list item which pulls distinct values based on the customer name  

```
Distinct("InvoiceSharepoint",'CustID~customer identifier')
```
I might have multiple Work / group of tasks performed for a specific customer so the second drop down list is now pulling all the unique task ID

```
Distinct(Filter(InvoiceSharepoint, 'CustID' = CustID.Selected.Result),TaskIDs)
```
 the User experience would be as below where the associate would select the customer first and then the app will pull all the Tasks / orderID for which the Invoice would be generated subsequently 

2._Add Container for datatable_: Add the **_Datatable_** (Preview) to the container and filter the data to pull all the lineitems against the unique task ID selected by the user 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6q5qb59nqdophfzgf2eh.png)


```
Filter('InvoiceSharepoint', 'Title' = TaskIDs.SelectedText.Value)
```
The data scoping logic would be something like below 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m7p5yf2r3z6lses7ipph.png)

So the data scoping on an App would look something as below

![Image description]
(https://media.giphy.com/media/YBCm7AA7g4riKifWbw/giphy.gif)


3.PDF Magic and email - The last set is to Wrap the data on the container (**_Container10_**) as a PDF document and pushing that using the outlook send email function as an attachment. 

```
Office365Outlook.SendEmailV2(User().Email,"PDF Invoice", "Here is your Invoice",
   {Attachments:Table({
       Name:"Invoice.PDF",ContentBytes:PDF(Container10)
   }
   )
   }
)
```
The above magical code would wrap all the data in a container and push it as a PDF to the user. 

![Image description]
(https://media.giphy.com/media/EQplNJjDqLRjEPeJ8T/giphy.gif)



**Conclusion**

Digital invoice generation can help small businesses offering logistics services to streamline their billing process, increase efficiency and improve cash flow. Power Platform could assist with automation with invoice generation

Product reference:

1. **_PDF Functions_** https://learn.microsoft.com/en-us/power-apps/maker/canvas-apps/controls/control-pdf-viewer

2. **Container Controls** https://dev.to/balagmadhu/container-controls-powerapps-2l7d