Ejemplo n.º 1
0
 def onRangeChange(self, index):
     item = self.range_combo.itemData(index)
     self._begin, self._end = self.report_ranges[item][ITEM_METHOD]()
     self.changing_range = True
     self.from_date.setDateTime(QDateTime.fromSecsSinceEpoch(self._begin, spec=Qt.UTC))
     self.to_date.setDateTime(QDateTime.fromSecsSinceEpoch(self._end, spec=Qt.UTC))
     self.changing_range = False
     self.changed.emit(self._begin, self._end)
Ejemplo n.º 2
0
 def onReportRangeChange(self, range_index):
     report_ranges = {
         0: lambda: (0, 0),
         1: ManipulateDate.Last3Months,
         2: ManipulateDate.RangeYTD,
         3: ManipulateDate.RangeThisYear,
         4: ManipulateDate.RangePreviousYear
     }
     begin, end = report_ranges[range_index]()
     self.ReportFromDate.setDateTime(
         QDateTime.fromSecsSinceEpoch(begin, spec=Qt.UTC))
     self.ReportToDate.setDateTime(
         QDateTime.fromSecsSinceEpoch(end, spec=Qt.UTC))
Ejemplo n.º 3
0
 def setEditorData(self, editor, index):
     timestamp = index.model().data(index, Qt.EditRole)
     if timestamp == '':
         QStyledItemDelegate.setEditorData(self, editor, index)
     else:
         editor.setDateTime(
             QDateTime.fromSecsSinceEpoch(timestamp, spec=Qt.UTC))
Ejemplo n.º 4
0
 def setEditorData(self, editor, index):
     if index.column() == self._key:
         editor.setCurrentIndex(
             editor.findData(index.model().data(index, Qt.EditRole)))
     elif index.column() == self._value:
         type_idx = index.model().data(index.sibling(
             index.row(), self._key),
                                       role=Qt.EditRole)
         if self.types[type_idx][1] == "str":
             editor.setText(index.model().data(index, Qt.EditRole))
         elif self.types[type_idx][1] == "date":
             try:
                 timestamp = int(index.model().data(index, Qt.EditRole))
                 editor.setDateTime(
                     QDateTime.fromSecsSinceEpoch(timestamp, spec=Qt.UTC))
             except ValueError:
                 QStyledItemDelegate.setEditorData(self, editor, index)
         elif self.types[type_idx][1] == "float":
             try:
                 amount = float(index.model().data(index, Qt.EditRole))
             except (ValueError, TypeError):
                 amount = 0.0
             # QLocale().toString works in a bit weird way with float formatting - garbage appears after 5-6 decimal digits
             # if too long precision is specified for short number. So we need to be more precise setting precision.
             decimal_places = -decimal.Decimal(
                 str(amount).rstrip('0')).as_tuple().exponent
             editor.setText(QLocale().toString(amount, 'f', decimal_places))
         else:
             assert False, f"Unknown data type '{self.types[type_idx][1]}' in DataDelegate.setEditorData()"
     else:
         assert False, f"Delegate DataDelegate.setEditorData() called for not-initialized column {index.column()}"
Ejemplo n.º 5
0
    def parseJSON(self):
        # Slip data might be in a root element or in ticket/document/receipt
        if 'ticket' in self.slip_json:
            sub = self.slip_json['ticket']
            if 'document' in sub:
                sub = sub['document']
                if 'receipt' in sub:
                    slip = sub['receipt']
                else:
                    logging.error(self.tr("Can't find 'receipt' tag in json 'document'"))
                    return
            else:
                logging.error(self.tr("Can't find 'document' tag in json 'ticket'"))
                return
        else:
            slip = self.slip_json

        # Get operation type
        operation = 0
        if 'operationType' in slip:
            operation = int(slip['operationType'])
        else:
            logging.error(self.tr("Can't find 'operationType' tag in json 'ticket'"))
            return
        # Get shop name
        shop_name = ''
        if 'user' in slip:
            shop_name = self.SlipShopName.setText(slip['user'])
        if (not shop_name) and ('userInn' in slip):
            shop_name = self.slipsAPI.get_shop_name_by_inn(slip['userInn'])
        self.SlipShopName.setText(shop_name)

        peer_id = self.match_shop_name(self.SlipShopName.text())
        if peer_id is not None:
            self.PeerEdit.selected_id = peer_id

        try:
            self.slip_lines = pd.DataFrame(slip['items'])
        except:
            return

        # Get date from timestamp
        if 'dateTime' in slip:
            slip_datetime = QDateTime.fromSecsSinceEpoch(int(slip['dateTime']))
            slip_datetime.setTimeSpec(Qt.UTC)
            self.SlipDateTime.setDateTime(slip_datetime)

        # Convert price to roubles
        self.slip_lines['price'] = self.slip_lines['price'] / 100
        if operation == self.OPERATION_PURCHASE:
            self.slip_lines['sum'] = -self.slip_lines['sum'] / 100
        elif operation == self.OPERATION_RETURN:
            self.slip_lines['sum'] = self.slip_lines['sum'] / 100
        else:
            logging.error(self.tr("Unknown operation type ") + f"{operation}")
            return
        # Use quantity if it differs from 1 unit value
        self.slip_lines.loc[self.slip_lines['quantity'] != 1, 'name'] = \
            self.slip_lines.agg('{0[name]} ({0[quantity]:g} x {0[price]:.2f})'.format, axis=1)
        # Assign empty category
        self.slip_lines['category'] = 0
        self.slip_lines['confidence'] = 1
        # Assign empty tags
        self.slip_lines['tag'] = None
        self.slip_lines = self.slip_lines[['name', 'category', 'confidence', 'tag', 'sum']]

        self.model = PandasLinesModel(self.slip_lines)
        self.LinesTableView.setModel(self.model)

        self.delegate = SlipLinesDelegate(self.LinesTableView)
        for column in range(self.model.columnCount()):
            if column == 0:
                self.LinesTableView.horizontalHeader().setSectionResizeMode(column, QHeaderView.Stretch)
            elif column == 1:
                self.LinesTableView.setColumnWidth(column, 200)
            elif column == 2:
                self.LinesTableView.setColumnHidden(column, True)
            else:
                self.LinesTableView.setColumnWidth(column, 100)
            self.LinesTableView.setItemDelegateForColumn(column, self.delegate)
        font = self.LinesTableView.horizontalHeader().font()
        font.setBold(True)
        self.LinesTableView.horizontalHeader().setFont(font)
        self.LinesTableView.show()
        self.qr_data_loaded.emit()