OLDataProvider.m 516 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344
  1. //
  2. // OLDataProvider.m
  3. // iSales-NPD
  4. //
  5. // Created by Ray on 2/2/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "OLDataProvider.h"
  9. #import "iSalesDB.h"
  10. #import "RAUtils.h"
  11. #import "AESCrypt.h"
  12. #import "AppDelegate.h"
  13. #import "ZipArchive.h"
  14. #import "UIProgressView+AFNetworking.h"
  15. #import "AFHTTPSessionManager.h"
  16. #import "AppDelegate.h"
  17. #import "pdfCreator.h"
  18. #import "Singleton.h"
  19. #import "QRCodeGenerator.h"
  20. #import "config.h"
  21. @interface OLDataProvider ()
  22. @end
  23. @implementation OLDataProvider
  24. +(NSString*) get_portfolio_price:(NSString*) contact_id item_id:(int) item_id price:(int)price_id db:(sqlite3*)db
  25. {
  26. NSString * sqlQuery = [NSString stringWithFormat:@"select decrypt(price) from model_price where item_id=%d and price_name in (select name from price where type=%d) ",item_id,price_id];
  27. NSString* ret=@"No Price.";
  28. sqlite3_stmt * statement;
  29. // int count=0;
  30. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  31. {
  32. if (sqlite3_step(statement) == SQLITE_ROW)
  33. {
  34. char *price = (char*)sqlite3_column_text(statement, 0);
  35. if(price==nil)
  36. price="";
  37. NSString *nsprice = [[NSString alloc]initWithUTF8String:price];
  38. double dp= [nsprice doubleValue];
  39. ret=[NSString stringWithFormat:@"%.2f",dp];
  40. }
  41. sqlite3_finalize(statement);
  42. }
  43. return ret;
  44. // // [iSalesDB close_db:db];
  45. //
  46. // return nil;
  47. //// if(dprice==DBL_MAX)
  48. //// ret= nil;
  49. //// else
  50. //// ret= [NSNumber numberWithDouble:dprice];
  51. //// return ret;
  52. }
  53. + (NSString *)portfolio_img:(NSString *)item_id db:(sqlite3 *)db
  54. {
  55. NSString* ret= nil;
  56. NSString *sqlQuery = nil;
  57. // sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.item_id=%@ order by i.default_img desc, i._id asc limit 1;",item_id];// select i.url from model m
  58. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image as i where item_id = %@ and i.type<=1 order by i.default_img desc, i.type desc limit 1;",item_id];
  59. sqlite3_stmt * statement;
  60. // int count=0;
  61. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  62. {
  63. if (sqlite3_step(statement) == SQLITE_ROW)
  64. {
  65. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  66. if(imgurl==nil)
  67. imgurl="";
  68. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  69. ret=nsimgurl;
  70. }
  71. sqlite3_finalize(statement);
  72. }
  73. else
  74. {
  75. ret=@"";
  76. }
  77. // [iSalesDB close_db:db];
  78. // DebugLog(@"data string: %@",ret );
  79. return ret;
  80. }
  81. +(NSMutableDictionary*) prepare_portfolio_data:(NSMutableDictionary*) params
  82. {
  83. NSMutableDictionary* values=params[@"replaceValue"];
  84. //生成portfolio pdf需要的数据
  85. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  86. data[@"npd_url"]=@"www.newpacificdirect.com";
  87. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  88. [formatter setDateFormat:@"MMMM yyyy"];
  89. NSString* date = [formatter stringFromDate:[NSDate date]];
  90. data[@"create_month"]=date;
  91. data[@"company_name"]=@"New Pacific Direct,Inc.";
  92. data[@"catalog_name"]=params[@"catalog_name"];
  93. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  94. data[@"TOTAL_PAGE"]=0;
  95. data[@"CURRENT_PAGE"]=0;
  96. NSMutableDictionary* grid = [[NSMutableDictionary alloc]init];
  97. bool show_stockout=[params[@"show_stockout"] boolValue];
  98. NSString *sql = nil;
  99. if(values==nil)
  100. {
  101. sql=[NSString stringWithFormat:@"select a.product_id,a.name,a.description,a.price,a.sheet_discount,a.qty,a.is_percentage,a.item_id,a.fashion_id,a.line_note,a.percent,a._id,b.availability,b.color,b.legcolor,b.demension,b.seat_height,b.material,b.box_dim,b.volume,b.weight,stockuom,b.load_ability,b.fabric_content,b.assembling,b.made_in from (select _id, product_id,name,description,ifnull(sheet_price,'null') as price,sheet_discount,ifnull(available_qty,'null') as qty,percentage as is_percentage,item_id,fashion_id,line_note,percent from offline_portfolio where product_id in(%@)) a left join (select * from product where product_id in(%@)) b on a.product_id=b.product_id order by a.name",params[@"product_ids"],params[@"product_ids"]];
  102. }
  103. else
  104. {
  105. sql=[NSString stringWithFormat:@"select product_id,name,description,null,null,null,null,item_id,null,null,null,-1,availability,color,legcolor,demension,seat_height,material,box_dim,volume,weight,stockuom,load_ability,fabric_content,assembling,made_in from product where product_id in(%@) order by name",params[@"product_ids"]];
  106. }
  107. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  108. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  109. sqlite3 *db = [iSalesDB get_db];
  110. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  111. // [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  112. int product_id = sqlite3_column_int(stmt, 0);
  113. // NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  114. NSString *name = [self textAtColumn:1 statement:stmt];
  115. NSString *description = [self textAtColumn:2 statement:stmt];
  116. double price = sqlite3_column_double(stmt, 3);
  117. NSString *s_price = [self textAtColumn:3 statement:stmt];
  118. double discount = sqlite3_column_double(stmt,4);
  119. int qty = sqlite3_column_int(stmt, 5);
  120. NSString *s_qty = [self textAtColumn:5 statement:stmt];
  121. int is_percentage = sqlite3_column_int(stmt, 6);
  122. NSString *s_is_percentage = [self textAtColumn:6 statement:stmt];
  123. int item_id = sqlite3_column_int(stmt, 7);
  124. // int fashion_id = sqlite3_column_int(stmt, 8);
  125. NSString *line_note = [self textAtColumn:9 statement:stmt];
  126. NSString *s_percent = [self textAtColumn:10 statement:stmt];
  127. double percent = sqlite3_column_double(stmt, 10);
  128. // int portfolio_id = sqlite3_column_int(stmt, 11);
  129. NSString *s_availability = [self textAtColumn:12 statement:stmt];
  130. int availability = sqlite3_column_int(stmt, 12);
  131. NSString *color = [self textAtColumn:13 statement:stmt];
  132. NSString *legcolor = [self textAtColumn:14 statement:stmt];
  133. NSString *demension = [self textAtColumn:15 statement:stmt];
  134. NSString *seat_height = [self textAtColumn:16 statement:stmt];
  135. NSString *material = [self textAtColumn:17 statement:stmt];
  136. NSString *box_dim = [self textAtColumn:18 statement:stmt];
  137. NSString *s_volume = [self textAtColumn:19 statement:stmt];
  138. double volume = sqlite3_column_double(stmt, 19);
  139. NSString *s_weight = [self textAtColumn:20 statement:stmt];
  140. double weight = sqlite3_column_double(stmt, 20);
  141. NSString *s_model_set = [self textAtColumn:21 statement:stmt];
  142. int model_set = sqlite3_column_int(stmt, 21);
  143. NSString *s_load_ability = [self textAtColumn:22 statement:stmt];
  144. double load_ability = sqlite3_column_double(stmt, 22);
  145. NSString *fabric_content = [self textAtColumn:23 statement:stmt];
  146. NSString *assembling = [self textAtColumn:24 statement:stmt];
  147. NSString *made_in = [self textAtColumn:25 statement:stmt];
  148. NSString* gprice = [self get_portfolio_price :appDelegate.contact_id item_id:item_id price:[params[@"regular_price"] intValue] db:db];
  149. if(is_percentage==0&&qty==0&&!show_stockout)
  150. {
  151. return;
  152. }
  153. if((is_percentage!=0||s_is_percentage.length==0)&&availability==0&&!show_stockout)
  154. {
  155. return;
  156. }
  157. if(s_percent.length==0||is_percentage==0)
  158. {
  159. percent=100.0;
  160. if([s_qty isEqualToString:@"null"])
  161. qty=availability;
  162. }
  163. else
  164. {
  165. qty=availability;
  166. }
  167. qty=qty*percent/100+0.5;
  168. for(int i=0;i<[values[@"count"] intValue];i++)
  169. {
  170. NSMutableDictionary * valueitem=values[[NSString stringWithFormat:@"item_%d",i]];;
  171. if([valueitem[@"product_id"] intValue]==product_id)
  172. {
  173. line_note=valueitem[@"line_note"];
  174. qty=[valueitem[@"available_qty"] intValue];
  175. if([[valueitem allKeys] containsObject:@"available_qty"])
  176. s_qty=[NSString stringWithFormat:@"%d",qty];
  177. else
  178. s_qty=@"null";
  179. if(valueitem[@"regular_price"]!=nil)
  180. gprice=[NSString stringWithFormat:@"%.2f",[valueitem[@"regular_price"] floatValue]];
  181. s_price=valueitem[@"special_price"];
  182. price=[valueitem[@"special_price"] floatValue];
  183. discount=0;
  184. }
  185. }
  186. NSString* set_price=@"";
  187. if([params[@"entered_price"] boolValue])
  188. {
  189. if (s_price==nil ) {
  190. // set_price=[NSString stringWithFormat:@"Special: $ %@",gprice];
  191. }
  192. else
  193. {
  194. if(price*(1-discount/100.0)!=gprice.floatValue)
  195. set_price=[NSString stringWithFormat:@"Special: $ %.2f",price*(1-discount/100.0)];
  196. }
  197. }
  198. NSString* get_price=@"";
  199. {
  200. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  201. // DebugLog(@"price time interval");
  202. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  203. if(gprice==nil)
  204. get_price=@"Price:No Price.";
  205. else
  206. get_price=[NSString stringWithFormat:@"Price: $ %@",gprice];
  207. }
  208. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  209. // if ([qty_null isEqualToString:@"null"]) {
  210. // qty=availability;
  211. // }
  212. //
  213. // NSString *qty_percent_null = [self textAtColumn:6 statement:stmt];
  214. // if ([qty_percent_null isEqualToString:@"null"]) {
  215. // percentage=1;
  216. // }
  217. NSString * str_availability=[NSString stringWithFormat:@"<b>Availability:</b> %d",(int)qty];
  218. NSString * str_color=[NSString stringWithFormat:@"<b>Color:</b> %@",color];
  219. NSString * str_model_set=[NSString stringWithFormat:@"<b>Set:</b> Sold in QTY of %d",model_set];
  220. NSString * str_legcolor=[NSString stringWithFormat:@"<b>Leg Color:</b> %@",legcolor];
  221. NSString * str_demension=[NSString stringWithFormat:@"<b>Dimension:</b> %@",demension];
  222. NSString * str_seat_height=[NSString stringWithFormat:@"<b>Seat Height:</b> %@",seat_height];
  223. NSString * str_meterial=[NSString stringWithFormat:@"<b>Meterial:</b> %@",material];
  224. NSString * str_box_dim=[NSString stringWithFormat:@"<b>Box Dimension:</b> %@",box_dim];
  225. NSString * str_volume=[NSString stringWithFormat:@"<b>Volume:</b> %.2f",volume];
  226. NSString * str_weight=[NSString stringWithFormat:@"<b>Weight:</b> %.2f",weight];
  227. NSString * str_load_ability=[NSString stringWithFormat:@"<b>Load Ability:</b> %.2f",load_ability];
  228. NSString * str_fabric_content=[NSString stringWithFormat:@"<b>Fabric Content:</b> %@",fabric_content];
  229. NSString * str_assembling=[NSString stringWithFormat:@"<b>Assembling:</b> %@",assembling];
  230. NSString * str_made_in=[NSString stringWithFormat:@"<b>Made In:</b> %@",made_in];
  231. NSString * str_line_note=[NSString stringWithFormat:@"<b>Line Note:</b> %@",line_note];
  232. if([params[@"availability"] boolValue]==false)
  233. str_availability=@"";
  234. else
  235. {
  236. if ([s_qty isEqualToString:@"null"] && [s_availability isEqualToString:@"0"])
  237. str_availability=@"<b>Availability:</b> In Production";
  238. }
  239. if([params[@"color"] boolValue]==false || color.length==0 )
  240. str_color=@"";
  241. if([params[@"set"] boolValue]==false || s_model_set.length==0 )
  242. str_model_set=@"";
  243. if([params[@"leg_color"] boolValue]==false || legcolor.length==0 )
  244. str_legcolor=@"";
  245. if([params[@"dimension"] boolValue]==false || demension.length==0 )
  246. str_demension=@"";
  247. if([params[@"seat_height"] boolValue]==false || seat_height.length==0 )
  248. str_seat_height=@"";
  249. if([params[@"material"] boolValue]==false || material.length==0 )
  250. str_meterial=@"";
  251. if([params[@"box_dimension"] boolValue]==false || box_dim.length==0 )
  252. str_box_dim=@"";
  253. if([params[@"volume"] boolValue]==false || s_volume.length==0 )
  254. str_volume=@"";
  255. if([params[@"weight"] boolValue]==false || s_weight.length==0 )
  256. str_weight=@"";
  257. if([params[@"load_ability"] boolValue]==false || s_load_ability.length==0 )
  258. str_load_ability=@"";
  259. if([params[@"fabric_content"] boolValue]==false || fabric_content.length==0 )
  260. str_fabric_content=@"";
  261. if([params[@"assembling"] boolValue]==false || assembling.length==0 )
  262. str_assembling=@"";
  263. if([params[@"made"] boolValue]==false || made_in.length==0 )
  264. str_made_in=@"";
  265. if([params[@"show_line_note"] boolValue]==false || line_note.length==0 )
  266. str_line_note=@"";
  267. NSMutableArray* arr_detail=[[NSMutableArray alloc]init];
  268. [arr_detail addObject:str_availability];
  269. [arr_detail addObject:str_color];
  270. [arr_detail addObject:str_model_set];
  271. [arr_detail addObject:str_legcolor];
  272. [arr_detail addObject:str_demension];
  273. [arr_detail addObject:str_seat_height];
  274. [arr_detail addObject:str_meterial];
  275. [arr_detail addObject:str_box_dim];
  276. [arr_detail addObject:str_volume];
  277. [arr_detail addObject:str_weight];
  278. [arr_detail addObject:str_load_ability];
  279. [arr_detail addObject:str_fabric_content];
  280. [arr_detail addObject:str_assembling];
  281. [arr_detail addObject:str_made_in];
  282. [arr_detail addObject:str_line_note];
  283. // NSString* detail =[NSString stringWithFormat:@"<font size=0.2>%@</font>",[RAUtils arr2string:arr_detail separator:@" " trim:true]];
  284. NSString* detail =[RAUtils arr2string:arr_detail separator:@" " trim:true];
  285. //model image;
  286. NSString* img_path = [self portfolio_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  287. // qr image
  288. NSString* qrpath=nil;
  289. if([params[@"show_barcode"] boolValue])
  290. {
  291. NSString* temp = NSTemporaryDirectory();
  292. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  293. qrpath=[temp stringByAppendingPathComponent:filename];
  294. UIImage* qrimg=[QRCodeGenerator qrImageForString:name imageSize:240];
  295. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  296. }
  297. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  298. item[@"img"]=img_path;
  299. item[@"img_qr"]=qrpath;
  300. item[@"special_price"]=set_price;
  301. item[@"price"]=get_price;
  302. item[@"name"]=name;
  303. item[@"description"]=description;
  304. item[@"detail"]=detail;
  305. item[@"img"]=img_path;
  306. // @{
  307. // //@"linenotes": line_note,
  308. //// @"product_id": product_id_string,
  309. //// @"available_qty": @(qty),
  310. //// @"available_percent" : @(percent),
  311. //// @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  312. //// @"item_id": [NSString stringWithFormat:@"%d",item_id],
  313. //// @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  314. // @"": ,
  315. // @"": ,
  316. // @"": ,
  317. // @"": ,
  318. // @"": ,
  319. // @"": ,
  320. // @"":
  321. // }.mutableCopy;
  322. // if (percentage) {
  323. // [item removeObjectForKey:@"available_qty"];
  324. // } else {
  325. // [item removeObjectForKey:@"available_percent"];
  326. // }
  327. //
  328. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  329. // if ([qty_null isEqualToString:@"null"]) {
  330. // [item removeObjectForKey:@"available_qty"];
  331. // }
  332. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  333. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  334. }];
  335. [iSalesDB close_db:db];
  336. // [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  337. // [dic setValue:@"" forKey:@"email_content"];
  338. // [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  339. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  340. //
  341. // NSMutableDictionary* cell = [[NSMutableDictionary alloc]init];
  342. // cell[@"count"]=[NSNumber numberWithInt:10];
  343. grid[@"cell0"]=dic;
  344. data[@"grid0"]=grid;
  345. return data;
  346. }
  347. +(NSMutableDictionary*) get_pdftemplate:(NSString*) template
  348. {
  349. NSString *default_path = [[NSBundle mainBundle] pathForResource:template ofType:nil];
  350. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  351. NSString *cache_folder=[paths objectAtIndex:0];
  352. NSString *template_path = [cache_folder stringByAppendingPathComponent:[NSString stringWithFormat:@"offline_data/%@",template]];
  353. BOOL bdir=NO;
  354. NSFileManager* fileManager = [NSFileManager defaultManager];
  355. if(PDF_DEBUG)
  356. {
  357. NSData *data = [NSData dataWithContentsOfFile:default_path];
  358. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  359. return ret;
  360. }
  361. if(! [fileManager fileExistsAtPath:template_path isDirectory:&bdir] )
  362. {
  363. NSError * error=nil;
  364. if(![fileManager copyItemAtPath:default_path toPath:template_path error:&error])
  365. {
  366. return nil;
  367. }
  368. }
  369. NSData *data = [NSData dataWithContentsOfFile:template_path];
  370. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  371. return ret;
  372. }
  373. +(NSMutableDictionary*) query_login:(NSString*) user db:(sqlite3*) db
  374. {
  375. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  376. NSString *sqlQuery = [NSString stringWithFormat:@"select can_show_price,can_see_price,contact_id,user_type,can_cancel_order,can_set_cart_price,can_create_portfolio,can_delete_order,can_submit_order,can_set_tearsheet_price,can_create_order,mode,username,can_update_contact_info,first_name from offline_login where lower(username)='%@' ",user.lowercaseString ];
  377. DebugLog(@"offline_login sql:%@",sqlQuery);
  378. sqlite3_stmt * statement;
  379. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  380. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  381. {
  382. if (sqlite3_step(statement) == SQLITE_ROW)
  383. {
  384. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  385. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  386. int can_show_price = sqlite3_column_int(statement, 0);
  387. int can_see_price = sqlite3_column_int(statement, 1);
  388. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  389. if(contact_id==nil)
  390. contact_id="";
  391. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  392. int user_type = sqlite3_column_int(statement, 3);
  393. int can_cancel_order = sqlite3_column_int(statement, 4);
  394. int can_set_cart_price = sqlite3_column_int(statement, 5);
  395. int can_create_portfolio = sqlite3_column_int(statement, 6);
  396. int can_delete_order = sqlite3_column_int(statement, 7);
  397. int can_submit_order = sqlite3_column_int(statement, 8);
  398. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  399. int can_create_order = sqlite3_column_int(statement, 10);
  400. char *mode = (char*)sqlite3_column_text(statement, 11);
  401. if(mode==nil)
  402. mode="";
  403. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  404. char *username = (char*)sqlite3_column_text(statement, 12);
  405. if(username==nil)
  406. username="";
  407. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  408. int can_update_contact_info = sqlite3_column_int(statement, 13);
  409. char *first_name = (char*)sqlite3_column_text(statement, 14);
  410. if(first_name==nil)
  411. first_name="";
  412. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  413. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  414. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  415. // [header setValue:nscontact_id forKey:@"contact_id"];
  416. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  417. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  418. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  419. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  420. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  421. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  422. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  423. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  424. //
  425. // [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  426. //
  427. // [header setValue:nsusername forKey:@"username"];
  428. //
  429. // NSError* error=nil;
  430. //
  431. // NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  432. // [header setValue:statusFilter forKey:@"statusFilter"];
  433. //
  434. //
  435. //
  436. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  437. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  438. //
  439. // // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  440. // // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  441. // int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  442. // [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  443. //
  444. // [ret setObject:header forKey:@"header"];
  445. [ret setValue:nsfirst_name forKey:@"first_name"];
  446. }
  447. sqlite3_finalize(statement);
  448. }
  449. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  450. return ret;
  451. }
  452. +(NSMutableDictionary*) prepare_salesorder_data:(NSString*) order_code type:(int) type
  453. {
  454. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  455. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  456. data[@"npd_url"]=@"www.newpacificdirect.com";
  457. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  458. [formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
  459. NSString* date = [formatter stringFromDate:[NSDate date]];
  460. data[@"print_date"]=date;
  461. data[@"company_name"]=@"New Pacific Direct,Inc.";
  462. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  463. data[@"TOTAL_PAGE"]=0;
  464. data[@"CURRENT_PAGE"]=0;
  465. // [formatter setDateFormat:@"MM/dd/yyyy"];
  466. // date = [formatter stringFromDate:[NSDate date]];
  467. // data[@"creat_date"]=date;
  468. NSString* temp = NSTemporaryDirectory();
  469. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  470. NSString* qrpath=[temp stringByAppendingPathComponent:filename];
  471. UIImage* qrimg=[RAUtils generateBarCode:order_code width:480 height:44];//[QRCodeGenerator qrImageForString:order_code imageSize:240];
  472. bool bwrite=[UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  473. data[@"img_barcode"]=qrpath;
  474. data[@"so_id"]=order_code;
  475. data[@"company"]=@"NEW PACIFIC DIRECT, INC.";
  476. // NSString *sql = [NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),decrypt(o.credit_card_expiration_month),o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus,decrypt(o.credit_card_expiration_year),o.sign_picpath,o.customer_contact,o.customer_email,o.customer_phone,o.customer_fax,o.editable,o.lift_null,o.handling_null,o.ship_via from (select _id,so_id,poNumber,create_time,status,customer_cid,customer_contact,logist,shipping,lift_gate_value,general_notes,internal_notes,paymentType,lift_gate,receive_name,receive_contact,receive_ext,sender_name,sender_contact,sender_ext,shipping_billto_name,shipping_billto_contact,shipping_billto_ext,billing_name,billing_contact,billing_ext,returnto_name,returnto_contact,returnto_ext,handling_fee_value,paymentsAndCredits,total_price,customer_cid,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,credit_card_number,credit_card_security_code,credit_card_expiration_month,credit_card_city,credit_card_state,ifnull(shipping,'ship_null') as shipNull,erpOrderStatus,credit_card_expiration_year,sign_picpath,customer_contact,customer_email,customer_phone,customer_fax,order_id is null as editable,lift_gate_value is null as lift_null,handling_fee_value is null as handling_null,ship_via from offline_order where so_id=%@) o left join offline_contact c on o.customer_cid=c.contact_id",order_code];
  477. NSString *sql = [NSString stringWithFormat:@"select erpOrderStatus,sender_name,sender_ext,sender_phone,sender_fax,sender_email,warehouse_name,billing_name,billing_ext,billing_phone,billing_fax,billing_email,receive_name,receive_ext,receive_phone,receive_fax,receive_email,poNumber,sales_rep,carrier,vendor_no,terms,sales_terms,port_destination,etd,sign_picpath,status,create_time,shipping,lift_gate_value,handling_fee_value,billing_contact,receive_contact,general_notes,customer_cid from offline_order where so_id='%@'",order_code];
  478. sqlite3 *db = [iSalesDB get_db];
  479. data[@"print_by"]= [self query_login:appDelegate.user db:db][@"first_name"];
  480. sqlite3_stmt * statement;
  481. NSMutableDictionary* datasource = [[NSMutableDictionary alloc]init];
  482. NSMutableDictionary* row0 = [[NSMutableDictionary alloc]init];
  483. row0[@"count"]=[NSNumber numberWithInt:1];
  484. datasource[@"row0"]=row0;
  485. NSString* print_status=@"";
  486. NSString* order_type=@"Offline Quote";
  487. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  488. {
  489. if (sqlite3_step(statement) == SQLITE_ROW)
  490. {
  491. int erpOrderStatus = sqlite3_column_int(statement, 0);
  492. int status = sqlite3_column_int(statement, 26);
  493. if(status==2)
  494. {
  495. switch (erpOrderStatus) {
  496. case 10:
  497. // order_type=@"Quoted Order";
  498. print_status=@"Quote";
  499. break;
  500. case 11:
  501. print_status=@"Booked";
  502. break;
  503. case 12:
  504. print_status=@"Picked";
  505. break;
  506. case 13:
  507. print_status=@"Shipped";
  508. break;
  509. case 14:
  510. print_status=@"Closed";
  511. break;
  512. case 15:
  513. print_status=@"Canceled";
  514. break;
  515. case 16:
  516. print_status=@"All";
  517. break;
  518. case 18:
  519. print_status=@"Processing";
  520. break;
  521. default:
  522. break;
  523. }
  524. }
  525. else{
  526. switch (status) {
  527. case 0:
  528. {
  529. print_status=@"Quote";
  530. break;
  531. }
  532. case 1:
  533. {
  534. print_status=@"Saved";
  535. break;
  536. }
  537. case 3:
  538. {
  539. //canceled
  540. print_status=@"Canceled";
  541. break;
  542. }
  543. default:
  544. break;
  545. }
  546. }
  547. NSString* sender_name = [self textAtColumn:1 statement:statement];
  548. NSString* sender_ext = [self textAtColumn:2 statement:statement];
  549. NSString* sender_phone = [self textAtColumn:3 statement:statement];
  550. NSString* sender_fax = [self textAtColumn:4 statement:statement];
  551. NSString* sender_email = [self textAtColumn:5 statement:statement];
  552. NSString* warehouse_name = [self textAtColumn:6 statement:statement];
  553. NSString* billing_name = [self textAtColumn:7 statement:statement];
  554. NSString* billing_ext = [self textAtColumn:8 statement:statement];
  555. NSString* billing_phone = [self textAtColumn:9 statement:statement];
  556. if(billing_phone.length>0)
  557. billing_phone= [@"TEL: " stringByAppendingString:billing_phone];
  558. NSString* billing_fax = [self textAtColumn:10 statement:statement];
  559. if(billing_fax.length>0)
  560. billing_fax= [@"FAX: " stringByAppendingString:billing_fax];
  561. NSMutableArray* billto_arr0 = [[NSMutableArray alloc]init];
  562. [billto_arr0 addObject:billing_phone];
  563. [billto_arr0 addObject:billing_fax];
  564. NSString* billing_pf=[RAUtils arr2string:billto_arr0 separator:@" " trim:true];
  565. NSString* billing_email = [self textAtColumn:11 statement:statement];
  566. if(billing_email.length>0)
  567. billing_email= [@"EMAIL: " stringByAppendingString:billing_email];
  568. NSString* billing_contact = [self textAtColumn:31 statement:statement];
  569. if(billing_contact.length>0)
  570. billing_contact= [@"ATTN: " stringByAppendingString:billing_contact];
  571. NSString* receive_name = [self textAtColumn:12 statement:statement];
  572. NSString* receive_ext = [self textAtColumn:13 statement:statement];
  573. NSString* receive_phone = [self textAtColumn:14 statement:statement];
  574. if(receive_phone.length>0)
  575. receive_phone= [@"TEL: " stringByAppendingString:receive_phone];
  576. NSString* receive_fax = [self textAtColumn:15 statement:statement];
  577. if(receive_fax.length>0)
  578. receive_fax= [@"FAX: " stringByAppendingString:receive_fax];
  579. NSMutableArray* receive_arr0 = [[NSMutableArray alloc]init];
  580. [receive_arr0 addObject:receive_phone];
  581. [receive_arr0 addObject:receive_fax];
  582. NSString* receive_email = [self textAtColumn:16 statement:statement];
  583. NSString* receive_pf=[RAUtils arr2string:billto_arr0 separator:@" " trim:true];
  584. if(receive_email.length>0)
  585. receive_email= [@"EMAIL: " stringByAppendingString:receive_email];
  586. NSString* receive_contact = [self textAtColumn:32 statement:statement];
  587. if(receive_contact.length>0)
  588. receive_contact= [@"ATTN: " stringByAppendingString:receive_contact];
  589. NSString* poNumber = [self textAtColumn:17 statement:statement];
  590. NSString* sales_rep = [self textAtColumn:18 statement:statement];
  591. NSString* carrier = [self textAtColumn:19 statement:statement];
  592. NSString* vendor_no = [self textAtColumn:20 statement:statement];
  593. NSString* terms = [self textAtColumn:21 statement:statement];
  594. NSString* sales_terms = [self textAtColumn:22 statement:statement];
  595. NSString* port_destination = [self textAtColumn:23 statement:statement];
  596. NSString* etd = [self textAtColumn:24 statement:statement];
  597. NSString* sign_picpath = [self textAtColumn:25 statement:statement];
  598. if(sign_picpath.length==0)
  599. sign_picpath=nil;
  600. NSString* create_date = [self textAtColumn:27 statement:statement];
  601. create_date=[self changeDateTime:create_date Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy"];
  602. NSString* general_notes=[self textAtColumn:33 statement:statement];
  603. NSString *customerID = [self textAtColumn:34 statement:statement];
  604. if (customerID.length == 0) {
  605. customerID = nil;
  606. }
  607. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  608. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  609. if(general_notes.length>0)
  610. {
  611. NSMutableDictionary* row2 = [[NSMutableDictionary alloc]init];
  612. row2[@"item_0"]=@{@"g_notes":general_notes };
  613. row2[@"count"]=[NSNumber numberWithInt:1];
  614. datasource[@"row2"]=row2;
  615. }
  616. NSMutableArray* billto_arr = [[NSMutableArray alloc]init];
  617. [billto_arr addObject:billing_ext];
  618. [billto_arr addObject:billing_pf];
  619. [billto_arr addObject:billing_contact];
  620. [billto_arr addObject:billing_email];
  621. NSString* billto=[RAUtils arr2string:billto_arr separator:@"\r\n" trim:true];
  622. NSMutableArray* receive_arr = [[NSMutableArray alloc]init];
  623. [receive_arr addObject:receive_ext];
  624. [receive_arr addObject:receive_pf];
  625. [receive_arr addObject:receive_contact];
  626. [receive_arr addObject:receive_email];
  627. NSString* receive=[RAUtils arr2string:receive_arr separator:@"\r\n" trim:true];
  628. float shipping = sqlite3_column_double(statement, 28);
  629. float lift_gate_value = sqlite3_column_double(statement, 29);
  630. float handling_fee_value = sqlite3_column_double(statement, 30);
  631. data[@"company"]=sender_name;
  632. data[@"shipfrom_addr"]=sender_ext;
  633. data[@"shipfrom_phone"]=sender_phone;
  634. data[@"shipfrom_fax"]=sender_fax;
  635. data[@"shipfrom_email"]=sender_email;
  636. data[@"warehouse_name"]=warehouse_name;
  637. data[@"bill_to_name"]=billing_name;
  638. data[@"bill_to_ext"]=billto;
  639. data[@"ship_to_name"]=receive_name;
  640. data[@"ship_to_ext"]=receive;
  641. data[@"po_no"]=poNumber;
  642. data[@"rep"]=sales_rep;
  643. data[@"e_schdule"]=etd;
  644. data[@"sales_terms"]=sales_terms;
  645. data[@"port_destination"]=port_destination;
  646. data[@"terms"]=terms;
  647. data[@"carrier"]=carrier;
  648. data[@"vendor_no"]=vendor_no;
  649. data[@"customer_sign"]=sign_picpath;
  650. data[@"print_status"]=print_status;
  651. data[@"create_date"]=create_date;
  652. NSString *sql_cart=[ NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id,modify_time from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id ",order_code];;
  653. sqlite3_stmt * statement_cart;
  654. // NSDate *date1 = [NSDate date];
  655. int count=0;
  656. int cart_count=0;
  657. float credits=0;
  658. int dbresult=sqlite3_prepare_v2(db, [sql_cart UTF8String], -1, &statement_cart, nil);
  659. if ( dbresult== SQLITE_OK)
  660. {
  661. NSMutableDictionary* row1 = [[NSMutableDictionary alloc]init];
  662. int row1count=0;
  663. while (sqlite3_step(statement_cart) == SQLITE_ROW)
  664. {
  665. NSDate *row_date = [NSDate date];
  666. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  667. // int product_id = sqlite3_column_int(statement_cart, 0);
  668. char *str_price = (char*)sqlite3_column_text(statement_cart, 1);
  669. int item_id = sqlite3_column_int(statement_cart, 7);
  670. double discount = sqlite3_column_double(statement_cart, 2);
  671. NSString* Price=nil;
  672. if(str_price==nil)
  673. {
  674. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  675. // NSDate *price_date = [NSDate date];
  676. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db];
  677. // DebugLog(@"price time interval");
  678. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  679. if(price==nil)
  680. Price=@"No Price.";
  681. else
  682. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  683. }
  684. else
  685. {
  686. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  687. }
  688. float dunitprice=0;
  689. int item_count = sqlite3_column_int(statement_cart, 3);
  690. NSString* Amount=@"";
  691. if(![Price isEqualToString:@"No Price."])
  692. {
  693. dunitprice=[Price floatValue];
  694. dunitprice=dunitprice* (1-discount/100.0);
  695. Price=[NSString stringWithFormat:@"%.2f",dunitprice];
  696. credits+=dunitprice*item_count;
  697. Amount=[NSString stringWithFormat:@"%.2f",dunitprice*item_count];
  698. }
  699. else
  700. {
  701. Price=@"";
  702. }
  703. // char *line_note = (char*)sqlite3_column_text(statement_cart, 4);
  704. // NSString *nsline_note=nil;
  705. // if(line_note!=nil)
  706. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  707. char *name = (char*)sqlite3_column_text(statement_cart, 5);
  708. NSString *nsname=nil;
  709. if(name!=nil)
  710. nsname= [[NSString alloc]initWithUTF8String:name];
  711. char *description = (char*)sqlite3_column_text(statement_cart, 6);
  712. NSString *nsdescription=nil;
  713. if(description!=nil)
  714. nsdescription= [[NSString alloc]initWithUTF8String:description];
  715. // int stockUom = sqlite3_column_int(statement_cart, 8);
  716. // int _id = sqlite3_column_int(statement_cart, 9);
  717. // NSDate *subtotal_date = [NSDate date];
  718. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  719. // DebugLog(@"subtotal_date time interval");
  720. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  721. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  722. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  723. // int carton=[bsubtotaljson[@"carton"] intValue];
  724. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  725. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  726. // NSDate *img_date = [NSDate date];
  727. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  728. // DebugLog(@"img_date time interval");
  729. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  730. itemjson[@"model"]=nsname;
  731. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  732. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  733. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  734. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  735. // itemjson[@"check"]=@"true";
  736. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  737. itemjson[@"count"]=[NSString stringWithFormat:@"%d",item_count];
  738. itemjson[@"unit_price"]=Price;
  739. itemjson[@"amount"]=Amount;
  740. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  741. itemjson[@"description"]=[NSString stringWithFormat:@"%@",nsdescription ];
  742. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  743. // itemjson[@"note"]=nsline_note;
  744. // NSDate *date2 = [NSDate date];
  745. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  746. // DebugLog(@"model_bundle time interval");
  747. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  748. itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  749. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=itemjson;
  750. row1count++;
  751. NSDictionary* combine =[self model_bundle:item_id db:db compute_part:false contactID:customerID];
  752. for(int c=0;c<[combine[@"count"] intValue];c++)
  753. {
  754. NSMutableDictionary* combine_item=[combine[[NSString stringWithFormat:@"item_%d",c]] mutableCopy];
  755. combine_item[@"count"]=[NSString stringWithFormat:@"%d",[combine_item[@"modulus"] intValue]*item_count];
  756. // combine_item[@"count"]=[NSNumber numberWithInt:[combine_item[@"modulus"] intValue]*item_count];
  757. double combine_amount=[combine_item[@"modulus"] intValue]*item_count* [combine_item[@"unit_price"] floatValue];
  758. credits+=combine_amount;
  759. combine_item[@"amount"]=[NSString stringWithFormat:@"%.2f",combine_amount];
  760. combine_item[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,c+2];
  761. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=combine_item;
  762. row1count++;
  763. }
  764. count++;
  765. // DebugLog(@"row time interval");
  766. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  767. }
  768. row1[@"count"]=[NSNumber numberWithInt:row1count];
  769. datasource[@"row1"]=row1;
  770. data[@"table_model"]=datasource;
  771. sqlite3_finalize(statement_cart);
  772. }
  773. NSMutableDictionary* fee = [[NSMutableDictionary alloc]init];
  774. NSMutableDictionary* fee_row0 = [[NSMutableDictionary alloc]init];
  775. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  776. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  777. // if(credits>0)
  778. fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  779. // else
  780. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": @""};
  781. if(shipping>0)
  782. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": [NSString stringWithFormat:@"%.2f",shipping]};
  783. else
  784. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": @""};
  785. if(lift_gate_value>0)
  786. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",lift_gate_value]};
  787. else
  788. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": @""};
  789. if(handling_fee_value>0)
  790. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",handling_fee_value]};
  791. else
  792. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": @""};
  793. float total = credits+shipping+lift_gate_value+handling_fee_value;
  794. if(total>0)
  795. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": [NSString stringWithFormat:@"%.2f",total]};
  796. else
  797. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": @""};
  798. fee_row0[@"count"]=[NSNumber numberWithInt:5];
  799. fee[@"row0"]=fee_row0;
  800. data[@"table_fee"]=fee;
  801. // NSString *sign_url = [self textAtColumn:51 statement:statement];
  802. // ret[@"sign_url"] = sign_url;
  803. //
  804. // customer_contact = [self textAtColumn:52 statement:statement];
  805. // customer_email = [self textAtColumn:53 statement:statement];
  806. // customer_phone = [self textAtColumn:54 statement:statement];
  807. // customer_fax = [self textAtColumn:55 statement:statement];
  808. //
  809. //
  810. // int offline_edit=sqlite3_column_int(statement, 56);
  811. //
  812. // ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  813. //
  814. // char *soid = (char*)sqlite3_column_text(statement, 1);
  815. // if(soid==nil)
  816. // soid= "";
  817. // nssoid= [[NSString alloc]initWithUTF8String:soid];
  818. // // so#
  819. // ret[@"so#"] = nssoid;
  820. //
  821. // char *poNumber = (char*)sqlite3_column_text(statement, 2);
  822. // if(poNumber==nil)
  823. // poNumber= "";
  824. // NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  825. //
  826. //
  827. //
  828. // char *create_time = (char*)sqlite3_column_text(statement, 3);
  829. // if(create_time==nil)
  830. // create_time= "";
  831. // NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  832. // nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  833. //
  834. // int status = sqlite3_column_int(statement, 4);
  835. // int erpStatus = sqlite3_column_int(statement, 49);
  836. // NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  837. //
  838. // // status
  839. // if (status > 1 && status != 3) {
  840. // status = erpStatus;
  841. // } else if (status == 3) {
  842. // status = 15;
  843. // }
  844. // ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  845. // ret[@"order_status"] = nsstatus;
  846. //
  847. //
  848. // char *company_name = (char*)sqlite3_column_text(statement, 5);
  849. // if(company_name==nil)
  850. // company_name= "";
  851. // NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  852. // // company name
  853. // ret[@"company_name"] = nscompany_name;
  854. //
  855. // char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  856. // if(customer_contact==nil)
  857. // customer_contact= "";
  858. // NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  859. //
  860. //
  861. // char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  862. // if(addr_1==nil)
  863. // addr_1="";
  864. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  865. //
  866. // char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  867. // if(addr_2==nil)
  868. // addr_2="";
  869. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  870. //
  871. //
  872. // char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  873. // if(addr_3==nil)
  874. // addr_3="";
  875. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  876. //
  877. //
  878. // char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  879. // if(addr_4==nil)
  880. // addr_4="";
  881. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  882. //
  883. // NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  884. // [arr_addr addObject:nsaddr_1];
  885. // [arr_addr addObject:nsaddr_2];
  886. // [arr_addr addObject:nsaddr_3];
  887. // [arr_addr addObject:nsaddr_4];
  888. // NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  889. //
  890. //
  891. // char *logist = (char*)sqlite3_column_text(statement, 11);
  892. // if(logist==nil)
  893. // logist= "";
  894. // NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  895. // if (status == -11 || status == 10 || status == 11) {
  896. // nslogist = [self textAtColumn:59 statement:statement];
  897. // };
  898. //
  899. // NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  900. // if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  901. // shipping = @"Shipping To Be Quoted";
  902. // } else {
  903. // shippingFee = sqlite3_column_double(statement, 12);
  904. // }
  905. //
  906. // // Shipping
  907. // ret[@"Shipping"] = shipping;
  908. //
  909. // int have_lift_gate = sqlite3_column_int(statement, 17);
  910. // lift_gate = sqlite3_column_double(statement, 13);
  911. // // Liftgate Fee(No loading dock)
  912. // if (!have_lift_gate) {
  913. // lift_gate = 0;
  914. // }
  915. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  916. // if (sqlite3_column_int(statement, 57)) {
  917. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  918. // }
  919. //
  920. // char *general_notes = (char*)sqlite3_column_text(statement, 14);
  921. // if(general_notes==nil)
  922. // general_notes= "";
  923. // NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  924. //
  925. // char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  926. // if(internal_notes==nil)
  927. // internal_notes= "";
  928. // NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  929. //
  930. // char *payment_type = (char*)sqlite3_column_text(statement, 16);
  931. // if(payment_type==nil)
  932. // payment_type= "";
  933. // NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  934. //
  935. //
  936. //
  937. // // order info
  938. // orderinfo = [self textFileName:@"order_info.html"];
  939. //
  940. //
  941. // orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  942. // orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  943. // orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  944. // orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  945. //
  946. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  947. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  948. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  949. // orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  950. // orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  951. // orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  952. // orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  953. // orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  954. //
  955. // NSString *payment = nil;
  956. // if([nspayment_type isEqualToString:@"Credit Card"])
  957. // {
  958. // payment = [self textFileName:@"creditcardpayment.html"];
  959. //
  960. // NSString *card_first_name = [self textAtColumn:37 statement:statement];
  961. // NSString *card_last_name = [self textAtColumn:38 statement:statement];
  962. // NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  963. // NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  964. // NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  965. // NSString *card_type = [self textAtColumn:42 statement:statement];
  966. // if (card_type.length > 0) { // 显示星号
  967. // card_type = @"****";
  968. // }
  969. // NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  970. // if (card_number.length > 0 && card_number.length > 4) {
  971. // for (int i = 0; i < card_number.length - 4; i++) {
  972. // card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  973. // }
  974. // } else {
  975. // card_number = @"";
  976. // }
  977. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  978. // NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  979. // NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  980. // NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  981. // if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  982. // card_expiration = @"****";
  983. // }
  984. //
  985. //
  986. // NSString *card_city = [self textAtColumn:46 statement:statement];
  987. // NSString *card_state = [self textAtColumn:47 statement:statement];
  988. //
  989. // payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  990. // payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  991. // payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  992. // payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  993. // payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  994. // payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  995. // payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  996. // payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  997. // payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  998. // payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  999. //
  1000. //
  1001. // }
  1002. // else
  1003. // {
  1004. // payment=[self textFileName:@"normalpayment.html"];
  1005. // payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  1006. // }
  1007. // orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  1008. //
  1009. // ret[@"result"]= [NSNumber numberWithInt:2];
  1010. //
  1011. // // more info
  1012. // moreInfo = [self textFileName:@"more_info.html"];
  1013. //
  1014. //
  1015. // // ShipToCompany_or_&nbsp
  1016. // NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  1017. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  1018. // NSString *shipToName = [self textAtColumn:19 statement:statement];
  1019. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  1020. // NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  1021. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  1022. //
  1023. //
  1024. // // ShipFromCompany_or_&nbsp
  1025. // NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  1026. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  1027. // NSString *shipFromName = [self textAtColumn:22 statement:statement];
  1028. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  1029. // NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  1030. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  1031. //
  1032. //
  1033. // // FreightBillToCompany_or_&nbsp
  1034. // NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  1035. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  1036. // NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  1037. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  1038. // NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  1039. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  1040. //
  1041. //
  1042. // // MerchandiseBillToCompany_or_&nbsp
  1043. // NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  1044. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  1045. // NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  1046. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  1047. // NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  1048. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  1049. //
  1050. //
  1051. // // ReturnToCompany_or_&nbsp
  1052. // NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  1053. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  1054. // NSString *returnToName = [self textAtColumn:31 statement:statement];
  1055. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  1056. // NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  1057. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  1058. // //
  1059. //
  1060. // // DebugLog(@"more info : %@",moreInfo);
  1061. //
  1062. // // handling fee
  1063. // handlingFee = sqlite3_column_double(statement, 33);
  1064. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  1065. // if (sqlite3_column_int(statement, 58)) {
  1066. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  1067. // }
  1068. // //
  1069. //
  1070. // // customer info
  1071. // customerID = [self textAtColumn:36 statement:statement];
  1072. //
  1073. // // mode
  1074. // ret[@"mode"] = appDelegate.mode;
  1075. //
  1076. // // model_count
  1077. // ret[@"model_count"] = @(0);
  1078. }
  1079. sqlite3_finalize(statement);
  1080. }
  1081. [iSalesDB close_db:db];
  1082. data[@"order_type"]=order_type;
  1083. /*
  1084. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  1085. // "customer_email" = "Shui Hu";
  1086. // "customer_fax" = "";
  1087. // "customer_first_name" = F;
  1088. // "customer_last_name" = L;
  1089. // "customer_name" = ",da He Xiang Dong Liu A";
  1090. // "customer_phone" = "Hey Xuan Feng";
  1091. contactInfo[@"customer_phone"] = customer_phone;
  1092. contactInfo[@"customer_fax"] = customer_fax;
  1093. contactInfo[@"customer_email"] = customer_email;
  1094. NSString *first_name = @"";
  1095. NSString *last_name = @"";
  1096. if ([customer_contact isEqualToString:@""]) {
  1097. } else if ([customer_contact containsString:@" "]) {
  1098. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  1099. first_name = [customer_contact substringToIndex:first_space_index];
  1100. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  1101. }
  1102. contactInfo[@"customer_first_name"] = first_name;
  1103. contactInfo[@"customer_last_name"] = last_name;
  1104. ret[@"customerInfo"] = contactInfo;
  1105. // models
  1106. if (nssoid) {
  1107. __block double TotalCuft = 0;
  1108. __block double TotalWeight = 0;
  1109. __block int TotalCarton = 0;
  1110. __block double allItemPrice = 0;
  1111. NSString *modelSql = [NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id",nssoid];
  1112. sqlite3 *db1 = [iSalesDB get_db];
  1113. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1114. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1115. int product_id = sqlite3_column_int(stmt, 0);
  1116. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  1117. int item_id = sqlite3_column_int(stmt, 7);
  1118. NSString* Price=nil;
  1119. if(str_price==nil)
  1120. {
  1121. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  1122. if(price==nil)
  1123. Price=@"No Price.";
  1124. else
  1125. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1126. }
  1127. else
  1128. {
  1129. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1130. }
  1131. double discount = sqlite3_column_double(stmt, 2);
  1132. int item_count = sqlite3_column_int(stmt, 3);
  1133. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  1134. NSString *nsline_note=nil;
  1135. if(line_note!=nil)
  1136. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1137. // char *name = (char*)sqlite3_column_text(stmt, 5);
  1138. // NSString *nsname = nil;
  1139. // if(name!=nil)
  1140. // nsname= [[NSString alloc]initWithUTF8String:name];
  1141. NSString *nsname = [self textAtColumn:5 statement:stmt];
  1142. // char *description = (char*)sqlite3_column_text(stmt, 6);
  1143. // NSString *nsdescription=nil;
  1144. // if(description!=nil)
  1145. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  1146. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  1147. // int stockUom = sqlite3_column_int(stmt, 8);
  1148. // int _id = sqlite3_column_int(stmt, 9);
  1149. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  1150. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1151. double weight=[bsubtotaljson[@"weight"] doubleValue];
  1152. int carton=[bsubtotaljson[@"carton"] intValue];
  1153. TotalCuft += cuft;
  1154. TotalWeight += weight;
  1155. TotalCarton += carton;
  1156. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  1157. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  1158. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  1159. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1160. itemjson[@"note"]=nsline_note;
  1161. itemjson[@"origin_price"] = Price;
  1162. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1163. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1164. itemjson[@"order_item_status"] = @""; // 暂时不处理
  1165. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  1166. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  1167. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  1168. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  1169. if(itemjson[@"combine"] != nil)
  1170. {
  1171. // int citem=0;
  1172. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  1173. for(int bc=0;bc<bcount;bc++)
  1174. {
  1175. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  1176. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  1177. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  1178. subTotal += uprice * modulus * item_count;
  1179. }
  1180. }
  1181. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  1182. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  1183. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  1184. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  1185. allItemPrice += subTotal;
  1186. }];
  1187. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  1188. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  1189. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  1190. // payments/Credits
  1191. // payments_and_credist = sqlite3_column_double(statement, 34);
  1192. payments_and_credist = allItemPrice;
  1193. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1194. // // total
  1195. // totalPrice = sqlite3_column_double(statement, 35);
  1196. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1197. } else {
  1198. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  1199. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  1200. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  1201. // payments/Credits
  1202. payments_and_credist = 0;
  1203. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1204. }
  1205. // total
  1206. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  1207. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1208. ret[@"order_info"]= orderinfo;
  1209. ret[@"more_order_info"] = moreInfo;
  1210. return [RAUtils dict2data:ret];
  1211. */
  1212. DebugLog(@"debug sales order data:%@", [RAUtils dict2string:data]);
  1213. return data;
  1214. }
  1215. +(NSData*) offline_request_salesorder :(NSMutableDictionary *) params
  1216. {
  1217. NSString* template_file=nil;
  1218. switch([params[@"thumb"] intValue])
  1219. {
  1220. case 0:
  1221. template_file= @"so.json";
  1222. break;
  1223. case 1:
  1224. template_file= @"so_thumb.json";
  1225. break;
  1226. default:
  1227. template_file= @"so.json";
  1228. }
  1229. NSMutableDictionary* data = [self prepare_salesorder_data:params[@"order_code"] type:[params[@"thumb"] intValue]];
  1230. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1231. if(data[@"table_model"][@"row2"]==nil)
  1232. template[@"pages"][@"page_0"][@"contents"][@"item_0"][@"rows"][@"count"]=[NSNumber numberWithInt:1];
  1233. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1234. DebugLog(@"%@",file);
  1235. // return nil;
  1236. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1237. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1238. if (file) {
  1239. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1240. [dic setObject:file forKey:@"pdf_path"];
  1241. dic[@"isLocalFile"]=@"true";
  1242. return [RAUtils dict2data:dic];
  1243. }
  1244. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1245. dic[@"isLocalFile"]=@"true";
  1246. return [RAUtils dict2data:dic];
  1247. }
  1248. +(NSData*) offline_request_tearsheet :(NSMutableDictionary *) params
  1249. {
  1250. NSString* template_file=nil;
  1251. switch([params[@"pdf_style"] intValue])
  1252. {
  1253. case 0:
  1254. template_file= @"portfolio_2x3.json";
  1255. break;
  1256. case 1:
  1257. template_file= @"portfolio_3x2.json";
  1258. break;
  1259. default:
  1260. template_file= @"portfolio_3x2.json";
  1261. }
  1262. NSMutableDictionary* data = [self prepare_portfolio_data:params];
  1263. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1264. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1265. DebugLog(@"%@",file);
  1266. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1267. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1268. if (file) {
  1269. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1270. [dic setObject:file forKey:@"pdf_path"];
  1271. dic[@"isLocalFile"]=@"true";
  1272. return [RAUtils dict2data:dic];
  1273. }
  1274. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1275. dic[@"isLocalFile"]=@"true";
  1276. return [RAUtils dict2data:dic];
  1277. }
  1278. +(NSString*) get_offline_soid:(sqlite3*)db
  1279. {
  1280. NSString* soid=nil;
  1281. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  1282. [formatter setDateFormat:@"yyMMdd"];
  1283. NSString* date = [formatter stringFromDate:[NSDate date]];
  1284. for(int i=1;i<999;i++)
  1285. {
  1286. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  1287. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  1288. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  1289. if(count==0)
  1290. return soid;
  1291. }
  1292. return nil;
  1293. }
  1294. +(NSArray*) enumOfflineOrder
  1295. {
  1296. NSMutableArray* ret = [[NSMutableArray alloc] init];
  1297. sqlite3 *db = [iSalesDB get_db];
  1298. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  1299. sqlite3_stmt * statement;
  1300. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1301. if ( dbresult== SQLITE_OK)
  1302. {
  1303. while (sqlite3_step(statement) == SQLITE_ROW)
  1304. {
  1305. char *so_id = (char*)sqlite3_column_text(statement, 0);
  1306. NSString* nsso_id=nil;
  1307. if(so_id!=nil)
  1308. {
  1309. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  1310. }
  1311. [ret addObject:nsso_id];
  1312. }
  1313. sqlite3_finalize(statement);
  1314. }
  1315. [iSalesDB close_db:db];
  1316. return ret;
  1317. }
  1318. //+(void) uploadFile:(NSString*) file
  1319. //{
  1320. // NSData* data = [NSData dataWithContentsOfFile: file];
  1321. // UIApplication * app = [UIApplication sharedApplication];
  1322. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1323. //
  1324. //
  1325. // AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  1326. //
  1327. //
  1328. //
  1329. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  1330. //
  1331. // if(appDelegate.user!=nil)
  1332. // [params setValue:appDelegate.user forKey:@"user"];
  1333. // // if(appDelegate.contact_id!=nil)
  1334. // // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  1335. // if(appDelegate.password!=nil)
  1336. // [params setValue:appDelegate.password forKey:@"password"];
  1337. //
  1338. //
  1339. // NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  1340. // [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
  1341. // } error:nil];
  1342. //
  1343. //
  1344. //
  1345. // AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  1346. //
  1347. // NSProgress *progress = nil;
  1348. //
  1349. // NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  1350. //
  1351. //
  1352. //
  1353. // if (error) {
  1354. //
  1355. // NSString* err_msg = [error localizedDescription];
  1356. // DebugLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  1357. //
  1358. //
  1359. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1360. //
  1361. // DebugLog(@"data string: %@",str);
  1362. // [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  1363. //
  1364. // } else {
  1365. // DebugLog(@"response ");
  1366. //
  1367. //
  1368. //
  1369. // NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  1370. //
  1371. // // 再将NSData转为字符串
  1372. // NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  1373. //
  1374. // // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1375. //
  1376. // DebugLog(@"data string: %@",jsonStr);
  1377. //
  1378. // NSDictionary* json = responseObject;
  1379. //
  1380. //
  1381. // if([[json valueForKey:@"result"] intValue]==2)
  1382. // {
  1383. //// NSString* img_url_down = json[@"img_url_aname"];
  1384. //// NSString* img_url_up = json[@"img_url"];
  1385. //
  1386. //
  1387. // }
  1388. // else
  1389. // {
  1390. // [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
  1391. // }
  1392. //
  1393. //
  1394. // }
  1395. // // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  1396. // }];
  1397. //
  1398. //
  1399. //
  1400. //
  1401. // // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  1402. // //
  1403. //
  1404. //
  1405. //
  1406. //
  1407. //
  1408. //
  1409. // [uploadTask resume];
  1410. //
  1411. //}
  1412. +(NSDictionary *) prepareUpload:(NSArray*) arr_order
  1413. {
  1414. NSString* serial= [[NSUUID UUID] UUIDString];
  1415. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1416. NSString *cachefolder = [paths objectAtIndex:0];
  1417. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1418. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1419. NSFileManager* fileManager = [NSFileManager defaultManager];
  1420. BOOL bdir=YES;
  1421. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  1422. {
  1423. NSError *error = nil;
  1424. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  1425. if(!bsuccess)
  1426. DebugLog(@"Create UPLOAD folder failed");
  1427. }
  1428. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1429. ret[@"contact"]=[self prepareContact:serial];
  1430. ret[@"wishlist"]=[self prepareWishlist:serial];
  1431. ret[@"order"]=[self prepareOrder:serial soid:arr_order];
  1432. ret[@"portfolio"] = [self preparePortfolio:serial];
  1433. ret[@"view_portfolio"] = [self preparePDF:serial];
  1434. NSString* str= [RAUtils dict2string:ret];
  1435. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  1436. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  1437. NSError *error=nil;
  1438. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  1439. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  1440. ZipArchive* zip = [[ZipArchive alloc] init];
  1441. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  1442. for(int i=0;i<arr_files.count;i++)
  1443. {
  1444. NSString* file=arr_files[i];
  1445. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  1446. }
  1447. if( ![zip CloseZipFile2] )
  1448. {
  1449. zippath = @"";
  1450. }
  1451. ret[@"file"]=zippath;
  1452. return ret;
  1453. }
  1454. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  1455. {
  1456. if(filename.length==0)
  1457. return false;
  1458. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1459. bool ret=false;
  1460. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1461. NSString *cachefolder = [paths objectAtIndex:0];
  1462. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  1463. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  1464. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1465. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1466. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  1467. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  1468. NSFileManager* fileManager = [NSFileManager defaultManager];
  1469. BOOL bdir=NO;
  1470. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  1471. {
  1472. NSError *error = nil;
  1473. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  1474. {
  1475. ret=false;
  1476. }
  1477. else
  1478. {
  1479. ret=true;
  1480. }
  1481. }
  1482. return ret;
  1483. }
  1484. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  1485. {
  1486. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1487. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  1488. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  1489. NSString* where=@" 1=1";
  1490. if (ver!=nil) {
  1491. where=@"is_dirty=1";
  1492. }
  1493. sqlite3 *db = [iSalesDB get_db];
  1494. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  1495. sqlite3_stmt * statement;
  1496. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1497. int count=0;
  1498. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1499. if ( dbresult== SQLITE_OK)
  1500. {
  1501. while (sqlite3_step(statement) == SQLITE_ROW)
  1502. {
  1503. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1504. int _id = sqlite3_column_int(statement, 0);
  1505. int product_id = sqlite3_column_int(statement, 1);
  1506. int item_id = sqlite3_column_int(statement, 2);
  1507. int qty = sqlite3_column_int(statement, 3);
  1508. char *create_time = (char*)sqlite3_column_text(statement, 4);
  1509. NSString* nscreate_time=nil;
  1510. if(create_time!=nil)
  1511. {
  1512. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  1513. }
  1514. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  1515. NSString* nsmodify_time=nil;
  1516. if(modify_time!=nil)
  1517. {
  1518. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  1519. }
  1520. int is_delete = sqlite3_column_int(statement, 6);
  1521. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1522. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  1523. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1524. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  1525. itemjson[@"create_time"]=nscreate_time;
  1526. itemjson[@"modify_time"]=nsmodify_time;
  1527. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  1528. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1529. count++;
  1530. }
  1531. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1532. sqlite3_finalize(statement);
  1533. }
  1534. ret[@"count"]=[NSNumber numberWithInt:count ];
  1535. [iSalesDB close_db:db];
  1536. return ret;
  1537. }
  1538. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode db:(sqlite3*)db
  1539. {
  1540. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1541. UIApplication * app = [UIApplication sharedApplication];
  1542. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1543. NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id,o.customer_cid from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id,so_no from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id left join offline_order as o on c.so_no = o.so_id;",orderCode ];
  1544. // NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,decrypt(c.str_price),c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from offline_cart c left join product m on c.product_id=m.product_id where c.so_no='%@'",orderCode ];
  1545. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  1546. DebugLog(@"offline_login sql:%@",sqlQuery);
  1547. sqlite3_stmt * statement;
  1548. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1549. int count=0;
  1550. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1551. if ( dbresult== SQLITE_OK)
  1552. {
  1553. while (sqlite3_step(statement) == SQLITE_ROW)
  1554. {
  1555. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1556. char *customer_cid = (char *)sqlite3_column_text(statement, 10);
  1557. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  1558. int product_id = sqlite3_column_int(statement, 0);
  1559. char *str_price = (char*)sqlite3_column_text(statement, 1);
  1560. int item_id = sqlite3_column_int(statement, 7);
  1561. NSString* Price=nil;
  1562. if(str_price==nil)
  1563. {
  1564. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1565. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db];
  1566. if(price==nil)
  1567. Price=@"No Price.";
  1568. else
  1569. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1570. }
  1571. else
  1572. {
  1573. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1574. }
  1575. double discount = sqlite3_column_double(statement, 2);
  1576. int item_count = sqlite3_column_int(statement, 3);
  1577. char *line_note = (char*)sqlite3_column_text(statement, 4);
  1578. NSString *nsline_note=nil;
  1579. if(line_note!=nil)
  1580. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1581. char *name = (char*)sqlite3_column_text(statement, 5);
  1582. NSString *nsname=nil;
  1583. if(name!=nil)
  1584. nsname= [[NSString alloc]initWithUTF8String:name];
  1585. char *description = (char*)sqlite3_column_text(statement, 6);
  1586. NSString *nsdescription=nil;
  1587. if(description!=nil)
  1588. nsdescription= [[NSString alloc]initWithUTF8String:description];
  1589. // int stockUom = sqlite3_column_int(statement, 8);
  1590. // int _id = sqlite3_column_int(statement, 9);
  1591. //
  1592. //
  1593. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  1594. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1595. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  1596. // int carton=[bsubtotaljson[@"carton"] intValue];
  1597. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1598. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  1599. itemjson[@"model"]=nsname;
  1600. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  1601. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  1602. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  1603. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  1604. // itemjson[@"check"]=@"true";
  1605. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1606. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  1607. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  1608. itemjson[@"unit_price"]=Price;
  1609. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  1610. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1611. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  1612. itemjson[@"note"]=nsline_note;
  1613. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:customerID];
  1614. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1615. count++;
  1616. }
  1617. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1618. sqlite3_finalize(statement);
  1619. }
  1620. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1621. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  1622. return ret;
  1623. }
  1624. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid
  1625. {
  1626. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1627. sqlite3 *db = [iSalesDB get_db];
  1628. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  1629. // for(int i=0;i<arr_soid.count;i++)
  1630. sqlite3_stmt * statement;
  1631. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1632. //int count=0;
  1633. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1634. if ( dbresult== SQLITE_OK)
  1635. {
  1636. int count=0;
  1637. while (sqlite3_step(statement) == SQLITE_ROW)
  1638. {
  1639. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1640. int _id = sqlite3_column_int(statement, 0);
  1641. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  1642. NSString* nssync_data=nil;
  1643. if(sync_data!=nil)
  1644. {
  1645. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1646. }
  1647. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1648. NSString* nsimg_1=nil;
  1649. if(img_1!=nil)
  1650. {
  1651. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1652. [self copy_upImg:serial file:nsimg_1];
  1653. }
  1654. char *so_no = (char*)sqlite3_column_text(statement, 3);
  1655. NSString* nsso_no=nil;
  1656. if(so_no!=nil)
  1657. {
  1658. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  1659. }
  1660. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  1661. itemjson[@"order_type"]=@"submit order";
  1662. else
  1663. itemjson[@"order_type"]=@"archive order";
  1664. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1665. itemjson[@"json_data"]= nssync_data;
  1666. itemjson[@"order_items"]=[self preparecart:nsso_no db:db];
  1667. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1668. count++;
  1669. }
  1670. ret[@"count"]=[NSNumber numberWithInt:count ];
  1671. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1672. }
  1673. sqlite3_finalize(statement);
  1674. [iSalesDB close_db:db];
  1675. return ret;
  1676. }
  1677. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  1678. {
  1679. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1680. sqlite3 *db = [iSalesDB get_db];
  1681. // UIApplication * app = [UIApplication sharedApplication];
  1682. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1683. NSString *sqlQuery =@"select _id,img_0,img_1,img_2,sync_data from offline_contact where is_active=1 and not sync_data is null";
  1684. sqlite3_stmt * statement;
  1685. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1686. int count=0;
  1687. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1688. if ( dbresult== SQLITE_OK)
  1689. {
  1690. while (sqlite3_step(statement) == SQLITE_ROW)
  1691. {
  1692. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1693. int _id = sqlite3_column_int(statement, 0);
  1694. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  1695. NSString* nsimg_0=nil;
  1696. if(img_0!=nil)
  1697. {
  1698. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  1699. [self copy_upImg:serial file:nsimg_0];
  1700. }
  1701. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1702. NSString* nsimg_1=nil;
  1703. if(img_1!=nil)
  1704. {
  1705. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1706. [self copy_upImg:serial file:nsimg_1];
  1707. }
  1708. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  1709. NSString* nsimg_2=nil;
  1710. if(img_2!=nil)
  1711. {
  1712. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  1713. [self copy_upImg:serial file:nsimg_2];
  1714. }
  1715. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  1716. NSString* nssync_data=nil;
  1717. if(sync_data!=nil)
  1718. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1719. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1720. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1721. itemjson[@"json_data"]= nssync_data;
  1722. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1723. count++;
  1724. }
  1725. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1726. sqlite3_finalize(statement);
  1727. }
  1728. ret[@"count"]=[NSNumber numberWithInt:count ];
  1729. [iSalesDB close_db:db];
  1730. return ret;
  1731. }
  1732. +(bool) check_offlinedata
  1733. {
  1734. UIApplication * app = [UIApplication sharedApplication];
  1735. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1736. sqlite3 *db = [iSalesDB get_db];
  1737. NSString * where=@"1=1";
  1738. // if(appDelegate.user!=nil)
  1739. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  1740. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  1741. [iSalesDB close_db:db];
  1742. if(count==0)
  1743. {
  1744. return false;
  1745. }
  1746. return true;
  1747. //
  1748. //[iSalesDB close_db:db];
  1749. }
  1750. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  1751. {
  1752. UIApplication * app = [UIApplication sharedApplication];
  1753. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1754. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1755. sqlite3 *db = [iSalesDB get_db];
  1756. NSString* collectId=params[@"collectId"];
  1757. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  1758. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  1759. [iSalesDB execSql:sqlQuery db:db];
  1760. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  1761. [iSalesDB close_db:db];
  1762. appDelegate.wish_count =count;
  1763. [appDelegate update_count_mark];
  1764. ret[@"result"]= [NSNumber numberWithInt:2];
  1765. return ret;
  1766. }
  1767. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  1768. //{
  1769. //
  1770. // UIApplication * app = [UIApplication sharedApplication];
  1771. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1772. //
  1773. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1774. // sqlite3 *db = [iSalesDB get_db];
  1775. // NSString* product_id=params[@"product_id"];
  1776. //
  1777. //
  1778. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  1779. //
  1780. //
  1781. // for(int i=0;i<arr.count;i++)
  1782. // {
  1783. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  1784. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  1785. // if(count==0)
  1786. // {
  1787. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  1788. // [iSalesDB execSql:sqlQuery db:db];
  1789. // }
  1790. // }
  1791. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  1792. // [iSalesDB close_db:db];
  1793. //
  1794. // appDelegate.wish_count =count;
  1795. //
  1796. // [appDelegate update_count_mark];
  1797. // ret[@"result"]= [NSNumber numberWithInt:2];
  1798. // return ret;
  1799. // //
  1800. // //return ret;
  1801. //}
  1802. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  1803. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  1804. DebugLog(@"time interval: %lf",interval);
  1805. }
  1806. +(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  1807. {
  1808. UIApplication * app = [UIApplication sharedApplication];
  1809. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1810. int sort = [[params objectForKey:@"sort"] intValue];
  1811. NSString *sort_str = @"";
  1812. switch (sort) {
  1813. case 0:{
  1814. sort_str = @"order by w.modify_time desc";
  1815. }
  1816. break;
  1817. case 1:{
  1818. sort_str = @"order by w.modify_time asc";
  1819. }
  1820. break;
  1821. case 2:{
  1822. sort_str = @"order by m.name asc";
  1823. }
  1824. break;
  1825. case 3:{
  1826. sort_str = @"order by m.name desc";
  1827. }
  1828. break;
  1829. case 4:{
  1830. sort_str = @"order by m.description asc";
  1831. }
  1832. break;
  1833. default:
  1834. break;
  1835. }
  1836. NSString* user = appDelegate.user;
  1837. sqlite3 *db = [iSalesDB get_db];
  1838. // order by w.create_time
  1839. NSString* sqlQuery = [NSString stringWithFormat:@"select w.product_id,m.name||'\n'||m.description,w._id,w.qty from wishlist as w left join product as m on w.product_id=m.product_id where w.is_delete is NULL or w.is_delete != 1 %@ ",sort_str];
  1840. // NSString* sqlQuery = [NSString stringWithFormat:@"select m.product_id,(m.name||'\n'||m.description) as des,w._id from product as m,wishlist as w where m.product_id = w.product_id %@",sort_str];
  1841. sqlite3_stmt * statement;
  1842. NSDate *date1 = [NSDate date];
  1843. NSDate *date2 = nil;
  1844. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1845. int count=0;
  1846. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1847. {
  1848. while (sqlite3_step(statement) == SQLITE_ROW)
  1849. {
  1850. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1851. int product_id = sqlite3_column_double(statement, 0);
  1852. char *description = (char*)sqlite3_column_text(statement, 1);
  1853. if(description==nil)
  1854. description= "";
  1855. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  1856. int item_id = sqlite3_column_double(statement, 2);
  1857. NSDate *date_image = [NSDate date];
  1858. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  1859. printf("image : ");
  1860. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  1861. // char *url = (char*)sqlite3_column_text(statement, 3);
  1862. // if(url==nil)
  1863. // url="";
  1864. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1865. int qty = sqlite3_column_int(statement, 3);
  1866. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  1867. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  1868. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  1869. item[@"description"]= nsdescription;
  1870. item[@"img"]= nsurl;
  1871. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  1872. count++;
  1873. }
  1874. printf("total time:");
  1875. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  1876. ret[@"count"]= [NSNumber numberWithInt:count];
  1877. ret[@"total_count"]= [NSNumber numberWithInt:count];
  1878. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  1879. ret[@"result"]= [NSNumber numberWithInt:2];
  1880. appDelegate.wish_count =count;
  1881. [appDelegate update_count_mark];
  1882. sqlite3_finalize(statement);
  1883. }
  1884. [iSalesDB close_db:db];
  1885. return ret;
  1886. }
  1887. +(NSDictionary*) offline_notimpl
  1888. {
  1889. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1890. ret[@"result"]=@"8";
  1891. ret[@"err_msg"]=@"offline mode does not support this function.";
  1892. return ret;
  1893. }
  1894. +(NSDictionary*) offline_home
  1895. {
  1896. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1897. NSString *cachefolder = [paths objectAtIndex:0];
  1898. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  1899. NSData* json =nil;
  1900. json=[NSData dataWithContentsOfFile:img_cache];
  1901. if(json==nil)
  1902. return nil;
  1903. NSError *error=nil;
  1904. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1905. return menu;
  1906. }
  1907. +(NSDictionary*) offline_category_menu
  1908. {
  1909. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1910. NSString *cachefolder = [paths objectAtIndex:0];
  1911. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  1912. NSData* json =nil;
  1913. json=[NSData dataWithContentsOfFile:img_cache];
  1914. if(json==nil)
  1915. return nil;
  1916. NSError *error=nil;
  1917. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1918. return menu;
  1919. }
  1920. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  1921. {
  1922. NSString* offline_command=params[@"offline_Command"];
  1923. NSDictionary* ret=nil;
  1924. if([offline_command isEqualToString:@"model_NIYMAL"])
  1925. {
  1926. NSString* category = params[@"category"];
  1927. ret = [self refresh_model_NIYMAL:category];
  1928. }
  1929. return ret;
  1930. }
  1931. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  1932. {
  1933. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1934. [ret setValue:@"2" forKey:@"result"];
  1935. [ret setValue:@"Regular Mode" forKey:@"mode"];
  1936. sqlite3* db= [iSalesDB get_db];
  1937. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  1938. [iSalesDB close_db:db];
  1939. [ret setObject:detail1_section forKey:@"detail_1"];
  1940. return ret;
  1941. }
  1942. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id db:(sqlite3*)db
  1943. {
  1944. UIApplication * app = [UIApplication sharedApplication];
  1945. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1946. // NSArray* arr1 = [self get_user_all_price_type];
  1947. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  1948. // NSSet *set1 = [NSSet setWithArray:arr1];
  1949. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  1950. // if(appDelegate.contact_id==nil)
  1951. // set2=[set1 mutableCopy];
  1952. // else
  1953. // [set2 intersectsSet:set1];
  1954. // NSArray *retarr = [set2 allObjects];
  1955. NSString* whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1956. // sqlite3 *db = [iSalesDB get_db];
  1957. NSString* sqlQuery = nil;
  1958. if(appDelegate.contact_id==nil)
  1959. {
  1960. if(!appDelegate.bLogin)
  1961. return nil;
  1962. sqlQuery=[NSString stringWithFormat:@"select a.name,b.price,b.type from price as a left join model_price as b on a.name=b.price_name and b.item_id=%d order by a.order_by;",item_id];
  1963. }
  1964. else
  1965. sqlQuery=[NSString stringWithFormat:@"select a.name,b.price,b.type from price as a left join model_price as b on a.name=b.price_name and b.item_id=%d and b.price_name in(%@) order by a.order_by;",item_id,whereprice];
  1966. sqlite3_stmt * statement;
  1967. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1968. int count=0;
  1969. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1970. {
  1971. while (sqlite3_step(statement) == SQLITE_ROW)
  1972. {
  1973. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1974. char *name = (char*)sqlite3_column_text(statement, 0);
  1975. if(name==nil)
  1976. name="";
  1977. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1978. // double price = sqlite3_column_double(statement, 1);
  1979. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  1980. // if(isnull==nil)
  1981. // item[nsname]= @"No Price";
  1982. // else
  1983. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  1984. char *price = (char*)sqlite3_column_text(statement, 1);
  1985. if(price!=nil)
  1986. {
  1987. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  1988. nsprice=[AESCrypt fastdecrypt:nsprice];
  1989. if(nsprice.length>0)
  1990. {
  1991. double dp= [nsprice doubleValue];
  1992. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  1993. }
  1994. }
  1995. else
  1996. {
  1997. item[nsname]= @"No Price";
  1998. }
  1999. // int type= sqlite3_column_int(statement, 2);
  2000. //item[@"type"]=@"price";
  2001. // item[nsname]= nsprice;
  2002. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2003. count++;
  2004. }
  2005. ret[@"count"]= [NSNumber numberWithInt:count];
  2006. sqlite3_finalize(statement);
  2007. }
  2008. // [iSalesDB close_db:db];
  2009. return ret;
  2010. }
  2011. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  2012. //{
  2013. // NSArray* arr1 = [self get_user_all_price_type:db];
  2014. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2015. //
  2016. // // NSSet *set1 = [NSSet setWithArray:arr1];
  2017. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2018. // // [set2 intersectsSet:set1];
  2019. // //
  2020. // //
  2021. // // NSArray *retarr = [set2 allObjects];
  2022. //
  2023. // NSString* whereprice=nil;
  2024. // if(contact_id==nil)
  2025. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2026. // else
  2027. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2028. //
  2029. //
  2030. // // sqlite3 *db = [iSalesDB get_db];
  2031. //
  2032. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  2033. // sqlite3_stmt * statement;
  2034. //
  2035. //
  2036. // NSNumber* ret = nil;
  2037. // double dprice=DBL_MAX;
  2038. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2039. // {
  2040. //
  2041. //
  2042. // while (sqlite3_step(statement) == SQLITE_ROW)
  2043. // {
  2044. //
  2045. // // double val = sqlite3_column_double(statement, 0);
  2046. // char *price = (char*)sqlite3_column_text(statement, 0);
  2047. // if(price!=nil)
  2048. // {
  2049. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2050. // nsprice=[AESCrypt fastdecrypt:nsprice];
  2051. // if(nsprice.length>0)
  2052. // {
  2053. // double dp= [nsprice doubleValue];
  2054. // if(dp<dprice)
  2055. // dprice=dp;
  2056. // }
  2057. // }
  2058. // }
  2059. //
  2060. //
  2061. //
  2062. //
  2063. // sqlite3_finalize(statement);
  2064. //
  2065. //
  2066. //
  2067. //
  2068. // }
  2069. //
  2070. // // [iSalesDB close_db:db];
  2071. //
  2072. // if(dprice==DBL_MAX)
  2073. // ret= nil;
  2074. // else
  2075. // ret= [NSNumber numberWithDouble:dprice];
  2076. // return ret;
  2077. //}
  2078. +(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  2079. {
  2080. NSArray* arr1 = [self get_user_all_price_type:db];
  2081. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2082. NSString* whereprice=nil;
  2083. if(contact_id==nil)
  2084. whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2085. else
  2086. whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2087. // sqlite3 *db = [iSalesDB get_db];
  2088. NSString *productIdCondition = @"1 = 1";
  2089. if (product_id) {
  2090. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  2091. }
  2092. NSString *itemIdCondition = @"";
  2093. if (item_id) {
  2094. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  2095. }
  2096. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  2097. sqlite3_stmt * statement;
  2098. NSNumber* ret = nil;
  2099. double dprice=DBL_MAX;
  2100. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2101. {
  2102. while (sqlite3_step(statement) == SQLITE_ROW)
  2103. {
  2104. // double val = sqlite3_column_double(statement, 0);
  2105. char *price = (char*)sqlite3_column_text(statement, 0);
  2106. if(price!=nil)
  2107. {
  2108. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2109. nsprice=[AESCrypt fastdecrypt:nsprice];
  2110. if(nsprice.length>0)
  2111. {
  2112. double dp= [nsprice doubleValue];
  2113. if(dp<dprice)
  2114. dprice=dp;
  2115. }
  2116. }
  2117. }
  2118. sqlite3_finalize(statement);
  2119. }
  2120. // [iSalesDB close_db:db];
  2121. if(dprice==DBL_MAX)
  2122. ret= nil;
  2123. else
  2124. ret= [NSNumber numberWithDouble:dprice];
  2125. return ret;
  2126. }
  2127. +(NSArray*) get_user_all_price_type:(sqlite3*)db
  2128. {
  2129. NSArray* ret=nil;
  2130. // sqlite3 *db = [iSalesDB get_db];
  2131. // no customer assigned , use login user contact_id
  2132. UIApplication * app = [UIApplication sharedApplication];
  2133. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2134. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",appDelegate.user];
  2135. sqlite3_stmt * statement;
  2136. // int count=0;
  2137. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2138. {
  2139. if (sqlite3_step(statement) == SQLITE_ROW)
  2140. {
  2141. char *val = (char*)sqlite3_column_text(statement, 0);
  2142. if(val==nil)
  2143. val="";
  2144. NSString* price = [[NSString alloc]initWithUTF8String:val];
  2145. ret=[RAUtils string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  2146. }
  2147. sqlite3_finalize(statement);
  2148. }
  2149. // [iSalesDB close_db:db];
  2150. return ret;
  2151. }
  2152. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id db:(sqlite3*)db
  2153. {
  2154. // sqlite3 *db = [iSalesDB get_db];
  2155. if(contact_id==nil)
  2156. {
  2157. // no customer assigned , use login user contact_id
  2158. UIApplication * app = [UIApplication sharedApplication];
  2159. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2160. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",appDelegate.user];
  2161. sqlite3_stmt * statement;
  2162. // int count=0;
  2163. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2164. {
  2165. if (sqlite3_step(statement) == SQLITE_ROW)
  2166. {
  2167. char *val = (char*)sqlite3_column_text(statement, 0);
  2168. if(val==nil)
  2169. val="";
  2170. contact_id = [[NSString alloc]initWithUTF8String:val];
  2171. }
  2172. sqlite3_finalize(statement);
  2173. }
  2174. if(contact_id.length<=0)
  2175. {
  2176. // [iSalesDB close_db:db];
  2177. return nil;
  2178. }
  2179. }
  2180. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  2181. sqlite3_stmt * statement;
  2182. NSArray* ret=nil;
  2183. // int count=0;
  2184. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2185. {
  2186. if (sqlite3_step(statement) == SQLITE_ROW)
  2187. {
  2188. char *price_type = (char*)sqlite3_column_text(statement, 0);
  2189. if(price_type==nil)
  2190. price_type="";
  2191. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  2192. if(nsprice_type.length>0)
  2193. ret=[RAUtils string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  2194. else
  2195. ret=nil;
  2196. }
  2197. sqlite3_finalize(statement);
  2198. }
  2199. // [iSalesDB close_db:db];
  2200. return ret;
  2201. }
  2202. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  2203. {
  2204. NSString* ret= nil;
  2205. // sqlite3 *db = [iSalesDB get_db];
  2206. NSString *sqlQuery = nil;
  2207. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  2208. // sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.name=%@ order by i.default_img desc, i._id asc limit 1;",model_name];// select i.url from model m LEFT join model_image i on m.product_id = i.product_id where m.name=%@ order by i.default_img desc, i._id asc limit 1;
  2209. // sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.product_id=%@ order by i.default_img desc, i._id asc limit 1;",product_id];// select i.url from model m
  2210. if(product_id==nil && model_name)
  2211. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image i where product_id in (select product_id from product where name='%@') order by i.default_img desc, i._id asc limit 1;",model_name];// select i.url from model m LEFT join model_image i on m.product_id = i.product_id where m.name=%@ order by i.default_img desc, i._id asc limit 1;
  2212. else if (product_id)
  2213. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image i where i.product_id=%@ order by i.default_img desc, i._id asc limit 1;",product_id];// select i.url from model m
  2214. sqlite3_stmt * statement;
  2215. // int count=0;
  2216. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2217. {
  2218. if (sqlite3_step(statement) == SQLITE_ROW)
  2219. {
  2220. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  2221. if(imgurl==nil)
  2222. imgurl="";
  2223. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  2224. ret=nsimgurl;
  2225. }
  2226. sqlite3_finalize(statement);
  2227. }
  2228. else
  2229. {
  2230. [ret setValue:@"8" forKey:@"result"];
  2231. }
  2232. // [iSalesDB close_db:db];
  2233. DebugLog(@"data string: %@",ret );
  2234. return ret;
  2235. }
  2236. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name
  2237. {
  2238. NSString* ret= nil;
  2239. sqlite3 *db = [iSalesDB get_db];
  2240. NSString *sqlQuery = nil;
  2241. if(product_id==nil)
  2242. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  2243. else
  2244. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  2245. sqlite3_stmt * statement;
  2246. // int count=0;
  2247. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2248. {
  2249. if (sqlite3_step(statement) == SQLITE_ROW)
  2250. {
  2251. char *default_category = (char*)sqlite3_column_text(statement, 0);
  2252. if(default_category==nil)
  2253. default_category="";
  2254. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2255. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  2256. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  2257. ret=nsdefault_category;
  2258. }
  2259. sqlite3_finalize(statement);
  2260. }
  2261. else
  2262. {
  2263. [ret setValue:@"8" forKey:@"result"];
  2264. }
  2265. [iSalesDB close_db:db];
  2266. DebugLog(@"data string: %@",ret );
  2267. return ret;
  2268. }
  2269. +(NSDictionary*) offline_model :(NSMutableDictionary *) params
  2270. {
  2271. NSString* model_name = [params valueForKey:@"product_name"];
  2272. NSString* product_id = [params valueForKey:@"product_id"];
  2273. NSString* category = [params valueForKey:@"category"];
  2274. if(category==nil)
  2275. category = [self model_default_category:product_id model_name:model_name];
  2276. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2277. sqlite3 *db = [iSalesDB get_db];
  2278. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  2279. NSString *sqlQuery = nil;
  2280. if(product_id==nil)
  2281. sqlQuery = [NSString stringWithFormat:@"select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging,ETA,item_id from product where name='%@';",model_name]; // select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging from model where name='%@';
  2282. else
  2283. sqlQuery = [NSString stringWithFormat:@"select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging,ETA,item_id from product where product_id=%@;",product_id]; // select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging from model where product_id=%@;
  2284. sqlite3_stmt * statement;
  2285. [ret setValue:@"2" forKey:@"result"];
  2286. [ret setValue:@"3" forKey:@"detail_section_count"];
  2287. // int count=0;
  2288. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2289. {
  2290. if (sqlite3_step(statement) == SQLITE_ROW)
  2291. {
  2292. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2293. char *name = (char*)sqlite3_column_text(statement, 0);
  2294. if(name==nil)
  2295. name="";
  2296. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2297. char *description = (char*)sqlite3_column_text(statement, 1);
  2298. if(description==nil)
  2299. description="";
  2300. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2301. int product_id = sqlite3_column_int(statement, 2);
  2302. char *color = (char*)sqlite3_column_text(statement, 3);
  2303. if(color==nil)
  2304. color="";
  2305. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  2306. //
  2307. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  2308. // if(legcolor==nil)
  2309. // legcolor="";
  2310. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  2311. //
  2312. //
  2313. int availability = sqlite3_column_int(statement, 5);
  2314. //
  2315. int incoming_stock = sqlite3_column_int(statement, 6);
  2316. char *demension = (char*)sqlite3_column_text(statement, 7);
  2317. if(demension==nil)
  2318. demension="";
  2319. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  2320. // ,,,,,,,,,
  2321. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  2322. if(seat_height==nil)
  2323. seat_height="";
  2324. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  2325. char *material = (char*)sqlite3_column_text(statement, 9);
  2326. if(material==nil)
  2327. material="";
  2328. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  2329. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  2330. if(box_dim==nil)
  2331. box_dim="";
  2332. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  2333. char *volume = (char*)sqlite3_column_text(statement, 11);
  2334. if(volume==nil)
  2335. volume="";
  2336. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  2337. double weight = sqlite3_column_double(statement, 12);
  2338. char *model_set = (char*)sqlite3_column_text(statement, 13);
  2339. if(model_set==nil)
  2340. model_set="";
  2341. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  2342. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  2343. if(load_ability==nil)
  2344. load_ability="";
  2345. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  2346. char *default_category = (char*)sqlite3_column_text(statement, 15);
  2347. if(default_category==nil)
  2348. default_category="";
  2349. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2350. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  2351. if(fabric_content==nil)
  2352. fabric_content="";
  2353. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  2354. char *assembling = (char*)sqlite3_column_text(statement, 17);
  2355. if(assembling==nil)
  2356. assembling="";
  2357. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  2358. char *made_in = (char*)sqlite3_column_text(statement, 18);
  2359. if(made_in==nil)
  2360. made_in="";
  2361. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  2362. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  2363. if(special_remarks==nil)
  2364. special_remarks="";
  2365. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  2366. int stockUcom = sqlite3_column_double(statement, 20);
  2367. char *product_group = (char*)sqlite3_column_text(statement, 21);
  2368. if(product_group==nil)
  2369. product_group="";
  2370. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  2371. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  2372. // if(fashion_selector==nil)
  2373. // fashion_selector="";
  2374. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  2375. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  2376. if(selector_field==nil)
  2377. selector_field="";
  2378. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  2379. char *property_field = (char*)sqlite3_column_text(statement, 23);
  2380. if(property_field==nil)
  2381. property_field="";
  2382. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  2383. char *packaging = (char*)sqlite3_column_text(statement, 24);
  2384. if(packaging==nil)
  2385. packaging="";
  2386. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  2387. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2388. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  2389. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  2390. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  2391. [img_section setValue:model_s_img forKey:@"model_s_img"];
  2392. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  2393. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  2394. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  2395. NSString* Availability=nil;
  2396. if(availability>0)
  2397. Availability=[NSString stringWithFormat:@"%d",availability];
  2398. else
  2399. Availability = @"In Production";
  2400. [img_section setValue:Availability forKey:@"Availability"];
  2401. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  2402. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  2403. char *eta = (char*)sqlite3_column_text(statement, 25);
  2404. if(eta==nil)
  2405. eta="";
  2406. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  2407. [img_section setValue:nseta forKey:@"ETA"];
  2408. int item_id = sqlite3_column_int(statement, 26);
  2409. NSString* Price=nil;
  2410. if(appDelegate.bLogin==false)
  2411. Price=@"Must Sign in.";
  2412. else
  2413. {
  2414. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2415. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  2416. if(price==nil)
  2417. Price=@"No Price.";
  2418. else
  2419. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2420. }
  2421. [img_section setValue:Price forKey:@"price"];
  2422. [img_section setValue:nsname forKey:@"model_name"];
  2423. [img_section setValue:nsdescription forKey:@"model_descrition"];
  2424. [ret setObject:img_section forKey:@"img_section"];
  2425. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  2426. int detail0_item_count=0;
  2427. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2428. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2429. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2430. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2431. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2432. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2433. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2434. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2435. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2436. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2437. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2438. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2439. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2440. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  2441. NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  2442. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  2443. {
  2444. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  2445. [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2446. }
  2447. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  2448. [detail0_section setValue:@"kv" forKey:@"type"];
  2449. [detail0_section setValue:@"Product Information" forKey:@"title"];
  2450. [ret setObject:detail0_section forKey:@"detail_0"];
  2451. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  2452. // [detail1_section setValue:@"detail" forKey:@"target"];
  2453. // [detail1_section setValue:@"popup" forKey:@"action"];
  2454. // [detail1_section setValue:@"content" forKey:@"type"];
  2455. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  2456. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  2457. // [detail1_section setValue:@"true" forKey:@"single_row"];
  2458. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  2459. [ret setObject:detail1_section forKey:@"detail_1"];
  2460. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  2461. [detail2_section setValue:@"detail" forKey:@"target"];
  2462. [detail2_section setValue:@"popup" forKey:@"action"];
  2463. [detail2_section setValue:@"content" forKey:@"type"];
  2464. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  2465. [detail2_section setValue:@"true" forKey:@"single_row"];
  2466. [detail2_section setValue:@"local" forKey:@"data"];
  2467. [ret setObject:detail2_section forKey:@"detail_2"];
  2468. }
  2469. sqlite3_finalize(statement);
  2470. }
  2471. else
  2472. {
  2473. [ret setValue:@"8" forKey:@"result"];
  2474. }
  2475. DebugLog(@"count:%d",count);
  2476. [iSalesDB close_db:db];
  2477. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2478. return ret;
  2479. }
  2480. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  2481. NSString* orderCode = [params valueForKey:@"orderCode"];
  2482. NSString* keyword = [params valueForKey:@"keyword"];
  2483. keyword=keyword.lowercaseString;
  2484. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue];
  2485. int limit = [[params valueForKey:@"limit"] intValue];
  2486. int offset = [[params valueForKey:@"offset"] intValue];
  2487. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2488. NSString *limit_str = @"";
  2489. if (limited) {
  2490. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  2491. }
  2492. sqlite3 *db = [iSalesDB get_db];
  2493. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  2494. UIApplication * app = [UIApplication sharedApplication];
  2495. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2496. NSString *sqlQuery = nil;
  2497. if(exactMatch )
  2498. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where lower(name) like'%@%%' and m.is_active = 1 order by m.name %@;",appDelegate.order_code, keyword,limit_str]; // select m.name,m.description,m.product_id,w._id,m.closeout from model m left join wishlist w on m.product_id=w.product_id where lower(name) like'%@%%' order by m.name limit %d offset %d ;
  2499. else
  2500. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where (lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and m.is_active = 1 order by m.name %@ ;",appDelegate.order_code, keyword,keyword,limit_str];// select m.name,m.description,m.product_id,w._id,m.closeout from model m left join wishlist w on m.product_id=w.product_id where lower(name) like'%%%@%%' or
  2501. DebugLog(@"offline_search sql:%@",sqlQuery);
  2502. sqlite3_stmt * statement;
  2503. [ret setValue:@"2" forKey:@"result"];
  2504. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  2505. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  2506. // int count=0;
  2507. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2508. {
  2509. int i=0;
  2510. while (sqlite3_step(statement) == SQLITE_ROW)
  2511. {
  2512. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2513. // char *name = (char*)sqlite3_column_text(statement, 1);
  2514. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2515. char *name = (char*)sqlite3_column_text(statement, 0);
  2516. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2517. char *description = (char*)sqlite3_column_text(statement, 1);
  2518. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2519. int product_id = sqlite3_column_int(statement, 2);
  2520. // char *url = (char*)sqlite3_column_text(statement, 3);
  2521. // if(url==nil)
  2522. // url="";
  2523. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2524. int wid = sqlite3_column_int(statement, 3);
  2525. int closeout = sqlite3_column_int(statement, 4);
  2526. int cid = sqlite3_column_int(statement, 5);
  2527. int wisdelete = sqlite3_column_int(statement, 6);
  2528. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  2529. if(wid !=0 && wisdelete != 1)
  2530. [item setValue:@"true" forKey:@"wish_exists"];
  2531. else
  2532. [item setValue:@"false" forKey:@"wish_exists"];
  2533. if(closeout==0)
  2534. [item setValue:@"false" forKey:@"is_closeout"];
  2535. else
  2536. [item setValue:@"true" forKey:@"is_closeout"];
  2537. if(cid==0)
  2538. [item setValue:@"false" forKey:@"cart_exists"];
  2539. else
  2540. [item setValue:@"true" forKey:@"cart_exists"];
  2541. [item addEntriesFromDictionary:imgjson];
  2542. // [item setValue:nsurl forKey:@"img"];
  2543. [item setValue:nsname forKey:@"fash_name"];
  2544. [item setValue:nsdescription forKey:@"description"];
  2545. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2546. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2547. i++;
  2548. }
  2549. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2550. [ret setObject:items forKey:@"items"];
  2551. sqlite3_finalize(statement);
  2552. }
  2553. DebugLog(@"count:%d",count);
  2554. [iSalesDB close_db:db];
  2555. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2556. return ret;
  2557. }
  2558. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  2559. {
  2560. return [self search:params limited:YES];
  2561. }
  2562. +(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  2563. {
  2564. UIApplication * app = [UIApplication sharedApplication];
  2565. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2566. appDelegate.disable_trigger=true;
  2567. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2568. appDelegate.disable_trigger=false;
  2569. //
  2570. // NSString* user = [params valueForKey:@"user"];
  2571. //
  2572. // NSString* password = [params valueForKey:@"password"];
  2573. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2574. ret[@"result"]=[NSNumber numberWithInt:2 ];
  2575. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  2576. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  2577. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  2578. [appDelegate update_count_mark];
  2579. appDelegate.can_show_price =false;
  2580. appDelegate.can_see_price =false;
  2581. appDelegate.can_create_portfolio =false;
  2582. appDelegate.can_create_order =false;
  2583. appDelegate.can_cancel_order =false;
  2584. appDelegate.can_set_cart_price =false;
  2585. appDelegate.can_delete_order =false;
  2586. appDelegate.can_submit_order =false;
  2587. appDelegate.can_set_tearsheet_price =false;
  2588. appDelegate.can_update_contact_info = false;
  2589. appDelegate.save_order_logout = false;
  2590. appDelegate.submit_order_logout = false;
  2591. appDelegate.alert_sold_in_quantities = false;
  2592. appDelegate.ipad_perm =nil ;
  2593. appDelegate.user_type = USER_ROLE_UNKNOWN;
  2594. appDelegate.OrderFilter= nil;
  2595. [appDelegate SetSo:nil];
  2596. [appDelegate set_main_button_panel];
  2597. // sqlite3 *db = [iSalesDB get_db];
  2598. //
  2599. //
  2600. //
  2601. //
  2602. //
  2603. // NSString *sqlQuery = [NSString stringWithFormat:@"select can_show_price,can_see_price,contact_id,user_type,can_cancel_order,can_set_cart_price,can_create_portfolio,can_delete_order,can_submit_order,can_set_tearsheet_price,can_create_order,mode,username from offline_login where lower(username)='%@' and password='%@'",user.lowercaseString ,[AESCrypt encrypt:password password:@"usai"]];
  2604. //
  2605. //
  2606. //
  2607. //
  2608. // DebugLog(@"offline_login sql:%@",sqlQuery);
  2609. // sqlite3_stmt * statement;
  2610. //
  2611. //
  2612. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2613. //
  2614. //
  2615. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2616. // {
  2617. //
  2618. //
  2619. // if (sqlite3_step(statement) == SQLITE_ROW)
  2620. // {
  2621. //
  2622. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2623. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  2624. //
  2625. //
  2626. //
  2627. // int can_show_price = sqlite3_column_int(statement, 0);
  2628. // int can_see_price = sqlite3_column_int(statement, 1);
  2629. //
  2630. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  2631. // if(contact_id==nil)
  2632. // contact_id="";
  2633. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2634. //
  2635. // int user_type = sqlite3_column_int(statement, 3);
  2636. //
  2637. // int can_cancel_order = sqlite3_column_int(statement, 4);
  2638. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  2639. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  2640. // int can_delete_order = sqlite3_column_int(statement, 7);
  2641. // int can_submit_order = sqlite3_column_int(statement, 8);
  2642. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  2643. // int can_create_order = sqlite3_column_int(statement, 10);
  2644. //
  2645. //
  2646. // char *mode = (char*)sqlite3_column_text(statement, 11);
  2647. // if(mode==nil)
  2648. // mode="";
  2649. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  2650. //
  2651. //
  2652. // char *username = (char*)sqlite3_column_text(statement, 12);
  2653. // if(username==nil)
  2654. // username="";
  2655. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  2656. //
  2657. //
  2658. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  2659. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  2660. // [header setValue:nscontact_id forKey:@"contact_id"];
  2661. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  2662. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  2663. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  2664. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  2665. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  2666. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  2667. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  2668. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  2669. //
  2670. // [header setValue:nsusername forKey:@"username"];
  2671. //
  2672. //
  2673. // [ret setObject:header forKey:@"header"];
  2674. // [ret setValue:nsmode forKey:@"mode"];
  2675. //
  2676. //
  2677. // }
  2678. //
  2679. //
  2680. //
  2681. // sqlite3_finalize(statement);
  2682. // }
  2683. //
  2684. //
  2685. //
  2686. // [iSalesDB close_db:db];
  2687. //
  2688. //
  2689. //
  2690. //
  2691. // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2692. return ret;
  2693. }
  2694. +(NSData*) offline_createorder :(NSMutableDictionary *) params
  2695. {
  2696. UIApplication * app = [UIApplication sharedApplication];
  2697. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2698. appDelegate.disable_trigger=true;
  2699. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2700. appDelegate.disable_trigger=false;
  2701. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2702. NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  2703. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  2704. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  2705. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  2706. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  2707. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  2708. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  2709. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  2710. NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  2711. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  2712. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  2713. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  2714. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  2715. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  2716. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  2717. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  2718. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  2719. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  2720. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  2721. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  2722. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  2723. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  2724. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  2725. [arr_name addObject:customer_first_name];
  2726. [arr_name addObject:customer_last_name];
  2727. NSString *contact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  2728. // default ship from
  2729. NSString *ship_from_default_sql = @"select contact_id,decrypt(company_name),decrypt(addr_1),first_name || ' ' || last_name,email,fax,decrypt(phone) from offline_contact where contact_id = 'NPD';";
  2730. __block NSString *cid = @"";
  2731. __block NSString *name = @"";
  2732. __block NSString *ext = @"";
  2733. __block NSString *contact = @"";
  2734. __block NSString *email = @"";
  2735. __block NSString *fax = @"";
  2736. __block NSString *phone = @"";
  2737. sqlite3 *db = [iSalesDB get_db];
  2738. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  2739. cid = [self textAtColumn:0 statement:statment];
  2740. name = [self textAtColumn:1 statement:statment];
  2741. ext = [self textAtColumn:2 statement:statment];
  2742. contact = [self textAtColumn:3 statement:statment];
  2743. email = [self textAtColumn:4 statement:statment];
  2744. fax = [self textAtColumn:5 statement:statment];
  2745. phone = [self textAtColumn:6 statement:statment];
  2746. }];
  2747. NSString* so_id = [self get_offline_soid:db];
  2748. if(so_id==nil)
  2749. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  2750. NSString* sql_neworder=[NSString stringWithFormat:@"insert into offline_order(so_id,status,customer_cid,customer_contact,customer_email,customer_fax,customer_phone,sales_rep,create_by,customer_contact,must_call,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,sales_terms) values('%@',0,'%@','%@','%@','%@','%@','%@','%@','%@',1,'%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','FOB')",so_id,customer_cid,customer_contact,customer_email,customer_fax,customer_phone,customer_sales_rep,create_by ,contact_name,cid,name,ext,contact,phone,fax,email,customer_cid,customer_name,customer_contact_ext,customer_contact,customer_phone,customer_fax,customer_email];
  2751. int result =[iSalesDB execSql:sql_neworder db:db];
  2752. [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  2753. //insert into offline_order(so_id,status,contact_id,sales_rep,create_by,customer_contact) select 'OFFLINE'||hex(randomblob(16)),-1,contact_id,sales_rep,company_name from offline_contact where contact_id='99FURO0002'
  2754. //soId
  2755. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  2756. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  2757. sqlite3_stmt * statement;
  2758. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2759. {
  2760. if (sqlite3_step(statement) == SQLITE_ROW)
  2761. {
  2762. // char *name = (char*)sqlite3_column_text(statement, 1);
  2763. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2764. //ret = sqlite3_column_int(statement, 0);
  2765. char *soId = (char*)sqlite3_column_text(statement, 0);
  2766. if(soId==nil)
  2767. soId="";
  2768. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  2769. [ret setValue:nssoId forKey:@"soId"];
  2770. [ret setValue:nssoId forKey:@"orderCode"];
  2771. }
  2772. sqlite3_finalize(statement);
  2773. }
  2774. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  2775. [ret setValue:@"Regular Mode" forKey:@"mode"];
  2776. [iSalesDB close_db:db];
  2777. return [RAUtils dict2data:ret];
  2778. }
  2779. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  2780. {
  2781. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2782. NSString* orderCode = [params valueForKey:@"orderCode"];
  2783. UIApplication * app = [UIApplication sharedApplication];
  2784. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2785. if(![appDelegate.order_code isEqualToString:orderCode]&& appDelegate.order_code.length!=0)
  2786. {
  2787. appDelegate.disable_trigger=true;
  2788. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2789. appDelegate.disable_trigger=false;
  2790. }
  2791. sqlite3 *db = [iSalesDB get_db];
  2792. int cart_count=[self query_ordercartcount:orderCode db:db];
  2793. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2794. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2795. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2796. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2797. [iSalesDB close_db:db];
  2798. return [RAUtils dict2data:ret];
  2799. }
  2800. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  2801. {
  2802. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2803. sqlite3 *db = [iSalesDB get_db];
  2804. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  2805. int count =0;
  2806. if(params[@"count"]!=nil)
  2807. {
  2808. count = [params[@"count"] intValue];
  2809. }
  2810. NSString* product_id=params[@"product_id"];
  2811. NSString* orderCode=params[@"orderCode"];
  2812. NSString *qty = params[@"qty"];
  2813. NSArray* arr_id=[RAUtils string2arr:product_id separator:@","];
  2814. NSArray *qty_arr = [RAUtils string2arr:qty separator:@","];
  2815. for(int i=0;i<arr_id.count;i++)
  2816. {
  2817. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  2818. int item_qty= count;
  2819. if (qty) {
  2820. item_qty = [qty_arr[i] integerValue];
  2821. }
  2822. if(item_qty==0)
  2823. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  2824. NSString* sql=nil;
  2825. sqlite3_stmt *stmt;
  2826. if(_id<0)
  2827. {
  2828. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  2829. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  2830. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2831. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],[arr_id[i] length],NULL);
  2832. sqlite3_bind_text(stmt,2,[orderCode UTF8String],[orderCode length],NULL);
  2833. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%d",item_qty] UTF8String],[[NSString stringWithFormat:@"%d",item_qty] length],NULL);
  2834. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  2835. }
  2836. else
  2837. {
  2838. if (qty) {
  2839. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  2840. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2841. sqlite3_bind_int(stmt, 1, _id);
  2842. } else {
  2843. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  2844. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2845. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%d",item_qty] UTF8String],[[NSString stringWithFormat:@"%d",item_qty] length],NULL);
  2846. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],[[NSString stringWithFormat:@"%d",_id] length],NULL);
  2847. }
  2848. }
  2849. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  2850. [iSalesDB execSql:@"ROLLBACK" db:db];
  2851. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  2852. [iSalesDB close_db:db];
  2853. DebugLog(@"add to cart error");
  2854. return [RAUtils dict2data:ret];
  2855. }
  2856. }
  2857. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2858. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2859. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2860. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2861. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2862. [iSalesDB execSql:@"END TRANSACTION" db:db];
  2863. [iSalesDB close_db:db];
  2864. return [RAUtils dict2data:ret];
  2865. }
  2866. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute contactID:(NSString *)contactID
  2867. {
  2868. UIApplication * app = [UIApplication sharedApplication];
  2869. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2870. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  2871. NSString *sqlQuery = [NSString stringWithFormat:@"select b.item_id,b.qty,c.name,c.description,b.unit_price,b.use_unitprice from (select item_id,qty,decrypt(unit_price) as unit_price,use_unitprice from bundle where product_item_id=%d) b left join catalog c on b.item_id=c.item_id;",item_id];
  2872. sqlite3_stmt * statement;
  2873. int count=0;
  2874. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2875. {
  2876. while (sqlite3_step(statement) == SQLITE_ROW)
  2877. {
  2878. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  2879. int bitem_id = sqlite3_column_int(statement, 0);
  2880. int bitem_qty = sqlite3_column_int(statement, 1);
  2881. char *name = (char*)sqlite3_column_text(statement, 2);
  2882. if(name==nil)
  2883. name="";
  2884. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  2885. char *description = (char*)sqlite3_column_text(statement, 3);
  2886. if(description==nil)
  2887. description="";
  2888. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  2889. double unit_price = sqlite3_column_double(statement, 4);
  2890. int use_unitprice = sqlite3_column_int(statement, 5);
  2891. if(use_unitprice!=1)
  2892. {
  2893. unit_price=[self get_model_default_price:contactID product_id:nil item_id:@(bitem_id) db:db].doubleValue;
  2894. }
  2895. itemjson[@"model"]=nsname;
  2896. itemjson[@"description"]=nsdescription;
  2897. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  2898. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  2899. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  2900. if(compute)
  2901. {
  2902. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute contactID:contactID];
  2903. }
  2904. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2905. count++;
  2906. }
  2907. sqlite3_finalize(statement);
  2908. }
  2909. ret[@"count"]=@(count);
  2910. if(count==0)
  2911. return nil;
  2912. else
  2913. return ret;
  2914. }
  2915. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  2916. {
  2917. //compute: add part to subtotal;
  2918. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  2919. dict_item[@(item_id)]=@"1";
  2920. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  2921. double cuft=0;
  2922. double weight=0;
  2923. int carton=0;
  2924. NSString *sqlQuery = [NSString stringWithFormat:@"select ulength ,uwidth ,uheight ,uweight ,mlength ,mwidth ,mheight ,mweigth ,ilength ,iwidth ,iheight , iweight ,pcs ,mpack ,ipack,ucbf,icbf,mcbf from catalog where item_id=%d;",item_id];
  2925. sqlite3_stmt * statement;
  2926. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2927. {
  2928. if (sqlite3_step(statement) == SQLITE_ROW)
  2929. {
  2930. double ulength = sqlite3_column_double(statement, 0);
  2931. double uwidth = sqlite3_column_double(statement, 1);
  2932. double uheight = sqlite3_column_double(statement, 2);
  2933. double uweight = sqlite3_column_double(statement, 3);
  2934. double mlength = sqlite3_column_double(statement, 4);
  2935. double mwidth = sqlite3_column_double(statement, 5);
  2936. double mheight = sqlite3_column_double(statement, 6);
  2937. double mweight = sqlite3_column_double(statement, 7);
  2938. double ilength = sqlite3_column_double(statement, 8);
  2939. double iwidth = sqlite3_column_double(statement, 9);
  2940. double iheight = sqlite3_column_double(statement, 10);
  2941. double iweight = sqlite3_column_double(statement, 11);
  2942. int pcs = sqlite3_column_int(statement,12);
  2943. int mpack = sqlite3_column_int(statement, 13);
  2944. int ipack = sqlite3_column_int(statement, 14);
  2945. double ucbf = sqlite3_column_double(statement, 15);
  2946. double icbf = sqlite3_column_double(statement, 16);
  2947. double mcbf = sqlite3_column_double(statement, 17);
  2948. if(ipack==0)
  2949. {
  2950. carton= count/mpack ;
  2951. weight = mweight*carton;
  2952. cuft= carton*(mlength*mwidth*mheight);
  2953. int remain=count%mpack;
  2954. if(remain==0)
  2955. {
  2956. //do nothing;
  2957. }
  2958. else
  2959. {
  2960. carton++;
  2961. weight += uweight*remain;
  2962. cuft += (ulength*uwidth*uheight)*remain;
  2963. }
  2964. }
  2965. else
  2966. {
  2967. carton = count/(mpack*ipack);
  2968. weight = mweight*carton;
  2969. cuft= carton*(mlength*mwidth*mheight);
  2970. int remain=count%(mpack*ipack);
  2971. if(remain==0)
  2972. {
  2973. // do nothing;
  2974. }
  2975. else
  2976. {
  2977. carton++;
  2978. int icarton =remain/ipack;
  2979. int iremain=remain%ipack;
  2980. weight += iweight*icarton;
  2981. cuft += (ilength*iwidth*iheight)*icarton;
  2982. if(iremain==0)
  2983. {
  2984. //do nothing;
  2985. }
  2986. else
  2987. {
  2988. weight += uweight*iremain;
  2989. cuft += (ulength*uwidth*uheight)*iremain;
  2990. }
  2991. }
  2992. }
  2993. #ifdef BUILD_NPD
  2994. cuft=ucbf*count;
  2995. weight= uweight*count;
  2996. #endif
  2997. }
  2998. sqlite3_finalize(statement);
  2999. }
  3000. if(compute)
  3001. {
  3002. NSArray * arr_count=nil;
  3003. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  3004. for(int i=0;i<arr_bundle.count;i++)
  3005. {
  3006. dict_item[arr_bundle[i]]=@"1";
  3007. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  3008. cuft+=[bundlejson[@"cuft"] doubleValue];
  3009. weight+=[bundlejson[@"weight"] doubleValue];
  3010. carton+=[bundlejson[@"carton"] intValue];
  3011. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  3012. }
  3013. }
  3014. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  3015. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  3016. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  3017. ret[@"items"]=dict_item;
  3018. return ret;
  3019. }
  3020. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  3021. {
  3022. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  3023. // NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,decrypt(c.str_price),c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from offline_cart c left join product m on c.product_id=m.product_id where c.so_no='%@'",orderCode ];
  3024. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3025. DebugLog(@"offline_login sql:%@",sqlQuery);
  3026. sqlite3_stmt * statement;
  3027. int cart_count=0;
  3028. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3029. if ( dbresult== SQLITE_OK)
  3030. {
  3031. while (sqlite3_step(statement) == SQLITE_ROW)
  3032. {
  3033. int item_id = sqlite3_column_int(statement, 0);
  3034. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  3035. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3036. }
  3037. sqlite3_finalize(statement);
  3038. }
  3039. return cart_count;
  3040. }
  3041. +(NSData*) offline_requestcart :(NSMutableDictionary *) params
  3042. {
  3043. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3044. sqlite3 *db = [iSalesDB get_db];
  3045. UIApplication * app = [UIApplication sharedApplication];
  3046. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3047. NSString* orderCode=params[@"orderCode"];
  3048. int sort = [[params objectForKey:@"sort"] intValue];
  3049. NSString *sort_str = @"";
  3050. switch (sort) {
  3051. case 0:{
  3052. sort_str = @"order by c.modify_time desc";
  3053. }
  3054. break;
  3055. case 1:{
  3056. sort_str = @"order by c.modify_time asc";
  3057. }
  3058. break;
  3059. case 2:{
  3060. sort_str = @"order by m.name asc";
  3061. }
  3062. break;
  3063. case 3:{
  3064. sort_str = @"order by m.name desc";
  3065. }
  3066. break;
  3067. case 4:{
  3068. sort_str = @"order by m.description asc";
  3069. }
  3070. break;
  3071. default:
  3072. break;
  3073. }
  3074. NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id,modify_time from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id %@",orderCode,sort_str ];
  3075. // NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,decrypt(c.str_price),c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from offline_cart c left join product m on c.product_id=m.product_id where c.so_no='%@'",orderCode ];
  3076. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3077. DebugLog(@"offline_login sql:%@",sqlQuery);
  3078. sqlite3_stmt * statement;
  3079. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3080. NSDate *date1 = [NSDate date];
  3081. int count=0;
  3082. int cart_count=0;
  3083. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3084. if ( dbresult== SQLITE_OK)
  3085. {
  3086. while (sqlite3_step(statement) == SQLITE_ROW)
  3087. {
  3088. NSDate *row_date = [NSDate date];
  3089. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  3090. int product_id = sqlite3_column_int(statement, 0);
  3091. char *str_price = (char*)sqlite3_column_text(statement, 1);
  3092. int item_id = sqlite3_column_int(statement, 7);
  3093. NSString* Price=nil;
  3094. if(str_price==nil)
  3095. {
  3096. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  3097. // NSDate *price_date = [NSDate date];
  3098. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  3099. // DebugLog(@"price time interval");
  3100. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  3101. if(price==nil)
  3102. Price=@"No Price.";
  3103. else
  3104. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  3105. }
  3106. else
  3107. {
  3108. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  3109. }
  3110. double discount = sqlite3_column_double(statement, 2);
  3111. int item_count = sqlite3_column_int(statement, 3);
  3112. char *line_note = (char*)sqlite3_column_text(statement, 4);
  3113. NSString *nsline_note=nil;
  3114. if(line_note!=nil)
  3115. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  3116. char *name = (char*)sqlite3_column_text(statement, 5);
  3117. NSString *nsname=nil;
  3118. if(name!=nil)
  3119. nsname= [[NSString alloc]initWithUTF8String:name];
  3120. char *description = (char*)sqlite3_column_text(statement, 6);
  3121. NSString *nsdescription=nil;
  3122. if(description!=nil)
  3123. nsdescription= [[NSString alloc]initWithUTF8String:description];
  3124. int stockUom = sqlite3_column_int(statement, 8);
  3125. int _id = sqlite3_column_int(statement, 9);
  3126. // NSDate *subtotal_date = [NSDate date];
  3127. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  3128. // DebugLog(@"subtotal_date time interval");
  3129. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  3130. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  3131. double weight=[bsubtotaljson[@"weight"] doubleValue];
  3132. int carton=[bsubtotaljson[@"carton"] intValue];
  3133. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3134. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  3135. // NSDate *img_date = [NSDate date];
  3136. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  3137. // DebugLog(@"img_date time interval");
  3138. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  3139. itemjson[@"model"]=nsname;
  3140. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  3141. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  3142. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  3143. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  3144. itemjson[@"check"]=@"true";
  3145. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  3146. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  3147. itemjson[@"unit_price"]=Price;
  3148. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  3149. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  3150. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  3151. itemjson[@"note"]=nsline_note;
  3152. // NSDate *date2 = [NSDate date];
  3153. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  3154. // DebugLog(@"model_bundle time interval");
  3155. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  3156. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  3157. count++;
  3158. // DebugLog(@"row time interval");
  3159. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  3160. }
  3161. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3162. sqlite3_finalize(statement);
  3163. }
  3164. DebugLog(@"request cart total time interval");
  3165. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  3166. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  3167. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  3168. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3169. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3170. ret[@"count"]=[NSNumber numberWithInt:count ];
  3171. ret[@"mode"]=@"Regular Mode";
  3172. [iSalesDB close_db:db];
  3173. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  3174. DebugLog(@"general notes :%@",general_note);
  3175. ret[@"general_note"]= general_note;
  3176. return [RAUtils dict2data:ret];
  3177. }
  3178. +(NSData*) offline_login :(NSMutableDictionary *) params
  3179. {
  3180. NSString* user = [params valueForKey:@"user"];
  3181. NSString* password = [params valueForKey:@"password"];
  3182. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3183. sqlite3 *db = [iSalesDB get_db];
  3184. NSString *sqlQuery = [NSString stringWithFormat:@"select can_show_price,can_see_price,contact_id,user_type,can_cancel_order,can_set_cart_price,can_create_portfolio,can_delete_order,can_submit_order,can_set_tearsheet_price,can_create_order,mode,username,can_update_contact_info from offline_login where lower(username)='%@' and password='%@'",user.lowercaseString ,[AESCrypt encrypt:password password:@"usai"]];
  3185. DebugLog(@"offline_login sql:%@",sqlQuery);
  3186. sqlite3_stmt * statement;
  3187. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3188. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3189. {
  3190. if (sqlite3_step(statement) == SQLITE_ROW)
  3191. {
  3192. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3193. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  3194. int can_show_price = sqlite3_column_int(statement, 0);
  3195. int can_see_price = sqlite3_column_int(statement, 1);
  3196. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  3197. if(contact_id==nil)
  3198. contact_id="";
  3199. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3200. int user_type = sqlite3_column_int(statement, 3);
  3201. int can_cancel_order = sqlite3_column_int(statement, 4);
  3202. int can_set_cart_price = sqlite3_column_int(statement, 5);
  3203. int can_create_portfolio = sqlite3_column_int(statement, 6);
  3204. int can_delete_order = sqlite3_column_int(statement, 7);
  3205. int can_submit_order = sqlite3_column_int(statement, 8);
  3206. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  3207. int can_create_order = sqlite3_column_int(statement, 10);
  3208. char *mode = (char*)sqlite3_column_text(statement, 11);
  3209. if(mode==nil)
  3210. mode="";
  3211. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  3212. char *username = (char*)sqlite3_column_text(statement, 12);
  3213. if(username==nil)
  3214. username="";
  3215. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  3216. int can_update_contact_info = sqlite3_column_int(statement, 13);
  3217. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  3218. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  3219. [header setValue:nscontact_id forKey:@"contact_id"];
  3220. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  3221. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  3222. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  3223. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  3224. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  3225. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  3226. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  3227. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  3228. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  3229. [header setValue:nsusername forKey:@"username"];
  3230. NSError* error=nil;
  3231. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  3232. [header setValue:statusFilter forKey:@"statusFilter"];
  3233. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3234. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  3235. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  3236. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  3237. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  3238. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  3239. [ret setObject:header forKey:@"header"];
  3240. [ret setValue:nsmode forKey:@"mode"];
  3241. }
  3242. sqlite3_finalize(statement);
  3243. }
  3244. [iSalesDB close_db:db];
  3245. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3246. return [RAUtils dict2data:ret];
  3247. }
  3248. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  3249. {
  3250. NSString* contactId = [params valueForKey:@"contactId"];
  3251. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3252. sqlite3 *db = [iSalesDB get_db];
  3253. NSString *sqlQuery = nil;
  3254. {
  3255. sqlQuery=[NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email,img_0,img_1,img_2,price_type,notes,sales_rep,type from offline_contact where contact_id='%@'",contactId];
  3256. }
  3257. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  3258. sqlite3_stmt * statement;
  3259. [ret setValue:@"2" forKey:@"result"];
  3260. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3261. {
  3262. //int i = 0;
  3263. if (sqlite3_step(statement) == SQLITE_ROW)
  3264. {
  3265. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3266. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3267. // int editable = sqlite3_column_int(statement, 0);
  3268. char *company_name = (char*)sqlite3_column_text(statement, 1);
  3269. NSString *nscompany_name =nil;
  3270. if(company_name==nil)
  3271. nscompany_name=@"";
  3272. else
  3273. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  3274. char *country = (char*)sqlite3_column_text(statement, 2);
  3275. if(country==nil)
  3276. country="";
  3277. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  3278. // char *addr = (char*)sqlite3_column_text(statement, 3);
  3279. // if(addr==nil)
  3280. // addr="";
  3281. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  3282. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  3283. if(zipcode==nil)
  3284. zipcode="";
  3285. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  3286. char *state = (char*)sqlite3_column_text(statement, 5);
  3287. if(state==nil)
  3288. state="";
  3289. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  3290. char *city = (char*)sqlite3_column_text(statement, 6);
  3291. if(city==nil)
  3292. city="";
  3293. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  3294. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  3295. // NSString *nscontact_name = nil;
  3296. // if(contact_name==nil)
  3297. // nscontact_name=@"";
  3298. // else
  3299. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  3300. char *phone = (char*)sqlite3_column_text(statement, 8);
  3301. NSString *nsphone = nil;
  3302. if(phone==nil)
  3303. nsphone=@"";
  3304. else
  3305. nsphone= [[NSString alloc]initWithUTF8String:phone];
  3306. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  3307. if(contact_id==nil)
  3308. contact_id="";
  3309. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3310. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  3311. if(addr_1==nil)
  3312. addr_1="";
  3313. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  3314. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  3315. if(addr_2==nil)
  3316. addr_2="";
  3317. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  3318. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  3319. if(addr_3==nil)
  3320. addr_3="";
  3321. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  3322. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  3323. if(addr_4==nil)
  3324. addr_4="";
  3325. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  3326. char *first_name = (char*)sqlite3_column_text(statement, 14);
  3327. if(first_name==nil)
  3328. first_name="";
  3329. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  3330. char *last_name = (char*)sqlite3_column_text(statement, 15);
  3331. if(last_name==nil)
  3332. last_name="";
  3333. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  3334. char *fax = (char*)sqlite3_column_text(statement, 16);
  3335. NSString *nsfax = nil;
  3336. if(fax==nil)
  3337. nsfax=@"";
  3338. else
  3339. nsfax= [[NSString alloc]initWithUTF8String:fax];
  3340. char *email = (char*)sqlite3_column_text(statement, 17);
  3341. NSString *nsemail = nil;
  3342. if(email==nil)
  3343. nsemail=@"";
  3344. else
  3345. nsemail= [[NSString alloc]initWithUTF8String:email];
  3346. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  3347. NSString *nsimg_0 = nil;
  3348. if(img_0==nil)
  3349. nsimg_0=@"";
  3350. else
  3351. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  3352. [self copy_bcardImg:nsimg_0];
  3353. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  3354. NSString *nsimg_1 = nil;
  3355. if(img_1==nil)
  3356. nsimg_1=@"";
  3357. else
  3358. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  3359. [self copy_bcardImg:nsimg_1];
  3360. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  3361. NSString *nsimg_2 = nil;
  3362. if(img_2==nil)
  3363. nsimg_2=@"";
  3364. else
  3365. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  3366. [self copy_bcardImg:nsimg_2];
  3367. char *price_type = (char*)sqlite3_column_text(statement, 21);
  3368. NSString *nsprice_type = nil;
  3369. if(price_type==nil)
  3370. nsprice_type=@"";
  3371. else
  3372. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  3373. char *notes = (char*)sqlite3_column_text(statement, 22);
  3374. NSString *nsnotes = nil;
  3375. if(notes==nil)
  3376. nsnotes=@"";
  3377. else
  3378. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  3379. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  3380. NSString *nssalesrep = nil;
  3381. if(salesrep==nil)
  3382. nssalesrep=@"";
  3383. else
  3384. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  3385. NSString *contact_type = [self textAtColumn:24 statement:statement];
  3386. {
  3387. // decrypt
  3388. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  3389. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  3390. nsphone=[AESCrypt fastdecrypt:nsphone];
  3391. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3392. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  3393. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3394. }
  3395. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3396. [arr_name addObject:nsfirst_name];
  3397. [arr_name addObject:nslast_name];
  3398. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  3399. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  3400. [arr_ext addObject:nsaddr_1];
  3401. [arr_ext addObject:nsaddr_2];
  3402. [arr_ext addObject:nsaddr_3];
  3403. [arr_ext addObject:nsaddr_4];
  3404. [arr_ext addObject:@"\r\n"];
  3405. [arr_ext addObject:nscity];
  3406. [arr_ext addObject:nsstate];
  3407. [arr_ext addObject:nszipcode];
  3408. [arr_ext addObject:nscountry];
  3409. NSString *nsext=[RAUtils arr2string:arr_ext separator:@", " trim:true];
  3410. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  3411. [item setValue:nsimg_2 forKey:@"business_card_2"];
  3412. [item setValue:nsimg_0 forKey:@"business_card_0"];
  3413. [item setValue:nscountry forKey:@"customer_country"];
  3414. [item setValue:nsphone forKey:@"customer_phone"];
  3415. [item setValue:nsimg_1 forKey:@"business_card_1"];
  3416. [item setValue:nscompany_name forKey:@"customer_name"];
  3417. [item setValue:nsprice_type forKey:@"customer_price_type"];
  3418. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  3419. [item setValue:nsext forKey:@"customer_contact_ext"];
  3420. [item setValue:nszipcode forKey:@"customer_zipcode"];
  3421. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  3422. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  3423. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  3424. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  3425. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  3426. [item setValue:nslast_name forKey:@"customer_last_name"];
  3427. [item setValue:nscity forKey:@"customer_city"];
  3428. [item setValue:nsstate forKey:@"customer_state"];
  3429. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  3430. [item setValue:contactId forKey:@"customer_cid"];
  3431. [item setValue:nscontact_name forKey:@"customer_contact"];
  3432. [item setValue:nsfax forKey:@"customer_fax"];
  3433. [item setValue:nsemail forKey:@"customer_email"];
  3434. [item setValue:contact_type forKey:@"customer_contact_type"];
  3435. [ret setObject:item forKey:@"customerInfo"];
  3436. // i++;
  3437. }
  3438. UIApplication * app = [UIApplication sharedApplication];
  3439. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3440. [ret setValue:appDelegate.mode forKey:@"mode"];
  3441. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  3442. sqlite3_finalize(statement);
  3443. }
  3444. [iSalesDB close_db:db];
  3445. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3446. return ret;
  3447. }
  3448. + (bool) copy_bcardImg:(NSString*) filename
  3449. {
  3450. if(filename.length==0)
  3451. return false;
  3452. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3453. bool ret=false;
  3454. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3455. NSString *cachefolder = [paths objectAtIndex:0];
  3456. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  3457. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  3458. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  3459. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  3460. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  3461. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3462. //
  3463. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3464. NSFileManager* fileManager = [NSFileManager defaultManager];
  3465. BOOL bdir=NO;
  3466. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  3467. {
  3468. NSError *error = nil;
  3469. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  3470. {
  3471. ret=false;
  3472. }
  3473. else
  3474. {
  3475. ret=true;
  3476. }
  3477. // NSError *error = nil;
  3478. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  3479. //
  3480. // if(!bsuccess)
  3481. // {
  3482. // DebugLog(@"Create offline_createimg folder failed");
  3483. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3484. // return [RAUtils dict2data:ret];
  3485. // }
  3486. // if(bsuccess)
  3487. // {
  3488. // sqlite3 *db = [self get_db];
  3489. //
  3490. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3491. // [iSalesDB close_db:db];
  3492. // }
  3493. }
  3494. return ret;
  3495. //
  3496. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3497. // if(bsuccess)
  3498. // {
  3499. // NSError *error = nil;
  3500. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3501. // {
  3502. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3503. // }
  3504. // else
  3505. // {
  3506. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3507. //
  3508. // ret[@"img_url_aname"]=filename;
  3509. // ret[@"img_url"]=savedImagePath;
  3510. // }
  3511. // }
  3512. }
  3513. +(NSData *) offline_saveBusinesscard:(NSData *) image
  3514. {
  3515. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3516. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3517. NSString *cachefolder = [paths objectAtIndex:0];
  3518. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  3519. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  3520. NSFileManager* fileManager = [NSFileManager defaultManager];
  3521. BOOL bdir=YES;
  3522. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  3523. {
  3524. NSError *error = nil;
  3525. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  3526. if(!bsuccess)
  3527. {
  3528. DebugLog(@"Create offline_createimg folder failed");
  3529. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3530. return [RAUtils dict2data:ret];
  3531. }
  3532. // if(bsuccess)
  3533. // {
  3534. // sqlite3 *db = [self get_db];
  3535. //
  3536. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3537. // [iSalesDB close_db:db];
  3538. // }
  3539. }
  3540. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  3541. //JEPG格式
  3542. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  3543. NSString* filename =[NSString stringWithFormat:@"%@.png",[[NSUUID UUID] UUIDString]];
  3544. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3545. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3546. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3547. if(bsuccess)
  3548. {
  3549. NSError *error = nil;
  3550. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3551. {
  3552. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3553. }
  3554. else
  3555. {
  3556. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3557. ret[@"img_url_aname"]=filename;
  3558. ret[@"img_url"]=filename;
  3559. }
  3560. }
  3561. else
  3562. {
  3563. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3564. }
  3565. return [RAUtils dict2data:ret];
  3566. }
  3567. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  3568. {
  3569. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  3570. category = [category substringToIndex:3];
  3571. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3572. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  3573. params[@"category"]= category;
  3574. ret[@"params"]= params;
  3575. [ret setValue:@"detail" forKey:@"target"];
  3576. [ret setValue:@"popup" forKey:@"action"];
  3577. [ret setValue:@"content" forKey:@"type"];
  3578. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  3579. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  3580. [ret setValue:@"true" forKey:@"single_row"];
  3581. [ret setValue:@"true" forKey:@"partial_refresh"];
  3582. // sqlite3 *db = [iSalesDB get_db];
  3583. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  3584. NSString *sqlQuery = [NSString stringWithFormat:@"select m.name,m.product_id,i.url,random() as aa from product m LEFT join model_image i on m.product_id = i.product_id where m.category like'%%#%@#%%' and m.category like'%%#005#%%' and i.default_img = %d order by aa limit 10 ;",category,OFFLINE_IMG_TYPE_CATEGORY]; // select m.name,m.product_id,i.url,random() as aa from model m LEFT join model_image i on m.product_id = i.product_id where m.category like'%%#%@#%%' and m.category like'%%#005#%%' and i.default_img = %d order by aa limit 10 ;
  3585. sqlite3_stmt * statement;
  3586. int count = 0;
  3587. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  3588. // int count=0;
  3589. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3590. {
  3591. int i=0;
  3592. while (sqlite3_step(statement) == SQLITE_ROW)
  3593. {
  3594. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3595. // char *name = (char*)sqlite3_column_text(statement, 1);
  3596. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3597. char *name = (char*)sqlite3_column_text(statement, 0);
  3598. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3599. int product_id = sqlite3_column_int(statement, 1);
  3600. char *url = (char*)sqlite3_column_text(statement, 2);
  3601. if(url==nil)
  3602. url="";
  3603. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3604. [item setValue:nsurl forKey:@"picture_path"];
  3605. [item setValue:nsname forKey:@"fash_name"];
  3606. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3607. [item setValue:category forKey:@"category"];
  3608. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3609. i++;
  3610. }
  3611. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3612. sqlite3_finalize(statement);
  3613. }
  3614. DebugLog(@"count:%d",count);
  3615. // [iSalesDB close_db:db];
  3616. return ret;
  3617. }
  3618. +(NSDictionary*) model_priceKvItem:(NSString*) key value:(NSString*)value
  3619. {
  3620. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3621. [ret setValue:key forKey:@"key"];
  3622. [ret setValue:value forKey:@"val"];
  3623. [ret setValue:@"price" forKey:@"type"];
  3624. return ret;
  3625. }
  3626. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  3627. {
  3628. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3629. [ret setValue:key forKey:@"key"];
  3630. [ret setValue:value forKey:@"val"];
  3631. return ret;
  3632. }
  3633. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  3634. {
  3635. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3636. [ret setValue:@"0" forKey:@"img_count"];
  3637. // sqlite3 *db = [iSalesDB get_db];
  3638. NSString *sqlQuery = [NSString stringWithFormat:@"select %@,property_display from product where product_id=%d ;",field,product_id]; // select %@,property_display from model where product_id=%d ;
  3639. sqlite3_stmt * statement;
  3640. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3641. {
  3642. int i=0;
  3643. if (sqlite3_step(statement) == SQLITE_ROW)
  3644. {
  3645. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3646. // char *name = (char*)sqlite3_column_text(statement, 1);
  3647. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3648. char *value = (char*)sqlite3_column_text(statement, 0);
  3649. if(value==nil)
  3650. value="";
  3651. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  3652. char *key = (char*)sqlite3_column_text(statement, 1);
  3653. if(key==nil)
  3654. key="";
  3655. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  3656. [item setValue:nsvalue forKey:@"val"];
  3657. [item setValue:nskey forKey:@"key"];
  3658. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3659. [ret setValue:@"1" forKey:@"count"];
  3660. i++;
  3661. }
  3662. sqlite3_finalize(statement);
  3663. }
  3664. // [iSalesDB close_db:db];
  3665. return ret;
  3666. }
  3667. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  3668. {
  3669. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3670. [ret setValue:@"0" forKey:@"count"];
  3671. // sqlite3 *db = [iSalesDB get_db];
  3672. NSString *sqlQuery = [NSString stringWithFormat:@"select %@,selector_display,product_id,category from product where product_group='%@' order by name;",field,product_group]; // select %@,selector_display,product_id,category from model where product_group='%@' order by name;
  3673. sqlite3_stmt * statement;
  3674. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3675. {
  3676. int i=0;
  3677. while (sqlite3_step(statement) == SQLITE_ROW)
  3678. {
  3679. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3680. // char *name = (char*)sqlite3_column_text(statement, 1);
  3681. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3682. char *value = (char*)sqlite3_column_text(statement, 0);
  3683. if(value==nil)
  3684. value="";
  3685. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  3686. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  3687. if(selector_display==nil)
  3688. selector_display="";
  3689. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  3690. int product_id = sqlite3_column_int(statement, 2);
  3691. char *category = (char*)sqlite3_column_text(statement, 3);
  3692. if(category==nil)
  3693. category="";
  3694. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  3695. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  3696. [item setValue:nsvalue forKey:@"title"];
  3697. [item setValue:url forKey:@"pic_url"];
  3698. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  3699. [params setValue:@"2" forKey:@"count"];
  3700. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  3701. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  3702. [param0 setValue:@"product_id" forKey:@"name"];
  3703. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  3704. [param1 setValue:nscategory forKey:@"val"];
  3705. [param1 setValue:@"category" forKey:@"name"];
  3706. [params setObject:param0 forKey:@"param_0"];
  3707. [params setObject:param1 forKey:@"param_1"];
  3708. [item setObject:params forKey:@"params"];
  3709. [ret setValue:nsselector_display forKey:@"name"];
  3710. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3711. i++;
  3712. }
  3713. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3714. [ret setValue:@"switch" forKey:@"action"];
  3715. sqlite3_finalize(statement);
  3716. }
  3717. // [iSalesDB close_db:db];
  3718. return ret;
  3719. }
  3720. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  3721. {
  3722. // model 在 category search 显示的图片。
  3723. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  3724. // sqlite3 *db = [iSalesDB get_db];
  3725. NSString *sqlQuery = [NSString stringWithFormat:@"select url,type from model_image where product_id=%d and type<2 order by default_img desc , _id asc limit 2;",product_id];
  3726. sqlite3_stmt * statement;
  3727. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3728. {
  3729. while (sqlite3_step(statement) == SQLITE_ROW)
  3730. {
  3731. char *url = (char*)sqlite3_column_text(statement, 0);
  3732. if(url==nil)
  3733. url="";
  3734. int type = sqlite3_column_int(statement, 1);
  3735. if(type==0)
  3736. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  3737. else
  3738. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  3739. }
  3740. sqlite3_finalize(statement);
  3741. }
  3742. // [iSalesDB close_db:db];
  3743. return ret;
  3744. }
  3745. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  3746. {
  3747. int item_id=-1;
  3748. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  3749. sqlite3_stmt * statement;
  3750. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3751. {
  3752. if (sqlite3_step(statement) == SQLITE_ROW)
  3753. {
  3754. item_id = sqlite3_column_int(statement, 0);
  3755. }
  3756. sqlite3_finalize(statement);
  3757. }
  3758. return item_id;
  3759. }
  3760. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  3761. {
  3762. // NSString* ret = @"";
  3763. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  3764. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  3765. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  3766. sqlite3_stmt * statement;
  3767. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3768. {
  3769. while (sqlite3_step(statement) == SQLITE_ROW)
  3770. {
  3771. int bitem_id = sqlite3_column_int(statement, 0);
  3772. int bitem_qty = sqlite3_column_int(statement, 1);
  3773. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  3774. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  3775. }
  3776. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  3777. sqlite3_finalize(statement);
  3778. }
  3779. // if(ret==nil)
  3780. // ret=@"";
  3781. *count=arr_count;
  3782. return arr_bundle;
  3783. }
  3784. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  3785. {
  3786. // get default sold qty, return -1 if model not found;
  3787. int ret = -1;
  3788. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  3789. sqlite3_stmt * statement;
  3790. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3791. {
  3792. if (sqlite3_step(statement) == SQLITE_ROW)
  3793. {
  3794. ret = sqlite3_column_int(statement, 0);
  3795. }
  3796. sqlite3_finalize(statement);
  3797. }
  3798. return ret;
  3799. }
  3800. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  3801. {
  3802. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3803. [ret setValue:@"0" forKey:@"img_count"];
  3804. // sqlite3 *db = [iSalesDB get_db];
  3805. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  3806. sqlite3_stmt * statement;
  3807. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3808. {
  3809. int i=0;
  3810. while (sqlite3_step(statement) == SQLITE_ROW)
  3811. {
  3812. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3813. // char *name = (char*)sqlite3_column_text(statement, 1);
  3814. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3815. char *url = (char*)sqlite3_column_text(statement, 0);
  3816. if(url==nil)
  3817. url="";
  3818. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3819. [item setValue:nsurl forKey:@"s"];
  3820. [item setValue:nsurl forKey:@"l"];
  3821. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  3822. i++;
  3823. }
  3824. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  3825. sqlite3_finalize(statement);
  3826. }
  3827. // [iSalesDB close_db:db];
  3828. return ret;
  3829. }
  3830. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  3831. UIApplication * app = [UIApplication sharedApplication];
  3832. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3833. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  3834. sqlite3 *db = [iSalesDB get_db];
  3835. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  3836. NSString* product_id=params[@"product_id"];
  3837. NSString *item_count_str = params[@"item_count"];
  3838. NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  3839. NSArray *item_count_arr = item_count_str ? [RAUtils string2arr:item_count_str separator:@","] : nil;
  3840. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  3841. // NSString *sql = @"";
  3842. for(int i=0;i<arr.count;i++)
  3843. {
  3844. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  3845. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  3846. __block int cart_count = 0;
  3847. if (!item_count_str) {
  3848. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  3849. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3850. NSString *model_set = [self textAtColumn:0 statement:stmt];
  3851. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  3852. cart_count = [[model_set_components lastObject] integerValue];
  3853. }];
  3854. }
  3855. if(count==0)
  3856. {
  3857. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  3858. sqlite3_stmt *stmt;
  3859. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  3860. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],[arr[i] length],NULL);
  3861. if (item_count_arr) {
  3862. sqlite3_bind_int(stmt,2,[item_count_arr[i] integerValue]);
  3863. } else {
  3864. sqlite3_bind_int(stmt,2,cart_count);
  3865. }
  3866. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  3867. [iSalesDB execSql:@"ROLLBACK" db:db];
  3868. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  3869. [iSalesDB close_db:db];
  3870. DebugLog(@"add to wishlist error");
  3871. return ret;
  3872. }
  3873. } else {
  3874. int qty = 0;
  3875. if (item_count_arr) {
  3876. qty = [item_count_arr[i] integerValue];
  3877. } else {
  3878. qty = cart_count;
  3879. }
  3880. sqlQuery = [NSString stringWithFormat:@"update wishlist set qty = 1 where product_id = %@ and (is_delete is null or is_delete != 1);update wishlist set qty = %d,is_delete = 0 where product_id = %@ and is_delete = 1;",arr[i],qty,arr[i]];
  3881. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  3882. [iSalesDB execSql:@"ROLLBACK" db:db];
  3883. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  3884. [iSalesDB close_db:db];
  3885. DebugLog(@"add to wishlist error");
  3886. return ret;
  3887. }
  3888. }
  3889. }
  3890. // [iSalesDB execSql:sql db:db];
  3891. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3892. [iSalesDB execSql:@"END TRANSACTION" db:db];
  3893. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  3894. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  3895. [iSalesDB close_db:db];
  3896. appDelegate.wish_count =count;
  3897. [appDelegate update_count_mark];
  3898. ret[@"result"]= [NSNumber numberWithInt:2];
  3899. return ret;
  3900. }
  3901. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  3902. // 0 category
  3903. // 1 search
  3904. // 2 itemsearch
  3905. NSData *ret = nil;
  3906. NSDictionary *items = nil;
  3907. switch (from) {
  3908. case 0:{
  3909. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  3910. }
  3911. break;
  3912. case 1:{
  3913. items = [[self search:params limited:NO] objectForKey:@"items"];
  3914. }
  3915. break;
  3916. case 2:{
  3917. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  3918. }
  3919. break;
  3920. default:
  3921. break;
  3922. }
  3923. if (!items) {
  3924. return ret;
  3925. }
  3926. int count = [[items objectForKey:@"count"] intValue];
  3927. NSMutableString *product_id_str = [@"" mutableCopy];
  3928. for (int i = 0; i < count; i++) {
  3929. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  3930. NSDictionary *item = [items objectForKey:key];
  3931. NSString *product_id = [item objectForKey:@"product_id"];
  3932. if (i == 0) {
  3933. [product_id_str appendString:product_id];
  3934. } else {
  3935. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  3936. }
  3937. }
  3938. NSString *add_to = [self valueInParams:params key:@"addTo"];
  3939. if ([add_to isEqualToString:@"cart"]) {
  3940. NSString *order_code = [params objectForKey:@"orderCode"];
  3941. if (order_code.length) {
  3942. NSDictionary *newParams = @{
  3943. @"product_id" : product_id_str,
  3944. @"orderCode" : order_code
  3945. };
  3946. ret = [self offline_add2cart:newParams.mutableCopy];
  3947. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3948. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  3949. ret = [RAUtils dict2data:retDic];
  3950. }
  3951. } else if([add_to isEqualToString:@"wishlist"]) {
  3952. NSDictionary *newParams = @{
  3953. @"product_id" : product_id_str
  3954. };
  3955. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  3956. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  3957. ret = [RAUtils dict2data:retDic];
  3958. } else if([add_to isEqualToString:@"portfolio"]) {
  3959. }
  3960. return ret;
  3961. }
  3962. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  3963. {
  3964. return [self addAll:params from:0];
  3965. }
  3966. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  3967. {
  3968. return [self addAll:params from:1];
  3969. }
  3970. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  3971. {
  3972. return [self addAll:params from:2];
  3973. }
  3974. #pragma mark - Jack
  3975. #warning 做SQL操作时转义!!
  3976. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  3977. // "val_227" : {
  3978. // "check" : 1,
  3979. // "value" : "US United States",
  3980. // "value_id" : "228"
  3981. // },
  3982. if (!countryCode) {
  3983. countryCode = @"US";
  3984. }
  3985. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  3986. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  3987. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  3988. int code_id = sqlite3_column_int(stmt, 3); // id
  3989. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3990. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  3991. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  3992. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3993. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  3994. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3995. }
  3996. long n = *count;
  3997. *count = n + 1;
  3998. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  3999. [container setValue:countryDic forKey:key];
  4000. }] mutableCopy];
  4001. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4002. return ret;
  4003. }
  4004. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  4005. countryCode = [self translateSingleQuote:countryCode];
  4006. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  4007. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4008. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  4009. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  4010. if (name == NULL) {
  4011. name = "";
  4012. }
  4013. if (code == NULL) {
  4014. code = "";
  4015. }
  4016. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4017. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4018. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  4019. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4020. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  4021. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4022. }
  4023. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4024. [container setValue:stateDic forKey:key];
  4025. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  4026. DebugLog(@"query all state error: %@",err_msg);
  4027. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4028. // [stateDic setValue:@"Other" forKey:@"value"];
  4029. // [stateDic setValue:@"" forKey:@"value_id"];
  4030. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4031. //
  4032. // if (state_code && [@"" isEqualToString:state_code]) {
  4033. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4034. // }
  4035. //
  4036. // NSString *key = [NSString stringWithFormat:@"val_0"];
  4037. // [container setValue:stateDic forKey:key];
  4038. }] mutableCopy];
  4039. [ret removeObjectForKey:@"result"];
  4040. // failure 可以不用了,一样的
  4041. if (ret.allKeys.count == 0) {
  4042. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4043. [stateDic setValue:@"Other" forKey:@"value"];
  4044. [stateDic setValue:@"" forKey:@"value_id"];
  4045. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4046. if (state_code && [@"" isEqualToString:state_code]) {
  4047. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4048. }
  4049. NSString *key = [NSString stringWithFormat:@"val_0"];
  4050. [ret setValue:stateDic forKey:key];
  4051. }
  4052. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4053. return ret;
  4054. }
  4055. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  4056. codeId = [self translateSingleQuote:codeId];
  4057. NSString *sql = [NSString stringWithFormat:@"select name,code from offline_state where country_code = (select code as country_code FROM offline_country where countrycode_id = %@);",codeId];
  4058. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4059. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  4060. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  4061. if (name == NULL) {
  4062. name = "";
  4063. }
  4064. if (code == NULL) {
  4065. code = "";
  4066. }
  4067. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4068. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4069. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  4070. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4071. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  4072. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4073. }
  4074. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4075. [container setValue:stateDic forKey:key];
  4076. }] mutableCopy];
  4077. [ret removeObjectForKey:@"result"];
  4078. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4079. return ret;
  4080. }
  4081. + (NSDictionary *)offline_getPrice {
  4082. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price;"];
  4083. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4084. char *name = (char *) sqlite3_column_text(stmt, 1);
  4085. int type = sqlite3_column_int(stmt, 2);
  4086. int orderBy = sqlite3_column_int(stmt, 3);
  4087. if (name == NULL) {
  4088. name = "";
  4089. }
  4090. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  4091. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4092. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  4093. NSString *key = [NSString stringWithFormat:@"val_%d",orderBy];
  4094. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4095. if (orderBy == 0) {
  4096. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4097. }
  4098. [container setValue:priceDic forKey:key];
  4099. }] mutableCopy];
  4100. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4101. return ret;
  4102. }
  4103. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  4104. NSString *sql = @"select _id,type_name from contact_type";
  4105. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  4106. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  4107. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4108. int _id = sqlite3_column_int(stmt, 0);
  4109. NSString *name = [self textAtColumn:1 statement:stmt];
  4110. NSDictionary *typeDic = @{
  4111. @"value_id" : name,
  4112. @"value" : name,
  4113. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  4114. };
  4115. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  4116. [ret setValue:[NSNumber numberWithInt:++(*count)] forKey:@"count"];
  4117. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  4118. }];
  4119. return ret;
  4120. }
  4121. + (NSDictionary *)offline_getSalesRep {
  4122. // 首先从offline_login表中取出sales_code
  4123. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4124. NSString *user = app.user;
  4125. user = [self translateSingleQuote:user];
  4126. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  4127. __block NSString *user_code = @"";
  4128. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4129. char *code = (char *)sqlite3_column_text(stmt, 0);
  4130. if (code == NULL) {
  4131. code = "";
  4132. }
  4133. user_code = [NSString stringWithUTF8String:code];
  4134. }];
  4135. // 再取所有salesRep
  4136. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  4137. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4138. // 1 name 2 code 3 salesrep_id
  4139. char *name = (char *)sqlite3_column_text(stmt, 1);
  4140. char *code = (char *)sqlite3_column_text(stmt, 2);
  4141. int salesrep_id = sqlite3_column_int(stmt, 3);
  4142. if (name == NULL) {
  4143. name = "";
  4144. }
  4145. if (code == NULL) {
  4146. code = "";
  4147. }
  4148. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  4149. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  4150. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  4151. // 比较code 相等则check
  4152. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  4153. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4154. }
  4155. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  4156. }] mutableCopy];
  4157. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4158. return ret;
  4159. }
  4160. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  4161. zipcode = [self translateSingleQuote:zipcode];
  4162. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  4163. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4164. char *country = (char *)sqlite3_column_text(stmt, 0);
  4165. char *state = (char *)sqlite3_column_text(stmt, 1);
  4166. char *city = (char *)sqlite3_column_text(stmt, 2);
  4167. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  4168. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  4169. if (country == NULL) {
  4170. country = "";
  4171. }
  4172. if (state == NULL) {
  4173. state = "";
  4174. }
  4175. if (city == NULL) {
  4176. city = "";
  4177. }
  4178. if (country_code == NULL) {
  4179. country_code = "";
  4180. }
  4181. if (state_code == NULL) {
  4182. state_code = "";
  4183. }
  4184. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  4185. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  4186. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  4187. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  4188. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  4189. }] mutableCopy];
  4190. return ret;
  4191. }
  4192. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  4193. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  4194. [item setValue:value forKey:valueKey];
  4195. [dic setValue:item forKey:itemKey];
  4196. }
  4197. + (NSString *)countryCodeByid:(NSString *)code_id {
  4198. NSString *ret = nil;
  4199. code_id = [self translateSingleQuote:code_id];
  4200. sqlite3 *db = [iSalesDB get_db];
  4201. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  4202. sqlite3_stmt * statement;
  4203. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4204. while (sqlite3_step(statement) == SQLITE_ROW) {
  4205. char *code = (char *)sqlite3_column_text(statement, 0);
  4206. if (code == NULL) {
  4207. code = "";
  4208. }
  4209. ret = [NSString stringWithUTF8String:code];
  4210. }
  4211. sqlite3_finalize(statement);
  4212. }
  4213. [iSalesDB close_db:db];
  4214. return ret;
  4215. }
  4216. + (NSString *)countryCodeIdByCode:(NSString *)code {
  4217. NSString *ret = nil;
  4218. code = [self translateSingleQuote:code];
  4219. sqlite3 *db = [iSalesDB get_db];
  4220. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  4221. sqlite3_stmt * statement;
  4222. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4223. while (sqlite3_step(statement) == SQLITE_ROW) {
  4224. char *_id = (char *)sqlite3_column_text(statement, 0);
  4225. if (_id == NULL) {
  4226. _id = "";
  4227. }
  4228. ret = [NSString stringWithFormat:@"%s",_id];
  4229. }
  4230. sqlite3_finalize(statement);
  4231. }
  4232. [iSalesDB close_db:db];
  4233. return ret;
  4234. }
  4235. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  4236. NSString *name = nil;
  4237. codeId = [self translateSingleQuote:codeId];
  4238. sqlite3 *db = [iSalesDB get_db];
  4239. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  4240. sqlite3_stmt * statement;
  4241. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4242. while (sqlite3_step(statement) == SQLITE_ROW) {
  4243. char *value = (char *)sqlite3_column_text(statement, 0);
  4244. if (value == NULL) {
  4245. value = "";
  4246. }
  4247. name = [NSString stringWithUTF8String:value];
  4248. }
  4249. sqlite3_finalize(statement);
  4250. }
  4251. [iSalesDB close_db:db];
  4252. return name;
  4253. }
  4254. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  4255. NSString *ret = nil;
  4256. sqlite3 *db = [iSalesDB get_db];
  4257. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  4258. sqlite3_stmt * statement;
  4259. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4260. while (sqlite3_step(statement) == SQLITE_ROW) {
  4261. char *name = (char *)sqlite3_column_text(statement, 0);
  4262. if (name == NULL) {
  4263. name = "";
  4264. }
  4265. ret = [NSString stringWithUTF8String:name];
  4266. }
  4267. sqlite3_finalize(statement);
  4268. }
  4269. [iSalesDB close_db:db];
  4270. return ret;
  4271. }
  4272. + (NSString *)salesRepCodeById:(NSString *)_id {
  4273. NSString *ret = nil;
  4274. _id = [self translateSingleQuote:_id];
  4275. sqlite3 *db = [iSalesDB get_db];
  4276. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  4277. sqlite3_stmt * statement;
  4278. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4279. while (sqlite3_step(statement) == SQLITE_ROW) {
  4280. char *rep = (char *)sqlite3_column_text(statement, 0);
  4281. if (rep == NULL) {
  4282. rep = "";
  4283. }
  4284. ret = [NSString stringWithUTF8String:rep];
  4285. }
  4286. sqlite3_finalize(statement);
  4287. }
  4288. [iSalesDB close_db:db];
  4289. return ret;
  4290. }
  4291. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  4292. char *tx = (char *)sqlite3_column_text(stmt, col);
  4293. if (tx == NULL) {
  4294. tx = "";
  4295. }
  4296. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  4297. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  4298. if (!text) {
  4299. text = @"";
  4300. }
  4301. // 将字符全部为' '的字符串干掉
  4302. int spaceCount = 0;
  4303. for (int i = 0; i < text.length; i++) {
  4304. if ([text characterAtIndex:i] == ' ') {
  4305. spaceCount++;
  4306. }
  4307. }
  4308. if (spaceCount == text.length) {
  4309. text = @"";
  4310. }
  4311. return text;
  4312. }
  4313. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  4314. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  4315. NSData *data = [NSData dataWithContentsOfFile:path];
  4316. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4317. return ret;
  4318. }
  4319. + (NSString *)textFileName:(NSString *)name {
  4320. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  4321. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  4322. if (!text) {
  4323. text = @"";
  4324. }
  4325. return text;
  4326. }
  4327. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  4328. return [[dic objectForKey:key] mutableCopy];
  4329. }
  4330. + (id)translateSingleQuote:(NSString *)string {
  4331. if ([string isKindOfClass:[NSString class]])
  4332. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4333. return string;
  4334. }
  4335. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  4336. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  4337. }
  4338. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  4339. NSString* ret= nil;
  4340. NSString *sqlQuery = nil;
  4341. // sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.item_id=%@ order by i.default_img desc, i._id asc limit 1;",item_id];// select i.url from model m
  4342. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image as i where item_id = %@ order by i.default_img desc, i._id asc limit 1;",item_id];
  4343. sqlite3_stmt * statement;
  4344. // int count=0;
  4345. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4346. {
  4347. if (sqlite3_step(statement) == SQLITE_ROW)
  4348. {
  4349. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  4350. if(imgurl==nil)
  4351. imgurl="";
  4352. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  4353. ret=nsimgurl;
  4354. }
  4355. sqlite3_finalize(statement);
  4356. }
  4357. else
  4358. {
  4359. [ret setValue:@"8" forKey:@"result"];
  4360. }
  4361. // [iSalesDB close_db:db];
  4362. // DebugLog(@"data string: %@",ret );
  4363. return ret;
  4364. }
  4365. #pragma mark contact list
  4366. +(NSDictionary*) offline_contactlist :(NSMutableDictionary *) params
  4367. {
  4368. // contactType = "Sales_Order_Customer";
  4369. // limit = 25;
  4370. // offset = 0;
  4371. // password = 123456;
  4372. // "price_name" = 16;
  4373. // user = EvanK;
  4374. sqlite3 *db = [iSalesDB get_db];
  4375. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  4376. if (contactType) {
  4377. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  4378. } else {
  4379. contactType = @"1 = 1";
  4380. }
  4381. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  4382. DebugLog(@"offline contact list keyword: %@",keyword);
  4383. // advanced search
  4384. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  4385. if (contact_name) {
  4386. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4387. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  4388. } else {
  4389. contact_name = @"";
  4390. }
  4391. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  4392. if (customer_phone) {
  4393. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4394. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  4395. } else {
  4396. customer_phone = @"";
  4397. }
  4398. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  4399. if (customer_fax) {
  4400. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4401. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  4402. } else {
  4403. customer_fax = @"";
  4404. }
  4405. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  4406. if (customer_zipcode) {
  4407. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4408. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  4409. } else {
  4410. customer_zipcode = @"";
  4411. }
  4412. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  4413. if (customer_sales_rep) {
  4414. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4415. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  4416. } else {
  4417. customer_sales_rep = @"";
  4418. }
  4419. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  4420. if (customer_state) {
  4421. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4422. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  4423. } else {
  4424. customer_state = @"";
  4425. }
  4426. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  4427. if (customer_name) {
  4428. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4429. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  4430. } else {
  4431. customer_name = @"";
  4432. }
  4433. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  4434. if (customer_country) {
  4435. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4436. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  4437. } else {
  4438. customer_country = @"";
  4439. }
  4440. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  4441. if (customer_cid) {
  4442. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4443. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  4444. } else {
  4445. customer_cid = @"";
  4446. }
  4447. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  4448. if (customer_city) {
  4449. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4450. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  4451. } else {
  4452. customer_city = @"";
  4453. }
  4454. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  4455. if (customer_address) {
  4456. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4457. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  4458. } else {
  4459. customer_address = @"";
  4460. }
  4461. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  4462. if (customer_email) {
  4463. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4464. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  4465. } else {
  4466. customer_email = @"";
  4467. }
  4468. NSString *price_name = [params valueForKey:@"price_name"];
  4469. if (price_name) {
  4470. if ([price_name containsString:@","]) {
  4471. // 首先从 price表中查处name
  4472. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  4473. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  4474. for (int i = 1;i < pArray.count;i++) {
  4475. NSString *p = pArray[i];
  4476. [mutablePStr appendFormat:@" or type = %@ ",p];
  4477. }
  4478. [mutablePStr appendString:@";"];
  4479. __block NSMutableArray *price_name_array = [NSMutableArray array];
  4480. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4481. char *name = (char *)sqlite3_column_text(stmt, 0);
  4482. if (!name)
  4483. name = "";
  4484. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  4485. }];
  4486. // 再根据name 拼sql
  4487. NSMutableString *mutable_price_name = [NSMutableString string];
  4488. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  4489. for (int i = 1; i < price_name_array.count; i++) {
  4490. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  4491. }
  4492. [mutable_price_name appendString:@")"];
  4493. price_name = mutable_price_name;
  4494. } else {
  4495. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  4496. if ([price_name isEqualToString:@""]) {
  4497. price_name = @"";
  4498. } else {
  4499. __block NSString *price;
  4500. price_name = [self translateSingleQuote:price_name];
  4501. [iSalesDB jk_query:[NSString stringWithFormat:@"SELECT name FROM price where type = %@ ;",price_name] db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4502. char *p = (char *)sqlite3_column_text(stmt, 0);
  4503. if (p == NULL) {
  4504. p = "";
  4505. }
  4506. price = [NSString stringWithUTF8String:p];
  4507. }];
  4508. if ([price isEqualToString:@""]) {
  4509. price_name = @"";
  4510. } else {
  4511. price = [self translateSingleQuote:price];
  4512. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  4513. }
  4514. }
  4515. }
  4516. } else {
  4517. price_name = @"";
  4518. }
  4519. int limit = [[params valueForKey:@"limit"] intValue];
  4520. int offset = [[params valueForKey:@"offset"] intValue];
  4521. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4522. NSString *where = [NSString stringWithFormat:@" %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ and is_active=1 ",contactType,contact_name,customer_phone,customer_fax,customer_zipcode,customer_sales_rep,customer_state,customer_name,customer_country,customer_cid,customer_city,customer_address,customer_email,price_name];
  4523. NSString *sql = [NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email from offline_contact where %@ order by decrypt(company_name) limit %d offset %d",where,limit,offset];
  4524. // int result= [iSalesDB AddExFunction:db];
  4525. int count =0;
  4526. NSString *sqlQuery = nil;
  4527. if(keyword.length==0)
  4528. {
  4529. // sqlQuery=[NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email from offline_contact where %@=1 order by decrypt(company_name) limit %d offset %d",contactType , limit, offset];
  4530. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  4531. sqlQuery = sql;
  4532. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  4533. }
  4534. else
  4535. {
  4536. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  4537. keyword = keyword.lowercaseString;
  4538. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4539. sqlQuery=[NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email from offline_contact where %@ and (lower(contact_id ) like'%%%@%%' or lower(decrypt(company_name)) like '%%%@%%' or lower(decrypt(phone)) like '%%%@%%' or lower(decrypt(addr)) like '%%%@%%') and is_active=1 order by decrypt(company_name) limit %d offset %d",contactType , keyword,keyword,keyword,keyword,limit, offset];
  4540. count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@=1 and (lower(contact_id ) like'%@' or lower(company_name) like'%@') and is_active=1",contactType,keyword,keyword]];
  4541. }
  4542. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  4543. sqlite3_stmt * statement;
  4544. [ret setValue:@"2" forKey:@"result"];
  4545. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  4546. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4547. {
  4548. int i = 0;
  4549. while (sqlite3_step(statement) == SQLITE_ROW)
  4550. {
  4551. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4552. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4553. int editable = sqlite3_column_int(statement, 0);
  4554. char *company_name = (char*)sqlite3_column_text(statement, 1);
  4555. NSString *nscompany_name =nil;
  4556. if(company_name==nil)
  4557. nscompany_name=@"";
  4558. else
  4559. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  4560. char *country = (char*)sqlite3_column_text(statement, 2);
  4561. if(country==nil)
  4562. country="";
  4563. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  4564. // char *addr = (char*)sqlite3_column_text(statement, 3);
  4565. // if(addr==nil)
  4566. // addr="";
  4567. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  4568. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  4569. if(zipcode==nil)
  4570. zipcode="";
  4571. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  4572. char *state = (char*)sqlite3_column_text(statement, 5);
  4573. if(state==nil)
  4574. state="";
  4575. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  4576. char *city = (char*)sqlite3_column_text(statement, 6);
  4577. if(city==nil)
  4578. city="";
  4579. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  4580. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  4581. // NSString *nscontact_name = nil;
  4582. // if(contact_name==nil)
  4583. // nscontact_name=@"";
  4584. // else
  4585. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  4586. char *phone = (char*)sqlite3_column_text(statement, 8);
  4587. NSString *nsphone = nil;
  4588. if(phone==nil)
  4589. nsphone=@"";
  4590. else
  4591. nsphone= [[NSString alloc]initWithUTF8String:phone];
  4592. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  4593. if(contact_id==nil)
  4594. contact_id="";
  4595. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4596. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  4597. if(addr_1==nil)
  4598. addr_1="";
  4599. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4600. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  4601. if(addr_2==nil)
  4602. addr_2="";
  4603. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4604. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  4605. if(addr_3==nil)
  4606. addr_3="";
  4607. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4608. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  4609. if(addr_4==nil)
  4610. addr_4="";
  4611. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4612. char *first_name = (char*)sqlite3_column_text(statement, 14);
  4613. if(first_name==nil)
  4614. first_name="";
  4615. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4616. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4617. if(last_name==nil)
  4618. last_name="";
  4619. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4620. char *fax = (char*)sqlite3_column_text(statement, 16);
  4621. NSString *nsfax = nil;
  4622. if(fax==nil)
  4623. nsfax=@"";
  4624. else
  4625. {
  4626. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4627. if(nsfax.length>0)
  4628. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  4629. }
  4630. char *email = (char*)sqlite3_column_text(statement, 17);
  4631. NSString *nsemail = nil;
  4632. if(email==nil)
  4633. nsemail=@"";
  4634. else
  4635. {
  4636. nsemail= [[NSString alloc]initWithUTF8String:email];
  4637. if(nsemail.length>0)
  4638. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  4639. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  4640. }
  4641. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4642. [arr_name addObject:nsfirst_name];
  4643. [arr_name addObject:nslast_name];
  4644. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  4645. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  4646. {
  4647. // decrypt
  4648. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4649. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4650. nsphone=[AESCrypt fastdecrypt:nsphone];
  4651. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4652. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4653. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4654. }
  4655. [arr_addr addObject:nscompany_name];
  4656. [arr_addr addObject:nscontact_name];
  4657. [arr_addr addObject:@"<br>"];
  4658. [arr_addr addObject:nsaddr_1];
  4659. [arr_addr addObject:nsaddr_2];
  4660. [arr_addr addObject:nsaddr_3];
  4661. [arr_addr addObject:nsaddr_4];
  4662. //[arr_addr addObject:nsaddr];
  4663. [arr_addr addObject:nszipcode];
  4664. [arr_addr addObject:nscity];
  4665. [arr_addr addObject:nsstate];
  4666. [arr_addr addObject:nscountry];
  4667. [arr_addr addObject:@"<br>"];
  4668. [arr_addr addObject:nsphone];
  4669. [arr_addr addObject:nsfax];
  4670. [arr_addr addObject:nsemail];
  4671. NSString * name = [RAUtils arr2string:arr_addr separator:@", " trim:true];
  4672. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  4673. [item setValue:name forKey:@"name"];
  4674. [item setValue:nscontact_id forKey:@"contact_id"];
  4675. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  4676. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4677. i++;
  4678. }
  4679. UIApplication * app = [UIApplication sharedApplication];
  4680. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4681. [ret setValue:appDelegate.mode forKey:@"mode"];
  4682. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  4683. sqlite3_finalize(statement);
  4684. }
  4685. [iSalesDB close_db:db];
  4686. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  4687. return ret;
  4688. }
  4689. #pragma mark contact Advanced search
  4690. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  4691. {
  4692. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  4693. return [RAUtils dict2data:contactAdvanceDic];
  4694. }
  4695. #pragma mark create new contact
  4696. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  4697. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  4698. NSData *data = [NSData dataWithContentsOfFile:path];
  4699. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4700. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  4701. NSString *countryCode = nil;
  4702. NSString *countryCode_id = nil;
  4703. NSString *stateCode = nil;
  4704. NSString *city = nil;
  4705. NSString *zipCode = nil;
  4706. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  4707. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  4708. NSString *code_id = params[@"country"];
  4709. countryCode_id = code_id;
  4710. countryCode = [self countryCodeByid:code_id];
  4711. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  4712. NSString *zip_code = params[@"zipcode"];
  4713. // 剔除全部为空格
  4714. int spaceCount = 0;
  4715. for (int i = 0; i < zip_code.length; i++) {
  4716. if ([zip_code characterAtIndex:i] == ' ') {
  4717. spaceCount++;
  4718. }
  4719. }
  4720. if (spaceCount == zip_code.length) {
  4721. zip_code = @"";
  4722. }
  4723. zipCode = zip_code;
  4724. if (zipCode.length > 0) {
  4725. countryCode_id = params[@"country"];
  4726. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  4727. countryCode = [dic valueForKey:@"country_code"];
  4728. if (!countryCode) {
  4729. // countryCode = @"US";
  4730. NSString *code_id = params[@"country"];
  4731. countryCode = [self countryCodeByid:code_id];
  4732. }
  4733. stateCode = [dic valueForKey:@"state_code"];
  4734. if (!stateCode.length) {
  4735. stateCode = params[@"state"];
  4736. }
  4737. city = [dic valueForKey:@"city"];
  4738. if (!city.length) {
  4739. city = params[@"city"];
  4740. }
  4741. // zip code
  4742. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  4743. [zipDic setValue:zipCode forKey:@"value"];
  4744. [section_0 setValue:zipDic forKey:@"item_11"];
  4745. } else {
  4746. NSString *code_id = params[@"country"];
  4747. countryCode = [self countryCodeByid:code_id];
  4748. stateCode = params[@"state"];
  4749. city = params[@"city"];
  4750. }
  4751. }
  4752. } else {
  4753. // default: US United States
  4754. countryCode = @"US";
  4755. countryCode_id = @"228";
  4756. }
  4757. // country
  4758. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  4759. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  4760. // state
  4761. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  4762. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  4763. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  4764. // city
  4765. if (city) {
  4766. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  4767. [cityDic setValue:city forKey:@"value"];
  4768. [section_0 setValue:cityDic forKey:@"item_13"];
  4769. }
  4770. // price type
  4771. NSDictionary *priceDic = [self offline_getPrice];
  4772. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  4773. // contact type
  4774. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  4775. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  4776. // Sales Rep
  4777. NSDictionary *repDic = [self offline_getSalesRep];
  4778. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  4779. [ret setValue:section_0 forKey:@"section_0"];
  4780. return [RAUtils dict2data:ret];
  4781. }
  4782. #pragma mark save
  4783. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  4784. NSString *addr = nil;
  4785. NSString *contact_name = nil;
  4786. NSString *companyName = [params objectForKey:@"company"];
  4787. if (companyName) {
  4788. companyName = [AESCrypt fastencrypt:companyName];
  4789. } else {
  4790. companyName = @"";
  4791. }
  4792. DebugLog(@"company");
  4793. companyName = [self translateSingleQuote:companyName];
  4794. NSString *addr1 = [params objectForKey:@"address"];
  4795. NSString *addr2 = [params objectForKey:@"address2"];
  4796. NSString *addr3 = [params objectForKey:@"address_3"];
  4797. NSString *addr4 = [params objectForKey:@"address_4"];
  4798. if (!addr2) {
  4799. addr2 = @"";
  4800. }
  4801. if (!addr3) {
  4802. addr3 = @"";
  4803. }
  4804. if (!addr4) {
  4805. addr4 = @"";
  4806. }
  4807. if (!addr1) {
  4808. addr1 = @"";
  4809. }
  4810. DebugLog(@"addr");
  4811. addr = [RAUtils arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  4812. addr = [AESCrypt fastencrypt:addr];
  4813. addr = [self translateSingleQuote:addr];
  4814. if (addr1 && ![addr1 isEqualToString:@""]) {
  4815. addr1 = [AESCrypt fastencrypt:addr1];
  4816. }
  4817. addr1 = [self translateSingleQuote:addr1];
  4818. addr2 = [self translateSingleQuote:addr2];
  4819. addr3 = [self translateSingleQuote:addr3];
  4820. addr4 = [self translateSingleQuote:addr4];
  4821. NSString *country = [params objectForKey:@"country"];
  4822. if (country) {
  4823. country = [self countryNameByCountryCodeId:country];
  4824. } else {
  4825. country = @"";
  4826. }
  4827. DebugLog(@"country");
  4828. country = [self translateSingleQuote:country];
  4829. NSString *state = [params objectForKey:@"state"];
  4830. if (!state) {
  4831. state = @"";
  4832. }
  4833. DebugLog(@"state");
  4834. state = [self translateSingleQuote:state];
  4835. NSString *city = [params objectForKey:@"city"];
  4836. if (!city) {
  4837. city = @"";
  4838. }
  4839. city = [self translateSingleQuote:city];
  4840. NSString *zipcode = [params objectForKey:@"zipcode"];
  4841. if (!zipcode) {
  4842. zipcode = @"";
  4843. }
  4844. DebugLog(@"zip");
  4845. zipcode = [self translateSingleQuote:zipcode];
  4846. NSString *fistName = [params objectForKey:@"firstname"];
  4847. if (!fistName) {
  4848. fistName = @"";
  4849. }
  4850. NSString *lastName = [params objectForKey:@"lastname"];
  4851. if (!lastName) {
  4852. lastName = @"";
  4853. }
  4854. contact_name = [RAUtils arr2string:@[fistName,lastName] separator:@" " trim:true];
  4855. DebugLog(@"contact_name");
  4856. contact_name = [self translateSingleQuote:contact_name];
  4857. fistName = [self translateSingleQuote:fistName];
  4858. lastName = [self translateSingleQuote:lastName];
  4859. NSString *phone = [params objectForKey:@"phone"];
  4860. if (phone) {
  4861. phone = [AESCrypt fastencrypt:phone];
  4862. } else {
  4863. phone = @"";
  4864. }
  4865. DebugLog(@"PHONE");
  4866. phone = [self translateSingleQuote:phone];
  4867. NSString *fax = [params objectForKey:@"fax"];
  4868. if (!fax) {
  4869. fax = @"";
  4870. }
  4871. DebugLog(@"FAX");
  4872. fax = [self translateSingleQuote:fax];
  4873. NSString *email = [params objectForKey:@"email"];
  4874. if (!email) {
  4875. email = @"";
  4876. }
  4877. DebugLog(@"EMAIL:%@",email);
  4878. email = [self translateSingleQuote:email];
  4879. NSString *notes = [params objectForKey:@"contact_notes"];
  4880. if (!notes) {
  4881. notes = @"";
  4882. }
  4883. DebugLog(@"NOTE:%@",notes);
  4884. notes = [self translateSingleQuote:notes];
  4885. NSString *price = [params objectForKey:@"price_name"];
  4886. if (price) {
  4887. price = [self priceNameByPriceId:price];
  4888. } else {
  4889. price = @"";
  4890. }
  4891. DebugLog(@"PRICE");
  4892. price = [self translateSingleQuote:price];
  4893. NSString *salesRep = [params objectForKey:@"sales_rep"];
  4894. if (salesRep) {
  4895. salesRep = [self salesRepCodeById:salesRep];
  4896. } else {
  4897. salesRep = @"";
  4898. }
  4899. salesRep = [self translateSingleQuote:salesRep];
  4900. NSString *img = [params objectForKey:@"business_card"];
  4901. NSArray *array = [img componentsSeparatedByString:@","];
  4902. NSString *img_0 = array[0];
  4903. if (!img_0) {
  4904. img_0 = @"";
  4905. }
  4906. img_0 = [self translateSingleQuote:img_0];
  4907. NSString *img_1 = array[1];
  4908. if (!img_1) {
  4909. img_1 = @"";
  4910. }
  4911. img_1 = [self translateSingleQuote:img_1];
  4912. NSString *img_2 = array[2];
  4913. if (!img_2) {
  4914. img_2 = @"";
  4915. }
  4916. img_2 = [self translateSingleQuote:img_2];
  4917. NSString *contact_id = [NSUUID UUID].UUIDString;
  4918. NSString *contact_type = [params objectForKey:@"type_name"];
  4919. if (!contact_type) {
  4920. contact_type = @"";
  4921. }
  4922. contact_type = [self translateSingleQuote:contact_type];
  4923. // 判断更新时是否为customer
  4924. if (update) {
  4925. contact_id = [params objectForKey:@"contact_id"];
  4926. if (!contact_id) {
  4927. contact_id = @"";
  4928. }
  4929. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  4930. __block int customer = 0;
  4931. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4932. customer = sqlite3_column_int(stmt, 0);
  4933. }];
  4934. isCustomer = customer ? YES : NO;
  4935. }
  4936. NSMutableDictionary *sync_dic = [params mutableCopy];
  4937. if (isCustomer) {
  4938. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  4939. } else {
  4940. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  4941. }
  4942. NSString *sync_data = nil;
  4943. NSString *sql = nil;
  4944. if (update){
  4945. contact_id = [params objectForKey:@"contact_id"];
  4946. if (!contact_id) {
  4947. contact_id = @"";
  4948. }
  4949. [sync_dic setValue:contact_id forKey:@"contact_id"];
  4950. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  4951. sync_data = [RAUtils dict2string:sync_dic];
  4952. sync_data = [self translateSingleQuote:sync_data];
  4953. sql = [NSString stringWithFormat:@"update offline_contact set company_name = '%@',addr_1 = '%@',addr_2 = '%@',addr_3 = '%@',addr_4 = '%@',country = '%@',state = '%@',city = '%@',zipcode = '%@',first_name = '%@',last_name = '%@',phone = '%@',fax = '%@',email = '%@',notes = '%@',price_type = '%@',sales_rep = '%@',img_0 = '%@',img_1 = '%@',img_2 = '%@',sync_data = '%@',contact_name = '%@',addr = '%@',type = '%@' where contact_id = '%@';",companyName,addr1,addr2,addr3,addr4,country,state,city,zipcode,fistName,lastName,phone,fax,email,notes,price,salesRep,img_0,img_1,img_2,sync_data,contact_name,addr,contact_type,contact_id];
  4954. } else {
  4955. contact_id = [self translateSingleQuote:contact_id];
  4956. [sync_dic setValue:contact_id forKey:@"contact_id"];
  4957. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  4958. sync_data = [RAUtils dict2string:sync_dic];
  4959. sync_data = [self translateSingleQuote:sync_data];
  4960. sql = [NSString stringWithFormat:@"insert into offline_contact (is_active,company_name,addr_1,addr_2,addr_3,addr_4,country,state,city,zipcode,first_name,last_name,phone,fax,email,notes,price_type,sales_rep,img_0,img_1,img_2,editable,contact_id,Sales_Order_Customer,Sales_Order_Freight_Bill_To,Sales_Order_Ship_From,Sales_Order_Merchandise_Bill_To,Contact_Return_To,Sales_Order_Ship_To,sync_data,contact_name,addr,type) values (1,'%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@',1,'%@',%d,%d,0,%d,0,1,'%@','%@','%@','%@')",companyName,addr1,addr2,addr3,addr4,country,state,city,zipcode,fistName,lastName,phone,fax,email,notes,price,salesRep,img_0,img_1,img_2,contact_id,isCustomer ? 1 : 0,isCustomer ? 1 : 0,isCustomer ? 1 : 0,sync_data,contact_name,addr,contact_type];
  4961. }
  4962. int result = [iSalesDB execSql:sql];
  4963. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  4964. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  4965. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  4966. }
  4967. #pragma mark save new contact
  4968. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  4969. {
  4970. return [self offline_saveContact:params update:NO isCustomer:YES];
  4971. }
  4972. #pragma mark edit contact
  4973. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  4974. {
  4975. // {
  4976. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  4977. // password = 123456;
  4978. // user = EvanK;
  4979. // }
  4980. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  4981. NSData *data = [NSData dataWithContentsOfFile:path];
  4982. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4983. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  4984. NSString *countryCode = nil;
  4985. NSString *countryCode_id = nil;
  4986. NSString *stateCode = nil;
  4987. /*------contact infor------*/
  4988. __block NSString *country = nil;
  4989. __block NSString *company_name = nil;
  4990. __block NSString *contact_id = params[@"contact_id"];
  4991. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  4992. __block NSString *zipcode = nil;
  4993. __block NSString *state = nil; // state_code
  4994. __block NSString *city = nil; //
  4995. __block NSString *firt_name,*last_name;
  4996. __block NSString *phone,*fax,*email;
  4997. __block NSString *notes,*price_type,*sales_rep;
  4998. __block NSString *img_0,*img_1,*img_2;
  4999. __block NSString *contact_type;
  5000. contact_id = [self translateSingleQuote:contact_id];
  5001. NSString *sql = [NSString stringWithFormat:@"select country,company_name,addr_1,addr_2,addr_3,addr_4,zipcode,state,city,first_name,last_name,phone,fax,email,notes,price_type,sales_rep,img_0,img_1,img_2,type from offline_contact where contact_id = '%@';",contact_id];
  5002. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5003. country = [self textAtColumn:0 statement:stmt]; // country name
  5004. company_name = [self textAtColumn:1 statement:stmt];
  5005. addr_1 = [self textAtColumn:2 statement:stmt];
  5006. addr_2 = [self textAtColumn:3 statement:stmt];
  5007. addr_3 = [self textAtColumn:4 statement:stmt];
  5008. addr_4 = [self textAtColumn:5 statement:stmt];
  5009. zipcode = [self textAtColumn:6 statement:stmt];
  5010. state = [self textAtColumn:7 statement:stmt]; // state code
  5011. city = [self textAtColumn:8 statement:stmt];
  5012. firt_name = [self textAtColumn:9 statement:stmt];
  5013. last_name = [self textAtColumn:10 statement:stmt];
  5014. phone = [self textAtColumn:11 statement:stmt];
  5015. fax = [self textAtColumn:12 statement:stmt];
  5016. email = [self textAtColumn:13 statement:stmt];
  5017. notes = [self textAtColumn:14 statement:stmt];
  5018. price_type = [self textAtColumn:15 statement:stmt]; // name
  5019. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  5020. img_0 = [self textAtColumn:17 statement:stmt];
  5021. img_1 = [self textAtColumn:18 statement:stmt];
  5022. img_2 = [self textAtColumn:19 statement:stmt];
  5023. contact_type = [self textAtColumn:20 statement:stmt];
  5024. }];
  5025. // decrypt
  5026. if (company_name) {
  5027. company_name = [AESCrypt fastdecrypt:company_name];
  5028. }
  5029. if (addr_1) {
  5030. addr_1 = [AESCrypt fastdecrypt:addr_1];
  5031. }
  5032. if (phone) {
  5033. phone = [AESCrypt fastdecrypt:phone];
  5034. }
  5035. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  5036. countryCode = [iSalesDB jk_queryText:countrySql];
  5037. stateCode = state;
  5038. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5039. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  5040. NSString *code_id = params[@"country"];
  5041. countryCode_id = code_id;
  5042. countryCode = [self countryCodeByid:code_id];
  5043. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  5044. NSString *zip_code = params[@"zipcode"];
  5045. // 剔除全部为空格
  5046. int spaceCount = 0;
  5047. for (int i = 0; i < zip_code.length; i++) {
  5048. if ([zip_code characterAtIndex:i] == ' ') {
  5049. spaceCount++;
  5050. }
  5051. }
  5052. if (spaceCount == zip_code.length) {
  5053. zip_code = @"";
  5054. }
  5055. if (zipcode.length > 0) {
  5056. zipcode = zip_code;
  5057. countryCode_id = params[@"country"];
  5058. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5059. countryCode = [dic valueForKey:@"country_code"];
  5060. if (!countryCode) {
  5061. NSString *code_id = params[@"country"];
  5062. countryCode = [self countryCodeByid:code_id];
  5063. }
  5064. stateCode = [dic valueForKey:@"state_code"];
  5065. if (!stateCode.length) {
  5066. stateCode = params[@"state"];
  5067. }
  5068. city = [dic valueForKey:@"city"];
  5069. if (!city.length) {
  5070. city = params[@"city"];
  5071. }
  5072. // zip code
  5073. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  5074. [zipDic setValue:zipcode forKey:@"value"];
  5075. [section_0 setValue:zipDic forKey:@"item_8"];
  5076. } else {
  5077. NSString *code_id = params[@"country"];
  5078. countryCode = [self countryCodeByid:code_id];
  5079. stateCode = params[@"state"];
  5080. city = params[@"city"];
  5081. }
  5082. }
  5083. }
  5084. // 0 Country
  5085. // 1 Company Name
  5086. // 2 Contact ID
  5087. // 3 Picture
  5088. // 4 Address 1
  5089. // 5 Address 2
  5090. // 6 Address 3
  5091. // 7 Address 4
  5092. // 8 Zip Code
  5093. // 9 State/Province
  5094. // 10 City
  5095. // 11 Contact First Name
  5096. // 12 Contact Last Name
  5097. // 13 Phone
  5098. // 14 Fax
  5099. // 15 Email
  5100. // 16 Contact Notes
  5101. // 17 Price Type
  5102. // 18 Contact Type
  5103. // 19 Sales Rep
  5104. // country
  5105. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5106. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  5107. // company
  5108. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  5109. // contact_id
  5110. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  5111. // picture
  5112. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  5113. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  5114. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  5115. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  5116. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  5117. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  5118. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  5119. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  5120. // addr 1 2 3 4
  5121. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  5122. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  5123. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  5124. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  5125. // zip code
  5126. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  5127. // state
  5128. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5129. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  5130. // city
  5131. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  5132. // first last
  5133. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  5134. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  5135. // phone fax email
  5136. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  5137. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  5138. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  5139. // notes
  5140. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  5141. // price
  5142. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  5143. for (NSString *key in priceDic.allKeys) {
  5144. if ([key containsString:@"val_"]) {
  5145. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  5146. if ([dic[@"value"] isEqualToString:price_type]) {
  5147. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5148. [priceDic setValue:dic forKey:key];
  5149. }
  5150. }
  5151. }
  5152. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  5153. // Contact Rep
  5154. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  5155. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5156. // Sales Rep
  5157. NSMutableDictionary *repDic = [[self offline_getSalesRep] mutableCopy];
  5158. for (NSString *key in repDic.allKeys) {
  5159. if ([key containsString:@"val_"]) {
  5160. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  5161. NSString *value = dic[@"value"];
  5162. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  5163. if (code && [code isEqualToString:sales_rep]) {
  5164. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5165. [repDic setValue:dic forKey:key];
  5166. }
  5167. }
  5168. }
  5169. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  5170. [ret setValue:section_0 forKey:@"section_0"];
  5171. return [RAUtils dict2data:ret];
  5172. }
  5173. #pragma mark save contact
  5174. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  5175. {
  5176. return [self offline_saveContact:params update:YES isCustomer:YES];
  5177. }
  5178. #pragma mark category
  5179. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5180. if (ck) {
  5181. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  5182. for (NSString *key in res.allKeys) {
  5183. if (![key isEqualToString:@"count"]) {
  5184. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  5185. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5186. if ([val[@"value"] isEqualToString:ck]) {
  5187. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5188. }
  5189. [res setValue:val forKey:key];
  5190. }
  5191. }
  5192. [dic setValue:res forKey:valueKey];
  5193. }
  5194. }
  5195. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  5196. // NSString* orderCode = [params valueForKey:@"orderCode"];
  5197. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5198. NSString* category = [params valueForKey:@"category"];
  5199. if (!category || [category isEqualToString:@""]) {
  5200. category = @"%";
  5201. }
  5202. category = [self translateSingleQuote:category];
  5203. int limit = [[params valueForKey:@"limit"] intValue];
  5204. int offset = [[params valueForKey:@"offset"] intValue];
  5205. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5206. NSString *limit_str = @"";
  5207. if (limited) {
  5208. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  5209. }
  5210. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5211. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5212. sqlite3 *db = [iSalesDB get_db];
  5213. // [iSalesDB AddExFunction:db];
  5214. int count;
  5215. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  5216. NSString *sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where m.category like'%%#%@#%%' and m.is_active = 1 order by m.name %@ ;",appDelegate.order_code, category,limit_str];
  5217. double price_min = 0;
  5218. double price_max = 0;
  5219. if ([params.allKeys containsObject:@"alert"]) {
  5220. // alert
  5221. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5222. NSString *alert = params[@"alert"];
  5223. if ([alert isEqualToString:@"Display All"]) {
  5224. alert = [NSString stringWithFormat:@""];
  5225. } else {
  5226. alert = [self translateSingleQuote:alert];
  5227. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5228. }
  5229. // available
  5230. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5231. NSString *available = params[@"available"];
  5232. NSString *available_condition;
  5233. if ([available isEqualToString:@"Display All"]) {
  5234. available_condition = @"";
  5235. } else if ([available isEqualToString:@"Available Now"]) {
  5236. available_condition = @"and availability > 0";
  5237. } else {
  5238. available_condition = @"and availability == 0";
  5239. }
  5240. // best seller
  5241. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5242. NSString *best_seller = @"";
  5243. NSString *order_best_seller = @"m.name asc";
  5244. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5245. best_seller = @"and best_seller > 0";
  5246. order_best_seller = @"m.best_seller desc,m.name asc";
  5247. }
  5248. // price
  5249. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5250. NSString *price = params[@"price"];
  5251. price_min = 0;
  5252. price_max = MAXFLOAT;
  5253. if (appDelegate.user) {
  5254. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  5255. NSMutableString *priceName = [NSMutableString string];
  5256. for (int i = 0; i < priceTypeArray.count; i++) {
  5257. NSString *pricetype = priceTypeArray[i];
  5258. pricetype = [self translateSingleQuote:pricetype];
  5259. if (i == 0) {
  5260. [priceName appendFormat:@"'%@'",pricetype];
  5261. } else {
  5262. [priceName appendFormat:@",'%@'",pricetype];
  5263. }
  5264. }
  5265. if ([price isEqualToString:@"Display All"]) {
  5266. price = [NSString stringWithFormat:@""];
  5267. } else if([price containsString:@"+"]){
  5268. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5269. price_min = [price doubleValue];
  5270. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  5271. } else {
  5272. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5273. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5274. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5275. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  5276. }
  5277. } else {
  5278. price = @"";
  5279. }
  5280. // sold_by_qty : Sold in quantities of %@
  5281. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5282. NSString *qty = params[@"sold_by_qty"];
  5283. if ([qty isEqualToString:@"Display All"]) {
  5284. qty = @"";
  5285. } else {
  5286. qty = [self translateSingleQuote:qty];
  5287. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5288. }
  5289. // cate
  5290. category = [self translateSingleQuote:category];
  5291. NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  5292. // where bestseller > 0 order by bestseller desc
  5293. // sql query: alert availability(int) best_seller(int) price qty
  5294. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from (select name,description,product_id,closeout,best_seller from product where %@ %@ %@ %@ %@ %@ and is_active = 1) m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id order by %@ %@;",cateWhere,best_seller,alert,available_condition,qty,price,appDelegate.order_code,order_best_seller,limit_str];
  5295. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  5296. }
  5297. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  5298. if (!appDelegate.user) {
  5299. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  5300. }
  5301. [ret setValue:filter forKey:@"filter"];
  5302. DebugLog(@"offline_category sql:%@",sqlQuery);
  5303. sqlite3_stmt * statement;
  5304. [ret setValue:@"2" forKey:@"result"];
  5305. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  5306. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  5307. // int count=0;
  5308. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5309. {
  5310. int i=0;
  5311. while (sqlite3_step(statement) == SQLITE_ROW)
  5312. {
  5313. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5314. char *name = (char*)sqlite3_column_text(statement, 0);
  5315. if(name==nil)
  5316. name="";
  5317. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5318. char *description = (char*)sqlite3_column_text(statement, 1);
  5319. if(description==nil)
  5320. description="";
  5321. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  5322. int product_id = sqlite3_column_int(statement, 2);
  5323. int wid = sqlite3_column_int(statement, 3);
  5324. int closeout = sqlite3_column_int(statement, 4);
  5325. int cid = sqlite3_column_int(statement, 5);
  5326. int wisdelete = sqlite3_column_int(statement, 6);
  5327. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  5328. if(wid !=0 && wisdelete != 1)
  5329. [item setValue:@"true" forKey:@"wish_exists"];
  5330. else
  5331. [item setValue:@"false" forKey:@"wish_exists"];
  5332. if(closeout==0)
  5333. [item setValue:@"false" forKey:@"is_closeout"];
  5334. else
  5335. [item setValue:@"true" forKey:@"is_closeout"];
  5336. if(cid==0)
  5337. [item setValue:@"false" forKey:@"cart_exists"];
  5338. else
  5339. [item setValue:@"true" forKey:@"cart_exists"];
  5340. [item addEntriesFromDictionary:imgjson];
  5341. // [item setValue:nsurl forKey:@"img"];
  5342. [item setValue:nsname forKey:@"name"];
  5343. [item setValue:nsdescription forKey:@"description"];
  5344. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5345. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5346. i++;
  5347. }
  5348. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5349. [ret setObject:items forKey:@"items"];
  5350. sqlite3_finalize(statement);
  5351. } else {
  5352. DebugLog(@"nothing...");
  5353. }
  5354. DebugLog(@"count:%d",count);
  5355. [iSalesDB close_db:db];
  5356. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  5357. return ret;
  5358. }
  5359. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  5360. {
  5361. return [self categoryList:params limited:YES];
  5362. }
  5363. # pragma mark item search
  5364. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  5365. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5366. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  5367. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5368. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5369. // category
  5370. NSDictionary *category_menu = [self offline_category_menu];
  5371. [filter setValue:category_menu forKey:@"category"];
  5372. NSString* where= nil;
  5373. NSString* orderby= @"m.name";
  5374. if (!filterSearch) {
  5375. int covertype = [[params valueForKey:@"covertype"] intValue];
  5376. switch (covertype) {
  5377. case 0:
  5378. {
  5379. where=@"m.category like'%%#005#%%'";
  5380. break;
  5381. }
  5382. case 1:
  5383. {
  5384. where=@"m.alert like '%QS%'";
  5385. break;
  5386. }
  5387. case 2:
  5388. {
  5389. where=@"m.availability>0";
  5390. break;
  5391. }
  5392. case 3:
  5393. {
  5394. where=@"m.best_seller>0";
  5395. orderby=@"m.best_seller desc,m.name asc";
  5396. break;
  5397. }
  5398. default:
  5399. where=@"1=1";
  5400. break;
  5401. }
  5402. }
  5403. int limit = [[params valueForKey:@"limit"] intValue];
  5404. int offset = [[params valueForKey:@"offset"] intValue];
  5405. NSString *limit_str = @"";
  5406. if (limited) {
  5407. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  5408. }
  5409. sqlite3 *db = [iSalesDB get_db];
  5410. // [iSalesDB AddExFunction:db];
  5411. int count;
  5412. NSString *sqlQuery = nil;
  5413. where = [where stringByAppendingString:@" and m.is_active = 1"];
  5414. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where %@ order by %@ %@;",appDelegate.order_code, where,orderby,limit_str];
  5415. double price_min = 0;
  5416. double price_max = 0;
  5417. if (filterSearch) {
  5418. // alert
  5419. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5420. NSString *alert = params[@"alert"];
  5421. if ([alert isEqualToString:@"Display All"]) {
  5422. alert = [NSString stringWithFormat:@""];
  5423. } else {
  5424. alert = [self translateSingleQuote:alert];
  5425. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5426. }
  5427. // available
  5428. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5429. NSString *available = params[@"available"];
  5430. NSString *available_condition;
  5431. if ([available isEqualToString:@"Display All"]) {
  5432. available_condition = @"";
  5433. } else if ([available isEqualToString:@"Available Now"]) {
  5434. available_condition = @"and availability > 0";
  5435. } else {
  5436. available_condition = @"and availability == 0";
  5437. }
  5438. // best seller
  5439. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5440. NSString *best_seller = @"";
  5441. NSString *order_best_seller = @"m.name asc";
  5442. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5443. best_seller = @"and best_seller > 0";
  5444. order_best_seller = @"m.best_seller desc,m.name asc";
  5445. }
  5446. // price
  5447. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5448. NSString *price = params[@"price"];
  5449. price_min = 0;
  5450. price_max = MAXFLOAT;
  5451. if (appDelegate.user) {
  5452. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  5453. NSMutableString *priceName = [NSMutableString string];
  5454. for (int i = 0; i < priceTypeArray.count; i++) {
  5455. NSString *pricetype = priceTypeArray[i];
  5456. pricetype = [self translateSingleQuote:pricetype];
  5457. if (i == 0) {
  5458. [priceName appendFormat:@"'%@'",pricetype];
  5459. } else {
  5460. [priceName appendFormat:@",'%@'",pricetype];
  5461. }
  5462. }
  5463. if ([price isEqualToString:@"Display All"]) {
  5464. price = [NSString stringWithFormat:@""];
  5465. } else if([price containsString:@"+"]){
  5466. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5467. price_min = [price doubleValue];
  5468. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  5469. } else {
  5470. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5471. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5472. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5473. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  5474. }
  5475. } else {
  5476. price = @"";
  5477. }
  5478. // sold_by_qty : Sold in quantities of %@
  5479. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5480. NSString *qty = params[@"sold_by_qty"];
  5481. if ([qty isEqualToString:@"Display All"]) {
  5482. qty = @"";
  5483. } else {
  5484. qty = [self translateSingleQuote:qty];
  5485. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5486. }
  5487. // category
  5488. NSString *category_id = params[@"ctgId"];
  5489. NSMutableArray *cate_id_array = nil;
  5490. NSMutableString *cateWhere = [NSMutableString string];
  5491. if ([category_id isEqualToString:@""] || !category_id) {
  5492. [cateWhere appendString:@"1 = 1"];
  5493. } else {
  5494. if ([category_id containsString:@","]) {
  5495. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  5496. } else {
  5497. cate_id_array = [@[category_id] mutableCopy];
  5498. }
  5499. /*-----------*/
  5500. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];
  5501. for (int i = 0; i < cate_id_array.count; i++) {
  5502. for (NSString *key0 in cateDic.allKeys) {
  5503. if ([key0 containsString:@"category_"]) {
  5504. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy];
  5505. for (NSString *key1 in category0.allKeys) {
  5506. if ([key1 containsString:@"category_"]) {
  5507. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy];
  5508. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5509. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  5510. cate_id_array[i] = [category1 objectForKey:@"id"];
  5511. if (i == 0) {
  5512. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5513. } else {
  5514. [cateWhere appendFormat:@"or category like'%%#%@#%%'",cate_id_array[i]];
  5515. }
  5516. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5517. [category0 setValue:category1 forKey:key1];
  5518. [cateDic setValue:category0 forKey:key0];
  5519. }
  5520. }
  5521. }
  5522. }
  5523. }
  5524. }
  5525. [filter setValue:cateDic forKey:@"category"];
  5526. }
  5527. // where bestseller > 0 order by bestseller desc
  5528. // sql query: alert availability(int) best_seller(int) price qty
  5529. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from (select name,description,product_id,closeout,best_seller from product where %@ %@ %@ %@ %@ %@ and is_active = 1) m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id order by %@ %@;", cateWhere,best_seller,alert,available_condition,qty,price,appDelegate.order_code,order_best_seller,limit_str];
  5530. // count
  5531. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  5532. }
  5533. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  5534. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  5535. if (!appDelegate.user) {
  5536. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  5537. }
  5538. [ret setValue:filter forKey:@"filter"];
  5539. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  5540. sqlite3_stmt * statement;
  5541. [ret setValue:@"2" forKey:@"result"];
  5542. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  5543. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  5544. // int count=0;
  5545. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5546. {
  5547. int i=0;
  5548. while (sqlite3_step(statement) == SQLITE_ROW)
  5549. {
  5550. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5551. char *name = (char*)sqlite3_column_text(statement, 0);
  5552. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5553. char *description = (char*)sqlite3_column_text(statement, 1);
  5554. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  5555. int product_id = sqlite3_column_int(statement, 2);
  5556. int wid = sqlite3_column_int(statement, 3);
  5557. int closeout = sqlite3_column_int(statement, 4);
  5558. int cid = sqlite3_column_int(statement, 5);
  5559. int wisdelete = sqlite3_column_int(statement, 6);
  5560. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  5561. if(wid !=0 && wisdelete != 1)
  5562. [item setValue:@"true" forKey:@"wish_exists"];
  5563. else
  5564. [item setValue:@"false" forKey:@"wish_exists"];
  5565. if(closeout==0)
  5566. [item setValue:@"false" forKey:@"is_closeout"];
  5567. else
  5568. [item setValue:@"true" forKey:@"is_closeout"];
  5569. if(cid==0)
  5570. [item setValue:@"false" forKey:@"cart_exists"];
  5571. else
  5572. [item setValue:@"true" forKey:@"cart_exists"];
  5573. [item addEntriesFromDictionary:imgjson];
  5574. // [item setValue:nsurl forKey:@"img"];
  5575. [item setValue:nsname forKey:@"fash_name"];
  5576. [item setValue:nsdescription forKey:@"description"];
  5577. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5578. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5579. i++;
  5580. }
  5581. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5582. [ret setObject:items forKey:@"items"];
  5583. sqlite3_finalize(statement);
  5584. }
  5585. [iSalesDB close_db:db];
  5586. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  5587. return ret;
  5588. }
  5589. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  5590. {
  5591. return [self itemsearch:params limited:YES];
  5592. }
  5593. #pragma mark order detail
  5594. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  5595. if (str1.length == 0) {
  5596. str1 = @"&nbsp";
  5597. }
  5598. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  5599. return str;
  5600. }
  5601. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  5602. {
  5603. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  5604. [fromformatter setDateFormat:from];
  5605. NSDate *date = [fromformatter dateFromString:datetime];
  5606. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  5607. [toformatter setDateFormat:to];
  5608. NSString * ret = [toformatter stringFromDate:date];
  5609. return ret;
  5610. }
  5611. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  5612. // 把毫秒去掉
  5613. if ([dateTime containsString:@"."]) {
  5614. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  5615. }
  5616. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  5617. formatter.dateFormat = formate;
  5618. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  5619. NSDate *date = [formatter dateFromString:dateTime];
  5620. formatter.dateFormat = newFormate;
  5621. NSString *result = [formatter stringFromDate:date];
  5622. return result ? result : @"";
  5623. }
  5624. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  5625. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  5626. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  5627. }
  5628. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  5629. {
  5630. DebugLog(@"offline oderdetail params: %@",params);
  5631. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5632. int orderId = [params[@"orderId"] intValue];
  5633. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  5634. // decrypt card number and card security code
  5635. // NSString* sql=[NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,o.TotalCuft,o.TotalWeight,o.TotalCarton,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),o.credit_card_expiration,o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus from (select _id,so_id,poNumber,create_time,status,TotalCuft,TotalWeight,TotalCarton,customer_cid,customer_contact,logist,shipping,lift_gate_value,general_notes,internal_notes,paymentType,lift_gate,receive_name,receive_contact,receive_ext,sender_name,sender_contact,sender_ext,shipping_billto_name,shipping_billto_contact,shipping_billto_ext,billing_name,billing_contact,billing_ext,returnto_name,returnto_contact,returnto_ext,handling_fee_value,paymentsAndCredits,total_price,customer_cid,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,credit_card_number,credit_card_security_code,credit_card_expiration,credit_card_city,credit_card_state,ifnull(shipping,'ship_null') as shipNull,erpOrderStatus from offline_order where _id=%d) o left join offline_contact c on o.customer_cid=c.contact_id",orderId ];
  5636. NSString *sql = [NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),decrypt(o.credit_card_expiration_month),o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus,decrypt(o.credit_card_expiration_year),o.sign_picpath,o.customer_contact,o.customer_email,o.customer_phone,o.customer_fax,o.editable,o.lift_null,o.handling_null,o.ship_via from (select _id,so_id,poNumber,create_time,status,customer_cid,customer_contact,logist,shipping,lift_gate_value,general_notes,internal_notes,paymentType,lift_gate,receive_name,receive_contact,receive_ext,sender_name,sender_contact,sender_ext,shipping_billto_name,shipping_billto_contact,shipping_billto_ext,billing_name,billing_contact,billing_ext,returnto_name,returnto_contact,returnto_ext,handling_fee_value,paymentsAndCredits,total_price,customer_cid,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,credit_card_number,credit_card_security_code,credit_card_expiration_month,credit_card_city,credit_card_state,ifnull(shipping,'ship_null') as shipNull,erpOrderStatus,credit_card_expiration_year,sign_picpath,customer_contact,customer_email,customer_phone,customer_fax,order_id is null as editable,lift_gate_value is null as lift_null,handling_fee_value is null as handling_null,ship_via from offline_order where _id=%d) o left join offline_contact c on o.customer_cid=c.contact_id",orderId];
  5637. sqlite3 *db = [iSalesDB get_db];
  5638. sqlite3_stmt * statement;
  5639. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  5640. NSString *nssoid = @"";
  5641. NSString* orderinfo = @"";
  5642. NSString *moreInfo = @"";
  5643. double handlingFee = 0;
  5644. double payments_and_credist = 0;
  5645. double totalPrice = 0;
  5646. double shippingFee = 0;
  5647. double lift_gate = 0;
  5648. NSString *customer_contact = @"";
  5649. NSString *customer_email = @"";
  5650. NSString *customer_fax = @"";
  5651. NSString *customer_phone = @"";
  5652. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  5653. {
  5654. if (sqlite3_step(statement) == SQLITE_ROW)
  5655. {
  5656. int order_id = sqlite3_column_int(statement, 0);
  5657. NSString *sign_url = [self textAtColumn:51 statement:statement];
  5658. ret[@"sign_url"] = sign_url;
  5659. customer_contact = [self textAtColumn:52 statement:statement];
  5660. customer_email = [self textAtColumn:53 statement:statement];
  5661. customer_phone = [self textAtColumn:54 statement:statement];
  5662. customer_fax = [self textAtColumn:55 statement:statement];
  5663. int offline_edit=sqlite3_column_int(statement, 56);
  5664. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  5665. char *soid = (char*)sqlite3_column_text(statement, 1);
  5666. if(soid==nil)
  5667. soid= "";
  5668. nssoid= [[NSString alloc]initWithUTF8String:soid];
  5669. // so#
  5670. ret[@"so#"] = nssoid;
  5671. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  5672. if(poNumber==nil)
  5673. poNumber= "";
  5674. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  5675. char *create_time = (char*)sqlite3_column_text(statement, 3);
  5676. if(create_time==nil)
  5677. create_time= "";
  5678. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  5679. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  5680. int status = sqlite3_column_int(statement, 4);
  5681. int erpStatus = sqlite3_column_int(statement, 49);
  5682. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  5683. // status
  5684. if (status > 1 && status != 3) {
  5685. status = erpStatus;
  5686. } else if (status == 3) {
  5687. status = 15;
  5688. }
  5689. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  5690. ret[@"order_status"] = nsstatus;
  5691. char *company_name = (char*)sqlite3_column_text(statement, 5);
  5692. if(company_name==nil)
  5693. company_name= "";
  5694. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  5695. // company name
  5696. ret[@"company_name"] = nscompany_name;
  5697. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  5698. if(customer_contact==nil)
  5699. customer_contact= "";
  5700. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  5701. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  5702. if(addr_1==nil)
  5703. addr_1="";
  5704. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  5705. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  5706. if(addr_2==nil)
  5707. addr_2="";
  5708. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  5709. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  5710. if(addr_3==nil)
  5711. addr_3="";
  5712. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  5713. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  5714. if(addr_4==nil)
  5715. addr_4="";
  5716. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  5717. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  5718. [arr_addr addObject:nsaddr_1];
  5719. [arr_addr addObject:nsaddr_2];
  5720. [arr_addr addObject:nsaddr_3];
  5721. [arr_addr addObject:nsaddr_4];
  5722. NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  5723. char *logist = (char*)sqlite3_column_text(statement, 11);
  5724. if(logist==nil)
  5725. logist= "";
  5726. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  5727. if (status == -11 || status == 10 || status == 11) {
  5728. nslogist = [self textAtColumn:59 statement:statement];
  5729. };
  5730. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  5731. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  5732. shipping = @"Shipping To Be Quoted";
  5733. } else {
  5734. shippingFee = sqlite3_column_double(statement, 12);
  5735. }
  5736. // Shipping
  5737. ret[@"Shipping"] = shipping;
  5738. int have_lift_gate = sqlite3_column_int(statement, 17);
  5739. lift_gate = sqlite3_column_double(statement, 13);
  5740. // Liftgate Fee(No loading dock)
  5741. if (!have_lift_gate) {
  5742. lift_gate = 0;
  5743. }
  5744. ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  5745. if (sqlite3_column_int(statement, 57)) {
  5746. ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  5747. }
  5748. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  5749. if(general_notes==nil)
  5750. general_notes= "";
  5751. NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  5752. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  5753. if(internal_notes==nil)
  5754. internal_notes= "";
  5755. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  5756. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  5757. if(payment_type==nil)
  5758. payment_type= "";
  5759. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  5760. // order info
  5761. orderinfo = [self textFileName:@"order_info.html"];
  5762. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  5763. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  5764. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  5765. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  5766. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  5767. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  5768. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  5769. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  5770. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  5771. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  5772. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  5773. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  5774. NSString *payment = nil;
  5775. if([nspayment_type isEqualToString:@"Credit Card"])
  5776. {
  5777. payment = [self textFileName:@"creditcardpayment.html"];
  5778. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  5779. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  5780. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  5781. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  5782. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  5783. NSString *card_type = [self textAtColumn:42 statement:statement];
  5784. if (card_type.length > 0) { // 显示星号
  5785. card_type = @"****";
  5786. }
  5787. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  5788. if (card_number.length > 0 && card_number.length > 4) {
  5789. for (int i = 0; i < card_number.length - 4; i++) {
  5790. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  5791. }
  5792. } else {
  5793. card_number = @"";
  5794. }
  5795. NSString *card_security_code = [self textAtColumn:44 statement:statement];
  5796. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  5797. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  5798. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  5799. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  5800. card_expiration = @"****";
  5801. }
  5802. NSString *card_city = [self textAtColumn:46 statement:statement];
  5803. NSString *card_state = [self textAtColumn:47 statement:statement];
  5804. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  5805. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  5806. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  5807. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  5808. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  5809. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  5810. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  5811. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  5812. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  5813. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  5814. }
  5815. else
  5816. {
  5817. payment=[self textFileName:@"normalpayment.html"];
  5818. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  5819. }
  5820. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  5821. ret[@"result"]= [NSNumber numberWithInt:2];
  5822. // more info
  5823. moreInfo = [self textFileName:@"more_info.html"];
  5824. /*****ship to******/
  5825. // ShipToCompany_or_&nbsp
  5826. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  5827. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  5828. NSString *shipToName = [self textAtColumn:19 statement:statement];
  5829. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  5830. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  5831. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  5832. /*****ship from******/
  5833. // ShipFromCompany_or_&nbsp
  5834. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  5835. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  5836. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  5837. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  5838. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  5839. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  5840. /*****freight to******/
  5841. // FreightBillToCompany_or_&nbsp
  5842. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  5843. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  5844. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  5845. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  5846. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  5847. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  5848. /*****merchandise to******/
  5849. // MerchandiseBillToCompany_or_&nbsp
  5850. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  5851. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  5852. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  5853. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  5854. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  5855. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  5856. /*****return to******/
  5857. // ReturnToCompany_or_&nbsp
  5858. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  5859. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  5860. NSString *returnToName = [self textAtColumn:31 statement:statement];
  5861. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  5862. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  5863. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  5864. //
  5865. // DebugLog(@"more info : %@",moreInfo);
  5866. // handling fee
  5867. handlingFee = sqlite3_column_double(statement, 33);
  5868. ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  5869. if (sqlite3_column_int(statement, 58)) {
  5870. ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  5871. }
  5872. //
  5873. // customer info
  5874. customerID = [self textAtColumn:36 statement:statement];
  5875. // mode
  5876. ret[@"mode"] = appDelegate.mode;
  5877. // model_count
  5878. ret[@"model_count"] = @(0);
  5879. }
  5880. sqlite3_finalize(statement);
  5881. }
  5882. [iSalesDB close_db:db];
  5883. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  5884. // "customer_email" = "Shui Hu";
  5885. // "customer_fax" = "";
  5886. // "customer_first_name" = F;
  5887. // "customer_last_name" = L;
  5888. // "customer_name" = ",da He Xiang Dong Liu A";
  5889. // "customer_phone" = "Hey Xuan Feng";
  5890. contactInfo[@"customer_phone"] = customer_phone;
  5891. contactInfo[@"customer_fax"] = customer_fax;
  5892. contactInfo[@"customer_email"] = customer_email;
  5893. NSString *first_name = @"";
  5894. NSString *last_name = @"";
  5895. if ([customer_contact isEqualToString:@""]) {
  5896. } else if ([customer_contact containsString:@" "]) {
  5897. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  5898. first_name = [customer_contact substringToIndex:first_space_index];
  5899. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  5900. }
  5901. contactInfo[@"customer_first_name"] = first_name;
  5902. contactInfo[@"customer_last_name"] = last_name;
  5903. ret[@"customerInfo"] = contactInfo;
  5904. // models
  5905. if (nssoid) {
  5906. __block double TotalCuft = 0;
  5907. __block double TotalWeight = 0;
  5908. __block int TotalCarton = 0;
  5909. __block double allItemPrice = 0;
  5910. NSString *modelSql = [NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id",nssoid];
  5911. sqlite3 *db1 = [iSalesDB get_db];
  5912. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5913. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  5914. int product_id = sqlite3_column_int(stmt, 0);
  5915. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  5916. int item_id = sqlite3_column_int(stmt, 7);
  5917. NSString* Price=nil;
  5918. if(str_price==nil)
  5919. {
  5920. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db1];
  5921. if(price==nil)
  5922. Price=@"No Price.";
  5923. else
  5924. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  5925. }
  5926. else
  5927. {
  5928. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  5929. }
  5930. double discount = sqlite3_column_double(stmt, 2);
  5931. int item_count = sqlite3_column_int(stmt, 3);
  5932. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  5933. NSString *nsline_note=nil;
  5934. if(line_note!=nil)
  5935. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  5936. // char *name = (char*)sqlite3_column_text(stmt, 5);
  5937. // NSString *nsname = nil;
  5938. // if(name!=nil)
  5939. // nsname= [[NSString alloc]initWithUTF8String:name];
  5940. NSString *nsname = [self textAtColumn:5 statement:stmt];
  5941. // char *description = (char*)sqlite3_column_text(stmt, 6);
  5942. // NSString *nsdescription=nil;
  5943. // if(description!=nil)
  5944. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  5945. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  5946. // int stockUom = sqlite3_column_int(stmt, 8);
  5947. // int _id = sqlite3_column_int(stmt, 9);
  5948. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  5949. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  5950. double weight=[bsubtotaljson[@"weight"] doubleValue];
  5951. int carton=[bsubtotaljson[@"carton"] intValue];
  5952. TotalCuft += cuft;
  5953. TotalWeight += weight;
  5954. TotalCarton += carton;
  5955. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID];
  5956. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  5957. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  5958. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  5959. itemjson[@"note"]=nsline_note;
  5960. itemjson[@"origin_price"] = Price;
  5961. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  5962. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  5963. itemjson[@"order_item_status"] = @""; // 暂时不处理
  5964. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  5965. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  5966. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  5967. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  5968. if(itemjson[@"combine"] != nil)
  5969. {
  5970. // int citem=0;
  5971. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  5972. for(int bc=0;bc<bcount;bc++)
  5973. {
  5974. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  5975. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  5976. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  5977. subTotal += uprice * modulus * item_count;
  5978. }
  5979. }
  5980. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  5981. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  5982. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  5983. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  5984. allItemPrice += subTotal;
  5985. }];
  5986. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  5987. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  5988. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  5989. // payments/Credits
  5990. // payments_and_credist = sqlite3_column_double(statement, 34);
  5991. payments_and_credist = allItemPrice;
  5992. ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  5993. // // total
  5994. // totalPrice = sqlite3_column_double(statement, 35);
  5995. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  5996. } else {
  5997. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  5998. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  5999. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  6000. // payments/Credits
  6001. payments_and_credist = 0;
  6002. ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6003. }
  6004. // total
  6005. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  6006. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6007. ret[@"order_info"]= orderinfo;
  6008. ret[@"more_order_info"] = moreInfo;
  6009. return [RAUtils dict2data:ret];
  6010. }
  6011. #pragma mark order list
  6012. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  6013. NSString *nsstatus = @"";
  6014. switch (status) {
  6015. case 0:
  6016. {
  6017. nsstatus=@"Temp Order";
  6018. break;
  6019. }
  6020. case 1:
  6021. {
  6022. nsstatus=@"Saved Order";
  6023. break;
  6024. }
  6025. case 2: {
  6026. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  6027. break;
  6028. }
  6029. case 3:
  6030. case 15:
  6031. {
  6032. nsstatus=@"Cancelled";
  6033. break;
  6034. }
  6035. case 10:
  6036. {
  6037. nsstatus=@"Quote Submitted";
  6038. break;
  6039. }
  6040. case 11:
  6041. {
  6042. nsstatus=@"Sales Order Submitted";
  6043. break;
  6044. }
  6045. case 12:
  6046. {
  6047. nsstatus=@"Processing";
  6048. break;
  6049. }
  6050. case 13:
  6051. {
  6052. nsstatus=@"Shipped";
  6053. break;
  6054. }
  6055. case 14:
  6056. {
  6057. nsstatus=@"Closed";
  6058. break;
  6059. }
  6060. case -11:
  6061. {
  6062. nsstatus = @"Ready For Submit";
  6063. break;
  6064. }
  6065. default:
  6066. break;
  6067. }
  6068. return nsstatus;
  6069. }
  6070. + (double) orderTotalPrice:(NSString *)so_id db:(sqlite3 *)db1 close:(BOOL)close{
  6071. double total = 0;
  6072. __block double payments_and_credist = 0;
  6073. __block double allItemPrice = 0;
  6074. // sqlite3 *db1 = [iSalesDB get_db];
  6075. if (so_id) {
  6076. // NSString *modelSql = [NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id",so_id];
  6077. NSString *modelSql = [NSString stringWithFormat:@"select product_id,price,discount,item_count,item_id,customer_cid from (select c.product_id,decrypt(c.str_price) as price,c.discount,c.item_count,c.item_id,o.customer_cid from offline_cart as c left join offline_order as o on c.so_no = o.so_id where c.so_no='%@');",so_id];
  6078. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6079. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6080. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  6081. int product_id = sqlite3_column_int(stmt, 0);
  6082. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  6083. int discount = sqlite3_column_int(stmt, 2);
  6084. int item_count = sqlite3_column_int(stmt, 3);
  6085. // int item_id = sqlite3_column_int(stmt, 7);
  6086. int item_id = sqlite3_column_int(stmt, 4);
  6087. char *customer_cid = (char *)sqlite3_column_text(stmt, 5);
  6088. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  6089. NSString* Price=nil;
  6090. if(str_price==nil)
  6091. {
  6092. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db1];
  6093. if(price==nil)
  6094. Price=@"No Price.";
  6095. else
  6096. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6097. }
  6098. else
  6099. {
  6100. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  6101. }
  6102. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID];
  6103. itemjson[@"The unit price"]=Price;
  6104. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  6105. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  6106. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6107. if(itemjson[@"combine"] != nil)
  6108. {
  6109. // int citem=0;
  6110. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  6111. for(int bc=0;bc<bcount;bc++)
  6112. {
  6113. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6114. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6115. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6116. subTotal += uprice * modulus * item_count;
  6117. }
  6118. }
  6119. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  6120. allItemPrice += subTotal;
  6121. }];
  6122. payments_and_credist = allItemPrice;
  6123. } else {
  6124. // payments/Credits
  6125. payments_and_credist = 0;
  6126. }
  6127. // lift_gate handlingFee shippingFee
  6128. __block double lift_gate = 0;
  6129. __block double handlingFee = 0;
  6130. __block double shippingFee = 0;
  6131. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  6132. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6133. int have_lift_gate = sqlite3_column_int(stmt, 0);
  6134. if (have_lift_gate) {
  6135. lift_gate = sqlite3_column_double(stmt, 1);
  6136. }
  6137. handlingFee = sqlite3_column_double(stmt, 2);
  6138. shippingFee = sqlite3_column_double(stmt, 3);
  6139. }];
  6140. // total
  6141. total = payments_and_credist + lift_gate + handlingFee + shippingFee;
  6142. if (close) {
  6143. [iSalesDB close_db:db1];
  6144. }
  6145. return total;
  6146. }
  6147. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  6148. {
  6149. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  6150. int limit = [[params valueForKey:@"limit"] intValue];
  6151. int offset = [[params valueForKey:@"offset"] intValue];
  6152. NSString* keyword = [params valueForKey:@"keyWord"];
  6153. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  6154. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  6155. NSString* where=@"1 = 1";
  6156. if(keyword.length>0)
  6157. where=[where stringByAppendingString:[NSString stringWithFormat:@" and (lower(o.so_id) like '%%%@%%' or lower(o.sales_rep) like '%%%@%%' or lower(decrypt(c.company_name)) like '%%%@%%' or lower(o.create_by) like '%%%@%%')",keyword.lowercaseString,keyword.lowercaseString,keyword.lowercaseString,keyword.lowercaseString]];
  6158. if (orderStatus.length > 0) {
  6159. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  6160. if (order_status_array.count == 1) {
  6161. int status_value = [[order_status_array firstObject] integerValue];
  6162. if (status_value <= 1 || status_value == 3) {
  6163. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  6164. } else {
  6165. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  6166. }
  6167. } else if (order_status_array.count > 1) {
  6168. for (int i = 0; i < order_status_array.count;i++) {
  6169. NSString *status = order_status_array[i];
  6170. NSString *condition = @" or";
  6171. if (i == 0) {
  6172. condition = @" and (";
  6173. }
  6174. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  6175. int status_value = [status integerValue];
  6176. if (status_value <= 1 || status_value == 3) {
  6177. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  6178. } else {
  6179. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  6180. }
  6181. }
  6182. where = [where stringByAppendingString:@" )"];
  6183. }
  6184. }
  6185. NSString* sqlQuery=[NSString stringWithFormat:@"SELECT o._id,o.so_id,o.status,o.sales_rep,o.create_by,decrypt(c.company_name),o.create_time,o.total_price,o.customer_contact,o.erpOrderStatus,o.order_id is null from offline_order o left join offline_contact c on o.customer_cid=c.contact_id where %@ order by o.create_time desc limit %d offset %d" ,where, limit, offset];
  6186. // DebugLog(@"order list sql: %@",sqlQuery);
  6187. sqlite3 *db = [iSalesDB get_db];
  6188. sqlite3_stmt * statement;
  6189. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  6190. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6191. {
  6192. int count=0;
  6193. while (sqlite3_step(statement) == SQLITE_ROW)
  6194. {
  6195. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  6196. int order_id = sqlite3_column_double(statement, 0);
  6197. char *soid = (char*)sqlite3_column_text(statement, 1);
  6198. if(soid==nil)
  6199. soid= "";
  6200. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  6201. int status = sqlite3_column_double(statement, 2);
  6202. int erpStatus = sqlite3_column_double(statement, 9);
  6203. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  6204. if(sales_rep==nil)
  6205. sales_rep= "";
  6206. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  6207. char *create_by = (char*)sqlite3_column_text(statement, 4);
  6208. if(create_by==nil)
  6209. create_by= "";
  6210. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  6211. char *company_name = (char*)sqlite3_column_text(statement, 5);
  6212. if(company_name==nil)
  6213. company_name= "";
  6214. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  6215. char *create_time = (char*)sqlite3_column_text(statement, 6);
  6216. if(create_time==nil)
  6217. create_time= "";
  6218. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  6219. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  6220. // double total_price = sqlite3_column_double(statement, 7);
  6221. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] db:db close:NO];
  6222. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  6223. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  6224. int offline_edit = sqlite3_column_int(statement, 10);
  6225. // ": "JH",
  6226. // "": "$8307.00",
  6227. // "": "MOB1608050001",
  6228. // "": "ArpithaT",
  6229. // "": "1st Stage Property Transformations",
  6230. // "": "JANICE SUTTON",
  6231. // "": 2255,
  6232. // "": "08/02/2016 09:49:18",
  6233. // "": 1,
  6234. // "": "Saved Order"
  6235. // "": "1470384050483",
  6236. // "model_count": "6 / 28"
  6237. item[@"sales_rep"]= nssales_rep;
  6238. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  6239. item[@"so#"]= nssoid;
  6240. item[@"create_by"]= nscreate_by;
  6241. item[@"customer_name"]= nscompany_name;
  6242. item[@"customer_contact"] = customer_contact;
  6243. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  6244. item[@"purchase_time"]= nscreate_time;
  6245. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",status];
  6246. item[@"order_status"]= nsstatus;
  6247. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  6248. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  6249. // item[@"model_count"]
  6250. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  6251. count++;
  6252. }
  6253. ret[@"count"]= [NSNumber numberWithInt:count];
  6254. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  6255. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  6256. ret[@"result"]= [NSNumber numberWithInt:2];
  6257. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  6258. ret[@"time_zone"] = @"PST";
  6259. sqlite3_finalize(statement);
  6260. }
  6261. int total_count = [iSalesDB get_recordcount:db table:@"offline_order o left join offline_contact c on o.customer_cid=c.contact_id" where:where];
  6262. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  6263. [iSalesDB execSql:@"END TRANSACTION" db:db];
  6264. [iSalesDB close_db:db];
  6265. return [RAUtils dict2data:ret];
  6266. }
  6267. #pragma mark update gnotes
  6268. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  6269. {
  6270. DebugLog(@"params: %@",params);
  6271. // comments = Meyoyoyoyoyoyoy;
  6272. // orderCode = MOB1608110001;
  6273. // password = 123456;
  6274. // user = EvanK;
  6275. NSString *sql = [NSString stringWithFormat:@"update offline_order set general_notes = '%@' where so_id = '%@';",[self valueInParams:params key:@"comments" translateSingleQuote:YES],[self valueInParams:params key:@"orderCode" translateSingleQuote:YES]];
  6276. int ret = [iSalesDB execSql:sql];
  6277. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6278. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6279. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6280. // [dic setValue:@"160409" forKey:@"min_ver"];
  6281. return [RAUtils dict2data:dic];
  6282. }
  6283. #pragma mark move to wishlist
  6284. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  6285. {
  6286. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  6287. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6288. _id = [NSString stringWithFormat:@"(%@)",_id];
  6289. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  6290. sqlite3 *db = [iSalesDB get_db];
  6291. __block NSString *product_id = @"";
  6292. __block NSString *item_count_str = @"";
  6293. // __block NSString *item_id = nil;
  6294. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6295. // product_id = [self textAtColumn:0 statement:stmt];
  6296. int item_count = sqlite3_column_int(stmt, 1);
  6297. // item_id = [self textAtColumn:2 statement:stmt];
  6298. NSString *p_id = [self textAtColumn:0 statement:stmt];
  6299. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  6300. if (p_id.length) {
  6301. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  6302. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  6303. }
  6304. }];
  6305. [iSalesDB close_db:db];
  6306. // 去除第一个,
  6307. product_id = [product_id substringFromIndex:1];
  6308. item_count_str = [item_count_str substringFromIndex:1];
  6309. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6310. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  6311. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  6312. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  6313. sqlite3 *db1 = [iSalesDB get_db];
  6314. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  6315. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6316. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  6317. // 删除
  6318. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  6319. int ret = [iSalesDB execSql:deleteSql db:db1];
  6320. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6321. [iSalesDB close_db:db1];
  6322. return [RAUtils dict2data:dic];
  6323. }
  6324. #pragma mark cart delete
  6325. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  6326. {
  6327. // cartItemId = 548;
  6328. // orderCode = MOB1608110001;
  6329. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  6330. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6331. _id = [NSString stringWithFormat:@"(%@)",_id];
  6332. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  6333. int ret = [iSalesDB execSql:sql];
  6334. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6335. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6336. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6337. // [dic setValue:@"160409" forKey:@"min_ver"];
  6338. return [RAUtils dict2data:dic];
  6339. }
  6340. #pragma mark set price
  6341. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  6342. {
  6343. DebugLog(@"cart set price params: %@",params);
  6344. // "cartitem_id" = 1;
  6345. // discount = "0.000000";
  6346. // "item_note" = "";
  6347. // price = "269.000000";
  6348. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6349. NSString *notes = [self valueInParams:params key:@"item_note"];
  6350. NSString *discount = [self valueInParams:params key:@"discount"];
  6351. NSString *price = [self valueInParams:params key:@"price"];
  6352. // bool badd_price_changed=false;
  6353. // sqlite3* db=[iSalesDB get_db];
  6354. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6355. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  6356. // NSRange range;
  6357. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  6358. //
  6359. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  6360. // badd_price_changed=true;
  6361. // [iSalesDB close_db:db];
  6362. //
  6363. // if(badd_price_changed)
  6364. // {
  6365. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  6366. // }
  6367. //
  6368. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  6369. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  6370. int ret = [iSalesDB execSql:sql];
  6371. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6372. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6373. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6374. // [dic setValue:@"160409" forKey:@"min_ver"];
  6375. return [RAUtils dict2data:dic];
  6376. }
  6377. #pragma mark set line notes
  6378. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  6379. {
  6380. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6381. NSString *notes = [self valueInParams:params key:@"notes"];
  6382. notes = [self translateSingleQuote:notes];
  6383. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  6384. int ret = [iSalesDB execSql:sql];
  6385. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6386. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6387. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6388. // [dic setValue:@"160409" forKey:@"min_ver"];
  6389. return [RAUtils dict2data:dic];
  6390. }
  6391. #pragma mark set qty
  6392. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  6393. {
  6394. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6395. int item_count = [params[@"inputInt"] integerValue];
  6396. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  6397. int ret = [iSalesDB execSql:sql];
  6398. __block int item_id = 0;
  6399. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6400. item_id = sqlite3_column_int(stmt, 0);
  6401. }];
  6402. sqlite3 *db = [iSalesDB get_db];
  6403. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  6404. [iSalesDB close_db:db];
  6405. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6406. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6407. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6408. // [dic setValue:@"160409" forKey:@"min_ver"];
  6409. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  6410. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  6411. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  6412. return [RAUtils dict2data:dic];
  6413. }
  6414. #pragma mark place order
  6415. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  6416. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6417. if (aname) {
  6418. [dic setValue:aname forKey:@"aname"];
  6419. }
  6420. if (control) {
  6421. [dic setValue:control forKey:@"control"];
  6422. }
  6423. if (keyboard) {
  6424. [dic setValue:keyboard forKey:@"keyboard"];
  6425. }
  6426. if (name) {
  6427. [dic setValue:name forKey:@"name"];
  6428. }
  6429. if (value) {
  6430. [dic setValue:value forKey:@"value"];
  6431. }
  6432. return dic;
  6433. }
  6434. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  6435. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6436. orderCode = [self translateSingleQuote:orderCode];
  6437. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  6438. NSString *customerSql = [NSString stringWithFormat:@"select c.contact_id,c.img_0,c.img_1,c.img_2,decrypt(c.company_name),decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,c.first_name,c.last_name,o.customer_email,o.customer_phone,o.customer_fax,c.zipcode,c.city,c.state,c.country,o.customer_contact from offline_contact as c join offline_order as o on c.contact_id = o.customer_cid where o.so_id = '%@';",orderCode];
  6439. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6440. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  6441. NSString *img0 = [self textAtColumn:1 statement:stmt];
  6442. NSString *img1 = [self textAtColumn:2 statement:stmt];
  6443. NSString *img2 = [self textAtColumn:3 statement:stmt];
  6444. NSString *company_name = [self textAtColumn:4 statement:stmt];
  6445. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  6446. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  6447. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  6448. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  6449. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  6450. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  6451. NSString *email = [self textAtColumn:11 statement:stmt];
  6452. NSString *phone = [self textAtColumn:12 statement:stmt];
  6453. NSString *fax = [self textAtColumn:13 statement:stmt];
  6454. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  6455. NSString *city = [self textAtColumn:15 statement:stmt];
  6456. NSString *state = [self textAtColumn:16 statement:stmt];
  6457. NSString *country = [self textAtColumn:17 statement:stmt];
  6458. NSString *name = [self textAtColumn:18 statement:stmt];
  6459. // contact id
  6460. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  6461. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  6462. [contact_id_dic setValue:@"text" forKey:@"control"];
  6463. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  6464. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  6465. [contact_id_dic setValue:@"true" forKey:@"required"];
  6466. [contact_id_dic setValue:contact_id forKey:@"value"];
  6467. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  6468. // business card
  6469. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  6470. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  6471. [business_card_dic setValue:@"img" forKey:@"control"];
  6472. [business_card_dic setValue:@"1" forKey:@"disable"];
  6473. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  6474. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  6475. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  6476. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  6477. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  6478. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  6479. [business_card_dic setValue:@"business_card" forKey:@"name"];
  6480. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  6481. // fax
  6482. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6483. [customer_dic setValue:fax_dic forKey:@"item_10"];
  6484. // zipcode
  6485. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  6486. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  6487. // city
  6488. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  6489. [customer_dic setValue:city_dic forKey:@"item_12"];
  6490. // state
  6491. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  6492. [customer_dic setValue:state_dic forKey:@"item_13"];
  6493. // country
  6494. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  6495. [customer_dic setValue:country_dic forKey:@"item_14"];
  6496. // company name
  6497. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  6498. [customer_dic setValue:company_dic forKey:@"item_2"];
  6499. // addr_1
  6500. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  6501. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  6502. // addr_2
  6503. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  6504. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  6505. // addr_3
  6506. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  6507. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  6508. // addr_4
  6509. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  6510. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  6511. // Contact
  6512. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  6513. [customer_dic setValue:contact_dic forKey:@"item_7"];
  6514. // email
  6515. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6516. [customer_dic setValue:email_dic forKey:@"item_8"];
  6517. // phone
  6518. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6519. [customer_dic setValue:phone_dic forKey:@"item_9"];
  6520. // title
  6521. [customer_dic setValue:@"Customer" forKey:@"title"];
  6522. // count
  6523. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  6524. }];
  6525. // setting
  6526. NSDictionary *setting = params[@"setting"];
  6527. NSNumber *hide = setting[@"CustomerHide"];
  6528. [customer_dic setValue:hide forKey:@"hide"];
  6529. return customer_dic;
  6530. }
  6531. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  6532. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6533. // setting
  6534. NSDictionary *setting = params[@"setting"];
  6535. NSNumber *hide = setting[@"ShipToHide"];
  6536. [dic setValue:hide forKey:@"hide"];
  6537. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6538. orderCode = [self translateSingleQuote:orderCode];
  6539. NSString *sql = [NSString stringWithFormat:@"select receive_cid,receive_name,receive_ext,receive_contact,receive_email,receive_fax,receive_phone from offline_order where so_id = '%@';",orderCode];
  6540. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6541. NSString *cid = [self textAtColumn:0 statement:stmt];
  6542. NSString *name = [self textAtColumn:1 statement:stmt];
  6543. NSString *ext = [self textAtColumn:2 statement:stmt];
  6544. NSString *contact = [self textAtColumn:3 statement:stmt];
  6545. NSString *email = [self textAtColumn:4 statement:stmt];
  6546. NSString *fax = [self textAtColumn:5 statement:stmt];
  6547. NSString *phone = [self textAtColumn:6 statement:stmt];
  6548. // count
  6549. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6550. // title
  6551. [dic setValue:@"Ship To" forKey:@"title"];
  6552. // choose
  6553. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6554. [choose_dic setValue:@"choose" forKey:@"aname"];
  6555. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6556. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  6557. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6558. @"key_map" : @{@"receive_cid" : @"customer_cid",
  6559. @"receive_contact" : @"customer_contact",
  6560. @"receive_email" : @"customer_email",
  6561. @"receive_ext" : @"customer_contact_ext",
  6562. @"receive_fax" : @"customer_fax",
  6563. @"receive_name" : @"customer_name",
  6564. @"receive_phone" : @"customer_phone"},
  6565. @"refresh" : [NSNumber numberWithInteger:1],
  6566. @"type" : @"pull"};
  6567. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  6568. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  6569. @"name" : @"Add new address",
  6570. @"refresh" : [NSNumber numberWithInteger:1],
  6571. @"value" : @"new_addr"
  6572. };
  6573. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  6574. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  6575. @"key_map" : @{@"receive_cid" : @"customer_cid",
  6576. @"receive_contact" : @"customer_contact",
  6577. @"receive_email" : @"customer_email",
  6578. @"receive_ext" : @"customer_contact_ext",
  6579. @"receive_fax" : @"customer_fax",
  6580. @"receive_name" : @"customer_name",
  6581. @"receive_phone" : @"customer_phone"},
  6582. @"name" : @"select_ship_to",
  6583. @"refresh" : [NSNumber numberWithInteger:1],
  6584. @"value" : @"Sales_Order_Ship_To"};
  6585. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  6586. [dic setValue:choose_dic forKey:@"item_0"];
  6587. // contact id
  6588. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6589. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6590. [contact_id_dic setValue:@"true" forKey:@"required"];
  6591. [dic setValue:contact_id_dic forKey:@"item_1"];
  6592. // company name
  6593. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6594. [dic setValue:company_name_dic forKey:@"item_2"];
  6595. // address
  6596. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  6597. NSNumber *required = setting[@"ShippingToAddressRequire"];
  6598. if ([required integerValue]) {
  6599. [ext_dic setValue:@"true" forKey:@"required"];
  6600. }
  6601. [dic setValue:ext_dic forKey:@"item_3"];
  6602. // contact
  6603. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6604. [dic setValue:contact_dic forKey:@"item_4"];
  6605. // phone
  6606. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6607. [dic setValue:phone_dic forKey:@"item_5"];
  6608. // fax
  6609. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6610. [dic setValue:fax_dic forKey:@"item_6"];
  6611. // email
  6612. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6613. [dic setValue:email_dic forKey:@"item_7"];
  6614. }];
  6615. return dic;
  6616. }
  6617. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  6618. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6619. // setting
  6620. NSDictionary *setting = params[@"setting"];
  6621. NSNumber *hide = setting[@"ShipFromHide"];
  6622. [dic setValue:hide forKey:@"hide"];
  6623. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6624. orderCode = [self translateSingleQuote:orderCode];
  6625. NSString *sql = [NSString stringWithFormat:@"select sender_cid,sender_name,sender_ext,sender_contact,sender_email,sender_fax,sender_phone from offline_order where so_id = '%@';",orderCode];
  6626. NSString *ship_from_default_sql = @"select contact_id,decrypt(company_name),decrypt(addr_1),first_name || ' ' || last_name,email,fax,phone from offline_contact where contact_id = 'NPD';";
  6627. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6628. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  6629. __block NSString *name = [self textAtColumn:1 statement:stmt];
  6630. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  6631. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  6632. __block NSString *email = [self textAtColumn:4 statement:stmt];
  6633. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  6634. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  6635. if (!cid.length) {
  6636. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  6637. cid = [self textAtColumn:0 statement:statment];
  6638. name = [self textAtColumn:1 statement:statment];
  6639. ext = [self textAtColumn:2 statement:statment];
  6640. contact = [self textAtColumn:3 statement:statment];
  6641. email = [self textAtColumn:4 statement:statment];
  6642. fax = [self textAtColumn:5 statement:statment];
  6643. phone = [self textAtColumn:6 statement:statment];
  6644. }];
  6645. }
  6646. // count
  6647. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6648. // title
  6649. [dic setValue:@"Ship From" forKey:@"title"];
  6650. // hide
  6651. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6652. // choose
  6653. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6654. [choose_dic setValue:@"choose" forKey:@"aname"];
  6655. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6656. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6657. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  6658. @"key_map" : @{@"sender_cid" : @"customer_cid",
  6659. @"sender_contact" : @"customer_contact",
  6660. @"sender_email" : @"customer_email",
  6661. @"sender_ext" : @"customer_contact_ext",
  6662. @"sender_fax" : @"customer_fax",
  6663. @"sender_name" : @"customer_name",
  6664. @"sender_phone" : @"customer_phone"},
  6665. @"name" : @"select_cid",
  6666. @"refresh" : [NSNumber numberWithInteger:0],
  6667. @"value" : @"Sales_Order_Ship_From"};
  6668. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  6669. [dic setValue:choose_dic forKey:@"item_0"];
  6670. // contact id
  6671. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6672. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6673. [contact_id_dic setValue:@"true" forKey:@"required"];
  6674. [dic setValue:contact_id_dic forKey:@"item_1"];
  6675. // company name
  6676. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6677. [dic setValue:company_name_dic forKey:@"item_2"];
  6678. // address
  6679. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  6680. [dic setValue:ext_dic forKey:@"item_3"];
  6681. // contact
  6682. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6683. [dic setValue:contact_dic forKey:@"item_4"];
  6684. // phone
  6685. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6686. [dic setValue:phone_dic forKey:@"item_5"];
  6687. // fax
  6688. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6689. [dic setValue:fax_dic forKey:@"item_6"];
  6690. // email
  6691. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6692. [dic setValue:email_dic forKey:@"item_7"];
  6693. }];
  6694. return dic;
  6695. }
  6696. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  6697. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6698. // setting
  6699. NSDictionary *setting = params[@"setting"];
  6700. NSNumber *hide = setting[@"FreightBillToHide"];
  6701. [dic setValue:hide forKey:@"hide"];
  6702. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6703. orderCode = [self translateSingleQuote:orderCode];
  6704. NSString *sql = [NSString stringWithFormat:@"select shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_email,shipping_billto_fax,shipping_billto_phone from offline_order where so_id = '%@';",orderCode];
  6705. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6706. NSString *cid = [self textAtColumn:0 statement:stmt];
  6707. NSString *name = [self textAtColumn:1 statement:stmt];
  6708. NSString *ext = [self textAtColumn:2 statement:stmt];
  6709. NSString *contact = [self textAtColumn:3 statement:stmt];
  6710. NSString *email = [self textAtColumn:4 statement:stmt];
  6711. NSString *fax = [self textAtColumn:5 statement:stmt];
  6712. NSString *phone = [self textAtColumn:6 statement:stmt];
  6713. // count
  6714. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6715. // title
  6716. [dic setValue:@"Freight Bill To" forKey:@"title"];
  6717. // hide
  6718. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6719. // choose
  6720. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6721. [choose_dic setValue:@"choose" forKey:@"aname"];
  6722. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6723. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  6724. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  6725. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  6726. @"shipping_billto_contact" : @"receive_contact",
  6727. @"shipping_billto_email" : @"receive_email",
  6728. @"shipping_billto_ext" : @"receive_ext",
  6729. @"shipping_billto_fax" : @"receive_fax",
  6730. @"shipping_billto_name" : @"receive_name",
  6731. @"shipping_billto_phone" : @"receive_phone"},
  6732. @"type" : @"pull"};
  6733. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  6734. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6735. @"type" : @"pull",
  6736. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  6737. @"shipping_billto_contact" : @"customer_contact",
  6738. @"shipping_billto_email" : @"customer_email",
  6739. @"shipping_billto_ext" : @"customer_contact_ext",
  6740. @"shipping_billto_fax" : @"customer_fax",
  6741. @"shipping_billto_name" : @"customer_name",
  6742. @"shipping_billto_phone" : @"customer_phone"}
  6743. };
  6744. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  6745. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  6746. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  6747. @"shipping_billto_contact" : @"sender_contact",
  6748. @"shipping_billto_email" : @"sender_email",
  6749. @"shipping_billto_ext" : @"sender_ext",
  6750. @"shipping_billto_fax" : @"sender_fax",
  6751. @"shipping_billto_name" : @"sender_name",
  6752. @"shipping_billto_phone" : @"sender_phone"},
  6753. @"type" : @"pull"
  6754. };
  6755. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  6756. NSDictionary *select_freight_bill_to_dic = @{
  6757. @"aname" : @"Select freight bill to",
  6758. @"name" : @"select_cid",
  6759. @"refresh" : [NSNumber numberWithInteger:0],
  6760. @"value" : @"Sales_Order_Freight_Bill_To",
  6761. @"key_map" : @{
  6762. @"shipping_billto_cid" : @"customer_cid",
  6763. @"shipping_billto_contact" : @"customer_contact",
  6764. @"shipping_billto_email" : @"customer_email",
  6765. @"shipping_billto_ext" : @"customer_contact_ext",
  6766. @"shipping_billto_fax" : @"customer_fax",
  6767. @"shipping_billto_name" : @"customer_name",
  6768. @"shipping_billto_phone" : @"customer_phone"
  6769. }
  6770. };
  6771. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  6772. [dic setValue:choose_dic forKey:@"item_0"];
  6773. // contact id
  6774. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6775. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6776. [contact_id_dic setValue:@"true" forKey:@"required"];
  6777. [dic setValue:contact_id_dic forKey:@"item_1"];
  6778. // company name
  6779. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6780. [dic setValue:company_name_dic forKey:@"item_2"];
  6781. // address
  6782. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  6783. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  6784. if ([ext_required integerValue]) {
  6785. [ext_dic setValue:@"true" forKey:@"required"];
  6786. }
  6787. [dic setValue:ext_dic forKey:@"item_3"];
  6788. // contact
  6789. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6790. [dic setValue:contact_dic forKey:@"item_4"];
  6791. // phone
  6792. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6793. [dic setValue:phone_dic forKey:@"item_5"];
  6794. // fax
  6795. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6796. [dic setValue:fax_dic forKey:@"item_6"];
  6797. // email
  6798. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6799. [dic setValue:email_dic forKey:@"item_7"];
  6800. }];
  6801. return dic;
  6802. }
  6803. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  6804. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6805. // setting
  6806. NSDictionary *setting = params[@"setting"];
  6807. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  6808. [dic setValue:hide forKey:@"hide"];
  6809. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6810. orderCode = [self translateSingleQuote:orderCode];
  6811. NSString *sql = [NSString stringWithFormat:@"select billing_cid,billing_name,billing_ext,billing_contact,billing_email,billing_fax,billing_phone from offline_order where so_id = '%@';",orderCode];
  6812. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6813. NSString *cid = [self textAtColumn:0 statement:stmt];
  6814. NSString *name = [self textAtColumn:1 statement:stmt];
  6815. NSString *ext = [self textAtColumn:2 statement:stmt];
  6816. NSString *contact = [self textAtColumn:3 statement:stmt];
  6817. NSString *email = [self textAtColumn:4 statement:stmt];
  6818. NSString *fax = [self textAtColumn:5 statement:stmt];
  6819. NSString *phone = [self textAtColumn:6 statement:stmt];
  6820. // count
  6821. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6822. // title
  6823. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  6824. // hide
  6825. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6826. // choose
  6827. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6828. [choose_dic setValue:@"choose" forKey:@"aname"];
  6829. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6830. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  6831. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  6832. @"key_map" : @{@"billing_cid" : @"receive_cid",
  6833. @"billing_contact" : @"receive_contact",
  6834. @"billing_email" : @"receive_email",
  6835. @"billing_ext" : @"receive_ext",
  6836. @"billing_fax" : @"receive_fax",
  6837. @"billing_name" : @"receive_name",
  6838. @"billing_phone" : @"receive_phone"},
  6839. @"type" : @"pull"};
  6840. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  6841. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6842. @"type" : @"pull",
  6843. @"key_map" : @{@"billing_cid" : @"customer_cid",
  6844. @"billing_contact" : @"customer_contact",
  6845. @"billing_email" : @"customer_email",
  6846. @"billing_ext" : @"customer_contact_ext",
  6847. @"billing_fax" : @"customer_fax",
  6848. @"billing_name" : @"customer_name",
  6849. @"billing_phone" : @"customer_phone"}
  6850. };
  6851. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  6852. NSDictionary *select_bill_to_dic = @{
  6853. @"aname" : @"Select bill to",
  6854. @"name" : @"select_cid",
  6855. @"refresh" : [NSNumber numberWithInteger:0],
  6856. @"value" : @"Sales_Order_Merchandise_Bill_To",
  6857. @"key_map" : @{
  6858. @"billing_cid" : @"customer_cid",
  6859. @"billing_contact" : @"customer_contact",
  6860. @"billing_email" : @"customer_email",
  6861. @"billing_ext" : @"customer_contact_ext",
  6862. @"billing_fax" : @"customer_fax",
  6863. @"billing_name" : @"customer_name",
  6864. @"billing_phone" : @"customer_phone"
  6865. }
  6866. };
  6867. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  6868. [dic setValue:choose_dic forKey:@"item_0"];
  6869. // contact id
  6870. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6871. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6872. [contact_id_dic setValue:@"true" forKey:@"required"];
  6873. [dic setValue:contact_id_dic forKey:@"item_1"];
  6874. // company name
  6875. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6876. [dic setValue:company_name_dic forKey:@"item_2"];
  6877. // address
  6878. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  6879. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  6880. if ([ext_required integerValue]) {
  6881. [ext_dic setValue:@"true" forKey:@"required"];
  6882. }
  6883. [dic setValue:ext_dic forKey:@"item_3"];
  6884. // contact
  6885. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6886. [dic setValue:contact_dic forKey:@"item_4"];
  6887. // phone
  6888. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6889. [dic setValue:phone_dic forKey:@"item_5"];
  6890. // fax
  6891. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6892. [dic setValue:fax_dic forKey:@"item_6"];
  6893. // email
  6894. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6895. [dic setValue:email_dic forKey:@"item_7"];
  6896. }];
  6897. return dic;
  6898. }
  6899. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  6900. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6901. // setting
  6902. NSDictionary *setting = params[@"setting"];
  6903. NSNumber *hide = setting[@"ReturnToHide"];
  6904. [dic setValue:hide forKey:@"hide"];
  6905. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6906. orderCode = [self translateSingleQuote:orderCode];
  6907. NSString *sql = [NSString stringWithFormat:@"select returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_email,returnto_fax,returnto_phone from offline_order where so_id = '%@';",orderCode];
  6908. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6909. NSString *cid = [self textAtColumn:0 statement:stmt];
  6910. NSString *name = [self textAtColumn:1 statement:stmt];
  6911. NSString *ext = [self textAtColumn:2 statement:stmt];
  6912. NSString *contact = [self textAtColumn:3 statement:stmt];
  6913. NSString *email = [self textAtColumn:4 statement:stmt];
  6914. NSString *fax = [self textAtColumn:5 statement:stmt];
  6915. NSString *phone = [self textAtColumn:6 statement:stmt];
  6916. // count
  6917. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6918. // title
  6919. [dic setValue:@"Return To" forKey:@"title"];
  6920. // hide
  6921. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6922. // choose
  6923. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6924. [choose_dic setValue:@"choose" forKey:@"aname"];
  6925. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6926. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  6927. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  6928. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  6929. @"returnto_contact" : @"sender_contact",
  6930. @"returnto_email" : @"sender_email",
  6931. @"returnto_ext" : @"sender_ext",
  6932. @"returnto_fax" : @"sender_fax",
  6933. @"returnto_name" : @"sender_name",
  6934. @"returnto_phone" : @"sender_phone"},
  6935. @"type" : @"pull"};
  6936. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  6937. NSDictionary *select_return_to_dic = @{
  6938. @"aname" : @"Select return to",
  6939. @"name" : @"select_cid",
  6940. @"refresh" : [NSNumber numberWithInteger:0],
  6941. @"value" : @"Contact_Return_To",
  6942. @"key_map" : @{
  6943. @"returnto_cid" : @"customer_cid",
  6944. @"returnto_contact" : @"customer_contact",
  6945. @"returnto_email" : @"customer_email",
  6946. @"returnto_ext" : @"customer_contact_ext",
  6947. @"returnto_fax" : @"customer_fax",
  6948. @"returnto_name" : @"customer_name",
  6949. @"returnto_phone" : @"customer_phone"
  6950. }
  6951. };
  6952. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  6953. [dic setValue:choose_dic forKey:@"item_0"];
  6954. // contact id
  6955. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6956. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6957. [contact_id_dic setValue:@"true" forKey:@"required"];
  6958. [dic setValue:contact_id_dic forKey:@"item_1"];
  6959. // company name
  6960. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6961. [dic setValue:company_name_dic forKey:@"item_2"];
  6962. // address
  6963. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  6964. [dic setValue:ext_dic forKey:@"item_3"];
  6965. // contact
  6966. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6967. [dic setValue:contact_dic forKey:@"item_4"];
  6968. // phone
  6969. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6970. [dic setValue:phone_dic forKey:@"item_5"];
  6971. // fax
  6972. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6973. [dic setValue:fax_dic forKey:@"item_6"];
  6974. // email
  6975. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6976. [dic setValue:email_dic forKey:@"item_7"];
  6977. }];
  6978. return dic;
  6979. }
  6980. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  6981. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6982. orderCode = [self translateSingleQuote:orderCode];
  6983. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6984. __block double TotalCuft = 0;
  6985. __block double TotalWeight = 0;
  6986. __block int TotalCarton = 0;
  6987. __block double payments = 0;
  6988. // setting
  6989. NSDictionary *setting = params[@"setting"];
  6990. NSNumber *hide = setting[@"ModelInformationHide"];
  6991. [dic setValue:hide forKey:@"hide"];
  6992. NSString *sql = [NSString stringWithFormat:@"select c.item_id,c.item_count,m.stockUom,c.price,c.discount,m.name,m.description,c.line_note,m.availability from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id;",orderCode];
  6993. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6994. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6995. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  6996. // item id
  6997. int item_id = sqlite3_column_int(stmt, 0);
  6998. // count
  6999. int item_count = sqlite3_column_int(stmt, 1);
  7000. // stockUom
  7001. int stockUom = sqlite3_column_int(stmt, 2);
  7002. // unit price
  7003. NSString *str_price = [self textAtColumn:3 statement:stmt];
  7004. NSString* Price=nil;
  7005. if([str_price isEqualToString:@""])
  7006. {
  7007. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  7008. if(price==nil)
  7009. Price=@"No Price.";
  7010. else
  7011. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7012. }
  7013. else
  7014. {
  7015. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  7016. }
  7017. // discount
  7018. double discount = sqlite3_column_double(stmt, 4);
  7019. // name
  7020. NSString *name = [self textAtColumn:5 statement:stmt];
  7021. // description
  7022. NSString *description = [self textAtColumn:6 statement:stmt];
  7023. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  7024. // line note
  7025. NSString *line_note = [self textAtColumn:7 statement:stmt];
  7026. int avaulability = sqlite3_column_int(stmt, 8);
  7027. // img
  7028. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  7029. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  7030. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7031. if(combine != nil)
  7032. {
  7033. // int citem=0;
  7034. int bcount=[[combine valueForKey:@"count"] intValue];
  7035. for(int bc=0;bc<bcount;bc++)
  7036. {
  7037. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7038. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7039. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7040. subTotal += uprice * modulus * item_count;
  7041. }
  7042. }
  7043. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  7044. [model_dic setValue:@"model" forKey:@"control"];
  7045. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  7046. [model_dic setValue:description forKey:@"description"];
  7047. [model_dic setValue:line_note forKey:@"note"];
  7048. [model_dic setValue:img forKey:@"img_url"];
  7049. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  7050. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  7051. [model_dic setValue:Price forKey:@"unit_price"];
  7052. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  7053. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  7054. if (combine) {
  7055. [model_dic setValue:combine forKey:@"combine"];
  7056. }
  7057. // well,what under the row is the info for total
  7058. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  7059. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  7060. double weight=[bsubtotaljson[@"weight"] doubleValue];
  7061. int carton=[bsubtotaljson[@"carton"] intValue];
  7062. TotalCuft += cuft;
  7063. TotalWeight += weight;
  7064. TotalCarton += carton;
  7065. payments += subTotal;
  7066. //---------------------------
  7067. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  7068. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  7069. }];
  7070. [dic setValue:@"Model Information" forKey:@"title"];
  7071. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  7072. return dic;
  7073. }
  7074. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  7075. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7076. [dic setValue:@"Remarks Content" forKey:@"title"];
  7077. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  7078. // setting
  7079. NSDictionary *setting = params[@"setting"];
  7080. NSNumber *hide = setting[@"RemarksContentHide"];
  7081. [dic setValue:hide forKey:@"hide"];
  7082. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7083. orderCode = [self translateSingleQuote:orderCode];
  7084. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes from offline_order where so_id = '%@';",orderCode];
  7085. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7086. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  7087. int mustCall = sqlite3_column_int(stmt, 1);
  7088. NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  7089. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  7090. NSDictionary *po_dic = @{
  7091. @"aname" : @"PO#",
  7092. @"control" : @"edit",
  7093. @"keyboard" : @"text",
  7094. @"name" : @"poNumber",
  7095. @"value" : poNumber
  7096. };
  7097. NSDictionary *must_call_dic = @{
  7098. @"aname" : @"MUST CALL BEFORE DELIVERY",
  7099. @"control" : @"switch",
  7100. @"name" : @"must_call",
  7101. @"value" : mustCall ? @"true" : @"false"
  7102. };
  7103. NSDictionary *general_notes_dic = @{
  7104. @"aname" : @"General notes",
  7105. @"control" : @"text_view",
  7106. @"keyboard" : @"text",
  7107. @"name" : @"comments",
  7108. @"value" : generalNotes
  7109. };
  7110. NSDictionary *internal_notes_dic = @{
  7111. @"aname" : @"Internal notes",
  7112. @"control" : @"text_view",
  7113. @"keyboard" : @"text",
  7114. @"name" : @"internal_notes",
  7115. @"value" : internalNotes
  7116. };
  7117. [dic setValue:po_dic forKey:@"item_0"];
  7118. [dic setValue:must_call_dic forKey:@"item_1"];
  7119. [dic setValue:general_notes_dic forKey:@"item_2"];
  7120. [dic setValue:internal_notes_dic forKey:@"item_3"];
  7121. }];
  7122. return dic;
  7123. }
  7124. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  7125. // params
  7126. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7127. orderCode = [self translateSingleQuote:orderCode];
  7128. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7129. // setting
  7130. NSDictionary *setting = params[@"setting"];
  7131. NSNumber *hide = setting[@"OrderTotalHide"];
  7132. [dic setValue:hide forKey:@"hide"];
  7133. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  7134. __block double lift_gate_value = 0;
  7135. __block double handling_fee = 0;
  7136. __block double shipping = 0;
  7137. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7138. int lift_gate = sqlite3_column_int(stmt, 0);
  7139. lift_gate_value = sqlite3_column_double(stmt, 1);
  7140. shipping = sqlite3_column_double(stmt, 2);
  7141. handling_fee = sqlite3_column_double(stmt, 3);
  7142. if (!lift_gate) {
  7143. lift_gate_value = 0;
  7144. }
  7145. }];
  7146. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  7147. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  7148. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  7149. double payments = [[total valueForKey:@"payments"] doubleValue];
  7150. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  7151. double totalPrice = payments;
  7152. [dic setValue:@"Order Total" forKey:@"title"];
  7153. NSDictionary *payments_dic = @{
  7154. @"align" : @"right",
  7155. @"aname" : @"Payments/Credits",
  7156. @"control" : @"text",
  7157. @"name" : @"paymentsAndCredits",
  7158. @"type" : @"price",
  7159. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  7160. };
  7161. [dic setValue:payments_dic forKey:@"item_0"];
  7162. NSDictionary *handling_fee_dic = @{
  7163. @"align" : @"right",
  7164. @"aname" : @"Handling Fee",
  7165. @"control" : @"text",
  7166. @"name" : @"handling_fee_value",
  7167. @"required" : @"true",
  7168. @"type" : @"price",
  7169. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  7170. };
  7171. NSDictionary *shipping_dic = @{
  7172. @"align" : @"right",
  7173. @"aname" : @"Shipping*",
  7174. @"control" : @"text",
  7175. @"name" : @"shipping",
  7176. @"required" : @"true",
  7177. @"type" : @"price",
  7178. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  7179. };
  7180. NSDictionary *lift_gate_dic = @{
  7181. @"align" : @"right",
  7182. @"aname" : @"Liftgate Fee(No Loading Dock)",
  7183. @"control" : @"text",
  7184. @"name" : @"lift_gate_value",
  7185. @"required" : @"true",
  7186. @"type" : @"price",
  7187. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  7188. };
  7189. int item_count = 1;
  7190. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  7191. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  7192. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7193. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7194. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  7195. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7196. } else {
  7197. }
  7198. [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7199. NSDictionary *total_price_dic = @{
  7200. @"align" : @"right",
  7201. @"aname" : @"Total",
  7202. @"control" : @"text",
  7203. @"name" : @"totalPrice",
  7204. @"type" : @"price",
  7205. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  7206. };
  7207. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7208. NSDictionary *total_cuft_dic = @{
  7209. @"align" : @"right",
  7210. @"aname" : @"Total Cuft",
  7211. @"control" : @"text",
  7212. @"name" : @"",
  7213. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  7214. };
  7215. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7216. NSDictionary *total_weight_dic = @{
  7217. @"align" : @"right",
  7218. @"aname" : @"Total Weight",
  7219. @"control" : @"text",
  7220. @"name" : @"",
  7221. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  7222. };
  7223. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7224. NSDictionary *total_carton_dic = @{
  7225. @"align" : @"right",
  7226. @"aname" : @"Total Carton",
  7227. @"control" : @"text",
  7228. @"name" : @"",
  7229. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  7230. };
  7231. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7232. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  7233. return dic;
  7234. }
  7235. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  7236. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7237. orderCode = [self translateSingleQuote:orderCode];
  7238. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7239. // setting
  7240. NSDictionary *setting = params[@"setting"];
  7241. NSNumber *hide = setting[@"SignatureHide"];
  7242. [dic setValue:hide forKey:@"hide"];
  7243. [dic setValue:@"Signature" forKey:@"title"];
  7244. __block NSString *pic_path = @"";
  7245. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  7246. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7247. pic_path = [self textAtColumn:0 statement:stmt];
  7248. }];
  7249. NSDictionary *pic_dic = @{
  7250. @"aname" : @"Signature",
  7251. @"avalue" :pic_path,
  7252. @"control" : @"signature",
  7253. @"name" : @"sign_picpath",
  7254. @"value" : pic_path
  7255. };
  7256. [dic setValue:pic_dic forKey:@"item_0"];
  7257. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7258. return dic;
  7259. }
  7260. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  7261. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7262. // setting
  7263. NSDictionary *setting = params[@"setting"];
  7264. NSNumber *hide = setting[@"ShippingMethodHide"];
  7265. [dic setValue:hide forKey:@"hide"];
  7266. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7267. orderCode = [self translateSingleQuote:orderCode];
  7268. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  7269. __block NSString *logist = @"";
  7270. __block NSString *lift_gate = @"";
  7271. __block int lift_gate_integer = 0;
  7272. __block NSString *logistic_note = @"";
  7273. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7274. logist = [self textAtColumn:0 statement:stmt];
  7275. lift_gate_integer = sqlite3_column_int(stmt, 1);
  7276. logistic_note = [self textAtColumn:2 statement:stmt];
  7277. }];
  7278. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7279. [dic setValue:@"Shipping Method" forKey:@"title"];
  7280. // val_0
  7281. int PERSONAL_PICK_UP_check = 0;
  7282. int USE_MY_CARRIER_check = 0;
  7283. NSString *logistic_note_text = @"";
  7284. int will_call_check = 0;
  7285. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  7286. will_call_check = 1;
  7287. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  7288. PERSONAL_PICK_UP_check = 1;
  7289. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  7290. USE_MY_CARRIER_check = 1;
  7291. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  7292. logistic_note = [logistic_note_array firstObject];
  7293. if (logistic_note_array.count > 1) {
  7294. logistic_note_text = [logistic_note_array lastObject];
  7295. }
  7296. }
  7297. }
  7298. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  7299. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  7300. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  7301. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  7302. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  7303. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7304. NSDictionary *val0_subItem_item0 = @{
  7305. @"aname" : @"Option",
  7306. @"cadedate" : @{
  7307. @"count" : [NSNumber numberWithInteger:2],
  7308. @"val_0" : @{
  7309. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  7310. @"refresh" : [NSNumber numberWithInteger:0],
  7311. @"value" : @"PERSONAL PICK UP",
  7312. @"value_id" : @"PERSONAL PICK UP"
  7313. },
  7314. @"val_1" : @{
  7315. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  7316. @"refresh" : [NSNumber numberWithInteger:0],
  7317. @"sub_item" : @{
  7318. @"count" : [NSNumber numberWithInteger:1],
  7319. @"item_0" : @{
  7320. @"aname" : @"BOL",
  7321. @"control" : @"edit",
  7322. @"keyboard" : @"text",
  7323. @"name" : @"logist_note_text",
  7324. @"refresh" : [NSNumber numberWithInteger:0],
  7325. @"required" : @"false",
  7326. @"value" : logistic_note_text
  7327. }
  7328. },
  7329. @"value" : @"USE MY CARRIER",
  7330. @"value_id" : @"USE MY CARRIER"
  7331. }
  7332. },
  7333. @"control" : @"enum",
  7334. @"name" : @"logistic_note",
  7335. @"required" : @"true",
  7336. @"single_select" : @"true"
  7337. };
  7338. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  7339. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  7340. // val_1
  7341. lift_gate = [self valueInParams:params key:@"lift_gate"];
  7342. if([lift_gate isEqualToString:@""]) {
  7343. if (lift_gate_integer == 1) {
  7344. lift_gate = @"true";
  7345. } else {
  7346. lift_gate = @"false";
  7347. }
  7348. }
  7349. int common_carrier_check = 0;
  7350. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  7351. common_carrier_check = 1;
  7352. [params setValue:lift_gate forKey:@"lift_gate"];
  7353. }
  7354. NSDictionary *val_1 = @{
  7355. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  7356. @"sub_item" : @{
  7357. @"count" : [NSNumber numberWithInteger:1],
  7358. @"item_0" : @{
  7359. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  7360. @"control" : @"switch",
  7361. @"name" : @"lift_gate",
  7362. @"refresh" : [NSNumber numberWithInteger:1],
  7363. @"required" : @"true",
  7364. @"value" : lift_gate
  7365. }
  7366. },
  7367. @"value" : @"COMMON CARRIER",
  7368. @"value_id" : @"COMMON CARRIER"
  7369. };
  7370. // cadedate
  7371. NSDictionary *cadedate = @{
  7372. @"count" : [NSNumber numberWithInteger:2],
  7373. @"val_0" : val_0,
  7374. @"val_1" : val_1
  7375. };
  7376. // item_0
  7377. NSMutableDictionary *item_0 = @{
  7378. @"aname" : @"Shipping",
  7379. @"cadedate" : cadedate,
  7380. @"control" : @"enum",
  7381. @"name" : @"logist",
  7382. @"refresh" : [NSNumber numberWithInteger:1],
  7383. @"single_select" : @"true",
  7384. }.mutableCopy;
  7385. NSNumber *required = setting[@"ShippingMethodRequire"];
  7386. if ([required integerValue]) {
  7387. [item_0 setValue:@"true" forKey:@"required"];
  7388. }
  7389. [dic setValue:item_0 forKey:@"item_0"];
  7390. return dic;
  7391. }
  7392. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  7393. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7394. orderCode = [self translateSingleQuote:orderCode];
  7395. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  7396. __block int submit_as_integer = 0;
  7397. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7398. submit_as_integer = sqlite3_column_int(stmt, 0);
  7399. }];
  7400. int check11 = 0;
  7401. int check10 = 0;
  7402. if (submit_as_integer == 11) {
  7403. check11 = 1;
  7404. }
  7405. if (submit_as_integer == 10) {
  7406. check10 = 1;
  7407. }
  7408. // section_0
  7409. NSMutableDictionary *dic = @{
  7410. @"count" : @(1),
  7411. @"item_0" : @{
  7412. @"aname" : @"Submit Order As",
  7413. @"cadedate" : @{
  7414. @"count" : @(2),
  7415. @"val_0" : @{
  7416. @"check" : [NSNumber numberWithInteger:check11],
  7417. @"value" : @"Sales Order",
  7418. @"value_id" : @(11)
  7419. },
  7420. @"val_1" : @{
  7421. @"check" : [NSNumber numberWithInteger:check10],
  7422. @"value" : @"Quote",
  7423. @"value_id" : @(10)
  7424. }
  7425. },
  7426. @"control" : @"enum",
  7427. @"name" : @"erpOrderStatus",
  7428. @"required" : @"true",
  7429. @"single_select" : @"true"
  7430. },
  7431. @"title" : @"Order Type"
  7432. }.mutableCopy;
  7433. // setting
  7434. NSDictionary *setting = params[@"setting"];
  7435. NSNumber *hide = setting[@"OrderTypeHide"];
  7436. [dic setValue:hide forKey:@"hide"];
  7437. return dic;
  7438. }
  7439. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  7440. countryCode = [self translateSingleQuote:countryCode];
  7441. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  7442. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7443. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  7444. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  7445. if (name == NULL) {
  7446. name = "";
  7447. }
  7448. if (code == NULL) {
  7449. code = "";
  7450. }
  7451. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  7452. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  7453. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  7454. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  7455. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  7456. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  7457. }
  7458. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  7459. [container setValue:stateDic forKey:key];
  7460. }] mutableCopy];
  7461. [ret removeObjectForKey:@"result"];
  7462. // failure 可以不用了,一样的
  7463. if (ret.allKeys.count == 0) {
  7464. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  7465. [stateDic setValue:@"Other" forKey:@"value"];
  7466. [stateDic setValue:@"" forKey:@"value_id"];
  7467. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  7468. if (state_code && [@"" isEqualToString:state_code]) {
  7469. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  7470. }
  7471. NSString *key = [NSString stringWithFormat:@"val_0"];
  7472. [ret setValue:stateDic forKey:key];
  7473. }
  7474. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  7475. return ret;
  7476. }
  7477. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  7478. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7479. orderCode = [self translateSingleQuote:orderCode];
  7480. NSString *sql = [NSString stringWithFormat:@"select paymentType,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,decrypt(credit_card_number),decrypt(credit_card_security_code),decrypt(credit_card_expiration_month),decrypt(credit_card_expiration_year),credit_card_city,credit_card_state from offline_order where so_id = '%@';",orderCode];
  7481. __block NSString *payType = @"";
  7482. __block NSString *firstName = @"";
  7483. __block NSString *lastName = @"";
  7484. __block NSString *addr1 = @"";
  7485. __block NSString *addr2 = @"";
  7486. __block NSString *zipcode = @"";
  7487. __block NSString *cardType = @"";
  7488. __block NSString *cardNumber = @"";
  7489. __block NSString *securityCode = @"";
  7490. __block NSString *month = @"";
  7491. __block NSString *year = @"";
  7492. __block NSString *city = @"";
  7493. __block NSString *state = @"";
  7494. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7495. payType = [self textAtColumn:0 statement:stmt];
  7496. firstName = [self textAtColumn:1 statement:stmt];
  7497. lastName = [self textAtColumn:2 statement:stmt];
  7498. addr1 = [self textAtColumn:3 statement:stmt];
  7499. addr2 = [self textAtColumn:4 statement:stmt];
  7500. zipcode = [self textAtColumn:5 statement:stmt];
  7501. cardType = [self textAtColumn:6 statement:stmt];
  7502. cardNumber = [self textAtColumn:7 statement:stmt];
  7503. securityCode = [self textAtColumn:8 statement:stmt];
  7504. month = [self textAtColumn:9 statement:stmt];
  7505. year = [self textAtColumn:10 statement:stmt];
  7506. city = [self textAtColumn:11 statement:stmt];
  7507. state = [self textAtColumn:12 statement:stmt];
  7508. }];
  7509. NSString *required = @"true";
  7510. // setting
  7511. NSDictionary *setting = params[@"setting"];
  7512. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  7513. if ([requiredNumber integerValue]) {
  7514. required = @"true";
  7515. } else {
  7516. required = @"false";
  7517. }
  7518. // "section_2"
  7519. NSMutableDictionary *dic = @{
  7520. @"count" : @(1),
  7521. @"item_0" : @{
  7522. @"aname" : @"Payment",
  7523. @"required" : required,
  7524. @"cadedate" : @{
  7525. @"count" : @(6),
  7526. @"val_0" : @{
  7527. @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  7528. @"value" : @"Check",
  7529. @"value_id" : @"Check"
  7530. },
  7531. @"val_1" : @{
  7532. @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  7533. @"value" : @"Cash",
  7534. @"value_id" : @"Cash"
  7535. },
  7536. @"val_2" : @{
  7537. @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  7538. @"value" : @"NET 30",
  7539. @"value_id" : @"NET 30"
  7540. },
  7541. @"val_3" : @{
  7542. @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  7543. @"value" : @"Wire Transfer",
  7544. @"value_id" : @"Wire Transfer"
  7545. },
  7546. @"val_4" : @{
  7547. @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  7548. @"sub_item" : @{
  7549. @"count" : @(3),
  7550. @"item_0" : @{
  7551. @"aname" : @"choose",
  7552. @"control" : @"multi_action",
  7553. @"count" : @(1),
  7554. @"item_0" : @{
  7555. @"aname" : @"Same as customer",
  7556. @"key_map" : @{
  7557. @"credit_card_address1" : @"customer_address1",
  7558. @"credit_card_address2" : @"customer_address2",
  7559. @"credit_card_city" : @"customer_city",
  7560. @"credit_card_first_name" : @"customer_first_name",
  7561. @"credit_card_last_name" : @"customer_last_name",
  7562. @"credit_card_state" : @"customer_state",
  7563. @"credit_card_zipcode" : @"customer_zipcode"
  7564. },
  7565. @"type" : @"pull"
  7566. }
  7567. },
  7568. @"item_1" : @{
  7569. @"aname" : @"",
  7570. @"color" : @"red",
  7571. @"control" : @"text",
  7572. @"name" : @"",
  7573. @"value" : @"USA Credit cards only"
  7574. },
  7575. @"item_2" : @{
  7576. @"aname" : @"Fill",
  7577. @"cadedate" : @{
  7578. @"count" : @(2),
  7579. @"val_0" : @{
  7580. @"check" : @(1),
  7581. @"sub_item" : @{
  7582. @"count" : @(11),
  7583. @"item_0" : @{
  7584. @"aname" : @"Type",
  7585. @"cadedate" : @{
  7586. @"count" : @(2),
  7587. @"val_0" : @{
  7588. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  7589. @"value" : @"VISA",
  7590. @"value_id" : @(0)
  7591. },
  7592. @"val_1" : @{
  7593. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  7594. @"value" : @"MASTER CARD",
  7595. @"value_id" : @(1)
  7596. }
  7597. },
  7598. @"control" : @"enum",
  7599. @"name" : @"credit_card_type",
  7600. @"required" : @"true",
  7601. @"single_select" : @"true"
  7602. },
  7603. @"item_1" : @{
  7604. @"aname" : @"Number",
  7605. @"control" : @"edit",
  7606. @"keyboard" : @"int",
  7607. @"length" : @"16",
  7608. @"name" : @"credit_card_number",
  7609. @"required" : @"true",
  7610. @"value" : cardNumber
  7611. },
  7612. @"item_10" : @{
  7613. @"aname" : @"State",
  7614. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  7615. @"control" : @"enum",
  7616. @"enum" : @"true",
  7617. @"name" : @"credit_card_state",
  7618. @"required" : @"true",
  7619. @"single_select" : @"true"
  7620. },
  7621. @"item_2" : @{
  7622. @"aname" : @"Expiration Date",
  7623. @"control" : @"monthpicker",
  7624. @"name" : @"credit_card_expiration",
  7625. @"required" : @"true",
  7626. @"type" : @"date",
  7627. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  7628. },
  7629. @"item_3" : @{
  7630. @"aname" : @"Security Code",
  7631. @"control" : @"edit",
  7632. @"keyboard" : @"int",
  7633. @"length" : @"3",
  7634. @"name" : @"credit_card_security_code",
  7635. @"required" : @"true",
  7636. @"value" : securityCode
  7637. },
  7638. @"item_4" : @{
  7639. @"aname" : @"First Name",
  7640. @"control" : @"edit",
  7641. @"keyboard" : @"text",
  7642. @"name" : @"credit_card_first_name",
  7643. @"required" : @"true",
  7644. @"value" : firstName
  7645. },
  7646. @"item_5" : @{
  7647. @"aname" : @"Last Name",
  7648. @"control" : @"edit",
  7649. @"keyboard" : @"text",
  7650. @"name" : @"credit_card_last_name",
  7651. @"required" : @"true",
  7652. @"value" : lastName
  7653. },
  7654. @"item_6" : @{
  7655. @"aname" : @"Address 1",
  7656. @"control" : @"edit",
  7657. @"keyboard" : @"text",
  7658. @"name" : @"credit_card_address1",
  7659. @"required" : @"true",
  7660. @"value" : addr1
  7661. },
  7662. @"item_7" : @{
  7663. @"aname" : @"Address 2",
  7664. @"control" : @"edit",
  7665. @"keyboard" : @"text",
  7666. @"name" : @"credit_card_address2",
  7667. @"value" : addr2
  7668. },
  7669. @"item_8" : @{
  7670. @"aname" : @"zip code",
  7671. @"control" : @"edit",
  7672. @"keyboard" : @"text",
  7673. @"name" : @"credit_card_zipcode",
  7674. @"required" : @"true",
  7675. @"value" : zipcode
  7676. },
  7677. @"item_9" : @{
  7678. @"aname" : @"City",
  7679. @"control" : @"edit",
  7680. @"keyboard" : @"text",
  7681. @"name" : @"credit_card_city",
  7682. @"required" : @"true",
  7683. @"value" : city
  7684. }
  7685. },
  7686. @"value" : @"Fill Now",
  7687. @"value_id" : @""
  7688. },
  7689. @"val_1" : @{
  7690. @"check" : @(0),
  7691. @"value" : @"Fill Later",
  7692. @"value_id" : @""
  7693. }
  7694. },
  7695. @"control" : @"enum",
  7696. @"name" : @"",
  7697. @"single_select" : @"true"
  7698. }
  7699. },
  7700. @"value" : @"Credit Card",
  7701. @"value_id" : @"Credit Card"
  7702. },
  7703. @"val_5" : @{
  7704. @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  7705. @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  7706. @"value_id" : @"FOLLOW EXISTING"
  7707. }
  7708. },
  7709. @"control" : @"enum",
  7710. @"name" : @"paymentType",
  7711. @"single_select" : @"true"
  7712. },
  7713. @"title" : @"Payment Information"
  7714. }.mutableCopy;
  7715. NSNumber *hide = setting[@"PaymentInformationHide"];
  7716. [dic setValue:hide forKey:@"hide"];
  7717. return dic;
  7718. }
  7719. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  7720. {
  7721. sqlite3 *db = [iSalesDB get_db];
  7722. // UISetting
  7723. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  7724. NSString *cachefolder = [paths objectAtIndex:0];
  7725. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  7726. NSData* json =nil;
  7727. json=[NSData dataWithContentsOfFile:img_cache];
  7728. NSError *error=nil;
  7729. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  7730. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  7731. [params setObject:setting forKey:@"setting"];
  7732. int section_count = 0;
  7733. // params
  7734. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7735. orderCode = [self translateSingleQuote:orderCode];
  7736. // 0 Order Type 1 Shipping Method 2 Payment Information
  7737. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  7738. // 0 Order Type
  7739. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  7740. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  7741. [ret setValue:order_type_dic forKey:key0];
  7742. // 1 Shipping Method
  7743. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  7744. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  7745. [ret setValue:shipping_method_dic forKey:key1];
  7746. // 2 Payment Information
  7747. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  7748. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  7749. [ret setValue:payment_info_dic forKey:key2];
  7750. // 3 Customer
  7751. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  7752. NSDictionary *customer_dic = [self customerDic:params db:db];
  7753. [ret setValue:customer_dic forKey:key3];
  7754. // 4 Ship To
  7755. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  7756. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  7757. [ret setValue:ship_to_dic forKey:key4];
  7758. // 5 Ship From
  7759. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  7760. if (![shipFromDisable integerValue]) {
  7761. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  7762. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  7763. [ret setValue:ship_from_dic forKey:key5];
  7764. }
  7765. // 6 Freight Bill To
  7766. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  7767. if (![freightBillToDisable integerValue]) {
  7768. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  7769. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  7770. [ret setValue:freight_bill_to forKey:key6];
  7771. }
  7772. // 7 Merchandise Bill To
  7773. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  7774. if (![merchandiseBillToDisable integerValue]) {
  7775. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  7776. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  7777. [ret setValue:merchandise_bill_to_dic forKey:key7];
  7778. }
  7779. // 8 Return To
  7780. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  7781. if (![returnToDisable integerValue]) {
  7782. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  7783. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  7784. [ret setValue:return_to_dic forKey:key8];
  7785. }
  7786. // 9 Model Information
  7787. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  7788. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  7789. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  7790. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  7791. [ret setValue:model_info_dic forKey:key9];
  7792. // 10 Remarks Content
  7793. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  7794. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  7795. [ret setValue:remarks_content_dic forKey:key10];
  7796. // 11 Order Total
  7797. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  7798. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  7799. [ret setValue:order_total_dic forKey:key11];
  7800. // 12 Signature
  7801. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  7802. NSDictionary *sign_dic = [self signatureDic:params db:db];
  7803. [ret setValue:sign_dic forKey:key12];
  7804. [ret setValue:@(section_count) forKey:@"section_count"];
  7805. [iSalesDB close_db:db];
  7806. return [RAUtils dict2data:ret];
  7807. // return nil;
  7808. }
  7809. #pragma mark addr editor
  7810. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  7811. NSMutableDictionary *new_item = [item mutableCopy];
  7812. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  7813. return new_item;
  7814. }
  7815. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  7816. {
  7817. // "is_subaction" = true;
  7818. // orderCode = MOB1608240002;
  7819. // password = 123456;
  7820. // "subaction_tag" = 1;
  7821. // user = EvanK;
  7822. // {
  7823. // "is_subaction" = true;
  7824. // orderCode = MOB1608240002;
  7825. // password = 123456;
  7826. // "refresh_trigger" = zipcode;
  7827. // "subaction_tag" = 1;
  7828. // user = EvanK;
  7829. // }
  7830. NSString *country_code = nil;
  7831. NSString *zipCode = nil;
  7832. NSString *stateCode = nil;
  7833. NSString *city = nil;
  7834. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  7835. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  7836. NSString *code_id = params[@"country"];
  7837. country_code = [self countryCodeByid:code_id];
  7838. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  7839. NSString *zip_code = params[@"zipcode"];
  7840. // 剔除全部为空格
  7841. int spaceCount = 0;
  7842. for (int i = 0; i < zip_code.length; i++) {
  7843. if ([zip_code characterAtIndex:i] == ' ') {
  7844. spaceCount++;
  7845. }
  7846. }
  7847. if (spaceCount == zip_code.length) {
  7848. zip_code = @"";
  7849. }
  7850. zipCode = zip_code;
  7851. if (zipCode.length > 0) {
  7852. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  7853. country_code = [dic valueForKey:@"country_code"];
  7854. if (!country_code) {
  7855. // country_code = @"US";
  7856. NSString *code_id = params[@"country"];
  7857. country_code = [self countryCodeByid:code_id];
  7858. }
  7859. stateCode = [dic valueForKey:@"state_code"];
  7860. if(!stateCode.length) {
  7861. stateCode = params[@"state"];
  7862. }
  7863. city = [dic valueForKey:@"city"];
  7864. if (!city.length) {
  7865. city = params[@"city"];
  7866. }
  7867. // zip code
  7868. // NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  7869. // [zipDic setValue:zipCode forKey:@"value"];
  7870. // [section_0 setValue:zipDic forKey:@"item_11"];
  7871. } else {
  7872. NSString *code_id = params[@"country"];
  7873. country_code = [self countryCodeByid:code_id];
  7874. stateCode = params[@"state"];
  7875. city = params[@"city"];
  7876. }
  7877. }
  7878. }
  7879. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  7880. // [ret removeObjectForKey:@"up_params"];
  7881. [ret setObject:@"New Address" forKey:@"title"];
  7882. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  7883. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  7884. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  7885. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  7886. // [country_dic removeObjectForKey:@"refresh"];
  7887. // [country_dic removeObjectForKey:@"restore"];
  7888. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  7889. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  7890. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  7891. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  7892. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  7893. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  7894. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  7895. // [zip_code_dic removeObjectForKey:@"refresh"];
  7896. [zip_code_dic setValue:zipCode ? zipCode :@"" forKey:@"value"];
  7897. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  7898. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  7899. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  7900. NSDictionary *cityDic = [[new_section_0 objectForKey:@"item_8"] mutableCopy];
  7901. [cityDic setValue:city ? city : @"" forKey:@"value"];
  7902. [new_section_0 setObject:cityDic forKey:@"item_8"];
  7903. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  7904. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  7905. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  7906. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  7907. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  7908. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  7909. // country
  7910. NSString *countryCode = country_code == nil ? @"US" : country_code;
  7911. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  7912. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  7913. // state
  7914. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:stateCode] mutableCopy];
  7915. // NSDictionary *tmpDic = @{
  7916. // @"value" : @"Other",
  7917. // @"value_id" : @"",
  7918. // @"check" : [NSNumber numberWithInteger:0]
  7919. // };
  7920. //
  7921. // for (int i = 0; i < allState.allKeys.count; i++) {
  7922. // NSString *key = [NSString stringWithFormat:@"val_%d",i];
  7923. //
  7924. // NSDictionary *tmp = allState[key];
  7925. // [allState setValue:tmpDic forKey:key];
  7926. // tmpDic = tmp;
  7927. // }
  7928. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  7929. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  7930. [ret setValue:new_section_0 forKey:@"section_0"];
  7931. return [RAUtils dict2data:ret];
  7932. }
  7933. #pragma mark save addr
  7934. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  7935. {
  7936. // NSString *companyName = [self valueInParams:params key:@"company"];
  7937. // NSString *addr1 = [self valueInParams:params key:@"address"];
  7938. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  7939. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  7940. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  7941. // NSString *countryId = [self valueInParams:params key:@"country"];
  7942. // NSString *stateCode = [self valueInParams:params key:@"state"];
  7943. // NSString *city = [self valueInParams:params key:@"city"];
  7944. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  7945. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  7946. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  7947. // NSString *phone = [self valueInParams:params key:@"phone"];
  7948. // NSString *fax = [self valueInParams:params key:@"fax"];
  7949. // NSString *email = [self valueInParams:params key:@"email"];
  7950. return [self offline_saveContact:params update:NO isCustomer:NO];
  7951. }
  7952. #pragma mark cancel order
  7953. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  7954. {
  7955. // NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7956. NSString *order_id = [self valueInParams:params key:@"orderId"];
  7957. // NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  7958. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  7959. int ret = [iSalesDB execSql:sql];
  7960. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7961. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7962. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7963. // [dic setValue:@"160409" forKey:@"min_ver"];
  7964. return [RAUtils dict2data:dic];
  7965. }
  7966. #pragma mark sign order
  7967. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  7968. {
  7969. //参考 offline_saveBusinesscard
  7970. DebugLog(@"sign order params: %@",params);
  7971. // orderCode = MOB1608240002;
  7972. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  7973. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7974. orderCode = [self translateSingleQuote:orderCode];
  7975. NSString *picPath = [self valueInParams:params key:@"picpath"];
  7976. picPath = [self translateSingleQuote:picPath];
  7977. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  7978. int ret = [iSalesDB execSql:sql];
  7979. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7980. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7981. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7982. // [dic setValue:@"160409" forKey:@"min_ver"];
  7983. return [RAUtils dict2data:dic];
  7984. }
  7985. #pragma mark save order
  7986. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  7987. NSString *ret = [self valueInParams:params key:key];
  7988. if (translate) {
  7989. ret = [self translateSingleQuote:ret];
  7990. }
  7991. return ret;
  7992. }
  7993. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  7994. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  7995. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  7996. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  7997. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  7998. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  7999. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  8000. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  8001. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  8002. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  8003. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  8004. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  8005. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  8006. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  8007. if (![total_price isEqualToString:@""]) {
  8008. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  8009. } else {
  8010. total_price = @"";
  8011. }
  8012. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  8013. if ([paymentsAndCredits isEqualToString:@""]) {
  8014. paymentsAndCredits = @"";
  8015. } else {
  8016. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  8017. }
  8018. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  8019. if ([handling_fee_value isEqualToString:@""]) {
  8020. handling_fee_value = @"";
  8021. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  8022. handling_fee_value = @"";
  8023. } else {
  8024. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  8025. }
  8026. NSString *handling_fee_placeholder = handling_fee_value;
  8027. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  8028. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  8029. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  8030. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  8031. NSString *lift_gate_placeholder = @"";
  8032. if ([lift_gate_value isEqualToString:@""]) {
  8033. lift_gate_placeholder = @"";
  8034. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  8035. lift_gate_placeholder = @"";
  8036. } else {
  8037. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  8038. }
  8039. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  8040. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  8041. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  8042. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  8043. logistic_note = [logistic_note isEqualToString:@"USE MY CARRIER"] ? [NSString stringWithFormat:@"logistic_note = '%@\n\t%@',",logistic_note,logistic_note_text] : [NSString stringWithFormat:@"logistic_note = '%@',",logistic_note];
  8044. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  8045. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  8046. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  8047. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  8048. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  8049. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  8050. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  8051. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  8052. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  8053. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  8054. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  8055. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  8056. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  8057. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  8058. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  8059. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  8060. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  8061. BOOL number_nil = [credit_card_number isEqualToString:@""];
  8062. if (!number_nil) {
  8063. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  8064. }
  8065. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  8066. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  8067. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  8068. if (!security_code_nil) {
  8069. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  8070. }
  8071. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  8072. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  8073. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  8074. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  8075. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  8076. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  8077. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  8078. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  8079. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  8080. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  8081. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  8082. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  8083. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  8084. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  8085. NSString *contact_id = customer_cid;
  8086. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  8087. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  8088. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  8089. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  8090. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  8091. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  8092. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  8093. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  8094. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  8095. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  8096. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  8097. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  8098. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  8099. //
  8100. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  8101. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  8102. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  8103. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  8104. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  8105. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  8106. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  8107. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  8108. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  8109. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  8110. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  8111. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  8112. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  8113. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  8114. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  8115. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  8116. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  8117. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  8118. NSString *contactSql = [NSString stringWithFormat:@"update offline_contact set %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ where contact_id = '%@'",contact_contact,contact_email,contact_phone,contact_fax,customer_name,customer_address1,customer_address2,customer_address3,customer_address4,customer_country,customer_state,customer_city,customer_zipcode,contact_id];
  8119. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  8120. if (receive_cid.length) {
  8121. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  8122. }
  8123. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  8124. if (receive_name.length) {
  8125. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  8126. }
  8127. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  8128. if (receive_ext.length) {
  8129. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  8130. }
  8131. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  8132. if (receive_contact.length) {
  8133. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  8134. }
  8135. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  8136. if (receive_phone.length) {
  8137. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  8138. }
  8139. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  8140. if (receive_email.length) {
  8141. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  8142. }
  8143. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  8144. if (receive_fax.length) {
  8145. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  8146. }
  8147. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  8148. if (sender_cid.length) {
  8149. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  8150. }
  8151. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  8152. if (sender_name.length) {
  8153. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  8154. }
  8155. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  8156. if (sender_ext.length) {
  8157. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  8158. }
  8159. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  8160. if(sender_contact.length) {
  8161. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  8162. }
  8163. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  8164. if (sender_phone.length) {
  8165. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  8166. }
  8167. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  8168. if (sender_fax.length) {
  8169. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  8170. }
  8171. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  8172. if (sender_email.length) {
  8173. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  8174. }
  8175. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  8176. if (shipping_billto_cid.length) {
  8177. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  8178. }
  8179. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  8180. if (shipping_billto_name.length) {
  8181. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  8182. }
  8183. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  8184. if (shipping_billto_ext.length) {
  8185. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  8186. }
  8187. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  8188. if (shipping_billto_contact.length) {
  8189. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  8190. }
  8191. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  8192. if (shipping_billto_phone.length) {
  8193. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  8194. }
  8195. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  8196. if (shipping_billto_fax.length) {
  8197. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  8198. }
  8199. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  8200. if (shipping_billto_email.length) {
  8201. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  8202. }
  8203. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  8204. if (billing_cid.length) {
  8205. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  8206. }
  8207. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  8208. if (billing_name.length) {
  8209. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  8210. }
  8211. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  8212. if (billing_ext.length) {
  8213. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  8214. }
  8215. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  8216. if (billing_contact.length) {
  8217. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  8218. }
  8219. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  8220. if (billing_phone.length) {
  8221. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  8222. }
  8223. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  8224. if (billing_fax.length) {
  8225. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  8226. }
  8227. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  8228. if (billing_email.length) {
  8229. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  8230. }
  8231. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  8232. if (returnto_cid.length) {
  8233. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  8234. }
  8235. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  8236. if (returnto_name.length) {
  8237. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  8238. }
  8239. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  8240. if (returnto_ext.length) {
  8241. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  8242. }
  8243. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  8244. if (returnto_contact.length) {
  8245. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  8246. }
  8247. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  8248. if (returnto_phone.length) {
  8249. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  8250. }
  8251. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  8252. if (returnto_fax.length) {
  8253. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  8254. }
  8255. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  8256. if (returnto_email.length) {
  8257. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  8258. }
  8259. NSString *order_status = @"status = 1,";
  8260. if (submit) {
  8261. order_status = @"status = -11,";
  8262. }
  8263. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  8264. NSString *sync_sql = @"";
  8265. if (submit) {
  8266. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  8267. [param setValue:sales_rep forKey:@"sales_rep"];
  8268. NSString *sync_data = [self translateSingleQuote:[RAUtils dict2string:param]];
  8269. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  8270. }
  8271. NSString *orderSql = [NSString stringWithFormat:@"update offline_order set %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ so_id = '%@' %@ where so_id = '%@';",order_status,general_notes,internal_notes,poNumber,must_call,sign_picpath,total_price,paymentsAndCredits,handling_fee_placeholder,lift_gate,lift_gate_placeholder,logist,logistic_note,erpOrderStatus,paymentType,credit_card_address1,credit_card_address2,credit_card_city,credit_card_first_name,credit_card_last_name,credit_card_number,credit_card_security_code,credit_card_state,credit_card_type,credit_card_zipcode,credit_card_expiration_year,credit_card_expiration_month,customer_cid,customer_contact,customer_email,customer_phone,customer_fax,receive_cid,receive_name,receive_ext,receive_contact,receive_phone,receive_email,receive_fax,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_phone,shipping_billto_fax,shipping_billto_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_phone,returnto_fax,returnto_email,so_id,sync_sql,so_id];
  8272. DebugLog(@"save order contactSql: %@",contactSql);
  8273. DebugLog(@"save order orderSql: %@",orderSql);
  8274. // int contact_ret = [iSalesDB execSql:contactSql];
  8275. int order_ret = [iSalesDB execSql:orderSql];
  8276. int ret = order_ret;
  8277. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8278. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8279. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8280. // [dic setValue:@"160409" forKey:@"min_ver"];
  8281. [dic setObject:so_id forKey:@"so#"];
  8282. return [RAUtils dict2data:dic];
  8283. }
  8284. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  8285. {
  8286. // id foo = nil;
  8287. // NSMutableArray *a = @[].mutableCopy;
  8288. // [a addObject:foo];
  8289. return [self saveorder:param submit:NO];
  8290. }
  8291. #pragma mark add to cart by name
  8292. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  8293. {
  8294. NSString *orderCode = [params objectForKey:@"orderCode"];
  8295. NSString *product_name = [params objectForKey:@"product_name"];
  8296. product_name = [self translateSingleQuote:product_name];
  8297. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  8298. sqlite3 *db = [iSalesDB get_db];
  8299. __block NSMutableString *product_id_string = [NSMutableString string];
  8300. for (int i = 0; i < product_name_array.count; i++) {
  8301. NSString *name = [product_name_array objectAtIndex:i];
  8302. NSString *sql = [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  8303. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8304. int product_id = sqlite3_column_int(stmt, 0);
  8305. if (i == product_name_array.count - 1) {
  8306. [product_id_string appendFormat:@"%d",product_id];
  8307. } else {
  8308. [product_id_string appendFormat:@"%d,",product_id];
  8309. }
  8310. }];
  8311. }
  8312. NSDictionary *newParams = @{
  8313. @"product_id" : product_id_string,
  8314. @"orderCode" : orderCode
  8315. };
  8316. [iSalesDB close_db:db];
  8317. return [self offline_add2cart:[newParams mutableCopy]];
  8318. }
  8319. #pragma mark reset order
  8320. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  8321. {
  8322. UIApplication * app = [UIApplication sharedApplication];
  8323. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  8324. appDelegate.disable_trigger=true;
  8325. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  8326. appDelegate.disable_trigger=false;
  8327. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8328. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  8329. return [RAUtils dict2data:dic];
  8330. }
  8331. #pragma mark submit order
  8332. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  8333. {
  8334. return [self saveorder:params submit:YES];
  8335. }
  8336. #pragma mark copy order
  8337. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  8338. {
  8339. NSMutableDictionary *ret = @{}.mutableCopy;
  8340. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  8341. sqlite3 *db = [iSalesDB get_db];
  8342. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  8343. // 首先查看联系人是否active
  8344. NSString *customer_sql = [NSString stringWithFormat:@"select is_active from offline_contact where contact_id in (select customer_cid from offline_order where so_id = '%@');",order_code];
  8345. __block int customer_is_active = 1;
  8346. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8347. customer_is_active = sqlite3_column_int(stmt, 0);
  8348. }];
  8349. if (!customer_is_active) {
  8350. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8351. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8352. [iSalesDB close_db:db];
  8353. return [RAUtils dict2data:ret];
  8354. }
  8355. // new order
  8356. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  8357. NSString *new_order_code = [self get_offline_soid:db];
  8358. NSString *user = ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  8359. user = [self translateSingleQuote:user];
  8360. NSString *insert_order_sql = [NSString stringWithFormat:@"insert into offline_order (so_id,status,submit_as,internal_notes,sales_rep,create_by,logist,lift_gate,logistic_note,customer_cid,customer_contact,customer_email,customer_phone,customer_fax,receive_cid,receive_name,receive_ext,receive_contact,receive_phone,receive_fax,receive_email,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_phone,shipping_billto_fax,shipping_billto_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_phone,returnto_fax,returnto_email,must_call,poNumber,comments,comments_ext,sales_terms) select '%@',1,submit_as,internal_notes,sales_rep,'%@',logist,lift_gate,logistic_note,customer_cid,customer_contact,customer_email,customer_phone,customer_fax,receive_cid,receive_name,receive_ext,receive_contact,receive_phone,receive_fax,receive_email,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_phone,shipping_billto_fax,shipping_billto_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_phone,returnto_fax,returnto_email,must_call,poNumber,comments,comments_ext,sales_terms from offline_order where so_id = '%@';",new_order_code,user,order_code]; // ,paymentsAndCredits,shipping,lift_gate_value,handling_fee_value
  8361. __block int result = 1;
  8362. result = [iSalesDB execSql:insert_order_sql db:db];
  8363. if (!result) {
  8364. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8365. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8366. [iSalesDB close_db:db];
  8367. return [RAUtils dict2data:ret];
  8368. }
  8369. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  8370. // __block NSString *product_id = @"";
  8371. __weak typeof(self) weakSelf = self;
  8372. NSString *product_id_sql = [NSString stringWithFormat:@"select product_id,is_active from product WHERE product_id in (select product_id from offline_cart where so_no = '%@');",order_code];
  8373. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8374. int is_active = sqlite3_column_int(stmt, 1);
  8375. if (is_active) {
  8376. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  8377. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  8378. NSString *insert_cart_sql = [NSString stringWithFormat:@"insert into offline_cart (so_no,orderitem_id,product_id,item_id,item_count,bundle_item,type) select '%@',orderitem_id,product_id,item_id,item_count,bundle_item,type from offline_cart where product_id = %@ and so_no = '%@'",new_order_code,product_id,order_code];
  8379. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  8380. }
  8381. }];
  8382. // product_id = [product_id substringFromIndex:1];
  8383. //
  8384. // [self offline_add2cart:@{}.mutableCopy];
  8385. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8386. [iSalesDB close_db:db];
  8387. if (result) {
  8388. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8389. } else {
  8390. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8391. }
  8392. [ret setObject:new_order_code forKey:@"so_id"];
  8393. return [RAUtils dict2data:ret];
  8394. }
  8395. #pragma mark move wish list to cart
  8396. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  8397. NSString *collectId = params[@"collectId"];
  8398. NSString *sql = [NSString stringWithFormat:@"select product_id,qty from wishlist where _id in (%@);",collectId];
  8399. __block NSString *product_id = @"";
  8400. __block NSString *qty = @"";
  8401. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8402. int productId = sqlite3_column_int(stmt, 0);
  8403. int item_qty = sqlite3_column_int(stmt, 1);
  8404. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  8405. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  8406. }];
  8407. product_id = [product_id substringFromIndex:1];
  8408. qty = [qty substringFromIndex:1];
  8409. NSDictionary *newParams = @{
  8410. @"product_id" : product_id,
  8411. @"orderCode" : params[@"orderCode"],
  8412. @"qty" : qty
  8413. };
  8414. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  8415. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  8416. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  8417. // NSString *deleteSQL = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  8418. //
  8419. // int ret = [iSalesDB execSql:deleteSQL];
  8420. int ret = [[[self offline_deletewishlist:@{@"collectId" : collectId}.mutableCopy] valueForKey:@"result"] integerValue];
  8421. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  8422. }
  8423. return [RAUtils dict2data:retDic];
  8424. }
  8425. #pragma mark - portfolio
  8426. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  8427. int sort = [[params valueForKey:@"sort"] integerValue];
  8428. int offset = [[params valueForKey:@"offset"] integerValue];
  8429. int limit = [[params valueForKey:@"limit"] integerValue];
  8430. NSString *orderBy = @"";
  8431. switch (sort) {
  8432. case 0:{
  8433. orderBy = @"modify_time desc";
  8434. }
  8435. break;
  8436. case 1:{
  8437. orderBy = @"modify_time asc";
  8438. }
  8439. break;
  8440. case 2:{
  8441. orderBy = @"name asc";
  8442. }
  8443. break;
  8444. case 3:{
  8445. orderBy = @"name desc";
  8446. }
  8447. break;
  8448. case 4:{
  8449. orderBy = @"description asc";
  8450. }
  8451. break;
  8452. default:
  8453. break;
  8454. }
  8455. NSString *sql = [NSString stringWithFormat:@"select p.product_id,p.name,p.description,ifnull(p.sheet_price,'null'),p.sheet_discount,ifnull(p.available_qty,'null'),p.percentage,p.item_id,p.fashion_id,i.url,p.line_note,p.percent from offline_portfolio as p left join model_image as i on i.product_id = p.product_id where i.type = 1 and (p.is_delete is null or p.is_delete = 0) group by p.product_id order by %@ limit %d offset %d;",orderBy,limit,offset];
  8456. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8457. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8458. sqlite3 *db = [iSalesDB get_db];
  8459. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8460. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8461. int product_id = sqlite3_column_int(stmt, 0);
  8462. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  8463. NSString *name = [self textAtColumn:1 statement:stmt];
  8464. NSString *description = [self textAtColumn:2 statement:stmt];
  8465. double price = sqlite3_column_double(stmt, 3);
  8466. double discount = sqlite3_column_double(stmt,4);
  8467. int qty = sqlite3_column_int(stmt, 5);
  8468. int percentage = sqlite3_column_int(stmt, 6);
  8469. int item_id = sqlite3_column_int(stmt, 7);
  8470. // int fashion_id = sqlite3_column_int(stmt, 8);
  8471. NSString *img_path = [self textAtColumn:9 statement:stmt];
  8472. NSString *line_note = [self textAtColumn:10 statement:stmt];
  8473. double percent = sqlite3_column_double(stmt, 11);
  8474. NSString *price_null = [self textAtColumn:3 statement:stmt];
  8475. if ([price_null isEqualToString:@"null"]) {
  8476. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  8477. }
  8478. NSMutableDictionary *item = @{
  8479. @"linenotes": line_note,
  8480. @"check": @(1),
  8481. @"product_id": product_id_string,
  8482. @"available_qty": @(qty),
  8483. @"available_percent" : @(percent),
  8484. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  8485. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  8486. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  8487. @"img": img_path,
  8488. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  8489. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  8490. }.mutableCopy;
  8491. if (percentage) {
  8492. [item removeObjectForKey:@"available_qty"];
  8493. } else {
  8494. [item removeObjectForKey:@"available_percent"];
  8495. }
  8496. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  8497. if ([qty_null isEqualToString:@"null"]) {
  8498. [item removeObjectForKey:@"available_qty"];
  8499. }
  8500. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  8501. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  8502. }];
  8503. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  8504. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8505. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  8506. }
  8507. [iSalesDB close_db:db];
  8508. [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  8509. [dic setValue:@"" forKey:@"email_content"];
  8510. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  8511. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8512. return [RAUtils dict2data:dic];
  8513. }
  8514. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  8515. __block int qty = 0;
  8516. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  8517. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8518. qty = sqlite3_column_int(stmt, 0);
  8519. }];
  8520. return qty;
  8521. }
  8522. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct {
  8523. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  8524. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  8525. NSMutableDictionary * values = params[@"replaceValue"];
  8526. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  8527. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  8528. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  8529. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  8530. NSString *pdf_path = @"";
  8531. if (direct) {
  8532. configureParams = [self valueInParams:params key:@"pdfUrl"];
  8533. } else {
  8534. pdf_path = [self valueInParams:params key:@"pdfPath"];
  8535. }
  8536. NSString *create_user = [self valueInParams:params key:@"user"];
  8537. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  8538. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  8539. // model info
  8540. NSString *sql = [NSString stringWithFormat:@"select product_id,ifnull(sheet_price,'null'),ifnull(sheet_discount,'null'),ifnull(available_qty,'null'),percentage,item_id,line_note,percent from offline_portfolio where product_id in (%@);",product_ids];
  8541. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8542. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  8543. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8544. sqlite3 *db = [iSalesDB get_db];
  8545. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8546. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8547. int product_id = sqlite3_column_int(stmt, 0);
  8548. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  8549. double price = sqlite3_column_double(stmt, 1);
  8550. double discount = sqlite3_column_double(stmt,2);
  8551. int qty = sqlite3_column_int(stmt, 3);
  8552. int percentage = sqlite3_column_int(stmt, 4);
  8553. int item_id = sqlite3_column_int(stmt, 5);
  8554. NSString *line_note = [self textAtColumn:6 statement:stmt];
  8555. double percent = sqlite3_column_double(stmt, 7);
  8556. NSString *price_null = [self textAtColumn:1 statement:stmt];
  8557. /* if ([price_null isEqualToString:@"null"]) {
  8558. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  8559. }
  8560. */
  8561. [product_ids_string appendFormat:@"%@,",product_id_string];
  8562. // Regular Price
  8563. int regular_price = [[params objectForKey:@"regular_price"] integerValue];
  8564. NSString *regular_price_str = [self get_portfolio_price:appDelegate.contact_id item_id:item_id price:regular_price db:db];
  8565. // QTY
  8566. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  8567. if ([qty_null isEqualToString:@"null"]) {
  8568. // 查available
  8569. qty = [self model_QTY:product_id_string db:db];
  8570. }
  8571. if (percentage) {
  8572. qty = qty * percent / 100;
  8573. }
  8574. // Special Price
  8575. if ([price_null isEqualToString:@"null"]) {
  8576. // price = regular price
  8577. price = [regular_price_str doubleValue];
  8578. }
  8579. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  8580. if (![discount_null isEqualToString:@"null"]) {
  8581. price = price * (1 - discount / 100.0);
  8582. }
  8583. NSMutableDictionary *item = @{
  8584. @"line_note": line_note,
  8585. @"product_id": product_id_string,
  8586. @"available_qty": @(qty),
  8587. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  8588. @"regular_price" : regular_price_str,
  8589. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  8590. }.mutableCopy;
  8591. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  8592. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  8593. }];
  8594. [iSalesDB close_db:db];
  8595. if (product_ids_string.length > 0) {
  8596. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  8597. }
  8598. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  8599. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  8600. return [RAUtils dict2data:resultDictionary];
  8601. }
  8602. NSString *model_info = [RAUtils dict2string:dic];
  8603. // 创建PDF
  8604. // 在preview情况下保存,则不需要新建了
  8605. if (direct) {
  8606. NSMutableDictionary *tear_sheet_params = params;
  8607. // if (tear_sheet_id) {
  8608. // tear_sheet_params = values;
  8609. // }
  8610. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  8611. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  8612. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  8613. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  8614. resultDictionary = pdfInfo.mutableCopy;
  8615. } else { // 创建PDF失败
  8616. return pdfData;
  8617. }
  8618. } else {
  8619. // pdf_path 就是本地路径
  8620. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  8621. }
  8622. // 将文件移动到PDF Cache文件夹
  8623. NSString *newPath = [pdf_path lastPathComponent];
  8624. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8625. NSString *cachefolder = [paths objectAtIndex:0];
  8626. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  8627. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  8628. NSFileManager *fileManager = [NSFileManager defaultManager];
  8629. NSError *error = nil;
  8630. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  8631. if (error) { // 移动文件失败
  8632. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  8633. return [RAUtils dict2data:resultDictionary];
  8634. }
  8635. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  8636. pdf_path = [newPath lastPathComponent];
  8637. // 保存信息
  8638. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  8639. NSString *off_params = [RAUtils dict2string:params];
  8640. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  8641. pdf_path = [self translateSingleQuote:pdf_path];
  8642. create_user = [self translateSingleQuote:create_user];
  8643. tear_note = [self translateSingleQuote:tear_note];
  8644. tear_name = [self translateSingleQuote:tear_name];
  8645. model_info = [self translateSingleQuote:model_info];
  8646. configureParams = [self translateSingleQuote:configureParams];
  8647. off_params = [self translateSingleQuote:off_params];
  8648. NSString *save_pdf_sql = [NSString stringWithFormat:@"insert into offline_pdf (pdf_path,create_user,tear_note,tear_name,model_info,configureParams,is_local,off_params) values ('%@','%@','%@','%@','%@','%@',1,'%@');",pdf_path,create_user,tear_note,tear_name,model_info,configureParams,off_params];
  8649. if (tear_sheet_id) {
  8650. int _id = [tear_sheet_id integerValue];
  8651. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  8652. }
  8653. int result = [iSalesDB execSql:save_pdf_sql];
  8654. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8655. //
  8656. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  8657. if (remove_Item) {
  8658. // portfolioId
  8659. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  8660. }
  8661. return [RAUtils dict2data:resultDictionary];
  8662. }
  8663. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  8664. return [self offline_savePDF:params direct:YES];
  8665. }
  8666. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  8667. return [self offline_savePDF:params direct:NO];
  8668. }
  8669. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  8670. int offset = [[params valueForKey:@"offset"] integerValue];
  8671. int limit = [[params valueForKey:@"limit"] integerValue];
  8672. NSString *keyword = [params valueForKey:@"keyWord"];
  8673. NSString *where = @"where is_delete is null or is_delete = 0";
  8674. if (keyword.length) {
  8675. keyword = [self translateSingleQuote:keyword];
  8676. where = [NSString stringWithFormat:@"where (is_delete is null or is_delete = 0 ) and lower(create_user) like '%%%@%%' or lower(tear_note) like '%%%@%%' or lower(tear_name) like '%%%@%%'",keyword.lowercaseString,keyword.lowercaseString,keyword.lowercaseString];
  8677. }
  8678. NSString *sql = [NSString stringWithFormat:@"select tear_name,tear_note,createtime,create_user,pdf_path,_id,model_info,off_params from offline_pdf %@ order by createtime desc limit %d offset %d;",where,limit,offset];// tearsheets_id ---> _id
  8679. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8680. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8681. NSString *cachefolder = [paths objectAtIndex:0];
  8682. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  8683. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8684. NSString *name = [self textAtColumn:0 statement:stmt];
  8685. NSString *note = [self textAtColumn:1 statement:stmt];
  8686. NSString *time = [self textAtColumn:2 statement:stmt];
  8687. NSString *user = [self textAtColumn:3 statement:stmt];
  8688. NSString *path = [self textAtColumn:4 statement:stmt];
  8689. NSString *model_info = [self textAtColumn:6 statement:stmt];
  8690. NSString *off_params = [self textAtColumn:7 statement:stmt];
  8691. path = [pdfFolder stringByAppendingPathComponent:path];
  8692. BOOL bdir=NO;
  8693. NSFileManager* fileManager = [NSFileManager defaultManager];
  8694. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  8695. {
  8696. //pdf文件不存在
  8697. path=nil;
  8698. }
  8699. time = [self changeDateTimeFormate:time];
  8700. time = [time stringByAppendingString:@" PST"];
  8701. int sheet_id = sqlite3_column_int(stmt, 5);
  8702. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  8703. item[@"tearsheetsId"]=@(sheet_id);
  8704. item[@"pdf_path"]=path;
  8705. item[@"create_time"]=time;
  8706. item[@"create_user"]=user;
  8707. item[@"tear_note"]=note;
  8708. item[@"tear_name"]=name;
  8709. item[@"isLocalFile"]=[NSNumber numberWithBool:YES];
  8710. item[@"model_info"]=model_info;
  8711. item[@"off_params"]=off_params;
  8712. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  8713. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  8714. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  8715. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8716. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  8717. }];
  8718. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8719. return [RAUtils dict2data:dic];
  8720. }
  8721. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  8722. NSString *prodct_ids = [params objectForKey:@"product_id"];
  8723. // NSString *user = [params objectForKey:@"user"];
  8724. // NSString *sql = [NSString stringWithFormat:@"insert into offline_portfolio (product_id,name,description,item_id) select * from (select p.product_id,p.name,p.description,p.item_id from product as p where p.product_id in (%@) and (select count(0) from offline_portfolio where product_id in (%@) and (is_delete is null or is_delete = 0)) = 0);",prodct_ids,prodct_ids];
  8725. NSString *sql = [NSString stringWithFormat:@"insert into offline_portfolio (product_id,name,description,item_id) select * from (select p.product_id,p.name,p.description,p.item_id from product as p where p.product_id in (%@) and p.product_id not in (select product_id from offline_portfolio));insert into offline_portfolio (product_id,name,description,item_id,is_delete) select * from (select p.product_id,p.name,p.description,p.item_id,0 from product as p left join offline_portfolio as f on p.product_id = f.product_id where p.product_id in (%@) and p.product_id in (select product_id from offline_portfolio) and f.is_delete = 1);delete from offline_portfolio where (select count(0) from offline_portfolio where product_id in (%@)) > 1 and is_delete = 1;",prodct_ids,prodct_ids,prodct_ids];
  8726. int result = [iSalesDB execSql:sql];
  8727. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8728. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  8729. if (result == RESULT_TRUE) {
  8730. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  8731. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  8732. sqlite3 *db = [iSalesDB get_db];
  8733. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  8734. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  8735. [iSalesDB close_db:db];
  8736. }
  8737. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8738. return [RAUtils dict2data:dic];
  8739. }
  8740. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  8741. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  8742. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  8743. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  8744. // Regurlar Price
  8745. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  8746. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  8747. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  8748. __block long val_count = 0;
  8749. NSString *sql = @"select name,type,order_by from price order by order_by";
  8750. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8751. NSString *name = [self textAtColumn:0 statement:stmt];
  8752. int type = sqlite3_column_int(stmt, 1);
  8753. int order_by = sqlite3_column_int(stmt, 2);
  8754. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8755. NSDictionary *price_dic = @{
  8756. @"value" : name,
  8757. @"value_id" : [NSNumber numberWithInteger:type],
  8758. @"check" : order_by == 0 ? @(1) : @(0)
  8759. };
  8760. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  8761. val_count = ++(*count);
  8762. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  8763. val_count = 0;
  8764. }];
  8765. [cadedate setObject:@{@"value" : @"None",
  8766. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  8767. val_count++;
  8768. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  8769. [price setObject:cadedate forKey:@"cadedate"];
  8770. [section1 setObject:price forKey:@"item_2"];
  8771. [dic setObject:section1 forKey:@"section_1"];
  8772. return [RAUtils dict2data:dic];
  8773. }
  8774. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  8775. NSString *product_id = [params objectForKey:@"fashionId"];
  8776. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  8777. __block int result = RESULT_TRUE;
  8778. __block int qty = 0;
  8779. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8780. qty = sqlite3_column_int(stmt, 0);
  8781. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  8782. result = RESULT_FALSE;
  8783. }];
  8784. NSMutableDictionary *dic = @{
  8785. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  8786. @"mode" : @"Regular Mode",
  8787. @"quantity_available" : @(qty),
  8788. @"result" : @(result),
  8789. }.mutableCopy;
  8790. return [RAUtils dict2data:dic];
  8791. }
  8792. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  8793. NSString *item_ids = [params objectForKey:@"item_id"];
  8794. NSString *line_notes = [params objectForKey:@"notes"];
  8795. NSString *price_str = [params objectForKey:@"price"];
  8796. NSString *discount_str = [params objectForKey:@"discount"];
  8797. NSString *percent = [params objectForKey:@"available_percent"];
  8798. NSString *qty = [params objectForKey:@"available_qty"];
  8799. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  8800. int dot = 0;
  8801. if (line_notes) {
  8802. line_notes = [self translateSingleQuote:line_notes];
  8803. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  8804. sql = [sql stringByAppendingString:line_notes];
  8805. dot = 1;
  8806. } else {
  8807. line_notes = @"";
  8808. }
  8809. if (price_str) {
  8810. if (dot) {
  8811. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  8812. } else {
  8813. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  8814. dot = 1;
  8815. }
  8816. sql = [sql stringByAppendingString:price_str];
  8817. } else {
  8818. price_str = @"";
  8819. }
  8820. if (discount_str) {
  8821. if (dot) {
  8822. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  8823. } else {
  8824. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  8825. dot = 1;
  8826. }
  8827. sql = [sql stringByAppendingString:discount_str];
  8828. } else {
  8829. discount_str = @"";
  8830. }
  8831. if (percent) {
  8832. if (dot) {
  8833. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  8834. } else {
  8835. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  8836. dot = 1;
  8837. }
  8838. sql = [sql stringByAppendingString:percent];
  8839. } else {
  8840. percent = @"";
  8841. }
  8842. if (qty) {
  8843. if (dot) {
  8844. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  8845. } else {
  8846. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  8847. dot = 1;
  8848. }
  8849. sql = [sql stringByAppendingString:qty];
  8850. } else {
  8851. qty = @"";
  8852. }
  8853. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  8854. sql = [sql stringByAppendingString:where];
  8855. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8856. int result = [iSalesDB execSql:sql];
  8857. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8858. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8859. return [RAUtils dict2data:dic];
  8860. }
  8861. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  8862. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  8863. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8864. sqlite3 *db = [iSalesDB get_db];
  8865. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set is_delete = 1 where item_id in (%@) and (is_delete is null or is_delete = 0);",portfolio_ids];
  8866. int result = [iSalesDB execSql:sql db:db];
  8867. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8868. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8869. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  8870. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  8871. [iSalesDB close_db:db];
  8872. return [RAUtils dict2data:dic];
  8873. }
  8874. + (void)offline_removePDFWithName:(NSString *)name {
  8875. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8876. NSString *cachefolder = [paths objectAtIndex:0];
  8877. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  8878. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  8879. NSFileManager *fileManager = [NSFileManager defaultManager];
  8880. [fileManager removeItemAtPath:path error:nil];
  8881. }
  8882. + (void)offline_clear_PDFCache {
  8883. NSFileManager *fileManager = [NSFileManager defaultManager];
  8884. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8885. NSString *cachefolder = [paths objectAtIndex:0];
  8886. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  8887. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  8888. for (NSString *path in pdf_files) {
  8889. [self offline_removePDFWithName:[path lastPathComponent]];
  8890. }
  8891. }
  8892. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  8893. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8894. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  8895. NSString *user = [params objectForKey:@"user"];
  8896. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %d;",tearsheetsId]]; // tearsheets_id ---> _id
  8897. if (![create_user isEqualToString:user]) {
  8898. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  8899. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8900. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  8901. return [RAUtils dict2data:dic];
  8902. }
  8903. NSString *is_local_sql = [NSString stringWithFormat:@"select is_local,pdf_path from offline_pdf where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  8904. __block int is_local = 0;
  8905. __block NSString *path = @"";
  8906. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8907. is_local = sqlite3_column_int(stmt, 0);
  8908. path = [self textAtColumn:1 statement:stmt];
  8909. }];
  8910. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  8911. if (is_local == 1) {
  8912. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  8913. // 删除文件
  8914. [self offline_removePDFWithName:path];
  8915. }
  8916. int result = [iSalesDB execSql:sql];
  8917. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8918. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8919. return [RAUtils dict2data:dic];
  8920. }
  8921. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial
  8922. {
  8923. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  8924. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  8925. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  8926. NSString* where=@"1=1";
  8927. if (ver!=nil) {
  8928. where=@"is_dirty=1";
  8929. }
  8930. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,name,description,item_id,fashion_id,ifnull(available_qty,'null'),ifnull(percentage,'null'),ifnull(percent,'null'),ifnull(sheet_price,'null'),sheet_discount,img,line_note,is_delete,createtime,modify_time from offline_portfolio where %@",where];
  8931. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  8932. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8933. sqlite3 *db = [iSalesDB get_db];
  8934. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8935. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  8936. NSInteger _id = sqlite3_column_int(stmt, 0);
  8937. NSInteger product_id = sqlite3_column_int(stmt, 1);
  8938. NSString *name = [self textAtColumn:2 statement:stmt];
  8939. NSString *desc = [self textAtColumn:3 statement:stmt];
  8940. NSInteger item_id = sqlite3_column_int(stmt, 4);
  8941. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  8942. NSInteger qty = sqlite3_column_int(stmt, 6);
  8943. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  8944. double percent = sqlite3_column_double(stmt, 8);
  8945. double price = sqlite3_column_double(stmt, 9);
  8946. double discount = sqlite3_column_double(stmt, 10);
  8947. NSString *img = [self textAtColumn:11 statement:stmt];
  8948. NSString *line_note = [self textAtColumn:12 statement:stmt];
  8949. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  8950. NSString *create_time = [self textAtColumn:14 statement:stmt];
  8951. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  8952. NSString *price_null = [self textAtColumn:9 statement:stmt];
  8953. if ([price_null isEqualToString:@"null"]) {
  8954. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  8955. }
  8956. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  8957. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  8958. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  8959. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  8960. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  8961. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  8962. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  8963. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  8964. if ([qty_null isEqualToString:@"null"]) {
  8965. // [item setValue:@"null" forKey:@"available_qty"];
  8966. } else {
  8967. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  8968. }
  8969. if ([is_percent_null isEqualToString:@"null"]) {
  8970. // [item setValue:@"null" forKey:@"percentage"];
  8971. } else {
  8972. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  8973. }
  8974. if ([percent_null isEqualToString:@"null"]) {
  8975. // [item setValue:@"null" forKey:@"percent"];
  8976. } else {
  8977. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  8978. }
  8979. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  8980. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  8981. [item setValue:name forKey:@"name"];
  8982. [item setValue:desc forKey:@"description"];
  8983. [item setValue:img forKey:@"img"];
  8984. [item setValue:line_note forKey:@"line_note"];
  8985. [item setValue:create_time forKey:@"createtime"];
  8986. [item setValue:modify_time forKey:@"modifytime"];
  8987. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  8988. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8989. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  8990. }];
  8991. [iSalesDB close_db:db];
  8992. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  8993. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8994. }
  8995. return ret;
  8996. }
  8997. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  8998. {
  8999. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  9000. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  9001. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  9002. NSString* where=@"1=1";
  9003. if (ver!=nil) {
  9004. where=@"is_dirty=1";
  9005. }
  9006. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,ifnull(tearsheets_id,'null'),pdf_path,create_user,tear_note,tear_name,model_info,createtime,modify_time,configureParams,off_params,is_delete,is_local from offline_pdf where %@",where];
  9007. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  9008. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9009. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  9010. NSInteger _id = sqlite3_column_int(stmt, 0);
  9011. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  9012. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  9013. NSString *create_user = [self textAtColumn:3 statement:stmt];
  9014. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  9015. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  9016. NSString *model_info = [self textAtColumn:6 statement:stmt];
  9017. NSString *createtime = [self textAtColumn:7 statement:stmt];
  9018. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  9019. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  9020. NSString *off_params = [self textAtColumn:10 statement:stmt];
  9021. NSString *uuid = [NSUUID UUID].UUIDString;
  9022. int is_delete = sqlite3_column_int(stmt, 11);
  9023. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  9024. int is_local = sqlite3_column_int(stmt, 12);
  9025. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  9026. if (![tearsheet_id_null isEqualToString:@"null"]) {
  9027. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  9028. }
  9029. [item setObject:pdf_path forKey:@"pdf_path"];
  9030. [item setObject:create_user forKey:@"create_user"];
  9031. [item setObject:tear_note forKey:@"tear_note"];
  9032. [item setObject:tear_name forKey:@"tear_name"];
  9033. [item setObject:model_info forKey:@"model_info"];
  9034. [item setObject:createtime forKey:@"createtime"];
  9035. [item setObject:modifytime forKey:@"modifytime"];
  9036. [item setObject:urlParams forKey:@"urlParams"];
  9037. [item setObject:off_params forKey:@"off_params"];
  9038. [item setObject:uuid forKey:@"pdf_token"];
  9039. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  9040. NSDictionary *off_Params_dic = [RAUtils string2dict:off_params];
  9041. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  9042. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  9043. } else {
  9044. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  9045. }
  9046. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9047. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  9048. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9049. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9050. }];
  9051. return ret;
  9052. }
  9053. @end