#include #include #include #include #include #include #include #include #include #include #include #include #include "../cg/cgsaldac.h" #include "../ef/ef0301.h" #include "pi0002.h" #include "pi0002100a.h" /////////////////////////////////////////////////////////// // Main Mask /////////////////////////////////////////////////////////// class TPF_mask : public TAutomask { TRelation* _rel; protected: virtual bool on_field_event(TOperable_field& o, TField_event e, long jolly); bool on_sheet_event(TOperable_field& o, TField_event e, long jolly); public: void update_scad(); void update_sheet(); bool calc_residual(const TRiga_scadenze& scad, real& impres, bool& partially_unassigned, int& tipopag) const; bool fill_row(const TRiga_partite& rp, const TRiga_scadenze& rs, TToken_string& row, bool& partially_unassigned, bool force); void update_total(); TPF_mask(); virtual ~TPF_mask(); }; bool TPF_mask::on_sheet_event(TOperable_field& o, TField_event e, long jolly) { TMask& m = o.mask(); switch (o.dlg()) { case F_PAGATO: if (e == fe_modify) update_total(); break; case F_IMPORTOANT: if (e == fe_modify) update_total(); break; case F_SHEET: switch(e) { case se_query_add: return false; case se_notify_modify: update_total(); break; default: break; } default: break; } return true; } bool TPF_mask::on_field_event(TOperable_field& o, TField_event e, long jolly) { const int id = o.dlg(); switch (id) { case DLG_SAVEREC: if (e == fe_button) { update_scad(); update_sheet(); update_total(); } break; case F_CLIFO: if (e == fe_modify) { update_sheet(); update_total(); } break; case F_SHEET: return on_sheet_event(o, e, jolly); break; default: if (jolly > 0) return on_sheet_event(o, e, jolly); break; } return true; } void TPF_mask::update_scad() { TSheet_field& s = sfield(F_SHEET); const long items = s.items(); int pospagato = s.cid2index(F_PAGATO); int posanticip = s.cid2index(F_IMPORTOANT); int posabi = s.cid2index(F_ABI); int poscab = s.cid2index(F_CAB); int posanno = s.cid2index(F_ANNO); int posnumpart = s.cid2index(F_PARTITA); int posnriga = s.cid2index(F_RIGA); int posnrata = s.cid2index(F_RATA); for (long pos = 0L; pos < items; pos++) { TToken_string row = s.row(pos); TLocalisamfile scad(LF_SCADENZE); scad.zero(); scad.put(SCAD_TIPOCF, "C"); scad.put(SCAD_GRUPPO, 0); scad.put(SCAD_CONTO, 0); scad.put(SCAD_SOTTOCONTO, get(F_CLIFO)); scad.put(SCAD_ANNO, row.get(posanno)); scad.put(SCAD_NUMPART, row.get(posnumpart)); scad.put(SCAD_NRIGA, row.get(posnriga)); scad.put(SCAD_NRATA, row.get(posnrata)); if (scad.read() == NOERR) { const real importoant(row.get(posanticip)); const real importopag(row.get(pospagato)); scad.put(SCAD_IMPORTOANT, importoant-importopag); scad.put(SCAD_CODABIPR, row.get(posabi)); scad.put(SCAD_CODCABPR, row.get(poscab)); scad.rewrite(); } } } // Calcola il residuo di una rata tenendo conto anche degli eventuali // effetti non ancora contabilizzati bool TPF_mask::calc_residual(const TRiga_scadenze& scad, real& impres, bool& partially_unassigned, int& tipopag) const { tipopag = scad.get_int(SCAD_TIPOPAG); const bool valuta = scad.in_valuta(); const char tipocf = scad.get_char(SCAD_TIPOCF); TImporto importo = scad.residuo(TRUE); importo.normalize(tipocf == 'C' ? 'D' : 'A'); impres = importo.valore(); partially_unassigned = FALSE; // Contolla se ci sono pagamenti non assegnati da gestire const TRiga_partite& fattura = scad.riga(); TPartita& partita = fattura.partita(); const TRecord_array& unassigned = partita.unassigned(); if (unassigned.rows() > 0) { real tot_unassigned; // Totale pagamenti non assegnati for (int u = unassigned.last_row(); u > 0; u = unassigned.pred_row(u)) { const real imp = unassigned[u].get(valuta ? PAGSCA_IMPORTOVAL : PAGSCA_IMPORTO); tot_unassigned += imp; } // Scala non assegnati dalle rate precedenti ancora aperte const int nrata = scad.get_int(SCAD_NRATA); for (int r = 1; r < nrata; r++) { if (!fattura.rata(r).chiusa()) { TImporto res = fattura.rata(r).residuo(TRUE); tot_unassigned -= res.valore(); if (tot_unassigned <= ZERO) break; } } // Scala non assegnati dalla rata corrente if (tot_unassigned > ZERO) { if (tot_unassigned >= impres) impres = ZERO; else impres -= tot_unassigned; partially_unassigned = TRUE; } } return !impres.is_zero(); } void TPF_mask::update_total() { TSheet_field& s = sfield(F_SHEET); const int postoa = s.cid2index(F_IMPORTOANT); const int postop = s.cid2index(F_PAGATO); real tota; real totp; FOR_EACH_SHEET_ROW_BACK(s, r, row) { tota += real(row->get(postoa)); totp += real(row->get(postop)); } set(F_TOTALEANT, tota); set(F_TOTALEPAG, totp); enable(DLG_SAVEREC, !tota.is_zero()||!totp.is_zero()); } bool TPF_mask::fill_row(const TRiga_partite& rp, const TRiga_scadenze& rs, TToken_string& row, bool& partially_unassigned, bool force) { int tipopag = 0; real impres; row.cut(0); bool ok = calc_residual(rs, impres, partially_unassigned, tipopag) || force; if (ok) { row.add(""); // importo pagato e' solo da inserire row.add(rs.get(SCAD_IMPORTOANT)); // importo anticipato row.add(rs.get(SCAD_CODABIPR)); row.add(rs.get(SCAD_CODCABPR)); row.add(rp.get(PART_IMPTOTDOC)); // importo fattura row.add(impres.string()); // residuo row.add(rs.get(SCAD_ANNO)); row.add(rs.get(SCAD_NUMPART)); row.add(rs.get(SCAD_NRIGA)); row.add(rs.get(SCAD_NRATA)); row.add(rs.get(SCAD_DATASCAD)); switch (tipopag) { case 1:row.add(TR("Rimessa Diretta")); break; case 2:row.add(TR("Tratta")); break; case 3:row.add(TR("Ricevuta Bancaria")); break; case 4:row.add(TR("Cessione")); break; case 5:row.add(TR("Pagherò")); break; case 6:row.add(TR("Lettera di credito")); break; case 7:row.add(TR("Tratta accettata")); break; case 8:row.add(TR("Rapporti interbancari diretti")); break; case 9:row.add(TR("Bonifico")); break; default: break; } } return ok; } void TPF_mask::update_sheet() { TSheet_field& sheet = sfield(F_SHEET); sheet.destroy(); TRelation rel(LF_PARTITE); TRectype filter(LF_PARTITE); filter.put(PART_TIPOCF, "C"); filter.put(PART_GRUPPO, 0); filter.put(PART_CONTO, 0); filter.put(PART_SOTTOCONTO, get(F_CLIFO)); TString filtro; filtro << "(CHIUSA!=\"X\")"; TCursor partite(&rel, filtro, 1, &filter, &filter); const long items = partite.items(); partite.freeze(); if (items > 0) { TRectype& partita = partite.curr(); TProgind pi(items, "Caricamento partite aperte", TRUE, TRUE); long last_cf = 0; int last_year = 0; TString16 last_game; TToken_string row; for (partite = 0L; partite.pos() < items; ++partite) { pi.addstatus(1); if (pi.iscancelled()) break; if (sheet.items() > 900) // Anche troppe righe break; const long cur_cf = partita.get_long(PART_SOTTOCONTO); const int cur_year = partita.get_int(PART_ANNO); const TString& cur_game = partita.get(PART_NUMPART); if (cur_cf == last_cf && cur_year == last_year && cur_game == last_game) continue; last_cf = cur_cf; last_year = cur_year; last_game = cur_game; const TPartita game(partita); const int last = game.last(); for (int riga = game.prima_fattura(); riga > 0 && riga <= last; riga = game.succ(riga)) { const TRiga_partite& rp = game.riga(riga); if (rp.is_fattura()) { for (int rata = 1; rata <= rp.rate(); rata++) { const TRiga_scadenze& rs = rp.rata(rata); if (!rs.chiusa()) { bool partially_unassigned; if (fill_row(rp, rs, row, partially_unassigned, false)) { const int numrow = sheet.items(); sheet.row(numrow) = row; } } // if rata aperta } // for ogni rata } // if e' una fattura } // for ogni fattura } // for ogni partita } // if ci sono partite sheet.force_update(); } TPF_mask::TPF_mask() : TAutomask("pi0002100a") { } TPF_mask::~TPF_mask() { } /////////////////////////////////////////////////////////// // Main Program /////////////////////////////////////////////////////////// class TPresent_fatture : public TSkeleton_application { protected: virtual const char* extra_modules() const { return "sc"; } public: virtual bool create(); virtual void main_loop(); virtual bool firm_change_enabled() const { return false; } }; void TPresent_fatture::main_loop() { open_files(LF_TABCOM, LF_TAB, LF_CLIFO, LF_CFVEN, LF_PARTITE, LF_SCADENZE, LF_PAGSCA, 0); TPF_mask m; while (m.run() == K_ENTER) { } } bool TPresent_fatture::create() { //se non ha la sc non può proseguire if (!has_module(SCAUT)) return error_box(TR("Modulo non autorizzato")); Tdninst dninst; if (!dninst.can_I_run(true)) return error_box(TR("Programma non autorizzato!")); return TSkeleton_application::create(); } int pi0002100(int argc, char* argv[]) { int n = 0; TPresent_fatture pe; pe.run(argc, argv, TR("Gestione anticipi fatture")); return 0; }