Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Presto: Crop Receipt #1284

Merged
merged 4 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 157 additions & 0 deletions pallets/presto/crop_receipt_template.json

Large diffs are not rendered by default.

145 changes: 143 additions & 2 deletions pallets/presto/src/crop_receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,150 @@
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use crate::{Config, Error, MomentOf};

use codec::{Decode, Encode, MaxEncodedLen};
use common::{AccountIdOf, Balance, BoundedString};
use derivative::Derivative;
use frame_support::ensure;
use frame_support::traits::Time;
use sp_runtime::DispatchResult;

#[derive(
Debug, Clone, Copy, PartialEq, Eq, Encode, Decode, scale_info::TypeInfo, MaxEncodedLen,
)]
pub enum Status {
Rating,
Decision,
Declined,
Published,
}

#[allow(clippy::upper_case_acronyms)]
#[derive(
Debug, Clone, Copy, PartialEq, Eq, Encode, Decode, scale_info::TypeInfo, MaxEncodedLen,
)]
pub enum Rating {
AAA,
AA,
A,
BBB,
BB,
B,
CCC,
CC,
C,
D,
NR,
}

#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, scale_info::TypeInfo, MaxEncodedLen)]
pub struct CropReceipt {
// TODO
#[scale_info(skip_type_params(T))]
pub struct Score<T: Config> {
pub rating: Rating,
pub by_auditor: AccountIdOf<T>,
}

#[derive(Debug, Encode, Decode, scale_info::TypeInfo, MaxEncodedLen, Derivative)]
#[derivative(Clone, PartialEq, Eq)]
#[scale_info(skip_type_params(T))]
pub struct CropReceiptContent<T: Config> {
pub json: BoundedString<T::MaxCropReceiptContentSize>,
}

#[derive(Debug, Encode, Decode, scale_info::TypeInfo, MaxEncodedLen, Derivative)]
#[derivative(Clone, PartialEq, Eq)]
#[scale_info(skip_type_params(T))]
pub struct CropReceipt<T: Config> {
pub owner: AccountIdOf<T>,
pub time: MomentOf<T>,
pub status: Status,
pub amount: Balance,
pub score: Option<Score<T>>,
pub close_initial_period: MomentOf<T>,
pub date_of_issue: MomentOf<T>,
pub place_of_issue: BoundedString<T::MaxPlaceOfIssueSize>,
pub debtor: BoundedString<T::MaxDebtorSize>,
pub creditor: BoundedString<T::MaxCreditorSize>,
pub perfomance_time: MomentOf<T>,
}

impl<T: Config> CropReceipt<T> {
#[allow(clippy::too_many_arguments)]
pub fn new(
owner: AccountIdOf<T>,
amount: Balance,
close_initial_period: MomentOf<T>,
date_of_issue: MomentOf<T>,
place_of_issue: BoundedString<T::MaxPlaceOfIssueSize>,
debtor: BoundedString<T::MaxDebtorSize>,
creditor: BoundedString<T::MaxCreditorSize>,
perfomance_time: MomentOf<T>,
) -> Self {
let time = T::Time::now();

Self {
owner,
time,
status: Status::Rating,
amount,
score: None,
close_initial_period,
date_of_issue,
place_of_issue,
debtor,
creditor,
perfomance_time,
}
}

pub fn ensure_is_owner(&self, who: &AccountIdOf<T>) -> DispatchResult {
ensure!(&self.owner == who, Error::<T>::CallerIsNotCropReceiptOwner);
Ok(())
}

pub fn rate(&mut self, rating: Rating, auditor: AccountIdOf<T>) -> DispatchResult {
ensure!(
self.status == Status::Rating,
Error::<T>::CropReceiptAlreadyRated
);

self.score = Some(Score {
rating,
by_auditor: auditor,
});
self.status = Status::Decision;

Ok(())
}

pub fn decline(&mut self) -> DispatchResult {
if self.status == Status::Rating {
return Err(Error::<T>::CropReceiptWaitingForRate.into());
}

ensure!(
self.status == Status::Decision,
Error::<T>::CropReceiptAlreadyHasDecision
);

self.status = Status::Declined;

Ok(())
}

pub fn publish(&mut self) -> DispatchResult {
if self.status == Status::Rating {
return Err(Error::<T>::CropReceiptWaitingForRate.into());
}

ensure!(
self.status == Status::Decision,
Error::<T>::CropReceiptAlreadyHasDecision
);

self.status = Status::Published;

Ok(())
}
}
Loading