Skip to content

Instantly share code, notes, and snippets.

@cygeorgel
Created February 16, 2020 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cygeorgel/635bc3950cdf0e94b34e1d5afb3da8ad to your computer and use it in GitHub Desktop.
Save cygeorgel/635bc3950cdf0e94b34e1d5afb3da8ad to your computer and use it in GitHub Desktop.
Export CNM
<?php
namespace App\Console\Commands;
use App\Family;
use App\Invoice;
use App\InvoicePreprod;
use App\Mail\BlueRockToolsExportAccountMessage;
use App\Term;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;
class BlueRockTELTelecom0545ExportCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'brtools:telecom0545Export {start} {end}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Specific export for CNM Telecom';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$start = \Carbon\Carbon::createFromFormat('Y-m-d', $this->argument('start'));
$end = \Carbon\Carbon::createFromFormat('Y-m-d', $this->argument('end'));
$families = Family::all();
$eol = "\r\n";
$invoices = Invoice::where('date', '>=', $start->format('Y-m-d') . ' 00:00:00')
->where('date', '<=', $end->format('Y-m-d') . ' 23:59:59')
->get();
$docInfos = [];
$docInfos['format'] = 'Export CNM (Test Cyrille)';
$docInfos['start'] = $start;
$docInfos['end'] = $end;
$docInfos['mode'] = 'production';
$csv = "DATE;No PIECE;COMPTE;LIBELLE;DEBIT;CREDIT;$eol";
foreach ($invoices as $invoice) {
foreach ($invoice->sales as $sale) {
if ($sale->family_id) {
if ($sale->item->family_id) {
$sale->family_id = $sale->item->family_id;
$sale->save();
} else {
// crashes if not set:
dd ($invoice->id . '/' . $sale->id);
}
}
}
$check = 0;
$account = $invoice->customer->customerAccount;
if ( ! empty($invoice->customer->accountsReference)) {
$account = $invoice->customer->accountsReference;
}
$csv .= $invoice->date->format('d/m/Y') . ';';
$csv .= $invoice->number . organisation()->journal_sales . ';';
$csv .= $account . ';';
$csv .= $invoice->customer->name . ';';
$csv .= str_replace('.', ',', round($invoice->totalWithTax, 2)) . ';';
$csv .= null . $eol;
$check -= $invoice->totalWithTax;
foreach ($families as $family) {
if ($invoice->sales->where('family_id', $family->id)->sum('lineWithoutTax') != 0) {
$csv .= $invoice->date->format('d/m/Y') . ';';
$csv .= $invoice->number . organisation()->journal_sales . ';';
$csv .= str_pad($family->revenueAccount, 10, '0') . ';';
$csv .= $invoice->customer->name . ';';
$csv .= null . ';';
$csv .= str_replace('.', ',', round($invoice->sales->where('family_id', $family->id)->sum('lineWithoutTax'), 2)) . $eol;
$check += $invoice->sales->where('family_id', $family->id)->sum('lineWithoutTax');
}
}
$csv .= $invoice->date->format('d/m/Y') . ';';
$csv .= $invoice->number . organisation()->journal_sales . ';';
$csv .= str_pad('445715', 10, '0') . ';';
$csv .= $invoice->customer->name . ';';
$csv .= null . ';';
$csv .= str_replace('.', ',', round($invoice->tax,2)) . $eol;
$check += $invoice->tax;
// crashes if invoice is not well balanced:
if (round(abs($check), 2) > 1) {
dd ($invoice->number . ' is not balanced! (' . round(abs($check)) . ')');
}
}
// export payment terms for the same period:
$terms = Term::where('paymentTerm', '>=', $start)
->where('paymentTerm', '<=', $end)
->where('step_id', 1)
->get();
foreach ($terms as $term) {
$csv .= $term->paymentTerm->format('d/m/Y') . ';';
$csv .= $term->invoice->number . organisation()->journal_bank . ';';
$csv .= str_pad('5121', 10, '0') . ';';
$csv .= $term->invoice->customer->name . ';';
$csv .= str_replace('.', ',', round($term->debitAmount,2));
$csv .= null . ';';
$csv .= $eol;
}
// make file:
$location = 'exports/' . 'production' . '/exportCNM';
$filename = 'export_' . $start->format('Ymd') . '-' . $end->format('Ymd') . '.csv';
$disks = ['local', 'ftp'];
foreach($disks as $disk) {
Storage::disk($disk)->makeDirectory($location);
Storage::disk($disk)->put($location . '/' . $filename, $csv);
}
// send file:
$recipients = explode(',', env('PRODUCTION'));
foreach ($recipients as $recipient) {
Mail::to($recipient)->send(new BlueRockToolsExportAccountMessage($location . '/'. $filename, $docInfos));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment