|
@@ -0,0 +1,250 @@
|
|
|
|
+// SPDX-License-Identifier: GPL-3.0
|
|
|
|
+pragma solidity >=0.7.0 <0.9.0;
|
|
|
|
+pragma experimental ABIEncoderV2;
|
|
|
|
+
|
|
|
|
+contract Store {
|
|
|
|
+
|
|
|
|
+ //Структура пользователя
|
|
|
|
+ struct User
|
|
|
|
+ {
|
|
|
|
+ bytes32 Password;
|
|
|
|
+ uint Role; // 1 - Администратор, 2 - Покупатель, 3 - Продавец
|
|
|
|
+ uint Balance;
|
|
|
|
+ bool isExist;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //Структура заявки на смеу роли
|
|
|
|
+ struct Request
|
|
|
|
+ {
|
|
|
|
+ address addr;
|
|
|
|
+ uint Role;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //Структура продукта
|
|
|
|
+ struct Product
|
|
|
|
+ {
|
|
|
|
+ uint IdProduct;
|
|
|
|
+ string Name;
|
|
|
|
+ uint Price;
|
|
|
|
+ uint ShopId;
|
|
|
|
+ bool isExist;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //Структура транзакции
|
|
|
|
+ struct ProductTransaction
|
|
|
|
+ {
|
|
|
|
+ address SalesmenAddr;
|
|
|
|
+ address BuyerAddr;
|
|
|
|
+ uint ProductId;
|
|
|
|
+ uint Status; // 0 - в расмотрении на покупку, 1 - принят, 2 - отклонен, 3 - в рассмотрении на возврат, 4 - возврат принят, 5 - возврат отклонен
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //Структура магазина
|
|
|
|
+ struct Shop
|
|
|
|
+ {
|
|
|
|
+ uint IdShop;
|
|
|
|
+ string NameShop;
|
|
|
|
+ bool isExist;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //Заявки на покупку
|
|
|
|
+ struct RequestBuy{
|
|
|
|
+ address User;
|
|
|
|
+ uint ProductNumber;
|
|
|
|
+ uint Count;
|
|
|
|
+ bool Status;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Request[] Requests; //Массив
|
|
|
|
+
|
|
|
|
+ Product[] Products; //Массив продукта
|
|
|
|
+
|
|
|
|
+ Shop[] Shops; //Массив магазина
|
|
|
|
+
|
|
|
|
+ RequestBuy[] RequestBuys; //Массив покупок
|
|
|
|
+
|
|
|
|
+ ProductTransaction[] ProductTransactions; //Массив транзакции
|
|
|
|
+
|
|
|
|
+ //Конструктор
|
|
|
|
+ constructor()
|
|
|
|
+ {
|
|
|
|
+ // 4 Shop
|
|
|
|
+ Shops.push(Shop(1, "DNS", true));
|
|
|
|
+ Shops.push(Shop(2, "e - katalog", true));
|
|
|
|
+ Shops.push(Shop(3, "Ozon", true));
|
|
|
|
+ Shops.push(Shop(4, "Wildberries", true));
|
|
|
|
+
|
|
|
|
+ // 4 Product
|
|
|
|
+ Products.push(Product(1, "ASUS Laptop 15 F515KA-BR110W", 29, 1, true));
|
|
|
|
+ Products.push(Product(2, "Acer Aspire 1 A115-22-R136", 30, 2, true));
|
|
|
|
+ Products.push(Product(3, "HP 255 G8", 32, 3, true));
|
|
|
|
+ Products.push(Product(4, "Lenovo IdeaPad 3 15IGL05", 35, 4, true));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ mapping(address => User) private users; //Маппинг пользователя
|
|
|
|
+
|
|
|
|
+ //Авторизация
|
|
|
|
+ function AuthUser(string memory _password) public view returns(bool)
|
|
|
|
+ {
|
|
|
|
+ require(users[msg.sender].isExist == true, "User not exist");
|
|
|
|
+ if(users[msg.sender].Password == keccak256(abi.encodePacked(_password)))
|
|
|
|
+ return true;
|
|
|
|
+ else
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //Регистрация
|
|
|
|
+ function RegUser(string memory password, uint role) public
|
|
|
|
+ {
|
|
|
|
+ require(users[msg.sender].isExist == false, "Users already exist");
|
|
|
|
+ users[msg.sender] = User(
|
|
|
|
+ keccak256(abi.encodePacked(password)),
|
|
|
|
+ role,
|
|
|
|
+ 1000,
|
|
|
|
+ true
|
|
|
|
+ );
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //Повышение до администратора
|
|
|
|
+ function NewAdmin(address addr) public
|
|
|
|
+ {
|
|
|
|
+ require(users[msg.sender].Role == 1, "You are not a admin");
|
|
|
|
+ users[addr].Role = 1;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ function SalesmenAdopted(uint idProductTran) public //Принят
|
|
|
|
+ {
|
|
|
|
+ require(users[msg.sender].Role == 3, "You are not a seller");
|
|
|
|
+ RequestBuys[idProductTran].Status = true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ function SalesmenRejected(uint idProductTran) public //Отклонен
|
|
|
|
+ {
|
|
|
|
+ require(users[msg.sender].Role == 3, "You are not a seller");
|
|
|
|
+ RequestBuys[idProductTran].Status = false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // function Salesmenin_consideration_for_a_refund(uint idProductTran) public //В рассмотрении на возврат
|
|
|
|
+ // {
|
|
|
|
+ // require(users[msg.sender].Role == 3, "You are not a seller");
|
|
|
|
+ // RequestBuys[idProductTran].Status = 3;
|
|
|
|
+ // }
|
|
|
|
+
|
|
|
|
+ // function SalesmenReturnAdopted(uint idProductTran) public //Возврат принят
|
|
|
|
+ // {
|
|
|
|
+ // require(users[msg.sender].Role == 3, "You are not a seller");
|
|
|
|
+ // RequestBuys[idProductTran].Status = 4;
|
|
|
|
+ // }
|
|
|
|
+
|
|
|
|
+ // function SalesmenReturnRejected(uint idProductTran) public //Возврат отклонен
|
|
|
|
+ // {
|
|
|
|
+ // require(users[msg.sender].Role == 3, "You are not a seller");
|
|
|
|
+ // RequestBuys[idProductTran].Status = 5;
|
|
|
|
+ // }
|
|
|
|
+
|
|
|
|
+ //Принятие заявки на изменение роли
|
|
|
|
+ function AcceptRequestRole(uint index) public
|
|
|
|
+ {
|
|
|
|
+ require(users[msg.sender].Role == 1, "You are not a admin");
|
|
|
|
+ require(index < Requests.length, "Error");
|
|
|
|
+ users[Requests[index].addr].Role = Requests[index].Role;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //Понижение продавца
|
|
|
|
+ function SellerDown() public
|
|
|
|
+ {
|
|
|
|
+ require(users[msg.sender].Role == 3, "You are not a seller");
|
|
|
|
+ Requests.push(Request(msg.sender, 2));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //Повышение покупателя
|
|
|
|
+ function BuyerUpd() public
|
|
|
|
+ {
|
|
|
|
+ require(users[msg.sender].Role == 2, "You are not a buyer");
|
|
|
|
+ Requests.push(Request(msg.sender, 3));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //function AddProduct(string memory name, uint Count, uint shopId) public
|
|
|
|
+ //Добавление продукта
|
|
|
|
+ // function AddProduct(string memory name, uint Price) public
|
|
|
|
+ // {
|
|
|
|
+ // require(users[msg.sender].Role == 1, "You are not a admin");
|
|
|
|
+ // Products.push(Product
|
|
|
|
+ // (
|
|
|
|
+ // Products.length,
|
|
|
|
+ // name,
|
|
|
|
+ // Price,
|
|
|
|
+ // shopId,
|
|
|
|
+ // true
|
|
|
|
+ // ));
|
|
|
|
+ // }
|
|
|
|
+
|
|
|
|
+ //Добавление магазина
|
|
|
|
+ // function AddShop(uint idShop, string memory nameShop) public
|
|
|
|
+ // {
|
|
|
|
+ // require(users[msg.sender].Role == 1, "You are not admin");
|
|
|
|
+ // Shops.push(Shop
|
|
|
|
+ // (
|
|
|
|
+ // idShop,
|
|
|
|
+ // nameShop,
|
|
|
|
+ // true
|
|
|
|
+ // ));
|
|
|
|
+ // }
|
|
|
|
+
|
|
|
|
+ //Заявка на покупку
|
|
|
|
+ function BuyProduct(uint _Product, uint _Count) public payable
|
|
|
|
+ {
|
|
|
|
+ require(users[msg.sender].Role == 2, "You are not a buyer");
|
|
|
|
+ RequestBuys.push(RequestBuy(msg.sender, _Product, _Count, false));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //Отмена покупки
|
|
|
|
+ function DeletePurchases(uint _NumberBuys) public
|
|
|
|
+ {
|
|
|
|
+ require(users[msg.sender].isExist == true, "User not exist");
|
|
|
|
+ require(users[msg.sender].Role == 2, "User not buyer");
|
|
|
|
+ require(RequestBuys[_NumberBuys].User == msg.sender, "Not your purchase");
|
|
|
|
+ delete RequestBuys[_NumberBuys];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //Возврат покупки
|
|
|
|
+ function ReturnPurchases(uint _NumberBuys) public
|
|
|
|
+ {
|
|
|
|
+ require(users[msg.sender].isExist == true, "User not exist");
|
|
|
|
+ require(users[msg.sender].Role == 2, "User not buyer");
|
|
|
|
+ require(RequestBuys[_NumberBuys].User == msg.sender, "Not your purchase");
|
|
|
|
+ require(RequestBuys[_NumberBuys].Status == true, "Buy don't access");
|
|
|
|
+ RequestBuys[_NumberBuys].Status=false;
|
|
|
|
+ users[msg.sender].Balance+=Products[RequestBuys[_NumberBuys].ProductNumber].Price*RequestBuys[_NumberBuys].Count;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //Просмотр баланса
|
|
|
|
+ function LookBalance() public view returns(uint)
|
|
|
|
+ {
|
|
|
|
+ require(users[msg.sender].isExist == true, "User not exist");
|
|
|
|
+ return users[msg.sender].Balance;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //Подтверждение покупки
|
|
|
|
+ function PurchaseConfirmation(uint _NumberBuys) public
|
|
|
|
+ {
|
|
|
|
+ require(users[msg.sender].isExist == true, "User not exist");
|
|
|
|
+ require(users[msg.sender].Role == 3, "User not seller");
|
|
|
|
+ require(RequestBuys[_NumberBuys].Status == false, "Buy access");
|
|
|
|
+ RequestBuys[_NumberBuys].Status = true;
|
|
|
|
+ users[msg.sender].Balance+=Products[RequestBuys[_NumberBuys].ProductNumber].Price*RequestBuys[_NumberBuys].Count;
|
|
|
|
+ users[RequestBuys[_NumberBuys].User].Balance-=Products[RequestBuys[_NumberBuys].ProductNumber].Price*RequestBuys[_NumberBuys].Count;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //Отображение запросов на покупку
|
|
|
|
+ function GetRequestBuy() public view returns (RequestBuy[] memory)
|
|
|
|
+ {
|
|
|
|
+ return RequestBuys;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //Отображение продуктов
|
|
|
|
+ function GetProd() public view returns (Product[] memory)
|
|
|
|
+ {
|
|
|
|
+ return Products;
|
|
|
|
+ }
|
|
|
|
+}
|