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.

178 lines
5.4 KiB

  1. package react.order;
  2. import react.ReactDOM;
  3. import react.ReactComponent;
  4. import react.ReactMacro.jsx;
  5. import Common;
  6. import react.product.Product;
  7. /**
  8. * A User order
  9. * @author fbarbut
  10. */
  11. class Order extends react.ReactComponentOfPropsAndState<{order:UserOrder,onUpdate:UserOrder->Void,parentBox:react.order.OrderBox},{order:UserOrder,inputValue:String}>
  12. {
  13. var hasPayments :Bool;
  14. var currency : String;
  15. public function new(props)
  16. {
  17. super(props);
  18. state = {order:props.order,inputValue:null};
  19. hasPayments = props.parentBox.props.hasPayments;
  20. currency = props.parentBox.props.currency;
  21. if (state.order.productUnit == null) state.order.productUnit = Piece;
  22. if (state.order.productQt == null) state.order.productQt = 1;
  23. state.inputValue = if ( isSmartQtInput(state.order) ){
  24. Std.string(round(state.order.quantity * state.order.productQt));
  25. }else{
  26. Std.string(state.order.quantity);
  27. }
  28. }
  29. override public function render(){
  30. var o = state.order;
  31. /*var unit = if (o.productHasFloatQt || o.productHasVariablePrice){
  32. jsx('<div className="col-md-1">${Formatting.unit(o.productUnit)}</div>');
  33. }else{
  34. jsx('<div className="col-md-1"></div>');
  35. }*/
  36. /*
  37. //use smart qt only if hasFloatQt
  38. var productName = if (o.productHasFloatQt || o.productHasVariablePrice){
  39. jsx('<div className="col-md-3">${o.productName}</div>');
  40. }else{
  41. jsx('<div className="col-md-3">${o.productName} ${o.productQt} ${Formatting.unit(o.productUnit)}</div>');
  42. }
  43. */
  44. /*var productName = if (o.productHasFloatQt || o.productHasVariablePrice){
  45. jsx('<div className="col-md-3">${o.productName}</div>');*/
  46. var input = if (isSmartQtInput(o)){
  47. jsx('<div className="input-group">
  48. <input type="text" className="form-control input-sm text-right" value="${state.inputValue}" onChange=${onChange} onKeyPress=${onKeyPress}/>
  49. <div className="input-group-addon">${Formatting.unit(o.productUnit)}</div>
  50. </div>');
  51. }else{
  52. jsx('<div className="input-group">
  53. <input type="text" className="form-control input-sm text-right" value="${state.inputValue}" onChange=${onChange} onKeyPress=${onKeyPress}/>
  54. </div>');
  55. }
  56. var alternated = if(props.parentBox.props.contractType==0 && props.parentBox.state.users!=null){
  57. //constant orders
  58. var options = props.parentBox.state.users.map(function(x) return jsx('<option key=${x.id} value=${x.id}>${x.name}</option>') );
  59. var checkbox = if(o.invertSharedOrder){
  60. jsx('<input data-toggle="tooltip" title="Inverser l\'alternance" checked="checked" type="checkbox" value="1" onChange=$onChangeInvert />');
  61. }else{
  62. jsx('<input data-toggle="tooltip" title="Inverser l\'alternance" type="checkbox" value="1" onChange=$onChangeInvert />');
  63. }
  64. jsx('<div>
  65. <select className="form-control input-sm" style=${{width:"150px",display:"inline-block"}} onChange=${onChangeUser2} value=${o.userId2}>
  66. <option value="0">-</option>
  67. $options
  68. </select>
  69. $checkbox
  70. </div>');
  71. }else{
  72. null;
  73. }
  74. return jsx('<div className="productOrder row">
  75. <div className="col-md-4">
  76. <$Product productInfo=${o.product} />
  77. </div>
  78. <div className="col-md-1 ref">
  79. ${o.productRef}
  80. </div>
  81. <div className="col-md-1">
  82. ${round(o.quantity * o.productPrice)}&nbsp;${currency}
  83. </div>
  84. <div className="col-md-2">
  85. $input
  86. ${makeInfos()}
  87. </div>
  88. ${paidInput()}
  89. <div className="col-md-3">$alternated</div>
  90. </div>');
  91. }
  92. function round(f){
  93. return Formatting.formatNum(f);
  94. }
  95. function paidInput(){
  96. if(hasPayments) return null;
  97. if(state.order.paid){
  98. return jsx('<div className="col-md-1"><input type="checkbox" name="paid" value="1" checked="checked" onChange=${onChangePaid} /></div>');
  99. }else{
  100. return jsx('<div className="col-md-1"><input type="checkbox" name="paid" value="1" onChange=${onChangePaid} /></div>');
  101. }
  102. }
  103. function makeInfos(){
  104. var o = state.order;
  105. return if ( isSmartQtInput(o) ){
  106. jsx('<div className="infos">
  107. <b> ${round(o.quantity)} </b> x <b>${o.productQt} ${Formatting.unit(o.productUnit)}</b > ${o.productName}
  108. </div>');
  109. }else{
  110. null;
  111. }
  112. }
  113. function isSmartQtInput(o:UserOrder):Bool{
  114. return o.product.hasFloatQt || o.product.variablePrice || o.product.wholesale;
  115. }
  116. function onChange(e:js.html.Event){
  117. e.preventDefault();
  118. var value :String = untyped (e.target.value == "") ? "0" : e.target.value;
  119. state.inputValue = value;
  120. var v = Formatting.parseFloat(value);
  121. var o = state.order;
  122. if ( isSmartQtInput(o) ){
  123. //the value is a smart qt, so we need re-compute the quantity
  124. o.quantity = v / o.productQt;
  125. }else{
  126. o.quantity = v;
  127. }
  128. this.setState(state);
  129. if (props.onUpdate != null) props.onUpdate(state.order);
  130. }
  131. function onChangePaid(e:js.html.Event){
  132. state.order.paid = untyped e.target.checked;
  133. this.setState(state);
  134. if (props.onUpdate != null) props.onUpdate(state.order);
  135. }
  136. function onChangeInvert(e:js.html.Event){
  137. state.order.invertSharedOrder = untyped e.target.checked;
  138. this.setState(state);
  139. if (props.onUpdate != null) props.onUpdate(state.order);
  140. }
  141. function onChangeUser2(e:js.html.Event){
  142. var v = Std.parseInt(untyped e.target.value);
  143. state.order.userId2 = v==0 ? null : v;
  144. this.setState(state);
  145. if (props.onUpdate != null) props.onUpdate(state.order);
  146. }
  147. function onKeyPress(event:js.html.KeyboardEvent){
  148. /*if(event.key == 'Enter'){
  149. trace('enter !');
  150. }*/
  151. }
  152. }