commit12
This commit is contained in:
@@ -25,7 +25,7 @@ final class ServiceWorkflow
|
||||
|
||||
private function query(string $area, array $scope)
|
||||
{
|
||||
return DB::table('a_'.$area)->where('tenant_id', $scope['tenant_id'])->where('company_id', $scope['company_id'])->where('unit_id', $scope['unit_id']);
|
||||
return \App\Shared\Context\ModuleRecords::query('a_'.$area)->where('tenant_id', $scope['tenant_id'])->where('company_id', $scope['company_id'])->where('unit_id', $scope['unit_id']);
|
||||
}
|
||||
|
||||
public function list(string $area, int $actor): array
|
||||
@@ -74,7 +74,9 @@ final class ServiceWorkflow
|
||||
$scope = $this->scope($area, $actor, $action);
|
||||
$selectedUnit = $this->context->unitId();
|
||||
abort_unless((bool) preg_match('/^[a-zA-Z0-9-]{8,100}$/D', $key), 422, 'É necessária uma chave de repetição válida.');
|
||||
$hash = hash('sha256', json_encode($this->canonical([$area, $id, $scope, $input]), JSON_THROW_ON_ERROR));
|
||||
$identity=[$area, $id, $scope, $input];
|
||||
if($module=\App\Shared\Context\ModuleRecords::active())$identity[]=$module;
|
||||
$hash = hash('sha256', json_encode($this->canonical($identity), JSON_THROW_ON_ERROR));
|
||||
return DB::transaction(function () use ($area, $id, $actor, $input, $key, $scope, $selectedUnit, $hash, $action): array {
|
||||
DB::table('companies')->where('id', $scope['company_id'])->where('tenant_id', $scope['tenant_id'])->lockForUpdate()->firstOrFail();
|
||||
$this->context->selectFor($actor, $scope['tenant_id'], $scope['company_id'], $selectedUnit);
|
||||
@@ -99,13 +101,18 @@ final class ServiceWorkflow
|
||||
$data = $values['data']; $values['data'] = json_encode($data === [] ? new \stdClass() : $data, JSON_THROW_ON_ERROR);
|
||||
$values += ['version' => $version, 'updated_at' => now()];
|
||||
if ($id) { $this->query($area, $scope)->where('id', $id)->update($values); }
|
||||
else { $id = DB::table('a_'.$area)->insertGetId($scope + $values + ['created_at' => now()]); }
|
||||
else {
|
||||
$id = DB::table('a_'.$area)->insertGetId($scope + $values + ['created_at' => now()]);
|
||||
if ($module=\App\Shared\Context\ModuleRecords::active()) {
|
||||
\App\Shared\Context\ModuleRecords::associate($area,$id,$actor,$scope,$module,'Origem explícita da criação no módulo activo.',false,$version);
|
||||
}
|
||||
}
|
||||
$record = $this->query($area, $scope)->where('id', $id)->first();
|
||||
if ($area === 'billing_drafts' && ! $old) { app(DocumentPreparation::class)->inheritToBilling((int) $record->source_id, $id, $actor, $scope); }
|
||||
$snapshot = (array) $record; $snapshot['data'] = $data;
|
||||
$pending = ServicePermissions::unitScoped($area) ? DraftValues::pending($data) : [];
|
||||
if ($area === 'billing_drafts') {
|
||||
$current = DB::table('a_service_drafts')->where('id', $record->source_id)->value('version');
|
||||
$current = \App\Shared\Context\ModuleRecords::query('a_service_drafts')->where('id', $record->source_id)->value('version');
|
||||
if ((int) $current !== (int) $data['source_version']) { $pending['source_changed'] = 'A prestação tem uma versão mais recente. Reveja ou actualize explicitamente os dados de origem.'; }
|
||||
}
|
||||
$snapshot['pending'] = $pending;
|
||||
@@ -147,7 +154,7 @@ final class ServiceWorkflow
|
||||
if (!$old && \Illuminate\Support\Facades\Schema::hasTable('wb_generations')) {
|
||||
abort_if(\App\Modules\Workshop\WorkshopCommercialState::linked('service_drafts', (int)$sourceId),409,'Prestação de oficina já convertida: não pode gerar um segundo rascunho por outro percurso.');
|
||||
}
|
||||
$source = DB::table('a_service_drafts')->where('tenant_id', $scope['tenant_id'])->where('company_id', $scope['company_id'])->where('unit_id', $scope['unit_id'])->where('id', $sourceId)->first();
|
||||
$source = \App\Shared\Context\ModuleRecords::query('a_service_drafts')->where('tenant_id', $scope['tenant_id'])->where('company_id', $scope['company_id'])->where('unit_id', $scope['unit_id'])->where('id', $sourceId)->first();
|
||||
abort_unless($source, 403, 'Prestação fora da unidade autorizada.');
|
||||
$reload = ! $old || ($input['reload_source'] ?? false);
|
||||
if ($reload) { $this->permissions->require($actor, $scope['tenant_id'], $scope['company_id'], 'service_drafts.view', $scope['unit_id']); }
|
||||
@@ -161,7 +168,7 @@ final class ServiceWorkflow
|
||||
} else {
|
||||
$clientId = $input['client_id']; $lines = $input['lines']; $currency = $input['currency'] ?? null;
|
||||
}
|
||||
$client = DB::table('a_clients')->where('tenant_id', $scope['tenant_id'])->where('company_id', $scope['company_id'])->where('id', $clientId)->first();
|
||||
$client = \App\Shared\Context\ModuleRecords::query('a_clients')->where('tenant_id', $scope['tenant_id'])->where('company_id', $scope['company_id'])->where('id', $clientId)->first();
|
||||
abort_unless($client, 403, 'Cliente fora da empresa autorizada.');
|
||||
$data = ['description' => $input['description'] ?? ($base['description'] ?? ($oldData['description'] ?? null)), 'currency' => $currency,
|
||||
'lines' => DraftValues::lines($lines, $scope), 'client_name' => $client->name, 'tax_status' => 'pending', 'accounting_status' => 'not_processed'];
|
||||
|
||||
Reference in New Issue
Block a user