Files
SGF/scripts/summarize-ledger-sales.mjs
2026-11-04 09:37:20 +02:00

27 lines
8.1 KiB
JavaScript

import fs from 'node:fs';import path from 'node:path';import crypto from 'node:crypto';import {spawnSync} from 'node:child_process';
const read=p=>JSON.parse(fs.readFileSync(p,'utf8').replace(/^\uFEFF/,'')),sha=p=>crypto.createHash('sha256').update(fs.readFileSync(p)).digest('hex'),state=read('C:/laragon/data/sgf-tempo/run/modular-regression-current.json'),output='docs/proofs/common-ledger-sales-receivable-20261103.json';if(state.port!==55439||fs.existsSync(output))throw Error('Fresh owned consolidation required');
const names={
source_rule_recovery:'ledger-fc19bcb8d9de/sales-cases.json',actual_http_workflow:'ledger-fc19bcb8d9de/sales-http.json',
concurrency:'ledger-78270fedb4e4/sales-race.json',concurrent_source_postcheck:'ledger-78270fedb4e4/sales-source-postcheck.json',
latest_schema_workflow:'ledger-7dabce07b0c3/sales-workflow.json',latest_final:'ledger-7dabce07b0c3/sales-final.json',defaults:'ledger-7dabce07b0c3/sales-defaults.json',latest_http_read:'ledger-7dabce07b0c3/sales-readcheck.json',
schema_scope_pagination:'ledger-2b60f38982b1/sales-integrity.json',module_isolation:'ledger-2b60f38982b1/sales-module-check.json',
supplier_compatibility:'ledger-c47dfbc3fad1/ledger-cases.json',native_cycle_compatibility:'ledger-native-c5ff3a6aa5a3/native-cycle-cases.json',native_sales_refusal:'ledger-native-c5ff3a6aa5a3/sales-capability-refusal.json',
supplier_customer_coexistence:'ledger-7a94f1f4fe7d/sales-coexistence.json',
team_access:'ledger-b4c7b11c1593/sales-demo-access.json',team_http_readiness:'ledger-b4c7b11c1593/sales-demo-http.json',rendered_forms:'ledger-b4c7b11c1593/sales-forms.json'
};
const tests={},evidence=[];for(const [name,file]of Object.entries(names)){const p=path.join(state.root,'private',file),v=read(p);if(!v.passed)throw Error('Unsuccessful evidence '+name);tests[name]=v;evidence.push({file:'private/'+file,sha256:sha(p)});}
// Preserve the historical artifact verbatim, but do not mislabel an explicit QA runtime value as a default.
tests.latest_final={...tests.latest_final,explicit_qa_runtime_flag:tests.latest_final.default_sales_flag};delete tests.latest_final.default_sales_flag;
if(tests.defaults.default_without_environment!==false||tests.concurrency.workers!==6||tests.actual_http_workflow.checks!==95||tests.latest_schema_workflow.lines!==9||!tests.supplier_customer_coexistence.customer_supplier_ids_coincide)throw Error('Expected authorised evidence missing');
const priorFiles=['docs/proofs/common-finance-bundle-20261103.json','docs/proofs/common-ledger-complete-cycle-20261102.json','docs/proofs/common-ledger-payable-20261102.json','docs/proofs/sgf-financial-ledger-flow-20261102.json'];let preserved=0;
for(const file of priorFiles){const old=read(file);for(const f of old.private_evidence??old.evidence??[]){const p=path.resolve(state.root,f.file);if(!p.startsWith(path.resolve(state.root)+path.sep)||sha(p)!==f.sha256)throw Error('Previous evidence changed '+f.file);preserved++;}}
const scopeFile='docs/GENERAL_LEDGER_SALES_RECEIVABLE_SCOPE.md',proposal='Proposta concreta, **ainda sem autorização ou implementação**, para completar a ligação Comercial → Financeiro → Contabilidade. O ciclo anterior aprovado cobre obrigações de fornecedor; não autoriza contabilizar outras origens por arrasto. A extensão usa o mesmo writer e preserva as aprovações existentes.',approved=fs.readFileSync(scopeFile,'utf8').replace(/^\*\*Autorização expressa recebida:.+$/m,proposal),approvedHash=crypto.createHash('sha256').update(approved).digest('hex'),expected=read(priorFiles[0]).sources.find(s=>s.file===scopeFile)?.sha256;
if(approvedHash!==expected)throw Error('Approved scope archive differs from presented scope');const archive=path.join(state.root,'private/ledger-sales-approved-scope.md');if(fs.existsSync(archive)){if(sha(archive)!==approvedHash)throw Error('Approved archive changed');}else fs.writeFileSync(archive,approved);evidence.push({file:'private/ledger-sales-approved-scope.md',sha256:approvedHash});
const frozen=read(path.join(state.root,'finance-bundle-latest.json'));if(frozen.manifest_sha256!=='6a507ea7d708a12a8e526e4c610696d8db84079d18aede470237b65505630e7e'||sha(path.join(frozen.root,'manifest.json'))!==frozen.manifest_sha256)throw Error('Frozen bundle changed');const bundle=read(path.join(frozen.root,'manifest.json'));for(const f of bundle.files)if(sha(path.join(frozen.root,f.file))!==f.sha256)throw Error('Frozen runtime changed');
const sources=['packages/general-ledger/bootstrap.php','packages/general-ledger/README.md',...['LedgerSchema','PayableJournal','LedgerCycle','CycleSchema','CycleWorkspace','LedgerSales','SalesSchema'].map(n=>'packages/general-ledger/src/'+n+'.php'),...['GeneralLedger','GeneralLedgerController','SalesLedgerController','SalesLedgerSource'].map(n=>'app/Modules/Accounting/'+n+'.php'),'app/Modules/Services/ServiceReceivables.php','app/Modules/Purchasing/SupplierPayables.php','app/Shared/Testing/TestingDatabaseGuard.php','config/sales_ledger.php','config/general_ledger.php','routes/web.php','resources/views/accounting/general-ledger.blade.php','resources/views/accounting/sales-ledger.blade.php','resources/views/accounting/sales-demo-entry.blade.php','scripts/payable-history-sgf-proof.php','scripts/ledger-run.mjs',...fs.readdirSync('scripts').filter(n=>n.startsWith('ledger-sales-')).map(n=>'scripts/'+n),'scripts/summarize-ledger-sales.mjs',scopeFile,'docs/GENERAL_LEDGER_SALES_RECEIVABLE_DELIVERY.md','README.md','docs/PRODUCT_DEMO.md','docs/SGF_MODULE_FLOW_PREPARATION.md'];
let php=0,js=0;for(const file of sources){if(file.endsWith('.blade.php')||(!file.endsWith('.php')&&!file.endsWith('.mjs')))continue;const isPhp=file.endsWith('.php'),r=spawnSync(isPhp?'C:/laragon/tools/sgf-tempo/php-8.4.27/php.exe':process.execPath,[isPhp?'-l':'--check',file],{encoding:'utf8',windowsHide:true});if(r.status!==0)throw Error('Syntax failed '+file);isPhp?php++:js++;}
for(const cwd of [process.cwd(),path.resolve('../compras')]){const r=spawnSync('git',['diff','--check'],{cwd,encoding:'utf8',windowsHide:true});if(r.status!==0)throw Error('Diff check failed '+cwd+' '+r.stdout);}
const demo=read(path.join(state.root,'private/ledger-b4c7b11c1593/sales-demo.json')),r=await fetch(demo.url,{signal:AbortSignal.timeout(5000)});if(!r.ok||!(await r.text()).includes('data-sales-demo="'+demo.tag+'"'))throw Error('Team demo unavailable');
const result={increment:'COMMON-LEDGER-SALES-RECEIVABLE-SIM-v1',consolidated_at_utc:new Date().toISOString(),functional_code:true,synthetic_only:true,status:'implemented_and_tested_with_explicit_limits',authorization:{answer:'Autorizar o ciclo sintético proposto',approved_scope_sha256:approvedHash,archive_matches_presented_proposal:true},tests,evidence,previous_proofs:priorFiles.map(file=>({file,sha256:sha(file)})),previous_private_artifacts_verified:preserved,frozen_native_bundle:{files:bundle.files.length,manifest_sha256:frozen.manifest_sha256,all_hashes_preserved:true,sales_extension_not_installed:true},sources:sources.map(file=>({file,sha256:sha(file)})),quality:{syntax:{php_files:php,javascript_files:js},git_diff_check_sgf_and_native:true,phpstan_sgf:'not_installed_not_run',pint_sgf:'not_installed_not_run',new_dependencies:false},team_demo:demo,rights:{distinct_ledger_rights:7,ledger_grants_per_fixture:9,qa_only:true,real_grants:0},preservation:{six_pending_associations_untouched:true,blocked_cache_not_cleared:true,existing_demos_not_reset:true,unrelated_work_preserved:true,previous_valid_proofs_preserved:true},limits:{visual_review_pending:true,actual_browser_executed:false,deployment_capacity_validated:false,new_load_benchmarks:false,new_indexes:false,real_tax_or_chart:false,real_monetary_operations:false,destination_migrations:false,real_or_public_rights_changes:false,reopening:false,stock_cost_payroll_accounting:false,publication:false,purchasing_activation:false,acceptance_presumed:false,checkpoint:false}};
fs.writeFileSync(output,JSON.stringify(result,null,2)+'\n');console.log(JSON.stringify({output,http:tests.actual_http_workflow.checks,workers:tests.concurrency.workers,team_http:tests.team_http_readiness.checks,forms:tests.rendered_forms.checks,previous_artifacts_verified:preserved,syntax:result.quality.syntax,demo:demo.url}));