Skip to content

Commit e2d39ae

Browse files
committed
init
0 parents  commit e2d39ae

File tree

9 files changed

+2133
-0
lines changed

9 files changed

+2133
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor
2+
/.idea
3+
composer.phar

README.md

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
# Print forms for Eloquent
2+
3+
Easy way to generate docx print forms of invoices, contracts and other documents.
4+
5+
## Usage
6+
7+
Installation
8+
9+
```bash
10+
composer require mnvx/eloquent-print-form
11+
```
12+
13+
Declare models for Eloquent.
14+
15+
Create print form (docx file) using next syntax.
16+
17+
- `${field_name}` - standard field
18+
- `${entity.field_name}` - field from linked entity
19+
- `${entity.field_name|date}` - format field as date
20+
- `${entity.field_name|date|placeholder}` - format field as date and replace to "____________________" if field is empty
21+
- `${entity1.entity2.field_name}` - field from linked entity through one table
22+
- `${entities.field_name}` - for table data
23+
24+
As you may see, it is possible to use pipes, for example: `|date`, `|date|placeholder`. Supported pipes are:
25+
- `placeholder` - replaces empty variable to "____________________".
26+
- `date` - format date
27+
- `dateTime` - format date time
28+
- `int` - format int value
29+
- `decimal` - format decimal value
30+
31+
It is possible also to specify custom pipes.
32+
33+
Generate print form for specified entity
34+
35+
```php
36+
$entity = Contract::find($id);
37+
$printFormProcessor = new PrintFormProcessor();
38+
$templateFile = resource_path('path_to_print_forms/your_print_form.docx');
39+
$tempFileName = $printFormProcessor->process($templateFile, $entity);
40+
```
41+
42+
## Examples
43+
44+
### Basic example
45+
46+
For example, if there are next models in project.
47+
48+
```php
49+
use Illuminate\Database\Eloquent\Model;
50+
51+
/**
52+
* @property string $number
53+
* @property string $start_at
54+
* @property int $customer_id
55+
* @property Customer $customer
56+
* @property ContractAppendix[] $appendixes
57+
*/
58+
class Contract extends Model
59+
{
60+
61+
public function customer()
62+
{
63+
return $this->belongsTo(Customer::class);
64+
}
65+
66+
public function appendixes()
67+
{
68+
return $this->hasMany(ContractAppendix::class);
69+
}
70+
}
71+
72+
/**
73+
* @property string $name
74+
* @property CustomerCategory $category
75+
*/
76+
class Customer extends Model
77+
{
78+
public function category()
79+
{
80+
return $this->belongsTo(CustomerCategory::class);
81+
}
82+
83+
}
84+
85+
/**
86+
* @property string $number
87+
* @property string $date
88+
* @property float $tax
89+
*/
90+
class ContractAppendix extends Model
91+
{
92+
93+
public function items()
94+
{
95+
return $this->hasMany(ContractAppendixItem::class, 'appendix_id');
96+
}
97+
98+
public function getTaxAttribute()
99+
{
100+
$tax = 0;
101+
foreach ($this->items as $item) {
102+
$tax += $item->total_amount * (1 - 100 / (100+($item->taxStatus->vat_rate ?? 0)));
103+
}
104+
return $tax;
105+
}
106+
}
107+
```
108+
109+
Example of Laravel's controller action for printing contract.
110+
111+
```php
112+
public function printInternet(FormRequest $request)
113+
{
114+
$id = $request->get('id');
115+
$entity = Contract::find($id);
116+
$printFormProcessor = new PrintFormProcessor();
117+
$templateFile = resource_path('path_to_print_forms/your_print_form.docx');
118+
$tempFileName = $printFormProcessor->process($templateFile, $entity);
119+
$filename = 'contract_' . $id;
120+
return response()
121+
->download($tempFileName, $filename . '.docx')
122+
->deleteFileAfterSend();
123+
}
124+
```
125+
126+
Example of docx template
127+
128+
---
129+
130+
Contract number: ${number|placeholder}
131+
Contract date: ${start_at|date|placeholder}
132+
Customer: ${customer.name}
133+
Customer category: ${customer.category.name}
134+
135+
Appendixes:
136+
137+
| Row number | Appendix number | Appendix tax |
138+
| ------------------------- | -------------------- | -----------------:|
139+
| ${appendixes.#row_number} | ${appendixes.number} | ${appendixes.tax} |
140+
141+
---
142+
143+
### How to specify custom pipes
144+
145+
```php
146+
class CustomPipes extends \Mnvx\EloquentPrintForm\Pipes
147+
{
148+
// Override
149+
public function placeholder($value)
150+
{
151+
return $value ?: "NO DATA";
152+
}
153+
154+
// New pipe
155+
public function custom($value)
156+
{
157+
return "CUSTOM";
158+
}
159+
}
160+
161+
$entity = Contract::find($id);
162+
$pipes = new CustomPipes();
163+
$printFormProcessor = new PrintFormProcessor($pipes);
164+
$templateFile = resource_path('path_to_print_forms/your_print_form.docx');
165+
$tempFileName = $printFormProcessor->process($templateFile, $entity);
166+
```
167+
168+
### How to specify custom processors for some variables
169+
170+
Example of custom processor for variable `${custom}`.
171+
172+
```php
173+
$tempFileName = $printFormProcessor->process($templateFile, $entity, [
174+
'custom' => function(TemplateProcessor $processor, string $variable, ?string $value) {
175+
$inline = new TextRun();
176+
$inline->addText('by a red italic text', array('italic' => true, 'color' => 'red'));
177+
$processor->setComplexValue($variable, $inline);
178+
},
179+
]);
180+
```

composer.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "mnvx/eloquent-print-form",
3+
"description": "Print form processor for Eloquent",
4+
"type": "library",
5+
"license": "MIT",
6+
"keywords": ["print form", "docx", "document", "word", "invoice", "contract"],
7+
"homepage": "https://github.com/mnvx/eloquent-print-form",
8+
"autoload": {
9+
"psr-4": {
10+
"Mnvx\\EloquentPrintForm\\": "src/"
11+
}
12+
},
13+
"authors": [
14+
{
15+
"name": "Nick",
16+
"email": "[email protected]"
17+
}
18+
],
19+
"require": {
20+
"php": "^7.4",
21+
"illuminate/database": "^8.13",
22+
"nesbot/carbon": "^2.41",
23+
"phpoffice/phpword": "^0.17.0"
24+
}
25+
}

0 commit comments

Comments
 (0)