project_property_decimal_view.js 8.7 KB

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