project_property_decimal_view.js 9.1 KB

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