project_property_decimal_view.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. let len = Object.keys(newV).length;
  72. reCompare(orgV, newV);
  73. function reCompare(orgV, newV){
  74. for(let attr in newV){
  75. if(Object.keys(newV[attr]).length > 0){
  76. if(isUndef(orgV[attr])){
  77. rst = true;
  78. return;
  79. }
  80. else{
  81. reCompare(orgV[attr], newV[attr]);
  82. }
  83. }
  84. else {
  85. if(isUndef(orgV[attr]) || parseInt(newV[attr]) !== parseInt(orgV[attr])){
  86. rst = true;
  87. return;
  88. }
  89. }
  90. }
  91. }
  92. return rst;
  93. }
  94. function setDecimal(_digits, data){
  95. if(isDef(data)){
  96. for(let attr in data){
  97. _digits[attr] = data[attr] || defaultDecimal['_def'][attr]['data'];
  98. }
  99. }
  100. else {
  101. for(let attr in defaultDecimal['_def']){
  102. _digits[attr] = defaultDecimal['_def'][attr]['data'];
  103. }
  104. }
  105. }
  106. //获取decimalPanel中要展示的数据
  107. function m_getInitData(data){
  108. let rst = Object.create(null);
  109. for(let attr in defaultDecimal['_def']){
  110. if(defaultDecimal['_def'][attr]['editable'] && isDef(data[attr])){
  111. rst[attr] = data[attr];
  112. }
  113. }
  114. return rst;
  115. }
  116. //获取小数位数panel里的数据
  117. function m_getDecimalData(inputs){
  118. let rst = Object.create(null);
  119. for(let i = 0, len = inputs.length ; i < len; i++){
  120. let name = $(inputs[i]).attr('name');
  121. let attrs = name.split('-');
  122. if(attrs.length > 1){
  123. if(isUndef(rst[attrs[0]])){
  124. rst[attrs[0]] = Object.create(null);
  125. }
  126. if(isDef(rst[attrs[0]])) {
  127. rst[attrs[0]][attrs[1]] = parseInt($(inputs[i]).val());
  128. }
  129. }
  130. else {
  131. rst[attrs[0]] = parseInt($(inputs[i]).val());
  132. }
  133. }
  134. for(let attr in defaultDecimal['_def']){
  135. if(!defaultDecimal['_def'][attr]['editable']){
  136. rst[attr] = defaultDecimal['_def'][attr]['data'];
  137. }
  138. }
  139. return rst;
  140. }
  141. function v_initPanel(data){
  142. if(this.isDef(data)){
  143. for(let attr in data){
  144. if(this.isObj(data[attr])){
  145. for(let subAttr in data[attr]){
  146. let str = attr + '-' + subAttr;
  147. let jqs = 'input[name="' + str + '"]';
  148. $(jqs).val(data[attr][subAttr]);
  149. $(jqs).attr('min', defaultDecimal.min);
  150. $(jqs).attr('max', defaultDecimal.max);
  151. }
  152. }
  153. else {
  154. let str = attr + '';
  155. let jqs = 'input[name="' + str + '"]';
  156. $(jqs).val(data[attr]);
  157. $(jqs).attr('min', defaultDecimal.min);
  158. $(jqs).attr('max', defaultDecimal.max);
  159. }
  160. }
  161. }
  162. }
  163. function e_validIn(inputs){
  164. for(let i = 0, len = inputs.length; i < len; i++){
  165. let orgV = $(inputs[i]).val();
  166. $(inputs[i]).bind('input', function () {
  167. let v = $(this).val();
  168. let inputName = $(this).attr('name');
  169. if(v.trim().length === 0 || !isValidDigit(v)){
  170. alert('小数位数范围在0-6!');
  171. $(this).val(orgV);
  172. }
  173. else{
  174. orgV = v;
  175. //清单单价、合价 与定额单价、合价同步
  176. if(inputName === 'bills-unitPrice'){
  177. $("input[name='ration-unitPrice']").val(v);
  178. }
  179. else if(inputName === 'bills-totalPrice'){
  180. $("input[name='ration-totalPrice']").val(v);
  181. }
  182. else if(inputName === 'ration-unitPrice'){
  183. $("input[name='bills-unitPrice']").val(v);
  184. }
  185. else if(inputName === 'ration-totalPrice'){
  186. $("input[name='bills-totalPrice']").val(v);
  187. }
  188. }
  189. });
  190. }
  191. }
  192. function e_bindCof(btn){
  193. btn.bind('click', function () {
  194. //获取更新的数据
  195. let updateDecimal = m_getDecimalData($('input', '#poj-settings-decimal'));
  196. if(toUpdateDecimal(decimalObj, updateDecimal)){
  197. a_updateDigits(updateDecimal);
  198. }
  199. });
  200. }
  201. function e_unbindCof(btn){
  202. btn.unbind();
  203. }
  204. function a_updateDigits(updateDecimal){
  205. let url = '/pm/api/updateProjects';
  206. let data = {
  207. updateType: 'update',
  208. updateData: {
  209. ID: parseInt(scUrlUtil.GetQueryString('project')),
  210. 'property.decimal': updateDecimal
  211. }
  212. };
  213. let postData = {
  214. user_id: userID,
  215. updateData: [data]
  216. };
  217. let scCaller = function (resultData) {
  218. //update data
  219. setDecimal(decimalObj, updateDecimal);
  220. let v_data = m_getInitData(decimalObj);
  221. v_initPanel(v_data);
  222. };
  223. let errCaller = function () {
  224. alert('更新小数位数失败!');
  225. let v_data = m_getInitData(decimalObj);
  226. v_initPanel(v_data);
  227. };
  228. CommonAjax.post(url, postData, scCaller, errCaller);
  229. }
  230. $(document).ready(function () {
  231. $('#poj-set').on('shown.bs.modal', function (e) {
  232. let v_data = m_getInitData(decimalObj);
  233. v_initPanel(v_data);
  234. });
  235. //绑定确定按钮
  236. //e_bindCof($('#property_ok'));
  237. //绑定小数位数输入控制
  238. e_validIn($('input', '#poj-settings-decimal'));
  239. });