136 lines
6.0 KiB
PHP
136 lines
6.0 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\Client;
|
|
use App\Models\Customers;
|
|
use App\Models\ClientInvoice;
|
|
use App\Models\ClientUserAssignation;
|
|
use App\Models\User;
|
|
use App\Services\ClientInvoiceApprovalService;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Services\GoogleAdsService;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Rap2hpoutre\FastExcel\FastExcel;
|
|
use Carbon\Carbon;
|
|
|
|
class CreateClientInvoice extends Command
|
|
{
|
|
protected $signature = 'customer:create-invoice';
|
|
protected $description = 'Create client invoice';
|
|
|
|
public function handle()
|
|
{
|
|
$adsService = new GoogleAdsService();
|
|
$approvalService = new ClientInvoiceApprovalService();
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$collection = (new FastExcel)->import(storage_path('app/public/csv/Fixed_EJ.csv'));
|
|
$array = $collection->toArray();
|
|
foreach ($array as $row) {
|
|
$startDate = Carbon::parse($row['start_date'])->format('Y-m-d');
|
|
$endDate = Carbon::parse($row['end_date'])->format('Y-m-d');
|
|
$client = Client::where('customer_id', str_replace('-', '', $row['customer_id']))->first();
|
|
if ($client) {
|
|
$client->update([
|
|
'industry' => $row['industry'],
|
|
]);
|
|
$salesUser = User::where('name', $row['sales'])->first();
|
|
$pic = User::where('name', $row['pic'])->first();
|
|
if ($pic) {
|
|
ClientUserAssignation::updateOrCreate(
|
|
[
|
|
'client_id' => $client->id,
|
|
'role' => ClientUserAssignation::ROLE_ASSIGNED_PERSON,
|
|
],
|
|
[
|
|
'user_id' => $pic->id,
|
|
]
|
|
);
|
|
}
|
|
if ($salesUser) {
|
|
ClientUserAssignation::updateOrCreate(
|
|
[
|
|
'client_id' => $client->id,
|
|
'role' => ClientUserAssignation::ROLE_SALES_PERSON,
|
|
],
|
|
[
|
|
'user_id' => $salesUser->id,
|
|
]
|
|
);
|
|
}
|
|
$row['client_id'] = $client->id;
|
|
// if ($client->status != 'CANCELED') {
|
|
// $campaigns = $adsService->listCampaigns($row['customer_id']);
|
|
// Log::info('Hydrated client data', [
|
|
// 'campaigns' => $campaigns,
|
|
// ]);
|
|
// foreach ($campaigns as $campaign) {
|
|
// Log::info('Hydrated client data', [
|
|
// 'campaigns' => $campaign['id'],
|
|
// ]);
|
|
|
|
// if (empty($invoice->start_date) || empty($invoice->end_date)) {
|
|
// $totalSpend = 0;
|
|
// $spend += number_format($totalSpend, 2, '.', '');
|
|
// } else {
|
|
// $metrics = $adsService->listCampaignsMetricsById(
|
|
// $row['customer_id'],
|
|
// $campaign['id'],
|
|
// $startDate,
|
|
// $endDate
|
|
// );
|
|
// Log::info('Hydrated client data', [
|
|
// 'metrics' => $metrics,
|
|
// ]);
|
|
// $totalSpend = array_sum(array_column($metrics, 'actual_spend'));
|
|
// $spend += number_format($totalSpend, 2, '.', '');
|
|
// }
|
|
// }
|
|
// } else {
|
|
$spend = 0;
|
|
// }
|
|
$managementFee = intval(str_replace(',', '', $row['management_fee'])) ?? 0;
|
|
$mediaFee = intval(str_replace(',', '', $row['media_fee'])) ?? 0;
|
|
$managementFeeAmount = $managementFee > 0 ? $managementFee / 1.08 : 0;
|
|
$mediaFeeAmount = $mediaFee > 0 ? $mediaFee / 1.08 : 0;
|
|
|
|
$invoice = ClientInvoice::updateOrCreate(
|
|
['invoice_no' => $row['invoice_no']],
|
|
[
|
|
'client_id' => $row['client_id'],
|
|
'is_credit_card' => $mediaFee == 0 ? 1 : 0,
|
|
'start_date' => $startDate,
|
|
'end_date' => $endDate,
|
|
'management_fee' => $managementFee,
|
|
'management_fee_amount' => $managementFeeAmount,
|
|
'management_fee_tax' => $managementFee - $managementFeeAmount,
|
|
'media_fee' => $mediaFee,
|
|
'media_fee_amount' => $mediaFeeAmount,
|
|
'media_fee_tax' => $mediaFee - $mediaFeeAmount,
|
|
'tax_percent' => 8,
|
|
'nett_amount' => $mediaFeeAmount,
|
|
'total_spending' => $spend,
|
|
]
|
|
);
|
|
|
|
$approvalService->approve($invoice);
|
|
} else {
|
|
Log::warning('Client not found for customer_id: '.str_replace('-', '', $row['customer_id']));
|
|
continue; // Skip this row if client not found
|
|
}
|
|
}
|
|
|
|
DB::commit();
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
Log::error('Error project linkage : '.$e->getMessage(), [
|
|
'trace' => $e->getTraceAsString(),
|
|
]);
|
|
return 1;
|
|
}
|
|
}
|
|
}
|