| 1 | #-*- coding: utf-8 -*- |
|---|
| 2 | |
|---|
| 3 | """ |
|---|
| 4 | Promogest |
|---|
| 5 | Copyright (C) 2005 by Promotux Informatica - http://www.promotux.it/ |
|---|
| 6 | """ |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | from sqlalchemy import * |
|---|
| 10 | from sqlalchemy.orm import * |
|---|
| 11 | from promogest.Environment import * |
|---|
| 12 | from Dao import Dao |
|---|
| 13 | from DaoUtils import * |
|---|
| 14 | from Multiplo import Multiplo |
|---|
| 15 | from RigaMovimento import RigaMovimento |
|---|
| 16 | from RigaDocumento import RigaDocumento |
|---|
| 17 | from Riga import Riga |
|---|
| 18 | from promogest.ui.utils import numeroRegistroGet |
|---|
| 19 | from Fornitore import Fornitore |
|---|
| 20 | from Cliente import Cliente |
|---|
| 21 | from Fornitura import Fornitura |
|---|
| 22 | from Operazione import Operazione |
|---|
| 23 | from ScontoFornitura import ScontoFornitura |
|---|
| 24 | from promogest.ui.utils import * |
|---|
| 25 | if hasattr(conf, "SuMisura") and getattr(conf.SuMisura,'mod_enable') == "yes": |
|---|
| 26 | from promogest.modules.SuMisura.dao.MisuraPezzo import MisuraPezzo |
|---|
| 27 | |
|---|
| 28 | class TestataMovimento(Dao): |
|---|
| 29 | |
|---|
| 30 | def __init__(self, arg=None): |
|---|
| 31 | Dao.__init__(self, entity=self) |
|---|
| 32 | self.__righeMovimento = [] |
|---|
| 33 | self.__dbRigheMovimento = [] |
|---|
| 34 | |
|---|
| 35 | @reconstructor |
|---|
| 36 | def init_on_load(self): |
|---|
| 37 | self.__righeMovimento = [] |
|---|
| 38 | self.__dbRigheMovimento = [] |
|---|
| 39 | |
|---|
| 40 | def _getRigheMovimento(self): |
|---|
| 41 | if not self.__righeMovimento: |
|---|
| 42 | self.__dbRigheMovimento = params['session'].query(RigaMovimento)\ |
|---|
| 43 | .with_parent(self)\ |
|---|
| 44 | .filter_by(id_testata_movimento=self.id)\ |
|---|
| 45 | .all() |
|---|
| 46 | self.__righeMovimento = self.__dbRigheMovimento[:] |
|---|
| 47 | return self.__righeMovimento |
|---|
| 48 | |
|---|
| 49 | def _setRigheMovimento(self, value): |
|---|
| 50 | self.__righeMovimento = value |
|---|
| 51 | |
|---|
| 52 | righe = property(_getRigheMovimento, _setRigheMovimento) |
|---|
| 53 | |
|---|
| 54 | def _segno_operazione(self): |
|---|
| 55 | if self.opera: return self.opera.segno |
|---|
| 56 | else: return "" |
|---|
| 57 | segnoOperazione = property(_segno_operazione) |
|---|
| 58 | |
|---|
| 59 | def _ragioneSocialeFornitore(self): |
|---|
| 60 | if self.forni: return self.forni.ragione_sociale |
|---|
| 61 | else: return "" |
|---|
| 62 | ragione_sociale_fornitore = property(_ragioneSocialeFornitore) |
|---|
| 63 | |
|---|
| 64 | def _ragioneSocialeCliente(self): |
|---|
| 65 | if self.cli: return self.cli.ragione_sociale |
|---|
| 66 | else: return "" |
|---|
| 67 | ragione_sociale_cliente= property(_ragioneSocialeCliente) |
|---|
| 68 | |
|---|
| 69 | def _cognome_cliente(self): |
|---|
| 70 | if self.cli: return self.cli.cognome |
|---|
| 71 | else: return "" |
|---|
| 72 | cognome_cliente= property(_cognome_cliente) |
|---|
| 73 | |
|---|
| 74 | def _nome_cliente(self): |
|---|
| 75 | if self.cli: return self.cli.nome |
|---|
| 76 | else: return "" |
|---|
| 77 | nome_cliente= property(_nome_cliente) |
|---|
| 78 | |
|---|
| 79 | def _cognome_fornitore(self): |
|---|
| 80 | if self.forni: return self.forni.cognome |
|---|
| 81 | else: return "" |
|---|
| 82 | cognome_fornitore= property(_cognome_fornitore) |
|---|
| 83 | |
|---|
| 84 | def _nome_fornitore(self): |
|---|
| 85 | if self.forni: return self.forni.nome |
|---|
| 86 | else: return "" |
|---|
| 87 | nome_fornitore= property(_nome_fornitore) |
|---|
| 88 | |
|---|
| 89 | def _getNumeroMagazzini(self): |
|---|
| 90 | """ |
|---|
| 91 | Restituisce il numero di magazzini presenti nel documento. Ci serve per poter effettuare |
|---|
| 92 | il trasferimento di articoli che partono tutti dallo stesso magazzino |
|---|
| 93 | """ |
|---|
| 94 | __numeroMagazzini = [] |
|---|
| 95 | for riga in self.righe: |
|---|
| 96 | if riga.id_magazzino not in __numeroMagazzini: |
|---|
| 97 | __numeroMagazzini.append(riga.id_magazzino) |
|---|
| 98 | return len(__numeroMagazzini) |
|---|
| 99 | #print len(self.righe), 'aaaaaaaa' |
|---|
| 100 | #if len(self.righe) > 0 and self.id: |
|---|
| 101 | #mov_query = params['session'].query(RigaMovimento.id).filter(RigaMovimento.id_testata_movimento == self.id) |
|---|
| 102 | #doc_query = params['session'].query(RigaDocumento.id).filter(RigaDocumento.id_testata_documento == self.id) |
|---|
| 103 | #res = params['session'].query(Riga.id_magazzino).filter(or_(Riga.id.in_(mov_query),Riga.id.in_(doc_query))).distinct().count() |
|---|
| 104 | #return res |
|---|
| 105 | |
|---|
| 106 | numeroMagazzini = property(_getNumeroMagazzini) |
|---|
| 107 | |
|---|
| 108 | def filter_values(self,k,v): |
|---|
| 109 | if k == 'daNumero': |
|---|
| 110 | dic = {k:testata_mov.c.numero >= v} |
|---|
| 111 | elif k == 'aNumero': |
|---|
| 112 | dic = {k:testata_mov.c.numero <= v} |
|---|
| 113 | elif k == 'daParte': |
|---|
| 114 | dic = {k:testata_mov.c.parte >= v} |
|---|
| 115 | elif k == 'aParte': |
|---|
| 116 | dic = {k:testata_mov.c.parte <= v} |
|---|
| 117 | elif k == 'daData': |
|---|
| 118 | dic = {k:testata_mov.c.data_movimento >= v} |
|---|
| 119 | elif k == 'aData': |
|---|
| 120 | dic = {k:testata_mov.c.data_movimento <= v} |
|---|
| 121 | elif k == 'idOperazione': |
|---|
| 122 | dic = {k:testata_mov.c.operazione == v} |
|---|
| 123 | elif k == 'idMagazzino': |
|---|
| 124 | dic = {k:testata_mov.c.id.in_(select([RigaMovimento.id_testata_movimento],and_(Riga.id==RigaMovimento.id,Riga.id_magazzino== v)))} |
|---|
| 125 | elif k == 'idCliente': |
|---|
| 126 | dic = {k:testata_mov.c.id_cliente == v} |
|---|
| 127 | elif k == 'idFornitore': |
|---|
| 128 | dic = {k:testata_mov.c.id_fornitore == v} |
|---|
| 129 | elif k == 'dataMovimento': |
|---|
| 130 | dic = {k: testata_mov.c.data_movimento == v} |
|---|
| 131 | elif k == 'registroNumerazione': |
|---|
| 132 | dic = {k:testata_mov.c.registro_numerazione==v} |
|---|
| 133 | elif k == 'id_testata_documento': |
|---|
| 134 | dic = {k:testata_mov.c.id_testata_documento ==v} |
|---|
| 135 | elif k == 'idTestataDocumento': |
|---|
| 136 | dic = {k:testata_mov.c.id_testata_documento ==v} |
|---|
| 137 | #'statoDocumento': testata_mov.c.stato_documento == v, |
|---|
| 138 | #'idArticolo': testata_movimento.c.id_articolo == v ARRIVANO QUI TRAMITE RIGA - RIGA DOCUMENTO |
|---|
| 139 | return dic[k] |
|---|
| 140 | |
|---|
| 141 | def righeMovimentoDel(self,id=None): |
|---|
| 142 | """ |
|---|
| 143 | Cancella le righe associate ad un documento |
|---|
| 144 | """ |
|---|
| 145 | #from promogest.dao.RigaMovimento import RigaMovimento |
|---|
| 146 | row = RigaMovimento().select(idTestataMovimento= id, |
|---|
| 147 | offset = None, |
|---|
| 148 | batchSize = None |
|---|
| 149 | ) |
|---|
| 150 | if row: |
|---|
| 151 | for r in row: |
|---|
| 152 | if "SuMisura" in modulesList: |
|---|
| 153 | mp = MisuraPezzo().select(idRiga=r.id) |
|---|
| 154 | if mp: |
|---|
| 155 | for m in mp: |
|---|
| 156 | params['session'].delete(m) |
|---|
| 157 | params["session"].commit() |
|---|
| 158 | params['session'].delete(r) |
|---|
| 159 | params["session"].commit() |
|---|
| 160 | return True |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | def persist(self): |
|---|
| 164 | """cancellazione righe associate alla testata |
|---|
| 165 | conn.execStoredProcedure('RigheMovimentoDel',(self.id, ))""" |
|---|
| 166 | pg2log.debug("DENTRO IL TESTATA MOVIMENTO") |
|---|
| 167 | if not self.numero: |
|---|
| 168 | valori = numeroRegistroGet(tipo="Movimento", date=self.data_movimento) |
|---|
| 169 | self.numero = valori[0] |
|---|
| 170 | self.registro_numerazione= valori[1] |
|---|
| 171 | params["session"].add(self) |
|---|
| 172 | params["session"].commit() |
|---|
| 173 | if self.righeMovimento: |
|---|
| 174 | #print "PRIMA DI CANCELLA RIGHE MOV", tempo() |
|---|
| 175 | self.righeMovimentoDel(id=self.id) |
|---|
| 176 | #print "DOPO CANCELLA RIGHE MOV", tempo() |
|---|
| 177 | for riga in self.righeMovimento: |
|---|
| 178 | if "RigaDocumento" in str(riga.__module__): |
|---|
| 179 | riga.persist() |
|---|
| 180 | else: |
|---|
| 181 | #annullamento id della riga |
|---|
| 182 | riga._resetId() |
|---|
| 183 | #associazione alla riga della testata |
|---|
| 184 | riga.id_testata_movimento = self.id |
|---|
| 185 | #salvataggio riga |
|---|
| 186 | riga.persist() |
|---|
| 187 | #print "DOPO il persist della riga", tempo() |
|---|
| 188 | if self.id_fornitore is not None and riga.id_articolo: |
|---|
| 189 | """aggiornamento forniture cerca la fornitura relativa al fornitore |
|---|
| 190 | con data <= alla data del movimento""" |
|---|
| 191 | fors = Fornitura().select(idArticolo=riga.id_articolo, |
|---|
| 192 | idFornitore=self.id_fornitore, |
|---|
| 193 | daDataPrezzo=None, |
|---|
| 194 | aDataPrezzo=self.data_movimento, |
|---|
| 195 | orderBy = 'data_prezzo DESC', |
|---|
| 196 | offset = None, |
|---|
| 197 | batchSize = None) |
|---|
| 198 | #print "DOPO dopo FORS", tempo() |
|---|
| 199 | daoFornitura = None |
|---|
| 200 | if fors: |
|---|
| 201 | if fors[0].data_prezzo == self.data_movimento: |
|---|
| 202 | # ha trovato una fornitura con stessa data: aggiorno questa fornitura |
|---|
| 203 | #pg2log.info("TROVATO UNA FORNITURA CON STESSA DATA: AGGIORNO QUESTA FORNITURA") |
|---|
| 204 | daoFornitura = Fornitura().getRecord(id=fors[0].id) |
|---|
| 205 | else: |
|---|
| 206 | """creo una nuova fornitura con data_prezzo pari alla data del movimento |
|---|
| 207 | copio alcuni dati dalla fornitura piu' prossima""" |
|---|
| 208 | #pg2log.info("CREO UNA NUOVA FORNITURA CON DATA_PREZZO PARI ALLA DATA DEL MOVIMENTO COPIO ALCUNI DATI DALLA FORNITURA PIU' PROSSIMA") |
|---|
| 209 | daoFornitura = Fornitura() |
|---|
| 210 | daoFornitura.scorta_minima = fors[0].scorta_minima |
|---|
| 211 | daoFornitura.id_multiplo = fors[0].id_multiplo |
|---|
| 212 | daoFornitura.tempo_arrivo_merce = fors[0].tempo_arrivo_merce |
|---|
| 213 | daoFornitura.fornitore_preferenziale = fors[0].fornitore_preferenziale |
|---|
| 214 | else: |
|---|
| 215 | # nessuna fornitura utilizzabile, ne creo una nuova (alcuni dati mancheranno) |
|---|
| 216 | #pg2log.info("NESSUNA FORNITURA UTILIZZABILE, NE CREO UNA NUOVA (ALCUNI DATI MANCHERANNO)") |
|---|
| 217 | daoFornitura = Fornitura() |
|---|
| 218 | |
|---|
| 219 | daoFornitura.id_fornitore = self.id_fornitore |
|---|
| 220 | daoFornitura.id_articolo = riga.id_articolo |
|---|
| 221 | if daoFornitura.data_fornitura is not None: |
|---|
| 222 | if self.data_movimento > daoFornitura.data_fornitura: |
|---|
| 223 | daoFornitura.data_fornitura = self.data_movimento |
|---|
| 224 | else: |
|---|
| 225 | daoFornitura.data_fornitura = self.data_movimento |
|---|
| 226 | daoFornitura.data_prezzo = self.data_movimento |
|---|
| 227 | daoFornitura.codice_articolo_fornitore = riga.codiceArticoloFornitore |
|---|
| 228 | daoFornitura.prezzo_lordo = riga.valore_unitario_lordo |
|---|
| 229 | daoFornitura.prezzo_netto = riga.valore_unitario_netto |
|---|
| 230 | daoFornitura.percentuale_iva = riga.percentuale_iva |
|---|
| 231 | daoFornitura.applicazione_sconti = riga.applicazione_sconti |
|---|
| 232 | sconti = [] |
|---|
| 233 | for s in riga.sconti: |
|---|
| 234 | daoSconto = ScontoFornitura() |
|---|
| 235 | daoSconto.id_fornitura = daoFornitura.id |
|---|
| 236 | daoSconto.valore = s.valore |
|---|
| 237 | daoSconto.tipo_sconto = s.tipo_sconto |
|---|
| 238 | sconti.append(daoSconto) |
|---|
| 239 | |
|---|
| 240 | daoFornitura.sconti = sconti |
|---|
| 241 | params["session"].add(daoFornitura) |
|---|
| 242 | params["session"].commit() |
|---|
| 243 | #print "DOPO il for generale di riga movimento", tempo() |
|---|
| 244 | self.__righeMovimento = [] |
|---|
| 245 | #params["session"].flush() |
|---|
| 246 | |
|---|
| 247 | testata_mov=Table('testata_movimento', params['metadata'],schema = params['schema'],autoload=True) |
|---|
| 248 | clie = Table('cliente',params['metadata'],schema = params['schema'],autoload=True) |
|---|
| 249 | rigamovi = Table('riga_movimento',params['metadata'],schema = params['schema'],autoload=True) |
|---|
| 250 | operaz = Table('operazione',params['metadata'],schema = params['mainSchema'],autoload=True) |
|---|
| 251 | |
|---|
| 252 | std_mapper = mapper(TestataMovimento, testata_mov,properties={ |
|---|
| 253 | "rigamov": relation(RigaMovimento,primaryjoin= |
|---|
| 254 | testata_mov.c.id==rigamovi.c.id_testata_movimento, |
|---|
| 255 | cascade="all, delete", |
|---|
| 256 | backref="testata_movimento"), |
|---|
| 257 | #"fornitore": relation(Fornitore, backref="testata_movimento"), |
|---|
| 258 | "forni":relation(Fornitore,primaryjoin= |
|---|
| 259 | (testata_mov.c.id_fornitore==Fornitore.id), backref="testata_movimento"), |
|---|
| 260 | "cli":relation(Cliente,primaryjoin= |
|---|
| 261 | (testata_mov.c.id_cliente==clie.c.id), backref="testata_movimento"), |
|---|
| 262 | "opera": relation(Operazione,primaryjoin = (testata_mov.c.operazione==Operazione.denominazione),backref="testata_movimento"), |
|---|
| 263 | }, order_by=testata_mov.c.id) |
|---|