From cdc26d4a94fbfecbb5677bf65bc48b015b87ece3 Mon Sep 17 00:00:00 2001 From: skytree-neo <36920875+skytree-neo@users.noreply.github.com> Date: Wed, 21 Mar 2018 19:58:43 +0800 Subject: [PATCH] =?UTF-8?q?Create=2009-=E7=94=B0=E5=90=9B-=E7=AC=AC?= =?UTF-8?q?=E4=B8=89=E8=AF=BE=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 截图放在word中,提交到目录中。 --- ...0\211\350\257\276\344\275\234\344\270\232" | 120 ++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 "09-\347\224\260\345\220\233-\347\254\254\344\270\211\350\257\276\344\275\234\344\270\232" diff --git "a/09-\347\224\260\345\220\233-\347\254\254\344\270\211\350\257\276\344\275\234\344\270\232" "b/09-\347\224\260\345\220\233-\347\254\254\344\270\211\350\257\276\344\275\234\344\270\232" new file mode 100644 index 00000000..6ea7950f --- /dev/null +++ "b/09-\347\224\260\345\220\233-\347\254\254\344\270\211\350\257\276\344\275\234\344\270\232" @@ -0,0 +1,120 @@ + +pragma solidity ^0.4.14; + + import './SafeMath.sol'; + + contract Payroll { + + address owner; + uint constant payDuration = 10 seconds; + uint totalSalary; + using SafeMath for uint; + + struct Employee { + address id;// same as index + uint salary; + uint payDay; + } + + mapping (address => Employee) public employees; + //mapping employees, + + function Payroll() { + owner = msg.sender; + } + + modifier onlyOwner { + require (msg.sender == owner); + _; // + } + + modifier employeeExist(address e) { // + var employee = employees[e]; + assert (employee.id != 0x0); + _; + } + + modifier indexNotUsed(address e) { + var employee = employees[e]; + assert (employee.id == 0x0); + _; + } + + function addEmployee(address e, uint s) onlyOwner indexNotUsed(e) returns (address) { + totalSalary = totalSalary.add (s.mul(1 ether)); //ETHER TO total + employees[e] = Employee(e, s.mul(1 ether), now); + return e; + } + + function _partialPaid (Employee e) private { + uint payment = e.salary.mul(now.sub(e.payDay)).div(payDuration); //ONLY one persons salary + e.payDay = now; + e.id.transfer(payment); + } + + function checkSumSalary (address e) returns (uint){ + var employee = employees[e]; + uint payment = employee.salary.mul(now.sub(employee.payDay)).div(payDuration); //all employee's salary + return payment; + } + + function removeEmployee(address e) onlyOwner employeeExist(e) returns (address) { + var employee = employees[e]; + _partialPaid(employee); + totalSalary = totalSalary.sub (employee.salary); + delete employees[e]; + return e; + } + + function checkEmployee (address e) returns (uint salary, address id, uint payDay) { + var employee = employees[e]; + return (employee.salary, employee.id, employee.payDay); + } + + + function addFund() payable returns (uint) { + return this.balance; + } + + function calculateRunway() returns (uint) { //caculate how many months + assert (totalSalary != 0); // safe for div + return this.balance.div(totalSalary); + } + + function hasEnoughFund() returns (bool) { + return this.calculateRunway() > 0; + } + + function updateEmployeeSalary (address e, uint s) onlyOwner employeeExist(e) { + var employee = employees[e]; + _partialPaid (employee); + + totalSalary = totalSalary.sub (employee.salary); + employees[e].salary = s.mul(1 ether); + totalSalary = totalSalary.add(s.mul(1 ether)); + employees[e].payDay = now; + } + + // change employee id + function changeId (address eid, address payment_addr) onlyOwner employeeExist(eid) { + var employee = employees[eid]; + _partialPaid (employee); + employees[eid].id = payment_addr; + } + + //Change employee index and id + function changePaymentAddress (address e_old, address e_new) onlyOwner employeeExist(e_old) indexNotUsed(e_new){ + var employee_old = employees[e_old]; + _partialPaid (employee_old); + employees[e_new] = Employee(e_new, employee_old.salary.mul(1 ether), now); + delete employees[e_old]; + } + + function getPaid() employeeExist(msg.sender) { + var employee = employees[msg.sender]; + uint nextPayDay = employee.payDay.add(payDuration); + assert (nextPayDay < now); + employees[msg.sender].payDay = nextPayDay; + employee.id.transfer(employee.salary); + } + }