From ceacee8e6cdf6d35ee739b679146276a2afda856 Mon Sep 17 00:00:00 2001
From: Vincent Vandemeulebrouck <vincent.vandeme@gmail.com>
Date: Tue, 3 Jan 2023 01:37:50 +0100
Subject: [PATCH] Les commerces peuvent appliquer un pourcentage

---
 module/actor/commerce-sheet.js                |  3 ++-
 module/actor/commerce.js                      |  5 +++++
 module/dialog-item-vente.js                   |  6 +++---
 module/item-service.js                        |  2 +-
 module/item.js                                |  9 ++++++++-
 module/rdd-utility.js                         |  2 ++
 template.json                                 |  1 +
 templates/actor/commerce-actor-sheet.html     | 10 +++++++++-
 templates/actor/commerce-inventaire-item.html | 20 +++++++++++++------
 9 files changed, 45 insertions(+), 13 deletions(-)

diff --git a/module/actor/commerce-sheet.js b/module/actor/commerce-sheet.js
index 6df94ed5..0aa89a2a 100644
--- a/module/actor/commerce-sheet.js
+++ b/module/actor/commerce-sheet.js
@@ -3,6 +3,7 @@ import { RdDItem } from "../item.js";
 import { RdDSheetUtility } from "../rdd-sheet-utility.js";
 import { RdDUtility } from "../rdd-utility.js";
 import { RdDBaseActorSheet } from "./base-actor-sheet.js";
+import { RdDCommerce } from "./commerce.js";
 
 /**
  * Extend the basic ActorSheet with some very simple modifications
@@ -67,7 +68,7 @@ export class RdDCommerceSheet extends RdDBaseActorSheet {
       quantiteIllimite: disponible == undefined,
       nbLots: disponible ?? 1,
       tailleLot: 1,
-      prixLot: item.system.cout
+      prixLot: item.calculerPrixCommercant()
     });
   }
 }
diff --git a/module/actor/commerce.js b/module/actor/commerce.js
index c29b318d..2735ac5e 100644
--- a/module/actor/commerce.js
+++ b/module/actor/commerce.js
@@ -1,3 +1,4 @@
+import { Misc } from "../misc.js";
 import { RdDBaseActor } from "./base-actor.js";
 
 export class RdDCommerce extends RdDBaseActor {
@@ -45,4 +46,8 @@ export class RdDCommerce extends RdDBaseActor {
     await super.decrementerQuantiteItem(itemVendu, quantite, {supprimerSiZero: false});
   }
 
+  calculerPrix(item) {
+    const pourcentage = this.system.pourcentage ?? 100;
+    return Misc.keepDecimals(Math.ceil(item.system.cout * pourcentage)/100, 2);
+  }
 }
\ No newline at end of file
diff --git a/module/dialog-item-vente.js b/module/dialog-item-vente.js
index a027ef61..78698d2a 100644
--- a/module/dialog-item-vente.js
+++ b/module/dialog-item-vente.js
@@ -9,9 +9,9 @@ export class DialogItemVente extends Dialog {
       item: item,
       alias: item.actor?.name ?? game.user.name,
       vendeurId: item.actor?.id ,
-      prixOrigine: item.system.cout,
-      prixUnitaire: item.system.cout,
-      prixLot: item.system.cout,
+      prixOrigine: item.calculerPrixCommercant(),
+      prixUnitaire: item.calculerPrixCommercant(),
+      prixLot: item.calculerPrixCommercant(),
       tailleLot: 1,
       quantiteNbLots: quantite,
       quantiteMaxLots: quantite,
diff --git a/module/item-service.js b/module/item-service.js
index 6d116dfa..95e192b7 100644
--- a/module/item-service.js
+++ b/module/item-service.js
@@ -11,7 +11,7 @@ export class RdDItemService extends RdDItem {
     return [
       RdDItem.propertyIfDefined('Qualité', this.system.qualite, this.system.qualite != 0),
       RdDItem.propertyIfDefined('Moral', 'Situation heureuse', this.system.moral),
-      RdDItem.propertyIfDefined('Coût', `${this.system.cout} sols`),
+      RdDItem.propertyIfDefined('Coût', `${this.calculerPrixCommercant()} sols`),
     ];
   }
 }
\ No newline at end of file
diff --git a/module/item.js b/module/item.js
index ac7ed712..5e9944bc 100644
--- a/module/item.js
+++ b/module/item.js
@@ -171,7 +171,6 @@ export class RdDItem extends Item {
     return typesObjetsConnaissance.includes(this.type)
   }
 
-
   getItemGroup() {
     if (this.isInventaire()) return "equipement";
     if (this.isOeuvre()) return "oeuvre";
@@ -258,6 +257,14 @@ export class RdDItem extends Item {
     return this.system.cout ?? 0
   }
 
+  calculerPrixCommercant() {
+    if (this.parent?.type == 'commerce') {
+      // appliquer le pourcentage
+      return this.parent.calculerPrix(this);
+    }
+    return this.system.cout;
+  }
+
   prepareDerivedData() {
     super.prepareDerivedData();
     if (this.isInventaire()) {
diff --git a/module/rdd-utility.js b/module/rdd-utility.js
index 0e739fb7..be4091d0 100644
--- a/module/rdd-utility.js
+++ b/module/rdd-utility.js
@@ -16,6 +16,7 @@ import { RdDCalendrier } from "./rdd-calendrier.js";
 import { Environnement } from "./environnement.js";
 import { RdDItemCompetence } from "./item-competence.js";
 import { RdDResolutionTable } from "./rdd-resolution-table.js";
+import { RdDCommerce } from "./actor/commerce.js";
 
 /* -------------------------------------------- */
 // This table starts at 0 -> niveau -10
