U8ServerService.php 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  1. <?php
  2. namespace App\Service;
  3. use App\Model\DDEmployee;
  4. use App\Model\FieldData;
  5. use App\Model\Inventory;
  6. use App\Model\Record;
  7. use App\Model\U8State;
  8. use App\Model\Vendor;
  9. use Illuminate\Support\Facades\DB;
  10. class U8ServerService extends Service
  11. {
  12. protected $databaseService;
  13. /**
  14. * @var string|null
  15. */
  16. protected $error;
  17. /**
  18. * U8ServerService constructor.
  19. */
  20. public function __construct($loginUser = [])
  21. {
  22. $service = new U8DatabaseServerService($loginUser);
  23. $this->databaseService = $service->db;
  24. $this->error = $service->error;
  25. }
  26. /**
  27. * 获取错误信息
  28. *
  29. * @return string|null
  30. */
  31. public function getError()
  32. {
  33. return $this->error;
  34. }
  35. public function purchaseOrder($data, $user){
  36. $qx = $user['qx'];
  37. $order_date = $data['order_date'] ?? [];
  38. $order_date = array_filter($order_date);
  39. $order_number = $data['order_number'] ?? "";
  40. $model = $this->databaseService->table('PO_Pomain as a')
  41. ->leftJoin('Vendor as c', 'c.cVenCode', 'a.cVenCode') // 供应商
  42. ->leftJoin('Department as d', 'd.cDepCode', 'a.cDepCode') // 部门
  43. ->leftJoin('Person as e', 'e.cPersonCode', 'a.cPersonCode')// 业务员
  44. ->leftJoin('PurchaseType as f', 'f.cPTCode', 'a.cPTCode') // 采购类型
  45. ->when(empty($qx), function ($query) use($user){
  46. return $query->where('a.cMaker', $user['name']);
  47. })
  48. ->when(!empty($order_number), function ($query) use($order_number){
  49. return $query->where('a.cPOID', 'LIKE', '%'.$order_number.'%');
  50. })
  51. ->when(!empty($order_date), function ($query) use($order_date){
  52. $start = date('Y-m-d 00:00:00.000', $order_date[0]);
  53. $end = date('Y-m-d 23:59:59.000', $order_date[1]);
  54. return $query->whereBetween('a.dPODate', [$start, $end]);
  55. })
  56. ->where(function ($query) {
  57. $query->where('a.iverifystateex', 0)
  58. ->orWhereNull('a.iverifystateex');
  59. })
  60. ->select(
  61. DB::raw("ISNULL(a.cBusType, '') as business_type"), // 业务类型
  62. DB::raw("ISNULL(CONVERT(varchar(10), a.dPODate, 120), '') as order_date"), // 订单日期
  63. DB::raw("ISNULL(a.cPOID, '') as order_number"), // 订单编号
  64. DB::raw("ISNULL(f.cPTName, '') as purchase_type"), // 采购类型
  65. DB::raw("ISNULL(c.cVenName, '') as supplier_title"), // 供应商
  66. DB::raw("ISNULL(d.cDepName, '') as department_name"), // 部门
  67. DB::raw("ISNULL(e.cPersonName, '') as person_name"), // 业务员
  68. DB::raw("CAST(ISNULL(a.iTaxRate, 0) AS varchar) as tax_rate"), // 税率(数值转字符,null转0或空)
  69. DB::raw("ISNULL(a.cexch_name, '') as currency_name"), // 币种
  70. DB::raw("CAST(ISNULL(a.nflat, 0) AS varchar) as exchange_rate"), // 汇率
  71. DB::raw("ISNULL(a.cMemo, '') as remark"), // 备注
  72. DB::raw("ISNULL(a.cMaker, '') as crt_name") // 制单人
  73. )
  74. ->orderBy('a.POID', 'desc');
  75. $list = $this->limit($model, '', $data);
  76. $list = $this->fillAll($list, U8State::type_one, $user['login_type']);
  77. return [true, $list];
  78. }
  79. public function purchaseOrderDetail($data, $user){
  80. if(empty($data['order_number'])) return [false, '采购单号不能为空'];
  81. $order = $this->databaseService->table('PO_Pomain as a')
  82. ->leftJoin('Vendor as c', 'c.cVenCode', 'a.cVenCode')
  83. ->leftJoin('Department as d', 'd.cDepCode', 'a.cDepCode')
  84. ->leftJoin('Person as e', 'e.cPersonCode', 'a.cPersonCode')
  85. ->leftJoin('PurchaseType as f', 'f.cPTCode', 'a.cPTCode')
  86. ->where('a.cPOID', $data['order_number'])
  87. ->select(
  88. 'a.POID',
  89. DB::raw("ISNULL(a.cBusType, '') as business_type"),
  90. DB::raw("ISNULL(CONVERT(varchar(10), a.dPODate, 120), '') as order_date"),
  91. DB::raw("ISNULL(a.cPOID, '') as order_number"),
  92. DB::raw("ISNULL(f.cPTName, '') as purchase_type"),
  93. DB::raw("ISNULL(c.cVenName, '') as supplier_title"),
  94. DB::raw("ISNULL(d.cDepName, '') as department_name"),
  95. DB::raw("ISNULL(e.cPersonName, '') as person_name"),
  96. DB::raw("CAST(ISNULL(a.iTaxRate, 0) AS varchar) as tax_rate"),
  97. DB::raw("ISNULL(a.cexch_name, '') as currency_name"),
  98. DB::raw("CAST(ISNULL(a.nflat, 0) AS varchar) as exchange_rate"),
  99. DB::raw("ISNULL(a.cMemo, '') as remark"),
  100. DB::raw("ISNULL(a.cMaker, '') as crt_name")
  101. )
  102. ->first();
  103. if(empty($order)) return [false, '采购单不存在'];
  104. $order = (array) $order;
  105. $order = $this->fillDetail($order, U8State::type_one, $user['login_type']);
  106. // 获取明细
  107. $detail = $this->databaseService->table('PO_Podetails as a')
  108. ->leftJoin('Inventory as b', 'b.cInvCode', 'a.cInvCode')
  109. ->leftJoin('ComputationUnit as c', 'c.cComunitCode', 'b.cComUnitCode')
  110. ->where('a.POID', $order['POID'])
  111. ->select(
  112. DB::raw("ISNULL(b.cInvCode, '') as product_code"),
  113. DB::raw("ISNULL(b.cInvName, '') as product_title"),
  114. DB::raw("ISNULL(b.cInvStd, '') as product_std"),
  115. DB::raw("ISNULL(c.cComUnitName, '') as unit_title"),
  116. // 数字转字符串并去除空格,NULL 则返回 '0.00' 或 ''
  117. DB::raw("ISNULL(LTRIM(STR(a.iQuantity, 20, 2)), '0.00') as quantity"),
  118. DB::raw("ISNULL(LTRIM(STR(a.iTaxPrice, 20, 4)), '0.0000') as tax_unit_price"),
  119. DB::raw("ISNULL(LTRIM(STR(a.iUnitPrice, 20, 4)), '0.0000') as unit_price"),
  120. DB::raw("ISNULL(LTRIM(STR(a.iMoney, 20, 2)), '0.00') as amount"),
  121. DB::raw("ISNULL(LTRIM(STR(a.iSum, 20, 2)), '0.00') as tax_amount"),
  122. DB::raw("ISNULL(LTRIM(STR(a.iPerTaxRate, 20, 2)), '0.00') as tax_rate"),
  123. DB::raw("ISNULL(CONVERT(varchar(10), a.dArriveDate, 120), '') as arrive_date"),
  124. DB::raw("ISNULL(b.cEnterprise, '') as factory_name")
  125. )
  126. ->get();
  127. // 转为数组
  128. $order['detail'] = $detail->map(function ($item) {
  129. return (array) $item;
  130. })->toArray();
  131. // 移除内部 ID 避免暴露
  132. unset($order['POID']);
  133. return [true, $order];
  134. }
  135. public function purchaseRequisition($data, $user){
  136. $qx = $user['qx'];
  137. $order_date = $data['order_date'] ?? [];
  138. $order_date = array_filter($order_date);
  139. $order_number = $data['order_number'] ?? "";
  140. $model = $this->databaseService->table('PU_AppVouch as a')
  141. ->leftJoin('Person as c', 'c.cPersonCode', 'a.cPersonCode') // 请购人
  142. ->leftJoin('Department as d', 'd.cDepCode', 'a.cDepCode') // 请购部门
  143. ->leftJoin('PurchaseType as f', 'f.cPTCode', 'a.cPTCode') // 采购类型
  144. ->when(empty($qx), function ($query) use($user){
  145. return $query->where('a.cMaker',$user['name']);
  146. })
  147. ->when(! empty($order_number), function ($query) use($order_number){
  148. return $query->where('a.cCode','LIKE', '%'.$order_number.'%');
  149. })
  150. ->when(! empty($order_date), function ($query) use($order_date){
  151. $start = date('Y-m-d 00:00:00.000', $order_date[0]);
  152. $end = date('Y-m-d 23:59:59.000', $order_date[1]);
  153. return $query->whereBetween('a.dDate', [$start, $end]);
  154. })
  155. ->where(function ($query) {
  156. $query->where('a.iverifystateex', 0)
  157. ->orWhereNull('a.iverifystateex');
  158. })
  159. ->select(
  160. DB::raw("ISNULL(a.cBusType, '') as business_type"), // 业务类型
  161. DB::raw("ISNULL(a.cCode, '') as order_number"), // 单据号
  162. DB::raw("ISNULL(CONVERT(varchar(10), a.dDate, 120), '') as order_date"), // 日期
  163. DB::raw("ISNULL(d.cDepName, '') as department_name"), // 请购部门
  164. DB::raw("ISNULL(c.cPersonName, '') as purchase_name"), // 请购人员
  165. DB::raw("ISNULL(f.cPTName, '') as purchase_type"), // 采购类型
  166. DB::raw("ISNULL(a.cMaker, '') as crt_name") // 制单人
  167. )
  168. ->orderBy('a.ID','desc');
  169. $list = $this->limit($model,'',$data);
  170. $list = $this->fillAll($list, U8State::type_two, $user['login_type']);
  171. return [true , $list];
  172. }
  173. public function purchaseRequisitionDetail($data, $user){
  174. if(empty($data['order_number'])) return [false, '采购请购单号不能为空'];
  175. $order = $this->databaseService->table('PU_AppVouch as a')
  176. ->leftJoin('Person as c', 'c.cPersonCode', 'a.cPersonCode')
  177. ->leftJoin('Department as d', 'd.cDepCode', 'a.cDepCode')
  178. ->leftJoin('PurchaseType as f', 'f.cPTCode', 'a.cPTCode')
  179. ->where('a.cCode', $data['order_number'])
  180. ->select(
  181. 'a.ID', // 用于关联子表
  182. DB::raw("ISNULL(a.cBusType, '') as business_type"),
  183. DB::raw("ISNULL(a.cCode, '') as order_number"),
  184. DB::raw("ISNULL(CONVERT(varchar(10), a.dDate, 120), '') as order_date"),
  185. DB::raw("ISNULL(d.cDepName, '') as department_name"),
  186. DB::raw("ISNULL(c.cPersonName, '') as purchase_name"),
  187. DB::raw("ISNULL(f.cPTName, '') as purchase_type"),
  188. DB::raw("ISNULL(a.cMaker, '') as crt_name")
  189. )
  190. ->first();
  191. if(empty($order)) return [false, '采购请购单不存在'];
  192. $order = (array) $order;
  193. $order = $this->fillDetail($order, U8State::type_two, $user['login_type']);
  194. $detail = $this->databaseService->table('PU_AppVouchs as a')
  195. ->leftJoin('Inventory as b', 'b.cInvCode', 'a.cInvCode')
  196. ->leftJoin('ComputationUnit as c', 'c.cComunitCode', 'b.cComUnitCode')
  197. ->where('a.ID', $order['ID'])
  198. ->select(
  199. DB::raw("ISNULL(b.cInvCode, '') as product_code"), // 存货编码
  200. DB::raw("ISNULL(b.cInvName, '') as product_title"), // 存货名称
  201. DB::raw("ISNULL(b.cInvStd, '') as product_std"), // 规格型号
  202. DB::raw("ISNULL(c.cComUnitName, '') as unit_title"), // 主计量
  203. DB::raw("ISNULL(LTRIM(STR(a.fQuantity, 20, 2)), '0.00') as quantity"), // 数量
  204. DB::raw("ISNULL(LTRIM(STR(a.fUnitPrice, 20, 4)), '0.0000') as unit_price"), // 本币单价
  205. DB::raw("ISNULL(LTRIM(STR(a.iOriSum, 20, 2)), '0.00') as tax_amount"), // 本币价税合计
  206. DB::raw("ISNULL(CONVERT(varchar(10), a.dRequirDate, 120), '') as need_arrived_date"), // 需求日期
  207. DB::raw("ISNULL(CONVERT(varchar(10), a.dArriveDate, 120), '') as suggest_order_date"), // 建议订货日期
  208. DB::raw("ISNULL(b.cEnterprise, '') as factory_name") // 生产企业
  209. )
  210. ->get();
  211. // 转为数组格式
  212. $order['detail'] = $detail->map(function ($item) {
  213. return (array) $item;
  214. })->toArray();
  215. unset($order['ID']); // 隐藏内部ID
  216. return [true, $order];
  217. }
  218. public function purchaseInOrder($data, $user){
  219. $qx = $user['qx'];
  220. $order_date = $data['order_date'] ?? [];
  221. $order_date = array_filter($order_date);
  222. $order_number = $data['order_number'] ?? "";
  223. $model = $this->databaseService->table('RdRecord01 as a')
  224. ->leftJoin('Vendor as c', 'c.cVenCode', 'a.cVenCode') // 供货单位
  225. ->leftJoin('Warehouse as w', 'w.cWhCode', 'a.cWhCode') // 仓库
  226. ->leftJoin('Department as d', 'd.cDepCode', 'a.cDepCode') // 部门
  227. ->leftJoin('Person as p', 'p.cPersonCode', 'a.cPersonCode')// 业务员
  228. ->leftJoin('PurchaseType as pt', 'pt.cPTCode', 'a.cPTCode')// 采购类型
  229. ->leftJoin('Rd_Style as rs', 'rs.cRdCode', 'a.cRdCode') // 入库类别
  230. ->when(empty($qx), function ($query) use($user){
  231. return $query->where('a.cMaker',$user['name']);
  232. })
  233. ->when(! empty($order_number), function ($query) use($order_number){
  234. return $query->where('a.cCode','LIKE', '%'.$order_number.'%');
  235. })
  236. ->when(! empty($order_date), function ($query) use($order_date){
  237. $start = date('Y-m-d 00:00:00.000', $order_date[0]);
  238. $end = date('Y-m-d 23:59:59.000', $order_date[1]);
  239. return $query->whereBetween('a.dDate', [$start, $end]);
  240. })
  241. ->where(function ($query) {
  242. $query->whereNull('a.cHandler')
  243. ->orWhere('a.cHandler', '');
  244. })// 未审核
  245. ->select(
  246. DB::raw("ISNULL(a.cCode, '') as order_number"), // 入库单号
  247. DB::raw("ISNULL(CONVERT(varchar(10), a.dDate, 120), '') as order_date"), // 入库日期
  248. DB::raw("ISNULL(w.cWhName, '') as warehouse_name"), // 仓库
  249. DB::raw("ISNULL(a.cOrderCode, '') as po_number"), // 订单号
  250. DB::raw("ISNULL(a.cARVCode, '') as arrival_number"), // 到货单号
  251. DB::raw("ISNULL(p.cPersonName, '') as person_name"), // 业务员
  252. DB::raw("ISNULL(c.cVenName, '') as supplier_title"), // 供货单位
  253. DB::raw("ISNULL(d.cDepName, '') as department_name"), // 部门
  254. DB::raw("ISNULL(CONVERT(varchar(10), a.dARVDate, 120), '') as arrival_date"), // 到货日期
  255. DB::raw("ISNULL(a.cBusType, '') as business_type"), // 业务类型
  256. DB::raw("ISNULL(pt.cPTName, '') as purchase_type"), // 采购类型
  257. DB::raw("ISNULL(rs.cRdName, '') as rd_style_name"), // 入库类别
  258. DB::raw("ISNULL(CONVERT(varchar(10), a.dVeriDate, 120), '') as audit_date"), // 审核日期
  259. DB::raw("ISNULL(a.cMemo, '') as remark"), // 备注
  260. DB::raw("ISNULL(a.cMaker, '') as crt_name") // 制单人
  261. )
  262. ->orderBy('a.ID','desc');
  263. $list = $this->limit($model,'',$data);
  264. $list = $this->fillAll($list, U8State::type_three, $user['login_type']);
  265. return [true , $list];
  266. }
  267. public function purchaseInOrderDetail($data, $user){
  268. if(empty($data['order_number'])) return [false, '入库单号不能为空'];
  269. $order = $this->databaseService->table('RdRecord01 as a')
  270. ->leftJoin('Vendor as c', 'c.cVenCode', 'a.cVenCode')
  271. ->leftJoin('Warehouse as w', 'w.cWhCode', 'a.cWhCode')
  272. ->leftJoin('Department as d', 'd.cDepCode', 'a.cDepCode')
  273. ->leftJoin('Person as p', 'p.cPersonCode', 'a.cPersonCode')
  274. ->leftJoin('PurchaseType as pt', 'pt.cPTCode', 'a.cPTCode')
  275. ->leftJoin('Rd_Style as rs', 'rs.cRdCode', 'a.cRdCode')
  276. ->where('a.cCode', $data['order_number'])
  277. ->select(
  278. 'a.ID',
  279. DB::raw("ISNULL(a.cCode, '') as order_number"),
  280. DB::raw("ISNULL(CONVERT(varchar(10), a.dDate, 120), '') as order_date"),
  281. DB::raw("ISNULL(w.cWhName, '') as warehouse_name"),
  282. DB::raw("ISNULL(a.cOrderCode, '') as po_number"),
  283. DB::raw("ISNULL(a.cARVCode, '') as arrival_number"),
  284. DB::raw("ISNULL(p.cPersonName, '') as person_name"),
  285. DB::raw("ISNULL(c.cVenName, '') as supplier_title"),
  286. DB::raw("ISNULL(d.cDepName, '') as department_name"),
  287. DB::raw("ISNULL(CONVERT(varchar(10), a.darvdate, 120), '') as arrival_date"),
  288. DB::raw("ISNULL(a.cBusType, '') as business_type"),
  289. DB::raw("ISNULL(pt.cPTName, '') as purchase_type"),
  290. DB::raw("ISNULL(rs.cRdName, '') as rd_style_name"),
  291. DB::raw("ISNULL(CONVERT(varchar(10), a.dVeriDate, 120), '') as audit_date"),
  292. DB::raw("ISNULL(a.cMemo, '') as remark"),
  293. DB::raw("ISNULL(a.cMaker, '') as crt_name")
  294. )
  295. ->first();
  296. if(empty($order)) return [false, '采购入库单不存在'];
  297. $order = (array) $order;
  298. $order = $this->fillDetail($order, U8State::type_three, $user['login_type']);
  299. $detail = $this->databaseService->table('rdrecords01 as a')
  300. ->leftJoin('Inventory as b', 'b.cInvCode', 'a.cInvCode')
  301. ->leftJoin('ComputationUnit as c', 'c.cComunitCode', 'b.cComUnitCode')
  302. ->where('a.ID', $order['ID'])
  303. ->select(
  304. DB::raw("ISNULL(b.cInvCode, '') as product_code"), // 存货编码
  305. DB::raw("ISNULL(b.cInvName, '') as product_title"), // 存货名称
  306. DB::raw("ISNULL(b.cInvStd, '') as product_std"), // 规格型号
  307. DB::raw("ISNULL(c.cComUnitName, '') as unit_title"), // 主计量单位
  308. DB::raw("ISNULL(LTRIM(STR(a.iQuantity, 20, 2)), '0.00') as quantity"), // 数量
  309. DB::raw("ISNULL(LTRIM(STR(a.iUnitCost, 20, 4)), '0.0000') as unit_price"), // 本币单价
  310. DB::raw("ISNULL(LTRIM(STR(a.iPrice, 20, 2)), '0.00') as amount"), // 本币金额
  311. DB::raw("ISNULL(LTRIM(STR(a.iTax, 20, 2)), '0.00') as tax"), // 税额
  312. DB::raw("ISNULL(LTRIM(STR(a.iTaxPrice, 20, 2)), '0.00') as nat_tax"), // 本币税额 (入库单本币税额通常等于原币税额)
  313. DB::raw("ISNULL(LTRIM(STR(a.iSum, 20, 2)), '0.00') as tax_amount") // 本币价税合计
  314. )
  315. ->get();
  316. $detailArr = $detail->map(function ($item) {
  317. return (array) $item;
  318. })->toArray();
  319. // 计算总金额(价税合计之和)
  320. $order['total_amount'] = number_format(array_sum(array_column($detailArr, 'tax_amount')), 2, '.', '');
  321. $order['detail'] = $detailArr;
  322. unset($order['ID']);
  323. return [true, $order];
  324. }
  325. public function inventoryDetail($data, $user){
  326. if(empty($data['order_number'])) return [false, '流水单号不能为空'];
  327. $order = Inventory::where('del_time',0)
  328. ->where('order_number', $data['order_number'])
  329. ->where('login_type', $user['login_type'])
  330. ->first();
  331. if(empty($order)) return [false, '存货不存在'];
  332. $order = $order->toArray() ;
  333. $order['crt_name'] = DDEmployee::where('login_type', $user['login_type'])->where('userid', $order['crt_id'])->value('name') ?? '';
  334. return [true, $order];
  335. }
  336. public function vendorDetail($data, $user){
  337. if(empty($data['order_number'])) return [false, '流水单号不能为空'];
  338. $order = Vendor::where('del_time',0)
  339. ->where('order_number', $data['order_number'])
  340. ->where('login_type', $user['login_type'])
  341. ->first();
  342. if(empty($order)) return [false, '供应商不存在'];
  343. $order = $order->toArray() ;
  344. $order['crt_name'] = DDEmployee::where('login_type', $user['login_type'])->where('userid', $order['crt_id'])->value('name') ?? '';
  345. return [true, $order];
  346. }
  347. public function getOrderDetails($data,$user){
  348. $type = $data['type'];
  349. [$success, $order] = [false, '异常错误'];
  350. if($type == 1){
  351. // 采购单
  352. // [$success, $order] = $this->purchaseOrderDetail($data,$user);
  353. [$success, $order] = (new U8XkyServerService())->purchaseOrderMyDetail($data,$user);
  354. }elseif ($type == 2){
  355. // 采购请购单
  356. // [$success, $order] = $this->purchaseRequisitionDetail($data,$user);
  357. [$success, $order] = (new U8XkyServerService())->purchaseRequisitionMyDetail($data,$user);
  358. }elseif ($type == 3){
  359. // 采购入库
  360. // [$success, $order] = $this->purchaseInOrderDetail($data,$user);
  361. [$success, $order] = (new U8XkyServerService())->purchaseOrderInMyDetail($data,$user);
  362. }elseif ($type == 4){
  363. // 存货
  364. [$success, $order] = $this->inventoryDetail($data,$user);
  365. }elseif ($type == 5){
  366. // 供应商
  367. [$success, $order] = $this->vendorDetail($data,$user);
  368. }
  369. return [$success, $order];
  370. }
  371. private function fillAll($list, $type, $login_type){
  372. if(empty($list['data'])) return $list;
  373. $map = U8State::where('del_time', 0)
  374. ->where('type', $type)
  375. ->where('login_type', $login_type)
  376. ->whereIn('order_number', array_column($list['data'], 'order_number'))
  377. ->pluck('state', 'order_number')
  378. ->toArray();
  379. foreach ($list['data'] as $key => $value){
  380. if(isset($map[$value->order_number])) {
  381. $m = $map[$value->order_number];
  382. $state = $m;
  383. $state_title = Record::state_name[$state];
  384. }else{
  385. $state = Record::state_minus_one;
  386. $state_title = Record::state_name[$state];
  387. }
  388. $list['data'][$key]->state = $state;
  389. $list['data'][$key]->state_title = $state_title;
  390. }
  391. return $list;
  392. }
  393. private function fillDetail($list, $type, $login_type)
  394. {
  395. if (empty($list)) return $list;
  396. // 1. 从 U8State 表中查询 state 和 result
  397. $u8Status = U8State::where('del_time', 0)
  398. ->where('type', $type)
  399. ->where('login_type', $login_type)
  400. ->where('order_number', $list['order_number'])
  401. ->select('state', 'result') // 明确查询需要的字段
  402. ->first();
  403. // 2. 逻辑判断
  404. if ($u8Status) {
  405. $state = $u8Status->state;
  406. $result = $u8Status->result; // 获取你想要的 result 字段
  407. $state_title = Record::state_name[$state] ?? '';
  408. } else {
  409. $state = Record::state_minus_one;
  410. $result = ''; // 或者根据业务给个默认值,如 ''
  411. $state_title = Record::state_name[$state] ?? '';
  412. }
  413. // 3. 注入到 list 中
  414. $list['state'] = $state;
  415. $list['state_title'] = $state_title;
  416. $list['result'] = $result; // 将 result 返回给前端或后续逻辑
  417. return $list;
  418. }
  419. public function stockList($data, $user)
  420. {
  421. try {
  422. $field_list = [];
  423. $employee = DDEmployee::where('userid', $user['userid'])->where('login_type', $user['login_type'])->first();
  424. $qx = $employee['qx'] ?? 0;
  425. if(empty($qx)) {
  426. $field_list = FieldData::where('userid', $user['userid'])
  427. ->where('login_type', $user['login_type'])
  428. ->where('type', FieldData::STATE_ZERO)
  429. ->pluck('key')
  430. ->all();
  431. }
  432. // 1. 构建基础查询:关联现存量表和存货档案表
  433. $query = $this->databaseService->table('CurrentStock as S')
  434. ->select([
  435. 'S.cWhCode', // 仓库编码
  436. 'W.cWhName', // 仓库名称
  437. 'S.cInvCode', // 存货编码
  438. 'I.cInvName', // 存货名称
  439. 'I.cInvStd', // 规格型号
  440. 'I.cInvCCode', // 分类编码
  441. // --- 数量字段对齐你的结构 ---
  442. 'S.iQuantity', // 结存数量 (账面现存量)
  443. 'S.fAvaQuantity', // 可用数量
  444. 'S.fOutQuantity', // 待发货数量 (待出)
  445. 'S.fInQuantity', // 待入库数量 (待入)
  446. 'S.fStopQuantity', // 冻结数量
  447. // --- 批次与日期 ---
  448. 'S.cBatch', // 批号
  449. 'S.dMdate', // 生产日期
  450. 'S.dVDate', // 失效日期
  451. ])
  452. ->join('Inventory as I', 'S.cInvCode', '=', 'I.cInvCode')
  453. ->leftJoin('Warehouse as W', 'S.cWhCode', '=', 'W.cWhCode')
  454. ->leftJoin('InventoryClass as IC', 'I.cInvCCode', '=', 'IC.cInvCCode');
  455. // 2. 过滤条件:存货名称 (支持模糊查询)
  456. if (!empty($data['material_title'])) {
  457. $query->where('I.cInvName', 'like', '%' . $data['material_title'] . '%');
  458. }
  459. // 2. 过滤条件:存货编码 (支持模糊查询)
  460. if (!empty($data['material_code'])) {
  461. $query->where('S.cInvCode', 'like', '%' . $data['material_code'] . '%');
  462. }
  463. // 3. 过滤条件:存货分类 (支持左匹配,即选大类查出所有子类)
  464. if (!empty($data['category_code'])) {
  465. // U8 分类是级次结构,用 like '01%' 可以查出 01 开头的所有子类
  466. $query->where('I.cInvCCode', 'like', $data['category_code'] . '%');
  467. }
  468. // 6. 排序
  469. $query->orderBy('S.cInvCode', 'asc')->orderBy('S.cWhCode', 'asc');
  470. // 7. 调用你定义的分页方法
  471. $columns = ['*'];
  472. $result = $this->limit($query, $columns, $data);
  473. // 注意这里的 &$item,加了 & 符号才能直接修改原数组里的内容
  474. foreach ($result['data'] as &$item) {
  475. $numFields = ['iQuantity', 'fAvaQuantity', 'fOutQuantity', 'fInQuantity', 'fStopQuantity'];
  476. foreach ($numFields as $field) {
  477. if (isset($item->$field)) {
  478. $item->$field = (float)$item->$field;
  479. }
  480. }
  481. $item->dMdate = $item->dMdate ? date('Y-m-d', strtotime($item->dMdate)) : '';
  482. $item->dVDate = $item->dVDate ? date('Y-m-d', strtotime($item->dVDate)) : '';
  483. // 脱敏处理
  484. if (!empty($field_list)) {
  485. foreach ($field_list as $blackField) {
  486. if (isset($item->$blackField)) {
  487. $item->$blackField = '*****';
  488. }
  489. }
  490. }
  491. }
  492. unset($item); // 销毁引用
  493. return [true, $result];
  494. } catch (\Throwable $exception) {
  495. return [false, "查询库存失败: " . $exception->getMessage()];
  496. }
  497. }
  498. public function vendorU8List($data, $user)
  499. {
  500. // 1. 构建基础查询
  501. $query = $this->databaseService->table('Vendor as V')
  502. ->select([
  503. 'V.cVenCode', // 供应商编码
  504. 'V.cVenName', // 供应商名称
  505. 'V.cVenAbbName', // 供应商简称
  506. 'V.cVCCode', // 分类编码
  507. 'VC.cVCName', // 分类名称 (来自 VendorClass)
  508. 'V.cVenAddress', // 地址
  509. 'V.cVenPhone', // 电话
  510. 'V.dVenDevDate', // 发展日期
  511. 'V.cCreatePerson', // 创建人
  512. 'V.bVenTax', // 是否计税
  513. 'V.iId' // 内部ID
  514. ])
  515. // 关联供应商分类表获取分类名称
  516. ->leftJoin('VendorClass as VC', 'V.cVCCode', '=', 'VC.cVCCode');
  517. // 2. 增加搜索逻辑 (可选)
  518. if (!empty($data['keyword'])) {
  519. $keyword = $data['keyword'];
  520. $query->where(function($q) use ($keyword) {
  521. $q->where('V.cVenCode', 'like', "%{$keyword}%")
  522. ->orWhere('V.cVenName', 'like', "%{$keyword}%")
  523. ->orWhere('V.cVenAbbName', 'like', "%{$keyword}%");
  524. });
  525. }
  526. // 3. 排序 (默认按编码排序)
  527. $query->orderBy('V.cVenCode', 'ASC');
  528. // 4. 调用你定义的分页方法
  529. // 注意:limit 方法内部会执行 paginate 并将结果填充到 $data
  530. $columns = ['*']; // select 已经在上面定义过了,这里传 * 即可
  531. $result = $this->limit($query, $columns, $data);
  532. return [true, $result];
  533. }
  534. public function vendorClassTree($data, $user)
  535. {
  536. try {
  537. // 1. 获取所有供应商分类 (表名: VendorClass)
  538. $classes = $this->databaseService->table('VendorClass')
  539. ->select('cVCCode', 'cVCName', 'iVCGrade', 'bVCEnd') // 编码、名称、级次
  540. ->orderBy('cVCCode', 'asc')
  541. ->get();
  542. if ($classes->isEmpty()) {
  543. return [true, []];
  544. }
  545. // 2. 格式化数据,以编码为 Key
  546. $classList = [];
  547. foreach ($classes as $item) {
  548. $classList[$item->cVCCode] = [
  549. 'label' => $item->cVCName,
  550. 'value' => $item->cVCCode, // 前端通常需要 value 字段
  551. 'code' => $item->cVCCode,
  552. 'grade' => $item->iVCGrade,
  553. 'is_end' => $item->bVCEnd,
  554. 'children' => []
  555. ];
  556. }
  557. // 3. 构建引用树
  558. $tree = [];
  559. foreach ($classList as $code => &$node) {
  560. // 获取父级编码
  561. $parentCode = $this->getParentCode($code);
  562. if ($parentCode === null || !isset($classList[$parentCode])) {
  563. // 顶级节点
  564. $tree[] = &$node;
  565. } else {
  566. // 挂载到父节点
  567. $classList[$parentCode]['children'][] = &$node;
  568. }
  569. }
  570. return [true, $tree];
  571. } catch (\Throwable $exception) {
  572. return [false, "获取供应商分类树失败: " . $exception->getMessage()];
  573. }
  574. }
  575. /**
  576. * 辅助函数:根据 U8 编码规则获取父级编码
  577. * U8 的级次通常存储在 GradeDef 表,但通用逻辑是截取末尾
  578. */
  579. private function getParentCode($code)
  580. {
  581. $len = strlen($code);
  582. if ($len <= 2) return null; // 假设第一级是2位,小于等于2位则无父级
  583. // 这里假设级次是 2-2-2-2 (最常见配置)
  584. // 实际生产中,如果级次不固定,建议查询 GradeDef 表
  585. return substr($code, 0, $len - 2);
  586. }
  587. //U8 存货分类树结构
  588. public function inventoryClassTree($data, $user)
  589. {
  590. try {
  591. // 1. 从数据库获取所有存货分类
  592. $classes = $this->databaseService->table('InventoryClass')
  593. ->select('cInvCCode', 'cInvCName', 'iInvCGrade', 'bInvCEnd')
  594. ->orderBy('cInvCCode', 'asc')
  595. ->get();
  596. if ($classes->isEmpty()) return [true, []];
  597. // 2. 将集合转换为数组并以编码作为 Key,方便查找
  598. $classList = [];
  599. foreach ($classes as $item) {
  600. $classList[$item->cInvCCode] = [
  601. 'label' => $item->cInvCName,
  602. 'code' => $item->cInvCCode,
  603. 'grade' => $item->iInvCGrade,
  604. 'is_end' => $item->bInvCEnd,
  605. 'children' => []
  606. ];
  607. }
  608. // 3. 构建树形结构
  609. $tree = [];
  610. foreach ($classList as $code => &$node) {
  611. // 获取当前分类的级次 (U8 逻辑通常根据编码长度判断父级)
  612. // 比如 0101 的父级是 01
  613. $parentCode = $this->getParentCode($code);
  614. if ($parentCode === null || !isset($classList[$parentCode])) {
  615. // 如果没有父级编码,或者父级编码不在列表里,说明是顶级分类
  616. $tree[] = &$node;
  617. } else {
  618. // 将当前节点引用到父节点的 children 数组中
  619. $classList[$parentCode]['children'][] = &$node;
  620. }
  621. }
  622. return [true, $tree];
  623. } catch (\Throwable $exception) {
  624. return [false, "获取分类树失败: " . $exception->getMessage()];
  625. }
  626. }
  627. //U8 计量单位组(带默认主计量单位)
  628. public function getUnitGroups($data, $user)
  629. {
  630. $list = $this->databaseService->select("
  631. SELECT
  632. G.cGroupCode,
  633. G.cGroupName,
  634. G.iGroupType,
  635. U.cComUnitCode,
  636. U.cComUnitName,
  637. U.iNumber
  638. FROM ComputationGroup AS G
  639. OUTER APPLY (
  640. SELECT TOP 1 cComUnitCode, cComUnitName, iNumber
  641. FROM ComputationUnit
  642. WHERE cGroupCode = G.cGroupCode
  643. ORDER BY
  644. bMainUnit DESC, -- 1. 优先主计量
  645. iNumber ASC, -- 2. 序号最小 (若 NULL 会排在最前)
  646. cComUnitCode ASC -- 3. 编码最小
  647. ) AS U
  648. ");
  649. return [true, $list];
  650. }
  651. //U8 计量单位档案
  652. public function getComputationUnitList($data, $user)
  653. {
  654. $list = $this->databaseService->table('ComputationUnit as U')
  655. ->select(
  656. 'U.cComUnitCode', // 单位编码
  657. 'U.cComUnitName', // 单位名称
  658. 'U.cGroupCode', // 所属组编码
  659. 'U.bMainUnit', // 是否主单位
  660. 'U.iNumber' // 排序序号
  661. )
  662. ->orderBy('U.cGroupCode', 'ASC')
  663. ->orderBy('U.iNumber', 'ASC') // 按照你要求的 iNumber 排序
  664. ->get();
  665. return [true, $list];
  666. }
  667. // U8 采购类型
  668. public function getPurchaseTypeList($data, $user)
  669. {
  670. $list = $this->databaseService->table('PurchaseType as P')
  671. // 核心修改:左关联收发类别表 Rd_Style
  672. ->leftJoin('Rd_Style as R', 'R.cRdCode', '=', 'P.cRdCode')
  673. ->select(
  674. 'P.cPTCode', // 采购类型编码 (如: 01, 02)
  675. 'P.cPTName', // 采购类型名称 (如: 国内采购, 国外采购)
  676. 'P.bDefault', // 是否默认值
  677. // 核心修改:查出对应的默认收发类别编码和名称
  678. DB::raw("ISNULL(P.cRdCode, '') as rd_code"),
  679. DB::raw("ISNULL(R.cRdName, '') as rd_name")
  680. )
  681. ->orderBy('P.bDefault', 'DESC')
  682. ->get();
  683. return [true, $list];
  684. }
  685. // 存货档案
  686. public function inventoryU8List($data, $user)
  687. {
  688. // 获取前端传过来的查询参数
  689. $search_code = $data['code'] ?? ""; // 存货编码查询条件
  690. $search_name = $data['name'] ?? ""; // 存货名称查询条件
  691. $model = $this->databaseService->table('Inventory as i')
  692. // 核心修改:关联计量单位表获取单位名称
  693. ->leftJoin('ComputationUnit as u', 'u.cComUnitCode', 'i.cComUnitCode')
  694. // 当编码查询条件不为空时,进行模糊查询
  695. ->when(!empty($search_code), function ($query) use ($search_code) {
  696. return $query->where('i.cInvCode', 'LIKE', '%' . $search_code . '%');
  697. })
  698. // 当名称查询条件不为空时,进行模糊查询
  699. ->when(!empty($search_name), function ($query) use ($search_name) {
  700. return $query->where('i.cInvName', 'LIKE', '%' . $search_name . '%');
  701. })
  702. ->select(
  703. 'i.cInvCode as code', // 存货编码
  704. 'i.cInvName as name', // 存货名称
  705. 'i.cInvStd as size', // 规格型号
  706. 'i.cComUnitCode as unit_code', // 主计量单位编码
  707. 'u.cComUnitName as unit', // 核心修改:主计量单位名称 (如: 个、千克、箱)
  708. 'i.iImpTaxRate as purchase_rate' // 进项税率
  709. )
  710. ->orderBy('i.cInvCode', 'ASC');
  711. // 如果存货档案数据量非常大,建议这里配合分页使用,例如:
  712. $list = $this->limit($model, '', $data);
  713. return [true, $list];
  714. }
  715. // 仓库档案
  716. public function warehouseU8List($data, $user)
  717. {
  718. $list = $this->databaseService->table('Warehouse as W')
  719. ->select(
  720. 'W.cWhCode as code', // 仓库编码 (如: 01, 02)
  721. 'W.cWhName as name' // 仓库名称 (如: 原材料库, 半成品库)
  722. )
  723. ->orderBy('W.cWhCode', 'ASC')
  724. ->get();
  725. return [true, $list];
  726. }
  727. //获取u8请购单
  728. public function purchaseRequisitionU8List($data, $user){
  729. $order_date = $data['order_date'] ?? [];
  730. $order_date = array_filter($order_date);
  731. $code = $data['code'] ?? "";
  732. $model = $this->databaseService->table('PU_AppVouch as a')
  733. ->when(! empty($code), function ($query) use($code){
  734. return $query->where('a.cCode', 'LIKE', '%'.$code.'%');
  735. })
  736. ->when(! empty($order_date), function ($query) use($order_date){
  737. $start = date('Y-m-d 00:00:00.000', $order_date[0]);
  738. $end = date('Y-m-d 23:59:59.000', $order_date[1]);
  739. return $query->whereBetween('a.dDate', [$start, $end]);
  740. })
  741. ->where('a.iverifystateex', 2)
  742. ->whereExists(function ($query) {
  743. $query->select(DB::raw(1))
  744. ->from('PU_AppVouchs as b')
  745. ->whereRaw('b.ID = a.ID') // 主外键关联
  746. ->whereRaw('ISNULL(b.fQuantity, 0) > ISNULL(b.iReceivedQTY, 0)');
  747. })
  748. ->select(
  749. 'a.ID as id', // 必须把主表ID查出来,后续点击需要用来查子表
  750. DB::raw("ISNULL(a.cBusType, '') as business_type"),
  751. DB::raw("ISNULL(a.cCode, '') as order_number"),
  752. DB::raw("ISNULL(CONVERT(varchar(10), a.dDate, 120), '') as order_date"),
  753. DB::raw("ISNULL(a.cMaker, '') as crt_name")
  754. )
  755. ->orderBy('a.ID', 'desc');
  756. $list = $this->limit($model, '', $data);
  757. return [true, $list];
  758. }
  759. public function getRequisitionDetails($data){
  760. if (empty($data['id'])) return [false, '请购单主表ID不能为空'];
  761. $mainId = $data['id'];
  762. $details = $this->databaseService->table('PU_AppVouchs as b')
  763. ->leftJoin('Inventory as i', 'i.cInvCode', 'b.cInvCode') // 通常明细需要关联存货档案拿名称和规格
  764. ->leftJoin('ComputationUnit as u', 'u.cComUnitCode', 'i.cComUnitCode')
  765. ->where('b.ID', $mainId)
  766. ->whereRaw('ISNULL(b.fQuantity, 0) > ISNULL(b.iReceivedQTY, 0)')
  767. ->select(
  768. 'b.AutoID as detail_id', // 子表行单据唯一标识
  769. 'b.ID as main_id', // 主表ID
  770. 'b.ivouchrowno as row_no', // 行号
  771. 'b.cInvCode as code', // 存货编码
  772. 'b.dRequirDate as plan_date', // 需求日期 | 采购订单里的计划到货日期
  773. 'b.fTaxPrice as price', // 原币含税单价
  774. 'b.iPerTaxRate as rate', // 税率
  775. 'i.cComUnitCode as unit_code', // 主计量单位编码
  776. 'u.cComUnitName as unit', // 核心修改:主计量单位名称 (如: 个、千克、箱)
  777. DB::raw("ISNULL(i.cInvName, '') as name"), // 存货名称
  778. DB::raw("ISNULL(i.cInvStd, '') as size"), // 规格型号
  779. DB::raw("ISNULL(b.fQuantity, 0) as req_qty"), // 请购数量 (fQuantity)
  780. DB::raw("ISNULL(b.iReceivedQTY, 0) as order_qty"), // 累计订货数量 (iReceivedQTY)
  781. DB::raw("(ISNULL(b.fQuantity, 0) - ISNULL(b.iReceivedQTY, 0)) as available_qty") // 剩余数量
  782. )
  783. ->orderBy('b.ivouchrowno', 'asc')
  784. ->get();
  785. // 转化为普通数组返回
  786. $list = json_decode(json_encode($details), true);
  787. return [true, $list];
  788. }
  789. public function getRequisitionDetailsByCode($code = [])
  790. {
  791. if (empty($code)) return [false, '请购单号不能为空'];
  792. // 2. 执行 U8 数据库查询
  793. $list = $this->databaseService->table('PU_AppVouchs as b')
  794. ->join('PU_AppVouch as a', 'a.ID', 'b.ID')
  795. ->leftJoin('Inventory as i', 'i.cInvCode', 'b.cInvCode')
  796. ->whereIn('a.cCode', $code)
  797. // 过滤:只查出【请购数量 > 累计订货数量】即还未订货完的明细
  798. ->whereRaw('ISNULL(b.fQuantity, 0) > ISNULL(b.iReceivedQTY, 0)')
  799. ->select(
  800. 'a.cCode as cappcode', // 顺便把主表单号也查出来,方便前端知道这一行属于哪张请购单
  801. 'b.AutoID as detail_id', // 子表行单据唯一标识 (iAppIds)
  802. 'b.ID as main_id', // 主表ID (cappcodeId)
  803. 'b.ivouchrowno as row_no', // 行号
  804. 'b.cInvCode as material_code', // 存货编码
  805. 'b.dRequirDate as plan_date', // 需求日期 -> 对应采购订单的计划到货日期
  806. DB::raw("ISNULL(i.cInvName, '') as material_name"), // 存货名称
  807. DB::raw("ISNULL(i.cInvStd, '') as material_std"), // 规格型号
  808. DB::raw("ISNULL(b.fQuantity, 0) as req_qty"), // 请购数量
  809. DB::raw("ISNULL(b.iReceivedQTY, 0) as order_qty"), // 累计订货数量
  810. DB::raw("(ISNULL(b.fQuantity, 0) - ISNULL(b.iReceivedQTY, 0)) as available_qty") // 剩余可用数量
  811. )
  812. // 按照主表单号和子表行号升序排序,方便前端按单据分组展示
  813. ->orderBy('a.cCode', 'asc')
  814. ->orderBy('b.ivouchrowno', 'asc')
  815. ->get();
  816. // 3. 转化为普通数组返回
  817. $list = json_decode(json_encode($list), true);
  818. return [true, $list];
  819. }
  820. //获取u8采购订单
  821. public function purchaseOrderU8List($data, $user){
  822. // 0 蓝单 1 红单
  823. if(! isset($data['bredvouch'])) return [false, '参照类型(蓝单|红单不能为空)'];
  824. $type = $data['bredvouch'];
  825. $order_date = $data['order_date'] ?? [];
  826. $order_date = array_filter($order_date);
  827. $code = $data['code'] ?? "";
  828. $model = $this->databaseService->table('PO_Pomain as a')
  829. ->leftJoin('Vendor as v', 'v.cVenCode', '=', 'a.cVenCode')
  830. ->when(! empty($code), function ($query) use($code){
  831. return $query->where('a.cPOID', 'LIKE', '%'.$code.'%');
  832. })
  833. ->when(! empty($order_date), function ($query) use($order_date){
  834. $start = date('Y-m-d 00:00:00.000', $order_date[0]);
  835. $end = date('Y-m-d 23:59:59.000', $order_date[1]);
  836. return $query->whereBetween('a.dPODate', [$start, $end]);
  837. })
  838. ->where('a.iverifystateex', 2)
  839. ->whereExists(function ($query) use($type){
  840. if($type == 0){
  841. $query->select(DB::raw(1))
  842. ->from('PO_Podetails as b')
  843. ->whereRaw('b.POID = a.POID')
  844. ->whereRaw('ISNULL(b.iQuantity, 0) > ISNULL(b.iReceivedQTY, 0)');
  845. }elseif($type == 1){
  846. $query->select(DB::raw(1))
  847. ->from('PO_Podetails as b')
  848. ->whereRaw('b.POID = a.POID')
  849. ->whereRaw('ISNULL(b.iReceivedQTY, 0) > 0');
  850. }
  851. })
  852. ->select(
  853. 'a.POID as id',
  854. DB::raw("ISNULL(a.cBusType, '') as business_type"),
  855. DB::raw("ISNULL(a.cVenCode, '') as supply_code"),
  856. DB::raw("ISNULL(v.cVenName, '') as supply_name"),
  857. DB::raw("ISNULL(a.cexch_name, '') as cexch_name"),
  858. DB::raw("ISNULL(a.nflat, '') as nflat"),
  859. DB::raw("ISNULL(a.iDiscountTaxType, '') as iDiscountTaxType"),
  860. DB::raw("ISNULL(a.cPOID, '') as order_number"),
  861. DB::raw("ISNULL(CONVERT(varchar(10), a.dPODate, 120), '') as order_date"),
  862. DB::raw("ISNULL(a.cMaker, '') as crt_name")
  863. )
  864. ->orderBy('a.ID', 'desc');
  865. $list = $this->limit($model, '', $data);
  866. return [true, $list];
  867. }
  868. // 供应商 业务类型 汇率 扣税类别 币种 相同才能一起选
  869. // 只能同时选择供应商、币种、汇率、扣税类别、业务类型、流程模式相同的行! 这是用友的提示
  870. // 获取u8采购订单明细
  871. public function getPurchaseOrderDetails($data){
  872. if (empty($data['id'])) return [false, '采购订单主表ID不能为空'];
  873. // 0 蓝单 1 红单
  874. if(! isset($data['bredvouch'])) return [false, '参照类型(蓝单|红单不能为空)'];
  875. $type = $data['bredvouch'];
  876. $mainId = $data['id'];
  877. $list = $this->databaseService->table('PO_Podetails as b')
  878. ->leftJoin('Inventory as i', 'i.cInvCode', 'b.cInvCode') // 通常明细需要关联存货档案拿名称和规格
  879. ->leftJoin('ComputationUnit as u', 'u.cComUnitCode', 'i.cComUnitCode')
  880. ->where('b.POID', $mainId)
  881. ->when(!empty($type), function ($query) use($type){
  882. if($type == 0){
  883. return $query->whereRaw('ISNULL(b.iQuantity, 0) > ISNULL(b.iReceivedQTY, 0)');
  884. }elseif($type == 1){
  885. return $query->whereRaw('ISNULL(b.iReceivedQTY, 0) > 0');
  886. }
  887. })
  888. ->select(
  889. 'b.POID as main_id', // 主表ID
  890. 'b.ID as detail_id', // 子表行单据唯一标识
  891. 'b.ivouchrowno as row_no', // 行号
  892. 'b.cInvCode as code', // 存货编码
  893. 'b.iTaxPrice as price', // 原币含税单价
  894. 'b.iPerTaxRate as rate', // 税率
  895. 'i.cComUnitCode as unit_code', // 主计量单位编码
  896. 'i.bInvBatch as pici', // 批次管理
  897. 'i.bInvQuality as baozhiqi', // 保质期管理
  898. 'u.cComUnitName as unit', // 核心修改:主计量单位名称 (如: 个、千克、箱)
  899. DB::raw("ISNULL(i.cInvName, '') as name"), // 存货名称
  900. DB::raw("ISNULL(i.cInvStd, '') as size"), // 规格型号
  901. DB::raw("ISNULL(b.iQuantity, 0) as i_qty"), // 采购订单数量 (iQuantity)
  902. DB::raw("ISNULL(b.iReceivedQTY, 0) as in_qty"), // 累计入库数量 (iReceivedQTY)
  903. DB::raw("(ISNULL(b.iQuantity, 0) - ISNULL(b.iReceivedQTY, 0)) as available_qty") // 剩余可入库数量
  904. )
  905. ->orderBy('b.ivouchrowno', 'asc')
  906. ->get();
  907. // 转化为普通数组返回
  908. // $list = json_decode(json_encode($list), true);
  909. return [true, $list];
  910. }
  911. public function getPurchaseOrderDetailsByCode($code = [])
  912. {
  913. if (empty($code)) return [false, '采购订单号不能为空'];
  914. // 执行 U8 数据库查询
  915. $list = $this->databaseService->table('PO_Podetails as b')
  916. ->join('PO_Pomain as a', 'a.POID', '=', 'b.POID')
  917. ->leftJoin('Inventory as i', 'i.cInvCode', '=', 'b.cInvCode')
  918. ->whereIn('a.cPOID', $code)
  919. ->select(
  920. 'a.cPOID as order_number',
  921. 'b.ID as detail_id',
  922. 'b.POID as main_id',
  923. 'b.ivouchrowno as row_no',
  924. 'b.cInvCode as cInvCode',
  925. DB::raw("ISNULL(i.cInvName, '') as name"),
  926. DB::raw("ISNULL(i.cInvStd, '') as size"),
  927. DB::raw("ISNULL(i.cComUnitCode, '') as unit"),
  928. DB::raw("ISNULL(b.iPerTaxRate, 0) as rate"),
  929. DB::raw("ISNULL(b.iUnitPrice, 0) as price"),
  930. DB::raw("ISNULL(b.iQuantity, 0) as po_qty"),
  931. DB::raw("ISNULL(b.iReceivedQTY, 0) as received_qty"),
  932. DB::raw("(ISNULL(b.iQuantity, 0) - ISNULL(b.iReceivedQTY, 0)) as available_qty"),
  933. // ==================== 新增:主表强控制联动校验字段 ====================
  934. DB::raw("ISNULL(a.cBusType, '') as cBusType"), // 业务类型
  935. DB::raw("ISNULL(a.cVenCode, '') as cVenCode"), // 供应商编码
  936. DB::raw("ISNULL(a.cexch_name, '人民币') as cexch_name"), // 币种
  937. DB::raw("CAST(ISNULL(a.nflat, 1.0) AS DECIMAL(10,4)) as nflat"),// 汇率
  938. DB::raw("ISNULL(a.iDiscountTaxType, 0) as iDiscountTaxType") // 扣税类别
  939. )
  940. ->orderBy('a.cPOID', 'asc')
  941. ->orderBy('b.ivouchrowno', 'asc')
  942. ->get();
  943. // 转化为普通数组返回
  944. $list = json_decode(json_encode($list), true);
  945. if (empty($list)) {
  946. return [false, '未查询到对应的采购订单明细数据'];
  947. }
  948. // ==================== 核心修改:多单合单严格一致性校验 ====================
  949. if (count($code) > 1) {
  950. // 1. 校验供应商
  951. $venCodes = array_unique(array_column($list, 'cVenCode'));
  952. if (count($venCodes) > 1) return [false, '只能同时选择【供应商】相同的采购订单进行合并入库!'];
  953. // 2. 校验业务类型
  954. $busTypes = array_unique(array_column($list, 'cBusType'));
  955. if (count($busTypes) > 1) return [false, '只能同时选择【业务类型】相同的采购订单进行合并入库!'];
  956. // 3. 校验币种
  957. $exchNames = array_unique(array_column($list, 'cexch_name'));
  958. if (count($exchNames) > 1) return [false, '只能同时选择【币种】相同的采购订单进行合并入库!'];
  959. // 4. 校验汇率 (转成 float 排除数据库浮点数末尾 0 的干扰)
  960. $nflats = array_unique(array_map('floatval', array_column($list, 'nflat')));
  961. if (count($nflats) > 1) return [false, '只能同时选择【汇率】相同的采购订单进行合并入库!'];
  962. // 5. 校验扣税类别
  963. $taxTypes = array_unique(array_column($list, 'iDiscountTaxType'));
  964. if (count($taxTypes) > 1) return [false, '只能同时选择【扣税类别】相同的采购订单进行合并入库!'];
  965. }
  966. // ====================================================================
  967. return [true, $list];
  968. }
  969. }