project_property_decimal_view.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /**
  2. * Created by Zhong on 2017/11/13.
  3. */
  4. //default setting
  5. let defaultDecimal = {
  6. min: 0,
  7. max: 6,
  8. _def: {//editable: 开放给用户编辑的
  9. bills: {editable: true, data: {unitPrice: 2, totalPrice: 2}},
  10. ration: {editable: true, data: {quantity: 3, unitPrice: 2, totalPrice: 2}},
  11. glj: {editable: true, data: {quantity: 3, unitPrice: 2}},
  12. feeRate: {editable: true, data: 2},
  13. quantity_detail: {editable: false, data: 4},
  14. process: {editable: false, data: 6}
  15. }
  16. };
  17. let decimalObj = Object.create(null);
  18. decimalObj.decimal = function (field, node) {
  19. if(isDef(field)){
  20. if(field === 'feeRate'||field==="quantity_detail"){
  21. return this[field];
  22. }
  23. else if (field.sameText('unitFee')) field = 'unitPrice'
  24. else if (field.sameText('totalFee')) field = 'totalPrice';
  25. if(isDef(node)){
  26. if(node.sourceType === projectObj.project.Bills.getSourceType()){
  27. if(field === 'quantity'){
  28. return billsQuanDecimal.decimal(node.data.unit);
  29. }
  30. else {
  31. return returnV(this['bills'][field], this.process);
  32. }
  33. }
  34. else if(node.sourceType === projectObj.project.Ration.getSourceType()){
  35. return returnV(this['ration'][field], this.process);
  36. }
  37. else if(node.sourceType === projectObj.project.GLJ.getSourceType()){
  38. return returnV(this['glj'][field], this.process);
  39. }
  40. }
  41. }
  42. return this.process;
  43. };
  44. function returnV(v, r){
  45. if(isDef(v)){
  46. return v;
  47. }
  48. return r;
  49. }
  50. function isUndef(v) {
  51. return v === undefined || v === null;
  52. }
  53. function isDef(v){
  54. return v !== undefined && v !== null;
  55. }
  56. function isObj(v){
  57. return isDef(v) && typeof v === 'object';
  58. }
  59. function isNum(v){
  60. return isDef(v) && !isNaN(v);
  61. }
  62. function isInt(v){
  63. return isNum(v) && v % 1 === 0;
  64. }
  65. function isValidDigit(v){
  66. return isInt(v) && v >= defaultDecimal.min && v <= defaultDecimal.max;
  67. }
  68. //newV用户可编辑数据
  69. function toUpdateDecimal(orgV, newV){
  70. let rst = false;
  71. reCompare(orgV, newV);
  72. function reCompare(orgV, newV){
  73. for(let attr in newV){
  74. if(Object.keys(newV[attr]).length > 0){
  75. if(isUndef(orgV[attr])){
  76. rst = true;
  77. return;
  78. }
  79. else{
  80. reCompare(orgV[attr], newV[attr]);
  81. }
  82. }
  83. else {
  84. if(isUndef(orgV[attr]) || parseInt(newV[attr]) !== parseInt(orgV[attr])){
  85. rst = true;
  86. return;
  87. }
  88. }
  89. }
  90. }
  91. return rst;
  92. }
  93. function setDecimal(_digits, data){
  94. if(isDef(data)){
  95. for(let attr in data){
  96. _digits[attr] = data[attr] || defaultDecimal['_def'][attr]['data'];
  97. }
  98. }
  99. else {
  100. for(let attr in defaultDecimal['_def']){
  101. _digits[attr] = defaultDecimal['_def'][attr]['data'];
  102. }
  103. }
  104. }
  105. //获取decimalPanel中要展示的数据
  106. function m_getInitData(data){
  107. let rst = Object.create(null);
  108. for(let attr in defaultDecimal['_def']){
  109. if(defaultDecimal['_def'][attr]['editable'] && isDef(data[attr])){
  110. rst[attr] = data[attr];
  111. }
  112. }
  113. return rst;
  114. }
  115. //获取小数位数panel里的数据
  116. function m_getDecimalData(inputs){
  117. let rst = Object.create(null);
  118. for(let i = 0, len = inputs.length ; i < len; i++){
  119. let name = $(inputs[i]).attr('name');
  120. let attrs = name.split('-');
  121. if(attrs.length > 1){
  122. if(isUndef(rst[attrs[0]])){
  123. rst[attrs[0]] = Object.create(null);
  124. }
  125. if(isDef(rst[attrs[0]])) {
  126. rst[attrs[0]][attrs[1]] = parseInt($(inputs[i]).val());
  127. }
  128. }
  129. else {
  130. rst[attrs[0]] = parseInt($(inputs[i]).val());
  131. }
  132. }
  133. for(let attr in defaultDecimal['_def']){
  134. if(!defaultDecimal['_def'][attr]['editable']){
  135. rst[attr] = defaultDecimal['_def'][attr]['data'];
  136. }
  137. }
  138. return rst;
  139. }
  140. function v_initPanel(data){
  141. if(this.isDef(data)){
  142. for(let attr in data){
  143. if(this.isObj(data[attr])){
  144. for(let subAttr in data[attr]){
  145. let str = attr + '-' + subAttr;
  146. let jqs = 'input[name="' + str + '"]';
  147. $(jqs).val(data[attr][subAttr]);
  148. $(jqs).attr('min', defaultDecimal.min);
  149. $(jqs).attr('max', defaultDecimal.max);
  150. }
  151. }
  152. else {
  153. let str = attr + '';
  154. let jqs = 'input[name="' + str + '"]';
  155. $(jqs).val(data[attr]);
  156. $(jqs).attr('min', defaultDecimal.min);
  157. $(jqs).attr('max', defaultDecimal.max);
  158. }
  159. }
  160. }
  161. }
  162. function e_validIn(inputs){
  163. for(let i = 0, len = inputs.length; i < len; i++){
  164. let orgV = $(inputs[i]).val();
  165. $(inputs[i]).bind('input', function () {
  166. let v = $(this).val();
  167. let inputName = $(this).attr('name');
  168. if(v.trim().length === 0 || !isValidDigit(v)){
  169. alert('小数位数范围在0-6!');
  170. $(this).val(orgV);
  171. }
  172. else{
  173. orgV = v;
  174. //清单单价、合价 与定额单价、合价同步
  175. if(inputName === 'bills-unitPrice'){
  176. $("input[name='ration-unitPrice']").val(v);
  177. }
  178. else if(inputName === 'bills-totalPrice'){
  179. $("input[name='ration-totalPrice']").val(v);
  180. }
  181. else if(inputName === 'ration-unitPrice'){
  182. $("input[name='bills-unitPrice']").val(v);
  183. }
  184. else if(inputName === 'ration-totalPrice'){
  185. $("input[name='bills-totalPrice']").val(v);
  186. }
  187. }
  188. });
  189. }
  190. }
  191. function e_bindCof(btn){
  192. btn.bind('click', function () {
  193. //获取更新的数据
  194. let updateDecimal = m_getDecimalData($('input', '#poj-settings-decimal'));
  195. if(toUpdateDecimal(decimalObj, updateDecimal)){
  196. a_updateDigits(updateDecimal);
  197. }
  198. });
  199. }
  200. function e_unbindCof(btn){
  201. btn.unbind();
  202. }
  203. function a_updateDigits(updateDecimal){
  204. let url = '/pm/api/updateProjects';
  205. let data = {
  206. updateType: 'update',
  207. updateData: {
  208. ID: parseInt(scUrlUtil.GetQueryString('project')),
  209. 'property.decimal': updateDecimal
  210. }
  211. };
  212. let postData = {
  213. user_id: userID,
  214. updateData: [data]
  215. };
  216. let scCaller = function (resultData) {
  217. //update data
  218. setDecimal(decimalObj, updateDecimal);
  219. let v_data = m_getInitData(decimalObj);
  220. v_initPanel(v_data);
  221. };
  222. let errCaller = function () {
  223. alert('更新小数位数失败!');
  224. let v_data = m_getInitData(decimalObj);
  225. v_initPanel(v_data);
  226. };
  227. CommonAjax.post(url, postData, scCaller, errCaller);
  228. }
  229. $(document).ready(function () {
  230. $('#poj-set').on('shown.bs.modal', function (e) {
  231. let v_data = m_getInitData(decimalObj);
  232. v_initPanel(v_data);
  233. });
  234. //绑定确定按钮
  235. //e_bindCof($('#property_ok'));
  236. //绑定小数位数输入控制
  237. e_validIn($('input', '#poj-settings-decimal'));
  238. });