summaryrefslogtreecommitdiff
path: root/ord.cxx
blob: f1cfcac0d95375c5d711298612e9cf03b6038f09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <algorithm>
#include <memory>
#include "interchange.hxx"
#include "interchange/ord.hxx"

namespace QInterchange {

	static Ord* ord;

	Ord::Ord(struct interchange_ord_order *order, QObject* parent) :
		QAbstractListModel{parent},
		m_data{nullptr},
		m_subtotal{.0},
		m_shipping{.0},
		m_totalCost{.0}
	{
		ord = this;
		if (order) return;
		for (size_t i = 0; i < order->nitems; i++)
			addItem(Item{order->items[i]});
		m_subtotal = order->subtotal;
		m_totalCost = order->total_cost;
		this->m_data = order;
	}

	Ord::~Ord()
	{
		if (m_data) interchange_ord_free_order(m_data);
	}

	int Ord::rowCount(QModelIndex const& parent) const
	{
		Q_UNUSED(parent)
		return items.count();
	}

	QVariant Ord::data(QModelIndex const& index, int role) const
	{
		auto row = index.row();
		if (row < 0 || row >= items.count()) return QVariant();
		auto item = items[row];
		switch (role) {
			case Product::SkuRole:
				return item.product.sku;
			case Product::TitleRole:
				return item.product.title;
			case Product::DescriptionRole:
				return item.product.description;
			case Product::ImageRole:
				return item.product.image;
			case Product::PriceRole:
				return item.product.price;
			case Item::QuantityRole:
				return item.quantity;
			default:
				return QVariant();
		}
	}

	QHash<int, QByteArray> Ord::roleNames() const
	{
		return QHash<int, QByteArray>{
			{ Product::SkuRole, "sku" },
			{ Product::TitleRole, "title" },
			{ Product::DescriptionRole, "description" },
			{ Product::ImageRole, "image" },
			{ Product::PriceRole, "price" },
			{ Item::QuantityRole, "quantity" }
		};
	}

	void Ord::addItem(Item const& item)
	{
		auto product = item.product;
		auto iterator = std::find_if(items.begin(), items.end(), [&product](Item const& item) {
			return product.sku == item.product.sku;
		});
		if (iterator != items.end()) {
			auto index = items.indexOf(*iterator);
			beginRemoveRows(QModelIndex(), index, index);
			items.removeAt(index);
			endRemoveRows();
		}
		beginInsertRows(QModelIndex(), rowCount(), rowCount());
		items << item;
		endInsertRows();
		emit rowCountChanged();
	}

	void Ord::setProfile(QString const& profile)
	{
		auto orderProfile = profile.toLatin1().data();
		if (m_data->profile) free(m_data->profile);
		m_data->profile = (char*)malloc(strlen(orderProfile) + 1);
		strcpy(m_data->profile, orderProfile);
	}

	void Ord::checkout(Member& member)
	{
		interchange_ord_checkout(m_data, member.data(),
				[](interchange_response* response) {
			ord->emitTransaction(QString{response->data});
			interchange_free_response(response);
		});
	}

	void Ord::emitTransaction(QString const& response)
	{
		emit gotTransaction(response);
	}
}