@@ -274,6 +275,7 @@ export class RdDUtility {
     Handlebars.registerHelper('accord', (genre, ...args) => Grammar.accord(genre, args));
     Handlebars.registerHelper('buildConteneur', (objet, templateItem, options) => { return new Handlebars.SafeString(RdDUtility.buildConteneur(objet, 1, templateItem, options)); });
     Handlebars.registerHelper('buildContenu', (objet) => { return new Handlebars.SafeString(RdDUtility.buildContenu(objet, 1, true)); });
+    Handlebars.registerHelper('calculerPrixCommercant', item => item.calculerPrixCommercant());
     Handlebars.registerHelper('caseTmr-label', coord => TMRUtility.getTMRLabel(coord));
     Handlebars.registerHelper('caseTmr-type', coord => TMRUtility.getTMRType(coord));
     Handlebars.registerHelper('typeTmr-name', type => TMRUtility.typeTmrName(type));
diff --git a/template.json b/template.json
index ed26bd06..7abda35a 100644
--- a/template.json
+++ b/template.json
@@ -557,6 +557,7 @@
     },
     "commerce":{
       "templates": [ "description" ],
+      "pourcentage": 100,
       "illimite": false
     }
   },
diff --git a/templates/actor/commerce-actor-sheet.html b/templates/actor/commerce-actor-sheet.html
index 1f80cfbd..4792bbe7 100644
--- a/templates/actor/commerce-actor-sheet.html
+++ b/templates/actor/commerce-actor-sheet.html
@@ -10,7 +10,15 @@
           {{#if @root.options.isObserver}}
           <div class="form-group">
             <input {{@root.disabled}} class="attribute-value" type="checkbox" name="system.illimite" {{#if system.illimite}}checked{{/if}}/>
-            <span for="system.illimite">Quantité illimitée en vente</span>
+            <label for="system.illimite">Quantité illimitée en vente</label>
+          </div>
+          <div class="form-group">
+            <span>
+              <label for="system.pourcentage">Appliquer un pourcentage sur les prix</label>
+              <input {{@root.disabled}} class="attribute-value" type="number" data-dtype="Number" 
+                name="system.pourcentage" value="{{system.pourcentage}}"
+                min="20" max="500" step="5"/>
+            </span>
           </div>
           {{/if}}
         </div>
diff --git a/templates/actor/commerce-inventaire-item.html b/templates/actor/commerce-inventaire-item.html
index ce23067e..c25239ac 100644
--- a/templates/actor/commerce-inventaire-item.html
+++ b/templates/actor/commerce-inventaire-item.html
@@ -1,7 +1,7 @@
 {{#if (ne item.type 'monnaie')}}
 <li class="item flexrow list-item" data-item-id="{{item._id}}" draggable="true">
   <span class="equipement-nom {{#if (eq item.type 'conteneur')}}conteneur-name{{/if}} ">
-    <a{{#if (ne item.type 'conteneur')}} class="item-edit"{{/if}} >
+    <a{{#if (and (ne item.type 'conteneur') options.isObserver)}} class="item-edit"{{/if}} >
     {{#if (eq item.type 'conteneur')}}
       <i class="{{~#if vide}}far fa-square
       {{else if ouvert}}far fa-minus-square
@@ -16,11 +16,13 @@
   {{#unless item.parent.system.illimite}}
   <span class="equipement-detail flexrow">
     {{#unless (or (eq item.type 'service') (and (eq item.type 'conteneur') (not vide)))}}
-      {{#if @root.options.isOwner}}
+      {{#if options.isOwner}}
       <a class="item-quantite-moins"><i class="fas fa-minus-square"></i></a>
       {{/if}}
-      <input {{#unless @root.options.isOwner}}disabled{{/unless}} type="number" class="item-quantite" name="items[{{key}}].system.quantite" value="{{item.system.quantite}}" data-dtype="Number" />
-      {{#if @root.options.isOwner}}
+      <input {{#unless options.isOwner}}disabled{{/unless}} type="number" data-dtype="Number" 
+        class="item-quantite" name="items[{{key}}].system.quantite"
+        value="{{item.system.quantite}}" />
+      {{#if options.isOwner}}
       <a class="item-quantite-plus"><i class="fas fa-plus-square"></i></a>
       {{/if}}
     {{/unless}}
@@ -28,12 +30,18 @@
   {{/unless}}
   <span class="equipement-detail">
     {{#unless (and (eq item.type 'conteneur') (not vide))}}
-    <input {{#unless @root.options.isOwner}}disabled{{/unless}} type="number" class="input-prix number-x3 item-cout" name="items[{{key}}].system.cout" value="{{numberFormat item.system.cout decimals=2 sign=false}}" data-dtype="Number" />
+    <input {{#unless options.isOwner}}disabled{{/unless}} type="number" data-dtype="Number" 
+      class="input-prix number-x3 item-cout" name="items[{{key}}].system.cout"
+      {{#if options.isObserver}}
+      value="{{numberFormat item.system.cout decimals=2 sign=false}}"
+      {{else}}
+      value="{{numberFormat (calculerPrixCommercant item) decimals=2 sign=false}}"
+      {{/if}} />
     {{/unless}}
   </span>
   <span class="equipement-actions item-controls">
     {{#unless (and (eq item.type 'conteneur') (not vide))}}
-    {{#if @root.options.isOwner}}
+    {{#if options.isOwner}}
     <a class="item-edit" title="Editer"><i class="fas fa-edit"></i></a>
     <a class="item-delete" title="Supprimer"><i class="fas fa-trash"></i></a>
     {{#if (or item.parent.system.illimite (ne item.system.quantite 0))}}