project_property_decimal_view.js 7.5 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. material: {editable: false, data: 5},
  13. feeRate: {editable: true, data: 2},
  14. quantity_detail: {editable: false, data: 4},
  15. process: {editable: false, data: 6}
  16. }
  17. };
  18. let decimalObj = Object.create(null);
  19. decimalObj.decimal = function (field, node) {
  20. if(isDef(field)){
  21. if(field === 'feeRate'||field==="quantity_detail"){
  22. return this[field];
  23. }
  24. else if (field.sameText('unitFee')) field = 'unitPrice'
  25. else if (field.sameText('totalFee')) field = 'totalPrice';
  26. if(isDef(node)){
  27. if(node.sourceType === projectObj.project.Bills.getSourceType()){
  28. if(field === 'quantity'){
  29. return billsQuanDecimal.decimal(node.data.unit);
  30. }
  31. else {
  32. return returnV(this['bills'][field], this.process);
  33. }
  34. }
  35. else if(node.sourceType === projectObj.project.Ration.getSourceType()){
  36. return returnV(this['ration'][field], this.process);
  37. }
  38. else if(node.sourceType === projectObj.project.ration_glj.getSourceType()){
  39. return returnV(this['glj'][field], this.process);
  40. }
  41. }
  42. }
  43. return this.process;
  44. };
  45. function returnV(v, r){
  46. if(isDef(v)){
  47. return v;
  48. }
  49. return r;
  50. }
  51. function isUndef(v) {
  52. return v === undefined || v === null;
  53. }
  54. function isDef(v){
  55. return v !== undefined && v !== null;
  56. }
  57. function isObj(v){
  58. return isDef(v) && typeof v === 'object';
  59. }
  60. function isNum(v){
  61. return isDef(v) && !isNaN(v);
  62. }
  63. function isInt(v){
  64. return isNum(v) && v % 1 === 0;
  65. }
  66. function isValidDigit(v){
  67. return isInt(v) && v >= defaultDecimal.min && v <= defaultDecimal.max;
  68. }
  69. //newV用户可编辑数据
  70. function toUpdateDecimal(orgV, newV){
  71. let rst = false;
  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] = returnV(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. });