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. _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: 3},
  14. quantity_detail: {editable: false, data: 4},
  15. process: {editable: false, data: 6},
  16. marketPriceProcess:{editable: false, data: 2}
  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){
  103. return isInt(v) && v >= defaultDecimal.min && v <= defaultDecimal.max;
  104. }
  105. //newV用户可编辑数据
  106. function toUpdateDecimal(orgV, newV){
  107. let rst = false;
  108. reCompare(orgV, newV);
  109. function reCompare(orgV, newV){
  110. for(let attr in newV){
  111. if(Object.keys(newV[attr]).length > 0){
  112. if(isUndef(orgV[attr])){
  113. rst = true;
  114. return;
  115. }
  116. else{
  117. reCompare(orgV[attr], newV[attr]);
  118. }
  119. }
  120. else {
  121. if(isUndef(orgV[attr]) || parseInt(newV[attr]) !== parseInt(orgV[attr])){
  122. rst = true;
  123. return;
  124. }
  125. }
  126. }
  127. }
  128. return rst;
  129. }
  130. function setDecimal(_digits, data){
  131. if(isDef(data)){
  132. for(let attr in data){
  133. _digits[attr] = returnV(data[attr], defaultDecimal['_def'][attr]['data']);
  134. }
  135. }
  136. else {
  137. for(let attr in defaultDecimal['_def']){
  138. _digits[attr] = defaultDecimal['_def'][attr]['data'];
  139. }
  140. }
  141. }
  142. //获取decimalPanel中要展示的数据
  143. function m_getInitData(data){
  144. let rst = Object.create(null);
  145. for(let attr in defaultDecimal['_def']){
  146. if(defaultDecimal['_def'][attr]['editable'] && isDef(data[attr])){
  147. rst[attr] = data[attr];
  148. }
  149. }
  150. return rst;
  151. }
  152. //获取小数位数panel里的数据
  153. function m_getDecimalData(inputs){
  154. let rst = Object.create(null);
  155. for(let i = 0, len = inputs.length ; i < len; i++){
  156. let name = $(inputs[i]).attr('name');
  157. let attrs = name.split('-');
  158. if(attrs.length > 1){
  159. if(isUndef(rst[attrs[0]])){
  160. rst[attrs[0]] = Object.create(null);
  161. }
  162. if(isDef(rst[attrs[0]])) {
  163. rst[attrs[0]][attrs[1]] = parseInt($(inputs[i]).val());
  164. }
  165. }
  166. else {
  167. rst[attrs[0]] = parseInt($(inputs[i]).val());
  168. }
  169. }
  170. for(let attr in defaultDecimal['_def']){
  171. if(!defaultDecimal['_def'][attr]['editable']){
  172. rst[attr] = defaultDecimal['_def'][attr]['data'];
  173. }
  174. }
  175. return rst;
  176. }
  177. function v_initPanel(data){
  178. if(this.isDef(data)){
  179. for(let attr in data){
  180. if(this.isObj(data[attr])){
  181. for(let subAttr in data[attr]){
  182. let str = attr + '-' + subAttr;
  183. let jqs = 'input[name="' + str + '"]';
  184. $(jqs).val(data[attr][subAttr]);
  185. $(jqs).attr('min', defaultDecimal.min);
  186. $(jqs).attr('max', defaultDecimal.max);
  187. }
  188. }
  189. else {
  190. let str = attr + '';
  191. let jqs = 'input[name="' + str + '"]';
  192. $(jqs).val(data[attr]);
  193. $(jqs).attr('min', defaultDecimal.min);
  194. $(jqs).attr('max', defaultDecimal.max);
  195. }
  196. }
  197. }
  198. }
  199. function e_validIn(inputs){
  200. for(let i = 0, len = inputs.length; i < len; i++){
  201. let orgV = $(inputs[i]).val();
  202. $(inputs[i]).bind('input', function () {
  203. let v = $(this).val();
  204. let inputName = $(this).attr('name');
  205. if(v.trim().length === 0 || !isValidDigit(v)){
  206. alert('小数位数范围在0-6!');
  207. $(this).val(orgV);
  208. }
  209. else{
  210. orgV = v;
  211. //清单单价、合价 与定额单价、合价同步
  212. if(inputName === 'bills-unitPrice'){
  213. $("input[name='ration-unitPrice']").val(v);
  214. }
  215. else if(inputName === 'bills-totalPrice'){
  216. $("input[name='ration-totalPrice']").val(v);
  217. }
  218. else if(inputName === 'ration-unitPrice'){
  219. $("input[name='bills-unitPrice']").val(v);
  220. }
  221. else if(inputName === 'ration-totalPrice'){
  222. $("input[name='bills-totalPrice']").val(v);
  223. }
  224. }
  225. });
  226. }
  227. }
  228. function e_bindCof(btn){
  229. btn.bind('click', function () {
  230. //获取更新的数据
  231. let updateDecimal = m_getDecimalData($('input', '#poj-settings-decimal'));
  232. if(toUpdateDecimal(decimalObj, updateDecimal)){
  233. a_updateDigits(updateDecimal);
  234. }
  235. });
  236. }
  237. function e_unbindCof(btn){
  238. btn.unbind();
  239. }
  240. function a_updateDigits(updateDecimal){
  241. let url = '/pm/api/updateProjects';
  242. let data = {
  243. updateType: 'update',
  244. updateData: {
  245. ID: parseInt(scUrlUtil.GetQueryString('project')),
  246. 'property.decimal': updateDecimal
  247. }
  248. };
  249. let postData = {
  250. user_id: userID,
  251. updateData: [data]
  252. };
  253. let scCaller = function (resultData) {
  254. //update data
  255. setDecimal(decimalObj, updateDecimal);
  256. let v_data = m_getInitData(decimalObj);
  257. v_initPanel(v_data);
  258. };
  259. let errCaller = function () {
  260. alert('更新小数位数失败!');
  261. let v_data = m_getInitData(decimalObj);
  262. v_initPanel(v_data);
  263. };
  264. CommonAjax.post(url, postData, scCaller, errCaller);
  265. }
  266. $(document).ready(function () {
  267. $('#poj-set').on('shown.bs.modal', function (e) {
  268. let v_data = m_getInitData(decimalObj);
  269. v_initPanel(v_data);
  270. let headerText = projectObj.project.projectInfo.property && projectObj.project.projectInfo.property.valuationType == "bill"?"项目节":"清单";//项目属性-小数位数,“清单”改文字为“项目节”,工程量清单中保持不变。
  271. $("#bills_decimal_header").text(headerText);
  272. });
  273. //绑定确定按钮
  274. //e_bindCof($('#property_ok'));
  275. //绑定小数位数输入控制
  276. e_validIn($('input', '#poj-settings-decimal'));
  277. });