You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

318 lines
7.8 KiB

  1. using Std;
  2. import Common;
  3. import haxe.Utf8;
  4. import tools.ArrayTool;
  5. class View extends sugoi.BaseView {
  6. var t : sugoi.i18n.GetText;
  7. public function new() {
  8. super();
  9. this.Std = Std;
  10. this.Date = Date;
  11. this.Web = sugoi.Web;
  12. this.Lambda = Lambda;
  13. this.VERSION = App.VERSION.toString();
  14. this.ArrayTool = ArrayTool;
  15. this.t = sugoi.i18n.Locale.texts;
  16. }
  17. public function count(i) {
  18. return Lambda.count(i);
  19. }
  20. public function abs(n){
  21. return Math.abs(n);
  22. }
  23. /**
  24. * init view in main loop, just before rendering
  25. */
  26. override function init() {
  27. super.init();
  28. //tuto widget display
  29. var u = App.current.user;
  30. if (u!=null && u.tutoState!=null) {
  31. //trace("view init "+u.tutoState.name+" , "+u.tutoState.step);
  32. this.displayTuto(u.tutoState.name, u.tutoState.step);
  33. }
  34. }
  35. function getCurrentGroup(){
  36. return App.current.getCurrentGroup();
  37. }
  38. function getUser(uid:Int):db.User {
  39. return db.User.manager.get(uid, false);
  40. }
  41. function getProduct (pid:Int){
  42. return db.Product.manager.get(pid, false);
  43. }
  44. /**
  45. * Round a number to r digits after coma.
  46. *
  47. * @param n
  48. * @param r
  49. */
  50. public function roundTo(n:Float, r:Int):Float {
  51. return Math.round(n * Math.pow(10,r)) / Math.pow(10,r) ;
  52. }
  53. public function color(id:Int) {
  54. if (id == null) throw "color cant be null";
  55. //try{
  56. return intToHex(db.CategoryGroup.COLORS[id]);
  57. //}catch (e:Dynamic) return "#000000";
  58. }
  59. /**
  60. * convert a RVB color from Int to Hexa
  61. * @param c
  62. * @param leadingZeros=6
  63. */
  64. public function intToHex(c:Int, ?leadingZeros=6):String {
  65. var h = StringTools.hex(c);
  66. while (h.length<leadingZeros)
  67. h="0"+h;
  68. return "#"+h;
  69. }
  70. /**
  71. * Format prices
  72. */
  73. public function formatNum(n:Float):String {
  74. if (n == null) return "";
  75. //round with 2 digits after comma
  76. var out = Std.string(roundTo(n, 2));
  77. //add a zero, 1,8-->1,80
  78. if (out.indexOf(".")!=-1 && out.split(".")[1].length == 1) out = out +"0";
  79. //french : replace point by comma
  80. return out.split(".").join(",");
  81. }
  82. /**
  83. * Price per Kg/Liter...
  84. * @param qt
  85. * @param unit
  86. */
  87. public function pricePerUnit(price:Float,qt:Float, unit:Unit){
  88. if (unit==null || qt == null || qt == 0 || price==null || price==0) return "";
  89. var _price = price / qt;
  90. var _unit = unit;
  91. //turn small prices in Kg
  92. if (_price < 1 ){
  93. switch(unit){
  94. case Gram:
  95. _price *= 1000;
  96. _unit = Kilogram;
  97. case Centilitre:
  98. _price *= 100;
  99. _unit = Litre;
  100. default :
  101. }
  102. }
  103. return formatNum(_price) + "&nbsp;" + currency() + "/" + this.unit(_unit);
  104. }
  105. /**
  106. * clean numbers in views
  107. * to avoid bugs like : 13.79 - 13.79 = 1.77635683940025e-15
  108. */
  109. public function numClean(f:Float):Float{
  110. return Math.round(f * 100) / 100;
  111. }
  112. /**
  113. * max length for strings, usefull for tables
  114. */
  115. public function short(text:String, length:Int){
  116. if (Utf8.length(text) > length){
  117. return Utf8.sub(text,0, length)+"";
  118. }else{
  119. return text;
  120. }
  121. }
  122. public function isToday(d:Date) {
  123. var n = Date.now();
  124. return d.getDate() == n.getDate() && d.getMonth() == n.getMonth() && d.getFullYear() == n.getFullYear();
  125. }
  126. /**
  127. * Prints a measuring unit
  128. */
  129. public function unit(u:Unit,?plural=false){
  130. t = sugoi.i18n.Locale.texts;
  131. return switch(u){
  132. case Kilogram: t._("Kg.||kilogramms");
  133. case Gram: t._("g.||gramms");
  134. case null,Piece: if(plural) t._("pieces||unit of a product)") else t._("piece||unit of a product)");
  135. case Litre: t._("L.||liter");
  136. case Centilitre: t._("cl.||centiliter");
  137. }
  138. }
  139. public function currency(){
  140. if (App.current.user == null || App.current.user.amap == null){
  141. return "";
  142. }else{
  143. return App.current.user.amap.getCurrency();
  144. }
  145. }
  146. public static var DAYS = null;
  147. public static var MONTHS = null;
  148. public static var HOURS = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23];
  149. public static var MINUTES = [0,5,10,15,20,25,30,35,40,45,50,55];
  150. public function initDate(){
  151. t = sugoi.i18n.Locale.texts;
  152. DAYS = [t._("Sunday"), t._("Monday"), t._("Tuesday"), t._("Wednesday"), t._("Thursday"), t._("Friday"), t._("Saturday")];
  153. MONTHS = [t._("January"), t._("February"), t._("March"), t._("April"), t._("May"), t._("June"), t._("July"), t._("August"), t._("September"), t._("October"), t._("November"), t._("December")];
  154. this.DAYS = DAYS;
  155. this.MONTHS = MONTHS;
  156. this.HOURS = HOURS;
  157. this.MINUTES = MINUTES;
  158. }
  159. /**
  160. * human readable date + time
  161. */
  162. public function hDate(date:Date):String {
  163. if (date == null) return t._("no date set");
  164. if (DAYS == null) initDate();
  165. var out = DAYS[date.getDay()] + " " + date.getDate() + " " + MONTHS[date.getMonth()];
  166. out += " " + date.getFullYear();
  167. if ( date.getHours() != 0 || date.getMinutes() != 0){
  168. out += " " + sugoi.i18n.Locale.texts._("at||time : at 12:30") + " " + StringTools.lpad(Std.string(date.getHours()), "0", 2) + ":" + StringTools.lpad(Std.string(date.getMinutes()), "0", 2);
  169. }
  170. return out;
  171. }
  172. /**
  173. * Human readable hour
  174. */
  175. public function hHour(date:Date){
  176. return StringTools.lpad(date.getHours().string(), "0", 2) + ":" + StringTools.lpad(date.getMinutes().string(), "0", 2);
  177. }
  178. public function oHour(hour:Int,min:Int){
  179. return StringTools.lpad(hour.string(), "0", 2) + ":" + StringTools.lpad(min.string(), "0", 2);
  180. }
  181. /**
  182. * human readable date
  183. */
  184. public function dDate(date:Date):String {
  185. if (date == null) return t._("no date set");
  186. if (DAYS == null) initDate();
  187. return DAYS[date.getDay()] + " " + date.getDate() + " " + MONTHS[date.getMonth()] + " " + date.getFullYear();
  188. }
  189. public function getDate(date:Date) {
  190. if (date == null) throw "date is null";
  191. if (DAYS == null) initDate();
  192. return {
  193. dow: DAYS[date.getDay()],
  194. d : date.getDate(),
  195. m: MONTHS[date.getMonth()],
  196. y: date.getFullYear(),
  197. h: StringTools.lpad(Std.string(date.getHours()),"0",2),
  198. i: StringTools.lpad(Std.string(date.getMinutes()),"0",2)
  199. };
  200. }
  201. public function getProductImage(e):String {
  202. return Std.string(e).substr(2).toLowerCase()+".png";
  203. }
  204. public function prepare(orders:Iterable<db.UserContract>){
  205. return service.OrderService.prepare(orders);
  206. }
  207. public function displayTuto(tuto:String, step:Int) {
  208. if (tuto == null) return;
  209. var t = plugin.Tutorial.all().get(tuto);
  210. //check if we are on the correct page (last step page)
  211. //otherwise the popovers could be displayed on wrong elements
  212. var previous = t.steps[step - 1];
  213. if (previous != null) {
  214. switch(previous.action) {
  215. case TAPage(uri):
  216. var here = sugoi.Web.getURI();
  217. if (!plugin.Tutorial.match(uri,here)) {
  218. return;
  219. }
  220. default:
  221. }
  222. }
  223. this.tuto = { name:tuto, step:step };
  224. }
  225. /**
  226. * renvoie 0 si c'est user.firstName qui est connecté,
  227. * renvoie 1 si c'est user.firstName2 qui est connecté
  228. * @return
  229. */
  230. public function whichUser():Int {
  231. if (App.current.session.data == null) return 0;
  232. return App.current.session.data.whichUser == null?0:App.current.session.data.whichUser;
  233. }
  234. public function isAmap(){
  235. return App.current.user.amap.groupType == db.Amap.GroupType.Amap;
  236. }
  237. public function getBasket(userId, placeId, date){
  238. var user = getUser(userId);
  239. var place = db.Place.manager.get(placeId, false);
  240. return db.Basket.getOrCreate(user, place, date);
  241. }
  242. public function getPlatform(){
  243. return #if neko "Neko" #else "PHP" #end ;
  244. }
  245. /**
  246. * Smart quantity (tm) : displays human readable quantity
  247. * 0.33 x Lemon 12kg => 2kg Lemon
  248. */
  249. public function smartQt(orderQt:Float, productQt:Float, unit:Unit):String{
  250. if (orderQt == null) orderQt = 1;
  251. if (productQt == null) productQt = 1;
  252. if (unit == null) unit = Unit.Piece;
  253. if (unit == Unit.Piece && productQt == 1 ){
  254. return this.formatNum(orderQt);
  255. }else{
  256. return this.formatNum(orderQt * productQt) + "&nbsp;" + this.unit(unit,orderQt*productQt>1);
  257. }
  258. }
  259. }